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