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