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