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