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