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