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