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