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