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