]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - sound/soc/soc-dapm.c
ASoC: dapm: Revert "ASoC: dapm: Fix double prefix addition"
[mirror_ubuntu-zesty-kernel.git] / sound / soc / soc-dapm.c
CommitLineData
2b97eabc
RP
1/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
d331124d 5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
2b97eabc
RP
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
2b97eabc
RP
12 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
74b8f955 15 * DACs/ADCs.
2b97eabc 16 * o Platform power domain - can support external components i.e. amps and
612a3fec 17 * mic/headphone insertion events.
2b97eabc
RP
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
612a3fec 21 * o Delayed power down of audio subsystem to reduce pops between a quick
2b97eabc
RP
22 * device reopen.
23 *
2b97eabc
RP
24 */
25
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/init.h>
9d0624a7 29#include <linux/async.h>
2b97eabc
RP
30#include <linux/delay.h>
31#include <linux/pm.h>
32#include <linux/bitops.h>
33#include <linux/platform_device.h>
34#include <linux/jiffies.h>
20496ff3 35#include <linux/debugfs.h>
f1aac484 36#include <linux/pm_runtime.h>
62ea874a 37#include <linux/regulator/consumer.h>
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,
a4e9154c 2384 const struct snd_soc_dapm_route *route)
2553628e
LPC
2385{
2386 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2387 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2388 const char *sink;
2389 const char *source;
2390 char prefixed_sink[80];
2391 char prefixed_source[80];
2392 int ret;
2393
a4e9154c 2394 if (dapm->codec && dapm->codec->name_prefix) {
2553628e
LPC
2395 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2396 dapm->codec->name_prefix, route->sink);
2397 sink = prefixed_sink;
2398 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2399 dapm->codec->name_prefix, route->source);
2400 source = prefixed_source;
2401 } else {
2402 sink = route->sink;
2403 source = route->source;
2404 }
2405
2406 /*
2407 * find src and dest widgets over all widgets but favor a widget from
2408 * current DAPM context
2409 */
2410 list_for_each_entry(w, &dapm->card->widgets, list) {
2411 if (!wsink && !(strcmp(w->name, sink))) {
2412 wtsink = w;
2413 if (w->dapm == dapm)
2414 wsink = w;
2415 continue;
2416 }
2417 if (!wsource && !(strcmp(w->name, source))) {
2418 wtsource = w;
2419 if (w->dapm == dapm)
2420 wsource = w;
2421 }
2422 }
2423 /* use widget from another DAPM context if not found from this */
2424 if (!wsink)
2425 wsink = wtsink;
2426 if (!wsource)
2427 wsource = wtsource;
2428
2429 if (wsource == NULL) {
2430 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2431 route->source);
2432 return -ENODEV;
2433 }
2434 if (wsink == NULL) {
2435 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2436 route->sink);
2437 return -ENODEV;
2438 }
2439
2440 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2441 route->connected);
2442 if (ret)
2443 goto err;
2444
2445 return 0;
2b97eabc 2446err:
30a6a1a4 2447 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2553628e 2448 source, route->control, sink);
2b97eabc
RP
2449 return ret;
2450}
105f1c28 2451
efcc3c61
MB
2452static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2453 const struct snd_soc_dapm_route *route)
2454{
2455 struct snd_soc_dapm_path *path, *p;
2456 const char *sink;
2457 const char *source;
2458 char prefixed_sink[80];
2459 char prefixed_source[80];
2460
2461 if (route->control) {
2462 dev_err(dapm->dev,
30a6a1a4 2463 "ASoC: Removal of routes with controls not supported\n");
efcc3c61
MB
2464 return -EINVAL;
2465 }
2466
2467 if (dapm->codec && dapm->codec->name_prefix) {
2468 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2469 dapm->codec->name_prefix, route->sink);
2470 sink = prefixed_sink;
2471 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2472 dapm->codec->name_prefix, route->source);
2473 source = prefixed_source;
2474 } else {
2475 sink = route->sink;
2476 source = route->source;
2477 }
2478
2479 path = NULL;
2480 list_for_each_entry(p, &dapm->card->paths, list) {
2481 if (strcmp(p->source->name, source) != 0)
2482 continue;
2483 if (strcmp(p->sink->name, sink) != 0)
2484 continue;
2485 path = p;
2486 break;
2487 }
2488
2489 if (path) {
2490 dapm_mark_dirty(path->source, "Route removed");
2491 dapm_mark_dirty(path->sink, "Route removed");
2492
8872293f 2493 dapm_free_path(path);
efcc3c61 2494 } else {
30a6a1a4 2495 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
efcc3c61
MB
2496 source, sink);
2497 }
2498
2499 return 0;
2500}
2501
105f1c28
MB
2502/**
2503 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
ce6120cc 2504 * @dapm: DAPM context
105f1c28
MB
2505 * @route: audio routes
2506 * @num: number of routes
2507 *
2508 * Connects 2 dapm widgets together via a named audio path. The sink is
2509 * the widget receiving the audio signal, whilst the source is the sender
2510 * of the audio signal.
2511 *
2512 * Returns 0 for success else error. On error all resources can be freed
2513 * with a call to snd_soc_card_free().
2514 */
ce6120cc 2515int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
105f1c28
MB
2516 const struct snd_soc_dapm_route *route, int num)
2517{
62d4a4b9 2518 int i, r, ret = 0;
105f1c28 2519
a73fb2df 2520 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
105f1c28 2521 for (i = 0; i < num; i++) {
a4e9154c 2522 r = snd_soc_dapm_add_route(dapm, route);
62d4a4b9 2523 if (r < 0) {
30a6a1a4
LG
2524 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2525 route->source,
2526 route->control ? route->control : "direct",
2527 route->sink);
62d4a4b9 2528 ret = r;
105f1c28
MB
2529 }
2530 route++;
2531 }
a73fb2df 2532 mutex_unlock(&dapm->card->dapm_mutex);
105f1c28 2533
60884c27 2534 return ret;
105f1c28
MB
2535}
2536EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2537
efcc3c61
MB
2538/**
2539 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2540 * @dapm: DAPM context
2541 * @route: audio routes
2542 * @num: number of routes
2543 *
2544 * Removes routes from the DAPM context.
2545 */
2546int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2547 const struct snd_soc_dapm_route *route, int num)
2548{
2549 int i, ret = 0;
2550
2551 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2552 for (i = 0; i < num; i++) {
2553 snd_soc_dapm_del_route(dapm, route);
2554 route++;
2555 }
2556 mutex_unlock(&dapm->card->dapm_mutex);
2557
2558 return ret;
2559}
2560EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2561
bf3a9e13
MB
2562static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2563 const struct snd_soc_dapm_route *route)
2564{
2565 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2566 route->source,
2567 true);
2568 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2569 route->sink,
2570 true);
2571 struct snd_soc_dapm_path *path;
2572 int count = 0;
2573
2574 if (!source) {
30a6a1a4 2575 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
bf3a9e13
MB
2576 route->source);
2577 return -ENODEV;
2578 }
2579
2580 if (!sink) {
30a6a1a4 2581 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
bf3a9e13
MB
2582 route->sink);
2583 return -ENODEV;
2584 }
2585
2586 if (route->control || route->connected)
30a6a1a4 2587 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
bf3a9e13
MB
2588 route->source, route->sink);
2589
2590 list_for_each_entry(path, &source->sinks, list_source) {
2591 if (path->sink == sink) {
2592 path->weak = 1;
2593 count++;
2594 }
2595 }
2596
2597 if (count == 0)
30a6a1a4 2598 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
bf3a9e13
MB
2599 route->source, route->sink);
2600 if (count > 1)
30a6a1a4 2601 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
bf3a9e13
MB
2602 count, route->source, route->sink);
2603
2604 return 0;
2605}
2606
2607/**
2608 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2609 * @dapm: DAPM context
2610 * @route: audio routes
2611 * @num: number of routes
2612 *
2613 * Mark existing routes matching those specified in the passed array
2614 * as being weak, meaning that they are ignored for the purpose of
2615 * power decisions. The main intended use case is for sidetone paths
2616 * which couple audio between other independent paths if they are both
2617 * active in order to make the combination work better at the user
2618 * level but which aren't intended to be "used".
2619 *
2620 * Note that CODEC drivers should not use this as sidetone type paths
2621 * can frequently also be used as bypass paths.
2622 */
2623int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2624 const struct snd_soc_dapm_route *route, int num)
2625{
2626 int i, err;
2627 int ret = 0;
2628
a73fb2df 2629 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
bf3a9e13
MB
2630 for (i = 0; i < num; i++) {
2631 err = snd_soc_dapm_weak_route(dapm, route);
2632 if (err)
2633 ret = err;
2634 route++;
2635 }
a73fb2df 2636 mutex_unlock(&dapm->card->dapm_mutex);
bf3a9e13
MB
2637
2638 return ret;
2639}
2640EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2641
2b97eabc
RP
2642/**
2643 * snd_soc_dapm_new_widgets - add new dapm widgets
ce6120cc 2644 * @dapm: DAPM context
2b97eabc
RP
2645 *
2646 * Checks the codec for any new dapm widgets and creates them if found.
2647 *
2648 * Returns 0 for success.
2649 */
824ef826 2650int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
2b97eabc
RP
2651{
2652 struct snd_soc_dapm_widget *w;
b66a70d5 2653 unsigned int val;
2b97eabc 2654
95dd5cd6 2655 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
a73fb2df 2656
95dd5cd6 2657 list_for_each_entry(w, &card->widgets, list)
2b97eabc
RP
2658 {
2659 if (w->new)
2660 continue;
2661
fad59888
SW
2662 if (w->num_kcontrols) {
2663 w->kcontrols = kzalloc(w->num_kcontrols *
2664 sizeof(struct snd_kcontrol *),
2665 GFP_KERNEL);
a73fb2df 2666 if (!w->kcontrols) {
95dd5cd6 2667 mutex_unlock(&card->dapm_mutex);
fad59888 2668 return -ENOMEM;
a73fb2df 2669 }
fad59888
SW
2670 }
2671
2b97eabc
RP
2672 switch(w->id) {
2673 case snd_soc_dapm_switch:
2674 case snd_soc_dapm_mixer:
ca9c1aae 2675 case snd_soc_dapm_mixer_named_ctl:
4b80b8c2 2676 dapm_new_mixer(w);
2b97eabc
RP
2677 break;
2678 case snd_soc_dapm_mux:
4b80b8c2 2679 dapm_new_mux(w);
2b97eabc 2680 break;
2b97eabc 2681 case snd_soc_dapm_pga:
d88429a6 2682 case snd_soc_dapm_out_drv:
4b80b8c2 2683 dapm_new_pga(w);
2b97eabc 2684 break;
7ca3a18b 2685 default:
2b97eabc
RP
2686 break;
2687 }
b66a70d5
MB
2688
2689 /* Read the initial power state from the device */
2690 if (w->reg >= 0) {
f7d3c170
ASL
2691 soc_widget_read(w, w->reg, &val);
2692 val = val >> w->shift;
de9ba98b
LPC
2693 val &= w->mask;
2694 if (val == w->on_val)
b66a70d5
MB
2695 w->power = 1;
2696 }
2697
2b97eabc 2698 w->new = 1;
d5d1e0be 2699
7508b12a 2700 dapm_mark_dirty(w, "new widget");
d5d1e0be 2701 dapm_debugfs_add_widget(w);
2b97eabc
RP
2702 }
2703
95dd5cd6
LPC
2704 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2705 mutex_unlock(&card->dapm_mutex);
2b97eabc
RP
2706 return 0;
2707}
2708EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2709
2710/**
2711 * snd_soc_dapm_get_volsw - dapm mixer get callback
2712 * @kcontrol: mixer control
ac11a2b3 2713 * @ucontrol: control element information
2b97eabc
RP
2714 *
2715 * Callback to get the value of a dapm mixer control.
2716 *
2717 * Returns 0 for success.
2718 */
2719int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2720 struct snd_ctl_elem_value *ucontrol)
2721{
eee5d7f9 2722 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
57295073 2723 struct snd_soc_card *card = codec->card;
4eaa9819
JS
2724 struct soc_mixer_control *mc =
2725 (struct soc_mixer_control *)kcontrol->private_value;
249ce138 2726 int reg = mc->reg;
815ecf8d 2727 unsigned int shift = mc->shift;
4eaa9819 2728 int max = mc->max;
815ecf8d 2729 unsigned int mask = (1 << fls(max)) - 1;
da602ab8 2730 unsigned int invert = mc->invert;
57295073 2731 unsigned int val;
da602ab8
BT
2732
2733 if (snd_soc_volsw_is_stereo(mc))
eee5d7f9 2734 dev_warn(codec->dapm.dev,
30a6a1a4 2735 "ASoC: Control '%s' is stereo, which is not supported\n",
da602ab8 2736 kcontrol->id.name);
2b97eabc 2737
57295073 2738 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
249ce138 2739 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
57295073
LPC
2740 val = (snd_soc_read(codec, reg) >> shift) & mask;
2741 else
2742 val = dapm_kcontrol_get_value(kcontrol);
2743 mutex_unlock(&card->dapm_mutex);
2744
da602ab8 2745 if (invert)
57295073
LPC
2746 ucontrol->value.integer.value[0] = max - val;
2747 else
2748 ucontrol->value.integer.value[0] = val;
2b97eabc
RP
2749
2750 return 0;
2751}
2752EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2753
2754/**
2755 * snd_soc_dapm_put_volsw - dapm mixer set callback
2756 * @kcontrol: mixer control
ac11a2b3 2757 * @ucontrol: control element information
2b97eabc
RP
2758 *
2759 * Callback to set the value of a dapm mixer control.
2760 *
2761 * Returns 0 for success.
2762 */
2763int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2764 struct snd_ctl_elem_value *ucontrol)
2765{
eee5d7f9 2766 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
a73fb2df 2767 struct snd_soc_card *card = codec->card;
4eaa9819
JS
2768 struct soc_mixer_control *mc =
2769 (struct soc_mixer_control *)kcontrol->private_value;
249ce138 2770 int reg = mc->reg;
815ecf8d 2771 unsigned int shift = mc->shift;
4eaa9819 2772 int max = mc->max;
815ecf8d
JS
2773 unsigned int mask = (1 << fls(max)) - 1;
2774 unsigned int invert = mc->invert;
e9cf7049 2775 unsigned int val;
97404f2e
MB
2776 int connect, change;
2777 struct snd_soc_dapm_update update;
52765976 2778 int ret = 0;
2b97eabc 2779
da602ab8 2780 if (snd_soc_volsw_is_stereo(mc))
eee5d7f9 2781 dev_warn(codec->dapm.dev,
30a6a1a4 2782 "ASoC: Control '%s' is stereo, which is not supported\n",
da602ab8
BT
2783 kcontrol->id.name);
2784
2b97eabc 2785 val = (ucontrol->value.integer.value[0] & mask);
8a720718 2786 connect = !!val;
2b97eabc
RP
2787
2788 if (invert)
a7a4ac86 2789 val = max - val;
2b97eabc 2790
3cd04343 2791 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2b97eabc 2792
249ce138 2793 change = dapm_kcontrol_set_value(kcontrol, val);
97404f2e 2794 if (change) {
249ce138 2795 if (reg != SND_SOC_NOPM) {
c9e065c2
LPC
2796 mask = mask << shift;
2797 val = val << shift;
2798
2799 if (snd_soc_test_bits(codec, reg, mask, val)) {
2800 update.kcontrol = kcontrol;
2801 update.reg = reg;
2802 update.mask = mask;
2803 update.val = val;
2804 card->update = &update;
2805 }
97404f2e 2806
249ce138 2807 }
97404f2e 2808
52765976 2809 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
fafd2176 2810
564c6504 2811 card->update = NULL;
283375ce
MB
2812 }
2813
a73fb2df 2814 mutex_unlock(&card->dapm_mutex);
52765976
NC
2815
2816 if (ret > 0)
2817 soc_dpcm_runtime_update(card);
2818
56a67834 2819 return change;
2b97eabc
RP
2820}
2821EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2822
2823/**
2824 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2825 * @kcontrol: mixer control
ac11a2b3 2826 * @ucontrol: control element information
2b97eabc
RP
2827 *
2828 * Callback to get the value of a dapm enumerated double mixer control.
2829 *
2830 * Returns 0 for success.
2831 */
2832int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2833 struct snd_ctl_elem_value *ucontrol)
2834{
eee5d7f9 2835 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2b97eabc 2836 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3727b496 2837 unsigned int reg_val, val;
52765976 2838
236aaa68
LPC
2839 if (e->reg != SND_SOC_NOPM)
2840 reg_val = snd_soc_read(codec, e->reg);
2841 else
2842 reg_val = dapm_kcontrol_get_value(kcontrol);
2e72f8e3 2843
2e72f8e3 2844 val = (reg_val >> e->shift_l) & e->mask;
3727b496 2845 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2e72f8e3
PU
2846 if (e->shift_l != e->shift_r) {
2847 val = (reg_val >> e->shift_r) & e->mask;
3727b496
LPC
2848 val = snd_soc_enum_val_to_item(e, val);
2849 ucontrol->value.enumerated.item[1] = val;
2e72f8e3
PU
2850 }
2851
2852 return 0;
2853}
2b97eabc 2854EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2e72f8e3
PU
2855
2856/**
2b97eabc 2857 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2e72f8e3
PU
2858 * @kcontrol: mixer control
2859 * @ucontrol: control element information
2860 *
2b97eabc 2861 * Callback to set the value of a dapm enumerated double mixer control.
2e72f8e3
PU
2862 *
2863 * Returns 0 for success.
2864 */
2b97eabc 2865int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2e72f8e3
PU
2866 struct snd_ctl_elem_value *ucontrol)
2867{
cf7c1de2 2868 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
a73fb2df 2869 struct snd_soc_card *card = codec->card;
74155556 2870 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3727b496
LPC
2871 unsigned int *item = ucontrol->value.enumerated.item;
2872 unsigned int val, change;
46f5822f 2873 unsigned int mask;
97404f2e 2874 struct snd_soc_dapm_update update;
52765976 2875 int ret = 0;
2e72f8e3 2876
3727b496 2877 if (item[0] >= e->items)
2e72f8e3 2878 return -EINVAL;
3727b496
LPC
2879
2880 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
2e72f8e3
PU
2881 mask = e->mask << e->shift_l;
2882 if (e->shift_l != e->shift_r) {
3727b496 2883 if (item[1] > e->items)
2e72f8e3 2884 return -EINVAL;
3727b496 2885 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
2e72f8e3
PU
2886 mask |= e->mask << e->shift_r;
2887 }
2888
3cd04343 2889 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
fafd2176 2890
236aaa68
LPC
2891 if (e->reg != SND_SOC_NOPM)
2892 change = snd_soc_test_bits(codec, e->reg, mask, val);
2893 else
2894 change = dapm_kcontrol_set_value(kcontrol, val);
2895
fafd2176 2896 if (change) {
236aaa68
LPC
2897 if (e->reg != SND_SOC_NOPM) {
2898 update.kcontrol = kcontrol;
2899 update.reg = e->reg;
2900 update.mask = mask;
2901 update.val = val;
2902 card->update = &update;
2903 }
1642e3d4 2904
3727b496 2905 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
fafd2176 2906
564c6504 2907 card->update = NULL;
fafd2176 2908 }
2e72f8e3 2909
a73fb2df 2910 mutex_unlock(&card->dapm_mutex);
52765976
NC
2911
2912 if (ret > 0)
2913 soc_dpcm_runtime_update(card);
2914
97404f2e 2915 return change;
2e72f8e3 2916}
2b97eabc 2917EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2e72f8e3 2918
8b37dbd2
MB
2919/**
2920 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2921 *
2922 * @kcontrol: mixer control
2923 * @uinfo: control element information
2924 *
2925 * Callback to provide information about a pin switch control.
2926 */
2927int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2928 struct snd_ctl_elem_info *uinfo)
2929{
2930 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2931 uinfo->count = 1;
2932 uinfo->value.integer.min = 0;
2933 uinfo->value.integer.max = 1;
2934
2935 return 0;
2936}
2937EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2938
2939/**
2940 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2941 *
2942 * @kcontrol: mixer control
2943 * @ucontrol: Value
2944 */
2945int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2946 struct snd_ctl_elem_value *ucontrol)
2947{
48a8c394 2948 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
8b37dbd2
MB
2949 const char *pin = (const char *)kcontrol->private_value;
2950
3cd04343 2951 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
8b37dbd2
MB
2952
2953 ucontrol->value.integer.value[0] =
48a8c394 2954 snd_soc_dapm_get_pin_status(&card->dapm, pin);
8b37dbd2 2955
a73fb2df 2956 mutex_unlock(&card->dapm_mutex);
8b37dbd2
MB
2957
2958 return 0;
2959}
2960EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2961
2962/**
2963 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2964 *
2965 * @kcontrol: mixer control
2966 * @ucontrol: Value
2967 */
2968int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2969 struct snd_ctl_elem_value *ucontrol)
2970{
48a8c394 2971 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
8b37dbd2
MB
2972 const char *pin = (const char *)kcontrol->private_value;
2973
8b37dbd2 2974 if (ucontrol->value.integer.value[0])
48a8c394 2975 snd_soc_dapm_enable_pin(&card->dapm, pin);
8b37dbd2 2976 else
48a8c394 2977 snd_soc_dapm_disable_pin(&card->dapm, pin);
8b37dbd2 2978
a73fb2df 2979 snd_soc_dapm_sync(&card->dapm);
8b37dbd2
MB
2980 return 0;
2981}
2982EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2983
5ba06fc9
MB
2984static struct snd_soc_dapm_widget *
2985snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2986 const struct snd_soc_dapm_widget *widget)
2b97eabc
RP
2987{
2988 struct snd_soc_dapm_widget *w;
62ea874a 2989 int ret;
2b97eabc
RP
2990
2991 if ((w = dapm_cnew_widget(widget)) == NULL)
5ba06fc9 2992 return NULL;
2b97eabc 2993
62ea874a
MB
2994 switch (w->id) {
2995 case snd_soc_dapm_regulator_supply:
a3cc056b
LG
2996 w->regulator = devm_regulator_get(dapm->dev, w->name);
2997 if (IS_ERR(w->regulator)) {
2998 ret = PTR_ERR(w->regulator);
30a6a1a4 2999 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
62ea874a 3000 w->name, ret);
5ba06fc9 3001 return NULL;
62ea874a 3002 }
8784c77a 3003
de9ba98b 3004 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
8784c77a
MB
3005 ret = regulator_allow_bypass(w->regulator, true);
3006 if (ret != 0)
3007 dev_warn(w->dapm->dev,
30686c35 3008 "ASoC: Failed to bypass %s: %d\n",
8784c77a
MB
3009 w->name, ret);
3010 }
62ea874a 3011 break;
d7e7eb91 3012 case snd_soc_dapm_clock_supply:
165961ef 3013#ifdef CONFIG_CLKDEV_LOOKUP
695594f1 3014 w->clk = devm_clk_get(dapm->dev, w->name);
d7e7eb91
OL
3015 if (IS_ERR(w->clk)) {
3016 ret = PTR_ERR(w->clk);
30a6a1a4 3017 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
d7e7eb91
OL
3018 w->name, ret);
3019 return NULL;
3020 }
ec02995a
MB
3021#else
3022 return NULL;
3023#endif
d7e7eb91 3024 break;
62ea874a
MB
3025 default:
3026 break;
3027 }
2b97eabc 3028
88e8b9a8 3029 if (dapm->codec && dapm->codec->name_prefix)
2b581074
LPC
3030 w->name = kasprintf(GFP_KERNEL, "%s %s",
3031 dapm->codec->name_prefix, widget->name);
3032 else
3033 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3034
ead9b919
JN
3035 if (w->name == NULL) {
3036 kfree(w);
5ba06fc9 3037 return NULL;
ead9b919 3038 }
ead9b919 3039
7ca3a18b
MB
3040 switch (w->id) {
3041 case snd_soc_dapm_switch:
3042 case snd_soc_dapm_mixer:
3043 case snd_soc_dapm_mixer_named_ctl:
3044 w->power_check = dapm_generic_check_power;
3045 break;
3046 case snd_soc_dapm_mux:
7ca3a18b
MB
3047 w->power_check = dapm_generic_check_power;
3048 break;
4616274d 3049 case snd_soc_dapm_dai_out:
7ca3a18b
MB
3050 w->power_check = dapm_adc_check_power;
3051 break;
4616274d 3052 case snd_soc_dapm_dai_in:
7ca3a18b
MB
3053 w->power_check = dapm_dac_check_power;
3054 break;
63c69a6e
MB
3055 case snd_soc_dapm_adc:
3056 case snd_soc_dapm_aif_out:
3057 case snd_soc_dapm_dac:
3058 case snd_soc_dapm_aif_in:
7ca3a18b
MB
3059 case snd_soc_dapm_pga:
3060 case snd_soc_dapm_out_drv:
3061 case snd_soc_dapm_input:
3062 case snd_soc_dapm_output:
3063 case snd_soc_dapm_micbias:
3064 case snd_soc_dapm_spk:
3065 case snd_soc_dapm_hp:
3066 case snd_soc_dapm_mic:
3067 case snd_soc_dapm_line:
c74184ed 3068 case snd_soc_dapm_dai_link:
7ca3a18b
MB
3069 w->power_check = dapm_generic_check_power;
3070 break;
3071 case snd_soc_dapm_supply:
62ea874a 3072 case snd_soc_dapm_regulator_supply:
d7e7eb91 3073 case snd_soc_dapm_clock_supply:
57295073 3074 case snd_soc_dapm_kcontrol:
7ca3a18b
MB
3075 w->power_check = dapm_supply_check_power;
3076 break;
3077 default:
3078 w->power_check = dapm_always_on_check_power;
3079 break;
3080 }
3081
ce6120cc
LG
3082 w->dapm = dapm;
3083 w->codec = dapm->codec;
b7950641 3084 w->platform = dapm->platform;
2b97eabc
RP
3085 INIT_LIST_HEAD(&w->sources);
3086 INIT_LIST_HEAD(&w->sinks);
3087 INIT_LIST_HEAD(&w->list);
db432b41 3088 INIT_LIST_HEAD(&w->dirty);
97c866de 3089 list_add(&w->list, &dapm->card->widgets);
2b97eabc
RP
3090
3091 /* machine layer set ups unconnected pins and insertions */
3092 w->connected = 1;
5ba06fc9 3093 return w;
2b97eabc 3094}
2b97eabc 3095
4ba1327a
MB
3096/**
3097 * snd_soc_dapm_new_controls - create new dapm controls
ce6120cc 3098 * @dapm: DAPM context
4ba1327a
MB
3099 * @widget: widget array
3100 * @num: number of widgets
3101 *
3102 * Creates new DAPM controls based upon the templates.
3103 *
3104 * Returns 0 for success else error.
3105 */
ce6120cc 3106int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
4ba1327a
MB
3107 const struct snd_soc_dapm_widget *widget,
3108 int num)
3109{
5ba06fc9
MB
3110 struct snd_soc_dapm_widget *w;
3111 int i;
60884c27 3112 int ret = 0;
4ba1327a 3113
a73fb2df 3114 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
4ba1327a 3115 for (i = 0; i < num; i++) {
5ba06fc9
MB
3116 w = snd_soc_dapm_new_control(dapm, widget);
3117 if (!w) {
f7d41ae8 3118 dev_err(dapm->dev,
5ba06fc9
MB
3119 "ASoC: Failed to create DAPM control %s\n",
3120 widget->name);
60884c27
DC
3121 ret = -ENOMEM;
3122 break;
b8b33cb5 3123 }
4ba1327a
MB
3124 widget++;
3125 }
a73fb2df 3126 mutex_unlock(&dapm->card->dapm_mutex);
60884c27 3127 return ret;
4ba1327a
MB
3128}
3129EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3130
c74184ed
MB
3131static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3132 struct snd_kcontrol *kcontrol, int event)
3133{
3134 struct snd_soc_dapm_path *source_p, *sink_p;
3135 struct snd_soc_dai *source, *sink;
3136 const struct snd_soc_pcm_stream *config = w->params;
3137 struct snd_pcm_substream substream;
9747cec2 3138 struct snd_pcm_hw_params *params = NULL;
c74184ed
MB
3139 u64 fmt;
3140 int ret;
3141
bf4edea8
TI
3142 if (WARN_ON(!config) ||
3143 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3144 return -EINVAL;
c74184ed
MB
3145
3146 /* We only support a single source and sink, pick the first */
3147 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3148 list_sink);
3149 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3150 list_source);
3151
bf4edea8
TI
3152 if (WARN_ON(!source_p || !sink_p) ||
3153 WARN_ON(!sink_p->source || !source_p->sink) ||
3154 WARN_ON(!source_p->source || !sink_p->sink))
3155 return -EINVAL;
c74184ed
MB
3156
3157 source = source_p->source->priv;
3158 sink = sink_p->sink->priv;
3159
3160 /* Be a little careful as we don't want to overflow the mask array */
3161 if (config->formats) {
3162 fmt = ffs(config->formats) - 1;
3163 } else {
30a6a1a4 3164 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
c74184ed
MB
3165 config->formats);
3166 fmt = 0;
3167 }
3168
3169 /* Currently very limited parameter selection */
9747cec2
MB
3170 params = kzalloc(sizeof(*params), GFP_KERNEL);
3171 if (!params) {
3172 ret = -ENOMEM;
3173 goto out;
3174 }
3175 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
c74184ed 3176
9747cec2 3177 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
c74184ed 3178 config->rate_min;
9747cec2 3179 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
c74184ed
MB
3180 config->rate_max;
3181
9747cec2 3182 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
c74184ed 3183 = config->channels_min;
9747cec2 3184 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
c74184ed
MB
3185 = config->channels_max;
3186
3187 memset(&substream, 0, sizeof(substream));
3188
3189 switch (event) {
3190 case SND_SOC_DAPM_PRE_PMU:
3191 if (source->driver->ops && source->driver->ops->hw_params) {
3192 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3193 ret = source->driver->ops->hw_params(&substream,
9747cec2 3194 params, source);
c74184ed
MB
3195 if (ret != 0) {
3196 dev_err(source->dev,
30a6a1a4 3197 "ASoC: hw_params() failed: %d\n", ret);
9747cec2 3198 goto out;
c74184ed
MB
3199 }
3200 }
3201
3202 if (sink->driver->ops && sink->driver->ops->hw_params) {
3203 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
9747cec2 3204 ret = sink->driver->ops->hw_params(&substream, params,
c74184ed
MB
3205 sink);
3206 if (ret != 0) {
3207 dev_err(sink->dev,
30a6a1a4 3208 "ASoC: hw_params() failed: %d\n", ret);
9747cec2 3209 goto out;
c74184ed
MB
3210 }
3211 }
3212 break;
3213
3214 case SND_SOC_DAPM_POST_PMU:
da18396f
MB
3215 ret = snd_soc_dai_digital_mute(sink, 0,
3216 SNDRV_PCM_STREAM_PLAYBACK);
c74184ed 3217 if (ret != 0 && ret != -ENOTSUPP)
30a6a1a4 3218 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
9747cec2 3219 ret = 0;
c74184ed
MB
3220 break;
3221
3222 case SND_SOC_DAPM_PRE_PMD:
da18396f
MB
3223 ret = snd_soc_dai_digital_mute(sink, 1,
3224 SNDRV_PCM_STREAM_PLAYBACK);
c74184ed 3225 if (ret != 0 && ret != -ENOTSUPP)
30a6a1a4 3226 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
9747cec2 3227 ret = 0;
c74184ed
MB
3228 break;
3229
3230 default:
a6ed0608 3231 WARN(1, "Unknown event %d\n", event);
c74184ed
MB
3232 return -EINVAL;
3233 }
3234
9747cec2
MB
3235out:
3236 kfree(params);
3237 return ret;
c74184ed
MB
3238}
3239
3240int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3241 const struct snd_soc_pcm_stream *params,
3242 struct snd_soc_dapm_widget *source,
3243 struct snd_soc_dapm_widget *sink)
3244{
3245 struct snd_soc_dapm_route routes[2];
3246 struct snd_soc_dapm_widget template;
3247 struct snd_soc_dapm_widget *w;
3248 size_t len;
3249 char *link_name;
3250
3251 len = strlen(source->name) + strlen(sink->name) + 2;
3252 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3253 if (!link_name)
3254 return -ENOMEM;
3255 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3256
3257 memset(&template, 0, sizeof(template));
3258 template.reg = SND_SOC_NOPM;
3259 template.id = snd_soc_dapm_dai_link;
3260 template.name = link_name;
3261 template.event = snd_soc_dai_link_event;
3262 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3263 SND_SOC_DAPM_PRE_PMD;
3264
30a6a1a4 3265 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
c74184ed
MB
3266
3267 w = snd_soc_dapm_new_control(&card->dapm, &template);
3268 if (!w) {
30a6a1a4 3269 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
c74184ed
MB
3270 link_name);
3271 return -ENOMEM;
3272 }
3273
3274 w->params = params;
3275
3276 memset(&routes, 0, sizeof(routes));
3277
3278 routes[0].source = source->name;
3279 routes[0].sink = link_name;
3280 routes[1].source = link_name;
3281 routes[1].sink = sink->name;
3282
3283 return snd_soc_dapm_add_routes(&card->dapm, routes,
3284 ARRAY_SIZE(routes));
3285}
3286
888df395
MB
3287int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3288 struct snd_soc_dai *dai)
2b97eabc 3289{
888df395 3290 struct snd_soc_dapm_widget template;
2b97eabc
RP
3291 struct snd_soc_dapm_widget *w;
3292
888df395
MB
3293 WARN_ON(dapm->dev != dai->dev);
3294
3295 memset(&template, 0, sizeof(template));
3296 template.reg = SND_SOC_NOPM;
3297
3298 if (dai->driver->playback.stream_name) {
4616274d 3299 template.id = snd_soc_dapm_dai_in;
888df395
MB
3300 template.name = dai->driver->playback.stream_name;
3301 template.sname = dai->driver->playback.stream_name;
3302
30a6a1a4 3303 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
888df395
MB
3304 template.name);
3305
3306 w = snd_soc_dapm_new_control(dapm, &template);
3307 if (!w) {
30a6a1a4 3308 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
888df395 3309 dai->driver->playback.stream_name);
298402a3 3310 return -ENOMEM;
888df395
MB
3311 }
3312
3313 w->priv = dai;
3314 dai->playback_widget = w;
3315 }
3316
3317 if (dai->driver->capture.stream_name) {
4616274d 3318 template.id = snd_soc_dapm_dai_out;
888df395
MB
3319 template.name = dai->driver->capture.stream_name;
3320 template.sname = dai->driver->capture.stream_name;
3321
30a6a1a4 3322 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
888df395
MB
3323 template.name);
3324
3325 w = snd_soc_dapm_new_control(dapm, &template);
3326 if (!w) {
30a6a1a4 3327 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
888df395 3328 dai->driver->capture.stream_name);
298402a3 3329 return -ENOMEM;
888df395
MB
3330 }
3331
3332 w->priv = dai;
3333 dai->capture_widget = w;
3334 }
3335
3336 return 0;
3337}
3338
3339int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3340{
3341 struct snd_soc_dapm_widget *dai_w, *w;
3342 struct snd_soc_dai *dai;
888df395
MB
3343
3344 /* For each DAI widget... */
3345 list_for_each_entry(dai_w, &card->widgets, list) {
4616274d
MB
3346 switch (dai_w->id) {
3347 case snd_soc_dapm_dai_in:
3348 case snd_soc_dapm_dai_out:
3349 break;
3350 default:
2b97eabc 3351 continue;
4616274d 3352 }
888df395
MB
3353
3354 dai = dai_w->priv;
3355
3356 /* ...find all widgets with the same stream and link them */
3357 list_for_each_entry(w, &card->widgets, list) {
3358 if (w->dapm != dai_w->dapm)
3359 continue;
3360
4616274d
MB
3361 switch (w->id) {
3362 case snd_soc_dapm_dai_in:
3363 case snd_soc_dapm_dai_out:
888df395 3364 continue;
4616274d
MB
3365 default:
3366 break;
3367 }
888df395 3368
19c2c5f5 3369 if (!w->sname || !strstr(w->sname, dai_w->name))
888df395
MB
3370 continue;
3371
3372 if (dai->driver->playback.stream_name &&
3373 strstr(w->sname,
3374 dai->driver->playback.stream_name)) {
888df395 3375 dev_dbg(dai->dev, "%s -> %s\n",
2553628e 3376 dai->playback_widget->name, w->name);
888df395 3377
2553628e
LPC
3378 snd_soc_dapm_add_path(w->dapm,
3379 dai->playback_widget, w, NULL, NULL);
888df395
MB
3380 }
3381
3382 if (dai->driver->capture.stream_name &&
3383 strstr(w->sname,
3384 dai->driver->capture.stream_name)) {
888df395 3385 dev_dbg(dai->dev, "%s -> %s\n",
2553628e 3386 w->name, dai->capture_widget->name);
888df395 3387
2553628e
LPC
3388 snd_soc_dapm_add_path(w->dapm, w,
3389 dai->capture_widget, NULL, NULL);
2b97eabc
RP
3390 }
3391 }
3392 }
2b97eabc 3393
888df395
MB
3394 return 0;
3395}
64a648c2 3396
b893ea5f
LG
3397void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3398{
3399 struct snd_soc_pcm_runtime *rtd = card->rtd;
3400 struct snd_soc_dai *cpu_dai, *codec_dai;
3401 struct snd_soc_dapm_route r;
3402 int i;
3403
3404 memset(&r, 0, sizeof(r));
3405
3406 /* for each BE DAI link... */
3407 for (i = 0; i < card->num_rtd; i++) {
3408 rtd = &card->rtd[i];
3409 cpu_dai = rtd->cpu_dai;
3410 codec_dai = rtd->codec_dai;
3411
3412 /* dynamic FE links have no fixed DAI mapping */
3413 if (rtd->dai_link->dynamic)
3414 continue;
3415
3416 /* there is no point in connecting BE DAI links with dummies */
3417 if (snd_soc_dai_is_dummy(codec_dai) ||
3418 snd_soc_dai_is_dummy(cpu_dai))
3419 continue;
3420
3421 /* connect BE DAI playback if widgets are valid */
3422 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3423 r.source = cpu_dai->playback_widget->name;
3424 r.sink = codec_dai->playback_widget->name;
3425 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3426 cpu_dai->codec->name, r.source,
3427 codec_dai->platform->name, r.sink);
3428
a4e9154c 3429 snd_soc_dapm_add_route(&card->dapm, &r);
b893ea5f
LG
3430 }
3431
3432 /* connect BE DAI capture if widgets are valid */
3433 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3434 r.source = codec_dai->capture_widget->name;
3435 r.sink = cpu_dai->capture_widget->name;
3436 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3437 codec_dai->codec->name, r.source,
3438 cpu_dai->platform->name, r.sink);
3439
a4e9154c 3440 snd_soc_dapm_add_route(&card->dapm, &r);
b893ea5f
LG
3441 }
3442
3443 }
3444}
3445
c471fdd1 3446static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
d9b0951b 3447 int event)
2b97eabc 3448{
c471fdd1 3449 struct snd_soc_dapm_widget *w;
7bd3a6f3 3450
c471fdd1
LPC
3451 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3452 w = dai->playback_widget;
3453 else
3454 w = dai->capture_widget;
fe360685 3455
c471fdd1
LPC
3456 if (w) {
3457 dapm_mark_dirty(w, "stream event");
d9b0951b
LG
3458
3459 switch (event) {
3460 case SND_SOC_DAPM_STREAM_START:
c471fdd1 3461 w->active = 1;
d9b0951b
LG
3462 break;
3463 case SND_SOC_DAPM_STREAM_STOP:
c471fdd1 3464 w->active = 0;
d9b0951b
LG
3465 break;
3466 case SND_SOC_DAPM_STREAM_SUSPEND:
3467 case SND_SOC_DAPM_STREAM_RESUME:
3468 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3469 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3470 break;
3471 }
3472 }
c471fdd1 3473}
d9b0951b 3474
c471fdd1
LPC
3475static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3476 int event)
3477{
3478 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
3479 soc_dapm_dai_stream_event(rtd->codec_dai, stream, event);
2b97eabc 3480
95dd5cd6 3481 dapm_power_widgets(rtd->card, event);
ce6120cc
LG
3482}
3483
3484/**
3485 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3486 * @rtd: PCM runtime data
3487 * @stream: stream name
3488 * @event: stream event
3489 *
3490 * Sends a stream event to the dapm core. The core then makes any
3491 * necessary widget power changes.
3492 *
3493 * Returns 0 for success else error.
3494 */
d9b0951b
LG
3495void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3496 int event)
ce6120cc 3497{
a73fb2df 3498 struct snd_soc_card *card = rtd->card;
ce6120cc 3499
3cd04343 3500 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
d9b0951b 3501 soc_dapm_stream_event(rtd, stream, event);
a73fb2df 3502 mutex_unlock(&card->dapm_mutex);
2b97eabc 3503}
2b97eabc 3504
11391100
CK
3505/**
3506 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3507 * @dapm: DAPM context
3508 * @pin: pin name
3509 *
3510 * Enables input/output pin and its parents or children widgets iff there is
3511 * a valid audio route and active audio stream.
3512 *
3513 * Requires external locking.
3514 *
3515 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3516 * do any widget power switching.
3517 */
3518int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3519 const char *pin)
3520{
3521 return snd_soc_dapm_set_pin(dapm, pin, 1);
3522}
3523EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3524
2b97eabc 3525/**
a5302181 3526 * snd_soc_dapm_enable_pin - enable pin.
ce6120cc 3527 * @dapm: DAPM context
a5302181 3528 * @pin: pin name
2b97eabc 3529 *
74b8f955 3530 * Enables input/output pin and its parents or children widgets iff there is
a5302181 3531 * a valid audio route and active audio stream.
11391100 3532 *
a5302181
LG
3533 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3534 * do any widget power switching.
2b97eabc 3535 */
ce6120cc 3536int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2b97eabc 3537{
11391100
CK
3538 int ret;
3539
3540 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3541
3542 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3543
3544 mutex_unlock(&dapm->card->dapm_mutex);
3545
3546 return ret;
a5302181
LG
3547}
3548EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2b97eabc 3549
da34183e 3550/**
11391100 3551 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
ce6120cc 3552 * @dapm: DAPM context
da34183e
MB
3553 * @pin: pin name
3554 *
3555 * Enables input/output pin regardless of any other state. This is
3556 * intended for use with microphone bias supplies used in microphone
3557 * jack detection.
3558 *
11391100
CK
3559 * Requires external locking.
3560 *
da34183e
MB
3561 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3562 * do any widget power switching.
3563 */
11391100
CK
3564int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3565 const char *pin)
da34183e 3566{
91a5fca4 3567 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
da34183e 3568
91a5fca4 3569 if (!w) {
30a6a1a4 3570 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
91a5fca4 3571 return -EINVAL;
0d86733c
MB
3572 }
3573
30a6a1a4 3574 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
91a5fca4
LPC
3575 w->connected = 1;
3576 w->force = 1;
75c1f891 3577 dapm_mark_dirty(w, "force enable");
da34183e 3578
91a5fca4 3579 return 0;
da34183e 3580}
11391100
CK
3581EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3582
3583/**
3584 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3585 * @dapm: DAPM context
3586 * @pin: pin name
3587 *
3588 * Enables input/output pin regardless of any other state. This is
3589 * intended for use with microphone bias supplies used in microphone
3590 * jack detection.
3591 *
3592 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3593 * do any widget power switching.
3594 */
3595int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3596 const char *pin)
3597{
3598 int ret;
3599
3600 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3601
3602 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
3603
3604 mutex_unlock(&dapm->card->dapm_mutex);
3605
3606 return ret;
3607}
da34183e
MB
3608EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3609
11391100
CK
3610/**
3611 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3612 * @dapm: DAPM context
3613 * @pin: pin name
3614 *
3615 * Disables input/output pin and its parents or children widgets.
3616 *
3617 * Requires external locking.
3618 *
3619 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3620 * do any widget power switching.
3621 */
3622int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3623 const char *pin)
3624{
3625 return snd_soc_dapm_set_pin(dapm, pin, 0);
3626}
3627EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3628
a5302181
LG
3629/**
3630 * snd_soc_dapm_disable_pin - disable pin.
ce6120cc 3631 * @dapm: DAPM context
a5302181
LG
3632 * @pin: pin name
3633 *
74b8f955 3634 * Disables input/output pin and its parents or children widgets.
11391100 3635 *
a5302181
LG
3636 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3637 * do any widget power switching.
3638 */
ce6120cc
LG
3639int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3640 const char *pin)
a5302181 3641{
11391100
CK
3642 int ret;
3643
3644 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3645
3646 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3647
3648 mutex_unlock(&dapm->card->dapm_mutex);
3649
3650 return ret;
2b97eabc 3651}
a5302181 3652EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2b97eabc 3653
11391100
CK
3654/**
3655 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3656 * @dapm: DAPM context
3657 * @pin: pin name
3658 *
3659 * Marks the specified pin as being not connected, disabling it along
3660 * any parent or child widgets. At present this is identical to
3661 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3662 * additional things such as disabling controls which only affect
3663 * paths through the pin.
3664 *
3665 * Requires external locking.
3666 *
3667 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3668 * do any widget power switching.
3669 */
3670int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3671 const char *pin)
3672{
3673 return snd_soc_dapm_set_pin(dapm, pin, 0);
3674}
3675EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3676
5817b52a
MB
3677/**
3678 * snd_soc_dapm_nc_pin - permanently disable pin.
ce6120cc 3679 * @dapm: DAPM context
5817b52a
MB
3680 * @pin: pin name
3681 *
3682 * Marks the specified pin as being not connected, disabling it along
3683 * any parent or child widgets. At present this is identical to
3684 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3685 * additional things such as disabling controls which only affect
3686 * paths through the pin.
3687 *
3688 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3689 * do any widget power switching.
3690 */
ce6120cc 3691int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
5817b52a 3692{
11391100
CK
3693 int ret;
3694
3695 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3696
3697 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3698
3699 mutex_unlock(&dapm->card->dapm_mutex);
3700
3701 return ret;
5817b52a
MB
3702}
3703EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3704
eeec12bf 3705/**
a5302181 3706 * snd_soc_dapm_get_pin_status - get audio pin status
ce6120cc 3707 * @dapm: DAPM context
a5302181 3708 * @pin: audio signal pin endpoint (or start point)
eeec12bf 3709 *
a5302181 3710 * Get audio pin status - connected or disconnected.
eeec12bf 3711 *
a5302181 3712 * Returns 1 for connected otherwise 0.
eeec12bf 3713 */
ce6120cc
LG
3714int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3715 const char *pin)
eeec12bf 3716{
91a5fca4 3717 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
eeec12bf 3718
91a5fca4
LPC
3719 if (w)
3720 return w->connected;
a68b38ad 3721
eeec12bf
GG
3722 return 0;
3723}
a5302181 3724EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
eeec12bf 3725
1547aba9
MB
3726/**
3727 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
ce6120cc 3728 * @dapm: DAPM context
1547aba9
MB
3729 * @pin: audio signal pin endpoint (or start point)
3730 *
3731 * Mark the given endpoint or pin as ignoring suspend. When the
3732 * system is disabled a path between two endpoints flagged as ignoring
3733 * suspend will not be disabled. The path must already be enabled via
3734 * normal means at suspend time, it will not be turned on if it was not
3735 * already enabled.
3736 */
ce6120cc
LG
3737int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3738 const char *pin)
1547aba9 3739{
91a5fca4 3740 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
1547aba9 3741
91a5fca4 3742 if (!w) {
30a6a1a4 3743 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
91a5fca4 3744 return -EINVAL;
1547aba9
MB
3745 }
3746
91a5fca4
LPC
3747 w->ignore_suspend = 1;
3748
3749 return 0;
1547aba9
MB
3750}
3751EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3752
1633281b
SW
3753static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3754 struct snd_soc_dapm_widget *w)
3755{
3756 struct snd_soc_dapm_path *p;
3757
3758 list_for_each_entry(p, &card->paths, list) {
3759 if ((p->source == w) || (p->sink == w)) {
3760 dev_dbg(card->dev,
3761 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3762 p->source->name, p->source->id, p->source->dapm,
3763 p->sink->name, p->sink->id, p->sink->dapm);
3764
3765 /* Connected to something other than the codec */
3766 if (p->source->dapm != p->sink->dapm)
3767 return true;
3768 /*
3769 * Loopback connection from codec external pin to
3770 * codec external pin
3771 */
3772 if (p->sink->id == snd_soc_dapm_input) {
3773 switch (p->source->id) {
3774 case snd_soc_dapm_output:
3775 case snd_soc_dapm_micbias:
3776 return true;
3777 default:
3778 break;
3779 }
3780 }
3781 }
3782 }
3783
3784 return false;
3785}
3786
3787/**
3788 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3789 * @codec: The codec whose pins should be processed
3790 *
3791 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3792 * which are unused. Pins are used if they are connected externally to the
3793 * codec, whether that be to some other device, or a loop-back connection to
3794 * the codec itself.
3795 */
3796void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3797{
3798 struct snd_soc_card *card = codec->card;
3799 struct snd_soc_dapm_context *dapm = &codec->dapm;
3800 struct snd_soc_dapm_widget *w;
3801
30a6a1a4 3802 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
1633281b
SW
3803 &card->dapm, &codec->dapm);
3804
3805 list_for_each_entry(w, &card->widgets, list) {
3806 if (w->dapm != dapm)
3807 continue;
3808 switch (w->id) {
3809 case snd_soc_dapm_input:
3810 case snd_soc_dapm_output:
3811 case snd_soc_dapm_micbias:
30a6a1a4 3812 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
1633281b
SW
3813 w->name);
3814 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
a094b80b 3815 dev_dbg(codec->dev,
1633281b
SW
3816 "... Not in map; disabling\n");
3817 snd_soc_dapm_nc_pin(dapm, w->name);
3818 }
3819 break;
3820 default:
3821 break;
3822 }
3823 }
3824}
3825
2b97eabc
RP
3826/**
3827 * snd_soc_dapm_free - free dapm resources
728a5222 3828 * @dapm: DAPM context
2b97eabc
RP
3829 *
3830 * Free all dapm widgets and resources.
3831 */
ce6120cc 3832void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2b97eabc 3833{
ce6120cc 3834 snd_soc_dapm_sys_remove(dapm->dev);
6c45e126 3835 dapm_debugfs_cleanup(dapm);
ce6120cc 3836 dapm_free_widgets(dapm);
7be31be8 3837 list_del(&dapm->list);
2b97eabc
RP
3838}
3839EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3840
57996358 3841static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
51737470 3842{
01005a72 3843 struct snd_soc_card *card = dapm->card;
51737470
MB
3844 struct snd_soc_dapm_widget *w;
3845 LIST_HEAD(down_list);
3846 int powerdown = 0;
3847
01005a72
LG
3848 mutex_lock(&card->dapm_mutex);
3849
97c866de
JN
3850 list_for_each_entry(w, &dapm->card->widgets, list) {
3851 if (w->dapm != dapm)
3852 continue;
51737470 3853 if (w->power) {
828a842f 3854 dapm_seq_insert(w, &down_list, false);
c2caa4da 3855 w->power = 0;
51737470
MB
3856 powerdown = 1;
3857 }
3858 }
3859
3860 /* If there were no widgets to power down we're already in
3861 * standby.
3862 */
3863 if (powerdown) {
7679e42e
MB
3864 if (dapm->bias_level == SND_SOC_BIAS_ON)
3865 snd_soc_dapm_set_bias_level(dapm,
3866 SND_SOC_BIAS_PREPARE);
95dd5cd6 3867 dapm_seq_run(card, &down_list, 0, false);
7679e42e
MB
3868 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3869 snd_soc_dapm_set_bias_level(dapm,
3870 SND_SOC_BIAS_STANDBY);
51737470 3871 }
01005a72
LG
3872
3873 mutex_unlock(&card->dapm_mutex);
f0fba2ad
LG
3874}
3875
3876/*
3877 * snd_soc_dapm_shutdown - callback for system shutdown
3878 */
3879void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3880{
57996358 3881 struct snd_soc_dapm_context *dapm;
f0fba2ad 3882
57996358 3883 list_for_each_entry(dapm, &card->dapm_list, list) {
17282ba4
XX
3884 if (dapm != &card->dapm) {
3885 soc_dapm_shutdown_dapm(dapm);
3886 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3887 snd_soc_dapm_set_bias_level(dapm,
3888 SND_SOC_BIAS_OFF);
3889 }
ce6120cc 3890 }
17282ba4
XX
3891
3892 soc_dapm_shutdown_dapm(&card->dapm);
3893 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3894 snd_soc_dapm_set_bias_level(&card->dapm,
3895 SND_SOC_BIAS_OFF);
51737470
MB
3896}
3897
2b97eabc 3898/* Module information */
d331124d 3899MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2b97eabc
RP
3900MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3901MODULE_LICENSE("GPL");