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