xref: /openbmc/linux/sound/soc/soc-dapm.c (revision 8dd99871)
1 /*
2  * soc-dapm.c  --  ALSA SoC Dynamic Audio Power Management
3  *
4  * Copyright 2005 Wolfson Microelectronics PLC.
5  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
6  *
7  *  This program is free software; you can redistribute  it and/or modify it
8  *  under  the terms of  the GNU General  Public License as published by the
9  *  Free Software Foundation;  either version 2 of the  License, or (at your
10  *  option) any later version.
11  *
12  *  Features:
13  *    o Changes power status of internal codec blocks depending on the
14  *      dynamic configuration of codec internal audio paths and active
15  *      DACs/ADCs.
16  *    o Platform power domain - can support external components i.e. amps and
17  *      mic/headphone insertion events.
18  *    o Automatic Mic Bias support
19  *    o Jack insertion power event initiation - e.g. hp insertion will enable
20  *      sinks, dacs, etc
21  *    o Delayed power down of audio subsystem to reduce pops between a quick
22  *      device reopen.
23  *
24  */
25 
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
29 #include <linux/async.h>
30 #include <linux/delay.h>
31 #include <linux/pm.h>
32 #include <linux/bitops.h>
33 #include <linux/platform_device.h>
34 #include <linux/jiffies.h>
35 #include <linux/debugfs.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/pinctrl/consumer.h>
39 #include <linux/clk.h>
40 #include <linux/slab.h>
41 #include <sound/core.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/soc.h>
45 #include <sound/initval.h>
46 
47 #include <trace/events/asoc.h>
48 
49 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
50 
51 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
52 	SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
53 
54 #define snd_soc_dapm_for_each_direction(dir) \
55 	for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
56 		(dir)++)
57 
58 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
59 	struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
60 	const char *control,
61 	int (*connected)(struct snd_soc_dapm_widget *source,
62 			 struct snd_soc_dapm_widget *sink));
63 
64 struct snd_soc_dapm_widget *
65 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
66 			 const struct snd_soc_dapm_widget *widget);
67 
68 struct snd_soc_dapm_widget *
69 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
70 			 const struct snd_soc_dapm_widget *widget);
71 
72 /* dapm power sequences - make this per codec in the future */
73 static int dapm_up_seq[] = {
74 	[snd_soc_dapm_pre] = 0,
75 	[snd_soc_dapm_regulator_supply] = 1,
76 	[snd_soc_dapm_pinctrl] = 1,
77 	[snd_soc_dapm_clock_supply] = 1,
78 	[snd_soc_dapm_supply] = 2,
79 	[snd_soc_dapm_micbias] = 3,
80 	[snd_soc_dapm_dai_link] = 2,
81 	[snd_soc_dapm_dai_in] = 4,
82 	[snd_soc_dapm_dai_out] = 4,
83 	[snd_soc_dapm_aif_in] = 4,
84 	[snd_soc_dapm_aif_out] = 4,
85 	[snd_soc_dapm_mic] = 5,
86 	[snd_soc_dapm_mux] = 6,
87 	[snd_soc_dapm_demux] = 6,
88 	[snd_soc_dapm_dac] = 7,
89 	[snd_soc_dapm_switch] = 8,
90 	[snd_soc_dapm_mixer] = 8,
91 	[snd_soc_dapm_mixer_named_ctl] = 8,
92 	[snd_soc_dapm_pga] = 9,
93 	[snd_soc_dapm_adc] = 10,
94 	[snd_soc_dapm_out_drv] = 11,
95 	[snd_soc_dapm_hp] = 11,
96 	[snd_soc_dapm_spk] = 11,
97 	[snd_soc_dapm_line] = 11,
98 	[snd_soc_dapm_kcontrol] = 12,
99 	[snd_soc_dapm_post] = 13,
100 };
101 
102 static int dapm_down_seq[] = {
103 	[snd_soc_dapm_pre] = 0,
104 	[snd_soc_dapm_kcontrol] = 1,
105 	[snd_soc_dapm_adc] = 2,
106 	[snd_soc_dapm_hp] = 3,
107 	[snd_soc_dapm_spk] = 3,
108 	[snd_soc_dapm_line] = 3,
109 	[snd_soc_dapm_out_drv] = 3,
110 	[snd_soc_dapm_pga] = 4,
111 	[snd_soc_dapm_switch] = 5,
112 	[snd_soc_dapm_mixer_named_ctl] = 5,
113 	[snd_soc_dapm_mixer] = 5,
114 	[snd_soc_dapm_dac] = 6,
115 	[snd_soc_dapm_mic] = 7,
116 	[snd_soc_dapm_micbias] = 8,
117 	[snd_soc_dapm_mux] = 9,
118 	[snd_soc_dapm_demux] = 9,
119 	[snd_soc_dapm_aif_in] = 10,
120 	[snd_soc_dapm_aif_out] = 10,
121 	[snd_soc_dapm_dai_in] = 10,
122 	[snd_soc_dapm_dai_out] = 10,
123 	[snd_soc_dapm_dai_link] = 11,
124 	[snd_soc_dapm_supply] = 12,
125 	[snd_soc_dapm_clock_supply] = 13,
126 	[snd_soc_dapm_pinctrl] = 13,
127 	[snd_soc_dapm_regulator_supply] = 13,
128 	[snd_soc_dapm_post] = 14,
129 };
130 
131 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
132 {
133 	if (dapm->card && dapm->card->instantiated)
134 		lockdep_assert_held(&dapm->card->dapm_mutex);
135 }
136 
137 static void pop_wait(u32 pop_time)
138 {
139 	if (pop_time)
140 		schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
141 }
142 
143 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
144 {
145 	va_list args;
146 	char *buf;
147 
148 	if (!pop_time)
149 		return;
150 
151 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
152 	if (buf == NULL)
153 		return;
154 
155 	va_start(args, fmt);
156 	vsnprintf(buf, PAGE_SIZE, fmt, args);
157 	dev_info(dev, "%s", buf);
158 	va_end(args);
159 
160 	kfree(buf);
161 }
162 
163 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
164 {
165 	return !list_empty(&w->dirty);
166 }
167 
168 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
169 {
170 	dapm_assert_locked(w->dapm);
171 
172 	if (!dapm_dirty_widget(w)) {
173 		dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
174 			 w->name, reason);
175 		list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
176 	}
177 }
178 
179 /*
180  * Common implementation for dapm_widget_invalidate_input_paths() and
181  * dapm_widget_invalidate_output_paths(). The function is inlined since the
182  * combined size of the two specialized functions is only marginally larger then
183  * the size of the generic function and at the same time the fast path of the
184  * specialized functions is significantly smaller than the generic function.
185  */
186 static __always_inline void dapm_widget_invalidate_paths(
187 	struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
188 {
189 	enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
190 	struct snd_soc_dapm_widget *node;
191 	struct snd_soc_dapm_path *p;
192 	LIST_HEAD(list);
193 
194 	dapm_assert_locked(w->dapm);
195 
196 	if (w->endpoints[dir] == -1)
197 		return;
198 
199 	list_add_tail(&w->work_list, &list);
200 	w->endpoints[dir] = -1;
201 
202 	list_for_each_entry(w, &list, work_list) {
203 		snd_soc_dapm_widget_for_each_path(w, dir, p) {
204 			if (p->is_supply || p->weak || !p->connect)
205 				continue;
206 			node = p->node[rdir];
207 			if (node->endpoints[dir] != -1) {
208 				node->endpoints[dir] = -1;
209 				list_add_tail(&node->work_list, &list);
210 			}
211 		}
212 	}
213 }
214 
215 /*
216  * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
217  *  input paths
218  * @w: The widget for which to invalidate the cached number of input paths
219  *
220  * Resets the cached number of inputs for the specified widget and all widgets
221  * that can be reached via outcoming paths from the widget.
222  *
223  * This function must be called if the number of output paths for a widget might
224  * have changed. E.g. if the source state of a widget changes or a path is added
225  * or activated with the widget as the sink.
226  */
227 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
228 {
229 	dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
230 }
231 
232 /*
233  * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
234  *  output paths
235  * @w: The widget for which to invalidate the cached number of output paths
236  *
237  * Resets the cached number of outputs for the specified widget and all widgets
238  * that can be reached via incoming paths from the widget.
239  *
240  * This function must be called if the number of output paths for a widget might
241  * have changed. E.g. if the sink state of a widget changes or a path is added
242  * or activated with the widget as the source.
243  */
244 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
245 {
246 	dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
247 }
248 
249 /*
250  * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
251  *  for the widgets connected to a path
252  * @p: The path to invalidate
253  *
254  * Resets the cached number of inputs for the sink of the path and the cached
255  * number of outputs for the source of the path.
256  *
257  * This function must be called when a path is added, removed or the connected
258  * state changes.
259  */
260 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
261 {
262 	/*
263 	 * Weak paths or supply paths do not influence the number of input or
264 	 * output paths of their neighbors.
265 	 */
266 	if (p->weak || p->is_supply)
267 		return;
268 
269 	/*
270 	 * The number of connected endpoints is the sum of the number of
271 	 * connected endpoints of all neighbors. If a node with 0 connected
272 	 * endpoints is either connected or disconnected that sum won't change,
273 	 * so there is no need to re-check the path.
274 	 */
275 	if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
276 		dapm_widget_invalidate_input_paths(p->sink);
277 	if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
278 		dapm_widget_invalidate_output_paths(p->source);
279 }
280 
281 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
282 {
283 	struct snd_soc_dapm_widget *w;
284 
285 	mutex_lock(&card->dapm_mutex);
286 
287 	list_for_each_entry(w, &card->widgets, list) {
288 		if (w->is_ep) {
289 			dapm_mark_dirty(w, "Rechecking endpoints");
290 			if (w->is_ep & SND_SOC_DAPM_EP_SINK)
291 				dapm_widget_invalidate_output_paths(w);
292 			if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
293 				dapm_widget_invalidate_input_paths(w);
294 		}
295 	}
296 
297 	mutex_unlock(&card->dapm_mutex);
298 }
299 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
300 
301 /* create a new dapm widget */
302 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
303 	const struct snd_soc_dapm_widget *_widget)
304 {
305 	return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
306 }
307 
308 struct dapm_kcontrol_data {
309 	unsigned int value;
310 	struct snd_soc_dapm_widget *widget;
311 	struct list_head paths;
312 	struct snd_soc_dapm_widget_list *wlist;
313 };
314 
315 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
316 	struct snd_kcontrol *kcontrol, const char *ctrl_name)
317 {
318 	struct dapm_kcontrol_data *data;
319 	struct soc_mixer_control *mc;
320 	struct soc_enum *e;
321 	const char *name;
322 	int ret;
323 
324 	data = kzalloc(sizeof(*data), GFP_KERNEL);
325 	if (!data)
326 		return -ENOMEM;
327 
328 	INIT_LIST_HEAD(&data->paths);
329 
330 	switch (widget->id) {
331 	case snd_soc_dapm_switch:
332 	case snd_soc_dapm_mixer:
333 	case snd_soc_dapm_mixer_named_ctl:
334 		mc = (struct soc_mixer_control *)kcontrol->private_value;
335 
336 		if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
337 			dev_warn(widget->dapm->dev,
338 				 "ASoC: Unsupported stereo autodisable control '%s'\n",
339 				 ctrl_name);
340 
341 		if (mc->autodisable) {
342 			struct snd_soc_dapm_widget template;
343 
344 			name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
345 					 "Autodisable");
346 			if (!name) {
347 				ret = -ENOMEM;
348 				goto err_data;
349 			}
350 
351 			memset(&template, 0, sizeof(template));
352 			template.reg = mc->reg;
353 			template.mask = (1 << fls(mc->max)) - 1;
354 			template.shift = mc->shift;
355 			if (mc->invert)
356 				template.off_val = mc->max;
357 			else
358 				template.off_val = 0;
359 			template.on_val = template.off_val;
360 			template.id = snd_soc_dapm_kcontrol;
361 			template.name = name;
362 
363 			data->value = template.on_val;
364 
365 			data->widget =
366 				snd_soc_dapm_new_control_unlocked(widget->dapm,
367 				&template);
368 			kfree(name);
369 			if (IS_ERR(data->widget)) {
370 				ret = PTR_ERR(data->widget);
371 				goto err_data;
372 			}
373 			if (!data->widget) {
374 				ret = -ENOMEM;
375 				goto err_data;
376 			}
377 		}
378 		break;
379 	case snd_soc_dapm_demux:
380 	case snd_soc_dapm_mux:
381 		e = (struct soc_enum *)kcontrol->private_value;
382 
383 		if (e->autodisable) {
384 			struct snd_soc_dapm_widget template;
385 
386 			name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
387 					 "Autodisable");
388 			if (!name) {
389 				ret = -ENOMEM;
390 				goto err_data;
391 			}
392 
393 			memset(&template, 0, sizeof(template));
394 			template.reg = e->reg;
395 			template.mask = e->mask << e->shift_l;
396 			template.shift = e->shift_l;
397 			template.off_val = snd_soc_enum_item_to_val(e, 0);
398 			template.on_val = template.off_val;
399 			template.id = snd_soc_dapm_kcontrol;
400 			template.name = name;
401 
402 			data->value = template.on_val;
403 
404 			data->widget = snd_soc_dapm_new_control_unlocked(
405 						widget->dapm, &template);
406 			kfree(name);
407 			if (IS_ERR(data->widget)) {
408 				ret = PTR_ERR(data->widget);
409 				goto err_data;
410 			}
411 			if (!data->widget) {
412 				ret = -ENOMEM;
413 				goto err_data;
414 			}
415 
416 			snd_soc_dapm_add_path(widget->dapm, data->widget,
417 					      widget, NULL, NULL);
418 		}
419 		break;
420 	default:
421 		break;
422 	}
423 
424 	kcontrol->private_data = data;
425 
426 	return 0;
427 
428 err_data:
429 	kfree(data);
430 	return ret;
431 }
432 
433 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
434 {
435 	struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
436 
437 	list_del(&data->paths);
438 	kfree(data->wlist);
439 	kfree(data);
440 }
441 
442 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
443 	const struct snd_kcontrol *kcontrol)
444 {
445 	struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
446 
447 	return data->wlist;
448 }
449 
450 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
451 	struct snd_soc_dapm_widget *widget)
452 {
453 	struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
454 	struct snd_soc_dapm_widget_list *new_wlist;
455 	unsigned int n;
456 
457 	if (data->wlist)
458 		n = data->wlist->num_widgets + 1;
459 	else
460 		n = 1;
461 
462 	new_wlist = krealloc(data->wlist,
463 			sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
464 	if (!new_wlist)
465 		return -ENOMEM;
466 
467 	new_wlist->widgets[n - 1] = widget;
468 	new_wlist->num_widgets = n;
469 
470 	data->wlist = new_wlist;
471 
472 	return 0;
473 }
474 
475 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
476 	struct snd_soc_dapm_path *path)
477 {
478 	struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
479 
480 	list_add_tail(&path->list_kcontrol, &data->paths);
481 }
482 
483 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
484 {
485 	struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
486 
487 	if (!data->widget)
488 		return true;
489 
490 	return data->widget->power;
491 }
492 
493 static struct list_head *dapm_kcontrol_get_path_list(
494 	const struct snd_kcontrol *kcontrol)
495 {
496 	struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
497 
498 	return &data->paths;
499 }
500 
501 #define dapm_kcontrol_for_each_path(path, kcontrol) \
502 	list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
503 		list_kcontrol)
504 
505 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
506 {
507 	struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
508 
509 	return data->value;
510 }
511 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
512 
513 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
514 	unsigned int value)
515 {
516 	struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
517 
518 	if (data->value == value)
519 		return false;
520 
521 	if (data->widget)
522 		data->widget->on_val = value;
523 
524 	data->value = value;
525 
526 	return true;
527 }
528 
529 /**
530  * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
531  *   kcontrol
532  * @kcontrol: The kcontrol
533  */
534 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
535 				struct snd_kcontrol *kcontrol)
536 {
537 	return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
538 }
539 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
540 
541 /**
542  * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
543  *  kcontrol
544  * @kcontrol: The kcontrol
545  *
546  * Note: This function must only be used on kcontrols that are known to have
547  * been registered for a CODEC. Otherwise the behaviour is undefined.
548  */
549 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
550 	struct snd_kcontrol *kcontrol)
551 {
552 	return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
553 }
554 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
555 
556 static void dapm_reset(struct snd_soc_card *card)
557 {
558 	struct snd_soc_dapm_widget *w;
559 
560 	lockdep_assert_held(&card->dapm_mutex);
561 
562 	memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
563 
564 	list_for_each_entry(w, &card->widgets, list) {
565 		w->new_power = w->power;
566 		w->power_checked = false;
567 	}
568 }
569 
570 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
571 {
572 	if (!dapm->component)
573 		return NULL;
574 	return dapm->component->name_prefix;
575 }
576 
577 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
578 	unsigned int *value)
579 {
580 	if (!dapm->component)
581 		return -EIO;
582 	return snd_soc_component_read(dapm->component, reg, value);
583 }
584 
585 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
586 	int reg, unsigned int mask, unsigned int value)
587 {
588 	if (!dapm->component)
589 		return -EIO;
590 	return snd_soc_component_update_bits(dapm->component, reg,
591 					     mask, value);
592 }
593 
594 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
595 	int reg, unsigned int mask, unsigned int value)
596 {
597 	if (!dapm->component)
598 		return -EIO;
599 	return snd_soc_component_test_bits(dapm->component, reg, mask, value);
600 }
601 
602 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
603 {
604 	if (dapm->component)
605 		snd_soc_component_async_complete(dapm->component);
606 }
607 
608 static struct snd_soc_dapm_widget *
609 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
610 {
611 	struct snd_soc_dapm_widget *w = wcache->widget;
612 	struct list_head *wlist;
613 	const int depth = 2;
614 	int i = 0;
615 
616 	if (w) {
617 		wlist = &w->dapm->card->widgets;
618 
619 		list_for_each_entry_from(w, wlist, list) {
620 			if (!strcmp(name, w->name))
621 				return w;
622 
623 			if (++i == depth)
624 				break;
625 		}
626 	}
627 
628 	return NULL;
629 }
630 
631 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
632 				      struct snd_soc_dapm_widget *w)
633 {
634 	wcache->widget = w;
635 }
636 
637 /**
638  * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
639  * @dapm: The DAPM context for which to set the level
640  * @level: The level to set
641  *
642  * Forces the DAPM bias level to a specific state. It will call the bias level
643  * callback of DAPM context with the specified level. This will even happen if
644  * the context is already at the same level. Furthermore it will not go through
645  * the normal bias level sequencing, meaning any intermediate states between the
646  * current and the target state will not be entered.
647  *
648  * Note that the change in bias level is only temporary and the next time
649  * snd_soc_dapm_sync() is called the state will be set to the level as
650  * determined by the DAPM core. The function is mainly intended to be used to
651  * used during probe or resume from suspend to power up the device so
652  * initialization can be done, before the DAPM core takes over.
653  */
654 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
655 	enum snd_soc_bias_level level)
656 {
657 	int ret = 0;
658 
659 	if (dapm->set_bias_level)
660 		ret = dapm->set_bias_level(dapm, level);
661 
662 	if (ret == 0)
663 		dapm->bias_level = level;
664 
665 	return ret;
666 }
667 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
668 
669 /**
670  * snd_soc_dapm_set_bias_level - set the bias level for the system
671  * @dapm: DAPM context
672  * @level: level to configure
673  *
674  * Configure the bias (power) levels for the SoC audio device.
675  *
676  * Returns 0 for success else error.
677  */
678 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
679 				       enum snd_soc_bias_level level)
680 {
681 	struct snd_soc_card *card = dapm->card;
682 	int ret = 0;
683 
684 	trace_snd_soc_bias_level_start(card, level);
685 
686 	if (card && card->set_bias_level)
687 		ret = card->set_bias_level(card, dapm, level);
688 	if (ret != 0)
689 		goto out;
690 
691 	if (!card || dapm != &card->dapm)
692 		ret = snd_soc_dapm_force_bias_level(dapm, level);
693 
694 	if (ret != 0)
695 		goto out;
696 
697 	if (card && card->set_bias_level_post)
698 		ret = card->set_bias_level_post(card, dapm, level);
699 out:
700 	trace_snd_soc_bias_level_done(card, level);
701 
702 	return ret;
703 }
704 
705 /* connect mux widget to its interconnecting audio paths */
706 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
707 	struct snd_soc_dapm_path *path, const char *control_name,
708 	struct snd_soc_dapm_widget *w)
709 {
710 	const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
711 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
712 	unsigned int val, item;
713 	int i;
714 
715 	if (e->reg != SND_SOC_NOPM) {
716 		soc_dapm_read(dapm, e->reg, &val);
717 		val = (val >> e->shift_l) & e->mask;
718 		item = snd_soc_enum_val_to_item(e, val);
719 	} else {
720 		/* since a virtual mux has no backing registers to
721 		 * decide which path to connect, it will try to match
722 		 * with the first enumeration.  This is to ensure
723 		 * that the default mux choice (the first) will be
724 		 * correctly powered up during initialization.
725 		 */
726 		item = 0;
727 	}
728 
729 	i = match_string(e->texts, e->items, control_name);
730 	if (i < 0)
731 		return -ENODEV;
732 
733 	path->name = e->texts[i];
734 	path->connect = (i == item);
735 	return 0;
736 
737 }
738 
739 /* set up initial codec paths */
740 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
741 				       int nth_path)
742 {
743 	struct soc_mixer_control *mc = (struct soc_mixer_control *)
744 		p->sink->kcontrol_news[i].private_value;
745 	unsigned int reg = mc->reg;
746 	unsigned int shift = mc->shift;
747 	unsigned int max = mc->max;
748 	unsigned int mask = (1 << fls(max)) - 1;
749 	unsigned int invert = mc->invert;
750 	unsigned int val;
751 
752 	if (reg != SND_SOC_NOPM) {
753 		soc_dapm_read(p->sink->dapm, reg, &val);
754 		/*
755 		 * The nth_path argument allows this function to know
756 		 * which path of a kcontrol it is setting the initial
757 		 * status for. Ideally this would support any number
758 		 * of paths and channels. But since kcontrols only come
759 		 * in mono and stereo variants, we are limited to 2
760 		 * channels.
761 		 *
762 		 * The following code assumes for stereo controls the
763 		 * first path is the left channel, and all remaining
764 		 * paths are the right channel.
765 		 */
766 		if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
767 			if (reg != mc->rreg)
768 				soc_dapm_read(p->sink->dapm, mc->rreg, &val);
769 			val = (val >> mc->rshift) & mask;
770 		} else {
771 			val = (val >> shift) & mask;
772 		}
773 		if (invert)
774 			val = max - val;
775 		p->connect = !!val;
776 	} else {
777 		p->connect = 0;
778 	}
779 }
780 
781 /* connect mixer widget to its interconnecting audio paths */
782 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
783 	struct snd_soc_dapm_path *path, const char *control_name)
784 {
785 	int i, nth_path = 0;
786 
787 	/* search for mixer kcontrol */
788 	for (i = 0; i < path->sink->num_kcontrols; i++) {
789 		if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
790 			path->name = path->sink->kcontrol_news[i].name;
791 			dapm_set_mixer_path_status(path, i, nth_path++);
792 			return 0;
793 		}
794 	}
795 	return -ENODEV;
796 }
797 
798 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
799 	struct snd_soc_dapm_widget *kcontrolw,
800 	const struct snd_kcontrol_new *kcontrol_new,
801 	struct snd_kcontrol **kcontrol)
802 {
803 	struct snd_soc_dapm_widget *w;
804 	int i;
805 
806 	*kcontrol = NULL;
807 
808 	list_for_each_entry(w, &dapm->card->widgets, list) {
809 		if (w == kcontrolw || w->dapm != kcontrolw->dapm)
810 			continue;
811 		for (i = 0; i < w->num_kcontrols; i++) {
812 			if (&w->kcontrol_news[i] == kcontrol_new) {
813 				if (w->kcontrols)
814 					*kcontrol = w->kcontrols[i];
815 				return 1;
816 			}
817 		}
818 	}
819 
820 	return 0;
821 }
822 
823 /*
824  * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
825  * create it. Either way, add the widget into the control's widget list
826  */
827 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
828 	int kci)
829 {
830 	struct snd_soc_dapm_context *dapm = w->dapm;
831 	struct snd_card *card = dapm->card->snd_card;
832 	const char *prefix;
833 	size_t prefix_len;
834 	int shared;
835 	struct snd_kcontrol *kcontrol;
836 	bool wname_in_long_name, kcname_in_long_name;
837 	char *long_name = NULL;
838 	const char *name;
839 	int ret = 0;
840 
841 	prefix = soc_dapm_prefix(dapm);
842 	if (prefix)
843 		prefix_len = strlen(prefix) + 1;
844 	else
845 		prefix_len = 0;
846 
847 	shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
848 					 &kcontrol);
849 
850 	if (!kcontrol) {
851 		if (shared) {
852 			wname_in_long_name = false;
853 			kcname_in_long_name = true;
854 		} else {
855 			switch (w->id) {
856 			case snd_soc_dapm_switch:
857 			case snd_soc_dapm_mixer:
858 			case snd_soc_dapm_pga:
859 			case snd_soc_dapm_out_drv:
860 				wname_in_long_name = true;
861 				kcname_in_long_name = true;
862 				break;
863 			case snd_soc_dapm_mixer_named_ctl:
864 				wname_in_long_name = false;
865 				kcname_in_long_name = true;
866 				break;
867 			case snd_soc_dapm_demux:
868 			case snd_soc_dapm_mux:
869 				wname_in_long_name = true;
870 				kcname_in_long_name = false;
871 				break;
872 			default:
873 				return -EINVAL;
874 			}
875 		}
876 
877 		if (wname_in_long_name && kcname_in_long_name) {
878 			/*
879 			 * The control will get a prefix from the control
880 			 * creation process but we're also using the same
881 			 * prefix for widgets so cut the prefix off the
882 			 * front of the widget name.
883 			 */
884 			long_name = kasprintf(GFP_KERNEL, "%s %s",
885 				 w->name + prefix_len,
886 				 w->kcontrol_news[kci].name);
887 			if (long_name == NULL)
888 				return -ENOMEM;
889 
890 			name = long_name;
891 		} else if (wname_in_long_name) {
892 			long_name = NULL;
893 			name = w->name + prefix_len;
894 		} else {
895 			long_name = NULL;
896 			name = w->kcontrol_news[kci].name;
897 		}
898 
899 		kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
900 					prefix);
901 		if (!kcontrol) {
902 			ret = -ENOMEM;
903 			goto exit_free;
904 		}
905 
906 		kcontrol->private_free = dapm_kcontrol_free;
907 
908 		ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
909 		if (ret) {
910 			snd_ctl_free_one(kcontrol);
911 			goto exit_free;
912 		}
913 
914 		ret = snd_ctl_add(card, kcontrol);
915 		if (ret < 0) {
916 			dev_err(dapm->dev,
917 				"ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
918 				w->name, name, ret);
919 			goto exit_free;
920 		}
921 	}
922 
923 	ret = dapm_kcontrol_add_widget(kcontrol, w);
924 	if (ret == 0)
925 		w->kcontrols[kci] = kcontrol;
926 
927 exit_free:
928 	kfree(long_name);
929 
930 	return ret;
931 }
932 
933 /* create new dapm mixer control */
934 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
935 {
936 	int i, ret;
937 	struct snd_soc_dapm_path *path;
938 	struct dapm_kcontrol_data *data;
939 
940 	/* add kcontrol */
941 	for (i = 0; i < w->num_kcontrols; i++) {
942 		/* match name */
943 		snd_soc_dapm_widget_for_each_source_path(w, path) {
944 			/* mixer/mux paths name must match control name */
945 			if (path->name != (char *)w->kcontrol_news[i].name)
946 				continue;
947 
948 			if (!w->kcontrols[i]) {
949 				ret = dapm_create_or_share_kcontrol(w, i);
950 				if (ret < 0)
951 					return ret;
952 			}
953 
954 			dapm_kcontrol_add_path(w->kcontrols[i], path);
955 
956 			data = snd_kcontrol_chip(w->kcontrols[i]);
957 			if (data->widget)
958 				snd_soc_dapm_add_path(data->widget->dapm,
959 						      data->widget,
960 						      path->source,
961 						      NULL, NULL);
962 		}
963 	}
964 
965 	return 0;
966 }
967 
968 /* create new dapm mux control */
969 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
970 {
971 	struct snd_soc_dapm_context *dapm = w->dapm;
972 	enum snd_soc_dapm_direction dir;
973 	struct snd_soc_dapm_path *path;
974 	const char *type;
975 	int ret;
976 
977 	switch (w->id) {
978 	case snd_soc_dapm_mux:
979 		dir = SND_SOC_DAPM_DIR_OUT;
980 		type = "mux";
981 		break;
982 	case snd_soc_dapm_demux:
983 		dir = SND_SOC_DAPM_DIR_IN;
984 		type = "demux";
985 		break;
986 	default:
987 		return -EINVAL;
988 	}
989 
990 	if (w->num_kcontrols != 1) {
991 		dev_err(dapm->dev,
992 			"ASoC: %s %s has incorrect number of controls\n", type,
993 			w->name);
994 		return -EINVAL;
995 	}
996 
997 	if (list_empty(&w->edges[dir])) {
998 		dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
999 		return -EINVAL;
1000 	}
1001 
1002 	ret = dapm_create_or_share_kcontrol(w, 0);
1003 	if (ret < 0)
1004 		return ret;
1005 
1006 	snd_soc_dapm_widget_for_each_path(w, dir, path) {
1007 		if (path->name)
1008 			dapm_kcontrol_add_path(w->kcontrols[0], path);
1009 	}
1010 
1011 	return 0;
1012 }
1013 
1014 /* create new dapm volume control */
1015 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1016 {
1017 	int i, ret;
1018 
1019 	for (i = 0; i < w->num_kcontrols; i++) {
1020 		ret = dapm_create_or_share_kcontrol(w, i);
1021 		if (ret < 0)
1022 			return ret;
1023 	}
1024 
1025 	return 0;
1026 }
1027 
1028 /* create new dapm dai link control */
1029 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1030 {
1031 	int i, ret;
1032 	struct snd_kcontrol *kcontrol;
1033 	struct snd_soc_dapm_context *dapm = w->dapm;
1034 	struct snd_card *card = dapm->card->snd_card;
1035 
1036 	/* create control for links with > 1 config */
1037 	if (w->num_params <= 1)
1038 		return 0;
1039 
1040 	/* add kcontrol */
1041 	for (i = 0; i < w->num_kcontrols; i++) {
1042 		kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1043 					w->name, NULL);
1044 		ret = snd_ctl_add(card, kcontrol);
1045 		if (ret < 0) {
1046 			dev_err(dapm->dev,
1047 				"ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1048 				w->name, w->kcontrol_news[i].name, ret);
1049 			return ret;
1050 		}
1051 		kcontrol->private_data = w;
1052 		w->kcontrols[i] = kcontrol;
1053 	}
1054 
1055 	return 0;
1056 }
1057 
1058 /* We implement power down on suspend by checking the power state of
1059  * the ALSA card - when we are suspending the ALSA state for the card
1060  * is set to D3.
1061  */
1062 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1063 {
1064 	int level = snd_power_get_state(widget->dapm->card->snd_card);
1065 
1066 	switch (level) {
1067 	case SNDRV_CTL_POWER_D3hot:
1068 	case SNDRV_CTL_POWER_D3cold:
1069 		if (widget->ignore_suspend)
1070 			dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1071 				widget->name);
1072 		return widget->ignore_suspend;
1073 	default:
1074 		return 1;
1075 	}
1076 }
1077 
1078 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1079 	struct list_head *widgets)
1080 {
1081 	struct snd_soc_dapm_widget *w;
1082 	struct list_head *it;
1083 	unsigned int size = 0;
1084 	unsigned int i = 0;
1085 
1086 	list_for_each(it, widgets)
1087 		size++;
1088 
1089 	*list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1090 	if (*list == NULL)
1091 		return -ENOMEM;
1092 
1093 	list_for_each_entry(w, widgets, work_list)
1094 		(*list)->widgets[i++] = w;
1095 
1096 	(*list)->num_widgets = i;
1097 
1098 	return 0;
1099 }
1100 
1101 /*
1102  * Common implementation for is_connected_output_ep() and
1103  * is_connected_input_ep(). The function is inlined since the combined size of
1104  * the two specialized functions is only marginally larger then the size of the
1105  * generic function and at the same time the fast path of the specialized
1106  * functions is significantly smaller than the generic function.
1107  */
1108 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1109 	struct list_head *list, enum snd_soc_dapm_direction dir,
1110 	int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1111 		  bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1112 						enum snd_soc_dapm_direction)),
1113 	bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1114 				      enum snd_soc_dapm_direction))
1115 {
1116 	enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1117 	struct snd_soc_dapm_path *path;
1118 	int con = 0;
1119 
1120 	if (widget->endpoints[dir] >= 0)
1121 		return widget->endpoints[dir];
1122 
1123 	DAPM_UPDATE_STAT(widget, path_checks);
1124 
1125 	/* do we need to add this widget to the list ? */
1126 	if (list)
1127 		list_add_tail(&widget->work_list, list);
1128 
1129 	if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1130 		widget->endpoints[dir] = 1;
1131 		return widget->endpoints[dir];
1132 	}
1133 
1134 	if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1135 		widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1136 		return widget->endpoints[dir];
1137 	}
1138 
1139 	snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1140 		DAPM_UPDATE_STAT(widget, neighbour_checks);
1141 
1142 		if (path->weak || path->is_supply)
1143 			continue;
1144 
1145 		if (path->walking)
1146 			return 1;
1147 
1148 		trace_snd_soc_dapm_path(widget, dir, path);
1149 
1150 		if (path->connect) {
1151 			path->walking = 1;
1152 			con += fn(path->node[dir], list, custom_stop_condition);
1153 			path->walking = 0;
1154 		}
1155 	}
1156 
1157 	widget->endpoints[dir] = con;
1158 
1159 	return con;
1160 }
1161 
1162 /*
1163  * Recursively check for a completed path to an active or physically connected
1164  * output widget. Returns number of complete paths.
1165  *
1166  * Optionally, can be supplied with a function acting as a stopping condition.
1167  * This function takes the dapm widget currently being examined and the walk
1168  * direction as an arguments, it should return true if the walk should be
1169  * stopped and false otherwise.
1170  */
1171 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1172 	struct list_head *list,
1173 	bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1174 				      enum snd_soc_dapm_direction))
1175 {
1176 	return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1177 			is_connected_output_ep, custom_stop_condition);
1178 }
1179 
1180 /*
1181  * Recursively check for a completed path to an active or physically connected
1182  * input widget. Returns number of complete paths.
1183  *
1184  * Optionally, can be supplied with a function acting as a stopping condition.
1185  * This function takes the dapm widget currently being examined and the walk
1186  * direction as an arguments, it should return true if the walk should be
1187  * stopped and false otherwise.
1188  */
1189 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1190 	struct list_head *list,
1191 	bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1192 				      enum snd_soc_dapm_direction))
1193 {
1194 	return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1195 			is_connected_input_ep, custom_stop_condition);
1196 }
1197 
1198 /**
1199  * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1200  * @dai: the soc DAI.
1201  * @stream: stream direction.
1202  * @list: list of active widgets for this stream.
1203  * @custom_stop_condition: (optional) a function meant to stop the widget graph
1204  *                         walk based on custom logic.
1205  *
1206  * Queries DAPM graph as to whether a valid audio stream path exists for
1207  * the initial stream specified by name. This takes into account
1208  * current mixer and mux kcontrol settings. Creates list of valid widgets.
1209  *
1210  * Optionally, can be supplied with a function acting as a stopping condition.
1211  * This function takes the dapm widget currently being examined and the walk
1212  * direction as an arguments, it should return true if the walk should be
1213  * stopped and false otherwise.
1214  *
1215  * Returns the number of valid paths or negative error.
1216  */
1217 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1218 	struct snd_soc_dapm_widget_list **list,
1219 	bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1220 				      enum snd_soc_dapm_direction))
1221 {
1222 	struct snd_soc_card *card = dai->component->card;
1223 	struct snd_soc_dapm_widget *w;
1224 	LIST_HEAD(widgets);
1225 	int paths;
1226 	int ret;
1227 
1228 	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1229 
1230 	/*
1231 	 * For is_connected_{output,input}_ep fully discover the graph we need
1232 	 * to reset the cached number of inputs and outputs.
1233 	 */
1234 	list_for_each_entry(w, &card->widgets, list) {
1235 		w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1236 		w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
1237 	}
1238 
1239 	if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1240 		paths = is_connected_output_ep(dai->playback_widget, &widgets,
1241 				custom_stop_condition);
1242 	else
1243 		paths = is_connected_input_ep(dai->capture_widget, &widgets,
1244 				custom_stop_condition);
1245 
1246 	/* Drop starting point */
1247 	list_del(widgets.next);
1248 
1249 	ret = dapm_widget_list_create(list, &widgets);
1250 	if (ret)
1251 		paths = ret;
1252 
1253 	trace_snd_soc_dapm_connected(paths, stream);
1254 	mutex_unlock(&card->dapm_mutex);
1255 
1256 	return paths;
1257 }
1258 
1259 /*
1260  * Handler for regulator supply widget.
1261  */
1262 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1263 		   struct snd_kcontrol *kcontrol, int event)
1264 {
1265 	int ret;
1266 
1267 	soc_dapm_async_complete(w->dapm);
1268 
1269 	if (SND_SOC_DAPM_EVENT_ON(event)) {
1270 		if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1271 			ret = regulator_allow_bypass(w->regulator, false);
1272 			if (ret != 0)
1273 				dev_warn(w->dapm->dev,
1274 					 "ASoC: Failed to unbypass %s: %d\n",
1275 					 w->name, ret);
1276 		}
1277 
1278 		return regulator_enable(w->regulator);
1279 	} else {
1280 		if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1281 			ret = regulator_allow_bypass(w->regulator, true);
1282 			if (ret != 0)
1283 				dev_warn(w->dapm->dev,
1284 					 "ASoC: Failed to bypass %s: %d\n",
1285 					 w->name, ret);
1286 		}
1287 
1288 		return regulator_disable_deferred(w->regulator, w->shift);
1289 	}
1290 }
1291 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1292 
1293 /*
1294  * Handler for pinctrl widget.
1295  */
1296 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1297 		       struct snd_kcontrol *kcontrol, int event)
1298 {
1299 	struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1300 	struct pinctrl *p = w->pinctrl;
1301 	struct pinctrl_state *s;
1302 
1303 	if (!p || !priv)
1304 		return -EIO;
1305 
1306 	if (SND_SOC_DAPM_EVENT_ON(event))
1307 		s = pinctrl_lookup_state(p, priv->active_state);
1308 	else
1309 		s = pinctrl_lookup_state(p, priv->sleep_state);
1310 
1311 	if (IS_ERR(s))
1312 		return PTR_ERR(s);
1313 
1314 	return pinctrl_select_state(p, s);
1315 }
1316 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1317 
1318 /*
1319  * Handler for clock supply widget.
1320  */
1321 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1322 		   struct snd_kcontrol *kcontrol, int event)
1323 {
1324 	if (!w->clk)
1325 		return -EIO;
1326 
1327 	soc_dapm_async_complete(w->dapm);
1328 
1329 #ifdef CONFIG_HAVE_CLK
1330 	if (SND_SOC_DAPM_EVENT_ON(event)) {
1331 		return clk_prepare_enable(w->clk);
1332 	} else {
1333 		clk_disable_unprepare(w->clk);
1334 		return 0;
1335 	}
1336 #endif
1337 	return 0;
1338 }
1339 EXPORT_SYMBOL_GPL(dapm_clock_event);
1340 
1341 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1342 {
1343 	if (w->power_checked)
1344 		return w->new_power;
1345 
1346 	if (w->force)
1347 		w->new_power = 1;
1348 	else
1349 		w->new_power = w->power_check(w);
1350 
1351 	w->power_checked = true;
1352 
1353 	return w->new_power;
1354 }
1355 
1356 /* Generic check to see if a widget should be powered. */
1357 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1358 {
1359 	int in, out;
1360 
1361 	DAPM_UPDATE_STAT(w, power_checks);
1362 
1363 	in = is_connected_input_ep(w, NULL, NULL);
1364 	out = is_connected_output_ep(w, NULL, NULL);
1365 	return out != 0 && in != 0;
1366 }
1367 
1368 /* Check to see if a power supply is needed */
1369 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1370 {
1371 	struct snd_soc_dapm_path *path;
1372 
1373 	DAPM_UPDATE_STAT(w, power_checks);
1374 
1375 	/* Check if one of our outputs is connected */
1376 	snd_soc_dapm_widget_for_each_sink_path(w, path) {
1377 		DAPM_UPDATE_STAT(w, neighbour_checks);
1378 
1379 		if (path->weak)
1380 			continue;
1381 
1382 		if (path->connected &&
1383 		    !path->connected(path->source, path->sink))
1384 			continue;
1385 
1386 		if (dapm_widget_power_check(path->sink))
1387 			return 1;
1388 	}
1389 
1390 	return 0;
1391 }
1392 
1393 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1394 {
1395 	return w->connected;
1396 }
1397 
1398 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1399 			    struct snd_soc_dapm_widget *b,
1400 			    bool power_up)
1401 {
1402 	int *sort;
1403 
1404 	if (power_up)
1405 		sort = dapm_up_seq;
1406 	else
1407 		sort = dapm_down_seq;
1408 
1409 	if (sort[a->id] != sort[b->id])
1410 		return sort[a->id] - sort[b->id];
1411 	if (a->subseq != b->subseq) {
1412 		if (power_up)
1413 			return a->subseq - b->subseq;
1414 		else
1415 			return b->subseq - a->subseq;
1416 	}
1417 	if (a->reg != b->reg)
1418 		return a->reg - b->reg;
1419 	if (a->dapm != b->dapm)
1420 		return (unsigned long)a->dapm - (unsigned long)b->dapm;
1421 
1422 	return 0;
1423 }
1424 
1425 /* Insert a widget in order into a DAPM power sequence. */
1426 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1427 			    struct list_head *list,
1428 			    bool power_up)
1429 {
1430 	struct snd_soc_dapm_widget *w;
1431 
1432 	list_for_each_entry(w, list, power_list)
1433 		if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1434 			list_add_tail(&new_widget->power_list, &w->power_list);
1435 			return;
1436 		}
1437 
1438 	list_add_tail(&new_widget->power_list, list);
1439 }
1440 
1441 static void dapm_seq_check_event(struct snd_soc_card *card,
1442 				 struct snd_soc_dapm_widget *w, int event)
1443 {
1444 	const char *ev_name;
1445 	int power, ret;
1446 
1447 	switch (event) {
1448 	case SND_SOC_DAPM_PRE_PMU:
1449 		ev_name = "PRE_PMU";
1450 		power = 1;
1451 		break;
1452 	case SND_SOC_DAPM_POST_PMU:
1453 		ev_name = "POST_PMU";
1454 		power = 1;
1455 		break;
1456 	case SND_SOC_DAPM_PRE_PMD:
1457 		ev_name = "PRE_PMD";
1458 		power = 0;
1459 		break;
1460 	case SND_SOC_DAPM_POST_PMD:
1461 		ev_name = "POST_PMD";
1462 		power = 0;
1463 		break;
1464 	case SND_SOC_DAPM_WILL_PMU:
1465 		ev_name = "WILL_PMU";
1466 		power = 1;
1467 		break;
1468 	case SND_SOC_DAPM_WILL_PMD:
1469 		ev_name = "WILL_PMD";
1470 		power = 0;
1471 		break;
1472 	default:
1473 		WARN(1, "Unknown event %d\n", event);
1474 		return;
1475 	}
1476 
1477 	if (w->new_power != power)
1478 		return;
1479 
1480 	if (w->event && (w->event_flags & event)) {
1481 		pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1482 			w->name, ev_name);
1483 		soc_dapm_async_complete(w->dapm);
1484 		trace_snd_soc_dapm_widget_event_start(w, event);
1485 		ret = w->event(w, NULL, event);
1486 		trace_snd_soc_dapm_widget_event_done(w, event);
1487 		if (ret < 0)
1488 			dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1489 			       ev_name, w->name, ret);
1490 	}
1491 }
1492 
1493 /* Apply the coalesced changes from a DAPM sequence */
1494 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1495 				   struct list_head *pending)
1496 {
1497 	struct snd_soc_dapm_context *dapm;
1498 	struct snd_soc_dapm_widget *w;
1499 	int reg;
1500 	unsigned int value = 0;
1501 	unsigned int mask = 0;
1502 
1503 	w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1504 	reg = w->reg;
1505 	dapm = w->dapm;
1506 
1507 	list_for_each_entry(w, pending, power_list) {
1508 		WARN_ON(reg != w->reg || dapm != w->dapm);
1509 		w->power = w->new_power;
1510 
1511 		mask |= w->mask << w->shift;
1512 		if (w->power)
1513 			value |= w->on_val << w->shift;
1514 		else
1515 			value |= w->off_val << w->shift;
1516 
1517 		pop_dbg(dapm->dev, card->pop_time,
1518 			"pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1519 			w->name, reg, value, mask);
1520 
1521 		/* Check for events */
1522 		dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1523 		dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1524 	}
1525 
1526 	if (reg >= 0) {
1527 		/* Any widget will do, they should all be updating the
1528 		 * same register.
1529 		 */
1530 
1531 		pop_dbg(dapm->dev, card->pop_time,
1532 			"pop test : Applying 0x%x/0x%x to %x in %dms\n",
1533 			value, mask, reg, card->pop_time);
1534 		pop_wait(card->pop_time);
1535 		soc_dapm_update_bits(dapm, reg, mask, value);
1536 	}
1537 
1538 	list_for_each_entry(w, pending, power_list) {
1539 		dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1540 		dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1541 	}
1542 }
1543 
1544 /* Apply a DAPM power sequence.
1545  *
1546  * We walk over a pre-sorted list of widgets to apply power to.  In
1547  * order to minimise the number of writes to the device required
1548  * multiple widgets will be updated in a single write where possible.
1549  * Currently anything that requires more than a single write is not
1550  * handled.
1551  */
1552 static void dapm_seq_run(struct snd_soc_card *card,
1553 	struct list_head *list, int event, bool power_up)
1554 {
1555 	struct snd_soc_dapm_widget *w, *n;
1556 	struct snd_soc_dapm_context *d;
1557 	LIST_HEAD(pending);
1558 	int cur_sort = -1;
1559 	int cur_subseq = -1;
1560 	int cur_reg = SND_SOC_NOPM;
1561 	struct snd_soc_dapm_context *cur_dapm = NULL;
1562 	int ret, i;
1563 	int *sort;
1564 
1565 	if (power_up)
1566 		sort = dapm_up_seq;
1567 	else
1568 		sort = dapm_down_seq;
1569 
1570 	list_for_each_entry_safe(w, n, list, power_list) {
1571 		ret = 0;
1572 
1573 		/* Do we need to apply any queued changes? */
1574 		if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1575 		    w->dapm != cur_dapm || w->subseq != cur_subseq) {
1576 			if (!list_empty(&pending))
1577 				dapm_seq_run_coalesced(card, &pending);
1578 
1579 			if (cur_dapm && cur_dapm->seq_notifier) {
1580 				for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1581 					if (sort[i] == cur_sort)
1582 						cur_dapm->seq_notifier(cur_dapm,
1583 								       i,
1584 								       cur_subseq);
1585 			}
1586 
1587 			if (cur_dapm && w->dapm != cur_dapm)
1588 				soc_dapm_async_complete(cur_dapm);
1589 
1590 			INIT_LIST_HEAD(&pending);
1591 			cur_sort = -1;
1592 			cur_subseq = INT_MIN;
1593 			cur_reg = SND_SOC_NOPM;
1594 			cur_dapm = NULL;
1595 		}
1596 
1597 		switch (w->id) {
1598 		case snd_soc_dapm_pre:
1599 			if (!w->event)
1600 				list_for_each_entry_safe_continue(w, n, list,
1601 								  power_list);
1602 
1603 			if (event == SND_SOC_DAPM_STREAM_START)
1604 				ret = w->event(w,
1605 					       NULL, SND_SOC_DAPM_PRE_PMU);
1606 			else if (event == SND_SOC_DAPM_STREAM_STOP)
1607 				ret = w->event(w,
1608 					       NULL, SND_SOC_DAPM_PRE_PMD);
1609 			break;
1610 
1611 		case snd_soc_dapm_post:
1612 			if (!w->event)
1613 				list_for_each_entry_safe_continue(w, n, list,
1614 								  power_list);
1615 
1616 			if (event == SND_SOC_DAPM_STREAM_START)
1617 				ret = w->event(w,
1618 					       NULL, SND_SOC_DAPM_POST_PMU);
1619 			else if (event == SND_SOC_DAPM_STREAM_STOP)
1620 				ret = w->event(w,
1621 					       NULL, SND_SOC_DAPM_POST_PMD);
1622 			break;
1623 
1624 		default:
1625 			/* Queue it up for application */
1626 			cur_sort = sort[w->id];
1627 			cur_subseq = w->subseq;
1628 			cur_reg = w->reg;
1629 			cur_dapm = w->dapm;
1630 			list_move(&w->power_list, &pending);
1631 			break;
1632 		}
1633 
1634 		if (ret < 0)
1635 			dev_err(w->dapm->dev,
1636 				"ASoC: Failed to apply widget power: %d\n", ret);
1637 	}
1638 
1639 	if (!list_empty(&pending))
1640 		dapm_seq_run_coalesced(card, &pending);
1641 
1642 	if (cur_dapm && cur_dapm->seq_notifier) {
1643 		for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1644 			if (sort[i] == cur_sort)
1645 				cur_dapm->seq_notifier(cur_dapm,
1646 						       i, cur_subseq);
1647 	}
1648 
1649 	list_for_each_entry(d, &card->dapm_list, list) {
1650 		soc_dapm_async_complete(d);
1651 	}
1652 }
1653 
1654 static void dapm_widget_update(struct snd_soc_card *card)
1655 {
1656 	struct snd_soc_dapm_update *update = card->update;
1657 	struct snd_soc_dapm_widget_list *wlist;
1658 	struct snd_soc_dapm_widget *w = NULL;
1659 	unsigned int wi;
1660 	int ret;
1661 
1662 	if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1663 		return;
1664 
1665 	wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1666 
1667 	for (wi = 0; wi < wlist->num_widgets; wi++) {
1668 		w = wlist->widgets[wi];
1669 
1670 		if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1671 			ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1672 			if (ret != 0)
1673 				dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1674 					   w->name, ret);
1675 		}
1676 	}
1677 
1678 	if (!w)
1679 		return;
1680 
1681 	ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1682 		update->val);
1683 	if (ret < 0)
1684 		dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1685 			w->name, ret);
1686 
1687 	if (update->has_second_set) {
1688 		ret = soc_dapm_update_bits(w->dapm, update->reg2,
1689 					   update->mask2, update->val2);
1690 		if (ret < 0)
1691 			dev_err(w->dapm->dev,
1692 				"ASoC: %s DAPM update failed: %d\n",
1693 				w->name, ret);
1694 	}
1695 
1696 	for (wi = 0; wi < wlist->num_widgets; wi++) {
1697 		w = wlist->widgets[wi];
1698 
1699 		if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1700 			ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1701 			if (ret != 0)
1702 				dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1703 					   w->name, ret);
1704 		}
1705 	}
1706 }
1707 
1708 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1709  * they're changing state.
1710  */
1711 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1712 {
1713 	struct snd_soc_dapm_context *d = data;
1714 	int ret;
1715 
1716 	/* If we're off and we're not supposed to go into STANDBY */
1717 	if (d->bias_level == SND_SOC_BIAS_OFF &&
1718 	    d->target_bias_level != SND_SOC_BIAS_OFF) {
1719 		if (d->dev)
1720 			pm_runtime_get_sync(d->dev);
1721 
1722 		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1723 		if (ret != 0)
1724 			dev_err(d->dev,
1725 				"ASoC: Failed to turn on bias: %d\n", ret);
1726 	}
1727 
1728 	/* Prepare for a transition to ON or away from ON */
1729 	if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1730 	     d->bias_level != SND_SOC_BIAS_ON) ||
1731 	    (d->target_bias_level != SND_SOC_BIAS_ON &&
1732 	     d->bias_level == SND_SOC_BIAS_ON)) {
1733 		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1734 		if (ret != 0)
1735 			dev_err(d->dev,
1736 				"ASoC: Failed to prepare bias: %d\n", ret);
1737 	}
1738 }
1739 
1740 /* Async callback run prior to DAPM sequences - brings to their final
1741  * state.
1742  */
1743 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1744 {
1745 	struct snd_soc_dapm_context *d = data;
1746 	int ret;
1747 
1748 	/* If we just powered the last thing off drop to standby bias */
1749 	if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1750 	    (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1751 	     d->target_bias_level == SND_SOC_BIAS_OFF)) {
1752 		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1753 		if (ret != 0)
1754 			dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1755 				ret);
1756 	}
1757 
1758 	/* If we're in standby and can support bias off then do that */
1759 	if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1760 	    d->target_bias_level == SND_SOC_BIAS_OFF) {
1761 		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1762 		if (ret != 0)
1763 			dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1764 				ret);
1765 
1766 		if (d->dev)
1767 			pm_runtime_put(d->dev);
1768 	}
1769 
1770 	/* If we just powered up then move to active bias */
1771 	if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1772 	    d->target_bias_level == SND_SOC_BIAS_ON) {
1773 		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1774 		if (ret != 0)
1775 			dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1776 				ret);
1777 	}
1778 }
1779 
1780 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1781 				       bool power, bool connect)
1782 {
1783 	/* If a connection is being made or broken then that update
1784 	 * will have marked the peer dirty, otherwise the widgets are
1785 	 * not connected and this update has no impact. */
1786 	if (!connect)
1787 		return;
1788 
1789 	/* If the peer is already in the state we're moving to then we
1790 	 * won't have an impact on it. */
1791 	if (power != peer->power)
1792 		dapm_mark_dirty(peer, "peer state change");
1793 }
1794 
1795 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1796 				  struct list_head *up_list,
1797 				  struct list_head *down_list)
1798 {
1799 	struct snd_soc_dapm_path *path;
1800 
1801 	if (w->power == power)
1802 		return;
1803 
1804 	trace_snd_soc_dapm_widget_power(w, power);
1805 
1806 	/* If we changed our power state perhaps our neigbours changed
1807 	 * also.
1808 	 */
1809 	snd_soc_dapm_widget_for_each_source_path(w, path)
1810 		dapm_widget_set_peer_power(path->source, power, path->connect);
1811 
1812 	/* Supplies can't affect their outputs, only their inputs */
1813 	if (!w->is_supply) {
1814 		snd_soc_dapm_widget_for_each_sink_path(w, path)
1815 			dapm_widget_set_peer_power(path->sink, power,
1816 						   path->connect);
1817 	}
1818 
1819 	if (power)
1820 		dapm_seq_insert(w, up_list, true);
1821 	else
1822 		dapm_seq_insert(w, down_list, false);
1823 }
1824 
1825 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1826 				  struct list_head *up_list,
1827 				  struct list_head *down_list)
1828 {
1829 	int power;
1830 
1831 	switch (w->id) {
1832 	case snd_soc_dapm_pre:
1833 		dapm_seq_insert(w, down_list, false);
1834 		break;
1835 	case snd_soc_dapm_post:
1836 		dapm_seq_insert(w, up_list, true);
1837 		break;
1838 
1839 	default:
1840 		power = dapm_widget_power_check(w);
1841 
1842 		dapm_widget_set_power(w, power, up_list, down_list);
1843 		break;
1844 	}
1845 }
1846 
1847 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1848 {
1849 	if (dapm->idle_bias_off)
1850 		return true;
1851 
1852 	switch (snd_power_get_state(dapm->card->snd_card)) {
1853 	case SNDRV_CTL_POWER_D3hot:
1854 	case SNDRV_CTL_POWER_D3cold:
1855 		return dapm->suspend_bias_off;
1856 	default:
1857 		break;
1858 	}
1859 
1860 	return false;
1861 }
1862 
1863 /*
1864  * Scan each dapm widget for complete audio path.
1865  * A complete path is a route that has valid endpoints i.e.:-
1866  *
1867  *  o DAC to output pin.
1868  *  o Input pin to ADC.
1869  *  o Input pin to Output pin (bypass, sidetone)
1870  *  o DAC to ADC (loopback).
1871  */
1872 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1873 {
1874 	struct snd_soc_dapm_widget *w;
1875 	struct snd_soc_dapm_context *d;
1876 	LIST_HEAD(up_list);
1877 	LIST_HEAD(down_list);
1878 	ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1879 	enum snd_soc_bias_level bias;
1880 
1881 	lockdep_assert_held(&card->dapm_mutex);
1882 
1883 	trace_snd_soc_dapm_start(card);
1884 
1885 	list_for_each_entry(d, &card->dapm_list, list) {
1886 		if (dapm_idle_bias_off(d))
1887 			d->target_bias_level = SND_SOC_BIAS_OFF;
1888 		else
1889 			d->target_bias_level = SND_SOC_BIAS_STANDBY;
1890 	}
1891 
1892 	dapm_reset(card);
1893 
1894 	/* Check which widgets we need to power and store them in
1895 	 * lists indicating if they should be powered up or down.  We
1896 	 * only check widgets that have been flagged as dirty but note
1897 	 * that new widgets may be added to the dirty list while we
1898 	 * iterate.
1899 	 */
1900 	list_for_each_entry(w, &card->dapm_dirty, dirty) {
1901 		dapm_power_one_widget(w, &up_list, &down_list);
1902 	}
1903 
1904 	list_for_each_entry(w, &card->widgets, list) {
1905 		switch (w->id) {
1906 		case snd_soc_dapm_pre:
1907 		case snd_soc_dapm_post:
1908 			/* These widgets always need to be powered */
1909 			break;
1910 		default:
1911 			list_del_init(&w->dirty);
1912 			break;
1913 		}
1914 
1915 		if (w->new_power) {
1916 			d = w->dapm;
1917 
1918 			/* Supplies and micbiases only bring the
1919 			 * context up to STANDBY as unless something
1920 			 * else is active and passing audio they
1921 			 * generally don't require full power.  Signal
1922 			 * generators are virtual pins and have no
1923 			 * power impact themselves.
1924 			 */
1925 			switch (w->id) {
1926 			case snd_soc_dapm_siggen:
1927 			case snd_soc_dapm_vmid:
1928 				break;
1929 			case snd_soc_dapm_supply:
1930 			case snd_soc_dapm_regulator_supply:
1931 			case snd_soc_dapm_pinctrl:
1932 			case snd_soc_dapm_clock_supply:
1933 			case snd_soc_dapm_micbias:
1934 				if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1935 					d->target_bias_level = SND_SOC_BIAS_STANDBY;
1936 				break;
1937 			default:
1938 				d->target_bias_level = SND_SOC_BIAS_ON;
1939 				break;
1940 			}
1941 		}
1942 
1943 	}
1944 
1945 	/* Force all contexts in the card to the same bias state if
1946 	 * they're not ground referenced.
1947 	 */
1948 	bias = SND_SOC_BIAS_OFF;
1949 	list_for_each_entry(d, &card->dapm_list, list)
1950 		if (d->target_bias_level > bias)
1951 			bias = d->target_bias_level;
1952 	list_for_each_entry(d, &card->dapm_list, list)
1953 		if (!dapm_idle_bias_off(d))
1954 			d->target_bias_level = bias;
1955 
1956 	trace_snd_soc_dapm_walk_done(card);
1957 
1958 	/* Run card bias changes at first */
1959 	dapm_pre_sequence_async(&card->dapm, 0);
1960 	/* Run other bias changes in parallel */
1961 	list_for_each_entry(d, &card->dapm_list, list) {
1962 		if (d != &card->dapm)
1963 			async_schedule_domain(dapm_pre_sequence_async, d,
1964 						&async_domain);
1965 	}
1966 	async_synchronize_full_domain(&async_domain);
1967 
1968 	list_for_each_entry(w, &down_list, power_list) {
1969 		dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1970 	}
1971 
1972 	list_for_each_entry(w, &up_list, power_list) {
1973 		dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1974 	}
1975 
1976 	/* Power down widgets first; try to avoid amplifying pops. */
1977 	dapm_seq_run(card, &down_list, event, false);
1978 
1979 	dapm_widget_update(card);
1980 
1981 	/* Now power up. */
1982 	dapm_seq_run(card, &up_list, event, true);
1983 
1984 	/* Run all the bias changes in parallel */
1985 	list_for_each_entry(d, &card->dapm_list, list) {
1986 		if (d != &card->dapm)
1987 			async_schedule_domain(dapm_post_sequence_async, d,
1988 						&async_domain);
1989 	}
1990 	async_synchronize_full_domain(&async_domain);
1991 	/* Run card bias changes at last */
1992 	dapm_post_sequence_async(&card->dapm, 0);
1993 
1994 	/* do we need to notify any clients that DAPM event is complete */
1995 	list_for_each_entry(d, &card->dapm_list, list) {
1996 		if (d->stream_event)
1997 			d->stream_event(d, event);
1998 	}
1999 
2000 	pop_dbg(card->dev, card->pop_time,
2001 		"DAPM sequencing finished, waiting %dms\n", card->pop_time);
2002 	pop_wait(card->pop_time);
2003 
2004 	trace_snd_soc_dapm_done(card);
2005 
2006 	return 0;
2007 }
2008 
2009 #ifdef CONFIG_DEBUG_FS
2010 static ssize_t dapm_widget_power_read_file(struct file *file,
2011 					   char __user *user_buf,
2012 					   size_t count, loff_t *ppos)
2013 {
2014 	struct snd_soc_dapm_widget *w = file->private_data;
2015 	struct snd_soc_card *card = w->dapm->card;
2016 	enum snd_soc_dapm_direction dir, rdir;
2017 	char *buf;
2018 	int in, out;
2019 	ssize_t ret;
2020 	struct snd_soc_dapm_path *p = NULL;
2021 
2022 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2023 	if (!buf)
2024 		return -ENOMEM;
2025 
2026 	mutex_lock(&card->dapm_mutex);
2027 
2028 	/* Supply widgets are not handled by is_connected_{input,output}_ep() */
2029 	if (w->is_supply) {
2030 		in = 0;
2031 		out = 0;
2032 	} else {
2033 		in = is_connected_input_ep(w, NULL, NULL);
2034 		out = is_connected_output_ep(w, NULL, NULL);
2035 	}
2036 
2037 	ret = snprintf(buf, PAGE_SIZE, "%s: %s%s  in %d out %d",
2038 		       w->name, w->power ? "On" : "Off",
2039 		       w->force ? " (forced)" : "", in, out);
2040 
2041 	if (w->reg >= 0)
2042 		ret += snprintf(buf + ret, PAGE_SIZE - ret,
2043 				" - R%d(0x%x) mask 0x%x",
2044 				w->reg, w->reg, w->mask << w->shift);
2045 
2046 	ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
2047 
2048 	if (w->sname)
2049 		ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2050 				w->sname,
2051 				w->active ? "active" : "inactive");
2052 
2053 	snd_soc_dapm_for_each_direction(dir) {
2054 		rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2055 		snd_soc_dapm_widget_for_each_path(w, dir, p) {
2056 			if (p->connected && !p->connected(p->source, p->sink))
2057 				continue;
2058 
2059 			if (!p->connect)
2060 				continue;
2061 
2062 			ret += snprintf(buf + ret, PAGE_SIZE - ret,
2063 					" %s  \"%s\" \"%s\"\n",
2064 					(rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2065 					p->name ? p->name : "static",
2066 					p->node[rdir]->name);
2067 		}
2068 	}
2069 
2070 	mutex_unlock(&card->dapm_mutex);
2071 
2072 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2073 
2074 	kfree(buf);
2075 	return ret;
2076 }
2077 
2078 static const struct file_operations dapm_widget_power_fops = {
2079 	.open = simple_open,
2080 	.read = dapm_widget_power_read_file,
2081 	.llseek = default_llseek,
2082 };
2083 
2084 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2085 				   size_t count, loff_t *ppos)
2086 {
2087 	struct snd_soc_dapm_context *dapm = file->private_data;
2088 	char *level;
2089 
2090 	switch (dapm->bias_level) {
2091 	case SND_SOC_BIAS_ON:
2092 		level = "On\n";
2093 		break;
2094 	case SND_SOC_BIAS_PREPARE:
2095 		level = "Prepare\n";
2096 		break;
2097 	case SND_SOC_BIAS_STANDBY:
2098 		level = "Standby\n";
2099 		break;
2100 	case SND_SOC_BIAS_OFF:
2101 		level = "Off\n";
2102 		break;
2103 	default:
2104 		WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2105 		level = "Unknown\n";
2106 		break;
2107 	}
2108 
2109 	return simple_read_from_buffer(user_buf, count, ppos, level,
2110 				       strlen(level));
2111 }
2112 
2113 static const struct file_operations dapm_bias_fops = {
2114 	.open = simple_open,
2115 	.read = dapm_bias_read_file,
2116 	.llseek = default_llseek,
2117 };
2118 
2119 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2120 	struct dentry *parent)
2121 {
2122 	struct dentry *d;
2123 
2124 	if (!parent)
2125 		return;
2126 
2127 	dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2128 
2129 	if (!dapm->debugfs_dapm) {
2130 		dev_warn(dapm->dev,
2131 		       "ASoC: Failed to create DAPM debugfs directory\n");
2132 		return;
2133 	}
2134 
2135 	d = debugfs_create_file("bias_level", 0444,
2136 				dapm->debugfs_dapm, dapm,
2137 				&dapm_bias_fops);
2138 	if (!d)
2139 		dev_warn(dapm->dev,
2140 			 "ASoC: Failed to create bias level debugfs file\n");
2141 }
2142 
2143 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2144 {
2145 	struct snd_soc_dapm_context *dapm = w->dapm;
2146 	struct dentry *d;
2147 
2148 	if (!dapm->debugfs_dapm || !w->name)
2149 		return;
2150 
2151 	d = debugfs_create_file(w->name, 0444,
2152 				dapm->debugfs_dapm, w,
2153 				&dapm_widget_power_fops);
2154 	if (!d)
2155 		dev_warn(w->dapm->dev,
2156 			"ASoC: Failed to create %s debugfs file\n",
2157 			w->name);
2158 }
2159 
2160 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2161 {
2162 	debugfs_remove_recursive(dapm->debugfs_dapm);
2163 }
2164 
2165 #else
2166 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2167 	struct dentry *parent)
2168 {
2169 }
2170 
2171 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2172 {
2173 }
2174 
2175 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2176 {
2177 }
2178 
2179 #endif
2180 
2181 /*
2182  * soc_dapm_connect_path() - Connects or disconnects a path
2183  * @path: The path to update
2184  * @connect: The new connect state of the path. True if the path is connected,
2185  *  false if it is disconnected.
2186  * @reason: The reason why the path changed (for debugging only)
2187  */
2188 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2189 	bool connect, const char *reason)
2190 {
2191 	if (path->connect == connect)
2192 		return;
2193 
2194 	path->connect = connect;
2195 	dapm_mark_dirty(path->source, reason);
2196 	dapm_mark_dirty(path->sink, reason);
2197 	dapm_path_invalidate(path);
2198 }
2199 
2200 /* test and update the power status of a mux widget */
2201 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2202 				 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2203 {
2204 	struct snd_soc_dapm_path *path;
2205 	int found = 0;
2206 	bool connect;
2207 
2208 	lockdep_assert_held(&card->dapm_mutex);
2209 
2210 	/* find dapm widget path assoc with kcontrol */
2211 	dapm_kcontrol_for_each_path(path, kcontrol) {
2212 		found = 1;
2213 		/* we now need to match the string in the enum to the path */
2214 		if (!(strcmp(path->name, e->texts[mux])))
2215 			connect = true;
2216 		else
2217 			connect = false;
2218 
2219 		soc_dapm_connect_path(path, connect, "mux update");
2220 	}
2221 
2222 	if (found)
2223 		dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2224 
2225 	return found;
2226 }
2227 
2228 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2229 	struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2230 	struct snd_soc_dapm_update *update)
2231 {
2232 	struct snd_soc_card *card = dapm->card;
2233 	int ret;
2234 
2235 	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2236 	card->update = update;
2237 	ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2238 	card->update = NULL;
2239 	mutex_unlock(&card->dapm_mutex);
2240 	if (ret > 0)
2241 		soc_dpcm_runtime_update(card);
2242 	return ret;
2243 }
2244 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2245 
2246 /* test and update the power status of a mixer or switch widget */
2247 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2248 				       struct snd_kcontrol *kcontrol,
2249 				       int connect, int rconnect)
2250 {
2251 	struct snd_soc_dapm_path *path;
2252 	int found = 0;
2253 
2254 	lockdep_assert_held(&card->dapm_mutex);
2255 
2256 	/* find dapm widget path assoc with kcontrol */
2257 	dapm_kcontrol_for_each_path(path, kcontrol) {
2258 		/*
2259 		 * Ideally this function should support any number of
2260 		 * paths and channels. But since kcontrols only come
2261 		 * in mono and stereo variants, we are limited to 2
2262 		 * channels.
2263 		 *
2264 		 * The following code assumes for stereo controls the
2265 		 * first path (when 'found == 0') is the left channel,
2266 		 * and all remaining paths (when 'found == 1') are the
2267 		 * right channel.
2268 		 *
2269 		 * A stereo control is signified by a valid 'rconnect'
2270 		 * value, either 0 for unconnected, or >= 0 for connected.
2271 		 * This is chosen instead of using snd_soc_volsw_is_stereo,
2272 		 * so that the behavior of snd_soc_dapm_mixer_update_power
2273 		 * doesn't change even when the kcontrol passed in is
2274 		 * stereo.
2275 		 *
2276 		 * It passes 'connect' as the path connect status for
2277 		 * the left channel, and 'rconnect' for the right
2278 		 * channel.
2279 		 */
2280 		if (found && rconnect >= 0)
2281 			soc_dapm_connect_path(path, rconnect, "mixer update");
2282 		else
2283 			soc_dapm_connect_path(path, connect, "mixer update");
2284 		found = 1;
2285 	}
2286 
2287 	if (found)
2288 		dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2289 
2290 	return found;
2291 }
2292 
2293 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2294 	struct snd_kcontrol *kcontrol, int connect,
2295 	struct snd_soc_dapm_update *update)
2296 {
2297 	struct snd_soc_card *card = dapm->card;
2298 	int ret;
2299 
2300 	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2301 	card->update = update;
2302 	ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2303 	card->update = NULL;
2304 	mutex_unlock(&card->dapm_mutex);
2305 	if (ret > 0)
2306 		soc_dpcm_runtime_update(card);
2307 	return ret;
2308 }
2309 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2310 
2311 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2312 	char *buf)
2313 {
2314 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2315 	struct snd_soc_dapm_widget *w;
2316 	int count = 0;
2317 	char *state = "not set";
2318 
2319 	/* card won't be set for the dummy component, as a spot fix
2320 	 * we're checking for that case specifically here but in future
2321 	 * we will ensure that the dummy component looks like others.
2322 	 */
2323 	if (!cmpnt->card)
2324 		return 0;
2325 
2326 	list_for_each_entry(w, &cmpnt->card->widgets, list) {
2327 		if (w->dapm != dapm)
2328 			continue;
2329 
2330 		/* only display widgets that burn power */
2331 		switch (w->id) {
2332 		case snd_soc_dapm_hp:
2333 		case snd_soc_dapm_mic:
2334 		case snd_soc_dapm_spk:
2335 		case snd_soc_dapm_line:
2336 		case snd_soc_dapm_micbias:
2337 		case snd_soc_dapm_dac:
2338 		case snd_soc_dapm_adc:
2339 		case snd_soc_dapm_pga:
2340 		case snd_soc_dapm_out_drv:
2341 		case snd_soc_dapm_mixer:
2342 		case snd_soc_dapm_mixer_named_ctl:
2343 		case snd_soc_dapm_supply:
2344 		case snd_soc_dapm_regulator_supply:
2345 		case snd_soc_dapm_pinctrl:
2346 		case snd_soc_dapm_clock_supply:
2347 			if (w->name)
2348 				count += sprintf(buf + count, "%s: %s\n",
2349 					w->name, w->power ? "On":"Off");
2350 		break;
2351 		default:
2352 		break;
2353 		}
2354 	}
2355 
2356 	switch (snd_soc_dapm_get_bias_level(dapm)) {
2357 	case SND_SOC_BIAS_ON:
2358 		state = "On";
2359 		break;
2360 	case SND_SOC_BIAS_PREPARE:
2361 		state = "Prepare";
2362 		break;
2363 	case SND_SOC_BIAS_STANDBY:
2364 		state = "Standby";
2365 		break;
2366 	case SND_SOC_BIAS_OFF:
2367 		state = "Off";
2368 		break;
2369 	}
2370 	count += sprintf(buf + count, "PM State: %s\n", state);
2371 
2372 	return count;
2373 }
2374 
2375 /* show dapm widget status in sys fs */
2376 static ssize_t dapm_widget_show(struct device *dev,
2377 	struct device_attribute *attr, char *buf)
2378 {
2379 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2380 	int i, count = 0;
2381 
2382 	mutex_lock(&rtd->card->dapm_mutex);
2383 
2384 	for (i = 0; i < rtd->num_codecs; i++) {
2385 		struct snd_soc_component *cmpnt = rtd->codec_dais[i]->component;
2386 
2387 		count += dapm_widget_show_component(cmpnt, buf + count);
2388 	}
2389 
2390 	mutex_unlock(&rtd->card->dapm_mutex);
2391 
2392 	return count;
2393 }
2394 
2395 static DEVICE_ATTR_RO(dapm_widget);
2396 
2397 struct attribute *soc_dapm_dev_attrs[] = {
2398 	&dev_attr_dapm_widget.attr,
2399 	NULL
2400 };
2401 
2402 static void dapm_free_path(struct snd_soc_dapm_path *path)
2403 {
2404 	list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2405 	list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2406 	list_del(&path->list_kcontrol);
2407 	list_del(&path->list);
2408 	kfree(path);
2409 }
2410 
2411 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2412 {
2413 	struct snd_soc_dapm_path *p, *next_p;
2414 	enum snd_soc_dapm_direction dir;
2415 
2416 	list_del(&w->list);
2417 	/*
2418 	 * remove source and sink paths associated to this widget.
2419 	 * While removing the path, remove reference to it from both
2420 	 * source and sink widgets so that path is removed only once.
2421 	 */
2422 	snd_soc_dapm_for_each_direction(dir) {
2423 		snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2424 			dapm_free_path(p);
2425 	}
2426 
2427 	kfree(w->kcontrols);
2428 	kfree_const(w->name);
2429 	kfree(w);
2430 }
2431 
2432 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2433 {
2434 	dapm->path_sink_cache.widget = NULL;
2435 	dapm->path_source_cache.widget = NULL;
2436 }
2437 
2438 /* free all dapm widgets and resources */
2439 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2440 {
2441 	struct snd_soc_dapm_widget *w, *next_w;
2442 
2443 	list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2444 		if (w->dapm != dapm)
2445 			continue;
2446 		snd_soc_dapm_free_widget(w);
2447 	}
2448 	snd_soc_dapm_reset_cache(dapm);
2449 }
2450 
2451 static struct snd_soc_dapm_widget *dapm_find_widget(
2452 			struct snd_soc_dapm_context *dapm, const char *pin,
2453 			bool search_other_contexts)
2454 {
2455 	struct snd_soc_dapm_widget *w;
2456 	struct snd_soc_dapm_widget *fallback = NULL;
2457 
2458 	list_for_each_entry(w, &dapm->card->widgets, list) {
2459 		if (!strcmp(w->name, pin)) {
2460 			if (w->dapm == dapm)
2461 				return w;
2462 			else
2463 				fallback = w;
2464 		}
2465 	}
2466 
2467 	if (search_other_contexts)
2468 		return fallback;
2469 
2470 	return NULL;
2471 }
2472 
2473 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2474 				const char *pin, int status)
2475 {
2476 	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2477 
2478 	dapm_assert_locked(dapm);
2479 
2480 	if (!w) {
2481 		dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2482 		return -EINVAL;
2483 	}
2484 
2485 	if (w->connected != status) {
2486 		dapm_mark_dirty(w, "pin configuration");
2487 		dapm_widget_invalidate_input_paths(w);
2488 		dapm_widget_invalidate_output_paths(w);
2489 	}
2490 
2491 	w->connected = status;
2492 	if (status == 0)
2493 		w->force = 0;
2494 
2495 	return 0;
2496 }
2497 
2498 /**
2499  * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2500  * @dapm: DAPM context
2501  *
2502  * Walks all dapm audio paths and powers widgets according to their
2503  * stream or path usage.
2504  *
2505  * Requires external locking.
2506  *
2507  * Returns 0 for success.
2508  */
2509 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2510 {
2511 	/*
2512 	 * Suppress early reports (eg, jacks syncing their state) to avoid
2513 	 * silly DAPM runs during card startup.
2514 	 */
2515 	if (!dapm->card || !dapm->card->instantiated)
2516 		return 0;
2517 
2518 	return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2519 }
2520 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2521 
2522 /**
2523  * snd_soc_dapm_sync - scan and power dapm paths
2524  * @dapm: DAPM context
2525  *
2526  * Walks all dapm audio paths and powers widgets according to their
2527  * stream or path usage.
2528  *
2529  * Returns 0 for success.
2530  */
2531 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2532 {
2533 	int ret;
2534 
2535 	mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2536 	ret = snd_soc_dapm_sync_unlocked(dapm);
2537 	mutex_unlock(&dapm->card->dapm_mutex);
2538 	return ret;
2539 }
2540 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2541 
2542 /*
2543  * dapm_update_widget_flags() - Re-compute widget sink and source flags
2544  * @w: The widget for which to update the flags
2545  *
2546  * Some widgets have a dynamic category which depends on which neighbors they
2547  * are connected to. This function update the category for these widgets.
2548  *
2549  * This function must be called whenever a path is added or removed to a widget.
2550  */
2551 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2552 {
2553 	enum snd_soc_dapm_direction dir;
2554 	struct snd_soc_dapm_path *p;
2555 	unsigned int ep;
2556 
2557 	switch (w->id) {
2558 	case snd_soc_dapm_input:
2559 		/* On a fully routed card an input is never a source */
2560 		if (w->dapm->card->fully_routed)
2561 			return;
2562 		ep = SND_SOC_DAPM_EP_SOURCE;
2563 		snd_soc_dapm_widget_for_each_source_path(w, p) {
2564 			if (p->source->id == snd_soc_dapm_micbias ||
2565 				p->source->id == snd_soc_dapm_mic ||
2566 				p->source->id == snd_soc_dapm_line ||
2567 				p->source->id == snd_soc_dapm_output) {
2568 					ep = 0;
2569 					break;
2570 			}
2571 		}
2572 		break;
2573 	case snd_soc_dapm_output:
2574 		/* On a fully routed card a output is never a sink */
2575 		if (w->dapm->card->fully_routed)
2576 			return;
2577 		ep = SND_SOC_DAPM_EP_SINK;
2578 		snd_soc_dapm_widget_for_each_sink_path(w, p) {
2579 			if (p->sink->id == snd_soc_dapm_spk ||
2580 				p->sink->id == snd_soc_dapm_hp ||
2581 				p->sink->id == snd_soc_dapm_line ||
2582 				p->sink->id == snd_soc_dapm_input) {
2583 					ep = 0;
2584 					break;
2585 			}
2586 		}
2587 		break;
2588 	case snd_soc_dapm_line:
2589 		ep = 0;
2590 		snd_soc_dapm_for_each_direction(dir) {
2591 			if (!list_empty(&w->edges[dir]))
2592 				ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2593 		}
2594 		break;
2595 	default:
2596 		return;
2597 	}
2598 
2599 	w->is_ep = ep;
2600 }
2601 
2602 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2603 	struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2604 	const char *control)
2605 {
2606 	bool dynamic_source = false;
2607 	bool dynamic_sink = false;
2608 
2609 	if (!control)
2610 		return 0;
2611 
2612 	switch (source->id) {
2613 	case snd_soc_dapm_demux:
2614 		dynamic_source = true;
2615 		break;
2616 	default:
2617 		break;
2618 	}
2619 
2620 	switch (sink->id) {
2621 	case snd_soc_dapm_mux:
2622 	case snd_soc_dapm_switch:
2623 	case snd_soc_dapm_mixer:
2624 	case snd_soc_dapm_mixer_named_ctl:
2625 		dynamic_sink = true;
2626 		break;
2627 	default:
2628 		break;
2629 	}
2630 
2631 	if (dynamic_source && dynamic_sink) {
2632 		dev_err(dapm->dev,
2633 			"Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2634 			source->name, control, sink->name);
2635 		return -EINVAL;
2636 	} else if (!dynamic_source && !dynamic_sink) {
2637 		dev_err(dapm->dev,
2638 			"Control not supported for path %s -> [%s] -> %s\n",
2639 			source->name, control, sink->name);
2640 		return -EINVAL;
2641 	}
2642 
2643 	return 0;
2644 }
2645 
2646 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2647 	struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2648 	const char *control,
2649 	int (*connected)(struct snd_soc_dapm_widget *source,
2650 			 struct snd_soc_dapm_widget *sink))
2651 {
2652 	struct snd_soc_dapm_widget *widgets[2];
2653 	enum snd_soc_dapm_direction dir;
2654 	struct snd_soc_dapm_path *path;
2655 	int ret;
2656 
2657 	if (wsink->is_supply && !wsource->is_supply) {
2658 		dev_err(dapm->dev,
2659 			"Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2660 			wsource->name, wsink->name);
2661 		return -EINVAL;
2662 	}
2663 
2664 	if (connected && !wsource->is_supply) {
2665 		dev_err(dapm->dev,
2666 			"connected() callback only supported for supply widgets (%s -> %s)\n",
2667 			wsource->name, wsink->name);
2668 		return -EINVAL;
2669 	}
2670 
2671 	if (wsource->is_supply && control) {
2672 		dev_err(dapm->dev,
2673 			"Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2674 			wsource->name, control, wsink->name);
2675 		return -EINVAL;
2676 	}
2677 
2678 	ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2679 	if (ret)
2680 		return ret;
2681 
2682 	path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2683 	if (!path)
2684 		return -ENOMEM;
2685 
2686 	path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2687 	path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2688 	widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2689 	widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2690 
2691 	path->connected = connected;
2692 	INIT_LIST_HEAD(&path->list);
2693 	INIT_LIST_HEAD(&path->list_kcontrol);
2694 
2695 	if (wsource->is_supply || wsink->is_supply)
2696 		path->is_supply = 1;
2697 
2698 	/* connect static paths */
2699 	if (control == NULL) {
2700 		path->connect = 1;
2701 	} else {
2702 		switch (wsource->id) {
2703 		case snd_soc_dapm_demux:
2704 			ret = dapm_connect_mux(dapm, path, control, wsource);
2705 			if (ret)
2706 				goto err;
2707 			break;
2708 		default:
2709 			break;
2710 		}
2711 
2712 		switch (wsink->id) {
2713 		case snd_soc_dapm_mux:
2714 			ret = dapm_connect_mux(dapm, path, control, wsink);
2715 			if (ret != 0)
2716 				goto err;
2717 			break;
2718 		case snd_soc_dapm_switch:
2719 		case snd_soc_dapm_mixer:
2720 		case snd_soc_dapm_mixer_named_ctl:
2721 			ret = dapm_connect_mixer(dapm, path, control);
2722 			if (ret != 0)
2723 				goto err;
2724 			break;
2725 		default:
2726 			break;
2727 		}
2728 	}
2729 
2730 	list_add(&path->list, &dapm->card->paths);
2731 	snd_soc_dapm_for_each_direction(dir)
2732 		list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2733 
2734 	snd_soc_dapm_for_each_direction(dir) {
2735 		dapm_update_widget_flags(widgets[dir]);
2736 		dapm_mark_dirty(widgets[dir], "Route added");
2737 	}
2738 
2739 	if (dapm->card->instantiated && path->connect)
2740 		dapm_path_invalidate(path);
2741 
2742 	return 0;
2743 err:
2744 	kfree(path);
2745 	return ret;
2746 }
2747 
2748 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2749 				  const struct snd_soc_dapm_route *route)
2750 {
2751 	struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2752 	struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2753 	const char *sink;
2754 	const char *source;
2755 	char prefixed_sink[80];
2756 	char prefixed_source[80];
2757 	const char *prefix;
2758 	int ret;
2759 
2760 	prefix = soc_dapm_prefix(dapm);
2761 	if (prefix) {
2762 		snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2763 			 prefix, route->sink);
2764 		sink = prefixed_sink;
2765 		snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2766 			 prefix, route->source);
2767 		source = prefixed_source;
2768 	} else {
2769 		sink = route->sink;
2770 		source = route->source;
2771 	}
2772 
2773 	wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2774 	wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2775 
2776 	if (wsink && wsource)
2777 		goto skip_search;
2778 
2779 	/*
2780 	 * find src and dest widgets over all widgets but favor a widget from
2781 	 * current DAPM context
2782 	 */
2783 	list_for_each_entry(w, &dapm->card->widgets, list) {
2784 		if (!wsink && !(strcmp(w->name, sink))) {
2785 			wtsink = w;
2786 			if (w->dapm == dapm) {
2787 				wsink = w;
2788 				if (wsource)
2789 					break;
2790 			}
2791 			continue;
2792 		}
2793 		if (!wsource && !(strcmp(w->name, source))) {
2794 			wtsource = w;
2795 			if (w->dapm == dapm) {
2796 				wsource = w;
2797 				if (wsink)
2798 					break;
2799 			}
2800 		}
2801 	}
2802 	/* use widget from another DAPM context if not found from this */
2803 	if (!wsink)
2804 		wsink = wtsink;
2805 	if (!wsource)
2806 		wsource = wtsource;
2807 
2808 	if (wsource == NULL) {
2809 		dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2810 			route->source);
2811 		return -ENODEV;
2812 	}
2813 	if (wsink == NULL) {
2814 		dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2815 			route->sink);
2816 		return -ENODEV;
2817 	}
2818 
2819 skip_search:
2820 	dapm_wcache_update(&dapm->path_sink_cache, wsink);
2821 	dapm_wcache_update(&dapm->path_source_cache, wsource);
2822 
2823 	ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2824 		route->connected);
2825 	if (ret)
2826 		goto err;
2827 
2828 	return 0;
2829 err:
2830 	dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2831 		 source, route->control, sink);
2832 	return ret;
2833 }
2834 
2835 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2836 				  const struct snd_soc_dapm_route *route)
2837 {
2838 	struct snd_soc_dapm_widget *wsource, *wsink;
2839 	struct snd_soc_dapm_path *path, *p;
2840 	const char *sink;
2841 	const char *source;
2842 	char prefixed_sink[80];
2843 	char prefixed_source[80];
2844 	const char *prefix;
2845 
2846 	if (route->control) {
2847 		dev_err(dapm->dev,
2848 			"ASoC: Removal of routes with controls not supported\n");
2849 		return -EINVAL;
2850 	}
2851 
2852 	prefix = soc_dapm_prefix(dapm);
2853 	if (prefix) {
2854 		snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2855 			 prefix, route->sink);
2856 		sink = prefixed_sink;
2857 		snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2858 			 prefix, route->source);
2859 		source = prefixed_source;
2860 	} else {
2861 		sink = route->sink;
2862 		source = route->source;
2863 	}
2864 
2865 	path = NULL;
2866 	list_for_each_entry(p, &dapm->card->paths, list) {
2867 		if (strcmp(p->source->name, source) != 0)
2868 			continue;
2869 		if (strcmp(p->sink->name, sink) != 0)
2870 			continue;
2871 		path = p;
2872 		break;
2873 	}
2874 
2875 	if (path) {
2876 		wsource = path->source;
2877 		wsink = path->sink;
2878 
2879 		dapm_mark_dirty(wsource, "Route removed");
2880 		dapm_mark_dirty(wsink, "Route removed");
2881 		if (path->connect)
2882 			dapm_path_invalidate(path);
2883 
2884 		dapm_free_path(path);
2885 
2886 		/* Update any path related flags */
2887 		dapm_update_widget_flags(wsource);
2888 		dapm_update_widget_flags(wsink);
2889 	} else {
2890 		dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2891 			 source, sink);
2892 	}
2893 
2894 	return 0;
2895 }
2896 
2897 /**
2898  * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2899  * @dapm: DAPM context
2900  * @route: audio routes
2901  * @num: number of routes
2902  *
2903  * Connects 2 dapm widgets together via a named audio path. The sink is
2904  * the widget receiving the audio signal, whilst the source is the sender
2905  * of the audio signal.
2906  *
2907  * Returns 0 for success else error. On error all resources can be freed
2908  * with a call to snd_soc_card_free().
2909  */
2910 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2911 			    const struct snd_soc_dapm_route *route, int num)
2912 {
2913 	int i, r, ret = 0;
2914 
2915 	mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2916 	for (i = 0; i < num; i++) {
2917 		r = snd_soc_dapm_add_route(dapm, route);
2918 		if (r < 0) {
2919 			dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2920 				route->source,
2921 				route->control ? route->control : "direct",
2922 				route->sink);
2923 			ret = r;
2924 		}
2925 		route++;
2926 	}
2927 	mutex_unlock(&dapm->card->dapm_mutex);
2928 
2929 	return ret;
2930 }
2931 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2932 
2933 /**
2934  * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2935  * @dapm: DAPM context
2936  * @route: audio routes
2937  * @num: number of routes
2938  *
2939  * Removes routes from the DAPM context.
2940  */
2941 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2942 			    const struct snd_soc_dapm_route *route, int num)
2943 {
2944 	int i;
2945 
2946 	mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2947 	for (i = 0; i < num; i++) {
2948 		snd_soc_dapm_del_route(dapm, route);
2949 		route++;
2950 	}
2951 	mutex_unlock(&dapm->card->dapm_mutex);
2952 
2953 	return 0;
2954 }
2955 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2956 
2957 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2958 				   const struct snd_soc_dapm_route *route)
2959 {
2960 	struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2961 							      route->source,
2962 							      true);
2963 	struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2964 							    route->sink,
2965 							    true);
2966 	struct snd_soc_dapm_path *path;
2967 	int count = 0;
2968 
2969 	if (!source) {
2970 		dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2971 			route->source);
2972 		return -ENODEV;
2973 	}
2974 
2975 	if (!sink) {
2976 		dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2977 			route->sink);
2978 		return -ENODEV;
2979 	}
2980 
2981 	if (route->control || route->connected)
2982 		dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2983 			 route->source, route->sink);
2984 
2985 	snd_soc_dapm_widget_for_each_sink_path(source, path) {
2986 		if (path->sink == sink) {
2987 			path->weak = 1;
2988 			count++;
2989 		}
2990 	}
2991 
2992 	if (count == 0)
2993 		dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2994 			route->source, route->sink);
2995 	if (count > 1)
2996 		dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2997 			 count, route->source, route->sink);
2998 
2999 	return 0;
3000 }
3001 
3002 /**
3003  * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3004  * @dapm: DAPM context
3005  * @route: audio routes
3006  * @num: number of routes
3007  *
3008  * Mark existing routes matching those specified in the passed array
3009  * as being weak, meaning that they are ignored for the purpose of
3010  * power decisions.  The main intended use case is for sidetone paths
3011  * which couple audio between other independent paths if they are both
3012  * active in order to make the combination work better at the user
3013  * level but which aren't intended to be "used".
3014  *
3015  * Note that CODEC drivers should not use this as sidetone type paths
3016  * can frequently also be used as bypass paths.
3017  */
3018 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3019 			     const struct snd_soc_dapm_route *route, int num)
3020 {
3021 	int i, err;
3022 	int ret = 0;
3023 
3024 	mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3025 	for (i = 0; i < num; i++) {
3026 		err = snd_soc_dapm_weak_route(dapm, route);
3027 		if (err)
3028 			ret = err;
3029 		route++;
3030 	}
3031 	mutex_unlock(&dapm->card->dapm_mutex);
3032 
3033 	return ret;
3034 }
3035 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3036 
3037 /**
3038  * snd_soc_dapm_new_widgets - add new dapm widgets
3039  * @card: card to be checked for new dapm widgets
3040  *
3041  * Checks the codec for any new dapm widgets and creates them if found.
3042  *
3043  * Returns 0 for success.
3044  */
3045 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3046 {
3047 	struct snd_soc_dapm_widget *w;
3048 	unsigned int val;
3049 
3050 	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3051 
3052 	list_for_each_entry(w, &card->widgets, list)
3053 	{
3054 		if (w->new)
3055 			continue;
3056 
3057 		if (w->num_kcontrols) {
3058 			w->kcontrols = kcalloc(w->num_kcontrols,
3059 						sizeof(struct snd_kcontrol *),
3060 						GFP_KERNEL);
3061 			if (!w->kcontrols) {
3062 				mutex_unlock(&card->dapm_mutex);
3063 				return -ENOMEM;
3064 			}
3065 		}
3066 
3067 		switch(w->id) {
3068 		case snd_soc_dapm_switch:
3069 		case snd_soc_dapm_mixer:
3070 		case snd_soc_dapm_mixer_named_ctl:
3071 			dapm_new_mixer(w);
3072 			break;
3073 		case snd_soc_dapm_mux:
3074 		case snd_soc_dapm_demux:
3075 			dapm_new_mux(w);
3076 			break;
3077 		case snd_soc_dapm_pga:
3078 		case snd_soc_dapm_out_drv:
3079 			dapm_new_pga(w);
3080 			break;
3081 		case snd_soc_dapm_dai_link:
3082 			dapm_new_dai_link(w);
3083 			break;
3084 		default:
3085 			break;
3086 		}
3087 
3088 		/* Read the initial power state from the device */
3089 		if (w->reg >= 0) {
3090 			soc_dapm_read(w->dapm, w->reg, &val);
3091 			val = val >> w->shift;
3092 			val &= w->mask;
3093 			if (val == w->on_val)
3094 				w->power = 1;
3095 		}
3096 
3097 		w->new = 1;
3098 
3099 		dapm_mark_dirty(w, "new widget");
3100 		dapm_debugfs_add_widget(w);
3101 	}
3102 
3103 	dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3104 	mutex_unlock(&card->dapm_mutex);
3105 	return 0;
3106 }
3107 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3108 
3109 /**
3110  * snd_soc_dapm_get_volsw - dapm mixer get callback
3111  * @kcontrol: mixer control
3112  * @ucontrol: control element information
3113  *
3114  * Callback to get the value of a dapm mixer control.
3115  *
3116  * Returns 0 for success.
3117  */
3118 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3119 	struct snd_ctl_elem_value *ucontrol)
3120 {
3121 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3122 	struct snd_soc_card *card = dapm->card;
3123 	struct soc_mixer_control *mc =
3124 		(struct soc_mixer_control *)kcontrol->private_value;
3125 	int reg = mc->reg;
3126 	unsigned int shift = mc->shift;
3127 	int max = mc->max;
3128 	unsigned int width = fls(max);
3129 	unsigned int mask = (1 << fls(max)) - 1;
3130 	unsigned int invert = mc->invert;
3131 	unsigned int reg_val, val, rval = 0;
3132 	int ret = 0;
3133 
3134 	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3135 	if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3136 		ret = soc_dapm_read(dapm, reg, &reg_val);
3137 		val = (reg_val >> shift) & mask;
3138 
3139 		if (ret == 0 && reg != mc->rreg)
3140 			ret = soc_dapm_read(dapm, mc->rreg, &reg_val);
3141 
3142 		if (snd_soc_volsw_is_stereo(mc))
3143 			rval = (reg_val >> mc->rshift) & mask;
3144 	} else {
3145 		reg_val = dapm_kcontrol_get_value(kcontrol);
3146 		val = reg_val & mask;
3147 
3148 		if (snd_soc_volsw_is_stereo(mc))
3149 			rval = (reg_val >> width) & mask;
3150 	}
3151 	mutex_unlock(&card->dapm_mutex);
3152 
3153 	if (ret)
3154 		return ret;
3155 
3156 	if (invert)
3157 		ucontrol->value.integer.value[0] = max - val;
3158 	else
3159 		ucontrol->value.integer.value[0] = val;
3160 
3161 	if (snd_soc_volsw_is_stereo(mc)) {
3162 		if (invert)
3163 			ucontrol->value.integer.value[1] = max - rval;
3164 		else
3165 			ucontrol->value.integer.value[1] = rval;
3166 	}
3167 
3168 	return ret;
3169 }
3170 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3171 
3172 /**
3173  * snd_soc_dapm_put_volsw - dapm mixer set callback
3174  * @kcontrol: mixer control
3175  * @ucontrol: control element information
3176  *
3177  * Callback to set the value of a dapm mixer control.
3178  *
3179  * Returns 0 for success.
3180  */
3181 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3182 	struct snd_ctl_elem_value *ucontrol)
3183 {
3184 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3185 	struct snd_soc_card *card = dapm->card;
3186 	struct soc_mixer_control *mc =
3187 		(struct soc_mixer_control *)kcontrol->private_value;
3188 	int reg = mc->reg;
3189 	unsigned int shift = mc->shift;
3190 	int max = mc->max;
3191 	unsigned int width = fls(max);
3192 	unsigned int mask = (1 << width) - 1;
3193 	unsigned int invert = mc->invert;
3194 	unsigned int val, rval = 0;
3195 	int connect, rconnect = -1, change, reg_change = 0;
3196 	struct snd_soc_dapm_update update = {};
3197 	int ret = 0;
3198 
3199 	val = (ucontrol->value.integer.value[0] & mask);
3200 	connect = !!val;
3201 
3202 	if (invert)
3203 		val = max - val;
3204 
3205 	if (snd_soc_volsw_is_stereo(mc)) {
3206 		rval = (ucontrol->value.integer.value[1] & mask);
3207 		rconnect = !!rval;
3208 		if (invert)
3209 			rval = max - rval;
3210 	}
3211 
3212 	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3213 
3214 	/* This assumes field width < (bits in unsigned int / 2) */
3215 	if (width > sizeof(unsigned int) * 8 / 2)
3216 		dev_warn(dapm->dev,
3217 			 "ASoC: control %s field width limit exceeded\n",
3218 			 kcontrol->id.name);
3219 	change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3220 
3221 	if (reg != SND_SOC_NOPM) {
3222 		val = val << shift;
3223 		rval = rval << mc->rshift;
3224 
3225 		reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3226 
3227 		if (snd_soc_volsw_is_stereo(mc))
3228 			reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3229 							 mask << mc->rshift,
3230 							 rval);
3231 	}
3232 
3233 	if (change || reg_change) {
3234 		if (reg_change) {
3235 			if (snd_soc_volsw_is_stereo(mc)) {
3236 				update.has_second_set = true;
3237 				update.reg2 = mc->rreg;
3238 				update.mask2 = mask << mc->rshift;
3239 				update.val2 = rval;
3240 			}
3241 			update.kcontrol = kcontrol;
3242 			update.reg = reg;
3243 			update.mask = mask << shift;
3244 			update.val = val;
3245 			card->update = &update;
3246 		}
3247 		change |= reg_change;
3248 
3249 		ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3250 						  rconnect);
3251 
3252 		card->update = NULL;
3253 	}
3254 
3255 	mutex_unlock(&card->dapm_mutex);
3256 
3257 	if (ret > 0)
3258 		soc_dpcm_runtime_update(card);
3259 
3260 	return change;
3261 }
3262 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3263 
3264 /**
3265  * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3266  * @kcontrol: mixer control
3267  * @ucontrol: control element information
3268  *
3269  * Callback to get the value of a dapm enumerated double mixer control.
3270  *
3271  * Returns 0 for success.
3272  */
3273 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3274 	struct snd_ctl_elem_value *ucontrol)
3275 {
3276 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3277 	struct snd_soc_card *card = dapm->card;
3278 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3279 	unsigned int reg_val, val;
3280 
3281 	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3282 	if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3283 		int ret = soc_dapm_read(dapm, e->reg, &reg_val);
3284 		if (ret) {
3285 			mutex_unlock(&card->dapm_mutex);
3286 			return ret;
3287 		}
3288 	} else {
3289 		reg_val = dapm_kcontrol_get_value(kcontrol);
3290 	}
3291 	mutex_unlock(&card->dapm_mutex);
3292 
3293 	val = (reg_val >> e->shift_l) & e->mask;
3294 	ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3295 	if (e->shift_l != e->shift_r) {
3296 		val = (reg_val >> e->shift_r) & e->mask;
3297 		val = snd_soc_enum_val_to_item(e, val);
3298 		ucontrol->value.enumerated.item[1] = val;
3299 	}
3300 
3301 	return 0;
3302 }
3303 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3304 
3305 /**
3306  * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3307  * @kcontrol: mixer control
3308  * @ucontrol: control element information
3309  *
3310  * Callback to set the value of a dapm enumerated double mixer control.
3311  *
3312  * Returns 0 for success.
3313  */
3314 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3315 	struct snd_ctl_elem_value *ucontrol)
3316 {
3317 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3318 	struct snd_soc_card *card = dapm->card;
3319 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3320 	unsigned int *item = ucontrol->value.enumerated.item;
3321 	unsigned int val, change, reg_change = 0;
3322 	unsigned int mask;
3323 	struct snd_soc_dapm_update update = {};
3324 	int ret = 0;
3325 
3326 	if (item[0] >= e->items)
3327 		return -EINVAL;
3328 
3329 	val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3330 	mask = e->mask << e->shift_l;
3331 	if (e->shift_l != e->shift_r) {
3332 		if (item[1] > e->items)
3333 			return -EINVAL;
3334 		val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3335 		mask |= e->mask << e->shift_r;
3336 	}
3337 
3338 	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3339 
3340 	change = dapm_kcontrol_set_value(kcontrol, val);
3341 
3342 	if (e->reg != SND_SOC_NOPM)
3343 		reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3344 
3345 	if (change || reg_change) {
3346 		if (reg_change) {
3347 			update.kcontrol = kcontrol;
3348 			update.reg = e->reg;
3349 			update.mask = mask;
3350 			update.val = val;
3351 			card->update = &update;
3352 		}
3353 		change |= reg_change;
3354 
3355 		ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3356 
3357 		card->update = NULL;
3358 	}
3359 
3360 	mutex_unlock(&card->dapm_mutex);
3361 
3362 	if (ret > 0)
3363 		soc_dpcm_runtime_update(card);
3364 
3365 	return change;
3366 }
3367 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3368 
3369 /**
3370  * snd_soc_dapm_info_pin_switch - Info for a pin switch
3371  *
3372  * @kcontrol: mixer control
3373  * @uinfo: control element information
3374  *
3375  * Callback to provide information about a pin switch control.
3376  */
3377 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3378 				 struct snd_ctl_elem_info *uinfo)
3379 {
3380 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3381 	uinfo->count = 1;
3382 	uinfo->value.integer.min = 0;
3383 	uinfo->value.integer.max = 1;
3384 
3385 	return 0;
3386 }
3387 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3388 
3389 /**
3390  * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3391  *
3392  * @kcontrol: mixer control
3393  * @ucontrol: Value
3394  */
3395 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3396 				struct snd_ctl_elem_value *ucontrol)
3397 {
3398 	struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3399 	const char *pin = (const char *)kcontrol->private_value;
3400 
3401 	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3402 
3403 	ucontrol->value.integer.value[0] =
3404 		snd_soc_dapm_get_pin_status(&card->dapm, pin);
3405 
3406 	mutex_unlock(&card->dapm_mutex);
3407 
3408 	return 0;
3409 }
3410 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3411 
3412 /**
3413  * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3414  *
3415  * @kcontrol: mixer control
3416  * @ucontrol: Value
3417  */
3418 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3419 				struct snd_ctl_elem_value *ucontrol)
3420 {
3421 	struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3422 	const char *pin = (const char *)kcontrol->private_value;
3423 
3424 	if (ucontrol->value.integer.value[0])
3425 		snd_soc_dapm_enable_pin(&card->dapm, pin);
3426 	else
3427 		snd_soc_dapm_disable_pin(&card->dapm, pin);
3428 
3429 	snd_soc_dapm_sync(&card->dapm);
3430 	return 0;
3431 }
3432 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3433 
3434 struct snd_soc_dapm_widget *
3435 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3436 	const struct snd_soc_dapm_widget *widget)
3437 {
3438 	struct snd_soc_dapm_widget *w;
3439 
3440 	mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3441 	w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3442 	/* Do not nag about probe deferrals */
3443 	if (IS_ERR(w)) {
3444 		int ret = PTR_ERR(w);
3445 
3446 		if (ret != -EPROBE_DEFER)
3447 			dev_err(dapm->dev,
3448 				"ASoC: Failed to create DAPM control %s (%d)\n",
3449 				widget->name, ret);
3450 		goto out_unlock;
3451 	}
3452 	if (!w)
3453 		dev_err(dapm->dev,
3454 			"ASoC: Failed to create DAPM control %s\n",
3455 			widget->name);
3456 
3457 out_unlock:
3458 	mutex_unlock(&dapm->card->dapm_mutex);
3459 	return w;
3460 }
3461 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3462 
3463 struct snd_soc_dapm_widget *
3464 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3465 			 const struct snd_soc_dapm_widget *widget)
3466 {
3467 	enum snd_soc_dapm_direction dir;
3468 	struct snd_soc_dapm_widget *w;
3469 	const char *prefix;
3470 	int ret;
3471 
3472 	if ((w = dapm_cnew_widget(widget)) == NULL)
3473 		return NULL;
3474 
3475 	switch (w->id) {
3476 	case snd_soc_dapm_regulator_supply:
3477 		w->regulator = devm_regulator_get(dapm->dev, w->name);
3478 		if (IS_ERR(w->regulator)) {
3479 			ret = PTR_ERR(w->regulator);
3480 			if (ret == -EPROBE_DEFER)
3481 				return ERR_PTR(ret);
3482 			dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3483 				w->name, ret);
3484 			return NULL;
3485 		}
3486 
3487 		if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3488 			ret = regulator_allow_bypass(w->regulator, true);
3489 			if (ret != 0)
3490 				dev_warn(w->dapm->dev,
3491 					 "ASoC: Failed to bypass %s: %d\n",
3492 					 w->name, ret);
3493 		}
3494 		break;
3495 	case snd_soc_dapm_pinctrl:
3496 		w->pinctrl = devm_pinctrl_get(dapm->dev);
3497 		if (IS_ERR_OR_NULL(w->pinctrl)) {
3498 			ret = PTR_ERR(w->pinctrl);
3499 			if (ret == -EPROBE_DEFER)
3500 				return ERR_PTR(ret);
3501 			dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3502 				w->name, ret);
3503 			return NULL;
3504 		}
3505 		break;
3506 	case snd_soc_dapm_clock_supply:
3507 #ifdef CONFIG_CLKDEV_LOOKUP
3508 		w->clk = devm_clk_get(dapm->dev, w->name);
3509 		if (IS_ERR(w->clk)) {
3510 			ret = PTR_ERR(w->clk);
3511 			if (ret == -EPROBE_DEFER)
3512 				return ERR_PTR(ret);
3513 			dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3514 				w->name, ret);
3515 			return NULL;
3516 		}
3517 #else
3518 		return NULL;
3519 #endif
3520 		break;
3521 	default:
3522 		break;
3523 	}
3524 
3525 	prefix = soc_dapm_prefix(dapm);
3526 	if (prefix)
3527 		w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3528 	else
3529 		w->name = kstrdup_const(widget->name, GFP_KERNEL);
3530 	if (w->name == NULL) {
3531 		kfree(w);
3532 		return NULL;
3533 	}
3534 
3535 	switch (w->id) {
3536 	case snd_soc_dapm_mic:
3537 		w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3538 		w->power_check = dapm_generic_check_power;
3539 		break;
3540 	case snd_soc_dapm_input:
3541 		if (!dapm->card->fully_routed)
3542 			w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3543 		w->power_check = dapm_generic_check_power;
3544 		break;
3545 	case snd_soc_dapm_spk:
3546 	case snd_soc_dapm_hp:
3547 		w->is_ep = SND_SOC_DAPM_EP_SINK;
3548 		w->power_check = dapm_generic_check_power;
3549 		break;
3550 	case snd_soc_dapm_output:
3551 		if (!dapm->card->fully_routed)
3552 			w->is_ep = SND_SOC_DAPM_EP_SINK;
3553 		w->power_check = dapm_generic_check_power;
3554 		break;
3555 	case snd_soc_dapm_vmid:
3556 	case snd_soc_dapm_siggen:
3557 		w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3558 		w->power_check = dapm_always_on_check_power;
3559 		break;
3560 	case snd_soc_dapm_sink:
3561 		w->is_ep = SND_SOC_DAPM_EP_SINK;
3562 		w->power_check = dapm_always_on_check_power;
3563 		break;
3564 
3565 	case snd_soc_dapm_mux:
3566 	case snd_soc_dapm_demux:
3567 	case snd_soc_dapm_switch:
3568 	case snd_soc_dapm_mixer:
3569 	case snd_soc_dapm_mixer_named_ctl:
3570 	case snd_soc_dapm_adc:
3571 	case snd_soc_dapm_aif_out:
3572 	case snd_soc_dapm_dac:
3573 	case snd_soc_dapm_aif_in:
3574 	case snd_soc_dapm_pga:
3575 	case snd_soc_dapm_out_drv:
3576 	case snd_soc_dapm_micbias:
3577 	case snd_soc_dapm_line:
3578 	case snd_soc_dapm_dai_link:
3579 	case snd_soc_dapm_dai_out:
3580 	case snd_soc_dapm_dai_in:
3581 		w->power_check = dapm_generic_check_power;
3582 		break;
3583 	case snd_soc_dapm_supply:
3584 	case snd_soc_dapm_regulator_supply:
3585 	case snd_soc_dapm_pinctrl:
3586 	case snd_soc_dapm_clock_supply:
3587 	case snd_soc_dapm_kcontrol:
3588 		w->is_supply = 1;
3589 		w->power_check = dapm_supply_check_power;
3590 		break;
3591 	default:
3592 		w->power_check = dapm_always_on_check_power;
3593 		break;
3594 	}
3595 
3596 	w->dapm = dapm;
3597 	INIT_LIST_HEAD(&w->list);
3598 	INIT_LIST_HEAD(&w->dirty);
3599 	list_add_tail(&w->list, &dapm->card->widgets);
3600 
3601 	snd_soc_dapm_for_each_direction(dir) {
3602 		INIT_LIST_HEAD(&w->edges[dir]);
3603 		w->endpoints[dir] = -1;
3604 	}
3605 
3606 	/* machine layer sets up unconnected pins and insertions */
3607 	w->connected = 1;
3608 	return w;
3609 }
3610 
3611 /**
3612  * snd_soc_dapm_new_controls - create new dapm controls
3613  * @dapm: DAPM context
3614  * @widget: widget array
3615  * @num: number of widgets
3616  *
3617  * Creates new DAPM controls based upon the templates.
3618  *
3619  * Returns 0 for success else error.
3620  */
3621 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3622 	const struct snd_soc_dapm_widget *widget,
3623 	int num)
3624 {
3625 	struct snd_soc_dapm_widget *w;
3626 	int i;
3627 	int ret = 0;
3628 
3629 	mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3630 	for (i = 0; i < num; i++) {
3631 		w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3632 		if (IS_ERR(w)) {
3633 			ret = PTR_ERR(w);
3634 			/* Do not nag about probe deferrals */
3635 			if (ret == -EPROBE_DEFER)
3636 				break;
3637 			dev_err(dapm->dev,
3638 				"ASoC: Failed to create DAPM control %s (%d)\n",
3639 				widget->name, ret);
3640 			break;
3641 		}
3642 		if (!w) {
3643 			dev_err(dapm->dev,
3644 				"ASoC: Failed to create DAPM control %s\n",
3645 				widget->name);
3646 			ret = -ENOMEM;
3647 			break;
3648 		}
3649 		widget++;
3650 	}
3651 	mutex_unlock(&dapm->card->dapm_mutex);
3652 	return ret;
3653 }
3654 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3655 
3656 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3657 				  struct snd_kcontrol *kcontrol, int event)
3658 {
3659 	struct snd_soc_dapm_path *source_p, *sink_p;
3660 	struct snd_soc_dai *source, *sink;
3661 	const struct snd_soc_pcm_stream *config = w->params + w->params_select;
3662 	struct snd_pcm_substream substream;
3663 	struct snd_pcm_hw_params *params = NULL;
3664 	struct snd_pcm_runtime *runtime = NULL;
3665 	u64 fmt;
3666 	int ret;
3667 
3668 	if (WARN_ON(!config) ||
3669 	    WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3670 		    list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3671 		return -EINVAL;
3672 
3673 	/* We only support a single source and sink, pick the first */
3674 	source_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_OUT],
3675 				    struct snd_soc_dapm_path,
3676 				    list_node[SND_SOC_DAPM_DIR_OUT]);
3677 	sink_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_IN],
3678 				    struct snd_soc_dapm_path,
3679 				    list_node[SND_SOC_DAPM_DIR_IN]);
3680 
3681 	source = source_p->source->priv;
3682 	sink = sink_p->sink->priv;
3683 
3684 	/* Be a little careful as we don't want to overflow the mask array */
3685 	if (config->formats) {
3686 		fmt = ffs(config->formats) - 1;
3687 	} else {
3688 		dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3689 			 config->formats);
3690 		fmt = 0;
3691 	}
3692 
3693 	/* Currently very limited parameter selection */
3694 	params = kzalloc(sizeof(*params), GFP_KERNEL);
3695 	if (!params) {
3696 		ret = -ENOMEM;
3697 		goto out;
3698 	}
3699 	snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3700 
3701 	hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3702 		config->rate_min;
3703 	hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3704 		config->rate_max;
3705 
3706 	hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3707 		= config->channels_min;
3708 	hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3709 		= config->channels_max;
3710 
3711 	memset(&substream, 0, sizeof(substream));
3712 
3713 	/* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3714 	runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3715 	if (!runtime) {
3716 		ret = -ENOMEM;
3717 		goto out;
3718 	}
3719 	substream.runtime = runtime;
3720 
3721 	switch (event) {
3722 	case SND_SOC_DAPM_PRE_PMU:
3723 		substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3724 		if (source->driver->ops->startup) {
3725 			ret = source->driver->ops->startup(&substream, source);
3726 			if (ret < 0) {
3727 				dev_err(source->dev,
3728 					"ASoC: startup() failed: %d\n", ret);
3729 				goto out;
3730 			}
3731 			source->active++;
3732 		}
3733 		ret = soc_dai_hw_params(&substream, params, source);
3734 		if (ret < 0)
3735 			goto out;
3736 
3737 		substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3738 		if (sink->driver->ops->startup) {
3739 			ret = sink->driver->ops->startup(&substream, sink);
3740 			if (ret < 0) {
3741 				dev_err(sink->dev,
3742 					"ASoC: startup() failed: %d\n", ret);
3743 				goto out;
3744 			}
3745 			sink->active++;
3746 		}
3747 		ret = soc_dai_hw_params(&substream, params, sink);
3748 		if (ret < 0)
3749 			goto out;
3750 		break;
3751 
3752 	case SND_SOC_DAPM_POST_PMU:
3753 		ret = snd_soc_dai_digital_mute(sink, 0,
3754 					       SNDRV_PCM_STREAM_PLAYBACK);
3755 		if (ret != 0 && ret != -ENOTSUPP)
3756 			dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3757 		ret = 0;
3758 		break;
3759 
3760 	case SND_SOC_DAPM_PRE_PMD:
3761 		ret = snd_soc_dai_digital_mute(sink, 1,
3762 					       SNDRV_PCM_STREAM_PLAYBACK);
3763 		if (ret != 0 && ret != -ENOTSUPP)
3764 			dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3765 		ret = 0;
3766 
3767 		source->active--;
3768 		if (source->driver->ops->shutdown) {
3769 			substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3770 			source->driver->ops->shutdown(&substream, source);
3771 		}
3772 
3773 		sink->active--;
3774 		if (sink->driver->ops->shutdown) {
3775 			substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3776 			sink->driver->ops->shutdown(&substream, sink);
3777 		}
3778 		break;
3779 
3780 	default:
3781 		WARN(1, "Unknown event %d\n", event);
3782 		ret = -EINVAL;
3783 	}
3784 
3785 out:
3786 	kfree(runtime);
3787 	kfree(params);
3788 	return ret;
3789 }
3790 
3791 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3792 			  struct snd_ctl_elem_value *ucontrol)
3793 {
3794 	struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3795 
3796 	ucontrol->value.enumerated.item[0] = w->params_select;
3797 
3798 	return 0;
3799 }
3800 
3801 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3802 			  struct snd_ctl_elem_value *ucontrol)
3803 {
3804 	struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3805 
3806 	/* Can't change the config when widget is already powered */
3807 	if (w->power)
3808 		return -EBUSY;
3809 
3810 	if (ucontrol->value.enumerated.item[0] == w->params_select)
3811 		return 0;
3812 
3813 	if (ucontrol->value.enumerated.item[0] >= w->num_params)
3814 		return -EINVAL;
3815 
3816 	w->params_select = ucontrol->value.enumerated.item[0];
3817 
3818 	return 0;
3819 }
3820 
3821 static void
3822 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
3823 			unsigned long *private_value,
3824 			int num_params,
3825 			const char **w_param_text)
3826 {
3827 	int count;
3828 
3829 	devm_kfree(card->dev, (void *)*private_value);
3830 	for (count = 0 ; count < num_params; count++)
3831 		devm_kfree(card->dev, (void *)w_param_text[count]);
3832 	devm_kfree(card->dev, w_param_text);
3833 }
3834 
3835 static struct snd_kcontrol_new *
3836 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
3837 			char *link_name,
3838 			const struct snd_soc_pcm_stream *params,
3839 			int num_params, const char **w_param_text,
3840 			unsigned long *private_value)
3841 {
3842 	struct soc_enum w_param_enum[] = {
3843 		SOC_ENUM_SINGLE(0, 0, 0, NULL),
3844 	};
3845 	struct snd_kcontrol_new kcontrol_dai_link[] = {
3846 		SOC_ENUM_EXT(NULL, w_param_enum[0],
3847 			     snd_soc_dapm_dai_link_get,
3848 			     snd_soc_dapm_dai_link_put),
3849 	};
3850 	struct snd_kcontrol_new *kcontrol_news;
3851 	const struct snd_soc_pcm_stream *config = params;
3852 	int count;
3853 
3854 	for (count = 0 ; count < num_params; count++) {
3855 		if (!config->stream_name) {
3856 			dev_warn(card->dapm.dev,
3857 				"ASoC: anonymous config %d for dai link %s\n",
3858 				count, link_name);
3859 			w_param_text[count] =
3860 				devm_kasprintf(card->dev, GFP_KERNEL,
3861 					       "Anonymous Configuration %d",
3862 					       count);
3863 		} else {
3864 			w_param_text[count] = devm_kmemdup(card->dev,
3865 						config->stream_name,
3866 						strlen(config->stream_name) + 1,
3867 						GFP_KERNEL);
3868 		}
3869 		if (!w_param_text[count])
3870 			goto outfree_w_param;
3871 		config++;
3872 	}
3873 
3874 	w_param_enum[0].items = num_params;
3875 	w_param_enum[0].texts = w_param_text;
3876 
3877 	*private_value =
3878 		(unsigned long) devm_kmemdup(card->dev,
3879 			(void *)(kcontrol_dai_link[0].private_value),
3880 			sizeof(struct soc_enum), GFP_KERNEL);
3881 	if (!*private_value) {
3882 		dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3883 			link_name);
3884 		goto outfree_w_param;
3885 	}
3886 	kcontrol_dai_link[0].private_value = *private_value;
3887 	/* duplicate kcontrol_dai_link on heap so that memory persists */
3888 	kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3889 					sizeof(struct snd_kcontrol_new),
3890 					GFP_KERNEL);
3891 	if (!kcontrol_news) {
3892 		dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3893 			link_name);
3894 		goto outfree_w_param;
3895 	}
3896 	return kcontrol_news;
3897 
3898 outfree_w_param:
3899 	snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
3900 	return NULL;
3901 }
3902 
3903 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3904 			 const struct snd_soc_pcm_stream *params,
3905 			 unsigned int num_params,
3906 			 struct snd_soc_dapm_widget *source,
3907 			 struct snd_soc_dapm_widget *sink)
3908 {
3909 	struct snd_soc_dapm_widget template;
3910 	struct snd_soc_dapm_widget *w;
3911 	const char **w_param_text;
3912 	unsigned long private_value;
3913 	char *link_name;
3914 	int ret;
3915 
3916 	link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3917 				   source->name, sink->name);
3918 	if (!link_name)
3919 		return -ENOMEM;
3920 
3921 	memset(&template, 0, sizeof(template));
3922 	template.reg = SND_SOC_NOPM;
3923 	template.id = snd_soc_dapm_dai_link;
3924 	template.name = link_name;
3925 	template.event = snd_soc_dai_link_event;
3926 	template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3927 		SND_SOC_DAPM_PRE_PMD;
3928 	template.kcontrol_news = NULL;
3929 
3930 	/* allocate memory for control, only in case of multiple configs */
3931 	if (num_params > 1) {
3932 		w_param_text = devm_kcalloc(card->dev, num_params,
3933 					sizeof(char *), GFP_KERNEL);
3934 		if (!w_param_text) {
3935 			ret = -ENOMEM;
3936 			goto param_fail;
3937 		}
3938 
3939 		template.num_kcontrols = 1;
3940 		template.kcontrol_news =
3941 					snd_soc_dapm_alloc_kcontrol(card,
3942 						link_name, params, num_params,
3943 						w_param_text, &private_value);
3944 		if (!template.kcontrol_news) {
3945 			ret = -ENOMEM;
3946 			goto param_fail;
3947 		}
3948 	} else {
3949 		w_param_text = NULL;
3950 	}
3951 	dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3952 
3953 	w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
3954 	if (IS_ERR(w)) {
3955 		ret = PTR_ERR(w);
3956 		/* Do not nag about probe deferrals */
3957 		if (ret != -EPROBE_DEFER)
3958 			dev_err(card->dev,
3959 				"ASoC: Failed to create %s widget (%d)\n",
3960 				link_name, ret);
3961 		goto outfree_kcontrol_news;
3962 	}
3963 	if (!w) {
3964 		dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3965 			link_name);
3966 		ret = -ENOMEM;
3967 		goto outfree_kcontrol_news;
3968 	}
3969 
3970 	w->params = params;
3971 	w->num_params = num_params;
3972 
3973 	ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3974 	if (ret)
3975 		goto outfree_w;
3976 	return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
3977 
3978 outfree_w:
3979 	devm_kfree(card->dev, w);
3980 outfree_kcontrol_news:
3981 	devm_kfree(card->dev, (void *)template.kcontrol_news);
3982 	snd_soc_dapm_free_kcontrol(card, &private_value, num_params, w_param_text);
3983 param_fail:
3984 	devm_kfree(card->dev, link_name);
3985 	return ret;
3986 }
3987 
3988 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3989 				 struct snd_soc_dai *dai)
3990 {
3991 	struct snd_soc_dapm_widget template;
3992 	struct snd_soc_dapm_widget *w;
3993 
3994 	WARN_ON(dapm->dev != dai->dev);
3995 
3996 	memset(&template, 0, sizeof(template));
3997 	template.reg = SND_SOC_NOPM;
3998 
3999 	if (dai->driver->playback.stream_name) {
4000 		template.id = snd_soc_dapm_dai_in;
4001 		template.name = dai->driver->playback.stream_name;
4002 		template.sname = dai->driver->playback.stream_name;
4003 
4004 		dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4005 			template.name);
4006 
4007 		w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4008 		if (IS_ERR(w)) {
4009 			int ret = PTR_ERR(w);
4010 
4011 			/* Do not nag about probe deferrals */
4012 			if (ret != -EPROBE_DEFER)
4013 				dev_err(dapm->dev,
4014 				"ASoC: Failed to create %s widget (%d)\n",
4015 				dai->driver->playback.stream_name, ret);
4016 			return ret;
4017 		}
4018 		if (!w) {
4019 			dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
4020 				dai->driver->playback.stream_name);
4021 			return -ENOMEM;
4022 		}
4023 
4024 		w->priv = dai;
4025 		dai->playback_widget = w;
4026 	}
4027 
4028 	if (dai->driver->capture.stream_name) {
4029 		template.id = snd_soc_dapm_dai_out;
4030 		template.name = dai->driver->capture.stream_name;
4031 		template.sname = dai->driver->capture.stream_name;
4032 
4033 		dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4034 			template.name);
4035 
4036 		w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4037 		if (IS_ERR(w)) {
4038 			int ret = PTR_ERR(w);
4039 
4040 			/* Do not nag about probe deferrals */
4041 			if (ret != -EPROBE_DEFER)
4042 				dev_err(dapm->dev,
4043 				"ASoC: Failed to create %s widget (%d)\n",
4044 				dai->driver->playback.stream_name, ret);
4045 			return ret;
4046 		}
4047 		if (!w) {
4048 			dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
4049 				dai->driver->capture.stream_name);
4050 			return -ENOMEM;
4051 		}
4052 
4053 		w->priv = dai;
4054 		dai->capture_widget = w;
4055 	}
4056 
4057 	return 0;
4058 }
4059 
4060 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4061 {
4062 	struct snd_soc_dapm_widget *dai_w, *w;
4063 	struct snd_soc_dapm_widget *src, *sink;
4064 	struct snd_soc_dai *dai;
4065 
4066 	/* For each DAI widget... */
4067 	list_for_each_entry(dai_w, &card->widgets, list) {
4068 		switch (dai_w->id) {
4069 		case snd_soc_dapm_dai_in:
4070 		case snd_soc_dapm_dai_out:
4071 			break;
4072 		default:
4073 			continue;
4074 		}
4075 
4076 		dai = dai_w->priv;
4077 
4078 		/* ...find all widgets with the same stream and link them */
4079 		list_for_each_entry(w, &card->widgets, list) {
4080 			if (w->dapm != dai_w->dapm)
4081 				continue;
4082 
4083 			switch (w->id) {
4084 			case snd_soc_dapm_dai_in:
4085 			case snd_soc_dapm_dai_out:
4086 				continue;
4087 			default:
4088 				break;
4089 			}
4090 
4091 			if (!w->sname || !strstr(w->sname, dai_w->sname))
4092 				continue;
4093 
4094 			if (dai_w->id == snd_soc_dapm_dai_in) {
4095 				src = dai_w;
4096 				sink = w;
4097 			} else {
4098 				src = w;
4099 				sink = dai_w;
4100 			}
4101 			dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4102 			snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4103 		}
4104 	}
4105 
4106 	return 0;
4107 }
4108 
4109 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4110 					  struct snd_soc_pcm_runtime *rtd)
4111 {
4112 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4113 	struct snd_soc_dapm_widget *sink, *source;
4114 	int i;
4115 
4116 	for (i = 0; i < rtd->num_codecs; i++) {
4117 		struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
4118 
4119 		/* connect BE DAI playback if widgets are valid */
4120 		if (codec_dai->playback_widget && cpu_dai->playback_widget) {
4121 			source = cpu_dai->playback_widget;
4122 			sink = codec_dai->playback_widget;
4123 			dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4124 				cpu_dai->component->name, source->name,
4125 				codec_dai->component->name, sink->name);
4126 
4127 			snd_soc_dapm_add_path(&card->dapm, source, sink,
4128 				NULL, NULL);
4129 		}
4130 
4131 		/* connect BE DAI capture if widgets are valid */
4132 		if (codec_dai->capture_widget && cpu_dai->capture_widget) {
4133 			source = codec_dai->capture_widget;
4134 			sink = cpu_dai->capture_widget;
4135 			dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4136 				codec_dai->component->name, source->name,
4137 				cpu_dai->component->name, sink->name);
4138 
4139 			snd_soc_dapm_add_path(&card->dapm, source, sink,
4140 				NULL, NULL);
4141 		}
4142 	}
4143 }
4144 
4145 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4146 	int event)
4147 {
4148 	struct snd_soc_dapm_widget *w;
4149 	unsigned int ep;
4150 
4151 	if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4152 		w = dai->playback_widget;
4153 	else
4154 		w = dai->capture_widget;
4155 
4156 	if (w) {
4157 		dapm_mark_dirty(w, "stream event");
4158 
4159 		if (w->id == snd_soc_dapm_dai_in) {
4160 			ep = SND_SOC_DAPM_EP_SOURCE;
4161 			dapm_widget_invalidate_input_paths(w);
4162 		} else {
4163 			ep = SND_SOC_DAPM_EP_SINK;
4164 			dapm_widget_invalidate_output_paths(w);
4165 		}
4166 
4167 		switch (event) {
4168 		case SND_SOC_DAPM_STREAM_START:
4169 			w->active = 1;
4170 			w->is_ep = ep;
4171 			break;
4172 		case SND_SOC_DAPM_STREAM_STOP:
4173 			w->active = 0;
4174 			w->is_ep = 0;
4175 			break;
4176 		case SND_SOC_DAPM_STREAM_SUSPEND:
4177 		case SND_SOC_DAPM_STREAM_RESUME:
4178 		case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4179 		case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4180 			break;
4181 		}
4182 	}
4183 }
4184 
4185 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4186 {
4187 	struct snd_soc_pcm_runtime *rtd;
4188 
4189 	/* for each BE DAI link... */
4190 	list_for_each_entry(rtd, &card->rtd_list, list)  {
4191 		/*
4192 		 * dynamic FE links have no fixed DAI mapping.
4193 		 * CODEC<->CODEC links have no direct connection.
4194 		 */
4195 		if (rtd->dai_link->dynamic || rtd->dai_link->params)
4196 			continue;
4197 
4198 		dapm_connect_dai_link_widgets(card, rtd);
4199 	}
4200 }
4201 
4202 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4203 	int event)
4204 {
4205 	int i;
4206 
4207 	soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
4208 	for (i = 0; i < rtd->num_codecs; i++)
4209 		soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
4210 
4211 	dapm_power_widgets(rtd->card, event);
4212 }
4213 
4214 /**
4215  * snd_soc_dapm_stream_event - send a stream event to the dapm core
4216  * @rtd: PCM runtime data
4217  * @stream: stream name
4218  * @event: stream event
4219  *
4220  * Sends a stream event to the dapm core. The core then makes any
4221  * necessary widget power changes.
4222  *
4223  * Returns 0 for success else error.
4224  */
4225 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4226 			      int event)
4227 {
4228 	struct snd_soc_card *card = rtd->card;
4229 
4230 	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4231 	soc_dapm_stream_event(rtd, stream, event);
4232 	mutex_unlock(&card->dapm_mutex);
4233 }
4234 
4235 /**
4236  * snd_soc_dapm_enable_pin_unlocked - enable pin.
4237  * @dapm: DAPM context
4238  * @pin: pin name
4239  *
4240  * Enables input/output pin and its parents or children widgets iff there is
4241  * a valid audio route and active audio stream.
4242  *
4243  * Requires external locking.
4244  *
4245  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4246  * do any widget power switching.
4247  */
4248 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4249 				   const char *pin)
4250 {
4251 	return snd_soc_dapm_set_pin(dapm, pin, 1);
4252 }
4253 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4254 
4255 /**
4256  * snd_soc_dapm_enable_pin - enable pin.
4257  * @dapm: DAPM context
4258  * @pin: pin name
4259  *
4260  * Enables input/output pin and its parents or children widgets iff there is
4261  * a valid audio route and active audio stream.
4262  *
4263  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4264  * do any widget power switching.
4265  */
4266 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4267 {
4268 	int ret;
4269 
4270 	mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4271 
4272 	ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4273 
4274 	mutex_unlock(&dapm->card->dapm_mutex);
4275 
4276 	return ret;
4277 }
4278 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4279 
4280 /**
4281  * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4282  * @dapm: DAPM context
4283  * @pin: pin name
4284  *
4285  * Enables input/output pin regardless of any other state.  This is
4286  * intended for use with microphone bias supplies used in microphone
4287  * jack detection.
4288  *
4289  * Requires external locking.
4290  *
4291  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4292  * do any widget power switching.
4293  */
4294 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4295 					 const char *pin)
4296 {
4297 	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4298 
4299 	if (!w) {
4300 		dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4301 		return -EINVAL;
4302 	}
4303 
4304 	dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4305 	if (!w->connected) {
4306 		/*
4307 		 * w->force does not affect the number of input or output paths,
4308 		 * so we only have to recheck if w->connected is changed
4309 		 */
4310 		dapm_widget_invalidate_input_paths(w);
4311 		dapm_widget_invalidate_output_paths(w);
4312 		w->connected = 1;
4313 	}
4314 	w->force = 1;
4315 	dapm_mark_dirty(w, "force enable");
4316 
4317 	return 0;
4318 }
4319 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4320 
4321 /**
4322  * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4323  * @dapm: DAPM context
4324  * @pin: pin name
4325  *
4326  * Enables input/output pin regardless of any other state.  This is
4327  * intended for use with microphone bias supplies used in microphone
4328  * jack detection.
4329  *
4330  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4331  * do any widget power switching.
4332  */
4333 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4334 				  const char *pin)
4335 {
4336 	int ret;
4337 
4338 	mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4339 
4340 	ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4341 
4342 	mutex_unlock(&dapm->card->dapm_mutex);
4343 
4344 	return ret;
4345 }
4346 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4347 
4348 /**
4349  * snd_soc_dapm_disable_pin_unlocked - disable pin.
4350  * @dapm: DAPM context
4351  * @pin: pin name
4352  *
4353  * Disables input/output pin and its parents or children widgets.
4354  *
4355  * Requires external locking.
4356  *
4357  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4358  * do any widget power switching.
4359  */
4360 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4361 				    const char *pin)
4362 {
4363 	return snd_soc_dapm_set_pin(dapm, pin, 0);
4364 }
4365 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4366 
4367 /**
4368  * snd_soc_dapm_disable_pin - disable pin.
4369  * @dapm: DAPM context
4370  * @pin: pin name
4371  *
4372  * Disables input/output pin and its parents or children widgets.
4373  *
4374  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4375  * do any widget power switching.
4376  */
4377 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4378 			     const char *pin)
4379 {
4380 	int ret;
4381 
4382 	mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4383 
4384 	ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4385 
4386 	mutex_unlock(&dapm->card->dapm_mutex);
4387 
4388 	return ret;
4389 }
4390 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4391 
4392 /**
4393  * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4394  * @dapm: DAPM context
4395  * @pin: pin name
4396  *
4397  * Marks the specified pin as being not connected, disabling it along
4398  * any parent or child widgets.  At present this is identical to
4399  * snd_soc_dapm_disable_pin() but in future it will be extended to do
4400  * additional things such as disabling controls which only affect
4401  * paths through the pin.
4402  *
4403  * Requires external locking.
4404  *
4405  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4406  * do any widget power switching.
4407  */
4408 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4409 			       const char *pin)
4410 {
4411 	return snd_soc_dapm_set_pin(dapm, pin, 0);
4412 }
4413 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4414 
4415 /**
4416  * snd_soc_dapm_nc_pin - permanently disable pin.
4417  * @dapm: DAPM context
4418  * @pin: pin name
4419  *
4420  * Marks the specified pin as being not connected, disabling it along
4421  * any parent or child widgets.  At present this is identical to
4422  * snd_soc_dapm_disable_pin() but in future it will be extended to do
4423  * additional things such as disabling controls which only affect
4424  * paths through the pin.
4425  *
4426  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4427  * do any widget power switching.
4428  */
4429 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4430 {
4431 	int ret;
4432 
4433 	mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4434 
4435 	ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4436 
4437 	mutex_unlock(&dapm->card->dapm_mutex);
4438 
4439 	return ret;
4440 }
4441 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4442 
4443 /**
4444  * snd_soc_dapm_get_pin_status - get audio pin status
4445  * @dapm: DAPM context
4446  * @pin: audio signal pin endpoint (or start point)
4447  *
4448  * Get audio pin status - connected or disconnected.
4449  *
4450  * Returns 1 for connected otherwise 0.
4451  */
4452 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4453 				const char *pin)
4454 {
4455 	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4456 
4457 	if (w)
4458 		return w->connected;
4459 
4460 	return 0;
4461 }
4462 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4463 
4464 /**
4465  * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4466  * @dapm: DAPM context
4467  * @pin: audio signal pin endpoint (or start point)
4468  *
4469  * Mark the given endpoint or pin as ignoring suspend.  When the
4470  * system is disabled a path between two endpoints flagged as ignoring
4471  * suspend will not be disabled.  The path must already be enabled via
4472  * normal means at suspend time, it will not be turned on if it was not
4473  * already enabled.
4474  */
4475 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4476 				const char *pin)
4477 {
4478 	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4479 
4480 	if (!w) {
4481 		dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4482 		return -EINVAL;
4483 	}
4484 
4485 	w->ignore_suspend = 1;
4486 
4487 	return 0;
4488 }
4489 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4490 
4491 /**
4492  * snd_soc_dapm_free - free dapm resources
4493  * @dapm: DAPM context
4494  *
4495  * Free all dapm widgets and resources.
4496  */
4497 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4498 {
4499 	dapm_debugfs_cleanup(dapm);
4500 	dapm_free_widgets(dapm);
4501 	list_del(&dapm->list);
4502 }
4503 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4504 
4505 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4506 {
4507 	struct snd_soc_card *card = dapm->card;
4508 	struct snd_soc_dapm_widget *w;
4509 	LIST_HEAD(down_list);
4510 	int powerdown = 0;
4511 
4512 	mutex_lock(&card->dapm_mutex);
4513 
4514 	list_for_each_entry(w, &dapm->card->widgets, list) {
4515 		if (w->dapm != dapm)
4516 			continue;
4517 		if (w->power) {
4518 			dapm_seq_insert(w, &down_list, false);
4519 			w->power = 0;
4520 			powerdown = 1;
4521 		}
4522 	}
4523 
4524 	/* If there were no widgets to power down we're already in
4525 	 * standby.
4526 	 */
4527 	if (powerdown) {
4528 		if (dapm->bias_level == SND_SOC_BIAS_ON)
4529 			snd_soc_dapm_set_bias_level(dapm,
4530 						    SND_SOC_BIAS_PREPARE);
4531 		dapm_seq_run(card, &down_list, 0, false);
4532 		if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4533 			snd_soc_dapm_set_bias_level(dapm,
4534 						    SND_SOC_BIAS_STANDBY);
4535 	}
4536 
4537 	mutex_unlock(&card->dapm_mutex);
4538 }
4539 
4540 /*
4541  * snd_soc_dapm_shutdown - callback for system shutdown
4542  */
4543 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4544 {
4545 	struct snd_soc_dapm_context *dapm;
4546 
4547 	list_for_each_entry(dapm, &card->dapm_list, list) {
4548 		if (dapm != &card->dapm) {
4549 			soc_dapm_shutdown_dapm(dapm);
4550 			if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4551 				snd_soc_dapm_set_bias_level(dapm,
4552 							    SND_SOC_BIAS_OFF);
4553 		}
4554 	}
4555 
4556 	soc_dapm_shutdown_dapm(&card->dapm);
4557 	if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4558 		snd_soc_dapm_set_bias_level(&card->dapm,
4559 					    SND_SOC_BIAS_OFF);
4560 }
4561 
4562 /* Module information */
4563 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4564 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4565 MODULE_LICENSE("GPL");
4566