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