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