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