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