xref: /linux/sound/soc/soc-core.c (revision 56cf10db2ae0bb90c69b644d639b559106d52a8d)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // soc-core.c  --  ALSA SoC Audio Layer
4 //
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Copyright 2005 Openedhand Ltd.
7 // Copyright (C) 2010 Slimlogic Ltd.
8 // Copyright (C) 2010 Texas Instruments Inc.
9 //
10 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
11 //         with code, comments and ideas from :-
12 //         Richard Purdie <richard@openedhand.com>
13 //
14 //  TODO:
15 //   o Add hw rules to enforce rates, etc.
16 //   o More testing with other codecs/machines.
17 //   o Add more codecs and platforms to ensure good API coverage.
18 //   o Support TDM on PCM and I2S
19 
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/pm.h>
25 #include <linux/bitops.h>
26 #include <linux/debugfs.h>
27 #include <linux/platform_device.h>
28 #include <linux/pinctrl/consumer.h>
29 #include <linux/ctype.h>
30 #include <linux/slab.h>
31 #include <linux/of.h>
32 #include <linux/of_graph.h>
33 #include <linux/dmi.h>
34 #include <linux/acpi.h>
35 #include <linux/string_choices.h>
36 #include <sound/core.h>
37 #include <sound/pcm.h>
38 #include <sound/pcm_params.h>
39 #include <sound/soc.h>
40 #include <sound/soc-dpcm.h>
41 #include <sound/soc-topology.h>
42 #include <sound/soc-link.h>
43 #include <sound/initval.h>
44 
45 #define CREATE_TRACE_POINTS
46 #include <trace/events/asoc.h>
47 
48 static DEFINE_MUTEX(client_mutex);
49 static LIST_HEAD(component_list);
50 static LIST_HEAD(unbind_card_list);
51 
52 #define for_each_component(component)			\
53 	list_for_each_entry(component, &component_list, list)
54 
55 /*
56  * This is used if driver don't need to have CPU/Codec/Platform
57  * dai_link. see soc.h
58  */
59 struct snd_soc_dai_link_component null_dailink_component[0];
60 EXPORT_SYMBOL_GPL(null_dailink_component);
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 
pmdown_time_show(struct device * dev,struct device_attribute * attr,char * buf)71 static ssize_t pmdown_time_show(struct device *dev,
72 				struct device_attribute *attr, char *buf)
73 {
74 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
75 
76 	return sysfs_emit(buf, "%ld\n", rtd->pmdown_time);
77 }
78 
pmdown_time_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)79 static ssize_t pmdown_time_store(struct device *dev,
80 				 struct device_attribute *attr,
81 				 const char *buf, size_t count)
82 {
83 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
84 	int ret;
85 
86 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
87 	if (ret)
88 		return ret;
89 
90 	return count;
91 }
92 
93 static DEVICE_ATTR_RW(pmdown_time);
94 
95 static struct attribute *soc_dev_attrs[] = {
96 	&dev_attr_pmdown_time.attr,
97 	NULL
98 };
99 
soc_dev_attr_is_visible(struct kobject * kobj,struct attribute * attr,int idx)100 static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
101 				       struct attribute *attr, int idx)
102 {
103 	struct device *dev = kobj_to_dev(kobj);
104 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
105 
106 	if (!rtd)
107 		return 0;
108 
109 	if (attr == &dev_attr_pmdown_time.attr)
110 		return attr->mode; /* always visible */
111 	return rtd->dai_link->num_codecs ? attr->mode : 0; /* enabled only with codec */
112 }
113 
114 static const struct attribute_group soc_dapm_dev_group = {
115 	.attrs = snd_soc_dapm_dev_attrs,
116 	.is_visible = soc_dev_attr_is_visible,
117 };
118 
119 static const struct attribute_group soc_dev_group = {
120 	.attrs = soc_dev_attrs,
121 	.is_visible = soc_dev_attr_is_visible,
122 };
123 
124 static const struct attribute_group *soc_dev_attr_groups[] = {
125 	&soc_dapm_dev_group,
126 	&soc_dev_group,
127 	NULL
128 };
129 
130 #ifdef CONFIG_DEBUG_FS
131 struct dentry *snd_soc_debugfs_root;
132 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
133 
soc_init_component_debugfs(struct snd_soc_component * component)134 static void soc_init_component_debugfs(struct snd_soc_component *component)
135 {
136 	if (!component->card->debugfs_card_root)
137 		return;
138 
139 	if (component->debugfs_prefix) {
140 		char *name;
141 
142 		name = kasprintf(GFP_KERNEL, "%s:%s",
143 			component->debugfs_prefix, component->name);
144 		if (name) {
145 			component->debugfs_root = debugfs_create_dir(name,
146 				component->card->debugfs_card_root);
147 			kfree(name);
148 		}
149 	} else {
150 		component->debugfs_root = debugfs_create_dir(component->name,
151 				component->card->debugfs_card_root);
152 	}
153 
154 	snd_soc_dapm_debugfs_init(snd_soc_component_to_dapm(component),
155 		component->debugfs_root);
156 }
157 
soc_cleanup_component_debugfs(struct snd_soc_component * component)158 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
159 {
160 	if (!component->debugfs_root)
161 		return;
162 	debugfs_remove_recursive(component->debugfs_root);
163 	component->debugfs_root = NULL;
164 }
165 
dai_list_show(struct seq_file * m,void * v)166 static int dai_list_show(struct seq_file *m, void *v)
167 {
168 	struct snd_soc_component *component;
169 	struct snd_soc_dai *dai;
170 
171 	mutex_lock(&client_mutex);
172 
173 	for_each_component(component)
174 		for_each_component_dais(component, dai)
175 			seq_printf(m, "%s\n", dai->name);
176 
177 	mutex_unlock(&client_mutex);
178 
179 	return 0;
180 }
181 DEFINE_SHOW_ATTRIBUTE(dai_list);
182 
component_list_show(struct seq_file * m,void * v)183 static int component_list_show(struct seq_file *m, void *v)
184 {
185 	struct snd_soc_component *component;
186 
187 	mutex_lock(&client_mutex);
188 
189 	for_each_component(component)
190 		seq_printf(m, "%s\n", component->name);
191 
192 	mutex_unlock(&client_mutex);
193 
194 	return 0;
195 }
196 DEFINE_SHOW_ATTRIBUTE(component_list);
197 
soc_init_card_debugfs(struct snd_soc_card * card)198 static void soc_init_card_debugfs(struct snd_soc_card *card)
199 {
200 	card->debugfs_card_root = debugfs_create_dir(card->name,
201 						     snd_soc_debugfs_root);
202 
203 	debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
204 			   &card->pop_time);
205 
206 	snd_soc_dapm_debugfs_init(snd_soc_card_to_dapm(card), card->debugfs_card_root);
207 }
208 
soc_cleanup_card_debugfs(struct snd_soc_card * card)209 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
210 {
211 	debugfs_remove_recursive(card->debugfs_card_root);
212 	card->debugfs_card_root = NULL;
213 }
214 
snd_soc_debugfs_init(void)215 static void snd_soc_debugfs_init(void)
216 {
217 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
218 
219 	debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
220 			    &dai_list_fops);
221 
222 	debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
223 			    &component_list_fops);
224 }
225 
snd_soc_debugfs_exit(void)226 static void snd_soc_debugfs_exit(void)
227 {
228 	debugfs_remove_recursive(snd_soc_debugfs_root);
229 }
230 
231 #else
232 
soc_init_component_debugfs(struct snd_soc_component * component)233 static inline void soc_init_component_debugfs(struct snd_soc_component *component) { }
soc_cleanup_component_debugfs(struct snd_soc_component * component)234 static inline void soc_cleanup_component_debugfs(struct snd_soc_component *component) { }
soc_init_card_debugfs(struct snd_soc_card * card)235 static inline void soc_init_card_debugfs(struct snd_soc_card *card) { }
soc_cleanup_card_debugfs(struct snd_soc_card * card)236 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) { }
snd_soc_debugfs_init(void)237 static inline void snd_soc_debugfs_init(void) { }
snd_soc_debugfs_exit(void)238 static inline void snd_soc_debugfs_exit(void) { }
239 
240 #endif
241 
snd_soc_is_match_dai_args(const struct of_phandle_args * args1,const struct of_phandle_args * args2)242 static int snd_soc_is_match_dai_args(const struct of_phandle_args *args1,
243 				     const struct of_phandle_args *args2)
244 {
245 	if (!args1 || !args2)
246 		return 0;
247 
248 	if (args1->np != args2->np)
249 		return 0;
250 
251 	for (int i = 0; i < args1->args_count; i++)
252 		if (args1->args[i] != args2->args[i])
253 			return 0;
254 
255 	return 1;
256 }
257 
snd_soc_dlc_component_is_empty(struct snd_soc_dai_link_component * dlc)258 static inline int snd_soc_dlc_component_is_empty(struct snd_soc_dai_link_component *dlc)
259 {
260 	return !(dlc->dai_args || dlc->name || dlc->of_node);
261 }
262 
snd_soc_dlc_component_is_invalid(struct snd_soc_dai_link_component * dlc)263 static inline int snd_soc_dlc_component_is_invalid(struct snd_soc_dai_link_component *dlc)
264 {
265 	return (dlc->name && dlc->of_node);
266 }
267 
snd_soc_dlc_dai_is_empty(struct snd_soc_dai_link_component * dlc)268 static inline int snd_soc_dlc_dai_is_empty(struct snd_soc_dai_link_component *dlc)
269 {
270 	return !(dlc->dai_args || dlc->dai_name);
271 }
272 
snd_soc_is_matching_dai(const struct snd_soc_dai_link_component * dlc,struct snd_soc_dai * dai)273 static int snd_soc_is_matching_dai(const struct snd_soc_dai_link_component *dlc,
274 				   struct snd_soc_dai *dai)
275 {
276 	if (!dlc)
277 		return 0;
278 
279 	if (dlc->dai_args)
280 		return snd_soc_is_match_dai_args(dai->driver->dai_args, dlc->dai_args);
281 
282 	if (!dlc->dai_name)
283 		return 1;
284 
285 	/* see snd_soc_dai_name_get() */
286 
287 	if (dai->driver->name &&
288 	    strcmp(dlc->dai_name, dai->driver->name) == 0)
289 		return 1;
290 
291 	if (strcmp(dlc->dai_name, dai->name) == 0)
292 		return 1;
293 
294 	if (dai->component->name &&
295 	    strcmp(dlc->dai_name, dai->component->name) == 0)
296 		return 1;
297 
298 	return 0;
299 }
300 
snd_soc_dai_name_get(const struct snd_soc_dai * dai)301 const char *snd_soc_dai_name_get(const struct snd_soc_dai *dai)
302 {
303 	/* see snd_soc_is_matching_dai() */
304 	if (dai->driver->name)
305 		return dai->driver->name;
306 
307 	if (dai->name)
308 		return dai->name;
309 
310 	if (dai->component->name)
311 		return dai->component->name;
312 
313 	return NULL;
314 }
315 EXPORT_SYMBOL_GPL(snd_soc_dai_name_get);
316 
snd_soc_rtd_add_component(struct snd_soc_pcm_runtime * rtd,struct snd_soc_component * component)317 static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd,
318 				     struct snd_soc_component *component)
319 {
320 	struct snd_soc_component *comp;
321 	int i;
322 
323 	for_each_rtd_components(rtd, i, comp) {
324 		/* already connected */
325 		if (comp == component)
326 			return 0;
327 	}
328 
329 	/* see for_each_rtd_components */
330 	rtd->num_components++; // increment flex array count at first
331 	rtd->components[rtd->num_components - 1] = component;
332 
333 	return 0;
334 }
335 
snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime * rtd,const char * driver_name)336 struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
337 						const char *driver_name)
338 {
339 	struct snd_soc_component *component;
340 	int i;
341 
342 	if (!driver_name)
343 		return NULL;
344 
345 	/*
346 	 * NOTE
347 	 *
348 	 * snd_soc_rtdcom_lookup() will find component from rtd by using
349 	 * specified driver name.
350 	 * But, if many components which have same driver name are connected
351 	 * to 1 rtd, this function will return 1st found component.
352 	 */
353 	for_each_rtd_components(rtd, i, component) {
354 		const char *component_name = component->driver->name;
355 
356 		if (!component_name)
357 			continue;
358 
359 		if ((component_name == driver_name) ||
360 		    strcmp(component_name, driver_name) == 0)
361 			return component;
362 	}
363 
364 	return NULL;
365 }
366 EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
367 
368 struct snd_soc_component
snd_soc_lookup_component_nolocked(struct device * dev,const char * driver_name)369 *snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
370 {
371 	struct snd_soc_component *component;
372 
373 	for_each_component(component) {
374 		if (dev != component->dev)
375 			continue;
376 
377 		if (!driver_name)
378 			return component;
379 
380 		if (!component->driver->name)
381 			continue;
382 
383 		if (component->driver->name == driver_name)
384 			return component;
385 
386 		if (strcmp(component->driver->name, driver_name) == 0)
387 			return component;
388 	}
389 
390 	return NULL;
391 }
392 EXPORT_SYMBOL_GPL(snd_soc_lookup_component_nolocked);
393 
snd_soc_lookup_component(struct device * dev,const char * driver_name)394 struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
395 						   const char *driver_name)
396 {
397 	struct snd_soc_component *component;
398 
399 	mutex_lock(&client_mutex);
400 	component = snd_soc_lookup_component_nolocked(dev, driver_name);
401 	mutex_unlock(&client_mutex);
402 
403 	return component;
404 }
405 EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
406 
407 struct snd_soc_pcm_runtime
snd_soc_get_pcm_runtime(struct snd_soc_card * card,struct snd_soc_dai_link * dai_link)408 *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
409 			 struct snd_soc_dai_link *dai_link)
410 {
411 	struct snd_soc_pcm_runtime *rtd;
412 
413 	for_each_card_rtds(card, rtd) {
414 		if (rtd->dai_link == dai_link)
415 			return rtd;
416 	}
417 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name);
418 	return NULL;
419 }
420 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
421 
422 /*
423  * Power down the audio subsystem pmdown_time msecs after close is called.
424  * This is to ensure there are no pops or clicks in between any music tracks
425  * due to DAPM power cycling.
426  */
snd_soc_close_delayed_work(struct snd_soc_pcm_runtime * rtd)427 void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
428 {
429 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
430 	int playback = SNDRV_PCM_STREAM_PLAYBACK;
431 
432 	snd_soc_dpcm_mutex_lock(rtd);
433 
434 	dev_dbg(rtd->dev,
435 		"ASoC: pop wq checking: %s status: %s waiting: %s\n",
436 		codec_dai->driver->playback.stream_name,
437 		snd_soc_dai_stream_active(codec_dai, playback) ?
438 		"active" : "inactive",
439 		str_yes_no(rtd->pop_wait));
440 
441 	/* are we waiting on this codec DAI stream */
442 	if (rtd->pop_wait == 1) {
443 		rtd->pop_wait = 0;
444 		snd_soc_dapm_stream_event(rtd, playback,
445 					  SND_SOC_DAPM_STREAM_STOP);
446 	}
447 
448 	snd_soc_dpcm_mutex_unlock(rtd);
449 }
450 EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work);
451 
soc_release_rtd_dev(struct device * dev)452 static void soc_release_rtd_dev(struct device *dev)
453 {
454 	/* "dev" means "rtd->dev" */
455 	kfree(dev);
456 }
457 
soc_free_pcm_runtime(struct snd_soc_pcm_runtime * rtd)458 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
459 {
460 	if (!rtd)
461 		return;
462 
463 	list_del(&rtd->list);
464 
465 	flush_delayed_work(&rtd->delayed_work);
466 	snd_soc_pcm_component_free(rtd);
467 
468 	/*
469 	 * we don't need to call kfree() for rtd->dev
470 	 * see
471 	 *	soc_release_rtd_dev()
472 	 *
473 	 * We don't need rtd->dev NULL check, because
474 	 * it is alloced *before* rtd.
475 	 * see
476 	 *	soc_new_pcm_runtime()
477 	 *
478 	 * We don't need to mind freeing for rtd,
479 	 * because it was created from dev (= rtd->dev)
480 	 * see
481 	 *	soc_new_pcm_runtime()
482 	 *
483 	 *		rtd = devm_kzalloc(dev, ...);
484 	 *		rtd->dev = dev
485 	 */
486 	device_unregister(rtd->dev);
487 }
488 
close_delayed_work(struct work_struct * work)489 static void close_delayed_work(struct work_struct *work) {
490 	struct snd_soc_pcm_runtime *rtd =
491 			container_of(work, struct snd_soc_pcm_runtime,
492 				     delayed_work.work);
493 
494 	if (rtd->close_delayed_work_func)
495 		rtd->close_delayed_work_func(rtd);
496 }
497 
soc_new_pcm_runtime(struct snd_soc_card * card,struct snd_soc_dai_link * dai_link)498 static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
499 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
500 {
501 	struct snd_soc_pcm_runtime *rtd;
502 	struct device *dev;
503 	int ret;
504 	int stream;
505 
506 	/*
507 	 * for rtd->dev
508 	 */
509 	dev = kzalloc_obj(struct device);
510 	if (!dev)
511 		return NULL;
512 
513 	dev->parent	= card->dev;
514 	dev->release	= soc_release_rtd_dev;
515 
516 	dev_set_name(dev, "%s", dai_link->name);
517 
518 	ret = device_register(dev);
519 	if (ret < 0) {
520 		put_device(dev); /* soc_release_rtd_dev */
521 		return NULL;
522 	}
523 
524 	/*
525 	 * for rtd
526 	 */
527 	rtd = devm_kzalloc(dev,
528 			   struct_size(rtd, components,
529 				       dai_link->num_cpus +
530 				       dai_link->num_codecs +
531 				       dai_link->num_platforms),
532 			   GFP_KERNEL);
533 	if (!rtd) {
534 		device_unregister(dev);
535 		return NULL;
536 	}
537 
538 	rtd->dev = dev;
539 	INIT_LIST_HEAD(&rtd->list);
540 	for_each_pcm_streams(stream) {
541 		INIT_LIST_HEAD(&rtd->dpcm[stream].be_clients);
542 		INIT_LIST_HEAD(&rtd->dpcm[stream].fe_clients);
543 	}
544 	dev_set_drvdata(dev, rtd);
545 	INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
546 
547 	if ((dai_link->num_cpus + dai_link->num_codecs) == 0) {
548 		dev_err(dev, "ASoC: it has no CPU or codec DAIs\n");
549 		goto free_rtd;
550 	}
551 
552 	/*
553 	 * for rtd->dais
554 	 */
555 	rtd->dais = devm_kcalloc(dev, dai_link->num_cpus + dai_link->num_codecs,
556 					sizeof(struct snd_soc_dai *),
557 					GFP_KERNEL);
558 	if (!rtd->dais)
559 		goto free_rtd;
560 
561 	/*
562 	 * dais = [][][][][][][][][][][][][][][][][][]
563 	 *	  ^cpu_dais         ^codec_dais
564 	 *	  |--- num_cpus ---|--- num_codecs --|
565 	 * see
566 	 *	snd_soc_rtd_to_cpu()
567 	 *	snd_soc_rtd_to_codec()
568 	 */
569 	rtd->card	= card;
570 	rtd->dai_link	= dai_link;
571 	rtd->id		= card->num_rtd++;
572 	rtd->pmdown_time = pmdown_time;			/* default power off timeout */
573 
574 	/* see for_each_card_rtds */
575 	list_add_tail(&rtd->list, &card->rtd_list);
576 
577 	ret = device_add_groups(dev, soc_dev_attr_groups);
578 	if (ret < 0)
579 		goto free_rtd;
580 
581 	return rtd;
582 
583 free_rtd:
584 	soc_free_pcm_runtime(rtd);
585 	return NULL;
586 }
587 
snd_soc_fill_dummy_dai(struct snd_soc_card * card)588 static void snd_soc_fill_dummy_dai(struct snd_soc_card *card)
589 {
590 	struct snd_soc_dai_link *dai_link;
591 	int i;
592 
593 	/*
594 	 * COMP_DUMMY() creates size 0 array on dai_link.
595 	 * Fill it as dummy DAI in case of CPU/Codec here.
596 	 * Do nothing for Platform.
597 	 */
598 	for_each_card_prelinks(card, i, dai_link) {
599 		if (dai_link->num_cpus == 0 && dai_link->cpus) {
600 			dai_link->num_cpus	= 1;
601 			dai_link->cpus		= &snd_soc_dummy_dlc;
602 		}
603 		if (dai_link->num_codecs == 0 && dai_link->codecs) {
604 			dai_link->num_codecs	= 1;
605 			dai_link->codecs	= &snd_soc_dummy_dlc;
606 		}
607 	}
608 }
609 
snd_soc_flush_all_delayed_work(struct snd_soc_card * card)610 static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
611 {
612 	struct snd_soc_pcm_runtime *rtd;
613 
614 	for_each_card_rtds(card, rtd)
615 		flush_delayed_work(&rtd->delayed_work);
616 }
617 
618 #ifdef CONFIG_PM_SLEEP
soc_playback_digital_mute(struct snd_soc_card * card,int mute)619 static void soc_playback_digital_mute(struct snd_soc_card *card, int mute)
620 {
621 	struct snd_soc_pcm_runtime *rtd;
622 	struct snd_soc_dai *dai;
623 	int playback = SNDRV_PCM_STREAM_PLAYBACK;
624 	int i;
625 
626 	for_each_card_rtds(card, rtd) {
627 
628 		if (rtd->dai_link->ignore_suspend)
629 			continue;
630 
631 		for_each_rtd_dais(rtd, i, dai) {
632 			if (snd_soc_dai_stream_active(dai, playback))
633 				snd_soc_dai_digital_mute(dai, mute, playback);
634 		}
635 	}
636 }
637 
soc_dapm_suspend_resume(struct snd_soc_card * card,int event)638 static void soc_dapm_suspend_resume(struct snd_soc_card *card, int event)
639 {
640 	struct snd_soc_pcm_runtime *rtd;
641 	int stream;
642 
643 	for_each_card_rtds(card, rtd) {
644 
645 		if (rtd->dai_link->ignore_suspend)
646 			continue;
647 
648 		for_each_pcm_streams(stream)
649 			snd_soc_dapm_stream_event(rtd, stream, event);
650 	}
651 }
652 
653 /* powers down audio subsystem for suspend */
snd_soc_suspend(struct device * dev)654 int snd_soc_suspend(struct device *dev)
655 {
656 	struct snd_soc_card *card = dev_get_drvdata(dev);
657 	struct snd_soc_component *component;
658 	struct snd_soc_pcm_runtime *rtd;
659 	int i;
660 
661 	/* If the card is not initialized yet there is nothing to do */
662 	if (!snd_soc_card_is_instantiated(card))
663 		return 0;
664 
665 	/*
666 	 * Due to the resume being scheduled into a workqueue we could
667 	 * suspend before that's finished - wait for it to complete.
668 	 */
669 	snd_power_wait(card->snd_card);
670 
671 	/* we're going to block userspace touching us until resume completes */
672 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
673 
674 	/* mute any active DACs */
675 	soc_playback_digital_mute(card, 1);
676 
677 	/* suspend all pcms */
678 	for_each_card_rtds(card, rtd) {
679 		if (rtd->dai_link->ignore_suspend)
680 			continue;
681 
682 		snd_pcm_suspend_all(rtd->pcm);
683 	}
684 
685 	snd_soc_card_suspend_pre(card);
686 
687 	/* close any waiting streams */
688 	snd_soc_flush_all_delayed_work(card);
689 
690 	soc_dapm_suspend_resume(card, SND_SOC_DAPM_STREAM_SUSPEND);
691 
692 	/* Recheck all endpoints too, their state is affected by suspend */
693 	snd_soc_dapm_mark_endpoints_dirty(card);
694 	snd_soc_dapm_sync(snd_soc_card_to_dapm(card));
695 
696 	/* suspend all COMPONENTs */
697 	for_each_card_rtds(card, rtd) {
698 
699 		if (rtd->dai_link->ignore_suspend)
700 			continue;
701 
702 		for_each_rtd_components(rtd, i, component) {
703 			struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
704 
705 			/*
706 			 * ignore if component was already suspended
707 			 */
708 			if (snd_soc_component_is_suspended(component))
709 				continue;
710 
711 			/*
712 			 * If there are paths active then the COMPONENT will be
713 			 * held with bias _ON and should not be suspended.
714 			 */
715 			switch (snd_soc_dapm_get_bias_level(dapm)) {
716 			case SND_SOC_BIAS_STANDBY:
717 				/*
718 				 * If the COMPONENT is capable of idle
719 				 * bias off then being in STANDBY
720 				 * means it's doing something,
721 				 * otherwise fall through.
722 				 */
723 				if (!snd_soc_dapm_get_idle_bias(dapm)) {
724 					dev_dbg(component->dev,
725 						"ASoC: idle_bias_off CODEC on over suspend\n");
726 					break;
727 				}
728 				fallthrough;
729 
730 			case SND_SOC_BIAS_OFF:
731 				snd_soc_component_suspend(component);
732 				if (component->regmap)
733 					regcache_mark_dirty(component->regmap);
734 				/* deactivate pins to sleep state */
735 				pinctrl_pm_select_sleep_state(component->dev);
736 				break;
737 			default:
738 				dev_dbg(component->dev,
739 					"ASoC: COMPONENT is on over suspend\n");
740 				break;
741 			}
742 		}
743 	}
744 
745 	snd_soc_card_suspend_post(card);
746 
747 	return 0;
748 }
749 EXPORT_SYMBOL_GPL(snd_soc_suspend);
750 
751 /*
752  * deferred resume work, so resume can complete before we finished
753  * setting our codec back up, which can be very slow on I2C
754  */
soc_resume_deferred(struct work_struct * work)755 static void soc_resume_deferred(struct work_struct *work)
756 {
757 	struct snd_soc_card *card =
758 			container_of(work, struct snd_soc_card,
759 				     deferred_resume_work);
760 	struct snd_soc_component *component;
761 
762 	/*
763 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
764 	 * so userspace apps are blocked from touching us
765 	 */
766 
767 	dev_dbg(card->dev, "ASoC: starting resume work\n");
768 
769 	/* Bring us up into D2 so that DAPM starts enabling things */
770 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
771 
772 	snd_soc_card_resume_pre(card);
773 
774 	for_each_card_components(card, component) {
775 		if (snd_soc_component_is_suspended(component))
776 			snd_soc_component_resume(component);
777 	}
778 
779 	soc_dapm_suspend_resume(card, SND_SOC_DAPM_STREAM_RESUME);
780 
781 	/* unmute any active DACs */
782 	soc_playback_digital_mute(card, 0);
783 
784 	snd_soc_card_resume_post(card);
785 
786 	dev_dbg(card->dev, "ASoC: resume work completed\n");
787 
788 	/* Recheck all endpoints too, their state is affected by suspend */
789 	snd_soc_dapm_mark_endpoints_dirty(card);
790 	snd_soc_dapm_sync(snd_soc_card_to_dapm(card));
791 
792 	/* userspace can access us now we are back as we were before */
793 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
794 }
795 
796 /* powers up audio subsystem after a suspend */
snd_soc_resume(struct device * dev)797 int snd_soc_resume(struct device *dev)
798 {
799 	struct snd_soc_card *card = dev_get_drvdata(dev);
800 	struct snd_soc_component *component;
801 
802 	/* If the card is not initialized yet there is nothing to do */
803 	if (!snd_soc_card_is_instantiated(card))
804 		return 0;
805 
806 	/* activate pins from sleep state */
807 	for_each_card_components(card, component)
808 		if (snd_soc_component_active(component))
809 			pinctrl_pm_select_default_state(component->dev);
810 
811 	dev_dbg(dev, "ASoC: Scheduling resume work\n");
812 	if (!schedule_work(&card->deferred_resume_work))
813 		dev_err(dev, "ASoC: resume work item may be lost\n");
814 
815 	return 0;
816 }
817 EXPORT_SYMBOL_GPL(snd_soc_resume);
818 
soc_resume_init(struct snd_soc_card * card)819 static void soc_resume_init(struct snd_soc_card *card)
820 {
821 	/* deferred resume work */
822 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
823 }
824 #else
825 #define snd_soc_suspend NULL
826 #define snd_soc_resume NULL
soc_resume_init(struct snd_soc_card * card)827 static inline void soc_resume_init(struct snd_soc_card *card) { }
828 #endif
829 
830 static struct device_node
soc_component_to_node(struct snd_soc_component * component)831 *soc_component_to_node(struct snd_soc_component *component)
832 {
833 	struct device_node *of_node;
834 
835 	of_node = component->dev->of_node;
836 	if (!of_node && component->dev->parent)
837 		of_node = component->dev->parent->of_node;
838 
839 	return of_node;
840 }
841 
snd_soc_copy_dai_args(struct device * dev,const struct of_phandle_args * args)842 struct of_phandle_args *snd_soc_copy_dai_args(struct device *dev,
843 					      const struct of_phandle_args *args)
844 {
845 	struct of_phandle_args *ret = devm_kzalloc(dev, sizeof(*ret), GFP_KERNEL);
846 
847 	if (!ret)
848 		return NULL;
849 
850 	*ret = *args;
851 
852 	return ret;
853 }
854 EXPORT_SYMBOL_GPL(snd_soc_copy_dai_args);
855 
snd_soc_is_matching_component(const struct snd_soc_dai_link_component * dlc,struct snd_soc_component * component)856 static int snd_soc_is_matching_component(
857 	const struct snd_soc_dai_link_component *dlc,
858 	struct snd_soc_component *component)
859 {
860 	struct device_node *component_of_node;
861 
862 	if (!dlc)
863 		return 0;
864 
865 	if (dlc->dai_args) {
866 		struct snd_soc_dai *dai;
867 
868 		for_each_component_dais(component, dai)
869 			if (snd_soc_is_matching_dai(dlc, dai))
870 				return 1;
871 		return 0;
872 	}
873 
874 	component_of_node = soc_component_to_node(component);
875 
876 	if (dlc->of_node && component_of_node != dlc->of_node)
877 		return 0;
878 	if (dlc->name && strcmp(component->name, dlc->name))
879 		return 0;
880 
881 	return 1;
882 }
883 
soc_find_component(const struct snd_soc_dai_link_component * dlc)884 static struct snd_soc_component *soc_find_component(
885 	const struct snd_soc_dai_link_component *dlc)
886 {
887 	struct snd_soc_component *component;
888 
889 	lockdep_assert_held(&client_mutex);
890 
891 	/*
892 	 * NOTE
893 	 *
894 	 * It returns *1st* found component, but some driver
895 	 * has few components by same of_node/name
896 	 * ex)
897 	 *	CPU component and generic DMAEngine component
898 	 */
899 	for_each_component(component)
900 		if (snd_soc_is_matching_component(dlc, component))
901 			return component;
902 
903 	return NULL;
904 }
905 
906 /**
907  * snd_soc_find_dai - Find a registered DAI
908  *
909  * @dlc: name of the DAI or the DAI driver and optional component info to match
910  *
911  * This function will search all registered components and their DAIs to
912  * find the DAI of the same name. The component's of_node and name
913  * should also match if being specified.
914  *
915  * Return: pointer of DAI, or NULL if not found.
916  */
snd_soc_find_dai(const struct snd_soc_dai_link_component * dlc)917 struct snd_soc_dai *snd_soc_find_dai(
918 	const struct snd_soc_dai_link_component *dlc)
919 {
920 	struct snd_soc_component *component;
921 	struct snd_soc_dai *dai;
922 
923 	lockdep_assert_held(&client_mutex);
924 
925 	/* Find CPU DAI from registered DAIs */
926 	for_each_component(component)
927 		if (snd_soc_is_matching_component(dlc, component))
928 			for_each_component_dais(component, dai)
929 				if (snd_soc_is_matching_dai(dlc, dai))
930 					return dai;
931 
932 	return NULL;
933 }
934 EXPORT_SYMBOL_GPL(snd_soc_find_dai);
935 
snd_soc_find_dai_with_mutex(const struct snd_soc_dai_link_component * dlc)936 struct snd_soc_dai *snd_soc_find_dai_with_mutex(
937 	const struct snd_soc_dai_link_component *dlc)
938 {
939 	struct snd_soc_dai *dai;
940 
941 	mutex_lock(&client_mutex);
942 	dai = snd_soc_find_dai(dlc);
943 	mutex_unlock(&client_mutex);
944 
945 	return dai;
946 }
947 EXPORT_SYMBOL_GPL(snd_soc_find_dai_with_mutex);
948 
soc_dai_link_sanity_check(struct snd_soc_card * card,struct snd_soc_dai_link * link)949 static int soc_dai_link_sanity_check(struct snd_soc_card *card,
950 				     struct snd_soc_dai_link *link)
951 {
952 	int i;
953 	struct snd_soc_dai_link_component *dlc;
954 
955 	/* Codec check */
956 	for_each_link_codecs(link, i, dlc) {
957 		/*
958 		 * Codec must be specified by 1 of name or OF node,
959 		 * not both or neither.
960 		 */
961 		if (snd_soc_dlc_component_is_invalid(dlc))
962 			goto component_invalid;
963 
964 		if (snd_soc_dlc_component_is_empty(dlc))
965 			goto component_empty;
966 
967 		/* Codec DAI name must be specified */
968 		if (snd_soc_dlc_dai_is_empty(dlc))
969 			goto dai_empty;
970 
971 		/*
972 		 * Defer card registration if codec component is not added to
973 		 * component list.
974 		 */
975 		if (!soc_find_component(dlc))
976 			goto component_not_found;
977 	}
978 
979 	/* Platform check */
980 	for_each_link_platforms(link, i, dlc) {
981 		/*
982 		 * Platform may be specified by either name or OF node, but it
983 		 * can be left unspecified, then no components will be inserted
984 		 * in the rtdcom list
985 		 */
986 		if (snd_soc_dlc_component_is_invalid(dlc))
987 			goto component_invalid;
988 
989 		if (snd_soc_dlc_component_is_empty(dlc))
990 			goto component_empty;
991 
992 		/*
993 		 * Defer card registration if platform component is not added to
994 		 * component list.
995 		 */
996 		if (!soc_find_component(dlc))
997 			goto component_not_found;
998 	}
999 
1000 	/* CPU check */
1001 	for_each_link_cpus(link, i, dlc) {
1002 		/*
1003 		 * CPU device may be specified by either name or OF node, but
1004 		 * can be left unspecified, and will be matched based on DAI
1005 		 * name alone..
1006 		 */
1007 		if (snd_soc_dlc_component_is_invalid(dlc))
1008 			goto component_invalid;
1009 
1010 
1011 		if (snd_soc_dlc_component_is_empty(dlc)) {
1012 			/*
1013 			 * At least one of CPU DAI name or CPU device name/node must be specified
1014 			 */
1015 			if (snd_soc_dlc_dai_is_empty(dlc))
1016 				goto component_dai_empty;
1017 		} else {
1018 			/*
1019 			 * Defer card registration if Component is not added
1020 			 */
1021 			if (!soc_find_component(dlc))
1022 				goto component_not_found;
1023 		}
1024 	}
1025 
1026 	return 0;
1027 
1028 component_invalid:
1029 	dev_err(card->dev, "ASoC: Both Component name/of_node are set for %s\n", link->name);
1030 	return -EINVAL;
1031 
1032 component_empty:
1033 	dev_err(card->dev, "ASoC: Neither Component name/of_node are set for %s\n", link->name);
1034 	return -EINVAL;
1035 
1036 component_not_found:
1037 	dev_dbg(card->dev, "ASoC: Component %s not found for link %s\n", dlc->name, link->name);
1038 	return -EPROBE_DEFER;
1039 
1040 dai_empty:
1041 	dev_err(card->dev, "ASoC: DAI name is not set for %s\n", link->name);
1042 	return -EINVAL;
1043 
1044 component_dai_empty:
1045 	dev_err(card->dev, "ASoC: Neither DAI/Component name/of_node are set for %s\n", link->name);
1046 	return -EINVAL;
1047 }
1048 
1049 #define MAX_DEFAULT_CH_MAP_SIZE 8
1050 static struct snd_soc_dai_link_ch_map default_ch_map_sync[MAX_DEFAULT_CH_MAP_SIZE] = {
1051 	{ .cpu = 0, .codec = 0 },
1052 	{ .cpu = 1, .codec = 1 },
1053 	{ .cpu = 2, .codec = 2 },
1054 	{ .cpu = 3, .codec = 3 },
1055 	{ .cpu = 4, .codec = 4 },
1056 	{ .cpu = 5, .codec = 5 },
1057 	{ .cpu = 6, .codec = 6 },
1058 	{ .cpu = 7, .codec = 7 },
1059 };
1060 static struct snd_soc_dai_link_ch_map default_ch_map_1cpu[MAX_DEFAULT_CH_MAP_SIZE] = {
1061 	{ .cpu = 0, .codec = 0 },
1062 	{ .cpu = 0, .codec = 1 },
1063 	{ .cpu = 0, .codec = 2 },
1064 	{ .cpu = 0, .codec = 3 },
1065 	{ .cpu = 0, .codec = 4 },
1066 	{ .cpu = 0, .codec = 5 },
1067 	{ .cpu = 0, .codec = 6 },
1068 	{ .cpu = 0, .codec = 7 },
1069 };
1070 static struct snd_soc_dai_link_ch_map default_ch_map_1codec[MAX_DEFAULT_CH_MAP_SIZE] = {
1071 	{ .cpu = 0, .codec = 0 },
1072 	{ .cpu = 1, .codec = 0 },
1073 	{ .cpu = 2, .codec = 0 },
1074 	{ .cpu = 3, .codec = 0 },
1075 	{ .cpu = 4, .codec = 0 },
1076 	{ .cpu = 5, .codec = 0 },
1077 	{ .cpu = 6, .codec = 0 },
1078 	{ .cpu = 7, .codec = 0 },
1079 };
snd_soc_compensate_channel_connection_map(struct snd_soc_card * card,struct snd_soc_dai_link * dai_link)1080 static int snd_soc_compensate_channel_connection_map(struct snd_soc_card *card,
1081 						     struct snd_soc_dai_link *dai_link)
1082 {
1083 	struct snd_soc_dai_link_ch_map *ch_maps;
1084 	int i;
1085 
1086 	/*
1087 	 * dai_link->ch_maps indicates how CPU/Codec are connected.
1088 	 * It will be a map seen from a larger number of DAI.
1089 	 * see
1090 	 *	soc.h :: [dai_link->ch_maps Image sample]
1091 	 */
1092 
1093 	/* it should have ch_maps if connection was N:M */
1094 	if (dai_link->num_cpus > 1 && dai_link->num_codecs > 1 &&
1095 	    dai_link->num_cpus != dai_link->num_codecs && !dai_link->ch_maps) {
1096 		dev_err(card->dev, "need to have ch_maps when N:M connection (%s)",
1097 			dai_link->name);
1098 		return -EINVAL;
1099 	}
1100 
1101 	/* do nothing if it has own maps */
1102 	if (dai_link->ch_maps)
1103 		goto sanity_check;
1104 
1105 	/* check default map size */
1106 	if (dai_link->num_cpus   > MAX_DEFAULT_CH_MAP_SIZE ||
1107 	    dai_link->num_codecs > MAX_DEFAULT_CH_MAP_SIZE) {
1108 		dev_err(card->dev, "soc-core.c needs update default_connection_maps");
1109 		return -EINVAL;
1110 	}
1111 
1112 	/* Compensate missing map for ... */
1113 	if (dai_link->num_cpus == dai_link->num_codecs)
1114 		dai_link->ch_maps = default_ch_map_sync;	/* for 1:1 or N:N */
1115 	else if (dai_link->num_cpus <  dai_link->num_codecs)
1116 		dai_link->ch_maps = default_ch_map_1cpu;	/* for 1:N */
1117 	else
1118 		dai_link->ch_maps = default_ch_map_1codec;	/* for N:1 */
1119 
1120 sanity_check:
1121 	dev_dbg(card->dev, "dai_link %s\n", dai_link->stream_name);
1122 	for_each_link_ch_maps(dai_link, i, ch_maps) {
1123 		if ((ch_maps->cpu   >= dai_link->num_cpus) ||
1124 		    (ch_maps->codec >= dai_link->num_codecs)) {
1125 			dev_err(card->dev,
1126 				"unexpected dai_link->ch_maps[%d] index (cpu(%d/%d) codec(%d/%d))",
1127 				i,
1128 				ch_maps->cpu,	dai_link->num_cpus,
1129 				ch_maps->codec,	dai_link->num_codecs);
1130 			return -EINVAL;
1131 		}
1132 
1133 		dev_dbg(card->dev, "  [%d] cpu%d <-> codec%d\n",
1134 			i, ch_maps->cpu, ch_maps->codec);
1135 	}
1136 
1137 	return 0;
1138 }
1139 
1140 /**
1141  * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card
1142  * @card: The ASoC card to which the pcm_runtime has
1143  * @rtd: The pcm_runtime to remove
1144  *
1145  * This function removes a pcm_runtime from the ASoC card.
1146  */
snd_soc_remove_pcm_runtime(struct snd_soc_card * card,struct snd_soc_pcm_runtime * rtd)1147 void snd_soc_remove_pcm_runtime(struct snd_soc_card *card,
1148 				struct snd_soc_pcm_runtime *rtd)
1149 {
1150 	if (!rtd)
1151 		return;
1152 
1153 	lockdep_assert_held(&client_mutex);
1154 
1155 	/*
1156 	 * Notify the machine driver for extra destruction
1157 	 */
1158 	snd_soc_card_remove_dai_link(card, rtd->dai_link);
1159 
1160 	soc_free_pcm_runtime(rtd);
1161 }
1162 EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime);
1163 
1164 /**
1165  * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
1166  * @card: The ASoC card to which the pcm_runtime is added
1167  * @dai_link: The DAI link to find pcm_runtime
1168  *
1169  * This function adds a pcm_runtime ASoC card by using dai_link.
1170  *
1171  * Note: Topology can use this API to add pcm_runtime when probing the
1172  * topology component. And machine drivers can still define static
1173  * DAI links in dai_link array.
1174  */
snd_soc_add_pcm_runtime(struct snd_soc_card * card,struct snd_soc_dai_link * dai_link)1175 static int snd_soc_add_pcm_runtime(struct snd_soc_card *card,
1176 				   struct snd_soc_dai_link *dai_link)
1177 {
1178 	struct snd_soc_pcm_runtime *rtd;
1179 	struct snd_soc_dai_link_component *codec, *platform, *cpu;
1180 	struct snd_soc_component *component;
1181 	int i, id, ret;
1182 
1183 	lockdep_assert_held(&client_mutex);
1184 
1185 	/*
1186 	 * Notify the machine driver for extra initialization
1187 	 */
1188 	ret = snd_soc_card_add_dai_link(card, dai_link);
1189 	if (ret < 0)
1190 		return ret;
1191 
1192 	if (dai_link->ignore)
1193 		return 0;
1194 
1195 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
1196 
1197 	ret = soc_dai_link_sanity_check(card, dai_link);
1198 	if (ret < 0)
1199 		return ret;
1200 
1201 	rtd = soc_new_pcm_runtime(card, dai_link);
1202 	if (!rtd)
1203 		return -ENOMEM;
1204 
1205 	for_each_link_cpus(dai_link, i, cpu) {
1206 		snd_soc_rtd_to_cpu(rtd, i) = snd_soc_find_dai(cpu);
1207 		if (!snd_soc_rtd_to_cpu(rtd, i)) {
1208 			dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
1209 				 cpu->dai_name);
1210 			goto _err_defer;
1211 		}
1212 		snd_soc_rtd_add_component(rtd, snd_soc_rtd_to_cpu(rtd, i)->component);
1213 	}
1214 
1215 	/* Find CODEC from registered CODECs */
1216 	for_each_link_codecs(dai_link, i, codec) {
1217 		snd_soc_rtd_to_codec(rtd, i) = snd_soc_find_dai(codec);
1218 		if (!snd_soc_rtd_to_codec(rtd, i)) {
1219 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
1220 				 codec->dai_name);
1221 			goto _err_defer;
1222 		}
1223 
1224 		snd_soc_rtd_add_component(rtd, snd_soc_rtd_to_codec(rtd, i)->component);
1225 	}
1226 
1227 	/* Find PLATFORM from registered PLATFORMs */
1228 	for_each_link_platforms(dai_link, i, platform) {
1229 		for_each_component(component) {
1230 			if (!snd_soc_is_matching_component(platform, component))
1231 				continue;
1232 
1233 			if (snd_soc_component_is_dummy(component) && component->num_dai)
1234 				continue;
1235 
1236 			snd_soc_rtd_add_component(rtd, component);
1237 		}
1238 	}
1239 
1240 	/*
1241 	 * Most drivers will register their PCMs using DAI link ordering but
1242 	 * topology based drivers can use the DAI link id field to set PCM
1243 	 * device number and then use rtd + a base offset of the BEs.
1244 	 *
1245 	 * FIXME
1246 	 *
1247 	 * This should be implemented by using "dai_link" feature instead of
1248 	 * "component" feature.
1249 	 */
1250 	id = rtd->id;
1251 	for_each_rtd_components(rtd, i, component) {
1252 		if (!component->driver->use_dai_pcm_id)
1253 			continue;
1254 
1255 		if (rtd->dai_link->no_pcm)
1256 			id += component->driver->be_pcm_base;
1257 		else
1258 			id = rtd->dai_link->id;
1259 	}
1260 	rtd->id = id;
1261 
1262 	return 0;
1263 
1264 _err_defer:
1265 	snd_soc_remove_pcm_runtime(card, rtd);
1266 	return -EPROBE_DEFER;
1267 }
1268 
snd_soc_add_pcm_runtimes(struct snd_soc_card * card,struct snd_soc_dai_link * dai_link,int num_dai_link)1269 int snd_soc_add_pcm_runtimes(struct snd_soc_card *card,
1270 			     struct snd_soc_dai_link *dai_link,
1271 			     int num_dai_link)
1272 {
1273 	for (int i = 0; i < num_dai_link; i++) {
1274 		int ret;
1275 
1276 		ret = snd_soc_compensate_channel_connection_map(card, dai_link + i);
1277 		if (ret < 0)
1278 			return ret;
1279 
1280 		ret = snd_soc_add_pcm_runtime(card, dai_link + i);
1281 		if (ret < 0)
1282 			return ret;
1283 	}
1284 
1285 	return 0;
1286 }
1287 EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtimes);
1288 
snd_soc_runtime_get_dai_fmt(struct snd_soc_pcm_runtime * rtd)1289 static void snd_soc_runtime_get_dai_fmt(struct snd_soc_pcm_runtime *rtd)
1290 {
1291 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1292 	struct snd_soc_dai *dai, *not_used;
1293 	u64 pos, possible_fmt;
1294 	unsigned int mask = 0, dai_fmt = 0;
1295 	int i, j, priority, pri, until;
1296 
1297 	/*
1298 	 * Get selectable format from each DAIs.
1299 	 *
1300 	 ****************************
1301 	 *            NOTE
1302 	 * Using .auto_selectable_formats is not mandatory,
1303 	 * we can select format manually from Sound Card.
1304 	 * When use it, driver should list well tested format only.
1305 	 ****************************
1306 	 *
1307 	 * ex)
1308 	 *	auto_selectable_formats (= SND_SOC_POSSIBLE_xxx)
1309 	 *		 (A)	 (B)	 (C)
1310 	 *	DAI0_: { 0x000F, 0x00F0, 0x0F00 };
1311 	 *	DAI1 : { 0xF000, 0x0F00 };
1312 	 *		 (X)	 (Y)
1313 	 *
1314 	 * "until" will be 3 in this case (MAX array size from DAI0 and DAI1)
1315 	 * Here is dev_dbg() message and comments
1316 	 *
1317 	 * priority = 1
1318 	 * DAI0: (pri, fmt) = (1, 000000000000000F) // 1st check (A) DAI1 is not selected
1319 	 * DAI1: (pri, fmt) = (0, 0000000000000000) //               Necessary Waste
1320 	 * DAI0: (pri, fmt) = (1, 000000000000000F) // 2nd check (A)
1321 	 * DAI1: (pri, fmt) = (1, 000000000000F000) //           (X)
1322 	 * priority = 2
1323 	 * DAI0: (pri, fmt) = (2, 00000000000000FF) // 3rd check (A) + (B)
1324 	 * DAI1: (pri, fmt) = (1, 000000000000F000) //           (X)
1325 	 * DAI0: (pri, fmt) = (2, 00000000000000FF) // 4th check (A) + (B)
1326 	 * DAI1: (pri, fmt) = (2, 000000000000FF00) //           (X) + (Y)
1327 	 * priority = 3
1328 	 * DAI0: (pri, fmt) = (3, 0000000000000FFF) // 5th check (A) + (B) + (C)
1329 	 * DAI1: (pri, fmt) = (2, 000000000000FF00) //           (X) + (Y)
1330 	 * found auto selected format: 0000000000000F00
1331 	 */
1332 	until = snd_soc_dai_get_fmt_max_priority(rtd);
1333 	for (priority = 1; priority <= until; priority++) {
1334 		for_each_rtd_dais(rtd, j, not_used) {
1335 
1336 			possible_fmt = ULLONG_MAX;
1337 			for_each_rtd_dais(rtd, i, dai) {
1338 				u64 fmt = 0;
1339 
1340 				pri = (j >= i) ? priority : priority - 1;
1341 				fmt = snd_soc_dai_get_fmt(dai, pri);
1342 				possible_fmt &= fmt;
1343 			}
1344 			if (possible_fmt)
1345 				goto found;
1346 		}
1347 	}
1348 	/* Not Found */
1349 	return;
1350 found:
1351 	/*
1352 	 * convert POSSIBLE_DAIFMT to DAIFMT
1353 	 *
1354 	 * Some basic/default settings on each is defined as 0.
1355 	 * see
1356 	 *	SND_SOC_DAIFMT_NB_NF
1357 	 *	SND_SOC_DAIFMT_GATED
1358 	 *
1359 	 * SND_SOC_DAIFMT_xxx_MASK can't notice it if Sound Card specify
1360 	 * these value, and will be overwrite to auto selected value.
1361 	 *
1362 	 * To avoid such issue, loop from 63 to 0 here.
1363 	 * Small number of SND_SOC_POSSIBLE_xxx will be Hi priority.
1364 	 * Basic/Default settings of each part and above are defined
1365 	 * as Hi priority (= small number) of SND_SOC_POSSIBLE_xxx.
1366 	 */
1367 	for (i = 63; i >= 0; i--) {
1368 		pos = 1ULL << i;
1369 		switch (possible_fmt & pos) {
1370 		/*
1371 		 * for format
1372 		 */
1373 		case SND_SOC_POSSIBLE_DAIFMT_I2S:
1374 		case SND_SOC_POSSIBLE_DAIFMT_RIGHT_J:
1375 		case SND_SOC_POSSIBLE_DAIFMT_LEFT_J:
1376 		case SND_SOC_POSSIBLE_DAIFMT_DSP_A:
1377 		case SND_SOC_POSSIBLE_DAIFMT_DSP_B:
1378 		case SND_SOC_POSSIBLE_DAIFMT_AC97:
1379 		case SND_SOC_POSSIBLE_DAIFMT_PDM:
1380 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_FORMAT_MASK) | i;
1381 			break;
1382 		/*
1383 		 * for clock
1384 		 */
1385 		case SND_SOC_POSSIBLE_DAIFMT_CONT:
1386 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_MASK) | SND_SOC_DAIFMT_CONT;
1387 			break;
1388 		case SND_SOC_POSSIBLE_DAIFMT_GATED:
1389 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_MASK) | SND_SOC_DAIFMT_GATED;
1390 			break;
1391 		/*
1392 		 * for clock invert
1393 		 */
1394 		case SND_SOC_POSSIBLE_DAIFMT_NB_NF:
1395 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_NB_NF;
1396 			break;
1397 		case SND_SOC_POSSIBLE_DAIFMT_NB_IF:
1398 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_NB_IF;
1399 			break;
1400 		case SND_SOC_POSSIBLE_DAIFMT_IB_NF:
1401 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_IB_NF;
1402 			break;
1403 		case SND_SOC_POSSIBLE_DAIFMT_IB_IF:
1404 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_IB_IF;
1405 			break;
1406 		/*
1407 		 * for clock provider / consumer
1408 		 */
1409 		case SND_SOC_POSSIBLE_DAIFMT_CBP_CFP:
1410 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBP_CFP;
1411 			break;
1412 		case SND_SOC_POSSIBLE_DAIFMT_CBC_CFP:
1413 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBC_CFP;
1414 			break;
1415 		case SND_SOC_POSSIBLE_DAIFMT_CBP_CFC:
1416 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBP_CFC;
1417 			break;
1418 		case SND_SOC_POSSIBLE_DAIFMT_CBC_CFC:
1419 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBC_CFC;
1420 			break;
1421 		}
1422 	}
1423 
1424 	/*
1425 	 * Some driver might have very complex limitation.
1426 	 * In such case, user want to auto-select non-limitation part,
1427 	 * and want to manually specify complex part.
1428 	 *
1429 	 * Or for example, if both CPU and Codec can be clock provider,
1430 	 * but because of its quality, user want to specify it manually.
1431 	 *
1432 	 * Use manually specified settings if sound card did.
1433 	 */
1434 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK))
1435 		mask |= SND_SOC_DAIFMT_FORMAT_MASK;
1436 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_CLOCK_MASK))
1437 		mask |= SND_SOC_DAIFMT_CLOCK_MASK;
1438 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_INV_MASK))
1439 		mask |= SND_SOC_DAIFMT_INV_MASK;
1440 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK))
1441 		mask |= SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
1442 
1443 	dai_link->dai_fmt |= (dai_fmt & mask);
1444 }
1445 
1446 /**
1447  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1448  * @rtd: The runtime for which the DAI link format should be changed
1449  * @dai_fmt: The new DAI link format
1450  *
1451  * This function updates the DAI link format for all DAIs connected to the DAI
1452  * link for the specified runtime.
1453  *
1454  * Note: For setups with a static format set the dai_fmt field in the
1455  * corresponding snd_dai_link struct instead of using this function.
1456  *
1457  * Returns 0 on success, otherwise a negative error code.
1458  */
snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime * rtd,unsigned int dai_fmt)1459 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1460 				unsigned int dai_fmt)
1461 {
1462 	struct snd_soc_dai *cpu_dai;
1463 	struct snd_soc_dai *codec_dai;
1464 	unsigned int ext_fmt;
1465 	unsigned int i;
1466 	int ret;
1467 
1468 	if (!dai_fmt)
1469 		return 0;
1470 
1471 	/*
1472 	 * dai_fmt has 4 types
1473 	 *	1. SND_SOC_DAIFMT_FORMAT_MASK
1474 	 *	2. SND_SOC_DAIFMT_CLOCK
1475 	 *	3. SND_SOC_DAIFMT_INV
1476 	 *	4. SND_SOC_DAIFMT_CLOCK_PROVIDER
1477 	 *
1478 	 * 4. CLOCK_PROVIDER is set from Codec perspective in dai_fmt. So it will be flipped
1479 	 * when this function calls set_fmt() for CPU (CBx_CFx -> Bx_Cx). see below.
1480 	 * This mean, we can't set CPU/Codec both are clock consumer for example.
1481 	 * New idea handles 4. in each dai->ext_fmt. It can keep compatibility.
1482 	 *
1483 	 * Legacy
1484 	 *	dai_fmt  includes 1, 2, 3, 4
1485 	 *
1486 	 * New idea
1487 	 *	dai_fmt  includes 1, 2, 3
1488 	 *	ext_fmt  includes 4
1489 	 */
1490 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
1491 		ext_fmt = rtd->dai_link->codecs[i].ext_fmt;
1492 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt | ext_fmt);
1493 		if (ret != 0 && ret != -ENOTSUPP)
1494 			return ret;
1495 	}
1496 
1497 	/* Flip the polarity for the "CPU" end of link */
1498 	/* Will effect only for 4. SND_SOC_DAIFMT_CLOCK_PROVIDER */
1499 	dai_fmt = snd_soc_daifmt_clock_provider_flipped(dai_fmt);
1500 
1501 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1502 		ext_fmt = rtd->dai_link->cpus[i].ext_fmt;
1503 		ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt | ext_fmt);
1504 		if (ret != 0 && ret != -ENOTSUPP)
1505 			return ret;
1506 	}
1507 
1508 	return 0;
1509 }
1510 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1511 
soc_init_pcm_runtime(struct snd_soc_card * card,struct snd_soc_pcm_runtime * rtd)1512 static int soc_init_pcm_runtime(struct snd_soc_card *card,
1513 				struct snd_soc_pcm_runtime *rtd)
1514 {
1515 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1516 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
1517 	int ret;
1518 
1519 	/* do machine specific initialization */
1520 	ret = snd_soc_link_init(rtd);
1521 	if (ret < 0)
1522 		return ret;
1523 
1524 	snd_soc_runtime_get_dai_fmt(rtd);
1525 	ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1526 	if (ret)
1527 		goto err;
1528 
1529 	/* add DPCM sysfs entries */
1530 	soc_dpcm_debugfs_add(rtd);
1531 
1532 	/* create compress_device if possible */
1533 	ret = snd_soc_dai_compress_new(cpu_dai, rtd);
1534 	if (ret != -ENOTSUPP)
1535 		goto err;
1536 
1537 	/* create the pcm */
1538 	ret = soc_new_pcm(rtd);
1539 	if (ret < 0) {
1540 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1541 			dai_link->stream_name, ret);
1542 		goto err;
1543 	}
1544 
1545 	ret = snd_soc_pcm_dai_new(rtd);
1546 	if (ret < 0)
1547 		goto err;
1548 
1549 	rtd->initialized = true;
1550 
1551 	return 0;
1552 err:
1553 	snd_soc_link_exit(rtd);
1554 	return ret;
1555 }
1556 
soc_set_name_prefix(struct snd_soc_card * card,struct snd_soc_component * component)1557 static void soc_set_name_prefix(struct snd_soc_card *card,
1558 				struct snd_soc_component *component)
1559 {
1560 	struct device_node *of_node = soc_component_to_node(component);
1561 	const char *str;
1562 	int ret, i;
1563 
1564 	for (i = 0; i < card->num_configs; i++) {
1565 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1566 
1567 		if (snd_soc_is_matching_component(&map->dlc, component) &&
1568 		    map->name_prefix) {
1569 			component->name_prefix = map->name_prefix;
1570 			return;
1571 		}
1572 	}
1573 
1574 	/*
1575 	 * If there is no configuration table or no match in the table,
1576 	 * check if a prefix is provided in the node
1577 	 */
1578 	ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1579 	if (ret < 0)
1580 		return;
1581 
1582 	component->name_prefix = str;
1583 }
1584 
soc_remove_component(struct snd_soc_component * component,int probed)1585 static void soc_remove_component(struct snd_soc_component *component,
1586 				 int probed)
1587 {
1588 
1589 	if (!component->card)
1590 		return;
1591 
1592 	if (probed)
1593 		snd_soc_component_remove(component);
1594 
1595 	list_del_init(&component->card_list);
1596 	snd_soc_dapm_free(snd_soc_component_to_dapm(component));
1597 	soc_cleanup_component_debugfs(component);
1598 	component->card = NULL;
1599 	snd_soc_component_module_put_when_remove(component);
1600 }
1601 
soc_probe_component(struct snd_soc_card * card,struct snd_soc_component * component)1602 static int soc_probe_component(struct snd_soc_card *card,
1603 			       struct snd_soc_component *component)
1604 {
1605 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
1606 	struct snd_soc_dai *dai;
1607 	int probed = 0;
1608 	int ret;
1609 
1610 	if (snd_soc_component_is_dummy(component))
1611 		return 0;
1612 
1613 	if (component->card) {
1614 		if (component->card != card) {
1615 			dev_err(component->dev,
1616 				"Trying to bind component \"%s\" to card \"%s\" but is already bound to card \"%s\"\n",
1617 				component->name, card->name, component->card->name);
1618 			return -ENODEV;
1619 		}
1620 		return 0;
1621 	}
1622 
1623 	ret = snd_soc_component_module_get_when_probe(component);
1624 	if (ret < 0)
1625 		return ret;
1626 
1627 	component->card = card;
1628 	soc_set_name_prefix(card, component);
1629 
1630 	soc_init_component_debugfs(component);
1631 
1632 	snd_soc_dapm_init(dapm, card, component);
1633 
1634 	ret = snd_soc_dapm_new_controls(dapm,
1635 					component->driver->dapm_widgets,
1636 					component->driver->num_dapm_widgets);
1637 
1638 	if (ret != 0) {
1639 		dev_err(component->dev,
1640 			"Failed to create new controls %d\n", ret);
1641 		goto err_probe;
1642 	}
1643 
1644 	for_each_component_dais(component, dai) {
1645 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1646 		if (ret != 0) {
1647 			dev_err(component->dev,
1648 				"Failed to create DAI widgets %d\n", ret);
1649 			goto err_probe;
1650 		}
1651 	}
1652 
1653 	ret = snd_soc_component_probe(component);
1654 	if (ret < 0)
1655 		goto err_probe;
1656 
1657 	WARN(!snd_soc_dapm_get_idle_bias(dapm) &&
1658 	     snd_soc_dapm_get_bias_level(dapm) != SND_SOC_BIAS_OFF,
1659 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1660 	     component->name);
1661 	probed = 1;
1662 
1663 	/*
1664 	 * machine specific init
1665 	 * see
1666 	 *	snd_soc_component_set_aux()
1667 	 */
1668 	ret = snd_soc_component_init(component);
1669 	if (ret < 0)
1670 		goto err_probe;
1671 
1672 	ret = snd_soc_add_component_controls(component,
1673 					     component->driver->controls,
1674 					     component->driver->num_controls);
1675 	if (ret < 0)
1676 		goto err_probe;
1677 
1678 	ret = snd_soc_dapm_add_routes(dapm,
1679 				      component->driver->dapm_routes,
1680 				      component->driver->num_dapm_routes);
1681 	if (ret < 0)
1682 		goto err_probe;
1683 
1684 	/* see for_each_card_components */
1685 	list_add(&component->card_list, &card->component_dev_list);
1686 
1687 err_probe:
1688 	if (ret < 0)
1689 		soc_remove_component(component, probed);
1690 
1691 	return ret;
1692 }
1693 
soc_remove_link_dais(struct snd_soc_card * card)1694 static void soc_remove_link_dais(struct snd_soc_card *card)
1695 {
1696 	struct snd_soc_pcm_runtime *rtd;
1697 	int order;
1698 
1699 	for_each_comp_order(order) {
1700 		for_each_card_rtds(card, rtd) {
1701 			/* remove all rtd connected DAIs in good order */
1702 			snd_soc_pcm_dai_remove(rtd, order);
1703 		}
1704 	}
1705 }
1706 
soc_probe_link_dais(struct snd_soc_card * card)1707 static int soc_probe_link_dais(struct snd_soc_card *card)
1708 {
1709 	struct snd_soc_pcm_runtime *rtd;
1710 	int order, ret;
1711 
1712 	for_each_comp_order(order) {
1713 		for_each_card_rtds(card, rtd) {
1714 			/* probe all rtd connected DAIs in good order */
1715 			ret = snd_soc_pcm_dai_probe(rtd, order);
1716 			if (ret)
1717 				return ret;
1718 		}
1719 	}
1720 
1721 	return 0;
1722 }
1723 
soc_remove_link_components(struct snd_soc_card * card)1724 static void soc_remove_link_components(struct snd_soc_card *card)
1725 {
1726 	struct snd_soc_component *component;
1727 	struct snd_soc_pcm_runtime *rtd;
1728 	int i, order;
1729 
1730 	for_each_comp_order(order) {
1731 		for_each_card_rtds(card, rtd) {
1732 			for_each_rtd_components(rtd, i, component) {
1733 				if (component->driver->remove_order != order)
1734 					continue;
1735 
1736 				soc_remove_component(component, 1);
1737 			}
1738 		}
1739 	}
1740 }
1741 
soc_probe_link_components(struct snd_soc_card * card)1742 static int soc_probe_link_components(struct snd_soc_card *card)
1743 {
1744 	struct snd_soc_component *component;
1745 	struct snd_soc_pcm_runtime *rtd;
1746 	int i, ret, order;
1747 
1748 	for_each_comp_order(order) {
1749 		for_each_card_rtds(card, rtd) {
1750 			for_each_rtd_components(rtd, i, component) {
1751 				if (component->driver->probe_order != order)
1752 					continue;
1753 
1754 				ret = soc_probe_component(card, component);
1755 				if (ret < 0)
1756 					return ret;
1757 			}
1758 		}
1759 	}
1760 
1761 	return 0;
1762 }
1763 
soc_unbind_aux_dev(struct snd_soc_card * card)1764 static void soc_unbind_aux_dev(struct snd_soc_card *card)
1765 {
1766 	struct snd_soc_component *component, *_component;
1767 
1768 	for_each_card_auxs_safe(card, component, _component) {
1769 		/* for snd_soc_component_init() */
1770 		snd_soc_component_set_aux(component, NULL);
1771 		list_del(&component->card_aux_list);
1772 	}
1773 }
1774 
soc_bind_aux_dev(struct snd_soc_card * card)1775 static int soc_bind_aux_dev(struct snd_soc_card *card)
1776 {
1777 	struct snd_soc_component *component;
1778 	struct snd_soc_aux_dev *aux;
1779 	int i;
1780 
1781 	for_each_card_pre_auxs(card, i, aux) {
1782 		/* codecs, usually analog devices */
1783 		component = soc_find_component(&aux->dlc);
1784 		if (!component)
1785 			return -EPROBE_DEFER;
1786 
1787 		/* for snd_soc_component_init() */
1788 		snd_soc_component_set_aux(component, aux);
1789 		/* see for_each_card_auxs */
1790 		list_add(&component->card_aux_list, &card->aux_comp_list);
1791 	}
1792 	return 0;
1793 }
1794 
soc_probe_aux_devices(struct snd_soc_card * card)1795 static int soc_probe_aux_devices(struct snd_soc_card *card)
1796 {
1797 	struct snd_soc_component *component;
1798 	int order;
1799 	int ret;
1800 
1801 	for_each_comp_order(order) {
1802 		for_each_card_auxs(card, component) {
1803 			if (component->driver->probe_order != order)
1804 				continue;
1805 
1806 			ret = soc_probe_component(card,	component);
1807 			if (ret < 0)
1808 				return ret;
1809 		}
1810 	}
1811 
1812 	return 0;
1813 }
1814 
soc_remove_aux_devices(struct snd_soc_card * card)1815 static void soc_remove_aux_devices(struct snd_soc_card *card)
1816 {
1817 	struct snd_soc_component *comp, *_comp;
1818 	int order;
1819 
1820 	for_each_comp_order(order) {
1821 		for_each_card_auxs_safe(card, comp, _comp) {
1822 			if (comp->driver->remove_order == order)
1823 				soc_remove_component(comp, 1);
1824 		}
1825 	}
1826 }
1827 
1828 #ifdef CONFIG_DMI
1829 /*
1830  * If a DMI filed contain strings in this blacklist (e.g.
1831  * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
1832  * as invalid and dropped when setting the card long name from DMI info.
1833  */
1834 static const char * const dmi_blacklist[] = {
1835 	"To be filled by OEM",
1836 	"TBD by OEM",
1837 	"Default String",
1838 	"Board Manufacturer",
1839 	"Board Vendor Name",
1840 	"Board Product Name",
1841 	NULL,	/* terminator */
1842 };
1843 
1844 /*
1845  * Trim special characters, and replace '-' with '_' since '-' is used to
1846  * separate different DMI fields in the card long name. Only number and
1847  * alphabet characters and a few separator characters are kept.
1848  */
cleanup_dmi_name(char * name)1849 static void cleanup_dmi_name(char *name)
1850 {
1851 	int i, j = 0;
1852 
1853 	for (i = 0; name[i]; i++) {
1854 		if (isalnum(name[i]) || (name[i] == '.')
1855 		    || (name[i] == '_'))
1856 			name[j++] = name[i];
1857 		else if (name[i] == '-')
1858 			name[j++] = '_';
1859 	}
1860 
1861 	name[j] = '\0';
1862 }
1863 
1864 /*
1865  * Check if a DMI field is valid, i.e. not containing any string
1866  * in the black list and not the empty string.
1867  */
is_dmi_valid(const char * field)1868 static int is_dmi_valid(const char *field)
1869 {
1870 	int i = 0;
1871 
1872 	if (!field[0])
1873 		return 0;
1874 
1875 	while (dmi_blacklist[i]) {
1876 		if (strstr(field, dmi_blacklist[i]))
1877 			return 0;
1878 		i++;
1879 	}
1880 
1881 	return 1;
1882 }
1883 
1884 /*
1885  * Append a string to card->dmi_longname with character cleanups.
1886  */
append_dmi_string(struct snd_soc_card * card,const char * str)1887 static void append_dmi_string(struct snd_soc_card *card, const char *str)
1888 {
1889 	char *dst = card->dmi_longname;
1890 	size_t dst_len = sizeof(card->dmi_longname);
1891 	size_t len;
1892 
1893 	len = strlen(dst);
1894 	snprintf(dst + len, dst_len - len, "-%s", str);
1895 
1896 	len++;	/* skip the separator "-" */
1897 	if (len < dst_len)
1898 		cleanup_dmi_name(dst + len);
1899 }
1900 
1901 /**
1902  * snd_soc_set_dmi_name() - Register DMI names to card
1903  * @card: The card to register DMI names
1904  *
1905  * An Intel machine driver may be used by many different devices but are
1906  * difficult for userspace to differentiate, since machine drivers usually
1907  * use their own name as the card short name and leave the card long name
1908  * blank. To differentiate such devices and fix bugs due to lack of
1909  * device-specific configurations, this function allows DMI info to be used
1910  * as the sound card long name, in the format of
1911  * "vendor-product-version-board"
1912  * (Character '-' is used to separate different DMI fields here).
1913  * This will help the user space to load the device-specific Use Case Manager
1914  * (UCM) configurations for the card.
1915  *
1916  * Possible card long names may be:
1917  * DellInc.-XPS139343-01-0310JH
1918  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1919  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1920  *
1921  * This function also supports flavoring the card longname to provide
1922  * the extra differentiation, like "vendor-product-version-board-flavor".
1923  *
1924  * We only keep number and alphabet characters and a few separator characters
1925  * in the card long name since UCM in the user space uses the card long names
1926  * as card configuration directory names and AudoConf cannot support special
1927  * characters like SPACE.
1928  *
1929  * Returns 0 on success, otherwise a negative error code.
1930  */
snd_soc_set_dmi_name(struct snd_soc_card * card)1931 static int snd_soc_set_dmi_name(struct snd_soc_card *card)
1932 {
1933 	const char *vendor, *product, *board;
1934 
1935 	if (card->long_name)
1936 		return 0; /* long name already set by driver or from DMI */
1937 
1938 	if (!dmi_available)
1939 		return 0;
1940 
1941 	/* make up dmi long name as: vendor-product-version-board */
1942 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1943 	if (!vendor || !is_dmi_valid(vendor)) {
1944 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1945 		return 0;
1946 	}
1947 
1948 	snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
1949 	cleanup_dmi_name(card->dmi_longname);
1950 
1951 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
1952 	if (product && is_dmi_valid(product)) {
1953 		const char *product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1954 
1955 		append_dmi_string(card, product);
1956 
1957 		/*
1958 		 * some vendors like Lenovo may only put a self-explanatory
1959 		 * name in the product version field
1960 		 */
1961 		if (product_version && is_dmi_valid(product_version))
1962 			append_dmi_string(card, product_version);
1963 	}
1964 
1965 	board = dmi_get_system_info(DMI_BOARD_NAME);
1966 	if (board && is_dmi_valid(board)) {
1967 		if (!product || strcasecmp(board, product))
1968 			append_dmi_string(card, board);
1969 	} else if (!product) {
1970 		/* fall back to using legacy name */
1971 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1972 		return 0;
1973 	}
1974 
1975 	/* set the card long name */
1976 	card->long_name = card->dmi_longname;
1977 
1978 	return 0;
1979 }
1980 #else
snd_soc_set_dmi_name(struct snd_soc_card * card)1981 static inline int snd_soc_set_dmi_name(struct snd_soc_card *card)
1982 {
1983 	return 0;
1984 }
1985 #endif /* CONFIG_DMI */
1986 
soc_check_tplg_fes(struct snd_soc_card * card)1987 static void soc_check_tplg_fes(struct snd_soc_card *card)
1988 {
1989 	struct snd_soc_component *component;
1990 	const struct snd_soc_component_driver *comp_drv;
1991 	struct snd_soc_dai_link *dai_link;
1992 	int i;
1993 
1994 	for_each_component(component) {
1995 
1996 		/* does this component override BEs ? */
1997 		if (!component->driver->ignore_machine)
1998 			continue;
1999 
2000 		/* for this machine ? */
2001 		if (!strcmp(component->driver->ignore_machine,
2002 			    card->dev->driver->name))
2003 			goto match;
2004 		if (strcmp(component->driver->ignore_machine,
2005 			   dev_name(card->dev)))
2006 			continue;
2007 match:
2008 		/* machine matches, so override the rtd data */
2009 		for_each_card_prelinks(card, i, dai_link) {
2010 
2011 			/* ignore this FE */
2012 			if (dai_link->dynamic) {
2013 				dai_link->ignore = true;
2014 				continue;
2015 			}
2016 
2017 			dev_dbg(card->dev, "info: override BE DAI link %s\n",
2018 				card->dai_link[i].name);
2019 
2020 			/* override platform component */
2021 			if (!dai_link->platforms) {
2022 				dev_err(card->dev, "init platform error");
2023 				continue;
2024 			}
2025 
2026 			if (component->dev->of_node)
2027 				dai_link->platforms->of_node = component->dev->of_node;
2028 			else
2029 				dai_link->platforms->name = component->name;
2030 
2031 			/* convert non BE into BE */
2032 			dai_link->no_pcm = 1;
2033 
2034 			/*
2035 			 * override any BE fixups
2036 			 * see
2037 			 *	snd_soc_link_be_hw_params_fixup()
2038 			 */
2039 			dai_link->be_hw_params_fixup =
2040 				component->driver->be_hw_params_fixup;
2041 
2042 			/*
2043 			 * most BE links don't set stream name, so set it to
2044 			 * dai link name if it's NULL to help bind widgets.
2045 			 */
2046 			if (!dai_link->stream_name)
2047 				dai_link->stream_name = dai_link->name;
2048 		}
2049 
2050 		/* Inform userspace we are using alternate topology */
2051 		if (component->driver->topology_name_prefix) {
2052 
2053 			/* topology shortname created? */
2054 			if (!card->topology_shortname_created) {
2055 				comp_drv = component->driver;
2056 
2057 				snprintf(card->topology_shortname, 32, "%s-%s",
2058 					 comp_drv->topology_name_prefix,
2059 					 card->name);
2060 				card->topology_shortname_created = true;
2061 			}
2062 
2063 			/* use topology shortname */
2064 			card->name = card->topology_shortname;
2065 		}
2066 	}
2067 }
2068 
2069 #define soc_setup_card_name(card, name, name1, name2) \
2070 	__soc_setup_card_name(card, name, sizeof(name), name1, name2)
__soc_setup_card_name(struct snd_soc_card * card,char * name,int len,const char * name1,const char * name2)2071 static void __soc_setup_card_name(struct snd_soc_card *card,
2072 				  char *name, int len,
2073 				  const char *name1, const char *name2)
2074 {
2075 	const char *src = name1 ? name1 : name2;
2076 	int i;
2077 
2078 	snprintf(name, len, "%s", src);
2079 
2080 	if (name != card->snd_card->driver)
2081 		return;
2082 
2083 	/*
2084 	 * Name normalization (driver field)
2085 	 *
2086 	 * The driver name is somewhat special, as it's used as a key for
2087 	 * searches in the user-space.
2088 	 *
2089 	 * ex)
2090 	 *	"abcd??efg" -> "abcd__efg"
2091 	 */
2092 	for (i = 0; i < len; i++) {
2093 		switch (name[i]) {
2094 		case '_':
2095 		case '-':
2096 		case '\0':
2097 			break;
2098 		default:
2099 			if (!isalnum(name[i]))
2100 				name[i] = '_';
2101 			break;
2102 		}
2103 	}
2104 
2105 	/*
2106 	 * The driver field should contain a valid string from the user view.
2107 	 * The wrapping usually does not work so well here. Set a smaller string
2108 	 * in the specific ASoC driver.
2109 	 */
2110 	if (strlen(src) > len - 1)
2111 		dev_err(card->dev, "ASoC: driver name too long '%s' -> '%s'\n", src, name);
2112 }
2113 
soc_cleanup_card_resources(struct snd_soc_card * card)2114 static void soc_cleanup_card_resources(struct snd_soc_card *card)
2115 {
2116 	struct snd_soc_pcm_runtime *rtd, *n;
2117 
2118 	if (card->snd_card)
2119 		snd_card_disconnect_sync(card->snd_card);
2120 
2121 	snd_soc_dapm_shutdown(card);
2122 
2123 	/* release machine specific resources */
2124 	for_each_card_rtds(card, rtd)
2125 		if (rtd->initialized)
2126 			snd_soc_link_exit(rtd);
2127 	/* flush delayed work before removing DAIs and DAPM widgets */
2128 	snd_soc_flush_all_delayed_work(card);
2129 
2130 	/* remove and free each DAI */
2131 	soc_remove_link_dais(card);
2132 	soc_remove_link_components(card);
2133 
2134 	for_each_card_rtds_safe(card, rtd, n)
2135 		snd_soc_remove_pcm_runtime(card, rtd);
2136 
2137 	/* remove auxiliary devices */
2138 	soc_remove_aux_devices(card);
2139 	soc_unbind_aux_dev(card);
2140 
2141 	snd_soc_dapm_free(snd_soc_card_to_dapm(card));
2142 	soc_cleanup_card_debugfs(card);
2143 
2144 	/* remove the card */
2145 	snd_soc_card_remove(card);
2146 
2147 	if (card->snd_card) {
2148 		snd_card_free(card->snd_card);
2149 		card->snd_card = NULL;
2150 	}
2151 }
2152 
snd_soc_unbind_card(struct snd_soc_card * card)2153 static void snd_soc_unbind_card(struct snd_soc_card *card)
2154 {
2155 	if (snd_soc_card_is_instantiated(card)) {
2156 		card->instantiated = false;
2157 		snd_soc_flush_all_delayed_work(card);
2158 
2159 		soc_cleanup_card_resources(card);
2160 	}
2161 }
2162 
snd_soc_bind_card(struct snd_soc_card * card)2163 static int snd_soc_bind_card(struct snd_soc_card *card)
2164 {
2165 	struct snd_soc_pcm_runtime *rtd;
2166 	struct snd_soc_component *component;
2167 	struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
2168 	int ret;
2169 
2170 	snd_soc_card_mutex_lock_root(card);
2171 	snd_soc_fill_dummy_dai(card);
2172 
2173 	snd_soc_dapm_init(dapm, card, NULL);
2174 
2175 	/* check whether any platform is ignore machine FE and using topology */
2176 	soc_check_tplg_fes(card);
2177 
2178 	/* bind aux_devs too */
2179 	ret = soc_bind_aux_dev(card);
2180 	if (ret < 0)
2181 		goto probe_end;
2182 
2183 	/* add predefined DAI links to the list */
2184 	card->num_rtd = 0;
2185 	ret = snd_soc_add_pcm_runtimes(card, card->dai_link, card->num_links);
2186 	if (ret < 0)
2187 		goto probe_end;
2188 
2189 	/* card bind complete so register a sound card */
2190 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2191 			card->owner, 0, &card->snd_card);
2192 	if (ret < 0) {
2193 		dev_err(card->dev,
2194 			"ASoC: can't create sound card for card %s: %d\n",
2195 			card->name, ret);
2196 		goto probe_end;
2197 	}
2198 
2199 	soc_init_card_debugfs(card);
2200 
2201 	soc_resume_init(card);
2202 
2203 	ret = snd_soc_dapm_new_controls(dapm, card->dapm_widgets,
2204 					card->num_dapm_widgets);
2205 	if (ret < 0)
2206 		goto probe_end;
2207 
2208 	ret = snd_soc_dapm_new_controls(dapm, card->of_dapm_widgets,
2209 					card->num_of_dapm_widgets);
2210 	if (ret < 0)
2211 		goto probe_end;
2212 
2213 	/* initialise the sound card only once */
2214 	ret = snd_soc_card_probe(card);
2215 	if (ret < 0)
2216 		goto probe_end;
2217 
2218 	/* probe all components used by DAI links on this card */
2219 	ret = soc_probe_link_components(card);
2220 	if (ret < 0) {
2221 		if (ret != -EPROBE_DEFER) {
2222 			dev_err(card->dev,
2223 				"ASoC: failed to instantiate card %d\n", ret);
2224 		}
2225 		goto probe_end;
2226 	}
2227 
2228 	/* probe auxiliary components */
2229 	ret = soc_probe_aux_devices(card);
2230 	if (ret < 0) {
2231 		dev_err(card->dev,
2232 			"ASoC: failed to probe aux component %d\n", ret);
2233 		goto probe_end;
2234 	}
2235 
2236 	/* probe all DAI links on this card */
2237 	ret = soc_probe_link_dais(card);
2238 	if (ret < 0) {
2239 		dev_err(card->dev,
2240 			"ASoC: failed to instantiate card %d\n", ret);
2241 		goto probe_end;
2242 	}
2243 
2244 	for_each_card_rtds(card, rtd) {
2245 		ret = soc_init_pcm_runtime(card, rtd);
2246 		if (ret < 0)
2247 			goto probe_end;
2248 	}
2249 
2250 	snd_soc_dapm_link_dai_widgets(card);
2251 	snd_soc_dapm_connect_dai_link_widgets(card);
2252 
2253 	ret = snd_soc_add_card_controls(card, card->controls,
2254 					card->num_controls);
2255 	if (ret < 0)
2256 		goto probe_end;
2257 
2258 	ret = snd_soc_dapm_add_routes(dapm, card->dapm_routes,
2259 				      card->num_dapm_routes);
2260 	if (ret < 0)
2261 		goto probe_end;
2262 
2263 	ret = snd_soc_dapm_add_routes(dapm, card->of_dapm_routes,
2264 				      card->num_of_dapm_routes);
2265 	if (ret < 0)
2266 		goto probe_end;
2267 
2268 	/* try to set some sane longname if DMI is available */
2269 	snd_soc_set_dmi_name(card);
2270 
2271 	soc_setup_card_name(card, card->snd_card->shortname,
2272 			    card->name, NULL);
2273 	soc_setup_card_name(card, card->snd_card->longname,
2274 			    card->long_name, card->name);
2275 	soc_setup_card_name(card, card->snd_card->driver,
2276 			    card->driver_name, card->name);
2277 
2278 	if (card->components) {
2279 		/* the current implementation of snd_component_add() accepts */
2280 		/* multiple components in the string separated by space, */
2281 		/* but the string collision (identical string) check might */
2282 		/* not work correctly */
2283 		ret = snd_component_add(card->snd_card, card->components);
2284 		if (ret < 0) {
2285 			dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
2286 				card->name, ret);
2287 			goto probe_end;
2288 		}
2289 	}
2290 
2291 	ret = snd_soc_card_late_probe(card);
2292 	if (ret < 0)
2293 		goto probe_end;
2294 
2295 	snd_soc_dapm_new_widgets(card);
2296 	snd_soc_card_fixup_controls(card);
2297 
2298 	ret = snd_card_register(card->snd_card);
2299 	if (ret < 0) {
2300 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2301 				ret);
2302 		goto probe_end;
2303 	}
2304 
2305 	card->instantiated = 1;
2306 	snd_soc_dapm_mark_endpoints_dirty(card);
2307 	snd_soc_dapm_sync(dapm);
2308 
2309 	/* deactivate pins to sleep state */
2310 	for_each_card_components(card, component)
2311 		if (!snd_soc_component_active(component))
2312 			pinctrl_pm_select_sleep_state(component->dev);
2313 
2314 probe_end:
2315 	if (ret < 0)
2316 		soc_cleanup_card_resources(card);
2317 	snd_soc_card_mutex_unlock(card);
2318 
2319 	return ret;
2320 }
2321 
devm_card_bind_release(struct device * dev,void * res)2322 static void devm_card_bind_release(struct device *dev, void *res)
2323 {
2324 	snd_soc_unregister_card(*(struct snd_soc_card **)res);
2325 }
2326 
devm_snd_soc_bind_card(struct device * dev,struct snd_soc_card * card)2327 static int devm_snd_soc_bind_card(struct device *dev, struct snd_soc_card *card)
2328 {
2329 	struct snd_soc_card **ptr;
2330 	int ret;
2331 
2332 	ptr = devres_alloc(devm_card_bind_release, sizeof(*ptr), GFP_KERNEL);
2333 	if (!ptr)
2334 		return -ENOMEM;
2335 
2336 	ret = snd_soc_bind_card(card);
2337 	if (ret == 0 || ret == -EPROBE_DEFER) {
2338 		*ptr = card;
2339 		devres_add(dev, ptr);
2340 	} else {
2341 		devres_free(ptr);
2342 	}
2343 
2344 	return ret;
2345 }
2346 
snd_soc_rebind_card(struct snd_soc_card * card)2347 static int snd_soc_rebind_card(struct snd_soc_card *card)
2348 {
2349 	int ret;
2350 
2351 	if (card->devres_dev) {
2352 		devres_destroy(card->devres_dev, devm_card_bind_release, NULL, NULL);
2353 		ret = devm_snd_soc_bind_card(card->devres_dev, card);
2354 	} else {
2355 		ret = snd_soc_bind_card(card);
2356 	}
2357 
2358 	if (ret != -EPROBE_DEFER)
2359 		list_del_init(&card->list);
2360 
2361 	return ret;
2362 }
2363 
2364 /* probes a new socdev */
soc_probe(struct platform_device * pdev)2365 static int soc_probe(struct platform_device *pdev)
2366 {
2367 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2368 
2369 	/*
2370 	 * no card, so machine driver should be registering card
2371 	 * we should not be here in that case so ret error
2372 	 */
2373 	if (!card)
2374 		return -EINVAL;
2375 
2376 	dev_warn(&pdev->dev,
2377 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2378 		 card->name);
2379 
2380 	/* Bodge while we unpick instantiation */
2381 	card->dev = &pdev->dev;
2382 
2383 	return devm_snd_soc_register_card(&pdev->dev, card);
2384 }
2385 
snd_soc_poweroff(struct device * dev)2386 int snd_soc_poweroff(struct device *dev)
2387 {
2388 	struct snd_soc_card *card = dev_get_drvdata(dev);
2389 	struct snd_soc_component *component;
2390 
2391 	if (!snd_soc_card_is_instantiated(card))
2392 		return 0;
2393 
2394 	/*
2395 	 * Flush out pmdown_time work - we actually do want to run it
2396 	 * now, we're shutting down so no imminent restart.
2397 	 */
2398 	snd_soc_flush_all_delayed_work(card);
2399 
2400 	snd_soc_dapm_shutdown(card);
2401 
2402 	/* deactivate pins to sleep state */
2403 	for_each_card_components(card, component)
2404 		pinctrl_pm_select_sleep_state(component->dev);
2405 
2406 	return 0;
2407 }
2408 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2409 
2410 const struct dev_pm_ops snd_soc_pm_ops = {
2411 	.suspend = snd_soc_suspend,
2412 	.resume = snd_soc_resume,
2413 	.freeze = snd_soc_suspend,
2414 	.thaw = snd_soc_resume,
2415 	.poweroff = snd_soc_poweroff,
2416 	.restore = snd_soc_resume,
2417 };
2418 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2419 
2420 /* ASoC platform driver */
2421 static struct platform_driver soc_driver = {
2422 	.driver		= {
2423 		.name		= "soc-audio",
2424 		.pm		= &snd_soc_pm_ops,
2425 	},
2426 	.probe		= soc_probe,
2427 };
2428 
2429 /**
2430  * snd_soc_cnew - create new control
2431  * @_template: control template
2432  * @data: control private data
2433  * @long_name: control long name
2434  * @prefix: control name prefix
2435  *
2436  * Create a new mixer control from a template control.
2437  *
2438  * Returns 0 for success, else error.
2439  */
snd_soc_cnew(const struct snd_kcontrol_new * _template,void * data,const char * long_name,const char * prefix)2440 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2441 				  void *data, const char *long_name,
2442 				  const char *prefix)
2443 {
2444 	struct snd_kcontrol_new template;
2445 	struct snd_kcontrol *kcontrol;
2446 	char *name = NULL;
2447 
2448 	memcpy(&template, _template, sizeof(template));
2449 	template.index = 0;
2450 
2451 	if (!long_name)
2452 		long_name = template.name;
2453 
2454 	if (prefix) {
2455 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2456 		if (!name)
2457 			return NULL;
2458 
2459 		template.name = name;
2460 	} else {
2461 		template.name = long_name;
2462 	}
2463 
2464 	kcontrol = snd_ctl_new1(&template, data);
2465 
2466 	kfree(name);
2467 
2468 	return kcontrol;
2469 }
2470 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2471 
snd_soc_add_controls(struct snd_card * card,struct device * dev,const struct snd_kcontrol_new * controls,int num_controls,const char * prefix,void * data)2472 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2473 	const struct snd_kcontrol_new *controls, int num_controls,
2474 	const char *prefix, void *data)
2475 {
2476 	int i;
2477 
2478 	for (i = 0; i < num_controls; i++) {
2479 		const struct snd_kcontrol_new *control = &controls[i];
2480 		int err = snd_ctl_add(card, snd_soc_cnew(control, data,
2481 							 control->name, prefix));
2482 		if (err < 0) {
2483 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2484 				control->name, err);
2485 			return err;
2486 		}
2487 	}
2488 
2489 	return 0;
2490 }
2491 
2492 /**
2493  * snd_soc_add_component_controls - Add an array of controls to a component.
2494  *
2495  * @component: Component to add controls to
2496  * @controls: Array of controls to add
2497  * @num_controls: Number of elements in the array
2498  *
2499  * Return: 0 for success, else error.
2500  */
snd_soc_add_component_controls(struct snd_soc_component * component,const struct snd_kcontrol_new * controls,unsigned int num_controls)2501 int snd_soc_add_component_controls(struct snd_soc_component *component,
2502 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
2503 {
2504 	struct snd_card *card = component->card->snd_card;
2505 
2506 	return snd_soc_add_controls(card, component->dev, controls,
2507 			num_controls, component->name_prefix, component);
2508 }
2509 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2510 
2511 /**
2512  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2513  * Convenience function to add a list of controls.
2514  *
2515  * @soc_card: SoC card to add controls to
2516  * @controls: array of controls to add
2517  * @num_controls: number of elements in the array
2518  *
2519  * Return 0 for success, else error.
2520  */
snd_soc_add_card_controls(struct snd_soc_card * soc_card,const struct snd_kcontrol_new * controls,int num_controls)2521 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2522 	const struct snd_kcontrol_new *controls, int num_controls)
2523 {
2524 	struct snd_card *card = soc_card->snd_card;
2525 
2526 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2527 			NULL, soc_card);
2528 }
2529 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2530 
2531 /**
2532  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2533  * Convenience function to add a list of controls.
2534  *
2535  * @dai: DAI to add controls to
2536  * @controls: array of controls to add
2537  * @num_controls: number of elements in the array
2538  *
2539  * Return 0 for success, else error.
2540  */
snd_soc_add_dai_controls(struct snd_soc_dai * dai,const struct snd_kcontrol_new * controls,int num_controls)2541 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2542 	const struct snd_kcontrol_new *controls, int num_controls)
2543 {
2544 	struct snd_card *card = dai->component->card->snd_card;
2545 
2546 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2547 			NULL, dai);
2548 }
2549 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2550 
2551 /**
2552  * snd_soc_register_card - Register a card with the ASoC core
2553  *
2554  * @card: Card to register
2555  *
2556  */
snd_soc_register_card(struct snd_soc_card * card)2557 int snd_soc_register_card(struct snd_soc_card *card)
2558 {
2559 	int ret;
2560 
2561 	if (!card->name || !card->dev)
2562 		return -EINVAL;
2563 
2564 	card->dapm = snd_soc_dapm_alloc(card->dev);
2565 	if (!card->dapm)
2566 		return -ENOMEM;
2567 
2568 	dev_set_drvdata(card->dev, card);
2569 
2570 	INIT_LIST_HEAD(&card->widgets);
2571 	INIT_LIST_HEAD(&card->paths);
2572 	INIT_LIST_HEAD(&card->dapm_list);
2573 	INIT_LIST_HEAD(&card->aux_comp_list);
2574 	INIT_LIST_HEAD(&card->component_dev_list);
2575 	INIT_LIST_HEAD(&card->list);
2576 	INIT_LIST_HEAD(&card->rtd_list);
2577 	INIT_LIST_HEAD(&card->dapm_dirty);
2578 	INIT_LIST_HEAD(&card->dobj_list);
2579 
2580 	card->instantiated = 0;
2581 	mutex_init(&card->mutex);
2582 	mutex_init(&card->dapm_mutex);
2583 	mutex_init(&card->pcm_mutex);
2584 
2585 	mutex_lock(&client_mutex);
2586 
2587 	if (card->devres_dev) {
2588 		ret = devm_snd_soc_bind_card(card->devres_dev, card);
2589 		if (ret == -EPROBE_DEFER) {
2590 			list_add(&card->list, &unbind_card_list);
2591 			ret = 0;
2592 		}
2593 	} else {
2594 		ret = snd_soc_bind_card(card);
2595 	}
2596 
2597 	mutex_unlock(&client_mutex);
2598 
2599 	return ret;
2600 }
2601 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2602 
2603 /**
2604  * snd_soc_unregister_card - Unregister a card with the ASoC core
2605  *
2606  * @card: Card to unregister
2607  *
2608  */
snd_soc_unregister_card(struct snd_soc_card * card)2609 void snd_soc_unregister_card(struct snd_soc_card *card)
2610 {
2611 	mutex_lock(&client_mutex);
2612 	snd_soc_unbind_card(card);
2613 	list_del(&card->list);
2614 	mutex_unlock(&client_mutex);
2615 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2616 }
2617 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2618 
2619 /*
2620  * Simplify DAI link configuration by removing ".-1" from device names
2621  * and sanitizing names.
2622  */
fmt_single_name(struct device * dev,int * id)2623 static char *fmt_single_name(struct device *dev, int *id)
2624 {
2625 	const char *devname = dev_name(dev);
2626 	char *found, *name;
2627 	unsigned int id1, id2;
2628 	int __id;
2629 
2630 	if (devname == NULL)
2631 		return NULL;
2632 
2633 	name = devm_kstrdup(dev, devname, GFP_KERNEL);
2634 	if (!name)
2635 		return NULL;
2636 
2637 	/* are we a "%s.%d" name (platform and SPI components) */
2638 	found = strstr(name, dev->driver->name);
2639 	if (found) {
2640 		/* get ID */
2641 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", &__id) == 1) {
2642 
2643 			/* discard ID from name if ID == -1 */
2644 			if (__id == -1)
2645 				found[strlen(dev->driver->name)] = '\0';
2646 		}
2647 
2648 	/* I2C component devices are named "bus-addr" */
2649 	} else if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2650 
2651 		/* create unique ID number from I2C addr and bus */
2652 		__id = ((id1 & 0xffff) << 16) + id2;
2653 
2654 		devm_kfree(dev, name);
2655 
2656 		/* sanitize component name for DAI link creation */
2657 		name = devm_kasprintf(dev, GFP_KERNEL, "%s.%s", dev->driver->name, devname);
2658 	} else {
2659 		__id = 0;
2660 	}
2661 
2662 	if (id)
2663 		*id = __id;
2664 
2665 	return name;
2666 }
2667 
2668 /*
2669  * Simplify DAI link naming for single devices with multiple DAIs by removing
2670  * any ".-1" and using the DAI name (instead of device name).
2671  */
fmt_multiple_name(struct device * dev,struct snd_soc_dai_driver * dai_drv)2672 static inline char *fmt_multiple_name(struct device *dev,
2673 		struct snd_soc_dai_driver *dai_drv)
2674 {
2675 	if (dai_drv->name == NULL) {
2676 		dev_err(dev,
2677 			"ASoC: error - multiple DAI %s registered with no name\n",
2678 			dev_name(dev));
2679 		return NULL;
2680 	}
2681 
2682 	return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2683 }
2684 
snd_soc_unregister_dai(struct snd_soc_dai * dai)2685 void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2686 {
2687 	lockdep_assert_held(&client_mutex);
2688 
2689 	dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2690 	list_del(&dai->list);
2691 }
2692 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2693 
2694 /**
2695  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2696  *
2697  * @component: The component the DAIs are registered for
2698  * @dai_drv: DAI driver to use for the DAI
2699  * @legacy_dai_naming: if %true, use legacy single-name format;
2700  * 	if %false, use multiple-name format;
2701  *
2702  * Topology can use this API to register DAIs when probing a component.
2703  * These DAIs's widgets will be freed in the card cleanup and the DAIs
2704  * will be freed in the component cleanup.
2705  */
snd_soc_register_dai(struct snd_soc_component * component,struct snd_soc_dai_driver * dai_drv,bool legacy_dai_naming)2706 struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
2707 					 struct snd_soc_dai_driver *dai_drv,
2708 					 bool legacy_dai_naming)
2709 {
2710 	struct device *dev = component->dev;
2711 	struct snd_soc_dai *dai;
2712 
2713 	lockdep_assert_held(&client_mutex);
2714 
2715 	dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
2716 	if (dai == NULL)
2717 		return NULL;
2718 
2719 	/*
2720 	 * Back in the old days when we still had component-less DAIs,
2721 	 * instead of having a static name, component-less DAIs would
2722 	 * inherit the name of the parent device so it is possible to
2723 	 * register multiple instances of the DAI. We still need to keep
2724 	 * the same naming style even though those DAIs are not
2725 	 * component-less anymore.
2726 	 */
2727 	if (legacy_dai_naming &&
2728 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
2729 		dai->name = fmt_single_name(dev, &dai->id);
2730 	} else {
2731 		dai->name = fmt_multiple_name(dev, dai_drv);
2732 		if (dai_drv->id)
2733 			dai->id = dai_drv->id;
2734 		else
2735 			dai->id = component->num_dai;
2736 	}
2737 	if (!dai->name)
2738 		return NULL;
2739 
2740 	dai->component = component;
2741 	dai->dev = dev;
2742 	dai->driver = dai_drv;
2743 
2744 	/* see for_each_component_dais */
2745 	list_add_tail(&dai->list, &component->dai_list);
2746 	component->num_dai++;
2747 
2748 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2749 	return dai;
2750 }
2751 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2752 
2753 /**
2754  * snd_soc_unregister_dais - Unregister DAIs from the ASoC core
2755  *
2756  * @component: The component for which the DAIs should be unregistered
2757  */
snd_soc_unregister_dais(struct snd_soc_component * component)2758 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2759 {
2760 	struct snd_soc_dai *dai, *_dai;
2761 
2762 	for_each_component_dais_safe(component, dai, _dai)
2763 		snd_soc_unregister_dai(dai);
2764 }
2765 
2766 /**
2767  * snd_soc_register_dais - Register a DAI with the ASoC core
2768  *
2769  * @component: The component the DAIs are registered for
2770  * @dai_drv: DAI driver to use for the DAIs
2771  * @count: Number of DAIs
2772  */
snd_soc_register_dais(struct snd_soc_component * component,struct snd_soc_dai_driver * dai_drv,size_t count)2773 static int snd_soc_register_dais(struct snd_soc_component *component,
2774 				 struct snd_soc_dai_driver *dai_drv,
2775 				 size_t count)
2776 {
2777 	struct snd_soc_dai *dai;
2778 	unsigned int i;
2779 	int ret;
2780 
2781 	for (i = 0; i < count; i++) {
2782 		dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2783 					   component->driver->legacy_dai_naming);
2784 		if (dai == NULL) {
2785 			ret = -ENOMEM;
2786 			goto err;
2787 		}
2788 	}
2789 
2790 	return 0;
2791 
2792 err:
2793 	snd_soc_unregister_dais(component);
2794 
2795 	return ret;
2796 }
2797 
2798 #define ENDIANNESS_MAP(name) \
2799 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2800 static u64 endianness_format_map[] = {
2801 	ENDIANNESS_MAP(S16_),
2802 	ENDIANNESS_MAP(U16_),
2803 	ENDIANNESS_MAP(S24_),
2804 	ENDIANNESS_MAP(U24_),
2805 	ENDIANNESS_MAP(S32_),
2806 	ENDIANNESS_MAP(U32_),
2807 	ENDIANNESS_MAP(S24_3),
2808 	ENDIANNESS_MAP(U24_3),
2809 	ENDIANNESS_MAP(S20_3),
2810 	ENDIANNESS_MAP(U20_3),
2811 	ENDIANNESS_MAP(S18_3),
2812 	ENDIANNESS_MAP(U18_3),
2813 	ENDIANNESS_MAP(FLOAT_),
2814 	ENDIANNESS_MAP(FLOAT64_),
2815 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2816 };
2817 
2818 /*
2819  * Fix up the DAI formats for endianness: codecs don't actually see
2820  * the endianness of the data but we're using the CPU format
2821  * definitions which do need to include endianness so we ensure that
2822  * codec DAIs always have both big and little endian variants set.
2823  */
convert_endianness_formats(struct snd_soc_pcm_stream * stream)2824 static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2825 {
2826 	int i;
2827 
2828 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2829 		if (stream->formats & endianness_format_map[i])
2830 			stream->formats |= endianness_format_map[i];
2831 }
2832 
snd_soc_del_component_unlocked(struct snd_soc_component * component)2833 static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2834 {
2835 	struct snd_soc_card *card = component->card;
2836 	bool instantiated;
2837 
2838 	snd_soc_unregister_dais(component);
2839 
2840 	if (card) {
2841 		instantiated = card->instantiated;
2842 		snd_soc_unbind_card(card);
2843 		if (instantiated)
2844 			list_add(&card->list, &unbind_card_list);
2845 	}
2846 
2847 	list_del(&component->list);
2848 }
2849 
snd_soc_component_initialize(struct snd_soc_component * component,const struct snd_soc_component_driver * driver,struct device * dev)2850 int snd_soc_component_initialize(struct snd_soc_component *component,
2851 				 const struct snd_soc_component_driver *driver,
2852 				 struct device *dev)
2853 {
2854 	component->dapm = snd_soc_dapm_alloc(dev);
2855 	if (!component->dapm)
2856 		return -ENOMEM;
2857 
2858 	INIT_LIST_HEAD(&component->dai_list);
2859 	INIT_LIST_HEAD(&component->dobj_list);
2860 	INIT_LIST_HEAD(&component->card_list);
2861 	INIT_LIST_HEAD(&component->list);
2862 	mutex_init(&component->io_mutex);
2863 
2864 	if (!component->name) {
2865 		component->name = fmt_single_name(dev, NULL);
2866 		if (!component->name) {
2867 			dev_err(dev, "ASoC: Failed to allocate name\n");
2868 			return -ENOMEM;
2869 		}
2870 	}
2871 
2872 	component->dev		= dev;
2873 	component->driver	= driver;
2874 
2875 #ifdef CONFIG_DEBUG_FS
2876 	if (!component->debugfs_prefix)
2877 		component->debugfs_prefix = driver->debugfs_prefix;
2878 #endif
2879 
2880 	return 0;
2881 }
2882 EXPORT_SYMBOL_GPL(snd_soc_component_initialize);
2883 
snd_soc_add_component(struct snd_soc_component * component,struct snd_soc_dai_driver * dai_drv,int num_dai)2884 int snd_soc_add_component(struct snd_soc_component *component,
2885 			  struct snd_soc_dai_driver *dai_drv,
2886 			  int num_dai)
2887 {
2888 	struct snd_soc_card *card, *c;
2889 	int ret;
2890 	int i;
2891 
2892 	mutex_lock(&client_mutex);
2893 
2894 	if (component->driver->endianness) {
2895 		for (i = 0; i < num_dai; i++) {
2896 			convert_endianness_formats(&dai_drv[i].playback);
2897 			convert_endianness_formats(&dai_drv[i].capture);
2898 		}
2899 	}
2900 
2901 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2902 	if (ret < 0) {
2903 		dev_err(component->dev, "ASoC: Failed to register DAIs: %d\n",
2904 			ret);
2905 		goto err_cleanup;
2906 	}
2907 
2908 	if (!component->driver->write && !component->driver->read) {
2909 		if (!component->regmap)
2910 			component->regmap = dev_get_regmap(component->dev,
2911 							   NULL);
2912 		if (component->regmap)
2913 			snd_soc_component_setup_regmap(component);
2914 	}
2915 
2916 	/* see for_each_component */
2917 	list_add(&component->list, &component_list);
2918 
2919 	list_for_each_entry_safe(card, c, &unbind_card_list, list)
2920 		snd_soc_rebind_card(card);
2921 
2922 err_cleanup:
2923 	if (ret < 0)
2924 		snd_soc_del_component_unlocked(component);
2925 
2926 	mutex_unlock(&client_mutex);
2927 	return ret;
2928 }
2929 EXPORT_SYMBOL_GPL(snd_soc_add_component);
2930 
snd_soc_register_component(struct device * dev,const struct snd_soc_component_driver * component_driver,struct snd_soc_dai_driver * dai_drv,int num_dai)2931 int snd_soc_register_component(struct device *dev,
2932 			const struct snd_soc_component_driver *component_driver,
2933 			struct snd_soc_dai_driver *dai_drv,
2934 			int num_dai)
2935 {
2936 	struct snd_soc_component *component;
2937 	int ret;
2938 
2939 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
2940 	if (!component)
2941 		return -ENOMEM;
2942 
2943 	ret = snd_soc_component_initialize(component, component_driver, dev);
2944 	if (ret < 0)
2945 		return ret;
2946 
2947 	return snd_soc_add_component(component, dai_drv, num_dai);
2948 }
2949 EXPORT_SYMBOL_GPL(snd_soc_register_component);
2950 
2951 /**
2952  * snd_soc_unregister_component_by_driver - Unregister component using a given driver
2953  * from the ASoC core
2954  *
2955  * @dev: The device to unregister
2956  * @component_driver: The component driver to unregister
2957  */
snd_soc_unregister_component_by_driver(struct device * dev,const struct snd_soc_component_driver * component_driver)2958 void snd_soc_unregister_component_by_driver(struct device *dev,
2959 					    const struct snd_soc_component_driver *component_driver)
2960 {
2961 	const char *driver_name = NULL;
2962 
2963 	if (component_driver)
2964 		driver_name = component_driver->name;
2965 
2966 	mutex_lock(&client_mutex);
2967 	while (1) {
2968 		struct snd_soc_component *component = snd_soc_lookup_component_nolocked(dev, driver_name);
2969 
2970 		if (!component)
2971 			break;
2972 
2973 		snd_soc_del_component_unlocked(component);
2974 	}
2975 	mutex_unlock(&client_mutex);
2976 }
2977 EXPORT_SYMBOL_GPL(snd_soc_unregister_component_by_driver);
2978 
2979 /* Retrieve a card's name from device tree */
snd_soc_of_parse_card_name(struct snd_soc_card * card,const char * propname)2980 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2981 			       const char *propname)
2982 {
2983 	struct device_node *np;
2984 	int ret;
2985 
2986 	if (!card->dev) {
2987 		pr_err("card->dev is not set before calling %s\n", __func__);
2988 		return -EINVAL;
2989 	}
2990 
2991 	np = card->dev->of_node;
2992 
2993 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2994 	/*
2995 	 * EINVAL means the property does not exist. This is fine providing
2996 	 * card->name was previously set, which is checked later in
2997 	 * snd_soc_register_card.
2998 	 */
2999 	if (ret < 0 && ret != -EINVAL) {
3000 		dev_err(card->dev,
3001 			"ASoC: Property '%s' could not be read: %d\n",
3002 			propname, ret);
3003 		return ret;
3004 	}
3005 
3006 	return 0;
3007 }
3008 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3009 
3010 static const struct snd_soc_dapm_widget simple_widgets[] = {
3011 	SND_SOC_DAPM_MIC("Microphone", NULL),
3012 	SND_SOC_DAPM_LINE("Line", NULL),
3013 	SND_SOC_DAPM_HP("Headphone", NULL),
3014 	SND_SOC_DAPM_SPK("Speaker", NULL),
3015 };
3016 
snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card * card,const char * propname)3017 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
3018 					  const char *propname)
3019 {
3020 	struct device_node *np = card->dev->of_node;
3021 	struct snd_soc_dapm_widget *widgets;
3022 	const char *template, *wname;
3023 	int i, j, num_widgets;
3024 
3025 	num_widgets = of_property_count_strings(np, propname);
3026 	if (num_widgets < 0) {
3027 		dev_err(card->dev,
3028 			"ASoC: Property '%s' does not exist\n",	propname);
3029 		return -EINVAL;
3030 	}
3031 	if (!num_widgets) {
3032 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3033 			propname);
3034 		return -EINVAL;
3035 	}
3036 	if (num_widgets & 1) {
3037 		dev_err(card->dev,
3038 			"ASoC: Property '%s' length is not even\n", propname);
3039 		return -EINVAL;
3040 	}
3041 
3042 	num_widgets /= 2;
3043 
3044 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3045 			       GFP_KERNEL);
3046 	if (!widgets) {
3047 		dev_err(card->dev,
3048 			"ASoC: Could not allocate memory for widgets\n");
3049 		return -ENOMEM;
3050 	}
3051 
3052 	for (i = 0; i < num_widgets; i++) {
3053 		int ret = of_property_read_string_index(np, propname,
3054 							2 * i, &template);
3055 		if (ret) {
3056 			dev_err(card->dev,
3057 				"ASoC: Property '%s' index %d read error:%d\n",
3058 				propname, 2 * i, ret);
3059 			return -EINVAL;
3060 		}
3061 
3062 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3063 			if (!strncmp(template, simple_widgets[j].name,
3064 				     strlen(simple_widgets[j].name))) {
3065 				widgets[i] = simple_widgets[j];
3066 				break;
3067 			}
3068 		}
3069 
3070 		if (j >= ARRAY_SIZE(simple_widgets)) {
3071 			dev_err(card->dev,
3072 				"ASoC: DAPM widget '%s' is not supported\n",
3073 				template);
3074 			return -EINVAL;
3075 		}
3076 
3077 		ret = of_property_read_string_index(np, propname,
3078 						    (2 * i) + 1,
3079 						    &wname);
3080 		if (ret) {
3081 			dev_err(card->dev,
3082 				"ASoC: Property '%s' index %d read error:%d\n",
3083 				propname, (2 * i) + 1, ret);
3084 			return -EINVAL;
3085 		}
3086 
3087 		widgets[i].name = wname;
3088 	}
3089 
3090 	card->of_dapm_widgets = widgets;
3091 	card->num_of_dapm_widgets = num_widgets;
3092 
3093 	return 0;
3094 }
3095 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3096 
snd_soc_of_parse_pin_switches(struct snd_soc_card * card,const char * prop)3097 int snd_soc_of_parse_pin_switches(struct snd_soc_card *card, const char *prop)
3098 {
3099 	const unsigned int nb_controls_max = 16;
3100 	const char **strings, *control_name;
3101 	struct snd_kcontrol_new *controls;
3102 	struct device *dev = card->dev;
3103 	unsigned int i, nb_controls;
3104 	int ret;
3105 
3106 	if (!of_property_present(dev->of_node, prop))
3107 		return 0;
3108 
3109 	strings = devm_kcalloc(dev, nb_controls_max,
3110 			       sizeof(*strings), GFP_KERNEL);
3111 	if (!strings)
3112 		return -ENOMEM;
3113 
3114 	ret = of_property_read_string_array(dev->of_node, prop,
3115 					    strings, nb_controls_max);
3116 	if (ret < 0)
3117 		return ret;
3118 
3119 	nb_controls = (unsigned int)ret;
3120 
3121 	controls = devm_kcalloc(dev, nb_controls,
3122 				sizeof(*controls), GFP_KERNEL);
3123 	if (!controls)
3124 		return -ENOMEM;
3125 
3126 	for (i = 0; i < nb_controls; i++) {
3127 		control_name = devm_kasprintf(dev, GFP_KERNEL,
3128 					      "%s Switch", strings[i]);
3129 		if (!control_name)
3130 			return -ENOMEM;
3131 
3132 		controls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
3133 		controls[i].name = control_name;
3134 		controls[i].info = snd_soc_dapm_info_pin_switch;
3135 		controls[i].get = snd_soc_dapm_get_pin_switch;
3136 		controls[i].put = snd_soc_dapm_put_pin_switch;
3137 		controls[i].private_value = (unsigned long)strings[i];
3138 	}
3139 
3140 	card->controls = controls;
3141 	card->num_controls = nb_controls;
3142 
3143 	return 0;
3144 }
3145 EXPORT_SYMBOL_GPL(snd_soc_of_parse_pin_switches);
3146 
snd_soc_of_get_slot_mask(struct device_node * np,const char * prop_name,unsigned int * mask)3147 int snd_soc_of_get_slot_mask(struct device_node *np,
3148 			     const char *prop_name,
3149 			     unsigned int *mask)
3150 {
3151 	u32 val;
3152 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
3153 	int i;
3154 
3155 	if (!of_slot_mask)
3156 		return 0;
3157 	val /= sizeof(u32);
3158 	for (i = 0; i < val; i++)
3159 		if (be32_to_cpup(&of_slot_mask[i]))
3160 			*mask |= (1 << i);
3161 
3162 	return val;
3163 }
3164 EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
3165 
snd_soc_of_parse_tdm_slot(struct device_node * np,unsigned int * tx_mask,unsigned int * rx_mask,unsigned int * slots,unsigned int * slot_width)3166 int snd_soc_of_parse_tdm_slot(struct device_node *np,
3167 			      unsigned int *tx_mask,
3168 			      unsigned int *rx_mask,
3169 			      unsigned int *slots,
3170 			      unsigned int *slot_width)
3171 {
3172 	u32 val;
3173 	int ret;
3174 
3175 	if (tx_mask)
3176 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3177 	if (rx_mask)
3178 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3179 
3180 	ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3181 	if (ret && ret != -EINVAL)
3182 		return ret;
3183 	if (!ret && slots)
3184 		*slots = val;
3185 
3186 	ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3187 	if (ret && ret != -EINVAL)
3188 		return ret;
3189 	if (!ret && slot_width)
3190 		*slot_width = val;
3191 
3192 	return 0;
3193 }
3194 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3195 
snd_soc_dlc_use_cpu_as_platform(struct snd_soc_dai_link_component * platforms,struct snd_soc_dai_link_component * cpus)3196 void snd_soc_dlc_use_cpu_as_platform(struct snd_soc_dai_link_component *platforms,
3197 				     struct snd_soc_dai_link_component *cpus)
3198 {
3199 	platforms->of_node	= cpus->of_node;
3200 	platforms->dai_args	= cpus->dai_args;
3201 }
3202 EXPORT_SYMBOL_GPL(snd_soc_dlc_use_cpu_as_platform);
3203 
snd_soc_of_parse_node_prefix(struct device_node * np,struct snd_soc_codec_conf * codec_conf,struct device_node * of_node,const char * propname)3204 void snd_soc_of_parse_node_prefix(struct device_node *np,
3205 				  struct snd_soc_codec_conf *codec_conf,
3206 				  struct device_node *of_node,
3207 				  const char *propname)
3208 {
3209 	const char *str;
3210 	int ret;
3211 
3212 	ret = of_property_read_string(np, propname, &str);
3213 	if (ret < 0) {
3214 		/* no prefix is not error */
3215 		return;
3216 	}
3217 
3218 	codec_conf->dlc.of_node	= of_node;
3219 	codec_conf->name_prefix	= str;
3220 }
3221 EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
3222 
snd_soc_of_parse_audio_routing(struct snd_soc_card * card,const char * propname)3223 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3224 				   const char *propname)
3225 {
3226 	struct device_node *np = card->dev->of_node;
3227 	int num_routes;
3228 	struct snd_soc_dapm_route *routes;
3229 	int i;
3230 
3231 	num_routes = of_property_count_strings(np, propname);
3232 	if (num_routes < 0 || num_routes & 1) {
3233 		dev_err(card->dev,
3234 			"ASoC: Property '%s' does not exist or its length is not even\n",
3235 			propname);
3236 		return -EINVAL;
3237 	}
3238 	num_routes /= 2;
3239 
3240 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3241 			      GFP_KERNEL);
3242 	if (!routes) {
3243 		dev_err(card->dev,
3244 			"ASoC: Could not allocate DAPM route table\n");
3245 		return -ENOMEM;
3246 	}
3247 
3248 	for (i = 0; i < num_routes; i++) {
3249 		int ret = of_property_read_string_index(np, propname,
3250 							2 * i, &routes[i].sink);
3251 		if (ret) {
3252 			dev_err(card->dev,
3253 				"ASoC: Property '%s' index %d could not be read: %d\n",
3254 				propname, 2 * i, ret);
3255 			return -EINVAL;
3256 		}
3257 		ret = of_property_read_string_index(np, propname,
3258 			(2 * i) + 1, &routes[i].source);
3259 		if (ret) {
3260 			dev_err(card->dev,
3261 				"ASoC: Property '%s' index %d could not be read: %d\n",
3262 				propname, (2 * i) + 1, ret);
3263 			return -EINVAL;
3264 		}
3265 	}
3266 
3267 	card->num_of_dapm_routes = num_routes;
3268 	card->of_dapm_routes = routes;
3269 
3270 	return 0;
3271 }
3272 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3273 
snd_soc_of_parse_aux_devs(struct snd_soc_card * card,const char * propname)3274 int snd_soc_of_parse_aux_devs(struct snd_soc_card *card, const char *propname)
3275 {
3276 	struct device_node *node = card->dev->of_node;
3277 	struct snd_soc_aux_dev *aux;
3278 	int num, i;
3279 
3280 	num = of_count_phandle_with_args(node, propname, NULL);
3281 	if (num == -ENOENT) {
3282 		return 0;
3283 	} else if (num < 0) {
3284 		dev_err(card->dev, "ASOC: Property '%s' could not be read: %d\n",
3285 			propname, num);
3286 		return num;
3287 	}
3288 
3289 	aux = devm_kcalloc(card->dev, num, sizeof(*aux), GFP_KERNEL);
3290 	if (!aux)
3291 		return -ENOMEM;
3292 	card->aux_dev = aux;
3293 	card->num_aux_devs = num;
3294 
3295 	for_each_card_pre_auxs(card, i, aux) {
3296 		aux->dlc.of_node = of_parse_phandle(node, propname, i);
3297 		if (!aux->dlc.of_node)
3298 			return -EINVAL;
3299 	}
3300 
3301 	return 0;
3302 }
3303 EXPORT_SYMBOL_GPL(snd_soc_of_parse_aux_devs);
3304 
snd_soc_daifmt_clock_provider_flipped(unsigned int dai_fmt)3305 unsigned int snd_soc_daifmt_clock_provider_flipped(unsigned int dai_fmt)
3306 {
3307 	unsigned int inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
3308 
3309 	switch (dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
3310 	case SND_SOC_DAIFMT_CBP_CFP:
3311 		inv_dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
3312 		break;
3313 	case SND_SOC_DAIFMT_CBP_CFC:
3314 		inv_dai_fmt |= SND_SOC_DAIFMT_CBC_CFP;
3315 		break;
3316 	case SND_SOC_DAIFMT_CBC_CFP:
3317 		inv_dai_fmt |= SND_SOC_DAIFMT_CBP_CFC;
3318 		break;
3319 	case SND_SOC_DAIFMT_CBC_CFC:
3320 		inv_dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
3321 		break;
3322 	}
3323 
3324 	return inv_dai_fmt;
3325 }
3326 EXPORT_SYMBOL_GPL(snd_soc_daifmt_clock_provider_flipped);
3327 
snd_soc_daifmt_clock_provider_from_bitmap(unsigned int bit_frame)3328 unsigned int snd_soc_daifmt_clock_provider_from_bitmap(unsigned int bit_frame)
3329 {
3330 	/*
3331 	 * bit_frame is return value from
3332 	 *	snd_soc_daifmt_parse_clock_provider_raw()
3333 	 */
3334 
3335 	/* Codec base */
3336 	switch (bit_frame) {
3337 	case 0x11:
3338 		return SND_SOC_DAIFMT_CBP_CFP;
3339 	case 0x10:
3340 		return SND_SOC_DAIFMT_CBP_CFC;
3341 	case 0x01:
3342 		return SND_SOC_DAIFMT_CBC_CFP;
3343 	default:
3344 		return SND_SOC_DAIFMT_CBC_CFC;
3345 	}
3346 
3347 	return 0;
3348 }
3349 EXPORT_SYMBOL_GPL(snd_soc_daifmt_clock_provider_from_bitmap);
3350 
snd_soc_daifmt_parse_format(struct device_node * np,const char * prefix)3351 unsigned int snd_soc_daifmt_parse_format(struct device_node *np,
3352 					 const char *prefix)
3353 {
3354 	int ret;
3355 	char prop[128];
3356 	unsigned int format = 0;
3357 	int bit, frame;
3358 	const char *str;
3359 	struct {
3360 		char *name;
3361 		unsigned int val;
3362 	} of_fmt_table[] = {
3363 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3364 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3365 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3366 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3367 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3368 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3369 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3370 		{ "msb",	SND_SOC_DAIFMT_MSB },
3371 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3372 	};
3373 
3374 	if (!prefix)
3375 		prefix = "";
3376 
3377 	/*
3378 	 * check "dai-format = xxx"
3379 	 * or    "[prefix]format = xxx"
3380 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3381 	 */
3382 	ret = of_property_read_string(np, "dai-format", &str);
3383 	if (ret < 0) {
3384 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3385 		ret = of_property_read_string(np, prop, &str);
3386 	}
3387 	if (ret == 0) {
3388 		int i;
3389 
3390 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3391 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3392 				format |= of_fmt_table[i].val;
3393 				break;
3394 			}
3395 		}
3396 	}
3397 
3398 	/*
3399 	 * check "[prefix]continuous-clock"
3400 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3401 	 */
3402 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3403 	if (of_property_read_bool(np, prop))
3404 		format |= SND_SOC_DAIFMT_CONT;
3405 	else
3406 		format |= SND_SOC_DAIFMT_GATED;
3407 
3408 	/*
3409 	 * check "[prefix]bitclock-inversion"
3410 	 * check "[prefix]frame-inversion"
3411 	 * SND_SOC_DAIFMT_INV_MASK area
3412 	 */
3413 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3414 	bit = of_property_read_bool(np, prop);
3415 
3416 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3417 	frame = of_property_read_bool(np, prop);
3418 
3419 	switch ((bit << 4) + frame) {
3420 	case 0x11:
3421 		format |= SND_SOC_DAIFMT_IB_IF;
3422 		break;
3423 	case 0x10:
3424 		format |= SND_SOC_DAIFMT_IB_NF;
3425 		break;
3426 	case 0x01:
3427 		format |= SND_SOC_DAIFMT_NB_IF;
3428 		break;
3429 	default:
3430 		/* SND_SOC_DAIFMT_NB_NF is default */
3431 		break;
3432 	}
3433 
3434 	return format;
3435 }
3436 EXPORT_SYMBOL_GPL(snd_soc_daifmt_parse_format);
3437 
snd_soc_daifmt_parse_clock_provider_raw(struct device_node * np,const char * prefix,struct device_node ** bitclkmaster,struct device_node ** framemaster)3438 unsigned int snd_soc_daifmt_parse_clock_provider_raw(struct device_node *np,
3439 						     const char *prefix,
3440 						     struct device_node **bitclkmaster,
3441 						     struct device_node **framemaster)
3442 {
3443 	char prop[128];
3444 	unsigned int bit, frame;
3445 
3446 	if (!np)
3447 		return 0;
3448 
3449 	if (!prefix)
3450 		prefix = "";
3451 
3452 	/*
3453 	 * check "[prefix]bitclock-master"
3454 	 * check "[prefix]frame-master"
3455 	 */
3456 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3457 	bit = of_property_present(np, prop);
3458 	if (bit && bitclkmaster)
3459 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3460 
3461 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3462 	frame = of_property_present(np, prop);
3463 	if (frame && framemaster)
3464 		*framemaster = of_parse_phandle(np, prop, 0);
3465 
3466 	/*
3467 	 * return bitmap.
3468 	 * It will be parameter of
3469 	 *	snd_soc_daifmt_clock_provider_from_bitmap()
3470 	 */
3471 	return (bit << 4) + frame;
3472 }
3473 EXPORT_SYMBOL_GPL(snd_soc_daifmt_parse_clock_provider_raw);
3474 
snd_soc_get_stream_cpu(const struct snd_soc_dai_link * dai_link,int stream)3475 int snd_soc_get_stream_cpu(const struct snd_soc_dai_link *dai_link, int stream)
3476 {
3477 	/*
3478 	 * [Normal]
3479 	 *
3480 	 * Playback
3481 	 *	CPU  : SNDRV_PCM_STREAM_PLAYBACK
3482 	 *	Codec: SNDRV_PCM_STREAM_PLAYBACK
3483 	 *
3484 	 * Capture
3485 	 *	CPU  : SNDRV_PCM_STREAM_CAPTURE
3486 	 *	Codec: SNDRV_PCM_STREAM_CAPTURE
3487 	 */
3488 	if (!dai_link->c2c_params)
3489 		return stream;
3490 
3491 	/*
3492 	 * [Codec2Codec]
3493 	 *
3494 	 * Playback
3495 	 *	CPU  : SNDRV_PCM_STREAM_CAPTURE
3496 	 *	Codec: SNDRV_PCM_STREAM_PLAYBACK
3497 	 *
3498 	 * Capture
3499 	 *	CPU  : SNDRV_PCM_STREAM_PLAYBACK
3500 	 *	Codec: SNDRV_PCM_STREAM_CAPTURE
3501 	 */
3502 	if (stream == SNDRV_PCM_STREAM_CAPTURE)
3503 		return SNDRV_PCM_STREAM_PLAYBACK;
3504 
3505 	return SNDRV_PCM_STREAM_CAPTURE;
3506 }
3507 EXPORT_SYMBOL_GPL(snd_soc_get_stream_cpu);
3508 
snd_soc_get_dai_id(struct device_node * ep)3509 int snd_soc_get_dai_id(struct device_node *ep)
3510 {
3511 	struct snd_soc_component *component;
3512 	struct snd_soc_dai_link_component dlc = {
3513 		.of_node = of_graph_get_port_parent(ep),
3514 	};
3515 	int ret;
3516 
3517 
3518 	/*
3519 	 * For example HDMI case, HDMI has video/sound port,
3520 	 * but ALSA SoC needs sound port number only.
3521 	 * Thus counting HDMI DT port/endpoint doesn't work.
3522 	 * Then, it should have .of_xlate_dai_id
3523 	 */
3524 	ret = -ENOTSUPP;
3525 	mutex_lock(&client_mutex);
3526 	component = soc_find_component(&dlc);
3527 	if (component)
3528 		ret = snd_soc_component_of_xlate_dai_id(component, ep);
3529 	mutex_unlock(&client_mutex);
3530 
3531 	of_node_put(dlc.of_node);
3532 
3533 	return ret;
3534 }
3535 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3536 
snd_soc_get_dlc(const struct of_phandle_args * args,struct snd_soc_dai_link_component * dlc)3537 int snd_soc_get_dlc(const struct of_phandle_args *args, struct snd_soc_dai_link_component *dlc)
3538 {
3539 	struct snd_soc_component *pos;
3540 	int ret = -EPROBE_DEFER;
3541 
3542 	mutex_lock(&client_mutex);
3543 	for_each_component(pos) {
3544 		struct device_node *component_of_node = soc_component_to_node(pos);
3545 
3546 		if (component_of_node != args->np || !pos->num_dai)
3547 			continue;
3548 
3549 		ret = snd_soc_component_of_xlate_dai_name(pos, args, &dlc->dai_name);
3550 		if (ret == -ENOTSUPP) {
3551 			struct snd_soc_dai *dai;
3552 			int id = -1;
3553 
3554 			switch (args->args_count) {
3555 			case 0:
3556 				id = 0; /* same as dai_drv[0] */
3557 				break;
3558 			case 1:
3559 				id = args->args[0];
3560 				break;
3561 			default:
3562 				/* not supported */
3563 				break;
3564 			}
3565 
3566 			if (id < 0 || id >= pos->num_dai) {
3567 				ret = -EINVAL;
3568 				continue;
3569 			}
3570 
3571 			ret = 0;
3572 
3573 			/* find target DAI */
3574 			for_each_component_dais(pos, dai) {
3575 				if (id == 0)
3576 					break;
3577 				id--;
3578 			}
3579 
3580 			dlc->dai_name	= snd_soc_dai_name_get(dai);
3581 		} else if (ret) {
3582 			/*
3583 			 * if another error than ENOTSUPP is returned go on and
3584 			 * check if another component is provided with the same
3585 			 * node. This may happen if a device provides several
3586 			 * components
3587 			 */
3588 			continue;
3589 		}
3590 
3591 		break;
3592 	}
3593 
3594 	if (ret == 0)
3595 		dlc->of_node = args->np;
3596 
3597 	mutex_unlock(&client_mutex);
3598 	return ret;
3599 }
3600 EXPORT_SYMBOL_GPL(snd_soc_get_dlc);
3601 
snd_soc_of_get_dlc(struct device_node * of_node,struct of_phandle_args * args,struct snd_soc_dai_link_component * dlc,int index)3602 int snd_soc_of_get_dlc(struct device_node *of_node,
3603 		       struct of_phandle_args *args,
3604 		       struct snd_soc_dai_link_component *dlc,
3605 		       int index)
3606 {
3607 	struct of_phandle_args __args;
3608 	int ret;
3609 
3610 	if (!args)
3611 		args = &__args;
3612 
3613 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
3614 					 "#sound-dai-cells", index, args);
3615 	if (ret)
3616 		return ret;
3617 
3618 	return snd_soc_get_dlc(args, dlc);
3619 }
3620 EXPORT_SYMBOL_GPL(snd_soc_of_get_dlc);
3621 
snd_soc_get_dai_name(const struct of_phandle_args * args,const char ** dai_name)3622 int snd_soc_get_dai_name(const struct of_phandle_args *args,
3623 			 const char **dai_name)
3624 {
3625 	struct snd_soc_dai_link_component dlc;
3626 	int ret = snd_soc_get_dlc(args, &dlc);
3627 
3628 	if (ret == 0)
3629 		*dai_name = dlc.dai_name;
3630 
3631 	return ret;
3632 }
3633 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
3634 
snd_soc_of_get_dai_name(struct device_node * of_node,const char ** dai_name,int index)3635 int snd_soc_of_get_dai_name(struct device_node *of_node,
3636 			    const char **dai_name, int index)
3637 {
3638 	struct snd_soc_dai_link_component dlc;
3639 	int ret = snd_soc_of_get_dlc(of_node, NULL, &dlc, index);
3640 
3641 	if (ret == 0)
3642 		*dai_name = dlc.dai_name;
3643 
3644 	return ret;
3645 }
3646 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3647 
snd_soc_get_dai_via_args(const struct of_phandle_args * dai_args)3648 struct snd_soc_dai *snd_soc_get_dai_via_args(const struct of_phandle_args *dai_args)
3649 {
3650 	struct snd_soc_dai *dai;
3651 	struct snd_soc_component *component;
3652 
3653 	mutex_lock(&client_mutex);
3654 	for_each_component(component) {
3655 		for_each_component_dais(component, dai)
3656 			if (snd_soc_is_match_dai_args(dai->driver->dai_args, dai_args))
3657 				goto found;
3658 	}
3659 	dai = NULL;
3660 found:
3661 	mutex_unlock(&client_mutex);
3662 	return dai;
3663 }
3664 EXPORT_SYMBOL_GPL(snd_soc_get_dai_via_args);
3665 
__snd_soc_of_put_component(struct snd_soc_dai_link_component * component)3666 static void __snd_soc_of_put_component(struct snd_soc_dai_link_component *component)
3667 {
3668 	if (component->of_node) {
3669 		of_node_put(component->of_node);
3670 		component->of_node = NULL;
3671 	}
3672 }
3673 
__snd_soc_of_get_dai_link_component_alloc(struct device * dev,struct device_node * of_node,struct snd_soc_dai_link_component ** ret_component,int * ret_num)3674 static int __snd_soc_of_get_dai_link_component_alloc(
3675 	struct device *dev, struct device_node *of_node,
3676 	struct snd_soc_dai_link_component **ret_component,
3677 	int *ret_num)
3678 {
3679 	struct snd_soc_dai_link_component *component;
3680 	int num;
3681 
3682 	/* Count the number of CPUs/CODECs */
3683 	num = of_count_phandle_with_args(of_node, "sound-dai", "#sound-dai-cells");
3684 	if (num <= 0) {
3685 		if (num == -ENOENT)
3686 			dev_err(dev, "No 'sound-dai' property\n");
3687 		else
3688 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
3689 		return num;
3690 	}
3691 	component = devm_kcalloc(dev, num, sizeof(*component), GFP_KERNEL);
3692 	if (!component)
3693 		return -ENOMEM;
3694 
3695 	*ret_component	= component;
3696 	*ret_num	= num;
3697 
3698 	return 0;
3699 }
3700 
3701 /*
3702  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3703  * @dai_link: DAI link
3704  *
3705  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3706  */
snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link * dai_link)3707 void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3708 {
3709 	struct snd_soc_dai_link_component *component;
3710 	int index;
3711 
3712 	for_each_link_codecs(dai_link, index, component)
3713 		__snd_soc_of_put_component(component);
3714 }
3715 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3716 
3717 /*
3718  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3719  * @dev: Card device
3720  * @of_node: Device node
3721  * @dai_link: DAI link
3722  *
3723  * Builds an array of CODEC DAI components from the DAI link property
3724  * 'sound-dai'.
3725  * The array is set in the DAI link and the number of DAIs is set accordingly.
3726  * The device nodes in the array (of_node) must be dereferenced by calling
3727  * snd_soc_of_put_dai_link_codecs() on @dai_link.
3728  *
3729  * Returns 0 for success
3730  */
snd_soc_of_get_dai_link_codecs(struct device * dev,struct device_node * of_node,struct snd_soc_dai_link * dai_link)3731 int snd_soc_of_get_dai_link_codecs(struct device *dev,
3732 				   struct device_node *of_node,
3733 				   struct snd_soc_dai_link *dai_link)
3734 {
3735 	struct snd_soc_dai_link_component *component;
3736 	int index, ret;
3737 
3738 	ret = __snd_soc_of_get_dai_link_component_alloc(dev, of_node,
3739 					 &dai_link->codecs, &dai_link->num_codecs);
3740 	if (ret < 0)
3741 		return ret;
3742 
3743 	/* Parse the list */
3744 	for_each_link_codecs(dai_link, index, component) {
3745 		ret = snd_soc_of_get_dlc(of_node, NULL, component, index);
3746 		if (ret)
3747 			goto err;
3748 	}
3749 	return 0;
3750 err:
3751 	snd_soc_of_put_dai_link_codecs(dai_link);
3752 	dai_link->codecs = NULL;
3753 	dai_link->num_codecs = 0;
3754 	return ret;
3755 }
3756 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3757 
3758 /*
3759  * snd_soc_of_put_dai_link_cpus - Dereference device nodes in the codecs array
3760  * @dai_link: DAI link
3761  *
3762  * Dereference device nodes acquired by snd_soc_of_get_dai_link_cpus().
3763  */
snd_soc_of_put_dai_link_cpus(struct snd_soc_dai_link * dai_link)3764 void snd_soc_of_put_dai_link_cpus(struct snd_soc_dai_link *dai_link)
3765 {
3766 	struct snd_soc_dai_link_component *component;
3767 	int index;
3768 
3769 	for_each_link_cpus(dai_link, index, component)
3770 		__snd_soc_of_put_component(component);
3771 }
3772 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_cpus);
3773 
3774 /*
3775  * snd_soc_of_get_dai_link_cpus - Parse a list of CPU DAIs in the devicetree
3776  * @dev: Card device
3777  * @of_node: Device node
3778  * @dai_link: DAI link
3779  *
3780  * Is analogous to snd_soc_of_get_dai_link_codecs but parses a list of CPU DAIs
3781  * instead.
3782  *
3783  * Returns 0 for success
3784  */
snd_soc_of_get_dai_link_cpus(struct device * dev,struct device_node * of_node,struct snd_soc_dai_link * dai_link)3785 int snd_soc_of_get_dai_link_cpus(struct device *dev,
3786 				 struct device_node *of_node,
3787 				 struct snd_soc_dai_link *dai_link)
3788 {
3789 	struct snd_soc_dai_link_component *component;
3790 	int index, ret;
3791 
3792 	/* Count the number of CPUs */
3793 	ret = __snd_soc_of_get_dai_link_component_alloc(dev, of_node,
3794 					 &dai_link->cpus, &dai_link->num_cpus);
3795 	if (ret < 0)
3796 		return ret;
3797 
3798 	/* Parse the list */
3799 	for_each_link_cpus(dai_link, index, component) {
3800 		ret = snd_soc_of_get_dlc(of_node, NULL, component, index);
3801 		if (ret)
3802 			goto err;
3803 	}
3804 	return 0;
3805 err:
3806 	snd_soc_of_put_dai_link_cpus(dai_link);
3807 	dai_link->cpus = NULL;
3808 	dai_link->num_cpus = 0;
3809 	return ret;
3810 }
3811 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_cpus);
3812 
snd_soc_init(void)3813 static int __init snd_soc_init(void)
3814 {
3815 	int ret;
3816 
3817 	snd_soc_debugfs_init();
3818 	ret = snd_soc_util_init();
3819 	if (ret)
3820 		goto err_util_init;
3821 
3822 	ret = platform_driver_register(&soc_driver);
3823 	if (ret)
3824 		goto err_register;
3825 	return 0;
3826 
3827 err_register:
3828 	snd_soc_util_exit();
3829 err_util_init:
3830 	snd_soc_debugfs_exit();
3831 	return ret;
3832 }
3833 module_init(snd_soc_init);
3834 
snd_soc_exit(void)3835 static void __exit snd_soc_exit(void)
3836 {
3837 	snd_soc_util_exit();
3838 	snd_soc_debugfs_exit();
3839 
3840 	platform_driver_unregister(&soc_driver);
3841 }
3842 module_exit(snd_soc_exit);
3843 
3844 /* Module information */
3845 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3846 MODULE_DESCRIPTION("ALSA SoC Core");
3847 MODULE_LICENSE("GPL");
3848 MODULE_ALIAS("platform:soc-audio");
3849