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