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