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