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