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