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