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