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