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