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