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