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