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