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