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