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