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