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