xref: /openbmc/linux/sound/soc/soc-core.c (revision 85488037bb9b533b064be66412dbe1dbcd2734d9)
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>
31474828a4SMarek Vasut #include <sound/ac97_codec.h>
32db2a4165SFrank Mandarino #include <sound/core.h>
33db2a4165SFrank Mandarino #include <sound/pcm.h>
34db2a4165SFrank Mandarino #include <sound/pcm_params.h>
35db2a4165SFrank Mandarino #include <sound/soc.h>
36db2a4165SFrank Mandarino #include <sound/soc-dapm.h>
37db2a4165SFrank Mandarino #include <sound/initval.h>
38db2a4165SFrank Mandarino 
39db2a4165SFrank Mandarino static DEFINE_MUTEX(pcm_mutex);
40db2a4165SFrank Mandarino static DEFINE_MUTEX(io_mutex);
41db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
42db2a4165SFrank Mandarino 
43384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
44384c89e2SMark Brown static struct dentry *debugfs_root;
45384c89e2SMark Brown #endif
46384c89e2SMark Brown 
47c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
48c5af3a2eSMark Brown static LIST_HEAD(card_list);
499115171aSMark Brown static LIST_HEAD(dai_list);
5012a48a8cSMark Brown static LIST_HEAD(platform_list);
510d0cf00aSMark Brown static LIST_HEAD(codec_list);
52c5af3a2eSMark Brown 
53c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card);
54c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card);
55c5af3a2eSMark Brown 
56db2a4165SFrank Mandarino /*
57db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
58db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
59db2a4165SFrank Mandarino  * between two audio tracks.
60db2a4165SFrank Mandarino  */
61db2a4165SFrank Mandarino static int pmdown_time = 5000;
62db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
63db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
64db2a4165SFrank Mandarino 
65965ac42cSLiam Girdwood /*
66965ac42cSLiam Girdwood  * This function forces any delayed work to be queued and run.
67965ac42cSLiam Girdwood  */
68965ac42cSLiam Girdwood static int run_delayed_work(struct delayed_work *dwork)
69965ac42cSLiam Girdwood {
70965ac42cSLiam Girdwood 	int ret;
71965ac42cSLiam Girdwood 
72965ac42cSLiam Girdwood 	/* cancel any work waiting to be queued. */
73965ac42cSLiam Girdwood 	ret = cancel_delayed_work(dwork);
74965ac42cSLiam Girdwood 
75965ac42cSLiam Girdwood 	/* if there was any work waiting then we run it now and
76965ac42cSLiam Girdwood 	 * wait for it's completion */
77965ac42cSLiam Girdwood 	if (ret) {
78965ac42cSLiam Girdwood 		schedule_delayed_work(dwork, 0);
79965ac42cSLiam Girdwood 		flush_scheduled_work();
80965ac42cSLiam Girdwood 	}
81965ac42cSLiam Girdwood 	return ret;
82965ac42cSLiam Girdwood }
83965ac42cSLiam Girdwood 
84db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
85db2a4165SFrank Mandarino /* unregister ac97 codec */
86db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
87db2a4165SFrank Mandarino {
88db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
89db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
90db2a4165SFrank Mandarino 	return 0;
91db2a4165SFrank Mandarino }
92db2a4165SFrank Mandarino 
93db2a4165SFrank Mandarino /* stop no dev release warning */
94db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
95db2a4165SFrank Mandarino 
96db2a4165SFrank Mandarino /* register ac97 codec to bus */
97db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
98db2a4165SFrank Mandarino {
99db2a4165SFrank Mandarino 	int err;
100db2a4165SFrank Mandarino 
101db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
1024ac5c61fSMark Brown 	codec->ac97->dev.parent = codec->card->dev;
103db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
104db2a4165SFrank Mandarino 
105bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
106db2a4165SFrank Mandarino 		     codec->card->number, 0, codec->name);
107db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
108db2a4165SFrank Mandarino 	if (err < 0) {
109db2a4165SFrank Mandarino 		snd_printk(KERN_ERR "Can't register ac97 bus\n");
110db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
111db2a4165SFrank Mandarino 		return err;
112db2a4165SFrank Mandarino 	}
113db2a4165SFrank Mandarino 	return 0;
114db2a4165SFrank Mandarino }
115db2a4165SFrank Mandarino #endif
116db2a4165SFrank Mandarino 
11706f409d7SMark Brown static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
11806f409d7SMark Brown {
11906f409d7SMark Brown 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
12006f409d7SMark Brown 	struct snd_soc_device *socdev = rtd->socdev;
12106f409d7SMark Brown 	struct snd_soc_card *card = socdev->card;
12206f409d7SMark Brown 	struct snd_soc_dai_link *machine = rtd->dai;
12306f409d7SMark Brown 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
12406f409d7SMark Brown 	struct snd_soc_dai *codec_dai = machine->codec_dai;
12506f409d7SMark Brown 	int ret;
12606f409d7SMark Brown 
12706f409d7SMark Brown 	if (codec_dai->symmetric_rates || cpu_dai->symmetric_rates ||
12806f409d7SMark Brown 	    machine->symmetric_rates) {
12906f409d7SMark Brown 		dev_dbg(card->dev, "Symmetry forces %dHz rate\n",
13006f409d7SMark Brown 			machine->rate);
13106f409d7SMark Brown 
13206f409d7SMark Brown 		ret = snd_pcm_hw_constraint_minmax(substream->runtime,
13306f409d7SMark Brown 						   SNDRV_PCM_HW_PARAM_RATE,
13406f409d7SMark Brown 						   machine->rate,
13506f409d7SMark Brown 						   machine->rate);
13606f409d7SMark Brown 		if (ret < 0) {
13706f409d7SMark Brown 			dev_err(card->dev,
13806f409d7SMark Brown 				"Unable to apply rate symmetry constraint: %d\n", ret);
13906f409d7SMark Brown 			return ret;
14006f409d7SMark Brown 		}
14106f409d7SMark Brown 	}
14206f409d7SMark Brown 
14306f409d7SMark Brown 	return 0;
14406f409d7SMark Brown }
14506f409d7SMark Brown 
146db2a4165SFrank Mandarino /*
147db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
148db2a4165SFrank Mandarino  * then initialized and any private data can be allocated. This also calls
149db2a4165SFrank Mandarino  * startup for the cpu DAI, platform, machine and codec DAI.
150db2a4165SFrank Mandarino  */
151db2a4165SFrank Mandarino static int soc_pcm_open(struct snd_pcm_substream *substream)
152db2a4165SFrank Mandarino {
153db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
154db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
15587689d56SMark Brown 	struct snd_soc_card *card = socdev->card;
156db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
157cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
15887689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
1593c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
1603c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
161db2a4165SFrank Mandarino 	int ret = 0;
162db2a4165SFrank Mandarino 
163db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
164db2a4165SFrank Mandarino 
165db2a4165SFrank Mandarino 	/* startup the audio subsystem */
1666335d055SEric Miao 	if (cpu_dai->ops->startup) {
1676335d055SEric Miao 		ret = cpu_dai->ops->startup(substream, cpu_dai);
168db2a4165SFrank Mandarino 		if (ret < 0) {
169db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open interface %s\n",
170cb666e5bSLiam Girdwood 				cpu_dai->name);
171db2a4165SFrank Mandarino 			goto out;
172db2a4165SFrank Mandarino 		}
173db2a4165SFrank Mandarino 	}
174db2a4165SFrank Mandarino 
175db2a4165SFrank Mandarino 	if (platform->pcm_ops->open) {
176db2a4165SFrank Mandarino 		ret = platform->pcm_ops->open(substream);
177db2a4165SFrank Mandarino 		if (ret < 0) {
178db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
179db2a4165SFrank Mandarino 			goto platform_err;
180db2a4165SFrank Mandarino 		}
181db2a4165SFrank Mandarino 	}
182db2a4165SFrank Mandarino 
1836335d055SEric Miao 	if (codec_dai->ops->startup) {
1846335d055SEric Miao 		ret = codec_dai->ops->startup(substream, codec_dai);
185cb666e5bSLiam Girdwood 		if (ret < 0) {
186cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: can't open codec %s\n",
187cb666e5bSLiam Girdwood 				codec_dai->name);
188cb666e5bSLiam Girdwood 			goto codec_dai_err;
189cb666e5bSLiam Girdwood 		}
190cb666e5bSLiam Girdwood 	}
191cb666e5bSLiam Girdwood 
192db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->startup) {
193db2a4165SFrank Mandarino 		ret = machine->ops->startup(substream);
194db2a4165SFrank Mandarino 		if (ret < 0) {
195db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
196db2a4165SFrank Mandarino 			goto machine_err;
197db2a4165SFrank Mandarino 		}
198db2a4165SFrank Mandarino 	}
199db2a4165SFrank Mandarino 
200db2a4165SFrank Mandarino 	/* Check that the codec and cpu DAI's are compatible */
201db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
202db2a4165SFrank Mandarino 		runtime->hw.rate_min =
2033ff3f64bSMark Brown 			max(codec_dai->playback.rate_min,
2043ff3f64bSMark Brown 			    cpu_dai->playback.rate_min);
205db2a4165SFrank Mandarino 		runtime->hw.rate_max =
2063ff3f64bSMark Brown 			min(codec_dai->playback.rate_max,
2073ff3f64bSMark Brown 			    cpu_dai->playback.rate_max);
208db2a4165SFrank Mandarino 		runtime->hw.channels_min =
209cb666e5bSLiam Girdwood 			max(codec_dai->playback.channels_min,
210cb666e5bSLiam Girdwood 				cpu_dai->playback.channels_min);
211db2a4165SFrank Mandarino 		runtime->hw.channels_max =
212cb666e5bSLiam Girdwood 			min(codec_dai->playback.channels_max,
213cb666e5bSLiam Girdwood 				cpu_dai->playback.channels_max);
214cb666e5bSLiam Girdwood 		runtime->hw.formats =
215cb666e5bSLiam Girdwood 			codec_dai->playback.formats & cpu_dai->playback.formats;
216cb666e5bSLiam Girdwood 		runtime->hw.rates =
217cb666e5bSLiam Girdwood 			codec_dai->playback.rates & cpu_dai->playback.rates;
218db2a4165SFrank Mandarino 	} else {
219db2a4165SFrank Mandarino 		runtime->hw.rate_min =
2203ff3f64bSMark Brown 			max(codec_dai->capture.rate_min,
2213ff3f64bSMark Brown 			    cpu_dai->capture.rate_min);
222db2a4165SFrank Mandarino 		runtime->hw.rate_max =
2233ff3f64bSMark Brown 			min(codec_dai->capture.rate_max,
2243ff3f64bSMark Brown 			    cpu_dai->capture.rate_max);
225db2a4165SFrank Mandarino 		runtime->hw.channels_min =
226cb666e5bSLiam Girdwood 			max(codec_dai->capture.channels_min,
227cb666e5bSLiam Girdwood 				cpu_dai->capture.channels_min);
228db2a4165SFrank Mandarino 		runtime->hw.channels_max =
229cb666e5bSLiam Girdwood 			min(codec_dai->capture.channels_max,
230cb666e5bSLiam Girdwood 				cpu_dai->capture.channels_max);
231cb666e5bSLiam Girdwood 		runtime->hw.formats =
232cb666e5bSLiam Girdwood 			codec_dai->capture.formats & cpu_dai->capture.formats;
233cb666e5bSLiam Girdwood 		runtime->hw.rates =
234cb666e5bSLiam Girdwood 			codec_dai->capture.rates & cpu_dai->capture.rates;
235db2a4165SFrank Mandarino 	}
236db2a4165SFrank Mandarino 
237db2a4165SFrank Mandarino 	snd_pcm_limit_hw_rates(runtime);
238db2a4165SFrank Mandarino 	if (!runtime->hw.rates) {
239db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
240cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
241cb666e5bSLiam Girdwood 		goto machine_err;
242db2a4165SFrank Mandarino 	}
243db2a4165SFrank Mandarino 	if (!runtime->hw.formats) {
244db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
245cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
246cb666e5bSLiam Girdwood 		goto machine_err;
247db2a4165SFrank Mandarino 	}
248db2a4165SFrank Mandarino 	if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
249db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
250cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
251cb666e5bSLiam Girdwood 		goto machine_err;
252db2a4165SFrank Mandarino 	}
253db2a4165SFrank Mandarino 
25406f409d7SMark Brown 	/* Symmetry only applies if we've already got an active stream. */
25506f409d7SMark Brown 	if (cpu_dai->active || codec_dai->active) {
25606f409d7SMark Brown 		ret = soc_pcm_apply_symmetry(substream);
25706f409d7SMark Brown 		if (ret != 0)
25806f409d7SMark Brown 			goto machine_err;
25906f409d7SMark Brown 	}
26006f409d7SMark Brown 
261f24368c2SMark Brown 	pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
262f24368c2SMark Brown 	pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
263f24368c2SMark Brown 	pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
264b5c5fd24SLiam Girdwood 		 runtime->hw.channels_max);
265f24368c2SMark Brown 	pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
266b5c5fd24SLiam Girdwood 		 runtime->hw.rate_max);
267db2a4165SFrank Mandarino 
268db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
269cb666e5bSLiam Girdwood 		cpu_dai->playback.active = codec_dai->playback.active = 1;
270db2a4165SFrank Mandarino 	else
271cb666e5bSLiam Girdwood 		cpu_dai->capture.active = codec_dai->capture.active = 1;
272cb666e5bSLiam Girdwood 	cpu_dai->active = codec_dai->active = 1;
273cb666e5bSLiam Girdwood 	cpu_dai->runtime = runtime;
2746627a653SMark Brown 	card->codec->active++;
275db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
276db2a4165SFrank Mandarino 	return 0;
277db2a4165SFrank Mandarino 
278cb666e5bSLiam Girdwood machine_err:
279db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->shutdown)
280db2a4165SFrank Mandarino 		machine->ops->shutdown(substream);
281db2a4165SFrank Mandarino 
282cb666e5bSLiam Girdwood codec_dai_err:
283db2a4165SFrank Mandarino 	if (platform->pcm_ops->close)
284db2a4165SFrank Mandarino 		platform->pcm_ops->close(substream);
285db2a4165SFrank Mandarino 
286db2a4165SFrank Mandarino platform_err:
2876335d055SEric Miao 	if (cpu_dai->ops->shutdown)
2886335d055SEric Miao 		cpu_dai->ops->shutdown(substream, cpu_dai);
289db2a4165SFrank Mandarino out:
290db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
291db2a4165SFrank Mandarino 	return ret;
292db2a4165SFrank Mandarino }
293db2a4165SFrank Mandarino 
294db2a4165SFrank Mandarino /*
2953a4fa0a2SRobert P. J. Day  * Power down the audio subsystem pmdown_time msecs after close is called.
296db2a4165SFrank Mandarino  * This is to ensure there are no pops or clicks in between any music tracks
297db2a4165SFrank Mandarino  * due to DAPM power cycling.
298db2a4165SFrank Mandarino  */
2994484bb2eSAndrew Morton static void close_delayed_work(struct work_struct *work)
300db2a4165SFrank Mandarino {
3016308419aSMark Brown 	struct snd_soc_card *card = container_of(work, struct snd_soc_card,
3026308419aSMark Brown 						 delayed_work.work);
3036627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
3043c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai;
305db2a4165SFrank Mandarino 	int i;
306db2a4165SFrank Mandarino 
307db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
308db2a4165SFrank Mandarino 	for (i = 0; i < codec->num_dai; i++) {
309db2a4165SFrank Mandarino 		codec_dai = &codec->dai[i];
310db2a4165SFrank Mandarino 
311f24368c2SMark Brown 		pr_debug("pop wq checking: %s status: %s waiting: %s\n",
312db2a4165SFrank Mandarino 			 codec_dai->playback.stream_name,
313db2a4165SFrank Mandarino 			 codec_dai->playback.active ? "active" : "inactive",
314db2a4165SFrank Mandarino 			 codec_dai->pop_wait ? "yes" : "no");
315db2a4165SFrank Mandarino 
316db2a4165SFrank Mandarino 		/* are we waiting on this codec DAI stream */
317db2a4165SFrank Mandarino 		if (codec_dai->pop_wait == 1) {
318db2a4165SFrank Mandarino 			codec_dai->pop_wait = 0;
3190b4d221bSLiam Girdwood 			snd_soc_dapm_stream_event(codec,
3200b4d221bSLiam Girdwood 				codec_dai->playback.stream_name,
321db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_STOP);
322db2a4165SFrank Mandarino 		}
323db2a4165SFrank Mandarino 	}
324db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
325db2a4165SFrank Mandarino }
326db2a4165SFrank Mandarino 
327db2a4165SFrank Mandarino /*
328db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is closed. Private data can be
329db2a4165SFrank Mandarino  * freed here. The cpu DAI, codec DAI, machine and platform are also
330db2a4165SFrank Mandarino  * shutdown.
331db2a4165SFrank Mandarino  */
332db2a4165SFrank Mandarino static int soc_codec_close(struct snd_pcm_substream *substream)
333db2a4165SFrank Mandarino {
334db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
335db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
3366308419aSMark Brown 	struct snd_soc_card *card = socdev->card;
337cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
33887689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
3393c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
3403c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
3416627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
342db2a4165SFrank Mandarino 
343db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
344db2a4165SFrank Mandarino 
345db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
346cb666e5bSLiam Girdwood 		cpu_dai->playback.active = codec_dai->playback.active = 0;
347db2a4165SFrank Mandarino 	else
348cb666e5bSLiam Girdwood 		cpu_dai->capture.active = codec_dai->capture.active = 0;
349db2a4165SFrank Mandarino 
350cb666e5bSLiam Girdwood 	if (codec_dai->playback.active == 0 &&
351cb666e5bSLiam Girdwood 		codec_dai->capture.active == 0) {
352cb666e5bSLiam Girdwood 		cpu_dai->active = codec_dai->active = 0;
353db2a4165SFrank Mandarino 	}
354db2a4165SFrank Mandarino 	codec->active--;
355db2a4165SFrank Mandarino 
3566010b2daSMark Brown 	/* Muting the DAC suppresses artifacts caused during digital
3576010b2daSMark Brown 	 * shutdown, for example from stopping clocks.
3586010b2daSMark Brown 	 */
3596010b2daSMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3606010b2daSMark Brown 		snd_soc_dai_digital_mute(codec_dai, 1);
3616010b2daSMark Brown 
3626335d055SEric Miao 	if (cpu_dai->ops->shutdown)
3636335d055SEric Miao 		cpu_dai->ops->shutdown(substream, cpu_dai);
364db2a4165SFrank Mandarino 
3656335d055SEric Miao 	if (codec_dai->ops->shutdown)
3666335d055SEric Miao 		codec_dai->ops->shutdown(substream, codec_dai);
367db2a4165SFrank Mandarino 
368db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->shutdown)
369db2a4165SFrank Mandarino 		machine->ops->shutdown(substream);
370db2a4165SFrank Mandarino 
371db2a4165SFrank Mandarino 	if (platform->pcm_ops->close)
372db2a4165SFrank Mandarino 		platform->pcm_ops->close(substream);
373cb666e5bSLiam Girdwood 	cpu_dai->runtime = NULL;
374db2a4165SFrank Mandarino 
375db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
376db2a4165SFrank Mandarino 		/* start delayed pop wq here for playback streams */
377cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 1;
3786308419aSMark Brown 		schedule_delayed_work(&card->delayed_work,
379db2a4165SFrank Mandarino 			msecs_to_jiffies(pmdown_time));
380db2a4165SFrank Mandarino 	} else {
381db2a4165SFrank Mandarino 		/* capture streams can be powered down now */
382cb666e5bSLiam Girdwood 		snd_soc_dapm_stream_event(codec,
3830b4d221bSLiam Girdwood 			codec_dai->capture.stream_name,
3840b4d221bSLiam Girdwood 			SND_SOC_DAPM_STREAM_STOP);
385db2a4165SFrank Mandarino 	}
386db2a4165SFrank Mandarino 
387db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
388db2a4165SFrank Mandarino 	return 0;
389db2a4165SFrank Mandarino }
390db2a4165SFrank Mandarino 
391db2a4165SFrank Mandarino /*
392db2a4165SFrank Mandarino  * Called by ALSA when the PCM substream is prepared, can set format, sample
393db2a4165SFrank Mandarino  * rate, etc.  This function is non atomic and can be called multiple times,
394db2a4165SFrank Mandarino  * it can refer to the runtime info.
395db2a4165SFrank Mandarino  */
396db2a4165SFrank Mandarino static int soc_pcm_prepare(struct snd_pcm_substream *substream)
397db2a4165SFrank Mandarino {
398db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
399db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
4006308419aSMark Brown 	struct snd_soc_card *card = socdev->card;
401cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
40287689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
4033c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
4043c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
4056627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
406db2a4165SFrank Mandarino 	int ret = 0;
407db2a4165SFrank Mandarino 
408db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
409cb666e5bSLiam Girdwood 
410cb666e5bSLiam Girdwood 	if (machine->ops && machine->ops->prepare) {
411cb666e5bSLiam Girdwood 		ret = machine->ops->prepare(substream);
412cb666e5bSLiam Girdwood 		if (ret < 0) {
413cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine prepare error\n");
414cb666e5bSLiam Girdwood 			goto out;
415cb666e5bSLiam Girdwood 		}
416cb666e5bSLiam Girdwood 	}
417cb666e5bSLiam Girdwood 
418db2a4165SFrank Mandarino 	if (platform->pcm_ops->prepare) {
419db2a4165SFrank Mandarino 		ret = platform->pcm_ops->prepare(substream);
420a71a468aSLiam Girdwood 		if (ret < 0) {
421a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: platform prepare error\n");
422db2a4165SFrank Mandarino 			goto out;
423db2a4165SFrank Mandarino 		}
424a71a468aSLiam Girdwood 	}
425db2a4165SFrank Mandarino 
4266335d055SEric Miao 	if (codec_dai->ops->prepare) {
4276335d055SEric Miao 		ret = codec_dai->ops->prepare(substream, codec_dai);
428a71a468aSLiam Girdwood 		if (ret < 0) {
429a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: codec DAI prepare error\n");
430db2a4165SFrank Mandarino 			goto out;
431db2a4165SFrank Mandarino 		}
432a71a468aSLiam Girdwood 	}
433db2a4165SFrank Mandarino 
4346335d055SEric Miao 	if (cpu_dai->ops->prepare) {
4356335d055SEric Miao 		ret = cpu_dai->ops->prepare(substream, cpu_dai);
436cb666e5bSLiam Girdwood 		if (ret < 0) {
437cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: cpu DAI prepare error\n");
438cb666e5bSLiam Girdwood 			goto out;
439cb666e5bSLiam Girdwood 		}
440cb666e5bSLiam Girdwood 	}
441db2a4165SFrank Mandarino 
442d45f6219SMark Brown 	/* cancel any delayed stream shutdown that is pending */
443d45f6219SMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
444d45f6219SMark Brown 	    codec_dai->pop_wait) {
445cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 0;
4466308419aSMark Brown 		cancel_delayed_work(&card->delayed_work);
447db2a4165SFrank Mandarino 	}
448db2a4165SFrank Mandarino 
449db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
450db2a4165SFrank Mandarino 		snd_soc_dapm_stream_event(codec,
451cb666e5bSLiam Girdwood 					  codec_dai->playback.stream_name,
452db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_START);
453db2a4165SFrank Mandarino 	else
454db2a4165SFrank Mandarino 		snd_soc_dapm_stream_event(codec,
455cb666e5bSLiam Girdwood 					  codec_dai->capture.stream_name,
456db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_START);
4578c6529dbSLiam Girdwood 
4588c6529dbSLiam Girdwood 	snd_soc_dai_digital_mute(codec_dai, 0);
459db2a4165SFrank Mandarino 
460db2a4165SFrank Mandarino out:
461db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
462db2a4165SFrank Mandarino 	return ret;
463db2a4165SFrank Mandarino }
464db2a4165SFrank Mandarino 
465db2a4165SFrank Mandarino /*
466db2a4165SFrank Mandarino  * Called by ALSA when the hardware params are set by application. This
467db2a4165SFrank Mandarino  * function can also be called multiple times and can allocate buffers
468db2a4165SFrank Mandarino  * (using snd_pcm_lib_* ). It's non-atomic.
469db2a4165SFrank Mandarino  */
470db2a4165SFrank Mandarino static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
471db2a4165SFrank Mandarino 				struct snd_pcm_hw_params *params)
472db2a4165SFrank Mandarino {
473db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
474db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
475cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
47687689d56SMark Brown 	struct snd_soc_card *card = socdev->card;
47787689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
4783c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
4793c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
480db2a4165SFrank Mandarino 	int ret = 0;
481db2a4165SFrank Mandarino 
482db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
483db2a4165SFrank Mandarino 
484cb666e5bSLiam Girdwood 	if (machine->ops && machine->ops->hw_params) {
485cb666e5bSLiam Girdwood 		ret = machine->ops->hw_params(substream, params);
486cb666e5bSLiam Girdwood 		if (ret < 0) {
487cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine hw_params failed\n");
488db2a4165SFrank Mandarino 			goto out;
489db2a4165SFrank Mandarino 		}
490cb666e5bSLiam Girdwood 	}
491db2a4165SFrank Mandarino 
4926335d055SEric Miao 	if (codec_dai->ops->hw_params) {
4936335d055SEric Miao 		ret = codec_dai->ops->hw_params(substream, params, codec_dai);
494db2a4165SFrank Mandarino 		if (ret < 0) {
495db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't set codec %s hw params\n",
496cb666e5bSLiam Girdwood 				codec_dai->name);
497cb666e5bSLiam Girdwood 			goto codec_err;
498db2a4165SFrank Mandarino 		}
499db2a4165SFrank Mandarino 	}
500db2a4165SFrank Mandarino 
5016335d055SEric Miao 	if (cpu_dai->ops->hw_params) {
5026335d055SEric Miao 		ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
503db2a4165SFrank Mandarino 		if (ret < 0) {
5043ff3f64bSMark Brown 			printk(KERN_ERR "asoc: interface %s hw params failed\n",
505cb666e5bSLiam Girdwood 				cpu_dai->name);
506db2a4165SFrank Mandarino 			goto interface_err;
507db2a4165SFrank Mandarino 		}
508db2a4165SFrank Mandarino 	}
509db2a4165SFrank Mandarino 
510db2a4165SFrank Mandarino 	if (platform->pcm_ops->hw_params) {
511db2a4165SFrank Mandarino 		ret = platform->pcm_ops->hw_params(substream, params);
512db2a4165SFrank Mandarino 		if (ret < 0) {
5133ff3f64bSMark Brown 			printk(KERN_ERR "asoc: platform %s hw params failed\n",
514db2a4165SFrank Mandarino 				platform->name);
515db2a4165SFrank Mandarino 			goto platform_err;
516db2a4165SFrank Mandarino 		}
517db2a4165SFrank Mandarino 	}
518db2a4165SFrank Mandarino 
51906f409d7SMark Brown 	machine->rate = params_rate(params);
52006f409d7SMark Brown 
521db2a4165SFrank Mandarino out:
522db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
523db2a4165SFrank Mandarino 	return ret;
524db2a4165SFrank Mandarino 
525db2a4165SFrank Mandarino platform_err:
5266335d055SEric Miao 	if (cpu_dai->ops->hw_free)
5276335d055SEric Miao 		cpu_dai->ops->hw_free(substream, cpu_dai);
528db2a4165SFrank Mandarino 
529db2a4165SFrank Mandarino interface_err:
5306335d055SEric Miao 	if (codec_dai->ops->hw_free)
5316335d055SEric Miao 		codec_dai->ops->hw_free(substream, codec_dai);
532cb666e5bSLiam Girdwood 
533cb666e5bSLiam Girdwood codec_err:
534cb666e5bSLiam Girdwood 	if (machine->ops && machine->ops->hw_free)
535cb666e5bSLiam Girdwood 		machine->ops->hw_free(substream);
536db2a4165SFrank Mandarino 
537db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
538db2a4165SFrank Mandarino 	return ret;
539db2a4165SFrank Mandarino }
540db2a4165SFrank Mandarino 
541db2a4165SFrank Mandarino /*
542db2a4165SFrank Mandarino  * Free's resources allocated by hw_params, can be called multiple times
543db2a4165SFrank Mandarino  */
544db2a4165SFrank Mandarino static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
545db2a4165SFrank Mandarino {
546db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
547db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
548cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
54987689d56SMark Brown 	struct snd_soc_card *card = socdev->card;
55087689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
5513c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
5523c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
5536627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
554db2a4165SFrank Mandarino 
555db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
556db2a4165SFrank Mandarino 
557db2a4165SFrank Mandarino 	/* apply codec digital mute */
5588c6529dbSLiam Girdwood 	if (!codec->active)
5598c6529dbSLiam Girdwood 		snd_soc_dai_digital_mute(codec_dai, 1);
560db2a4165SFrank Mandarino 
561db2a4165SFrank Mandarino 	/* free any machine hw params */
562db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->hw_free)
563db2a4165SFrank Mandarino 		machine->ops->hw_free(substream);
564db2a4165SFrank Mandarino 
565db2a4165SFrank Mandarino 	/* free any DMA resources */
566db2a4165SFrank Mandarino 	if (platform->pcm_ops->hw_free)
567db2a4165SFrank Mandarino 		platform->pcm_ops->hw_free(substream);
568db2a4165SFrank Mandarino 
569db2a4165SFrank Mandarino 	/* now free hw params for the DAI's  */
5706335d055SEric Miao 	if (codec_dai->ops->hw_free)
5716335d055SEric Miao 		codec_dai->ops->hw_free(substream, codec_dai);
572db2a4165SFrank Mandarino 
5736335d055SEric Miao 	if (cpu_dai->ops->hw_free)
5746335d055SEric Miao 		cpu_dai->ops->hw_free(substream, cpu_dai);
575db2a4165SFrank Mandarino 
576db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
577db2a4165SFrank Mandarino 	return 0;
578db2a4165SFrank Mandarino }
579db2a4165SFrank Mandarino 
580db2a4165SFrank Mandarino static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
581db2a4165SFrank Mandarino {
582db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
583db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
58487689d56SMark Brown 	struct snd_soc_card *card= socdev->card;
585cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
58687689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
5873c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
5883c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
589db2a4165SFrank Mandarino 	int ret;
590db2a4165SFrank Mandarino 
5916335d055SEric Miao 	if (codec_dai->ops->trigger) {
5926335d055SEric Miao 		ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
593db2a4165SFrank Mandarino 		if (ret < 0)
594db2a4165SFrank Mandarino 			return ret;
595db2a4165SFrank Mandarino 	}
596db2a4165SFrank Mandarino 
597db2a4165SFrank Mandarino 	if (platform->pcm_ops->trigger) {
598db2a4165SFrank Mandarino 		ret = platform->pcm_ops->trigger(substream, cmd);
599db2a4165SFrank Mandarino 		if (ret < 0)
600db2a4165SFrank Mandarino 			return ret;
601db2a4165SFrank Mandarino 	}
602db2a4165SFrank Mandarino 
6036335d055SEric Miao 	if (cpu_dai->ops->trigger) {
6046335d055SEric Miao 		ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
605db2a4165SFrank Mandarino 		if (ret < 0)
606db2a4165SFrank Mandarino 			return ret;
607db2a4165SFrank Mandarino 	}
608db2a4165SFrank Mandarino 	return 0;
609db2a4165SFrank Mandarino }
610db2a4165SFrank Mandarino 
611db2a4165SFrank Mandarino /* ASoC PCM operations */
612db2a4165SFrank Mandarino static struct snd_pcm_ops soc_pcm_ops = {
613db2a4165SFrank Mandarino 	.open		= soc_pcm_open,
614db2a4165SFrank Mandarino 	.close		= soc_codec_close,
615db2a4165SFrank Mandarino 	.hw_params	= soc_pcm_hw_params,
616db2a4165SFrank Mandarino 	.hw_free	= soc_pcm_hw_free,
617db2a4165SFrank Mandarino 	.prepare	= soc_pcm_prepare,
618db2a4165SFrank Mandarino 	.trigger	= soc_pcm_trigger,
619db2a4165SFrank Mandarino };
620db2a4165SFrank Mandarino 
621db2a4165SFrank Mandarino #ifdef CONFIG_PM
622db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
623416356fcSMark Brown static int soc_suspend(struct device *dev)
624db2a4165SFrank Mandarino {
625416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
626db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
62787506549SMark Brown 	struct snd_soc_card *card = socdev->card;
62887689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
629db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
6306627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
631db2a4165SFrank Mandarino 	int i;
632db2a4165SFrank Mandarino 
633e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
634e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
635e3509ff0SDaniel Mack 	 */
636e3509ff0SDaniel Mack 	if (!codec)
637e3509ff0SDaniel Mack 		return 0;
638e3509ff0SDaniel Mack 
6396ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
6406ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
6416ed25978SAndy Green 	 */
6426ed25978SAndy Green 	snd_power_lock(codec->card);
6436ed25978SAndy Green 	snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
6446ed25978SAndy Green 	snd_power_unlock(codec->card);
6456ed25978SAndy Green 
6466ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
6476ed25978SAndy Green 	snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
6486ed25978SAndy Green 
649db2a4165SFrank Mandarino 	/* mute any active DAC's */
650dee89c4dSMark Brown 	for (i = 0; i < card->num_links; i++) {
651dee89c4dSMark Brown 		struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
6526335d055SEric Miao 		if (dai->ops->digital_mute && dai->playback.active)
6536335d055SEric Miao 			dai->ops->digital_mute(dai, 1);
654db2a4165SFrank Mandarino 	}
655db2a4165SFrank Mandarino 
6564ccab3e7SLiam Girdwood 	/* suspend all pcms */
65787506549SMark Brown 	for (i = 0; i < card->num_links; i++)
65887506549SMark Brown 		snd_pcm_suspend_all(card->dai_link[i].pcm);
6594ccab3e7SLiam Girdwood 
66087506549SMark Brown 	if (card->suspend_pre)
661416356fcSMark Brown 		card->suspend_pre(pdev, PMSG_SUSPEND);
662db2a4165SFrank Mandarino 
66387506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
66487506549SMark Brown 		struct snd_soc_dai  *cpu_dai = card->dai_link[i].cpu_dai;
6653ba9e10aSMark Brown 		if (cpu_dai->suspend && !cpu_dai->ac97_control)
666dc7d7b83SMark Brown 			cpu_dai->suspend(cpu_dai);
667db2a4165SFrank Mandarino 		if (platform->suspend)
66807c84d04SMark Brown 			platform->suspend(cpu_dai);
669db2a4165SFrank Mandarino 	}
670db2a4165SFrank Mandarino 
671db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
6726308419aSMark Brown 	run_delayed_work(&card->delayed_work);
6730be9898aSMark Brown 	codec->suspend_bias_level = codec->bias_level;
674db2a4165SFrank Mandarino 
675db2a4165SFrank Mandarino 	for (i = 0; i < codec->num_dai; i++) {
676db2a4165SFrank Mandarino 		char *stream = codec->dai[i].playback.stream_name;
677db2a4165SFrank Mandarino 		if (stream != NULL)
678db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
679db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
680db2a4165SFrank Mandarino 		stream = codec->dai[i].capture.stream_name;
681db2a4165SFrank Mandarino 		if (stream != NULL)
682db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
683db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
684db2a4165SFrank Mandarino 	}
685db2a4165SFrank Mandarino 
686db2a4165SFrank Mandarino 	if (codec_dev->suspend)
687416356fcSMark Brown 		codec_dev->suspend(pdev, PMSG_SUSPEND);
688db2a4165SFrank Mandarino 
68987506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
69087506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
6913ba9e10aSMark Brown 		if (cpu_dai->suspend && cpu_dai->ac97_control)
692dc7d7b83SMark Brown 			cpu_dai->suspend(cpu_dai);
693db2a4165SFrank Mandarino 	}
694db2a4165SFrank Mandarino 
69587506549SMark Brown 	if (card->suspend_post)
696416356fcSMark Brown 		card->suspend_post(pdev, PMSG_SUSPEND);
697db2a4165SFrank Mandarino 
698db2a4165SFrank Mandarino 	return 0;
699db2a4165SFrank Mandarino }
700db2a4165SFrank Mandarino 
7016ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
7026ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
7036ed25978SAndy Green  */
7046ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
705db2a4165SFrank Mandarino {
7066308419aSMark Brown 	struct snd_soc_card *card = container_of(work,
7076308419aSMark Brown 						 struct snd_soc_card,
7086ed25978SAndy Green 						 deferred_resume_work);
7096308419aSMark Brown 	struct snd_soc_device *socdev = card->socdev;
71087689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
711db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
7126627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
7136ed25978SAndy Green 	struct platform_device *pdev = to_platform_device(socdev->dev);
714db2a4165SFrank Mandarino 	int i;
715db2a4165SFrank Mandarino 
7166ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
7176ed25978SAndy Green 	 * so userspace apps are blocked from touching us
7186ed25978SAndy Green 	 */
7196ed25978SAndy Green 
720fde22f27SMark Brown 	dev_dbg(socdev->dev, "starting resume work\n");
7216ed25978SAndy Green 
72287506549SMark Brown 	if (card->resume_pre)
72387506549SMark Brown 		card->resume_pre(pdev);
724db2a4165SFrank Mandarino 
72587506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
72687506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
7273ba9e10aSMark Brown 		if (cpu_dai->resume && cpu_dai->ac97_control)
728dc7d7b83SMark Brown 			cpu_dai->resume(cpu_dai);
729db2a4165SFrank Mandarino 	}
730db2a4165SFrank Mandarino 
731db2a4165SFrank Mandarino 	if (codec_dev->resume)
732db2a4165SFrank Mandarino 		codec_dev->resume(pdev);
733db2a4165SFrank Mandarino 
734db2a4165SFrank Mandarino 	for (i = 0; i < codec->num_dai; i++) {
735db2a4165SFrank Mandarino 		char *stream = codec->dai[i].playback.stream_name;
736db2a4165SFrank Mandarino 		if (stream != NULL)
737db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
738db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
739db2a4165SFrank Mandarino 		stream = codec->dai[i].capture.stream_name;
740db2a4165SFrank Mandarino 		if (stream != NULL)
741db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
742db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
743db2a4165SFrank Mandarino 	}
744db2a4165SFrank Mandarino 
7453ff3f64bSMark Brown 	/* unmute any active DACs */
746dee89c4dSMark Brown 	for (i = 0; i < card->num_links; i++) {
747dee89c4dSMark Brown 		struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
7486335d055SEric Miao 		if (dai->ops->digital_mute && dai->playback.active)
7496335d055SEric Miao 			dai->ops->digital_mute(dai, 0);
750db2a4165SFrank Mandarino 	}
751db2a4165SFrank Mandarino 
75287506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
75387506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
7543ba9e10aSMark Brown 		if (cpu_dai->resume && !cpu_dai->ac97_control)
755dc7d7b83SMark Brown 			cpu_dai->resume(cpu_dai);
756db2a4165SFrank Mandarino 		if (platform->resume)
75707c84d04SMark Brown 			platform->resume(cpu_dai);
758db2a4165SFrank Mandarino 	}
759db2a4165SFrank Mandarino 
76087506549SMark Brown 	if (card->resume_post)
76187506549SMark Brown 		card->resume_post(pdev);
762db2a4165SFrank Mandarino 
763fde22f27SMark Brown 	dev_dbg(socdev->dev, "resume work completed\n");
7646ed25978SAndy Green 
7656ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
7666ed25978SAndy Green 	snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
7676ed25978SAndy Green }
7686ed25978SAndy Green 
7696ed25978SAndy Green /* powers up audio subsystem after a suspend */
770416356fcSMark Brown static int soc_resume(struct device *dev)
7716ed25978SAndy Green {
772416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
7736ed25978SAndy Green 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
7746308419aSMark Brown 	struct snd_soc_card *card = socdev->card;
77564ab9baaSMark Brown 	struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
7766ed25978SAndy Green 
77764ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
77864ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
77964ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
78064ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
78164ab9baaSMark Brown 	 */
78264ab9baaSMark Brown 	if (cpu_dai->ac97_control) {
78364ab9baaSMark Brown 		dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
78464ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
78564ab9baaSMark Brown 	} else {
78664ab9baaSMark Brown 		dev_dbg(socdev->dev, "Scheduling resume work\n");
7876308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
788fde22f27SMark Brown 			dev_err(socdev->dev, "resume work item may be lost\n");
78964ab9baaSMark Brown 	}
7906ed25978SAndy Green 
791db2a4165SFrank Mandarino 	return 0;
792db2a4165SFrank Mandarino }
793db2a4165SFrank Mandarino 
794831dc0f1SMark Brown /**
795831dc0f1SMark Brown  * snd_soc_suspend_device: Notify core of device suspend
796831dc0f1SMark Brown  *
797831dc0f1SMark Brown  * @dev: Device being suspended.
798831dc0f1SMark Brown  *
799831dc0f1SMark Brown  * In order to ensure that the entire audio subsystem is suspended in a
800831dc0f1SMark Brown  * coordinated fashion ASoC devices should suspend themselves when
801831dc0f1SMark Brown  * called by ASoC.  When the standard kernel suspend process asks the
802831dc0f1SMark Brown  * device to suspend it should call this function to initiate a suspend
803831dc0f1SMark Brown  * of the entire ASoC card.
804831dc0f1SMark Brown  *
805831dc0f1SMark Brown  * \note Currently this function is stubbed out.
806831dc0f1SMark Brown  */
807831dc0f1SMark Brown int snd_soc_suspend_device(struct device *dev)
808831dc0f1SMark Brown {
809831dc0f1SMark Brown 	return 0;
810831dc0f1SMark Brown }
811831dc0f1SMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend_device);
812831dc0f1SMark Brown 
813831dc0f1SMark Brown /**
814831dc0f1SMark Brown  * snd_soc_resume_device: Notify core of device resume
815831dc0f1SMark Brown  *
816831dc0f1SMark Brown  * @dev: Device being resumed.
817831dc0f1SMark Brown  *
818831dc0f1SMark Brown  * In order to ensure that the entire audio subsystem is resumed in a
819831dc0f1SMark Brown  * coordinated fashion ASoC devices should resume themselves when called
820831dc0f1SMark Brown  * by ASoC.  When the standard kernel resume process asks the device
821831dc0f1SMark Brown  * to resume it should call this function.  Once all the components of
822831dc0f1SMark Brown  * the card have notified that they are ready to be resumed the card
823831dc0f1SMark Brown  * will be resumed.
824831dc0f1SMark Brown  *
825831dc0f1SMark Brown  * \note Currently this function is stubbed out.
826831dc0f1SMark Brown  */
827831dc0f1SMark Brown int snd_soc_resume_device(struct device *dev)
828831dc0f1SMark Brown {
829831dc0f1SMark Brown 	return 0;
830831dc0f1SMark Brown }
831831dc0f1SMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume_device);
832db2a4165SFrank Mandarino #else
833db2a4165SFrank Mandarino #define soc_suspend	NULL
834db2a4165SFrank Mandarino #define soc_resume	NULL
835db2a4165SFrank Mandarino #endif
836db2a4165SFrank Mandarino 
837435c5e25SMark Brown static void snd_soc_instantiate_card(struct snd_soc_card *card)
838db2a4165SFrank Mandarino {
839435c5e25SMark Brown 	struct platform_device *pdev = container_of(card->dev,
840435c5e25SMark Brown 						    struct platform_device,
841435c5e25SMark Brown 						    dev);
842435c5e25SMark Brown 	struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
843435c5e25SMark Brown 	struct snd_soc_platform *platform;
844435c5e25SMark Brown 	struct snd_soc_dai *dai;
8456b05eda6SMark Brown 	int i, found, ret, ac97;
846db2a4165SFrank Mandarino 
847435c5e25SMark Brown 	if (card->instantiated)
848435c5e25SMark Brown 		return;
8496308419aSMark Brown 
850435c5e25SMark Brown 	found = 0;
851435c5e25SMark Brown 	list_for_each_entry(platform, &platform_list, list)
852435c5e25SMark Brown 		if (card->platform == platform) {
853435c5e25SMark Brown 			found = 1;
854435c5e25SMark Brown 			break;
855435c5e25SMark Brown 		}
856435c5e25SMark Brown 	if (!found) {
857435c5e25SMark Brown 		dev_dbg(card->dev, "Platform %s not registered\n",
858435c5e25SMark Brown 			card->platform->name);
859435c5e25SMark Brown 		return;
860c5af3a2eSMark Brown 	}
861c5af3a2eSMark Brown 
8626b05eda6SMark Brown 	ac97 = 0;
863435c5e25SMark Brown 	for (i = 0; i < card->num_links; i++) {
864435c5e25SMark Brown 		found = 0;
865435c5e25SMark Brown 		list_for_each_entry(dai, &dai_list, list)
866435c5e25SMark Brown 			if (card->dai_link[i].cpu_dai == dai) {
867435c5e25SMark Brown 				found = 1;
868435c5e25SMark Brown 				break;
869435c5e25SMark Brown 			}
870435c5e25SMark Brown 		if (!found) {
871435c5e25SMark Brown 			dev_dbg(card->dev, "DAI %s not registered\n",
872435c5e25SMark Brown 				card->dai_link[i].cpu_dai->name);
873435c5e25SMark Brown 			return;
874435c5e25SMark Brown 		}
8756b05eda6SMark Brown 
8766b05eda6SMark Brown 		if (card->dai_link[i].cpu_dai->ac97_control)
8776b05eda6SMark Brown 			ac97 = 1;
8786b05eda6SMark Brown 	}
8796b05eda6SMark Brown 
8806b05eda6SMark Brown 	/* If we have AC97 in the system then don't wait for the
8816b05eda6SMark Brown 	 * codec.  This will need revisiting if we have to handle
8826b05eda6SMark Brown 	 * systems with mixed AC97 and non-AC97 parts.  Only check for
8836b05eda6SMark Brown 	 * DAIs currently; we can't do this per link since some AC97
8846b05eda6SMark Brown 	 * codecs have non-AC97 DAIs.
8856b05eda6SMark Brown 	 */
8866b05eda6SMark Brown 	if (!ac97)
8876b05eda6SMark Brown 		for (i = 0; i < card->num_links; i++) {
8886b05eda6SMark Brown 			found = 0;
8896b05eda6SMark Brown 			list_for_each_entry(dai, &dai_list, list)
8906b05eda6SMark Brown 				if (card->dai_link[i].codec_dai == dai) {
8916b05eda6SMark Brown 					found = 1;
8926b05eda6SMark Brown 					break;
8936b05eda6SMark Brown 				}
8946b05eda6SMark Brown 			if (!found) {
8956b05eda6SMark Brown 				dev_dbg(card->dev, "DAI %s not registered\n",
8966b05eda6SMark Brown 					card->dai_link[i].codec_dai->name);
8976b05eda6SMark Brown 				return;
8986b05eda6SMark Brown 			}
899435c5e25SMark Brown 		}
900435c5e25SMark Brown 
901435c5e25SMark Brown 	/* Note that we do not current check for codec components */
902435c5e25SMark Brown 
903435c5e25SMark Brown 	dev_dbg(card->dev, "All components present, instantiating\n");
904435c5e25SMark Brown 
905435c5e25SMark Brown 	/* Found everything, bring it up */
90687506549SMark Brown 	if (card->probe) {
90787506549SMark Brown 		ret = card->probe(pdev);
908db2a4165SFrank Mandarino 		if (ret < 0)
909435c5e25SMark Brown 			return;
910db2a4165SFrank Mandarino 	}
911db2a4165SFrank Mandarino 
91287506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
91387506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
914db2a4165SFrank Mandarino 		if (cpu_dai->probe) {
915bdb92876SMark Brown 			ret = cpu_dai->probe(pdev, cpu_dai);
916db2a4165SFrank Mandarino 			if (ret < 0)
917db2a4165SFrank Mandarino 				goto cpu_dai_err;
918db2a4165SFrank Mandarino 		}
919db2a4165SFrank Mandarino 	}
920db2a4165SFrank Mandarino 
921db2a4165SFrank Mandarino 	if (codec_dev->probe) {
922db2a4165SFrank Mandarino 		ret = codec_dev->probe(pdev);
923db2a4165SFrank Mandarino 		if (ret < 0)
924db2a4165SFrank Mandarino 			goto cpu_dai_err;
925db2a4165SFrank Mandarino 	}
926db2a4165SFrank Mandarino 
927db2a4165SFrank Mandarino 	if (platform->probe) {
928db2a4165SFrank Mandarino 		ret = platform->probe(pdev);
929db2a4165SFrank Mandarino 		if (ret < 0)
930db2a4165SFrank Mandarino 			goto platform_err;
931db2a4165SFrank Mandarino 	}
932db2a4165SFrank Mandarino 
933db2a4165SFrank Mandarino 	/* DAPM stream work */
9346308419aSMark Brown 	INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
9351301a964SRandy Dunlap #ifdef CONFIG_PM
9366ed25978SAndy Green 	/* deferred resume work */
9376308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
9381301a964SRandy Dunlap #endif
9396ed25978SAndy Green 
940435c5e25SMark Brown 	card->instantiated = 1;
941435c5e25SMark Brown 
942435c5e25SMark Brown 	return;
943db2a4165SFrank Mandarino 
944db2a4165SFrank Mandarino platform_err:
945db2a4165SFrank Mandarino 	if (codec_dev->remove)
946db2a4165SFrank Mandarino 		codec_dev->remove(pdev);
947db2a4165SFrank Mandarino 
948db2a4165SFrank Mandarino cpu_dai_err:
94918b9b3d9SLiam Girdwood 	for (i--; i >= 0; i--) {
95087506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
951db2a4165SFrank Mandarino 		if (cpu_dai->remove)
952bdb92876SMark Brown 			cpu_dai->remove(pdev, cpu_dai);
953db2a4165SFrank Mandarino 	}
954db2a4165SFrank Mandarino 
95587506549SMark Brown 	if (card->remove)
95687506549SMark Brown 		card->remove(pdev);
957435c5e25SMark Brown }
958db2a4165SFrank Mandarino 
959435c5e25SMark Brown /*
960435c5e25SMark Brown  * Attempt to initialise any uninitalised cards.  Must be called with
961435c5e25SMark Brown  * client_mutex.
962435c5e25SMark Brown  */
963435c5e25SMark Brown static void snd_soc_instantiate_cards(void)
964435c5e25SMark Brown {
965435c5e25SMark Brown 	struct snd_soc_card *card;
966435c5e25SMark Brown 	list_for_each_entry(card, &card_list, list)
967435c5e25SMark Brown 		snd_soc_instantiate_card(card);
968435c5e25SMark Brown }
969435c5e25SMark Brown 
970435c5e25SMark Brown /* probes a new socdev */
971435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
972435c5e25SMark Brown {
973435c5e25SMark Brown 	int ret = 0;
974435c5e25SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
975435c5e25SMark Brown 	struct snd_soc_card *card = socdev->card;
976435c5e25SMark Brown 
977435c5e25SMark Brown 	/* Bodge while we push things out of socdev */
978435c5e25SMark Brown 	card->socdev = socdev;
979435c5e25SMark Brown 
980435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
981435c5e25SMark Brown 	card->dev = &pdev->dev;
982435c5e25SMark Brown 	ret = snd_soc_register_card(card);
983435c5e25SMark Brown 	if (ret != 0) {
984435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
985db2a4165SFrank Mandarino 		return ret;
986db2a4165SFrank Mandarino 	}
987db2a4165SFrank Mandarino 
988435c5e25SMark Brown 	return 0;
989435c5e25SMark Brown }
990435c5e25SMark Brown 
991db2a4165SFrank Mandarino /* removes a socdev */
992db2a4165SFrank Mandarino static int soc_remove(struct platform_device *pdev)
993db2a4165SFrank Mandarino {
994db2a4165SFrank Mandarino 	int i;
995db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
99687506549SMark Brown 	struct snd_soc_card *card = socdev->card;
99787689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
998db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
999db2a4165SFrank Mandarino 
1000914dc182SMike Rapoport 	if (!card->instantiated)
1001914dc182SMike Rapoport 		return 0;
1002914dc182SMike Rapoport 
10036308419aSMark Brown 	run_delayed_work(&card->delayed_work);
1004965ac42cSLiam Girdwood 
1005db2a4165SFrank Mandarino 	if (platform->remove)
1006db2a4165SFrank Mandarino 		platform->remove(pdev);
1007db2a4165SFrank Mandarino 
1008db2a4165SFrank Mandarino 	if (codec_dev->remove)
1009db2a4165SFrank Mandarino 		codec_dev->remove(pdev);
1010db2a4165SFrank Mandarino 
101187506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
101287506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1013db2a4165SFrank Mandarino 		if (cpu_dai->remove)
1014bdb92876SMark Brown 			cpu_dai->remove(pdev, cpu_dai);
1015db2a4165SFrank Mandarino 	}
1016db2a4165SFrank Mandarino 
101787506549SMark Brown 	if (card->remove)
101887506549SMark Brown 		card->remove(pdev);
1019db2a4165SFrank Mandarino 
1020c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1021c5af3a2eSMark Brown 
1022db2a4165SFrank Mandarino 	return 0;
1023db2a4165SFrank Mandarino }
1024db2a4165SFrank Mandarino 
1025416356fcSMark Brown static int soc_poweroff(struct device *dev)
102651737470SMark Brown {
1027416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
102851737470SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
102951737470SMark Brown 	struct snd_soc_card *card = socdev->card;
103051737470SMark Brown 
103151737470SMark Brown 	if (!card->instantiated)
1032416356fcSMark Brown 		return 0;
103351737470SMark Brown 
103451737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
103551737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
103651737470SMark Brown 	run_delayed_work(&card->delayed_work);
103751737470SMark Brown 
103851737470SMark Brown 	snd_soc_dapm_shutdown(socdev);
1039416356fcSMark Brown 
1040416356fcSMark Brown 	return 0;
104151737470SMark Brown }
104251737470SMark Brown 
1043416356fcSMark Brown static struct dev_pm_ops soc_pm_ops = {
1044416356fcSMark Brown 	.suspend = soc_suspend,
1045416356fcSMark Brown 	.resume = soc_resume,
1046416356fcSMark Brown 	.poweroff = soc_poweroff,
1047416356fcSMark Brown };
1048416356fcSMark Brown 
1049db2a4165SFrank Mandarino /* ASoC platform driver */
1050db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1051db2a4165SFrank Mandarino 	.driver		= {
1052db2a4165SFrank Mandarino 		.name		= "soc-audio",
10538b45a209SKay Sievers 		.owner		= THIS_MODULE,
1054416356fcSMark Brown 		.pm		= &soc_pm_ops,
1055db2a4165SFrank Mandarino 	},
1056db2a4165SFrank Mandarino 	.probe		= soc_probe,
1057db2a4165SFrank Mandarino 	.remove		= soc_remove,
1058db2a4165SFrank Mandarino };
1059db2a4165SFrank Mandarino 
1060db2a4165SFrank Mandarino /* create a new pcm */
1061db2a4165SFrank Mandarino static int soc_new_pcm(struct snd_soc_device *socdev,
1062db2a4165SFrank Mandarino 	struct snd_soc_dai_link *dai_link, int num)
1063db2a4165SFrank Mandarino {
106487689d56SMark Brown 	struct snd_soc_card *card = socdev->card;
10656627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
106687689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
10673c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = dai_link->codec_dai;
10683c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
1069db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd;
1070db2a4165SFrank Mandarino 	struct snd_pcm *pcm;
1071db2a4165SFrank Mandarino 	char new_name[64];
1072db2a4165SFrank Mandarino 	int ret = 0, playback = 0, capture = 0;
1073db2a4165SFrank Mandarino 
1074db2a4165SFrank Mandarino 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1075db2a4165SFrank Mandarino 	if (rtd == NULL)
1076db2a4165SFrank Mandarino 		return -ENOMEM;
1077cb666e5bSLiam Girdwood 
1078cb666e5bSLiam Girdwood 	rtd->dai = dai_link;
1079db2a4165SFrank Mandarino 	rtd->socdev = socdev;
10806627a653SMark Brown 	codec_dai->codec = card->codec;
1081db2a4165SFrank Mandarino 
1082db2a4165SFrank Mandarino 	/* check client and interface hw capabilities */
10833ba9e10aSMark Brown 	sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name,
10843ba9e10aSMark Brown 		num);
1085db2a4165SFrank Mandarino 
1086db2a4165SFrank Mandarino 	if (codec_dai->playback.channels_min)
1087db2a4165SFrank Mandarino 		playback = 1;
1088db2a4165SFrank Mandarino 	if (codec_dai->capture.channels_min)
1089db2a4165SFrank Mandarino 		capture = 1;
1090db2a4165SFrank Mandarino 
1091db2a4165SFrank Mandarino 	ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1092db2a4165SFrank Mandarino 		capture, &pcm);
1093db2a4165SFrank Mandarino 	if (ret < 0) {
10943ff3f64bSMark Brown 		printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
10953ff3f64bSMark Brown 			codec->name);
1096db2a4165SFrank Mandarino 		kfree(rtd);
1097db2a4165SFrank Mandarino 		return ret;
1098db2a4165SFrank Mandarino 	}
1099db2a4165SFrank Mandarino 
11004ccab3e7SLiam Girdwood 	dai_link->pcm = pcm;
1101db2a4165SFrank Mandarino 	pcm->private_data = rtd;
110287689d56SMark Brown 	soc_pcm_ops.mmap = platform->pcm_ops->mmap;
110387689d56SMark Brown 	soc_pcm_ops.pointer = platform->pcm_ops->pointer;
110487689d56SMark Brown 	soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
110587689d56SMark Brown 	soc_pcm_ops.copy = platform->pcm_ops->copy;
110687689d56SMark Brown 	soc_pcm_ops.silence = platform->pcm_ops->silence;
110787689d56SMark Brown 	soc_pcm_ops.ack = platform->pcm_ops->ack;
110887689d56SMark Brown 	soc_pcm_ops.page = platform->pcm_ops->page;
1109db2a4165SFrank Mandarino 
1110db2a4165SFrank Mandarino 	if (playback)
1111db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1112db2a4165SFrank Mandarino 
1113db2a4165SFrank Mandarino 	if (capture)
1114db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1115db2a4165SFrank Mandarino 
111687689d56SMark Brown 	ret = platform->pcm_new(codec->card, codec_dai, pcm);
1117db2a4165SFrank Mandarino 	if (ret < 0) {
1118db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1119db2a4165SFrank Mandarino 		kfree(rtd);
1120db2a4165SFrank Mandarino 		return ret;
1121db2a4165SFrank Mandarino 	}
1122db2a4165SFrank Mandarino 
112387689d56SMark Brown 	pcm->private_free = platform->pcm_free;
1124db2a4165SFrank Mandarino 	printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1125db2a4165SFrank Mandarino 		cpu_dai->name);
1126db2a4165SFrank Mandarino 	return ret;
1127db2a4165SFrank Mandarino }
1128db2a4165SFrank Mandarino 
1129096e49d5SMark Brown /**
1130096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
1131096e49d5SMark Brown  *
1132096e49d5SMark Brown  * @codec: CODEC to query.
1133096e49d5SMark Brown  * @reg: Register to query.
1134096e49d5SMark Brown  *
1135096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
1136096e49d5SMark Brown  */
1137096e49d5SMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1138096e49d5SMark Brown {
1139096e49d5SMark Brown 	if (codec->volatile_register)
1140096e49d5SMark Brown 		return codec->volatile_register(reg);
1141096e49d5SMark Brown 	else
1142096e49d5SMark Brown 		return 0;
1143096e49d5SMark Brown }
1144096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1145096e49d5SMark Brown 
1146db2a4165SFrank Mandarino /* codec register dump */
11476627a653SMark Brown static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
1148db2a4165SFrank Mandarino {
1149db2a4165SFrank Mandarino 	int i, step = 1, count = 0;
1150db2a4165SFrank Mandarino 
1151db2a4165SFrank Mandarino 	if (!codec->reg_cache_size)
1152db2a4165SFrank Mandarino 		return 0;
1153db2a4165SFrank Mandarino 
1154db2a4165SFrank Mandarino 	if (codec->reg_cache_step)
1155db2a4165SFrank Mandarino 		step = codec->reg_cache_step;
1156db2a4165SFrank Mandarino 
1157db2a4165SFrank Mandarino 	count += sprintf(buf, "%s registers\n", codec->name);
115858cd33c0SMark Brown 	for (i = 0; i < codec->reg_cache_size; i += step) {
115977ee09c6SMark Brown 		if (codec->readable_register && !codec->readable_register(i))
116077ee09c6SMark Brown 			continue;
116177ee09c6SMark Brown 
116258cd33c0SMark Brown 		count += sprintf(buf + count, "%2x: ", i);
116358cd33c0SMark Brown 		if (count >= PAGE_SIZE - 1)
116458cd33c0SMark Brown 			break;
116558cd33c0SMark Brown 
116658cd33c0SMark Brown 		if (codec->display_register)
116758cd33c0SMark Brown 			count += codec->display_register(codec, buf + count,
116858cd33c0SMark Brown 							 PAGE_SIZE - count, i);
116958cd33c0SMark Brown 		else
117058cd33c0SMark Brown 			count += snprintf(buf + count, PAGE_SIZE - count,
117158cd33c0SMark Brown 					  "%4x", codec->read(codec, i));
117258cd33c0SMark Brown 
117358cd33c0SMark Brown 		if (count >= PAGE_SIZE - 1)
117458cd33c0SMark Brown 			break;
117558cd33c0SMark Brown 
117658cd33c0SMark Brown 		count += snprintf(buf + count, PAGE_SIZE - count, "\n");
117758cd33c0SMark Brown 		if (count >= PAGE_SIZE - 1)
117858cd33c0SMark Brown 			break;
117958cd33c0SMark Brown 	}
118058cd33c0SMark Brown 
118158cd33c0SMark Brown 	/* Truncate count; min() would cause a warning */
118258cd33c0SMark Brown 	if (count >= PAGE_SIZE)
118358cd33c0SMark Brown 		count = PAGE_SIZE - 1;
1184db2a4165SFrank Mandarino 
1185db2a4165SFrank Mandarino 	return count;
1186db2a4165SFrank Mandarino }
118712ef193dSTroy Kisky static ssize_t codec_reg_show(struct device *dev,
118812ef193dSTroy Kisky 	struct device_attribute *attr, char *buf)
118912ef193dSTroy Kisky {
119012ef193dSTroy Kisky 	struct snd_soc_device *devdata = dev_get_drvdata(dev);
11916627a653SMark Brown 	return soc_codec_reg_show(devdata->card->codec, buf);
119212ef193dSTroy Kisky }
119312ef193dSTroy Kisky 
1194db2a4165SFrank Mandarino static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1195db2a4165SFrank Mandarino 
119612ef193dSTroy Kisky #ifdef CONFIG_DEBUG_FS
119712ef193dSTroy Kisky static int codec_reg_open_file(struct inode *inode, struct file *file)
119812ef193dSTroy Kisky {
119912ef193dSTroy Kisky 	file->private_data = inode->i_private;
120012ef193dSTroy Kisky 	return 0;
120112ef193dSTroy Kisky }
120212ef193dSTroy Kisky 
120312ef193dSTroy Kisky static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
120412ef193dSTroy Kisky 			       size_t count, loff_t *ppos)
120512ef193dSTroy Kisky {
120612ef193dSTroy Kisky 	ssize_t ret;
1207384c89e2SMark Brown 	struct snd_soc_codec *codec = file->private_data;
120812ef193dSTroy Kisky 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
120912ef193dSTroy Kisky 	if (!buf)
121012ef193dSTroy Kisky 		return -ENOMEM;
12116627a653SMark Brown 	ret = soc_codec_reg_show(codec, buf);
121212ef193dSTroy Kisky 	if (ret >= 0)
121312ef193dSTroy Kisky 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
121412ef193dSTroy Kisky 	kfree(buf);
121512ef193dSTroy Kisky 	return ret;
121612ef193dSTroy Kisky }
121712ef193dSTroy Kisky 
121812ef193dSTroy Kisky static ssize_t codec_reg_write_file(struct file *file,
121912ef193dSTroy Kisky 		const char __user *user_buf, size_t count, loff_t *ppos)
122012ef193dSTroy Kisky {
122112ef193dSTroy Kisky 	char buf[32];
122212ef193dSTroy Kisky 	int buf_size;
122312ef193dSTroy Kisky 	char *start = buf;
122412ef193dSTroy Kisky 	unsigned long reg, value;
122512ef193dSTroy Kisky 	int step = 1;
1226384c89e2SMark Brown 	struct snd_soc_codec *codec = file->private_data;
122712ef193dSTroy Kisky 
122812ef193dSTroy Kisky 	buf_size = min(count, (sizeof(buf)-1));
122912ef193dSTroy Kisky 	if (copy_from_user(buf, user_buf, buf_size))
123012ef193dSTroy Kisky 		return -EFAULT;
123112ef193dSTroy Kisky 	buf[buf_size] = 0;
123212ef193dSTroy Kisky 
123312ef193dSTroy Kisky 	if (codec->reg_cache_step)
123412ef193dSTroy Kisky 		step = codec->reg_cache_step;
123512ef193dSTroy Kisky 
123612ef193dSTroy Kisky 	while (*start == ' ')
123712ef193dSTroy Kisky 		start++;
123812ef193dSTroy Kisky 	reg = simple_strtoul(start, &start, 16);
123912ef193dSTroy Kisky 	if ((reg >= codec->reg_cache_size) || (reg % step))
124012ef193dSTroy Kisky 		return -EINVAL;
124112ef193dSTroy Kisky 	while (*start == ' ')
124212ef193dSTroy Kisky 		start++;
124312ef193dSTroy Kisky 	if (strict_strtoul(start, 16, &value))
124412ef193dSTroy Kisky 		return -EINVAL;
124512ef193dSTroy Kisky 	codec->write(codec, reg, value);
124612ef193dSTroy Kisky 	return buf_size;
124712ef193dSTroy Kisky }
124812ef193dSTroy Kisky 
124912ef193dSTroy Kisky static const struct file_operations codec_reg_fops = {
125012ef193dSTroy Kisky 	.open = codec_reg_open_file,
125112ef193dSTroy Kisky 	.read = codec_reg_read_file,
125212ef193dSTroy Kisky 	.write = codec_reg_write_file,
125312ef193dSTroy Kisky };
125412ef193dSTroy Kisky 
1255384c89e2SMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
125612ef193dSTroy Kisky {
1257384c89e2SMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
1258384c89e2SMark Brown 						 debugfs_root, codec,
1259384c89e2SMark Brown 						 &codec_reg_fops);
1260384c89e2SMark Brown 	if (!codec->debugfs_reg)
1261384c89e2SMark Brown 		printk(KERN_WARNING
1262384c89e2SMark Brown 		       "ASoC: Failed to create codec register debugfs file\n");
126312ef193dSTroy Kisky 
1264384c89e2SMark Brown 	codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
1265384c89e2SMark Brown 						     debugfs_root,
1266384c89e2SMark Brown 						     &codec->pop_time);
1267384c89e2SMark Brown 	if (!codec->debugfs_pop_time)
1268384c89e2SMark Brown 		printk(KERN_WARNING
1269384c89e2SMark Brown 		       "Failed to create pop time debugfs file\n");
127079fb9387SMark Brown 
127179fb9387SMark Brown 	codec->debugfs_dapm = debugfs_create_dir("dapm", debugfs_root);
127279fb9387SMark Brown 	if (!codec->debugfs_dapm)
127379fb9387SMark Brown 		printk(KERN_WARNING
127479fb9387SMark Brown 		       "Failed to create DAPM debugfs directory\n");
127579fb9387SMark Brown 
127679fb9387SMark Brown 	snd_soc_dapm_debugfs_init(codec);
127712ef193dSTroy Kisky }
127812ef193dSTroy Kisky 
1279384c89e2SMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
128012ef193dSTroy Kisky {
128179fb9387SMark Brown 	debugfs_remove_recursive(codec->debugfs_dapm);
1282384c89e2SMark Brown 	debugfs_remove(codec->debugfs_pop_time);
1283384c89e2SMark Brown 	debugfs_remove(codec->debugfs_reg);
128412ef193dSTroy Kisky }
128512ef193dSTroy Kisky 
128612ef193dSTroy Kisky #else
128712ef193dSTroy Kisky 
1288384c89e2SMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
128912ef193dSTroy Kisky {
129012ef193dSTroy Kisky }
129112ef193dSTroy Kisky 
1292384c89e2SMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
129312ef193dSTroy Kisky {
129412ef193dSTroy Kisky }
129512ef193dSTroy Kisky #endif
129612ef193dSTroy Kisky 
1297db2a4165SFrank Mandarino /**
1298db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
1299db2a4165SFrank Mandarino  * @codec: audio codec
1300db2a4165SFrank Mandarino  * @ops: AC97 bus operations
1301db2a4165SFrank Mandarino  * @num: AC97 codec number
1302db2a4165SFrank Mandarino  *
1303db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
1304db2a4165SFrank Mandarino  */
1305db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1306db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
1307db2a4165SFrank Mandarino {
1308db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1309db2a4165SFrank Mandarino 
1310db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1311db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
1312db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1313db2a4165SFrank Mandarino 		return -ENOMEM;
1314db2a4165SFrank Mandarino 	}
1315db2a4165SFrank Mandarino 
1316db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1317db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
1318db2a4165SFrank Mandarino 		kfree(codec->ac97);
1319db2a4165SFrank Mandarino 		codec->ac97 = NULL;
1320db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1321db2a4165SFrank Mandarino 		return -ENOMEM;
1322db2a4165SFrank Mandarino 	}
1323db2a4165SFrank Mandarino 
1324db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
1325db2a4165SFrank Mandarino 	codec->ac97->num = num;
1326db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1327db2a4165SFrank Mandarino 	return 0;
1328db2a4165SFrank Mandarino }
1329db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1330db2a4165SFrank Mandarino 
1331db2a4165SFrank Mandarino /**
1332db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
1333db2a4165SFrank Mandarino  * @codec: audio codec
1334db2a4165SFrank Mandarino  *
1335db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
1336db2a4165SFrank Mandarino  */
1337db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1338db2a4165SFrank Mandarino {
1339db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1340db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
1341db2a4165SFrank Mandarino 	kfree(codec->ac97);
1342db2a4165SFrank Mandarino 	codec->ac97 = NULL;
1343db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1344db2a4165SFrank Mandarino }
1345db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1346db2a4165SFrank Mandarino 
1347db2a4165SFrank Mandarino /**
1348db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
1349db2a4165SFrank Mandarino  * @codec: audio codec
1350db2a4165SFrank Mandarino  * @reg: codec register
1351db2a4165SFrank Mandarino  * @mask: register mask
1352db2a4165SFrank Mandarino  * @value: new value
1353db2a4165SFrank Mandarino  *
1354db2a4165SFrank Mandarino  * Writes new register value.
1355db2a4165SFrank Mandarino  *
1356db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1357db2a4165SFrank Mandarino  */
1358db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
135946f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1360db2a4165SFrank Mandarino {
1361db2a4165SFrank Mandarino 	int change;
136246f5822fSDaniel Ribeiro 	unsigned int old, new;
1363db2a4165SFrank Mandarino 
1364db2a4165SFrank Mandarino 	mutex_lock(&io_mutex);
1365db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
1366db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
1367db2a4165SFrank Mandarino 	change = old != new;
1368db2a4165SFrank Mandarino 	if (change)
1369db2a4165SFrank Mandarino 		snd_soc_write(codec, reg, new);
1370db2a4165SFrank Mandarino 
1371db2a4165SFrank Mandarino 	mutex_unlock(&io_mutex);
1372db2a4165SFrank Mandarino 	return change;
1373db2a4165SFrank Mandarino }
1374db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1375db2a4165SFrank Mandarino 
1376db2a4165SFrank Mandarino /**
1377db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
1378db2a4165SFrank Mandarino  * @codec: audio codec
1379db2a4165SFrank Mandarino  * @reg: codec register
1380db2a4165SFrank Mandarino  * @mask: register mask
1381db2a4165SFrank Mandarino  * @value: new value
1382db2a4165SFrank Mandarino  *
1383db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
1384db2a4165SFrank Mandarino  * different from the old value.
1385db2a4165SFrank Mandarino  *
1386db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1387db2a4165SFrank Mandarino  */
1388db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
138946f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1390db2a4165SFrank Mandarino {
1391db2a4165SFrank Mandarino 	int change;
139246f5822fSDaniel Ribeiro 	unsigned int old, new;
1393db2a4165SFrank Mandarino 
1394db2a4165SFrank Mandarino 	mutex_lock(&io_mutex);
1395db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
1396db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
1397db2a4165SFrank Mandarino 	change = old != new;
1398db2a4165SFrank Mandarino 	mutex_unlock(&io_mutex);
1399db2a4165SFrank Mandarino 
1400db2a4165SFrank Mandarino 	return change;
1401db2a4165SFrank Mandarino }
1402db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1403db2a4165SFrank Mandarino 
1404db2a4165SFrank Mandarino /**
1405db2a4165SFrank Mandarino  * snd_soc_new_pcms - create new sound card and pcms
1406db2a4165SFrank Mandarino  * @socdev: the SoC audio device
1407ac11a2b3SMark Brown  * @idx: ALSA card index
1408ac11a2b3SMark Brown  * @xid: card identification
1409db2a4165SFrank Mandarino  *
1410db2a4165SFrank Mandarino  * Create a new sound card based upon the codec and interface pcms.
1411db2a4165SFrank Mandarino  *
1412db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1413db2a4165SFrank Mandarino  */
1414db2a4165SFrank Mandarino int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1415db2a4165SFrank Mandarino {
141687506549SMark Brown 	struct snd_soc_card *card = socdev->card;
14176627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
1418bd7dd77cSTakashi Iwai 	int ret, i;
1419db2a4165SFrank Mandarino 
1420db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1421db2a4165SFrank Mandarino 
1422db2a4165SFrank Mandarino 	/* register a sound card */
1423bd7dd77cSTakashi Iwai 	ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1424bd7dd77cSTakashi Iwai 	if (ret < 0) {
1425db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1426db2a4165SFrank Mandarino 			codec->name);
1427db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1428bd7dd77cSTakashi Iwai 		return ret;
1429db2a4165SFrank Mandarino 	}
1430db2a4165SFrank Mandarino 
1431452c5eaaSMark Brown 	codec->socdev = socdev;
1432db2a4165SFrank Mandarino 	codec->card->dev = socdev->dev;
1433db2a4165SFrank Mandarino 	codec->card->private_data = codec;
1434db2a4165SFrank Mandarino 	strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1435db2a4165SFrank Mandarino 
1436db2a4165SFrank Mandarino 	/* create the pcms */
143787506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
143887506549SMark Brown 		ret = soc_new_pcm(socdev, &card->dai_link[i], i);
1439db2a4165SFrank Mandarino 		if (ret < 0) {
1440db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't create pcm %s\n",
144187506549SMark Brown 				card->dai_link[i].stream_name);
1442db2a4165SFrank Mandarino 			mutex_unlock(&codec->mutex);
1443db2a4165SFrank Mandarino 			return ret;
1444db2a4165SFrank Mandarino 		}
1445db2a4165SFrank Mandarino 	}
1446db2a4165SFrank Mandarino 
1447db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1448db2a4165SFrank Mandarino 	return ret;
1449db2a4165SFrank Mandarino }
1450db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1451db2a4165SFrank Mandarino 
1452db2a4165SFrank Mandarino /**
1453968a6025SMark Brown  * snd_soc_init_card - register sound card
1454db2a4165SFrank Mandarino  * @socdev: the SoC audio device
1455db2a4165SFrank Mandarino  *
1456db2a4165SFrank Mandarino  * Register a SoC sound card. Also registers an AC97 device if the
1457db2a4165SFrank Mandarino  * codec is AC97 for ad hoc devices.
1458db2a4165SFrank Mandarino  *
1459db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1460db2a4165SFrank Mandarino  */
1461968a6025SMark Brown int snd_soc_init_card(struct snd_soc_device *socdev)
1462db2a4165SFrank Mandarino {
146387506549SMark Brown 	struct snd_soc_card *card = socdev->card;
14646627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
146512e74f7dSLiam Girdwood 	int ret = 0, i, ac97 = 0, err = 0;
1466db2a4165SFrank Mandarino 
146787506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
146887506549SMark Brown 		if (card->dai_link[i].init) {
146987506549SMark Brown 			err = card->dai_link[i].init(codec);
147012e74f7dSLiam Girdwood 			if (err < 0) {
147112e74f7dSLiam Girdwood 				printk(KERN_ERR "asoc: failed to init %s\n",
147287506549SMark Brown 					card->dai_link[i].stream_name);
147312e74f7dSLiam Girdwood 				continue;
147412e74f7dSLiam Girdwood 			}
147512e74f7dSLiam Girdwood 		}
1476474828a4SMarek Vasut 		if (card->dai_link[i].codec_dai->ac97_control) {
1477db2a4165SFrank Mandarino 			ac97 = 1;
1478474828a4SMarek Vasut 			snd_ac97_dev_add_pdata(codec->ac97,
1479474828a4SMarek Vasut 				card->dai_link[i].cpu_dai->ac97_pdata);
1480474828a4SMarek Vasut 		}
1481db2a4165SFrank Mandarino 	}
1482db2a4165SFrank Mandarino 	snprintf(codec->card->shortname, sizeof(codec->card->shortname),
148387506549SMark Brown 		 "%s",  card->name);
1484db2a4165SFrank Mandarino 	snprintf(codec->card->longname, sizeof(codec->card->longname),
148587506549SMark Brown 		 "%s (%s)", card->name, codec->name);
1486db2a4165SFrank Mandarino 
14876d5701b2SMark Brown 	/* Make sure all DAPM widgets are instantiated */
14886d5701b2SMark Brown 	snd_soc_dapm_new_widgets(codec);
14896d5701b2SMark Brown 
1490db2a4165SFrank Mandarino 	ret = snd_card_register(codec->card);
1491db2a4165SFrank Mandarino 	if (ret < 0) {
14923ff3f64bSMark Brown 		printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1493db2a4165SFrank Mandarino 				codec->name);
149412e74f7dSLiam Girdwood 		goto out;
1495db2a4165SFrank Mandarino 	}
1496db2a4165SFrank Mandarino 
149708c8efe6SMark Brown 	mutex_lock(&codec->mutex);
1498db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
149914fa43f5SMark Brown 	/* Only instantiate AC97 if not already done by the adaptor
150014fa43f5SMark Brown 	 * for the generic AC97 subsystem.
150114fa43f5SMark Brown 	 */
150214fa43f5SMark Brown 	if (ac97 && strcmp(codec->name, "AC97") != 0) {
150312e74f7dSLiam Girdwood 		ret = soc_ac97_dev_register(codec);
150412e74f7dSLiam Girdwood 		if (ret < 0) {
150512e74f7dSLiam Girdwood 			printk(KERN_ERR "asoc: AC97 device register failed\n");
150612e74f7dSLiam Girdwood 			snd_card_free(codec->card);
150708c8efe6SMark Brown 			mutex_unlock(&codec->mutex);
150812e74f7dSLiam Girdwood 			goto out;
150912e74f7dSLiam Girdwood 		}
151012e74f7dSLiam Girdwood 	}
1511db2a4165SFrank Mandarino #endif
1512db2a4165SFrank Mandarino 
151312e74f7dSLiam Girdwood 	err = snd_soc_dapm_sys_add(socdev->dev);
151412e74f7dSLiam Girdwood 	if (err < 0)
151512e74f7dSLiam Girdwood 		printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
151612e74f7dSLiam Girdwood 
151712e74f7dSLiam Girdwood 	err = device_create_file(socdev->dev, &dev_attr_codec_reg);
151812e74f7dSLiam Girdwood 	if (err < 0)
15193ff3f64bSMark Brown 		printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
152008c8efe6SMark Brown 
15216627a653SMark Brown 	soc_init_codec_debugfs(codec);
1522db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
152308c8efe6SMark Brown 
152408c8efe6SMark Brown out:
1525db2a4165SFrank Mandarino 	return ret;
1526db2a4165SFrank Mandarino }
1527968a6025SMark Brown EXPORT_SYMBOL_GPL(snd_soc_init_card);
1528db2a4165SFrank Mandarino 
1529db2a4165SFrank Mandarino /**
1530db2a4165SFrank Mandarino  * snd_soc_free_pcms - free sound card and pcms
1531db2a4165SFrank Mandarino  * @socdev: the SoC audio device
1532db2a4165SFrank Mandarino  *
1533db2a4165SFrank Mandarino  * Frees sound card and pcms associated with the socdev.
1534db2a4165SFrank Mandarino  * Also unregister the codec if it is an AC97 device.
1535db2a4165SFrank Mandarino  */
1536db2a4165SFrank Mandarino void snd_soc_free_pcms(struct snd_soc_device *socdev)
1537db2a4165SFrank Mandarino {
15386627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
1539a68660e0SLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
15403c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai;
1541a68660e0SLiam Girdwood 	int i;
1542a68660e0SLiam Girdwood #endif
1543db2a4165SFrank Mandarino 
1544db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
15456627a653SMark Brown 	soc_cleanup_codec_debugfs(codec);
1546db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
1547a68660e0SLiam Girdwood 	for (i = 0; i < codec->num_dai; i++) {
1548a68660e0SLiam Girdwood 		codec_dai = &codec->dai[i];
1549d2314e0eSAtsushi Nemoto 		if (codec_dai->ac97_control && codec->ac97 &&
1550d2314e0eSAtsushi Nemoto 		    strcmp(codec->name, "AC97") != 0) {
1551db2a4165SFrank Mandarino 			soc_ac97_dev_unregister(codec);
1552a68660e0SLiam Girdwood 			goto free_card;
1553a68660e0SLiam Girdwood 		}
1554a68660e0SLiam Girdwood 	}
1555a68660e0SLiam Girdwood free_card:
1556db2a4165SFrank Mandarino #endif
1557db2a4165SFrank Mandarino 
1558db2a4165SFrank Mandarino 	if (codec->card)
1559db2a4165SFrank Mandarino 		snd_card_free(codec->card);
1560db2a4165SFrank Mandarino 	device_remove_file(socdev->dev, &dev_attr_codec_reg);
1561db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1562db2a4165SFrank Mandarino }
1563db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1564db2a4165SFrank Mandarino 
1565db2a4165SFrank Mandarino /**
1566db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1567db2a4165SFrank Mandarino  * @substream: the pcm substream
1568db2a4165SFrank Mandarino  * @hw: the hardware parameters
1569db2a4165SFrank Mandarino  *
1570db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
1571db2a4165SFrank Mandarino  */
1572db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1573db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
1574db2a4165SFrank Mandarino {
1575db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
1576db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
1577db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
1578db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
1579db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
1580db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
1581db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
1582db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1583db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
1584db2a4165SFrank Mandarino 	return 0;
1585db2a4165SFrank Mandarino }
1586db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1587db2a4165SFrank Mandarino 
1588db2a4165SFrank Mandarino /**
1589db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
1590db2a4165SFrank Mandarino  * @_template: control template
1591db2a4165SFrank Mandarino  * @data: control private data
1592ac11a2b3SMark Brown  * @long_name: control long name
1593db2a4165SFrank Mandarino  *
1594db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
1595db2a4165SFrank Mandarino  *
1596db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1597db2a4165SFrank Mandarino  */
1598db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1599db2a4165SFrank Mandarino 	void *data, char *long_name)
1600db2a4165SFrank Mandarino {
1601db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
1602db2a4165SFrank Mandarino 
1603db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
1604db2a4165SFrank Mandarino 	if (long_name)
1605db2a4165SFrank Mandarino 		template.name = long_name;
1606db2a4165SFrank Mandarino 	template.index = 0;
1607db2a4165SFrank Mandarino 
1608db2a4165SFrank Mandarino 	return snd_ctl_new1(&template, data);
1609db2a4165SFrank Mandarino }
1610db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
1611db2a4165SFrank Mandarino 
1612db2a4165SFrank Mandarino /**
16133e8e1952SIan Molton  * snd_soc_add_controls - add an array of controls to a codec.
16143e8e1952SIan Molton  * Convienience function to add a list of controls. Many codecs were
16153e8e1952SIan Molton  * duplicating this code.
16163e8e1952SIan Molton  *
16173e8e1952SIan Molton  * @codec: codec to add controls to
16183e8e1952SIan Molton  * @controls: array of controls to add
16193e8e1952SIan Molton  * @num_controls: number of elements in the array
16203e8e1952SIan Molton  *
16213e8e1952SIan Molton  * Return 0 for success, else error.
16223e8e1952SIan Molton  */
16233e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec,
16243e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
16253e8e1952SIan Molton {
16263e8e1952SIan Molton 	struct snd_card *card = codec->card;
16273e8e1952SIan Molton 	int err, i;
16283e8e1952SIan Molton 
16293e8e1952SIan Molton 	for (i = 0; i < num_controls; i++) {
16303e8e1952SIan Molton 		const struct snd_kcontrol_new *control = &controls[i];
16313e8e1952SIan Molton 		err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
16323e8e1952SIan Molton 		if (err < 0) {
16333e8e1952SIan Molton 			dev_err(codec->dev, "%s: Failed to add %s\n",
16343e8e1952SIan Molton 				codec->name, control->name);
16353e8e1952SIan Molton 			return err;
16363e8e1952SIan Molton 		}
16373e8e1952SIan Molton 	}
16383e8e1952SIan Molton 
16393e8e1952SIan Molton 	return 0;
16403e8e1952SIan Molton }
16413e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls);
16423e8e1952SIan Molton 
16433e8e1952SIan Molton /**
1644db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
1645db2a4165SFrank Mandarino  * @kcontrol: mixer control
1646db2a4165SFrank Mandarino  * @uinfo: control element information
1647db2a4165SFrank Mandarino  *
1648db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
1649db2a4165SFrank Mandarino  * mixer control.
1650db2a4165SFrank Mandarino  *
1651db2a4165SFrank Mandarino  * Returns 0 for success.
1652db2a4165SFrank Mandarino  */
1653db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1654db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1655db2a4165SFrank Mandarino {
1656db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1657db2a4165SFrank Mandarino 
1658db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1659db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1660f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
1661db2a4165SFrank Mandarino 
1662f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
1663f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
1664db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
1665db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
1666db2a4165SFrank Mandarino 	return 0;
1667db2a4165SFrank Mandarino }
1668db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1669db2a4165SFrank Mandarino 
1670db2a4165SFrank Mandarino /**
1671db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
1672db2a4165SFrank Mandarino  * @kcontrol: mixer control
1673ac11a2b3SMark Brown  * @ucontrol: control element information
1674db2a4165SFrank Mandarino  *
1675db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
1676db2a4165SFrank Mandarino  *
1677db2a4165SFrank Mandarino  * Returns 0 for success.
1678db2a4165SFrank Mandarino  */
1679db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1680db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1681db2a4165SFrank Mandarino {
1682db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1683db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
168446f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
1685db2a4165SFrank Mandarino 
1686f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1687db2a4165SFrank Mandarino 		;
1688db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
16893ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
16903ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
1691db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
1692db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
1693db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
1694db2a4165SFrank Mandarino 
1695db2a4165SFrank Mandarino 	return 0;
1696db2a4165SFrank Mandarino }
1697db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1698db2a4165SFrank Mandarino 
1699db2a4165SFrank Mandarino /**
1700db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
1701db2a4165SFrank Mandarino  * @kcontrol: mixer control
1702ac11a2b3SMark Brown  * @ucontrol: control element information
1703db2a4165SFrank Mandarino  *
1704db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
1705db2a4165SFrank Mandarino  *
1706db2a4165SFrank Mandarino  * Returns 0 for success.
1707db2a4165SFrank Mandarino  */
1708db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1709db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1710db2a4165SFrank Mandarino {
1711db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1712db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
171346f5822fSDaniel Ribeiro 	unsigned int val;
171446f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
1715db2a4165SFrank Mandarino 
1716f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1717db2a4165SFrank Mandarino 		;
1718f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
1719db2a4165SFrank Mandarino 		return -EINVAL;
1720db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
1721db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
1722db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
1723f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
1724db2a4165SFrank Mandarino 			return -EINVAL;
1725db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1726db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
1727db2a4165SFrank Mandarino 	}
1728db2a4165SFrank Mandarino 
1729db2a4165SFrank Mandarino 	return snd_soc_update_bits(codec, e->reg, mask, val);
1730db2a4165SFrank Mandarino }
1731db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1732db2a4165SFrank Mandarino 
1733db2a4165SFrank Mandarino /**
17342e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
17352e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
17362e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
17372e72f8e3SPeter Ujfalusi  *
17382e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
17392e72f8e3SPeter Ujfalusi  *
17402e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
17412e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
17422e72f8e3SPeter Ujfalusi  *
17432e72f8e3SPeter Ujfalusi  * Returns 0 for success.
17442e72f8e3SPeter Ujfalusi  */
17452e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
17462e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
17472e72f8e3SPeter Ujfalusi {
17482e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
174974155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
175046f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
17512e72f8e3SPeter Ujfalusi 
17522e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
17532e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
17542e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
17552e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
17562e72f8e3SPeter Ujfalusi 			break;
17572e72f8e3SPeter Ujfalusi 	}
17582e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
17592e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
17602e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
17612e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
17622e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
17632e72f8e3SPeter Ujfalusi 				break;
17642e72f8e3SPeter Ujfalusi 		}
17652e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
17662e72f8e3SPeter Ujfalusi 	}
17672e72f8e3SPeter Ujfalusi 
17682e72f8e3SPeter Ujfalusi 	return 0;
17692e72f8e3SPeter Ujfalusi }
17702e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
17712e72f8e3SPeter Ujfalusi 
17722e72f8e3SPeter Ujfalusi /**
17732e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
17742e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
17752e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
17762e72f8e3SPeter Ujfalusi  *
17772e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
17782e72f8e3SPeter Ujfalusi  *
17792e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
17802e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
17812e72f8e3SPeter Ujfalusi  *
17822e72f8e3SPeter Ujfalusi  * Returns 0 for success.
17832e72f8e3SPeter Ujfalusi  */
17842e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
17852e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
17862e72f8e3SPeter Ujfalusi {
17872e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
178874155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
178946f5822fSDaniel Ribeiro 	unsigned int val;
179046f5822fSDaniel Ribeiro 	unsigned int mask;
17912e72f8e3SPeter Ujfalusi 
17922e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
17932e72f8e3SPeter Ujfalusi 		return -EINVAL;
17942e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
17952e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
17962e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
17972e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
17982e72f8e3SPeter Ujfalusi 			return -EINVAL;
17992e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
18002e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
18012e72f8e3SPeter Ujfalusi 	}
18022e72f8e3SPeter Ujfalusi 
18032e72f8e3SPeter Ujfalusi 	return snd_soc_update_bits(codec, e->reg, mask, val);
18042e72f8e3SPeter Ujfalusi }
18052e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
18062e72f8e3SPeter Ujfalusi 
18072e72f8e3SPeter Ujfalusi /**
1808db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
1809db2a4165SFrank Mandarino  * @kcontrol: mixer control
1810db2a4165SFrank Mandarino  * @uinfo: control element information
1811db2a4165SFrank Mandarino  *
1812db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
1813db2a4165SFrank Mandarino  * single mixer.
1814db2a4165SFrank Mandarino  *
1815db2a4165SFrank Mandarino  * Returns 0 for success.
1816db2a4165SFrank Mandarino  */
1817db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1818db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1819db2a4165SFrank Mandarino {
1820db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1821db2a4165SFrank Mandarino 
1822db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1823db2a4165SFrank Mandarino 	uinfo->count = 1;
1824f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
1825db2a4165SFrank Mandarino 
1826f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
1827f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
1828db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
1829db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
1830db2a4165SFrank Mandarino 	return 0;
1831db2a4165SFrank Mandarino }
1832db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1833db2a4165SFrank Mandarino 
1834db2a4165SFrank Mandarino /**
1835db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
1836db2a4165SFrank Mandarino  * @kcontrol: mixer control
1837db2a4165SFrank Mandarino  * @uinfo: control element information
1838db2a4165SFrank Mandarino  *
1839db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
1840db2a4165SFrank Mandarino  *
1841db2a4165SFrank Mandarino  * Returns 0 for success.
1842db2a4165SFrank Mandarino  */
1843db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1844db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1845db2a4165SFrank Mandarino {
1846a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
1847db2a4165SFrank Mandarino 
1848fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
1849a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1850a7a4ac86SPhilipp Zabel 	else
1851a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1852a7a4ac86SPhilipp Zabel 
1853db2a4165SFrank Mandarino 	uinfo->count = 1;
1854db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
1855a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
1856db2a4165SFrank Mandarino 	return 0;
1857db2a4165SFrank Mandarino }
1858db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1859db2a4165SFrank Mandarino 
1860db2a4165SFrank Mandarino /**
1861db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
1862db2a4165SFrank Mandarino  * @kcontrol: mixer control
1863db2a4165SFrank Mandarino  * @uinfo: control element information
1864db2a4165SFrank Mandarino  *
1865db2a4165SFrank Mandarino  * Callback to provide information about a single mixer control.
1866db2a4165SFrank Mandarino  *
1867db2a4165SFrank Mandarino  * Returns 0 for success.
1868db2a4165SFrank Mandarino  */
1869db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1870db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1871db2a4165SFrank Mandarino {
18724eaa9819SJon Smirl 	struct soc_mixer_control *mc =
18734eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
18744eaa9819SJon Smirl 	int max = mc->max;
1875762b8df7SMark Brown 	unsigned int shift = mc->shift;
1876815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
1877db2a4165SFrank Mandarino 
1878fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
1879a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1880a7a4ac86SPhilipp Zabel 	else
1881a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1882a7a4ac86SPhilipp Zabel 
1883db2a4165SFrank Mandarino 	uinfo->count = shift == rshift ? 1 : 2;
1884db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
1885a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
1886db2a4165SFrank Mandarino 	return 0;
1887db2a4165SFrank Mandarino }
1888db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1889db2a4165SFrank Mandarino 
1890db2a4165SFrank Mandarino /**
1891db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
1892db2a4165SFrank Mandarino  * @kcontrol: mixer control
1893ac11a2b3SMark Brown  * @ucontrol: control element information
1894db2a4165SFrank Mandarino  *
1895db2a4165SFrank Mandarino  * Callback to get the value of a single mixer control.
1896db2a4165SFrank Mandarino  *
1897db2a4165SFrank Mandarino  * Returns 0 for success.
1898db2a4165SFrank Mandarino  */
1899db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1900db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1901db2a4165SFrank Mandarino {
19024eaa9819SJon Smirl 	struct soc_mixer_control *mc =
19034eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
1904db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1905815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
1906815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
1907815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
19084eaa9819SJon Smirl 	int max = mc->max;
1909815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
1910815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
1911db2a4165SFrank Mandarino 
1912db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
1913db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
1914db2a4165SFrank Mandarino 	if (shift != rshift)
1915db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
1916db2a4165SFrank Mandarino 			(snd_soc_read(codec, reg) >> rshift) & mask;
1917db2a4165SFrank Mandarino 	if (invert) {
1918db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
1919a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
1920db2a4165SFrank Mandarino 		if (shift != rshift)
1921db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
1922a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
1923db2a4165SFrank Mandarino 	}
1924db2a4165SFrank Mandarino 
1925db2a4165SFrank Mandarino 	return 0;
1926db2a4165SFrank Mandarino }
1927db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1928db2a4165SFrank Mandarino 
1929db2a4165SFrank Mandarino /**
1930db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
1931db2a4165SFrank Mandarino  * @kcontrol: mixer control
1932ac11a2b3SMark Brown  * @ucontrol: control element information
1933db2a4165SFrank Mandarino  *
1934db2a4165SFrank Mandarino  * Callback to set the value of a single mixer control.
1935db2a4165SFrank Mandarino  *
1936db2a4165SFrank Mandarino  * Returns 0 for success.
1937db2a4165SFrank Mandarino  */
1938db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1939db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1940db2a4165SFrank Mandarino {
19414eaa9819SJon Smirl 	struct soc_mixer_control *mc =
19424eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
1943db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1944815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
1945815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
1946815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
19474eaa9819SJon Smirl 	int max = mc->max;
1948815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
1949815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
195046f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
1951db2a4165SFrank Mandarino 
1952db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
1953db2a4165SFrank Mandarino 	if (invert)
1954a7a4ac86SPhilipp Zabel 		val = max - val;
1955db2a4165SFrank Mandarino 	val_mask = mask << shift;
1956db2a4165SFrank Mandarino 	val = val << shift;
1957db2a4165SFrank Mandarino 	if (shift != rshift) {
1958db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
1959db2a4165SFrank Mandarino 		if (invert)
1960a7a4ac86SPhilipp Zabel 			val2 = max - val2;
1961db2a4165SFrank Mandarino 		val_mask |= mask << rshift;
1962db2a4165SFrank Mandarino 		val |= val2 << rshift;
1963db2a4165SFrank Mandarino 	}
1964a7a4ac86SPhilipp Zabel 	return snd_soc_update_bits(codec, reg, val_mask, val);
1965db2a4165SFrank Mandarino }
1966db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1967db2a4165SFrank Mandarino 
1968db2a4165SFrank Mandarino /**
1969db2a4165SFrank Mandarino  * snd_soc_info_volsw_2r - double mixer info callback
1970db2a4165SFrank Mandarino  * @kcontrol: mixer control
1971db2a4165SFrank Mandarino  * @uinfo: control element information
1972db2a4165SFrank Mandarino  *
1973db2a4165SFrank Mandarino  * Callback to provide information about a double mixer control that
1974db2a4165SFrank Mandarino  * spans 2 codec registers.
1975db2a4165SFrank Mandarino  *
1976db2a4165SFrank Mandarino  * Returns 0 for success.
1977db2a4165SFrank Mandarino  */
1978db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1979db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1980db2a4165SFrank Mandarino {
19814eaa9819SJon Smirl 	struct soc_mixer_control *mc =
19824eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
19834eaa9819SJon Smirl 	int max = mc->max;
1984db2a4165SFrank Mandarino 
1985fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
1986a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1987a7a4ac86SPhilipp Zabel 	else
1988a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1989a7a4ac86SPhilipp Zabel 
1990db2a4165SFrank Mandarino 	uinfo->count = 2;
1991db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
1992a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
1993db2a4165SFrank Mandarino 	return 0;
1994db2a4165SFrank Mandarino }
1995db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1996db2a4165SFrank Mandarino 
1997db2a4165SFrank Mandarino /**
1998db2a4165SFrank Mandarino  * snd_soc_get_volsw_2r - double mixer get callback
1999db2a4165SFrank Mandarino  * @kcontrol: mixer control
2000ac11a2b3SMark Brown  * @ucontrol: control element information
2001db2a4165SFrank Mandarino  *
2002db2a4165SFrank Mandarino  * Callback to get the value of a double mixer control that spans 2 registers.
2003db2a4165SFrank Mandarino  *
2004db2a4165SFrank Mandarino  * Returns 0 for success.
2005db2a4165SFrank Mandarino  */
2006db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2007db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2008db2a4165SFrank Mandarino {
20094eaa9819SJon Smirl 	struct soc_mixer_control *mc =
20104eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2011db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2012815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2013815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2014815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
20154eaa9819SJon Smirl 	int max = mc->max;
2016815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2017815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2018db2a4165SFrank Mandarino 
2019db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2020db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2021db2a4165SFrank Mandarino 	ucontrol->value.integer.value[1] =
2022db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg2) >> shift) & mask;
2023db2a4165SFrank Mandarino 	if (invert) {
2024db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2025a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2026db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2027a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[1];
2028db2a4165SFrank Mandarino 	}
2029db2a4165SFrank Mandarino 
2030db2a4165SFrank Mandarino 	return 0;
2031db2a4165SFrank Mandarino }
2032db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2033db2a4165SFrank Mandarino 
2034db2a4165SFrank Mandarino /**
2035db2a4165SFrank Mandarino  * snd_soc_put_volsw_2r - double mixer set callback
2036db2a4165SFrank Mandarino  * @kcontrol: mixer control
2037ac11a2b3SMark Brown  * @ucontrol: control element information
2038db2a4165SFrank Mandarino  *
2039db2a4165SFrank Mandarino  * Callback to set the value of a double mixer control that spans 2 registers.
2040db2a4165SFrank Mandarino  *
2041db2a4165SFrank Mandarino  * Returns 0 for success.
2042db2a4165SFrank Mandarino  */
2043db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2044db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2045db2a4165SFrank Mandarino {
20464eaa9819SJon Smirl 	struct soc_mixer_control *mc =
20474eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2048db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2049815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2050815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2051815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
20524eaa9819SJon Smirl 	int max = mc->max;
2053815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2054815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2055db2a4165SFrank Mandarino 	int err;
205646f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2057db2a4165SFrank Mandarino 
2058db2a4165SFrank Mandarino 	val_mask = mask << shift;
2059db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2060db2a4165SFrank Mandarino 	val2 = (ucontrol->value.integer.value[1] & mask);
2061db2a4165SFrank Mandarino 
2062db2a4165SFrank Mandarino 	if (invert) {
2063a7a4ac86SPhilipp Zabel 		val = max - val;
2064a7a4ac86SPhilipp Zabel 		val2 = max - val2;
2065db2a4165SFrank Mandarino 	}
2066db2a4165SFrank Mandarino 
2067db2a4165SFrank Mandarino 	val = val << shift;
2068db2a4165SFrank Mandarino 	val2 = val2 << shift;
2069db2a4165SFrank Mandarino 
20703ff3f64bSMark Brown 	err = snd_soc_update_bits(codec, reg, val_mask, val);
20713ff3f64bSMark Brown 	if (err < 0)
2072db2a4165SFrank Mandarino 		return err;
2073db2a4165SFrank Mandarino 
2074db2a4165SFrank Mandarino 	err = snd_soc_update_bits(codec, reg2, val_mask, val2);
2075db2a4165SFrank Mandarino 	return err;
2076db2a4165SFrank Mandarino }
2077db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2078db2a4165SFrank Mandarino 
2079e13ac2e9SMark Brown /**
2080e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2081e13ac2e9SMark Brown  * @kcontrol: mixer control
2082e13ac2e9SMark Brown  * @uinfo: control element information
2083e13ac2e9SMark Brown  *
2084e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2085e13ac2e9SMark Brown  *
2086e13ac2e9SMark Brown  * Returns 0 for success.
2087e13ac2e9SMark Brown  */
2088e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2089e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2090e13ac2e9SMark Brown {
20914eaa9819SJon Smirl 	struct soc_mixer_control *mc =
20924eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
20934eaa9819SJon Smirl 	int max = mc->max;
20944eaa9819SJon Smirl 	int min = mc->min;
2095e13ac2e9SMark Brown 
2096e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2097e13ac2e9SMark Brown 	uinfo->count = 2;
2098e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2099e13ac2e9SMark Brown 	uinfo->value.integer.max = max-min;
2100e13ac2e9SMark Brown 	return 0;
2101e13ac2e9SMark Brown }
2102e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2103e13ac2e9SMark Brown 
2104e13ac2e9SMark Brown /**
2105e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2106e13ac2e9SMark Brown  * @kcontrol: mixer control
2107ac11a2b3SMark Brown  * @ucontrol: control element information
2108e13ac2e9SMark Brown  *
2109e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2110e13ac2e9SMark Brown  *
2111e13ac2e9SMark Brown  * Returns 0 for success.
2112e13ac2e9SMark Brown  */
2113e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2114e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2115e13ac2e9SMark Brown {
21164eaa9819SJon Smirl 	struct soc_mixer_control *mc =
21174eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2118e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2119815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
21204eaa9819SJon Smirl 	int min = mc->min;
2121e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2122e13ac2e9SMark Brown 
2123e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2124e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2125e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2126e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2127e13ac2e9SMark Brown 	return 0;
2128e13ac2e9SMark Brown }
2129e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2130e13ac2e9SMark Brown 
2131e13ac2e9SMark Brown /**
2132e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2133e13ac2e9SMark Brown  * @kcontrol: mixer control
2134ac11a2b3SMark Brown  * @ucontrol: control element information
2135e13ac2e9SMark Brown  *
2136e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2137e13ac2e9SMark Brown  *
2138e13ac2e9SMark Brown  * Returns 0 for success.
2139e13ac2e9SMark Brown  */
2140e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2141e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2142e13ac2e9SMark Brown {
21434eaa9819SJon Smirl 	struct soc_mixer_control *mc =
21444eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2145e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2146815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
21474eaa9819SJon Smirl 	int min = mc->min;
214846f5822fSDaniel Ribeiro 	unsigned int val;
2149e13ac2e9SMark Brown 
2150e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2151e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2152e13ac2e9SMark Brown 
2153e13ac2e9SMark Brown 	return snd_soc_update_bits(codec, reg, 0xffff, val);
2154e13ac2e9SMark Brown }
2155e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2156e13ac2e9SMark Brown 
21578c6529dbSLiam Girdwood /**
21588c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
21598c6529dbSLiam Girdwood  * @dai: DAI
21608c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
21618c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
21628c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
21638c6529dbSLiam Girdwood  *
21648c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
21658c6529dbSLiam Girdwood  */
21668c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
21678c6529dbSLiam Girdwood 	unsigned int freq, int dir)
21688c6529dbSLiam Girdwood {
21693f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_sysclk)
21706335d055SEric Miao 		return dai->ops->set_sysclk(dai, clk_id, freq, dir);
21718c6529dbSLiam Girdwood 	else
21728c6529dbSLiam Girdwood 		return -EINVAL;
21738c6529dbSLiam Girdwood }
21748c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
21758c6529dbSLiam Girdwood 
21768c6529dbSLiam Girdwood /**
21778c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
21788c6529dbSLiam Girdwood  * @dai: DAI
2179ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
21808c6529dbSLiam Girdwood  * @div: new clock divisor.
21818c6529dbSLiam Girdwood  *
21828c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
21838c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
21848c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
21858c6529dbSLiam Girdwood  */
21868c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
21878c6529dbSLiam Girdwood 	int div_id, int div)
21888c6529dbSLiam Girdwood {
21893f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_clkdiv)
21906335d055SEric Miao 		return dai->ops->set_clkdiv(dai, div_id, div);
21918c6529dbSLiam Girdwood 	else
21928c6529dbSLiam Girdwood 		return -EINVAL;
21938c6529dbSLiam Girdwood }
21948c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
21958c6529dbSLiam Girdwood 
21968c6529dbSLiam Girdwood /**
21978c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
21988c6529dbSLiam Girdwood  * @dai: DAI
21998c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
2200*85488037SMark Brown  * @source: DAI specific source for the PLL
22018c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
22028c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
22038c6529dbSLiam Girdwood  *
22048c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
22058c6529dbSLiam Girdwood  */
2206*85488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2207*85488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
22088c6529dbSLiam Girdwood {
22093f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_pll)
2210*85488037SMark Brown 		return dai->ops->set_pll(dai, pll_id, source,
2211*85488037SMark Brown 					 freq_in, freq_out);
22128c6529dbSLiam Girdwood 	else
22138c6529dbSLiam Girdwood 		return -EINVAL;
22148c6529dbSLiam Girdwood }
22158c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
22168c6529dbSLiam Girdwood 
22178c6529dbSLiam Girdwood /**
22188c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
22198c6529dbSLiam Girdwood  * @dai: DAI
22208c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
22218c6529dbSLiam Girdwood  *
22228c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
22238c6529dbSLiam Girdwood  */
22248c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
22258c6529dbSLiam Girdwood {
22263f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_fmt)
22276335d055SEric Miao 		return dai->ops->set_fmt(dai, fmt);
22288c6529dbSLiam Girdwood 	else
22298c6529dbSLiam Girdwood 		return -EINVAL;
22308c6529dbSLiam Girdwood }
22318c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
22328c6529dbSLiam Girdwood 
22338c6529dbSLiam Girdwood /**
22348c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
22358c6529dbSLiam Girdwood  * @dai: DAI
2236a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2237a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
22388c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2239a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
22408c6529dbSLiam Girdwood  *
22418c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
22428c6529dbSLiam Girdwood  * specific.
22438c6529dbSLiam Girdwood  */
22448c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2245a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
22468c6529dbSLiam Girdwood {
22473f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_tdm_slot)
2248a5479e38SDaniel Ribeiro 		return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2249a5479e38SDaniel Ribeiro 				slots, slot_width);
22508c6529dbSLiam Girdwood 	else
22518c6529dbSLiam Girdwood 		return -EINVAL;
22528c6529dbSLiam Girdwood }
22538c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
22548c6529dbSLiam Girdwood 
22558c6529dbSLiam Girdwood /**
22568c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
22578c6529dbSLiam Girdwood  * @dai: DAI
22588c6529dbSLiam Girdwood  * @tristate: tristate enable
22598c6529dbSLiam Girdwood  *
22608c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
22618c6529dbSLiam Girdwood  */
22628c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
22638c6529dbSLiam Girdwood {
22643f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_tristate)
22656335d055SEric Miao 		return dai->ops->set_tristate(dai, tristate);
22668c6529dbSLiam Girdwood 	else
22678c6529dbSLiam Girdwood 		return -EINVAL;
22688c6529dbSLiam Girdwood }
22698c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
22708c6529dbSLiam Girdwood 
22718c6529dbSLiam Girdwood /**
22728c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
22738c6529dbSLiam Girdwood  * @dai: DAI
22748c6529dbSLiam Girdwood  * @mute: mute enable
22758c6529dbSLiam Girdwood  *
22768c6529dbSLiam Girdwood  * Mutes the DAI DAC.
22778c6529dbSLiam Girdwood  */
22788c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
22798c6529dbSLiam Girdwood {
22803f1a4d82SMark Brown 	if (dai->ops && dai->ops->digital_mute)
22816335d055SEric Miao 		return dai->ops->digital_mute(dai, mute);
22828c6529dbSLiam Girdwood 	else
22838c6529dbSLiam Girdwood 		return -EINVAL;
22848c6529dbSLiam Girdwood }
22858c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
22868c6529dbSLiam Girdwood 
2287c5af3a2eSMark Brown /**
2288c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2289c5af3a2eSMark Brown  *
2290ac11a2b3SMark Brown  * @card: Card to register
2291c5af3a2eSMark Brown  *
2292c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
2293c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
2294c5af3a2eSMark Brown  * registration APIs.
2295c5af3a2eSMark Brown  */
2296c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card)
2297c5af3a2eSMark Brown {
2298c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2299c5af3a2eSMark Brown 		return -EINVAL;
2300c5af3a2eSMark Brown 
2301c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
2302c5af3a2eSMark Brown 	card->instantiated = 0;
2303c5af3a2eSMark Brown 
2304c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
2305c5af3a2eSMark Brown 	list_add(&card->list, &card_list);
2306435c5e25SMark Brown 	snd_soc_instantiate_cards();
2307c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
2308c5af3a2eSMark Brown 
2309c5af3a2eSMark Brown 	dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2310c5af3a2eSMark Brown 
2311c5af3a2eSMark Brown 	return 0;
2312c5af3a2eSMark Brown }
2313c5af3a2eSMark Brown 
2314c5af3a2eSMark Brown /**
2315c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2316c5af3a2eSMark Brown  *
2317ac11a2b3SMark Brown  * @card: Card to unregister
2318c5af3a2eSMark Brown  *
2319c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
2320c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
2321c5af3a2eSMark Brown  * registration APIs.
2322c5af3a2eSMark Brown  */
2323c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card)
2324c5af3a2eSMark Brown {
2325c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
2326c5af3a2eSMark Brown 	list_del(&card->list);
2327c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
2328c5af3a2eSMark Brown 
2329c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2330c5af3a2eSMark Brown 
2331c5af3a2eSMark Brown 	return 0;
2332c5af3a2eSMark Brown }
2333c5af3a2eSMark Brown 
23346335d055SEric Miao static struct snd_soc_dai_ops null_dai_ops = {
23356335d055SEric Miao };
23366335d055SEric Miao 
23379115171aSMark Brown /**
23389115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
23399115171aSMark Brown  *
2340ac11a2b3SMark Brown  * @dai: DAI to register
23419115171aSMark Brown  */
23429115171aSMark Brown int snd_soc_register_dai(struct snd_soc_dai *dai)
23439115171aSMark Brown {
23449115171aSMark Brown 	if (!dai->name)
23459115171aSMark Brown 		return -EINVAL;
23469115171aSMark Brown 
23479115171aSMark Brown 	/* The device should become mandatory over time */
23489115171aSMark Brown 	if (!dai->dev)
23499115171aSMark Brown 		printk(KERN_WARNING "No device for DAI %s\n", dai->name);
23509115171aSMark Brown 
23516335d055SEric Miao 	if (!dai->ops)
23526335d055SEric Miao 		dai->ops = &null_dai_ops;
23536335d055SEric Miao 
23549115171aSMark Brown 	INIT_LIST_HEAD(&dai->list);
23559115171aSMark Brown 
23569115171aSMark Brown 	mutex_lock(&client_mutex);
23579115171aSMark Brown 	list_add(&dai->list, &dai_list);
2358435c5e25SMark Brown 	snd_soc_instantiate_cards();
23599115171aSMark Brown 	mutex_unlock(&client_mutex);
23609115171aSMark Brown 
23619115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
23629115171aSMark Brown 
23639115171aSMark Brown 	return 0;
23649115171aSMark Brown }
23659115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
23669115171aSMark Brown 
23679115171aSMark Brown /**
23689115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
23699115171aSMark Brown  *
2370ac11a2b3SMark Brown  * @dai: DAI to unregister
23719115171aSMark Brown  */
23729115171aSMark Brown void snd_soc_unregister_dai(struct snd_soc_dai *dai)
23739115171aSMark Brown {
23749115171aSMark Brown 	mutex_lock(&client_mutex);
23759115171aSMark Brown 	list_del(&dai->list);
23769115171aSMark Brown 	mutex_unlock(&client_mutex);
23779115171aSMark Brown 
23789115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
23799115171aSMark Brown }
23809115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
23819115171aSMark Brown 
23829115171aSMark Brown /**
23839115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
23849115171aSMark Brown  *
2385ac11a2b3SMark Brown  * @dai: Array of DAIs to register
2386ac11a2b3SMark Brown  * @count: Number of DAIs
23879115171aSMark Brown  */
23889115171aSMark Brown int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
23899115171aSMark Brown {
23909115171aSMark Brown 	int i, ret;
23919115171aSMark Brown 
23929115171aSMark Brown 	for (i = 0; i < count; i++) {
23939115171aSMark Brown 		ret = snd_soc_register_dai(&dai[i]);
23949115171aSMark Brown 		if (ret != 0)
23959115171aSMark Brown 			goto err;
23969115171aSMark Brown 	}
23979115171aSMark Brown 
23989115171aSMark Brown 	return 0;
23999115171aSMark Brown 
24009115171aSMark Brown err:
24019115171aSMark Brown 	for (i--; i >= 0; i--)
24029115171aSMark Brown 		snd_soc_unregister_dai(&dai[i]);
24039115171aSMark Brown 
24049115171aSMark Brown 	return ret;
24059115171aSMark Brown }
24069115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
24079115171aSMark Brown 
24089115171aSMark Brown /**
24099115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
24109115171aSMark Brown  *
2411ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
2412ac11a2b3SMark Brown  * @count: Number of DAIs
24139115171aSMark Brown  */
24149115171aSMark Brown void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
24159115171aSMark Brown {
24169115171aSMark Brown 	int i;
24179115171aSMark Brown 
24189115171aSMark Brown 	for (i = 0; i < count; i++)
24199115171aSMark Brown 		snd_soc_unregister_dai(&dai[i]);
24209115171aSMark Brown }
24219115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
24229115171aSMark Brown 
242312a48a8cSMark Brown /**
242412a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
242512a48a8cSMark Brown  *
2426ac11a2b3SMark Brown  * @platform: platform to register
242712a48a8cSMark Brown  */
242812a48a8cSMark Brown int snd_soc_register_platform(struct snd_soc_platform *platform)
242912a48a8cSMark Brown {
243012a48a8cSMark Brown 	if (!platform->name)
243112a48a8cSMark Brown 		return -EINVAL;
243212a48a8cSMark Brown 
243312a48a8cSMark Brown 	INIT_LIST_HEAD(&platform->list);
243412a48a8cSMark Brown 
243512a48a8cSMark Brown 	mutex_lock(&client_mutex);
243612a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
2437435c5e25SMark Brown 	snd_soc_instantiate_cards();
243812a48a8cSMark Brown 	mutex_unlock(&client_mutex);
243912a48a8cSMark Brown 
244012a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
244112a48a8cSMark Brown 
244212a48a8cSMark Brown 	return 0;
244312a48a8cSMark Brown }
244412a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
244512a48a8cSMark Brown 
244612a48a8cSMark Brown /**
244712a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
244812a48a8cSMark Brown  *
2449ac11a2b3SMark Brown  * @platform: platform to unregister
245012a48a8cSMark Brown  */
245112a48a8cSMark Brown void snd_soc_unregister_platform(struct snd_soc_platform *platform)
245212a48a8cSMark Brown {
245312a48a8cSMark Brown 	mutex_lock(&client_mutex);
245412a48a8cSMark Brown 	list_del(&platform->list);
245512a48a8cSMark Brown 	mutex_unlock(&client_mutex);
245612a48a8cSMark Brown 
245712a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
245812a48a8cSMark Brown }
245912a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
246012a48a8cSMark Brown 
2461151ab22cSMark Brown static u64 codec_format_map[] = {
2462151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2463151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2464151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2465151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2466151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2467151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2468151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2469151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2470151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2471151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2472151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2473151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2474151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2475151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2476151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2477151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2478151ab22cSMark Brown };
2479151ab22cSMark Brown 
2480151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
2481151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
2482151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
2483151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
2484151ab22cSMark Brown  */
2485151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2486151ab22cSMark Brown {
2487151ab22cSMark Brown 	int i;
2488151ab22cSMark Brown 
2489151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2490151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
2491151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
2492151ab22cSMark Brown }
2493151ab22cSMark Brown 
24940d0cf00aSMark Brown /**
24950d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
24960d0cf00aSMark Brown  *
2497ac11a2b3SMark Brown  * @codec: codec to register
24980d0cf00aSMark Brown  */
24990d0cf00aSMark Brown int snd_soc_register_codec(struct snd_soc_codec *codec)
25000d0cf00aSMark Brown {
2501151ab22cSMark Brown 	int i;
2502151ab22cSMark Brown 
25030d0cf00aSMark Brown 	if (!codec->name)
25040d0cf00aSMark Brown 		return -EINVAL;
25050d0cf00aSMark Brown 
25060d0cf00aSMark Brown 	/* The device should become mandatory over time */
25070d0cf00aSMark Brown 	if (!codec->dev)
25080d0cf00aSMark Brown 		printk(KERN_WARNING "No device for codec %s\n", codec->name);
25090d0cf00aSMark Brown 
25100d0cf00aSMark Brown 	INIT_LIST_HEAD(&codec->list);
25110d0cf00aSMark Brown 
2512151ab22cSMark Brown 	for (i = 0; i < codec->num_dai; i++) {
2513151ab22cSMark Brown 		fixup_codec_formats(&codec->dai[i].playback);
2514151ab22cSMark Brown 		fixup_codec_formats(&codec->dai[i].capture);
2515151ab22cSMark Brown 	}
2516151ab22cSMark Brown 
25170d0cf00aSMark Brown 	mutex_lock(&client_mutex);
25180d0cf00aSMark Brown 	list_add(&codec->list, &codec_list);
25190d0cf00aSMark Brown 	snd_soc_instantiate_cards();
25200d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
25210d0cf00aSMark Brown 
25220d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
25230d0cf00aSMark Brown 
25240d0cf00aSMark Brown 	return 0;
25250d0cf00aSMark Brown }
25260d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
25270d0cf00aSMark Brown 
25280d0cf00aSMark Brown /**
25290d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
25300d0cf00aSMark Brown  *
2531ac11a2b3SMark Brown  * @codec: codec to unregister
25320d0cf00aSMark Brown  */
25330d0cf00aSMark Brown void snd_soc_unregister_codec(struct snd_soc_codec *codec)
25340d0cf00aSMark Brown {
25350d0cf00aSMark Brown 	mutex_lock(&client_mutex);
25360d0cf00aSMark Brown 	list_del(&codec->list);
25370d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
25380d0cf00aSMark Brown 
25390d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
25400d0cf00aSMark Brown }
25410d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
25420d0cf00aSMark Brown 
2543c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
2544db2a4165SFrank Mandarino {
2545384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
2546384c89e2SMark Brown 	debugfs_root = debugfs_create_dir("asoc", NULL);
2547384c89e2SMark Brown 	if (IS_ERR(debugfs_root) || !debugfs_root) {
2548384c89e2SMark Brown 		printk(KERN_WARNING
2549384c89e2SMark Brown 		       "ASoC: Failed to create debugfs directory\n");
2550384c89e2SMark Brown 		debugfs_root = NULL;
2551384c89e2SMark Brown 	}
2552384c89e2SMark Brown #endif
2553384c89e2SMark Brown 
2554db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
2555db2a4165SFrank Mandarino }
2556db2a4165SFrank Mandarino 
25577d8c16a6SMark Brown static void __exit snd_soc_exit(void)
2558db2a4165SFrank Mandarino {
2559384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
2560384c89e2SMark Brown 	debugfs_remove_recursive(debugfs_root);
2561384c89e2SMark Brown #endif
2562db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
2563db2a4165SFrank Mandarino }
2564db2a4165SFrank Mandarino 
2565db2a4165SFrank Mandarino module_init(snd_soc_init);
2566db2a4165SFrank Mandarino module_exit(snd_soc_exit);
2567db2a4165SFrank Mandarino 
2568db2a4165SFrank Mandarino /* Module information */
2569d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2570db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
2571db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
25728b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
2573