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