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