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