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