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