xref: /openbmc/linux/sound/soc/soc-core.c (revision 968a6025aa9f909d487988efb542217a126023a0)
1db2a4165SFrank Mandarino /*
2db2a4165SFrank Mandarino  * soc-core.c  --  ALSA SoC Audio Layer
3db2a4165SFrank Mandarino  *
4db2a4165SFrank Mandarino  * Copyright 2005 Wolfson Microelectronics PLC.
50664d888SLiam Girdwood  * Copyright 2005 Openedhand Ltd.
60664d888SLiam Girdwood  *
7d331124dSLiam Girdwood  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
80664d888SLiam Girdwood  *         with code, comments and ideas from :-
90664d888SLiam Girdwood  *         Richard Purdie <richard@openedhand.com>
10db2a4165SFrank Mandarino  *
11db2a4165SFrank Mandarino  *  This program is free software; you can redistribute  it and/or modify it
12db2a4165SFrank Mandarino  *  under  the terms of  the GNU General  Public License as published by the
13db2a4165SFrank Mandarino  *  Free Software Foundation;  either version 2 of the  License, or (at your
14db2a4165SFrank Mandarino  *  option) any later version.
15db2a4165SFrank Mandarino  *
16db2a4165SFrank Mandarino  *  TODO:
17db2a4165SFrank Mandarino  *   o Add hw rules to enforce rates, etc.
18db2a4165SFrank Mandarino  *   o More testing with other codecs/machines.
19db2a4165SFrank Mandarino  *   o Add more codecs and platforms to ensure good API coverage.
20db2a4165SFrank Mandarino  *   o Support TDM on PCM and I2S
21db2a4165SFrank Mandarino  */
22db2a4165SFrank Mandarino 
23db2a4165SFrank Mandarino #include <linux/module.h>
24db2a4165SFrank Mandarino #include <linux/moduleparam.h>
25db2a4165SFrank Mandarino #include <linux/init.h>
26db2a4165SFrank Mandarino #include <linux/delay.h>
27db2a4165SFrank Mandarino #include <linux/pm.h>
28db2a4165SFrank Mandarino #include <linux/bitops.h>
2912ef193dSTroy Kisky #include <linux/debugfs.h>
30db2a4165SFrank Mandarino #include <linux/platform_device.h>
31db2a4165SFrank Mandarino #include <sound/core.h>
32db2a4165SFrank Mandarino #include <sound/pcm.h>
33db2a4165SFrank Mandarino #include <sound/pcm_params.h>
34db2a4165SFrank Mandarino #include <sound/soc.h>
35db2a4165SFrank Mandarino #include <sound/soc-dapm.h>
36db2a4165SFrank Mandarino #include <sound/initval.h>
37db2a4165SFrank Mandarino 
38db2a4165SFrank Mandarino static DEFINE_MUTEX(pcm_mutex);
39db2a4165SFrank Mandarino static DEFINE_MUTEX(io_mutex);
40db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
41db2a4165SFrank Mandarino 
42db2a4165SFrank Mandarino /*
43db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
44db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
45db2a4165SFrank Mandarino  * between two audio tracks.
46db2a4165SFrank Mandarino  */
47db2a4165SFrank Mandarino static int pmdown_time = 5000;
48db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
49db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
50db2a4165SFrank Mandarino 
51965ac42cSLiam Girdwood /*
52965ac42cSLiam Girdwood  * This function forces any delayed work to be queued and run.
53965ac42cSLiam Girdwood  */
54965ac42cSLiam Girdwood static int run_delayed_work(struct delayed_work *dwork)
55965ac42cSLiam Girdwood {
56965ac42cSLiam Girdwood 	int ret;
57965ac42cSLiam Girdwood 
58965ac42cSLiam Girdwood 	/* cancel any work waiting to be queued. */
59965ac42cSLiam Girdwood 	ret = cancel_delayed_work(dwork);
60965ac42cSLiam Girdwood 
61965ac42cSLiam Girdwood 	/* if there was any work waiting then we run it now and
62965ac42cSLiam Girdwood 	 * wait for it's completion */
63965ac42cSLiam Girdwood 	if (ret) {
64965ac42cSLiam Girdwood 		schedule_delayed_work(dwork, 0);
65965ac42cSLiam Girdwood 		flush_scheduled_work();
66965ac42cSLiam Girdwood 	}
67965ac42cSLiam Girdwood 	return ret;
68965ac42cSLiam Girdwood }
69965ac42cSLiam Girdwood 
70db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
71db2a4165SFrank Mandarino /* unregister ac97 codec */
72db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
73db2a4165SFrank Mandarino {
74db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
75db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
76db2a4165SFrank Mandarino 	return 0;
77db2a4165SFrank Mandarino }
78db2a4165SFrank Mandarino 
79db2a4165SFrank Mandarino /* stop no dev release warning */
80db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
81db2a4165SFrank Mandarino 
82db2a4165SFrank Mandarino /* register ac97 codec to bus */
83db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
84db2a4165SFrank Mandarino {
85db2a4165SFrank Mandarino 	int err;
86db2a4165SFrank Mandarino 
87db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
88db2a4165SFrank Mandarino 	codec->ac97->dev.parent = NULL;
89db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
90db2a4165SFrank Mandarino 
91bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
92db2a4165SFrank Mandarino 		     codec->card->number, 0, codec->name);
93db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
94db2a4165SFrank Mandarino 	if (err < 0) {
95db2a4165SFrank Mandarino 		snd_printk(KERN_ERR "Can't register ac97 bus\n");
96db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
97db2a4165SFrank Mandarino 		return err;
98db2a4165SFrank Mandarino 	}
99db2a4165SFrank Mandarino 	return 0;
100db2a4165SFrank Mandarino }
101db2a4165SFrank Mandarino #endif
102db2a4165SFrank Mandarino 
103db2a4165SFrank Mandarino /*
104db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
105db2a4165SFrank Mandarino  * then initialized and any private data can be allocated. This also calls
106db2a4165SFrank Mandarino  * startup for the cpu DAI, platform, machine and codec DAI.
107db2a4165SFrank Mandarino  */
108db2a4165SFrank Mandarino static int soc_pcm_open(struct snd_pcm_substream *substream)
109db2a4165SFrank Mandarino {
110db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
111db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
112db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
113cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
114db2a4165SFrank Mandarino 	struct snd_soc_platform *platform = socdev->platform;
1153c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
1163c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
117db2a4165SFrank Mandarino 	int ret = 0;
118db2a4165SFrank Mandarino 
119db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
120db2a4165SFrank Mandarino 
121db2a4165SFrank Mandarino 	/* startup the audio subsystem */
122cb666e5bSLiam Girdwood 	if (cpu_dai->ops.startup) {
123dee89c4dSMark Brown 		ret = cpu_dai->ops.startup(substream, cpu_dai);
124db2a4165SFrank Mandarino 		if (ret < 0) {
125db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open interface %s\n",
126cb666e5bSLiam Girdwood 				cpu_dai->name);
127db2a4165SFrank Mandarino 			goto out;
128db2a4165SFrank Mandarino 		}
129db2a4165SFrank Mandarino 	}
130db2a4165SFrank Mandarino 
131db2a4165SFrank Mandarino 	if (platform->pcm_ops->open) {
132db2a4165SFrank Mandarino 		ret = platform->pcm_ops->open(substream);
133db2a4165SFrank Mandarino 		if (ret < 0) {
134db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
135db2a4165SFrank Mandarino 			goto platform_err;
136db2a4165SFrank Mandarino 		}
137db2a4165SFrank Mandarino 	}
138db2a4165SFrank Mandarino 
139cb666e5bSLiam Girdwood 	if (codec_dai->ops.startup) {
140dee89c4dSMark Brown 		ret = codec_dai->ops.startup(substream, codec_dai);
141cb666e5bSLiam Girdwood 		if (ret < 0) {
142cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: can't open codec %s\n",
143cb666e5bSLiam Girdwood 				codec_dai->name);
144cb666e5bSLiam Girdwood 			goto codec_dai_err;
145cb666e5bSLiam Girdwood 		}
146cb666e5bSLiam Girdwood 	}
147cb666e5bSLiam Girdwood 
148db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->startup) {
149db2a4165SFrank Mandarino 		ret = machine->ops->startup(substream);
150db2a4165SFrank Mandarino 		if (ret < 0) {
151db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
152db2a4165SFrank Mandarino 			goto machine_err;
153db2a4165SFrank Mandarino 		}
154db2a4165SFrank Mandarino 	}
155db2a4165SFrank Mandarino 
156db2a4165SFrank Mandarino 	/* Check that the codec and cpu DAI's are compatible */
157db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
158db2a4165SFrank Mandarino 		runtime->hw.rate_min =
1593ff3f64bSMark Brown 			max(codec_dai->playback.rate_min,
1603ff3f64bSMark Brown 			    cpu_dai->playback.rate_min);
161db2a4165SFrank Mandarino 		runtime->hw.rate_max =
1623ff3f64bSMark Brown 			min(codec_dai->playback.rate_max,
1633ff3f64bSMark Brown 			    cpu_dai->playback.rate_max);
164db2a4165SFrank Mandarino 		runtime->hw.channels_min =
165cb666e5bSLiam Girdwood 			max(codec_dai->playback.channels_min,
166cb666e5bSLiam Girdwood 				cpu_dai->playback.channels_min);
167db2a4165SFrank Mandarino 		runtime->hw.channels_max =
168cb666e5bSLiam Girdwood 			min(codec_dai->playback.channels_max,
169cb666e5bSLiam Girdwood 				cpu_dai->playback.channels_max);
170cb666e5bSLiam Girdwood 		runtime->hw.formats =
171cb666e5bSLiam Girdwood 			codec_dai->playback.formats & cpu_dai->playback.formats;
172cb666e5bSLiam Girdwood 		runtime->hw.rates =
173cb666e5bSLiam Girdwood 			codec_dai->playback.rates & cpu_dai->playback.rates;
174db2a4165SFrank Mandarino 	} else {
175db2a4165SFrank Mandarino 		runtime->hw.rate_min =
1763ff3f64bSMark Brown 			max(codec_dai->capture.rate_min,
1773ff3f64bSMark Brown 			    cpu_dai->capture.rate_min);
178db2a4165SFrank Mandarino 		runtime->hw.rate_max =
1793ff3f64bSMark Brown 			min(codec_dai->capture.rate_max,
1803ff3f64bSMark Brown 			    cpu_dai->capture.rate_max);
181db2a4165SFrank Mandarino 		runtime->hw.channels_min =
182cb666e5bSLiam Girdwood 			max(codec_dai->capture.channels_min,
183cb666e5bSLiam Girdwood 				cpu_dai->capture.channels_min);
184db2a4165SFrank Mandarino 		runtime->hw.channels_max =
185cb666e5bSLiam Girdwood 			min(codec_dai->capture.channels_max,
186cb666e5bSLiam Girdwood 				cpu_dai->capture.channels_max);
187cb666e5bSLiam Girdwood 		runtime->hw.formats =
188cb666e5bSLiam Girdwood 			codec_dai->capture.formats & cpu_dai->capture.formats;
189cb666e5bSLiam Girdwood 		runtime->hw.rates =
190cb666e5bSLiam Girdwood 			codec_dai->capture.rates & cpu_dai->capture.rates;
191db2a4165SFrank Mandarino 	}
192db2a4165SFrank Mandarino 
193db2a4165SFrank Mandarino 	snd_pcm_limit_hw_rates(runtime);
194db2a4165SFrank Mandarino 	if (!runtime->hw.rates) {
195db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
196cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
197cb666e5bSLiam Girdwood 		goto machine_err;
198db2a4165SFrank Mandarino 	}
199db2a4165SFrank Mandarino 	if (!runtime->hw.formats) {
200db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
201cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
202cb666e5bSLiam Girdwood 		goto machine_err;
203db2a4165SFrank Mandarino 	}
204db2a4165SFrank Mandarino 	if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
205db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
206cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
207cb666e5bSLiam Girdwood 		goto machine_err;
208db2a4165SFrank Mandarino 	}
209db2a4165SFrank Mandarino 
210f24368c2SMark Brown 	pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
211f24368c2SMark Brown 	pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
212f24368c2SMark Brown 	pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
213b5c5fd24SLiam Girdwood 		 runtime->hw.channels_max);
214f24368c2SMark Brown 	pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
215b5c5fd24SLiam Girdwood 		 runtime->hw.rate_max);
216db2a4165SFrank Mandarino 
217db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
218cb666e5bSLiam Girdwood 		cpu_dai->playback.active = codec_dai->playback.active = 1;
219db2a4165SFrank Mandarino 	else
220cb666e5bSLiam Girdwood 		cpu_dai->capture.active = codec_dai->capture.active = 1;
221cb666e5bSLiam Girdwood 	cpu_dai->active = codec_dai->active = 1;
222cb666e5bSLiam Girdwood 	cpu_dai->runtime = runtime;
223db2a4165SFrank Mandarino 	socdev->codec->active++;
224db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
225db2a4165SFrank Mandarino 	return 0;
226db2a4165SFrank Mandarino 
227cb666e5bSLiam Girdwood machine_err:
228db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->shutdown)
229db2a4165SFrank Mandarino 		machine->ops->shutdown(substream);
230db2a4165SFrank Mandarino 
231cb666e5bSLiam Girdwood codec_dai_err:
232db2a4165SFrank Mandarino 	if (platform->pcm_ops->close)
233db2a4165SFrank Mandarino 		platform->pcm_ops->close(substream);
234db2a4165SFrank Mandarino 
235db2a4165SFrank Mandarino platform_err:
236cb666e5bSLiam Girdwood 	if (cpu_dai->ops.shutdown)
237dee89c4dSMark Brown 		cpu_dai->ops.shutdown(substream, cpu_dai);
238db2a4165SFrank Mandarino out:
239db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
240db2a4165SFrank Mandarino 	return ret;
241db2a4165SFrank Mandarino }
242db2a4165SFrank Mandarino 
243db2a4165SFrank Mandarino /*
2443a4fa0a2SRobert P. J. Day  * Power down the audio subsystem pmdown_time msecs after close is called.
245db2a4165SFrank Mandarino  * This is to ensure there are no pops or clicks in between any music tracks
246db2a4165SFrank Mandarino  * due to DAPM power cycling.
247db2a4165SFrank Mandarino  */
2484484bb2eSAndrew Morton static void close_delayed_work(struct work_struct *work)
249db2a4165SFrank Mandarino {
2504484bb2eSAndrew Morton 	struct snd_soc_device *socdev =
2514484bb2eSAndrew Morton 		container_of(work, struct snd_soc_device, delayed_work.work);
252db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
2533c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai;
254db2a4165SFrank Mandarino 	int i;
255db2a4165SFrank Mandarino 
256db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
257db2a4165SFrank Mandarino 	for (i = 0; i < codec->num_dai; i++) {
258db2a4165SFrank Mandarino 		codec_dai = &codec->dai[i];
259db2a4165SFrank Mandarino 
260f24368c2SMark Brown 		pr_debug("pop wq checking: %s status: %s waiting: %s\n",
261db2a4165SFrank Mandarino 			 codec_dai->playback.stream_name,
262db2a4165SFrank Mandarino 			 codec_dai->playback.active ? "active" : "inactive",
263db2a4165SFrank Mandarino 			 codec_dai->pop_wait ? "yes" : "no");
264db2a4165SFrank Mandarino 
265db2a4165SFrank Mandarino 		/* are we waiting on this codec DAI stream */
266db2a4165SFrank Mandarino 		if (codec_dai->pop_wait == 1) {
267db2a4165SFrank Mandarino 
2680be9898aSMark Brown 			/* Reduce power if no longer active */
2693c1c47e0SLiam Girdwood 			if (codec->active == 0) {
270f24368c2SMark Brown 				pr_debug("pop wq D1 %s %s\n", codec->name,
2713c1c47e0SLiam Girdwood 					 codec_dai->playback.stream_name);
2720be9898aSMark Brown 				snd_soc_dapm_set_bias_level(socdev,
2730be9898aSMark Brown 					SND_SOC_BIAS_PREPARE);
2743c1c47e0SLiam Girdwood 			}
2753c1c47e0SLiam Girdwood 
276db2a4165SFrank Mandarino 			codec_dai->pop_wait = 0;
2770b4d221bSLiam Girdwood 			snd_soc_dapm_stream_event(codec,
2780b4d221bSLiam Girdwood 				codec_dai->playback.stream_name,
279db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_STOP);
280db2a4165SFrank Mandarino 
2810be9898aSMark Brown 			/* Fall into standby if no longer active */
282db2a4165SFrank Mandarino 			if (codec->active == 0) {
283f24368c2SMark Brown 				pr_debug("pop wq D3 %s %s\n", codec->name,
284db2a4165SFrank Mandarino 					 codec_dai->playback.stream_name);
2850be9898aSMark Brown 				snd_soc_dapm_set_bias_level(socdev,
2860be9898aSMark Brown 					SND_SOC_BIAS_STANDBY);
287db2a4165SFrank Mandarino 			}
288db2a4165SFrank Mandarino 		}
289db2a4165SFrank Mandarino 	}
290db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
291db2a4165SFrank Mandarino }
292db2a4165SFrank Mandarino 
293db2a4165SFrank Mandarino /*
294db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is closed. Private data can be
295db2a4165SFrank Mandarino  * freed here. The cpu DAI, codec DAI, machine and platform are also
296db2a4165SFrank Mandarino  * shutdown.
297db2a4165SFrank Mandarino  */
298db2a4165SFrank Mandarino static int soc_codec_close(struct snd_pcm_substream *substream)
299db2a4165SFrank Mandarino {
300db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
301db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
302cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
303db2a4165SFrank Mandarino 	struct snd_soc_platform *platform = socdev->platform;
3043c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
3053c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
306db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
307db2a4165SFrank Mandarino 
308db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
309db2a4165SFrank Mandarino 
310db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
311cb666e5bSLiam Girdwood 		cpu_dai->playback.active = codec_dai->playback.active = 0;
312db2a4165SFrank Mandarino 	else
313cb666e5bSLiam Girdwood 		cpu_dai->capture.active = codec_dai->capture.active = 0;
314db2a4165SFrank Mandarino 
315cb666e5bSLiam Girdwood 	if (codec_dai->playback.active == 0 &&
316cb666e5bSLiam Girdwood 		codec_dai->capture.active == 0) {
317cb666e5bSLiam Girdwood 		cpu_dai->active = codec_dai->active = 0;
318db2a4165SFrank Mandarino 	}
319db2a4165SFrank Mandarino 	codec->active--;
320db2a4165SFrank Mandarino 
3216010b2daSMark Brown 	/* Muting the DAC suppresses artifacts caused during digital
3226010b2daSMark Brown 	 * shutdown, for example from stopping clocks.
3236010b2daSMark Brown 	 */
3246010b2daSMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3256010b2daSMark Brown 		snd_soc_dai_digital_mute(codec_dai, 1);
3266010b2daSMark Brown 
327cb666e5bSLiam Girdwood 	if (cpu_dai->ops.shutdown)
328dee89c4dSMark Brown 		cpu_dai->ops.shutdown(substream, cpu_dai);
329db2a4165SFrank Mandarino 
330cb666e5bSLiam Girdwood 	if (codec_dai->ops.shutdown)
331dee89c4dSMark Brown 		codec_dai->ops.shutdown(substream, codec_dai);
332db2a4165SFrank Mandarino 
333db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->shutdown)
334db2a4165SFrank Mandarino 		machine->ops->shutdown(substream);
335db2a4165SFrank Mandarino 
336db2a4165SFrank Mandarino 	if (platform->pcm_ops->close)
337db2a4165SFrank Mandarino 		platform->pcm_ops->close(substream);
338cb666e5bSLiam Girdwood 	cpu_dai->runtime = NULL;
339db2a4165SFrank Mandarino 
340db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
341db2a4165SFrank Mandarino 		/* start delayed pop wq here for playback streams */
342cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 1;
3434bb09523STakashi Iwai 		schedule_delayed_work(&socdev->delayed_work,
344db2a4165SFrank Mandarino 			msecs_to_jiffies(pmdown_time));
345db2a4165SFrank Mandarino 	} else {
346db2a4165SFrank Mandarino 		/* capture streams can be powered down now */
347cb666e5bSLiam Girdwood 		snd_soc_dapm_stream_event(codec,
3480b4d221bSLiam Girdwood 			codec_dai->capture.stream_name,
3490b4d221bSLiam Girdwood 			SND_SOC_DAPM_STREAM_STOP);
350db2a4165SFrank Mandarino 
3510b4d221bSLiam Girdwood 		if (codec->active == 0 && codec_dai->pop_wait == 0)
3520be9898aSMark Brown 			snd_soc_dapm_set_bias_level(socdev,
3530be9898aSMark Brown 						SND_SOC_BIAS_STANDBY);
354db2a4165SFrank Mandarino 	}
355db2a4165SFrank Mandarino 
356db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
357db2a4165SFrank Mandarino 	return 0;
358db2a4165SFrank Mandarino }
359db2a4165SFrank Mandarino 
360db2a4165SFrank Mandarino /*
361db2a4165SFrank Mandarino  * Called by ALSA when the PCM substream is prepared, can set format, sample
362db2a4165SFrank Mandarino  * rate, etc.  This function is non atomic and can be called multiple times,
363db2a4165SFrank Mandarino  * it can refer to the runtime info.
364db2a4165SFrank Mandarino  */
365db2a4165SFrank Mandarino static int soc_pcm_prepare(struct snd_pcm_substream *substream)
366db2a4165SFrank Mandarino {
367db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
368db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
369cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
370db2a4165SFrank Mandarino 	struct snd_soc_platform *platform = socdev->platform;
3713c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
3723c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
373db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
374db2a4165SFrank Mandarino 	int ret = 0;
375db2a4165SFrank Mandarino 
376db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
377cb666e5bSLiam Girdwood 
378cb666e5bSLiam Girdwood 	if (machine->ops && machine->ops->prepare) {
379cb666e5bSLiam Girdwood 		ret = machine->ops->prepare(substream);
380cb666e5bSLiam Girdwood 		if (ret < 0) {
381cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine prepare error\n");
382cb666e5bSLiam Girdwood 			goto out;
383cb666e5bSLiam Girdwood 		}
384cb666e5bSLiam Girdwood 	}
385cb666e5bSLiam Girdwood 
386db2a4165SFrank Mandarino 	if (platform->pcm_ops->prepare) {
387db2a4165SFrank Mandarino 		ret = platform->pcm_ops->prepare(substream);
388a71a468aSLiam Girdwood 		if (ret < 0) {
389a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: platform prepare error\n");
390db2a4165SFrank Mandarino 			goto out;
391db2a4165SFrank Mandarino 		}
392a71a468aSLiam Girdwood 	}
393db2a4165SFrank Mandarino 
394cb666e5bSLiam Girdwood 	if (codec_dai->ops.prepare) {
395dee89c4dSMark Brown 		ret = codec_dai->ops.prepare(substream, codec_dai);
396a71a468aSLiam Girdwood 		if (ret < 0) {
397a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: codec DAI prepare error\n");
398db2a4165SFrank Mandarino 			goto out;
399db2a4165SFrank Mandarino 		}
400a71a468aSLiam Girdwood 	}
401db2a4165SFrank Mandarino 
402cb666e5bSLiam Girdwood 	if (cpu_dai->ops.prepare) {
403dee89c4dSMark Brown 		ret = cpu_dai->ops.prepare(substream, cpu_dai);
404cb666e5bSLiam Girdwood 		if (ret < 0) {
405cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: cpu DAI prepare error\n");
406cb666e5bSLiam Girdwood 			goto out;
407cb666e5bSLiam Girdwood 		}
408cb666e5bSLiam Girdwood 	}
409db2a4165SFrank Mandarino 
410d45f6219SMark Brown 	/* cancel any delayed stream shutdown that is pending */
411d45f6219SMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
412d45f6219SMark Brown 	    codec_dai->pop_wait) {
413cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 0;
4144484bb2eSAndrew Morton 		cancel_delayed_work(&socdev->delayed_work);
415db2a4165SFrank Mandarino 	}
416db2a4165SFrank Mandarino 
417d45f6219SMark Brown 	/* do we need to power up codec */
418d45f6219SMark Brown 	if (codec->bias_level != SND_SOC_BIAS_ON) {
4190be9898aSMark Brown 		snd_soc_dapm_set_bias_level(socdev,
4200be9898aSMark Brown 					    SND_SOC_BIAS_PREPARE);
421db2a4165SFrank Mandarino 
422db2a4165SFrank Mandarino 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
423db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec,
424cb666e5bSLiam Girdwood 					codec_dai->playback.stream_name,
425db2a4165SFrank Mandarino 					SND_SOC_DAPM_STREAM_START);
426db2a4165SFrank Mandarino 		else
427db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec,
428cb666e5bSLiam Girdwood 					codec_dai->capture.stream_name,
429db2a4165SFrank Mandarino 					SND_SOC_DAPM_STREAM_START);
430db2a4165SFrank Mandarino 
4310be9898aSMark Brown 		snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
4328c6529dbSLiam Girdwood 		snd_soc_dai_digital_mute(codec_dai, 0);
433db2a4165SFrank Mandarino 
434db2a4165SFrank Mandarino 	} else {
435db2a4165SFrank Mandarino 		/* codec already powered - power on widgets */
436db2a4165SFrank Mandarino 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
437db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec,
438cb666e5bSLiam Girdwood 					codec_dai->playback.stream_name,
439db2a4165SFrank Mandarino 					SND_SOC_DAPM_STREAM_START);
440db2a4165SFrank Mandarino 		else
441db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec,
442cb666e5bSLiam Girdwood 					codec_dai->capture.stream_name,
443db2a4165SFrank Mandarino 					SND_SOC_DAPM_STREAM_START);
4448c6529dbSLiam Girdwood 
4458c6529dbSLiam Girdwood 		snd_soc_dai_digital_mute(codec_dai, 0);
446db2a4165SFrank Mandarino 	}
447db2a4165SFrank Mandarino 
448db2a4165SFrank Mandarino out:
449db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
450db2a4165SFrank Mandarino 	return ret;
451db2a4165SFrank Mandarino }
452db2a4165SFrank Mandarino 
453db2a4165SFrank Mandarino /*
454db2a4165SFrank Mandarino  * Called by ALSA when the hardware params are set by application. This
455db2a4165SFrank Mandarino  * function can also be called multiple times and can allocate buffers
456db2a4165SFrank Mandarino  * (using snd_pcm_lib_* ). It's non-atomic.
457db2a4165SFrank Mandarino  */
458db2a4165SFrank Mandarino static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
459db2a4165SFrank Mandarino 				struct snd_pcm_hw_params *params)
460db2a4165SFrank Mandarino {
461db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
462db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
463cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
464db2a4165SFrank Mandarino 	struct snd_soc_platform *platform = socdev->platform;
4653c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
4663c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
467db2a4165SFrank Mandarino 	int ret = 0;
468db2a4165SFrank Mandarino 
469db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
470db2a4165SFrank Mandarino 
471cb666e5bSLiam Girdwood 	if (machine->ops && machine->ops->hw_params) {
472cb666e5bSLiam Girdwood 		ret = machine->ops->hw_params(substream, params);
473cb666e5bSLiam Girdwood 		if (ret < 0) {
474cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine hw_params failed\n");
475db2a4165SFrank Mandarino 			goto out;
476db2a4165SFrank Mandarino 		}
477cb666e5bSLiam Girdwood 	}
478db2a4165SFrank Mandarino 
479cb666e5bSLiam Girdwood 	if (codec_dai->ops.hw_params) {
480dee89c4dSMark Brown 		ret = codec_dai->ops.hw_params(substream, params, codec_dai);
481db2a4165SFrank Mandarino 		if (ret < 0) {
482db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't set codec %s hw params\n",
483cb666e5bSLiam Girdwood 				codec_dai->name);
484cb666e5bSLiam Girdwood 			goto codec_err;
485db2a4165SFrank Mandarino 		}
486db2a4165SFrank Mandarino 	}
487db2a4165SFrank Mandarino 
488cb666e5bSLiam Girdwood 	if (cpu_dai->ops.hw_params) {
489dee89c4dSMark Brown 		ret = cpu_dai->ops.hw_params(substream, params, cpu_dai);
490db2a4165SFrank Mandarino 		if (ret < 0) {
4913ff3f64bSMark Brown 			printk(KERN_ERR "asoc: interface %s hw params failed\n",
492cb666e5bSLiam Girdwood 				cpu_dai->name);
493db2a4165SFrank Mandarino 			goto interface_err;
494db2a4165SFrank Mandarino 		}
495db2a4165SFrank Mandarino 	}
496db2a4165SFrank Mandarino 
497db2a4165SFrank Mandarino 	if (platform->pcm_ops->hw_params) {
498db2a4165SFrank Mandarino 		ret = platform->pcm_ops->hw_params(substream, params);
499db2a4165SFrank Mandarino 		if (ret < 0) {
5003ff3f64bSMark Brown 			printk(KERN_ERR "asoc: platform %s hw params failed\n",
501db2a4165SFrank Mandarino 				platform->name);
502db2a4165SFrank Mandarino 			goto platform_err;
503db2a4165SFrank Mandarino 		}
504db2a4165SFrank Mandarino 	}
505db2a4165SFrank Mandarino 
506db2a4165SFrank Mandarino out:
507db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
508db2a4165SFrank Mandarino 	return ret;
509db2a4165SFrank Mandarino 
510db2a4165SFrank Mandarino platform_err:
511cb666e5bSLiam Girdwood 	if (cpu_dai->ops.hw_free)
512dee89c4dSMark Brown 		cpu_dai->ops.hw_free(substream, cpu_dai);
513db2a4165SFrank Mandarino 
514db2a4165SFrank Mandarino interface_err:
515cb666e5bSLiam Girdwood 	if (codec_dai->ops.hw_free)
516dee89c4dSMark Brown 		codec_dai->ops.hw_free(substream, codec_dai);
517cb666e5bSLiam Girdwood 
518cb666e5bSLiam Girdwood codec_err:
519cb666e5bSLiam Girdwood 	if (machine->ops && machine->ops->hw_free)
520cb666e5bSLiam Girdwood 		machine->ops->hw_free(substream);
521db2a4165SFrank Mandarino 
522db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
523db2a4165SFrank Mandarino 	return ret;
524db2a4165SFrank Mandarino }
525db2a4165SFrank Mandarino 
526db2a4165SFrank Mandarino /*
527db2a4165SFrank Mandarino  * Free's resources allocated by hw_params, can be called multiple times
528db2a4165SFrank Mandarino  */
529db2a4165SFrank Mandarino static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
530db2a4165SFrank Mandarino {
531db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
532db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
533cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
534db2a4165SFrank Mandarino 	struct snd_soc_platform *platform = socdev->platform;
5353c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
5363c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
537db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
538db2a4165SFrank Mandarino 
539db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
540db2a4165SFrank Mandarino 
541db2a4165SFrank Mandarino 	/* apply codec digital mute */
5428c6529dbSLiam Girdwood 	if (!codec->active)
5438c6529dbSLiam Girdwood 		snd_soc_dai_digital_mute(codec_dai, 1);
544db2a4165SFrank Mandarino 
545db2a4165SFrank Mandarino 	/* free any machine hw params */
546db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->hw_free)
547db2a4165SFrank Mandarino 		machine->ops->hw_free(substream);
548db2a4165SFrank Mandarino 
549db2a4165SFrank Mandarino 	/* free any DMA resources */
550db2a4165SFrank Mandarino 	if (platform->pcm_ops->hw_free)
551db2a4165SFrank Mandarino 		platform->pcm_ops->hw_free(substream);
552db2a4165SFrank Mandarino 
553db2a4165SFrank Mandarino 	/* now free hw params for the DAI's  */
554cb666e5bSLiam Girdwood 	if (codec_dai->ops.hw_free)
555dee89c4dSMark Brown 		codec_dai->ops.hw_free(substream, codec_dai);
556db2a4165SFrank Mandarino 
557cb666e5bSLiam Girdwood 	if (cpu_dai->ops.hw_free)
558dee89c4dSMark Brown 		cpu_dai->ops.hw_free(substream, cpu_dai);
559db2a4165SFrank Mandarino 
560db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
561db2a4165SFrank Mandarino 	return 0;
562db2a4165SFrank Mandarino }
563db2a4165SFrank Mandarino 
564db2a4165SFrank Mandarino static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
565db2a4165SFrank Mandarino {
566db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
567db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
568cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
569db2a4165SFrank Mandarino 	struct snd_soc_platform *platform = socdev->platform;
5703c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
5713c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
572db2a4165SFrank Mandarino 	int ret;
573db2a4165SFrank Mandarino 
574cb666e5bSLiam Girdwood 	if (codec_dai->ops.trigger) {
575dee89c4dSMark Brown 		ret = codec_dai->ops.trigger(substream, cmd, codec_dai);
576db2a4165SFrank Mandarino 		if (ret < 0)
577db2a4165SFrank Mandarino 			return ret;
578db2a4165SFrank Mandarino 	}
579db2a4165SFrank Mandarino 
580db2a4165SFrank Mandarino 	if (platform->pcm_ops->trigger) {
581db2a4165SFrank Mandarino 		ret = platform->pcm_ops->trigger(substream, cmd);
582db2a4165SFrank Mandarino 		if (ret < 0)
583db2a4165SFrank Mandarino 			return ret;
584db2a4165SFrank Mandarino 	}
585db2a4165SFrank Mandarino 
586cb666e5bSLiam Girdwood 	if (cpu_dai->ops.trigger) {
587dee89c4dSMark Brown 		ret = cpu_dai->ops.trigger(substream, cmd, cpu_dai);
588db2a4165SFrank Mandarino 		if (ret < 0)
589db2a4165SFrank Mandarino 			return ret;
590db2a4165SFrank Mandarino 	}
591db2a4165SFrank Mandarino 	return 0;
592db2a4165SFrank Mandarino }
593db2a4165SFrank Mandarino 
594db2a4165SFrank Mandarino /* ASoC PCM operations */
595db2a4165SFrank Mandarino static struct snd_pcm_ops soc_pcm_ops = {
596db2a4165SFrank Mandarino 	.open		= soc_pcm_open,
597db2a4165SFrank Mandarino 	.close		= soc_codec_close,
598db2a4165SFrank Mandarino 	.hw_params	= soc_pcm_hw_params,
599db2a4165SFrank Mandarino 	.hw_free	= soc_pcm_hw_free,
600db2a4165SFrank Mandarino 	.prepare	= soc_pcm_prepare,
601db2a4165SFrank Mandarino 	.trigger	= soc_pcm_trigger,
602db2a4165SFrank Mandarino };
603db2a4165SFrank Mandarino 
604db2a4165SFrank Mandarino #ifdef CONFIG_PM
605db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
606db2a4165SFrank Mandarino static int soc_suspend(struct platform_device *pdev, pm_message_t state)
607db2a4165SFrank Mandarino {
608db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
60987506549SMark Brown 	struct snd_soc_card *card = socdev->card;
610db2a4165SFrank Mandarino 	struct snd_soc_platform *platform = socdev->platform;
611db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
612db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
613db2a4165SFrank Mandarino 	int i;
614db2a4165SFrank Mandarino 
6156ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
6166ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
6176ed25978SAndy Green 	 */
6186ed25978SAndy Green 	snd_power_lock(codec->card);
6196ed25978SAndy Green 	snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
6206ed25978SAndy Green 	snd_power_unlock(codec->card);
6216ed25978SAndy Green 
6226ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
6236ed25978SAndy Green 	snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
6246ed25978SAndy Green 
625db2a4165SFrank Mandarino 	/* mute any active DAC's */
626dee89c4dSMark Brown 	for (i = 0; i < card->num_links; i++) {
627dee89c4dSMark Brown 		struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
628dee89c4dSMark Brown 		if (dai->ops.digital_mute && dai->playback.active)
629dee89c4dSMark Brown 			dai->ops.digital_mute(dai, 1);
630db2a4165SFrank Mandarino 	}
631db2a4165SFrank Mandarino 
6324ccab3e7SLiam Girdwood 	/* suspend all pcms */
63387506549SMark Brown 	for (i = 0; i < card->num_links; i++)
63487506549SMark Brown 		snd_pcm_suspend_all(card->dai_link[i].pcm);
6354ccab3e7SLiam Girdwood 
63687506549SMark Brown 	if (card->suspend_pre)
63787506549SMark Brown 		card->suspend_pre(pdev, state);
638db2a4165SFrank Mandarino 
63987506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
64087506549SMark Brown 		struct snd_soc_dai  *cpu_dai = card->dai_link[i].cpu_dai;
6413ba9e10aSMark Brown 		if (cpu_dai->suspend && !cpu_dai->ac97_control)
642db2a4165SFrank Mandarino 			cpu_dai->suspend(pdev, cpu_dai);
643db2a4165SFrank Mandarino 		if (platform->suspend)
644db2a4165SFrank Mandarino 			platform->suspend(pdev, cpu_dai);
645db2a4165SFrank Mandarino 	}
646db2a4165SFrank Mandarino 
647db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
648965ac42cSLiam Girdwood 	run_delayed_work(&socdev->delayed_work);
6490be9898aSMark Brown 	codec->suspend_bias_level = codec->bias_level;
650db2a4165SFrank Mandarino 
651db2a4165SFrank Mandarino 	for (i = 0; i < codec->num_dai; i++) {
652db2a4165SFrank Mandarino 		char *stream = codec->dai[i].playback.stream_name;
653db2a4165SFrank Mandarino 		if (stream != NULL)
654db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
655db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
656db2a4165SFrank Mandarino 		stream = codec->dai[i].capture.stream_name;
657db2a4165SFrank Mandarino 		if (stream != NULL)
658db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
659db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
660db2a4165SFrank Mandarino 	}
661db2a4165SFrank Mandarino 
662db2a4165SFrank Mandarino 	if (codec_dev->suspend)
663db2a4165SFrank Mandarino 		codec_dev->suspend(pdev, state);
664db2a4165SFrank Mandarino 
66587506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
66687506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
6673ba9e10aSMark Brown 		if (cpu_dai->suspend && cpu_dai->ac97_control)
668db2a4165SFrank Mandarino 			cpu_dai->suspend(pdev, cpu_dai);
669db2a4165SFrank Mandarino 	}
670db2a4165SFrank Mandarino 
67187506549SMark Brown 	if (card->suspend_post)
67287506549SMark Brown 		card->suspend_post(pdev, state);
673db2a4165SFrank Mandarino 
674db2a4165SFrank Mandarino 	return 0;
675db2a4165SFrank Mandarino }
676db2a4165SFrank Mandarino 
6776ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
6786ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6796ed25978SAndy Green  */
6806ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
681db2a4165SFrank Mandarino {
6826ed25978SAndy Green 	struct snd_soc_device *socdev = container_of(work,
6836ed25978SAndy Green 						     struct snd_soc_device,
6846ed25978SAndy Green 						     deferred_resume_work);
68587506549SMark Brown 	struct snd_soc_card *card = socdev->card;
686db2a4165SFrank Mandarino 	struct snd_soc_platform *platform = socdev->platform;
687db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
688db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
6896ed25978SAndy Green 	struct platform_device *pdev = to_platform_device(socdev->dev);
690db2a4165SFrank Mandarino 	int i;
691db2a4165SFrank Mandarino 
6926ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6936ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6946ed25978SAndy Green 	 */
6956ed25978SAndy Green 
696fde22f27SMark Brown 	dev_dbg(socdev->dev, "starting resume work\n");
6976ed25978SAndy Green 
69887506549SMark Brown 	if (card->resume_pre)
69987506549SMark Brown 		card->resume_pre(pdev);
700db2a4165SFrank Mandarino 
70187506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
70287506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
7033ba9e10aSMark Brown 		if (cpu_dai->resume && cpu_dai->ac97_control)
704db2a4165SFrank Mandarino 			cpu_dai->resume(pdev, cpu_dai);
705db2a4165SFrank Mandarino 	}
706db2a4165SFrank Mandarino 
707db2a4165SFrank Mandarino 	if (codec_dev->resume)
708db2a4165SFrank Mandarino 		codec_dev->resume(pdev);
709db2a4165SFrank Mandarino 
710db2a4165SFrank Mandarino 	for (i = 0; i < codec->num_dai; i++) {
711db2a4165SFrank Mandarino 		char *stream = codec->dai[i].playback.stream_name;
712db2a4165SFrank Mandarino 		if (stream != NULL)
713db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
714db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
715db2a4165SFrank Mandarino 		stream = codec->dai[i].capture.stream_name;
716db2a4165SFrank Mandarino 		if (stream != NULL)
717db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
718db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
719db2a4165SFrank Mandarino 	}
720db2a4165SFrank Mandarino 
7213ff3f64bSMark Brown 	/* unmute any active DACs */
722dee89c4dSMark Brown 	for (i = 0; i < card->num_links; i++) {
723dee89c4dSMark Brown 		struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
724dee89c4dSMark Brown 		if (dai->ops.digital_mute && dai->playback.active)
725dee89c4dSMark Brown 			dai->ops.digital_mute(dai, 0);
726db2a4165SFrank Mandarino 	}
727db2a4165SFrank Mandarino 
72887506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
72987506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
7303ba9e10aSMark Brown 		if (cpu_dai->resume && !cpu_dai->ac97_control)
731db2a4165SFrank Mandarino 			cpu_dai->resume(pdev, cpu_dai);
732db2a4165SFrank Mandarino 		if (platform->resume)
733db2a4165SFrank Mandarino 			platform->resume(pdev, cpu_dai);
734db2a4165SFrank Mandarino 	}
735db2a4165SFrank Mandarino 
73687506549SMark Brown 	if (card->resume_post)
73787506549SMark Brown 		card->resume_post(pdev);
738db2a4165SFrank Mandarino 
739fde22f27SMark Brown 	dev_dbg(socdev->dev, "resume work completed\n");
7406ed25978SAndy Green 
7416ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
7426ed25978SAndy Green 	snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
7436ed25978SAndy Green }
7446ed25978SAndy Green 
7456ed25978SAndy Green /* powers up audio subsystem after a suspend */
7466ed25978SAndy Green static int soc_resume(struct platform_device *pdev)
7476ed25978SAndy Green {
7486ed25978SAndy Green 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
7496ed25978SAndy Green 
750fde22f27SMark Brown 	dev_dbg(socdev->dev, "scheduling resume work\n");
7516ed25978SAndy Green 
7526ed25978SAndy Green 	if (!schedule_work(&socdev->deferred_resume_work))
753fde22f27SMark Brown 		dev_err(socdev->dev, "resume work item may be lost\n");
7546ed25978SAndy Green 
755db2a4165SFrank Mandarino 	return 0;
756db2a4165SFrank Mandarino }
757db2a4165SFrank Mandarino 
758db2a4165SFrank Mandarino #else
759db2a4165SFrank Mandarino #define soc_suspend	NULL
760db2a4165SFrank Mandarino #define soc_resume	NULL
761db2a4165SFrank Mandarino #endif
762db2a4165SFrank Mandarino 
763db2a4165SFrank Mandarino /* probes a new socdev */
764db2a4165SFrank Mandarino static int soc_probe(struct platform_device *pdev)
765db2a4165SFrank Mandarino {
766db2a4165SFrank Mandarino 	int ret = 0, i;
767db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
76887506549SMark Brown 	struct snd_soc_card *card = socdev->card;
769db2a4165SFrank Mandarino 	struct snd_soc_platform *platform = socdev->platform;
770db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
771db2a4165SFrank Mandarino 
77287506549SMark Brown 	if (card->probe) {
77387506549SMark Brown 		ret = card->probe(pdev);
774db2a4165SFrank Mandarino 		if (ret < 0)
775db2a4165SFrank Mandarino 			return ret;
776db2a4165SFrank Mandarino 	}
777db2a4165SFrank Mandarino 
77887506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
77987506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
780db2a4165SFrank Mandarino 		if (cpu_dai->probe) {
781bdb92876SMark Brown 			ret = cpu_dai->probe(pdev, cpu_dai);
782db2a4165SFrank Mandarino 			if (ret < 0)
783db2a4165SFrank Mandarino 				goto cpu_dai_err;
784db2a4165SFrank Mandarino 		}
785db2a4165SFrank Mandarino 	}
786db2a4165SFrank Mandarino 
787db2a4165SFrank Mandarino 	if (codec_dev->probe) {
788db2a4165SFrank Mandarino 		ret = codec_dev->probe(pdev);
789db2a4165SFrank Mandarino 		if (ret < 0)
790db2a4165SFrank Mandarino 			goto cpu_dai_err;
791db2a4165SFrank Mandarino 	}
792db2a4165SFrank Mandarino 
793db2a4165SFrank Mandarino 	if (platform->probe) {
794db2a4165SFrank Mandarino 		ret = platform->probe(pdev);
795db2a4165SFrank Mandarino 		if (ret < 0)
796db2a4165SFrank Mandarino 			goto platform_err;
797db2a4165SFrank Mandarino 	}
798db2a4165SFrank Mandarino 
799db2a4165SFrank Mandarino 	/* DAPM stream work */
8004484bb2eSAndrew Morton 	INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
8011301a964SRandy Dunlap #ifdef CONFIG_PM
8026ed25978SAndy Green 	/* deferred resume work */
8036ed25978SAndy Green 	INIT_WORK(&socdev->deferred_resume_work, soc_resume_deferred);
8041301a964SRandy Dunlap #endif
8056ed25978SAndy Green 
806db2a4165SFrank Mandarino 	return 0;
807db2a4165SFrank Mandarino 
808db2a4165SFrank Mandarino platform_err:
809db2a4165SFrank Mandarino 	if (codec_dev->remove)
810db2a4165SFrank Mandarino 		codec_dev->remove(pdev);
811db2a4165SFrank Mandarino 
812db2a4165SFrank Mandarino cpu_dai_err:
81318b9b3d9SLiam Girdwood 	for (i--; i >= 0; i--) {
81487506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
815db2a4165SFrank Mandarino 		if (cpu_dai->remove)
816bdb92876SMark Brown 			cpu_dai->remove(pdev, cpu_dai);
817db2a4165SFrank Mandarino 	}
818db2a4165SFrank Mandarino 
81987506549SMark Brown 	if (card->remove)
82087506549SMark Brown 		card->remove(pdev);
821db2a4165SFrank Mandarino 
822db2a4165SFrank Mandarino 	return ret;
823db2a4165SFrank Mandarino }
824db2a4165SFrank Mandarino 
825db2a4165SFrank Mandarino /* removes a socdev */
826db2a4165SFrank Mandarino static int soc_remove(struct platform_device *pdev)
827db2a4165SFrank Mandarino {
828db2a4165SFrank Mandarino 	int i;
829db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
83087506549SMark Brown 	struct snd_soc_card *card = socdev->card;
831db2a4165SFrank Mandarino 	struct snd_soc_platform *platform = socdev->platform;
832db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
833db2a4165SFrank Mandarino 
834965ac42cSLiam Girdwood 	run_delayed_work(&socdev->delayed_work);
835965ac42cSLiam Girdwood 
836db2a4165SFrank Mandarino 	if (platform->remove)
837db2a4165SFrank Mandarino 		platform->remove(pdev);
838db2a4165SFrank Mandarino 
839db2a4165SFrank Mandarino 	if (codec_dev->remove)
840db2a4165SFrank Mandarino 		codec_dev->remove(pdev);
841db2a4165SFrank Mandarino 
84287506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
84387506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
844db2a4165SFrank Mandarino 		if (cpu_dai->remove)
845bdb92876SMark Brown 			cpu_dai->remove(pdev, cpu_dai);
846db2a4165SFrank Mandarino 	}
847db2a4165SFrank Mandarino 
84887506549SMark Brown 	if (card->remove)
84987506549SMark Brown 		card->remove(pdev);
850db2a4165SFrank Mandarino 
851db2a4165SFrank Mandarino 	return 0;
852db2a4165SFrank Mandarino }
853db2a4165SFrank Mandarino 
854db2a4165SFrank Mandarino /* ASoC platform driver */
855db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
856db2a4165SFrank Mandarino 	.driver		= {
857db2a4165SFrank Mandarino 		.name		= "soc-audio",
8588b45a209SKay Sievers 		.owner		= THIS_MODULE,
859db2a4165SFrank Mandarino 	},
860db2a4165SFrank Mandarino 	.probe		= soc_probe,
861db2a4165SFrank Mandarino 	.remove		= soc_remove,
862db2a4165SFrank Mandarino 	.suspend	= soc_suspend,
863db2a4165SFrank Mandarino 	.resume		= soc_resume,
864db2a4165SFrank Mandarino };
865db2a4165SFrank Mandarino 
866db2a4165SFrank Mandarino /* create a new pcm */
867db2a4165SFrank Mandarino static int soc_new_pcm(struct snd_soc_device *socdev,
868db2a4165SFrank Mandarino 	struct snd_soc_dai_link *dai_link, int num)
869db2a4165SFrank Mandarino {
870db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
8713c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = dai_link->codec_dai;
8723c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
873db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd;
874db2a4165SFrank Mandarino 	struct snd_pcm *pcm;
875db2a4165SFrank Mandarino 	char new_name[64];
876db2a4165SFrank Mandarino 	int ret = 0, playback = 0, capture = 0;
877db2a4165SFrank Mandarino 
878db2a4165SFrank Mandarino 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
879db2a4165SFrank Mandarino 	if (rtd == NULL)
880db2a4165SFrank Mandarino 		return -ENOMEM;
881cb666e5bSLiam Girdwood 
882cb666e5bSLiam Girdwood 	rtd->dai = dai_link;
883db2a4165SFrank Mandarino 	rtd->socdev = socdev;
884cb666e5bSLiam Girdwood 	codec_dai->codec = socdev->codec;
885db2a4165SFrank Mandarino 
886db2a4165SFrank Mandarino 	/* check client and interface hw capabilities */
8873ba9e10aSMark Brown 	sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name,
8883ba9e10aSMark Brown 		num);
889db2a4165SFrank Mandarino 
890db2a4165SFrank Mandarino 	if (codec_dai->playback.channels_min)
891db2a4165SFrank Mandarino 		playback = 1;
892db2a4165SFrank Mandarino 	if (codec_dai->capture.channels_min)
893db2a4165SFrank Mandarino 		capture = 1;
894db2a4165SFrank Mandarino 
895db2a4165SFrank Mandarino 	ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
896db2a4165SFrank Mandarino 		capture, &pcm);
897db2a4165SFrank Mandarino 	if (ret < 0) {
8983ff3f64bSMark Brown 		printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
8993ff3f64bSMark Brown 			codec->name);
900db2a4165SFrank Mandarino 		kfree(rtd);
901db2a4165SFrank Mandarino 		return ret;
902db2a4165SFrank Mandarino 	}
903db2a4165SFrank Mandarino 
9044ccab3e7SLiam Girdwood 	dai_link->pcm = pcm;
905db2a4165SFrank Mandarino 	pcm->private_data = rtd;
906db2a4165SFrank Mandarino 	soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
907db2a4165SFrank Mandarino 	soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
908db2a4165SFrank Mandarino 	soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
909db2a4165SFrank Mandarino 	soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
910db2a4165SFrank Mandarino 	soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
911db2a4165SFrank Mandarino 	soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
912db2a4165SFrank Mandarino 	soc_pcm_ops.page = socdev->platform->pcm_ops->page;
913db2a4165SFrank Mandarino 
914db2a4165SFrank Mandarino 	if (playback)
915db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
916db2a4165SFrank Mandarino 
917db2a4165SFrank Mandarino 	if (capture)
918db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
919db2a4165SFrank Mandarino 
920db2a4165SFrank Mandarino 	ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
921db2a4165SFrank Mandarino 	if (ret < 0) {
922db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: platform pcm constructor failed\n");
923db2a4165SFrank Mandarino 		kfree(rtd);
924db2a4165SFrank Mandarino 		return ret;
925db2a4165SFrank Mandarino 	}
926db2a4165SFrank Mandarino 
927db2a4165SFrank Mandarino 	pcm->private_free = socdev->platform->pcm_free;
928db2a4165SFrank Mandarino 	printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
929db2a4165SFrank Mandarino 		cpu_dai->name);
930db2a4165SFrank Mandarino 	return ret;
931db2a4165SFrank Mandarino }
932db2a4165SFrank Mandarino 
933db2a4165SFrank Mandarino /* codec register dump */
93412ef193dSTroy Kisky static ssize_t soc_codec_reg_show(struct snd_soc_device *devdata, char *buf)
935db2a4165SFrank Mandarino {
936db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = devdata->codec;
937db2a4165SFrank Mandarino 	int i, step = 1, count = 0;
938db2a4165SFrank Mandarino 
939db2a4165SFrank Mandarino 	if (!codec->reg_cache_size)
940db2a4165SFrank Mandarino 		return 0;
941db2a4165SFrank Mandarino 
942db2a4165SFrank Mandarino 	if (codec->reg_cache_step)
943db2a4165SFrank Mandarino 		step = codec->reg_cache_step;
944db2a4165SFrank Mandarino 
945db2a4165SFrank Mandarino 	count += sprintf(buf, "%s registers\n", codec->name);
94658cd33c0SMark Brown 	for (i = 0; i < codec->reg_cache_size; i += step) {
94758cd33c0SMark Brown 		count += sprintf(buf + count, "%2x: ", i);
94858cd33c0SMark Brown 		if (count >= PAGE_SIZE - 1)
94958cd33c0SMark Brown 			break;
95058cd33c0SMark Brown 
95158cd33c0SMark Brown 		if (codec->display_register)
95258cd33c0SMark Brown 			count += codec->display_register(codec, buf + count,
95358cd33c0SMark Brown 							 PAGE_SIZE - count, i);
95458cd33c0SMark Brown 		else
95558cd33c0SMark Brown 			count += snprintf(buf + count, PAGE_SIZE - count,
95658cd33c0SMark Brown 					  "%4x", codec->read(codec, i));
95758cd33c0SMark Brown 
95858cd33c0SMark Brown 		if (count >= PAGE_SIZE - 1)
95958cd33c0SMark Brown 			break;
96058cd33c0SMark Brown 
96158cd33c0SMark Brown 		count += snprintf(buf + count, PAGE_SIZE - count, "\n");
96258cd33c0SMark Brown 		if (count >= PAGE_SIZE - 1)
96358cd33c0SMark Brown 			break;
96458cd33c0SMark Brown 	}
96558cd33c0SMark Brown 
96658cd33c0SMark Brown 	/* Truncate count; min() would cause a warning */
96758cd33c0SMark Brown 	if (count >= PAGE_SIZE)
96858cd33c0SMark Brown 		count = PAGE_SIZE - 1;
969db2a4165SFrank Mandarino 
970db2a4165SFrank Mandarino 	return count;
971db2a4165SFrank Mandarino }
97212ef193dSTroy Kisky static ssize_t codec_reg_show(struct device *dev,
97312ef193dSTroy Kisky 	struct device_attribute *attr, char *buf)
97412ef193dSTroy Kisky {
97512ef193dSTroy Kisky 	struct snd_soc_device *devdata = dev_get_drvdata(dev);
97612ef193dSTroy Kisky 	return soc_codec_reg_show(devdata, buf);
97712ef193dSTroy Kisky }
97812ef193dSTroy Kisky 
979db2a4165SFrank Mandarino static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
980db2a4165SFrank Mandarino 
98112ef193dSTroy Kisky #ifdef CONFIG_DEBUG_FS
98212ef193dSTroy Kisky static int codec_reg_open_file(struct inode *inode, struct file *file)
98312ef193dSTroy Kisky {
98412ef193dSTroy Kisky 	file->private_data = inode->i_private;
98512ef193dSTroy Kisky 	return 0;
98612ef193dSTroy Kisky }
98712ef193dSTroy Kisky 
98812ef193dSTroy Kisky static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
98912ef193dSTroy Kisky 			       size_t count, loff_t *ppos)
99012ef193dSTroy Kisky {
99112ef193dSTroy Kisky 	ssize_t ret;
99212ef193dSTroy Kisky 	struct snd_soc_device *devdata = file->private_data;
99312ef193dSTroy Kisky 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
99412ef193dSTroy Kisky 	if (!buf)
99512ef193dSTroy Kisky 		return -ENOMEM;
99612ef193dSTroy Kisky 	ret = soc_codec_reg_show(devdata, buf);
99712ef193dSTroy Kisky 	if (ret >= 0)
99812ef193dSTroy Kisky 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
99912ef193dSTroy Kisky 	kfree(buf);
100012ef193dSTroy Kisky 	return ret;
100112ef193dSTroy Kisky }
100212ef193dSTroy Kisky 
100312ef193dSTroy Kisky static ssize_t codec_reg_write_file(struct file *file,
100412ef193dSTroy Kisky 		const char __user *user_buf, size_t count, loff_t *ppos)
100512ef193dSTroy Kisky {
100612ef193dSTroy Kisky 	char buf[32];
100712ef193dSTroy Kisky 	int buf_size;
100812ef193dSTroy Kisky 	char *start = buf;
100912ef193dSTroy Kisky 	unsigned long reg, value;
101012ef193dSTroy Kisky 	int step = 1;
101112ef193dSTroy Kisky 	struct snd_soc_device *devdata = file->private_data;
101212ef193dSTroy Kisky 	struct snd_soc_codec *codec = devdata->codec;
101312ef193dSTroy Kisky 
101412ef193dSTroy Kisky 	buf_size = min(count, (sizeof(buf)-1));
101512ef193dSTroy Kisky 	if (copy_from_user(buf, user_buf, buf_size))
101612ef193dSTroy Kisky 		return -EFAULT;
101712ef193dSTroy Kisky 	buf[buf_size] = 0;
101812ef193dSTroy Kisky 
101912ef193dSTroy Kisky 	if (codec->reg_cache_step)
102012ef193dSTroy Kisky 		step = codec->reg_cache_step;
102112ef193dSTroy Kisky 
102212ef193dSTroy Kisky 	while (*start == ' ')
102312ef193dSTroy Kisky 		start++;
102412ef193dSTroy Kisky 	reg = simple_strtoul(start, &start, 16);
102512ef193dSTroy Kisky 	if ((reg >= codec->reg_cache_size) || (reg % step))
102612ef193dSTroy Kisky 		return -EINVAL;
102712ef193dSTroy Kisky 	while (*start == ' ')
102812ef193dSTroy Kisky 		start++;
102912ef193dSTroy Kisky 	if (strict_strtoul(start, 16, &value))
103012ef193dSTroy Kisky 		return -EINVAL;
103112ef193dSTroy Kisky 	codec->write(codec, reg, value);
103212ef193dSTroy Kisky 	return buf_size;
103312ef193dSTroy Kisky }
103412ef193dSTroy Kisky 
103512ef193dSTroy Kisky static const struct file_operations codec_reg_fops = {
103612ef193dSTroy Kisky 	.open = codec_reg_open_file,
103712ef193dSTroy Kisky 	.read = codec_reg_read_file,
103812ef193dSTroy Kisky 	.write = codec_reg_write_file,
103912ef193dSTroy Kisky };
104012ef193dSTroy Kisky 
104112ef193dSTroy Kisky static void soc_init_debugfs(struct snd_soc_device *socdev)
104212ef193dSTroy Kisky {
104312ef193dSTroy Kisky 	struct dentry *root, *file;
104412ef193dSTroy Kisky 	struct snd_soc_codec *codec = socdev->codec;
104512ef193dSTroy Kisky 	root = debugfs_create_dir(dev_name(socdev->dev), NULL);
104612ef193dSTroy Kisky 	if (IS_ERR(root) || !root)
104712ef193dSTroy Kisky 		goto exit1;
104812ef193dSTroy Kisky 
104912ef193dSTroy Kisky 	file = debugfs_create_file("codec_reg", 0644,
105012ef193dSTroy Kisky 			root, socdev, &codec_reg_fops);
105112ef193dSTroy Kisky 	if (!file)
105212ef193dSTroy Kisky 		goto exit2;
105312ef193dSTroy Kisky 
105412ef193dSTroy Kisky 	file = debugfs_create_u32("dapm_pop_time", 0744,
105512ef193dSTroy Kisky 			root, &codec->pop_time);
105612ef193dSTroy Kisky 	if (!file)
105712ef193dSTroy Kisky 		goto exit2;
105812ef193dSTroy Kisky 	socdev->debugfs_root = root;
105912ef193dSTroy Kisky 	return;
106012ef193dSTroy Kisky exit2:
106112ef193dSTroy Kisky 	debugfs_remove_recursive(root);
106212ef193dSTroy Kisky exit1:
106312ef193dSTroy Kisky 	dev_err(socdev->dev, "debugfs is not available\n");
106412ef193dSTroy Kisky }
106512ef193dSTroy Kisky 
106612ef193dSTroy Kisky static void soc_cleanup_debugfs(struct snd_soc_device *socdev)
106712ef193dSTroy Kisky {
106812ef193dSTroy Kisky 	debugfs_remove_recursive(socdev->debugfs_root);
106912ef193dSTroy Kisky 	socdev->debugfs_root = NULL;
107012ef193dSTroy Kisky }
107112ef193dSTroy Kisky 
107212ef193dSTroy Kisky #else
107312ef193dSTroy Kisky 
107412ef193dSTroy Kisky static inline void soc_init_debugfs(struct snd_soc_device *socdev)
107512ef193dSTroy Kisky {
107612ef193dSTroy Kisky }
107712ef193dSTroy Kisky 
107812ef193dSTroy Kisky static inline void soc_cleanup_debugfs(struct snd_soc_device *socdev)
107912ef193dSTroy Kisky {
108012ef193dSTroy Kisky }
108112ef193dSTroy Kisky #endif
108212ef193dSTroy Kisky 
1083db2a4165SFrank Mandarino /**
1084db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
1085db2a4165SFrank Mandarino  * @codec: audio codec
1086db2a4165SFrank Mandarino  * @ops: AC97 bus operations
1087db2a4165SFrank Mandarino  * @num: AC97 codec number
1088db2a4165SFrank Mandarino  *
1089db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
1090db2a4165SFrank Mandarino  */
1091db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1092db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
1093db2a4165SFrank Mandarino {
1094db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1095db2a4165SFrank Mandarino 
1096db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1097db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
1098db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1099db2a4165SFrank Mandarino 		return -ENOMEM;
1100db2a4165SFrank Mandarino 	}
1101db2a4165SFrank Mandarino 
1102db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1103db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
1104db2a4165SFrank Mandarino 		kfree(codec->ac97);
1105db2a4165SFrank Mandarino 		codec->ac97 = NULL;
1106db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1107db2a4165SFrank Mandarino 		return -ENOMEM;
1108db2a4165SFrank Mandarino 	}
1109db2a4165SFrank Mandarino 
1110db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
1111db2a4165SFrank Mandarino 	codec->ac97->num = num;
1112db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1113db2a4165SFrank Mandarino 	return 0;
1114db2a4165SFrank Mandarino }
1115db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1116db2a4165SFrank Mandarino 
1117db2a4165SFrank Mandarino /**
1118db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
1119db2a4165SFrank Mandarino  * @codec: audio codec
1120db2a4165SFrank Mandarino  *
1121db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
1122db2a4165SFrank Mandarino  */
1123db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1124db2a4165SFrank Mandarino {
1125db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1126db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
1127db2a4165SFrank Mandarino 	kfree(codec->ac97);
1128db2a4165SFrank Mandarino 	codec->ac97 = NULL;
1129db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1130db2a4165SFrank Mandarino }
1131db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1132db2a4165SFrank Mandarino 
1133db2a4165SFrank Mandarino /**
1134db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
1135db2a4165SFrank Mandarino  * @codec: audio codec
1136db2a4165SFrank Mandarino  * @reg: codec register
1137db2a4165SFrank Mandarino  * @mask: register mask
1138db2a4165SFrank Mandarino  * @value: new value
1139db2a4165SFrank Mandarino  *
1140db2a4165SFrank Mandarino  * Writes new register value.
1141db2a4165SFrank Mandarino  *
1142db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1143db2a4165SFrank Mandarino  */
1144db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1145db2a4165SFrank Mandarino 				unsigned short mask, unsigned short value)
1146db2a4165SFrank Mandarino {
1147db2a4165SFrank Mandarino 	int change;
1148db2a4165SFrank Mandarino 	unsigned short old, new;
1149db2a4165SFrank Mandarino 
1150db2a4165SFrank Mandarino 	mutex_lock(&io_mutex);
1151db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
1152db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
1153db2a4165SFrank Mandarino 	change = old != new;
1154db2a4165SFrank Mandarino 	if (change)
1155db2a4165SFrank Mandarino 		snd_soc_write(codec, reg, new);
1156db2a4165SFrank Mandarino 
1157db2a4165SFrank Mandarino 	mutex_unlock(&io_mutex);
1158db2a4165SFrank Mandarino 	return change;
1159db2a4165SFrank Mandarino }
1160db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1161db2a4165SFrank Mandarino 
1162db2a4165SFrank Mandarino /**
1163db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
1164db2a4165SFrank Mandarino  * @codec: audio codec
1165db2a4165SFrank Mandarino  * @reg: codec register
1166db2a4165SFrank Mandarino  * @mask: register mask
1167db2a4165SFrank Mandarino  * @value: new value
1168db2a4165SFrank Mandarino  *
1169db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
1170db2a4165SFrank Mandarino  * different from the old value.
1171db2a4165SFrank Mandarino  *
1172db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1173db2a4165SFrank Mandarino  */
1174db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1175db2a4165SFrank Mandarino 				unsigned short mask, unsigned short value)
1176db2a4165SFrank Mandarino {
1177db2a4165SFrank Mandarino 	int change;
1178db2a4165SFrank Mandarino 	unsigned short old, new;
1179db2a4165SFrank Mandarino 
1180db2a4165SFrank Mandarino 	mutex_lock(&io_mutex);
1181db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
1182db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
1183db2a4165SFrank Mandarino 	change = old != new;
1184db2a4165SFrank Mandarino 	mutex_unlock(&io_mutex);
1185db2a4165SFrank Mandarino 
1186db2a4165SFrank Mandarino 	return change;
1187db2a4165SFrank Mandarino }
1188db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1189db2a4165SFrank Mandarino 
1190db2a4165SFrank Mandarino /**
1191db2a4165SFrank Mandarino  * snd_soc_new_pcms - create new sound card and pcms
1192db2a4165SFrank Mandarino  * @socdev: the SoC audio device
1193db2a4165SFrank Mandarino  *
1194db2a4165SFrank Mandarino  * Create a new sound card based upon the codec and interface pcms.
1195db2a4165SFrank Mandarino  *
1196db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1197db2a4165SFrank Mandarino  */
1198db2a4165SFrank Mandarino int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1199db2a4165SFrank Mandarino {
1200db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
120187506549SMark Brown 	struct snd_soc_card *card = socdev->card;
1202db2a4165SFrank Mandarino 	int ret = 0, i;
1203db2a4165SFrank Mandarino 
1204db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1205db2a4165SFrank Mandarino 
1206db2a4165SFrank Mandarino 	/* register a sound card */
1207db2a4165SFrank Mandarino 	codec->card = snd_card_new(idx, xid, codec->owner, 0);
1208db2a4165SFrank Mandarino 	if (!codec->card) {
1209db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1210db2a4165SFrank Mandarino 			codec->name);
1211db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1212db2a4165SFrank Mandarino 		return -ENODEV;
1213db2a4165SFrank Mandarino 	}
1214db2a4165SFrank Mandarino 
1215db2a4165SFrank Mandarino 	codec->card->dev = socdev->dev;
1216db2a4165SFrank Mandarino 	codec->card->private_data = codec;
1217db2a4165SFrank Mandarino 	strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1218db2a4165SFrank Mandarino 
1219db2a4165SFrank Mandarino 	/* create the pcms */
122087506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
122187506549SMark Brown 		ret = soc_new_pcm(socdev, &card->dai_link[i], i);
1222db2a4165SFrank Mandarino 		if (ret < 0) {
1223db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't create pcm %s\n",
122487506549SMark Brown 				card->dai_link[i].stream_name);
1225db2a4165SFrank Mandarino 			mutex_unlock(&codec->mutex);
1226db2a4165SFrank Mandarino 			return ret;
1227db2a4165SFrank Mandarino 		}
1228db2a4165SFrank Mandarino 	}
1229db2a4165SFrank Mandarino 
1230db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1231db2a4165SFrank Mandarino 	return ret;
1232db2a4165SFrank Mandarino }
1233db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1234db2a4165SFrank Mandarino 
1235db2a4165SFrank Mandarino /**
1236*968a6025SMark Brown  * snd_soc_init_card - register sound card
1237db2a4165SFrank Mandarino  * @socdev: the SoC audio device
1238db2a4165SFrank Mandarino  *
1239db2a4165SFrank Mandarino  * Register a SoC sound card. Also registers an AC97 device if the
1240db2a4165SFrank Mandarino  * codec is AC97 for ad hoc devices.
1241db2a4165SFrank Mandarino  *
1242db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1243db2a4165SFrank Mandarino  */
1244*968a6025SMark Brown int snd_soc_init_card(struct snd_soc_device *socdev)
1245db2a4165SFrank Mandarino {
1246db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
124787506549SMark Brown 	struct snd_soc_card *card = socdev->card;
124812e74f7dSLiam Girdwood 	int ret = 0, i, ac97 = 0, err = 0;
1249db2a4165SFrank Mandarino 
125087506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
125187506549SMark Brown 		if (card->dai_link[i].init) {
125287506549SMark Brown 			err = card->dai_link[i].init(codec);
125312e74f7dSLiam Girdwood 			if (err < 0) {
125412e74f7dSLiam Girdwood 				printk(KERN_ERR "asoc: failed to init %s\n",
125587506549SMark Brown 					card->dai_link[i].stream_name);
125612e74f7dSLiam Girdwood 				continue;
125712e74f7dSLiam Girdwood 			}
125812e74f7dSLiam Girdwood 		}
12593ba9e10aSMark Brown 		if (card->dai_link[i].codec_dai->ac97_control)
1260db2a4165SFrank Mandarino 			ac97 = 1;
1261db2a4165SFrank Mandarino 	}
1262db2a4165SFrank Mandarino 	snprintf(codec->card->shortname, sizeof(codec->card->shortname),
126387506549SMark Brown 		 "%s",  card->name);
1264db2a4165SFrank Mandarino 	snprintf(codec->card->longname, sizeof(codec->card->longname),
126587506549SMark Brown 		 "%s (%s)", card->name, codec->name);
1266db2a4165SFrank Mandarino 
1267db2a4165SFrank Mandarino 	ret = snd_card_register(codec->card);
1268db2a4165SFrank Mandarino 	if (ret < 0) {
12693ff3f64bSMark Brown 		printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1270db2a4165SFrank Mandarino 				codec->name);
127112e74f7dSLiam Girdwood 		goto out;
1272db2a4165SFrank Mandarino 	}
1273db2a4165SFrank Mandarino 
127408c8efe6SMark Brown 	mutex_lock(&codec->mutex);
1275db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
127612e74f7dSLiam Girdwood 	if (ac97) {
127712e74f7dSLiam Girdwood 		ret = soc_ac97_dev_register(codec);
127812e74f7dSLiam Girdwood 		if (ret < 0) {
127912e74f7dSLiam Girdwood 			printk(KERN_ERR "asoc: AC97 device register failed\n");
128012e74f7dSLiam Girdwood 			snd_card_free(codec->card);
128108c8efe6SMark Brown 			mutex_unlock(&codec->mutex);
128212e74f7dSLiam Girdwood 			goto out;
128312e74f7dSLiam Girdwood 		}
128412e74f7dSLiam Girdwood 	}
1285db2a4165SFrank Mandarino #endif
1286db2a4165SFrank Mandarino 
128712e74f7dSLiam Girdwood 	err = snd_soc_dapm_sys_add(socdev->dev);
128812e74f7dSLiam Girdwood 	if (err < 0)
128912e74f7dSLiam Girdwood 		printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
129012e74f7dSLiam Girdwood 
129112e74f7dSLiam Girdwood 	err = device_create_file(socdev->dev, &dev_attr_codec_reg);
129212e74f7dSLiam Girdwood 	if (err < 0)
12933ff3f64bSMark Brown 		printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
129408c8efe6SMark Brown 
129512ef193dSTroy Kisky 	soc_init_debugfs(socdev);
1296db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
129708c8efe6SMark Brown 
129808c8efe6SMark Brown out:
1299db2a4165SFrank Mandarino 	return ret;
1300db2a4165SFrank Mandarino }
1301*968a6025SMark Brown EXPORT_SYMBOL_GPL(snd_soc_init_card);
1302db2a4165SFrank Mandarino 
1303db2a4165SFrank Mandarino /**
1304db2a4165SFrank Mandarino  * snd_soc_free_pcms - free sound card and pcms
1305db2a4165SFrank Mandarino  * @socdev: the SoC audio device
1306db2a4165SFrank Mandarino  *
1307db2a4165SFrank Mandarino  * Frees sound card and pcms associated with the socdev.
1308db2a4165SFrank Mandarino  * Also unregister the codec if it is an AC97 device.
1309db2a4165SFrank Mandarino  */
1310db2a4165SFrank Mandarino void snd_soc_free_pcms(struct snd_soc_device *socdev)
1311db2a4165SFrank Mandarino {
1312db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
1313a68660e0SLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
13143c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai;
1315a68660e0SLiam Girdwood 	int i;
1316a68660e0SLiam Girdwood #endif
1317db2a4165SFrank Mandarino 
1318db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
131912ef193dSTroy Kisky 	soc_cleanup_debugfs(socdev);
1320db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
1321a68660e0SLiam Girdwood 	for (i = 0; i < codec->num_dai; i++) {
1322a68660e0SLiam Girdwood 		codec_dai = &codec->dai[i];
13233ba9e10aSMark Brown 		if (codec_dai->ac97_control && codec->ac97) {
1324db2a4165SFrank Mandarino 			soc_ac97_dev_unregister(codec);
1325a68660e0SLiam Girdwood 			goto free_card;
1326a68660e0SLiam Girdwood 		}
1327a68660e0SLiam Girdwood 	}
1328a68660e0SLiam Girdwood free_card:
1329db2a4165SFrank Mandarino #endif
1330db2a4165SFrank Mandarino 
1331db2a4165SFrank Mandarino 	if (codec->card)
1332db2a4165SFrank Mandarino 		snd_card_free(codec->card);
1333db2a4165SFrank Mandarino 	device_remove_file(socdev->dev, &dev_attr_codec_reg);
1334db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1335db2a4165SFrank Mandarino }
1336db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1337db2a4165SFrank Mandarino 
1338db2a4165SFrank Mandarino /**
1339db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1340db2a4165SFrank Mandarino  * @substream: the pcm substream
1341db2a4165SFrank Mandarino  * @hw: the hardware parameters
1342db2a4165SFrank Mandarino  *
1343db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
1344db2a4165SFrank Mandarino  */
1345db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1346db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
1347db2a4165SFrank Mandarino {
1348db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
1349db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
1350db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
1351db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
1352db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
1353db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
1354db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
1355db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1356db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
1357db2a4165SFrank Mandarino 	return 0;
1358db2a4165SFrank Mandarino }
1359db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1360db2a4165SFrank Mandarino 
1361db2a4165SFrank Mandarino /**
1362db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
1363db2a4165SFrank Mandarino  * @_template: control template
1364db2a4165SFrank Mandarino  * @data: control private data
1365db2a4165SFrank Mandarino  * @lnng_name: control long name
1366db2a4165SFrank Mandarino  *
1367db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
1368db2a4165SFrank Mandarino  *
1369db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1370db2a4165SFrank Mandarino  */
1371db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1372db2a4165SFrank Mandarino 	void *data, char *long_name)
1373db2a4165SFrank Mandarino {
1374db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
1375db2a4165SFrank Mandarino 
1376db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
1377db2a4165SFrank Mandarino 	if (long_name)
1378db2a4165SFrank Mandarino 		template.name = long_name;
1379db2a4165SFrank Mandarino 	template.index = 0;
1380db2a4165SFrank Mandarino 
1381db2a4165SFrank Mandarino 	return snd_ctl_new1(&template, data);
1382db2a4165SFrank Mandarino }
1383db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
1384db2a4165SFrank Mandarino 
1385db2a4165SFrank Mandarino /**
1386db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
1387db2a4165SFrank Mandarino  * @kcontrol: mixer control
1388db2a4165SFrank Mandarino  * @uinfo: control element information
1389db2a4165SFrank Mandarino  *
1390db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
1391db2a4165SFrank Mandarino  * mixer control.
1392db2a4165SFrank Mandarino  *
1393db2a4165SFrank Mandarino  * Returns 0 for success.
1394db2a4165SFrank Mandarino  */
1395db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1396db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1397db2a4165SFrank Mandarino {
1398db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1399db2a4165SFrank Mandarino 
1400db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1401db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1402f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
1403db2a4165SFrank Mandarino 
1404f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
1405f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
1406db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
1407db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
1408db2a4165SFrank Mandarino 	return 0;
1409db2a4165SFrank Mandarino }
1410db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1411db2a4165SFrank Mandarino 
1412db2a4165SFrank Mandarino /**
1413db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
1414db2a4165SFrank Mandarino  * @kcontrol: mixer control
1415db2a4165SFrank Mandarino  * @uinfo: control element information
1416db2a4165SFrank Mandarino  *
1417db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
1418db2a4165SFrank Mandarino  *
1419db2a4165SFrank Mandarino  * Returns 0 for success.
1420db2a4165SFrank Mandarino  */
1421db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1422db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1423db2a4165SFrank Mandarino {
1424db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1425db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1426db2a4165SFrank Mandarino 	unsigned short val, bitmask;
1427db2a4165SFrank Mandarino 
1428f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1429db2a4165SFrank Mandarino 		;
1430db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
14313ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
14323ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
1433db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
1434db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
1435db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
1436db2a4165SFrank Mandarino 
1437db2a4165SFrank Mandarino 	return 0;
1438db2a4165SFrank Mandarino }
1439db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1440db2a4165SFrank Mandarino 
1441db2a4165SFrank Mandarino /**
1442db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
1443db2a4165SFrank Mandarino  * @kcontrol: mixer control
1444db2a4165SFrank Mandarino  * @uinfo: control element information
1445db2a4165SFrank Mandarino  *
1446db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
1447db2a4165SFrank Mandarino  *
1448db2a4165SFrank Mandarino  * Returns 0 for success.
1449db2a4165SFrank Mandarino  */
1450db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1451db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1452db2a4165SFrank Mandarino {
1453db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1454db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1455db2a4165SFrank Mandarino 	unsigned short val;
1456db2a4165SFrank Mandarino 	unsigned short mask, bitmask;
1457db2a4165SFrank Mandarino 
1458f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1459db2a4165SFrank Mandarino 		;
1460f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
1461db2a4165SFrank Mandarino 		return -EINVAL;
1462db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
1463db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
1464db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
1465f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
1466db2a4165SFrank Mandarino 			return -EINVAL;
1467db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1468db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
1469db2a4165SFrank Mandarino 	}
1470db2a4165SFrank Mandarino 
1471db2a4165SFrank Mandarino 	return snd_soc_update_bits(codec, e->reg, mask, val);
1472db2a4165SFrank Mandarino }
1473db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1474db2a4165SFrank Mandarino 
1475db2a4165SFrank Mandarino /**
1476db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
1477db2a4165SFrank Mandarino  * @kcontrol: mixer control
1478db2a4165SFrank Mandarino  * @uinfo: control element information
1479db2a4165SFrank Mandarino  *
1480db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
1481db2a4165SFrank Mandarino  * single mixer.
1482db2a4165SFrank Mandarino  *
1483db2a4165SFrank Mandarino  * Returns 0 for success.
1484db2a4165SFrank Mandarino  */
1485db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1486db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1487db2a4165SFrank Mandarino {
1488db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1489db2a4165SFrank Mandarino 
1490db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1491db2a4165SFrank Mandarino 	uinfo->count = 1;
1492f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
1493db2a4165SFrank Mandarino 
1494f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
1495f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
1496db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
1497db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
1498db2a4165SFrank Mandarino 	return 0;
1499db2a4165SFrank Mandarino }
1500db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1501db2a4165SFrank Mandarino 
1502db2a4165SFrank Mandarino /**
1503db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
1504db2a4165SFrank Mandarino  * @kcontrol: mixer control
1505db2a4165SFrank Mandarino  * @uinfo: control element information
1506db2a4165SFrank Mandarino  *
1507db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
1508db2a4165SFrank Mandarino  *
1509db2a4165SFrank Mandarino  * Returns 0 for success.
1510db2a4165SFrank Mandarino  */
1511db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1512db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1513db2a4165SFrank Mandarino {
1514a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
1515db2a4165SFrank Mandarino 
1516a7a4ac86SPhilipp Zabel 	if (max == 1)
1517a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1518a7a4ac86SPhilipp Zabel 	else
1519a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1520a7a4ac86SPhilipp Zabel 
1521db2a4165SFrank Mandarino 	uinfo->count = 1;
1522db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
1523a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
1524db2a4165SFrank Mandarino 	return 0;
1525db2a4165SFrank Mandarino }
1526db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1527db2a4165SFrank Mandarino 
1528db2a4165SFrank Mandarino /**
1529db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
1530db2a4165SFrank Mandarino  * @kcontrol: mixer control
1531db2a4165SFrank Mandarino  * @uinfo: control element information
1532db2a4165SFrank Mandarino  *
1533db2a4165SFrank Mandarino  * Callback to provide information about a single mixer control.
1534db2a4165SFrank Mandarino  *
1535db2a4165SFrank Mandarino  * Returns 0 for success.
1536db2a4165SFrank Mandarino  */
1537db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1538db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1539db2a4165SFrank Mandarino {
15404eaa9819SJon Smirl 	struct soc_mixer_control *mc =
15414eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
15424eaa9819SJon Smirl 	int max = mc->max;
1543762b8df7SMark Brown 	unsigned int shift = mc->shift;
1544815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
1545db2a4165SFrank Mandarino 
1546a7a4ac86SPhilipp Zabel 	if (max == 1)
1547a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1548a7a4ac86SPhilipp Zabel 	else
1549a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1550a7a4ac86SPhilipp Zabel 
1551db2a4165SFrank Mandarino 	uinfo->count = shift == rshift ? 1 : 2;
1552db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
1553a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
1554db2a4165SFrank Mandarino 	return 0;
1555db2a4165SFrank Mandarino }
1556db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1557db2a4165SFrank Mandarino 
1558db2a4165SFrank Mandarino /**
1559db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
1560db2a4165SFrank Mandarino  * @kcontrol: mixer control
1561db2a4165SFrank Mandarino  * @uinfo: control element information
1562db2a4165SFrank Mandarino  *
1563db2a4165SFrank Mandarino  * Callback to get the value of a single mixer control.
1564db2a4165SFrank Mandarino  *
1565db2a4165SFrank Mandarino  * Returns 0 for success.
1566db2a4165SFrank Mandarino  */
1567db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1568db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1569db2a4165SFrank Mandarino {
15704eaa9819SJon Smirl 	struct soc_mixer_control *mc =
15714eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
1572db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1573815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
1574815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
1575815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
15764eaa9819SJon Smirl 	int max = mc->max;
1577815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
1578815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
1579db2a4165SFrank Mandarino 
1580db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
1581db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
1582db2a4165SFrank Mandarino 	if (shift != rshift)
1583db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
1584db2a4165SFrank Mandarino 			(snd_soc_read(codec, reg) >> rshift) & mask;
1585db2a4165SFrank Mandarino 	if (invert) {
1586db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
1587a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
1588db2a4165SFrank Mandarino 		if (shift != rshift)
1589db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
1590a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
1591db2a4165SFrank Mandarino 	}
1592db2a4165SFrank Mandarino 
1593db2a4165SFrank Mandarino 	return 0;
1594db2a4165SFrank Mandarino }
1595db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1596db2a4165SFrank Mandarino 
1597db2a4165SFrank Mandarino /**
1598db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
1599db2a4165SFrank Mandarino  * @kcontrol: mixer control
1600db2a4165SFrank Mandarino  * @uinfo: control element information
1601db2a4165SFrank Mandarino  *
1602db2a4165SFrank Mandarino  * Callback to set the value of a single mixer control.
1603db2a4165SFrank Mandarino  *
1604db2a4165SFrank Mandarino  * Returns 0 for success.
1605db2a4165SFrank Mandarino  */
1606db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1607db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1608db2a4165SFrank Mandarino {
16094eaa9819SJon Smirl 	struct soc_mixer_control *mc =
16104eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
1611db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1612815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
1613815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
1614815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
16154eaa9819SJon Smirl 	int max = mc->max;
1616815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
1617815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
1618db2a4165SFrank Mandarino 	unsigned short val, val2, val_mask;
1619db2a4165SFrank Mandarino 
1620db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
1621db2a4165SFrank Mandarino 	if (invert)
1622a7a4ac86SPhilipp Zabel 		val = max - val;
1623db2a4165SFrank Mandarino 	val_mask = mask << shift;
1624db2a4165SFrank Mandarino 	val = val << shift;
1625db2a4165SFrank Mandarino 	if (shift != rshift) {
1626db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
1627db2a4165SFrank Mandarino 		if (invert)
1628a7a4ac86SPhilipp Zabel 			val2 = max - val2;
1629db2a4165SFrank Mandarino 		val_mask |= mask << rshift;
1630db2a4165SFrank Mandarino 		val |= val2 << rshift;
1631db2a4165SFrank Mandarino 	}
1632a7a4ac86SPhilipp Zabel 	return snd_soc_update_bits(codec, reg, val_mask, val);
1633db2a4165SFrank Mandarino }
1634db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1635db2a4165SFrank Mandarino 
1636db2a4165SFrank Mandarino /**
1637db2a4165SFrank Mandarino  * snd_soc_info_volsw_2r - double mixer info callback
1638db2a4165SFrank Mandarino  * @kcontrol: mixer control
1639db2a4165SFrank Mandarino  * @uinfo: control element information
1640db2a4165SFrank Mandarino  *
1641db2a4165SFrank Mandarino  * Callback to provide information about a double mixer control that
1642db2a4165SFrank Mandarino  * spans 2 codec registers.
1643db2a4165SFrank Mandarino  *
1644db2a4165SFrank Mandarino  * Returns 0 for success.
1645db2a4165SFrank Mandarino  */
1646db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1647db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1648db2a4165SFrank Mandarino {
16494eaa9819SJon Smirl 	struct soc_mixer_control *mc =
16504eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
16514eaa9819SJon Smirl 	int max = mc->max;
1652db2a4165SFrank Mandarino 
1653a7a4ac86SPhilipp Zabel 	if (max == 1)
1654a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1655a7a4ac86SPhilipp Zabel 	else
1656a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1657a7a4ac86SPhilipp Zabel 
1658db2a4165SFrank Mandarino 	uinfo->count = 2;
1659db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
1660a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
1661db2a4165SFrank Mandarino 	return 0;
1662db2a4165SFrank Mandarino }
1663db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1664db2a4165SFrank Mandarino 
1665db2a4165SFrank Mandarino /**
1666db2a4165SFrank Mandarino  * snd_soc_get_volsw_2r - double mixer get callback
1667db2a4165SFrank Mandarino  * @kcontrol: mixer control
1668db2a4165SFrank Mandarino  * @uinfo: control element information
1669db2a4165SFrank Mandarino  *
1670db2a4165SFrank Mandarino  * Callback to get the value of a double mixer control that spans 2 registers.
1671db2a4165SFrank Mandarino  *
1672db2a4165SFrank Mandarino  * Returns 0 for success.
1673db2a4165SFrank Mandarino  */
1674db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1675db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1676db2a4165SFrank Mandarino {
16774eaa9819SJon Smirl 	struct soc_mixer_control *mc =
16784eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
1679db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1680815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
1681815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
1682815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
16834eaa9819SJon Smirl 	int max = mc->max;
1684815ecf8dSJon Smirl 	unsigned int mask = (1<<fls(max))-1;
1685815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
1686db2a4165SFrank Mandarino 
1687db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
1688db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
1689db2a4165SFrank Mandarino 	ucontrol->value.integer.value[1] =
1690db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg2) >> shift) & mask;
1691db2a4165SFrank Mandarino 	if (invert) {
1692db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
1693a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
1694db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
1695a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[1];
1696db2a4165SFrank Mandarino 	}
1697db2a4165SFrank Mandarino 
1698db2a4165SFrank Mandarino 	return 0;
1699db2a4165SFrank Mandarino }
1700db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1701db2a4165SFrank Mandarino 
1702db2a4165SFrank Mandarino /**
1703db2a4165SFrank Mandarino  * snd_soc_put_volsw_2r - double mixer set callback
1704db2a4165SFrank Mandarino  * @kcontrol: mixer control
1705db2a4165SFrank Mandarino  * @uinfo: control element information
1706db2a4165SFrank Mandarino  *
1707db2a4165SFrank Mandarino  * Callback to set the value of a double mixer control that spans 2 registers.
1708db2a4165SFrank Mandarino  *
1709db2a4165SFrank Mandarino  * Returns 0 for success.
1710db2a4165SFrank Mandarino  */
1711db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1712db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1713db2a4165SFrank Mandarino {
17144eaa9819SJon Smirl 	struct soc_mixer_control *mc =
17154eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
1716db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1717815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
1718815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
1719815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
17204eaa9819SJon Smirl 	int max = mc->max;
1721815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
1722815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
1723db2a4165SFrank Mandarino 	int err;
1724db2a4165SFrank Mandarino 	unsigned short val, val2, val_mask;
1725db2a4165SFrank Mandarino 
1726db2a4165SFrank Mandarino 	val_mask = mask << shift;
1727db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
1728db2a4165SFrank Mandarino 	val2 = (ucontrol->value.integer.value[1] & mask);
1729db2a4165SFrank Mandarino 
1730db2a4165SFrank Mandarino 	if (invert) {
1731a7a4ac86SPhilipp Zabel 		val = max - val;
1732a7a4ac86SPhilipp Zabel 		val2 = max - val2;
1733db2a4165SFrank Mandarino 	}
1734db2a4165SFrank Mandarino 
1735db2a4165SFrank Mandarino 	val = val << shift;
1736db2a4165SFrank Mandarino 	val2 = val2 << shift;
1737db2a4165SFrank Mandarino 
17383ff3f64bSMark Brown 	err = snd_soc_update_bits(codec, reg, val_mask, val);
17393ff3f64bSMark Brown 	if (err < 0)
1740db2a4165SFrank Mandarino 		return err;
1741db2a4165SFrank Mandarino 
1742db2a4165SFrank Mandarino 	err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1743db2a4165SFrank Mandarino 	return err;
1744db2a4165SFrank Mandarino }
1745db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1746db2a4165SFrank Mandarino 
1747e13ac2e9SMark Brown /**
1748e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
1749e13ac2e9SMark Brown  * @kcontrol: mixer control
1750e13ac2e9SMark Brown  * @uinfo: control element information
1751e13ac2e9SMark Brown  *
1752e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
1753e13ac2e9SMark Brown  *
1754e13ac2e9SMark Brown  * Returns 0 for success.
1755e13ac2e9SMark Brown  */
1756e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1757e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
1758e13ac2e9SMark Brown {
17594eaa9819SJon Smirl 	struct soc_mixer_control *mc =
17604eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
17614eaa9819SJon Smirl 	int max = mc->max;
17624eaa9819SJon Smirl 	int min = mc->min;
1763e13ac2e9SMark Brown 
1764e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1765e13ac2e9SMark Brown 	uinfo->count = 2;
1766e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
1767e13ac2e9SMark Brown 	uinfo->value.integer.max = max-min;
1768e13ac2e9SMark Brown 	return 0;
1769e13ac2e9SMark Brown }
1770e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1771e13ac2e9SMark Brown 
1772e13ac2e9SMark Brown /**
1773e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
1774e13ac2e9SMark Brown  * @kcontrol: mixer control
1775e13ac2e9SMark Brown  * @uinfo: control element information
1776e13ac2e9SMark Brown  *
1777e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
1778e13ac2e9SMark Brown  *
1779e13ac2e9SMark Brown  * Returns 0 for success.
1780e13ac2e9SMark Brown  */
1781e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1782e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
1783e13ac2e9SMark Brown {
17844eaa9819SJon Smirl 	struct soc_mixer_control *mc =
17854eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
1786e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1787815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
17884eaa9819SJon Smirl 	int min = mc->min;
1789e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
1790e13ac2e9SMark Brown 
1791e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
1792e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
1793e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
1794e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
1795e13ac2e9SMark Brown 	return 0;
1796e13ac2e9SMark Brown }
1797e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
1798e13ac2e9SMark Brown 
1799e13ac2e9SMark Brown /**
1800e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
1801e13ac2e9SMark Brown  * @kcontrol: mixer control
1802e13ac2e9SMark Brown  * @uinfo: control element information
1803e13ac2e9SMark Brown  *
1804e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
1805e13ac2e9SMark Brown  *
1806e13ac2e9SMark Brown  * Returns 0 for success.
1807e13ac2e9SMark Brown  */
1808e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
1809e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
1810e13ac2e9SMark Brown {
18114eaa9819SJon Smirl 	struct soc_mixer_control *mc =
18124eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
1813e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1814815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
18154eaa9819SJon Smirl 	int min = mc->min;
1816e13ac2e9SMark Brown 	unsigned short val;
1817e13ac2e9SMark Brown 
1818e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
1819e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
1820e13ac2e9SMark Brown 
1821e13ac2e9SMark Brown 	return snd_soc_update_bits(codec, reg, 0xffff, val);
1822e13ac2e9SMark Brown }
1823e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
1824e13ac2e9SMark Brown 
18258c6529dbSLiam Girdwood /**
18268c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
18278c6529dbSLiam Girdwood  * @dai: DAI
18288c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
18298c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
18308c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
18318c6529dbSLiam Girdwood  *
18328c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
18338c6529dbSLiam Girdwood  */
18348c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
18358c6529dbSLiam Girdwood 	unsigned int freq, int dir)
18368c6529dbSLiam Girdwood {
1837dee89c4dSMark Brown 	if (dai->ops.set_sysclk)
1838dee89c4dSMark Brown 		return dai->ops.set_sysclk(dai, clk_id, freq, dir);
18398c6529dbSLiam Girdwood 	else
18408c6529dbSLiam Girdwood 		return -EINVAL;
18418c6529dbSLiam Girdwood }
18428c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
18438c6529dbSLiam Girdwood 
18448c6529dbSLiam Girdwood /**
18458c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
18468c6529dbSLiam Girdwood  * @dai: DAI
18478c6529dbSLiam Girdwood  * @clk_id: DAI specific clock divider ID
18488c6529dbSLiam Girdwood  * @div: new clock divisor.
18498c6529dbSLiam Girdwood  *
18508c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
18518c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
18528c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
18538c6529dbSLiam Girdwood  */
18548c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
18558c6529dbSLiam Girdwood 	int div_id, int div)
18568c6529dbSLiam Girdwood {
1857dee89c4dSMark Brown 	if (dai->ops.set_clkdiv)
1858dee89c4dSMark Brown 		return dai->ops.set_clkdiv(dai, div_id, div);
18598c6529dbSLiam Girdwood 	else
18608c6529dbSLiam Girdwood 		return -EINVAL;
18618c6529dbSLiam Girdwood }
18628c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
18638c6529dbSLiam Girdwood 
18648c6529dbSLiam Girdwood /**
18658c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
18668c6529dbSLiam Girdwood  * @dai: DAI
18678c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
18688c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
18698c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
18708c6529dbSLiam Girdwood  *
18718c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
18728c6529dbSLiam Girdwood  */
18738c6529dbSLiam Girdwood int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
18748c6529dbSLiam Girdwood 	int pll_id, unsigned int freq_in, unsigned int freq_out)
18758c6529dbSLiam Girdwood {
1876dee89c4dSMark Brown 	if (dai->ops.set_pll)
1877dee89c4dSMark Brown 		return dai->ops.set_pll(dai, pll_id, freq_in, freq_out);
18788c6529dbSLiam Girdwood 	else
18798c6529dbSLiam Girdwood 		return -EINVAL;
18808c6529dbSLiam Girdwood }
18818c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
18828c6529dbSLiam Girdwood 
18838c6529dbSLiam Girdwood /**
18848c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
18858c6529dbSLiam Girdwood  * @dai: DAI
18868c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
18878c6529dbSLiam Girdwood  *
18888c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
18898c6529dbSLiam Girdwood  */
18908c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
18918c6529dbSLiam Girdwood {
1892dee89c4dSMark Brown 	if (dai->ops.set_fmt)
1893dee89c4dSMark Brown 		return dai->ops.set_fmt(dai, fmt);
18948c6529dbSLiam Girdwood 	else
18958c6529dbSLiam Girdwood 		return -EINVAL;
18968c6529dbSLiam Girdwood }
18978c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
18988c6529dbSLiam Girdwood 
18998c6529dbSLiam Girdwood /**
19008c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
19018c6529dbSLiam Girdwood  * @dai: DAI
19028c6529dbSLiam Girdwood  * @mask: DAI specific mask representing used slots.
19038c6529dbSLiam Girdwood  * @slots: Number of slots in use.
19048c6529dbSLiam Girdwood  *
19058c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
19068c6529dbSLiam Girdwood  * specific.
19078c6529dbSLiam Girdwood  */
19088c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
19098c6529dbSLiam Girdwood 	unsigned int mask, int slots)
19108c6529dbSLiam Girdwood {
1911dee89c4dSMark Brown 	if (dai->ops.set_sysclk)
1912dee89c4dSMark Brown 		return dai->ops.set_tdm_slot(dai, mask, slots);
19138c6529dbSLiam Girdwood 	else
19148c6529dbSLiam Girdwood 		return -EINVAL;
19158c6529dbSLiam Girdwood }
19168c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
19178c6529dbSLiam Girdwood 
19188c6529dbSLiam Girdwood /**
19198c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
19208c6529dbSLiam Girdwood  * @dai: DAI
19218c6529dbSLiam Girdwood  * @tristate: tristate enable
19228c6529dbSLiam Girdwood  *
19238c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
19248c6529dbSLiam Girdwood  */
19258c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
19268c6529dbSLiam Girdwood {
1927dee89c4dSMark Brown 	if (dai->ops.set_sysclk)
1928dee89c4dSMark Brown 		return dai->ops.set_tristate(dai, tristate);
19298c6529dbSLiam Girdwood 	else
19308c6529dbSLiam Girdwood 		return -EINVAL;
19318c6529dbSLiam Girdwood }
19328c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
19338c6529dbSLiam Girdwood 
19348c6529dbSLiam Girdwood /**
19358c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
19368c6529dbSLiam Girdwood  * @dai: DAI
19378c6529dbSLiam Girdwood  * @mute: mute enable
19388c6529dbSLiam Girdwood  *
19398c6529dbSLiam Girdwood  * Mutes the DAI DAC.
19408c6529dbSLiam Girdwood  */
19418c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
19428c6529dbSLiam Girdwood {
1943dee89c4dSMark Brown 	if (dai->ops.digital_mute)
1944dee89c4dSMark Brown 		return dai->ops.digital_mute(dai, mute);
19458c6529dbSLiam Girdwood 	else
19468c6529dbSLiam Girdwood 		return -EINVAL;
19478c6529dbSLiam Girdwood }
19488c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
19498c6529dbSLiam Girdwood 
1950db2a4165SFrank Mandarino static int __devinit snd_soc_init(void)
1951db2a4165SFrank Mandarino {
1952db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
1953db2a4165SFrank Mandarino }
1954db2a4165SFrank Mandarino 
1955db2a4165SFrank Mandarino static void snd_soc_exit(void)
1956db2a4165SFrank Mandarino {
1957db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
1958db2a4165SFrank Mandarino }
1959db2a4165SFrank Mandarino 
1960db2a4165SFrank Mandarino module_init(snd_soc_init);
1961db2a4165SFrank Mandarino module_exit(snd_soc_exit);
1962db2a4165SFrank Mandarino 
1963db2a4165SFrank Mandarino /* Module information */
1964d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
1965db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
1966db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
19678b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
1968