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