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