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