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