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> 31db2a4165SFrank Mandarino #include <sound/core.h> 32db2a4165SFrank Mandarino #include <sound/pcm.h> 33db2a4165SFrank Mandarino #include <sound/pcm_params.h> 34db2a4165SFrank Mandarino #include <sound/soc.h> 35db2a4165SFrank Mandarino #include <sound/soc-dapm.h> 36db2a4165SFrank Mandarino #include <sound/initval.h> 37db2a4165SFrank Mandarino 38db2a4165SFrank Mandarino static DEFINE_MUTEX(pcm_mutex); 39db2a4165SFrank Mandarino static DEFINE_MUTEX(io_mutex); 40db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq); 41db2a4165SFrank Mandarino 42384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 43384c89e2SMark Brown static struct dentry *debugfs_root; 44384c89e2SMark Brown #endif 45384c89e2SMark Brown 46c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex); 47c5af3a2eSMark Brown static LIST_HEAD(card_list); 489115171aSMark Brown static LIST_HEAD(dai_list); 4912a48a8cSMark Brown static LIST_HEAD(platform_list); 500d0cf00aSMark Brown static LIST_HEAD(codec_list); 51c5af3a2eSMark Brown 52c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card); 53c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card); 54c5af3a2eSMark Brown 55db2a4165SFrank Mandarino /* 56db2a4165SFrank Mandarino * This is a timeout to do a DAPM powerdown after a stream is closed(). 57db2a4165SFrank Mandarino * It can be used to eliminate pops between different playback streams, e.g. 58db2a4165SFrank Mandarino * between two audio tracks. 59db2a4165SFrank Mandarino */ 60db2a4165SFrank Mandarino static int pmdown_time = 5000; 61db2a4165SFrank Mandarino module_param(pmdown_time, int, 0); 62db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); 63db2a4165SFrank Mandarino 64965ac42cSLiam Girdwood /* 65965ac42cSLiam Girdwood * This function forces any delayed work to be queued and run. 66965ac42cSLiam Girdwood */ 67965ac42cSLiam Girdwood static int run_delayed_work(struct delayed_work *dwork) 68965ac42cSLiam Girdwood { 69965ac42cSLiam Girdwood int ret; 70965ac42cSLiam Girdwood 71965ac42cSLiam Girdwood /* cancel any work waiting to be queued. */ 72965ac42cSLiam Girdwood ret = cancel_delayed_work(dwork); 73965ac42cSLiam Girdwood 74965ac42cSLiam Girdwood /* if there was any work waiting then we run it now and 75965ac42cSLiam Girdwood * wait for it's completion */ 76965ac42cSLiam Girdwood if (ret) { 77965ac42cSLiam Girdwood schedule_delayed_work(dwork, 0); 78965ac42cSLiam Girdwood flush_scheduled_work(); 79965ac42cSLiam Girdwood } 80965ac42cSLiam Girdwood return ret; 81965ac42cSLiam Girdwood } 82965ac42cSLiam Girdwood 83db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS 84db2a4165SFrank Mandarino /* unregister ac97 codec */ 85db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec) 86db2a4165SFrank Mandarino { 87db2a4165SFrank Mandarino if (codec->ac97->dev.bus) 88db2a4165SFrank Mandarino device_unregister(&codec->ac97->dev); 89db2a4165SFrank Mandarino return 0; 90db2a4165SFrank Mandarino } 91db2a4165SFrank Mandarino 92db2a4165SFrank Mandarino /* stop no dev release warning */ 93db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){} 94db2a4165SFrank Mandarino 95db2a4165SFrank Mandarino /* register ac97 codec to bus */ 96db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec) 97db2a4165SFrank Mandarino { 98db2a4165SFrank Mandarino int err; 99db2a4165SFrank Mandarino 100db2a4165SFrank Mandarino codec->ac97->dev.bus = &ac97_bus_type; 1014ac5c61fSMark Brown codec->ac97->dev.parent = codec->card->dev; 102db2a4165SFrank Mandarino codec->ac97->dev.release = soc_ac97_device_release; 103db2a4165SFrank Mandarino 104bb072bf0SKay Sievers dev_set_name(&codec->ac97->dev, "%d-%d:%s", 105db2a4165SFrank Mandarino codec->card->number, 0, codec->name); 106db2a4165SFrank Mandarino err = device_register(&codec->ac97->dev); 107db2a4165SFrank Mandarino if (err < 0) { 108db2a4165SFrank Mandarino snd_printk(KERN_ERR "Can't register ac97 bus\n"); 109db2a4165SFrank Mandarino codec->ac97->dev.bus = NULL; 110db2a4165SFrank Mandarino return err; 111db2a4165SFrank Mandarino } 112db2a4165SFrank Mandarino return 0; 113db2a4165SFrank Mandarino } 114db2a4165SFrank Mandarino #endif 115db2a4165SFrank Mandarino 11606f409d7SMark Brown static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream) 11706f409d7SMark Brown { 11806f409d7SMark Brown struct snd_soc_pcm_runtime *rtd = substream->private_data; 11906f409d7SMark Brown struct snd_soc_device *socdev = rtd->socdev; 12006f409d7SMark Brown struct snd_soc_card *card = socdev->card; 12106f409d7SMark Brown struct snd_soc_dai_link *machine = rtd->dai; 12206f409d7SMark Brown struct snd_soc_dai *cpu_dai = machine->cpu_dai; 12306f409d7SMark Brown struct snd_soc_dai *codec_dai = machine->codec_dai; 12406f409d7SMark Brown int ret; 12506f409d7SMark Brown 12606f409d7SMark Brown if (codec_dai->symmetric_rates || cpu_dai->symmetric_rates || 12706f409d7SMark Brown machine->symmetric_rates) { 12806f409d7SMark Brown dev_dbg(card->dev, "Symmetry forces %dHz rate\n", 12906f409d7SMark Brown machine->rate); 13006f409d7SMark Brown 13106f409d7SMark Brown ret = snd_pcm_hw_constraint_minmax(substream->runtime, 13206f409d7SMark Brown SNDRV_PCM_HW_PARAM_RATE, 13306f409d7SMark Brown machine->rate, 13406f409d7SMark Brown machine->rate); 13506f409d7SMark Brown if (ret < 0) { 13606f409d7SMark Brown dev_err(card->dev, 13706f409d7SMark Brown "Unable to apply rate symmetry constraint: %d\n", ret); 13806f409d7SMark Brown return ret; 13906f409d7SMark Brown } 14006f409d7SMark Brown } 14106f409d7SMark Brown 14206f409d7SMark Brown return 0; 14306f409d7SMark Brown } 14406f409d7SMark Brown 145db2a4165SFrank Mandarino /* 146db2a4165SFrank Mandarino * Called by ALSA when a PCM substream is opened, the runtime->hw record is 147db2a4165SFrank Mandarino * then initialized and any private data can be allocated. This also calls 148db2a4165SFrank Mandarino * startup for the cpu DAI, platform, machine and codec DAI. 149db2a4165SFrank Mandarino */ 150db2a4165SFrank Mandarino static int soc_pcm_open(struct snd_pcm_substream *substream) 151db2a4165SFrank Mandarino { 152db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 153db2a4165SFrank Mandarino struct snd_soc_device *socdev = rtd->socdev; 15487689d56SMark Brown struct snd_soc_card *card = socdev->card; 155db2a4165SFrank Mandarino struct snd_pcm_runtime *runtime = substream->runtime; 156cb666e5bSLiam Girdwood struct snd_soc_dai_link *machine = rtd->dai; 15787689d56SMark Brown struct snd_soc_platform *platform = card->platform; 1583c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = machine->cpu_dai; 1593c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = machine->codec_dai; 160db2a4165SFrank Mandarino int ret = 0; 161db2a4165SFrank Mandarino 162db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 163db2a4165SFrank Mandarino 164db2a4165SFrank Mandarino /* startup the audio subsystem */ 1656335d055SEric Miao if (cpu_dai->ops->startup) { 1666335d055SEric Miao ret = cpu_dai->ops->startup(substream, cpu_dai); 167db2a4165SFrank Mandarino if (ret < 0) { 168db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't open interface %s\n", 169cb666e5bSLiam Girdwood cpu_dai->name); 170db2a4165SFrank Mandarino goto out; 171db2a4165SFrank Mandarino } 172db2a4165SFrank Mandarino } 173db2a4165SFrank Mandarino 174db2a4165SFrank Mandarino if (platform->pcm_ops->open) { 175db2a4165SFrank Mandarino ret = platform->pcm_ops->open(substream); 176db2a4165SFrank Mandarino if (ret < 0) { 177db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't open platform %s\n", platform->name); 178db2a4165SFrank Mandarino goto platform_err; 179db2a4165SFrank Mandarino } 180db2a4165SFrank Mandarino } 181db2a4165SFrank Mandarino 1826335d055SEric Miao if (codec_dai->ops->startup) { 1836335d055SEric Miao ret = codec_dai->ops->startup(substream, codec_dai); 184cb666e5bSLiam Girdwood if (ret < 0) { 185cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: can't open codec %s\n", 186cb666e5bSLiam Girdwood codec_dai->name); 187cb666e5bSLiam Girdwood goto codec_dai_err; 188cb666e5bSLiam Girdwood } 189cb666e5bSLiam Girdwood } 190cb666e5bSLiam Girdwood 191db2a4165SFrank Mandarino if (machine->ops && machine->ops->startup) { 192db2a4165SFrank Mandarino ret = machine->ops->startup(substream); 193db2a4165SFrank Mandarino if (ret < 0) { 194db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s startup failed\n", machine->name); 195db2a4165SFrank Mandarino goto machine_err; 196db2a4165SFrank Mandarino } 197db2a4165SFrank Mandarino } 198db2a4165SFrank Mandarino 199db2a4165SFrank Mandarino /* Check that the codec and cpu DAI's are compatible */ 200db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 201db2a4165SFrank Mandarino runtime->hw.rate_min = 2023ff3f64bSMark Brown max(codec_dai->playback.rate_min, 2033ff3f64bSMark Brown cpu_dai->playback.rate_min); 204db2a4165SFrank Mandarino runtime->hw.rate_max = 2053ff3f64bSMark Brown min(codec_dai->playback.rate_max, 2063ff3f64bSMark Brown cpu_dai->playback.rate_max); 207db2a4165SFrank Mandarino runtime->hw.channels_min = 208cb666e5bSLiam Girdwood max(codec_dai->playback.channels_min, 209cb666e5bSLiam Girdwood cpu_dai->playback.channels_min); 210db2a4165SFrank Mandarino runtime->hw.channels_max = 211cb666e5bSLiam Girdwood min(codec_dai->playback.channels_max, 212cb666e5bSLiam Girdwood cpu_dai->playback.channels_max); 213cb666e5bSLiam Girdwood runtime->hw.formats = 214cb666e5bSLiam Girdwood codec_dai->playback.formats & cpu_dai->playback.formats; 215cb666e5bSLiam Girdwood runtime->hw.rates = 216cb666e5bSLiam Girdwood codec_dai->playback.rates & cpu_dai->playback.rates; 217db2a4165SFrank Mandarino } else { 218db2a4165SFrank Mandarino runtime->hw.rate_min = 2193ff3f64bSMark Brown max(codec_dai->capture.rate_min, 2203ff3f64bSMark Brown cpu_dai->capture.rate_min); 221db2a4165SFrank Mandarino runtime->hw.rate_max = 2223ff3f64bSMark Brown min(codec_dai->capture.rate_max, 2233ff3f64bSMark Brown cpu_dai->capture.rate_max); 224db2a4165SFrank Mandarino runtime->hw.channels_min = 225cb666e5bSLiam Girdwood max(codec_dai->capture.channels_min, 226cb666e5bSLiam Girdwood cpu_dai->capture.channels_min); 227db2a4165SFrank Mandarino runtime->hw.channels_max = 228cb666e5bSLiam Girdwood min(codec_dai->capture.channels_max, 229cb666e5bSLiam Girdwood cpu_dai->capture.channels_max); 230cb666e5bSLiam Girdwood runtime->hw.formats = 231cb666e5bSLiam Girdwood codec_dai->capture.formats & cpu_dai->capture.formats; 232cb666e5bSLiam Girdwood runtime->hw.rates = 233cb666e5bSLiam Girdwood codec_dai->capture.rates & cpu_dai->capture.rates; 234db2a4165SFrank Mandarino } 235db2a4165SFrank Mandarino 236db2a4165SFrank Mandarino snd_pcm_limit_hw_rates(runtime); 237db2a4165SFrank Mandarino if (!runtime->hw.rates) { 238db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching rates\n", 239cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 240cb666e5bSLiam Girdwood goto machine_err; 241db2a4165SFrank Mandarino } 242db2a4165SFrank Mandarino if (!runtime->hw.formats) { 243db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching formats\n", 244cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 245cb666e5bSLiam Girdwood goto machine_err; 246db2a4165SFrank Mandarino } 247db2a4165SFrank Mandarino if (!runtime->hw.channels_min || !runtime->hw.channels_max) { 248db2a4165SFrank Mandarino printk(KERN_ERR "asoc: %s <-> %s No matching channels\n", 249cb666e5bSLiam Girdwood codec_dai->name, cpu_dai->name); 250cb666e5bSLiam Girdwood goto machine_err; 251db2a4165SFrank Mandarino } 252db2a4165SFrank Mandarino 25306f409d7SMark Brown /* Symmetry only applies if we've already got an active stream. */ 25406f409d7SMark Brown if (cpu_dai->active || codec_dai->active) { 25506f409d7SMark Brown ret = soc_pcm_apply_symmetry(substream); 25606f409d7SMark Brown if (ret != 0) 25706f409d7SMark Brown goto machine_err; 25806f409d7SMark Brown } 25906f409d7SMark Brown 260f24368c2SMark Brown pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name); 261f24368c2SMark Brown pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates); 262f24368c2SMark Brown pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min, 263b5c5fd24SLiam Girdwood runtime->hw.channels_max); 264f24368c2SMark Brown pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min, 265b5c5fd24SLiam Girdwood runtime->hw.rate_max); 266db2a4165SFrank Mandarino 267db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 268cb666e5bSLiam Girdwood cpu_dai->playback.active = codec_dai->playback.active = 1; 269db2a4165SFrank Mandarino else 270cb666e5bSLiam Girdwood cpu_dai->capture.active = codec_dai->capture.active = 1; 271cb666e5bSLiam Girdwood cpu_dai->active = codec_dai->active = 1; 272cb666e5bSLiam Girdwood cpu_dai->runtime = runtime; 2736627a653SMark Brown card->codec->active++; 274db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 275db2a4165SFrank Mandarino return 0; 276db2a4165SFrank Mandarino 277cb666e5bSLiam Girdwood machine_err: 278db2a4165SFrank Mandarino if (machine->ops && machine->ops->shutdown) 279db2a4165SFrank Mandarino machine->ops->shutdown(substream); 280db2a4165SFrank Mandarino 281cb666e5bSLiam Girdwood codec_dai_err: 282db2a4165SFrank Mandarino if (platform->pcm_ops->close) 283db2a4165SFrank Mandarino platform->pcm_ops->close(substream); 284db2a4165SFrank Mandarino 285db2a4165SFrank Mandarino platform_err: 2866335d055SEric Miao if (cpu_dai->ops->shutdown) 2876335d055SEric Miao cpu_dai->ops->shutdown(substream, cpu_dai); 288db2a4165SFrank Mandarino out: 289db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 290db2a4165SFrank Mandarino return ret; 291db2a4165SFrank Mandarino } 292db2a4165SFrank Mandarino 293db2a4165SFrank Mandarino /* 2943a4fa0a2SRobert P. J. Day * Power down the audio subsystem pmdown_time msecs after close is called. 295db2a4165SFrank Mandarino * This is to ensure there are no pops or clicks in between any music tracks 296db2a4165SFrank Mandarino * due to DAPM power cycling. 297db2a4165SFrank Mandarino */ 2984484bb2eSAndrew Morton static void close_delayed_work(struct work_struct *work) 299db2a4165SFrank Mandarino { 3006308419aSMark Brown struct snd_soc_card *card = container_of(work, struct snd_soc_card, 3016308419aSMark Brown delayed_work.work); 3026627a653SMark Brown struct snd_soc_codec *codec = card->codec; 3033c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai; 304db2a4165SFrank Mandarino int i; 305db2a4165SFrank Mandarino 306db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 307db2a4165SFrank Mandarino for (i = 0; i < codec->num_dai; i++) { 308db2a4165SFrank Mandarino codec_dai = &codec->dai[i]; 309db2a4165SFrank Mandarino 310f24368c2SMark Brown pr_debug("pop wq checking: %s status: %s waiting: %s\n", 311db2a4165SFrank Mandarino codec_dai->playback.stream_name, 312db2a4165SFrank Mandarino codec_dai->playback.active ? "active" : "inactive", 313db2a4165SFrank Mandarino codec_dai->pop_wait ? "yes" : "no"); 314db2a4165SFrank Mandarino 315db2a4165SFrank Mandarino /* are we waiting on this codec DAI stream */ 316db2a4165SFrank Mandarino if (codec_dai->pop_wait == 1) { 317db2a4165SFrank Mandarino codec_dai->pop_wait = 0; 3180b4d221bSLiam Girdwood snd_soc_dapm_stream_event(codec, 3190b4d221bSLiam Girdwood codec_dai->playback.stream_name, 320db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_STOP); 321db2a4165SFrank Mandarino } 322db2a4165SFrank Mandarino } 323db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 324db2a4165SFrank Mandarino } 325db2a4165SFrank Mandarino 326db2a4165SFrank Mandarino /* 327db2a4165SFrank Mandarino * Called by ALSA when a PCM substream is closed. Private data can be 328db2a4165SFrank Mandarino * freed here. The cpu DAI, codec DAI, machine and platform are also 329db2a4165SFrank Mandarino * shutdown. 330db2a4165SFrank Mandarino */ 331db2a4165SFrank Mandarino static int soc_codec_close(struct snd_pcm_substream *substream) 332db2a4165SFrank Mandarino { 333db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 334db2a4165SFrank Mandarino struct snd_soc_device *socdev = rtd->socdev; 3356308419aSMark Brown struct snd_soc_card *card = socdev->card; 336cb666e5bSLiam Girdwood struct snd_soc_dai_link *machine = rtd->dai; 33787689d56SMark Brown struct snd_soc_platform *platform = card->platform; 3383c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = machine->cpu_dai; 3393c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = machine->codec_dai; 3406627a653SMark Brown struct snd_soc_codec *codec = card->codec; 341db2a4165SFrank Mandarino 342db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 343db2a4165SFrank Mandarino 344db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 345cb666e5bSLiam Girdwood cpu_dai->playback.active = codec_dai->playback.active = 0; 346db2a4165SFrank Mandarino else 347cb666e5bSLiam Girdwood cpu_dai->capture.active = codec_dai->capture.active = 0; 348db2a4165SFrank Mandarino 349cb666e5bSLiam Girdwood if (codec_dai->playback.active == 0 && 350cb666e5bSLiam Girdwood codec_dai->capture.active == 0) { 351cb666e5bSLiam Girdwood cpu_dai->active = codec_dai->active = 0; 352db2a4165SFrank Mandarino } 353db2a4165SFrank Mandarino codec->active--; 354db2a4165SFrank Mandarino 3556010b2daSMark Brown /* Muting the DAC suppresses artifacts caused during digital 3566010b2daSMark Brown * shutdown, for example from stopping clocks. 3576010b2daSMark Brown */ 3586010b2daSMark Brown if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 3596010b2daSMark Brown snd_soc_dai_digital_mute(codec_dai, 1); 3606010b2daSMark Brown 3616335d055SEric Miao if (cpu_dai->ops->shutdown) 3626335d055SEric Miao cpu_dai->ops->shutdown(substream, cpu_dai); 363db2a4165SFrank Mandarino 3646335d055SEric Miao if (codec_dai->ops->shutdown) 3656335d055SEric Miao codec_dai->ops->shutdown(substream, codec_dai); 366db2a4165SFrank Mandarino 367db2a4165SFrank Mandarino if (machine->ops && machine->ops->shutdown) 368db2a4165SFrank Mandarino machine->ops->shutdown(substream); 369db2a4165SFrank Mandarino 370db2a4165SFrank Mandarino if (platform->pcm_ops->close) 371db2a4165SFrank Mandarino platform->pcm_ops->close(substream); 372cb666e5bSLiam Girdwood cpu_dai->runtime = NULL; 373db2a4165SFrank Mandarino 374db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 375db2a4165SFrank Mandarino /* start delayed pop wq here for playback streams */ 376cb666e5bSLiam Girdwood codec_dai->pop_wait = 1; 3776308419aSMark Brown schedule_delayed_work(&card->delayed_work, 378db2a4165SFrank Mandarino msecs_to_jiffies(pmdown_time)); 379db2a4165SFrank Mandarino } else { 380db2a4165SFrank Mandarino /* capture streams can be powered down now */ 381cb666e5bSLiam Girdwood snd_soc_dapm_stream_event(codec, 3820b4d221bSLiam Girdwood codec_dai->capture.stream_name, 3830b4d221bSLiam Girdwood SND_SOC_DAPM_STREAM_STOP); 384db2a4165SFrank Mandarino } 385db2a4165SFrank Mandarino 386db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 387db2a4165SFrank Mandarino return 0; 388db2a4165SFrank Mandarino } 389db2a4165SFrank Mandarino 390db2a4165SFrank Mandarino /* 391db2a4165SFrank Mandarino * Called by ALSA when the PCM substream is prepared, can set format, sample 392db2a4165SFrank Mandarino * rate, etc. This function is non atomic and can be called multiple times, 393db2a4165SFrank Mandarino * it can refer to the runtime info. 394db2a4165SFrank Mandarino */ 395db2a4165SFrank Mandarino static int soc_pcm_prepare(struct snd_pcm_substream *substream) 396db2a4165SFrank Mandarino { 397db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 398db2a4165SFrank Mandarino struct snd_soc_device *socdev = rtd->socdev; 3996308419aSMark Brown struct snd_soc_card *card = socdev->card; 400cb666e5bSLiam Girdwood struct snd_soc_dai_link *machine = rtd->dai; 40187689d56SMark Brown struct snd_soc_platform *platform = card->platform; 4023c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = machine->cpu_dai; 4033c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = machine->codec_dai; 4046627a653SMark Brown struct snd_soc_codec *codec = card->codec; 405db2a4165SFrank Mandarino int ret = 0; 406db2a4165SFrank Mandarino 407db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 408cb666e5bSLiam Girdwood 409cb666e5bSLiam Girdwood if (machine->ops && machine->ops->prepare) { 410cb666e5bSLiam Girdwood ret = machine->ops->prepare(substream); 411cb666e5bSLiam Girdwood if (ret < 0) { 412cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: machine prepare error\n"); 413cb666e5bSLiam Girdwood goto out; 414cb666e5bSLiam Girdwood } 415cb666e5bSLiam Girdwood } 416cb666e5bSLiam Girdwood 417db2a4165SFrank Mandarino if (platform->pcm_ops->prepare) { 418db2a4165SFrank Mandarino ret = platform->pcm_ops->prepare(substream); 419a71a468aSLiam Girdwood if (ret < 0) { 420a71a468aSLiam Girdwood printk(KERN_ERR "asoc: platform prepare error\n"); 421db2a4165SFrank Mandarino goto out; 422db2a4165SFrank Mandarino } 423a71a468aSLiam Girdwood } 424db2a4165SFrank Mandarino 4256335d055SEric Miao if (codec_dai->ops->prepare) { 4266335d055SEric Miao ret = codec_dai->ops->prepare(substream, codec_dai); 427a71a468aSLiam Girdwood if (ret < 0) { 428a71a468aSLiam Girdwood printk(KERN_ERR "asoc: codec DAI prepare error\n"); 429db2a4165SFrank Mandarino goto out; 430db2a4165SFrank Mandarino } 431a71a468aSLiam Girdwood } 432db2a4165SFrank Mandarino 4336335d055SEric Miao if (cpu_dai->ops->prepare) { 4346335d055SEric Miao ret = cpu_dai->ops->prepare(substream, cpu_dai); 435cb666e5bSLiam Girdwood if (ret < 0) { 436cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: cpu DAI prepare error\n"); 437cb666e5bSLiam Girdwood goto out; 438cb666e5bSLiam Girdwood } 439cb666e5bSLiam Girdwood } 440db2a4165SFrank Mandarino 441d45f6219SMark Brown /* cancel any delayed stream shutdown that is pending */ 442d45f6219SMark Brown if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 443d45f6219SMark Brown codec_dai->pop_wait) { 444cb666e5bSLiam Girdwood codec_dai->pop_wait = 0; 4456308419aSMark Brown cancel_delayed_work(&card->delayed_work); 446db2a4165SFrank Mandarino } 447db2a4165SFrank Mandarino 448db2a4165SFrank Mandarino if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 449db2a4165SFrank Mandarino snd_soc_dapm_stream_event(codec, 450cb666e5bSLiam Girdwood codec_dai->playback.stream_name, 451db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_START); 452db2a4165SFrank Mandarino else 453db2a4165SFrank Mandarino snd_soc_dapm_stream_event(codec, 454cb666e5bSLiam Girdwood codec_dai->capture.stream_name, 455db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_START); 4568c6529dbSLiam Girdwood 4578c6529dbSLiam Girdwood snd_soc_dai_digital_mute(codec_dai, 0); 458db2a4165SFrank Mandarino 459db2a4165SFrank Mandarino out: 460db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 461db2a4165SFrank Mandarino return ret; 462db2a4165SFrank Mandarino } 463db2a4165SFrank Mandarino 464db2a4165SFrank Mandarino /* 465db2a4165SFrank Mandarino * Called by ALSA when the hardware params are set by application. This 466db2a4165SFrank Mandarino * function can also be called multiple times and can allocate buffers 467db2a4165SFrank Mandarino * (using snd_pcm_lib_* ). It's non-atomic. 468db2a4165SFrank Mandarino */ 469db2a4165SFrank Mandarino static int soc_pcm_hw_params(struct snd_pcm_substream *substream, 470db2a4165SFrank Mandarino struct snd_pcm_hw_params *params) 471db2a4165SFrank Mandarino { 472db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 473db2a4165SFrank Mandarino struct snd_soc_device *socdev = rtd->socdev; 474cb666e5bSLiam Girdwood struct snd_soc_dai_link *machine = rtd->dai; 47587689d56SMark Brown struct snd_soc_card *card = socdev->card; 47687689d56SMark Brown struct snd_soc_platform *platform = card->platform; 4773c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = machine->cpu_dai; 4783c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = machine->codec_dai; 479db2a4165SFrank Mandarino int ret = 0; 480db2a4165SFrank Mandarino 481db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 482db2a4165SFrank Mandarino 483cb666e5bSLiam Girdwood if (machine->ops && machine->ops->hw_params) { 484cb666e5bSLiam Girdwood ret = machine->ops->hw_params(substream, params); 485cb666e5bSLiam Girdwood if (ret < 0) { 486cb666e5bSLiam Girdwood printk(KERN_ERR "asoc: machine hw_params failed\n"); 487db2a4165SFrank Mandarino goto out; 488db2a4165SFrank Mandarino } 489cb666e5bSLiam Girdwood } 490db2a4165SFrank Mandarino 4916335d055SEric Miao if (codec_dai->ops->hw_params) { 4926335d055SEric Miao ret = codec_dai->ops->hw_params(substream, params, codec_dai); 493db2a4165SFrank Mandarino if (ret < 0) { 494db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't set codec %s hw params\n", 495cb666e5bSLiam Girdwood codec_dai->name); 496cb666e5bSLiam Girdwood goto codec_err; 497db2a4165SFrank Mandarino } 498db2a4165SFrank Mandarino } 499db2a4165SFrank Mandarino 5006335d055SEric Miao if (cpu_dai->ops->hw_params) { 5016335d055SEric Miao ret = cpu_dai->ops->hw_params(substream, params, cpu_dai); 502db2a4165SFrank Mandarino if (ret < 0) { 5033ff3f64bSMark Brown printk(KERN_ERR "asoc: interface %s hw params failed\n", 504cb666e5bSLiam Girdwood cpu_dai->name); 505db2a4165SFrank Mandarino goto interface_err; 506db2a4165SFrank Mandarino } 507db2a4165SFrank Mandarino } 508db2a4165SFrank Mandarino 509db2a4165SFrank Mandarino if (platform->pcm_ops->hw_params) { 510db2a4165SFrank Mandarino ret = platform->pcm_ops->hw_params(substream, params); 511db2a4165SFrank Mandarino if (ret < 0) { 5123ff3f64bSMark Brown printk(KERN_ERR "asoc: platform %s hw params failed\n", 513db2a4165SFrank Mandarino platform->name); 514db2a4165SFrank Mandarino goto platform_err; 515db2a4165SFrank Mandarino } 516db2a4165SFrank Mandarino } 517db2a4165SFrank Mandarino 51806f409d7SMark Brown machine->rate = params_rate(params); 51906f409d7SMark Brown 520db2a4165SFrank Mandarino out: 521db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 522db2a4165SFrank Mandarino return ret; 523db2a4165SFrank Mandarino 524db2a4165SFrank Mandarino platform_err: 5256335d055SEric Miao if (cpu_dai->ops->hw_free) 5266335d055SEric Miao cpu_dai->ops->hw_free(substream, cpu_dai); 527db2a4165SFrank Mandarino 528db2a4165SFrank Mandarino interface_err: 5296335d055SEric Miao if (codec_dai->ops->hw_free) 5306335d055SEric Miao codec_dai->ops->hw_free(substream, codec_dai); 531cb666e5bSLiam Girdwood 532cb666e5bSLiam Girdwood codec_err: 533cb666e5bSLiam Girdwood if (machine->ops && machine->ops->hw_free) 534cb666e5bSLiam Girdwood machine->ops->hw_free(substream); 535db2a4165SFrank Mandarino 536db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 537db2a4165SFrank Mandarino return ret; 538db2a4165SFrank Mandarino } 539db2a4165SFrank Mandarino 540db2a4165SFrank Mandarino /* 541db2a4165SFrank Mandarino * Free's resources allocated by hw_params, can be called multiple times 542db2a4165SFrank Mandarino */ 543db2a4165SFrank Mandarino static int soc_pcm_hw_free(struct snd_pcm_substream *substream) 544db2a4165SFrank Mandarino { 545db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 546db2a4165SFrank Mandarino struct snd_soc_device *socdev = rtd->socdev; 547cb666e5bSLiam Girdwood struct snd_soc_dai_link *machine = rtd->dai; 54887689d56SMark Brown struct snd_soc_card *card = socdev->card; 54987689d56SMark Brown struct snd_soc_platform *platform = card->platform; 5503c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = machine->cpu_dai; 5513c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = machine->codec_dai; 5526627a653SMark Brown struct snd_soc_codec *codec = card->codec; 553db2a4165SFrank Mandarino 554db2a4165SFrank Mandarino mutex_lock(&pcm_mutex); 555db2a4165SFrank Mandarino 556db2a4165SFrank Mandarino /* apply codec digital mute */ 5578c6529dbSLiam Girdwood if (!codec->active) 5588c6529dbSLiam Girdwood snd_soc_dai_digital_mute(codec_dai, 1); 559db2a4165SFrank Mandarino 560db2a4165SFrank Mandarino /* free any machine hw params */ 561db2a4165SFrank Mandarino if (machine->ops && machine->ops->hw_free) 562db2a4165SFrank Mandarino machine->ops->hw_free(substream); 563db2a4165SFrank Mandarino 564db2a4165SFrank Mandarino /* free any DMA resources */ 565db2a4165SFrank Mandarino if (platform->pcm_ops->hw_free) 566db2a4165SFrank Mandarino platform->pcm_ops->hw_free(substream); 567db2a4165SFrank Mandarino 568db2a4165SFrank Mandarino /* now free hw params for the DAI's */ 5696335d055SEric Miao if (codec_dai->ops->hw_free) 5706335d055SEric Miao codec_dai->ops->hw_free(substream, codec_dai); 571db2a4165SFrank Mandarino 5726335d055SEric Miao if (cpu_dai->ops->hw_free) 5736335d055SEric Miao cpu_dai->ops->hw_free(substream, cpu_dai); 574db2a4165SFrank Mandarino 575db2a4165SFrank Mandarino mutex_unlock(&pcm_mutex); 576db2a4165SFrank Mandarino return 0; 577db2a4165SFrank Mandarino } 578db2a4165SFrank Mandarino 579db2a4165SFrank Mandarino static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 580db2a4165SFrank Mandarino { 581db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd = substream->private_data; 582db2a4165SFrank Mandarino struct snd_soc_device *socdev = rtd->socdev; 58387689d56SMark Brown struct snd_soc_card *card= socdev->card; 584cb666e5bSLiam Girdwood struct snd_soc_dai_link *machine = rtd->dai; 58587689d56SMark Brown struct snd_soc_platform *platform = card->platform; 5863c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = machine->cpu_dai; 5873c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = machine->codec_dai; 588db2a4165SFrank Mandarino int ret; 589db2a4165SFrank Mandarino 5906335d055SEric Miao if (codec_dai->ops->trigger) { 5916335d055SEric Miao ret = codec_dai->ops->trigger(substream, cmd, codec_dai); 592db2a4165SFrank Mandarino if (ret < 0) 593db2a4165SFrank Mandarino return ret; 594db2a4165SFrank Mandarino } 595db2a4165SFrank Mandarino 596db2a4165SFrank Mandarino if (platform->pcm_ops->trigger) { 597db2a4165SFrank Mandarino ret = platform->pcm_ops->trigger(substream, cmd); 598db2a4165SFrank Mandarino if (ret < 0) 599db2a4165SFrank Mandarino return ret; 600db2a4165SFrank Mandarino } 601db2a4165SFrank Mandarino 6026335d055SEric Miao if (cpu_dai->ops->trigger) { 6036335d055SEric Miao ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai); 604db2a4165SFrank Mandarino if (ret < 0) 605db2a4165SFrank Mandarino return ret; 606db2a4165SFrank Mandarino } 607db2a4165SFrank Mandarino return 0; 608db2a4165SFrank Mandarino } 609db2a4165SFrank Mandarino 610db2a4165SFrank Mandarino /* ASoC PCM operations */ 611db2a4165SFrank Mandarino static struct snd_pcm_ops soc_pcm_ops = { 612db2a4165SFrank Mandarino .open = soc_pcm_open, 613db2a4165SFrank Mandarino .close = soc_codec_close, 614db2a4165SFrank Mandarino .hw_params = soc_pcm_hw_params, 615db2a4165SFrank Mandarino .hw_free = soc_pcm_hw_free, 616db2a4165SFrank Mandarino .prepare = soc_pcm_prepare, 617db2a4165SFrank Mandarino .trigger = soc_pcm_trigger, 618db2a4165SFrank Mandarino }; 619db2a4165SFrank Mandarino 620db2a4165SFrank Mandarino #ifdef CONFIG_PM 621db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */ 622*416356fcSMark Brown static int soc_suspend(struct device *dev) 623db2a4165SFrank Mandarino { 624*416356fcSMark Brown struct platform_device *pdev = to_platform_device(dev); 625db2a4165SFrank Mandarino struct snd_soc_device *socdev = platform_get_drvdata(pdev); 62687506549SMark Brown struct snd_soc_card *card = socdev->card; 62787689d56SMark Brown struct snd_soc_platform *platform = card->platform; 628db2a4165SFrank Mandarino struct snd_soc_codec_device *codec_dev = socdev->codec_dev; 6296627a653SMark Brown struct snd_soc_codec *codec = card->codec; 630db2a4165SFrank Mandarino int i; 631db2a4165SFrank Mandarino 632e3509ff0SDaniel Mack /* If the initialization of this soc device failed, there is no codec 633e3509ff0SDaniel Mack * associated with it. Just bail out in this case. 634e3509ff0SDaniel Mack */ 635e3509ff0SDaniel Mack if (!codec) 636e3509ff0SDaniel Mack return 0; 637e3509ff0SDaniel Mack 6386ed25978SAndy Green /* Due to the resume being scheduled into a workqueue we could 6396ed25978SAndy Green * suspend before that's finished - wait for it to complete. 6406ed25978SAndy Green */ 6416ed25978SAndy Green snd_power_lock(codec->card); 6426ed25978SAndy Green snd_power_wait(codec->card, SNDRV_CTL_POWER_D0); 6436ed25978SAndy Green snd_power_unlock(codec->card); 6446ed25978SAndy Green 6456ed25978SAndy Green /* we're going to block userspace touching us until resume completes */ 6466ed25978SAndy Green snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot); 6476ed25978SAndy Green 648db2a4165SFrank Mandarino /* mute any active DAC's */ 649dee89c4dSMark Brown for (i = 0; i < card->num_links; i++) { 650dee89c4dSMark Brown struct snd_soc_dai *dai = card->dai_link[i].codec_dai; 6516335d055SEric Miao if (dai->ops->digital_mute && dai->playback.active) 6526335d055SEric Miao dai->ops->digital_mute(dai, 1); 653db2a4165SFrank Mandarino } 654db2a4165SFrank Mandarino 6554ccab3e7SLiam Girdwood /* suspend all pcms */ 65687506549SMark Brown for (i = 0; i < card->num_links; i++) 65787506549SMark Brown snd_pcm_suspend_all(card->dai_link[i].pcm); 6584ccab3e7SLiam Girdwood 65987506549SMark Brown if (card->suspend_pre) 660*416356fcSMark Brown card->suspend_pre(pdev, PMSG_SUSPEND); 661db2a4165SFrank Mandarino 66287506549SMark Brown for (i = 0; i < card->num_links; i++) { 66387506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 6643ba9e10aSMark Brown if (cpu_dai->suspend && !cpu_dai->ac97_control) 665dc7d7b83SMark Brown cpu_dai->suspend(cpu_dai); 666db2a4165SFrank Mandarino if (platform->suspend) 66707c84d04SMark Brown platform->suspend(cpu_dai); 668db2a4165SFrank Mandarino } 669db2a4165SFrank Mandarino 670db2a4165SFrank Mandarino /* close any waiting streams and save state */ 6716308419aSMark Brown run_delayed_work(&card->delayed_work); 6720be9898aSMark Brown codec->suspend_bias_level = codec->bias_level; 673db2a4165SFrank Mandarino 674db2a4165SFrank Mandarino for (i = 0; i < codec->num_dai; i++) { 675db2a4165SFrank Mandarino char *stream = codec->dai[i].playback.stream_name; 676db2a4165SFrank Mandarino if (stream != NULL) 677db2a4165SFrank Mandarino snd_soc_dapm_stream_event(codec, stream, 678db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 679db2a4165SFrank Mandarino stream = codec->dai[i].capture.stream_name; 680db2a4165SFrank Mandarino if (stream != NULL) 681db2a4165SFrank Mandarino snd_soc_dapm_stream_event(codec, stream, 682db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 683db2a4165SFrank Mandarino } 684db2a4165SFrank Mandarino 685db2a4165SFrank Mandarino if (codec_dev->suspend) 686*416356fcSMark Brown codec_dev->suspend(pdev, PMSG_SUSPEND); 687db2a4165SFrank Mandarino 68887506549SMark Brown for (i = 0; i < card->num_links; i++) { 68987506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 6903ba9e10aSMark Brown if (cpu_dai->suspend && cpu_dai->ac97_control) 691dc7d7b83SMark Brown cpu_dai->suspend(cpu_dai); 692db2a4165SFrank Mandarino } 693db2a4165SFrank Mandarino 69487506549SMark Brown if (card->suspend_post) 695*416356fcSMark Brown card->suspend_post(pdev, PMSG_SUSPEND); 696db2a4165SFrank Mandarino 697db2a4165SFrank Mandarino return 0; 698db2a4165SFrank Mandarino } 699db2a4165SFrank Mandarino 7006ed25978SAndy Green /* deferred resume work, so resume can complete before we finished 7016ed25978SAndy Green * setting our codec back up, which can be very slow on I2C 7026ed25978SAndy Green */ 7036ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work) 704db2a4165SFrank Mandarino { 7056308419aSMark Brown struct snd_soc_card *card = container_of(work, 7066308419aSMark Brown struct snd_soc_card, 7076ed25978SAndy Green deferred_resume_work); 7086308419aSMark Brown struct snd_soc_device *socdev = card->socdev; 70987689d56SMark Brown struct snd_soc_platform *platform = card->platform; 710db2a4165SFrank Mandarino struct snd_soc_codec_device *codec_dev = socdev->codec_dev; 7116627a653SMark Brown struct snd_soc_codec *codec = card->codec; 7126ed25978SAndy Green struct platform_device *pdev = to_platform_device(socdev->dev); 713db2a4165SFrank Mandarino int i; 714db2a4165SFrank Mandarino 7156ed25978SAndy Green /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time, 7166ed25978SAndy Green * so userspace apps are blocked from touching us 7176ed25978SAndy Green */ 7186ed25978SAndy Green 719fde22f27SMark Brown dev_dbg(socdev->dev, "starting resume work\n"); 7206ed25978SAndy Green 72187506549SMark Brown if (card->resume_pre) 72287506549SMark Brown card->resume_pre(pdev); 723db2a4165SFrank Mandarino 72487506549SMark Brown for (i = 0; i < card->num_links; i++) { 72587506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 7263ba9e10aSMark Brown if (cpu_dai->resume && cpu_dai->ac97_control) 727dc7d7b83SMark Brown cpu_dai->resume(cpu_dai); 728db2a4165SFrank Mandarino } 729db2a4165SFrank Mandarino 730db2a4165SFrank Mandarino if (codec_dev->resume) 731db2a4165SFrank Mandarino codec_dev->resume(pdev); 732db2a4165SFrank Mandarino 733db2a4165SFrank Mandarino for (i = 0; i < codec->num_dai; i++) { 734db2a4165SFrank Mandarino char *stream = codec->dai[i].playback.stream_name; 735db2a4165SFrank Mandarino if (stream != NULL) 736db2a4165SFrank Mandarino snd_soc_dapm_stream_event(codec, stream, 737db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 738db2a4165SFrank Mandarino stream = codec->dai[i].capture.stream_name; 739db2a4165SFrank Mandarino if (stream != NULL) 740db2a4165SFrank Mandarino snd_soc_dapm_stream_event(codec, stream, 741db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 742db2a4165SFrank Mandarino } 743db2a4165SFrank Mandarino 7443ff3f64bSMark Brown /* unmute any active DACs */ 745dee89c4dSMark Brown for (i = 0; i < card->num_links; i++) { 746dee89c4dSMark Brown struct snd_soc_dai *dai = card->dai_link[i].codec_dai; 7476335d055SEric Miao if (dai->ops->digital_mute && dai->playback.active) 7486335d055SEric Miao dai->ops->digital_mute(dai, 0); 749db2a4165SFrank Mandarino } 750db2a4165SFrank Mandarino 75187506549SMark Brown for (i = 0; i < card->num_links; i++) { 75287506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 7533ba9e10aSMark Brown if (cpu_dai->resume && !cpu_dai->ac97_control) 754dc7d7b83SMark Brown cpu_dai->resume(cpu_dai); 755db2a4165SFrank Mandarino if (platform->resume) 75607c84d04SMark Brown platform->resume(cpu_dai); 757db2a4165SFrank Mandarino } 758db2a4165SFrank Mandarino 75987506549SMark Brown if (card->resume_post) 76087506549SMark Brown card->resume_post(pdev); 761db2a4165SFrank Mandarino 762fde22f27SMark Brown dev_dbg(socdev->dev, "resume work completed\n"); 7636ed25978SAndy Green 7646ed25978SAndy Green /* userspace can access us now we are back as we were before */ 7656ed25978SAndy Green snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0); 7666ed25978SAndy Green } 7676ed25978SAndy Green 7686ed25978SAndy Green /* powers up audio subsystem after a suspend */ 769*416356fcSMark Brown static int soc_resume(struct device *dev) 7706ed25978SAndy Green { 771*416356fcSMark Brown struct platform_device *pdev = to_platform_device(dev); 7726ed25978SAndy Green struct snd_soc_device *socdev = platform_get_drvdata(pdev); 7736308419aSMark Brown struct snd_soc_card *card = socdev->card; 77464ab9baaSMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai; 7756ed25978SAndy Green 77664ab9baaSMark Brown /* AC97 devices might have other drivers hanging off them so 77764ab9baaSMark Brown * need to resume immediately. Other drivers don't have that 77864ab9baaSMark Brown * problem and may take a substantial amount of time to resume 77964ab9baaSMark Brown * due to I/O costs and anti-pop so handle them out of line. 78064ab9baaSMark Brown */ 78164ab9baaSMark Brown if (cpu_dai->ac97_control) { 78264ab9baaSMark Brown dev_dbg(socdev->dev, "Resuming AC97 immediately\n"); 78364ab9baaSMark Brown soc_resume_deferred(&card->deferred_resume_work); 78464ab9baaSMark Brown } else { 78564ab9baaSMark Brown dev_dbg(socdev->dev, "Scheduling resume work\n"); 7866308419aSMark Brown if (!schedule_work(&card->deferred_resume_work)) 787fde22f27SMark Brown dev_err(socdev->dev, "resume work item may be lost\n"); 78864ab9baaSMark Brown } 7896ed25978SAndy Green 790db2a4165SFrank Mandarino return 0; 791db2a4165SFrank Mandarino } 792db2a4165SFrank Mandarino 793831dc0f1SMark Brown /** 794831dc0f1SMark Brown * snd_soc_suspend_device: Notify core of device suspend 795831dc0f1SMark Brown * 796831dc0f1SMark Brown * @dev: Device being suspended. 797831dc0f1SMark Brown * 798831dc0f1SMark Brown * In order to ensure that the entire audio subsystem is suspended in a 799831dc0f1SMark Brown * coordinated fashion ASoC devices should suspend themselves when 800831dc0f1SMark Brown * called by ASoC. When the standard kernel suspend process asks the 801831dc0f1SMark Brown * device to suspend it should call this function to initiate a suspend 802831dc0f1SMark Brown * of the entire ASoC card. 803831dc0f1SMark Brown * 804831dc0f1SMark Brown * \note Currently this function is stubbed out. 805831dc0f1SMark Brown */ 806831dc0f1SMark Brown int snd_soc_suspend_device(struct device *dev) 807831dc0f1SMark Brown { 808831dc0f1SMark Brown return 0; 809831dc0f1SMark Brown } 810831dc0f1SMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend_device); 811831dc0f1SMark Brown 812831dc0f1SMark Brown /** 813831dc0f1SMark Brown * snd_soc_resume_device: Notify core of device resume 814831dc0f1SMark Brown * 815831dc0f1SMark Brown * @dev: Device being resumed. 816831dc0f1SMark Brown * 817831dc0f1SMark Brown * In order to ensure that the entire audio subsystem is resumed in a 818831dc0f1SMark Brown * coordinated fashion ASoC devices should resume themselves when called 819831dc0f1SMark Brown * by ASoC. When the standard kernel resume process asks the device 820831dc0f1SMark Brown * to resume it should call this function. Once all the components of 821831dc0f1SMark Brown * the card have notified that they are ready to be resumed the card 822831dc0f1SMark Brown * will be resumed. 823831dc0f1SMark Brown * 824831dc0f1SMark Brown * \note Currently this function is stubbed out. 825831dc0f1SMark Brown */ 826831dc0f1SMark Brown int snd_soc_resume_device(struct device *dev) 827831dc0f1SMark Brown { 828831dc0f1SMark Brown return 0; 829831dc0f1SMark Brown } 830831dc0f1SMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume_device); 831db2a4165SFrank Mandarino #else 832db2a4165SFrank Mandarino #define soc_suspend NULL 833db2a4165SFrank Mandarino #define soc_resume NULL 834db2a4165SFrank Mandarino #endif 835db2a4165SFrank Mandarino 836435c5e25SMark Brown static void snd_soc_instantiate_card(struct snd_soc_card *card) 837db2a4165SFrank Mandarino { 838435c5e25SMark Brown struct platform_device *pdev = container_of(card->dev, 839435c5e25SMark Brown struct platform_device, 840435c5e25SMark Brown dev); 841435c5e25SMark Brown struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev; 842435c5e25SMark Brown struct snd_soc_platform *platform; 843435c5e25SMark Brown struct snd_soc_dai *dai; 8446b05eda6SMark Brown int i, found, ret, ac97; 845db2a4165SFrank Mandarino 846435c5e25SMark Brown if (card->instantiated) 847435c5e25SMark Brown return; 8486308419aSMark Brown 849435c5e25SMark Brown found = 0; 850435c5e25SMark Brown list_for_each_entry(platform, &platform_list, list) 851435c5e25SMark Brown if (card->platform == platform) { 852435c5e25SMark Brown found = 1; 853435c5e25SMark Brown break; 854435c5e25SMark Brown } 855435c5e25SMark Brown if (!found) { 856435c5e25SMark Brown dev_dbg(card->dev, "Platform %s not registered\n", 857435c5e25SMark Brown card->platform->name); 858435c5e25SMark Brown return; 859c5af3a2eSMark Brown } 860c5af3a2eSMark Brown 8616b05eda6SMark Brown ac97 = 0; 862435c5e25SMark Brown for (i = 0; i < card->num_links; i++) { 863435c5e25SMark Brown found = 0; 864435c5e25SMark Brown list_for_each_entry(dai, &dai_list, list) 865435c5e25SMark Brown if (card->dai_link[i].cpu_dai == dai) { 866435c5e25SMark Brown found = 1; 867435c5e25SMark Brown break; 868435c5e25SMark Brown } 869435c5e25SMark Brown if (!found) { 870435c5e25SMark Brown dev_dbg(card->dev, "DAI %s not registered\n", 871435c5e25SMark Brown card->dai_link[i].cpu_dai->name); 872435c5e25SMark Brown return; 873435c5e25SMark Brown } 8746b05eda6SMark Brown 8756b05eda6SMark Brown if (card->dai_link[i].cpu_dai->ac97_control) 8766b05eda6SMark Brown ac97 = 1; 8776b05eda6SMark Brown } 8786b05eda6SMark Brown 8796b05eda6SMark Brown /* If we have AC97 in the system then don't wait for the 8806b05eda6SMark Brown * codec. This will need revisiting if we have to handle 8816b05eda6SMark Brown * systems with mixed AC97 and non-AC97 parts. Only check for 8826b05eda6SMark Brown * DAIs currently; we can't do this per link since some AC97 8836b05eda6SMark Brown * codecs have non-AC97 DAIs. 8846b05eda6SMark Brown */ 8856b05eda6SMark Brown if (!ac97) 8866b05eda6SMark Brown for (i = 0; i < card->num_links; i++) { 8876b05eda6SMark Brown found = 0; 8886b05eda6SMark Brown list_for_each_entry(dai, &dai_list, list) 8896b05eda6SMark Brown if (card->dai_link[i].codec_dai == dai) { 8906b05eda6SMark Brown found = 1; 8916b05eda6SMark Brown break; 8926b05eda6SMark Brown } 8936b05eda6SMark Brown if (!found) { 8946b05eda6SMark Brown dev_dbg(card->dev, "DAI %s not registered\n", 8956b05eda6SMark Brown card->dai_link[i].codec_dai->name); 8966b05eda6SMark Brown return; 8976b05eda6SMark Brown } 898435c5e25SMark Brown } 899435c5e25SMark Brown 900435c5e25SMark Brown /* Note that we do not current check for codec components */ 901435c5e25SMark Brown 902435c5e25SMark Brown dev_dbg(card->dev, "All components present, instantiating\n"); 903435c5e25SMark Brown 904435c5e25SMark Brown /* Found everything, bring it up */ 90587506549SMark Brown if (card->probe) { 90687506549SMark Brown ret = card->probe(pdev); 907db2a4165SFrank Mandarino if (ret < 0) 908435c5e25SMark Brown return; 909db2a4165SFrank Mandarino } 910db2a4165SFrank Mandarino 91187506549SMark Brown for (i = 0; i < card->num_links; i++) { 91287506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 913db2a4165SFrank Mandarino if (cpu_dai->probe) { 914bdb92876SMark Brown ret = cpu_dai->probe(pdev, cpu_dai); 915db2a4165SFrank Mandarino if (ret < 0) 916db2a4165SFrank Mandarino goto cpu_dai_err; 917db2a4165SFrank Mandarino } 918db2a4165SFrank Mandarino } 919db2a4165SFrank Mandarino 920db2a4165SFrank Mandarino if (codec_dev->probe) { 921db2a4165SFrank Mandarino ret = codec_dev->probe(pdev); 922db2a4165SFrank Mandarino if (ret < 0) 923db2a4165SFrank Mandarino goto cpu_dai_err; 924db2a4165SFrank Mandarino } 925db2a4165SFrank Mandarino 926db2a4165SFrank Mandarino if (platform->probe) { 927db2a4165SFrank Mandarino ret = platform->probe(pdev); 928db2a4165SFrank Mandarino if (ret < 0) 929db2a4165SFrank Mandarino goto platform_err; 930db2a4165SFrank Mandarino } 931db2a4165SFrank Mandarino 932db2a4165SFrank Mandarino /* DAPM stream work */ 9336308419aSMark Brown INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work); 9341301a964SRandy Dunlap #ifdef CONFIG_PM 9356ed25978SAndy Green /* deferred resume work */ 9366308419aSMark Brown INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); 9371301a964SRandy Dunlap #endif 9386ed25978SAndy Green 939435c5e25SMark Brown card->instantiated = 1; 940435c5e25SMark Brown 941435c5e25SMark Brown return; 942db2a4165SFrank Mandarino 943db2a4165SFrank Mandarino platform_err: 944db2a4165SFrank Mandarino if (codec_dev->remove) 945db2a4165SFrank Mandarino codec_dev->remove(pdev); 946db2a4165SFrank Mandarino 947db2a4165SFrank Mandarino cpu_dai_err: 94818b9b3d9SLiam Girdwood for (i--; i >= 0; i--) { 94987506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 950db2a4165SFrank Mandarino if (cpu_dai->remove) 951bdb92876SMark Brown cpu_dai->remove(pdev, cpu_dai); 952db2a4165SFrank Mandarino } 953db2a4165SFrank Mandarino 95487506549SMark Brown if (card->remove) 95587506549SMark Brown card->remove(pdev); 956435c5e25SMark Brown } 957db2a4165SFrank Mandarino 958435c5e25SMark Brown /* 959435c5e25SMark Brown * Attempt to initialise any uninitalised cards. Must be called with 960435c5e25SMark Brown * client_mutex. 961435c5e25SMark Brown */ 962435c5e25SMark Brown static void snd_soc_instantiate_cards(void) 963435c5e25SMark Brown { 964435c5e25SMark Brown struct snd_soc_card *card; 965435c5e25SMark Brown list_for_each_entry(card, &card_list, list) 966435c5e25SMark Brown snd_soc_instantiate_card(card); 967435c5e25SMark Brown } 968435c5e25SMark Brown 969435c5e25SMark Brown /* probes a new socdev */ 970435c5e25SMark Brown static int soc_probe(struct platform_device *pdev) 971435c5e25SMark Brown { 972435c5e25SMark Brown int ret = 0; 973435c5e25SMark Brown struct snd_soc_device *socdev = platform_get_drvdata(pdev); 974435c5e25SMark Brown struct snd_soc_card *card = socdev->card; 975435c5e25SMark Brown 976435c5e25SMark Brown /* Bodge while we push things out of socdev */ 977435c5e25SMark Brown card->socdev = socdev; 978435c5e25SMark Brown 979435c5e25SMark Brown /* Bodge while we unpick instantiation */ 980435c5e25SMark Brown card->dev = &pdev->dev; 981435c5e25SMark Brown ret = snd_soc_register_card(card); 982435c5e25SMark Brown if (ret != 0) { 983435c5e25SMark Brown dev_err(&pdev->dev, "Failed to register card\n"); 984db2a4165SFrank Mandarino return ret; 985db2a4165SFrank Mandarino } 986db2a4165SFrank Mandarino 987435c5e25SMark Brown return 0; 988435c5e25SMark Brown } 989435c5e25SMark Brown 990db2a4165SFrank Mandarino /* removes a socdev */ 991db2a4165SFrank Mandarino static int soc_remove(struct platform_device *pdev) 992db2a4165SFrank Mandarino { 993db2a4165SFrank Mandarino int i; 994db2a4165SFrank Mandarino struct snd_soc_device *socdev = platform_get_drvdata(pdev); 99587506549SMark Brown struct snd_soc_card *card = socdev->card; 99687689d56SMark Brown struct snd_soc_platform *platform = card->platform; 997db2a4165SFrank Mandarino struct snd_soc_codec_device *codec_dev = socdev->codec_dev; 998db2a4165SFrank Mandarino 999914dc182SMike Rapoport if (!card->instantiated) 1000914dc182SMike Rapoport return 0; 1001914dc182SMike Rapoport 10026308419aSMark Brown run_delayed_work(&card->delayed_work); 1003965ac42cSLiam Girdwood 1004db2a4165SFrank Mandarino if (platform->remove) 1005db2a4165SFrank Mandarino platform->remove(pdev); 1006db2a4165SFrank Mandarino 1007db2a4165SFrank Mandarino if (codec_dev->remove) 1008db2a4165SFrank Mandarino codec_dev->remove(pdev); 1009db2a4165SFrank Mandarino 101087506549SMark Brown for (i = 0; i < card->num_links; i++) { 101187506549SMark Brown struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; 1012db2a4165SFrank Mandarino if (cpu_dai->remove) 1013bdb92876SMark Brown cpu_dai->remove(pdev, cpu_dai); 1014db2a4165SFrank Mandarino } 1015db2a4165SFrank Mandarino 101687506549SMark Brown if (card->remove) 101787506549SMark Brown card->remove(pdev); 1018db2a4165SFrank Mandarino 1019c5af3a2eSMark Brown snd_soc_unregister_card(card); 1020c5af3a2eSMark Brown 1021db2a4165SFrank Mandarino return 0; 1022db2a4165SFrank Mandarino } 1023db2a4165SFrank Mandarino 1024*416356fcSMark Brown static int soc_poweroff(struct device *dev) 102551737470SMark Brown { 1026*416356fcSMark Brown struct platform_device *pdev = to_platform_device(dev); 102751737470SMark Brown struct snd_soc_device *socdev = platform_get_drvdata(pdev); 102851737470SMark Brown struct snd_soc_card *card = socdev->card; 102951737470SMark Brown 103051737470SMark Brown if (!card->instantiated) 1031*416356fcSMark Brown return 0; 103251737470SMark Brown 103351737470SMark Brown /* Flush out pmdown_time work - we actually do want to run it 103451737470SMark Brown * now, we're shutting down so no imminent restart. */ 103551737470SMark Brown run_delayed_work(&card->delayed_work); 103651737470SMark Brown 103751737470SMark Brown snd_soc_dapm_shutdown(socdev); 1038*416356fcSMark Brown 1039*416356fcSMark Brown return 0; 104051737470SMark Brown } 104151737470SMark Brown 1042*416356fcSMark Brown static struct dev_pm_ops soc_pm_ops = { 1043*416356fcSMark Brown .suspend = soc_suspend, 1044*416356fcSMark Brown .resume = soc_resume, 1045*416356fcSMark Brown .poweroff = soc_poweroff, 1046*416356fcSMark Brown }; 1047*416356fcSMark Brown 1048db2a4165SFrank Mandarino /* ASoC platform driver */ 1049db2a4165SFrank Mandarino static struct platform_driver soc_driver = { 1050db2a4165SFrank Mandarino .driver = { 1051db2a4165SFrank Mandarino .name = "soc-audio", 10528b45a209SKay Sievers .owner = THIS_MODULE, 1053*416356fcSMark Brown .pm = &soc_pm_ops, 1054db2a4165SFrank Mandarino }, 1055db2a4165SFrank Mandarino .probe = soc_probe, 1056db2a4165SFrank Mandarino .remove = soc_remove, 1057db2a4165SFrank Mandarino }; 1058db2a4165SFrank Mandarino 1059db2a4165SFrank Mandarino /* create a new pcm */ 1060db2a4165SFrank Mandarino static int soc_new_pcm(struct snd_soc_device *socdev, 1061db2a4165SFrank Mandarino struct snd_soc_dai_link *dai_link, int num) 1062db2a4165SFrank Mandarino { 106387689d56SMark Brown struct snd_soc_card *card = socdev->card; 10646627a653SMark Brown struct snd_soc_codec *codec = card->codec; 106587689d56SMark Brown struct snd_soc_platform *platform = card->platform; 10663c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai = dai_link->codec_dai; 10673c4b266fSLiam Girdwood struct snd_soc_dai *cpu_dai = dai_link->cpu_dai; 1068db2a4165SFrank Mandarino struct snd_soc_pcm_runtime *rtd; 1069db2a4165SFrank Mandarino struct snd_pcm *pcm; 1070db2a4165SFrank Mandarino char new_name[64]; 1071db2a4165SFrank Mandarino int ret = 0, playback = 0, capture = 0; 1072db2a4165SFrank Mandarino 1073db2a4165SFrank Mandarino rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL); 1074db2a4165SFrank Mandarino if (rtd == NULL) 1075db2a4165SFrank Mandarino return -ENOMEM; 1076cb666e5bSLiam Girdwood 1077cb666e5bSLiam Girdwood rtd->dai = dai_link; 1078db2a4165SFrank Mandarino rtd->socdev = socdev; 10796627a653SMark Brown codec_dai->codec = card->codec; 1080db2a4165SFrank Mandarino 1081db2a4165SFrank Mandarino /* check client and interface hw capabilities */ 10823ba9e10aSMark Brown sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name, 10833ba9e10aSMark Brown num); 1084db2a4165SFrank Mandarino 1085db2a4165SFrank Mandarino if (codec_dai->playback.channels_min) 1086db2a4165SFrank Mandarino playback = 1; 1087db2a4165SFrank Mandarino if (codec_dai->capture.channels_min) 1088db2a4165SFrank Mandarino capture = 1; 1089db2a4165SFrank Mandarino 1090db2a4165SFrank Mandarino ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback, 1091db2a4165SFrank Mandarino capture, &pcm); 1092db2a4165SFrank Mandarino if (ret < 0) { 10933ff3f64bSMark Brown printk(KERN_ERR "asoc: can't create pcm for codec %s\n", 10943ff3f64bSMark Brown codec->name); 1095db2a4165SFrank Mandarino kfree(rtd); 1096db2a4165SFrank Mandarino return ret; 1097db2a4165SFrank Mandarino } 1098db2a4165SFrank Mandarino 10994ccab3e7SLiam Girdwood dai_link->pcm = pcm; 1100db2a4165SFrank Mandarino pcm->private_data = rtd; 110187689d56SMark Brown soc_pcm_ops.mmap = platform->pcm_ops->mmap; 110287689d56SMark Brown soc_pcm_ops.pointer = platform->pcm_ops->pointer; 110387689d56SMark Brown soc_pcm_ops.ioctl = platform->pcm_ops->ioctl; 110487689d56SMark Brown soc_pcm_ops.copy = platform->pcm_ops->copy; 110587689d56SMark Brown soc_pcm_ops.silence = platform->pcm_ops->silence; 110687689d56SMark Brown soc_pcm_ops.ack = platform->pcm_ops->ack; 110787689d56SMark Brown soc_pcm_ops.page = platform->pcm_ops->page; 1108db2a4165SFrank Mandarino 1109db2a4165SFrank Mandarino if (playback) 1110db2a4165SFrank Mandarino snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops); 1111db2a4165SFrank Mandarino 1112db2a4165SFrank Mandarino if (capture) 1113db2a4165SFrank Mandarino snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops); 1114db2a4165SFrank Mandarino 111587689d56SMark Brown ret = platform->pcm_new(codec->card, codec_dai, pcm); 1116db2a4165SFrank Mandarino if (ret < 0) { 1117db2a4165SFrank Mandarino printk(KERN_ERR "asoc: platform pcm constructor failed\n"); 1118db2a4165SFrank Mandarino kfree(rtd); 1119db2a4165SFrank Mandarino return ret; 1120db2a4165SFrank Mandarino } 1121db2a4165SFrank Mandarino 112287689d56SMark Brown pcm->private_free = platform->pcm_free; 1123db2a4165SFrank Mandarino printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name, 1124db2a4165SFrank Mandarino cpu_dai->name); 1125db2a4165SFrank Mandarino return ret; 1126db2a4165SFrank Mandarino } 1127db2a4165SFrank Mandarino 1128db2a4165SFrank Mandarino /* codec register dump */ 11296627a653SMark Brown static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf) 1130db2a4165SFrank Mandarino { 1131db2a4165SFrank Mandarino int i, step = 1, count = 0; 1132db2a4165SFrank Mandarino 1133db2a4165SFrank Mandarino if (!codec->reg_cache_size) 1134db2a4165SFrank Mandarino return 0; 1135db2a4165SFrank Mandarino 1136db2a4165SFrank Mandarino if (codec->reg_cache_step) 1137db2a4165SFrank Mandarino step = codec->reg_cache_step; 1138db2a4165SFrank Mandarino 1139db2a4165SFrank Mandarino count += sprintf(buf, "%s registers\n", codec->name); 114058cd33c0SMark Brown for (i = 0; i < codec->reg_cache_size; i += step) { 114158cd33c0SMark Brown count += sprintf(buf + count, "%2x: ", i); 114258cd33c0SMark Brown if (count >= PAGE_SIZE - 1) 114358cd33c0SMark Brown break; 114458cd33c0SMark Brown 114558cd33c0SMark Brown if (codec->display_register) 114658cd33c0SMark Brown count += codec->display_register(codec, buf + count, 114758cd33c0SMark Brown PAGE_SIZE - count, i); 114858cd33c0SMark Brown else 114958cd33c0SMark Brown count += snprintf(buf + count, PAGE_SIZE - count, 115058cd33c0SMark Brown "%4x", codec->read(codec, i)); 115158cd33c0SMark Brown 115258cd33c0SMark Brown if (count >= PAGE_SIZE - 1) 115358cd33c0SMark Brown break; 115458cd33c0SMark Brown 115558cd33c0SMark Brown count += snprintf(buf + count, PAGE_SIZE - count, "\n"); 115658cd33c0SMark Brown if (count >= PAGE_SIZE - 1) 115758cd33c0SMark Brown break; 115858cd33c0SMark Brown } 115958cd33c0SMark Brown 116058cd33c0SMark Brown /* Truncate count; min() would cause a warning */ 116158cd33c0SMark Brown if (count >= PAGE_SIZE) 116258cd33c0SMark Brown count = PAGE_SIZE - 1; 1163db2a4165SFrank Mandarino 1164db2a4165SFrank Mandarino return count; 1165db2a4165SFrank Mandarino } 116612ef193dSTroy Kisky static ssize_t codec_reg_show(struct device *dev, 116712ef193dSTroy Kisky struct device_attribute *attr, char *buf) 116812ef193dSTroy Kisky { 116912ef193dSTroy Kisky struct snd_soc_device *devdata = dev_get_drvdata(dev); 11706627a653SMark Brown return soc_codec_reg_show(devdata->card->codec, buf); 117112ef193dSTroy Kisky } 117212ef193dSTroy Kisky 1173db2a4165SFrank Mandarino static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL); 1174db2a4165SFrank Mandarino 117512ef193dSTroy Kisky #ifdef CONFIG_DEBUG_FS 117612ef193dSTroy Kisky static int codec_reg_open_file(struct inode *inode, struct file *file) 117712ef193dSTroy Kisky { 117812ef193dSTroy Kisky file->private_data = inode->i_private; 117912ef193dSTroy Kisky return 0; 118012ef193dSTroy Kisky } 118112ef193dSTroy Kisky 118212ef193dSTroy Kisky static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf, 118312ef193dSTroy Kisky size_t count, loff_t *ppos) 118412ef193dSTroy Kisky { 118512ef193dSTroy Kisky ssize_t ret; 1186384c89e2SMark Brown struct snd_soc_codec *codec = file->private_data; 118712ef193dSTroy Kisky char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 118812ef193dSTroy Kisky if (!buf) 118912ef193dSTroy Kisky return -ENOMEM; 11906627a653SMark Brown ret = soc_codec_reg_show(codec, buf); 119112ef193dSTroy Kisky if (ret >= 0) 119212ef193dSTroy Kisky ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 119312ef193dSTroy Kisky kfree(buf); 119412ef193dSTroy Kisky return ret; 119512ef193dSTroy Kisky } 119612ef193dSTroy Kisky 119712ef193dSTroy Kisky static ssize_t codec_reg_write_file(struct file *file, 119812ef193dSTroy Kisky const char __user *user_buf, size_t count, loff_t *ppos) 119912ef193dSTroy Kisky { 120012ef193dSTroy Kisky char buf[32]; 120112ef193dSTroy Kisky int buf_size; 120212ef193dSTroy Kisky char *start = buf; 120312ef193dSTroy Kisky unsigned long reg, value; 120412ef193dSTroy Kisky int step = 1; 1205384c89e2SMark Brown struct snd_soc_codec *codec = file->private_data; 120612ef193dSTroy Kisky 120712ef193dSTroy Kisky buf_size = min(count, (sizeof(buf)-1)); 120812ef193dSTroy Kisky if (copy_from_user(buf, user_buf, buf_size)) 120912ef193dSTroy Kisky return -EFAULT; 121012ef193dSTroy Kisky buf[buf_size] = 0; 121112ef193dSTroy Kisky 121212ef193dSTroy Kisky if (codec->reg_cache_step) 121312ef193dSTroy Kisky step = codec->reg_cache_step; 121412ef193dSTroy Kisky 121512ef193dSTroy Kisky while (*start == ' ') 121612ef193dSTroy Kisky start++; 121712ef193dSTroy Kisky reg = simple_strtoul(start, &start, 16); 121812ef193dSTroy Kisky if ((reg >= codec->reg_cache_size) || (reg % step)) 121912ef193dSTroy Kisky return -EINVAL; 122012ef193dSTroy Kisky while (*start == ' ') 122112ef193dSTroy Kisky start++; 122212ef193dSTroy Kisky if (strict_strtoul(start, 16, &value)) 122312ef193dSTroy Kisky return -EINVAL; 122412ef193dSTroy Kisky codec->write(codec, reg, value); 122512ef193dSTroy Kisky return buf_size; 122612ef193dSTroy Kisky } 122712ef193dSTroy Kisky 122812ef193dSTroy Kisky static const struct file_operations codec_reg_fops = { 122912ef193dSTroy Kisky .open = codec_reg_open_file, 123012ef193dSTroy Kisky .read = codec_reg_read_file, 123112ef193dSTroy Kisky .write = codec_reg_write_file, 123212ef193dSTroy Kisky }; 123312ef193dSTroy Kisky 1234384c89e2SMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec) 123512ef193dSTroy Kisky { 1236384c89e2SMark Brown codec->debugfs_reg = debugfs_create_file("codec_reg", 0644, 1237384c89e2SMark Brown debugfs_root, codec, 1238384c89e2SMark Brown &codec_reg_fops); 1239384c89e2SMark Brown if (!codec->debugfs_reg) 1240384c89e2SMark Brown printk(KERN_WARNING 1241384c89e2SMark Brown "ASoC: Failed to create codec register debugfs file\n"); 124212ef193dSTroy Kisky 1243384c89e2SMark Brown codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744, 1244384c89e2SMark Brown debugfs_root, 1245384c89e2SMark Brown &codec->pop_time); 1246384c89e2SMark Brown if (!codec->debugfs_pop_time) 1247384c89e2SMark Brown printk(KERN_WARNING 1248384c89e2SMark Brown "Failed to create pop time debugfs file\n"); 124912ef193dSTroy Kisky } 125012ef193dSTroy Kisky 1251384c89e2SMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) 125212ef193dSTroy Kisky { 1253384c89e2SMark Brown debugfs_remove(codec->debugfs_pop_time); 1254384c89e2SMark Brown debugfs_remove(codec->debugfs_reg); 125512ef193dSTroy Kisky } 125612ef193dSTroy Kisky 125712ef193dSTroy Kisky #else 125812ef193dSTroy Kisky 1259384c89e2SMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec) 126012ef193dSTroy Kisky { 126112ef193dSTroy Kisky } 126212ef193dSTroy Kisky 1263384c89e2SMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) 126412ef193dSTroy Kisky { 126512ef193dSTroy Kisky } 126612ef193dSTroy Kisky #endif 126712ef193dSTroy Kisky 1268db2a4165SFrank Mandarino /** 1269db2a4165SFrank Mandarino * snd_soc_new_ac97_codec - initailise AC97 device 1270db2a4165SFrank Mandarino * @codec: audio codec 1271db2a4165SFrank Mandarino * @ops: AC97 bus operations 1272db2a4165SFrank Mandarino * @num: AC97 codec number 1273db2a4165SFrank Mandarino * 1274db2a4165SFrank Mandarino * Initialises AC97 codec resources for use by ad-hoc devices only. 1275db2a4165SFrank Mandarino */ 1276db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec, 1277db2a4165SFrank Mandarino struct snd_ac97_bus_ops *ops, int num) 1278db2a4165SFrank Mandarino { 1279db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 1280db2a4165SFrank Mandarino 1281db2a4165SFrank Mandarino codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL); 1282db2a4165SFrank Mandarino if (codec->ac97 == NULL) { 1283db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1284db2a4165SFrank Mandarino return -ENOMEM; 1285db2a4165SFrank Mandarino } 1286db2a4165SFrank Mandarino 1287db2a4165SFrank Mandarino codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL); 1288db2a4165SFrank Mandarino if (codec->ac97->bus == NULL) { 1289db2a4165SFrank Mandarino kfree(codec->ac97); 1290db2a4165SFrank Mandarino codec->ac97 = NULL; 1291db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1292db2a4165SFrank Mandarino return -ENOMEM; 1293db2a4165SFrank Mandarino } 1294db2a4165SFrank Mandarino 1295db2a4165SFrank Mandarino codec->ac97->bus->ops = ops; 1296db2a4165SFrank Mandarino codec->ac97->num = num; 1297db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1298db2a4165SFrank Mandarino return 0; 1299db2a4165SFrank Mandarino } 1300db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec); 1301db2a4165SFrank Mandarino 1302db2a4165SFrank Mandarino /** 1303db2a4165SFrank Mandarino * snd_soc_free_ac97_codec - free AC97 codec device 1304db2a4165SFrank Mandarino * @codec: audio codec 1305db2a4165SFrank Mandarino * 1306db2a4165SFrank Mandarino * Frees AC97 codec device resources. 1307db2a4165SFrank Mandarino */ 1308db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec) 1309db2a4165SFrank Mandarino { 1310db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 1311db2a4165SFrank Mandarino kfree(codec->ac97->bus); 1312db2a4165SFrank Mandarino kfree(codec->ac97); 1313db2a4165SFrank Mandarino codec->ac97 = NULL; 1314db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1315db2a4165SFrank Mandarino } 1316db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec); 1317db2a4165SFrank Mandarino 1318db2a4165SFrank Mandarino /** 1319db2a4165SFrank Mandarino * snd_soc_update_bits - update codec register bits 1320db2a4165SFrank Mandarino * @codec: audio codec 1321db2a4165SFrank Mandarino * @reg: codec register 1322db2a4165SFrank Mandarino * @mask: register mask 1323db2a4165SFrank Mandarino * @value: new value 1324db2a4165SFrank Mandarino * 1325db2a4165SFrank Mandarino * Writes new register value. 1326db2a4165SFrank Mandarino * 1327db2a4165SFrank Mandarino * Returns 1 for change else 0. 1328db2a4165SFrank Mandarino */ 1329db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg, 133046f5822fSDaniel Ribeiro unsigned int mask, unsigned int value) 1331db2a4165SFrank Mandarino { 1332db2a4165SFrank Mandarino int change; 133346f5822fSDaniel Ribeiro unsigned int old, new; 1334db2a4165SFrank Mandarino 1335db2a4165SFrank Mandarino mutex_lock(&io_mutex); 1336db2a4165SFrank Mandarino old = snd_soc_read(codec, reg); 1337db2a4165SFrank Mandarino new = (old & ~mask) | value; 1338db2a4165SFrank Mandarino change = old != new; 1339db2a4165SFrank Mandarino if (change) 1340db2a4165SFrank Mandarino snd_soc_write(codec, reg, new); 1341db2a4165SFrank Mandarino 1342db2a4165SFrank Mandarino mutex_unlock(&io_mutex); 1343db2a4165SFrank Mandarino return change; 1344db2a4165SFrank Mandarino } 1345db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits); 1346db2a4165SFrank Mandarino 1347db2a4165SFrank Mandarino /** 1348db2a4165SFrank Mandarino * snd_soc_test_bits - test register for change 1349db2a4165SFrank Mandarino * @codec: audio codec 1350db2a4165SFrank Mandarino * @reg: codec register 1351db2a4165SFrank Mandarino * @mask: register mask 1352db2a4165SFrank Mandarino * @value: new value 1353db2a4165SFrank Mandarino * 1354db2a4165SFrank Mandarino * Tests a register with a new value and checks if the new value is 1355db2a4165SFrank Mandarino * different from the old value. 1356db2a4165SFrank Mandarino * 1357db2a4165SFrank Mandarino * Returns 1 for change else 0. 1358db2a4165SFrank Mandarino */ 1359db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg, 136046f5822fSDaniel Ribeiro unsigned int mask, unsigned int value) 1361db2a4165SFrank Mandarino { 1362db2a4165SFrank Mandarino int change; 136346f5822fSDaniel Ribeiro unsigned int old, new; 1364db2a4165SFrank Mandarino 1365db2a4165SFrank Mandarino mutex_lock(&io_mutex); 1366db2a4165SFrank Mandarino old = snd_soc_read(codec, reg); 1367db2a4165SFrank Mandarino new = (old & ~mask) | value; 1368db2a4165SFrank Mandarino change = old != new; 1369db2a4165SFrank Mandarino mutex_unlock(&io_mutex); 1370db2a4165SFrank Mandarino 1371db2a4165SFrank Mandarino return change; 1372db2a4165SFrank Mandarino } 1373db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits); 1374db2a4165SFrank Mandarino 1375db2a4165SFrank Mandarino /** 1376db2a4165SFrank Mandarino * snd_soc_new_pcms - create new sound card and pcms 1377db2a4165SFrank Mandarino * @socdev: the SoC audio device 1378ac11a2b3SMark Brown * @idx: ALSA card index 1379ac11a2b3SMark Brown * @xid: card identification 1380db2a4165SFrank Mandarino * 1381db2a4165SFrank Mandarino * Create a new sound card based upon the codec and interface pcms. 1382db2a4165SFrank Mandarino * 1383db2a4165SFrank Mandarino * Returns 0 for success, else error. 1384db2a4165SFrank Mandarino */ 1385db2a4165SFrank Mandarino int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid) 1386db2a4165SFrank Mandarino { 138787506549SMark Brown struct snd_soc_card *card = socdev->card; 13886627a653SMark Brown struct snd_soc_codec *codec = card->codec; 1389bd7dd77cSTakashi Iwai int ret, i; 1390db2a4165SFrank Mandarino 1391db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 1392db2a4165SFrank Mandarino 1393db2a4165SFrank Mandarino /* register a sound card */ 1394bd7dd77cSTakashi Iwai ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card); 1395bd7dd77cSTakashi Iwai if (ret < 0) { 1396db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't create sound card for codec %s\n", 1397db2a4165SFrank Mandarino codec->name); 1398db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1399bd7dd77cSTakashi Iwai return ret; 1400db2a4165SFrank Mandarino } 1401db2a4165SFrank Mandarino 1402452c5eaaSMark Brown codec->socdev = socdev; 1403db2a4165SFrank Mandarino codec->card->dev = socdev->dev; 1404db2a4165SFrank Mandarino codec->card->private_data = codec; 1405db2a4165SFrank Mandarino strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver)); 1406db2a4165SFrank Mandarino 1407db2a4165SFrank Mandarino /* create the pcms */ 140887506549SMark Brown for (i = 0; i < card->num_links; i++) { 140987506549SMark Brown ret = soc_new_pcm(socdev, &card->dai_link[i], i); 1410db2a4165SFrank Mandarino if (ret < 0) { 1411db2a4165SFrank Mandarino printk(KERN_ERR "asoc: can't create pcm %s\n", 141287506549SMark Brown card->dai_link[i].stream_name); 1413db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1414db2a4165SFrank Mandarino return ret; 1415db2a4165SFrank Mandarino } 1416db2a4165SFrank Mandarino } 1417db2a4165SFrank Mandarino 1418db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1419db2a4165SFrank Mandarino return ret; 1420db2a4165SFrank Mandarino } 1421db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_pcms); 1422db2a4165SFrank Mandarino 1423db2a4165SFrank Mandarino /** 1424968a6025SMark Brown * snd_soc_init_card - register sound card 1425db2a4165SFrank Mandarino * @socdev: the SoC audio device 1426db2a4165SFrank Mandarino * 1427db2a4165SFrank Mandarino * Register a SoC sound card. Also registers an AC97 device if the 1428db2a4165SFrank Mandarino * codec is AC97 for ad hoc devices. 1429db2a4165SFrank Mandarino * 1430db2a4165SFrank Mandarino * Returns 0 for success, else error. 1431db2a4165SFrank Mandarino */ 1432968a6025SMark Brown int snd_soc_init_card(struct snd_soc_device *socdev) 1433db2a4165SFrank Mandarino { 143487506549SMark Brown struct snd_soc_card *card = socdev->card; 14356627a653SMark Brown struct snd_soc_codec *codec = card->codec; 143612e74f7dSLiam Girdwood int ret = 0, i, ac97 = 0, err = 0; 1437db2a4165SFrank Mandarino 143887506549SMark Brown for (i = 0; i < card->num_links; i++) { 143987506549SMark Brown if (card->dai_link[i].init) { 144087506549SMark Brown err = card->dai_link[i].init(codec); 144112e74f7dSLiam Girdwood if (err < 0) { 144212e74f7dSLiam Girdwood printk(KERN_ERR "asoc: failed to init %s\n", 144387506549SMark Brown card->dai_link[i].stream_name); 144412e74f7dSLiam Girdwood continue; 144512e74f7dSLiam Girdwood } 144612e74f7dSLiam Girdwood } 14473ba9e10aSMark Brown if (card->dai_link[i].codec_dai->ac97_control) 1448db2a4165SFrank Mandarino ac97 = 1; 1449db2a4165SFrank Mandarino } 1450db2a4165SFrank Mandarino snprintf(codec->card->shortname, sizeof(codec->card->shortname), 145187506549SMark Brown "%s", card->name); 1452db2a4165SFrank Mandarino snprintf(codec->card->longname, sizeof(codec->card->longname), 145387506549SMark Brown "%s (%s)", card->name, codec->name); 1454db2a4165SFrank Mandarino 14556d5701b2SMark Brown /* Make sure all DAPM widgets are instantiated */ 14566d5701b2SMark Brown snd_soc_dapm_new_widgets(codec); 14576d5701b2SMark Brown 1458db2a4165SFrank Mandarino ret = snd_card_register(codec->card); 1459db2a4165SFrank Mandarino if (ret < 0) { 14603ff3f64bSMark Brown printk(KERN_ERR "asoc: failed to register soundcard for %s\n", 1461db2a4165SFrank Mandarino codec->name); 146212e74f7dSLiam Girdwood goto out; 1463db2a4165SFrank Mandarino } 1464db2a4165SFrank Mandarino 146508c8efe6SMark Brown mutex_lock(&codec->mutex); 1466db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS 146714fa43f5SMark Brown /* Only instantiate AC97 if not already done by the adaptor 146814fa43f5SMark Brown * for the generic AC97 subsystem. 146914fa43f5SMark Brown */ 147014fa43f5SMark Brown if (ac97 && strcmp(codec->name, "AC97") != 0) { 147112e74f7dSLiam Girdwood ret = soc_ac97_dev_register(codec); 147212e74f7dSLiam Girdwood if (ret < 0) { 147312e74f7dSLiam Girdwood printk(KERN_ERR "asoc: AC97 device register failed\n"); 147412e74f7dSLiam Girdwood snd_card_free(codec->card); 147508c8efe6SMark Brown mutex_unlock(&codec->mutex); 147612e74f7dSLiam Girdwood goto out; 147712e74f7dSLiam Girdwood } 147812e74f7dSLiam Girdwood } 1479db2a4165SFrank Mandarino #endif 1480db2a4165SFrank Mandarino 148112e74f7dSLiam Girdwood err = snd_soc_dapm_sys_add(socdev->dev); 148212e74f7dSLiam Girdwood if (err < 0) 148312e74f7dSLiam Girdwood printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n"); 148412e74f7dSLiam Girdwood 148512e74f7dSLiam Girdwood err = device_create_file(socdev->dev, &dev_attr_codec_reg); 148612e74f7dSLiam Girdwood if (err < 0) 14873ff3f64bSMark Brown printk(KERN_WARNING "asoc: failed to add codec sysfs files\n"); 148808c8efe6SMark Brown 14896627a653SMark Brown soc_init_codec_debugfs(codec); 1490db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 149108c8efe6SMark Brown 149208c8efe6SMark Brown out: 1493db2a4165SFrank Mandarino return ret; 1494db2a4165SFrank Mandarino } 1495968a6025SMark Brown EXPORT_SYMBOL_GPL(snd_soc_init_card); 1496db2a4165SFrank Mandarino 1497db2a4165SFrank Mandarino /** 1498db2a4165SFrank Mandarino * snd_soc_free_pcms - free sound card and pcms 1499db2a4165SFrank Mandarino * @socdev: the SoC audio device 1500db2a4165SFrank Mandarino * 1501db2a4165SFrank Mandarino * Frees sound card and pcms associated with the socdev. 1502db2a4165SFrank Mandarino * Also unregister the codec if it is an AC97 device. 1503db2a4165SFrank Mandarino */ 1504db2a4165SFrank Mandarino void snd_soc_free_pcms(struct snd_soc_device *socdev) 1505db2a4165SFrank Mandarino { 15066627a653SMark Brown struct snd_soc_codec *codec = socdev->card->codec; 1507a68660e0SLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS 15083c4b266fSLiam Girdwood struct snd_soc_dai *codec_dai; 1509a68660e0SLiam Girdwood int i; 1510a68660e0SLiam Girdwood #endif 1511db2a4165SFrank Mandarino 1512db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 15136627a653SMark Brown soc_cleanup_codec_debugfs(codec); 1514db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS 1515a68660e0SLiam Girdwood for (i = 0; i < codec->num_dai; i++) { 1516a68660e0SLiam Girdwood codec_dai = &codec->dai[i]; 1517d2314e0eSAtsushi Nemoto if (codec_dai->ac97_control && codec->ac97 && 1518d2314e0eSAtsushi Nemoto strcmp(codec->name, "AC97") != 0) { 1519db2a4165SFrank Mandarino soc_ac97_dev_unregister(codec); 1520a68660e0SLiam Girdwood goto free_card; 1521a68660e0SLiam Girdwood } 1522a68660e0SLiam Girdwood } 1523a68660e0SLiam Girdwood free_card: 1524db2a4165SFrank Mandarino #endif 1525db2a4165SFrank Mandarino 1526db2a4165SFrank Mandarino if (codec->card) 1527db2a4165SFrank Mandarino snd_card_free(codec->card); 1528db2a4165SFrank Mandarino device_remove_file(socdev->dev, &dev_attr_codec_reg); 1529db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 1530db2a4165SFrank Mandarino } 1531db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_pcms); 1532db2a4165SFrank Mandarino 1533db2a4165SFrank Mandarino /** 1534db2a4165SFrank Mandarino * snd_soc_set_runtime_hwparams - set the runtime hardware parameters 1535db2a4165SFrank Mandarino * @substream: the pcm substream 1536db2a4165SFrank Mandarino * @hw: the hardware parameters 1537db2a4165SFrank Mandarino * 1538db2a4165SFrank Mandarino * Sets the substream runtime hardware parameters. 1539db2a4165SFrank Mandarino */ 1540db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, 1541db2a4165SFrank Mandarino const struct snd_pcm_hardware *hw) 1542db2a4165SFrank Mandarino { 1543db2a4165SFrank Mandarino struct snd_pcm_runtime *runtime = substream->runtime; 1544db2a4165SFrank Mandarino runtime->hw.info = hw->info; 1545db2a4165SFrank Mandarino runtime->hw.formats = hw->formats; 1546db2a4165SFrank Mandarino runtime->hw.period_bytes_min = hw->period_bytes_min; 1547db2a4165SFrank Mandarino runtime->hw.period_bytes_max = hw->period_bytes_max; 1548db2a4165SFrank Mandarino runtime->hw.periods_min = hw->periods_min; 1549db2a4165SFrank Mandarino runtime->hw.periods_max = hw->periods_max; 1550db2a4165SFrank Mandarino runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; 1551db2a4165SFrank Mandarino runtime->hw.fifo_size = hw->fifo_size; 1552db2a4165SFrank Mandarino return 0; 1553db2a4165SFrank Mandarino } 1554db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); 1555db2a4165SFrank Mandarino 1556db2a4165SFrank Mandarino /** 1557db2a4165SFrank Mandarino * snd_soc_cnew - create new control 1558db2a4165SFrank Mandarino * @_template: control template 1559db2a4165SFrank Mandarino * @data: control private data 1560ac11a2b3SMark Brown * @long_name: control long name 1561db2a4165SFrank Mandarino * 1562db2a4165SFrank Mandarino * Create a new mixer control from a template control. 1563db2a4165SFrank Mandarino * 1564db2a4165SFrank Mandarino * Returns 0 for success, else error. 1565db2a4165SFrank Mandarino */ 1566db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, 1567db2a4165SFrank Mandarino void *data, char *long_name) 1568db2a4165SFrank Mandarino { 1569db2a4165SFrank Mandarino struct snd_kcontrol_new template; 1570db2a4165SFrank Mandarino 1571db2a4165SFrank Mandarino memcpy(&template, _template, sizeof(template)); 1572db2a4165SFrank Mandarino if (long_name) 1573db2a4165SFrank Mandarino template.name = long_name; 1574db2a4165SFrank Mandarino template.index = 0; 1575db2a4165SFrank Mandarino 1576db2a4165SFrank Mandarino return snd_ctl_new1(&template, data); 1577db2a4165SFrank Mandarino } 1578db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew); 1579db2a4165SFrank Mandarino 1580db2a4165SFrank Mandarino /** 15813e8e1952SIan Molton * snd_soc_add_controls - add an array of controls to a codec. 15823e8e1952SIan Molton * Convienience function to add a list of controls. Many codecs were 15833e8e1952SIan Molton * duplicating this code. 15843e8e1952SIan Molton * 15853e8e1952SIan Molton * @codec: codec to add controls to 15863e8e1952SIan Molton * @controls: array of controls to add 15873e8e1952SIan Molton * @num_controls: number of elements in the array 15883e8e1952SIan Molton * 15893e8e1952SIan Molton * Return 0 for success, else error. 15903e8e1952SIan Molton */ 15913e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec, 15923e8e1952SIan Molton const struct snd_kcontrol_new *controls, int num_controls) 15933e8e1952SIan Molton { 15943e8e1952SIan Molton struct snd_card *card = codec->card; 15953e8e1952SIan Molton int err, i; 15963e8e1952SIan Molton 15973e8e1952SIan Molton for (i = 0; i < num_controls; i++) { 15983e8e1952SIan Molton const struct snd_kcontrol_new *control = &controls[i]; 15993e8e1952SIan Molton err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL)); 16003e8e1952SIan Molton if (err < 0) { 16013e8e1952SIan Molton dev_err(codec->dev, "%s: Failed to add %s\n", 16023e8e1952SIan Molton codec->name, control->name); 16033e8e1952SIan Molton return err; 16043e8e1952SIan Molton } 16053e8e1952SIan Molton } 16063e8e1952SIan Molton 16073e8e1952SIan Molton return 0; 16083e8e1952SIan Molton } 16093e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls); 16103e8e1952SIan Molton 16113e8e1952SIan Molton /** 1612db2a4165SFrank Mandarino * snd_soc_info_enum_double - enumerated double mixer info callback 1613db2a4165SFrank Mandarino * @kcontrol: mixer control 1614db2a4165SFrank Mandarino * @uinfo: control element information 1615db2a4165SFrank Mandarino * 1616db2a4165SFrank Mandarino * Callback to provide information about a double enumerated 1617db2a4165SFrank Mandarino * mixer control. 1618db2a4165SFrank Mandarino * 1619db2a4165SFrank Mandarino * Returns 0 for success. 1620db2a4165SFrank Mandarino */ 1621db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol, 1622db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 1623db2a4165SFrank Mandarino { 1624db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 1625db2a4165SFrank Mandarino 1626db2a4165SFrank Mandarino uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 1627db2a4165SFrank Mandarino uinfo->count = e->shift_l == e->shift_r ? 1 : 2; 1628f8ba0b7bSJon Smirl uinfo->value.enumerated.items = e->max; 1629db2a4165SFrank Mandarino 1630f8ba0b7bSJon Smirl if (uinfo->value.enumerated.item > e->max - 1) 1631f8ba0b7bSJon Smirl uinfo->value.enumerated.item = e->max - 1; 1632db2a4165SFrank Mandarino strcpy(uinfo->value.enumerated.name, 1633db2a4165SFrank Mandarino e->texts[uinfo->value.enumerated.item]); 1634db2a4165SFrank Mandarino return 0; 1635db2a4165SFrank Mandarino } 1636db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double); 1637db2a4165SFrank Mandarino 1638db2a4165SFrank Mandarino /** 1639db2a4165SFrank Mandarino * snd_soc_get_enum_double - enumerated double mixer get callback 1640db2a4165SFrank Mandarino * @kcontrol: mixer control 1641ac11a2b3SMark Brown * @ucontrol: control element information 1642db2a4165SFrank Mandarino * 1643db2a4165SFrank Mandarino * Callback to get the value of a double enumerated mixer. 1644db2a4165SFrank Mandarino * 1645db2a4165SFrank Mandarino * Returns 0 for success. 1646db2a4165SFrank Mandarino */ 1647db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol, 1648db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 1649db2a4165SFrank Mandarino { 1650db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 1651db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 165246f5822fSDaniel Ribeiro unsigned int val, bitmask; 1653db2a4165SFrank Mandarino 1654f8ba0b7bSJon Smirl for (bitmask = 1; bitmask < e->max; bitmask <<= 1) 1655db2a4165SFrank Mandarino ; 1656db2a4165SFrank Mandarino val = snd_soc_read(codec, e->reg); 16573ff3f64bSMark Brown ucontrol->value.enumerated.item[0] 16583ff3f64bSMark Brown = (val >> e->shift_l) & (bitmask - 1); 1659db2a4165SFrank Mandarino if (e->shift_l != e->shift_r) 1660db2a4165SFrank Mandarino ucontrol->value.enumerated.item[1] = 1661db2a4165SFrank Mandarino (val >> e->shift_r) & (bitmask - 1); 1662db2a4165SFrank Mandarino 1663db2a4165SFrank Mandarino return 0; 1664db2a4165SFrank Mandarino } 1665db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double); 1666db2a4165SFrank Mandarino 1667db2a4165SFrank Mandarino /** 1668db2a4165SFrank Mandarino * snd_soc_put_enum_double - enumerated double mixer put callback 1669db2a4165SFrank Mandarino * @kcontrol: mixer control 1670ac11a2b3SMark Brown * @ucontrol: control element information 1671db2a4165SFrank Mandarino * 1672db2a4165SFrank Mandarino * Callback to set the value of a double enumerated mixer. 1673db2a4165SFrank Mandarino * 1674db2a4165SFrank Mandarino * Returns 0 for success. 1675db2a4165SFrank Mandarino */ 1676db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol, 1677db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 1678db2a4165SFrank Mandarino { 1679db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 1680db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 168146f5822fSDaniel Ribeiro unsigned int val; 168246f5822fSDaniel Ribeiro unsigned int mask, bitmask; 1683db2a4165SFrank Mandarino 1684f8ba0b7bSJon Smirl for (bitmask = 1; bitmask < e->max; bitmask <<= 1) 1685db2a4165SFrank Mandarino ; 1686f8ba0b7bSJon Smirl if (ucontrol->value.enumerated.item[0] > e->max - 1) 1687db2a4165SFrank Mandarino return -EINVAL; 1688db2a4165SFrank Mandarino val = ucontrol->value.enumerated.item[0] << e->shift_l; 1689db2a4165SFrank Mandarino mask = (bitmask - 1) << e->shift_l; 1690db2a4165SFrank Mandarino if (e->shift_l != e->shift_r) { 1691f8ba0b7bSJon Smirl if (ucontrol->value.enumerated.item[1] > e->max - 1) 1692db2a4165SFrank Mandarino return -EINVAL; 1693db2a4165SFrank Mandarino val |= ucontrol->value.enumerated.item[1] << e->shift_r; 1694db2a4165SFrank Mandarino mask |= (bitmask - 1) << e->shift_r; 1695db2a4165SFrank Mandarino } 1696db2a4165SFrank Mandarino 1697db2a4165SFrank Mandarino return snd_soc_update_bits(codec, e->reg, mask, val); 1698db2a4165SFrank Mandarino } 1699db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double); 1700db2a4165SFrank Mandarino 1701db2a4165SFrank Mandarino /** 17022e72f8e3SPeter Ujfalusi * snd_soc_get_value_enum_double - semi enumerated double mixer get callback 17032e72f8e3SPeter Ujfalusi * @kcontrol: mixer control 17042e72f8e3SPeter Ujfalusi * @ucontrol: control element information 17052e72f8e3SPeter Ujfalusi * 17062e72f8e3SPeter Ujfalusi * Callback to get the value of a double semi enumerated mixer. 17072e72f8e3SPeter Ujfalusi * 17082e72f8e3SPeter Ujfalusi * Semi enumerated mixer: the enumerated items are referred as values. Can be 17092e72f8e3SPeter Ujfalusi * used for handling bitfield coded enumeration for example. 17102e72f8e3SPeter Ujfalusi * 17112e72f8e3SPeter Ujfalusi * Returns 0 for success. 17122e72f8e3SPeter Ujfalusi */ 17132e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol, 17142e72f8e3SPeter Ujfalusi struct snd_ctl_elem_value *ucontrol) 17152e72f8e3SPeter Ujfalusi { 17162e72f8e3SPeter Ujfalusi struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 171774155556SPeter Ujfalusi struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 171846f5822fSDaniel Ribeiro unsigned int reg_val, val, mux; 17192e72f8e3SPeter Ujfalusi 17202e72f8e3SPeter Ujfalusi reg_val = snd_soc_read(codec, e->reg); 17212e72f8e3SPeter Ujfalusi val = (reg_val >> e->shift_l) & e->mask; 17222e72f8e3SPeter Ujfalusi for (mux = 0; mux < e->max; mux++) { 17232e72f8e3SPeter Ujfalusi if (val == e->values[mux]) 17242e72f8e3SPeter Ujfalusi break; 17252e72f8e3SPeter Ujfalusi } 17262e72f8e3SPeter Ujfalusi ucontrol->value.enumerated.item[0] = mux; 17272e72f8e3SPeter Ujfalusi if (e->shift_l != e->shift_r) { 17282e72f8e3SPeter Ujfalusi val = (reg_val >> e->shift_r) & e->mask; 17292e72f8e3SPeter Ujfalusi for (mux = 0; mux < e->max; mux++) { 17302e72f8e3SPeter Ujfalusi if (val == e->values[mux]) 17312e72f8e3SPeter Ujfalusi break; 17322e72f8e3SPeter Ujfalusi } 17332e72f8e3SPeter Ujfalusi ucontrol->value.enumerated.item[1] = mux; 17342e72f8e3SPeter Ujfalusi } 17352e72f8e3SPeter Ujfalusi 17362e72f8e3SPeter Ujfalusi return 0; 17372e72f8e3SPeter Ujfalusi } 17382e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double); 17392e72f8e3SPeter Ujfalusi 17402e72f8e3SPeter Ujfalusi /** 17412e72f8e3SPeter Ujfalusi * snd_soc_put_value_enum_double - semi enumerated double mixer put callback 17422e72f8e3SPeter Ujfalusi * @kcontrol: mixer control 17432e72f8e3SPeter Ujfalusi * @ucontrol: control element information 17442e72f8e3SPeter Ujfalusi * 17452e72f8e3SPeter Ujfalusi * Callback to set the value of a double semi enumerated mixer. 17462e72f8e3SPeter Ujfalusi * 17472e72f8e3SPeter Ujfalusi * Semi enumerated mixer: the enumerated items are referred as values. Can be 17482e72f8e3SPeter Ujfalusi * used for handling bitfield coded enumeration for example. 17492e72f8e3SPeter Ujfalusi * 17502e72f8e3SPeter Ujfalusi * Returns 0 for success. 17512e72f8e3SPeter Ujfalusi */ 17522e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol, 17532e72f8e3SPeter Ujfalusi struct snd_ctl_elem_value *ucontrol) 17542e72f8e3SPeter Ujfalusi { 17552e72f8e3SPeter Ujfalusi struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 175674155556SPeter Ujfalusi struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 175746f5822fSDaniel Ribeiro unsigned int val; 175846f5822fSDaniel Ribeiro unsigned int mask; 17592e72f8e3SPeter Ujfalusi 17602e72f8e3SPeter Ujfalusi if (ucontrol->value.enumerated.item[0] > e->max - 1) 17612e72f8e3SPeter Ujfalusi return -EINVAL; 17622e72f8e3SPeter Ujfalusi val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l; 17632e72f8e3SPeter Ujfalusi mask = e->mask << e->shift_l; 17642e72f8e3SPeter Ujfalusi if (e->shift_l != e->shift_r) { 17652e72f8e3SPeter Ujfalusi if (ucontrol->value.enumerated.item[1] > e->max - 1) 17662e72f8e3SPeter Ujfalusi return -EINVAL; 17672e72f8e3SPeter Ujfalusi val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r; 17682e72f8e3SPeter Ujfalusi mask |= e->mask << e->shift_r; 17692e72f8e3SPeter Ujfalusi } 17702e72f8e3SPeter Ujfalusi 17712e72f8e3SPeter Ujfalusi return snd_soc_update_bits(codec, e->reg, mask, val); 17722e72f8e3SPeter Ujfalusi } 17732e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double); 17742e72f8e3SPeter Ujfalusi 17752e72f8e3SPeter Ujfalusi /** 1776db2a4165SFrank Mandarino * snd_soc_info_enum_ext - external enumerated single mixer info callback 1777db2a4165SFrank Mandarino * @kcontrol: mixer control 1778db2a4165SFrank Mandarino * @uinfo: control element information 1779db2a4165SFrank Mandarino * 1780db2a4165SFrank Mandarino * Callback to provide information about an external enumerated 1781db2a4165SFrank Mandarino * single mixer. 1782db2a4165SFrank Mandarino * 1783db2a4165SFrank Mandarino * Returns 0 for success. 1784db2a4165SFrank Mandarino */ 1785db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol, 1786db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 1787db2a4165SFrank Mandarino { 1788db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 1789db2a4165SFrank Mandarino 1790db2a4165SFrank Mandarino uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 1791db2a4165SFrank Mandarino uinfo->count = 1; 1792f8ba0b7bSJon Smirl uinfo->value.enumerated.items = e->max; 1793db2a4165SFrank Mandarino 1794f8ba0b7bSJon Smirl if (uinfo->value.enumerated.item > e->max - 1) 1795f8ba0b7bSJon Smirl uinfo->value.enumerated.item = e->max - 1; 1796db2a4165SFrank Mandarino strcpy(uinfo->value.enumerated.name, 1797db2a4165SFrank Mandarino e->texts[uinfo->value.enumerated.item]); 1798db2a4165SFrank Mandarino return 0; 1799db2a4165SFrank Mandarino } 1800db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext); 1801db2a4165SFrank Mandarino 1802db2a4165SFrank Mandarino /** 1803db2a4165SFrank Mandarino * snd_soc_info_volsw_ext - external single mixer info callback 1804db2a4165SFrank Mandarino * @kcontrol: mixer control 1805db2a4165SFrank Mandarino * @uinfo: control element information 1806db2a4165SFrank Mandarino * 1807db2a4165SFrank Mandarino * Callback to provide information about a single external mixer control. 1808db2a4165SFrank Mandarino * 1809db2a4165SFrank Mandarino * Returns 0 for success. 1810db2a4165SFrank Mandarino */ 1811db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol, 1812db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 1813db2a4165SFrank Mandarino { 1814a7a4ac86SPhilipp Zabel int max = kcontrol->private_value; 1815db2a4165SFrank Mandarino 1816fd5dfad9SMark Brown if (max == 1 && !strstr(kcontrol->id.name, " Volume")) 1817a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 1818a7a4ac86SPhilipp Zabel else 1819a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1820a7a4ac86SPhilipp Zabel 1821db2a4165SFrank Mandarino uinfo->count = 1; 1822db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 1823a7a4ac86SPhilipp Zabel uinfo->value.integer.max = max; 1824db2a4165SFrank Mandarino return 0; 1825db2a4165SFrank Mandarino } 1826db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext); 1827db2a4165SFrank Mandarino 1828db2a4165SFrank Mandarino /** 1829db2a4165SFrank Mandarino * snd_soc_info_volsw - single mixer info callback 1830db2a4165SFrank Mandarino * @kcontrol: mixer control 1831db2a4165SFrank Mandarino * @uinfo: control element information 1832db2a4165SFrank Mandarino * 1833db2a4165SFrank Mandarino * Callback to provide information about a single mixer control. 1834db2a4165SFrank Mandarino * 1835db2a4165SFrank Mandarino * Returns 0 for success. 1836db2a4165SFrank Mandarino */ 1837db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol, 1838db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 1839db2a4165SFrank Mandarino { 18404eaa9819SJon Smirl struct soc_mixer_control *mc = 18414eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 18424eaa9819SJon Smirl int max = mc->max; 1843762b8df7SMark Brown unsigned int shift = mc->shift; 1844815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 1845db2a4165SFrank Mandarino 1846fd5dfad9SMark Brown if (max == 1 && !strstr(kcontrol->id.name, " Volume")) 1847a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 1848a7a4ac86SPhilipp Zabel else 1849a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1850a7a4ac86SPhilipp Zabel 1851db2a4165SFrank Mandarino uinfo->count = shift == rshift ? 1 : 2; 1852db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 1853a7a4ac86SPhilipp Zabel uinfo->value.integer.max = max; 1854db2a4165SFrank Mandarino return 0; 1855db2a4165SFrank Mandarino } 1856db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw); 1857db2a4165SFrank Mandarino 1858db2a4165SFrank Mandarino /** 1859db2a4165SFrank Mandarino * snd_soc_get_volsw - single mixer get callback 1860db2a4165SFrank Mandarino * @kcontrol: mixer control 1861ac11a2b3SMark Brown * @ucontrol: control element information 1862db2a4165SFrank Mandarino * 1863db2a4165SFrank Mandarino * Callback to get the value of a single mixer control. 1864db2a4165SFrank Mandarino * 1865db2a4165SFrank Mandarino * Returns 0 for success. 1866db2a4165SFrank Mandarino */ 1867db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol, 1868db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 1869db2a4165SFrank Mandarino { 18704eaa9819SJon Smirl struct soc_mixer_control *mc = 18714eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 1872db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 1873815ecf8dSJon Smirl unsigned int reg = mc->reg; 1874815ecf8dSJon Smirl unsigned int shift = mc->shift; 1875815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 18764eaa9819SJon Smirl int max = mc->max; 1877815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 1878815ecf8dSJon Smirl unsigned int invert = mc->invert; 1879db2a4165SFrank Mandarino 1880db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 1881db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> shift) & mask; 1882db2a4165SFrank Mandarino if (shift != rshift) 1883db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 1884db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> rshift) & mask; 1885db2a4165SFrank Mandarino if (invert) { 1886db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 1887a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[0]; 1888db2a4165SFrank Mandarino if (shift != rshift) 1889db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 1890a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[1]; 1891db2a4165SFrank Mandarino } 1892db2a4165SFrank Mandarino 1893db2a4165SFrank Mandarino return 0; 1894db2a4165SFrank Mandarino } 1895db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw); 1896db2a4165SFrank Mandarino 1897db2a4165SFrank Mandarino /** 1898db2a4165SFrank Mandarino * snd_soc_put_volsw - single mixer put callback 1899db2a4165SFrank Mandarino * @kcontrol: mixer control 1900ac11a2b3SMark Brown * @ucontrol: control element information 1901db2a4165SFrank Mandarino * 1902db2a4165SFrank Mandarino * Callback to set the value of a single mixer control. 1903db2a4165SFrank Mandarino * 1904db2a4165SFrank Mandarino * Returns 0 for success. 1905db2a4165SFrank Mandarino */ 1906db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol, 1907db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 1908db2a4165SFrank Mandarino { 19094eaa9819SJon Smirl struct soc_mixer_control *mc = 19104eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 1911db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 1912815ecf8dSJon Smirl unsigned int reg = mc->reg; 1913815ecf8dSJon Smirl unsigned int shift = mc->shift; 1914815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 19154eaa9819SJon Smirl int max = mc->max; 1916815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 1917815ecf8dSJon Smirl unsigned int invert = mc->invert; 191846f5822fSDaniel Ribeiro unsigned int val, val2, val_mask; 1919db2a4165SFrank Mandarino 1920db2a4165SFrank Mandarino val = (ucontrol->value.integer.value[0] & mask); 1921db2a4165SFrank Mandarino if (invert) 1922a7a4ac86SPhilipp Zabel val = max - val; 1923db2a4165SFrank Mandarino val_mask = mask << shift; 1924db2a4165SFrank Mandarino val = val << shift; 1925db2a4165SFrank Mandarino if (shift != rshift) { 1926db2a4165SFrank Mandarino val2 = (ucontrol->value.integer.value[1] & mask); 1927db2a4165SFrank Mandarino if (invert) 1928a7a4ac86SPhilipp Zabel val2 = max - val2; 1929db2a4165SFrank Mandarino val_mask |= mask << rshift; 1930db2a4165SFrank Mandarino val |= val2 << rshift; 1931db2a4165SFrank Mandarino } 1932a7a4ac86SPhilipp Zabel return snd_soc_update_bits(codec, reg, val_mask, val); 1933db2a4165SFrank Mandarino } 1934db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw); 1935db2a4165SFrank Mandarino 1936db2a4165SFrank Mandarino /** 1937db2a4165SFrank Mandarino * snd_soc_info_volsw_2r - double mixer info callback 1938db2a4165SFrank Mandarino * @kcontrol: mixer control 1939db2a4165SFrank Mandarino * @uinfo: control element information 1940db2a4165SFrank Mandarino * 1941db2a4165SFrank Mandarino * Callback to provide information about a double mixer control that 1942db2a4165SFrank Mandarino * spans 2 codec registers. 1943db2a4165SFrank Mandarino * 1944db2a4165SFrank Mandarino * Returns 0 for success. 1945db2a4165SFrank Mandarino */ 1946db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol, 1947db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 1948db2a4165SFrank Mandarino { 19494eaa9819SJon Smirl struct soc_mixer_control *mc = 19504eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 19514eaa9819SJon Smirl int max = mc->max; 1952db2a4165SFrank Mandarino 1953fd5dfad9SMark Brown if (max == 1 && !strstr(kcontrol->id.name, " Volume")) 1954a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 1955a7a4ac86SPhilipp Zabel else 1956a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1957a7a4ac86SPhilipp Zabel 1958db2a4165SFrank Mandarino uinfo->count = 2; 1959db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 1960a7a4ac86SPhilipp Zabel uinfo->value.integer.max = max; 1961db2a4165SFrank Mandarino return 0; 1962db2a4165SFrank Mandarino } 1963db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r); 1964db2a4165SFrank Mandarino 1965db2a4165SFrank Mandarino /** 1966db2a4165SFrank Mandarino * snd_soc_get_volsw_2r - double mixer get callback 1967db2a4165SFrank Mandarino * @kcontrol: mixer control 1968ac11a2b3SMark Brown * @ucontrol: control element information 1969db2a4165SFrank Mandarino * 1970db2a4165SFrank Mandarino * Callback to get the value of a double mixer control that spans 2 registers. 1971db2a4165SFrank Mandarino * 1972db2a4165SFrank Mandarino * Returns 0 for success. 1973db2a4165SFrank Mandarino */ 1974db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol, 1975db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 1976db2a4165SFrank Mandarino { 19774eaa9819SJon Smirl struct soc_mixer_control *mc = 19784eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 1979db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 1980815ecf8dSJon Smirl unsigned int reg = mc->reg; 1981815ecf8dSJon Smirl unsigned int reg2 = mc->rreg; 1982815ecf8dSJon Smirl unsigned int shift = mc->shift; 19834eaa9819SJon Smirl int max = mc->max; 1984815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 1985815ecf8dSJon Smirl unsigned int invert = mc->invert; 1986db2a4165SFrank Mandarino 1987db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 1988db2a4165SFrank Mandarino (snd_soc_read(codec, reg) >> shift) & mask; 1989db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 1990db2a4165SFrank Mandarino (snd_soc_read(codec, reg2) >> shift) & mask; 1991db2a4165SFrank Mandarino if (invert) { 1992db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 1993a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[0]; 1994db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 1995a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[1]; 1996db2a4165SFrank Mandarino } 1997db2a4165SFrank Mandarino 1998db2a4165SFrank Mandarino return 0; 1999db2a4165SFrank Mandarino } 2000db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r); 2001db2a4165SFrank Mandarino 2002db2a4165SFrank Mandarino /** 2003db2a4165SFrank Mandarino * snd_soc_put_volsw_2r - double mixer set callback 2004db2a4165SFrank Mandarino * @kcontrol: mixer control 2005ac11a2b3SMark Brown * @ucontrol: control element information 2006db2a4165SFrank Mandarino * 2007db2a4165SFrank Mandarino * Callback to set the value of a double mixer control that spans 2 registers. 2008db2a4165SFrank Mandarino * 2009db2a4165SFrank Mandarino * Returns 0 for success. 2010db2a4165SFrank Mandarino */ 2011db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol, 2012db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2013db2a4165SFrank Mandarino { 20144eaa9819SJon Smirl struct soc_mixer_control *mc = 20154eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2016db2a4165SFrank Mandarino struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2017815ecf8dSJon Smirl unsigned int reg = mc->reg; 2018815ecf8dSJon Smirl unsigned int reg2 = mc->rreg; 2019815ecf8dSJon Smirl unsigned int shift = mc->shift; 20204eaa9819SJon Smirl int max = mc->max; 2021815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2022815ecf8dSJon Smirl unsigned int invert = mc->invert; 2023db2a4165SFrank Mandarino int err; 202446f5822fSDaniel Ribeiro unsigned int val, val2, val_mask; 2025db2a4165SFrank Mandarino 2026db2a4165SFrank Mandarino val_mask = mask << shift; 2027db2a4165SFrank Mandarino val = (ucontrol->value.integer.value[0] & mask); 2028db2a4165SFrank Mandarino val2 = (ucontrol->value.integer.value[1] & mask); 2029db2a4165SFrank Mandarino 2030db2a4165SFrank Mandarino if (invert) { 2031a7a4ac86SPhilipp Zabel val = max - val; 2032a7a4ac86SPhilipp Zabel val2 = max - val2; 2033db2a4165SFrank Mandarino } 2034db2a4165SFrank Mandarino 2035db2a4165SFrank Mandarino val = val << shift; 2036db2a4165SFrank Mandarino val2 = val2 << shift; 2037db2a4165SFrank Mandarino 20383ff3f64bSMark Brown err = snd_soc_update_bits(codec, reg, val_mask, val); 20393ff3f64bSMark Brown if (err < 0) 2040db2a4165SFrank Mandarino return err; 2041db2a4165SFrank Mandarino 2042db2a4165SFrank Mandarino err = snd_soc_update_bits(codec, reg2, val_mask, val2); 2043db2a4165SFrank Mandarino return err; 2044db2a4165SFrank Mandarino } 2045db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r); 2046db2a4165SFrank Mandarino 2047e13ac2e9SMark Brown /** 2048e13ac2e9SMark Brown * snd_soc_info_volsw_s8 - signed mixer info callback 2049e13ac2e9SMark Brown * @kcontrol: mixer control 2050e13ac2e9SMark Brown * @uinfo: control element information 2051e13ac2e9SMark Brown * 2052e13ac2e9SMark Brown * Callback to provide information about a signed mixer control. 2053e13ac2e9SMark Brown * 2054e13ac2e9SMark Brown * Returns 0 for success. 2055e13ac2e9SMark Brown */ 2056e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol, 2057e13ac2e9SMark Brown struct snd_ctl_elem_info *uinfo) 2058e13ac2e9SMark Brown { 20594eaa9819SJon Smirl struct soc_mixer_control *mc = 20604eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 20614eaa9819SJon Smirl int max = mc->max; 20624eaa9819SJon Smirl int min = mc->min; 2063e13ac2e9SMark Brown 2064e13ac2e9SMark Brown uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2065e13ac2e9SMark Brown uinfo->count = 2; 2066e13ac2e9SMark Brown uinfo->value.integer.min = 0; 2067e13ac2e9SMark Brown uinfo->value.integer.max = max-min; 2068e13ac2e9SMark Brown return 0; 2069e13ac2e9SMark Brown } 2070e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8); 2071e13ac2e9SMark Brown 2072e13ac2e9SMark Brown /** 2073e13ac2e9SMark Brown * snd_soc_get_volsw_s8 - signed mixer get callback 2074e13ac2e9SMark Brown * @kcontrol: mixer control 2075ac11a2b3SMark Brown * @ucontrol: control element information 2076e13ac2e9SMark Brown * 2077e13ac2e9SMark Brown * Callback to get the value of a signed mixer control. 2078e13ac2e9SMark Brown * 2079e13ac2e9SMark Brown * Returns 0 for success. 2080e13ac2e9SMark Brown */ 2081e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol, 2082e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 2083e13ac2e9SMark Brown { 20844eaa9819SJon Smirl struct soc_mixer_control *mc = 20854eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2086e13ac2e9SMark Brown struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2087815ecf8dSJon Smirl unsigned int reg = mc->reg; 20884eaa9819SJon Smirl int min = mc->min; 2089e13ac2e9SMark Brown int val = snd_soc_read(codec, reg); 2090e13ac2e9SMark Brown 2091e13ac2e9SMark Brown ucontrol->value.integer.value[0] = 2092e13ac2e9SMark Brown ((signed char)(val & 0xff))-min; 2093e13ac2e9SMark Brown ucontrol->value.integer.value[1] = 2094e13ac2e9SMark Brown ((signed char)((val >> 8) & 0xff))-min; 2095e13ac2e9SMark Brown return 0; 2096e13ac2e9SMark Brown } 2097e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8); 2098e13ac2e9SMark Brown 2099e13ac2e9SMark Brown /** 2100e13ac2e9SMark Brown * snd_soc_put_volsw_sgn - signed mixer put callback 2101e13ac2e9SMark Brown * @kcontrol: mixer control 2102ac11a2b3SMark Brown * @ucontrol: control element information 2103e13ac2e9SMark Brown * 2104e13ac2e9SMark Brown * Callback to set the value of a signed mixer control. 2105e13ac2e9SMark Brown * 2106e13ac2e9SMark Brown * Returns 0 for success. 2107e13ac2e9SMark Brown */ 2108e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol, 2109e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 2110e13ac2e9SMark Brown { 21114eaa9819SJon Smirl struct soc_mixer_control *mc = 21124eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2113e13ac2e9SMark Brown struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 2114815ecf8dSJon Smirl unsigned int reg = mc->reg; 21154eaa9819SJon Smirl int min = mc->min; 211646f5822fSDaniel Ribeiro unsigned int val; 2117e13ac2e9SMark Brown 2118e13ac2e9SMark Brown val = (ucontrol->value.integer.value[0]+min) & 0xff; 2119e13ac2e9SMark Brown val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8; 2120e13ac2e9SMark Brown 2121e13ac2e9SMark Brown return snd_soc_update_bits(codec, reg, 0xffff, val); 2122e13ac2e9SMark Brown } 2123e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8); 2124e13ac2e9SMark Brown 21258c6529dbSLiam Girdwood /** 21268c6529dbSLiam Girdwood * snd_soc_dai_set_sysclk - configure DAI system or master clock. 21278c6529dbSLiam Girdwood * @dai: DAI 21288c6529dbSLiam Girdwood * @clk_id: DAI specific clock ID 21298c6529dbSLiam Girdwood * @freq: new clock frequency in Hz 21308c6529dbSLiam Girdwood * @dir: new clock direction - input/output. 21318c6529dbSLiam Girdwood * 21328c6529dbSLiam Girdwood * Configures the DAI master (MCLK) or system (SYSCLK) clocking. 21338c6529dbSLiam Girdwood */ 21348c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id, 21358c6529dbSLiam Girdwood unsigned int freq, int dir) 21368c6529dbSLiam Girdwood { 21373f1a4d82SMark Brown if (dai->ops && dai->ops->set_sysclk) 21386335d055SEric Miao return dai->ops->set_sysclk(dai, clk_id, freq, dir); 21398c6529dbSLiam Girdwood else 21408c6529dbSLiam Girdwood return -EINVAL; 21418c6529dbSLiam Girdwood } 21428c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk); 21438c6529dbSLiam Girdwood 21448c6529dbSLiam Girdwood /** 21458c6529dbSLiam Girdwood * snd_soc_dai_set_clkdiv - configure DAI clock dividers. 21468c6529dbSLiam Girdwood * @dai: DAI 2147ac11a2b3SMark Brown * @div_id: DAI specific clock divider ID 21488c6529dbSLiam Girdwood * @div: new clock divisor. 21498c6529dbSLiam Girdwood * 21508c6529dbSLiam Girdwood * Configures the clock dividers. This is used to derive the best DAI bit and 21518c6529dbSLiam Girdwood * frame clocks from the system or master clock. It's best to set the DAI bit 21528c6529dbSLiam Girdwood * and frame clocks as low as possible to save system power. 21538c6529dbSLiam Girdwood */ 21548c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai, 21558c6529dbSLiam Girdwood int div_id, int div) 21568c6529dbSLiam Girdwood { 21573f1a4d82SMark Brown if (dai->ops && dai->ops->set_clkdiv) 21586335d055SEric Miao return dai->ops->set_clkdiv(dai, div_id, div); 21598c6529dbSLiam Girdwood else 21608c6529dbSLiam Girdwood return -EINVAL; 21618c6529dbSLiam Girdwood } 21628c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv); 21638c6529dbSLiam Girdwood 21648c6529dbSLiam Girdwood /** 21658c6529dbSLiam Girdwood * snd_soc_dai_set_pll - configure DAI PLL. 21668c6529dbSLiam Girdwood * @dai: DAI 21678c6529dbSLiam Girdwood * @pll_id: DAI specific PLL ID 21688c6529dbSLiam Girdwood * @freq_in: PLL input clock frequency in Hz 21698c6529dbSLiam Girdwood * @freq_out: requested PLL output clock frequency in Hz 21708c6529dbSLiam Girdwood * 21718c6529dbSLiam Girdwood * Configures and enables PLL to generate output clock based on input clock. 21728c6529dbSLiam Girdwood */ 21738c6529dbSLiam Girdwood int snd_soc_dai_set_pll(struct snd_soc_dai *dai, 21748c6529dbSLiam Girdwood int pll_id, unsigned int freq_in, unsigned int freq_out) 21758c6529dbSLiam Girdwood { 21763f1a4d82SMark Brown if (dai->ops && dai->ops->set_pll) 21776335d055SEric Miao return dai->ops->set_pll(dai, pll_id, freq_in, freq_out); 21788c6529dbSLiam Girdwood else 21798c6529dbSLiam Girdwood return -EINVAL; 21808c6529dbSLiam Girdwood } 21818c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll); 21828c6529dbSLiam Girdwood 21838c6529dbSLiam Girdwood /** 21848c6529dbSLiam Girdwood * snd_soc_dai_set_fmt - configure DAI hardware audio format. 21858c6529dbSLiam Girdwood * @dai: DAI 21868c6529dbSLiam Girdwood * @fmt: SND_SOC_DAIFMT_ format value. 21878c6529dbSLiam Girdwood * 21888c6529dbSLiam Girdwood * Configures the DAI hardware format and clocking. 21898c6529dbSLiam Girdwood */ 21908c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 21918c6529dbSLiam Girdwood { 21923f1a4d82SMark Brown if (dai->ops && dai->ops->set_fmt) 21936335d055SEric Miao return dai->ops->set_fmt(dai, fmt); 21948c6529dbSLiam Girdwood else 21958c6529dbSLiam Girdwood return -EINVAL; 21968c6529dbSLiam Girdwood } 21978c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt); 21988c6529dbSLiam Girdwood 21998c6529dbSLiam Girdwood /** 22008c6529dbSLiam Girdwood * snd_soc_dai_set_tdm_slot - configure DAI TDM. 22018c6529dbSLiam Girdwood * @dai: DAI 22028c6529dbSLiam Girdwood * @mask: DAI specific mask representing used slots. 22038c6529dbSLiam Girdwood * @slots: Number of slots in use. 22048c6529dbSLiam Girdwood * 22058c6529dbSLiam Girdwood * Configures a DAI for TDM operation. Both mask and slots are codec and DAI 22068c6529dbSLiam Girdwood * specific. 22078c6529dbSLiam Girdwood */ 22088c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai, 22098c6529dbSLiam Girdwood unsigned int mask, int slots) 22108c6529dbSLiam Girdwood { 22113f1a4d82SMark Brown if (dai->ops && dai->ops->set_tdm_slot) 22126335d055SEric Miao return dai->ops->set_tdm_slot(dai, mask, slots); 22138c6529dbSLiam Girdwood else 22148c6529dbSLiam Girdwood return -EINVAL; 22158c6529dbSLiam Girdwood } 22168c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot); 22178c6529dbSLiam Girdwood 22188c6529dbSLiam Girdwood /** 22198c6529dbSLiam Girdwood * snd_soc_dai_set_tristate - configure DAI system or master clock. 22208c6529dbSLiam Girdwood * @dai: DAI 22218c6529dbSLiam Girdwood * @tristate: tristate enable 22228c6529dbSLiam Girdwood * 22238c6529dbSLiam Girdwood * Tristates the DAI so that others can use it. 22248c6529dbSLiam Girdwood */ 22258c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate) 22268c6529dbSLiam Girdwood { 22273f1a4d82SMark Brown if (dai->ops && dai->ops->set_tristate) 22286335d055SEric Miao return dai->ops->set_tristate(dai, tristate); 22298c6529dbSLiam Girdwood else 22308c6529dbSLiam Girdwood return -EINVAL; 22318c6529dbSLiam Girdwood } 22328c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate); 22338c6529dbSLiam Girdwood 22348c6529dbSLiam Girdwood /** 22358c6529dbSLiam Girdwood * snd_soc_dai_digital_mute - configure DAI system or master clock. 22368c6529dbSLiam Girdwood * @dai: DAI 22378c6529dbSLiam Girdwood * @mute: mute enable 22388c6529dbSLiam Girdwood * 22398c6529dbSLiam Girdwood * Mutes the DAI DAC. 22408c6529dbSLiam Girdwood */ 22418c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute) 22428c6529dbSLiam Girdwood { 22433f1a4d82SMark Brown if (dai->ops && dai->ops->digital_mute) 22446335d055SEric Miao return dai->ops->digital_mute(dai, mute); 22458c6529dbSLiam Girdwood else 22468c6529dbSLiam Girdwood return -EINVAL; 22478c6529dbSLiam Girdwood } 22488c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute); 22498c6529dbSLiam Girdwood 2250c5af3a2eSMark Brown /** 2251c5af3a2eSMark Brown * snd_soc_register_card - Register a card with the ASoC core 2252c5af3a2eSMark Brown * 2253ac11a2b3SMark Brown * @card: Card to register 2254c5af3a2eSMark Brown * 2255c5af3a2eSMark Brown * Note that currently this is an internal only function: it will be 2256c5af3a2eSMark Brown * exposed to machine drivers after further backporting of ASoC v2 2257c5af3a2eSMark Brown * registration APIs. 2258c5af3a2eSMark Brown */ 2259c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card) 2260c5af3a2eSMark Brown { 2261c5af3a2eSMark Brown if (!card->name || !card->dev) 2262c5af3a2eSMark Brown return -EINVAL; 2263c5af3a2eSMark Brown 2264c5af3a2eSMark Brown INIT_LIST_HEAD(&card->list); 2265c5af3a2eSMark Brown card->instantiated = 0; 2266c5af3a2eSMark Brown 2267c5af3a2eSMark Brown mutex_lock(&client_mutex); 2268c5af3a2eSMark Brown list_add(&card->list, &card_list); 2269435c5e25SMark Brown snd_soc_instantiate_cards(); 2270c5af3a2eSMark Brown mutex_unlock(&client_mutex); 2271c5af3a2eSMark Brown 2272c5af3a2eSMark Brown dev_dbg(card->dev, "Registered card '%s'\n", card->name); 2273c5af3a2eSMark Brown 2274c5af3a2eSMark Brown return 0; 2275c5af3a2eSMark Brown } 2276c5af3a2eSMark Brown 2277c5af3a2eSMark Brown /** 2278c5af3a2eSMark Brown * snd_soc_unregister_card - Unregister a card with the ASoC core 2279c5af3a2eSMark Brown * 2280ac11a2b3SMark Brown * @card: Card to unregister 2281c5af3a2eSMark Brown * 2282c5af3a2eSMark Brown * Note that currently this is an internal only function: it will be 2283c5af3a2eSMark Brown * exposed to machine drivers after further backporting of ASoC v2 2284c5af3a2eSMark Brown * registration APIs. 2285c5af3a2eSMark Brown */ 2286c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card) 2287c5af3a2eSMark Brown { 2288c5af3a2eSMark Brown mutex_lock(&client_mutex); 2289c5af3a2eSMark Brown list_del(&card->list); 2290c5af3a2eSMark Brown mutex_unlock(&client_mutex); 2291c5af3a2eSMark Brown 2292c5af3a2eSMark Brown dev_dbg(card->dev, "Unregistered card '%s'\n", card->name); 2293c5af3a2eSMark Brown 2294c5af3a2eSMark Brown return 0; 2295c5af3a2eSMark Brown } 2296c5af3a2eSMark Brown 22976335d055SEric Miao static struct snd_soc_dai_ops null_dai_ops = { 22986335d055SEric Miao }; 22996335d055SEric Miao 23009115171aSMark Brown /** 23019115171aSMark Brown * snd_soc_register_dai - Register a DAI with the ASoC core 23029115171aSMark Brown * 2303ac11a2b3SMark Brown * @dai: DAI to register 23049115171aSMark Brown */ 23059115171aSMark Brown int snd_soc_register_dai(struct snd_soc_dai *dai) 23069115171aSMark Brown { 23079115171aSMark Brown if (!dai->name) 23089115171aSMark Brown return -EINVAL; 23099115171aSMark Brown 23109115171aSMark Brown /* The device should become mandatory over time */ 23119115171aSMark Brown if (!dai->dev) 23129115171aSMark Brown printk(KERN_WARNING "No device for DAI %s\n", dai->name); 23139115171aSMark Brown 23146335d055SEric Miao if (!dai->ops) 23156335d055SEric Miao dai->ops = &null_dai_ops; 23166335d055SEric Miao 23179115171aSMark Brown INIT_LIST_HEAD(&dai->list); 23189115171aSMark Brown 23199115171aSMark Brown mutex_lock(&client_mutex); 23209115171aSMark Brown list_add(&dai->list, &dai_list); 2321435c5e25SMark Brown snd_soc_instantiate_cards(); 23229115171aSMark Brown mutex_unlock(&client_mutex); 23239115171aSMark Brown 23249115171aSMark Brown pr_debug("Registered DAI '%s'\n", dai->name); 23259115171aSMark Brown 23269115171aSMark Brown return 0; 23279115171aSMark Brown } 23289115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai); 23299115171aSMark Brown 23309115171aSMark Brown /** 23319115171aSMark Brown * snd_soc_unregister_dai - Unregister a DAI from the ASoC core 23329115171aSMark Brown * 2333ac11a2b3SMark Brown * @dai: DAI to unregister 23349115171aSMark Brown */ 23359115171aSMark Brown void snd_soc_unregister_dai(struct snd_soc_dai *dai) 23369115171aSMark Brown { 23379115171aSMark Brown mutex_lock(&client_mutex); 23389115171aSMark Brown list_del(&dai->list); 23399115171aSMark Brown mutex_unlock(&client_mutex); 23409115171aSMark Brown 23419115171aSMark Brown pr_debug("Unregistered DAI '%s'\n", dai->name); 23429115171aSMark Brown } 23439115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai); 23449115171aSMark Brown 23459115171aSMark Brown /** 23469115171aSMark Brown * snd_soc_register_dais - Register multiple DAIs with the ASoC core 23479115171aSMark Brown * 2348ac11a2b3SMark Brown * @dai: Array of DAIs to register 2349ac11a2b3SMark Brown * @count: Number of DAIs 23509115171aSMark Brown */ 23519115171aSMark Brown int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count) 23529115171aSMark Brown { 23539115171aSMark Brown int i, ret; 23549115171aSMark Brown 23559115171aSMark Brown for (i = 0; i < count; i++) { 23569115171aSMark Brown ret = snd_soc_register_dai(&dai[i]); 23579115171aSMark Brown if (ret != 0) 23589115171aSMark Brown goto err; 23599115171aSMark Brown } 23609115171aSMark Brown 23619115171aSMark Brown return 0; 23629115171aSMark Brown 23639115171aSMark Brown err: 23649115171aSMark Brown for (i--; i >= 0; i--) 23659115171aSMark Brown snd_soc_unregister_dai(&dai[i]); 23669115171aSMark Brown 23679115171aSMark Brown return ret; 23689115171aSMark Brown } 23699115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais); 23709115171aSMark Brown 23719115171aSMark Brown /** 23729115171aSMark Brown * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core 23739115171aSMark Brown * 2374ac11a2b3SMark Brown * @dai: Array of DAIs to unregister 2375ac11a2b3SMark Brown * @count: Number of DAIs 23769115171aSMark Brown */ 23779115171aSMark Brown void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count) 23789115171aSMark Brown { 23799115171aSMark Brown int i; 23809115171aSMark Brown 23819115171aSMark Brown for (i = 0; i < count; i++) 23829115171aSMark Brown snd_soc_unregister_dai(&dai[i]); 23839115171aSMark Brown } 23849115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais); 23859115171aSMark Brown 238612a48a8cSMark Brown /** 238712a48a8cSMark Brown * snd_soc_register_platform - Register a platform with the ASoC core 238812a48a8cSMark Brown * 2389ac11a2b3SMark Brown * @platform: platform to register 239012a48a8cSMark Brown */ 239112a48a8cSMark Brown int snd_soc_register_platform(struct snd_soc_platform *platform) 239212a48a8cSMark Brown { 239312a48a8cSMark Brown if (!platform->name) 239412a48a8cSMark Brown return -EINVAL; 239512a48a8cSMark Brown 239612a48a8cSMark Brown INIT_LIST_HEAD(&platform->list); 239712a48a8cSMark Brown 239812a48a8cSMark Brown mutex_lock(&client_mutex); 239912a48a8cSMark Brown list_add(&platform->list, &platform_list); 2400435c5e25SMark Brown snd_soc_instantiate_cards(); 240112a48a8cSMark Brown mutex_unlock(&client_mutex); 240212a48a8cSMark Brown 240312a48a8cSMark Brown pr_debug("Registered platform '%s'\n", platform->name); 240412a48a8cSMark Brown 240512a48a8cSMark Brown return 0; 240612a48a8cSMark Brown } 240712a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform); 240812a48a8cSMark Brown 240912a48a8cSMark Brown /** 241012a48a8cSMark Brown * snd_soc_unregister_platform - Unregister a platform from the ASoC core 241112a48a8cSMark Brown * 2412ac11a2b3SMark Brown * @platform: platform to unregister 241312a48a8cSMark Brown */ 241412a48a8cSMark Brown void snd_soc_unregister_platform(struct snd_soc_platform *platform) 241512a48a8cSMark Brown { 241612a48a8cSMark Brown mutex_lock(&client_mutex); 241712a48a8cSMark Brown list_del(&platform->list); 241812a48a8cSMark Brown mutex_unlock(&client_mutex); 241912a48a8cSMark Brown 242012a48a8cSMark Brown pr_debug("Unregistered platform '%s'\n", platform->name); 242112a48a8cSMark Brown } 242212a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform); 242312a48a8cSMark Brown 2424151ab22cSMark Brown static u64 codec_format_map[] = { 2425151ab22cSMark Brown SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE, 2426151ab22cSMark Brown SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE, 2427151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE, 2428151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE, 2429151ab22cSMark Brown SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE, 2430151ab22cSMark Brown SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE, 2431151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 2432151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 2433151ab22cSMark Brown SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE, 2434151ab22cSMark Brown SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE, 2435151ab22cSMark Brown SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE, 2436151ab22cSMark Brown SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE, 2437151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE, 2438151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE, 2439151ab22cSMark Brown SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE 2440151ab22cSMark Brown | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE, 2441151ab22cSMark Brown }; 2442151ab22cSMark Brown 2443151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see 2444151ab22cSMark Brown * the endianness of the data but we're using the CPU format 2445151ab22cSMark Brown * definitions which do need to include endianness so we ensure that 2446151ab22cSMark Brown * codec DAIs always have both big and little endian variants set. 2447151ab22cSMark Brown */ 2448151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream) 2449151ab22cSMark Brown { 2450151ab22cSMark Brown int i; 2451151ab22cSMark Brown 2452151ab22cSMark Brown for (i = 0; i < ARRAY_SIZE(codec_format_map); i++) 2453151ab22cSMark Brown if (stream->formats & codec_format_map[i]) 2454151ab22cSMark Brown stream->formats |= codec_format_map[i]; 2455151ab22cSMark Brown } 2456151ab22cSMark Brown 24570d0cf00aSMark Brown /** 24580d0cf00aSMark Brown * snd_soc_register_codec - Register a codec with the ASoC core 24590d0cf00aSMark Brown * 2460ac11a2b3SMark Brown * @codec: codec to register 24610d0cf00aSMark Brown */ 24620d0cf00aSMark Brown int snd_soc_register_codec(struct snd_soc_codec *codec) 24630d0cf00aSMark Brown { 2464151ab22cSMark Brown int i; 2465151ab22cSMark Brown 24660d0cf00aSMark Brown if (!codec->name) 24670d0cf00aSMark Brown return -EINVAL; 24680d0cf00aSMark Brown 24690d0cf00aSMark Brown /* The device should become mandatory over time */ 24700d0cf00aSMark Brown if (!codec->dev) 24710d0cf00aSMark Brown printk(KERN_WARNING "No device for codec %s\n", codec->name); 24720d0cf00aSMark Brown 24730d0cf00aSMark Brown INIT_LIST_HEAD(&codec->list); 24740d0cf00aSMark Brown 2475151ab22cSMark Brown for (i = 0; i < codec->num_dai; i++) { 2476151ab22cSMark Brown fixup_codec_formats(&codec->dai[i].playback); 2477151ab22cSMark Brown fixup_codec_formats(&codec->dai[i].capture); 2478151ab22cSMark Brown } 2479151ab22cSMark Brown 24800d0cf00aSMark Brown mutex_lock(&client_mutex); 24810d0cf00aSMark Brown list_add(&codec->list, &codec_list); 24820d0cf00aSMark Brown snd_soc_instantiate_cards(); 24830d0cf00aSMark Brown mutex_unlock(&client_mutex); 24840d0cf00aSMark Brown 24850d0cf00aSMark Brown pr_debug("Registered codec '%s'\n", codec->name); 24860d0cf00aSMark Brown 24870d0cf00aSMark Brown return 0; 24880d0cf00aSMark Brown } 24890d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec); 24900d0cf00aSMark Brown 24910d0cf00aSMark Brown /** 24920d0cf00aSMark Brown * snd_soc_unregister_codec - Unregister a codec from the ASoC core 24930d0cf00aSMark Brown * 2494ac11a2b3SMark Brown * @codec: codec to unregister 24950d0cf00aSMark Brown */ 24960d0cf00aSMark Brown void snd_soc_unregister_codec(struct snd_soc_codec *codec) 24970d0cf00aSMark Brown { 24980d0cf00aSMark Brown mutex_lock(&client_mutex); 24990d0cf00aSMark Brown list_del(&codec->list); 25000d0cf00aSMark Brown mutex_unlock(&client_mutex); 25010d0cf00aSMark Brown 25020d0cf00aSMark Brown pr_debug("Unregistered codec '%s'\n", codec->name); 25030d0cf00aSMark Brown } 25040d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec); 25050d0cf00aSMark Brown 2506c9b3a40fSTakashi Iwai static int __init snd_soc_init(void) 2507db2a4165SFrank Mandarino { 2508384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 2509384c89e2SMark Brown debugfs_root = debugfs_create_dir("asoc", NULL); 2510384c89e2SMark Brown if (IS_ERR(debugfs_root) || !debugfs_root) { 2511384c89e2SMark Brown printk(KERN_WARNING 2512384c89e2SMark Brown "ASoC: Failed to create debugfs directory\n"); 2513384c89e2SMark Brown debugfs_root = NULL; 2514384c89e2SMark Brown } 2515384c89e2SMark Brown #endif 2516384c89e2SMark Brown 2517db2a4165SFrank Mandarino return platform_driver_register(&soc_driver); 2518db2a4165SFrank Mandarino } 2519db2a4165SFrank Mandarino 25207d8c16a6SMark Brown static void __exit snd_soc_exit(void) 2521db2a4165SFrank Mandarino { 2522384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 2523384c89e2SMark Brown debugfs_remove_recursive(debugfs_root); 2524384c89e2SMark Brown #endif 2525db2a4165SFrank Mandarino platform_driver_unregister(&soc_driver); 2526db2a4165SFrank Mandarino } 2527db2a4165SFrank Mandarino 2528db2a4165SFrank Mandarino module_init(snd_soc_init); 2529db2a4165SFrank Mandarino module_exit(snd_soc_exit); 2530db2a4165SFrank Mandarino 2531db2a4165SFrank Mandarino /* Module information */ 2532d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); 2533db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core"); 2534db2a4165SFrank Mandarino MODULE_LICENSE("GPL"); 25358b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio"); 2536