blob: c4b22e6984e6a008139397c96bbaf90410e953b2 [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwood0664d882006-12-18 14:39:02 +01005 * Copyright 2005 Openedhand Ltd.
6 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +01008 * with code, comments and ideas from :-
9 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020010 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020016 * TODO:
17 * o Add hw rules to enforce rates, etc.
18 * o More testing with other codecs/machines.
19 * o Add more codecs and platforms to ensure good API coverage.
20 * o Support TDM on PCM and I2S
21 */
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/pm.h>
28#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070029#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020030#include <linux/platform_device.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020031#include <sound/core.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/soc.h>
35#include <sound/soc-dapm.h>
36#include <sound/initval.h>
37
Frank Mandarinodb2a4162006-10-06 18:31:09 +020038static DEFINE_MUTEX(pcm_mutex);
39static DEFINE_MUTEX(io_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +020040static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
41
Frank Mandarinodb2a4162006-10-06 18:31:09 +020042/*
43 * This is a timeout to do a DAPM powerdown after a stream is closed().
44 * It can be used to eliminate pops between different playback streams, e.g.
45 * between two audio tracks.
46 */
47static int pmdown_time = 5000;
48module_param(pmdown_time, int, 0);
49MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
50
Liam Girdwood965ac422007-01-31 14:14:57 +010051/*
52 * This function forces any delayed work to be queued and run.
53 */
54static int run_delayed_work(struct delayed_work *dwork)
55{
56 int ret;
57
58 /* cancel any work waiting to be queued. */
59 ret = cancel_delayed_work(dwork);
60
61 /* if there was any work waiting then we run it now and
62 * wait for it's completion */
63 if (ret) {
64 schedule_delayed_work(dwork, 0);
65 flush_scheduled_work();
66 }
67 return ret;
68}
69
Frank Mandarinodb2a4162006-10-06 18:31:09 +020070#ifdef CONFIG_SND_SOC_AC97_BUS
71/* unregister ac97 codec */
72static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
73{
74 if (codec->ac97->dev.bus)
75 device_unregister(&codec->ac97->dev);
76 return 0;
77}
78
79/* stop no dev release warning */
80static void soc_ac97_device_release(struct device *dev){}
81
82/* register ac97 codec to bus */
83static int soc_ac97_dev_register(struct snd_soc_codec *codec)
84{
85 int err;
86
87 codec->ac97->dev.bus = &ac97_bus_type;
88 codec->ac97->dev.parent = NULL;
89 codec->ac97->dev.release = soc_ac97_device_release;
90
Kay Sieversbb072bf2008-11-02 03:50:35 +010091 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
92 codec->card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +020093 err = device_register(&codec->ac97->dev);
94 if (err < 0) {
95 snd_printk(KERN_ERR "Can't register ac97 bus\n");
96 codec->ac97->dev.bus = NULL;
97 return err;
98 }
99 return 0;
100}
101#endif
102
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200103/*
104 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
105 * then initialized and any private data can be allocated. This also calls
106 * startup for the cpu DAI, platform, machine and codec DAI.
107 */
108static int soc_pcm_open(struct snd_pcm_substream *substream)
109{
110 struct snd_soc_pcm_runtime *rtd = substream->private_data;
111 struct snd_soc_device *socdev = rtd->socdev;
112 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100113 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200114 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100115 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
116 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200117 int ret = 0;
118
119 mutex_lock(&pcm_mutex);
120
121 /* startup the audio subsystem */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100122 if (cpu_dai->ops.startup) {
Mark Browndee89c42008-11-18 22:11:38 +0000123 ret = cpu_dai->ops.startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200124 if (ret < 0) {
125 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100126 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200127 goto out;
128 }
129 }
130
131 if (platform->pcm_ops->open) {
132 ret = platform->pcm_ops->open(substream);
133 if (ret < 0) {
134 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
135 goto platform_err;
136 }
137 }
138
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100139 if (codec_dai->ops.startup) {
Mark Browndee89c42008-11-18 22:11:38 +0000140 ret = codec_dai->ops.startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100141 if (ret < 0) {
142 printk(KERN_ERR "asoc: can't open codec %s\n",
143 codec_dai->name);
144 goto codec_dai_err;
145 }
146 }
147
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200148 if (machine->ops && machine->ops->startup) {
149 ret = machine->ops->startup(substream);
150 if (ret < 0) {
151 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
152 goto machine_err;
153 }
154 }
155
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200156 /* Check that the codec and cpu DAI's are compatible */
157 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
158 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200159 max(codec_dai->playback.rate_min,
160 cpu_dai->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200161 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200162 min(codec_dai->playback.rate_max,
163 cpu_dai->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200164 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100165 max(codec_dai->playback.channels_min,
166 cpu_dai->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200167 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100168 min(codec_dai->playback.channels_max,
169 cpu_dai->playback.channels_max);
170 runtime->hw.formats =
171 codec_dai->playback.formats & cpu_dai->playback.formats;
172 runtime->hw.rates =
173 codec_dai->playback.rates & cpu_dai->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200174 } else {
175 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200176 max(codec_dai->capture.rate_min,
177 cpu_dai->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200178 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200179 min(codec_dai->capture.rate_max,
180 cpu_dai->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200181 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100182 max(codec_dai->capture.channels_min,
183 cpu_dai->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200184 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100185 min(codec_dai->capture.channels_max,
186 cpu_dai->capture.channels_max);
187 runtime->hw.formats =
188 codec_dai->capture.formats & cpu_dai->capture.formats;
189 runtime->hw.rates =
190 codec_dai->capture.rates & cpu_dai->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200191 }
192
193 snd_pcm_limit_hw_rates(runtime);
194 if (!runtime->hw.rates) {
195 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100196 codec_dai->name, cpu_dai->name);
197 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200198 }
199 if (!runtime->hw.formats) {
200 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100201 codec_dai->name, cpu_dai->name);
202 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200203 }
204 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
205 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100206 codec_dai->name, cpu_dai->name);
207 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200208 }
209
Mark Brownf24368c2008-10-21 21:45:08 +0100210 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
211 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
212 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
213 runtime->hw.channels_max);
214 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
215 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200216
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200217 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100218 cpu_dai->playback.active = codec_dai->playback.active = 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200219 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100220 cpu_dai->capture.active = codec_dai->capture.active = 1;
221 cpu_dai->active = codec_dai->active = 1;
222 cpu_dai->runtime = runtime;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200223 socdev->codec->active++;
224 mutex_unlock(&pcm_mutex);
225 return 0;
226
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100227machine_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200228 if (machine->ops && machine->ops->shutdown)
229 machine->ops->shutdown(substream);
230
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100231codec_dai_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200232 if (platform->pcm_ops->close)
233 platform->pcm_ops->close(substream);
234
235platform_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100236 if (cpu_dai->ops.shutdown)
Mark Browndee89c42008-11-18 22:11:38 +0000237 cpu_dai->ops.shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200238out:
239 mutex_unlock(&pcm_mutex);
240 return ret;
241}
242
243/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200244 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200245 * This is to ensure there are no pops or clicks in between any music tracks
246 * due to DAPM power cycling.
247 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100248static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200249{
Mark Brown63084192008-12-02 15:08:03 +0000250 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
251 delayed_work.work);
252 struct snd_soc_device *socdev = card->socdev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200253 struct snd_soc_codec *codec = socdev->codec;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100254 struct snd_soc_dai *codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200255 int i;
256
257 mutex_lock(&pcm_mutex);
Mark Brown3ff3f642008-05-19 12:32:25 +0200258 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200259 codec_dai = &codec->dai[i];
260
Mark Brownf24368c2008-10-21 21:45:08 +0100261 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
262 codec_dai->playback.stream_name,
263 codec_dai->playback.active ? "active" : "inactive",
264 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200265
266 /* are we waiting on this codec DAI stream */
267 if (codec_dai->pop_wait == 1) {
268
Mark Brown0be98982008-05-19 12:31:28 +0200269 /* Reduce power if no longer active */
Liam Girdwood3c1c47e2008-01-10 14:38:24 +0100270 if (codec->active == 0) {
Mark Brownf24368c2008-10-21 21:45:08 +0100271 pr_debug("pop wq D1 %s %s\n", codec->name,
272 codec_dai->playback.stream_name);
Mark Brown0be98982008-05-19 12:31:28 +0200273 snd_soc_dapm_set_bias_level(socdev,
274 SND_SOC_BIAS_PREPARE);
Liam Girdwood3c1c47e2008-01-10 14:38:24 +0100275 }
276
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200277 codec_dai->pop_wait = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100278 snd_soc_dapm_stream_event(codec,
279 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200280 SND_SOC_DAPM_STREAM_STOP);
281
Mark Brown0be98982008-05-19 12:31:28 +0200282 /* Fall into standby if no longer active */
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200283 if (codec->active == 0) {
Mark Brownf24368c2008-10-21 21:45:08 +0100284 pr_debug("pop wq D3 %s %s\n", codec->name,
285 codec_dai->playback.stream_name);
Mark Brown0be98982008-05-19 12:31:28 +0200286 snd_soc_dapm_set_bias_level(socdev,
287 SND_SOC_BIAS_STANDBY);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200288 }
289 }
290 }
291 mutex_unlock(&pcm_mutex);
292}
293
294/*
295 * Called by ALSA when a PCM substream is closed. Private data can be
296 * freed here. The cpu DAI, codec DAI, machine and platform are also
297 * shutdown.
298 */
299static int soc_codec_close(struct snd_pcm_substream *substream)
300{
301 struct snd_soc_pcm_runtime *rtd = substream->private_data;
302 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000303 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100304 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200305 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100306 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
307 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200308 struct snd_soc_codec *codec = socdev->codec;
309
310 mutex_lock(&pcm_mutex);
311
312 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100313 cpu_dai->playback.active = codec_dai->playback.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200314 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100315 cpu_dai->capture.active = codec_dai->capture.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200316
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100317 if (codec_dai->playback.active == 0 &&
318 codec_dai->capture.active == 0) {
319 cpu_dai->active = codec_dai->active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200320 }
321 codec->active--;
322
Mark Brown6010b2d2008-09-06 18:33:24 +0100323 /* Muting the DAC suppresses artifacts caused during digital
324 * shutdown, for example from stopping clocks.
325 */
326 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
327 snd_soc_dai_digital_mute(codec_dai, 1);
328
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100329 if (cpu_dai->ops.shutdown)
Mark Browndee89c42008-11-18 22:11:38 +0000330 cpu_dai->ops.shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200331
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100332 if (codec_dai->ops.shutdown)
Mark Browndee89c42008-11-18 22:11:38 +0000333 codec_dai->ops.shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200334
335 if (machine->ops && machine->ops->shutdown)
336 machine->ops->shutdown(substream);
337
338 if (platform->pcm_ops->close)
339 platform->pcm_ops->close(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100340 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200341
342 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
343 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100344 codec_dai->pop_wait = 1;
Mark Brown63084192008-12-02 15:08:03 +0000345 schedule_delayed_work(&card->delayed_work,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200346 msecs_to_jiffies(pmdown_time));
347 } else {
348 /* capture streams can be powered down now */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100349 snd_soc_dapm_stream_event(codec,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100350 codec_dai->capture.stream_name,
351 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200352
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100353 if (codec->active == 0 && codec_dai->pop_wait == 0)
Mark Brown0be98982008-05-19 12:31:28 +0200354 snd_soc_dapm_set_bias_level(socdev,
355 SND_SOC_BIAS_STANDBY);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200356 }
357
358 mutex_unlock(&pcm_mutex);
359 return 0;
360}
361
362/*
363 * Called by ALSA when the PCM substream is prepared, can set format, sample
364 * rate, etc. This function is non atomic and can be called multiple times,
365 * it can refer to the runtime info.
366 */
367static int soc_pcm_prepare(struct snd_pcm_substream *substream)
368{
369 struct snd_soc_pcm_runtime *rtd = substream->private_data;
370 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000371 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100372 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200373 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100374 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
375 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200376 struct snd_soc_codec *codec = socdev->codec;
377 int ret = 0;
378
379 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100380
381 if (machine->ops && machine->ops->prepare) {
382 ret = machine->ops->prepare(substream);
383 if (ret < 0) {
384 printk(KERN_ERR "asoc: machine prepare error\n");
385 goto out;
386 }
387 }
388
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200389 if (platform->pcm_ops->prepare) {
390 ret = platform->pcm_ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200391 if (ret < 0) {
392 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200393 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200394 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200395 }
396
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100397 if (codec_dai->ops.prepare) {
Mark Browndee89c42008-11-18 22:11:38 +0000398 ret = codec_dai->ops.prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200399 if (ret < 0) {
400 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200401 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200402 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200403 }
404
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100405 if (cpu_dai->ops.prepare) {
Mark Browndee89c42008-11-18 22:11:38 +0000406 ret = cpu_dai->ops.prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100407 if (ret < 0) {
408 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
409 goto out;
410 }
411 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200412
Mark Brownd45f6212008-10-14 13:58:36 +0100413 /* cancel any delayed stream shutdown that is pending */
414 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
415 codec_dai->pop_wait) {
416 codec_dai->pop_wait = 0;
Mark Brown63084192008-12-02 15:08:03 +0000417 cancel_delayed_work(&card->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100418 }
419
420 /* do we need to power up codec */
421 if (codec->bias_level != SND_SOC_BIAS_ON) {
422 snd_soc_dapm_set_bias_level(socdev,
423 SND_SOC_BIAS_PREPARE);
424
425 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
426 snd_soc_dapm_stream_event(codec,
427 codec_dai->playback.stream_name,
428 SND_SOC_DAPM_STREAM_START);
429 else
430 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100431 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200432 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100433
434 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
435 snd_soc_dai_digital_mute(codec_dai, 0);
436
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200437 } else {
Mark Brownd45f6212008-10-14 13:58:36 +0100438 /* codec already powered - power on widgets */
439 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
440 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100441 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200442 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100443 else
444 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100445 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200446 SND_SOC_DAPM_STREAM_START);
447
Mark Brownd45f6212008-10-14 13:58:36 +0100448 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200449 }
450
451out:
452 mutex_unlock(&pcm_mutex);
453 return ret;
454}
455
456/*
457 * Called by ALSA when the hardware params are set by application. This
458 * function can also be called multiple times and can allocate buffers
459 * (using snd_pcm_lib_* ). It's non-atomic.
460 */
461static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
462 struct snd_pcm_hw_params *params)
463{
464 struct snd_soc_pcm_runtime *rtd = substream->private_data;
465 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100466 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200467 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100468 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
469 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200470 int ret = 0;
471
472 mutex_lock(&pcm_mutex);
473
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100474 if (machine->ops && machine->ops->hw_params) {
475 ret = machine->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200476 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100477 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200478 goto out;
479 }
480 }
481
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100482 if (codec_dai->ops.hw_params) {
Mark Browndee89c42008-11-18 22:11:38 +0000483 ret = codec_dai->ops.hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100484 if (ret < 0) {
485 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
486 codec_dai->name);
487 goto codec_err;
488 }
489 }
490
491 if (cpu_dai->ops.hw_params) {
Mark Browndee89c42008-11-18 22:11:38 +0000492 ret = cpu_dai->ops.hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200493 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200494 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100495 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200496 goto interface_err;
497 }
498 }
499
500 if (platform->pcm_ops->hw_params) {
501 ret = platform->pcm_ops->hw_params(substream, params);
502 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200503 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200504 platform->name);
505 goto platform_err;
506 }
507 }
508
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200509out:
510 mutex_unlock(&pcm_mutex);
511 return ret;
512
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200513platform_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100514 if (cpu_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000515 cpu_dai->ops.hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200516
517interface_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100518 if (codec_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000519 codec_dai->ops.hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100520
521codec_err:
Mark Brown3ff3f642008-05-19 12:32:25 +0200522 if (machine->ops && machine->ops->hw_free)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100523 machine->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200524
525 mutex_unlock(&pcm_mutex);
526 return ret;
527}
528
529/*
530 * Free's resources allocated by hw_params, can be called multiple times
531 */
532static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
533{
534 struct snd_soc_pcm_runtime *rtd = substream->private_data;
535 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100536 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200537 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100538 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
539 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200540 struct snd_soc_codec *codec = socdev->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200541
542 mutex_lock(&pcm_mutex);
543
544 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100545 if (!codec->active)
546 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200547
548 /* free any machine hw params */
549 if (machine->ops && machine->ops->hw_free)
550 machine->ops->hw_free(substream);
551
552 /* free any DMA resources */
553 if (platform->pcm_ops->hw_free)
554 platform->pcm_ops->hw_free(substream);
555
556 /* now free hw params for the DAI's */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100557 if (codec_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000558 codec_dai->ops.hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200559
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100560 if (cpu_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000561 cpu_dai->ops.hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200562
563 mutex_unlock(&pcm_mutex);
564 return 0;
565}
566
567static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
568{
569 struct snd_soc_pcm_runtime *rtd = substream->private_data;
570 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100571 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200572 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100573 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
574 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200575 int ret;
576
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100577 if (codec_dai->ops.trigger) {
Mark Browndee89c42008-11-18 22:11:38 +0000578 ret = codec_dai->ops.trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200579 if (ret < 0)
580 return ret;
581 }
582
583 if (platform->pcm_ops->trigger) {
584 ret = platform->pcm_ops->trigger(substream, cmd);
585 if (ret < 0)
586 return ret;
587 }
588
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100589 if (cpu_dai->ops.trigger) {
Mark Browndee89c42008-11-18 22:11:38 +0000590 ret = cpu_dai->ops.trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200591 if (ret < 0)
592 return ret;
593 }
594 return 0;
595}
596
597/* ASoC PCM operations */
598static struct snd_pcm_ops soc_pcm_ops = {
599 .open = soc_pcm_open,
600 .close = soc_codec_close,
601 .hw_params = soc_pcm_hw_params,
602 .hw_free = soc_pcm_hw_free,
603 .prepare = soc_pcm_prepare,
604 .trigger = soc_pcm_trigger,
605};
606
607#ifdef CONFIG_PM
608/* powers down audio subsystem for suspend */
609static int soc_suspend(struct platform_device *pdev, pm_message_t state)
610{
Mark Brown3ff3f642008-05-19 12:32:25 +0200611 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000612 struct snd_soc_card *card = socdev->card;
Mark Brown3ff3f642008-05-19 12:32:25 +0200613 struct snd_soc_platform *platform = socdev->platform;
614 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200615 struct snd_soc_codec *codec = socdev->codec;
616 int i;
617
Andy Green6ed25972008-06-13 16:24:05 +0100618 /* Due to the resume being scheduled into a workqueue we could
619 * suspend before that's finished - wait for it to complete.
620 */
621 snd_power_lock(codec->card);
622 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
623 snd_power_unlock(codec->card);
624
625 /* we're going to block userspace touching us until resume completes */
626 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
627
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200628 /* mute any active DAC's */
Mark Browndee89c42008-11-18 22:11:38 +0000629 for (i = 0; i < card->num_links; i++) {
630 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
631 if (dai->ops.digital_mute && dai->playback.active)
632 dai->ops.digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200633 }
634
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100635 /* suspend all pcms */
Mark Brown87506542008-11-18 20:50:34 +0000636 for (i = 0; i < card->num_links; i++)
637 snd_pcm_suspend_all(card->dai_link[i].pcm);
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100638
Mark Brown87506542008-11-18 20:50:34 +0000639 if (card->suspend_pre)
640 card->suspend_pre(pdev, state);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200641
Mark Brown87506542008-11-18 20:50:34 +0000642 for (i = 0; i < card->num_links; i++) {
643 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e102008-11-24 18:01:05 +0000644 if (cpu_dai->suspend && !cpu_dai->ac97_control)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200645 cpu_dai->suspend(pdev, cpu_dai);
646 if (platform->suspend)
647 platform->suspend(pdev, cpu_dai);
648 }
649
650 /* close any waiting streams and save state */
Mark Brown63084192008-12-02 15:08:03 +0000651 run_delayed_work(&card->delayed_work);
Mark Brown0be98982008-05-19 12:31:28 +0200652 codec->suspend_bias_level = codec->bias_level;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200653
Mark Brown3ff3f642008-05-19 12:32:25 +0200654 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200655 char *stream = codec->dai[i].playback.stream_name;
656 if (stream != NULL)
657 snd_soc_dapm_stream_event(codec, stream,
658 SND_SOC_DAPM_STREAM_SUSPEND);
659 stream = codec->dai[i].capture.stream_name;
660 if (stream != NULL)
661 snd_soc_dapm_stream_event(codec, stream,
662 SND_SOC_DAPM_STREAM_SUSPEND);
663 }
664
665 if (codec_dev->suspend)
666 codec_dev->suspend(pdev, state);
667
Mark Brown87506542008-11-18 20:50:34 +0000668 for (i = 0; i < card->num_links; i++) {
669 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e102008-11-24 18:01:05 +0000670 if (cpu_dai->suspend && cpu_dai->ac97_control)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200671 cpu_dai->suspend(pdev, cpu_dai);
672 }
673
Mark Brown87506542008-11-18 20:50:34 +0000674 if (card->suspend_post)
675 card->suspend_post(pdev, state);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200676
677 return 0;
678}
679
Andy Green6ed25972008-06-13 16:24:05 +0100680/* deferred resume work, so resume can complete before we finished
681 * setting our codec back up, which can be very slow on I2C
682 */
683static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200684{
Mark Brown63084192008-12-02 15:08:03 +0000685 struct snd_soc_card *card = container_of(work,
686 struct snd_soc_card,
687 deferred_resume_work);
688 struct snd_soc_device *socdev = card->socdev;
Mark Brown3ff3f642008-05-19 12:32:25 +0200689 struct snd_soc_platform *platform = socdev->platform;
690 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200691 struct snd_soc_codec *codec = socdev->codec;
Andy Green6ed25972008-06-13 16:24:05 +0100692 struct platform_device *pdev = to_platform_device(socdev->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200693 int i;
694
Andy Green6ed25972008-06-13 16:24:05 +0100695 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
696 * so userspace apps are blocked from touching us
697 */
698
Mark Brownfde22f22008-11-24 18:08:18 +0000699 dev_dbg(socdev->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100700
Mark Brown87506542008-11-18 20:50:34 +0000701 if (card->resume_pre)
702 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200703
Mark Brown87506542008-11-18 20:50:34 +0000704 for (i = 0; i < card->num_links; i++) {
705 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e102008-11-24 18:01:05 +0000706 if (cpu_dai->resume && cpu_dai->ac97_control)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200707 cpu_dai->resume(pdev, cpu_dai);
708 }
709
710 if (codec_dev->resume)
711 codec_dev->resume(pdev);
712
Mark Brown3ff3f642008-05-19 12:32:25 +0200713 for (i = 0; i < codec->num_dai; i++) {
714 char *stream = codec->dai[i].playback.stream_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200715 if (stream != NULL)
716 snd_soc_dapm_stream_event(codec, stream,
717 SND_SOC_DAPM_STREAM_RESUME);
718 stream = codec->dai[i].capture.stream_name;
719 if (stream != NULL)
720 snd_soc_dapm_stream_event(codec, stream,
721 SND_SOC_DAPM_STREAM_RESUME);
722 }
723
Mark Brown3ff3f642008-05-19 12:32:25 +0200724 /* unmute any active DACs */
Mark Browndee89c42008-11-18 22:11:38 +0000725 for (i = 0; i < card->num_links; i++) {
726 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
727 if (dai->ops.digital_mute && dai->playback.active)
728 dai->ops.digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200729 }
730
Mark Brown87506542008-11-18 20:50:34 +0000731 for (i = 0; i < card->num_links; i++) {
732 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e102008-11-24 18:01:05 +0000733 if (cpu_dai->resume && !cpu_dai->ac97_control)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200734 cpu_dai->resume(pdev, cpu_dai);
735 if (platform->resume)
736 platform->resume(pdev, cpu_dai);
737 }
738
Mark Brown87506542008-11-18 20:50:34 +0000739 if (card->resume_post)
740 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200741
Mark Brownfde22f22008-11-24 18:08:18 +0000742 dev_dbg(socdev->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100743
744 /* userspace can access us now we are back as we were before */
745 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
746}
747
748/* powers up audio subsystem after a suspend */
749static int soc_resume(struct platform_device *pdev)
750{
751 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown63084192008-12-02 15:08:03 +0000752 struct snd_soc_card *card = socdev->card;
Andy Green6ed25972008-06-13 16:24:05 +0100753
Mark Brownfde22f22008-11-24 18:08:18 +0000754 dev_dbg(socdev->dev, "scheduling resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100755
Mark Brown63084192008-12-02 15:08:03 +0000756 if (!schedule_work(&card->deferred_resume_work))
Mark Brownfde22f22008-11-24 18:08:18 +0000757 dev_err(socdev->dev, "resume work item may be lost\n");
Andy Green6ed25972008-06-13 16:24:05 +0100758
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200759 return 0;
760}
761
762#else
763#define soc_suspend NULL
764#define soc_resume NULL
765#endif
766
767/* probes a new socdev */
768static int soc_probe(struct platform_device *pdev)
769{
770 int ret = 0, i;
771 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000772 struct snd_soc_card *card = socdev->card;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200773 struct snd_soc_platform *platform = socdev->platform;
774 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
775
Mark Brown63084192008-12-02 15:08:03 +0000776 /* Bodge while we push things out of socdev */
777 card->socdev = socdev;
778
Mark Brown87506542008-11-18 20:50:34 +0000779 if (card->probe) {
780 ret = card->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200781 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200782 return ret;
783 }
784
Mark Brown87506542008-11-18 20:50:34 +0000785 for (i = 0; i < card->num_links; i++) {
786 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200787 if (cpu_dai->probe) {
Mark Brownbdb92872008-06-11 13:47:10 +0100788 ret = cpu_dai->probe(pdev, cpu_dai);
Mark Brown3ff3f642008-05-19 12:32:25 +0200789 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200790 goto cpu_dai_err;
791 }
792 }
793
794 if (codec_dev->probe) {
795 ret = codec_dev->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200796 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200797 goto cpu_dai_err;
798 }
799
800 if (platform->probe) {
801 ret = platform->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200802 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200803 goto platform_err;
804 }
805
806 /* DAPM stream work */
Mark Brown63084192008-12-02 15:08:03 +0000807 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
Randy Dunlap1301a962008-06-17 19:19:34 +0100808#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +0100809 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +0000810 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +0100811#endif
Andy Green6ed25972008-06-13 16:24:05 +0100812
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200813 return 0;
814
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200815platform_err:
816 if (codec_dev->remove)
817 codec_dev->remove(pdev);
818
819cpu_dai_err:
Liam Girdwood18b9b3d2007-01-30 17:18:45 +0100820 for (i--; i >= 0; i--) {
Mark Brown87506542008-11-18 20:50:34 +0000821 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200822 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +0100823 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200824 }
825
Mark Brown87506542008-11-18 20:50:34 +0000826 if (card->remove)
827 card->remove(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200828
829 return ret;
830}
831
832/* removes a socdev */
833static int soc_remove(struct platform_device *pdev)
834{
835 int i;
836 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000837 struct snd_soc_card *card = socdev->card;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200838 struct snd_soc_platform *platform = socdev->platform;
839 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
840
Mark Brown63084192008-12-02 15:08:03 +0000841 run_delayed_work(&card->delayed_work);
Liam Girdwood965ac422007-01-31 14:14:57 +0100842
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200843 if (platform->remove)
844 platform->remove(pdev);
845
846 if (codec_dev->remove)
847 codec_dev->remove(pdev);
848
Mark Brown87506542008-11-18 20:50:34 +0000849 for (i = 0; i < card->num_links; i++) {
850 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200851 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +0100852 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200853 }
854
Mark Brown87506542008-11-18 20:50:34 +0000855 if (card->remove)
856 card->remove(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200857
858 return 0;
859}
860
861/* ASoC platform driver */
862static struct platform_driver soc_driver = {
863 .driver = {
864 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +0200865 .owner = THIS_MODULE,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200866 },
867 .probe = soc_probe,
868 .remove = soc_remove,
869 .suspend = soc_suspend,
870 .resume = soc_resume,
871};
872
873/* create a new pcm */
874static int soc_new_pcm(struct snd_soc_device *socdev,
875 struct snd_soc_dai_link *dai_link, int num)
876{
877 struct snd_soc_codec *codec = socdev->codec;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100878 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
879 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200880 struct snd_soc_pcm_runtime *rtd;
881 struct snd_pcm *pcm;
882 char new_name[64];
883 int ret = 0, playback = 0, capture = 0;
884
885 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
886 if (rtd == NULL)
887 return -ENOMEM;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100888
889 rtd->dai = dai_link;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200890 rtd->socdev = socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100891 codec_dai->codec = socdev->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200892
893 /* check client and interface hw capabilities */
Mark Brown3ba9e102008-11-24 18:01:05 +0000894 sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name,
895 num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200896
897 if (codec_dai->playback.channels_min)
898 playback = 1;
899 if (codec_dai->capture.channels_min)
900 capture = 1;
901
902 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
903 capture, &pcm);
904 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200905 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
906 codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200907 kfree(rtd);
908 return ret;
909 }
910
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100911 dai_link->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200912 pcm->private_data = rtd;
913 soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
914 soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
915 soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
916 soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
917 soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
918 soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
919 soc_pcm_ops.page = socdev->platform->pcm_ops->page;
920
921 if (playback)
922 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
923
924 if (capture)
925 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
926
927 ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
928 if (ret < 0) {
929 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
930 kfree(rtd);
931 return ret;
932 }
933
934 pcm->private_free = socdev->platform->pcm_free;
935 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
936 cpu_dai->name);
937 return ret;
938}
939
940/* codec register dump */
Troy Kisky12ef1932008-10-13 17:42:14 -0700941static ssize_t soc_codec_reg_show(struct snd_soc_device *devdata, char *buf)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200942{
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200943 struct snd_soc_codec *codec = devdata->codec;
944 int i, step = 1, count = 0;
945
946 if (!codec->reg_cache_size)
947 return 0;
948
949 if (codec->reg_cache_step)
950 step = codec->reg_cache_step;
951
952 count += sprintf(buf, "%s registers\n", codec->name);
Mark Brown58cd33c2008-07-29 11:42:25 +0100953 for (i = 0; i < codec->reg_cache_size; i += step) {
954 count += sprintf(buf + count, "%2x: ", i);
955 if (count >= PAGE_SIZE - 1)
956 break;
957
958 if (codec->display_register)
959 count += codec->display_register(codec, buf + count,
960 PAGE_SIZE - count, i);
961 else
962 count += snprintf(buf + count, PAGE_SIZE - count,
963 "%4x", codec->read(codec, i));
964
965 if (count >= PAGE_SIZE - 1)
966 break;
967
968 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
969 if (count >= PAGE_SIZE - 1)
970 break;
971 }
972
973 /* Truncate count; min() would cause a warning */
974 if (count >= PAGE_SIZE)
975 count = PAGE_SIZE - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200976
977 return count;
978}
Troy Kisky12ef1932008-10-13 17:42:14 -0700979static ssize_t codec_reg_show(struct device *dev,
980 struct device_attribute *attr, char *buf)
981{
982 struct snd_soc_device *devdata = dev_get_drvdata(dev);
983 return soc_codec_reg_show(devdata, buf);
984}
985
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200986static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
987
Troy Kisky12ef1932008-10-13 17:42:14 -0700988#ifdef CONFIG_DEBUG_FS
989static int codec_reg_open_file(struct inode *inode, struct file *file)
990{
991 file->private_data = inode->i_private;
992 return 0;
993}
994
995static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
996 size_t count, loff_t *ppos)
997{
998 ssize_t ret;
999 struct snd_soc_device *devdata = file->private_data;
1000 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1001 if (!buf)
1002 return -ENOMEM;
1003 ret = soc_codec_reg_show(devdata, buf);
1004 if (ret >= 0)
1005 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1006 kfree(buf);
1007 return ret;
1008}
1009
1010static ssize_t codec_reg_write_file(struct file *file,
1011 const char __user *user_buf, size_t count, loff_t *ppos)
1012{
1013 char buf[32];
1014 int buf_size;
1015 char *start = buf;
1016 unsigned long reg, value;
1017 int step = 1;
1018 struct snd_soc_device *devdata = file->private_data;
1019 struct snd_soc_codec *codec = devdata->codec;
1020
1021 buf_size = min(count, (sizeof(buf)-1));
1022 if (copy_from_user(buf, user_buf, buf_size))
1023 return -EFAULT;
1024 buf[buf_size] = 0;
1025
1026 if (codec->reg_cache_step)
1027 step = codec->reg_cache_step;
1028
1029 while (*start == ' ')
1030 start++;
1031 reg = simple_strtoul(start, &start, 16);
1032 if ((reg >= codec->reg_cache_size) || (reg % step))
1033 return -EINVAL;
1034 while (*start == ' ')
1035 start++;
1036 if (strict_strtoul(start, 16, &value))
1037 return -EINVAL;
1038 codec->write(codec, reg, value);
1039 return buf_size;
1040}
1041
1042static const struct file_operations codec_reg_fops = {
1043 .open = codec_reg_open_file,
1044 .read = codec_reg_read_file,
1045 .write = codec_reg_write_file,
1046};
1047
1048static void soc_init_debugfs(struct snd_soc_device *socdev)
1049{
1050 struct dentry *root, *file;
1051 struct snd_soc_codec *codec = socdev->codec;
1052 root = debugfs_create_dir(dev_name(socdev->dev), NULL);
1053 if (IS_ERR(root) || !root)
1054 goto exit1;
1055
1056 file = debugfs_create_file("codec_reg", 0644,
1057 root, socdev, &codec_reg_fops);
1058 if (!file)
1059 goto exit2;
1060
1061 file = debugfs_create_u32("dapm_pop_time", 0744,
1062 root, &codec->pop_time);
1063 if (!file)
1064 goto exit2;
1065 socdev->debugfs_root = root;
1066 return;
1067exit2:
1068 debugfs_remove_recursive(root);
1069exit1:
1070 dev_err(socdev->dev, "debugfs is not available\n");
1071}
1072
1073static void soc_cleanup_debugfs(struct snd_soc_device *socdev)
1074{
1075 debugfs_remove_recursive(socdev->debugfs_root);
1076 socdev->debugfs_root = NULL;
1077}
1078
1079#else
1080
1081static inline void soc_init_debugfs(struct snd_soc_device *socdev)
1082{
1083}
1084
1085static inline void soc_cleanup_debugfs(struct snd_soc_device *socdev)
1086{
1087}
1088#endif
1089
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001090/**
1091 * snd_soc_new_ac97_codec - initailise AC97 device
1092 * @codec: audio codec
1093 * @ops: AC97 bus operations
1094 * @num: AC97 codec number
1095 *
1096 * Initialises AC97 codec resources for use by ad-hoc devices only.
1097 */
1098int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1099 struct snd_ac97_bus_ops *ops, int num)
1100{
1101 mutex_lock(&codec->mutex);
1102
1103 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1104 if (codec->ac97 == NULL) {
1105 mutex_unlock(&codec->mutex);
1106 return -ENOMEM;
1107 }
1108
1109 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1110 if (codec->ac97->bus == NULL) {
1111 kfree(codec->ac97);
1112 codec->ac97 = NULL;
1113 mutex_unlock(&codec->mutex);
1114 return -ENOMEM;
1115 }
1116
1117 codec->ac97->bus->ops = ops;
1118 codec->ac97->num = num;
1119 mutex_unlock(&codec->mutex);
1120 return 0;
1121}
1122EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1123
1124/**
1125 * snd_soc_free_ac97_codec - free AC97 codec device
1126 * @codec: audio codec
1127 *
1128 * Frees AC97 codec device resources.
1129 */
1130void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1131{
1132 mutex_lock(&codec->mutex);
1133 kfree(codec->ac97->bus);
1134 kfree(codec->ac97);
1135 codec->ac97 = NULL;
1136 mutex_unlock(&codec->mutex);
1137}
1138EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1139
1140/**
1141 * snd_soc_update_bits - update codec register bits
1142 * @codec: audio codec
1143 * @reg: codec register
1144 * @mask: register mask
1145 * @value: new value
1146 *
1147 * Writes new register value.
1148 *
1149 * Returns 1 for change else 0.
1150 */
1151int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1152 unsigned short mask, unsigned short value)
1153{
1154 int change;
1155 unsigned short old, new;
1156
1157 mutex_lock(&io_mutex);
1158 old = snd_soc_read(codec, reg);
1159 new = (old & ~mask) | value;
1160 change = old != new;
1161 if (change)
1162 snd_soc_write(codec, reg, new);
1163
1164 mutex_unlock(&io_mutex);
1165 return change;
1166}
1167EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1168
1169/**
1170 * snd_soc_test_bits - test register for change
1171 * @codec: audio codec
1172 * @reg: codec register
1173 * @mask: register mask
1174 * @value: new value
1175 *
1176 * Tests a register with a new value and checks if the new value is
1177 * different from the old value.
1178 *
1179 * Returns 1 for change else 0.
1180 */
1181int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1182 unsigned short mask, unsigned short value)
1183{
1184 int change;
1185 unsigned short old, new;
1186
1187 mutex_lock(&io_mutex);
1188 old = snd_soc_read(codec, reg);
1189 new = (old & ~mask) | value;
1190 change = old != new;
1191 mutex_unlock(&io_mutex);
1192
1193 return change;
1194}
1195EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1196
1197/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001198 * snd_soc_new_pcms - create new sound card and pcms
1199 * @socdev: the SoC audio device
1200 *
1201 * Create a new sound card based upon the codec and interface pcms.
1202 *
1203 * Returns 0 for success, else error.
1204 */
Liam Girdwoodbc7320c2007-02-01 12:26:07 +01001205int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001206{
1207 struct snd_soc_codec *codec = socdev->codec;
Mark Brown87506542008-11-18 20:50:34 +00001208 struct snd_soc_card *card = socdev->card;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001209 int ret = 0, i;
1210
1211 mutex_lock(&codec->mutex);
1212
1213 /* register a sound card */
1214 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1215 if (!codec->card) {
1216 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1217 codec->name);
1218 mutex_unlock(&codec->mutex);
1219 return -ENODEV;
1220 }
1221
1222 codec->card->dev = socdev->dev;
1223 codec->card->private_data = codec;
1224 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1225
1226 /* create the pcms */
Mark Brown87506542008-11-18 20:50:34 +00001227 for (i = 0; i < card->num_links; i++) {
1228 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001229 if (ret < 0) {
1230 printk(KERN_ERR "asoc: can't create pcm %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001231 card->dai_link[i].stream_name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001232 mutex_unlock(&codec->mutex);
1233 return ret;
1234 }
1235 }
1236
1237 mutex_unlock(&codec->mutex);
1238 return ret;
1239}
1240EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1241
1242/**
Mark Brown968a6022008-11-28 11:49:07 +00001243 * snd_soc_init_card - register sound card
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001244 * @socdev: the SoC audio device
1245 *
1246 * Register a SoC sound card. Also registers an AC97 device if the
1247 * codec is AC97 for ad hoc devices.
1248 *
1249 * Returns 0 for success, else error.
1250 */
Mark Brown968a6022008-11-28 11:49:07 +00001251int snd_soc_init_card(struct snd_soc_device *socdev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001252{
1253 struct snd_soc_codec *codec = socdev->codec;
Mark Brown87506542008-11-18 20:50:34 +00001254 struct snd_soc_card *card = socdev->card;
Liam Girdwood12e74f72006-10-16 21:19:48 +02001255 int ret = 0, i, ac97 = 0, err = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001256
Mark Brown87506542008-11-18 20:50:34 +00001257 for (i = 0; i < card->num_links; i++) {
1258 if (card->dai_link[i].init) {
1259 err = card->dai_link[i].init(codec);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001260 if (err < 0) {
1261 printk(KERN_ERR "asoc: failed to init %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001262 card->dai_link[i].stream_name);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001263 continue;
1264 }
1265 }
Mark Brown3ba9e102008-11-24 18:01:05 +00001266 if (card->dai_link[i].codec_dai->ac97_control)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001267 ac97 = 1;
1268 }
1269 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
Mark Brown87506542008-11-18 20:50:34 +00001270 "%s", card->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001271 snprintf(codec->card->longname, sizeof(codec->card->longname),
Mark Brown87506542008-11-18 20:50:34 +00001272 "%s (%s)", card->name, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001273
1274 ret = snd_card_register(codec->card);
1275 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001276 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001277 codec->name);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001278 goto out;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001279 }
1280
Mark Brown08c8efe2008-01-21 14:33:37 +01001281 mutex_lock(&codec->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001282#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood12e74f72006-10-16 21:19:48 +02001283 if (ac97) {
1284 ret = soc_ac97_dev_register(codec);
1285 if (ret < 0) {
1286 printk(KERN_ERR "asoc: AC97 device register failed\n");
1287 snd_card_free(codec->card);
Mark Brown08c8efe2008-01-21 14:33:37 +01001288 mutex_unlock(&codec->mutex);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001289 goto out;
1290 }
1291 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001292#endif
1293
Liam Girdwood12e74f72006-10-16 21:19:48 +02001294 err = snd_soc_dapm_sys_add(socdev->dev);
1295 if (err < 0)
1296 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1297
1298 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1299 if (err < 0)
Mark Brown3ff3f642008-05-19 12:32:25 +02001300 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
Mark Brown08c8efe2008-01-21 14:33:37 +01001301
Troy Kisky12ef1932008-10-13 17:42:14 -07001302 soc_init_debugfs(socdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001303 mutex_unlock(&codec->mutex);
Mark Brown08c8efe2008-01-21 14:33:37 +01001304
1305out:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001306 return ret;
1307}
Mark Brown968a6022008-11-28 11:49:07 +00001308EXPORT_SYMBOL_GPL(snd_soc_init_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001309
1310/**
1311 * snd_soc_free_pcms - free sound card and pcms
1312 * @socdev: the SoC audio device
1313 *
1314 * Frees sound card and pcms associated with the socdev.
1315 * Also unregister the codec if it is an AC97 device.
1316 */
1317void snd_soc_free_pcms(struct snd_soc_device *socdev)
1318{
1319 struct snd_soc_codec *codec = socdev->codec;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001320#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001321 struct snd_soc_dai *codec_dai;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001322 int i;
1323#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001324
1325 mutex_lock(&codec->mutex);
Troy Kisky12ef1932008-10-13 17:42:14 -07001326 soc_cleanup_debugfs(socdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001327#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown3ff3f642008-05-19 12:32:25 +02001328 for (i = 0; i < codec->num_dai; i++) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001329 codec_dai = &codec->dai[i];
Mark Brown3ba9e102008-11-24 18:01:05 +00001330 if (codec_dai->ac97_control && codec->ac97) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001331 soc_ac97_dev_unregister(codec);
1332 goto free_card;
1333 }
1334 }
1335free_card:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001336#endif
1337
1338 if (codec->card)
1339 snd_card_free(codec->card);
1340 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1341 mutex_unlock(&codec->mutex);
1342}
1343EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1344
1345/**
1346 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1347 * @substream: the pcm substream
1348 * @hw: the hardware parameters
1349 *
1350 * Sets the substream runtime hardware parameters.
1351 */
1352int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1353 const struct snd_pcm_hardware *hw)
1354{
1355 struct snd_pcm_runtime *runtime = substream->runtime;
1356 runtime->hw.info = hw->info;
1357 runtime->hw.formats = hw->formats;
1358 runtime->hw.period_bytes_min = hw->period_bytes_min;
1359 runtime->hw.period_bytes_max = hw->period_bytes_max;
1360 runtime->hw.periods_min = hw->periods_min;
1361 runtime->hw.periods_max = hw->periods_max;
1362 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1363 runtime->hw.fifo_size = hw->fifo_size;
1364 return 0;
1365}
1366EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1367
1368/**
1369 * snd_soc_cnew - create new control
1370 * @_template: control template
1371 * @data: control private data
1372 * @lnng_name: control long name
1373 *
1374 * Create a new mixer control from a template control.
1375 *
1376 * Returns 0 for success, else error.
1377 */
1378struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1379 void *data, char *long_name)
1380{
1381 struct snd_kcontrol_new template;
1382
1383 memcpy(&template, _template, sizeof(template));
1384 if (long_name)
1385 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001386 template.index = 0;
1387
1388 return snd_ctl_new1(&template, data);
1389}
1390EXPORT_SYMBOL_GPL(snd_soc_cnew);
1391
1392/**
1393 * snd_soc_info_enum_double - enumerated double mixer info callback
1394 * @kcontrol: mixer control
1395 * @uinfo: control element information
1396 *
1397 * Callback to provide information about a double enumerated
1398 * mixer control.
1399 *
1400 * Returns 0 for success.
1401 */
1402int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1403 struct snd_ctl_elem_info *uinfo)
1404{
1405 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1406
1407 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1408 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001409 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001410
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001411 if (uinfo->value.enumerated.item > e->max - 1)
1412 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001413 strcpy(uinfo->value.enumerated.name,
1414 e->texts[uinfo->value.enumerated.item]);
1415 return 0;
1416}
1417EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1418
1419/**
1420 * snd_soc_get_enum_double - enumerated double mixer get callback
1421 * @kcontrol: mixer control
1422 * @uinfo: control element information
1423 *
1424 * Callback to get the value of a double enumerated mixer.
1425 *
1426 * Returns 0 for success.
1427 */
1428int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1429 struct snd_ctl_elem_value *ucontrol)
1430{
1431 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1432 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1433 unsigned short val, bitmask;
1434
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001435 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001436 ;
1437 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001438 ucontrol->value.enumerated.item[0]
1439 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001440 if (e->shift_l != e->shift_r)
1441 ucontrol->value.enumerated.item[1] =
1442 (val >> e->shift_r) & (bitmask - 1);
1443
1444 return 0;
1445}
1446EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1447
1448/**
1449 * snd_soc_put_enum_double - enumerated double mixer put callback
1450 * @kcontrol: mixer control
1451 * @uinfo: control element information
1452 *
1453 * Callback to set the value of a double enumerated mixer.
1454 *
1455 * Returns 0 for success.
1456 */
1457int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1458 struct snd_ctl_elem_value *ucontrol)
1459{
1460 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1461 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1462 unsigned short val;
1463 unsigned short mask, bitmask;
1464
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001465 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001466 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001467 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001468 return -EINVAL;
1469 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1470 mask = (bitmask - 1) << e->shift_l;
1471 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001472 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001473 return -EINVAL;
1474 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1475 mask |= (bitmask - 1) << e->shift_r;
1476 }
1477
1478 return snd_soc_update_bits(codec, e->reg, mask, val);
1479}
1480EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1481
1482/**
1483 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1484 * @kcontrol: mixer control
1485 * @uinfo: control element information
1486 *
1487 * Callback to provide information about an external enumerated
1488 * single mixer.
1489 *
1490 * Returns 0 for success.
1491 */
1492int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1493 struct snd_ctl_elem_info *uinfo)
1494{
1495 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1496
1497 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1498 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001499 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001500
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001501 if (uinfo->value.enumerated.item > e->max - 1)
1502 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001503 strcpy(uinfo->value.enumerated.name,
1504 e->texts[uinfo->value.enumerated.item]);
1505 return 0;
1506}
1507EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1508
1509/**
1510 * snd_soc_info_volsw_ext - external single mixer info callback
1511 * @kcontrol: mixer control
1512 * @uinfo: control element information
1513 *
1514 * Callback to provide information about a single external mixer control.
1515 *
1516 * Returns 0 for success.
1517 */
1518int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1519 struct snd_ctl_elem_info *uinfo)
1520{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001521 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001522
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001523 if (max == 1)
1524 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1525 else
1526 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1527
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001528 uinfo->count = 1;
1529 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001530 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001531 return 0;
1532}
1533EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1534
1535/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001536 * snd_soc_info_volsw - single mixer info callback
1537 * @kcontrol: mixer control
1538 * @uinfo: control element information
1539 *
1540 * Callback to provide information about a single mixer control.
1541 *
1542 * Returns 0 for success.
1543 */
1544int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1545 struct snd_ctl_elem_info *uinfo)
1546{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001547 struct soc_mixer_control *mc =
1548 (struct soc_mixer_control *)kcontrol->private_value;
1549 int max = mc->max;
Mark Brown762b8df2008-10-30 12:37:08 +00001550 unsigned int shift = mc->shift;
Jon Smirl815ecf82008-07-29 10:22:24 -04001551 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001552
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001553 if (max == 1)
1554 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1555 else
1556 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1557
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001558 uinfo->count = shift == rshift ? 1 : 2;
1559 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001560 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001561 return 0;
1562}
1563EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1564
1565/**
1566 * snd_soc_get_volsw - single mixer get callback
1567 * @kcontrol: mixer control
1568 * @uinfo: control element information
1569 *
1570 * Callback to get the value of a single mixer control.
1571 *
1572 * Returns 0 for success.
1573 */
1574int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1575 struct snd_ctl_elem_value *ucontrol)
1576{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001577 struct soc_mixer_control *mc =
1578 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001579 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001580 unsigned int reg = mc->reg;
1581 unsigned int shift = mc->shift;
1582 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001583 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001584 unsigned int mask = (1 << fls(max)) - 1;
1585 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001586
1587 ucontrol->value.integer.value[0] =
1588 (snd_soc_read(codec, reg) >> shift) & mask;
1589 if (shift != rshift)
1590 ucontrol->value.integer.value[1] =
1591 (snd_soc_read(codec, reg) >> rshift) & mask;
1592 if (invert) {
1593 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001594 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001595 if (shift != rshift)
1596 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001597 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001598 }
1599
1600 return 0;
1601}
1602EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1603
1604/**
1605 * snd_soc_put_volsw - single mixer put callback
1606 * @kcontrol: mixer control
1607 * @uinfo: control element information
1608 *
1609 * Callback to set the value of a single mixer control.
1610 *
1611 * Returns 0 for success.
1612 */
1613int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1614 struct snd_ctl_elem_value *ucontrol)
1615{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001616 struct soc_mixer_control *mc =
1617 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001618 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001619 unsigned int reg = mc->reg;
1620 unsigned int shift = mc->shift;
1621 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001622 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001623 unsigned int mask = (1 << fls(max)) - 1;
1624 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001625 unsigned short val, val2, val_mask;
1626
1627 val = (ucontrol->value.integer.value[0] & mask);
1628 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001629 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001630 val_mask = mask << shift;
1631 val = val << shift;
1632 if (shift != rshift) {
1633 val2 = (ucontrol->value.integer.value[1] & mask);
1634 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001635 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001636 val_mask |= mask << rshift;
1637 val |= val2 << rshift;
1638 }
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001639 return snd_soc_update_bits(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001640}
1641EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1642
1643/**
1644 * snd_soc_info_volsw_2r - double mixer info callback
1645 * @kcontrol: mixer control
1646 * @uinfo: control element information
1647 *
1648 * Callback to provide information about a double mixer control that
1649 * spans 2 codec registers.
1650 *
1651 * Returns 0 for success.
1652 */
1653int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1654 struct snd_ctl_elem_info *uinfo)
1655{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001656 struct soc_mixer_control *mc =
1657 (struct soc_mixer_control *)kcontrol->private_value;
1658 int max = mc->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001659
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001660 if (max == 1)
1661 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1662 else
1663 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1664
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001665 uinfo->count = 2;
1666 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001667 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001668 return 0;
1669}
1670EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1671
1672/**
1673 * snd_soc_get_volsw_2r - double mixer get callback
1674 * @kcontrol: mixer control
1675 * @uinfo: control element information
1676 *
1677 * Callback to get the value of a double mixer control that spans 2 registers.
1678 *
1679 * Returns 0 for success.
1680 */
1681int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1682 struct snd_ctl_elem_value *ucontrol)
1683{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001684 struct soc_mixer_control *mc =
1685 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001686 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001687 unsigned int reg = mc->reg;
1688 unsigned int reg2 = mc->rreg;
1689 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001690 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001691 unsigned int mask = (1<<fls(max))-1;
1692 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001693
1694 ucontrol->value.integer.value[0] =
1695 (snd_soc_read(codec, reg) >> shift) & mask;
1696 ucontrol->value.integer.value[1] =
1697 (snd_soc_read(codec, reg2) >> shift) & mask;
1698 if (invert) {
1699 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001700 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001701 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001702 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001703 }
1704
1705 return 0;
1706}
1707EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1708
1709/**
1710 * snd_soc_put_volsw_2r - double mixer set callback
1711 * @kcontrol: mixer control
1712 * @uinfo: control element information
1713 *
1714 * Callback to set the value of a double mixer control that spans 2 registers.
1715 *
1716 * Returns 0 for success.
1717 */
1718int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1719 struct snd_ctl_elem_value *ucontrol)
1720{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001721 struct soc_mixer_control *mc =
1722 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001723 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001724 unsigned int reg = mc->reg;
1725 unsigned int reg2 = mc->rreg;
1726 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001727 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001728 unsigned int mask = (1 << fls(max)) - 1;
1729 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001730 int err;
1731 unsigned short val, val2, val_mask;
1732
1733 val_mask = mask << shift;
1734 val = (ucontrol->value.integer.value[0] & mask);
1735 val2 = (ucontrol->value.integer.value[1] & mask);
1736
1737 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001738 val = max - val;
1739 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001740 }
1741
1742 val = val << shift;
1743 val2 = val2 << shift;
1744
Mark Brown3ff3f642008-05-19 12:32:25 +02001745 err = snd_soc_update_bits(codec, reg, val_mask, val);
1746 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001747 return err;
1748
1749 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1750 return err;
1751}
1752EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1753
Mark Browne13ac2e2008-05-28 17:58:05 +01001754/**
1755 * snd_soc_info_volsw_s8 - signed mixer info callback
1756 * @kcontrol: mixer control
1757 * @uinfo: control element information
1758 *
1759 * Callback to provide information about a signed mixer control.
1760 *
1761 * Returns 0 for success.
1762 */
1763int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1764 struct snd_ctl_elem_info *uinfo)
1765{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001766 struct soc_mixer_control *mc =
1767 (struct soc_mixer_control *)kcontrol->private_value;
1768 int max = mc->max;
1769 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01001770
1771 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1772 uinfo->count = 2;
1773 uinfo->value.integer.min = 0;
1774 uinfo->value.integer.max = max-min;
1775 return 0;
1776}
1777EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1778
1779/**
1780 * snd_soc_get_volsw_s8 - signed mixer get callback
1781 * @kcontrol: mixer control
1782 * @uinfo: control element information
1783 *
1784 * Callback to get the value of a signed mixer control.
1785 *
1786 * Returns 0 for success.
1787 */
1788int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1789 struct snd_ctl_elem_value *ucontrol)
1790{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001791 struct soc_mixer_control *mc =
1792 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01001793 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001794 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001795 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01001796 int val = snd_soc_read(codec, reg);
1797
1798 ucontrol->value.integer.value[0] =
1799 ((signed char)(val & 0xff))-min;
1800 ucontrol->value.integer.value[1] =
1801 ((signed char)((val >> 8) & 0xff))-min;
1802 return 0;
1803}
1804EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
1805
1806/**
1807 * snd_soc_put_volsw_sgn - signed mixer put callback
1808 * @kcontrol: mixer control
1809 * @uinfo: control element information
1810 *
1811 * Callback to set the value of a signed mixer control.
1812 *
1813 * Returns 0 for success.
1814 */
1815int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
1816 struct snd_ctl_elem_value *ucontrol)
1817{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001818 struct soc_mixer_control *mc =
1819 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01001820 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001821 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001822 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01001823 unsigned short val;
1824
1825 val = (ucontrol->value.integer.value[0]+min) & 0xff;
1826 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
1827
1828 return snd_soc_update_bits(codec, reg, 0xffff, val);
1829}
1830EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
1831
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001832/**
1833 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
1834 * @dai: DAI
1835 * @clk_id: DAI specific clock ID
1836 * @freq: new clock frequency in Hz
1837 * @dir: new clock direction - input/output.
1838 *
1839 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
1840 */
1841int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1842 unsigned int freq, int dir)
1843{
Mark Browndee89c42008-11-18 22:11:38 +00001844 if (dai->ops.set_sysclk)
1845 return dai->ops.set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001846 else
1847 return -EINVAL;
1848}
1849EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
1850
1851/**
1852 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
1853 * @dai: DAI
1854 * @clk_id: DAI specific clock divider ID
1855 * @div: new clock divisor.
1856 *
1857 * Configures the clock dividers. This is used to derive the best DAI bit and
1858 * frame clocks from the system or master clock. It's best to set the DAI bit
1859 * and frame clocks as low as possible to save system power.
1860 */
1861int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
1862 int div_id, int div)
1863{
Mark Browndee89c42008-11-18 22:11:38 +00001864 if (dai->ops.set_clkdiv)
1865 return dai->ops.set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001866 else
1867 return -EINVAL;
1868}
1869EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
1870
1871/**
1872 * snd_soc_dai_set_pll - configure DAI PLL.
1873 * @dai: DAI
1874 * @pll_id: DAI specific PLL ID
1875 * @freq_in: PLL input clock frequency in Hz
1876 * @freq_out: requested PLL output clock frequency in Hz
1877 *
1878 * Configures and enables PLL to generate output clock based on input clock.
1879 */
1880int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
1881 int pll_id, unsigned int freq_in, unsigned int freq_out)
1882{
Mark Browndee89c42008-11-18 22:11:38 +00001883 if (dai->ops.set_pll)
1884 return dai->ops.set_pll(dai, pll_id, freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001885 else
1886 return -EINVAL;
1887}
1888EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
1889
1890/**
1891 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
1892 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001893 * @fmt: SND_SOC_DAIFMT_ format value.
1894 *
1895 * Configures the DAI hardware format and clocking.
1896 */
1897int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1898{
Mark Browndee89c42008-11-18 22:11:38 +00001899 if (dai->ops.set_fmt)
1900 return dai->ops.set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001901 else
1902 return -EINVAL;
1903}
1904EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
1905
1906/**
1907 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
1908 * @dai: DAI
1909 * @mask: DAI specific mask representing used slots.
1910 * @slots: Number of slots in use.
1911 *
1912 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
1913 * specific.
1914 */
1915int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
1916 unsigned int mask, int slots)
1917{
Mark Browndee89c42008-11-18 22:11:38 +00001918 if (dai->ops.set_sysclk)
1919 return dai->ops.set_tdm_slot(dai, mask, slots);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001920 else
1921 return -EINVAL;
1922}
1923EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
1924
1925/**
1926 * snd_soc_dai_set_tristate - configure DAI system or master clock.
1927 * @dai: DAI
1928 * @tristate: tristate enable
1929 *
1930 * Tristates the DAI so that others can use it.
1931 */
1932int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
1933{
Mark Browndee89c42008-11-18 22:11:38 +00001934 if (dai->ops.set_sysclk)
1935 return dai->ops.set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001936 else
1937 return -EINVAL;
1938}
1939EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
1940
1941/**
1942 * snd_soc_dai_digital_mute - configure DAI system or master clock.
1943 * @dai: DAI
1944 * @mute: mute enable
1945 *
1946 * Mutes the DAI DAC.
1947 */
1948int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
1949{
Mark Browndee89c42008-11-18 22:11:38 +00001950 if (dai->ops.digital_mute)
1951 return dai->ops.digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001952 else
1953 return -EINVAL;
1954}
1955EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
1956
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001957static int __devinit snd_soc_init(void)
1958{
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001959 return platform_driver_register(&soc_driver);
1960}
1961
Mark Brown7d8c16a2008-11-30 22:11:24 +00001962static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001963{
Mark Brown3ff3f642008-05-19 12:32:25 +02001964 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001965}
1966
1967module_init(snd_soc_init);
1968module_exit(snd_soc_exit);
1969
1970/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01001971MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001972MODULE_DESCRIPTION("ALSA SoC Core");
1973MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02001974MODULE_ALIAS("platform:soc-audio");