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