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