xref: /linux/sound/soc/soc-core.c (revision 0d456bad36d42d16022be045c8a53ddbb59ee478)
1 /*
2  * soc-core.c  --  ALSA SoC Audio Layer
3  *
4  * Copyright 2005 Wolfson Microelectronics PLC.
5  * Copyright 2005 Openedhand Ltd.
6  * Copyright (C) 2010 Slimlogic Ltd.
7  * Copyright (C) 2010 Texas Instruments Inc.
8  *
9  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10  *         with code, comments and ideas from :-
11  *         Richard Purdie <richard@openedhand.com>
12  *
13  *  This program is free software; you can redistribute  it and/or modify it
14  *  under  the terms of  the GNU General  Public License as published by the
15  *  Free Software Foundation;  either version 2 of the  License, or (at your
16  *  option) any later version.
17  *
18  *  TODO:
19  *   o Add hw rules to enforce rates, etc.
20  *   o More testing with other codecs/machines.
21  *   o Add more codecs and platforms to ensure good API coverage.
22  *   o Support TDM on PCM and I2S
23  */
24 
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/pm.h>
30 #include <linux/bitops.h>
31 #include <linux/debugfs.h>
32 #include <linux/platform_device.h>
33 #include <linux/ctype.h>
34 #include <linux/slab.h>
35 #include <linux/of.h>
36 #include <sound/ac97_codec.h>
37 #include <sound/core.h>
38 #include <sound/jack.h>
39 #include <sound/pcm.h>
40 #include <sound/pcm_params.h>
41 #include <sound/soc.h>
42 #include <sound/soc-dpcm.h>
43 #include <sound/initval.h>
44 
45 #define CREATE_TRACE_POINTS
46 #include <trace/events/asoc.h>
47 
48 #define NAME_SIZE	32
49 
50 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
51 
52 #ifdef CONFIG_DEBUG_FS
53 struct dentry *snd_soc_debugfs_root;
54 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
55 #endif
56 
57 static DEFINE_MUTEX(client_mutex);
58 static LIST_HEAD(dai_list);
59 static LIST_HEAD(platform_list);
60 static LIST_HEAD(codec_list);
61 
62 /*
63  * This is a timeout to do a DAPM powerdown after a stream is closed().
64  * It can be used to eliminate pops between different playback streams, e.g.
65  * between two audio tracks.
66  */
67 static int pmdown_time = 5000;
68 module_param(pmdown_time, int, 0);
69 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70 
71 /* returns the minimum number of bytes needed to represent
72  * a particular given value */
73 static int min_bytes_needed(unsigned long val)
74 {
75 	int c = 0;
76 	int i;
77 
78 	for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
79 		if (val & (1UL << i))
80 			break;
81 	c = (sizeof val * 8) - c;
82 	if (!c || (c % 8))
83 		c = (c + 8) / 8;
84 	else
85 		c /= 8;
86 	return c;
87 }
88 
89 /* fill buf which is 'len' bytes with a formatted
90  * string of the form 'reg: value\n' */
91 static int format_register_str(struct snd_soc_codec *codec,
92 			       unsigned int reg, char *buf, size_t len)
93 {
94 	int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
95 	int regsize = codec->driver->reg_word_size * 2;
96 	int ret;
97 	char tmpbuf[len + 1];
98 	char regbuf[regsize + 1];
99 
100 	/* since tmpbuf is allocated on the stack, warn the callers if they
101 	 * try to abuse this function */
102 	WARN_ON(len > 63);
103 
104 	/* +2 for ': ' and + 1 for '\n' */
105 	if (wordsize + regsize + 2 + 1 != len)
106 		return -EINVAL;
107 
108 	ret = snd_soc_read(codec, reg);
109 	if (ret < 0) {
110 		memset(regbuf, 'X', regsize);
111 		regbuf[regsize] = '\0';
112 	} else {
113 		snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
114 	}
115 
116 	/* prepare the buffer */
117 	snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
118 	/* copy it back to the caller without the '\0' */
119 	memcpy(buf, tmpbuf, len);
120 
121 	return 0;
122 }
123 
124 /* codec register dump */
125 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
126 				  size_t count, loff_t pos)
127 {
128 	int i, step = 1;
129 	int wordsize, regsize;
130 	int len;
131 	size_t total = 0;
132 	loff_t p = 0;
133 
134 	wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
135 	regsize = codec->driver->reg_word_size * 2;
136 
137 	len = wordsize + regsize + 2 + 1;
138 
139 	if (!codec->driver->reg_cache_size)
140 		return 0;
141 
142 	if (codec->driver->reg_cache_step)
143 		step = codec->driver->reg_cache_step;
144 
145 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
146 		if (!snd_soc_codec_readable_register(codec, i))
147 			continue;
148 		if (codec->driver->display_register) {
149 			count += codec->driver->display_register(codec, buf + count,
150 							 PAGE_SIZE - count, i);
151 		} else {
152 			/* only support larger than PAGE_SIZE bytes debugfs
153 			 * entries for the default case */
154 			if (p >= pos) {
155 				if (total + len >= count - 1)
156 					break;
157 				format_register_str(codec, i, buf + total, len);
158 				total += len;
159 			}
160 			p += len;
161 		}
162 	}
163 
164 	total = min(total, count - 1);
165 
166 	return total;
167 }
168 
169 static ssize_t codec_reg_show(struct device *dev,
170 	struct device_attribute *attr, char *buf)
171 {
172 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
173 
174 	return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
175 }
176 
177 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
178 
179 static ssize_t pmdown_time_show(struct device *dev,
180 				struct device_attribute *attr, char *buf)
181 {
182 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
183 
184 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
185 }
186 
187 static ssize_t pmdown_time_set(struct device *dev,
188 			       struct device_attribute *attr,
189 			       const char *buf, size_t count)
190 {
191 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
192 	int ret;
193 
194 	ret = strict_strtol(buf, 10, &rtd->pmdown_time);
195 	if (ret)
196 		return ret;
197 
198 	return count;
199 }
200 
201 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
202 
203 #ifdef CONFIG_DEBUG_FS
204 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
205 				   size_t count, loff_t *ppos)
206 {
207 	ssize_t ret;
208 	struct snd_soc_codec *codec = file->private_data;
209 	char *buf;
210 
211 	if (*ppos < 0 || !count)
212 		return -EINVAL;
213 
214 	buf = kmalloc(count, GFP_KERNEL);
215 	if (!buf)
216 		return -ENOMEM;
217 
218 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
219 	if (ret >= 0) {
220 		if (copy_to_user(user_buf, buf, ret)) {
221 			kfree(buf);
222 			return -EFAULT;
223 		}
224 		*ppos += ret;
225 	}
226 
227 	kfree(buf);
228 	return ret;
229 }
230 
231 static ssize_t codec_reg_write_file(struct file *file,
232 		const char __user *user_buf, size_t count, loff_t *ppos)
233 {
234 	char buf[32];
235 	size_t buf_size;
236 	char *start = buf;
237 	unsigned long reg, value;
238 	struct snd_soc_codec *codec = file->private_data;
239 
240 	buf_size = min(count, (sizeof(buf)-1));
241 	if (copy_from_user(buf, user_buf, buf_size))
242 		return -EFAULT;
243 	buf[buf_size] = 0;
244 
245 	while (*start == ' ')
246 		start++;
247 	reg = simple_strtoul(start, &start, 16);
248 	while (*start == ' ')
249 		start++;
250 	if (strict_strtoul(start, 16, &value))
251 		return -EINVAL;
252 
253 	/* Userspace has been fiddling around behind the kernel's back */
254 	add_taint(TAINT_USER);
255 
256 	snd_soc_write(codec, reg, value);
257 	return buf_size;
258 }
259 
260 static const struct file_operations codec_reg_fops = {
261 	.open = simple_open,
262 	.read = codec_reg_read_file,
263 	.write = codec_reg_write_file,
264 	.llseek = default_llseek,
265 };
266 
267 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
268 {
269 	struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
270 
271 	codec->debugfs_codec_root = debugfs_create_dir(codec->name,
272 						       debugfs_card_root);
273 	if (!codec->debugfs_codec_root) {
274 		dev_warn(codec->dev, "ASoC: Failed to create codec debugfs"
275 			" directory\n");
276 		return;
277 	}
278 
279 	debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
280 			    &codec->cache_sync);
281 	debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
282 			    &codec->cache_only);
283 
284 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
285 						 codec->debugfs_codec_root,
286 						 codec, &codec_reg_fops);
287 	if (!codec->debugfs_reg)
288 		dev_warn(codec->dev, "ASoC: Failed to create codec register"
289 			" debugfs file\n");
290 
291 	snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
292 }
293 
294 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
295 {
296 	debugfs_remove_recursive(codec->debugfs_codec_root);
297 }
298 
299 static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
300 {
301 	struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
302 
303 	platform->debugfs_platform_root = debugfs_create_dir(platform->name,
304 						       debugfs_card_root);
305 	if (!platform->debugfs_platform_root) {
306 		dev_warn(platform->dev,
307 			"ASoC: Failed to create platform debugfs directory\n");
308 		return;
309 	}
310 
311 	snd_soc_dapm_debugfs_init(&platform->dapm,
312 		platform->debugfs_platform_root);
313 }
314 
315 static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
316 {
317 	debugfs_remove_recursive(platform->debugfs_platform_root);
318 }
319 
320 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
321 				    size_t count, loff_t *ppos)
322 {
323 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
324 	ssize_t len, ret = 0;
325 	struct snd_soc_codec *codec;
326 
327 	if (!buf)
328 		return -ENOMEM;
329 
330 	list_for_each_entry(codec, &codec_list, list) {
331 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
332 			       codec->name);
333 		if (len >= 0)
334 			ret += len;
335 		if (ret > PAGE_SIZE) {
336 			ret = PAGE_SIZE;
337 			break;
338 		}
339 	}
340 
341 	if (ret >= 0)
342 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
343 
344 	kfree(buf);
345 
346 	return ret;
347 }
348 
349 static const struct file_operations codec_list_fops = {
350 	.read = codec_list_read_file,
351 	.llseek = default_llseek,/* read accesses f_pos */
352 };
353 
354 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
355 				  size_t count, loff_t *ppos)
356 {
357 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
358 	ssize_t len, ret = 0;
359 	struct snd_soc_dai *dai;
360 
361 	if (!buf)
362 		return -ENOMEM;
363 
364 	list_for_each_entry(dai, &dai_list, list) {
365 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
366 		if (len >= 0)
367 			ret += len;
368 		if (ret > PAGE_SIZE) {
369 			ret = PAGE_SIZE;
370 			break;
371 		}
372 	}
373 
374 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
375 
376 	kfree(buf);
377 
378 	return ret;
379 }
380 
381 static const struct file_operations dai_list_fops = {
382 	.read = dai_list_read_file,
383 	.llseek = default_llseek,/* read accesses f_pos */
384 };
385 
386 static ssize_t platform_list_read_file(struct file *file,
387 				       char __user *user_buf,
388 				       size_t count, loff_t *ppos)
389 {
390 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
391 	ssize_t len, ret = 0;
392 	struct snd_soc_platform *platform;
393 
394 	if (!buf)
395 		return -ENOMEM;
396 
397 	list_for_each_entry(platform, &platform_list, list) {
398 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
399 			       platform->name);
400 		if (len >= 0)
401 			ret += len;
402 		if (ret > PAGE_SIZE) {
403 			ret = PAGE_SIZE;
404 			break;
405 		}
406 	}
407 
408 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
409 
410 	kfree(buf);
411 
412 	return ret;
413 }
414 
415 static const struct file_operations platform_list_fops = {
416 	.read = platform_list_read_file,
417 	.llseek = default_llseek,/* read accesses f_pos */
418 };
419 
420 static void soc_init_card_debugfs(struct snd_soc_card *card)
421 {
422 	card->debugfs_card_root = debugfs_create_dir(card->name,
423 						     snd_soc_debugfs_root);
424 	if (!card->debugfs_card_root) {
425 		dev_warn(card->dev,
426 			 "ASoC: Failed to create card debugfs directory\n");
427 		return;
428 	}
429 
430 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
431 						    card->debugfs_card_root,
432 						    &card->pop_time);
433 	if (!card->debugfs_pop_time)
434 		dev_warn(card->dev,
435 		       "ASoC: Failed to create pop time debugfs file\n");
436 }
437 
438 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
439 {
440 	debugfs_remove_recursive(card->debugfs_card_root);
441 }
442 
443 #else
444 
445 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
446 {
447 }
448 
449 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
450 {
451 }
452 
453 static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
454 {
455 }
456 
457 static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
458 {
459 }
460 
461 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
462 {
463 }
464 
465 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
466 {
467 }
468 #endif
469 
470 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
471 		const char *dai_link, int stream)
472 {
473 	int i;
474 
475 	for (i = 0; i < card->num_links; i++) {
476 		if (card->rtd[i].dai_link->no_pcm &&
477 			!strcmp(card->rtd[i].dai_link->name, dai_link))
478 			return card->rtd[i].pcm->streams[stream].substream;
479 	}
480 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
481 	return NULL;
482 }
483 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
484 
485 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
486 		const char *dai_link)
487 {
488 	int i;
489 
490 	for (i = 0; i < card->num_links; i++) {
491 		if (!strcmp(card->rtd[i].dai_link->name, dai_link))
492 			return &card->rtd[i];
493 	}
494 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
495 	return NULL;
496 }
497 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
498 
499 #ifdef CONFIG_SND_SOC_AC97_BUS
500 /* unregister ac97 codec */
501 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
502 {
503 	if (codec->ac97->dev.bus)
504 		device_unregister(&codec->ac97->dev);
505 	return 0;
506 }
507 
508 /* stop no dev release warning */
509 static void soc_ac97_device_release(struct device *dev){}
510 
511 /* register ac97 codec to bus */
512 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
513 {
514 	int err;
515 
516 	codec->ac97->dev.bus = &ac97_bus_type;
517 	codec->ac97->dev.parent = codec->card->dev;
518 	codec->ac97->dev.release = soc_ac97_device_release;
519 
520 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
521 		     codec->card->snd_card->number, 0, codec->name);
522 	err = device_register(&codec->ac97->dev);
523 	if (err < 0) {
524 		dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
525 		codec->ac97->dev.bus = NULL;
526 		return err;
527 	}
528 	return 0;
529 }
530 #endif
531 
532 #ifdef CONFIG_PM_SLEEP
533 /* powers down audio subsystem for suspend */
534 int snd_soc_suspend(struct device *dev)
535 {
536 	struct snd_soc_card *card = dev_get_drvdata(dev);
537 	struct snd_soc_codec *codec;
538 	int i;
539 
540 	/* If the initialization of this soc device failed, there is no codec
541 	 * associated with it. Just bail out in this case.
542 	 */
543 	if (list_empty(&card->codec_dev_list))
544 		return 0;
545 
546 	/* Due to the resume being scheduled into a workqueue we could
547 	* suspend before that's finished - wait for it to complete.
548 	 */
549 	snd_power_lock(card->snd_card);
550 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
551 	snd_power_unlock(card->snd_card);
552 
553 	/* we're going to block userspace touching us until resume completes */
554 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
555 
556 	/* mute any active DACs */
557 	for (i = 0; i < card->num_rtd; i++) {
558 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
559 		struct snd_soc_dai_driver *drv = dai->driver;
560 
561 		if (card->rtd[i].dai_link->ignore_suspend)
562 			continue;
563 
564 		if (drv->ops->digital_mute && dai->playback_active)
565 			drv->ops->digital_mute(dai, 1);
566 	}
567 
568 	/* suspend all pcms */
569 	for (i = 0; i < card->num_rtd; i++) {
570 		if (card->rtd[i].dai_link->ignore_suspend)
571 			continue;
572 
573 		snd_pcm_suspend_all(card->rtd[i].pcm);
574 	}
575 
576 	if (card->suspend_pre)
577 		card->suspend_pre(card);
578 
579 	for (i = 0; i < card->num_rtd; i++) {
580 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
581 		struct snd_soc_platform *platform = card->rtd[i].platform;
582 
583 		if (card->rtd[i].dai_link->ignore_suspend)
584 			continue;
585 
586 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
587 			cpu_dai->driver->suspend(cpu_dai);
588 		if (platform->driver->suspend && !platform->suspended) {
589 			platform->driver->suspend(cpu_dai);
590 			platform->suspended = 1;
591 		}
592 	}
593 
594 	/* close any waiting streams and save state */
595 	for (i = 0; i < card->num_rtd; i++) {
596 		flush_delayed_work(&card->rtd[i].delayed_work);
597 		card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
598 	}
599 
600 	for (i = 0; i < card->num_rtd; i++) {
601 
602 		if (card->rtd[i].dai_link->ignore_suspend)
603 			continue;
604 
605 		snd_soc_dapm_stream_event(&card->rtd[i],
606 					  SNDRV_PCM_STREAM_PLAYBACK,
607 					  SND_SOC_DAPM_STREAM_SUSPEND);
608 
609 		snd_soc_dapm_stream_event(&card->rtd[i],
610 					  SNDRV_PCM_STREAM_CAPTURE,
611 					  SND_SOC_DAPM_STREAM_SUSPEND);
612 	}
613 
614 	/* Recheck all analogue paths too */
615 	dapm_mark_io_dirty(&card->dapm);
616 	snd_soc_dapm_sync(&card->dapm);
617 
618 	/* suspend all CODECs */
619 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
620 		/* If there are paths active then the CODEC will be held with
621 		 * bias _ON and should not be suspended. */
622 		if (!codec->suspended && codec->driver->suspend) {
623 			switch (codec->dapm.bias_level) {
624 			case SND_SOC_BIAS_STANDBY:
625 				/*
626 				 * If the CODEC is capable of idle
627 				 * bias off then being in STANDBY
628 				 * means it's doing something,
629 				 * otherwise fall through.
630 				 */
631 				if (codec->dapm.idle_bias_off) {
632 					dev_dbg(codec->dev,
633 						"ASoC: idle_bias_off CODEC on"
634 						" over suspend\n");
635 					break;
636 				}
637 			case SND_SOC_BIAS_OFF:
638 				codec->driver->suspend(codec);
639 				codec->suspended = 1;
640 				codec->cache_sync = 1;
641 				if (codec->using_regmap)
642 					regcache_mark_dirty(codec->control_data);
643 				break;
644 			default:
645 				dev_dbg(codec->dev, "ASoC: CODEC is on"
646 					" over suspend\n");
647 				break;
648 			}
649 		}
650 	}
651 
652 	for (i = 0; i < card->num_rtd; i++) {
653 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
654 
655 		if (card->rtd[i].dai_link->ignore_suspend)
656 			continue;
657 
658 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
659 			cpu_dai->driver->suspend(cpu_dai);
660 	}
661 
662 	if (card->suspend_post)
663 		card->suspend_post(card);
664 
665 	return 0;
666 }
667 EXPORT_SYMBOL_GPL(snd_soc_suspend);
668 
669 /* deferred resume work, so resume can complete before we finished
670  * setting our codec back up, which can be very slow on I2C
671  */
672 static void soc_resume_deferred(struct work_struct *work)
673 {
674 	struct snd_soc_card *card =
675 			container_of(work, struct snd_soc_card, deferred_resume_work);
676 	struct snd_soc_codec *codec;
677 	int i;
678 
679 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
680 	 * so userspace apps are blocked from touching us
681 	 */
682 
683 	dev_dbg(card->dev, "ASoC: starting resume work\n");
684 
685 	/* Bring us up into D2 so that DAPM starts enabling things */
686 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
687 
688 	if (card->resume_pre)
689 		card->resume_pre(card);
690 
691 	/* resume AC97 DAIs */
692 	for (i = 0; i < card->num_rtd; i++) {
693 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
694 
695 		if (card->rtd[i].dai_link->ignore_suspend)
696 			continue;
697 
698 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
699 			cpu_dai->driver->resume(cpu_dai);
700 	}
701 
702 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
703 		/* If the CODEC was idle over suspend then it will have been
704 		 * left with bias OFF or STANDBY and suspended so we must now
705 		 * resume.  Otherwise the suspend was suppressed.
706 		 */
707 		if (codec->driver->resume && codec->suspended) {
708 			switch (codec->dapm.bias_level) {
709 			case SND_SOC_BIAS_STANDBY:
710 			case SND_SOC_BIAS_OFF:
711 				codec->driver->resume(codec);
712 				codec->suspended = 0;
713 				break;
714 			default:
715 				dev_dbg(codec->dev, "ASoC: CODEC was on over"
716 					" suspend\n");
717 				break;
718 			}
719 		}
720 	}
721 
722 	for (i = 0; i < card->num_rtd; i++) {
723 
724 		if (card->rtd[i].dai_link->ignore_suspend)
725 			continue;
726 
727 		snd_soc_dapm_stream_event(&card->rtd[i],
728 					  SNDRV_PCM_STREAM_PLAYBACK,
729 					  SND_SOC_DAPM_STREAM_RESUME);
730 
731 		snd_soc_dapm_stream_event(&card->rtd[i],
732 					  SNDRV_PCM_STREAM_CAPTURE,
733 					  SND_SOC_DAPM_STREAM_RESUME);
734 	}
735 
736 	/* unmute any active DACs */
737 	for (i = 0; i < card->num_rtd; i++) {
738 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
739 		struct snd_soc_dai_driver *drv = dai->driver;
740 
741 		if (card->rtd[i].dai_link->ignore_suspend)
742 			continue;
743 
744 		if (drv->ops->digital_mute && dai->playback_active)
745 			drv->ops->digital_mute(dai, 0);
746 	}
747 
748 	for (i = 0; i < card->num_rtd; i++) {
749 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
750 		struct snd_soc_platform *platform = card->rtd[i].platform;
751 
752 		if (card->rtd[i].dai_link->ignore_suspend)
753 			continue;
754 
755 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
756 			cpu_dai->driver->resume(cpu_dai);
757 		if (platform->driver->resume && platform->suspended) {
758 			platform->driver->resume(cpu_dai);
759 			platform->suspended = 0;
760 		}
761 	}
762 
763 	if (card->resume_post)
764 		card->resume_post(card);
765 
766 	dev_dbg(card->dev, "ASoC: resume work completed\n");
767 
768 	/* userspace can access us now we are back as we were before */
769 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
770 
771 	/* Recheck all analogue paths too */
772 	dapm_mark_io_dirty(&card->dapm);
773 	snd_soc_dapm_sync(&card->dapm);
774 }
775 
776 /* powers up audio subsystem after a suspend */
777 int snd_soc_resume(struct device *dev)
778 {
779 	struct snd_soc_card *card = dev_get_drvdata(dev);
780 	int i, ac97_control = 0;
781 
782 	/* If the initialization of this soc device failed, there is no codec
783 	 * associated with it. Just bail out in this case.
784 	 */
785 	if (list_empty(&card->codec_dev_list))
786 		return 0;
787 
788 	/* AC97 devices might have other drivers hanging off them so
789 	 * need to resume immediately.  Other drivers don't have that
790 	 * problem and may take a substantial amount of time to resume
791 	 * due to I/O costs and anti-pop so handle them out of line.
792 	 */
793 	for (i = 0; i < card->num_rtd; i++) {
794 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
795 		ac97_control |= cpu_dai->driver->ac97_control;
796 	}
797 	if (ac97_control) {
798 		dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
799 		soc_resume_deferred(&card->deferred_resume_work);
800 	} else {
801 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
802 		if (!schedule_work(&card->deferred_resume_work))
803 			dev_err(dev, "ASoC: resume work item may be lost\n");
804 	}
805 
806 	return 0;
807 }
808 EXPORT_SYMBOL_GPL(snd_soc_resume);
809 #else
810 #define snd_soc_suspend NULL
811 #define snd_soc_resume NULL
812 #endif
813 
814 static const struct snd_soc_dai_ops null_dai_ops = {
815 };
816 
817 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
818 {
819 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
820 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
821 	struct snd_soc_codec *codec;
822 	struct snd_soc_platform *platform;
823 	struct snd_soc_dai *codec_dai, *cpu_dai;
824 	const char *platform_name;
825 
826 	dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
827 
828 	/* Find CPU DAI from registered DAIs*/
829 	list_for_each_entry(cpu_dai, &dai_list, list) {
830 		if (dai_link->cpu_of_node &&
831 		    (cpu_dai->dev->of_node != dai_link->cpu_of_node))
832 			continue;
833 		if (dai_link->cpu_name &&
834 		    strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
835 			continue;
836 		if (dai_link->cpu_dai_name &&
837 		    strcmp(cpu_dai->name, dai_link->cpu_dai_name))
838 			continue;
839 
840 		rtd->cpu_dai = cpu_dai;
841 	}
842 
843 	if (!rtd->cpu_dai) {
844 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
845 			dai_link->cpu_dai_name);
846 		return -EPROBE_DEFER;
847 	}
848 
849 	/* Find CODEC from registered CODECs */
850 	list_for_each_entry(codec, &codec_list, list) {
851 		if (dai_link->codec_of_node) {
852 			if (codec->dev->of_node != dai_link->codec_of_node)
853 				continue;
854 		} else {
855 			if (strcmp(codec->name, dai_link->codec_name))
856 				continue;
857 		}
858 
859 		rtd->codec = codec;
860 
861 		/*
862 		 * CODEC found, so find CODEC DAI from registered DAIs from
863 		 * this CODEC
864 		 */
865 		list_for_each_entry(codec_dai, &dai_list, list) {
866 			if (codec->dev == codec_dai->dev &&
867 				!strcmp(codec_dai->name,
868 					dai_link->codec_dai_name)) {
869 
870 				rtd->codec_dai = codec_dai;
871 			}
872 		}
873 
874 		if (!rtd->codec_dai) {
875 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
876 				dai_link->codec_dai_name);
877 			return -EPROBE_DEFER;
878 		}
879 	}
880 
881 	if (!rtd->codec) {
882 		dev_err(card->dev, "ASoC: CODEC %s not registered\n",
883 			dai_link->codec_name);
884 		return -EPROBE_DEFER;
885 	}
886 
887 	/* if there's no platform we match on the empty platform */
888 	platform_name = dai_link->platform_name;
889 	if (!platform_name && !dai_link->platform_of_node)
890 		platform_name = "snd-soc-dummy";
891 
892 	/* find one from the set of registered platforms */
893 	list_for_each_entry(platform, &platform_list, list) {
894 		if (dai_link->platform_of_node) {
895 			if (platform->dev->of_node !=
896 			    dai_link->platform_of_node)
897 				continue;
898 		} else {
899 			if (strcmp(platform->name, platform_name))
900 				continue;
901 		}
902 
903 		rtd->platform = platform;
904 	}
905 	if (!rtd->platform) {
906 		dev_err(card->dev, "ASoC: platform %s not registered\n",
907 			dai_link->platform_name);
908 		return -EPROBE_DEFER;
909 	}
910 
911 	card->num_rtd++;
912 
913 	return 0;
914 }
915 
916 static int soc_remove_platform(struct snd_soc_platform *platform)
917 {
918 	int ret;
919 
920 	if (platform->driver->remove) {
921 		ret = platform->driver->remove(platform);
922 		if (ret < 0)
923 			dev_err(platform->dev, "ASoC: failed to remove %d\n",
924 				ret);
925 	}
926 
927 	/* Make sure all DAPM widgets are freed */
928 	snd_soc_dapm_free(&platform->dapm);
929 
930 	soc_cleanup_platform_debugfs(platform);
931 	platform->probed = 0;
932 	list_del(&platform->card_list);
933 	module_put(platform->dev->driver->owner);
934 
935 	return 0;
936 }
937 
938 static void soc_remove_codec(struct snd_soc_codec *codec)
939 {
940 	int err;
941 
942 	if (codec->driver->remove) {
943 		err = codec->driver->remove(codec);
944 		if (err < 0)
945 			dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
946 	}
947 
948 	/* Make sure all DAPM widgets are freed */
949 	snd_soc_dapm_free(&codec->dapm);
950 
951 	soc_cleanup_codec_debugfs(codec);
952 	codec->probed = 0;
953 	list_del(&codec->card_list);
954 	module_put(codec->dev->driver->owner);
955 }
956 
957 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
958 {
959 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
960 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
961 	int err;
962 
963 	/* unregister the rtd device */
964 	if (rtd->dev_registered) {
965 		device_remove_file(rtd->dev, &dev_attr_pmdown_time);
966 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
967 		device_unregister(rtd->dev);
968 		rtd->dev_registered = 0;
969 	}
970 
971 	/* remove the CODEC DAI */
972 	if (codec_dai && codec_dai->probed &&
973 			codec_dai->driver->remove_order == order) {
974 		if (codec_dai->driver->remove) {
975 			err = codec_dai->driver->remove(codec_dai);
976 			if (err < 0)
977 				dev_err(codec_dai->dev,
978 					"ASoC: failed to remove %s: %d\n",
979 					codec_dai->name, err);
980 		}
981 		codec_dai->probed = 0;
982 		list_del(&codec_dai->card_list);
983 	}
984 
985 	/* remove the cpu_dai */
986 	if (cpu_dai && cpu_dai->probed &&
987 			cpu_dai->driver->remove_order == order) {
988 		if (cpu_dai->driver->remove) {
989 			err = cpu_dai->driver->remove(cpu_dai);
990 			if (err < 0)
991 				dev_err(cpu_dai->dev,
992 					"ASoC: failed to remove %s: %d\n",
993 					cpu_dai->name, err);
994 		}
995 		cpu_dai->probed = 0;
996 		list_del(&cpu_dai->card_list);
997 
998 		if (!cpu_dai->codec) {
999 			snd_soc_dapm_free(&cpu_dai->dapm);
1000 			module_put(cpu_dai->dev->driver->owner);
1001 		}
1002 	}
1003 }
1004 
1005 static void soc_remove_link_components(struct snd_soc_card *card, int num,
1006 				       int order)
1007 {
1008 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1009 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1010 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
1011 	struct snd_soc_platform *platform = rtd->platform;
1012 	struct snd_soc_codec *codec;
1013 
1014 	/* remove the platform */
1015 	if (platform && platform->probed &&
1016 	    platform->driver->remove_order == order) {
1017 		soc_remove_platform(platform);
1018 	}
1019 
1020 	/* remove the CODEC-side CODEC */
1021 	if (codec_dai) {
1022 		codec = codec_dai->codec;
1023 		if (codec && codec->probed &&
1024 		    codec->driver->remove_order == order)
1025 			soc_remove_codec(codec);
1026 	}
1027 
1028 	/* remove any CPU-side CODEC */
1029 	if (cpu_dai) {
1030 		codec = cpu_dai->codec;
1031 		if (codec && codec->probed &&
1032 		    codec->driver->remove_order == order)
1033 			soc_remove_codec(codec);
1034 	}
1035 }
1036 
1037 static void soc_remove_dai_links(struct snd_soc_card *card)
1038 {
1039 	int dai, order;
1040 
1041 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1042 			order++) {
1043 		for (dai = 0; dai < card->num_rtd; dai++)
1044 			soc_remove_link_dais(card, dai, order);
1045 	}
1046 
1047 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1048 			order++) {
1049 		for (dai = 0; dai < card->num_rtd; dai++)
1050 			soc_remove_link_components(card, dai, order);
1051 	}
1052 
1053 	card->num_rtd = 0;
1054 }
1055 
1056 static void soc_set_name_prefix(struct snd_soc_card *card,
1057 				struct snd_soc_codec *codec)
1058 {
1059 	int i;
1060 
1061 	if (card->codec_conf == NULL)
1062 		return;
1063 
1064 	for (i = 0; i < card->num_configs; i++) {
1065 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1066 		if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1067 			codec->name_prefix = map->name_prefix;
1068 			break;
1069 		}
1070 	}
1071 }
1072 
1073 static int soc_probe_codec(struct snd_soc_card *card,
1074 			   struct snd_soc_codec *codec)
1075 {
1076 	int ret = 0;
1077 	const struct snd_soc_codec_driver *driver = codec->driver;
1078 	struct snd_soc_dai *dai;
1079 
1080 	codec->card = card;
1081 	codec->dapm.card = card;
1082 	soc_set_name_prefix(card, codec);
1083 
1084 	if (!try_module_get(codec->dev->driver->owner))
1085 		return -ENODEV;
1086 
1087 	soc_init_codec_debugfs(codec);
1088 
1089 	if (driver->dapm_widgets)
1090 		snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1091 					  driver->num_dapm_widgets);
1092 
1093 	/* Create DAPM widgets for each DAI stream */
1094 	list_for_each_entry(dai, &dai_list, list) {
1095 		if (dai->dev != codec->dev)
1096 			continue;
1097 
1098 		snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1099 	}
1100 
1101 	codec->dapm.idle_bias_off = driver->idle_bias_off;
1102 
1103 	if (driver->probe) {
1104 		ret = driver->probe(codec);
1105 		if (ret < 0) {
1106 			dev_err(codec->dev,
1107 				"ASoC: failed to probe CODEC %d\n", ret);
1108 			goto err_probe;
1109 		}
1110 	}
1111 
1112 	/* If the driver didn't set I/O up try regmap */
1113 	if (!codec->write && dev_get_regmap(codec->dev, NULL))
1114 		snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
1115 
1116 	if (driver->controls)
1117 		snd_soc_add_codec_controls(codec, driver->controls,
1118 				     driver->num_controls);
1119 	if (driver->dapm_routes)
1120 		snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1121 					driver->num_dapm_routes);
1122 
1123 	/* mark codec as probed and add to card codec list */
1124 	codec->probed = 1;
1125 	list_add(&codec->card_list, &card->codec_dev_list);
1126 	list_add(&codec->dapm.list, &card->dapm_list);
1127 
1128 	return 0;
1129 
1130 err_probe:
1131 	soc_cleanup_codec_debugfs(codec);
1132 	module_put(codec->dev->driver->owner);
1133 
1134 	return ret;
1135 }
1136 
1137 static int soc_probe_platform(struct snd_soc_card *card,
1138 			   struct snd_soc_platform *platform)
1139 {
1140 	int ret = 0;
1141 	const struct snd_soc_platform_driver *driver = platform->driver;
1142 	struct snd_soc_dai *dai;
1143 
1144 	platform->card = card;
1145 	platform->dapm.card = card;
1146 
1147 	if (!try_module_get(platform->dev->driver->owner))
1148 		return -ENODEV;
1149 
1150 	soc_init_platform_debugfs(platform);
1151 
1152 	if (driver->dapm_widgets)
1153 		snd_soc_dapm_new_controls(&platform->dapm,
1154 			driver->dapm_widgets, driver->num_dapm_widgets);
1155 
1156 	/* Create DAPM widgets for each DAI stream */
1157 	list_for_each_entry(dai, &dai_list, list) {
1158 		if (dai->dev != platform->dev)
1159 			continue;
1160 
1161 		snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1162 	}
1163 
1164 	platform->dapm.idle_bias_off = 1;
1165 
1166 	if (driver->probe) {
1167 		ret = driver->probe(platform);
1168 		if (ret < 0) {
1169 			dev_err(platform->dev,
1170 				"ASoC: failed to probe platform %d\n", ret);
1171 			goto err_probe;
1172 		}
1173 	}
1174 
1175 	if (driver->controls)
1176 		snd_soc_add_platform_controls(platform, driver->controls,
1177 				     driver->num_controls);
1178 	if (driver->dapm_routes)
1179 		snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1180 					driver->num_dapm_routes);
1181 
1182 	/* mark platform as probed and add to card platform list */
1183 	platform->probed = 1;
1184 	list_add(&platform->card_list, &card->platform_dev_list);
1185 	list_add(&platform->dapm.list, &card->dapm_list);
1186 
1187 	return 0;
1188 
1189 err_probe:
1190 	soc_cleanup_platform_debugfs(platform);
1191 	module_put(platform->dev->driver->owner);
1192 
1193 	return ret;
1194 }
1195 
1196 static void rtd_release(struct device *dev)
1197 {
1198 	kfree(dev);
1199 }
1200 
1201 static int soc_post_component_init(struct snd_soc_card *card,
1202 				   struct snd_soc_codec *codec,
1203 				   int num, int dailess)
1204 {
1205 	struct snd_soc_dai_link *dai_link = NULL;
1206 	struct snd_soc_aux_dev *aux_dev = NULL;
1207 	struct snd_soc_pcm_runtime *rtd;
1208 	const char *temp, *name;
1209 	int ret = 0;
1210 
1211 	if (!dailess) {
1212 		dai_link = &card->dai_link[num];
1213 		rtd = &card->rtd[num];
1214 		name = dai_link->name;
1215 	} else {
1216 		aux_dev = &card->aux_dev[num];
1217 		rtd = &card->rtd_aux[num];
1218 		name = aux_dev->name;
1219 	}
1220 	rtd->card = card;
1221 
1222 	/* Make sure all DAPM widgets are instantiated */
1223 	snd_soc_dapm_new_widgets(&codec->dapm);
1224 
1225 	/* machine controls, routes and widgets are not prefixed */
1226 	temp = codec->name_prefix;
1227 	codec->name_prefix = NULL;
1228 
1229 	/* do machine specific initialization */
1230 	if (!dailess && dai_link->init)
1231 		ret = dai_link->init(rtd);
1232 	else if (dailess && aux_dev->init)
1233 		ret = aux_dev->init(&codec->dapm);
1234 	if (ret < 0) {
1235 		dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
1236 		return ret;
1237 	}
1238 	codec->name_prefix = temp;
1239 
1240 	/* register the rtd device */
1241 	rtd->codec = codec;
1242 
1243 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1244 	if (!rtd->dev)
1245 		return -ENOMEM;
1246 	device_initialize(rtd->dev);
1247 	rtd->dev->parent = card->dev;
1248 	rtd->dev->release = rtd_release;
1249 	rtd->dev->init_name = name;
1250 	dev_set_drvdata(rtd->dev, rtd);
1251 	mutex_init(&rtd->pcm_mutex);
1252 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1253 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1254 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1255 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1256 	ret = device_add(rtd->dev);
1257 	if (ret < 0) {
1258 		dev_err(card->dev,
1259 			"ASoC: failed to register runtime device: %d\n", ret);
1260 		return ret;
1261 	}
1262 	rtd->dev_registered = 1;
1263 
1264 	/* add DAPM sysfs entries for this codec */
1265 	ret = snd_soc_dapm_sys_add(rtd->dev);
1266 	if (ret < 0)
1267 		dev_err(codec->dev,
1268 			"ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
1269 
1270 	/* add codec sysfs entries */
1271 	ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1272 	if (ret < 0)
1273 		dev_err(codec->dev,
1274 			"ASoC: failed to add codec sysfs files: %d\n", ret);
1275 
1276 #ifdef CONFIG_DEBUG_FS
1277 	/* add DPCM sysfs entries */
1278 	if (!dailess && !dai_link->dynamic)
1279 		goto out;
1280 
1281 	ret = soc_dpcm_debugfs_add(rtd);
1282 	if (ret < 0)
1283 		dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
1284 
1285 out:
1286 #endif
1287 	return 0;
1288 }
1289 
1290 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1291 				     int order)
1292 {
1293 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1294 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1295 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
1296 	struct snd_soc_platform *platform = rtd->platform;
1297 	int ret;
1298 
1299 	/* probe the CPU-side component, if it is a CODEC */
1300 	if (cpu_dai->codec &&
1301 	    !cpu_dai->codec->probed &&
1302 	    cpu_dai->codec->driver->probe_order == order) {
1303 		ret = soc_probe_codec(card, cpu_dai->codec);
1304 		if (ret < 0)
1305 			return ret;
1306 	}
1307 
1308 	/* probe the CODEC-side component */
1309 	if (!codec_dai->codec->probed &&
1310 	    codec_dai->codec->driver->probe_order == order) {
1311 		ret = soc_probe_codec(card, codec_dai->codec);
1312 		if (ret < 0)
1313 			return ret;
1314 	}
1315 
1316 	/* probe the platform */
1317 	if (!platform->probed &&
1318 	    platform->driver->probe_order == order) {
1319 		ret = soc_probe_platform(card, platform);
1320 		if (ret < 0)
1321 			return ret;
1322 	}
1323 
1324 	return 0;
1325 }
1326 
1327 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1328 {
1329 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1330 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1331 	struct snd_soc_codec *codec = rtd->codec;
1332 	struct snd_soc_platform *platform = rtd->platform;
1333 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
1334 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1335 	struct snd_soc_dapm_widget *play_w, *capture_w;
1336 	int ret;
1337 
1338 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1339 			card->name, num, order);
1340 
1341 	/* config components */
1342 	cpu_dai->platform = platform;
1343 	codec_dai->card = card;
1344 	cpu_dai->card = card;
1345 
1346 	/* set default power off timeout */
1347 	rtd->pmdown_time = pmdown_time;
1348 
1349 	/* probe the cpu_dai */
1350 	if (!cpu_dai->probed &&
1351 			cpu_dai->driver->probe_order == order) {
1352 		if (!cpu_dai->codec) {
1353 			cpu_dai->dapm.card = card;
1354 			if (!try_module_get(cpu_dai->dev->driver->owner))
1355 				return -ENODEV;
1356 
1357 			list_add(&cpu_dai->dapm.list, &card->dapm_list);
1358 			snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1359 		}
1360 
1361 		if (cpu_dai->driver->probe) {
1362 			ret = cpu_dai->driver->probe(cpu_dai);
1363 			if (ret < 0) {
1364 				dev_err(cpu_dai->dev,
1365 					"ASoC: failed to probe CPU DAI %s: %d\n",
1366 					cpu_dai->name, ret);
1367 				module_put(cpu_dai->dev->driver->owner);
1368 				return ret;
1369 			}
1370 		}
1371 		cpu_dai->probed = 1;
1372 		/* mark cpu_dai as probed and add to card dai list */
1373 		list_add(&cpu_dai->card_list, &card->dai_dev_list);
1374 	}
1375 
1376 	/* probe the CODEC DAI */
1377 	if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1378 		if (codec_dai->driver->probe) {
1379 			ret = codec_dai->driver->probe(codec_dai);
1380 			if (ret < 0) {
1381 				dev_err(codec_dai->dev,
1382 					"ASoC: failed to probe CODEC DAI %s: %d\n",
1383 					codec_dai->name, ret);
1384 				return ret;
1385 			}
1386 		}
1387 
1388 		/* mark codec_dai as probed and add to card dai list */
1389 		codec_dai->probed = 1;
1390 		list_add(&codec_dai->card_list, &card->dai_dev_list);
1391 	}
1392 
1393 	/* complete DAI probe during last probe */
1394 	if (order != SND_SOC_COMP_ORDER_LAST)
1395 		return 0;
1396 
1397 	ret = soc_post_component_init(card, codec, num, 0);
1398 	if (ret)
1399 		return ret;
1400 
1401 	ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1402 	if (ret < 0)
1403 		dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1404 			ret);
1405 
1406 	if (cpu_dai->driver->compress_dai) {
1407 		/*create compress_device"*/
1408 		ret = soc_new_compress(rtd, num);
1409 		if (ret < 0) {
1410 			dev_err(card->dev, "ASoC: can't create compress %s\n",
1411 					 dai_link->stream_name);
1412 			return ret;
1413 		}
1414 	} else {
1415 
1416 		if (!dai_link->params) {
1417 			/* create the pcm */
1418 			ret = soc_new_pcm(rtd, num);
1419 			if (ret < 0) {
1420 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1421 				       dai_link->stream_name, ret);
1422 				return ret;
1423 			}
1424 		} else {
1425 			/* link the DAI widgets */
1426 			play_w = codec_dai->playback_widget;
1427 			capture_w = cpu_dai->capture_widget;
1428 			if (play_w && capture_w) {
1429 				ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1430 						   capture_w, play_w);
1431 				if (ret != 0) {
1432 					dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1433 						play_w->name, capture_w->name, ret);
1434 					return ret;
1435 				}
1436 			}
1437 
1438 			play_w = cpu_dai->playback_widget;
1439 			capture_w = codec_dai->capture_widget;
1440 			if (play_w && capture_w) {
1441 				ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1442 						   capture_w, play_w);
1443 				if (ret != 0) {
1444 					dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1445 						play_w->name, capture_w->name, ret);
1446 					return ret;
1447 				}
1448 			}
1449 		}
1450 	}
1451 
1452 	/* add platform data for AC97 devices */
1453 	if (rtd->codec_dai->driver->ac97_control)
1454 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1455 
1456 	return 0;
1457 }
1458 
1459 #ifdef CONFIG_SND_SOC_AC97_BUS
1460 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1461 {
1462 	int ret;
1463 
1464 	/* Only instantiate AC97 if not already done by the adaptor
1465 	 * for the generic AC97 subsystem.
1466 	 */
1467 	if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1468 		/*
1469 		 * It is possible that the AC97 device is already registered to
1470 		 * the device subsystem. This happens when the device is created
1471 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
1472 		 * is the generic AC97 glue but others migh emerge.
1473 		 *
1474 		 * In those cases we don't try to register the device again.
1475 		 */
1476 		if (!rtd->codec->ac97_created)
1477 			return 0;
1478 
1479 		ret = soc_ac97_dev_register(rtd->codec);
1480 		if (ret < 0) {
1481 			dev_err(rtd->codec->dev,
1482 				"ASoC: AC97 device register failed: %d\n", ret);
1483 			return ret;
1484 		}
1485 
1486 		rtd->codec->ac97_registered = 1;
1487 	}
1488 	return 0;
1489 }
1490 
1491 static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1492 {
1493 	if (codec->ac97_registered) {
1494 		soc_ac97_dev_unregister(codec);
1495 		codec->ac97_registered = 0;
1496 	}
1497 }
1498 #endif
1499 
1500 static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1501 {
1502 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1503 	struct snd_soc_codec *codec;
1504 
1505 	/* find CODEC from registered CODECs*/
1506 	list_for_each_entry(codec, &codec_list, list) {
1507 		if (!strcmp(codec->name, aux_dev->codec_name))
1508 			return 0;
1509 	}
1510 
1511 	dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
1512 
1513 	return -EPROBE_DEFER;
1514 }
1515 
1516 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1517 {
1518 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1519 	struct snd_soc_codec *codec;
1520 	int ret = -ENODEV;
1521 
1522 	/* find CODEC from registered CODECs*/
1523 	list_for_each_entry(codec, &codec_list, list) {
1524 		if (!strcmp(codec->name, aux_dev->codec_name)) {
1525 			if (codec->probed) {
1526 				dev_err(codec->dev,
1527 					"ASoC: codec already probed");
1528 				ret = -EBUSY;
1529 				goto out;
1530 			}
1531 			goto found;
1532 		}
1533 	}
1534 	/* codec not found */
1535 	dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
1536 	return -EPROBE_DEFER;
1537 
1538 found:
1539 	ret = soc_probe_codec(card, codec);
1540 	if (ret < 0)
1541 		return ret;
1542 
1543 	ret = soc_post_component_init(card, codec, num, 1);
1544 
1545 out:
1546 	return ret;
1547 }
1548 
1549 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1550 {
1551 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1552 	struct snd_soc_codec *codec = rtd->codec;
1553 
1554 	/* unregister the rtd device */
1555 	if (rtd->dev_registered) {
1556 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
1557 		device_del(rtd->dev);
1558 		rtd->dev_registered = 0;
1559 	}
1560 
1561 	if (codec && codec->probed)
1562 		soc_remove_codec(codec);
1563 }
1564 
1565 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1566 				    enum snd_soc_compress_type compress_type)
1567 {
1568 	int ret;
1569 
1570 	if (codec->cache_init)
1571 		return 0;
1572 
1573 	/* override the compress_type if necessary */
1574 	if (compress_type && codec->compress_type != compress_type)
1575 		codec->compress_type = compress_type;
1576 	ret = snd_soc_cache_init(codec);
1577 	if (ret < 0) {
1578 		dev_err(codec->dev, "ASoC: Failed to set cache compression"
1579 			" type: %d\n", ret);
1580 		return ret;
1581 	}
1582 	codec->cache_init = 1;
1583 	return 0;
1584 }
1585 
1586 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1587 {
1588 	struct snd_soc_codec *codec;
1589 	struct snd_soc_codec_conf *codec_conf;
1590 	enum snd_soc_compress_type compress_type;
1591 	struct snd_soc_dai_link *dai_link;
1592 	int ret, i, order, dai_fmt;
1593 
1594 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1595 
1596 	/* bind DAIs */
1597 	for (i = 0; i < card->num_links; i++) {
1598 		ret = soc_bind_dai_link(card, i);
1599 		if (ret != 0)
1600 			goto base_error;
1601 	}
1602 
1603 	/* check aux_devs too */
1604 	for (i = 0; i < card->num_aux_devs; i++) {
1605 		ret = soc_check_aux_dev(card, i);
1606 		if (ret != 0)
1607 			goto base_error;
1608 	}
1609 
1610 	/* initialize the register cache for each available codec */
1611 	list_for_each_entry(codec, &codec_list, list) {
1612 		if (codec->cache_init)
1613 			continue;
1614 		/* by default we don't override the compress_type */
1615 		compress_type = 0;
1616 		/* check to see if we need to override the compress_type */
1617 		for (i = 0; i < card->num_configs; ++i) {
1618 			codec_conf = &card->codec_conf[i];
1619 			if (!strcmp(codec->name, codec_conf->dev_name)) {
1620 				compress_type = codec_conf->compress_type;
1621 				if (compress_type && compress_type
1622 				    != codec->compress_type)
1623 					break;
1624 			}
1625 		}
1626 		ret = snd_soc_init_codec_cache(codec, compress_type);
1627 		if (ret < 0)
1628 			goto base_error;
1629 	}
1630 
1631 	/* card bind complete so register a sound card */
1632 	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1633 			card->owner, 0, &card->snd_card);
1634 	if (ret < 0) {
1635 		dev_err(card->dev, "ASoC: can't create sound card for"
1636 			" card %s: %d\n", card->name, ret);
1637 		goto base_error;
1638 	}
1639 	card->snd_card->dev = card->dev;
1640 
1641 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1642 	card->dapm.dev = card->dev;
1643 	card->dapm.card = card;
1644 	list_add(&card->dapm.list, &card->dapm_list);
1645 
1646 #ifdef CONFIG_DEBUG_FS
1647 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1648 #endif
1649 
1650 #ifdef CONFIG_PM_SLEEP
1651 	/* deferred resume work */
1652 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1653 #endif
1654 
1655 	if (card->dapm_widgets)
1656 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1657 					  card->num_dapm_widgets);
1658 
1659 	/* initialise the sound card only once */
1660 	if (card->probe) {
1661 		ret = card->probe(card);
1662 		if (ret < 0)
1663 			goto card_probe_error;
1664 	}
1665 
1666 	/* probe all components used by DAI links on this card */
1667 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1668 			order++) {
1669 		for (i = 0; i < card->num_links; i++) {
1670 			ret = soc_probe_link_components(card, i, order);
1671 			if (ret < 0) {
1672 				dev_err(card->dev,
1673 					"ASoC: failed to instantiate card %d\n",
1674 					ret);
1675 				goto probe_dai_err;
1676 			}
1677 		}
1678 	}
1679 
1680 	/* probe all DAI links on this card */
1681 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1682 			order++) {
1683 		for (i = 0; i < card->num_links; i++) {
1684 			ret = soc_probe_link_dais(card, i, order);
1685 			if (ret < 0) {
1686 				dev_err(card->dev,
1687 					"ASoC: failed to instantiate card %d\n",
1688 					ret);
1689 				goto probe_dai_err;
1690 			}
1691 		}
1692 	}
1693 
1694 	for (i = 0; i < card->num_aux_devs; i++) {
1695 		ret = soc_probe_aux_dev(card, i);
1696 		if (ret < 0) {
1697 			dev_err(card->dev,
1698 				"ASoC: failed to add auxiliary devices %d\n",
1699 				ret);
1700 			goto probe_aux_dev_err;
1701 		}
1702 	}
1703 
1704 	snd_soc_dapm_link_dai_widgets(card);
1705 
1706 	if (card->controls)
1707 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1708 
1709 	if (card->dapm_routes)
1710 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1711 					card->num_dapm_routes);
1712 
1713 	snd_soc_dapm_new_widgets(&card->dapm);
1714 
1715 	for (i = 0; i < card->num_links; i++) {
1716 		dai_link = &card->dai_link[i];
1717 		dai_fmt = dai_link->dai_fmt;
1718 
1719 		if (dai_fmt) {
1720 			ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1721 						  dai_fmt);
1722 			if (ret != 0 && ret != -ENOTSUPP)
1723 				dev_warn(card->rtd[i].codec_dai->dev,
1724 					 "ASoC: Failed to set DAI format: %d\n",
1725 					 ret);
1726 		}
1727 
1728 		/* If this is a regular CPU link there will be a platform */
1729 		if (dai_fmt &&
1730 		    (dai_link->platform_name || dai_link->platform_of_node)) {
1731 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1732 						  dai_fmt);
1733 			if (ret != 0 && ret != -ENOTSUPP)
1734 				dev_warn(card->rtd[i].cpu_dai->dev,
1735 					 "ASoC: Failed to set DAI format: %d\n",
1736 					 ret);
1737 		} else if (dai_fmt) {
1738 			/* Flip the polarity for the "CPU" end */
1739 			dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1740 			switch (dai_link->dai_fmt &
1741 				SND_SOC_DAIFMT_MASTER_MASK) {
1742 			case SND_SOC_DAIFMT_CBM_CFM:
1743 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1744 				break;
1745 			case SND_SOC_DAIFMT_CBM_CFS:
1746 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1747 				break;
1748 			case SND_SOC_DAIFMT_CBS_CFM:
1749 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1750 				break;
1751 			case SND_SOC_DAIFMT_CBS_CFS:
1752 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1753 				break;
1754 			}
1755 
1756 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1757 						  dai_fmt);
1758 			if (ret != 0 && ret != -ENOTSUPP)
1759 				dev_warn(card->rtd[i].cpu_dai->dev,
1760 					 "ASoC: Failed to set DAI format: %d\n",
1761 					 ret);
1762 		}
1763 	}
1764 
1765 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1766 		 "%s", card->name);
1767 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1768 		 "%s", card->long_name ? card->long_name : card->name);
1769 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1770 		 "%s", card->driver_name ? card->driver_name : card->name);
1771 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1772 		switch (card->snd_card->driver[i]) {
1773 		case '_':
1774 		case '-':
1775 		case '\0':
1776 			break;
1777 		default:
1778 			if (!isalnum(card->snd_card->driver[i]))
1779 				card->snd_card->driver[i] = '_';
1780 			break;
1781 		}
1782 	}
1783 
1784 	if (card->late_probe) {
1785 		ret = card->late_probe(card);
1786 		if (ret < 0) {
1787 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1788 				card->name, ret);
1789 			goto probe_aux_dev_err;
1790 		}
1791 	}
1792 
1793 	snd_soc_dapm_new_widgets(&card->dapm);
1794 
1795 	if (card->fully_routed)
1796 		list_for_each_entry(codec, &card->codec_dev_list, card_list)
1797 			snd_soc_dapm_auto_nc_codec_pins(codec);
1798 
1799 	ret = snd_card_register(card->snd_card);
1800 	if (ret < 0) {
1801 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1802 				ret);
1803 		goto probe_aux_dev_err;
1804 	}
1805 
1806 #ifdef CONFIG_SND_SOC_AC97_BUS
1807 	/* register any AC97 codecs */
1808 	for (i = 0; i < card->num_rtd; i++) {
1809 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1810 		if (ret < 0) {
1811 			dev_err(card->dev, "ASoC: failed to register AC97:"
1812 				" %d\n", ret);
1813 			while (--i >= 0)
1814 				soc_unregister_ac97_dai_link(card->rtd[i].codec);
1815 			goto probe_aux_dev_err;
1816 		}
1817 	}
1818 #endif
1819 
1820 	card->instantiated = 1;
1821 	snd_soc_dapm_sync(&card->dapm);
1822 	mutex_unlock(&card->mutex);
1823 
1824 	return 0;
1825 
1826 probe_aux_dev_err:
1827 	for (i = 0; i < card->num_aux_devs; i++)
1828 		soc_remove_aux_dev(card, i);
1829 
1830 probe_dai_err:
1831 	soc_remove_dai_links(card);
1832 
1833 card_probe_error:
1834 	if (card->remove)
1835 		card->remove(card);
1836 
1837 	snd_card_free(card->snd_card);
1838 
1839 base_error:
1840 	mutex_unlock(&card->mutex);
1841 
1842 	return ret;
1843 }
1844 
1845 /* probes a new socdev */
1846 static int soc_probe(struct platform_device *pdev)
1847 {
1848 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1849 
1850 	/*
1851 	 * no card, so machine driver should be registering card
1852 	 * we should not be here in that case so ret error
1853 	 */
1854 	if (!card)
1855 		return -EINVAL;
1856 
1857 	dev_warn(&pdev->dev,
1858 		 "ASoC: machine %s should use snd_soc_register_card()\n",
1859 		 card->name);
1860 
1861 	/* Bodge while we unpick instantiation */
1862 	card->dev = &pdev->dev;
1863 
1864 	return snd_soc_register_card(card);
1865 }
1866 
1867 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1868 {
1869 	int i;
1870 
1871 	/* make sure any delayed work runs */
1872 	for (i = 0; i < card->num_rtd; i++) {
1873 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1874 		flush_delayed_work(&rtd->delayed_work);
1875 	}
1876 
1877 	/* remove auxiliary devices */
1878 	for (i = 0; i < card->num_aux_devs; i++)
1879 		soc_remove_aux_dev(card, i);
1880 
1881 	/* remove and free each DAI */
1882 	soc_remove_dai_links(card);
1883 
1884 	soc_cleanup_card_debugfs(card);
1885 
1886 	/* remove the card */
1887 	if (card->remove)
1888 		card->remove(card);
1889 
1890 	snd_soc_dapm_free(&card->dapm);
1891 
1892 	snd_card_free(card->snd_card);
1893 	return 0;
1894 
1895 }
1896 
1897 /* removes a socdev */
1898 static int soc_remove(struct platform_device *pdev)
1899 {
1900 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1901 
1902 	snd_soc_unregister_card(card);
1903 	return 0;
1904 }
1905 
1906 int snd_soc_poweroff(struct device *dev)
1907 {
1908 	struct snd_soc_card *card = dev_get_drvdata(dev);
1909 	int i;
1910 
1911 	if (!card->instantiated)
1912 		return 0;
1913 
1914 	/* Flush out pmdown_time work - we actually do want to run it
1915 	 * now, we're shutting down so no imminent restart. */
1916 	for (i = 0; i < card->num_rtd; i++) {
1917 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1918 		flush_delayed_work(&rtd->delayed_work);
1919 	}
1920 
1921 	snd_soc_dapm_shutdown(card);
1922 
1923 	return 0;
1924 }
1925 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1926 
1927 const struct dev_pm_ops snd_soc_pm_ops = {
1928 	.suspend = snd_soc_suspend,
1929 	.resume = snd_soc_resume,
1930 	.freeze = snd_soc_suspend,
1931 	.thaw = snd_soc_resume,
1932 	.poweroff = snd_soc_poweroff,
1933 	.restore = snd_soc_resume,
1934 };
1935 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1936 
1937 /* ASoC platform driver */
1938 static struct platform_driver soc_driver = {
1939 	.driver		= {
1940 		.name		= "soc-audio",
1941 		.owner		= THIS_MODULE,
1942 		.pm		= &snd_soc_pm_ops,
1943 	},
1944 	.probe		= soc_probe,
1945 	.remove		= soc_remove,
1946 };
1947 
1948 /**
1949  * snd_soc_codec_volatile_register: Report if a register is volatile.
1950  *
1951  * @codec: CODEC to query.
1952  * @reg: Register to query.
1953  *
1954  * Boolean function indiciating if a CODEC register is volatile.
1955  */
1956 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1957 				    unsigned int reg)
1958 {
1959 	if (codec->volatile_register)
1960 		return codec->volatile_register(codec, reg);
1961 	else
1962 		return 0;
1963 }
1964 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1965 
1966 /**
1967  * snd_soc_codec_readable_register: Report if a register is readable.
1968  *
1969  * @codec: CODEC to query.
1970  * @reg: Register to query.
1971  *
1972  * Boolean function indicating if a CODEC register is readable.
1973  */
1974 int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1975 				    unsigned int reg)
1976 {
1977 	if (codec->readable_register)
1978 		return codec->readable_register(codec, reg);
1979 	else
1980 		return 1;
1981 }
1982 EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1983 
1984 /**
1985  * snd_soc_codec_writable_register: Report if a register is writable.
1986  *
1987  * @codec: CODEC to query.
1988  * @reg: Register to query.
1989  *
1990  * Boolean function indicating if a CODEC register is writable.
1991  */
1992 int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1993 				    unsigned int reg)
1994 {
1995 	if (codec->writable_register)
1996 		return codec->writable_register(codec, reg);
1997 	else
1998 		return 1;
1999 }
2000 EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2001 
2002 int snd_soc_platform_read(struct snd_soc_platform *platform,
2003 					unsigned int reg)
2004 {
2005 	unsigned int ret;
2006 
2007 	if (!platform->driver->read) {
2008 		dev_err(platform->dev, "ASoC: platform has no read back\n");
2009 		return -1;
2010 	}
2011 
2012 	ret = platform->driver->read(platform, reg);
2013 	dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
2014 	trace_snd_soc_preg_read(platform, reg, ret);
2015 
2016 	return ret;
2017 }
2018 EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2019 
2020 int snd_soc_platform_write(struct snd_soc_platform *platform,
2021 					 unsigned int reg, unsigned int val)
2022 {
2023 	if (!platform->driver->write) {
2024 		dev_err(platform->dev, "ASoC: platform has no write back\n");
2025 		return -1;
2026 	}
2027 
2028 	dev_dbg(platform->dev, "write %x = %x\n", reg, val);
2029 	trace_snd_soc_preg_write(platform, reg, val);
2030 	return platform->driver->write(platform, reg, val);
2031 }
2032 EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2033 
2034 /**
2035  * snd_soc_new_ac97_codec - initailise AC97 device
2036  * @codec: audio codec
2037  * @ops: AC97 bus operations
2038  * @num: AC97 codec number
2039  *
2040  * Initialises AC97 codec resources for use by ad-hoc devices only.
2041  */
2042 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2043 	struct snd_ac97_bus_ops *ops, int num)
2044 {
2045 	mutex_lock(&codec->mutex);
2046 
2047 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2048 	if (codec->ac97 == NULL) {
2049 		mutex_unlock(&codec->mutex);
2050 		return -ENOMEM;
2051 	}
2052 
2053 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2054 	if (codec->ac97->bus == NULL) {
2055 		kfree(codec->ac97);
2056 		codec->ac97 = NULL;
2057 		mutex_unlock(&codec->mutex);
2058 		return -ENOMEM;
2059 	}
2060 
2061 	codec->ac97->bus->ops = ops;
2062 	codec->ac97->num = num;
2063 
2064 	/*
2065 	 * Mark the AC97 device to be created by us. This way we ensure that the
2066 	 * device will be registered with the device subsystem later on.
2067 	 */
2068 	codec->ac97_created = 1;
2069 
2070 	mutex_unlock(&codec->mutex);
2071 	return 0;
2072 }
2073 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2074 
2075 /**
2076  * snd_soc_free_ac97_codec - free AC97 codec device
2077  * @codec: audio codec
2078  *
2079  * Frees AC97 codec device resources.
2080  */
2081 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2082 {
2083 	mutex_lock(&codec->mutex);
2084 #ifdef CONFIG_SND_SOC_AC97_BUS
2085 	soc_unregister_ac97_dai_link(codec);
2086 #endif
2087 	kfree(codec->ac97->bus);
2088 	kfree(codec->ac97);
2089 	codec->ac97 = NULL;
2090 	codec->ac97_created = 0;
2091 	mutex_unlock(&codec->mutex);
2092 }
2093 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2094 
2095 unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2096 {
2097 	unsigned int ret;
2098 
2099 	ret = codec->read(codec, reg);
2100 	dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2101 	trace_snd_soc_reg_read(codec, reg, ret);
2102 
2103 	return ret;
2104 }
2105 EXPORT_SYMBOL_GPL(snd_soc_read);
2106 
2107 unsigned int snd_soc_write(struct snd_soc_codec *codec,
2108 			   unsigned int reg, unsigned int val)
2109 {
2110 	dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2111 	trace_snd_soc_reg_write(codec, reg, val);
2112 	return codec->write(codec, reg, val);
2113 }
2114 EXPORT_SYMBOL_GPL(snd_soc_write);
2115 
2116 unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2117 				    unsigned int reg, const void *data, size_t len)
2118 {
2119 	return codec->bulk_write_raw(codec, reg, data, len);
2120 }
2121 EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2122 
2123 /**
2124  * snd_soc_update_bits - update codec register bits
2125  * @codec: audio codec
2126  * @reg: codec register
2127  * @mask: register mask
2128  * @value: new value
2129  *
2130  * Writes new register value.
2131  *
2132  * Returns 1 for change, 0 for no change, or negative error code.
2133  */
2134 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
2135 				unsigned int mask, unsigned int value)
2136 {
2137 	bool change;
2138 	unsigned int old, new;
2139 	int ret;
2140 
2141 	if (codec->using_regmap) {
2142 		ret = regmap_update_bits_check(codec->control_data, reg,
2143 					       mask, value, &change);
2144 	} else {
2145 		ret = snd_soc_read(codec, reg);
2146 		if (ret < 0)
2147 			return ret;
2148 
2149 		old = ret;
2150 		new = (old & ~mask) | (value & mask);
2151 		change = old != new;
2152 		if (change)
2153 			ret = snd_soc_write(codec, reg, new);
2154 	}
2155 
2156 	if (ret < 0)
2157 		return ret;
2158 
2159 	return change;
2160 }
2161 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2162 
2163 /**
2164  * snd_soc_update_bits_locked - update codec register bits
2165  * @codec: audio codec
2166  * @reg: codec register
2167  * @mask: register mask
2168  * @value: new value
2169  *
2170  * Writes new register value, and takes the codec mutex.
2171  *
2172  * Returns 1 for change else 0.
2173  */
2174 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2175 			       unsigned short reg, unsigned int mask,
2176 			       unsigned int value)
2177 {
2178 	int change;
2179 
2180 	mutex_lock(&codec->mutex);
2181 	change = snd_soc_update_bits(codec, reg, mask, value);
2182 	mutex_unlock(&codec->mutex);
2183 
2184 	return change;
2185 }
2186 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
2187 
2188 /**
2189  * snd_soc_test_bits - test register for change
2190  * @codec: audio codec
2191  * @reg: codec register
2192  * @mask: register mask
2193  * @value: new value
2194  *
2195  * Tests a register with a new value and checks if the new value is
2196  * different from the old value.
2197  *
2198  * Returns 1 for change else 0.
2199  */
2200 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
2201 				unsigned int mask, unsigned int value)
2202 {
2203 	int change;
2204 	unsigned int old, new;
2205 
2206 	old = snd_soc_read(codec, reg);
2207 	new = (old & ~mask) | value;
2208 	change = old != new;
2209 
2210 	return change;
2211 }
2212 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2213 
2214 /**
2215  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2216  * @substream: the pcm substream
2217  * @hw: the hardware parameters
2218  *
2219  * Sets the substream runtime hardware parameters.
2220  */
2221 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2222 	const struct snd_pcm_hardware *hw)
2223 {
2224 	struct snd_pcm_runtime *runtime = substream->runtime;
2225 	runtime->hw.info = hw->info;
2226 	runtime->hw.formats = hw->formats;
2227 	runtime->hw.period_bytes_min = hw->period_bytes_min;
2228 	runtime->hw.period_bytes_max = hw->period_bytes_max;
2229 	runtime->hw.periods_min = hw->periods_min;
2230 	runtime->hw.periods_max = hw->periods_max;
2231 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2232 	runtime->hw.fifo_size = hw->fifo_size;
2233 	return 0;
2234 }
2235 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2236 
2237 /**
2238  * snd_soc_cnew - create new control
2239  * @_template: control template
2240  * @data: control private data
2241  * @long_name: control long name
2242  * @prefix: control name prefix
2243  *
2244  * Create a new mixer control from a template control.
2245  *
2246  * Returns 0 for success, else error.
2247  */
2248 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2249 				  void *data, const char *long_name,
2250 				  const char *prefix)
2251 {
2252 	struct snd_kcontrol_new template;
2253 	struct snd_kcontrol *kcontrol;
2254 	char *name = NULL;
2255 	int name_len;
2256 
2257 	memcpy(&template, _template, sizeof(template));
2258 	template.index = 0;
2259 
2260 	if (!long_name)
2261 		long_name = template.name;
2262 
2263 	if (prefix) {
2264 		name_len = strlen(long_name) + strlen(prefix) + 2;
2265 		name = kmalloc(name_len, GFP_KERNEL);
2266 		if (!name)
2267 			return NULL;
2268 
2269 		snprintf(name, name_len, "%s %s", prefix, long_name);
2270 
2271 		template.name = name;
2272 	} else {
2273 		template.name = long_name;
2274 	}
2275 
2276 	kcontrol = snd_ctl_new1(&template, data);
2277 
2278 	kfree(name);
2279 
2280 	return kcontrol;
2281 }
2282 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2283 
2284 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2285 	const struct snd_kcontrol_new *controls, int num_controls,
2286 	const char *prefix, void *data)
2287 {
2288 	int err, i;
2289 
2290 	for (i = 0; i < num_controls; i++) {
2291 		const struct snd_kcontrol_new *control = &controls[i];
2292 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2293 						     control->name, prefix));
2294 		if (err < 0) {
2295 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2296 				control->name, err);
2297 			return err;
2298 		}
2299 	}
2300 
2301 	return 0;
2302 }
2303 
2304 /**
2305  * snd_soc_add_codec_controls - add an array of controls to a codec.
2306  * Convenience function to add a list of controls. Many codecs were
2307  * duplicating this code.
2308  *
2309  * @codec: codec to add controls to
2310  * @controls: array of controls to add
2311  * @num_controls: number of elements in the array
2312  *
2313  * Return 0 for success, else error.
2314  */
2315 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2316 	const struct snd_kcontrol_new *controls, int num_controls)
2317 {
2318 	struct snd_card *card = codec->card->snd_card;
2319 
2320 	return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2321 			codec->name_prefix, codec);
2322 }
2323 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2324 
2325 /**
2326  * snd_soc_add_platform_controls - add an array of controls to a platform.
2327  * Convenience function to add a list of controls.
2328  *
2329  * @platform: platform to add controls to
2330  * @controls: array of controls to add
2331  * @num_controls: number of elements in the array
2332  *
2333  * Return 0 for success, else error.
2334  */
2335 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2336 	const struct snd_kcontrol_new *controls, int num_controls)
2337 {
2338 	struct snd_card *card = platform->card->snd_card;
2339 
2340 	return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2341 			NULL, platform);
2342 }
2343 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2344 
2345 /**
2346  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2347  * Convenience function to add a list of controls.
2348  *
2349  * @soc_card: SoC card to add controls to
2350  * @controls: array of controls to add
2351  * @num_controls: number of elements in the array
2352  *
2353  * Return 0 for success, else error.
2354  */
2355 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2356 	const struct snd_kcontrol_new *controls, int num_controls)
2357 {
2358 	struct snd_card *card = soc_card->snd_card;
2359 
2360 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2361 			NULL, soc_card);
2362 }
2363 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2364 
2365 /**
2366  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2367  * Convienience function to add a list of controls.
2368  *
2369  * @dai: DAI to add controls to
2370  * @controls: array of controls to add
2371  * @num_controls: number of elements in the array
2372  *
2373  * Return 0 for success, else error.
2374  */
2375 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2376 	const struct snd_kcontrol_new *controls, int num_controls)
2377 {
2378 	struct snd_card *card = dai->card->snd_card;
2379 
2380 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2381 			NULL, dai);
2382 }
2383 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2384 
2385 /**
2386  * snd_soc_info_enum_double - enumerated double mixer info callback
2387  * @kcontrol: mixer control
2388  * @uinfo: control element information
2389  *
2390  * Callback to provide information about a double enumerated
2391  * mixer control.
2392  *
2393  * Returns 0 for success.
2394  */
2395 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2396 	struct snd_ctl_elem_info *uinfo)
2397 {
2398 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2399 
2400 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2401 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2402 	uinfo->value.enumerated.items = e->max;
2403 
2404 	if (uinfo->value.enumerated.item > e->max - 1)
2405 		uinfo->value.enumerated.item = e->max - 1;
2406 	strcpy(uinfo->value.enumerated.name,
2407 		e->texts[uinfo->value.enumerated.item]);
2408 	return 0;
2409 }
2410 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2411 
2412 /**
2413  * snd_soc_get_enum_double - enumerated double mixer get callback
2414  * @kcontrol: mixer control
2415  * @ucontrol: control element information
2416  *
2417  * Callback to get the value of a double enumerated mixer.
2418  *
2419  * Returns 0 for success.
2420  */
2421 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2422 	struct snd_ctl_elem_value *ucontrol)
2423 {
2424 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2425 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2426 	unsigned int val;
2427 
2428 	val = snd_soc_read(codec, e->reg);
2429 	ucontrol->value.enumerated.item[0]
2430 		= (val >> e->shift_l) & e->mask;
2431 	if (e->shift_l != e->shift_r)
2432 		ucontrol->value.enumerated.item[1] =
2433 			(val >> e->shift_r) & e->mask;
2434 
2435 	return 0;
2436 }
2437 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2438 
2439 /**
2440  * snd_soc_put_enum_double - enumerated double mixer put callback
2441  * @kcontrol: mixer control
2442  * @ucontrol: control element information
2443  *
2444  * Callback to set the value of a double enumerated mixer.
2445  *
2446  * Returns 0 for success.
2447  */
2448 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2449 	struct snd_ctl_elem_value *ucontrol)
2450 {
2451 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2452 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2453 	unsigned int val;
2454 	unsigned int mask;
2455 
2456 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2457 		return -EINVAL;
2458 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
2459 	mask = e->mask << e->shift_l;
2460 	if (e->shift_l != e->shift_r) {
2461 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2462 			return -EINVAL;
2463 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2464 		mask |= e->mask << e->shift_r;
2465 	}
2466 
2467 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2468 }
2469 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2470 
2471 /**
2472  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2473  * @kcontrol: mixer control
2474  * @ucontrol: control element information
2475  *
2476  * Callback to get the value of a double semi enumerated mixer.
2477  *
2478  * Semi enumerated mixer: the enumerated items are referred as values. Can be
2479  * used for handling bitfield coded enumeration for example.
2480  *
2481  * Returns 0 for success.
2482  */
2483 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2484 	struct snd_ctl_elem_value *ucontrol)
2485 {
2486 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2487 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2488 	unsigned int reg_val, val, mux;
2489 
2490 	reg_val = snd_soc_read(codec, e->reg);
2491 	val = (reg_val >> e->shift_l) & e->mask;
2492 	for (mux = 0; mux < e->max; mux++) {
2493 		if (val == e->values[mux])
2494 			break;
2495 	}
2496 	ucontrol->value.enumerated.item[0] = mux;
2497 	if (e->shift_l != e->shift_r) {
2498 		val = (reg_val >> e->shift_r) & e->mask;
2499 		for (mux = 0; mux < e->max; mux++) {
2500 			if (val == e->values[mux])
2501 				break;
2502 		}
2503 		ucontrol->value.enumerated.item[1] = mux;
2504 	}
2505 
2506 	return 0;
2507 }
2508 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2509 
2510 /**
2511  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2512  * @kcontrol: mixer control
2513  * @ucontrol: control element information
2514  *
2515  * Callback to set the value of a double semi enumerated mixer.
2516  *
2517  * Semi enumerated mixer: the enumerated items are referred as values. Can be
2518  * used for handling bitfield coded enumeration for example.
2519  *
2520  * Returns 0 for success.
2521  */
2522 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2523 	struct snd_ctl_elem_value *ucontrol)
2524 {
2525 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2526 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2527 	unsigned int val;
2528 	unsigned int mask;
2529 
2530 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2531 		return -EINVAL;
2532 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2533 	mask = e->mask << e->shift_l;
2534 	if (e->shift_l != e->shift_r) {
2535 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2536 			return -EINVAL;
2537 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2538 		mask |= e->mask << e->shift_r;
2539 	}
2540 
2541 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2542 }
2543 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2544 
2545 /**
2546  * snd_soc_info_enum_ext - external enumerated single mixer info callback
2547  * @kcontrol: mixer control
2548  * @uinfo: control element information
2549  *
2550  * Callback to provide information about an external enumerated
2551  * single mixer.
2552  *
2553  * Returns 0 for success.
2554  */
2555 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2556 	struct snd_ctl_elem_info *uinfo)
2557 {
2558 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2559 
2560 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2561 	uinfo->count = 1;
2562 	uinfo->value.enumerated.items = e->max;
2563 
2564 	if (uinfo->value.enumerated.item > e->max - 1)
2565 		uinfo->value.enumerated.item = e->max - 1;
2566 	strcpy(uinfo->value.enumerated.name,
2567 		e->texts[uinfo->value.enumerated.item]);
2568 	return 0;
2569 }
2570 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2571 
2572 /**
2573  * snd_soc_info_volsw_ext - external single mixer info callback
2574  * @kcontrol: mixer control
2575  * @uinfo: control element information
2576  *
2577  * Callback to provide information about a single external mixer control.
2578  *
2579  * Returns 0 for success.
2580  */
2581 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2582 	struct snd_ctl_elem_info *uinfo)
2583 {
2584 	int max = kcontrol->private_value;
2585 
2586 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2587 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2588 	else
2589 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2590 
2591 	uinfo->count = 1;
2592 	uinfo->value.integer.min = 0;
2593 	uinfo->value.integer.max = max;
2594 	return 0;
2595 }
2596 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2597 
2598 /**
2599  * snd_soc_info_volsw - single mixer info callback
2600  * @kcontrol: mixer control
2601  * @uinfo: control element information
2602  *
2603  * Callback to provide information about a single mixer control, or a double
2604  * mixer control that spans 2 registers.
2605  *
2606  * Returns 0 for success.
2607  */
2608 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2609 	struct snd_ctl_elem_info *uinfo)
2610 {
2611 	struct soc_mixer_control *mc =
2612 		(struct soc_mixer_control *)kcontrol->private_value;
2613 	int platform_max;
2614 
2615 	if (!mc->platform_max)
2616 		mc->platform_max = mc->max;
2617 	platform_max = mc->platform_max;
2618 
2619 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2620 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2621 	else
2622 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2623 
2624 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2625 	uinfo->value.integer.min = 0;
2626 	uinfo->value.integer.max = platform_max;
2627 	return 0;
2628 }
2629 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2630 
2631 /**
2632  * snd_soc_get_volsw - single mixer get callback
2633  * @kcontrol: mixer control
2634  * @ucontrol: control element information
2635  *
2636  * Callback to get the value of a single mixer control, or a double mixer
2637  * control that spans 2 registers.
2638  *
2639  * Returns 0 for success.
2640  */
2641 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2642 	struct snd_ctl_elem_value *ucontrol)
2643 {
2644 	struct soc_mixer_control *mc =
2645 		(struct soc_mixer_control *)kcontrol->private_value;
2646 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2647 	unsigned int reg = mc->reg;
2648 	unsigned int reg2 = mc->rreg;
2649 	unsigned int shift = mc->shift;
2650 	unsigned int rshift = mc->rshift;
2651 	int max = mc->max;
2652 	unsigned int mask = (1 << fls(max)) - 1;
2653 	unsigned int invert = mc->invert;
2654 
2655 	ucontrol->value.integer.value[0] =
2656 		(snd_soc_read(codec, reg) >> shift) & mask;
2657 	if (invert)
2658 		ucontrol->value.integer.value[0] =
2659 			max - ucontrol->value.integer.value[0];
2660 
2661 	if (snd_soc_volsw_is_stereo(mc)) {
2662 		if (reg == reg2)
2663 			ucontrol->value.integer.value[1] =
2664 				(snd_soc_read(codec, reg) >> rshift) & mask;
2665 		else
2666 			ucontrol->value.integer.value[1] =
2667 				(snd_soc_read(codec, reg2) >> shift) & mask;
2668 		if (invert)
2669 			ucontrol->value.integer.value[1] =
2670 				max - ucontrol->value.integer.value[1];
2671 	}
2672 
2673 	return 0;
2674 }
2675 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2676 
2677 /**
2678  * snd_soc_put_volsw - single mixer put callback
2679  * @kcontrol: mixer control
2680  * @ucontrol: control element information
2681  *
2682  * Callback to set the value of a single mixer control, or a double mixer
2683  * control that spans 2 registers.
2684  *
2685  * Returns 0 for success.
2686  */
2687 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2688 	struct snd_ctl_elem_value *ucontrol)
2689 {
2690 	struct soc_mixer_control *mc =
2691 		(struct soc_mixer_control *)kcontrol->private_value;
2692 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2693 	unsigned int reg = mc->reg;
2694 	unsigned int reg2 = mc->rreg;
2695 	unsigned int shift = mc->shift;
2696 	unsigned int rshift = mc->rshift;
2697 	int max = mc->max;
2698 	unsigned int mask = (1 << fls(max)) - 1;
2699 	unsigned int invert = mc->invert;
2700 	int err;
2701 	bool type_2r = 0;
2702 	unsigned int val2 = 0;
2703 	unsigned int val, val_mask;
2704 
2705 	val = (ucontrol->value.integer.value[0] & mask);
2706 	if (invert)
2707 		val = max - val;
2708 	val_mask = mask << shift;
2709 	val = val << shift;
2710 	if (snd_soc_volsw_is_stereo(mc)) {
2711 		val2 = (ucontrol->value.integer.value[1] & mask);
2712 		if (invert)
2713 			val2 = max - val2;
2714 		if (reg == reg2) {
2715 			val_mask |= mask << rshift;
2716 			val |= val2 << rshift;
2717 		} else {
2718 			val2 = val2 << shift;
2719 			type_2r = 1;
2720 		}
2721 	}
2722 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2723 	if (err < 0)
2724 		return err;
2725 
2726 	if (type_2r)
2727 		err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2728 
2729 	return err;
2730 }
2731 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2732 
2733 /**
2734  * snd_soc_get_volsw_sx - single mixer get callback
2735  * @kcontrol: mixer control
2736  * @ucontrol: control element information
2737  *
2738  * Callback to get the value of a single mixer control, or a double mixer
2739  * control that spans 2 registers.
2740  *
2741  * Returns 0 for success.
2742  */
2743 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2744 		      struct snd_ctl_elem_value *ucontrol)
2745 {
2746 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2747 	struct soc_mixer_control *mc =
2748 	    (struct soc_mixer_control *)kcontrol->private_value;
2749 
2750 	unsigned int reg = mc->reg;
2751 	unsigned int reg2 = mc->rreg;
2752 	unsigned int shift = mc->shift;
2753 	unsigned int rshift = mc->rshift;
2754 	int max = mc->max;
2755 	int min = mc->min;
2756 	int mask = (1 << (fls(min + max) - 1)) - 1;
2757 
2758 	ucontrol->value.integer.value[0] =
2759 	    ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2760 
2761 	if (snd_soc_volsw_is_stereo(mc))
2762 		ucontrol->value.integer.value[1] =
2763 			((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2764 
2765 	return 0;
2766 }
2767 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2768 
2769 /**
2770  * snd_soc_put_volsw_sx - double mixer set callback
2771  * @kcontrol: mixer control
2772  * @uinfo: control element information
2773  *
2774  * Callback to set the value of a double mixer control that spans 2 registers.
2775  *
2776  * Returns 0 for success.
2777  */
2778 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2779 			 struct snd_ctl_elem_value *ucontrol)
2780 {
2781 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2782 	struct soc_mixer_control *mc =
2783 	    (struct soc_mixer_control *)kcontrol->private_value;
2784 
2785 	unsigned int reg = mc->reg;
2786 	unsigned int reg2 = mc->rreg;
2787 	unsigned int shift = mc->shift;
2788 	unsigned int rshift = mc->rshift;
2789 	int max = mc->max;
2790 	int min = mc->min;
2791 	int mask = (1 << (fls(min + max) - 1)) - 1;
2792 	int err = 0;
2793 	unsigned short val, val_mask, val2 = 0;
2794 
2795 	val_mask = mask << shift;
2796 	val = (ucontrol->value.integer.value[0] + min) & mask;
2797 	val = val << shift;
2798 
2799 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2800 	if (err < 0)
2801 		return err;
2802 
2803 	if (snd_soc_volsw_is_stereo(mc)) {
2804 		val_mask = mask << rshift;
2805 		val2 = (ucontrol->value.integer.value[1] + min) & mask;
2806 		val2 = val2 << rshift;
2807 
2808 		if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2809 			return err;
2810 	}
2811 	return 0;
2812 }
2813 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2814 
2815 /**
2816  * snd_soc_info_volsw_s8 - signed mixer info callback
2817  * @kcontrol: mixer control
2818  * @uinfo: control element information
2819  *
2820  * Callback to provide information about a signed mixer control.
2821  *
2822  * Returns 0 for success.
2823  */
2824 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2825 	struct snd_ctl_elem_info *uinfo)
2826 {
2827 	struct soc_mixer_control *mc =
2828 		(struct soc_mixer_control *)kcontrol->private_value;
2829 	int platform_max;
2830 	int min = mc->min;
2831 
2832 	if (!mc->platform_max)
2833 		mc->platform_max = mc->max;
2834 	platform_max = mc->platform_max;
2835 
2836 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2837 	uinfo->count = 2;
2838 	uinfo->value.integer.min = 0;
2839 	uinfo->value.integer.max = platform_max - min;
2840 	return 0;
2841 }
2842 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2843 
2844 /**
2845  * snd_soc_get_volsw_s8 - signed mixer get callback
2846  * @kcontrol: mixer control
2847  * @ucontrol: control element information
2848  *
2849  * Callback to get the value of a signed mixer control.
2850  *
2851  * Returns 0 for success.
2852  */
2853 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2854 	struct snd_ctl_elem_value *ucontrol)
2855 {
2856 	struct soc_mixer_control *mc =
2857 		(struct soc_mixer_control *)kcontrol->private_value;
2858 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2859 	unsigned int reg = mc->reg;
2860 	int min = mc->min;
2861 	int val = snd_soc_read(codec, reg);
2862 
2863 	ucontrol->value.integer.value[0] =
2864 		((signed char)(val & 0xff))-min;
2865 	ucontrol->value.integer.value[1] =
2866 		((signed char)((val >> 8) & 0xff))-min;
2867 	return 0;
2868 }
2869 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2870 
2871 /**
2872  * snd_soc_put_volsw_sgn - signed mixer put callback
2873  * @kcontrol: mixer control
2874  * @ucontrol: control element information
2875  *
2876  * Callback to set the value of a signed mixer control.
2877  *
2878  * Returns 0 for success.
2879  */
2880 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2881 	struct snd_ctl_elem_value *ucontrol)
2882 {
2883 	struct soc_mixer_control *mc =
2884 		(struct soc_mixer_control *)kcontrol->private_value;
2885 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2886 	unsigned int reg = mc->reg;
2887 	int min = mc->min;
2888 	unsigned int val;
2889 
2890 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2891 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2892 
2893 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2894 }
2895 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2896 
2897 /**
2898  * snd_soc_info_volsw_range - single mixer info callback with range.
2899  * @kcontrol: mixer control
2900  * @uinfo: control element information
2901  *
2902  * Callback to provide information, within a range, about a single
2903  * mixer control.
2904  *
2905  * returns 0 for success.
2906  */
2907 int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2908 	struct snd_ctl_elem_info *uinfo)
2909 {
2910 	struct soc_mixer_control *mc =
2911 		(struct soc_mixer_control *)kcontrol->private_value;
2912 	int platform_max;
2913 	int min = mc->min;
2914 
2915 	if (!mc->platform_max)
2916 		mc->platform_max = mc->max;
2917 	platform_max = mc->platform_max;
2918 
2919 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2920 	uinfo->count = 1;
2921 	uinfo->value.integer.min = 0;
2922 	uinfo->value.integer.max = platform_max - min;
2923 
2924 	return 0;
2925 }
2926 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2927 
2928 /**
2929  * snd_soc_put_volsw_range - single mixer put value callback with range.
2930  * @kcontrol: mixer control
2931  * @ucontrol: control element information
2932  *
2933  * Callback to set the value, within a range, for a single mixer control.
2934  *
2935  * Returns 0 for success.
2936  */
2937 int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2938 	struct snd_ctl_elem_value *ucontrol)
2939 {
2940 	struct soc_mixer_control *mc =
2941 		(struct soc_mixer_control *)kcontrol->private_value;
2942 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2943 	unsigned int reg = mc->reg;
2944 	unsigned int shift = mc->shift;
2945 	int min = mc->min;
2946 	int max = mc->max;
2947 	unsigned int mask = (1 << fls(max)) - 1;
2948 	unsigned int invert = mc->invert;
2949 	unsigned int val, val_mask;
2950 
2951 	val = ((ucontrol->value.integer.value[0] + min) & mask);
2952 	if (invert)
2953 		val = max - val;
2954 	val_mask = mask << shift;
2955 	val = val << shift;
2956 
2957 	return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2958 }
2959 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2960 
2961 /**
2962  * snd_soc_get_volsw_range - single mixer get callback with range
2963  * @kcontrol: mixer control
2964  * @ucontrol: control element information
2965  *
2966  * Callback to get the value, within a range, of a single mixer control.
2967  *
2968  * Returns 0 for success.
2969  */
2970 int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2971 	struct snd_ctl_elem_value *ucontrol)
2972 {
2973 	struct soc_mixer_control *mc =
2974 		(struct soc_mixer_control *)kcontrol->private_value;
2975 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2976 	unsigned int reg = mc->reg;
2977 	unsigned int shift = mc->shift;
2978 	int min = mc->min;
2979 	int max = mc->max;
2980 	unsigned int mask = (1 << fls(max)) - 1;
2981 	unsigned int invert = mc->invert;
2982 
2983 	ucontrol->value.integer.value[0] =
2984 		(snd_soc_read(codec, reg) >> shift) & mask;
2985 	if (invert)
2986 		ucontrol->value.integer.value[0] =
2987 			max - ucontrol->value.integer.value[0];
2988 	ucontrol->value.integer.value[0] =
2989 		ucontrol->value.integer.value[0] - min;
2990 
2991 	return 0;
2992 }
2993 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2994 
2995 /**
2996  * snd_soc_limit_volume - Set new limit to an existing volume control.
2997  *
2998  * @codec: where to look for the control
2999  * @name: Name of the control
3000  * @max: new maximum limit
3001  *
3002  * Return 0 for success, else error.
3003  */
3004 int snd_soc_limit_volume(struct snd_soc_codec *codec,
3005 	const char *name, int max)
3006 {
3007 	struct snd_card *card = codec->card->snd_card;
3008 	struct snd_kcontrol *kctl;
3009 	struct soc_mixer_control *mc;
3010 	int found = 0;
3011 	int ret = -EINVAL;
3012 
3013 	/* Sanity check for name and max */
3014 	if (unlikely(!name || max <= 0))
3015 		return -EINVAL;
3016 
3017 	list_for_each_entry(kctl, &card->controls, list) {
3018 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3019 			found = 1;
3020 			break;
3021 		}
3022 	}
3023 	if (found) {
3024 		mc = (struct soc_mixer_control *)kctl->private_value;
3025 		if (max <= mc->max) {
3026 			mc->platform_max = max;
3027 			ret = 0;
3028 		}
3029 	}
3030 	return ret;
3031 }
3032 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3033 
3034 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3035 		       struct snd_ctl_elem_info *uinfo)
3036 {
3037 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3038 	struct soc_bytes *params = (void *)kcontrol->private_value;
3039 
3040 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3041 	uinfo->count = params->num_regs * codec->val_bytes;
3042 
3043 	return 0;
3044 }
3045 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3046 
3047 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3048 		      struct snd_ctl_elem_value *ucontrol)
3049 {
3050 	struct soc_bytes *params = (void *)kcontrol->private_value;
3051 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3052 	int ret;
3053 
3054 	if (codec->using_regmap)
3055 		ret = regmap_raw_read(codec->control_data, params->base,
3056 				      ucontrol->value.bytes.data,
3057 				      params->num_regs * codec->val_bytes);
3058 	else
3059 		ret = -EINVAL;
3060 
3061 	/* Hide any masked bytes to ensure consistent data reporting */
3062 	if (ret == 0 && params->mask) {
3063 		switch (codec->val_bytes) {
3064 		case 1:
3065 			ucontrol->value.bytes.data[0] &= ~params->mask;
3066 			break;
3067 		case 2:
3068 			((u16 *)(&ucontrol->value.bytes.data))[0]
3069 				&= ~params->mask;
3070 			break;
3071 		case 4:
3072 			((u32 *)(&ucontrol->value.bytes.data))[0]
3073 				&= ~params->mask;
3074 			break;
3075 		default:
3076 			return -EINVAL;
3077 		}
3078 	}
3079 
3080 	return ret;
3081 }
3082 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3083 
3084 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3085 		      struct snd_ctl_elem_value *ucontrol)
3086 {
3087 	struct soc_bytes *params = (void *)kcontrol->private_value;
3088 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3089 	int ret, len;
3090 	unsigned int val;
3091 	void *data;
3092 
3093 	if (!codec->using_regmap)
3094 		return -EINVAL;
3095 
3096 	data = ucontrol->value.bytes.data;
3097 	len = params->num_regs * codec->val_bytes;
3098 
3099 	/*
3100 	 * If we've got a mask then we need to preserve the register
3101 	 * bits.  We shouldn't modify the incoming data so take a
3102 	 * copy.
3103 	 */
3104 	if (params->mask) {
3105 		ret = regmap_read(codec->control_data, params->base, &val);
3106 		if (ret != 0)
3107 			return ret;
3108 
3109 		val &= params->mask;
3110 
3111 		data = kmemdup(data, len, GFP_KERNEL);
3112 		if (!data)
3113 			return -ENOMEM;
3114 
3115 		switch (codec->val_bytes) {
3116 		case 1:
3117 			((u8 *)data)[0] &= ~params->mask;
3118 			((u8 *)data)[0] |= val;
3119 			break;
3120 		case 2:
3121 			((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3122 			((u16 *)data)[0] |= cpu_to_be16(val);
3123 			break;
3124 		case 4:
3125 			((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3126 			((u32 *)data)[0] |= cpu_to_be32(val);
3127 			break;
3128 		default:
3129 			return -EINVAL;
3130 		}
3131 	}
3132 
3133 	ret = regmap_raw_write(codec->control_data, params->base,
3134 			       data, len);
3135 
3136 	if (params->mask)
3137 		kfree(data);
3138 
3139 	return ret;
3140 }
3141 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3142 
3143 /**
3144  * snd_soc_info_xr_sx - signed multi register info callback
3145  * @kcontrol: mreg control
3146  * @uinfo: control element information
3147  *
3148  * Callback to provide information of a control that can
3149  * span multiple codec registers which together
3150  * forms a single signed value in a MSB/LSB manner.
3151  *
3152  * Returns 0 for success.
3153  */
3154 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3155 	struct snd_ctl_elem_info *uinfo)
3156 {
3157 	struct soc_mreg_control *mc =
3158 		(struct soc_mreg_control *)kcontrol->private_value;
3159 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3160 	uinfo->count = 1;
3161 	uinfo->value.integer.min = mc->min;
3162 	uinfo->value.integer.max = mc->max;
3163 
3164 	return 0;
3165 }
3166 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3167 
3168 /**
3169  * snd_soc_get_xr_sx - signed multi register get callback
3170  * @kcontrol: mreg control
3171  * @ucontrol: control element information
3172  *
3173  * Callback to get the value of a control that can span
3174  * multiple codec registers which together forms a single
3175  * signed value in a MSB/LSB manner. The control supports
3176  * specifying total no of bits used to allow for bitfields
3177  * across the multiple codec registers.
3178  *
3179  * Returns 0 for success.
3180  */
3181 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3182 	struct snd_ctl_elem_value *ucontrol)
3183 {
3184 	struct soc_mreg_control *mc =
3185 		(struct soc_mreg_control *)kcontrol->private_value;
3186 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3187 	unsigned int regbase = mc->regbase;
3188 	unsigned int regcount = mc->regcount;
3189 	unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3190 	unsigned int regwmask = (1<<regwshift)-1;
3191 	unsigned int invert = mc->invert;
3192 	unsigned long mask = (1UL<<mc->nbits)-1;
3193 	long min = mc->min;
3194 	long max = mc->max;
3195 	long val = 0;
3196 	unsigned long regval;
3197 	unsigned int i;
3198 
3199 	for (i = 0; i < regcount; i++) {
3200 		regval = snd_soc_read(codec, regbase+i) & regwmask;
3201 		val |= regval << (regwshift*(regcount-i-1));
3202 	}
3203 	val &= mask;
3204 	if (min < 0 && val > max)
3205 		val |= ~mask;
3206 	if (invert)
3207 		val = max - val;
3208 	ucontrol->value.integer.value[0] = val;
3209 
3210 	return 0;
3211 }
3212 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3213 
3214 /**
3215  * snd_soc_put_xr_sx - signed multi register get callback
3216  * @kcontrol: mreg control
3217  * @ucontrol: control element information
3218  *
3219  * Callback to set the value of a control that can span
3220  * multiple codec registers which together forms a single
3221  * signed value in a MSB/LSB manner. The control supports
3222  * specifying total no of bits used to allow for bitfields
3223  * across the multiple codec registers.
3224  *
3225  * Returns 0 for success.
3226  */
3227 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3228 	struct snd_ctl_elem_value *ucontrol)
3229 {
3230 	struct soc_mreg_control *mc =
3231 		(struct soc_mreg_control *)kcontrol->private_value;
3232 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3233 	unsigned int regbase = mc->regbase;
3234 	unsigned int regcount = mc->regcount;
3235 	unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3236 	unsigned int regwmask = (1<<regwshift)-1;
3237 	unsigned int invert = mc->invert;
3238 	unsigned long mask = (1UL<<mc->nbits)-1;
3239 	long max = mc->max;
3240 	long val = ucontrol->value.integer.value[0];
3241 	unsigned int i, regval, regmask;
3242 	int err;
3243 
3244 	if (invert)
3245 		val = max - val;
3246 	val &= mask;
3247 	for (i = 0; i < regcount; i++) {
3248 		regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3249 		regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3250 		err = snd_soc_update_bits_locked(codec, regbase+i,
3251 				regmask, regval);
3252 		if (err < 0)
3253 			return err;
3254 	}
3255 
3256 	return 0;
3257 }
3258 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3259 
3260 /**
3261  * snd_soc_get_strobe - strobe get callback
3262  * @kcontrol: mixer control
3263  * @ucontrol: control element information
3264  *
3265  * Callback get the value of a strobe mixer control.
3266  *
3267  * Returns 0 for success.
3268  */
3269 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3270 	struct snd_ctl_elem_value *ucontrol)
3271 {
3272 	struct soc_mixer_control *mc =
3273 		(struct soc_mixer_control *)kcontrol->private_value;
3274 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3275 	unsigned int reg = mc->reg;
3276 	unsigned int shift = mc->shift;
3277 	unsigned int mask = 1 << shift;
3278 	unsigned int invert = mc->invert != 0;
3279 	unsigned int val = snd_soc_read(codec, reg) & mask;
3280 
3281 	if (shift != 0 && val != 0)
3282 		val = val >> shift;
3283 	ucontrol->value.enumerated.item[0] = val ^ invert;
3284 
3285 	return 0;
3286 }
3287 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3288 
3289 /**
3290  * snd_soc_put_strobe - strobe put callback
3291  * @kcontrol: mixer control
3292  * @ucontrol: control element information
3293  *
3294  * Callback strobe a register bit to high then low (or the inverse)
3295  * in one pass of a single mixer enum control.
3296  *
3297  * Returns 1 for success.
3298  */
3299 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3300 	struct snd_ctl_elem_value *ucontrol)
3301 {
3302 	struct soc_mixer_control *mc =
3303 		(struct soc_mixer_control *)kcontrol->private_value;
3304 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3305 	unsigned int reg = mc->reg;
3306 	unsigned int shift = mc->shift;
3307 	unsigned int mask = 1 << shift;
3308 	unsigned int invert = mc->invert != 0;
3309 	unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3310 	unsigned int val1 = (strobe ^ invert) ? mask : 0;
3311 	unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3312 	int err;
3313 
3314 	err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3315 	if (err < 0)
3316 		return err;
3317 
3318 	err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3319 	return err;
3320 }
3321 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3322 
3323 /**
3324  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3325  * @dai: DAI
3326  * @clk_id: DAI specific clock ID
3327  * @freq: new clock frequency in Hz
3328  * @dir: new clock direction - input/output.
3329  *
3330  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3331  */
3332 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3333 	unsigned int freq, int dir)
3334 {
3335 	if (dai->driver && dai->driver->ops->set_sysclk)
3336 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3337 	else if (dai->codec && dai->codec->driver->set_sysclk)
3338 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3339 						      freq, dir);
3340 	else
3341 		return -EINVAL;
3342 }
3343 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3344 
3345 /**
3346  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3347  * @codec: CODEC
3348  * @clk_id: DAI specific clock ID
3349  * @source: Source for the clock
3350  * @freq: new clock frequency in Hz
3351  * @dir: new clock direction - input/output.
3352  *
3353  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3354  */
3355 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3356 			     int source, unsigned int freq, int dir)
3357 {
3358 	if (codec->driver->set_sysclk)
3359 		return codec->driver->set_sysclk(codec, clk_id, source,
3360 						 freq, dir);
3361 	else
3362 		return -EINVAL;
3363 }
3364 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3365 
3366 /**
3367  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3368  * @dai: DAI
3369  * @div_id: DAI specific clock divider ID
3370  * @div: new clock divisor.
3371  *
3372  * Configures the clock dividers. This is used to derive the best DAI bit and
3373  * frame clocks from the system or master clock. It's best to set the DAI bit
3374  * and frame clocks as low as possible to save system power.
3375  */
3376 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3377 	int div_id, int div)
3378 {
3379 	if (dai->driver && dai->driver->ops->set_clkdiv)
3380 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
3381 	else
3382 		return -EINVAL;
3383 }
3384 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3385 
3386 /**
3387  * snd_soc_dai_set_pll - configure DAI PLL.
3388  * @dai: DAI
3389  * @pll_id: DAI specific PLL ID
3390  * @source: DAI specific source for the PLL
3391  * @freq_in: PLL input clock frequency in Hz
3392  * @freq_out: requested PLL output clock frequency in Hz
3393  *
3394  * Configures and enables PLL to generate output clock based on input clock.
3395  */
3396 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3397 	unsigned int freq_in, unsigned int freq_out)
3398 {
3399 	if (dai->driver && dai->driver->ops->set_pll)
3400 		return dai->driver->ops->set_pll(dai, pll_id, source,
3401 					 freq_in, freq_out);
3402 	else if (dai->codec && dai->codec->driver->set_pll)
3403 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3404 						   freq_in, freq_out);
3405 	else
3406 		return -EINVAL;
3407 }
3408 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3409 
3410 /*
3411  * snd_soc_codec_set_pll - configure codec PLL.
3412  * @codec: CODEC
3413  * @pll_id: DAI specific PLL ID
3414  * @source: DAI specific source for the PLL
3415  * @freq_in: PLL input clock frequency in Hz
3416  * @freq_out: requested PLL output clock frequency in Hz
3417  *
3418  * Configures and enables PLL to generate output clock based on input clock.
3419  */
3420 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3421 			  unsigned int freq_in, unsigned int freq_out)
3422 {
3423 	if (codec->driver->set_pll)
3424 		return codec->driver->set_pll(codec, pll_id, source,
3425 					      freq_in, freq_out);
3426 	else
3427 		return -EINVAL;
3428 }
3429 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3430 
3431 /**
3432  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3433  * @dai: DAI
3434  * @fmt: SND_SOC_DAIFMT_ format value.
3435  *
3436  * Configures the DAI hardware format and clocking.
3437  */
3438 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3439 {
3440 	if (dai->driver == NULL)
3441 		return -EINVAL;
3442 	if (dai->driver->ops->set_fmt == NULL)
3443 		return -ENOTSUPP;
3444 	return dai->driver->ops->set_fmt(dai, fmt);
3445 }
3446 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3447 
3448 /**
3449  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3450  * @dai: DAI
3451  * @tx_mask: bitmask representing active TX slots.
3452  * @rx_mask: bitmask representing active RX slots.
3453  * @slots: Number of slots in use.
3454  * @slot_width: Width in bits for each slot.
3455  *
3456  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3457  * specific.
3458  */
3459 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3460 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3461 {
3462 	if (dai->driver && dai->driver->ops->set_tdm_slot)
3463 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3464 				slots, slot_width);
3465 	else
3466 		return -EINVAL;
3467 }
3468 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3469 
3470 /**
3471  * snd_soc_dai_set_channel_map - configure DAI audio channel map
3472  * @dai: DAI
3473  * @tx_num: how many TX channels
3474  * @tx_slot: pointer to an array which imply the TX slot number channel
3475  *           0~num-1 uses
3476  * @rx_num: how many RX channels
3477  * @rx_slot: pointer to an array which imply the RX slot number channel
3478  *           0~num-1 uses
3479  *
3480  * configure the relationship between channel number and TDM slot number.
3481  */
3482 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3483 	unsigned int tx_num, unsigned int *tx_slot,
3484 	unsigned int rx_num, unsigned int *rx_slot)
3485 {
3486 	if (dai->driver && dai->driver->ops->set_channel_map)
3487 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3488 			rx_num, rx_slot);
3489 	else
3490 		return -EINVAL;
3491 }
3492 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3493 
3494 /**
3495  * snd_soc_dai_set_tristate - configure DAI system or master clock.
3496  * @dai: DAI
3497  * @tristate: tristate enable
3498  *
3499  * Tristates the DAI so that others can use it.
3500  */
3501 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3502 {
3503 	if (dai->driver && dai->driver->ops->set_tristate)
3504 		return dai->driver->ops->set_tristate(dai, tristate);
3505 	else
3506 		return -EINVAL;
3507 }
3508 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3509 
3510 /**
3511  * snd_soc_dai_digital_mute - configure DAI system or master clock.
3512  * @dai: DAI
3513  * @mute: mute enable
3514  *
3515  * Mutes the DAI DAC.
3516  */
3517 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3518 {
3519 	if (dai->driver && dai->driver->ops->digital_mute)
3520 		return dai->driver->ops->digital_mute(dai, mute);
3521 	else
3522 		return -ENOTSUPP;
3523 }
3524 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3525 
3526 /**
3527  * snd_soc_register_card - Register a card with the ASoC core
3528  *
3529  * @card: Card to register
3530  *
3531  */
3532 int snd_soc_register_card(struct snd_soc_card *card)
3533 {
3534 	int i, ret;
3535 
3536 	if (!card->name || !card->dev)
3537 		return -EINVAL;
3538 
3539 	for (i = 0; i < card->num_links; i++) {
3540 		struct snd_soc_dai_link *link = &card->dai_link[i];
3541 
3542 		/*
3543 		 * Codec must be specified by 1 of name or OF node,
3544 		 * not both or neither.
3545 		 */
3546 		if (!!link->codec_name == !!link->codec_of_node) {
3547 			dev_err(card->dev, "ASoC: Neither/both codec"
3548 				" name/of_node are set for %s\n", link->name);
3549 			return -EINVAL;
3550 		}
3551 		/* Codec DAI name must be specified */
3552 		if (!link->codec_dai_name) {
3553 			dev_err(card->dev, "ASoC: codec_dai_name not"
3554 				" set for %s\n", link->name);
3555 			return -EINVAL;
3556 		}
3557 
3558 		/*
3559 		 * Platform may be specified by either name or OF node, but
3560 		 * can be left unspecified, and a dummy platform will be used.
3561 		 */
3562 		if (link->platform_name && link->platform_of_node) {
3563 			dev_err(card->dev, "ASoC: Both platform name/of_node"
3564 				" are set for %s\n", link->name);
3565 			return -EINVAL;
3566 		}
3567 
3568 		/*
3569 		 * CPU device may be specified by either name or OF node, but
3570 		 * can be left unspecified, and will be matched based on DAI
3571 		 * name alone..
3572 		 */
3573 		if (link->cpu_name && link->cpu_of_node) {
3574 			dev_err(card->dev, "ASoC: Neither/both "
3575 				"cpu name/of_node are set for %s\n",link->name);
3576 			return -EINVAL;
3577 		}
3578 		/*
3579 		 * At least one of CPU DAI name or CPU device name/node must be
3580 		 * specified
3581 		 */
3582 		if (!link->cpu_dai_name &&
3583 		    !(link->cpu_name || link->cpu_of_node)) {
3584 			dev_err(card->dev, "ASoC: Neither cpu_dai_name nor "
3585 				"cpu_name/of_node are set for %s\n", link->name);
3586 			return -EINVAL;
3587 		}
3588 	}
3589 
3590 	dev_set_drvdata(card->dev, card);
3591 
3592 	snd_soc_initialize_card_lists(card);
3593 
3594 	soc_init_card_debugfs(card);
3595 
3596 	card->rtd = devm_kzalloc(card->dev,
3597 				 sizeof(struct snd_soc_pcm_runtime) *
3598 				 (card->num_links + card->num_aux_devs),
3599 				 GFP_KERNEL);
3600 	if (card->rtd == NULL)
3601 		return -ENOMEM;
3602 	card->num_rtd = 0;
3603 	card->rtd_aux = &card->rtd[card->num_links];
3604 
3605 	for (i = 0; i < card->num_links; i++)
3606 		card->rtd[i].dai_link = &card->dai_link[i];
3607 
3608 	INIT_LIST_HEAD(&card->list);
3609 	INIT_LIST_HEAD(&card->dapm_dirty);
3610 	card->instantiated = 0;
3611 	mutex_init(&card->mutex);
3612 	mutex_init(&card->dapm_mutex);
3613 
3614 	ret = snd_soc_instantiate_card(card);
3615 	if (ret != 0)
3616 		soc_cleanup_card_debugfs(card);
3617 
3618 	return ret;
3619 }
3620 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3621 
3622 /**
3623  * snd_soc_unregister_card - Unregister a card with the ASoC core
3624  *
3625  * @card: Card to unregister
3626  *
3627  */
3628 int snd_soc_unregister_card(struct snd_soc_card *card)
3629 {
3630 	if (card->instantiated)
3631 		soc_cleanup_card_resources(card);
3632 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3633 
3634 	return 0;
3635 }
3636 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3637 
3638 /*
3639  * Simplify DAI link configuration by removing ".-1" from device names
3640  * and sanitizing names.
3641  */
3642 static char *fmt_single_name(struct device *dev, int *id)
3643 {
3644 	char *found, name[NAME_SIZE];
3645 	int id1, id2;
3646 
3647 	if (dev_name(dev) == NULL)
3648 		return NULL;
3649 
3650 	strlcpy(name, dev_name(dev), NAME_SIZE);
3651 
3652 	/* are we a "%s.%d" name (platform and SPI components) */
3653 	found = strstr(name, dev->driver->name);
3654 	if (found) {
3655 		/* get ID */
3656 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3657 
3658 			/* discard ID from name if ID == -1 */
3659 			if (*id == -1)
3660 				found[strlen(dev->driver->name)] = '\0';
3661 		}
3662 
3663 	} else {
3664 		/* I2C component devices are named "bus-addr"  */
3665 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3666 			char tmp[NAME_SIZE];
3667 
3668 			/* create unique ID number from I2C addr and bus */
3669 			*id = ((id1 & 0xffff) << 16) + id2;
3670 
3671 			/* sanitize component name for DAI link creation */
3672 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3673 			strlcpy(name, tmp, NAME_SIZE);
3674 		} else
3675 			*id = 0;
3676 	}
3677 
3678 	return kstrdup(name, GFP_KERNEL);
3679 }
3680 
3681 /*
3682  * Simplify DAI link naming for single devices with multiple DAIs by removing
3683  * any ".-1" and using the DAI name (instead of device name).
3684  */
3685 static inline char *fmt_multiple_name(struct device *dev,
3686 		struct snd_soc_dai_driver *dai_drv)
3687 {
3688 	if (dai_drv->name == NULL) {
3689 		dev_err(dev, "ASoC: error - multiple DAI %s registered with"
3690 				" no name\n", dev_name(dev));
3691 		return NULL;
3692 	}
3693 
3694 	return kstrdup(dai_drv->name, GFP_KERNEL);
3695 }
3696 
3697 /**
3698  * snd_soc_register_dai - Register a DAI with the ASoC core
3699  *
3700  * @dai: DAI to register
3701  */
3702 int snd_soc_register_dai(struct device *dev,
3703 		struct snd_soc_dai_driver *dai_drv)
3704 {
3705 	struct snd_soc_codec *codec;
3706 	struct snd_soc_dai *dai;
3707 
3708 	dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
3709 
3710 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3711 	if (dai == NULL)
3712 		return -ENOMEM;
3713 
3714 	/* create DAI component name */
3715 	dai->name = fmt_single_name(dev, &dai->id);
3716 	if (dai->name == NULL) {
3717 		kfree(dai);
3718 		return -ENOMEM;
3719 	}
3720 
3721 	dai->dev = dev;
3722 	dai->driver = dai_drv;
3723 	dai->dapm.dev = dev;
3724 	if (!dai->driver->ops)
3725 		dai->driver->ops = &null_dai_ops;
3726 
3727 	mutex_lock(&client_mutex);
3728 
3729 	list_for_each_entry(codec, &codec_list, list) {
3730 		if (codec->dev == dev) {
3731 			dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
3732 				dai->name, codec->name);
3733 			dai->codec = codec;
3734 			break;
3735 		}
3736 	}
3737 
3738 	if (!dai->codec)
3739 		dai->dapm.idle_bias_off = 1;
3740 
3741 	list_add(&dai->list, &dai_list);
3742 
3743 	mutex_unlock(&client_mutex);
3744 
3745 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3746 
3747 	return 0;
3748 }
3749 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3750 
3751 /**
3752  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3753  *
3754  * @dai: DAI to unregister
3755  */
3756 void snd_soc_unregister_dai(struct device *dev)
3757 {
3758 	struct snd_soc_dai *dai;
3759 
3760 	list_for_each_entry(dai, &dai_list, list) {
3761 		if (dev == dai->dev)
3762 			goto found;
3763 	}
3764 	return;
3765 
3766 found:
3767 	mutex_lock(&client_mutex);
3768 	list_del(&dai->list);
3769 	mutex_unlock(&client_mutex);
3770 
3771 	dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
3772 	kfree(dai->name);
3773 	kfree(dai);
3774 }
3775 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3776 
3777 /**
3778  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3779  *
3780  * @dai: Array of DAIs to register
3781  * @count: Number of DAIs
3782  */
3783 int snd_soc_register_dais(struct device *dev,
3784 		struct snd_soc_dai_driver *dai_drv, size_t count)
3785 {
3786 	struct snd_soc_codec *codec;
3787 	struct snd_soc_dai *dai;
3788 	int i, ret = 0;
3789 
3790 	dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
3791 
3792 	for (i = 0; i < count; i++) {
3793 
3794 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3795 		if (dai == NULL) {
3796 			ret = -ENOMEM;
3797 			goto err;
3798 		}
3799 
3800 		/* create DAI component name */
3801 		dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3802 		if (dai->name == NULL) {
3803 			kfree(dai);
3804 			ret = -EINVAL;
3805 			goto err;
3806 		}
3807 
3808 		dai->dev = dev;
3809 		dai->driver = &dai_drv[i];
3810 		if (dai->driver->id)
3811 			dai->id = dai->driver->id;
3812 		else
3813 			dai->id = i;
3814 		dai->dapm.dev = dev;
3815 		if (!dai->driver->ops)
3816 			dai->driver->ops = &null_dai_ops;
3817 
3818 		mutex_lock(&client_mutex);
3819 
3820 		list_for_each_entry(codec, &codec_list, list) {
3821 			if (codec->dev == dev) {
3822 				dev_dbg(dev, "ASoC: Mapped DAI %s to "
3823 					"CODEC %s\n", dai->name, codec->name);
3824 				dai->codec = codec;
3825 				break;
3826 			}
3827 		}
3828 
3829 		if (!dai->codec)
3830 			dai->dapm.idle_bias_off = 1;
3831 
3832 		list_add(&dai->list, &dai_list);
3833 
3834 		mutex_unlock(&client_mutex);
3835 
3836 		dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
3837 	}
3838 
3839 	return 0;
3840 
3841 err:
3842 	for (i--; i >= 0; i--)
3843 		snd_soc_unregister_dai(dev);
3844 
3845 	return ret;
3846 }
3847 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3848 
3849 /**
3850  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3851  *
3852  * @dai: Array of DAIs to unregister
3853  * @count: Number of DAIs
3854  */
3855 void snd_soc_unregister_dais(struct device *dev, size_t count)
3856 {
3857 	int i;
3858 
3859 	for (i = 0; i < count; i++)
3860 		snd_soc_unregister_dai(dev);
3861 }
3862 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3863 
3864 /**
3865  * snd_soc_register_platform - Register a platform with the ASoC core
3866  *
3867  * @platform: platform to register
3868  */
3869 int snd_soc_register_platform(struct device *dev,
3870 		struct snd_soc_platform_driver *platform_drv)
3871 {
3872 	struct snd_soc_platform *platform;
3873 
3874 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3875 
3876 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3877 	if (platform == NULL)
3878 		return -ENOMEM;
3879 
3880 	/* create platform component name */
3881 	platform->name = fmt_single_name(dev, &platform->id);
3882 	if (platform->name == NULL) {
3883 		kfree(platform);
3884 		return -ENOMEM;
3885 	}
3886 
3887 	platform->dev = dev;
3888 	platform->driver = platform_drv;
3889 	platform->dapm.dev = dev;
3890 	platform->dapm.platform = platform;
3891 	platform->dapm.stream_event = platform_drv->stream_event;
3892 	mutex_init(&platform->mutex);
3893 
3894 	mutex_lock(&client_mutex);
3895 	list_add(&platform->list, &platform_list);
3896 	mutex_unlock(&client_mutex);
3897 
3898 	dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
3899 
3900 	return 0;
3901 }
3902 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3903 
3904 /**
3905  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3906  *
3907  * @platform: platform to unregister
3908  */
3909 void snd_soc_unregister_platform(struct device *dev)
3910 {
3911 	struct snd_soc_platform *platform;
3912 
3913 	list_for_each_entry(platform, &platform_list, list) {
3914 		if (dev == platform->dev)
3915 			goto found;
3916 	}
3917 	return;
3918 
3919 found:
3920 	mutex_lock(&client_mutex);
3921 	list_del(&platform->list);
3922 	mutex_unlock(&client_mutex);
3923 
3924 	dev_dbg(dev, "ASoC: Unregistered platform '%s'\n", platform->name);
3925 	kfree(platform->name);
3926 	kfree(platform);
3927 }
3928 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3929 
3930 static u64 codec_format_map[] = {
3931 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3932 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3933 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3934 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3935 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3936 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3937 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3938 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3939 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3940 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3941 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3942 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3943 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3944 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3945 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3946 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3947 };
3948 
3949 /* Fix up the DAI formats for endianness: codecs don't actually see
3950  * the endianness of the data but we're using the CPU format
3951  * definitions which do need to include endianness so we ensure that
3952  * codec DAIs always have both big and little endian variants set.
3953  */
3954 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3955 {
3956 	int i;
3957 
3958 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3959 		if (stream->formats & codec_format_map[i])
3960 			stream->formats |= codec_format_map[i];
3961 }
3962 
3963 /**
3964  * snd_soc_register_codec - Register a codec with the ASoC core
3965  *
3966  * @codec: codec to register
3967  */
3968 int snd_soc_register_codec(struct device *dev,
3969 			   const struct snd_soc_codec_driver *codec_drv,
3970 			   struct snd_soc_dai_driver *dai_drv,
3971 			   int num_dai)
3972 {
3973 	size_t reg_size;
3974 	struct snd_soc_codec *codec;
3975 	int ret, i;
3976 
3977 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
3978 
3979 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3980 	if (codec == NULL)
3981 		return -ENOMEM;
3982 
3983 	/* create CODEC component name */
3984 	codec->name = fmt_single_name(dev, &codec->id);
3985 	if (codec->name == NULL) {
3986 		kfree(codec);
3987 		return -ENOMEM;
3988 	}
3989 
3990 	if (codec_drv->compress_type)
3991 		codec->compress_type = codec_drv->compress_type;
3992 	else
3993 		codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3994 
3995 	codec->write = codec_drv->write;
3996 	codec->read = codec_drv->read;
3997 	codec->volatile_register = codec_drv->volatile_register;
3998 	codec->readable_register = codec_drv->readable_register;
3999 	codec->writable_register = codec_drv->writable_register;
4000 	codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4001 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4002 	codec->dapm.dev = dev;
4003 	codec->dapm.codec = codec;
4004 	codec->dapm.seq_notifier = codec_drv->seq_notifier;
4005 	codec->dapm.stream_event = codec_drv->stream_event;
4006 	codec->dev = dev;
4007 	codec->driver = codec_drv;
4008 	codec->num_dai = num_dai;
4009 	mutex_init(&codec->mutex);
4010 
4011 	/* allocate CODEC register cache */
4012 	if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
4013 		reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
4014 		codec->reg_size = reg_size;
4015 		/* it is necessary to make a copy of the default register cache
4016 		 * because in the case of using a compression type that requires
4017 		 * the default register cache to be marked as the
4018 		 * kernel might have freed the array by the time we initialize
4019 		 * the cache.
4020 		 */
4021 		if (codec_drv->reg_cache_default) {
4022 			codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
4023 						      reg_size, GFP_KERNEL);
4024 			if (!codec->reg_def_copy) {
4025 				ret = -ENOMEM;
4026 				goto fail;
4027 			}
4028 		}
4029 	}
4030 
4031 	if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
4032 		if (!codec->volatile_register)
4033 			codec->volatile_register = snd_soc_default_volatile_register;
4034 		if (!codec->readable_register)
4035 			codec->readable_register = snd_soc_default_readable_register;
4036 		if (!codec->writable_register)
4037 			codec->writable_register = snd_soc_default_writable_register;
4038 	}
4039 
4040 	for (i = 0; i < num_dai; i++) {
4041 		fixup_codec_formats(&dai_drv[i].playback);
4042 		fixup_codec_formats(&dai_drv[i].capture);
4043 	}
4044 
4045 	mutex_lock(&client_mutex);
4046 	list_add(&codec->list, &codec_list);
4047 	mutex_unlock(&client_mutex);
4048 
4049 	/* register any DAIs */
4050 	if (num_dai) {
4051 		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4052 		if (ret < 0)
4053 			dev_err(codec->dev, "ASoC: Failed to regster"
4054 				" DAIs: %d\n", ret);
4055 	}
4056 
4057 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
4058 	return 0;
4059 
4060 fail:
4061 	kfree(codec->name);
4062 	kfree(codec);
4063 	return ret;
4064 }
4065 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4066 
4067 /**
4068  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4069  *
4070  * @codec: codec to unregister
4071  */
4072 void snd_soc_unregister_codec(struct device *dev)
4073 {
4074 	struct snd_soc_codec *codec;
4075 	int i;
4076 
4077 	list_for_each_entry(codec, &codec_list, list) {
4078 		if (dev == codec->dev)
4079 			goto found;
4080 	}
4081 	return;
4082 
4083 found:
4084 	if (codec->num_dai)
4085 		for (i = 0; i < codec->num_dai; i++)
4086 			snd_soc_unregister_dai(dev);
4087 
4088 	mutex_lock(&client_mutex);
4089 	list_del(&codec->list);
4090 	mutex_unlock(&client_mutex);
4091 
4092 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
4093 
4094 	snd_soc_cache_exit(codec);
4095 	kfree(codec->reg_def_copy);
4096 	kfree(codec->name);
4097 	kfree(codec);
4098 }
4099 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4100 
4101 /* Retrieve a card's name from device tree */
4102 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4103 			       const char *propname)
4104 {
4105 	struct device_node *np = card->dev->of_node;
4106 	int ret;
4107 
4108 	ret = of_property_read_string_index(np, propname, 0, &card->name);
4109 	/*
4110 	 * EINVAL means the property does not exist. This is fine providing
4111 	 * card->name was previously set, which is checked later in
4112 	 * snd_soc_register_card.
4113 	 */
4114 	if (ret < 0 && ret != -EINVAL) {
4115 		dev_err(card->dev,
4116 			"ASoC: Property '%s' could not be read: %d\n",
4117 			propname, ret);
4118 		return ret;
4119 	}
4120 
4121 	return 0;
4122 }
4123 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4124 
4125 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4126 				   const char *propname)
4127 {
4128 	struct device_node *np = card->dev->of_node;
4129 	int num_routes;
4130 	struct snd_soc_dapm_route *routes;
4131 	int i, ret;
4132 
4133 	num_routes = of_property_count_strings(np, propname);
4134 	if (num_routes < 0 || num_routes & 1) {
4135 		dev_err(card->dev, "ASoC: Property '%s' does not exist or its"
4136 			" length is not even\n", propname);
4137 		return -EINVAL;
4138 	}
4139 	num_routes /= 2;
4140 	if (!num_routes) {
4141 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4142 			propname);
4143 		return -EINVAL;
4144 	}
4145 
4146 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4147 			      GFP_KERNEL);
4148 	if (!routes) {
4149 		dev_err(card->dev,
4150 			"ASoC: Could not allocate DAPM route table\n");
4151 		return -EINVAL;
4152 	}
4153 
4154 	for (i = 0; i < num_routes; i++) {
4155 		ret = of_property_read_string_index(np, propname,
4156 			2 * i, &routes[i].sink);
4157 		if (ret) {
4158 			dev_err(card->dev, "ASoC: Property '%s' index %d"
4159 				" could not be read: %d\n", propname, 2 * i,
4160 				ret);
4161 			kfree(routes);
4162 			return -EINVAL;
4163 		}
4164 		ret = of_property_read_string_index(np, propname,
4165 			(2 * i) + 1, &routes[i].source);
4166 		if (ret) {
4167 			dev_err(card->dev,
4168 				"ASoC: Property '%s' index %d could not be"
4169 				" read: %d\n", propname, (2 * i) + 1, ret);
4170 			kfree(routes);
4171 			return -EINVAL;
4172 		}
4173 	}
4174 
4175 	card->num_dapm_routes = num_routes;
4176 	card->dapm_routes = routes;
4177 
4178 	return 0;
4179 }
4180 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4181 
4182 static int __init snd_soc_init(void)
4183 {
4184 #ifdef CONFIG_DEBUG_FS
4185 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4186 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4187 		pr_warn("ASoC: Failed to create debugfs directory\n");
4188 		snd_soc_debugfs_root = NULL;
4189 	}
4190 
4191 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4192 				 &codec_list_fops))
4193 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4194 
4195 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4196 				 &dai_list_fops))
4197 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4198 
4199 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4200 				 &platform_list_fops))
4201 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
4202 #endif
4203 
4204 	snd_soc_util_init();
4205 
4206 	return platform_driver_register(&soc_driver);
4207 }
4208 module_init(snd_soc_init);
4209 
4210 static void __exit snd_soc_exit(void)
4211 {
4212 	snd_soc_util_exit();
4213 
4214 #ifdef CONFIG_DEBUG_FS
4215 	debugfs_remove_recursive(snd_soc_debugfs_root);
4216 #endif
4217 	platform_driver_unregister(&soc_driver);
4218 }
4219 module_exit(snd_soc_exit);
4220 
4221 /* Module information */
4222 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4223 MODULE_DESCRIPTION("ALSA SoC Core");
4224 MODULE_LICENSE("GPL");
4225 MODULE_ALIAS("platform:soc-audio");
4226