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