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