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