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