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