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