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