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