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