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