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