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