xref: /linux/sound/soc/soc-core.c (revision d39d0ed196aa1685bb24771e92f78633c66ac9cb)
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 <lrg@slimlogic.co.uk>
8  *         with code, comments and ideas from :-
9  *         Richard Purdie <richard@openedhand.com>
10  *
11  *  This program is free software; you can redistribute  it and/or modify it
12  *  under  the terms of  the GNU General  Public License as published by the
13  *  Free Software Foundation;  either version 2 of the  License, or (at your
14  *  option) any later version.
15  *
16  *  TODO:
17  *   o Add hw rules to enforce rates, etc.
18  *   o More testing with other codecs/machines.
19  *   o Add more codecs and platforms to ensure good API coverage.
20  *   o Support TDM on PCM and I2S
21  */
22 
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/pm.h>
28 #include <linux/bitops.h>
29 #include <linux/debugfs.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
32 #include <sound/ac97_codec.h>
33 #include <sound/core.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/soc.h>
37 #include <sound/soc-dapm.h>
38 #include <sound/initval.h>
39 
40 static DEFINE_MUTEX(pcm_mutex);
41 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
42 
43 #ifdef CONFIG_DEBUG_FS
44 static struct dentry *debugfs_root;
45 #endif
46 
47 static DEFINE_MUTEX(client_mutex);
48 static LIST_HEAD(card_list);
49 static LIST_HEAD(dai_list);
50 static LIST_HEAD(platform_list);
51 static LIST_HEAD(codec_list);
52 
53 static int snd_soc_register_card(struct snd_soc_card *card);
54 static int snd_soc_unregister_card(struct snd_soc_card *card);
55 
56 /*
57  * This is a timeout to do a DAPM powerdown after a stream is closed().
58  * It can be used to eliminate pops between different playback streams, e.g.
59  * between two audio tracks.
60  */
61 static int pmdown_time = 5000;
62 module_param(pmdown_time, int, 0);
63 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
64 
65 /*
66  * This function forces any delayed work to be queued and run.
67  */
68 static int run_delayed_work(struct delayed_work *dwork)
69 {
70 	int ret;
71 
72 	/* cancel any work waiting to be queued. */
73 	ret = cancel_delayed_work(dwork);
74 
75 	/* if there was any work waiting then we run it now and
76 	 * wait for it's completion */
77 	if (ret) {
78 		schedule_delayed_work(dwork, 0);
79 		flush_scheduled_work();
80 	}
81 	return ret;
82 }
83 
84 /* codec register dump */
85 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
86 {
87 	int ret, i, step = 1, count = 0;
88 
89 	if (!codec->reg_cache_size)
90 		return 0;
91 
92 	if (codec->reg_cache_step)
93 		step = codec->reg_cache_step;
94 
95 	count += sprintf(buf, "%s registers\n", codec->name);
96 	for (i = 0; i < codec->reg_cache_size; i += step) {
97 		if (codec->readable_register && !codec->readable_register(i))
98 			continue;
99 
100 		count += sprintf(buf + count, "%2x: ", i);
101 		if (count >= PAGE_SIZE - 1)
102 			break;
103 
104 		if (codec->display_register) {
105 			count += codec->display_register(codec, buf + count,
106 							 PAGE_SIZE - count, i);
107 		} else {
108 			/* If the read fails it's almost certainly due to
109 			 * the register being volatile and the device being
110 			 * powered off.
111 			 */
112 			ret = codec->read(codec, i);
113 			if (ret >= 0)
114 				count += snprintf(buf + count,
115 						  PAGE_SIZE - count,
116 						  "%4x", ret);
117 			else
118 				count += snprintf(buf + count,
119 						  PAGE_SIZE - count,
120 						  "<no data: %d>", ret);
121 		}
122 
123 		if (count >= PAGE_SIZE - 1)
124 			break;
125 
126 		count += snprintf(buf + count, PAGE_SIZE - count, "\n");
127 		if (count >= PAGE_SIZE - 1)
128 			break;
129 	}
130 
131 	/* Truncate count; min() would cause a warning */
132 	if (count >= PAGE_SIZE)
133 		count = PAGE_SIZE - 1;
134 
135 	return count;
136 }
137 static ssize_t codec_reg_show(struct device *dev,
138 	struct device_attribute *attr, char *buf)
139 {
140 	struct snd_soc_device *devdata = dev_get_drvdata(dev);
141 	return soc_codec_reg_show(devdata->card->codec, buf);
142 }
143 
144 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
145 
146 static ssize_t pmdown_time_show(struct device *dev,
147 				struct device_attribute *attr, char *buf)
148 {
149 	struct snd_soc_device *socdev = dev_get_drvdata(dev);
150 	struct snd_soc_card *card = socdev->card;
151 
152 	return sprintf(buf, "%ld\n", card->pmdown_time);
153 }
154 
155 static ssize_t pmdown_time_set(struct device *dev,
156 			       struct device_attribute *attr,
157 			       const char *buf, size_t count)
158 {
159 	struct snd_soc_device *socdev = dev_get_drvdata(dev);
160 	struct snd_soc_card *card = socdev->card;
161 
162 	strict_strtol(buf, 10, &card->pmdown_time);
163 
164 	return count;
165 }
166 
167 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
168 
169 #ifdef CONFIG_DEBUG_FS
170 static int codec_reg_open_file(struct inode *inode, struct file *file)
171 {
172 	file->private_data = inode->i_private;
173 	return 0;
174 }
175 
176 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
177 			       size_t count, loff_t *ppos)
178 {
179 	ssize_t ret;
180 	struct snd_soc_codec *codec = file->private_data;
181 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
182 	if (!buf)
183 		return -ENOMEM;
184 	ret = soc_codec_reg_show(codec, buf);
185 	if (ret >= 0)
186 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
187 	kfree(buf);
188 	return ret;
189 }
190 
191 static ssize_t codec_reg_write_file(struct file *file,
192 		const char __user *user_buf, size_t count, loff_t *ppos)
193 {
194 	char buf[32];
195 	int buf_size;
196 	char *start = buf;
197 	unsigned long reg, value;
198 	int step = 1;
199 	struct snd_soc_codec *codec = file->private_data;
200 
201 	buf_size = min(count, (sizeof(buf)-1));
202 	if (copy_from_user(buf, user_buf, buf_size))
203 		return -EFAULT;
204 	buf[buf_size] = 0;
205 
206 	if (codec->reg_cache_step)
207 		step = codec->reg_cache_step;
208 
209 	while (*start == ' ')
210 		start++;
211 	reg = simple_strtoul(start, &start, 16);
212 	if ((reg >= codec->reg_cache_size) || (reg % step))
213 		return -EINVAL;
214 	while (*start == ' ')
215 		start++;
216 	if (strict_strtoul(start, 16, &value))
217 		return -EINVAL;
218 	codec->write(codec, reg, value);
219 	return buf_size;
220 }
221 
222 static const struct file_operations codec_reg_fops = {
223 	.open = codec_reg_open_file,
224 	.read = codec_reg_read_file,
225 	.write = codec_reg_write_file,
226 };
227 
228 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
229 {
230 	char codec_root[128];
231 
232 	if (codec->dev)
233 		snprintf(codec_root, sizeof(codec_root),
234 			"%s.%s", codec->name, dev_name(codec->dev));
235 	else
236 		snprintf(codec_root, sizeof(codec_root),
237 			"%s", codec->name);
238 
239 	codec->debugfs_codec_root = debugfs_create_dir(codec_root,
240 						       debugfs_root);
241 	if (!codec->debugfs_codec_root) {
242 		printk(KERN_WARNING
243 		       "ASoC: Failed to create codec debugfs directory\n");
244 		return;
245 	}
246 
247 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
248 						 codec->debugfs_codec_root,
249 						 codec, &codec_reg_fops);
250 	if (!codec->debugfs_reg)
251 		printk(KERN_WARNING
252 		       "ASoC: Failed to create codec register debugfs file\n");
253 
254 	codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
255 						     codec->debugfs_codec_root,
256 						     &codec->pop_time);
257 	if (!codec->debugfs_pop_time)
258 		printk(KERN_WARNING
259 		       "Failed to create pop time debugfs file\n");
260 
261 	codec->debugfs_dapm = debugfs_create_dir("dapm",
262 						 codec->debugfs_codec_root);
263 	if (!codec->debugfs_dapm)
264 		printk(KERN_WARNING
265 		       "Failed to create DAPM debugfs directory\n");
266 
267 	snd_soc_dapm_debugfs_init(codec);
268 }
269 
270 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
271 {
272 	debugfs_remove_recursive(codec->debugfs_codec_root);
273 }
274 
275 #else
276 
277 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
278 {
279 }
280 
281 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
282 {
283 }
284 #endif
285 
286 #ifdef CONFIG_SND_SOC_AC97_BUS
287 /* unregister ac97 codec */
288 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
289 {
290 	if (codec->ac97->dev.bus)
291 		device_unregister(&codec->ac97->dev);
292 	return 0;
293 }
294 
295 /* stop no dev release warning */
296 static void soc_ac97_device_release(struct device *dev){}
297 
298 /* register ac97 codec to bus */
299 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
300 {
301 	int err;
302 
303 	codec->ac97->dev.bus = &ac97_bus_type;
304 	codec->ac97->dev.parent = codec->card->dev;
305 	codec->ac97->dev.release = soc_ac97_device_release;
306 
307 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
308 		     codec->card->number, 0, codec->name);
309 	err = device_register(&codec->ac97->dev);
310 	if (err < 0) {
311 		snd_printk(KERN_ERR "Can't register ac97 bus\n");
312 		codec->ac97->dev.bus = NULL;
313 		return err;
314 	}
315 	return 0;
316 }
317 #endif
318 
319 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
320 {
321 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
322 	struct snd_soc_device *socdev = rtd->socdev;
323 	struct snd_soc_card *card = socdev->card;
324 	struct snd_soc_dai_link *machine = rtd->dai;
325 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
326 	struct snd_soc_dai *codec_dai = machine->codec_dai;
327 	int ret;
328 
329 	if (codec_dai->symmetric_rates || cpu_dai->symmetric_rates ||
330 	    machine->symmetric_rates) {
331 		dev_dbg(card->dev, "Symmetry forces %dHz rate\n",
332 			machine->rate);
333 
334 		ret = snd_pcm_hw_constraint_minmax(substream->runtime,
335 						   SNDRV_PCM_HW_PARAM_RATE,
336 						   machine->rate,
337 						   machine->rate);
338 		if (ret < 0) {
339 			dev_err(card->dev,
340 				"Unable to apply rate symmetry constraint: %d\n", ret);
341 			return ret;
342 		}
343 	}
344 
345 	return 0;
346 }
347 
348 /*
349  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
350  * then initialized and any private data can be allocated. This also calls
351  * startup for the cpu DAI, platform, machine and codec DAI.
352  */
353 static int soc_pcm_open(struct snd_pcm_substream *substream)
354 {
355 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
356 	struct snd_soc_device *socdev = rtd->socdev;
357 	struct snd_soc_card *card = socdev->card;
358 	struct snd_pcm_runtime *runtime = substream->runtime;
359 	struct snd_soc_dai_link *machine = rtd->dai;
360 	struct snd_soc_platform *platform = card->platform;
361 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
362 	struct snd_soc_dai *codec_dai = machine->codec_dai;
363 	int ret = 0;
364 
365 	mutex_lock(&pcm_mutex);
366 
367 	/* startup the audio subsystem */
368 	if (cpu_dai->ops->startup) {
369 		ret = cpu_dai->ops->startup(substream, cpu_dai);
370 		if (ret < 0) {
371 			printk(KERN_ERR "asoc: can't open interface %s\n",
372 				cpu_dai->name);
373 			goto out;
374 		}
375 	}
376 
377 	if (platform->pcm_ops->open) {
378 		ret = platform->pcm_ops->open(substream);
379 		if (ret < 0) {
380 			printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
381 			goto platform_err;
382 		}
383 	}
384 
385 	if (codec_dai->ops->startup) {
386 		ret = codec_dai->ops->startup(substream, codec_dai);
387 		if (ret < 0) {
388 			printk(KERN_ERR "asoc: can't open codec %s\n",
389 				codec_dai->name);
390 			goto codec_dai_err;
391 		}
392 	}
393 
394 	if (machine->ops && machine->ops->startup) {
395 		ret = machine->ops->startup(substream);
396 		if (ret < 0) {
397 			printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
398 			goto machine_err;
399 		}
400 	}
401 
402 	/* Check that the codec and cpu DAI's are compatible */
403 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
404 		runtime->hw.rate_min =
405 			max(codec_dai->playback.rate_min,
406 			    cpu_dai->playback.rate_min);
407 		runtime->hw.rate_max =
408 			min(codec_dai->playback.rate_max,
409 			    cpu_dai->playback.rate_max);
410 		runtime->hw.channels_min =
411 			max(codec_dai->playback.channels_min,
412 				cpu_dai->playback.channels_min);
413 		runtime->hw.channels_max =
414 			min(codec_dai->playback.channels_max,
415 				cpu_dai->playback.channels_max);
416 		runtime->hw.formats =
417 			codec_dai->playback.formats & cpu_dai->playback.formats;
418 		runtime->hw.rates =
419 			codec_dai->playback.rates & cpu_dai->playback.rates;
420 		if (codec_dai->playback.rates
421 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
422 			runtime->hw.rates |= cpu_dai->playback.rates;
423 		if (cpu_dai->playback.rates
424 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
425 			runtime->hw.rates |= codec_dai->playback.rates;
426 	} else {
427 		runtime->hw.rate_min =
428 			max(codec_dai->capture.rate_min,
429 			    cpu_dai->capture.rate_min);
430 		runtime->hw.rate_max =
431 			min(codec_dai->capture.rate_max,
432 			    cpu_dai->capture.rate_max);
433 		runtime->hw.channels_min =
434 			max(codec_dai->capture.channels_min,
435 				cpu_dai->capture.channels_min);
436 		runtime->hw.channels_max =
437 			min(codec_dai->capture.channels_max,
438 				cpu_dai->capture.channels_max);
439 		runtime->hw.formats =
440 			codec_dai->capture.formats & cpu_dai->capture.formats;
441 		runtime->hw.rates =
442 			codec_dai->capture.rates & cpu_dai->capture.rates;
443 		if (codec_dai->capture.rates
444 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
445 			runtime->hw.rates |= cpu_dai->capture.rates;
446 		if (cpu_dai->capture.rates
447 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
448 			runtime->hw.rates |= codec_dai->capture.rates;
449 	}
450 
451 	snd_pcm_limit_hw_rates(runtime);
452 	if (!runtime->hw.rates) {
453 		printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
454 			codec_dai->name, cpu_dai->name);
455 		goto config_err;
456 	}
457 	if (!runtime->hw.formats) {
458 		printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
459 			codec_dai->name, cpu_dai->name);
460 		goto config_err;
461 	}
462 	if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
463 		printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
464 			codec_dai->name, cpu_dai->name);
465 		goto config_err;
466 	}
467 
468 	/* Symmetry only applies if we've already got an active stream. */
469 	if (cpu_dai->active || codec_dai->active) {
470 		ret = soc_pcm_apply_symmetry(substream);
471 		if (ret != 0)
472 			goto config_err;
473 	}
474 
475 	pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
476 	pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
477 	pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
478 		 runtime->hw.channels_max);
479 	pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
480 		 runtime->hw.rate_max);
481 
482 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
483 		cpu_dai->playback.active++;
484 		codec_dai->playback.active++;
485 	} else {
486 		cpu_dai->capture.active++;
487 		codec_dai->capture.active++;
488 	}
489 	cpu_dai->active++;
490 	codec_dai->active++;
491 	card->codec->active++;
492 	mutex_unlock(&pcm_mutex);
493 	return 0;
494 
495 config_err:
496 	if (machine->ops && machine->ops->shutdown)
497 		machine->ops->shutdown(substream);
498 
499 machine_err:
500 	if (codec_dai->ops->shutdown)
501 		codec_dai->ops->shutdown(substream, codec_dai);
502 
503 codec_dai_err:
504 	if (platform->pcm_ops->close)
505 		platform->pcm_ops->close(substream);
506 
507 platform_err:
508 	if (cpu_dai->ops->shutdown)
509 		cpu_dai->ops->shutdown(substream, cpu_dai);
510 out:
511 	mutex_unlock(&pcm_mutex);
512 	return ret;
513 }
514 
515 /*
516  * Power down the audio subsystem pmdown_time msecs after close is called.
517  * This is to ensure there are no pops or clicks in between any music tracks
518  * due to DAPM power cycling.
519  */
520 static void close_delayed_work(struct work_struct *work)
521 {
522 	struct snd_soc_card *card = container_of(work, struct snd_soc_card,
523 						 delayed_work.work);
524 	struct snd_soc_codec *codec = card->codec;
525 	struct snd_soc_dai *codec_dai;
526 	int i;
527 
528 	mutex_lock(&pcm_mutex);
529 	for (i = 0; i < codec->num_dai; i++) {
530 		codec_dai = &codec->dai[i];
531 
532 		pr_debug("pop wq checking: %s status: %s waiting: %s\n",
533 			 codec_dai->playback.stream_name,
534 			 codec_dai->playback.active ? "active" : "inactive",
535 			 codec_dai->pop_wait ? "yes" : "no");
536 
537 		/* are we waiting on this codec DAI stream */
538 		if (codec_dai->pop_wait == 1) {
539 			codec_dai->pop_wait = 0;
540 			snd_soc_dapm_stream_event(codec,
541 				codec_dai->playback.stream_name,
542 				SND_SOC_DAPM_STREAM_STOP);
543 		}
544 	}
545 	mutex_unlock(&pcm_mutex);
546 }
547 
548 /*
549  * Called by ALSA when a PCM substream is closed. Private data can be
550  * freed here. The cpu DAI, codec DAI, machine and platform are also
551  * shutdown.
552  */
553 static int soc_codec_close(struct snd_pcm_substream *substream)
554 {
555 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
556 	struct snd_soc_device *socdev = rtd->socdev;
557 	struct snd_soc_card *card = socdev->card;
558 	struct snd_soc_dai_link *machine = rtd->dai;
559 	struct snd_soc_platform *platform = card->platform;
560 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
561 	struct snd_soc_dai *codec_dai = machine->codec_dai;
562 	struct snd_soc_codec *codec = card->codec;
563 
564 	mutex_lock(&pcm_mutex);
565 
566 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
567 		cpu_dai->playback.active--;
568 		codec_dai->playback.active--;
569 	} else {
570 		cpu_dai->capture.active--;
571 		codec_dai->capture.active--;
572 	}
573 
574 	cpu_dai->active--;
575 	codec_dai->active--;
576 	codec->active--;
577 
578 	/* Muting the DAC suppresses artifacts caused during digital
579 	 * shutdown, for example from stopping clocks.
580 	 */
581 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
582 		snd_soc_dai_digital_mute(codec_dai, 1);
583 
584 	if (cpu_dai->ops->shutdown)
585 		cpu_dai->ops->shutdown(substream, cpu_dai);
586 
587 	if (codec_dai->ops->shutdown)
588 		codec_dai->ops->shutdown(substream, codec_dai);
589 
590 	if (machine->ops && machine->ops->shutdown)
591 		machine->ops->shutdown(substream);
592 
593 	if (platform->pcm_ops->close)
594 		platform->pcm_ops->close(substream);
595 
596 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
597 		/* start delayed pop wq here for playback streams */
598 		codec_dai->pop_wait = 1;
599 		schedule_delayed_work(&card->delayed_work,
600 			msecs_to_jiffies(card->pmdown_time));
601 	} else {
602 		/* capture streams can be powered down now */
603 		snd_soc_dapm_stream_event(codec,
604 			codec_dai->capture.stream_name,
605 			SND_SOC_DAPM_STREAM_STOP);
606 	}
607 
608 	mutex_unlock(&pcm_mutex);
609 	return 0;
610 }
611 
612 /*
613  * Called by ALSA when the PCM substream is prepared, can set format, sample
614  * rate, etc.  This function is non atomic and can be called multiple times,
615  * it can refer to the runtime info.
616  */
617 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
618 {
619 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
620 	struct snd_soc_device *socdev = rtd->socdev;
621 	struct snd_soc_card *card = socdev->card;
622 	struct snd_soc_dai_link *machine = rtd->dai;
623 	struct snd_soc_platform *platform = card->platform;
624 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
625 	struct snd_soc_dai *codec_dai = machine->codec_dai;
626 	struct snd_soc_codec *codec = card->codec;
627 	int ret = 0;
628 
629 	mutex_lock(&pcm_mutex);
630 
631 	if (machine->ops && machine->ops->prepare) {
632 		ret = machine->ops->prepare(substream);
633 		if (ret < 0) {
634 			printk(KERN_ERR "asoc: machine prepare error\n");
635 			goto out;
636 		}
637 	}
638 
639 	if (platform->pcm_ops->prepare) {
640 		ret = platform->pcm_ops->prepare(substream);
641 		if (ret < 0) {
642 			printk(KERN_ERR "asoc: platform prepare error\n");
643 			goto out;
644 		}
645 	}
646 
647 	if (codec_dai->ops->prepare) {
648 		ret = codec_dai->ops->prepare(substream, codec_dai);
649 		if (ret < 0) {
650 			printk(KERN_ERR "asoc: codec DAI prepare error\n");
651 			goto out;
652 		}
653 	}
654 
655 	if (cpu_dai->ops->prepare) {
656 		ret = cpu_dai->ops->prepare(substream, cpu_dai);
657 		if (ret < 0) {
658 			printk(KERN_ERR "asoc: cpu DAI prepare error\n");
659 			goto out;
660 		}
661 	}
662 
663 	/* cancel any delayed stream shutdown that is pending */
664 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
665 	    codec_dai->pop_wait) {
666 		codec_dai->pop_wait = 0;
667 		cancel_delayed_work(&card->delayed_work);
668 	}
669 
670 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
671 		snd_soc_dapm_stream_event(codec,
672 					  codec_dai->playback.stream_name,
673 					  SND_SOC_DAPM_STREAM_START);
674 	else
675 		snd_soc_dapm_stream_event(codec,
676 					  codec_dai->capture.stream_name,
677 					  SND_SOC_DAPM_STREAM_START);
678 
679 	snd_soc_dai_digital_mute(codec_dai, 0);
680 
681 out:
682 	mutex_unlock(&pcm_mutex);
683 	return ret;
684 }
685 
686 /*
687  * Called by ALSA when the hardware params are set by application. This
688  * function can also be called multiple times and can allocate buffers
689  * (using snd_pcm_lib_* ). It's non-atomic.
690  */
691 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
692 				struct snd_pcm_hw_params *params)
693 {
694 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
695 	struct snd_soc_device *socdev = rtd->socdev;
696 	struct snd_soc_dai_link *machine = rtd->dai;
697 	struct snd_soc_card *card = socdev->card;
698 	struct snd_soc_platform *platform = card->platform;
699 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
700 	struct snd_soc_dai *codec_dai = machine->codec_dai;
701 	int ret = 0;
702 
703 	mutex_lock(&pcm_mutex);
704 
705 	if (machine->ops && machine->ops->hw_params) {
706 		ret = machine->ops->hw_params(substream, params);
707 		if (ret < 0) {
708 			printk(KERN_ERR "asoc: machine hw_params failed\n");
709 			goto out;
710 		}
711 	}
712 
713 	if (codec_dai->ops->hw_params) {
714 		ret = codec_dai->ops->hw_params(substream, params, codec_dai);
715 		if (ret < 0) {
716 			printk(KERN_ERR "asoc: can't set codec %s hw params\n",
717 				codec_dai->name);
718 			goto codec_err;
719 		}
720 	}
721 
722 	if (cpu_dai->ops->hw_params) {
723 		ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
724 		if (ret < 0) {
725 			printk(KERN_ERR "asoc: interface %s hw params failed\n",
726 				cpu_dai->name);
727 			goto interface_err;
728 		}
729 	}
730 
731 	if (platform->pcm_ops->hw_params) {
732 		ret = platform->pcm_ops->hw_params(substream, params);
733 		if (ret < 0) {
734 			printk(KERN_ERR "asoc: platform %s hw params failed\n",
735 				platform->name);
736 			goto platform_err;
737 		}
738 	}
739 
740 	machine->rate = params_rate(params);
741 
742 out:
743 	mutex_unlock(&pcm_mutex);
744 	return ret;
745 
746 platform_err:
747 	if (cpu_dai->ops->hw_free)
748 		cpu_dai->ops->hw_free(substream, cpu_dai);
749 
750 interface_err:
751 	if (codec_dai->ops->hw_free)
752 		codec_dai->ops->hw_free(substream, codec_dai);
753 
754 codec_err:
755 	if (machine->ops && machine->ops->hw_free)
756 		machine->ops->hw_free(substream);
757 
758 	mutex_unlock(&pcm_mutex);
759 	return ret;
760 }
761 
762 /*
763  * Free's resources allocated by hw_params, can be called multiple times
764  */
765 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
766 {
767 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
768 	struct snd_soc_device *socdev = rtd->socdev;
769 	struct snd_soc_dai_link *machine = rtd->dai;
770 	struct snd_soc_card *card = socdev->card;
771 	struct snd_soc_platform *platform = card->platform;
772 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
773 	struct snd_soc_dai *codec_dai = machine->codec_dai;
774 	struct snd_soc_codec *codec = card->codec;
775 
776 	mutex_lock(&pcm_mutex);
777 
778 	/* apply codec digital mute */
779 	if (!codec->active)
780 		snd_soc_dai_digital_mute(codec_dai, 1);
781 
782 	/* free any machine hw params */
783 	if (machine->ops && machine->ops->hw_free)
784 		machine->ops->hw_free(substream);
785 
786 	/* free any DMA resources */
787 	if (platform->pcm_ops->hw_free)
788 		platform->pcm_ops->hw_free(substream);
789 
790 	/* now free hw params for the DAI's  */
791 	if (codec_dai->ops->hw_free)
792 		codec_dai->ops->hw_free(substream, codec_dai);
793 
794 	if (cpu_dai->ops->hw_free)
795 		cpu_dai->ops->hw_free(substream, cpu_dai);
796 
797 	mutex_unlock(&pcm_mutex);
798 	return 0;
799 }
800 
801 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
802 {
803 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
804 	struct snd_soc_device *socdev = rtd->socdev;
805 	struct snd_soc_card *card= socdev->card;
806 	struct snd_soc_dai_link *machine = rtd->dai;
807 	struct snd_soc_platform *platform = card->platform;
808 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
809 	struct snd_soc_dai *codec_dai = machine->codec_dai;
810 	int ret;
811 
812 	if (codec_dai->ops->trigger) {
813 		ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
814 		if (ret < 0)
815 			return ret;
816 	}
817 
818 	if (platform->pcm_ops->trigger) {
819 		ret = platform->pcm_ops->trigger(substream, cmd);
820 		if (ret < 0)
821 			return ret;
822 	}
823 
824 	if (cpu_dai->ops->trigger) {
825 		ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
826 		if (ret < 0)
827 			return ret;
828 	}
829 	return 0;
830 }
831 
832 /*
833  * soc level wrapper for pointer callback
834  * If cpu_dai, codec_dai, platform driver has the delay callback, than
835  * the runtime->delay will be updated accordingly.
836  */
837 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
838 {
839 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
840 	struct snd_soc_device *socdev = rtd->socdev;
841 	struct snd_soc_card *card = socdev->card;
842 	struct snd_soc_platform *platform = card->platform;
843 	struct snd_soc_dai_link *machine = rtd->dai;
844 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
845 	struct snd_soc_dai *codec_dai = machine->codec_dai;
846 	struct snd_pcm_runtime *runtime = substream->runtime;
847 	snd_pcm_uframes_t offset = 0;
848 	snd_pcm_sframes_t delay = 0;
849 
850 	if (platform->pcm_ops->pointer)
851 		offset = platform->pcm_ops->pointer(substream);
852 
853 	if (cpu_dai->ops->delay)
854 		delay += cpu_dai->ops->delay(substream, cpu_dai);
855 
856 	if (codec_dai->ops->delay)
857 		delay += codec_dai->ops->delay(substream, codec_dai);
858 
859 	if (platform->delay)
860 		delay += platform->delay(substream, codec_dai);
861 
862 	runtime->delay = delay;
863 
864 	return offset;
865 }
866 
867 /* ASoC PCM operations */
868 static struct snd_pcm_ops soc_pcm_ops = {
869 	.open		= soc_pcm_open,
870 	.close		= soc_codec_close,
871 	.hw_params	= soc_pcm_hw_params,
872 	.hw_free	= soc_pcm_hw_free,
873 	.prepare	= soc_pcm_prepare,
874 	.trigger	= soc_pcm_trigger,
875 	.pointer	= soc_pcm_pointer,
876 };
877 
878 #ifdef CONFIG_PM
879 /* powers down audio subsystem for suspend */
880 static int soc_suspend(struct device *dev)
881 {
882 	struct platform_device *pdev = to_platform_device(dev);
883 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
884 	struct snd_soc_card *card = socdev->card;
885 	struct snd_soc_platform *platform = card->platform;
886 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
887 	struct snd_soc_codec *codec = card->codec;
888 	int i;
889 
890 	/* If the initialization of this soc device failed, there is no codec
891 	 * associated with it. Just bail out in this case.
892 	 */
893 	if (!codec)
894 		return 0;
895 
896 	/* Due to the resume being scheduled into a workqueue we could
897 	* suspend before that's finished - wait for it to complete.
898 	 */
899 	snd_power_lock(codec->card);
900 	snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
901 	snd_power_unlock(codec->card);
902 
903 	/* we're going to block userspace touching us until resume completes */
904 	snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
905 
906 	/* mute any active DAC's */
907 	for (i = 0; i < card->num_links; i++) {
908 		struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
909 
910 		if (card->dai_link[i].ignore_suspend)
911 			continue;
912 
913 		if (dai->ops->digital_mute && dai->playback.active)
914 			dai->ops->digital_mute(dai, 1);
915 	}
916 
917 	/* suspend all pcms */
918 	for (i = 0; i < card->num_links; i++) {
919 		if (card->dai_link[i].ignore_suspend)
920 			continue;
921 
922 		snd_pcm_suspend_all(card->dai_link[i].pcm);
923 	}
924 
925 	if (card->suspend_pre)
926 		card->suspend_pre(pdev, PMSG_SUSPEND);
927 
928 	for (i = 0; i < card->num_links; i++) {
929 		struct snd_soc_dai  *cpu_dai = card->dai_link[i].cpu_dai;
930 
931 		if (card->dai_link[i].ignore_suspend)
932 			continue;
933 
934 		if (cpu_dai->suspend && !cpu_dai->ac97_control)
935 			cpu_dai->suspend(cpu_dai);
936 		if (platform->suspend)
937 			platform->suspend(&card->dai_link[i]);
938 	}
939 
940 	/* close any waiting streams and save state */
941 	run_delayed_work(&card->delayed_work);
942 	codec->suspend_bias_level = codec->bias_level;
943 
944 	for (i = 0; i < codec->num_dai; i++) {
945 		char *stream = codec->dai[i].playback.stream_name;
946 
947 		if (card->dai_link[i].ignore_suspend)
948 			continue;
949 
950 		if (stream != NULL)
951 			snd_soc_dapm_stream_event(codec, stream,
952 				SND_SOC_DAPM_STREAM_SUSPEND);
953 		stream = codec->dai[i].capture.stream_name;
954 		if (stream != NULL)
955 			snd_soc_dapm_stream_event(codec, stream,
956 				SND_SOC_DAPM_STREAM_SUSPEND);
957 	}
958 
959 	/* If there are paths active then the CODEC will be held with
960 	 * bias _ON and should not be suspended. */
961 	if (codec_dev->suspend) {
962 		switch (codec->bias_level) {
963 		case SND_SOC_BIAS_STANDBY:
964 		case SND_SOC_BIAS_OFF:
965 			codec_dev->suspend(pdev, PMSG_SUSPEND);
966 			break;
967 		default:
968 			dev_dbg(socdev->dev, "CODEC is on over suspend\n");
969 			break;
970 		}
971 	}
972 
973 	for (i = 0; i < card->num_links; i++) {
974 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
975 
976 		if (card->dai_link[i].ignore_suspend)
977 			continue;
978 
979 		if (cpu_dai->suspend && cpu_dai->ac97_control)
980 			cpu_dai->suspend(cpu_dai);
981 	}
982 
983 	if (card->suspend_post)
984 		card->suspend_post(pdev, PMSG_SUSPEND);
985 
986 	return 0;
987 }
988 
989 /* deferred resume work, so resume can complete before we finished
990  * setting our codec back up, which can be very slow on I2C
991  */
992 static void soc_resume_deferred(struct work_struct *work)
993 {
994 	struct snd_soc_card *card = container_of(work,
995 						 struct snd_soc_card,
996 						 deferred_resume_work);
997 	struct snd_soc_device *socdev = card->socdev;
998 	struct snd_soc_platform *platform = card->platform;
999 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1000 	struct snd_soc_codec *codec = card->codec;
1001 	struct platform_device *pdev = to_platform_device(socdev->dev);
1002 	int i;
1003 
1004 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1005 	 * so userspace apps are blocked from touching us
1006 	 */
1007 
1008 	dev_dbg(socdev->dev, "starting resume work\n");
1009 
1010 	/* Bring us up into D2 so that DAPM starts enabling things */
1011 	snd_power_change_state(codec->card, SNDRV_CTL_POWER_D2);
1012 
1013 	if (card->resume_pre)
1014 		card->resume_pre(pdev);
1015 
1016 	for (i = 0; i < card->num_links; i++) {
1017 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1018 
1019 		if (card->dai_link[i].ignore_suspend)
1020 			continue;
1021 
1022 		if (cpu_dai->resume && cpu_dai->ac97_control)
1023 			cpu_dai->resume(cpu_dai);
1024 	}
1025 
1026 	/* If the CODEC was idle over suspend then it will have been
1027 	 * left with bias OFF or STANDBY and suspended so we must now
1028 	 * resume.  Otherwise the suspend was suppressed.
1029 	 */
1030 	if (codec_dev->resume) {
1031 		switch (codec->bias_level) {
1032 		case SND_SOC_BIAS_STANDBY:
1033 		case SND_SOC_BIAS_OFF:
1034 			codec_dev->resume(pdev);
1035 			break;
1036 		default:
1037 			dev_dbg(socdev->dev, "CODEC was on over suspend\n");
1038 			break;
1039 		}
1040 	}
1041 
1042 	for (i = 0; i < codec->num_dai; i++) {
1043 		char *stream = codec->dai[i].playback.stream_name;
1044 
1045 		if (card->dai_link[i].ignore_suspend)
1046 			continue;
1047 
1048 		if (stream != NULL)
1049 			snd_soc_dapm_stream_event(codec, stream,
1050 				SND_SOC_DAPM_STREAM_RESUME);
1051 		stream = codec->dai[i].capture.stream_name;
1052 		if (stream != NULL)
1053 			snd_soc_dapm_stream_event(codec, stream,
1054 				SND_SOC_DAPM_STREAM_RESUME);
1055 	}
1056 
1057 	/* unmute any active DACs */
1058 	for (i = 0; i < card->num_links; i++) {
1059 		struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
1060 
1061 		if (card->dai_link[i].ignore_suspend)
1062 			continue;
1063 
1064 		if (dai->ops->digital_mute && dai->playback.active)
1065 			dai->ops->digital_mute(dai, 0);
1066 	}
1067 
1068 	for (i = 0; i < card->num_links; i++) {
1069 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1070 
1071 		if (card->dai_link[i].ignore_suspend)
1072 			continue;
1073 
1074 		if (cpu_dai->resume && !cpu_dai->ac97_control)
1075 			cpu_dai->resume(cpu_dai);
1076 		if (platform->resume)
1077 			platform->resume(&card->dai_link[i]);
1078 	}
1079 
1080 	if (card->resume_post)
1081 		card->resume_post(pdev);
1082 
1083 	dev_dbg(socdev->dev, "resume work completed\n");
1084 
1085 	/* userspace can access us now we are back as we were before */
1086 	snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
1087 }
1088 
1089 /* powers up audio subsystem after a suspend */
1090 static int soc_resume(struct device *dev)
1091 {
1092 	struct platform_device *pdev = to_platform_device(dev);
1093 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1094 	struct snd_soc_card *card = socdev->card;
1095 	struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
1096 
1097 	/* If the initialization of this soc device failed, there is no codec
1098 	 * associated with it. Just bail out in this case.
1099 	 */
1100 	if (!card->codec)
1101 		return 0;
1102 
1103 	/* AC97 devices might have other drivers hanging off them so
1104 	 * need to resume immediately.  Other drivers don't have that
1105 	 * problem and may take a substantial amount of time to resume
1106 	 * due to I/O costs and anti-pop so handle them out of line.
1107 	 */
1108 	if (cpu_dai->ac97_control) {
1109 		dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
1110 		soc_resume_deferred(&card->deferred_resume_work);
1111 	} else {
1112 		dev_dbg(socdev->dev, "Scheduling resume work\n");
1113 		if (!schedule_work(&card->deferred_resume_work))
1114 			dev_err(socdev->dev, "resume work item may be lost\n");
1115 	}
1116 
1117 	return 0;
1118 }
1119 #else
1120 #define soc_suspend	NULL
1121 #define soc_resume	NULL
1122 #endif
1123 
1124 static struct snd_soc_dai_ops null_dai_ops = {
1125 };
1126 
1127 static void snd_soc_instantiate_card(struct snd_soc_card *card)
1128 {
1129 	struct platform_device *pdev = container_of(card->dev,
1130 						    struct platform_device,
1131 						    dev);
1132 	struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
1133 	struct snd_soc_codec *codec;
1134 	struct snd_soc_platform *platform;
1135 	struct snd_soc_dai *dai;
1136 	int i, found, ret, ac97;
1137 
1138 	if (card->instantiated)
1139 		return;
1140 
1141 	found = 0;
1142 	list_for_each_entry(platform, &platform_list, list)
1143 		if (card->platform == platform) {
1144 			found = 1;
1145 			break;
1146 		}
1147 	if (!found) {
1148 		dev_dbg(card->dev, "Platform %s not registered\n",
1149 			card->platform->name);
1150 		return;
1151 	}
1152 
1153 	ac97 = 0;
1154 	for (i = 0; i < card->num_links; i++) {
1155 		found = 0;
1156 		list_for_each_entry(dai, &dai_list, list)
1157 			if (card->dai_link[i].cpu_dai == dai) {
1158 				found = 1;
1159 				break;
1160 			}
1161 		if (!found) {
1162 			dev_dbg(card->dev, "DAI %s not registered\n",
1163 				card->dai_link[i].cpu_dai->name);
1164 			return;
1165 		}
1166 
1167 		if (card->dai_link[i].cpu_dai->ac97_control)
1168 			ac97 = 1;
1169 	}
1170 
1171 	for (i = 0; i < card->num_links; i++) {
1172 		if (!card->dai_link[i].codec_dai->ops)
1173 			card->dai_link[i].codec_dai->ops = &null_dai_ops;
1174 	}
1175 
1176 	/* If we have AC97 in the system then don't wait for the
1177 	 * codec.  This will need revisiting if we have to handle
1178 	 * systems with mixed AC97 and non-AC97 parts.  Only check for
1179 	 * DAIs currently; we can't do this per link since some AC97
1180 	 * codecs have non-AC97 DAIs.
1181 	 */
1182 	if (!ac97)
1183 		for (i = 0; i < card->num_links; i++) {
1184 			found = 0;
1185 			list_for_each_entry(dai, &dai_list, list)
1186 				if (card->dai_link[i].codec_dai == dai) {
1187 					found = 1;
1188 					break;
1189 				}
1190 			if (!found) {
1191 				dev_dbg(card->dev, "DAI %s not registered\n",
1192 					card->dai_link[i].codec_dai->name);
1193 				return;
1194 			}
1195 		}
1196 
1197 	/* Note that we do not current check for codec components */
1198 
1199 	dev_dbg(card->dev, "All components present, instantiating\n");
1200 
1201 	/* Found everything, bring it up */
1202 	card->pmdown_time = pmdown_time;
1203 
1204 	if (card->probe) {
1205 		ret = card->probe(pdev);
1206 		if (ret < 0)
1207 			return;
1208 	}
1209 
1210 	for (i = 0; i < card->num_links; i++) {
1211 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1212 		if (cpu_dai->probe) {
1213 			ret = cpu_dai->probe(pdev, cpu_dai);
1214 			if (ret < 0)
1215 				goto cpu_dai_err;
1216 		}
1217 	}
1218 
1219 	if (codec_dev->probe) {
1220 		ret = codec_dev->probe(pdev);
1221 		if (ret < 0)
1222 			goto cpu_dai_err;
1223 	}
1224 	codec = card->codec;
1225 
1226 	if (platform->probe) {
1227 		ret = platform->probe(pdev);
1228 		if (ret < 0)
1229 			goto platform_err;
1230 	}
1231 
1232 	/* DAPM stream work */
1233 	INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
1234 #ifdef CONFIG_PM
1235 	/* deferred resume work */
1236 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1237 #endif
1238 
1239 	for (i = 0; i < card->num_links; i++) {
1240 		if (card->dai_link[i].init) {
1241 			ret = card->dai_link[i].init(codec);
1242 			if (ret < 0) {
1243 				printk(KERN_ERR "asoc: failed to init %s\n",
1244 					card->dai_link[i].stream_name);
1245 				continue;
1246 			}
1247 		}
1248 		if (card->dai_link[i].codec_dai->ac97_control)
1249 			ac97 = 1;
1250 	}
1251 
1252 	snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1253 		 "%s",  card->name);
1254 	snprintf(codec->card->longname, sizeof(codec->card->longname),
1255 		 "%s (%s)", card->name, codec->name);
1256 
1257 	/* Make sure all DAPM widgets are instantiated */
1258 	snd_soc_dapm_new_widgets(codec);
1259 
1260 	ret = snd_card_register(codec->card);
1261 	if (ret < 0) {
1262 		printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1263 				codec->name);
1264 		goto card_err;
1265 	}
1266 
1267 	mutex_lock(&codec->mutex);
1268 #ifdef CONFIG_SND_SOC_AC97_BUS
1269 	/* Only instantiate AC97 if not already done by the adaptor
1270 	 * for the generic AC97 subsystem.
1271 	 */
1272 	if (ac97 && strcmp(codec->name, "AC97") != 0) {
1273 		ret = soc_ac97_dev_register(codec);
1274 		if (ret < 0) {
1275 			printk(KERN_ERR "asoc: AC97 device register failed\n");
1276 			snd_card_free(codec->card);
1277 			mutex_unlock(&codec->mutex);
1278 			goto card_err;
1279 		}
1280 	}
1281 #endif
1282 
1283 	ret = snd_soc_dapm_sys_add(card->socdev->dev);
1284 	if (ret < 0)
1285 		printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1286 
1287 	ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time);
1288 	if (ret < 0)
1289 		printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1290 
1291 	ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg);
1292 	if (ret < 0)
1293 		printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1294 
1295 	soc_init_codec_debugfs(codec);
1296 	mutex_unlock(&codec->mutex);
1297 
1298 	card->instantiated = 1;
1299 
1300 	return;
1301 
1302 card_err:
1303 	if (platform->remove)
1304 		platform->remove(pdev);
1305 
1306 platform_err:
1307 	if (codec_dev->remove)
1308 		codec_dev->remove(pdev);
1309 
1310 cpu_dai_err:
1311 	for (i--; i >= 0; i--) {
1312 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1313 		if (cpu_dai->remove)
1314 			cpu_dai->remove(pdev, cpu_dai);
1315 	}
1316 
1317 	if (card->remove)
1318 		card->remove(pdev);
1319 }
1320 
1321 /*
1322  * Attempt to initialise any uninitialised cards.  Must be called with
1323  * client_mutex.
1324  */
1325 static void snd_soc_instantiate_cards(void)
1326 {
1327 	struct snd_soc_card *card;
1328 	list_for_each_entry(card, &card_list, list)
1329 		snd_soc_instantiate_card(card);
1330 }
1331 
1332 /* probes a new socdev */
1333 static int soc_probe(struct platform_device *pdev)
1334 {
1335 	int ret = 0;
1336 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1337 	struct snd_soc_card *card = socdev->card;
1338 
1339 	/* Bodge while we push things out of socdev */
1340 	card->socdev = socdev;
1341 
1342 	/* Bodge while we unpick instantiation */
1343 	card->dev = &pdev->dev;
1344 	ret = snd_soc_register_card(card);
1345 	if (ret != 0) {
1346 		dev_err(&pdev->dev, "Failed to register card\n");
1347 		return ret;
1348 	}
1349 
1350 	return 0;
1351 }
1352 
1353 /* removes a socdev */
1354 static int soc_remove(struct platform_device *pdev)
1355 {
1356 	int i;
1357 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1358 	struct snd_soc_card *card = socdev->card;
1359 	struct snd_soc_platform *platform = card->platform;
1360 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1361 
1362 	if (card->instantiated) {
1363 		run_delayed_work(&card->delayed_work);
1364 
1365 		if (platform->remove)
1366 			platform->remove(pdev);
1367 
1368 		if (codec_dev->remove)
1369 			codec_dev->remove(pdev);
1370 
1371 		for (i = 0; i < card->num_links; i++) {
1372 			struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1373 			if (cpu_dai->remove)
1374 				cpu_dai->remove(pdev, cpu_dai);
1375 		}
1376 
1377 		if (card->remove)
1378 			card->remove(pdev);
1379 	}
1380 
1381 	snd_soc_unregister_card(card);
1382 
1383 	return 0;
1384 }
1385 
1386 static int soc_poweroff(struct device *dev)
1387 {
1388 	struct platform_device *pdev = to_platform_device(dev);
1389 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1390 	struct snd_soc_card *card = socdev->card;
1391 
1392 	if (!card->instantiated)
1393 		return 0;
1394 
1395 	/* Flush out pmdown_time work - we actually do want to run it
1396 	 * now, we're shutting down so no imminent restart. */
1397 	run_delayed_work(&card->delayed_work);
1398 
1399 	snd_soc_dapm_shutdown(socdev);
1400 
1401 	return 0;
1402 }
1403 
1404 static const struct dev_pm_ops soc_pm_ops = {
1405 	.suspend = soc_suspend,
1406 	.resume = soc_resume,
1407 	.poweroff = soc_poweroff,
1408 };
1409 
1410 /* ASoC platform driver */
1411 static struct platform_driver soc_driver = {
1412 	.driver		= {
1413 		.name		= "soc-audio",
1414 		.owner		= THIS_MODULE,
1415 		.pm		= &soc_pm_ops,
1416 	},
1417 	.probe		= soc_probe,
1418 	.remove		= soc_remove,
1419 };
1420 
1421 /* create a new pcm */
1422 static int soc_new_pcm(struct snd_soc_device *socdev,
1423 	struct snd_soc_dai_link *dai_link, int num)
1424 {
1425 	struct snd_soc_card *card = socdev->card;
1426 	struct snd_soc_codec *codec = card->codec;
1427 	struct snd_soc_platform *platform = card->platform;
1428 	struct snd_soc_dai *codec_dai = dai_link->codec_dai;
1429 	struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
1430 	struct snd_soc_pcm_runtime *rtd;
1431 	struct snd_pcm *pcm;
1432 	char new_name[64];
1433 	int ret = 0, playback = 0, capture = 0;
1434 
1435 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1436 	if (rtd == NULL)
1437 		return -ENOMEM;
1438 
1439 	rtd->dai = dai_link;
1440 	rtd->socdev = socdev;
1441 	codec_dai->codec = card->codec;
1442 
1443 	/* check client and interface hw capabilities */
1444 	snprintf(new_name, sizeof(new_name), "%s %s-%d",
1445 		 dai_link->stream_name, codec_dai->name, num);
1446 
1447 	if (codec_dai->playback.channels_min)
1448 		playback = 1;
1449 	if (codec_dai->capture.channels_min)
1450 		capture = 1;
1451 
1452 	ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1453 		capture, &pcm);
1454 	if (ret < 0) {
1455 		printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1456 			codec->name);
1457 		kfree(rtd);
1458 		return ret;
1459 	}
1460 
1461 	dai_link->pcm = pcm;
1462 	pcm->private_data = rtd;
1463 	soc_pcm_ops.mmap = platform->pcm_ops->mmap;
1464 	soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1465 	soc_pcm_ops.copy = platform->pcm_ops->copy;
1466 	soc_pcm_ops.silence = platform->pcm_ops->silence;
1467 	soc_pcm_ops.ack = platform->pcm_ops->ack;
1468 	soc_pcm_ops.page = platform->pcm_ops->page;
1469 
1470 	if (playback)
1471 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1472 
1473 	if (capture)
1474 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1475 
1476 	ret = platform->pcm_new(codec->card, codec_dai, pcm);
1477 	if (ret < 0) {
1478 		printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1479 		kfree(rtd);
1480 		return ret;
1481 	}
1482 
1483 	pcm->private_free = platform->pcm_free;
1484 	printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1485 		cpu_dai->name);
1486 	return ret;
1487 }
1488 
1489 /**
1490  * snd_soc_codec_volatile_register: Report if a register is volatile.
1491  *
1492  * @codec: CODEC to query.
1493  * @reg: Register to query.
1494  *
1495  * Boolean function indiciating if a CODEC register is volatile.
1496  */
1497 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1498 {
1499 	if (codec->volatile_register)
1500 		return codec->volatile_register(reg);
1501 	else
1502 		return 0;
1503 }
1504 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1505 
1506 /**
1507  * snd_soc_new_ac97_codec - initailise AC97 device
1508  * @codec: audio codec
1509  * @ops: AC97 bus operations
1510  * @num: AC97 codec number
1511  *
1512  * Initialises AC97 codec resources for use by ad-hoc devices only.
1513  */
1514 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1515 	struct snd_ac97_bus_ops *ops, int num)
1516 {
1517 	mutex_lock(&codec->mutex);
1518 
1519 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1520 	if (codec->ac97 == NULL) {
1521 		mutex_unlock(&codec->mutex);
1522 		return -ENOMEM;
1523 	}
1524 
1525 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1526 	if (codec->ac97->bus == NULL) {
1527 		kfree(codec->ac97);
1528 		codec->ac97 = NULL;
1529 		mutex_unlock(&codec->mutex);
1530 		return -ENOMEM;
1531 	}
1532 
1533 	codec->ac97->bus->ops = ops;
1534 	codec->ac97->num = num;
1535 	codec->dev = &codec->ac97->dev;
1536 	mutex_unlock(&codec->mutex);
1537 	return 0;
1538 }
1539 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1540 
1541 /**
1542  * snd_soc_free_ac97_codec - free AC97 codec device
1543  * @codec: audio codec
1544  *
1545  * Frees AC97 codec device resources.
1546  */
1547 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1548 {
1549 	mutex_lock(&codec->mutex);
1550 	kfree(codec->ac97->bus);
1551 	kfree(codec->ac97);
1552 	codec->ac97 = NULL;
1553 	mutex_unlock(&codec->mutex);
1554 }
1555 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1556 
1557 /**
1558  * snd_soc_update_bits - update codec register bits
1559  * @codec: audio codec
1560  * @reg: codec register
1561  * @mask: register mask
1562  * @value: new value
1563  *
1564  * Writes new register value.
1565  *
1566  * Returns 1 for change else 0.
1567  */
1568 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1569 				unsigned int mask, unsigned int value)
1570 {
1571 	int change;
1572 	unsigned int old, new;
1573 
1574 	old = snd_soc_read(codec, reg);
1575 	new = (old & ~mask) | value;
1576 	change = old != new;
1577 	if (change)
1578 		snd_soc_write(codec, reg, new);
1579 
1580 	return change;
1581 }
1582 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1583 
1584 /**
1585  * snd_soc_update_bits_locked - update codec register bits
1586  * @codec: audio codec
1587  * @reg: codec register
1588  * @mask: register mask
1589  * @value: new value
1590  *
1591  * Writes new register value, and takes the codec mutex.
1592  *
1593  * Returns 1 for change else 0.
1594  */
1595 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1596 			       unsigned short reg, unsigned int mask,
1597 			       unsigned int value)
1598 {
1599 	int change;
1600 
1601 	mutex_lock(&codec->mutex);
1602 	change = snd_soc_update_bits(codec, reg, mask, value);
1603 	mutex_unlock(&codec->mutex);
1604 
1605 	return change;
1606 }
1607 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
1608 
1609 /**
1610  * snd_soc_test_bits - test register for change
1611  * @codec: audio codec
1612  * @reg: codec register
1613  * @mask: register mask
1614  * @value: new value
1615  *
1616  * Tests a register with a new value and checks if the new value is
1617  * different from the old value.
1618  *
1619  * Returns 1 for change else 0.
1620  */
1621 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1622 				unsigned int mask, unsigned int value)
1623 {
1624 	int change;
1625 	unsigned int old, new;
1626 
1627 	old = snd_soc_read(codec, reg);
1628 	new = (old & ~mask) | value;
1629 	change = old != new;
1630 
1631 	return change;
1632 }
1633 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1634 
1635 /**
1636  * snd_soc_new_pcms - create new sound card and pcms
1637  * @socdev: the SoC audio device
1638  * @idx: ALSA card index
1639  * @xid: card identification
1640  *
1641  * Create a new sound card based upon the codec and interface pcms.
1642  *
1643  * Returns 0 for success, else error.
1644  */
1645 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1646 {
1647 	struct snd_soc_card *card = socdev->card;
1648 	struct snd_soc_codec *codec = card->codec;
1649 	int ret, i;
1650 
1651 	mutex_lock(&codec->mutex);
1652 
1653 	/* register a sound card */
1654 	ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1655 	if (ret < 0) {
1656 		printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1657 			codec->name);
1658 		mutex_unlock(&codec->mutex);
1659 		return ret;
1660 	}
1661 
1662 	codec->socdev = socdev;
1663 	codec->card->dev = socdev->dev;
1664 	codec->card->private_data = codec;
1665 	strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1666 
1667 	/* create the pcms */
1668 	for (i = 0; i < card->num_links; i++) {
1669 		ret = soc_new_pcm(socdev, &card->dai_link[i], i);
1670 		if (ret < 0) {
1671 			printk(KERN_ERR "asoc: can't create pcm %s\n",
1672 				card->dai_link[i].stream_name);
1673 			mutex_unlock(&codec->mutex);
1674 			return ret;
1675 		}
1676 		/* Check for codec->ac97 to handle the ac97.c fun */
1677 		if (card->dai_link[i].codec_dai->ac97_control && codec->ac97) {
1678 			snd_ac97_dev_add_pdata(codec->ac97,
1679 				card->dai_link[i].cpu_dai->ac97_pdata);
1680 		}
1681 	}
1682 
1683 	mutex_unlock(&codec->mutex);
1684 	return ret;
1685 }
1686 EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1687 
1688 /**
1689  * snd_soc_free_pcms - free sound card and pcms
1690  * @socdev: the SoC audio device
1691  *
1692  * Frees sound card and pcms associated with the socdev.
1693  * Also unregister the codec if it is an AC97 device.
1694  */
1695 void snd_soc_free_pcms(struct snd_soc_device *socdev)
1696 {
1697 	struct snd_soc_codec *codec = socdev->card->codec;
1698 #ifdef CONFIG_SND_SOC_AC97_BUS
1699 	struct snd_soc_dai *codec_dai;
1700 	int i;
1701 #endif
1702 
1703 	mutex_lock(&codec->mutex);
1704 	soc_cleanup_codec_debugfs(codec);
1705 #ifdef CONFIG_SND_SOC_AC97_BUS
1706 	for (i = 0; i < codec->num_dai; i++) {
1707 		codec_dai = &codec->dai[i];
1708 		if (codec_dai->ac97_control && codec->ac97 &&
1709 		    strcmp(codec->name, "AC97") != 0) {
1710 			soc_ac97_dev_unregister(codec);
1711 			goto free_card;
1712 		}
1713 	}
1714 free_card:
1715 #endif
1716 
1717 	if (codec->card)
1718 		snd_card_free(codec->card);
1719 	device_remove_file(socdev->dev, &dev_attr_codec_reg);
1720 	mutex_unlock(&codec->mutex);
1721 }
1722 EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1723 
1724 /**
1725  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1726  * @substream: the pcm substream
1727  * @hw: the hardware parameters
1728  *
1729  * Sets the substream runtime hardware parameters.
1730  */
1731 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1732 	const struct snd_pcm_hardware *hw)
1733 {
1734 	struct snd_pcm_runtime *runtime = substream->runtime;
1735 	runtime->hw.info = hw->info;
1736 	runtime->hw.formats = hw->formats;
1737 	runtime->hw.period_bytes_min = hw->period_bytes_min;
1738 	runtime->hw.period_bytes_max = hw->period_bytes_max;
1739 	runtime->hw.periods_min = hw->periods_min;
1740 	runtime->hw.periods_max = hw->periods_max;
1741 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1742 	runtime->hw.fifo_size = hw->fifo_size;
1743 	return 0;
1744 }
1745 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1746 
1747 /**
1748  * snd_soc_cnew - create new control
1749  * @_template: control template
1750  * @data: control private data
1751  * @long_name: control long name
1752  *
1753  * Create a new mixer control from a template control.
1754  *
1755  * Returns 0 for success, else error.
1756  */
1757 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1758 	void *data, char *long_name)
1759 {
1760 	struct snd_kcontrol_new template;
1761 
1762 	memcpy(&template, _template, sizeof(template));
1763 	if (long_name)
1764 		template.name = long_name;
1765 	template.index = 0;
1766 
1767 	return snd_ctl_new1(&template, data);
1768 }
1769 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1770 
1771 /**
1772  * snd_soc_add_controls - add an array of controls to a codec.
1773  * Convienience function to add a list of controls. Many codecs were
1774  * duplicating this code.
1775  *
1776  * @codec: codec to add controls to
1777  * @controls: array of controls to add
1778  * @num_controls: number of elements in the array
1779  *
1780  * Return 0 for success, else error.
1781  */
1782 int snd_soc_add_controls(struct snd_soc_codec *codec,
1783 	const struct snd_kcontrol_new *controls, int num_controls)
1784 {
1785 	struct snd_card *card = codec->card;
1786 	int err, i;
1787 
1788 	for (i = 0; i < num_controls; i++) {
1789 		const struct snd_kcontrol_new *control = &controls[i];
1790 		err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1791 		if (err < 0) {
1792 			dev_err(codec->dev, "%s: Failed to add %s\n",
1793 				codec->name, control->name);
1794 			return err;
1795 		}
1796 	}
1797 
1798 	return 0;
1799 }
1800 EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1801 
1802 /**
1803  * snd_soc_info_enum_double - enumerated double mixer info callback
1804  * @kcontrol: mixer control
1805  * @uinfo: control element information
1806  *
1807  * Callback to provide information about a double enumerated
1808  * mixer control.
1809  *
1810  * Returns 0 for success.
1811  */
1812 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1813 	struct snd_ctl_elem_info *uinfo)
1814 {
1815 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1816 
1817 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1818 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1819 	uinfo->value.enumerated.items = e->max;
1820 
1821 	if (uinfo->value.enumerated.item > e->max - 1)
1822 		uinfo->value.enumerated.item = e->max - 1;
1823 	strcpy(uinfo->value.enumerated.name,
1824 		e->texts[uinfo->value.enumerated.item]);
1825 	return 0;
1826 }
1827 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1828 
1829 /**
1830  * snd_soc_get_enum_double - enumerated double mixer get callback
1831  * @kcontrol: mixer control
1832  * @ucontrol: control element information
1833  *
1834  * Callback to get the value of a double enumerated mixer.
1835  *
1836  * Returns 0 for success.
1837  */
1838 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1839 	struct snd_ctl_elem_value *ucontrol)
1840 {
1841 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1842 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1843 	unsigned int val, bitmask;
1844 
1845 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1846 		;
1847 	val = snd_soc_read(codec, e->reg);
1848 	ucontrol->value.enumerated.item[0]
1849 		= (val >> e->shift_l) & (bitmask - 1);
1850 	if (e->shift_l != e->shift_r)
1851 		ucontrol->value.enumerated.item[1] =
1852 			(val >> e->shift_r) & (bitmask - 1);
1853 
1854 	return 0;
1855 }
1856 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1857 
1858 /**
1859  * snd_soc_put_enum_double - enumerated double mixer put callback
1860  * @kcontrol: mixer control
1861  * @ucontrol: control element information
1862  *
1863  * Callback to set the value of a double enumerated mixer.
1864  *
1865  * Returns 0 for success.
1866  */
1867 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1868 	struct snd_ctl_elem_value *ucontrol)
1869 {
1870 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1871 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1872 	unsigned int val;
1873 	unsigned int mask, bitmask;
1874 
1875 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1876 		;
1877 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
1878 		return -EINVAL;
1879 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
1880 	mask = (bitmask - 1) << e->shift_l;
1881 	if (e->shift_l != e->shift_r) {
1882 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
1883 			return -EINVAL;
1884 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1885 		mask |= (bitmask - 1) << e->shift_r;
1886 	}
1887 
1888 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
1889 }
1890 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1891 
1892 /**
1893  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
1894  * @kcontrol: mixer control
1895  * @ucontrol: control element information
1896  *
1897  * Callback to get the value of a double semi enumerated mixer.
1898  *
1899  * Semi enumerated mixer: the enumerated items are referred as values. Can be
1900  * used for handling bitfield coded enumeration for example.
1901  *
1902  * Returns 0 for success.
1903  */
1904 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
1905 	struct snd_ctl_elem_value *ucontrol)
1906 {
1907 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1908 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1909 	unsigned int reg_val, val, mux;
1910 
1911 	reg_val = snd_soc_read(codec, e->reg);
1912 	val = (reg_val >> e->shift_l) & e->mask;
1913 	for (mux = 0; mux < e->max; mux++) {
1914 		if (val == e->values[mux])
1915 			break;
1916 	}
1917 	ucontrol->value.enumerated.item[0] = mux;
1918 	if (e->shift_l != e->shift_r) {
1919 		val = (reg_val >> e->shift_r) & e->mask;
1920 		for (mux = 0; mux < e->max; mux++) {
1921 			if (val == e->values[mux])
1922 				break;
1923 		}
1924 		ucontrol->value.enumerated.item[1] = mux;
1925 	}
1926 
1927 	return 0;
1928 }
1929 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
1930 
1931 /**
1932  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
1933  * @kcontrol: mixer control
1934  * @ucontrol: control element information
1935  *
1936  * Callback to set the value of a double semi enumerated mixer.
1937  *
1938  * Semi enumerated mixer: the enumerated items are referred as values. Can be
1939  * used for handling bitfield coded enumeration for example.
1940  *
1941  * Returns 0 for success.
1942  */
1943 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
1944 	struct snd_ctl_elem_value *ucontrol)
1945 {
1946 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1947 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1948 	unsigned int val;
1949 	unsigned int mask;
1950 
1951 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
1952 		return -EINVAL;
1953 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1954 	mask = e->mask << e->shift_l;
1955 	if (e->shift_l != e->shift_r) {
1956 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
1957 			return -EINVAL;
1958 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1959 		mask |= e->mask << e->shift_r;
1960 	}
1961 
1962 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
1963 }
1964 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
1965 
1966 /**
1967  * snd_soc_info_enum_ext - external enumerated single mixer info callback
1968  * @kcontrol: mixer control
1969  * @uinfo: control element information
1970  *
1971  * Callback to provide information about an external enumerated
1972  * single mixer.
1973  *
1974  * Returns 0 for success.
1975  */
1976 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1977 	struct snd_ctl_elem_info *uinfo)
1978 {
1979 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1980 
1981 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1982 	uinfo->count = 1;
1983 	uinfo->value.enumerated.items = e->max;
1984 
1985 	if (uinfo->value.enumerated.item > e->max - 1)
1986 		uinfo->value.enumerated.item = e->max - 1;
1987 	strcpy(uinfo->value.enumerated.name,
1988 		e->texts[uinfo->value.enumerated.item]);
1989 	return 0;
1990 }
1991 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1992 
1993 /**
1994  * snd_soc_info_volsw_ext - external single mixer info callback
1995  * @kcontrol: mixer control
1996  * @uinfo: control element information
1997  *
1998  * Callback to provide information about a single external mixer control.
1999  *
2000  * Returns 0 for success.
2001  */
2002 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2003 	struct snd_ctl_elem_info *uinfo)
2004 {
2005 	int max = kcontrol->private_value;
2006 
2007 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2008 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2009 	else
2010 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2011 
2012 	uinfo->count = 1;
2013 	uinfo->value.integer.min = 0;
2014 	uinfo->value.integer.max = max;
2015 	return 0;
2016 }
2017 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2018 
2019 /**
2020  * snd_soc_info_volsw - single mixer info callback
2021  * @kcontrol: mixer control
2022  * @uinfo: control element information
2023  *
2024  * Callback to provide information about a single mixer control.
2025  *
2026  * Returns 0 for success.
2027  */
2028 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2029 	struct snd_ctl_elem_info *uinfo)
2030 {
2031 	struct soc_mixer_control *mc =
2032 		(struct soc_mixer_control *)kcontrol->private_value;
2033 	int platform_max;
2034 	unsigned int shift = mc->shift;
2035 	unsigned int rshift = mc->rshift;
2036 
2037 	if (!mc->platform_max)
2038 		mc->platform_max = mc->max;
2039 	platform_max = mc->platform_max;
2040 
2041 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2042 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2043 	else
2044 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2045 
2046 	uinfo->count = shift == rshift ? 1 : 2;
2047 	uinfo->value.integer.min = 0;
2048 	uinfo->value.integer.max = platform_max;
2049 	return 0;
2050 }
2051 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2052 
2053 /**
2054  * snd_soc_get_volsw - single mixer get callback
2055  * @kcontrol: mixer control
2056  * @ucontrol: control element information
2057  *
2058  * Callback to get the value of a single mixer control.
2059  *
2060  * Returns 0 for success.
2061  */
2062 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2063 	struct snd_ctl_elem_value *ucontrol)
2064 {
2065 	struct soc_mixer_control *mc =
2066 		(struct soc_mixer_control *)kcontrol->private_value;
2067 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2068 	unsigned int reg = mc->reg;
2069 	unsigned int shift = mc->shift;
2070 	unsigned int rshift = mc->rshift;
2071 	int max = mc->max;
2072 	unsigned int mask = (1 << fls(max)) - 1;
2073 	unsigned int invert = mc->invert;
2074 
2075 	ucontrol->value.integer.value[0] =
2076 		(snd_soc_read(codec, reg) >> shift) & mask;
2077 	if (shift != rshift)
2078 		ucontrol->value.integer.value[1] =
2079 			(snd_soc_read(codec, reg) >> rshift) & mask;
2080 	if (invert) {
2081 		ucontrol->value.integer.value[0] =
2082 			max - ucontrol->value.integer.value[0];
2083 		if (shift != rshift)
2084 			ucontrol->value.integer.value[1] =
2085 				max - ucontrol->value.integer.value[1];
2086 	}
2087 
2088 	return 0;
2089 }
2090 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2091 
2092 /**
2093  * snd_soc_put_volsw - single mixer put callback
2094  * @kcontrol: mixer control
2095  * @ucontrol: control element information
2096  *
2097  * Callback to set the value of a single mixer control.
2098  *
2099  * Returns 0 for success.
2100  */
2101 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2102 	struct snd_ctl_elem_value *ucontrol)
2103 {
2104 	struct soc_mixer_control *mc =
2105 		(struct soc_mixer_control *)kcontrol->private_value;
2106 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2107 	unsigned int reg = mc->reg;
2108 	unsigned int shift = mc->shift;
2109 	unsigned int rshift = mc->rshift;
2110 	int max = mc->max;
2111 	unsigned int mask = (1 << fls(max)) - 1;
2112 	unsigned int invert = mc->invert;
2113 	unsigned int val, val2, val_mask;
2114 
2115 	val = (ucontrol->value.integer.value[0] & mask);
2116 	if (invert)
2117 		val = max - val;
2118 	val_mask = mask << shift;
2119 	val = val << shift;
2120 	if (shift != rshift) {
2121 		val2 = (ucontrol->value.integer.value[1] & mask);
2122 		if (invert)
2123 			val2 = max - val2;
2124 		val_mask |= mask << rshift;
2125 		val |= val2 << rshift;
2126 	}
2127 	return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2128 }
2129 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2130 
2131 /**
2132  * snd_soc_info_volsw_2r - double mixer info callback
2133  * @kcontrol: mixer control
2134  * @uinfo: control element information
2135  *
2136  * Callback to provide information about a double mixer control that
2137  * spans 2 codec registers.
2138  *
2139  * Returns 0 for success.
2140  */
2141 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2142 	struct snd_ctl_elem_info *uinfo)
2143 {
2144 	struct soc_mixer_control *mc =
2145 		(struct soc_mixer_control *)kcontrol->private_value;
2146 	int platform_max;
2147 
2148 	if (!mc->platform_max)
2149 		mc->platform_max = mc->max;
2150 	platform_max = mc->platform_max;
2151 
2152 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2153 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2154 	else
2155 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2156 
2157 	uinfo->count = 2;
2158 	uinfo->value.integer.min = 0;
2159 	uinfo->value.integer.max = platform_max;
2160 	return 0;
2161 }
2162 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2163 
2164 /**
2165  * snd_soc_get_volsw_2r - double mixer get callback
2166  * @kcontrol: mixer control
2167  * @ucontrol: control element information
2168  *
2169  * Callback to get the value of a double mixer control that spans 2 registers.
2170  *
2171  * Returns 0 for success.
2172  */
2173 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2174 	struct snd_ctl_elem_value *ucontrol)
2175 {
2176 	struct soc_mixer_control *mc =
2177 		(struct soc_mixer_control *)kcontrol->private_value;
2178 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2179 	unsigned int reg = mc->reg;
2180 	unsigned int reg2 = mc->rreg;
2181 	unsigned int shift = mc->shift;
2182 	int max = mc->max;
2183 	unsigned int mask = (1 << fls(max)) - 1;
2184 	unsigned int invert = mc->invert;
2185 
2186 	ucontrol->value.integer.value[0] =
2187 		(snd_soc_read(codec, reg) >> shift) & mask;
2188 	ucontrol->value.integer.value[1] =
2189 		(snd_soc_read(codec, reg2) >> shift) & mask;
2190 	if (invert) {
2191 		ucontrol->value.integer.value[0] =
2192 			max - ucontrol->value.integer.value[0];
2193 		ucontrol->value.integer.value[1] =
2194 			max - ucontrol->value.integer.value[1];
2195 	}
2196 
2197 	return 0;
2198 }
2199 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2200 
2201 /**
2202  * snd_soc_put_volsw_2r - double mixer set callback
2203  * @kcontrol: mixer control
2204  * @ucontrol: control element information
2205  *
2206  * Callback to set the value of a double mixer control that spans 2 registers.
2207  *
2208  * Returns 0 for success.
2209  */
2210 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2211 	struct snd_ctl_elem_value *ucontrol)
2212 {
2213 	struct soc_mixer_control *mc =
2214 		(struct soc_mixer_control *)kcontrol->private_value;
2215 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2216 	unsigned int reg = mc->reg;
2217 	unsigned int reg2 = mc->rreg;
2218 	unsigned int shift = mc->shift;
2219 	int max = mc->max;
2220 	unsigned int mask = (1 << fls(max)) - 1;
2221 	unsigned int invert = mc->invert;
2222 	int err;
2223 	unsigned int val, val2, val_mask;
2224 
2225 	val_mask = mask << shift;
2226 	val = (ucontrol->value.integer.value[0] & mask);
2227 	val2 = (ucontrol->value.integer.value[1] & mask);
2228 
2229 	if (invert) {
2230 		val = max - val;
2231 		val2 = max - val2;
2232 	}
2233 
2234 	val = val << shift;
2235 	val2 = val2 << shift;
2236 
2237 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2238 	if (err < 0)
2239 		return err;
2240 
2241 	err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2242 	return err;
2243 }
2244 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2245 
2246 /**
2247  * snd_soc_info_volsw_s8 - signed mixer info callback
2248  * @kcontrol: mixer control
2249  * @uinfo: control element information
2250  *
2251  * Callback to provide information about a signed mixer control.
2252  *
2253  * Returns 0 for success.
2254  */
2255 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2256 	struct snd_ctl_elem_info *uinfo)
2257 {
2258 	struct soc_mixer_control *mc =
2259 		(struct soc_mixer_control *)kcontrol->private_value;
2260 	int platform_max;
2261 	int min = mc->min;
2262 
2263 	if (!mc->platform_max)
2264 		mc->platform_max = mc->max;
2265 	platform_max = mc->platform_max;
2266 
2267 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2268 	uinfo->count = 2;
2269 	uinfo->value.integer.min = 0;
2270 	uinfo->value.integer.max = platform_max - min;
2271 	return 0;
2272 }
2273 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2274 
2275 /**
2276  * snd_soc_get_volsw_s8 - signed mixer get callback
2277  * @kcontrol: mixer control
2278  * @ucontrol: control element information
2279  *
2280  * Callback to get the value of a signed mixer control.
2281  *
2282  * Returns 0 for success.
2283  */
2284 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2285 	struct snd_ctl_elem_value *ucontrol)
2286 {
2287 	struct soc_mixer_control *mc =
2288 		(struct soc_mixer_control *)kcontrol->private_value;
2289 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2290 	unsigned int reg = mc->reg;
2291 	int min = mc->min;
2292 	int val = snd_soc_read(codec, reg);
2293 
2294 	ucontrol->value.integer.value[0] =
2295 		((signed char)(val & 0xff))-min;
2296 	ucontrol->value.integer.value[1] =
2297 		((signed char)((val >> 8) & 0xff))-min;
2298 	return 0;
2299 }
2300 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2301 
2302 /**
2303  * snd_soc_put_volsw_sgn - signed mixer put callback
2304  * @kcontrol: mixer control
2305  * @ucontrol: control element information
2306  *
2307  * Callback to set the value of a signed mixer control.
2308  *
2309  * Returns 0 for success.
2310  */
2311 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2312 	struct snd_ctl_elem_value *ucontrol)
2313 {
2314 	struct soc_mixer_control *mc =
2315 		(struct soc_mixer_control *)kcontrol->private_value;
2316 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2317 	unsigned int reg = mc->reg;
2318 	int min = mc->min;
2319 	unsigned int val;
2320 
2321 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2322 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2323 
2324 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2325 }
2326 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2327 
2328 /**
2329  * snd_soc_limit_volume - Set new limit to an existing volume control.
2330  *
2331  * @codec: where to look for the control
2332  * @name: Name of the control
2333  * @max: new maximum limit
2334  *
2335  * Return 0 for success, else error.
2336  */
2337 int snd_soc_limit_volume(struct snd_soc_codec *codec,
2338 	const char *name, int max)
2339 {
2340 	struct snd_card *card = codec->card;
2341 	struct snd_kcontrol *kctl;
2342 	struct soc_mixer_control *mc;
2343 	int found = 0;
2344 	int ret = -EINVAL;
2345 
2346 	/* Sanity check for name and max */
2347 	if (unlikely(!name || max <= 0))
2348 		return -EINVAL;
2349 
2350 	list_for_each_entry(kctl, &card->controls, list) {
2351 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2352 			found = 1;
2353 			break;
2354 		}
2355 	}
2356 	if (found) {
2357 		mc = (struct soc_mixer_control *)kctl->private_value;
2358 		if (max <= mc->max) {
2359 			mc->platform_max = max;
2360 			ret = 0;
2361 		}
2362 	}
2363 	return ret;
2364 }
2365 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2366 
2367 /**
2368  * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2369  *  mixer info callback
2370  * @kcontrol: mixer control
2371  * @uinfo: control element information
2372  *
2373  * Returns 0 for success.
2374  */
2375 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2376 			struct snd_ctl_elem_info *uinfo)
2377 {
2378 	struct soc_mixer_control *mc =
2379 		(struct soc_mixer_control *)kcontrol->private_value;
2380 	int max = mc->max;
2381 	int min = mc->min;
2382 
2383 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2384 	uinfo->count = 2;
2385 	uinfo->value.integer.min = 0;
2386 	uinfo->value.integer.max = max-min;
2387 
2388 	return 0;
2389 }
2390 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2391 
2392 /**
2393  * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2394  *  mixer get callback
2395  * @kcontrol: mixer control
2396  * @uinfo: control element information
2397  *
2398  * Returns 0 for success.
2399  */
2400 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2401 			struct snd_ctl_elem_value *ucontrol)
2402 {
2403 	struct soc_mixer_control *mc =
2404 		(struct soc_mixer_control *)kcontrol->private_value;
2405 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2406 	unsigned int mask = (1<<mc->shift)-1;
2407 	int min = mc->min;
2408 	int val = snd_soc_read(codec, mc->reg) & mask;
2409 	int valr = snd_soc_read(codec, mc->rreg) & mask;
2410 
2411 	ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2412 	ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2413 	return 0;
2414 }
2415 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2416 
2417 /**
2418  * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2419  *  mixer put callback
2420  * @kcontrol: mixer control
2421  * @uinfo: control element information
2422  *
2423  * Returns 0 for success.
2424  */
2425 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2426 			struct snd_ctl_elem_value *ucontrol)
2427 {
2428 	struct soc_mixer_control *mc =
2429 		(struct soc_mixer_control *)kcontrol->private_value;
2430 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2431 	unsigned int mask = (1<<mc->shift)-1;
2432 	int min = mc->min;
2433 	int ret;
2434 	unsigned int val, valr, oval, ovalr;
2435 
2436 	val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2437 	val &= mask;
2438 	valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2439 	valr &= mask;
2440 
2441 	oval = snd_soc_read(codec, mc->reg) & mask;
2442 	ovalr = snd_soc_read(codec, mc->rreg) & mask;
2443 
2444 	ret = 0;
2445 	if (oval != val) {
2446 		ret = snd_soc_write(codec, mc->reg, val);
2447 		if (ret < 0)
2448 			return ret;
2449 	}
2450 	if (ovalr != valr) {
2451 		ret = snd_soc_write(codec, mc->rreg, valr);
2452 		if (ret < 0)
2453 			return ret;
2454 	}
2455 
2456 	return 0;
2457 }
2458 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2459 
2460 /**
2461  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2462  * @dai: DAI
2463  * @clk_id: DAI specific clock ID
2464  * @freq: new clock frequency in Hz
2465  * @dir: new clock direction - input/output.
2466  *
2467  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2468  */
2469 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2470 	unsigned int freq, int dir)
2471 {
2472 	if (dai->ops && dai->ops->set_sysclk)
2473 		return dai->ops->set_sysclk(dai, clk_id, freq, dir);
2474 	else
2475 		return -EINVAL;
2476 }
2477 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2478 
2479 /**
2480  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2481  * @dai: DAI
2482  * @div_id: DAI specific clock divider ID
2483  * @div: new clock divisor.
2484  *
2485  * Configures the clock dividers. This is used to derive the best DAI bit and
2486  * frame clocks from the system or master clock. It's best to set the DAI bit
2487  * and frame clocks as low as possible to save system power.
2488  */
2489 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2490 	int div_id, int div)
2491 {
2492 	if (dai->ops && dai->ops->set_clkdiv)
2493 		return dai->ops->set_clkdiv(dai, div_id, div);
2494 	else
2495 		return -EINVAL;
2496 }
2497 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2498 
2499 /**
2500  * snd_soc_dai_set_pll - configure DAI PLL.
2501  * @dai: DAI
2502  * @pll_id: DAI specific PLL ID
2503  * @source: DAI specific source for the PLL
2504  * @freq_in: PLL input clock frequency in Hz
2505  * @freq_out: requested PLL output clock frequency in Hz
2506  *
2507  * Configures and enables PLL to generate output clock based on input clock.
2508  */
2509 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2510 	unsigned int freq_in, unsigned int freq_out)
2511 {
2512 	if (dai->ops && dai->ops->set_pll)
2513 		return dai->ops->set_pll(dai, pll_id, source,
2514 					 freq_in, freq_out);
2515 	else
2516 		return -EINVAL;
2517 }
2518 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2519 
2520 /**
2521  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2522  * @dai: DAI
2523  * @fmt: SND_SOC_DAIFMT_ format value.
2524  *
2525  * Configures the DAI hardware format and clocking.
2526  */
2527 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2528 {
2529 	if (dai->ops && dai->ops->set_fmt)
2530 		return dai->ops->set_fmt(dai, fmt);
2531 	else
2532 		return -EINVAL;
2533 }
2534 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2535 
2536 /**
2537  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2538  * @dai: DAI
2539  * @tx_mask: bitmask representing active TX slots.
2540  * @rx_mask: bitmask representing active RX slots.
2541  * @slots: Number of slots in use.
2542  * @slot_width: Width in bits for each slot.
2543  *
2544  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2545  * specific.
2546  */
2547 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2548 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2549 {
2550 	if (dai->ops && dai->ops->set_tdm_slot)
2551 		return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2552 				slots, slot_width);
2553 	else
2554 		return -EINVAL;
2555 }
2556 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2557 
2558 /**
2559  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2560  * @dai: DAI
2561  * @tx_num: how many TX channels
2562  * @tx_slot: pointer to an array which imply the TX slot number channel
2563  *           0~num-1 uses
2564  * @rx_num: how many RX channels
2565  * @rx_slot: pointer to an array which imply the RX slot number channel
2566  *           0~num-1 uses
2567  *
2568  * configure the relationship between channel number and TDM slot number.
2569  */
2570 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2571 	unsigned int tx_num, unsigned int *tx_slot,
2572 	unsigned int rx_num, unsigned int *rx_slot)
2573 {
2574 	if (dai->ops && dai->ops->set_channel_map)
2575 		return dai->ops->set_channel_map(dai, tx_num, tx_slot,
2576 			rx_num, rx_slot);
2577 	else
2578 		return -EINVAL;
2579 }
2580 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2581 
2582 /**
2583  * snd_soc_dai_set_tristate - configure DAI system or master clock.
2584  * @dai: DAI
2585  * @tristate: tristate enable
2586  *
2587  * Tristates the DAI so that others can use it.
2588  */
2589 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2590 {
2591 	if (dai->ops && dai->ops->set_tristate)
2592 		return dai->ops->set_tristate(dai, tristate);
2593 	else
2594 		return -EINVAL;
2595 }
2596 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2597 
2598 /**
2599  * snd_soc_dai_digital_mute - configure DAI system or master clock.
2600  * @dai: DAI
2601  * @mute: mute enable
2602  *
2603  * Mutes the DAI DAC.
2604  */
2605 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2606 {
2607 	if (dai->ops && dai->ops->digital_mute)
2608 		return dai->ops->digital_mute(dai, mute);
2609 	else
2610 		return -EINVAL;
2611 }
2612 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2613 
2614 /**
2615  * snd_soc_register_card - Register a card with the ASoC core
2616  *
2617  * @card: Card to register
2618  *
2619  * Note that currently this is an internal only function: it will be
2620  * exposed to machine drivers after further backporting of ASoC v2
2621  * registration APIs.
2622  */
2623 static int snd_soc_register_card(struct snd_soc_card *card)
2624 {
2625 	if (!card->name || !card->dev)
2626 		return -EINVAL;
2627 
2628 	INIT_LIST_HEAD(&card->list);
2629 	card->instantiated = 0;
2630 
2631 	mutex_lock(&client_mutex);
2632 	list_add(&card->list, &card_list);
2633 	snd_soc_instantiate_cards();
2634 	mutex_unlock(&client_mutex);
2635 
2636 	dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2637 
2638 	return 0;
2639 }
2640 
2641 /**
2642  * snd_soc_unregister_card - Unregister a card with the ASoC core
2643  *
2644  * @card: Card to unregister
2645  *
2646  * Note that currently this is an internal only function: it will be
2647  * exposed to machine drivers after further backporting of ASoC v2
2648  * registration APIs.
2649  */
2650 static int snd_soc_unregister_card(struct snd_soc_card *card)
2651 {
2652 	mutex_lock(&client_mutex);
2653 	list_del(&card->list);
2654 	mutex_unlock(&client_mutex);
2655 
2656 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2657 
2658 	return 0;
2659 }
2660 
2661 /**
2662  * snd_soc_register_dai - Register a DAI with the ASoC core
2663  *
2664  * @dai: DAI to register
2665  */
2666 int snd_soc_register_dai(struct snd_soc_dai *dai)
2667 {
2668 	if (!dai->name)
2669 		return -EINVAL;
2670 
2671 	/* The device should become mandatory over time */
2672 	if (!dai->dev)
2673 		printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2674 
2675 	if (!dai->ops)
2676 		dai->ops = &null_dai_ops;
2677 
2678 	INIT_LIST_HEAD(&dai->list);
2679 
2680 	mutex_lock(&client_mutex);
2681 	list_add(&dai->list, &dai_list);
2682 	snd_soc_instantiate_cards();
2683 	mutex_unlock(&client_mutex);
2684 
2685 	pr_debug("Registered DAI '%s'\n", dai->name);
2686 
2687 	return 0;
2688 }
2689 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2690 
2691 /**
2692  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2693  *
2694  * @dai: DAI to unregister
2695  */
2696 void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2697 {
2698 	mutex_lock(&client_mutex);
2699 	list_del(&dai->list);
2700 	mutex_unlock(&client_mutex);
2701 
2702 	pr_debug("Unregistered DAI '%s'\n", dai->name);
2703 }
2704 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2705 
2706 /**
2707  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2708  *
2709  * @dai: Array of DAIs to register
2710  * @count: Number of DAIs
2711  */
2712 int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2713 {
2714 	int i, ret;
2715 
2716 	for (i = 0; i < count; i++) {
2717 		ret = snd_soc_register_dai(&dai[i]);
2718 		if (ret != 0)
2719 			goto err;
2720 	}
2721 
2722 	return 0;
2723 
2724 err:
2725 	for (i--; i >= 0; i--)
2726 		snd_soc_unregister_dai(&dai[i]);
2727 
2728 	return ret;
2729 }
2730 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2731 
2732 /**
2733  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2734  *
2735  * @dai: Array of DAIs to unregister
2736  * @count: Number of DAIs
2737  */
2738 void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2739 {
2740 	int i;
2741 
2742 	for (i = 0; i < count; i++)
2743 		snd_soc_unregister_dai(&dai[i]);
2744 }
2745 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2746 
2747 /**
2748  * snd_soc_register_platform - Register a platform with the ASoC core
2749  *
2750  * @platform: platform to register
2751  */
2752 int snd_soc_register_platform(struct snd_soc_platform *platform)
2753 {
2754 	if (!platform->name)
2755 		return -EINVAL;
2756 
2757 	INIT_LIST_HEAD(&platform->list);
2758 
2759 	mutex_lock(&client_mutex);
2760 	list_add(&platform->list, &platform_list);
2761 	snd_soc_instantiate_cards();
2762 	mutex_unlock(&client_mutex);
2763 
2764 	pr_debug("Registered platform '%s'\n", platform->name);
2765 
2766 	return 0;
2767 }
2768 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2769 
2770 /**
2771  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2772  *
2773  * @platform: platform to unregister
2774  */
2775 void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2776 {
2777 	mutex_lock(&client_mutex);
2778 	list_del(&platform->list);
2779 	mutex_unlock(&client_mutex);
2780 
2781 	pr_debug("Unregistered platform '%s'\n", platform->name);
2782 }
2783 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2784 
2785 static u64 codec_format_map[] = {
2786 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2787 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2788 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2789 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2790 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2791 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2792 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2793 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2794 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2795 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2796 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2797 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2798 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2799 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2800 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2801 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2802 };
2803 
2804 /* Fix up the DAI formats for endianness: codecs don't actually see
2805  * the endianness of the data but we're using the CPU format
2806  * definitions which do need to include endianness so we ensure that
2807  * codec DAIs always have both big and little endian variants set.
2808  */
2809 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2810 {
2811 	int i;
2812 
2813 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2814 		if (stream->formats & codec_format_map[i])
2815 			stream->formats |= codec_format_map[i];
2816 }
2817 
2818 /**
2819  * snd_soc_register_codec - Register a codec with the ASoC core
2820  *
2821  * @codec: codec to register
2822  */
2823 int snd_soc_register_codec(struct snd_soc_codec *codec)
2824 {
2825 	int i;
2826 
2827 	if (!codec->name)
2828 		return -EINVAL;
2829 
2830 	/* The device should become mandatory over time */
2831 	if (!codec->dev)
2832 		printk(KERN_WARNING "No device for codec %s\n", codec->name);
2833 
2834 	INIT_LIST_HEAD(&codec->list);
2835 
2836 	for (i = 0; i < codec->num_dai; i++) {
2837 		fixup_codec_formats(&codec->dai[i].playback);
2838 		fixup_codec_formats(&codec->dai[i].capture);
2839 	}
2840 
2841 	mutex_lock(&client_mutex);
2842 	list_add(&codec->list, &codec_list);
2843 	snd_soc_instantiate_cards();
2844 	mutex_unlock(&client_mutex);
2845 
2846 	pr_debug("Registered codec '%s'\n", codec->name);
2847 
2848 	return 0;
2849 }
2850 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
2851 
2852 /**
2853  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
2854  *
2855  * @codec: codec to unregister
2856  */
2857 void snd_soc_unregister_codec(struct snd_soc_codec *codec)
2858 {
2859 	mutex_lock(&client_mutex);
2860 	list_del(&codec->list);
2861 	mutex_unlock(&client_mutex);
2862 
2863 	pr_debug("Unregistered codec '%s'\n", codec->name);
2864 }
2865 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
2866 
2867 static int __init snd_soc_init(void)
2868 {
2869 #ifdef CONFIG_DEBUG_FS
2870 	debugfs_root = debugfs_create_dir("asoc", NULL);
2871 	if (IS_ERR(debugfs_root) || !debugfs_root) {
2872 		printk(KERN_WARNING
2873 		       "ASoC: Failed to create debugfs directory\n");
2874 		debugfs_root = NULL;
2875 	}
2876 #endif
2877 
2878 	return platform_driver_register(&soc_driver);
2879 }
2880 
2881 static void __exit snd_soc_exit(void)
2882 {
2883 #ifdef CONFIG_DEBUG_FS
2884 	debugfs_remove_recursive(debugfs_root);
2885 #endif
2886 	platform_driver_unregister(&soc_driver);
2887 }
2888 
2889 module_init(snd_soc_init);
2890 module_exit(snd_soc_exit);
2891 
2892 /* Module information */
2893 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2894 MODULE_DESCRIPTION("ALSA SoC Core");
2895 MODULE_LICENSE("GPL");
2896 MODULE_ALIAS("platform:soc-audio");
2897