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