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