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