xref: /linux/sound/soc/soc-dapm.c (revision c20313e98b04ce543936431b6122dd639d3a8346)
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 			case snd_soc_dapm_encoder:
1207 			case snd_soc_dapm_decoder:
1208 				wname_in_long_name = true;
1209 				kcname_in_long_name = true;
1210 				break;
1211 			case snd_soc_dapm_mux_named_ctl:
1212 			case snd_soc_dapm_mixer_named_ctl:
1213 				wname_in_long_name = false;
1214 				kcname_in_long_name = true;
1215 				break;
1216 			case snd_soc_dapm_demux:
1217 			case snd_soc_dapm_mux:
1218 				wname_in_long_name = true;
1219 				kcname_in_long_name = false;
1220 				break;
1221 			default:
1222 				return -EINVAL;
1223 			}
1224 		}
1225 		if (w->no_wname_in_kcontrol_name)
1226 			wname_in_long_name = false;
1227 
1228 		if (wname_in_long_name && kcname_in_long_name) {
1229 			/*
1230 			 * The control will get a prefix from the control
1231 			 * creation process but we're also using the same
1232 			 * prefix for widgets so cut the prefix off the
1233 			 * front of the widget name.
1234 			 */
1235 			long_name = kasprintf(GFP_KERNEL, "%s %s",
1236 				 w->name + prefix_len,
1237 				 w->kcontrol_news[kci].name);
1238 			if (long_name == NULL)
1239 				return -ENOMEM;
1240 
1241 			name = long_name;
1242 		} else if (wname_in_long_name) {
1243 			long_name = NULL;
1244 			name = w->name + prefix_len;
1245 		} else {
1246 			long_name = NULL;
1247 			name = w->kcontrol_news[kci].name;
1248 		}
1249 
1250 		kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
1251 					prefix);
1252 		if (!kcontrol) {
1253 			ret = -ENOMEM;
1254 			goto exit_free;
1255 		}
1256 
1257 		kcontrol->private_free = dapm_kcontrol_free;
1258 
1259 		ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
1260 		if (ret) {
1261 			snd_ctl_free_one(kcontrol);
1262 			goto exit_free;
1263 		}
1264 
1265 		ret = snd_ctl_add(card, kcontrol);
1266 		if (ret < 0) {
1267 			dev_err(dev,
1268 				"ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1269 				w->name, name, ret);
1270 			goto exit_free;
1271 		}
1272 	}
1273 
1274 	ret = dapm_kcontrol_add_widget(kcontrol, w);
1275 	if (ret == 0)
1276 		w->kcontrols[kci] = kcontrol;
1277 
1278 exit_free:
1279 	kfree(long_name);
1280 
1281 	return ret;
1282 }
1283 
1284 /* create new dapm mixer control */
1285 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
1286 {
1287 	int i, ret;
1288 	struct snd_soc_dapm_path *path;
1289 	struct dapm_kcontrol_data *data;
1290 
1291 	/* add kcontrol */
1292 	for (i = 0; i < w->num_kcontrols; i++) {
1293 		/* match name */
1294 		snd_soc_dapm_widget_for_each_source_path(w, path) {
1295 			/* mixer/mux paths name must match control name */
1296 			if (path->name != (char *)w->kcontrol_news[i].name)
1297 				continue;
1298 
1299 			if (!w->kcontrols[i]) {
1300 				ret = dapm_create_or_share_kcontrol(w, i);
1301 				if (ret < 0)
1302 					return ret;
1303 			}
1304 
1305 			dapm_kcontrol_add_path(w->kcontrols[i], path);
1306 
1307 			data = snd_kcontrol_chip(w->kcontrols[i]);
1308 			if (data->widget)
1309 				dapm_add_path(data->widget->dapm,
1310 					      data->widget,
1311 					      path->source,
1312 					      NULL, NULL);
1313 		}
1314 	}
1315 
1316 	return 0;
1317 }
1318 
1319 /* create new dapm mux control */
1320 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
1321 {
1322 	struct snd_soc_dapm_context *dapm = w->dapm;
1323 	struct device *dev = snd_soc_dapm_to_dev(dapm);
1324 	enum snd_soc_dapm_direction dir;
1325 	struct snd_soc_dapm_path *path;
1326 	const char *type;
1327 	int ret;
1328 
1329 	switch (w->id) {
1330 	case snd_soc_dapm_mux:
1331 	case snd_soc_dapm_mux_named_ctl:
1332 		dir = SND_SOC_DAPM_DIR_OUT;
1333 		type = "mux";
1334 		break;
1335 	case snd_soc_dapm_demux:
1336 		dir = SND_SOC_DAPM_DIR_IN;
1337 		type = "demux";
1338 		break;
1339 	default:
1340 		return -EINVAL;
1341 	}
1342 
1343 	if (w->num_kcontrols != 1) {
1344 		dev_err(dev,
1345 			"ASoC: %s %s has incorrect number of controls\n", type,
1346 			w->name);
1347 		return -EINVAL;
1348 	}
1349 
1350 	if (list_empty(&w->edges[dir])) {
1351 		dev_err(dev, "ASoC: %s %s has no paths\n", type, w->name);
1352 		return -EINVAL;
1353 	}
1354 
1355 	ret = dapm_create_or_share_kcontrol(w, 0);
1356 	if (ret < 0)
1357 		return ret;
1358 
1359 	snd_soc_dapm_widget_for_each_path(w, dir, path) {
1360 		if (path->name)
1361 			dapm_kcontrol_add_path(w->kcontrols[0], path);
1362 	}
1363 
1364 	return 0;
1365 }
1366 
1367 /* create new dapm volume control */
1368 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1369 {
1370 	int i;
1371 
1372 	for (i = 0; i < w->num_kcontrols; i++) {
1373 		int ret = dapm_create_or_share_kcontrol(w, i);
1374 		if (ret < 0)
1375 			return ret;
1376 	}
1377 
1378 	return 0;
1379 }
1380 
1381 /* create new dapm dai link control */
1382 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1383 {
1384 	int i;
1385 	struct snd_soc_pcm_runtime *rtd = w->priv;
1386 
1387 	/* create control for links with > 1 config */
1388 	if (rtd->dai_link->num_c2c_params <= 1)
1389 		return 0;
1390 
1391 	/* add kcontrol */
1392 	for (i = 0; i < w->num_kcontrols; i++) {
1393 		struct snd_soc_dapm_context *dapm = w->dapm;
1394 		struct device *dev = snd_soc_dapm_to_dev(dapm);
1395 		struct snd_card *card = dapm->card->snd_card;
1396 		struct snd_kcontrol *kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
1397 							     w, w->name, NULL);
1398 		int ret = snd_ctl_add(card, kcontrol);
1399 
1400 		if (ret < 0) {
1401 			dev_err(dev,
1402 				"ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1403 				w->name, w->kcontrol_news[i].name, ret);
1404 			return ret;
1405 		}
1406 		kcontrol->private_data = w;
1407 		w->kcontrols[i] = kcontrol;
1408 	}
1409 
1410 	return 0;
1411 }
1412 
1413 /* We implement power down on suspend by checking the power state of
1414  * the ALSA card - when we are suspending the ALSA state for the card
1415  * is set to D3.
1416  */
1417 static int dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1418 {
1419 	struct device *dev = snd_soc_dapm_to_dev(widget->dapm);
1420 	int level = snd_power_get_state(widget->dapm->card->snd_card);
1421 
1422 	switch (level) {
1423 	case SNDRV_CTL_POWER_D3hot:
1424 	case SNDRV_CTL_POWER_D3cold:
1425 		if (widget->ignore_suspend)
1426 			dev_dbg(dev, "ASoC: %s ignoring suspend\n",
1427 				widget->name);
1428 		return widget->ignore_suspend;
1429 	default:
1430 		return 1;
1431 	}
1432 }
1433 
1434 static void dapm_widget_list_free(struct snd_soc_dapm_widget_list **list)
1435 {
1436 	kfree(*list);
1437 }
1438 
1439 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1440 	struct list_head *widgets)
1441 {
1442 	struct snd_soc_dapm_widget *w;
1443 	struct list_head *it;
1444 	unsigned int size = 0;
1445 	unsigned int i = 0;
1446 
1447 	list_for_each(it, widgets)
1448 		size++;
1449 
1450 	*list = kzalloc_flex(**list, widgets, size);
1451 	if (*list == NULL)
1452 		return -ENOMEM;
1453 
1454 	(*list)->num_widgets = size;
1455 
1456 	list_for_each_entry(w, widgets, work_list)
1457 		(*list)->widgets[i++] = w;
1458 
1459 	(*list)->num_widgets = i;
1460 
1461 	return 0;
1462 }
1463 
1464 /*
1465  * Recursively reset the cached number of inputs or outputs for the specified
1466  * widget and all widgets that can be reached via incoming or outcoming paths
1467  * from the widget.
1468  */
1469 static void dapm_invalidate_paths_ep(struct snd_soc_dapm_widget *widget,
1470 	enum snd_soc_dapm_direction dir)
1471 {
1472 	enum snd_soc_dapm_direction rdir = DAPM_DIR_REVERSE(dir);
1473 	struct snd_soc_dapm_path *path;
1474 
1475 	widget->endpoints[dir] = -1;
1476 
1477 	snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1478 		if (path->is_supply)
1479 			continue;
1480 
1481 		if (path->walking)
1482 			return;
1483 
1484 		if (path->connect) {
1485 			path->walking = 1;
1486 			dapm_invalidate_paths_ep(path->node[dir], dir);
1487 			path->walking = 0;
1488 		}
1489 	}
1490 }
1491 
1492 /*
1493  * Common implementation for is_connected_output_ep() and
1494  * is_connected_input_ep(). The function is inlined since the combined size of
1495  * the two specialized functions is only marginally larger then the size of the
1496  * generic function and at the same time the fast path of the specialized
1497  * functions is significantly smaller than the generic function.
1498  */
1499 static __always_inline int dapm_is_connected_ep(struct snd_soc_dapm_widget *widget,
1500 	struct list_head *list, enum snd_soc_dapm_direction dir,
1501 	int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1502 		  bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1503 						enum snd_soc_dapm_direction)),
1504 	bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1505 				      enum snd_soc_dapm_direction))
1506 {
1507 	enum snd_soc_dapm_direction rdir = DAPM_DIR_REVERSE(dir);
1508 	struct snd_soc_dapm_path *path;
1509 	int con = 0;
1510 
1511 	if (widget->endpoints[dir] >= 0)
1512 		return widget->endpoints[dir];
1513 
1514 	DAPM_UPDATE_STAT(widget, path_checks);
1515 
1516 	/* do we need to add this widget to the list ? */
1517 	if (list)
1518 		list_add_tail(&widget->work_list, list);
1519 
1520 	if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1521 		list = NULL;
1522 		custom_stop_condition = NULL;
1523 	}
1524 
1525 	if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1526 		widget->endpoints[dir] = dapm_suspend_check(widget);
1527 		return widget->endpoints[dir];
1528 	}
1529 
1530 	snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1531 		DAPM_UPDATE_STAT(widget, neighbour_checks);
1532 
1533 		if (path->is_supply)
1534 			continue;
1535 
1536 		if (path->walking)
1537 			return 1;
1538 
1539 		trace_snd_soc_dapm_path(widget, dir, path);
1540 
1541 		if (path->connect) {
1542 			path->walking = 1;
1543 			con += fn(path->node[dir], list, custom_stop_condition);
1544 			path->walking = 0;
1545 		}
1546 	}
1547 
1548 	widget->endpoints[dir] = con;
1549 
1550 	return con;
1551 }
1552 
1553 /*
1554  * Recursively check for a completed path to an active or physically connected
1555  * output widget. Returns number of complete paths.
1556  *
1557  * Optionally, can be supplied with a function acting as a stopping condition.
1558  * This function takes the dapm widget currently being examined and the walk
1559  * direction as an arguments, it should return true if widgets from that point
1560  * in the graph onwards should not be added to the widget list.
1561  */
1562 static int dapm_is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1563 	struct list_head *list,
1564 	bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1565 				      enum snd_soc_dapm_direction))
1566 {
1567 	return dapm_is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1568 			dapm_is_connected_output_ep, custom_stop_condition);
1569 }
1570 
1571 /*
1572  * Recursively check for a completed path to an active or physically connected
1573  * input widget. Returns number of complete paths.
1574  *
1575  * Optionally, can be supplied with a function acting as a stopping condition.
1576  * This function takes the dapm widget currently being examined and the walk
1577  * direction as an arguments, it should return true if the walk should be
1578  * stopped and false otherwise.
1579  */
1580 static int dapm_is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1581 	struct list_head *list,
1582 	bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1583 				      enum snd_soc_dapm_direction))
1584 {
1585 	return dapm_is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1586 			dapm_is_connected_input_ep, custom_stop_condition);
1587 }
1588 
1589 /**
1590  * snd_soc_dapm_dai_get_connected_widgets - query audio path and it's widgets.
1591  * @dai: the soc DAI.
1592  * @stream: stream direction.
1593  * @list: list of active widgets for this stream.
1594  * @custom_stop_condition: (optional) a function meant to stop the widget graph
1595  *                         walk based on custom logic.
1596  *
1597  * Queries DAPM graph as to whether a valid audio stream path exists for
1598  * the initial stream specified by name. This takes into account
1599  * current mixer and mux kcontrol settings. Creates list of valid widgets.
1600  *
1601  * Optionally, can be supplied with a function acting as a stopping condition.
1602  * This function takes the dapm widget currently being examined and the walk
1603  * direction as an arguments, it should return true if the walk should be
1604  * stopped and false otherwise.
1605  *
1606  * Returns the number of valid paths or negative error.
1607  */
1608 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1609 	struct snd_soc_dapm_widget_list **list,
1610 	bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1611 				      enum snd_soc_dapm_direction))
1612 {
1613 	struct snd_soc_card *card = dai->component->card;
1614 	struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(dai, stream);
1615 	LIST_HEAD(widgets);
1616 	int paths;
1617 	int ret;
1618 
1619 	snd_soc_dapm_mutex_lock(card);
1620 
1621 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1622 		dapm_invalidate_paths_ep(w, SND_SOC_DAPM_DIR_OUT);
1623 		paths = dapm_is_connected_output_ep(w, &widgets,
1624 				custom_stop_condition);
1625 	} else {
1626 		dapm_invalidate_paths_ep(w, SND_SOC_DAPM_DIR_IN);
1627 		paths = dapm_is_connected_input_ep(w, &widgets,
1628 				custom_stop_condition);
1629 	}
1630 
1631 	/* Drop starting point */
1632 	list_del(widgets.next);
1633 
1634 	ret = dapm_widget_list_create(list, &widgets);
1635 	if (ret)
1636 		paths = ret;
1637 
1638 	trace_snd_soc_dapm_connected(paths, stream);
1639 	snd_soc_dapm_mutex_unlock(card);
1640 
1641 	return paths;
1642 }
1643 EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_get_connected_widgets);
1644 
1645 void snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list **list)
1646 {
1647 	dapm_widget_list_free(list);
1648 }
1649 EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_free_widgets);
1650 
1651 /*
1652  * Handler for regulator supply widget.
1653  */
1654 int snd_soc_dapm_regulator_event(struct snd_soc_dapm_widget *w,
1655 				 struct snd_kcontrol *kcontrol, int event)
1656 {
1657 	struct device *dev = snd_soc_dapm_to_dev(w->dapm);
1658 	int ret;
1659 
1660 	dapm_async_complete(w->dapm);
1661 
1662 	if (SND_SOC_DAPM_EVENT_ON(event)) {
1663 		if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1664 			ret = regulator_allow_bypass(w->regulator, false);
1665 			if (ret != 0)
1666 				dev_warn(dev,
1667 					 "ASoC: Failed to unbypass %s: %d\n",
1668 					 w->name, ret);
1669 		}
1670 
1671 		return regulator_enable(w->regulator);
1672 	} else {
1673 		if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1674 			ret = regulator_allow_bypass(w->regulator, true);
1675 			if (ret != 0)
1676 				dev_warn(dev,
1677 					 "ASoC: Failed to bypass %s: %d\n",
1678 					 w->name, ret);
1679 		}
1680 
1681 		return regulator_disable_deferred(w->regulator, w->shift);
1682 	}
1683 }
1684 EXPORT_SYMBOL_GPL(snd_soc_dapm_regulator_event);
1685 
1686 /*
1687  * Handler for pinctrl widget.
1688  */
1689 int snd_soc_dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1690 			       struct snd_kcontrol *kcontrol, int event)
1691 {
1692 	struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1693 	struct pinctrl *p = w->pinctrl;
1694 	struct pinctrl_state *s;
1695 
1696 	if (!p || !priv)
1697 		return -EIO;
1698 
1699 	if (SND_SOC_DAPM_EVENT_ON(event))
1700 		s = pinctrl_lookup_state(p, priv->active_state);
1701 	else
1702 		s = pinctrl_lookup_state(p, priv->sleep_state);
1703 
1704 	if (IS_ERR(s))
1705 		return PTR_ERR(s);
1706 
1707 	return pinctrl_select_state(p, s);
1708 }
1709 EXPORT_SYMBOL_GPL(snd_soc_dapm_pinctrl_event);
1710 
1711 /*
1712  * Handler for clock supply widget.
1713  */
1714 int snd_soc_dapm_clock_event(struct snd_soc_dapm_widget *w,
1715 			     struct snd_kcontrol *kcontrol, int event)
1716 {
1717 	if (!w->clk)
1718 		return -EIO;
1719 
1720 	dapm_async_complete(w->dapm);
1721 
1722 	if (SND_SOC_DAPM_EVENT_ON(event)) {
1723 		return clk_prepare_enable(w->clk);
1724 	} else {
1725 		clk_disable_unprepare(w->clk);
1726 		return 0;
1727 	}
1728 
1729 	return 0;
1730 }
1731 EXPORT_SYMBOL_GPL(snd_soc_dapm_clock_event);
1732 
1733 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1734 {
1735 	if (w->power_checked)
1736 		return w->new_power;
1737 
1738 	if (w->force)
1739 		w->new_power = 1;
1740 	else
1741 		w->new_power = w->power_check(w);
1742 
1743 	w->power_checked = true;
1744 
1745 	return w->new_power;
1746 }
1747 
1748 /* Generic check to see if a widget should be powered. */
1749 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1750 {
1751 	int in, out;
1752 
1753 	DAPM_UPDATE_STAT(w, power_checks);
1754 
1755 	in  = dapm_is_connected_input_ep(w, NULL, NULL);
1756 	out = dapm_is_connected_output_ep(w, NULL, NULL);
1757 	return out != 0 && in != 0;
1758 }
1759 
1760 /* Check to see if a power supply is needed */
1761 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1762 {
1763 	struct snd_soc_dapm_path *path;
1764 
1765 	DAPM_UPDATE_STAT(w, power_checks);
1766 
1767 	/* Check if one of our outputs is connected */
1768 	snd_soc_dapm_widget_for_each_sink_path(w, path) {
1769 		DAPM_UPDATE_STAT(w, neighbour_checks);
1770 
1771 		if (path->connected &&
1772 		    !path->connected(path->source, path->sink))
1773 			continue;
1774 
1775 		if (dapm_widget_power_check(path->sink))
1776 			return 1;
1777 	}
1778 
1779 	return 0;
1780 }
1781 
1782 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1783 {
1784 	return w->connected;
1785 }
1786 
1787 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1788 			    struct snd_soc_dapm_widget *b,
1789 			    bool power_up)
1790 {
1791 	int *sort;
1792 
1793 	BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1794 	BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1795 
1796 	if (power_up)
1797 		sort = dapm_up_seq;
1798 	else
1799 		sort = dapm_down_seq;
1800 
1801 	WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1802 	WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1803 
1804 	if (sort[a->id] != sort[b->id])
1805 		return sort[a->id] - sort[b->id];
1806 	if (a->subseq != b->subseq) {
1807 		if (power_up)
1808 			return a->subseq - b->subseq;
1809 		else
1810 			return b->subseq - a->subseq;
1811 	}
1812 	if (a->reg != b->reg)
1813 		return a->reg - b->reg;
1814 	if (a->dapm != b->dapm)
1815 		return (unsigned long)a->dapm - (unsigned long)b->dapm;
1816 
1817 	return 0;
1818 }
1819 
1820 /* Insert a widget in order into a DAPM power sequence. */
1821 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1822 			    struct list_head *list,
1823 			    bool power_up)
1824 {
1825 	struct snd_soc_dapm_widget *w;
1826 
1827 	list_for_each_entry(w, list, power_list)
1828 		if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1829 			list_add_tail(&new_widget->power_list, &w->power_list);
1830 			return;
1831 		}
1832 
1833 	list_add_tail(&new_widget->power_list, list);
1834 }
1835 
1836 static void dapm_seq_check_event(struct snd_soc_card *card,
1837 				 struct snd_soc_dapm_widget *w, int event)
1838 {
1839 	struct device *dev = card->dev;
1840 	const char *ev_name;
1841 	int power;
1842 
1843 	switch (event) {
1844 	case SND_SOC_DAPM_PRE_PMU:
1845 		ev_name = "PRE_PMU";
1846 		power = 1;
1847 		break;
1848 	case SND_SOC_DAPM_POST_PMU:
1849 		ev_name = "POST_PMU";
1850 		power = 1;
1851 		break;
1852 	case SND_SOC_DAPM_PRE_PMD:
1853 		ev_name = "PRE_PMD";
1854 		power = 0;
1855 		break;
1856 	case SND_SOC_DAPM_POST_PMD:
1857 		ev_name = "POST_PMD";
1858 		power = 0;
1859 		break;
1860 	case SND_SOC_DAPM_WILL_PMU:
1861 		ev_name = "WILL_PMU";
1862 		power = 1;
1863 		break;
1864 	case SND_SOC_DAPM_WILL_PMD:
1865 		ev_name = "WILL_PMD";
1866 		power = 0;
1867 		break;
1868 	default:
1869 		WARN(1, "Unknown event %d\n", event);
1870 		return;
1871 	}
1872 
1873 	if (w->new_power != power)
1874 		return;
1875 
1876 	if (w->event && (w->event_flags & event)) {
1877 		int ret;
1878 
1879 		dapm_pop_dbg(dev, "pop test : %s %s\n", w->name, ev_name);
1880 		dapm_async_complete(w->dapm);
1881 		trace_snd_soc_dapm_widget_event_start(w, event);
1882 		ret = w->event(w, NULL, event);
1883 		trace_snd_soc_dapm_widget_event_done(w, event);
1884 		if (ret < 0)
1885 			dev_err(dev, "ASoC: %s: %s event failed: %d\n",
1886 			       ev_name, w->name, ret);
1887 	}
1888 }
1889 
1890 /* Apply the coalesced changes from a DAPM sequence */
1891 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1892 				   struct list_head *pending)
1893 {
1894 	struct device *dev = card->dev;
1895 	struct snd_soc_dapm_context *dapm;
1896 	struct snd_soc_dapm_widget *w;
1897 	int reg;
1898 	unsigned int value = 0;
1899 	unsigned int mask = 0;
1900 
1901 	w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1902 	reg = w->reg;
1903 	dapm = w->dapm;
1904 
1905 	list_for_each_entry(w, pending, power_list) {
1906 		WARN_ON(reg != w->reg || dapm != w->dapm);
1907 		w->power = w->new_power;
1908 
1909 		mask |= w->mask << w->shift;
1910 		if (w->power)
1911 			value |= w->on_val << w->shift;
1912 		else
1913 			value |= w->off_val << w->shift;
1914 
1915 		dapm_pop_dbg(dev,
1916 			"pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1917 			w->name, reg, value, mask);
1918 
1919 		/* Check for events */
1920 		dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1921 		dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1922 	}
1923 
1924 	if (reg >= 0) {
1925 		/* Any widget will do, they should all be updating the
1926 		 * same register.
1927 		 */
1928 
1929 		dapm_pop_dbg(dev,
1930 			"pop test : Applying 0x%x/0x%x to %x in %dms\n",
1931 			value, mask, reg, pop_time);
1932 		dapm_pop_wait();
1933 		dapm_update_bits(dapm, reg, mask, value);
1934 	}
1935 
1936 	list_for_each_entry(w, pending, power_list) {
1937 		dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1938 		dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1939 	}
1940 }
1941 
1942 /* Apply a DAPM power sequence.
1943  *
1944  * We walk over a pre-sorted list of widgets to apply power to.  In
1945  * order to minimise the number of writes to the device required
1946  * multiple widgets will be updated in a single write where possible.
1947  * Currently anything that requires more than a single write is not
1948  * handled.
1949  */
1950 static void dapm_seq_run(struct snd_soc_card *card,
1951 	struct list_head *list, int event, bool power_up)
1952 {
1953 	struct device *dev = card->dev;
1954 	struct snd_soc_dapm_widget *w, *n;
1955 	struct snd_soc_dapm_context *d;
1956 	LIST_HEAD(pending);
1957 	int cur_sort = -1;
1958 	int cur_subseq = -1;
1959 	int cur_reg = SND_SOC_NOPM;
1960 	struct snd_soc_dapm_context *cur_dapm = NULL;
1961 	int i;
1962 	int *sort;
1963 
1964 	if (power_up)
1965 		sort = dapm_up_seq;
1966 	else
1967 		sort = dapm_down_seq;
1968 
1969 	list_for_each_entry_safe(w, n, list, power_list) {
1970 		int ret = 0;
1971 
1972 		/* Do we need to apply any queued changes? */
1973 		if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1974 		    w->dapm != cur_dapm || w->subseq != cur_subseq) {
1975 			if (!list_empty(&pending))
1976 				dapm_seq_run_coalesced(card, &pending);
1977 
1978 			if (cur_dapm && cur_dapm->component) {
1979 				for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1980 					if (sort[i] == cur_sort)
1981 						snd_soc_component_seq_notifier(
1982 							cur_dapm->component,
1983 							i, cur_subseq);
1984 			}
1985 
1986 			if (cur_dapm && w->dapm != cur_dapm)
1987 				dapm_async_complete(cur_dapm);
1988 
1989 			INIT_LIST_HEAD(&pending);
1990 			cur_sort = -1;
1991 			cur_subseq = INT_MIN;
1992 			cur_reg = SND_SOC_NOPM;
1993 			cur_dapm = NULL;
1994 		}
1995 
1996 		switch (w->id) {
1997 		case snd_soc_dapm_pre:
1998 			if (!w->event)
1999 				continue;
2000 
2001 			if (event == SND_SOC_DAPM_STREAM_START)
2002 				ret = w->event(w,
2003 					       NULL, SND_SOC_DAPM_PRE_PMU);
2004 			else if (event == SND_SOC_DAPM_STREAM_STOP)
2005 				ret = w->event(w,
2006 					       NULL, SND_SOC_DAPM_PRE_PMD);
2007 			break;
2008 
2009 		case snd_soc_dapm_post:
2010 			if (!w->event)
2011 				continue;
2012 
2013 			if (event == SND_SOC_DAPM_STREAM_START)
2014 				ret = w->event(w,
2015 					       NULL, SND_SOC_DAPM_POST_PMU);
2016 			else if (event == SND_SOC_DAPM_STREAM_STOP)
2017 				ret = w->event(w,
2018 					       NULL, SND_SOC_DAPM_POST_PMD);
2019 			break;
2020 
2021 		default:
2022 			/* Queue it up for application */
2023 			cur_sort = sort[w->id];
2024 			cur_subseq = w->subseq;
2025 			cur_reg = w->reg;
2026 			cur_dapm = w->dapm;
2027 			list_move(&w->power_list, &pending);
2028 			break;
2029 		}
2030 
2031 		if (ret < 0)
2032 			dev_err(dev,
2033 				"ASoC: Failed to apply widget power: %d\n", ret);
2034 	}
2035 
2036 	if (!list_empty(&pending))
2037 		dapm_seq_run_coalesced(card, &pending);
2038 
2039 	if (cur_dapm && cur_dapm->component) {
2040 		for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
2041 			if (sort[i] == cur_sort)
2042 				snd_soc_component_seq_notifier(
2043 					cur_dapm->component,
2044 					i, cur_subseq);
2045 	}
2046 
2047 	for_each_card_dapms(card, d)
2048 		dapm_async_complete(d);
2049 }
2050 
2051 static void dapm_widget_update(struct snd_soc_card *card, struct snd_soc_dapm_update *update)
2052 {
2053 	struct device *dev = card->dev;
2054 	struct snd_soc_dapm_widget_list *wlist;
2055 	struct snd_soc_dapm_widget *w = NULL;
2056 	unsigned int wi;
2057 	int ret;
2058 
2059 	if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
2060 		return;
2061 
2062 	wlist = dapm_kcontrol_get_wlist(update->kcontrol);
2063 
2064 	for_each_dapm_widgets(wlist, wi, w) {
2065 		if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
2066 			ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
2067 			if (ret != 0)
2068 				dev_err(dev, "ASoC: %s DAPM pre-event failed: %d\n",
2069 					   w->name, ret);
2070 		}
2071 	}
2072 
2073 	if (!w)
2074 		return;
2075 
2076 	ret = dapm_update_bits(w->dapm, update->reg, update->mask,
2077 		update->val);
2078 	if (ret < 0)
2079 		dev_err(dev, "ASoC: %s DAPM update failed: %d\n",
2080 			w->name, ret);
2081 
2082 	if (update->has_second_set) {
2083 		ret = dapm_update_bits(w->dapm, update->reg2,
2084 					   update->mask2, update->val2);
2085 		if (ret < 0)
2086 			dev_err(dev,
2087 				"ASoC: %s DAPM update failed: %d\n",
2088 				w->name, ret);
2089 	}
2090 
2091 	for_each_dapm_widgets(wlist, wi, w) {
2092 		if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
2093 			ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
2094 			if (ret != 0)
2095 				dev_err(dev, "ASoC: %s DAPM post-event failed: %d\n",
2096 					   w->name, ret);
2097 		}
2098 	}
2099 }
2100 
2101 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
2102  * they're changing state.
2103  */
2104 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
2105 {
2106 	struct snd_soc_dapm_context *dapm = data;
2107 	struct device *dev = snd_soc_dapm_to_dev(dapm);
2108 	int ret;
2109 
2110 	/* If we're off and we're not supposed to go into STANDBY */
2111 	if (dapm->bias_level == SND_SOC_BIAS_OFF &&
2112 	    dapm->target_bias_level != SND_SOC_BIAS_OFF) {
2113 		if (dev && cookie)
2114 			pm_runtime_get_sync(dev);
2115 
2116 		ret = snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
2117 		if (ret != 0)
2118 			dev_err(dev,
2119 				"ASoC: Failed to turn on bias: %d\n", ret);
2120 	}
2121 
2122 	/* Prepare for a transition to ON or away from ON */
2123 	if ((dapm->target_bias_level == SND_SOC_BIAS_ON &&
2124 	     dapm->bias_level != SND_SOC_BIAS_ON) ||
2125 	    (dapm->target_bias_level != SND_SOC_BIAS_ON &&
2126 	     dapm->bias_level == SND_SOC_BIAS_ON)) {
2127 		ret = snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
2128 		if (ret != 0)
2129 			dev_err(dev,
2130 				"ASoC: Failed to prepare bias: %d\n", ret);
2131 	}
2132 }
2133 
2134 /* Async callback run prior to DAPM sequences - brings to their final
2135  * state.
2136  */
2137 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
2138 {
2139 	struct snd_soc_dapm_context *dapm = data;
2140 	struct device *dev = snd_soc_dapm_to_dev(dapm);
2141 	int ret;
2142 
2143 	/* If we just powered the last thing off drop to standby bias */
2144 	if (dapm->bias_level == SND_SOC_BIAS_PREPARE &&
2145 	    (dapm->target_bias_level == SND_SOC_BIAS_STANDBY ||
2146 	     dapm->target_bias_level == SND_SOC_BIAS_OFF)) {
2147 		ret = snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
2148 		if (ret != 0)
2149 			dev_err(dev, "ASoC: Failed to apply standby bias: %d\n", ret);
2150 	}
2151 
2152 	/* If we're in standby and can support bias off then do that */
2153 	if (dapm->bias_level == SND_SOC_BIAS_STANDBY &&
2154 	    dapm->target_bias_level == SND_SOC_BIAS_OFF) {
2155 		ret = snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_OFF);
2156 		if (ret != 0)
2157 			dev_err(dev, "ASoC: Failed to turn off bias: %d\n", ret);
2158 
2159 		if (dev && cookie)
2160 			pm_runtime_put(dev);
2161 	}
2162 
2163 	/* If we just powered up then move to active bias */
2164 	if (dapm->bias_level == SND_SOC_BIAS_PREPARE &&
2165 	    dapm->target_bias_level == SND_SOC_BIAS_ON) {
2166 		ret = snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_ON);
2167 		if (ret != 0)
2168 			dev_err(dev, "ASoC: Failed to apply active bias: %d\n", ret);
2169 	}
2170 }
2171 
2172 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
2173 				       bool power, bool connect)
2174 {
2175 	/* If a connection is being made or broken then that update
2176 	 * will have marked the peer dirty, otherwise the widgets are
2177 	 * not connected and this update has no impact. */
2178 	if (!connect)
2179 		return;
2180 
2181 	/* If the peer is already in the state we're moving to then we
2182 	 * won't have an impact on it. */
2183 	if (power != peer->power)
2184 		dapm_mark_dirty(peer, "peer state change");
2185 }
2186 
2187 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
2188 				  struct list_head *up_list,
2189 				  struct list_head *down_list)
2190 {
2191 	struct snd_soc_dapm_path *path;
2192 	int power;
2193 
2194 	switch (w->id) {
2195 	case snd_soc_dapm_pre:
2196 		power = 0;
2197 		goto end;
2198 	case snd_soc_dapm_post:
2199 		power = 1;
2200 		goto end;
2201 	default:
2202 		break;
2203 	}
2204 
2205 	power = dapm_widget_power_check(w);
2206 
2207 	if (w->power == power)
2208 		return;
2209 
2210 	trace_snd_soc_dapm_widget_power(w, power);
2211 
2212 	/*
2213 	 * If we changed our power state perhaps our neigbours
2214 	 * changed also.
2215 	 */
2216 	snd_soc_dapm_widget_for_each_source_path(w, path)
2217 		dapm_widget_set_peer_power(path->source, power, path->connect);
2218 
2219 	/*
2220 	 * Supplies can't affect their outputs, only their inputs
2221 	 */
2222 	if (!w->is_supply)
2223 		snd_soc_dapm_widget_for_each_sink_path(w, path)
2224 			dapm_widget_set_peer_power(path->sink, power, path->connect);
2225 
2226 end:
2227 	if (power)
2228 		dapm_seq_insert(w, up_list, true);
2229 	else
2230 		dapm_seq_insert(w, down_list, false);
2231 }
2232 
2233 bool snd_soc_dapm_get_idle_bias(struct snd_soc_dapm_context *dapm)
2234 {
2235 	if (dapm->idle_bias) {
2236 		struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
2237 		unsigned int state = snd_power_get_state(dapm->card->snd_card);
2238 
2239 		if ((state == SNDRV_CTL_POWER_D3hot || (state == SNDRV_CTL_POWER_D3cold)) &&
2240 		    component)
2241 			return !component->driver->suspend_bias_off;
2242 	}
2243 
2244 	return dapm->idle_bias;
2245 }
2246 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_idle_bias);
2247 
2248 void snd_soc_dapm_set_idle_bias(struct snd_soc_dapm_context *dapm, bool on)
2249 {
2250 	dapm->idle_bias = on;
2251 }
2252 EXPORT_SYMBOL_GPL(snd_soc_dapm_set_idle_bias);
2253 
2254 /*
2255  * Scan each dapm widget for complete audio path.
2256  * A complete path is a route that has valid endpoints i.e.:-
2257  *
2258  *  o DAC to output pin.
2259  *  o Input pin to ADC.
2260  *  o Input pin to Output pin (bypass, sidetone)
2261  *  o DAC to ADC (loopback).
2262  */
2263 static int dapm_power_widgets(struct snd_soc_card *card, int event,
2264 			      struct snd_soc_dapm_update *update)
2265 {
2266 	struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
2267 	struct snd_soc_dapm_widget *w;
2268 	struct snd_soc_dapm_context *d;
2269 	LIST_HEAD(up_list);
2270 	LIST_HEAD(down_list);
2271 	ASYNC_DOMAIN_EXCLUSIVE(async_domain);
2272 	enum snd_soc_bias_level bias;
2273 	int ret;
2274 
2275 	snd_soc_dapm_mutex_assert_held(card);
2276 
2277 	trace_snd_soc_dapm_start(card, event);
2278 
2279 	for_each_card_dapms(card, d) {
2280 		if (snd_soc_dapm_get_idle_bias(d))
2281 			d->target_bias_level = SND_SOC_BIAS_STANDBY;
2282 		else
2283 			d->target_bias_level = SND_SOC_BIAS_OFF;
2284 	}
2285 
2286 	dapm_reset(card);
2287 
2288 	/* Check which widgets we need to power and store them in
2289 	 * lists indicating if they should be powered up or down.  We
2290 	 * only check widgets that have been flagged as dirty but note
2291 	 * that new widgets may be added to the dirty list while we
2292 	 * iterate.
2293 	 */
2294 	list_for_each_entry(w, &card->dapm_dirty, dirty) {
2295 		dapm_power_one_widget(w, &up_list, &down_list);
2296 	}
2297 
2298 	for_each_card_widgets(card, w) {
2299 		switch (w->id) {
2300 		case snd_soc_dapm_pre:
2301 		case snd_soc_dapm_post:
2302 			/* These widgets always need to be powered */
2303 			break;
2304 		default:
2305 			list_del_init(&w->dirty);
2306 			break;
2307 		}
2308 
2309 		if (w->new_power) {
2310 			d = w->dapm;
2311 
2312 			/* Supplies and micbiases only bring the
2313 			 * context up to STANDBY as unless something
2314 			 * else is active and passing audio they
2315 			 * generally don't require full power.  Signal
2316 			 * generators are virtual pins and have no
2317 			 * power impact themselves.
2318 			 */
2319 			switch (w->id) {
2320 			case snd_soc_dapm_siggen:
2321 			case snd_soc_dapm_vmid:
2322 				break;
2323 			case snd_soc_dapm_supply:
2324 			case snd_soc_dapm_regulator_supply:
2325 			case snd_soc_dapm_pinctrl:
2326 			case snd_soc_dapm_clock_supply:
2327 			case snd_soc_dapm_micbias:
2328 				if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
2329 					d->target_bias_level = SND_SOC_BIAS_STANDBY;
2330 				break;
2331 			default:
2332 				d->target_bias_level = SND_SOC_BIAS_ON;
2333 				break;
2334 			}
2335 		}
2336 
2337 	}
2338 
2339 	/* Force all contexts in the card to the same bias state if
2340 	 * they're not ground referenced.
2341 	 */
2342 	bias = SND_SOC_BIAS_OFF;
2343 	for_each_card_dapms(card, d)
2344 		if (d->target_bias_level > bias)
2345 			bias = d->target_bias_level;
2346 	for_each_card_dapms(card, d)
2347 		if (snd_soc_dapm_get_idle_bias(d))
2348 			d->target_bias_level = bias;
2349 
2350 	trace_snd_soc_dapm_walk_done(card);
2351 
2352 	/* Run card bias changes at first */
2353 	dapm_pre_sequence_async(dapm, 0);
2354 	/* Run other bias changes in parallel */
2355 	for_each_card_dapms(card, d) {
2356 		if (d != dapm && d->bias_level != d->target_bias_level)
2357 			async_schedule_domain(dapm_pre_sequence_async, d,
2358 						&async_domain);
2359 	}
2360 	async_synchronize_full_domain(&async_domain);
2361 
2362 	list_for_each_entry(w, &down_list, power_list) {
2363 		dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
2364 	}
2365 
2366 	list_for_each_entry(w, &up_list, power_list) {
2367 		dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
2368 	}
2369 
2370 	/* Power down widgets first; try to avoid amplifying pops. */
2371 	dapm_seq_run(card, &down_list, event, false);
2372 
2373 	dapm_widget_update(card, update);
2374 
2375 	/* Now power up. */
2376 	dapm_seq_run(card, &up_list, event, true);
2377 
2378 	/* Run all the bias changes in parallel */
2379 	for_each_card_dapms(card, d) {
2380 		if (d != dapm && d->bias_level != d->target_bias_level)
2381 			async_schedule_domain(dapm_post_sequence_async, d,
2382 						&async_domain);
2383 	}
2384 	async_synchronize_full_domain(&async_domain);
2385 	/* Run card bias changes at last */
2386 	dapm_post_sequence_async(dapm, 0);
2387 
2388 	/* do we need to notify any clients that DAPM event is complete */
2389 	for_each_card_dapms(card, d) {
2390 		if (!d->component)
2391 			continue;
2392 
2393 		ret = snd_soc_component_stream_event(d->component, event);
2394 		if (ret < 0)
2395 			return ret;
2396 	}
2397 
2398 	dapm_pop_dbg(card->dev,
2399 		"DAPM sequencing finished, waiting %dms\n", pop_time);
2400 	dapm_pop_wait();
2401 
2402 	trace_snd_soc_dapm_done(card, event);
2403 
2404 	return 0;
2405 }
2406 
2407 #ifdef CONFIG_DEBUG_FS
2408 
2409 static const char * const dapm_type_name[] = {
2410 	[snd_soc_dapm_input]            = "input",
2411 	[snd_soc_dapm_output]           = "output",
2412 	[snd_soc_dapm_mux]              = "mux",
2413 	[snd_soc_dapm_mux_named_ctl]    = "mux_named_ctl",
2414 	[snd_soc_dapm_demux]            = "demux",
2415 	[snd_soc_dapm_mixer]            = "mixer",
2416 	[snd_soc_dapm_mixer_named_ctl]  = "mixer_named_ctl",
2417 	[snd_soc_dapm_pga]              = "pga",
2418 	[snd_soc_dapm_out_drv]          = "out_drv",
2419 	[snd_soc_dapm_adc]              = "adc",
2420 	[snd_soc_dapm_dac]              = "dac",
2421 	[snd_soc_dapm_micbias]          = "micbias",
2422 	[snd_soc_dapm_mic]              = "mic",
2423 	[snd_soc_dapm_hp]               = "hp",
2424 	[snd_soc_dapm_spk]              = "spk",
2425 	[snd_soc_dapm_line]             = "line",
2426 	[snd_soc_dapm_switch]           = "switch",
2427 	[snd_soc_dapm_vmid]             = "vmid",
2428 	[snd_soc_dapm_pre]              = "pre",
2429 	[snd_soc_dapm_post]             = "post",
2430 	[snd_soc_dapm_supply]           = "supply",
2431 	[snd_soc_dapm_pinctrl]          = "pinctrl",
2432 	[snd_soc_dapm_regulator_supply] = "regulator_supply",
2433 	[snd_soc_dapm_clock_supply]     = "clock_supply",
2434 	[snd_soc_dapm_aif_in]           = "aif_in",
2435 	[snd_soc_dapm_aif_out]          = "aif_out",
2436 	[snd_soc_dapm_siggen]           = "siggen",
2437 	[snd_soc_dapm_sink]             = "sink",
2438 	[snd_soc_dapm_dai_in]           = "dai_in",
2439 	[snd_soc_dapm_dai_out]          = "dai_out",
2440 	[snd_soc_dapm_dai_link]         = "dai_link",
2441 	[snd_soc_dapm_kcontrol]         = "kcontrol",
2442 	[snd_soc_dapm_buffer]           = "buffer",
2443 	[snd_soc_dapm_scheduler]        = "scheduler",
2444 	[snd_soc_dapm_effect]           = "effect",
2445 	[snd_soc_dapm_src]              = "src",
2446 	[snd_soc_dapm_asrc]             = "asrc",
2447 	[snd_soc_dapm_encoder]          = "encoder",
2448 	[snd_soc_dapm_decoder]          = "decoder",
2449 };
2450 
2451 static ssize_t dapm_widget_power_read_file(struct file *file,
2452 					   char __user *user_buf,
2453 					   size_t count, loff_t *ppos)
2454 {
2455 	struct snd_soc_dapm_widget *w = file->private_data;
2456 	enum snd_soc_dapm_direction dir, rdir;
2457 	char *buf;
2458 	int in, out;
2459 	ssize_t ret;
2460 	struct snd_soc_dapm_path *p = NULL;
2461 	const char *c_name;
2462 
2463 	BUILD_BUG_ON(ARRAY_SIZE(dapm_type_name) != SND_SOC_DAPM_TYPE_COUNT);
2464 
2465 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2466 	if (!buf)
2467 		return -ENOMEM;
2468 
2469 	snd_soc_dapm_mutex_lock_root(w->dapm);
2470 
2471 	/* Supply widgets are not handled by dapm_is_connected_{input,output}_ep() */
2472 	if (w->is_supply) {
2473 		in = 0;
2474 		out = 0;
2475 	} else {
2476 		in  = dapm_is_connected_input_ep(w, NULL, NULL);
2477 		out = dapm_is_connected_output_ep(w, NULL, NULL);
2478 	}
2479 
2480 	ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s  in %d out %d",
2481 		       w->name, w->power ? "On" : "Off",
2482 		       w->force ? " (forced)" : "", in, out);
2483 
2484 	if (w->reg >= 0)
2485 		ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2486 				" - R%d(0x%x) mask 0x%x",
2487 				w->reg, w->reg, w->mask << w->shift);
2488 
2489 	ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2490 
2491 	if (w->sname)
2492 		ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2493 				w->sname,
2494 				w->active ? "active" : "inactive");
2495 
2496 	ret += scnprintf(buf + ret, PAGE_SIZE - ret, " widget-type %s\n",
2497 			 dapm_type_name[w->id]);
2498 
2499 	dapm_for_each_direction(dir) {
2500 		rdir = DAPM_DIR_REVERSE(dir);
2501 		snd_soc_dapm_widget_for_each_path(w, dir, p) {
2502 			if (p->connected && !p->connected(p->source, p->sink))
2503 				continue;
2504 
2505 			if (!p->connect)
2506 				continue;
2507 
2508 			c_name = p->node[rdir]->dapm->component ?
2509 				p->node[rdir]->dapm->component->name : NULL;
2510 			ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2511 					" %s  \"%s\" \"%s\" \"%s\"\n",
2512 					(rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2513 					p->name ? p->name : "static",
2514 					p->node[rdir]->name, c_name);
2515 		}
2516 	}
2517 
2518 	snd_soc_dapm_mutex_unlock(w->dapm);
2519 
2520 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2521 
2522 	kfree(buf);
2523 	return ret;
2524 }
2525 
2526 static const struct file_operations dapm_widget_power_fops = {
2527 	.open = simple_open,
2528 	.read = dapm_widget_power_read_file,
2529 	.llseek = default_llseek,
2530 };
2531 
2532 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2533 				   size_t count, loff_t *ppos)
2534 {
2535 	struct snd_soc_dapm_context *dapm = file->private_data;
2536 	char *level;
2537 
2538 	switch (dapm->bias_level) {
2539 	case SND_SOC_BIAS_ON:
2540 		level = "On\n";
2541 		break;
2542 	case SND_SOC_BIAS_PREPARE:
2543 		level = "Prepare\n";
2544 		break;
2545 	case SND_SOC_BIAS_STANDBY:
2546 		level = "Standby\n";
2547 		break;
2548 	case SND_SOC_BIAS_OFF:
2549 		level = "Off\n";
2550 		break;
2551 	default:
2552 		WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2553 		level = "Unknown\n";
2554 		break;
2555 	}
2556 
2557 	return simple_read_from_buffer(user_buf, count, ppos, level,
2558 				       strlen(level));
2559 }
2560 
2561 static const struct file_operations dapm_bias_fops = {
2562 	.open = simple_open,
2563 	.read = dapm_bias_read_file,
2564 	.llseek = default_llseek,
2565 };
2566 
2567 void snd_soc_dapm_debugfs_pop_time(struct dentry *parent)
2568 {
2569 	debugfs_create_u32("dapm_pop_time", 0644, parent, &pop_time);
2570 }
2571 
2572 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2573 	struct dentry *parent)
2574 {
2575 	if (IS_ERR_OR_NULL(parent))
2576 		return;
2577 
2578 	dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2579 
2580 	debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
2581 			    &dapm_bias_fops);
2582 }
2583 
2584 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2585 {
2586 	struct snd_soc_dapm_context *dapm = w->dapm;
2587 
2588 	if (!dapm->debugfs_dapm || !w->name)
2589 		return;
2590 
2591 	debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
2592 			    &dapm_widget_power_fops);
2593 }
2594 
2595 static void dapm_debugfs_free_widget(struct snd_soc_dapm_widget *w)
2596 {
2597 	struct snd_soc_dapm_context *dapm = w->dapm;
2598 
2599 	if (!dapm->debugfs_dapm || !w->name)
2600 		return;
2601 
2602 	debugfs_lookup_and_remove(w->name, dapm->debugfs_dapm);
2603 }
2604 
2605 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2606 {
2607 	debugfs_remove_recursive(dapm->debugfs_dapm);
2608 	dapm->debugfs_dapm = NULL;
2609 }
2610 
2611 #else
2612 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2613 	struct dentry *parent)
2614 {
2615 }
2616 
2617 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2618 {
2619 }
2620 
2621 static inline void dapm_debugfs_free_widget(struct snd_soc_dapm_widget *w)
2622 {
2623 }
2624 
2625 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2626 {
2627 }
2628 
2629 #endif
2630 
2631 /*
2632  * dapm_connect_path() - Connects or disconnects a path
2633  * @path: The path to update
2634  * @connect: The new connect state of the path. True if the path is connected,
2635  *  false if it is disconnected.
2636  * @reason: The reason why the path changed (for debugging only)
2637  */
2638 static void dapm_connect_path(struct snd_soc_dapm_path *path,
2639 	bool connect, const char *reason)
2640 {
2641 	if (path->connect == connect)
2642 		return;
2643 
2644 	path->connect = connect;
2645 	dapm_mark_dirty(path->source, reason);
2646 	dapm_mark_dirty(path->sink, reason);
2647 	dapm_path_invalidate(path);
2648 }
2649 
2650 /* test and update the power status of a mux widget */
2651 static int dapm_mux_update_power(struct snd_soc_card *card,
2652 				 struct snd_kcontrol *kcontrol,
2653 				 struct snd_soc_dapm_update *update,
2654 				 int mux, struct soc_enum *e)
2655 {
2656 	struct snd_soc_dapm_path *path;
2657 	int found = 0;
2658 	bool connect;
2659 
2660 	snd_soc_dapm_mutex_assert_held(card);
2661 
2662 	/* find dapm widget path assoc with kcontrol */
2663 	dapm_kcontrol_for_each_path(path, kcontrol) {
2664 		found = 1;
2665 		/* we now need to match the string in the enum to the path */
2666 		if (e && !(strcmp(path->name, e->texts[mux])))
2667 			connect = true;
2668 		else
2669 			connect = false;
2670 
2671 		dapm_connect_path(path, connect, "mux update");
2672 	}
2673 
2674 	if (found)
2675 		dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP, update);
2676 
2677 	return found;
2678 }
2679 
2680 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2681 	struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2682 	struct snd_soc_dapm_update *update)
2683 {
2684 	struct snd_soc_card *card = dapm->card;
2685 	int ret;
2686 
2687 	snd_soc_dapm_mutex_lock(card);
2688 	ret = dapm_mux_update_power(card, kcontrol, update, mux, e);
2689 	snd_soc_dapm_mutex_unlock(card);
2690 	if (ret > 0)
2691 		snd_soc_dpcm_runtime_update(card);
2692 	return ret;
2693 }
2694 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2695 
2696 /* test and update the power status of a mixer or switch widget */
2697 static int dapm_mixer_update_power(struct snd_soc_card *card,
2698 				   struct snd_kcontrol *kcontrol,
2699 				   struct snd_soc_dapm_update *update,
2700 				   int connect, int rconnect)
2701 {
2702 	struct snd_soc_dapm_path *path;
2703 	int found = 0;
2704 
2705 	snd_soc_dapm_mutex_assert_held(card);
2706 
2707 	/* find dapm widget path assoc with kcontrol */
2708 	dapm_kcontrol_for_each_path(path, kcontrol) {
2709 		/*
2710 		 * Ideally this function should support any number of
2711 		 * paths and channels. But since kcontrols only come
2712 		 * in mono and stereo variants, we are limited to 2
2713 		 * channels.
2714 		 *
2715 		 * The following code assumes for stereo controls the
2716 		 * first path (when 'found == 0') is the left channel,
2717 		 * and all remaining paths (when 'found == 1') are the
2718 		 * right channel.
2719 		 *
2720 		 * A stereo control is signified by a valid 'rconnect'
2721 		 * value, either 0 for unconnected, or >= 0 for connected.
2722 		 * This is chosen instead of using snd_soc_volsw_is_stereo,
2723 		 * so that the behavior of snd_soc_dapm_mixer_update_power
2724 		 * doesn't change even when the kcontrol passed in is
2725 		 * stereo.
2726 		 *
2727 		 * It passes 'connect' as the path connect status for
2728 		 * the left channel, and 'rconnect' for the right
2729 		 * channel.
2730 		 */
2731 		if (found && rconnect >= 0)
2732 			dapm_connect_path(path, rconnect, "mixer update");
2733 		else
2734 			dapm_connect_path(path, connect, "mixer update");
2735 		found = 1;
2736 	}
2737 
2738 	if (found)
2739 		dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP, update);
2740 
2741 	return found;
2742 }
2743 
2744 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2745 	struct snd_kcontrol *kcontrol, int connect,
2746 	struct snd_soc_dapm_update *update)
2747 {
2748 	struct snd_soc_card *card = dapm->card;
2749 	int ret;
2750 
2751 	snd_soc_dapm_mutex_lock(card);
2752 	ret = dapm_mixer_update_power(card, kcontrol, update, connect, -1);
2753 	snd_soc_dapm_mutex_unlock(card);
2754 	if (ret > 0)
2755 		snd_soc_dpcm_runtime_update(card);
2756 	return ret;
2757 }
2758 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2759 
2760 static ssize_t dapm_widget_show_component(struct snd_soc_component *component,
2761 					  char *buf, int count)
2762 {
2763 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
2764 	struct snd_soc_dapm_widget *w;
2765 	char *state = "not set";
2766 
2767 	/* card won't be set for the dummy component, as a spot fix
2768 	 * we're checking for that case specifically here but in future
2769 	 * we will ensure that the dummy component looks like others.
2770 	 */
2771 	if (!component->card)
2772 		return 0;
2773 
2774 	for_each_card_widgets(component->card, w) {
2775 		if (w->dapm != dapm)
2776 			continue;
2777 
2778 		/* only display widgets that burn power */
2779 		switch (w->id) {
2780 		case snd_soc_dapm_hp:
2781 		case snd_soc_dapm_mic:
2782 		case snd_soc_dapm_spk:
2783 		case snd_soc_dapm_line:
2784 		case snd_soc_dapm_micbias:
2785 		case snd_soc_dapm_dac:
2786 		case snd_soc_dapm_adc:
2787 		case snd_soc_dapm_pga:
2788 		case snd_soc_dapm_effect:
2789 		case snd_soc_dapm_out_drv:
2790 		case snd_soc_dapm_encoder:
2791 		case snd_soc_dapm_decoder:
2792 		case snd_soc_dapm_mixer:
2793 		case snd_soc_dapm_mixer_named_ctl:
2794 		case snd_soc_dapm_supply:
2795 		case snd_soc_dapm_regulator_supply:
2796 		case snd_soc_dapm_pinctrl:
2797 		case snd_soc_dapm_clock_supply:
2798 			if (w->name)
2799 				count += sysfs_emit_at(buf, count, "%s: %s\n",
2800 					w->name, w->power ? "On":"Off");
2801 		break;
2802 		default:
2803 		break;
2804 		}
2805 	}
2806 
2807 	switch (snd_soc_dapm_get_bias_level(dapm)) {
2808 	case SND_SOC_BIAS_ON:
2809 		state = "On";
2810 		break;
2811 	case SND_SOC_BIAS_PREPARE:
2812 		state = "Prepare";
2813 		break;
2814 	case SND_SOC_BIAS_STANDBY:
2815 		state = "Standby";
2816 		break;
2817 	case SND_SOC_BIAS_OFF:
2818 		state = "Off";
2819 		break;
2820 	}
2821 	count += sysfs_emit_at(buf, count, "PM State: %s\n", state);
2822 
2823 	return count;
2824 }
2825 
2826 /* show dapm widget status in sys fs */
2827 static ssize_t dapm_widget_show(struct device *dev,
2828 	struct device_attribute *attr, char *buf)
2829 {
2830 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2831 	struct snd_soc_dai *codec_dai;
2832 	int i, count = 0;
2833 
2834 	snd_soc_dapm_mutex_lock_root(rtd->card);
2835 
2836 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
2837 		struct snd_soc_component *component = codec_dai->component;
2838 
2839 		count = dapm_widget_show_component(component, buf, count);
2840 	}
2841 
2842 	snd_soc_dapm_mutex_unlock(rtd->card);
2843 
2844 	return count;
2845 }
2846 
2847 static DEVICE_ATTR_RO(dapm_widget);
2848 
2849 struct attribute *snd_soc_dapm_dev_attrs[] = {
2850 	&dev_attr_dapm_widget.attr,
2851 	NULL
2852 };
2853 
2854 static void dapm_free_path(struct snd_soc_dapm_path *path)
2855 {
2856 	list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2857 	list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2858 	list_del(&path->list_kcontrol);
2859 	list_del(&path->list);
2860 	kfree(path);
2861 }
2862 
2863 /**
2864  * snd_soc_dapm_free_widget - Free specified widget
2865  * @w: widget to free
2866  *
2867  * Removes widget from all paths and frees memory occupied by it.
2868  */
2869 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2870 {
2871 	struct snd_soc_dapm_path *p, *next_p;
2872 	enum snd_soc_dapm_direction dir;
2873 
2874 	if (!w)
2875 		return;
2876 
2877 	list_del(&w->list);
2878 	list_del(&w->dirty);
2879 	/*
2880 	 * remove source and sink paths associated to this widget.
2881 	 * While removing the path, remove reference to it from both
2882 	 * source and sink widgets so that path is removed only once.
2883 	 */
2884 	dapm_for_each_direction(dir) {
2885 		snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2886 			dapm_free_path(p);
2887 	}
2888 
2889 	dapm_debugfs_free_widget(w);
2890 
2891 	kfree(w->kcontrols);
2892 	kfree_const(w->name);
2893 	kfree_const(w->sname);
2894 	kfree(w);
2895 }
2896 EXPORT_SYMBOL_GPL(snd_soc_dapm_free_widget);
2897 
2898 /* free all dapm widgets and resources */
2899 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2900 {
2901 	struct snd_soc_dapm_widget *w, *next_w;
2902 
2903 	for_each_card_widgets_safe(dapm->card, w, next_w) {
2904 		if (w->dapm != dapm)
2905 			continue;
2906 		snd_soc_dapm_free_widget(w);
2907 	}
2908 
2909 	dapm->wcache_sink	= NULL;
2910 	dapm->wcache_source	= NULL;
2911 }
2912 
2913 static struct snd_soc_dapm_widget *dapm_find_widget(
2914 			struct snd_soc_dapm_context *dapm, const char *pin,
2915 			bool search_other_contexts)
2916 {
2917 	struct snd_soc_dapm_widget *w;
2918 	struct snd_soc_dapm_widget *fallback = NULL;
2919 	bool pin_has_prefix = snd_soc_dapm_pin_has_prefix(dapm->card, pin);
2920 	bool match;
2921 
2922 	for_each_card_widgets(dapm->card, w) {
2923 		match = false;
2924 
2925 		if (!strcmp(pin, w->name))
2926 			match = true;
2927 		else if (!pin_has_prefix && !snd_soc_dapm_widget_name_cmp(w, pin))
2928 			match = true;
2929 
2930 		if (match) {
2931 			if (w->dapm == dapm)
2932 				return w;
2933 			else
2934 				fallback = w;
2935 		}
2936 	}
2937 
2938 	if (search_other_contexts)
2939 		return fallback;
2940 
2941 	return NULL;
2942 }
2943 
2944 /*
2945  * set the DAPM pin status:
2946  * returns 1 when the value has been updated, 0 when unchanged, or a negative
2947  * error code; called from kcontrol put callback
2948  */
2949 static int __dapm_set_pin(struct snd_soc_dapm_context *dapm,
2950 			  const char *pin, int status)
2951 {
2952 	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2953 	struct device *dev = snd_soc_dapm_to_dev(dapm);
2954 	int ret = 0;
2955 
2956 	dapm_assert_locked(dapm);
2957 
2958 	if (!w) {
2959 		dev_err(dev, "ASoC: DAPM unknown pin %s\n", pin);
2960 		return -EINVAL;
2961 	}
2962 
2963 	if (w->connected != status) {
2964 		dapm_mark_dirty(w, "pin configuration");
2965 		dapm_widget_invalidate_input_paths(w);
2966 		dapm_widget_invalidate_output_paths(w);
2967 		ret = 1;
2968 	}
2969 
2970 	w->connected = status;
2971 	if (status == 0)
2972 		w->force = 0;
2973 
2974 	return ret;
2975 }
2976 
2977 /*
2978  * similar as __dapm_set_pin(), but returns 0 when successful;
2979  * called from several API functions below
2980  */
2981 static int dapm_set_pin(struct snd_soc_dapm_context *dapm,
2982 				const char *pin, int status)
2983 {
2984 	int ret = __dapm_set_pin(dapm, pin, status);
2985 
2986 	return ret < 0 ? ret : 0;
2987 }
2988 
2989 /**
2990  * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2991  * @dapm: DAPM context
2992  *
2993  * Walks all dapm audio paths and powers widgets according to their
2994  * stream or path usage.
2995  *
2996  * Requires external locking.
2997  *
2998  * Returns 0 for success.
2999  */
3000 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
3001 {
3002 	/*
3003 	 * Suppress early reports (eg, jacks syncing their state) to avoid
3004 	 * silly DAPM runs during card startup.
3005 	 */
3006 	if (!snd_soc_card_is_instantiated(dapm->card))
3007 		return 0;
3008 
3009 	return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP, NULL);
3010 }
3011 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
3012 
3013 /**
3014  * snd_soc_dapm_sync - scan and power dapm paths
3015  * @dapm: DAPM context
3016  *
3017  * Walks all dapm audio paths and powers widgets according to their
3018  * stream or path usage.
3019  *
3020  * Returns 0 for success.
3021  */
3022 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
3023 {
3024 	int ret;
3025 
3026 	snd_soc_dapm_mutex_lock(dapm);
3027 	ret = snd_soc_dapm_sync_unlocked(dapm);
3028 	snd_soc_dapm_mutex_unlock(dapm);
3029 	return ret;
3030 }
3031 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
3032 
3033 static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
3034 				struct snd_soc_dapm_widget *w,
3035 				int channels)
3036 {
3037 	struct device *dev = snd_soc_dapm_to_dev(w->dapm);
3038 
3039 	switch (w->id) {
3040 	case snd_soc_dapm_aif_out:
3041 	case snd_soc_dapm_aif_in:
3042 		break;
3043 	default:
3044 		return 0;
3045 	}
3046 
3047 	dev_dbg(dev, "%s DAI route %s -> %s\n",
3048 		w->channel < channels ? "Connecting" : "Disconnecting",
3049 		p->source->name, p->sink->name);
3050 
3051 	if (w->channel < channels)
3052 		dapm_connect_path(p, true, "dai update");
3053 	else
3054 		dapm_connect_path(p, false, "dai update");
3055 
3056 	return 0;
3057 }
3058 
3059 static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
3060 				    struct snd_pcm_hw_params *params,
3061 				    struct snd_soc_dai *dai)
3062 {
3063 	int dir = substream->stream;
3064 	int channels = params_channels(params);
3065 	struct snd_soc_dapm_path *p;
3066 	struct snd_soc_dapm_widget *w;
3067 	int ret;
3068 
3069 	w = snd_soc_dai_get_widget(dai, dir);
3070 
3071 	if (!w)
3072 		return 0;
3073 
3074 	dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name, snd_pcm_direction_name(dir));
3075 
3076 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
3077 		ret = dapm_update_dai_chan(p, p->sink, channels);
3078 		if (ret < 0)
3079 			return ret;
3080 	}
3081 
3082 	snd_soc_dapm_widget_for_each_source_path(w, p) {
3083 		ret = dapm_update_dai_chan(p, p->source, channels);
3084 		if (ret < 0)
3085 			return ret;
3086 	}
3087 
3088 	return 0;
3089 }
3090 
3091 int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
3092 			    struct snd_pcm_hw_params *params,
3093 			    struct snd_soc_dai *dai)
3094 {
3095 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
3096 	int ret;
3097 
3098 	snd_soc_dapm_mutex_lock(rtd->card);
3099 	ret = dapm_update_dai_unlocked(substream, params, dai);
3100 	snd_soc_dapm_mutex_unlock(rtd->card);
3101 
3102 	return ret;
3103 }
3104 
3105 int snd_soc_dapm_widget_name_cmp(struct snd_soc_dapm_widget *widget, const char *s)
3106 {
3107 	struct snd_soc_component *component = widget->dapm->component;
3108 	const char *wname = widget->name;
3109 
3110 	if (component && component->name_prefix)
3111 		wname += strlen(component->name_prefix) + 1; /* plus space */
3112 
3113 	return strcmp(wname, s);
3114 }
3115 EXPORT_SYMBOL_GPL(snd_soc_dapm_widget_name_cmp);
3116 
3117 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
3118 				  const struct snd_soc_dapm_route *route)
3119 {
3120 	struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
3121 	struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
3122 	struct device *dev = snd_soc_dapm_to_dev(dapm);
3123 	const char *sink;
3124 	const char *source;
3125 	char prefixed_sink[80];
3126 	char prefixed_source[80];
3127 	const char *prefix;
3128 	unsigned int sink_ref = 0;
3129 	unsigned int source_ref = 0;
3130 	int ret;
3131 
3132 	prefix = dapm_prefix(dapm);
3133 	if (prefix) {
3134 		snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
3135 			 prefix, route->sink);
3136 		sink = prefixed_sink;
3137 		snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
3138 			 prefix, route->source);
3139 		source = prefixed_source;
3140 	} else {
3141 		sink = route->sink;
3142 		source = route->source;
3143 	}
3144 
3145 	wsource	= dapm_wcache_lookup(dapm->wcache_source, source);
3146 	wsink	= dapm_wcache_lookup(dapm->wcache_sink,   sink);
3147 
3148 	if (wsink && wsource)
3149 		goto skip_search;
3150 
3151 	/*
3152 	 * find src and dest widgets over all widgets but favor a widget from
3153 	 * current DAPM context
3154 	 */
3155 	for_each_card_widgets(dapm->card, w) {
3156 		if (!wsink && !(strcmp(w->name, sink))) {
3157 			wtsink = w;
3158 			if (w->dapm == dapm) {
3159 				wsink = w;
3160 				if (wsource)
3161 					break;
3162 			}
3163 			sink_ref++;
3164 			if (sink_ref > 1)
3165 				dev_warn(dev,
3166 					"ASoC: sink widget %s overwritten\n",
3167 					w->name);
3168 			continue;
3169 		}
3170 		if (!wsource && !(strcmp(w->name, source))) {
3171 			wtsource = w;
3172 			if (w->dapm == dapm) {
3173 				wsource = w;
3174 				if (wsink)
3175 					break;
3176 			}
3177 			source_ref++;
3178 			if (source_ref > 1)
3179 				dev_warn(dev,
3180 					"ASoC: source widget %s overwritten\n",
3181 					w->name);
3182 		}
3183 	}
3184 	/* use widget from another DAPM context if not found from this */
3185 	if (!wsink)
3186 		wsink = wtsink;
3187 	if (!wsource)
3188 		wsource = wtsource;
3189 
3190 	ret = -ENODEV;
3191 	if (!wsource)
3192 		goto err;
3193 	if (!wsink)
3194 		goto err;
3195 
3196 skip_search:
3197 	/* update cache */
3198 	dapm->wcache_sink	= wsink;
3199 	dapm->wcache_source	= wsource;
3200 
3201 	ret = dapm_add_path(dapm, wsource, wsink, route->control,
3202 		route->connected);
3203 err:
3204 	if (ret)
3205 		dev_err(dev, "ASoC: Failed to add route %s%s -%s%s%s> %s%s\n",
3206 			source, !wsource ? "(*)" : "",
3207 			!route->control ? "" : "> [",
3208 			!route->control ? "" : route->control,
3209 			!route->control ? "" : "] -",
3210 			sink,  !wsink ? "(*)" : "");
3211 	return ret;
3212 }
3213 
3214 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
3215 				  const struct snd_soc_dapm_route *route)
3216 {
3217 	struct device *dev = snd_soc_dapm_to_dev(dapm);
3218 	struct snd_soc_dapm_path *path, *p;
3219 	const char *sink;
3220 	const char *source;
3221 	char prefixed_sink[80];
3222 	char prefixed_source[80];
3223 	const char *prefix;
3224 
3225 	if (route->control) {
3226 		dev_err(dev,
3227 			"ASoC: Removal of routes with controls not supported\n");
3228 		return -EINVAL;
3229 	}
3230 
3231 	prefix = dapm_prefix(dapm);
3232 	if (prefix) {
3233 		snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
3234 			 prefix, route->sink);
3235 		sink = prefixed_sink;
3236 		snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
3237 			 prefix, route->source);
3238 		source = prefixed_source;
3239 	} else {
3240 		sink = route->sink;
3241 		source = route->source;
3242 	}
3243 
3244 	path = NULL;
3245 	list_for_each_entry(p, &dapm->card->paths, list) {
3246 		if (strcmp(p->source->name, source) != 0)
3247 			continue;
3248 		if (strcmp(p->sink->name, sink) != 0)
3249 			continue;
3250 		path = p;
3251 		break;
3252 	}
3253 
3254 	if (path) {
3255 		struct snd_soc_dapm_widget *wsource = path->source;
3256 		struct snd_soc_dapm_widget *wsink = path->sink;
3257 
3258 		dapm_mark_dirty(wsource, "Route removed");
3259 		dapm_mark_dirty(wsink, "Route removed");
3260 		if (path->connect)
3261 			dapm_path_invalidate(path);
3262 
3263 		dapm_free_path(path);
3264 
3265 		/* Update any path related flags */
3266 		dapm_update_widget_flags(wsource);
3267 		dapm_update_widget_flags(wsink);
3268 	} else {
3269 		dev_warn(dev, "ASoC: Route %s->%s does not exist\n",
3270 			 source, sink);
3271 	}
3272 
3273 	return 0;
3274 }
3275 
3276 /**
3277  * snd_soc_dapm_add_routes - Add routes between DAPM widgets
3278  * @dapm: DAPM context
3279  * @route: audio routes
3280  * @num: number of routes
3281  *
3282  * Connects 2 dapm widgets together via a named audio path. The sink is
3283  * the widget receiving the audio signal, whilst the source is the sender
3284  * of the audio signal.
3285  *
3286  * Returns 0 for success else error. On error all resources can be freed
3287  * with a call to snd_soc_card_free().
3288  */
3289 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
3290 			    const struct snd_soc_dapm_route *route, int num)
3291 {
3292 	int i, ret = 0;
3293 
3294 	snd_soc_dapm_mutex_lock(dapm);
3295 	for (i = 0; i < num; i++) {
3296 		int r = snd_soc_dapm_add_route(dapm, route);
3297 		if (r < 0)
3298 			ret = r;
3299 		route++;
3300 	}
3301 	snd_soc_dapm_mutex_unlock(dapm);
3302 
3303 	return ret;
3304 }
3305 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3306 
3307 /**
3308  * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3309  * @dapm: DAPM context
3310  * @route: audio routes
3311  * @num: number of routes
3312  *
3313  * Removes routes from the DAPM context.
3314  */
3315 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3316 			    const struct snd_soc_dapm_route *route, int num)
3317 {
3318 	int i;
3319 
3320 	snd_soc_dapm_mutex_lock(dapm);
3321 	for (i = 0; i < num; i++) {
3322 		snd_soc_dapm_del_route(dapm, route);
3323 		route++;
3324 	}
3325 	snd_soc_dapm_mutex_unlock(dapm);
3326 
3327 	return 0;
3328 }
3329 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3330 
3331 /**
3332  * snd_soc_dapm_new_widgets - add new dapm widgets
3333  * @card: card to be checked for new dapm widgets
3334  *
3335  * Checks the codec for any new dapm widgets and creates them if found.
3336  *
3337  * Returns 0 for success.
3338  */
3339 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3340 {
3341 	struct snd_soc_dapm_widget *w;
3342 	unsigned int val;
3343 
3344 	snd_soc_dapm_mutex_lock_root(card);
3345 
3346 	for_each_card_widgets(card, w)
3347 	{
3348 		if (w->new)
3349 			continue;
3350 
3351 		if (w->num_kcontrols) {
3352 			w->kcontrols = kzalloc_objs(struct snd_kcontrol *,
3353 						    w->num_kcontrols);
3354 			if (!w->kcontrols) {
3355 				snd_soc_dapm_mutex_unlock(card);
3356 				return -ENOMEM;
3357 			}
3358 		}
3359 
3360 		switch(w->id) {
3361 		case snd_soc_dapm_switch:
3362 		case snd_soc_dapm_mixer:
3363 		case snd_soc_dapm_mixer_named_ctl:
3364 			dapm_new_mixer(w);
3365 			break;
3366 		case snd_soc_dapm_mux:
3367 		case snd_soc_dapm_mux_named_ctl:
3368 		case snd_soc_dapm_demux:
3369 			dapm_new_mux(w);
3370 			break;
3371 		case snd_soc_dapm_pga:
3372 		case snd_soc_dapm_effect:
3373 		case snd_soc_dapm_out_drv:
3374 		case snd_soc_dapm_encoder:
3375 		case snd_soc_dapm_decoder:
3376 			dapm_new_pga(w);
3377 			break;
3378 		case snd_soc_dapm_dai_link:
3379 			dapm_new_dai_link(w);
3380 			break;
3381 		default:
3382 			break;
3383 		}
3384 
3385 		/* Read the initial power state from the device */
3386 		if (w->reg >= 0) {
3387 			val = dapm_read(w->dapm, w->reg);
3388 			val = val >> w->shift;
3389 			val &= w->mask;
3390 			if (val == w->on_val)
3391 				w->power = 1;
3392 		}
3393 
3394 		w->new = 1;
3395 
3396 		dapm_mark_dirty(w, "new widget");
3397 		dapm_debugfs_add_widget(w);
3398 	}
3399 
3400 	dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP, NULL);
3401 	snd_soc_dapm_mutex_unlock(card);
3402 	return 0;
3403 }
3404 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3405 
3406 /**
3407  * snd_soc_dapm_get_volsw - dapm mixer get callback
3408  * @kcontrol: mixer control
3409  * @ucontrol: control element information
3410  *
3411  * Callback to get the value of a dapm mixer control.
3412  *
3413  * Returns 0 for success.
3414  */
3415 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3416 	struct snd_ctl_elem_value *ucontrol)
3417 {
3418 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
3419 	struct soc_mixer_control *mc =
3420 		(struct soc_mixer_control *)kcontrol->private_value;
3421 	int reg = mc->reg;
3422 	unsigned int shift = mc->shift;
3423 	int max = mc->max;
3424 	unsigned int width = fls(max);
3425 	unsigned int mask = (1 << fls(max)) - 1;
3426 	unsigned int invert = mc->invert;
3427 	unsigned int reg_val, val, rval = 0;
3428 
3429 	snd_soc_dapm_mutex_lock(dapm);
3430 	if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3431 		reg_val = dapm_read(dapm, reg);
3432 		val = (reg_val >> shift) & mask;
3433 
3434 		if (reg != mc->rreg)
3435 			reg_val = dapm_read(dapm, mc->rreg);
3436 
3437 		if (snd_soc_volsw_is_stereo(mc))
3438 			rval = (reg_val >> mc->rshift) & mask;
3439 	} else {
3440 		reg_val = snd_soc_dapm_kcontrol_get_value(kcontrol);
3441 		val = reg_val & mask;
3442 
3443 		if (snd_soc_volsw_is_stereo(mc))
3444 			rval = (reg_val >> width) & mask;
3445 	}
3446 	snd_soc_dapm_mutex_unlock(dapm);
3447 
3448 	if (invert)
3449 		ucontrol->value.integer.value[0] = max - val;
3450 	else
3451 		ucontrol->value.integer.value[0] = val;
3452 
3453 	if (snd_soc_volsw_is_stereo(mc)) {
3454 		if (invert)
3455 			ucontrol->value.integer.value[1] = max - rval;
3456 		else
3457 			ucontrol->value.integer.value[1] = rval;
3458 	}
3459 
3460 	return 0;
3461 }
3462 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3463 
3464 /**
3465  * snd_soc_dapm_put_volsw - dapm mixer set callback
3466  * @kcontrol: mixer control
3467  * @ucontrol: control element information
3468  *
3469  * Callback to set the value of a dapm mixer control.
3470  *
3471  * Returns 0 for success.
3472  */
3473 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3474 	struct snd_ctl_elem_value *ucontrol)
3475 {
3476 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
3477 	struct device *dev = snd_soc_dapm_to_dev(dapm);
3478 	struct snd_soc_card *card = dapm->card;
3479 	struct soc_mixer_control *mc =
3480 		(struct soc_mixer_control *)kcontrol->private_value;
3481 	int reg = mc->reg;
3482 	unsigned int shift = mc->shift;
3483 	int max = mc->max;
3484 	unsigned int width = fls(max);
3485 	unsigned int mask = (1 << width) - 1;
3486 	unsigned int invert = mc->invert;
3487 	unsigned int val, rval = 0;
3488 	int connect, rconnect = -1, change, reg_change = 0;
3489 	struct snd_soc_dapm_update update = {};
3490 	struct snd_soc_dapm_update *pupdate = NULL;
3491 	int ret = 0;
3492 
3493 	val = (ucontrol->value.integer.value[0] & mask);
3494 	connect = !!val;
3495 
3496 	if (invert)
3497 		val = max - val;
3498 
3499 	if (snd_soc_volsw_is_stereo(mc)) {
3500 		rval = (ucontrol->value.integer.value[1] & mask);
3501 		rconnect = !!rval;
3502 		if (invert)
3503 			rval = max - rval;
3504 	}
3505 
3506 	snd_soc_dapm_mutex_lock(card);
3507 
3508 	/* This assumes field width < (bits in unsigned int / 2) */
3509 	if (width > sizeof(unsigned int) * 8 / 2)
3510 		dev_warn(dev,
3511 			 "ASoC: control %s field width limit exceeded\n",
3512 			 kcontrol->id.name);
3513 	change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3514 
3515 	if (reg != SND_SOC_NOPM) {
3516 		val = val << shift;
3517 		rval = rval << mc->rshift;
3518 
3519 		reg_change = dapm_test_bits(dapm, reg, mask << shift, val);
3520 
3521 		if (snd_soc_volsw_is_stereo(mc))
3522 			reg_change |= dapm_test_bits(dapm, mc->rreg,
3523 						     mask << mc->rshift,
3524 						     rval);
3525 	}
3526 
3527 	if (change || reg_change) {
3528 		if (reg_change) {
3529 			if (snd_soc_volsw_is_stereo(mc)) {
3530 				update.has_second_set = true;
3531 				update.reg2 = mc->rreg;
3532 				update.mask2 = mask << mc->rshift;
3533 				update.val2 = rval;
3534 			}
3535 			update.kcontrol = kcontrol;
3536 			update.reg = reg;
3537 			update.mask = mask << shift;
3538 			update.val = val;
3539 			pupdate = &update;
3540 		}
3541 		ret = dapm_mixer_update_power(card, kcontrol, pupdate, connect, rconnect);
3542 	}
3543 
3544 	snd_soc_dapm_mutex_unlock(card);
3545 
3546 	if (ret > 0)
3547 		snd_soc_dpcm_runtime_update(card);
3548 
3549 	return change;
3550 }
3551 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3552 
3553 /**
3554  * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3555  * @kcontrol: mixer control
3556  * @ucontrol: control element information
3557  *
3558  * Callback to get the value of a dapm enumerated double mixer control.
3559  *
3560  * Returns 0 for success.
3561  */
3562 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3563 	struct snd_ctl_elem_value *ucontrol)
3564 {
3565 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
3566 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3567 	unsigned int reg_val, val;
3568 
3569 	snd_soc_dapm_mutex_lock(dapm);
3570 	if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3571 		reg_val = dapm_read(dapm, e->reg);
3572 	} else {
3573 		reg_val = snd_soc_dapm_kcontrol_get_value(kcontrol);
3574 	}
3575 	snd_soc_dapm_mutex_unlock(dapm);
3576 
3577 	val = (reg_val >> e->shift_l) & e->mask;
3578 	ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3579 	if (e->shift_l != e->shift_r) {
3580 		val = (reg_val >> e->shift_r) & e->mask;
3581 		val = snd_soc_enum_val_to_item(e, val);
3582 		ucontrol->value.enumerated.item[1] = val;
3583 	}
3584 
3585 	return 0;
3586 }
3587 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3588 
3589 /**
3590  * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3591  * @kcontrol: mixer control
3592  * @ucontrol: control element information
3593  *
3594  * Callback to set the value of a dapm enumerated double mixer control.
3595  *
3596  * Returns 0 for success.
3597  */
3598 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3599 	struct snd_ctl_elem_value *ucontrol)
3600 {
3601 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
3602 	struct snd_soc_card *card = dapm->card;
3603 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3604 	unsigned int *item = ucontrol->value.enumerated.item;
3605 	unsigned int val, change, reg_change = 0;
3606 	unsigned int mask;
3607 	struct snd_soc_dapm_update update = {};
3608 	struct snd_soc_dapm_update *pupdate = NULL;
3609 	int ret = 0;
3610 
3611 	if (item[0] >= e->items)
3612 		return -EINVAL;
3613 
3614 	val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3615 	mask = e->mask << e->shift_l;
3616 	if (e->shift_l != e->shift_r) {
3617 		if (item[1] >= e->items)
3618 			return -EINVAL;
3619 		val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3620 		mask |= e->mask << e->shift_r;
3621 	}
3622 
3623 	snd_soc_dapm_mutex_lock(card);
3624 
3625 	change = dapm_kcontrol_set_value(kcontrol, val);
3626 
3627 	if (e->reg != SND_SOC_NOPM)
3628 		reg_change = dapm_test_bits(dapm, e->reg, mask, val);
3629 
3630 	if (change || reg_change) {
3631 		if (reg_change) {
3632 			update.kcontrol = kcontrol;
3633 			update.reg = e->reg;
3634 			update.mask = mask;
3635 			update.val = val;
3636 			pupdate = &update;
3637 		}
3638 		ret = dapm_mux_update_power(card, kcontrol, pupdate, item[0], e);
3639 	}
3640 
3641 	snd_soc_dapm_mutex_unlock(card);
3642 
3643 	if (ret > 0)
3644 		snd_soc_dpcm_runtime_update(card);
3645 
3646 	return change;
3647 }
3648 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3649 
3650 /**
3651  * snd_soc_dapm_info_pin_switch - Info for a pin switch
3652  *
3653  * @kcontrol: mixer control
3654  * @uinfo: control element information
3655  *
3656  * Callback to provide information about a pin switch control.
3657  */
3658 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3659 				 struct snd_ctl_elem_info *uinfo)
3660 {
3661 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3662 	uinfo->count = 1;
3663 	uinfo->value.integer.min = 0;
3664 	uinfo->value.integer.max = 1;
3665 
3666 	return 0;
3667 }
3668 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3669 
3670 static int __snd_soc_dapm_get_pin_switch(struct snd_soc_dapm_context *dapm,
3671 					 const char *pin,
3672 					 struct snd_ctl_elem_value *ucontrol)
3673 {
3674 	snd_soc_dapm_mutex_lock(dapm);
3675 	ucontrol->value.integer.value[0] = snd_soc_dapm_get_pin_status(dapm, pin);
3676 	snd_soc_dapm_mutex_unlock(dapm);
3677 
3678 	return 0;
3679 }
3680 
3681 /**
3682  * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3683  *
3684  * @kcontrol: mixer control
3685  * @ucontrol: Value
3686  *
3687  * Callback to provide information for a pin switch added at the card
3688  * level.
3689  */
3690 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3691 				struct snd_ctl_elem_value *ucontrol)
3692 {
3693 	struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3694 	struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
3695 	const char *pin = (const char *)kcontrol->private_value;
3696 
3697 	return __snd_soc_dapm_get_pin_switch(dapm, pin, ucontrol);
3698 }
3699 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3700 
3701 /**
3702  * snd_soc_dapm_get_component_pin_switch - Get information for a pin switch
3703  *
3704  * @kcontrol: mixer control
3705  * @ucontrol: Value
3706  *
3707  * Callback to provide information for a pin switch added at the component
3708  * level.
3709  */
3710 int snd_soc_dapm_get_component_pin_switch(struct snd_kcontrol *kcontrol,
3711 					  struct snd_ctl_elem_value *ucontrol)
3712 {
3713 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3714 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
3715 	const char *pin = (const char *)kcontrol->private_value;
3716 
3717 	return __snd_soc_dapm_get_pin_switch(dapm, pin, ucontrol);
3718 }
3719 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_component_pin_switch);
3720 
3721 static int __dapm_put_pin_switch(struct snd_soc_dapm_context *dapm,
3722 				 const char *pin,
3723 				 struct snd_ctl_elem_value *ucontrol)
3724 {
3725 	int ret;
3726 
3727 	snd_soc_dapm_mutex_lock(dapm);
3728 	ret = __dapm_set_pin(dapm, pin, !!ucontrol->value.integer.value[0]);
3729 	snd_soc_dapm_mutex_unlock(dapm);
3730 
3731 	snd_soc_dapm_sync(dapm);
3732 
3733 	return ret;
3734 }
3735 
3736 /**
3737  * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3738  *
3739  * @kcontrol: mixer control
3740  * @ucontrol: Value
3741  *
3742  * Callback to provide information for a pin switch added at the card
3743  * level.
3744  */
3745 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3746 				struct snd_ctl_elem_value *ucontrol)
3747 {
3748 	struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3749 	struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
3750 	const char *pin = (const char *)kcontrol->private_value;
3751 
3752 	return __dapm_put_pin_switch(dapm, pin, ucontrol);
3753 }
3754 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3755 
3756 /**
3757  * snd_soc_dapm_put_component_pin_switch - Set information for a pin switch
3758  *
3759  * @kcontrol: mixer control
3760  * @ucontrol: Value
3761  *
3762  * Callback to provide information for a pin switch added at the component
3763  * level.
3764  */
3765 int snd_soc_dapm_put_component_pin_switch(struct snd_kcontrol *kcontrol,
3766 					  struct snd_ctl_elem_value *ucontrol)
3767 {
3768 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3769 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
3770 	const char *pin = (const char *)kcontrol->private_value;
3771 
3772 	return __dapm_put_pin_switch(dapm, pin, ucontrol);
3773 }
3774 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_component_pin_switch);
3775 
3776 struct snd_soc_dapm_widget *
3777 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3778 			 const struct snd_soc_dapm_widget *widget)
3779 {
3780 	struct device *dev = snd_soc_dapm_to_dev(dapm);
3781 	enum snd_soc_dapm_direction dir;
3782 	struct snd_soc_dapm_widget *w;
3783 	int ret = -ENOMEM;
3784 
3785 	w = dapm_cnew_widget(widget, dapm_prefix(dapm));
3786 	if (!w)
3787 		goto cnew_failed;
3788 
3789 	switch (w->id) {
3790 	case snd_soc_dapm_regulator_supply:
3791 		w->regulator = devm_regulator_get(dev, widget->name);
3792 		if (IS_ERR(w->regulator)) {
3793 			ret = PTR_ERR(w->regulator);
3794 			goto request_failed;
3795 		}
3796 
3797 		if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3798 			ret = regulator_allow_bypass(w->regulator, true);
3799 			if (ret != 0)
3800 				dev_warn(dev,
3801 					 "ASoC: Failed to bypass %s: %d\n",
3802 					 w->name, ret);
3803 		}
3804 		break;
3805 	case snd_soc_dapm_pinctrl:
3806 		w->pinctrl = devm_pinctrl_get(dev);
3807 		if (IS_ERR(w->pinctrl)) {
3808 			ret = PTR_ERR(w->pinctrl);
3809 			goto request_failed;
3810 		}
3811 
3812 		/* set to sleep_state when initializing */
3813 		snd_soc_dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD);
3814 		break;
3815 	case snd_soc_dapm_clock_supply:
3816 		w->clk = devm_clk_get(dev, widget->name);
3817 		if (IS_ERR(w->clk)) {
3818 			ret = PTR_ERR(w->clk);
3819 			goto request_failed;
3820 		}
3821 		break;
3822 	default:
3823 		break;
3824 	}
3825 
3826 	switch (w->id) {
3827 	case snd_soc_dapm_mic:
3828 		w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3829 		w->power_check = dapm_generic_check_power;
3830 		break;
3831 	case snd_soc_dapm_input:
3832 		if (!dapm->card->fully_routed)
3833 			w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3834 		w->power_check = dapm_generic_check_power;
3835 		break;
3836 	case snd_soc_dapm_spk:
3837 	case snd_soc_dapm_hp:
3838 		w->is_ep = SND_SOC_DAPM_EP_SINK;
3839 		w->power_check = dapm_generic_check_power;
3840 		break;
3841 	case snd_soc_dapm_output:
3842 		if (!dapm->card->fully_routed)
3843 			w->is_ep = SND_SOC_DAPM_EP_SINK;
3844 		w->power_check = dapm_generic_check_power;
3845 		break;
3846 	case snd_soc_dapm_vmid:
3847 	case snd_soc_dapm_siggen:
3848 		w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3849 		w->power_check = dapm_always_on_check_power;
3850 		break;
3851 	case snd_soc_dapm_sink:
3852 		w->is_ep = SND_SOC_DAPM_EP_SINK;
3853 		w->power_check = dapm_always_on_check_power;
3854 		break;
3855 
3856 	case snd_soc_dapm_mux:
3857 	case snd_soc_dapm_mux_named_ctl:
3858 	case snd_soc_dapm_demux:
3859 	case snd_soc_dapm_switch:
3860 	case snd_soc_dapm_mixer:
3861 	case snd_soc_dapm_mixer_named_ctl:
3862 	case snd_soc_dapm_adc:
3863 	case snd_soc_dapm_aif_out:
3864 	case snd_soc_dapm_dac:
3865 	case snd_soc_dapm_aif_in:
3866 	case snd_soc_dapm_pga:
3867 	case snd_soc_dapm_buffer:
3868 	case snd_soc_dapm_scheduler:
3869 	case snd_soc_dapm_effect:
3870 	case snd_soc_dapm_src:
3871 	case snd_soc_dapm_asrc:
3872 	case snd_soc_dapm_encoder:
3873 	case snd_soc_dapm_decoder:
3874 	case snd_soc_dapm_out_drv:
3875 	case snd_soc_dapm_micbias:
3876 	case snd_soc_dapm_line:
3877 	case snd_soc_dapm_dai_link:
3878 	case snd_soc_dapm_dai_out:
3879 	case snd_soc_dapm_dai_in:
3880 		w->power_check = dapm_generic_check_power;
3881 		break;
3882 	case snd_soc_dapm_supply:
3883 	case snd_soc_dapm_regulator_supply:
3884 	case snd_soc_dapm_pinctrl:
3885 	case snd_soc_dapm_clock_supply:
3886 	case snd_soc_dapm_kcontrol:
3887 		w->is_supply = 1;
3888 		w->power_check = dapm_supply_check_power;
3889 		break;
3890 	default:
3891 		w->power_check = dapm_always_on_check_power;
3892 		break;
3893 	}
3894 
3895 	w->dapm = dapm;
3896 	INIT_LIST_HEAD(&w->list);
3897 	INIT_LIST_HEAD(&w->dirty);
3898 	/* see for_each_card_widgets */
3899 	list_add_tail(&w->list, &dapm->card->widgets);
3900 
3901 	dapm_for_each_direction(dir) {
3902 		INIT_LIST_HEAD(&w->edges[dir]);
3903 		w->endpoints[dir] = -1;
3904 	}
3905 
3906 	/* machine layer sets up unconnected pins and insertions */
3907 	w->connected = 1;
3908 	return w;
3909 
3910 request_failed:
3911 	dev_err_probe(dev, ret, "ASoC: Failed to request %s\n",
3912 		      w->name);
3913 	kfree_const(w->name);
3914 	kfree_const(w->sname);
3915 	kfree(w);
3916 cnew_failed:
3917 	return ERR_PTR(ret);
3918 }
3919 
3920 /**
3921  * snd_soc_dapm_new_control - create new dapm control
3922  * @dapm: DAPM context
3923  * @widget: widget template
3924  *
3925  * Creates new DAPM control based upon a template.
3926  *
3927  * Returns a widget pointer on success or an error pointer on failure
3928  */
3929 struct snd_soc_dapm_widget *
3930 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3931 			 const struct snd_soc_dapm_widget *widget)
3932 {
3933 	struct snd_soc_dapm_widget *w;
3934 
3935 	snd_soc_dapm_mutex_lock(dapm);
3936 	w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3937 	snd_soc_dapm_mutex_unlock(dapm);
3938 
3939 	return w;
3940 }
3941 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3942 
3943 /**
3944  * snd_soc_dapm_new_controls - create new dapm controls
3945  * @dapm: DAPM context
3946  * @widget: widget array
3947  * @num: number of widgets
3948  *
3949  * Creates new DAPM controls based upon the templates.
3950  *
3951  * Returns 0 for success else error.
3952  */
3953 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3954 	const struct snd_soc_dapm_widget *widget,
3955 	unsigned int num)
3956 {
3957 	int i;
3958 	int ret = 0;
3959 
3960 	snd_soc_dapm_mutex_lock_root(dapm);
3961 	for (i = 0; i < num; i++) {
3962 		struct snd_soc_dapm_widget *w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3963 		if (IS_ERR(w)) {
3964 			ret = PTR_ERR(w);
3965 			break;
3966 		}
3967 		widget++;
3968 	}
3969 	snd_soc_dapm_mutex_unlock(dapm);
3970 	return ret;
3971 }
3972 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3973 
3974 static int dapm_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3975 				       struct snd_pcm_substream *substream)
3976 {
3977 	struct device *dev = snd_soc_dapm_to_dev(w->dapm);
3978 	struct snd_soc_dapm_path *path;
3979 	struct snd_soc_dai *source, *sink;
3980 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
3981 	const struct snd_soc_pcm_stream *config = NULL;
3982 	struct snd_pcm_runtime *runtime = NULL;
3983 	unsigned int fmt;
3984 	int ret;
3985 
3986 	/*
3987 	 * NOTE
3988 	 *
3989 	 * snd_pcm_hw_params is quite large (608 bytes on arm64) and is
3990 	 * starting to get a bit excessive for allocation on the stack,
3991 	 * especially when you're building with some of the KASAN type
3992 	 * stuff that increases stack usage.
3993 	 * So, we use kzalloc()/kfree() for params in this function.
3994 	 */
3995 	struct snd_pcm_hw_params *params __free(kfree) = kzalloc_obj(*params);
3996 	if (!params)
3997 		return -ENOMEM;
3998 
3999 	runtime = kzalloc_obj(*runtime);
4000 	if (!runtime)
4001 		return -ENOMEM;
4002 
4003 	substream->runtime = runtime;
4004 
4005 	substream->stream = SNDRV_PCM_STREAM_CAPTURE;
4006 	snd_soc_dapm_widget_for_each_source_path(w, path) {
4007 		source = path->source->priv;
4008 
4009 		ret = snd_soc_dai_startup(source, substream);
4010 		if (ret < 0)
4011 			return ret;
4012 
4013 		snd_soc_dai_activate(source, substream->stream);
4014 	}
4015 
4016 	substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
4017 	snd_soc_dapm_widget_for_each_sink_path(w, path) {
4018 		sink = path->sink->priv;
4019 
4020 		ret = snd_soc_dai_startup(sink, substream);
4021 		if (ret < 0)
4022 			return ret;
4023 
4024 		snd_soc_dai_activate(sink, substream->stream);
4025 	}
4026 
4027 	substream->hw_opened = 1;
4028 
4029 	/*
4030 	 * Note: getting the config after .startup() gives a chance to
4031 	 * either party on the link to alter the configuration if
4032 	 * necessary
4033 	 */
4034 	config = rtd->dai_link->c2c_params + rtd->c2c_params_select;
4035 	if (!config) {
4036 		dev_err(dev, "ASoC: link config missing\n");
4037 		return -EINVAL;
4038 	}
4039 
4040 	/* Be a little careful as we don't want to overflow the mask array */
4041 	if (!config->formats) {
4042 		dev_warn(dev, "ASoC: Invalid format was specified\n");
4043 
4044 		return -EINVAL;
4045 	}
4046 
4047 	fmt = ffs(config->formats) - 1;
4048 
4049 	snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
4050 	hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
4051 		config->rate_min;
4052 	hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
4053 		config->rate_max;
4054 	hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
4055 		= config->channels_min;
4056 	hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
4057 		= config->channels_max;
4058 
4059 	substream->stream = SNDRV_PCM_STREAM_CAPTURE;
4060 	snd_soc_dapm_widget_for_each_source_path(w, path) {
4061 		source = path->source->priv;
4062 
4063 		ret = snd_soc_dai_hw_params(source, substream, params);
4064 		if (ret < 0)
4065 			return ret;
4066 
4067 		dapm_update_dai_unlocked(substream, params, source);
4068 	}
4069 
4070 	substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
4071 	snd_soc_dapm_widget_for_each_sink_path(w, path) {
4072 		sink = path->sink->priv;
4073 
4074 		ret = snd_soc_dai_hw_params(sink, substream, params);
4075 		if (ret < 0)
4076 			return ret;
4077 
4078 		dapm_update_dai_unlocked(substream, params, sink);
4079 	}
4080 
4081 	runtime->format = params_format(params);
4082 	runtime->subformat = params_subformat(params);
4083 	runtime->channels = params_channels(params);
4084 	runtime->rate = params_rate(params);
4085 
4086 	return 0;
4087 }
4088 
4089 static int dapm_dai_link_event(struct snd_soc_dapm_widget *w,
4090 			       struct snd_kcontrol *kcontrol, int event)
4091 {
4092 	struct snd_soc_dapm_path *path;
4093 	struct snd_soc_dai *source, *sink;
4094 	struct snd_pcm_substream *substream = w->priv;
4095 	int ret = 0, saved_stream = substream->stream;
4096 
4097 	if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
4098 		    list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
4099 		return -EINVAL;
4100 
4101 	switch (event) {
4102 	case SND_SOC_DAPM_PRE_PMU:
4103 		ret = dapm_dai_link_event_pre_pmu(w, substream);
4104 		if (ret < 0)
4105 			goto out;
4106 
4107 		break;
4108 
4109 	case SND_SOC_DAPM_POST_PMU:
4110 		snd_soc_dapm_widget_for_each_source_path(w, path) {
4111 			source = path->source->priv;
4112 
4113 			snd_soc_dai_prepare(source, substream);
4114 		}
4115 
4116 		snd_soc_dapm_widget_for_each_sink_path(w, path) {
4117 			sink = path->sink->priv;
4118 
4119 			snd_soc_dai_prepare(sink, substream);
4120 		}
4121 
4122 		snd_soc_dapm_widget_for_each_sink_path(w, path) {
4123 			sink = path->sink->priv;
4124 
4125 			snd_soc_dai_digital_mute(sink, 0, SNDRV_PCM_STREAM_PLAYBACK);
4126 			ret = 0;
4127 		}
4128 		break;
4129 
4130 	case SND_SOC_DAPM_PRE_PMD:
4131 		snd_soc_dapm_widget_for_each_sink_path(w, path) {
4132 			sink = path->sink->priv;
4133 
4134 			snd_soc_dai_digital_mute(sink, 1, SNDRV_PCM_STREAM_PLAYBACK);
4135 			ret = 0;
4136 		}
4137 
4138 		substream->stream = SNDRV_PCM_STREAM_CAPTURE;
4139 		snd_soc_dapm_widget_for_each_source_path(w, path) {
4140 			source = path->source->priv;
4141 			snd_soc_dai_hw_free(source, substream, 0);
4142 		}
4143 
4144 		substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
4145 		snd_soc_dapm_widget_for_each_sink_path(w, path) {
4146 			sink = path->sink->priv;
4147 			snd_soc_dai_hw_free(sink, substream, 0);
4148 		}
4149 
4150 		substream->stream = SNDRV_PCM_STREAM_CAPTURE;
4151 		snd_soc_dapm_widget_for_each_source_path(w, path) {
4152 			source = path->source->priv;
4153 			snd_soc_dai_deactivate(source, substream->stream);
4154 			snd_soc_dai_shutdown(source, substream, 0);
4155 		}
4156 
4157 		substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
4158 		snd_soc_dapm_widget_for_each_sink_path(w, path) {
4159 			sink = path->sink->priv;
4160 			snd_soc_dai_deactivate(sink, substream->stream);
4161 			snd_soc_dai_shutdown(sink, substream, 0);
4162 		}
4163 		break;
4164 
4165 	case SND_SOC_DAPM_POST_PMD:
4166 		kfree(substream->runtime);
4167 		substream->runtime = NULL;
4168 		break;
4169 
4170 	default:
4171 		WARN(1, "Unknown event %d\n", event);
4172 		ret = -EINVAL;
4173 	}
4174 
4175 out:
4176 	/* Restore the substream direction */
4177 	substream->stream = saved_stream;
4178 	return ret;
4179 }
4180 
4181 static int dapm_dai_link_get(struct snd_kcontrol *kcontrol,
4182 			     struct snd_ctl_elem_value *ucontrol)
4183 {
4184 	struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4185 	struct snd_soc_pcm_runtime *rtd = w->priv;
4186 
4187 	ucontrol->value.enumerated.item[0] = rtd->c2c_params_select;
4188 
4189 	return 0;
4190 }
4191 
4192 static int dapm_dai_link_put(struct snd_kcontrol *kcontrol,
4193 			     struct snd_ctl_elem_value *ucontrol)
4194 {
4195 	struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4196 	struct snd_soc_pcm_runtime *rtd = w->priv;
4197 
4198 	/* Can't change the config when widget is already powered */
4199 	if (w->power)
4200 		return -EBUSY;
4201 
4202 	if (ucontrol->value.enumerated.item[0] == rtd->c2c_params_select)
4203 		return 0;
4204 
4205 	if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_c2c_params)
4206 		return -EINVAL;
4207 
4208 	rtd->c2c_params_select = ucontrol->value.enumerated.item[0];
4209 
4210 	return 1;
4211 }
4212 
4213 static void dapm_free_kcontrol(struct snd_soc_card *card,
4214 			       unsigned long *private_value,
4215 			       int num_c2c_params,
4216 			       const char **w_param_text)
4217 {
4218 	int count;
4219 
4220 	devm_kfree(card->dev, (void *)*private_value);
4221 
4222 	if (!w_param_text)
4223 		return;
4224 
4225 	for (count = 0 ; count < num_c2c_params; count++)
4226 		devm_kfree(card->dev, (void *)w_param_text[count]);
4227 	devm_kfree(card->dev, w_param_text);
4228 }
4229 
4230 static struct snd_kcontrol_new *
4231 dapm_alloc_kcontrol(struct snd_soc_card *card,
4232 			char *link_name,
4233 			const struct snd_soc_pcm_stream *c2c_params,
4234 			int num_c2c_params, const char **w_param_text,
4235 			unsigned long *private_value)
4236 {
4237 	struct soc_enum w_param_enum[] = {
4238 		SOC_ENUM_SINGLE(0, 0, 0, NULL),
4239 	};
4240 	struct snd_kcontrol_new kcontrol_dai_link[] = {
4241 		SOC_ENUM_EXT(NULL, w_param_enum[0],
4242 			     dapm_dai_link_get,
4243 			     dapm_dai_link_put),
4244 	};
4245 	struct snd_kcontrol_new *kcontrol_news;
4246 	const struct snd_soc_pcm_stream *config = c2c_params;
4247 	int count;
4248 
4249 	for (count = 0 ; count < num_c2c_params; count++) {
4250 		if (!config->stream_name) {
4251 			dev_warn(card->dev,
4252 				"ASoC: anonymous config %d for dai link %s\n",
4253 				count, link_name);
4254 			w_param_text[count] =
4255 				devm_kasprintf(card->dev, GFP_KERNEL,
4256 					       "Anonymous Configuration %d",
4257 					       count);
4258 		} else {
4259 			w_param_text[count] = devm_kmemdup(card->dev,
4260 						config->stream_name,
4261 						strlen(config->stream_name) + 1,
4262 						GFP_KERNEL);
4263 		}
4264 		if (!w_param_text[count])
4265 			goto outfree_w_param;
4266 		config++;
4267 	}
4268 
4269 	w_param_enum[0].items = num_c2c_params;
4270 	w_param_enum[0].texts = w_param_text;
4271 
4272 	*private_value =
4273 		(unsigned long) devm_kmemdup(card->dev,
4274 			(void *)(kcontrol_dai_link[0].private_value),
4275 			sizeof(struct soc_enum), GFP_KERNEL);
4276 	if (!*private_value) {
4277 		dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4278 			link_name);
4279 		goto outfree_w_param;
4280 	}
4281 	kcontrol_dai_link[0].private_value = *private_value;
4282 	/* duplicate kcontrol_dai_link on heap so that memory persists */
4283 	kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4284 					sizeof(struct snd_kcontrol_new),
4285 					GFP_KERNEL);
4286 	if (!kcontrol_news) {
4287 		dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4288 			link_name);
4289 		goto outfree_w_param;
4290 	}
4291 	return kcontrol_news;
4292 
4293 outfree_w_param:
4294 	dapm_free_kcontrol(card, private_value, num_c2c_params, w_param_text);
4295 
4296 	return NULL;
4297 }
4298 
4299 static struct snd_soc_dapm_widget *dapm_new_dai(struct snd_soc_card *card,
4300 						struct snd_pcm_substream *substream,
4301 						char *id)
4302 {
4303 	struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
4304 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
4305 	struct snd_soc_dapm_widget template;
4306 	struct snd_soc_dapm_widget *w;
4307 	const struct snd_kcontrol_new *kcontrol_news;
4308 	int num_kcontrols;
4309 	const char **w_param_text;
4310 	unsigned long private_value = 0;
4311 	char *link_name;
4312 	int ret = -ENOMEM;
4313 
4314 	link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
4315 				   rtd->dai_link->name, id);
4316 	if (!link_name)
4317 		goto name_fail;
4318 
4319 	/* allocate memory for control, only in case of multiple configs */
4320 	w_param_text	= NULL;
4321 	kcontrol_news	= NULL;
4322 	num_kcontrols	= 0;
4323 	if (rtd->dai_link->num_c2c_params > 1) {
4324 		w_param_text = devm_kcalloc(card->dev,
4325 					    rtd->dai_link->num_c2c_params,
4326 					    sizeof(char *), GFP_KERNEL);
4327 		if (!w_param_text)
4328 			goto param_fail;
4329 
4330 		num_kcontrols = 1;
4331 		kcontrol_news = dapm_alloc_kcontrol(card, link_name,
4332 						    rtd->dai_link->c2c_params,
4333 						    rtd->dai_link->num_c2c_params,
4334 						    w_param_text, &private_value);
4335 		if (!kcontrol_news)
4336 			goto param_fail;
4337 	}
4338 
4339 	memset(&template, 0, sizeof(template));
4340 	template.reg		= SND_SOC_NOPM;
4341 	template.id		= snd_soc_dapm_dai_link;
4342 	template.name		= link_name;
4343 	template.event		= dapm_dai_link_event;
4344 	template.event_flags	= SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
4345 				  SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD;
4346 	template.kcontrol_news	= kcontrol_news;
4347 	template.num_kcontrols	= num_kcontrols;
4348 
4349 	dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
4350 
4351 	w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4352 	if (IS_ERR(w)) {
4353 		ret = PTR_ERR(w);
4354 		goto outfree_kcontrol_news;
4355 	}
4356 
4357 	w->priv = substream;
4358 
4359 	return w;
4360 
4361 outfree_kcontrol_news:
4362 	devm_kfree(card->dev, (void *)template.kcontrol_news);
4363 	dapm_free_kcontrol(card, &private_value,
4364 				   rtd->dai_link->num_c2c_params, w_param_text);
4365 param_fail:
4366 	devm_kfree(card->dev, link_name);
4367 name_fail:
4368 	dev_err(rtd->dev, "ASoC: Failed to create %s-%s widget: %d\n",
4369 		rtd->dai_link->name, id, ret);
4370 	return ERR_PTR(ret);
4371 }
4372 
4373 /**
4374  * snd_soc_dapm_new_dai_widgets - Create new DAPM widgets
4375  * @dapm: DAPM context
4376  * @dai: parent DAI
4377  *
4378  * Returns 0 on success, error code otherwise.
4379  */
4380 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4381 				 struct snd_soc_dai *dai)
4382 {
4383 	struct device *dev = snd_soc_dapm_to_dev(dapm);
4384 	struct snd_soc_dapm_widget template;
4385 	struct snd_soc_dapm_widget *w;
4386 
4387 	WARN_ON(dev != dai->dev);
4388 
4389 	memset(&template, 0, sizeof(template));
4390 	template.reg = SND_SOC_NOPM;
4391 
4392 	if (dai->driver->playback.stream_name) {
4393 		template.id = snd_soc_dapm_dai_in;
4394 		template.name = dai->driver->playback.stream_name;
4395 		template.sname = dai->driver->playback.stream_name;
4396 
4397 		dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4398 			template.name);
4399 
4400 		w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4401 		if (IS_ERR(w))
4402 			return PTR_ERR(w);
4403 
4404 		w->priv = dai;
4405 		snd_soc_dai_set_widget_playback(dai, w);
4406 	}
4407 
4408 	if (dai->driver->capture.stream_name) {
4409 		template.id = snd_soc_dapm_dai_out;
4410 		template.name = dai->driver->capture.stream_name;
4411 		template.sname = dai->driver->capture.stream_name;
4412 
4413 		dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4414 			template.name);
4415 
4416 		w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4417 		if (IS_ERR(w))
4418 			return PTR_ERR(w);
4419 
4420 		w->priv = dai;
4421 		snd_soc_dai_set_widget_capture(dai, w);
4422 	}
4423 
4424 	return 0;
4425 }
4426 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_dai_widgets);
4427 
4428 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4429 {
4430 	struct snd_soc_dapm_widget *dai_w, *w;
4431 	struct snd_soc_dapm_widget *src, *sink;
4432 	struct snd_soc_dai *dai;
4433 
4434 	/* For each DAI widget... */
4435 	for_each_card_widgets(card, dai_w) {
4436 		switch (dai_w->id) {
4437 		case snd_soc_dapm_dai_in:
4438 		case snd_soc_dapm_dai_out:
4439 			break;
4440 		default:
4441 			continue;
4442 		}
4443 
4444 		/* let users know there is no DAI to link */
4445 		if (!dai_w->priv) {
4446 			dev_dbg(card->dev, "dai widget %s has no DAI\n",
4447 				dai_w->name);
4448 			continue;
4449 		}
4450 
4451 		dai = dai_w->priv;
4452 
4453 		/* ...find all widgets with the same stream and link them */
4454 		for_each_card_widgets(card, w) {
4455 			if (w->dapm != dai_w->dapm)
4456 				continue;
4457 
4458 			switch (w->id) {
4459 			case snd_soc_dapm_dai_in:
4460 			case snd_soc_dapm_dai_out:
4461 				continue;
4462 			default:
4463 				break;
4464 			}
4465 
4466 			if (!w->sname || !strstr(w->sname, dai_w->sname))
4467 				continue;
4468 
4469 			if (dai_w->id == snd_soc_dapm_dai_in) {
4470 				src = dai_w;
4471 				sink = w;
4472 			} else {
4473 				src = w;
4474 				sink = dai_w;
4475 			}
4476 			dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4477 			dapm_add_path(w->dapm, src, sink, NULL, NULL);
4478 		}
4479 	}
4480 
4481 	return 0;
4482 }
4483 
4484 static void dapm_connect_dai_routes(struct snd_soc_dapm_context *dapm,
4485 				    struct snd_soc_dai *src_dai,
4486 				    struct snd_soc_dapm_widget *src,
4487 				    struct snd_soc_dapm_widget *dai,
4488 				    struct snd_soc_dai *sink_dai,
4489 				    struct snd_soc_dapm_widget *sink)
4490 {
4491 	struct device *dev = snd_soc_dapm_to_dev(dapm);
4492 
4493 	dev_dbg(dev, "connected DAI link %s:%s -> %s:%s\n",
4494 		src_dai->component->name, src->name,
4495 		sink_dai->component->name, sink->name);
4496 
4497 	if (dai) {
4498 		dapm_add_path(dapm, src, dai, NULL, NULL);
4499 		src = dai;
4500 	}
4501 
4502 	dapm_add_path(dapm, src, sink, NULL, NULL);
4503 }
4504 
4505 static void dapm_connect_dai_pair(struct snd_soc_card *card,
4506 				  struct snd_soc_pcm_runtime *rtd,
4507 				  struct snd_soc_dai *codec_dai,
4508 				  struct snd_soc_dai *cpu_dai)
4509 {
4510 	struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
4511 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
4512 	struct snd_soc_dapm_widget *codec, *cpu;
4513 	struct snd_soc_dai *src_dai[]		= { cpu_dai,	codec_dai };
4514 	struct snd_soc_dai *sink_dai[]		= { codec_dai,	cpu_dai };
4515 	struct snd_soc_dapm_widget **src[]	= { &cpu,	&codec };
4516 	struct snd_soc_dapm_widget **sink[]	= { &codec,	&cpu };
4517 	char *widget_name[]			= { "playback",	"capture" };
4518 	int stream;
4519 
4520 	for_each_pcm_streams(stream) {
4521 		int stream_cpu, stream_codec;
4522 
4523 		stream_cpu	= snd_soc_get_stream_cpu(dai_link, stream);
4524 		stream_codec	= stream;
4525 
4526 		/* connect BE DAI playback if widgets are valid */
4527 		cpu	= snd_soc_dai_get_widget(cpu_dai,	stream_cpu);
4528 		codec	= snd_soc_dai_get_widget(codec_dai,	stream_codec);
4529 
4530 		if (!cpu || !codec)
4531 			continue;
4532 
4533 		/* special handling for [Codec2Codec] */
4534 		if (dai_link->c2c_params && !rtd->c2c_widget[stream]) {
4535 			struct snd_pcm_substream *substream = rtd->pcm->streams[stream].substream;
4536 			struct snd_soc_dapm_widget *dai = dapm_new_dai(card, substream,
4537 								       widget_name[stream]);
4538 
4539 			if (IS_ERR(dai))
4540 				continue;
4541 
4542 			rtd->c2c_widget[stream] = dai;
4543 		}
4544 
4545 		dapm_connect_dai_routes(dapm, src_dai[stream], *src[stream],
4546 					rtd->c2c_widget[stream],
4547 					sink_dai[stream], *sink[stream]);
4548 	}
4549 }
4550 
4551 static void dapm_dai_stream_event(struct snd_soc_dai *dai, int stream, int event)
4552 {
4553 	struct snd_soc_dapm_widget *w;
4554 
4555 	w = snd_soc_dai_get_widget(dai, stream);
4556 
4557 	if (w) {
4558 		unsigned int ep;
4559 
4560 		dapm_mark_dirty(w, "stream event");
4561 
4562 		if (w->id == snd_soc_dapm_dai_in) {
4563 			ep = SND_SOC_DAPM_EP_SOURCE;
4564 			dapm_widget_invalidate_input_paths(w);
4565 		} else {
4566 			ep = SND_SOC_DAPM_EP_SINK;
4567 			dapm_widget_invalidate_output_paths(w);
4568 		}
4569 
4570 		switch (event) {
4571 		case SND_SOC_DAPM_STREAM_START:
4572 			w->active = 1;
4573 			w->is_ep = ep;
4574 			break;
4575 		case SND_SOC_DAPM_STREAM_STOP:
4576 			w->active = 0;
4577 			w->is_ep = 0;
4578 			break;
4579 		case SND_SOC_DAPM_STREAM_SUSPEND:
4580 		case SND_SOC_DAPM_STREAM_RESUME:
4581 		case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4582 		case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4583 			break;
4584 		}
4585 	}
4586 }
4587 
4588 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4589 {
4590 	struct snd_soc_pcm_runtime *rtd;
4591 	struct snd_soc_dai *cpu_dai;
4592 	struct snd_soc_dai *codec_dai;
4593 
4594 	/* for each BE DAI link... */
4595 	for_each_card_rtds(card, rtd)  {
4596 		struct snd_soc_dai_link_ch_map *ch_maps;
4597 		int i;
4598 
4599 		/*
4600 		 * dynamic FE links have no fixed DAI mapping.
4601 		 * CODEC<->CODEC links have no direct connection.
4602 		 */
4603 		if (rtd->dai_link->dynamic)
4604 			continue;
4605 
4606 		/*
4607 		 * see
4608 		 *	soc.h :: [dai_link->ch_maps Image sample]
4609 		 */
4610 		for_each_rtd_ch_maps(rtd, i, ch_maps) {
4611 			cpu_dai   = snd_soc_rtd_to_cpu(rtd,   ch_maps->cpu);
4612 			codec_dai = snd_soc_rtd_to_codec(rtd, ch_maps->codec);
4613 
4614 			dapm_connect_dai_pair(card, rtd, codec_dai, cpu_dai);
4615 		}
4616 	}
4617 }
4618 
4619 int snd_soc_dapm_ignore_suspend_widgets(struct snd_soc_card *card)
4620 {
4621 	struct snd_soc_dapm_widget *w;
4622 	int i;
4623 
4624 	for (i = 0; i < card->num_ignore_suspend_widgets; i++) {
4625 		w = dapm_find_widget(snd_soc_card_to_dapm(card),
4626 				     card->ignore_suspend_widgets[i], true);
4627 		if (!w) {
4628 			dev_err(card->dev, "ASoC: DAPM unknown ignore suspend widget %s\n",
4629 				card->ignore_suspend_widgets[i]);
4630 			return -EINVAL;
4631 		}
4632 		w->ignore_suspend = 1;
4633 	}
4634 
4635 	for (i = 0; i < card->num_of_ignore_suspend_widgets; i++) {
4636 		w = dapm_find_widget(snd_soc_card_to_dapm(card),
4637 				     card->of_ignore_suspend_widgets[i], true);
4638 		if (!w) {
4639 			dev_err(card->dev, "ASoC: DAPM unknown ignore suspend widget %s\n",
4640 				card->of_ignore_suspend_widgets[i]);
4641 			return -EINVAL;
4642 		}
4643 		w->ignore_suspend = 1;
4644 	}
4645 
4646 	return 0;
4647 }
4648 
4649 static void dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream, int event)
4650 {
4651 	struct snd_soc_dai *dai;
4652 	int i;
4653 
4654 	for_each_rtd_dais(rtd, i, dai)
4655 		dapm_dai_stream_event(dai, stream, event);
4656 
4657 	dapm_power_widgets(rtd->card, event, NULL);
4658 }
4659 
4660 /**
4661  * snd_soc_dapm_stream_event - send a stream event to the dapm core
4662  * @rtd: PCM runtime data
4663  * @stream: stream name
4664  * @event: stream event
4665  *
4666  * Sends a stream event to the dapm core. The core then makes any
4667  * necessary widget power changes.
4668  *
4669  * Returns 0 for success else error.
4670  */
4671 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4672 			      int event)
4673 {
4674 	struct snd_soc_card *card = rtd->card;
4675 
4676 	snd_soc_dapm_mutex_lock(card);
4677 	dapm_stream_event(rtd, stream, event);
4678 	snd_soc_dapm_mutex_unlock(card);
4679 }
4680 
4681 void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream)
4682 {
4683 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
4684 		if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
4685 			/* powered down playback stream now */
4686 			snd_soc_dapm_stream_event(rtd,
4687 						  SNDRV_PCM_STREAM_PLAYBACK,
4688 						  SND_SOC_DAPM_STREAM_STOP);
4689 		} else {
4690 			/* start delayed pop wq here for playback streams */
4691 			rtd->pop_wait = 1;
4692 			queue_delayed_work(system_power_efficient_wq,
4693 					   &rtd->delayed_work,
4694 					   msecs_to_jiffies(rtd->pmdown_time));
4695 		}
4696 	} else {
4697 		/* capture streams can be powered down now */
4698 		snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
4699 					  SND_SOC_DAPM_STREAM_STOP);
4700 	}
4701 }
4702 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop);
4703 
4704 /**
4705  * snd_soc_dapm_enable_pin_unlocked - enable pin.
4706  * @dapm: DAPM context
4707  * @pin: pin name
4708  *
4709  * Enables input/output pin and its parents or children widgets iff there is
4710  * a valid audio route and active audio stream.
4711  *
4712  * Requires external locking.
4713  *
4714  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4715  * do any widget power switching.
4716  */
4717 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4718 				   const char *pin)
4719 {
4720 	return dapm_set_pin(dapm, pin, 1);
4721 }
4722 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4723 
4724 /**
4725  * snd_soc_dapm_enable_pin - enable pin.
4726  * @dapm: DAPM context
4727  * @pin: pin name
4728  *
4729  * Enables input/output pin and its parents or children widgets iff there is
4730  * a valid audio route and active audio stream.
4731  *
4732  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4733  * do any widget power switching.
4734  */
4735 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4736 {
4737 	int ret;
4738 
4739 	snd_soc_dapm_mutex_lock(dapm);
4740 
4741 	ret = dapm_set_pin(dapm, pin, 1);
4742 
4743 	snd_soc_dapm_mutex_unlock(dapm);
4744 
4745 	return ret;
4746 }
4747 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4748 
4749 /**
4750  * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4751  * @dapm: DAPM context
4752  * @pin: pin name
4753  *
4754  * Enables input/output pin regardless of any other state.  This is
4755  * intended for use with microphone bias supplies used in microphone
4756  * jack detection.
4757  *
4758  * Requires external locking.
4759  *
4760  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4761  * do any widget power switching.
4762  */
4763 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4764 					 const char *pin)
4765 {
4766 	struct device *dev;
4767 	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4768 
4769 	if (!w) {
4770 		dev = snd_soc_dapm_to_dev(dapm);
4771 
4772 		dev_err(dev, "ASoC: unknown pin %s\n", pin);
4773 		return -EINVAL;
4774 	}
4775 
4776 	dev = snd_soc_dapm_to_dev(w->dapm);
4777 
4778 	dev_dbg(dev, "ASoC: force enable pin %s\n", pin);
4779 	if (!w->connected) {
4780 		/*
4781 		 * w->force does not affect the number of input or output paths,
4782 		 * so we only have to recheck if w->connected is changed
4783 		 */
4784 		dapm_widget_invalidate_input_paths(w);
4785 		dapm_widget_invalidate_output_paths(w);
4786 		w->connected = 1;
4787 	}
4788 	w->force = 1;
4789 	dapm_mark_dirty(w, "force enable");
4790 
4791 	return 0;
4792 }
4793 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4794 
4795 /**
4796  * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4797  * @dapm: DAPM context
4798  * @pin: pin name
4799  *
4800  * Enables input/output pin regardless of any other state.  This is
4801  * intended for use with microphone bias supplies used in microphone
4802  * jack detection.
4803  *
4804  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4805  * do any widget power switching.
4806  */
4807 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4808 				  const char *pin)
4809 {
4810 	int ret;
4811 
4812 	snd_soc_dapm_mutex_lock(dapm);
4813 
4814 	ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4815 
4816 	snd_soc_dapm_mutex_unlock(dapm);
4817 
4818 	return ret;
4819 }
4820 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4821 
4822 /**
4823  * snd_soc_dapm_disable_pin_unlocked - disable pin.
4824  * @dapm: DAPM context
4825  * @pin: pin name
4826  *
4827  * Disables input/output pin and its parents or children widgets.
4828  *
4829  * Requires external locking.
4830  *
4831  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4832  * do any widget power switching.
4833  */
4834 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4835 				    const char *pin)
4836 {
4837 	return dapm_set_pin(dapm, pin, 0);
4838 }
4839 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4840 
4841 /**
4842  * snd_soc_dapm_disable_pin - disable pin.
4843  * @dapm: DAPM context
4844  * @pin: pin name
4845  *
4846  * Disables input/output pin and its parents or children widgets.
4847  *
4848  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4849  * do any widget power switching.
4850  */
4851 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4852 			     const char *pin)
4853 {
4854 	int ret;
4855 
4856 	snd_soc_dapm_mutex_lock(dapm);
4857 
4858 	ret = dapm_set_pin(dapm, pin, 0);
4859 
4860 	snd_soc_dapm_mutex_unlock(dapm);
4861 
4862 	return ret;
4863 }
4864 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4865 
4866 /**
4867  * snd_soc_dapm_get_pin_status - get audio pin status
4868  * @dapm: DAPM context
4869  * @pin: audio signal pin endpoint (or start point)
4870  *
4871  * Get audio pin status - connected or disconnected.
4872  *
4873  * Returns 1 for connected otherwise 0.
4874  */
4875 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4876 				const char *pin)
4877 {
4878 	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4879 
4880 	if (w)
4881 		return w->connected;
4882 
4883 	return 0;
4884 }
4885 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4886 
4887 /**
4888  * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4889  * @dapm: DAPM context
4890  * @pin: audio signal pin endpoint (or start point)
4891  *
4892  * Mark the given endpoint or pin as ignoring suspend.  When the
4893  * system is disabled a path between two endpoints flagged as ignoring
4894  * suspend will not be disabled.  The path must already be enabled via
4895  * normal means at suspend time, it will not be turned on if it was not
4896  * already enabled.
4897  */
4898 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4899 				const char *pin)
4900 {
4901 	struct device *dev = snd_soc_dapm_to_dev(dapm);
4902 	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4903 
4904 	if (!w) {
4905 		dev_err(dev, "ASoC: unknown pin %s\n", pin);
4906 		return -EINVAL;
4907 	}
4908 
4909 	w->ignore_suspend = 1;
4910 
4911 	return 0;
4912 }
4913 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4914 
4915 /**
4916  * snd_soc_dapm_pin_has_prefix - check if given pin has a known prefix
4917  * @card: card to be checked
4918  * @pin: pin name
4919  *
4920  * Returns true if given pin has a known prefix
4921  */
4922 bool snd_soc_dapm_pin_has_prefix(struct snd_soc_card *card, const char *pin)
4923 {
4924 	struct snd_soc_component *component;
4925 	const char *prefix;
4926 	size_t prefix_len;
4927 
4928 	for_each_card_components(card, component) {
4929 		prefix = component->name_prefix;
4930 		if (!prefix)
4931 			continue;
4932 
4933 		prefix_len = strlen(prefix);
4934 		if (!strncmp(pin, prefix, prefix_len) && pin[prefix_len] == ' ')
4935 			return true;
4936 	}
4937 
4938 	return false;
4939 }
4940 EXPORT_SYMBOL_GPL(snd_soc_dapm_pin_has_prefix);
4941 
4942 /**
4943  * snd_soc_dapm_free - free dapm resources
4944  * @dapm: DAPM context
4945  *
4946  * Free all dapm widgets and resources.
4947  */
4948 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4949 {
4950 	dapm_debugfs_cleanup(dapm);
4951 	dapm_free_widgets(dapm);
4952 	list_del(&dapm->list);
4953 }
4954 
4955 void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
4956 		       struct snd_soc_card *card,
4957 		       struct snd_soc_component *component)
4958 {
4959 	dapm->card		= card;
4960 	dapm->component		= component;
4961 	dapm->bias_level	= SND_SOC_BIAS_OFF;
4962 
4963 	if (component)
4964 		dapm->idle_bias		= component->driver->idle_bias_on;
4965 
4966 	INIT_LIST_HEAD(&dapm->list);
4967 	/* see for_each_card_dapms */
4968 	list_add(&dapm->list, &card->dapm_list);
4969 }
4970 
4971 static void dapm_shutdown(struct snd_soc_dapm_context *dapm)
4972 {
4973 	struct snd_soc_card *card = dapm->card;
4974 	struct snd_soc_dapm_widget *w;
4975 	LIST_HEAD(down_list);
4976 	int powerdown = 0;
4977 
4978 	snd_soc_dapm_mutex_lock_root(card);
4979 
4980 	for_each_card_widgets(dapm->card, w) {
4981 		if (w->dapm != dapm)
4982 			continue;
4983 		if (w->power) {
4984 			dapm_seq_insert(w, &down_list, false);
4985 			w->new_power = 0;
4986 			powerdown = 1;
4987 		}
4988 	}
4989 
4990 	/* If there were no widgets to power down we're already in
4991 	 * standby.
4992 	 */
4993 	if (powerdown) {
4994 		if (dapm->bias_level == SND_SOC_BIAS_ON)
4995 			snd_soc_dapm_set_bias_level(dapm,
4996 						    SND_SOC_BIAS_PREPARE);
4997 		dapm_seq_run(card, &down_list, 0, false);
4998 		if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4999 			snd_soc_dapm_set_bias_level(dapm,
5000 						    SND_SOC_BIAS_STANDBY);
5001 	}
5002 
5003 	snd_soc_dapm_mutex_unlock(card);
5004 }
5005 
5006 /*
5007  * snd_soc_dapm_shutdown - callback for system shutdown
5008  */
5009 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
5010 {
5011 	struct snd_soc_dapm_context *card_dapm = snd_soc_card_to_dapm(card);
5012 	struct snd_soc_dapm_context *dapm;
5013 
5014 	for_each_card_dapms(card, dapm) {
5015 		if (dapm != card_dapm) {
5016 			dapm_shutdown(dapm);
5017 			if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
5018 				snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_OFF);
5019 		}
5020 	}
5021 
5022 	dapm_shutdown(card_dapm);
5023 	if (card_dapm->bias_level == SND_SOC_BIAS_STANDBY)
5024 		snd_soc_dapm_set_bias_level(card_dapm, SND_SOC_BIAS_OFF);
5025 }
5026 
5027 /* Module information */
5028 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
5029 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
5030 MODULE_LICENSE("GPL");
5031