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