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