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