]> bbs.cooldavid.org Git - net-next-2.6.git/blame - sound/soc/soc-core.c
ALSA: ca0106 - Add entry for another MSI K8N Diamond MB
[net-next-2.6.git] / sound / soc / soc-core.c
CommitLineData
db2a4165
FM
1/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
0664d888
LG
5 * Copyright 2005 Openedhand Ltd.
6 *
db2a4165
FM
7 * Author: Liam Girdwood
8 * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
0664d888
LG
9 * with code, comments and ideas from :-
10 * Richard Purdie <richard@openedhand.com>
db2a4165
FM
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
db2a4165
FM
17 * TODO:
18 * o Add hw rules to enforce rates, etc.
19 * o More testing with other codecs/machines.
20 * o Add more codecs and platforms to ensure good API coverage.
21 * o Support TDM on PCM and I2S
22 */
23
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/init.h>
27#include <linux/delay.h>
28#include <linux/pm.h>
29#include <linux/bitops.h>
30#include <linux/platform_device.h>
db2a4165
FM
31#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
38/* debug */
39#define SOC_DEBUG 0
40#if SOC_DEBUG
41#define dbg(format, arg...) printk(format, ## arg)
42#else
43#define dbg(format, arg...)
44#endif
a71a468a 45
db2a4165
FM
46static DEFINE_MUTEX(pcm_mutex);
47static DEFINE_MUTEX(io_mutex);
db2a4165
FM
48static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
49
db2a4165
FM
50/*
51 * This is a timeout to do a DAPM powerdown after a stream is closed().
52 * It can be used to eliminate pops between different playback streams, e.g.
53 * between two audio tracks.
54 */
55static int pmdown_time = 5000;
56module_param(pmdown_time, int, 0);
57MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
58
965ac42c
LG
59/*
60 * This function forces any delayed work to be queued and run.
61 */
62static int run_delayed_work(struct delayed_work *dwork)
63{
64 int ret;
65
66 /* cancel any work waiting to be queued. */
67 ret = cancel_delayed_work(dwork);
68
69 /* if there was any work waiting then we run it now and
70 * wait for it's completion */
71 if (ret) {
72 schedule_delayed_work(dwork, 0);
73 flush_scheduled_work();
74 }
75 return ret;
76}
77
db2a4165
FM
78#ifdef CONFIG_SND_SOC_AC97_BUS
79/* unregister ac97 codec */
80static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
81{
82 if (codec->ac97->dev.bus)
83 device_unregister(&codec->ac97->dev);
84 return 0;
85}
86
87/* stop no dev release warning */
88static void soc_ac97_device_release(struct device *dev){}
89
90/* register ac97 codec to bus */
91static int soc_ac97_dev_register(struct snd_soc_codec *codec)
92{
93 int err;
94
95 codec->ac97->dev.bus = &ac97_bus_type;
96 codec->ac97->dev.parent = NULL;
97 codec->ac97->dev.release = soc_ac97_device_release;
98
99 snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
100 codec->card->number, 0, codec->name);
101 err = device_register(&codec->ac97->dev);
102 if (err < 0) {
103 snd_printk(KERN_ERR "Can't register ac97 bus\n");
104 codec->ac97->dev.bus = NULL;
105 return err;
106 }
107 return 0;
108}
109#endif
110
3ff3f64b 111static inline const char *get_dai_name(int type)
db2a4165 112{
3ff3f64b 113 switch (type) {
a68660e0 114 case SND_SOC_DAI_AC97_BUS:
db2a4165
FM
115 case SND_SOC_DAI_AC97:
116 return "AC97";
117 case SND_SOC_DAI_I2S:
118 return "I2S";
119 case SND_SOC_DAI_PCM:
120 return "PCM";
121 }
122 return NULL;
123}
124
db2a4165
FM
125/*
126 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
127 * then initialized and any private data can be allocated. This also calls
128 * startup for the cpu DAI, platform, machine and codec DAI.
129 */
130static int soc_pcm_open(struct snd_pcm_substream *substream)
131{
132 struct snd_soc_pcm_runtime *rtd = substream->private_data;
133 struct snd_soc_device *socdev = rtd->socdev;
134 struct snd_pcm_runtime *runtime = substream->runtime;
cb666e5b 135 struct snd_soc_dai_link *machine = rtd->dai;
db2a4165 136 struct snd_soc_platform *platform = socdev->platform;
cb666e5b
LG
137 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
138 struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
db2a4165
FM
139 int ret = 0;
140
141 mutex_lock(&pcm_mutex);
142
143 /* startup the audio subsystem */
cb666e5b
LG
144 if (cpu_dai->ops.startup) {
145 ret = cpu_dai->ops.startup(substream);
db2a4165
FM
146 if (ret < 0) {
147 printk(KERN_ERR "asoc: can't open interface %s\n",
cb666e5b 148 cpu_dai->name);
db2a4165
FM
149 goto out;
150 }
151 }
152
153 if (platform->pcm_ops->open) {
154 ret = platform->pcm_ops->open(substream);
155 if (ret < 0) {
156 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
157 goto platform_err;
158 }
159 }
160
cb666e5b
LG
161 if (codec_dai->ops.startup) {
162 ret = codec_dai->ops.startup(substream);
db2a4165 163 if (ret < 0) {
cb666e5b
LG
164 printk(KERN_ERR "asoc: can't open codec %s\n",
165 codec_dai->name);
166 goto codec_dai_err;
db2a4165
FM
167 }
168 }
169
cb666e5b
LG
170 if (machine->ops && machine->ops->startup) {
171 ret = machine->ops->startup(substream);
db2a4165 172 if (ret < 0) {
cb666e5b
LG
173 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
174 goto machine_err;
db2a4165
FM
175 }
176 }
177
db2a4165
FM
178 /* Check that the codec and cpu DAI's are compatible */
179 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
180 runtime->hw.rate_min =
3ff3f64b
MB
181 max(codec_dai->playback.rate_min,
182 cpu_dai->playback.rate_min);
db2a4165 183 runtime->hw.rate_max =
3ff3f64b
MB
184 min(codec_dai->playback.rate_max,
185 cpu_dai->playback.rate_max);
db2a4165 186 runtime->hw.channels_min =
cb666e5b
LG
187 max(codec_dai->playback.channels_min,
188 cpu_dai->playback.channels_min);
db2a4165 189 runtime->hw.channels_max =
cb666e5b
LG
190 min(codec_dai->playback.channels_max,
191 cpu_dai->playback.channels_max);
192 runtime->hw.formats =
193 codec_dai->playback.formats & cpu_dai->playback.formats;
194 runtime->hw.rates =
195 codec_dai->playback.rates & cpu_dai->playback.rates;
db2a4165
FM
196 } else {
197 runtime->hw.rate_min =
3ff3f64b
MB
198 max(codec_dai->capture.rate_min,
199 cpu_dai->capture.rate_min);
db2a4165 200 runtime->hw.rate_max =
3ff3f64b
MB
201 min(codec_dai->capture.rate_max,
202 cpu_dai->capture.rate_max);
db2a4165 203 runtime->hw.channels_min =
cb666e5b
LG
204 max(codec_dai->capture.channels_min,
205 cpu_dai->capture.channels_min);
db2a4165 206 runtime->hw.channels_max =
cb666e5b
LG
207 min(codec_dai->capture.channels_max,
208 cpu_dai->capture.channels_max);
209 runtime->hw.formats =
210 codec_dai->capture.formats & cpu_dai->capture.formats;
211 runtime->hw.rates =
212 codec_dai->capture.rates & cpu_dai->capture.rates;
db2a4165
FM
213 }
214
215 snd_pcm_limit_hw_rates(runtime);
216 if (!runtime->hw.rates) {
217 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
cb666e5b
LG
218 codec_dai->name, cpu_dai->name);
219 goto machine_err;
db2a4165
FM
220 }
221 if (!runtime->hw.formats) {
222 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
cb666e5b
LG
223 codec_dai->name, cpu_dai->name);
224 goto machine_err;
db2a4165
FM
225 }
226 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
227 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
cb666e5b
LG
228 codec_dai->name, cpu_dai->name);
229 goto machine_err;
db2a4165
FM
230 }
231
3ff3f64b 232 dbg("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
b5c5fd24
LG
233 dbg("asoc: rate mask 0x%x\n", runtime->hw.rates);
234 dbg("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
235 runtime->hw.channels_max);
236 dbg("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
237 runtime->hw.rate_max);
db2a4165 238
db2a4165 239 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
cb666e5b 240 cpu_dai->playback.active = codec_dai->playback.active = 1;
db2a4165 241 else
cb666e5b
LG
242 cpu_dai->capture.active = codec_dai->capture.active = 1;
243 cpu_dai->active = codec_dai->active = 1;
244 cpu_dai->runtime = runtime;
db2a4165
FM
245 socdev->codec->active++;
246 mutex_unlock(&pcm_mutex);
247 return 0;
248
cb666e5b 249machine_err:
db2a4165
FM
250 if (machine->ops && machine->ops->shutdown)
251 machine->ops->shutdown(substream);
252
cb666e5b 253codec_dai_err:
db2a4165
FM
254 if (platform->pcm_ops->close)
255 platform->pcm_ops->close(substream);
256
257platform_err:
cb666e5b
LG
258 if (cpu_dai->ops.shutdown)
259 cpu_dai->ops.shutdown(substream);
db2a4165
FM
260out:
261 mutex_unlock(&pcm_mutex);
262 return ret;
263}
264
265/*
3a4fa0a2 266 * Power down the audio subsystem pmdown_time msecs after close is called.
db2a4165
FM
267 * This is to ensure there are no pops or clicks in between any music tracks
268 * due to DAPM power cycling.
269 */
4484bb2e 270static void close_delayed_work(struct work_struct *work)
db2a4165 271{
4484bb2e
AM
272 struct snd_soc_device *socdev =
273 container_of(work, struct snd_soc_device, delayed_work.work);
db2a4165
FM
274 struct snd_soc_codec *codec = socdev->codec;
275 struct snd_soc_codec_dai *codec_dai;
276 int i;
277
278 mutex_lock(&pcm_mutex);
3ff3f64b 279 for (i = 0; i < codec->num_dai; i++) {
db2a4165
FM
280 codec_dai = &codec->dai[i];
281
282 dbg("pop wq checking: %s status: %s waiting: %s\n",
283 codec_dai->playback.stream_name,
284 codec_dai->playback.active ? "active" : "inactive",
285 codec_dai->pop_wait ? "yes" : "no");
286
287 /* are we waiting on this codec DAI stream */
288 if (codec_dai->pop_wait == 1) {
289
0be9898a 290 /* Reduce power if no longer active */
3c1c47e0
LG
291 if (codec->active == 0) {
292 dbg("pop wq D1 %s %s\n", codec->name,
293 codec_dai->playback.stream_name);
0be9898a
MB
294 snd_soc_dapm_set_bias_level(socdev,
295 SND_SOC_BIAS_PREPARE);
3c1c47e0
LG
296 }
297
db2a4165 298 codec_dai->pop_wait = 0;
0b4d221b
LG
299 snd_soc_dapm_stream_event(codec,
300 codec_dai->playback.stream_name,
db2a4165
FM
301 SND_SOC_DAPM_STREAM_STOP);
302
0be9898a 303 /* Fall into standby if no longer active */
db2a4165
FM
304 if (codec->active == 0) {
305 dbg("pop wq D3 %s %s\n", codec->name,
306 codec_dai->playback.stream_name);
0be9898a
MB
307 snd_soc_dapm_set_bias_level(socdev,
308 SND_SOC_BIAS_STANDBY);
db2a4165
FM
309 }
310 }
311 }
312 mutex_unlock(&pcm_mutex);
313}
314
315/*
316 * Called by ALSA when a PCM substream is closed. Private data can be
317 * freed here. The cpu DAI, codec DAI, machine and platform are also
318 * shutdown.
319 */
320static int soc_codec_close(struct snd_pcm_substream *substream)
321{
322 struct snd_soc_pcm_runtime *rtd = substream->private_data;
323 struct snd_soc_device *socdev = rtd->socdev;
cb666e5b 324 struct snd_soc_dai_link *machine = rtd->dai;
db2a4165 325 struct snd_soc_platform *platform = socdev->platform;
cb666e5b
LG
326 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
327 struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
db2a4165
FM
328 struct snd_soc_codec *codec = socdev->codec;
329
330 mutex_lock(&pcm_mutex);
331
332 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
cb666e5b 333 cpu_dai->playback.active = codec_dai->playback.active = 0;
db2a4165 334 else
cb666e5b 335 cpu_dai->capture.active = codec_dai->capture.active = 0;
db2a4165 336
cb666e5b
LG
337 if (codec_dai->playback.active == 0 &&
338 codec_dai->capture.active == 0) {
339 cpu_dai->active = codec_dai->active = 0;
db2a4165
FM
340 }
341 codec->active--;
342
cb666e5b
LG
343 if (cpu_dai->ops.shutdown)
344 cpu_dai->ops.shutdown(substream);
db2a4165 345
cb666e5b
LG
346 if (codec_dai->ops.shutdown)
347 codec_dai->ops.shutdown(substream);
db2a4165
FM
348
349 if (machine->ops && machine->ops->shutdown)
350 machine->ops->shutdown(substream);
351
352 if (platform->pcm_ops->close)
353 platform->pcm_ops->close(substream);
cb666e5b 354 cpu_dai->runtime = NULL;
db2a4165
FM
355
356 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
357 /* start delayed pop wq here for playback streams */
cb666e5b 358 codec_dai->pop_wait = 1;
4bb09523 359 schedule_delayed_work(&socdev->delayed_work,
db2a4165
FM
360 msecs_to_jiffies(pmdown_time));
361 } else {
362 /* capture streams can be powered down now */
cb666e5b 363 snd_soc_dapm_stream_event(codec,
0b4d221b
LG
364 codec_dai->capture.stream_name,
365 SND_SOC_DAPM_STREAM_STOP);
db2a4165 366
0b4d221b 367 if (codec->active == 0 && codec_dai->pop_wait == 0)
0be9898a
MB
368 snd_soc_dapm_set_bias_level(socdev,
369 SND_SOC_BIAS_STANDBY);
db2a4165
FM
370 }
371
372 mutex_unlock(&pcm_mutex);
373 return 0;
374}
375
376/*
377 * Called by ALSA when the PCM substream is prepared, can set format, sample
378 * rate, etc. This function is non atomic and can be called multiple times,
379 * it can refer to the runtime info.
380 */
381static int soc_pcm_prepare(struct snd_pcm_substream *substream)
382{
383 struct snd_soc_pcm_runtime *rtd = substream->private_data;
384 struct snd_soc_device *socdev = rtd->socdev;
cb666e5b 385 struct snd_soc_dai_link *machine = rtd->dai;
db2a4165 386 struct snd_soc_platform *platform = socdev->platform;
cb666e5b
LG
387 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
388 struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
db2a4165
FM
389 struct snd_soc_codec *codec = socdev->codec;
390 int ret = 0;
391
392 mutex_lock(&pcm_mutex);
cb666e5b
LG
393
394 if (machine->ops && machine->ops->prepare) {
395 ret = machine->ops->prepare(substream);
396 if (ret < 0) {
397 printk(KERN_ERR "asoc: machine prepare error\n");
398 goto out;
399 }
400 }
401
db2a4165
FM
402 if (platform->pcm_ops->prepare) {
403 ret = platform->pcm_ops->prepare(substream);
a71a468a
LG
404 if (ret < 0) {
405 printk(KERN_ERR "asoc: platform prepare error\n");
db2a4165 406 goto out;
a71a468a 407 }
db2a4165
FM
408 }
409
cb666e5b
LG
410 if (codec_dai->ops.prepare) {
411 ret = codec_dai->ops.prepare(substream);
a71a468a
LG
412 if (ret < 0) {
413 printk(KERN_ERR "asoc: codec DAI prepare error\n");
db2a4165 414 goto out;
a71a468a 415 }
db2a4165
FM
416 }
417
cb666e5b
LG
418 if (cpu_dai->ops.prepare) {
419 ret = cpu_dai->ops.prepare(substream);
420 if (ret < 0) {
421 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
422 goto out;
423 }
424 }
db2a4165
FM
425
426 /* we only want to start a DAPM playback stream if we are not waiting
427 * on an existing one stopping */
cb666e5b 428 if (codec_dai->pop_wait) {
db2a4165
FM
429 /* we are waiting for the delayed work to start */
430 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
cb666e5b
LG
431 snd_soc_dapm_stream_event(socdev->codec,
432 codec_dai->capture.stream_name,
db2a4165
FM
433 SND_SOC_DAPM_STREAM_START);
434 else {
cb666e5b 435 codec_dai->pop_wait = 0;
4484bb2e 436 cancel_delayed_work(&socdev->delayed_work);
cb666e5b
LG
437 if (codec_dai->dai_ops.digital_mute)
438 codec_dai->dai_ops.digital_mute(codec_dai, 0);
db2a4165
FM
439 }
440 } else {
441 /* no delayed work - do we need to power up codec */
0be9898a 442 if (codec->bias_level != SND_SOC_BIAS_ON) {
db2a4165 443
0be9898a
MB
444 snd_soc_dapm_set_bias_level(socdev,
445 SND_SOC_BIAS_PREPARE);
db2a4165
FM
446
447 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
448 snd_soc_dapm_stream_event(codec,
cb666e5b 449 codec_dai->playback.stream_name,
db2a4165
FM
450 SND_SOC_DAPM_STREAM_START);
451 else
452 snd_soc_dapm_stream_event(codec,
cb666e5b 453 codec_dai->capture.stream_name,
db2a4165
FM
454 SND_SOC_DAPM_STREAM_START);
455
0be9898a 456 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
cb666e5b
LG
457 if (codec_dai->dai_ops.digital_mute)
458 codec_dai->dai_ops.digital_mute(codec_dai, 0);
db2a4165
FM
459
460 } else {
461 /* codec already powered - power on widgets */
462 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
463 snd_soc_dapm_stream_event(codec,
cb666e5b 464 codec_dai->playback.stream_name,
db2a4165
FM
465 SND_SOC_DAPM_STREAM_START);
466 else
467 snd_soc_dapm_stream_event(codec,
cb666e5b 468 codec_dai->capture.stream_name,
db2a4165 469 SND_SOC_DAPM_STREAM_START);
cb666e5b
LG
470 if (codec_dai->dai_ops.digital_mute)
471 codec_dai->dai_ops.digital_mute(codec_dai, 0);
db2a4165
FM
472 }
473 }
474
475out:
476 mutex_unlock(&pcm_mutex);
477 return ret;
478}
479
480/*
481 * Called by ALSA when the hardware params are set by application. This
482 * function can also be called multiple times and can allocate buffers
483 * (using snd_pcm_lib_* ). It's non-atomic.
484 */
485static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
486 struct snd_pcm_hw_params *params)
487{
488 struct snd_soc_pcm_runtime *rtd = substream->private_data;
489 struct snd_soc_device *socdev = rtd->socdev;
cb666e5b 490 struct snd_soc_dai_link *machine = rtd->dai;
db2a4165 491 struct snd_soc_platform *platform = socdev->platform;
cb666e5b
LG
492 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
493 struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
db2a4165
FM
494 int ret = 0;
495
496 mutex_lock(&pcm_mutex);
497
cb666e5b
LG
498 if (machine->ops && machine->ops->hw_params) {
499 ret = machine->ops->hw_params(substream, params);
500 if (ret < 0) {
501 printk(KERN_ERR "asoc: machine hw_params failed\n");
db2a4165 502 goto out;
cb666e5b 503 }
db2a4165
FM
504 }
505
cb666e5b
LG
506 if (codec_dai->ops.hw_params) {
507 ret = codec_dai->ops.hw_params(substream, params);
db2a4165
FM
508 if (ret < 0) {
509 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
cb666e5b
LG
510 codec_dai->name);
511 goto codec_err;
db2a4165
FM
512 }
513 }
514
cb666e5b
LG
515 if (cpu_dai->ops.hw_params) {
516 ret = cpu_dai->ops.hw_params(substream, params);
db2a4165 517 if (ret < 0) {
3ff3f64b 518 printk(KERN_ERR "asoc: interface %s hw params failed\n",
cb666e5b 519 cpu_dai->name);
db2a4165
FM
520 goto interface_err;
521 }
522 }
523
524 if (platform->pcm_ops->hw_params) {
525 ret = platform->pcm_ops->hw_params(substream, params);
526 if (ret < 0) {
3ff3f64b 527 printk(KERN_ERR "asoc: platform %s hw params failed\n",
db2a4165
FM
528 platform->name);
529 goto platform_err;
530 }
531 }
532
db2a4165
FM
533out:
534 mutex_unlock(&pcm_mutex);
535 return ret;
536
db2a4165 537platform_err:
cb666e5b
LG
538 if (cpu_dai->ops.hw_free)
539 cpu_dai->ops.hw_free(substream);
db2a4165
FM
540
541interface_err:
cb666e5b
LG
542 if (codec_dai->ops.hw_free)
543 codec_dai->ops.hw_free(substream);
544
545codec_err:
3ff3f64b 546 if (machine->ops && machine->ops->hw_free)
cb666e5b 547 machine->ops->hw_free(substream);
db2a4165
FM
548
549 mutex_unlock(&pcm_mutex);
550 return ret;
551}
552
553/*
554 * Free's resources allocated by hw_params, can be called multiple times
555 */
556static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
557{
558 struct snd_soc_pcm_runtime *rtd = substream->private_data;
559 struct snd_soc_device *socdev = rtd->socdev;
cb666e5b 560 struct snd_soc_dai_link *machine = rtd->dai;
db2a4165 561 struct snd_soc_platform *platform = socdev->platform;
cb666e5b
LG
562 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
563 struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
db2a4165 564 struct snd_soc_codec *codec = socdev->codec;
db2a4165
FM
565
566 mutex_lock(&pcm_mutex);
567
568 /* apply codec digital mute */
cb666e5b
LG
569 if (!codec->active && codec_dai->dai_ops.digital_mute)
570 codec_dai->dai_ops.digital_mute(codec_dai, 1);
db2a4165
FM
571
572 /* free any machine hw params */
573 if (machine->ops && machine->ops->hw_free)
574 machine->ops->hw_free(substream);
575
576 /* free any DMA resources */
577 if (platform->pcm_ops->hw_free)
578 platform->pcm_ops->hw_free(substream);
579
580 /* now free hw params for the DAI's */
cb666e5b
LG
581 if (codec_dai->ops.hw_free)
582 codec_dai->ops.hw_free(substream);
db2a4165 583
cb666e5b
LG
584 if (cpu_dai->ops.hw_free)
585 cpu_dai->ops.hw_free(substream);
db2a4165
FM
586
587 mutex_unlock(&pcm_mutex);
588 return 0;
589}
590
591static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
592{
593 struct snd_soc_pcm_runtime *rtd = substream->private_data;
594 struct snd_soc_device *socdev = rtd->socdev;
cb666e5b 595 struct snd_soc_dai_link *machine = rtd->dai;
db2a4165 596 struct snd_soc_platform *platform = socdev->platform;
cb666e5b
LG
597 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
598 struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
db2a4165
FM
599 int ret;
600
cb666e5b
LG
601 if (codec_dai->ops.trigger) {
602 ret = codec_dai->ops.trigger(substream, cmd);
db2a4165
FM
603 if (ret < 0)
604 return ret;
605 }
606
607 if (platform->pcm_ops->trigger) {
608 ret = platform->pcm_ops->trigger(substream, cmd);
609 if (ret < 0)
610 return ret;
611 }
612
cb666e5b
LG
613 if (cpu_dai->ops.trigger) {
614 ret = cpu_dai->ops.trigger(substream, cmd);
db2a4165
FM
615 if (ret < 0)
616 return ret;
617 }
618 return 0;
619}
620
621/* ASoC PCM operations */
622static struct snd_pcm_ops soc_pcm_ops = {
623 .open = soc_pcm_open,
624 .close = soc_codec_close,
625 .hw_params = soc_pcm_hw_params,
626 .hw_free = soc_pcm_hw_free,
627 .prepare = soc_pcm_prepare,
628 .trigger = soc_pcm_trigger,
629};
630
631#ifdef CONFIG_PM
632/* powers down audio subsystem for suspend */
633static int soc_suspend(struct platform_device *pdev, pm_message_t state)
634{
3ff3f64b
MB
635 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
636 struct snd_soc_machine *machine = socdev->machine;
637 struct snd_soc_platform *platform = socdev->platform;
638 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
db2a4165
FM
639 struct snd_soc_codec *codec = socdev->codec;
640 int i;
641
6ed25978
AG
642 /* Due to the resume being scheduled into a workqueue we could
643 * suspend before that's finished - wait for it to complete.
644 */
645 snd_power_lock(codec->card);
646 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
647 snd_power_unlock(codec->card);
648
649 /* we're going to block userspace touching us until resume completes */
650 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
651
db2a4165 652 /* mute any active DAC's */
3ff3f64b 653 for (i = 0; i < machine->num_links; i++) {
db2a4165 654 struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
cb666e5b
LG
655 if (dai->dai_ops.digital_mute && dai->playback.active)
656 dai->dai_ops.digital_mute(dai, 1);
db2a4165
FM
657 }
658
4ccab3e7
LG
659 /* suspend all pcms */
660 for (i = 0; i < machine->num_links; i++)
661 snd_pcm_suspend_all(machine->dai_link[i].pcm);
662
db2a4165
FM
663 if (machine->suspend_pre)
664 machine->suspend_pre(pdev, state);
665
3ff3f64b 666 for (i = 0; i < machine->num_links; i++) {
db2a4165
FM
667 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
668 if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
669 cpu_dai->suspend(pdev, cpu_dai);
670 if (platform->suspend)
671 platform->suspend(pdev, cpu_dai);
672 }
673
674 /* close any waiting streams and save state */
965ac42c 675 run_delayed_work(&socdev->delayed_work);
0be9898a 676 codec->suspend_bias_level = codec->bias_level;
db2a4165 677
3ff3f64b 678 for (i = 0; i < codec->num_dai; i++) {
db2a4165
FM
679 char *stream = codec->dai[i].playback.stream_name;
680 if (stream != NULL)
681 snd_soc_dapm_stream_event(codec, stream,
682 SND_SOC_DAPM_STREAM_SUSPEND);
683 stream = codec->dai[i].capture.stream_name;
684 if (stream != NULL)
685 snd_soc_dapm_stream_event(codec, stream,
686 SND_SOC_DAPM_STREAM_SUSPEND);
687 }
688
689 if (codec_dev->suspend)
690 codec_dev->suspend(pdev, state);
691
3ff3f64b 692 for (i = 0; i < machine->num_links; i++) {
db2a4165
FM
693 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
694 if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
695 cpu_dai->suspend(pdev, cpu_dai);
696 }
697
698 if (machine->suspend_post)
699 machine->suspend_post(pdev, state);
700
701 return 0;
702}
703
6ed25978
AG
704/* deferred resume work, so resume can complete before we finished
705 * setting our codec back up, which can be very slow on I2C
706 */
707static void soc_resume_deferred(struct work_struct *work)
db2a4165 708{
6ed25978
AG
709 struct snd_soc_device *socdev = container_of(work,
710 struct snd_soc_device,
711 deferred_resume_work);
3ff3f64b
MB
712 struct snd_soc_machine *machine = socdev->machine;
713 struct snd_soc_platform *platform = socdev->platform;
714 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
db2a4165 715 struct snd_soc_codec *codec = socdev->codec;
6ed25978 716 struct platform_device *pdev = to_platform_device(socdev->dev);
db2a4165
FM
717 int i;
718
6ed25978
AG
719 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
720 * so userspace apps are blocked from touching us
721 */
722
723 dev_info(socdev->dev, "starting resume work\n");
724
db2a4165
FM
725 if (machine->resume_pre)
726 machine->resume_pre(pdev);
727
3ff3f64b 728 for (i = 0; i < machine->num_links; i++) {
db2a4165
FM
729 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
730 if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
731 cpu_dai->resume(pdev, cpu_dai);
732 }
733
734 if (codec_dev->resume)
735 codec_dev->resume(pdev);
736
3ff3f64b
MB
737 for (i = 0; i < codec->num_dai; i++) {
738 char *stream = codec->dai[i].playback.stream_name;
db2a4165
FM
739 if (stream != NULL)
740 snd_soc_dapm_stream_event(codec, stream,
741 SND_SOC_DAPM_STREAM_RESUME);
742 stream = codec->dai[i].capture.stream_name;
743 if (stream != NULL)
744 snd_soc_dapm_stream_event(codec, stream,
745 SND_SOC_DAPM_STREAM_RESUME);
746 }
747
3ff3f64b
MB
748 /* unmute any active DACs */
749 for (i = 0; i < machine->num_links; i++) {
db2a4165 750 struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
cb666e5b
LG
751 if (dai->dai_ops.digital_mute && dai->playback.active)
752 dai->dai_ops.digital_mute(dai, 0);
db2a4165
FM
753 }
754
3ff3f64b 755 for (i = 0; i < machine->num_links; i++) {
db2a4165
FM
756 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
757 if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
758 cpu_dai->resume(pdev, cpu_dai);
759 if (platform->resume)
760 platform->resume(pdev, cpu_dai);
761 }
762
763 if (machine->resume_post)
764 machine->resume_post(pdev);
765
6ed25978
AG
766 dev_info(socdev->dev, "resume work completed\n");
767
768 /* userspace can access us now we are back as we were before */
769 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
770}
771
772/* powers up audio subsystem after a suspend */
773static int soc_resume(struct platform_device *pdev)
774{
775 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
776
777 dev_info(socdev->dev, "scheduling resume work\n");
778
779 if (!schedule_work(&socdev->deferred_resume_work))
780 dev_err(socdev->dev, "work item may be lost\n");
781
db2a4165
FM
782 return 0;
783}
784
785#else
786#define soc_suspend NULL
787#define soc_resume NULL
788#endif
789
790/* probes a new socdev */
791static int soc_probe(struct platform_device *pdev)
792{
793 int ret = 0, i;
794 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
795 struct snd_soc_machine *machine = socdev->machine;
796 struct snd_soc_platform *platform = socdev->platform;
797 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
798
799 if (machine->probe) {
800 ret = machine->probe(pdev);
3ff3f64b 801 if (ret < 0)
db2a4165
FM
802 return ret;
803 }
804
805 for (i = 0; i < machine->num_links; i++) {
806 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
807 if (cpu_dai->probe) {
bdb92876 808 ret = cpu_dai->probe(pdev, cpu_dai);
3ff3f64b 809 if (ret < 0)
db2a4165
FM
810 goto cpu_dai_err;
811 }
812 }
813
814 if (codec_dev->probe) {
815 ret = codec_dev->probe(pdev);
3ff3f64b 816 if (ret < 0)
db2a4165
FM
817 goto cpu_dai_err;
818 }
819
820 if (platform->probe) {
821 ret = platform->probe(pdev);
3ff3f64b 822 if (ret < 0)
db2a4165
FM
823 goto platform_err;
824 }
825
826 /* DAPM stream work */
4484bb2e 827 INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
6ed25978
AG
828 /* deferred resume work */
829 INIT_WORK(&socdev->deferred_resume_work, soc_resume_deferred);
830
db2a4165
FM
831 return 0;
832
db2a4165
FM
833platform_err:
834 if (codec_dev->remove)
835 codec_dev->remove(pdev);
836
837cpu_dai_err:
18b9b3d9 838 for (i--; i >= 0; i--) {
db2a4165
FM
839 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
840 if (cpu_dai->remove)
bdb92876 841 cpu_dai->remove(pdev, cpu_dai);
db2a4165
FM
842 }
843
844 if (machine->remove)
845 machine->remove(pdev);
846
847 return ret;
848}
849
850/* removes a socdev */
851static int soc_remove(struct platform_device *pdev)
852{
853 int i;
854 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
855 struct snd_soc_machine *machine = socdev->machine;
856 struct snd_soc_platform *platform = socdev->platform;
857 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
858
965ac42c
LG
859 run_delayed_work(&socdev->delayed_work);
860
db2a4165
FM
861 if (platform->remove)
862 platform->remove(pdev);
863
864 if (codec_dev->remove)
865 codec_dev->remove(pdev);
866
867 for (i = 0; i < machine->num_links; i++) {
868 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
869 if (cpu_dai->remove)
bdb92876 870 cpu_dai->remove(pdev, cpu_dai);
db2a4165
FM
871 }
872
873 if (machine->remove)
874 machine->remove(pdev);
875
876 return 0;
877}
878
879/* ASoC platform driver */
880static struct platform_driver soc_driver = {
881 .driver = {
882 .name = "soc-audio",
8b45a209 883 .owner = THIS_MODULE,
db2a4165
FM
884 },
885 .probe = soc_probe,
886 .remove = soc_remove,
887 .suspend = soc_suspend,
888 .resume = soc_resume,
889};
890
891/* create a new pcm */
892static int soc_new_pcm(struct snd_soc_device *socdev,
893 struct snd_soc_dai_link *dai_link, int num)
894{
895 struct snd_soc_codec *codec = socdev->codec;
896 struct snd_soc_codec_dai *codec_dai = dai_link->codec_dai;
897 struct snd_soc_cpu_dai *cpu_dai = dai_link->cpu_dai;
898 struct snd_soc_pcm_runtime *rtd;
899 struct snd_pcm *pcm;
900 char new_name[64];
901 int ret = 0, playback = 0, capture = 0;
902
903 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
904 if (rtd == NULL)
905 return -ENOMEM;
cb666e5b
LG
906
907 rtd->dai = dai_link;
db2a4165 908 rtd->socdev = socdev;
cb666e5b 909 codec_dai->codec = socdev->codec;
db2a4165
FM
910
911 /* check client and interface hw capabilities */
3ff3f64b 912 sprintf(new_name, "%s %s-%s-%d", dai_link->stream_name, codec_dai->name,
db2a4165
FM
913 get_dai_name(cpu_dai->type), num);
914
915 if (codec_dai->playback.channels_min)
916 playback = 1;
917 if (codec_dai->capture.channels_min)
918 capture = 1;
919
920 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
921 capture, &pcm);
922 if (ret < 0) {
3ff3f64b
MB
923 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
924 codec->name);
db2a4165
FM
925 kfree(rtd);
926 return ret;
927 }
928
4ccab3e7 929 dai_link->pcm = pcm;
db2a4165
FM
930 pcm->private_data = rtd;
931 soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
932 soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
933 soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
934 soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
935 soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
936 soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
937 soc_pcm_ops.page = socdev->platform->pcm_ops->page;
938
939 if (playback)
940 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
941
942 if (capture)
943 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
944
945 ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
946 if (ret < 0) {
947 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
948 kfree(rtd);
949 return ret;
950 }
951
952 pcm->private_free = socdev->platform->pcm_free;
953 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
954 cpu_dai->name);
955 return ret;
956}
957
958/* codec register dump */
959static ssize_t codec_reg_show(struct device *dev,
960 struct device_attribute *attr, char *buf)
961{
962 struct snd_soc_device *devdata = dev_get_drvdata(dev);
963 struct snd_soc_codec *codec = devdata->codec;
964 int i, step = 1, count = 0;
965
966 if (!codec->reg_cache_size)
967 return 0;
968
969 if (codec->reg_cache_step)
970 step = codec->reg_cache_step;
971
972 count += sprintf(buf, "%s registers\n", codec->name);
3ff3f64b
MB
973 for (i = 0; i < codec->reg_cache_size; i += step)
974 count += sprintf(buf + count, "%2x: %4x\n", i,
975 codec->read(codec, i));
db2a4165
FM
976
977 return count;
978}
979static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
980
981/**
982 * snd_soc_new_ac97_codec - initailise AC97 device
983 * @codec: audio codec
984 * @ops: AC97 bus operations
985 * @num: AC97 codec number
986 *
987 * Initialises AC97 codec resources for use by ad-hoc devices only.
988 */
989int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
990 struct snd_ac97_bus_ops *ops, int num)
991{
992 mutex_lock(&codec->mutex);
993
994 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
995 if (codec->ac97 == NULL) {
996 mutex_unlock(&codec->mutex);
997 return -ENOMEM;
998 }
999
1000 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1001 if (codec->ac97->bus == NULL) {
1002 kfree(codec->ac97);
1003 codec->ac97 = NULL;
1004 mutex_unlock(&codec->mutex);
1005 return -ENOMEM;
1006 }
1007
1008 codec->ac97->bus->ops = ops;
1009 codec->ac97->num = num;
1010 mutex_unlock(&codec->mutex);
1011 return 0;
1012}
1013EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1014
1015/**
1016 * snd_soc_free_ac97_codec - free AC97 codec device
1017 * @codec: audio codec
1018 *
1019 * Frees AC97 codec device resources.
1020 */
1021void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1022{
1023 mutex_lock(&codec->mutex);
1024 kfree(codec->ac97->bus);
1025 kfree(codec->ac97);
1026 codec->ac97 = NULL;
1027 mutex_unlock(&codec->mutex);
1028}
1029EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1030
1031/**
1032 * snd_soc_update_bits - update codec register bits
1033 * @codec: audio codec
1034 * @reg: codec register
1035 * @mask: register mask
1036 * @value: new value
1037 *
1038 * Writes new register value.
1039 *
1040 * Returns 1 for change else 0.
1041 */
1042int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1043 unsigned short mask, unsigned short value)
1044{
1045 int change;
1046 unsigned short old, new;
1047
1048 mutex_lock(&io_mutex);
1049 old = snd_soc_read(codec, reg);
1050 new = (old & ~mask) | value;
1051 change = old != new;
1052 if (change)
1053 snd_soc_write(codec, reg, new);
1054
1055 mutex_unlock(&io_mutex);
1056 return change;
1057}
1058EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1059
1060/**
1061 * snd_soc_test_bits - test register for change
1062 * @codec: audio codec
1063 * @reg: codec register
1064 * @mask: register mask
1065 * @value: new value
1066 *
1067 * Tests a register with a new value and checks if the new value is
1068 * different from the old value.
1069 *
1070 * Returns 1 for change else 0.
1071 */
1072int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1073 unsigned short mask, unsigned short value)
1074{
1075 int change;
1076 unsigned short old, new;
1077
1078 mutex_lock(&io_mutex);
1079 old = snd_soc_read(codec, reg);
1080 new = (old & ~mask) | value;
1081 change = old != new;
1082 mutex_unlock(&io_mutex);
1083
1084 return change;
1085}
1086EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1087
db2a4165
FM
1088/**
1089 * snd_soc_new_pcms - create new sound card and pcms
1090 * @socdev: the SoC audio device
1091 *
1092 * Create a new sound card based upon the codec and interface pcms.
1093 *
1094 * Returns 0 for success, else error.
1095 */
bc7320c5 1096int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
db2a4165
FM
1097{
1098 struct snd_soc_codec *codec = socdev->codec;
1099 struct snd_soc_machine *machine = socdev->machine;
1100 int ret = 0, i;
1101
1102 mutex_lock(&codec->mutex);
1103
1104 /* register a sound card */
1105 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1106 if (!codec->card) {
1107 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1108 codec->name);
1109 mutex_unlock(&codec->mutex);
1110 return -ENODEV;
1111 }
1112
1113 codec->card->dev = socdev->dev;
1114 codec->card->private_data = codec;
1115 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1116
1117 /* create the pcms */
3ff3f64b 1118 for (i = 0; i < machine->num_links; i++) {
db2a4165
FM
1119 ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
1120 if (ret < 0) {
1121 printk(KERN_ERR "asoc: can't create pcm %s\n",
1122 machine->dai_link[i].stream_name);
1123 mutex_unlock(&codec->mutex);
1124 return ret;
1125 }
1126 }
1127
1128 mutex_unlock(&codec->mutex);
1129 return ret;
1130}
1131EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1132
1133/**
1134 * snd_soc_register_card - register sound card
1135 * @socdev: the SoC audio device
1136 *
1137 * Register a SoC sound card. Also registers an AC97 device if the
1138 * codec is AC97 for ad hoc devices.
1139 *
1140 * Returns 0 for success, else error.
1141 */
1142int snd_soc_register_card(struct snd_soc_device *socdev)
1143{
1144 struct snd_soc_codec *codec = socdev->codec;
1145 struct snd_soc_machine *machine = socdev->machine;
12e74f7d 1146 int ret = 0, i, ac97 = 0, err = 0;
db2a4165 1147
3ff3f64b 1148 for (i = 0; i < machine->num_links; i++) {
12e74f7d
LG
1149 if (socdev->machine->dai_link[i].init) {
1150 err = socdev->machine->dai_link[i].init(codec);
1151 if (err < 0) {
1152 printk(KERN_ERR "asoc: failed to init %s\n",
1153 socdev->machine->dai_link[i].stream_name);
1154 continue;
1155 }
1156 }
3ff3f64b 1157 if (socdev->machine->dai_link[i].codec_dai->type ==
a68660e0 1158 SND_SOC_DAI_AC97_BUS)
db2a4165
FM
1159 ac97 = 1;
1160 }
1161 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1162 "%s", machine->name);
1163 snprintf(codec->card->longname, sizeof(codec->card->longname),
1164 "%s (%s)", machine->name, codec->name);
1165
1166 ret = snd_card_register(codec->card);
1167 if (ret < 0) {
3ff3f64b 1168 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
db2a4165 1169 codec->name);
12e74f7d 1170 goto out;
db2a4165
FM
1171 }
1172
08c8efe6 1173 mutex_lock(&codec->mutex);
db2a4165 1174#ifdef CONFIG_SND_SOC_AC97_BUS
12e74f7d
LG
1175 if (ac97) {
1176 ret = soc_ac97_dev_register(codec);
1177 if (ret < 0) {
1178 printk(KERN_ERR "asoc: AC97 device register failed\n");
1179 snd_card_free(codec->card);
08c8efe6 1180 mutex_unlock(&codec->mutex);
12e74f7d
LG
1181 goto out;
1182 }
1183 }
db2a4165
FM
1184#endif
1185
12e74f7d
LG
1186 err = snd_soc_dapm_sys_add(socdev->dev);
1187 if (err < 0)
1188 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1189
1190 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1191 if (err < 0)
3ff3f64b 1192 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
08c8efe6 1193
db2a4165 1194 mutex_unlock(&codec->mutex);
08c8efe6
MB
1195
1196out:
db2a4165
FM
1197 return ret;
1198}
1199EXPORT_SYMBOL_GPL(snd_soc_register_card);
1200
1201/**
1202 * snd_soc_free_pcms - free sound card and pcms
1203 * @socdev: the SoC audio device
1204 *
1205 * Frees sound card and pcms associated with the socdev.
1206 * Also unregister the codec if it is an AC97 device.
1207 */
1208void snd_soc_free_pcms(struct snd_soc_device *socdev)
1209{
1210 struct snd_soc_codec *codec = socdev->codec;
a68660e0
LG
1211#ifdef CONFIG_SND_SOC_AC97_BUS
1212 struct snd_soc_codec_dai *codec_dai;
1213 int i;
1214#endif
db2a4165
FM
1215
1216 mutex_lock(&codec->mutex);
1217#ifdef CONFIG_SND_SOC_AC97_BUS
3ff3f64b 1218 for (i = 0; i < codec->num_dai; i++) {
a68660e0
LG
1219 codec_dai = &codec->dai[i];
1220 if (codec_dai->type == SND_SOC_DAI_AC97_BUS && codec->ac97) {
1221 soc_ac97_dev_unregister(codec);
1222 goto free_card;
1223 }
1224 }
1225free_card:
db2a4165
FM
1226#endif
1227
1228 if (codec->card)
1229 snd_card_free(codec->card);
1230 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1231 mutex_unlock(&codec->mutex);
1232}
1233EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1234
1235/**
1236 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1237 * @substream: the pcm substream
1238 * @hw: the hardware parameters
1239 *
1240 * Sets the substream runtime hardware parameters.
1241 */
1242int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1243 const struct snd_pcm_hardware *hw)
1244{
1245 struct snd_pcm_runtime *runtime = substream->runtime;
1246 runtime->hw.info = hw->info;
1247 runtime->hw.formats = hw->formats;
1248 runtime->hw.period_bytes_min = hw->period_bytes_min;
1249 runtime->hw.period_bytes_max = hw->period_bytes_max;
1250 runtime->hw.periods_min = hw->periods_min;
1251 runtime->hw.periods_max = hw->periods_max;
1252 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1253 runtime->hw.fifo_size = hw->fifo_size;
1254 return 0;
1255}
1256EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1257
1258/**
1259 * snd_soc_cnew - create new control
1260 * @_template: control template
1261 * @data: control private data
1262 * @lnng_name: control long name
1263 *
1264 * Create a new mixer control from a template control.
1265 *
1266 * Returns 0 for success, else error.
1267 */
1268struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1269 void *data, char *long_name)
1270{
1271 struct snd_kcontrol_new template;
1272
1273 memcpy(&template, _template, sizeof(template));
1274 if (long_name)
1275 template.name = long_name;
db2a4165
FM
1276 template.index = 0;
1277
1278 return snd_ctl_new1(&template, data);
1279}
1280EXPORT_SYMBOL_GPL(snd_soc_cnew);
1281
1282/**
1283 * snd_soc_info_enum_double - enumerated double mixer info callback
1284 * @kcontrol: mixer control
1285 * @uinfo: control element information
1286 *
1287 * Callback to provide information about a double enumerated
1288 * mixer control.
1289 *
1290 * Returns 0 for success.
1291 */
1292int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1293 struct snd_ctl_elem_info *uinfo)
1294{
1295 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1296
1297 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1298 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1299 uinfo->value.enumerated.items = e->mask;
1300
1301 if (uinfo->value.enumerated.item > e->mask - 1)
1302 uinfo->value.enumerated.item = e->mask - 1;
1303 strcpy(uinfo->value.enumerated.name,
1304 e->texts[uinfo->value.enumerated.item]);
1305 return 0;
1306}
1307EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1308
1309/**
1310 * snd_soc_get_enum_double - enumerated double mixer get callback
1311 * @kcontrol: mixer control
1312 * @uinfo: control element information
1313 *
1314 * Callback to get the value of a double enumerated mixer.
1315 *
1316 * Returns 0 for success.
1317 */
1318int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1319 struct snd_ctl_elem_value *ucontrol)
1320{
1321 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1322 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1323 unsigned short val, bitmask;
1324
1325 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1326 ;
1327 val = snd_soc_read(codec, e->reg);
3ff3f64b
MB
1328 ucontrol->value.enumerated.item[0]
1329 = (val >> e->shift_l) & (bitmask - 1);
db2a4165
FM
1330 if (e->shift_l != e->shift_r)
1331 ucontrol->value.enumerated.item[1] =
1332 (val >> e->shift_r) & (bitmask - 1);
1333
1334 return 0;
1335}
1336EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1337
1338/**
1339 * snd_soc_put_enum_double - enumerated double mixer put callback
1340 * @kcontrol: mixer control
1341 * @uinfo: control element information
1342 *
1343 * Callback to set the value of a double enumerated mixer.
1344 *
1345 * Returns 0 for success.
1346 */
1347int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1348 struct snd_ctl_elem_value *ucontrol)
1349{
1350 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1351 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1352 unsigned short val;
1353 unsigned short mask, bitmask;
1354
1355 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1356 ;
1357 if (ucontrol->value.enumerated.item[0] > e->mask - 1)
1358 return -EINVAL;
1359 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1360 mask = (bitmask - 1) << e->shift_l;
1361 if (e->shift_l != e->shift_r) {
1362 if (ucontrol->value.enumerated.item[1] > e->mask - 1)
1363 return -EINVAL;
1364 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1365 mask |= (bitmask - 1) << e->shift_r;
1366 }
1367
1368 return snd_soc_update_bits(codec, e->reg, mask, val);
1369}
1370EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1371
1372/**
1373 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1374 * @kcontrol: mixer control
1375 * @uinfo: control element information
1376 *
1377 * Callback to provide information about an external enumerated
1378 * single mixer.
1379 *
1380 * Returns 0 for success.
1381 */
1382int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1383 struct snd_ctl_elem_info *uinfo)
1384{
1385 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1386
1387 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1388 uinfo->count = 1;
1389 uinfo->value.enumerated.items = e->mask;
1390
1391 if (uinfo->value.enumerated.item > e->mask - 1)
1392 uinfo->value.enumerated.item = e->mask - 1;
1393 strcpy(uinfo->value.enumerated.name,
1394 e->texts[uinfo->value.enumerated.item]);
1395 return 0;
1396}
1397EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1398
1399/**
1400 * snd_soc_info_volsw_ext - external single mixer info callback
1401 * @kcontrol: mixer control
1402 * @uinfo: control element information
1403 *
1404 * Callback to provide information about a single external mixer control.
1405 *
1406 * Returns 0 for success.
1407 */
1408int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1409 struct snd_ctl_elem_info *uinfo)
1410{
a7a4ac86
PZ
1411 int max = kcontrol->private_value;
1412
1413 if (max == 1)
1414 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1415 else
1416 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
db2a4165 1417
db2a4165
FM
1418 uinfo->count = 1;
1419 uinfo->value.integer.min = 0;
a7a4ac86 1420 uinfo->value.integer.max = max;
db2a4165
FM
1421 return 0;
1422}
1423EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1424
db2a4165
FM
1425/**
1426 * snd_soc_info_volsw - single mixer info callback
1427 * @kcontrol: mixer control
1428 * @uinfo: control element information
1429 *
1430 * Callback to provide information about a single mixer control.
1431 *
1432 * Returns 0 for success.
1433 */
1434int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1435 struct snd_ctl_elem_info *uinfo)
1436{
a7a4ac86 1437 int max = (kcontrol->private_value >> 16) & 0xff;
db2a4165
FM
1438 int shift = (kcontrol->private_value >> 8) & 0x0f;
1439 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1440
a7a4ac86
PZ
1441 if (max == 1)
1442 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1443 else
1444 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1445
db2a4165
FM
1446 uinfo->count = shift == rshift ? 1 : 2;
1447 uinfo->value.integer.min = 0;
a7a4ac86 1448 uinfo->value.integer.max = max;
db2a4165
FM
1449 return 0;
1450}
1451EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1452
1453/**
1454 * snd_soc_get_volsw - single mixer get callback
1455 * @kcontrol: mixer control
1456 * @uinfo: control element information
1457 *
1458 * Callback to get the value of a single mixer control.
1459 *
1460 * Returns 0 for success.
1461 */
1462int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1463 struct snd_ctl_elem_value *ucontrol)
1464{
1465 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1466 int reg = kcontrol->private_value & 0xff;
1467 int shift = (kcontrol->private_value >> 8) & 0x0f;
1468 int rshift = (kcontrol->private_value >> 12) & 0x0f;
a7a4ac86
PZ
1469 int max = (kcontrol->private_value >> 16) & 0xff;
1470 int mask = (1 << fls(max)) - 1;
db2a4165
FM
1471 int invert = (kcontrol->private_value >> 24) & 0x01;
1472
1473 ucontrol->value.integer.value[0] =
1474 (snd_soc_read(codec, reg) >> shift) & mask;
1475 if (shift != rshift)
1476 ucontrol->value.integer.value[1] =
1477 (snd_soc_read(codec, reg) >> rshift) & mask;
1478 if (invert) {
1479 ucontrol->value.integer.value[0] =
a7a4ac86 1480 max - ucontrol->value.integer.value[0];
db2a4165
FM
1481 if (shift != rshift)
1482 ucontrol->value.integer.value[1] =
a7a4ac86 1483 max - ucontrol->value.integer.value[1];
db2a4165
FM
1484 }
1485
1486 return 0;
1487}
1488EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1489
1490/**
1491 * snd_soc_put_volsw - single mixer put callback
1492 * @kcontrol: mixer control
1493 * @uinfo: control element information
1494 *
1495 * Callback to set the value of a single mixer control.
1496 *
1497 * Returns 0 for success.
1498 */
1499int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1500 struct snd_ctl_elem_value *ucontrol)
1501{
1502 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1503 int reg = kcontrol->private_value & 0xff;
1504 int shift = (kcontrol->private_value >> 8) & 0x0f;
1505 int rshift = (kcontrol->private_value >> 12) & 0x0f;
a7a4ac86
PZ
1506 int max = (kcontrol->private_value >> 16) & 0xff;
1507 int mask = (1 << fls(max)) - 1;
db2a4165 1508 int invert = (kcontrol->private_value >> 24) & 0x01;
db2a4165
FM
1509 unsigned short val, val2, val_mask;
1510
1511 val = (ucontrol->value.integer.value[0] & mask);
1512 if (invert)
a7a4ac86 1513 val = max - val;
db2a4165
FM
1514 val_mask = mask << shift;
1515 val = val << shift;
1516 if (shift != rshift) {
1517 val2 = (ucontrol->value.integer.value[1] & mask);
1518 if (invert)
a7a4ac86 1519 val2 = max - val2;
db2a4165
FM
1520 val_mask |= mask << rshift;
1521 val |= val2 << rshift;
1522 }
a7a4ac86 1523 return snd_soc_update_bits(codec, reg, val_mask, val);
db2a4165
FM
1524}
1525EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1526
1527/**
1528 * snd_soc_info_volsw_2r - double mixer info callback
1529 * @kcontrol: mixer control
1530 * @uinfo: control element information
1531 *
1532 * Callback to provide information about a double mixer control that
1533 * spans 2 codec registers.
1534 *
1535 * Returns 0 for success.
1536 */
1537int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1538 struct snd_ctl_elem_info *uinfo)
1539{
a7a4ac86
PZ
1540 int max = (kcontrol->private_value >> 12) & 0xff;
1541
1542 if (max == 1)
1543 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1544 else
1545 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
db2a4165 1546
db2a4165
FM
1547 uinfo->count = 2;
1548 uinfo->value.integer.min = 0;
a7a4ac86 1549 uinfo->value.integer.max = max;
db2a4165
FM
1550 return 0;
1551}
1552EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1553
1554/**
1555 * snd_soc_get_volsw_2r - double mixer get callback
1556 * @kcontrol: mixer control
1557 * @uinfo: control element information
1558 *
1559 * Callback to get the value of a double mixer control that spans 2 registers.
1560 *
1561 * Returns 0 for success.
1562 */
1563int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1564 struct snd_ctl_elem_value *ucontrol)
1565{
1566 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1567 int reg = kcontrol->private_value & 0xff;
1568 int reg2 = (kcontrol->private_value >> 24) & 0xff;
1569 int shift = (kcontrol->private_value >> 8) & 0x0f;
a7a4ac86
PZ
1570 int max = (kcontrol->private_value >> 12) & 0xff;
1571 int mask = (1<<fls(max))-1;
db2a4165
FM
1572 int invert = (kcontrol->private_value >> 20) & 0x01;
1573
1574 ucontrol->value.integer.value[0] =
1575 (snd_soc_read(codec, reg) >> shift) & mask;
1576 ucontrol->value.integer.value[1] =
1577 (snd_soc_read(codec, reg2) >> shift) & mask;
1578 if (invert) {
1579 ucontrol->value.integer.value[0] =
a7a4ac86 1580 max - ucontrol->value.integer.value[0];
db2a4165 1581 ucontrol->value.integer.value[1] =
a7a4ac86 1582 max - ucontrol->value.integer.value[1];
db2a4165
FM
1583 }
1584
1585 return 0;
1586}
1587EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1588
1589/**
1590 * snd_soc_put_volsw_2r - double mixer set callback
1591 * @kcontrol: mixer control
1592 * @uinfo: control element information
1593 *
1594 * Callback to set the value of a double mixer control that spans 2 registers.
1595 *
1596 * Returns 0 for success.
1597 */
1598int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1599 struct snd_ctl_elem_value *ucontrol)
1600{
1601 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1602 int reg = kcontrol->private_value & 0xff;
1603 int reg2 = (kcontrol->private_value >> 24) & 0xff;
1604 int shift = (kcontrol->private_value >> 8) & 0x0f;
a7a4ac86
PZ
1605 int max = (kcontrol->private_value >> 12) & 0xff;
1606 int mask = (1 << fls(max)) - 1;
db2a4165
FM
1607 int invert = (kcontrol->private_value >> 20) & 0x01;
1608 int err;
1609 unsigned short val, val2, val_mask;
1610
1611 val_mask = mask << shift;
1612 val = (ucontrol->value.integer.value[0] & mask);
1613 val2 = (ucontrol->value.integer.value[1] & mask);
1614
1615 if (invert) {
a7a4ac86
PZ
1616 val = max - val;
1617 val2 = max - val2;
db2a4165
FM
1618 }
1619
1620 val = val << shift;
1621 val2 = val2 << shift;
1622
3ff3f64b
MB
1623 err = snd_soc_update_bits(codec, reg, val_mask, val);
1624 if (err < 0)
db2a4165
FM
1625 return err;
1626
1627 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1628 return err;
1629}
1630EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1631
e13ac2e9
MB
1632/**
1633 * snd_soc_info_volsw_s8 - signed mixer info callback
1634 * @kcontrol: mixer control
1635 * @uinfo: control element information
1636 *
1637 * Callback to provide information about a signed mixer control.
1638 *
1639 * Returns 0 for success.
1640 */
1641int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1642 struct snd_ctl_elem_info *uinfo)
1643{
1644 int max = (signed char)((kcontrol->private_value >> 16) & 0xff);
1645 int min = (signed char)((kcontrol->private_value >> 24) & 0xff);
1646
1647 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1648 uinfo->count = 2;
1649 uinfo->value.integer.min = 0;
1650 uinfo->value.integer.max = max-min;
1651 return 0;
1652}
1653EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1654
1655/**
1656 * snd_soc_get_volsw_s8 - signed mixer get callback
1657 * @kcontrol: mixer control
1658 * @uinfo: control element information
1659 *
1660 * Callback to get the value of a signed mixer control.
1661 *
1662 * Returns 0 for success.
1663 */
1664int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1665 struct snd_ctl_elem_value *ucontrol)
1666{
1667 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1668 int reg = kcontrol->private_value & 0xff;
1669 int min = (signed char)((kcontrol->private_value >> 24) & 0xff);
1670 int val = snd_soc_read(codec, reg);
1671
1672 ucontrol->value.integer.value[0] =
1673 ((signed char)(val & 0xff))-min;
1674 ucontrol->value.integer.value[1] =
1675 ((signed char)((val >> 8) & 0xff))-min;
1676 return 0;
1677}
1678EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
1679
1680/**
1681 * snd_soc_put_volsw_sgn - signed mixer put callback
1682 * @kcontrol: mixer control
1683 * @uinfo: control element information
1684 *
1685 * Callback to set the value of a signed mixer control.
1686 *
1687 * Returns 0 for success.
1688 */
1689int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
1690 struct snd_ctl_elem_value *ucontrol)
1691{
1692 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1693 int reg = kcontrol->private_value & 0xff;
1694 int min = (signed char)((kcontrol->private_value >> 24) & 0xff);
1695 unsigned short val;
1696
1697 val = (ucontrol->value.integer.value[0]+min) & 0xff;
1698 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
1699
1700 return snd_soc_update_bits(codec, reg, 0xffff, val);
1701}
1702EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
1703
db2a4165
FM
1704static int __devinit snd_soc_init(void)
1705{
1706 printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
1707 return platform_driver_register(&soc_driver);
1708}
1709
1710static void snd_soc_exit(void)
1711{
3ff3f64b 1712 platform_driver_unregister(&soc_driver);
db2a4165
FM
1713}
1714
1715module_init(snd_soc_init);
1716module_exit(snd_soc_exit);
1717
1718/* Module information */
1719MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
1720MODULE_DESCRIPTION("ALSA SoC Core");
1721MODULE_LICENSE("GPL");
8b45a209 1722MODULE_ALIAS("platform:soc-audio");