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