]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - sound/soc/soc-dapm.c
ASoC: s/w->kcontrols/w->kcontrol_news/g
[mirror_ubuntu-hirsute-kernel.git] / sound / soc / soc-dapm.c
CommitLineData
2b97eabc
RP
1/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
d331124d 5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
2b97eabc
RP
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
2b97eabc
RP
12 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
74b8f955 15 * DACs/ADCs.
2b97eabc
RP
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
3a4fa0a2 21 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
2b97eabc
RP
22 * device reopen.
23 *
24 * Todo:
25 * o DAPM power change sequencing - allow for configurable per
26 * codec sequences.
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
30 */
31
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/init.h>
9d0624a7 35#include <linux/async.h>
2b97eabc
RP
36#include <linux/delay.h>
37#include <linux/pm.h>
38#include <linux/bitops.h>
39#include <linux/platform_device.h>
40#include <linux/jiffies.h>
20496ff3 41#include <linux/debugfs.h>
5a0e3ad6 42#include <linux/slab.h>
2b97eabc
RP
43#include <sound/core.h>
44#include <sound/pcm.h>
45#include <sound/pcm_params.h>
ce6120cc 46#include <sound/soc.h>
2b97eabc
RP
47#include <sound/initval.h>
48
84e90930
MB
49#include <trace/events/asoc.h>
50
2b97eabc
RP
51/* dapm power sequences - make this per codec in the future */
52static int dapm_up_seq[] = {
38357ab2
MB
53 [snd_soc_dapm_pre] = 0,
54 [snd_soc_dapm_supply] = 1,
55 [snd_soc_dapm_micbias] = 2,
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,
90 [snd_soc_dapm_supply] = 11,
91 [snd_soc_dapm_post] = 12,
2b97eabc
RP
92};
93
12ef193d 94static void pop_wait(u32 pop_time)
15e4c72f
MB
95{
96 if (pop_time)
97 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
98}
99
fd8d3bc0 100static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
15e4c72f
MB
101{
102 va_list args;
fd8d3bc0 103 char *buf;
15e4c72f 104
fd8d3bc0
JN
105 if (!pop_time)
106 return;
15e4c72f 107
fd8d3bc0
JN
108 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
109 if (buf == NULL)
110 return;
15e4c72f 111
fd8d3bc0
JN
112 va_start(args, fmt);
113 vsnprintf(buf, PAGE_SIZE, fmt, args);
9d01df06 114 dev_info(dev, "%s", buf);
15e4c72f 115 va_end(args);
fd8d3bc0
JN
116
117 kfree(buf);
15e4c72f
MB
118}
119
2b97eabc 120/* create a new dapm widget */
88cb4290 121static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
2b97eabc
RP
122 const struct snd_soc_dapm_widget *_widget)
123{
88cb4290 124 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
2b97eabc
RP
125}
126
452c5eaa
MB
127/**
128 * snd_soc_dapm_set_bias_level - set the bias level for the system
ed5a4c47 129 * @dapm: DAPM context
452c5eaa
MB
130 * @level: level to configure
131 *
132 * Configure the bias (power) levels for the SoC audio device.
133 *
134 * Returns 0 for success else error.
135 */
ed5a4c47 136static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
ce6120cc 137 enum snd_soc_bias_level level)
452c5eaa 138{
ed5a4c47 139 struct snd_soc_card *card = dapm->card;
452c5eaa
MB
140 int ret = 0;
141
f83fba8b
MB
142 switch (level) {
143 case SND_SOC_BIAS_ON:
ce6120cc 144 dev_dbg(dapm->dev, "Setting full bias\n");
f83fba8b
MB
145 break;
146 case SND_SOC_BIAS_PREPARE:
ce6120cc 147 dev_dbg(dapm->dev, "Setting bias prepare\n");
f83fba8b
MB
148 break;
149 case SND_SOC_BIAS_STANDBY:
ce6120cc 150 dev_dbg(dapm->dev, "Setting standby bias\n");
f83fba8b
MB
151 break;
152 case SND_SOC_BIAS_OFF:
ce6120cc 153 dev_dbg(dapm->dev, "Setting bias off\n");
f83fba8b
MB
154 break;
155 default:
ce6120cc 156 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
f83fba8b
MB
157 return -EINVAL;
158 }
159
84e90930
MB
160 trace_snd_soc_bias_level_start(card, level);
161
f0fba2ad 162 if (card && card->set_bias_level)
452c5eaa 163 ret = card->set_bias_level(card, level);
474e09ca 164 if (ret == 0) {
ce6120cc
LG
165 if (dapm->codec && dapm->codec->driver->set_bias_level)
166 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
474e09ca 167 else
ce6120cc 168 dapm->bias_level = level;
474e09ca 169 }
1badabd9
MB
170 if (ret == 0) {
171 if (card && card->set_bias_level_post)
172 ret = card->set_bias_level_post(card, level);
173 }
452c5eaa 174
84e90930
MB
175 trace_snd_soc_bias_level_done(card, level);
176
452c5eaa
MB
177 return ret;
178}
179
2b97eabc
RP
180/* set up initial codec paths */
181static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
182 struct snd_soc_dapm_path *p, int i)
183{
184 switch (w->id) {
185 case snd_soc_dapm_switch:
ca9c1aae
IM
186 case snd_soc_dapm_mixer:
187 case snd_soc_dapm_mixer_named_ctl: {
2b97eabc 188 int val;
4eaa9819 189 struct soc_mixer_control *mc = (struct soc_mixer_control *)
82cfecdc 190 w->kcontrol_news[i].private_value;
815ecf8d
JS
191 unsigned int reg = mc->reg;
192 unsigned int shift = mc->shift;
4eaa9819 193 int max = mc->max;
815ecf8d
JS
194 unsigned int mask = (1 << fls(max)) - 1;
195 unsigned int invert = mc->invert;
2b97eabc
RP
196
197 val = snd_soc_read(w->codec, reg);
198 val = (val >> shift) & mask;
199
200 if ((invert && !val) || (!invert && val))
201 p->connect = 1;
202 else
203 p->connect = 0;
204 }
205 break;
206 case snd_soc_dapm_mux: {
82cfecdc
SW
207 struct soc_enum *e = (struct soc_enum *)
208 w->kcontrol_news[i].private_value;
2b97eabc
RP
209 int val, item, bitmask;
210
f8ba0b7b 211 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2b97eabc
RP
212 ;
213 val = snd_soc_read(w->codec, e->reg);
214 item = (val >> e->shift_l) & (bitmask - 1);
215
216 p->connect = 0;
f8ba0b7b 217 for (i = 0; i < e->max; i++) {
2b97eabc
RP
218 if (!(strcmp(p->name, e->texts[i])) && item == i)
219 p->connect = 1;
220 }
221 }
222 break;
24ff33ac 223 case snd_soc_dapm_virt_mux: {
82cfecdc
SW
224 struct soc_enum *e = (struct soc_enum *)
225 w->kcontrol_news[i].private_value;
24ff33ac
DP
226
227 p->connect = 0;
228 /* since a virtual mux has no backing registers to
229 * decide which path to connect, it will try to match
230 * with the first enumeration. This is to ensure
231 * that the default mux choice (the first) will be
232 * correctly powered up during initialization.
233 */
234 if (!strcmp(p->name, e->texts[0]))
235 p->connect = 1;
236 }
237 break;
2e72f8e3 238 case snd_soc_dapm_value_mux: {
74155556 239 struct soc_enum *e = (struct soc_enum *)
82cfecdc 240 w->kcontrol_news[i].private_value;
2e72f8e3
PU
241 int val, item;
242
243 val = snd_soc_read(w->codec, e->reg);
244 val = (val >> e->shift_l) & e->mask;
245 for (item = 0; item < e->max; item++) {
246 if (val == e->values[item])
247 break;
248 }
249
250 p->connect = 0;
251 for (i = 0; i < e->max; i++) {
252 if (!(strcmp(p->name, e->texts[i])) && item == i)
253 p->connect = 1;
254 }
255 }
256 break;
2b97eabc
RP
257 /* does not effect routing - always connected */
258 case snd_soc_dapm_pga:
d88429a6 259 case snd_soc_dapm_out_drv:
2b97eabc
RP
260 case snd_soc_dapm_output:
261 case snd_soc_dapm_adc:
262 case snd_soc_dapm_input:
263 case snd_soc_dapm_dac:
264 case snd_soc_dapm_micbias:
265 case snd_soc_dapm_vmid:
246d0a17 266 case snd_soc_dapm_supply:
010ff262
MB
267 case snd_soc_dapm_aif_in:
268 case snd_soc_dapm_aif_out:
2b97eabc
RP
269 p->connect = 1;
270 break;
271 /* does effect routing - dynamically connected */
272 case snd_soc_dapm_hp:
273 case snd_soc_dapm_mic:
274 case snd_soc_dapm_spk:
275 case snd_soc_dapm_line:
276 case snd_soc_dapm_pre:
277 case snd_soc_dapm_post:
278 p->connect = 0;
279 break;
280 }
281}
282
74b8f955 283/* connect mux widget to its interconnecting audio paths */
ce6120cc 284static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
2b97eabc
RP
285 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
286 struct snd_soc_dapm_path *path, const char *control_name,
287 const struct snd_kcontrol_new *kcontrol)
288{
289 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
290 int i;
291
f8ba0b7b 292 for (i = 0; i < e->max; i++) {
2b97eabc 293 if (!(strcmp(control_name, e->texts[i]))) {
8ddab3f5 294 list_add(&path->list, &dapm->card->paths);
2b97eabc
RP
295 list_add(&path->list_sink, &dest->sources);
296 list_add(&path->list_source, &src->sinks);
297 path->name = (char*)e->texts[i];
298 dapm_set_path_status(dest, path, 0);
299 return 0;
300 }
301 }
302
303 return -ENODEV;
304}
305
74b8f955 306/* connect mixer widget to its interconnecting audio paths */
ce6120cc 307static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
2b97eabc
RP
308 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
309 struct snd_soc_dapm_path *path, const char *control_name)
310{
311 int i;
312
313 /* search for mixer kcontrol */
314 for (i = 0; i < dest->num_kcontrols; i++) {
82cfecdc 315 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
8ddab3f5 316 list_add(&path->list, &dapm->card->paths);
2b97eabc
RP
317 list_add(&path->list_sink, &dest->sources);
318 list_add(&path->list_source, &src->sinks);
82cfecdc 319 path->name = dest->kcontrol_news[i].name;
2b97eabc
RP
320 dapm_set_path_status(dest, path, i);
321 return 0;
322 }
323 }
324 return -ENODEV;
325}
326
2b97eabc 327/* create new dapm mixer control */
ce6120cc 328static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
2b97eabc
RP
329 struct snd_soc_dapm_widget *w)
330{
331 int i, ret = 0;
3e5ff4df 332 size_t name_len, prefix_len;
2b97eabc 333 struct snd_soc_dapm_path *path;
12ea2c78 334 struct snd_card *card = dapm->card->snd_card;
efb7ac3f
MB
335 const char *prefix;
336
337 if (dapm->codec)
338 prefix = dapm->codec->name_prefix;
339 else
340 prefix = NULL;
2b97eabc 341
3e5ff4df
MB
342 if (prefix)
343 prefix_len = strlen(prefix) + 1;
344 else
345 prefix_len = 0;
346
2b97eabc
RP
347 /* add kcontrol */
348 for (i = 0; i < w->num_kcontrols; i++) {
349
350 /* match name */
351 list_for_each_entry(path, &w->sources, list_sink) {
352
353 /* mixer/mux paths name must match control name */
82cfecdc 354 if (path->name != (char *)w->kcontrol_news[i].name)
2b97eabc
RP
355 continue;
356
ca9c1aae
IM
357 /* add dapm control with long name.
358 * for dapm_mixer this is the concatenation of the
359 * mixer and kcontrol name.
360 * for dapm_mixer_named_ctl this is simply the
361 * kcontrol name.
362 */
82cfecdc 363 name_len = strlen(w->kcontrol_news[i].name) + 1;
07495f3e 364 if (w->id != snd_soc_dapm_mixer_named_ctl)
ca9c1aae
IM
365 name_len += 1 + strlen(w->name);
366
219b93f5 367 path->long_name = kmalloc(name_len, GFP_KERNEL);
ca9c1aae 368
2b97eabc
RP
369 if (path->long_name == NULL)
370 return -ENOMEM;
371
ca9c1aae 372 switch (w->id) {
ca9c1aae 373 default:
3e5ff4df
MB
374 /* The control will get a prefix from
375 * the control creation process but
376 * we're also using the same prefix
377 * for widgets so cut the prefix off
378 * the front of the widget name.
379 */
ca9c1aae 380 snprintf(path->long_name, name_len, "%s %s",
3e5ff4df 381 w->name + prefix_len,
82cfecdc 382 w->kcontrol_news[i].name);
07495f3e 383 break;
ca9c1aae
IM
384 case snd_soc_dapm_mixer_named_ctl:
385 snprintf(path->long_name, name_len, "%s",
82cfecdc 386 w->kcontrol_news[i].name);
07495f3e 387 break;
ca9c1aae
IM
388 }
389
219b93f5
MB
390 path->long_name[name_len - 1] = '\0';
391
82cfecdc 392 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
efb7ac3f 393 path->long_name, prefix);
ce6120cc 394 ret = snd_ctl_add(card, path->kcontrol);
2b97eabc 395 if (ret < 0) {
f7d41ae8
JN
396 dev_err(dapm->dev,
397 "asoc: failed to add dapm kcontrol %s: %d\n",
398 path->long_name, ret);
2b97eabc
RP
399 kfree(path->long_name);
400 path->long_name = NULL;
401 return ret;
402 }
403 }
404 }
405 return ret;
406}
407
408/* create new dapm mux control */
ce6120cc 409static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
2b97eabc
RP
410 struct snd_soc_dapm_widget *w)
411{
412 struct snd_soc_dapm_path *path = NULL;
413 struct snd_kcontrol *kcontrol;
12ea2c78 414 struct snd_card *card = dapm->card->snd_card;
efb7ac3f 415 const char *prefix;
3e5ff4df 416 size_t prefix_len;
2b97eabc
RP
417 int ret = 0;
418
419 if (!w->num_kcontrols) {
f7d41ae8 420 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
2b97eabc
RP
421 return -EINVAL;
422 }
423
efb7ac3f
MB
424 if (dapm->codec)
425 prefix = dapm->codec->name_prefix;
426 else
427 prefix = NULL;
428
3e5ff4df
MB
429 if (prefix)
430 prefix_len = strlen(prefix) + 1;
431 else
432 prefix_len = 0;
433
434 /* The control will get a prefix from the control creation
435 * process but we're also using the same prefix for widgets so
436 * cut the prefix off the front of the widget name.
437 */
82cfecdc 438 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], w, w->name + prefix_len,
3e5ff4df 439 prefix);
ce6120cc
LG
440 ret = snd_ctl_add(card, kcontrol);
441
2b97eabc
RP
442 if (ret < 0)
443 goto err;
444
445 list_for_each_entry(path, &w->sources, list_sink)
446 path->kcontrol = kcontrol;
447
448 return ret;
449
450err:
f7d41ae8 451 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
2b97eabc
RP
452 return ret;
453}
454
455/* create new dapm volume control */
ce6120cc 456static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
2b97eabc
RP
457 struct snd_soc_dapm_widget *w)
458{
a6c65736 459 if (w->num_kcontrols)
f7d41ae8
JN
460 dev_err(w->dapm->dev,
461 "asoc: PGA controls not supported: '%s'\n", w->name);
2b97eabc 462
a6c65736 463 return 0;
2b97eabc
RP
464}
465
466/* reset 'walked' bit for each dapm path */
ce6120cc 467static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
2b97eabc
RP
468{
469 struct snd_soc_dapm_path *p;
470
8ddab3f5 471 list_for_each_entry(p, &dapm->card->paths, list)
2b97eabc
RP
472 p->walked = 0;
473}
474
9949788b
MB
475/* We implement power down on suspend by checking the power state of
476 * the ALSA card - when we are suspending the ALSA state for the card
477 * is set to D3.
478 */
479static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
480{
12ea2c78 481 int level = snd_power_get_state(widget->dapm->card->snd_card);
9949788b 482
f0fba2ad 483 switch (level) {
9949788b
MB
484 case SNDRV_CTL_POWER_D3hot:
485 case SNDRV_CTL_POWER_D3cold:
1547aba9 486 if (widget->ignore_suspend)
f7d41ae8
JN
487 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
488 widget->name);
1547aba9 489 return widget->ignore_suspend;
9949788b
MB
490 default:
491 return 1;
492 }
493}
494
2b97eabc
RP
495/*
496 * Recursively check for a completed path to an active or physically connected
497 * output widget. Returns number of complete paths.
498 */
499static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
500{
501 struct snd_soc_dapm_path *path;
502 int con = 0;
503
246d0a17
MB
504 if (widget->id == snd_soc_dapm_supply)
505 return 0;
506
010ff262
MB
507 switch (widget->id) {
508 case snd_soc_dapm_adc:
509 case snd_soc_dapm_aif_out:
510 if (widget->active)
9949788b 511 return snd_soc_dapm_suspend_check(widget);
010ff262
MB
512 default:
513 break;
514 }
2b97eabc
RP
515
516 if (widget->connected) {
517 /* connected pin ? */
518 if (widget->id == snd_soc_dapm_output && !widget->ext)
9949788b 519 return snd_soc_dapm_suspend_check(widget);
2b97eabc
RP
520
521 /* connected jack or spk ? */
522 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
eaeae5d9 523 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
9949788b 524 return snd_soc_dapm_suspend_check(widget);
2b97eabc
RP
525 }
526
527 list_for_each_entry(path, &widget->sinks, list_source) {
528 if (path->walked)
529 continue;
530
531 if (path->sink && path->connect) {
532 path->walked = 1;
533 con += is_connected_output_ep(path->sink);
534 }
535 }
536
537 return con;
538}
539
540/*
541 * Recursively check for a completed path to an active or physically connected
542 * input widget. Returns number of complete paths.
543 */
544static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
545{
546 struct snd_soc_dapm_path *path;
547 int con = 0;
548
246d0a17
MB
549 if (widget->id == snd_soc_dapm_supply)
550 return 0;
551
2b97eabc 552 /* active stream ? */
010ff262
MB
553 switch (widget->id) {
554 case snd_soc_dapm_dac:
555 case snd_soc_dapm_aif_in:
556 if (widget->active)
9949788b 557 return snd_soc_dapm_suspend_check(widget);
010ff262
MB
558 default:
559 break;
560 }
2b97eabc
RP
561
562 if (widget->connected) {
563 /* connected pin ? */
564 if (widget->id == snd_soc_dapm_input && !widget->ext)
9949788b 565 return snd_soc_dapm_suspend_check(widget);
2b97eabc
RP
566
567 /* connected VMID/Bias for lower pops */
568 if (widget->id == snd_soc_dapm_vmid)
9949788b 569 return snd_soc_dapm_suspend_check(widget);
2b97eabc
RP
570
571 /* connected jack ? */
eaeae5d9
PU
572 if (widget->id == snd_soc_dapm_mic ||
573 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
9949788b 574 return snd_soc_dapm_suspend_check(widget);
2b97eabc
RP
575 }
576
577 list_for_each_entry(path, &widget->sources, list_sink) {
578 if (path->walked)
579 continue;
580
581 if (path->source && path->connect) {
582 path->walked = 1;
583 con += is_connected_input_ep(path->source);
584 }
585 }
586
587 return con;
588}
589
e2be2ccf
JN
590/*
591 * Handler for generic register modifier widget.
592 */
593int dapm_reg_event(struct snd_soc_dapm_widget *w,
594 struct snd_kcontrol *kcontrol, int event)
595{
596 unsigned int val;
597
598 if (SND_SOC_DAPM_EVENT_ON(event))
599 val = w->on_val;
600 else
601 val = w->off_val;
602
603 snd_soc_update_bits(w->codec, -(w->reg + 1),
604 w->mask << w->shift, val << w->shift);
605
606 return 0;
607}
11589418 608EXPORT_SYMBOL_GPL(dapm_reg_event);
e2be2ccf 609
cd0f2d47
MB
610/* Generic check to see if a widget should be powered.
611 */
612static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
613{
614 int in, out;
615
616 in = is_connected_input_ep(w);
ce6120cc 617 dapm_clear_walk(w->dapm);
cd0f2d47 618 out = is_connected_output_ep(w);
ce6120cc 619 dapm_clear_walk(w->dapm);
cd0f2d47
MB
620 return out != 0 && in != 0;
621}
622
6ea31b9f
MB
623/* Check to see if an ADC has power */
624static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
625{
626 int in;
627
628 if (w->active) {
629 in = is_connected_input_ep(w);
ce6120cc 630 dapm_clear_walk(w->dapm);
6ea31b9f
MB
631 return in != 0;
632 } else {
633 return dapm_generic_check_power(w);
634 }
635}
636
637/* Check to see if a DAC has power */
638static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
639{
640 int out;
641
642 if (w->active) {
643 out = is_connected_output_ep(w);
ce6120cc 644 dapm_clear_walk(w->dapm);
6ea31b9f
MB
645 return out != 0;
646 } else {
647 return dapm_generic_check_power(w);
648 }
649}
650
246d0a17
MB
651/* Check to see if a power supply is needed */
652static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
653{
654 struct snd_soc_dapm_path *path;
655 int power = 0;
656
657 /* Check if one of our outputs is connected */
658 list_for_each_entry(path, &w->sinks, list_source) {
215edda3
MB
659 if (path->connected &&
660 !path->connected(path->source, path->sink))
661 continue;
662
3017358a
MB
663 if (!path->sink)
664 continue;
665
666 if (path->sink->force) {
667 power = 1;
668 break;
669 }
670
671 if (path->sink->power_check &&
246d0a17
MB
672 path->sink->power_check(path->sink)) {
673 power = 1;
674 break;
675 }
676 }
677
ce6120cc 678 dapm_clear_walk(w->dapm);
246d0a17
MB
679
680 return power;
681}
682
38357ab2
MB
683static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
684 struct snd_soc_dapm_widget *b,
828a842f 685 bool power_up)
42aa3418 686{
828a842f
MB
687 int *sort;
688
689 if (power_up)
690 sort = dapm_up_seq;
691 else
692 sort = dapm_down_seq;
693
38357ab2
MB
694 if (sort[a->id] != sort[b->id])
695 return sort[a->id] - sort[b->id];
20e4859d
MB
696 if (a->subseq != b->subseq) {
697 if (power_up)
698 return a->subseq - b->subseq;
699 else
700 return b->subseq - a->subseq;
701 }
b22ead2a
MB
702 if (a->reg != b->reg)
703 return a->reg - b->reg;
84dab567
MB
704 if (a->dapm != b->dapm)
705 return (unsigned long)a->dapm - (unsigned long)b->dapm;
42aa3418 706
38357ab2
MB
707 return 0;
708}
42aa3418 709
38357ab2
MB
710/* Insert a widget in order into a DAPM power sequence. */
711static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
712 struct list_head *list,
828a842f 713 bool power_up)
38357ab2
MB
714{
715 struct snd_soc_dapm_widget *w;
716
717 list_for_each_entry(w, list, power_list)
828a842f 718 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
38357ab2
MB
719 list_add_tail(&new_widget->power_list, &w->power_list);
720 return;
721 }
722
723 list_add_tail(&new_widget->power_list, list);
724}
725
68f89ad8
MB
726static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
727 struct snd_soc_dapm_widget *w, int event)
728{
729 struct snd_soc_card *card = dapm->card;
730 const char *ev_name;
731 int power, ret;
732
733 switch (event) {
734 case SND_SOC_DAPM_PRE_PMU:
735 ev_name = "PRE_PMU";
736 power = 1;
737 break;
738 case SND_SOC_DAPM_POST_PMU:
739 ev_name = "POST_PMU";
740 power = 1;
741 break;
742 case SND_SOC_DAPM_PRE_PMD:
743 ev_name = "PRE_PMD";
744 power = 0;
745 break;
746 case SND_SOC_DAPM_POST_PMD:
747 ev_name = "POST_PMD";
748 power = 0;
749 break;
750 default:
751 BUG();
752 return;
753 }
754
755 if (w->power != power)
756 return;
757
758 if (w->event && (w->event_flags & event)) {
759 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
760 w->name, ev_name);
84e90930 761 trace_snd_soc_dapm_widget_event_start(w, event);
68f89ad8 762 ret = w->event(w, NULL, event);
84e90930 763 trace_snd_soc_dapm_widget_event_done(w, event);
68f89ad8
MB
764 if (ret < 0)
765 pr_err("%s: %s event failed: %d\n",
766 ev_name, w->name, ret);
767 }
768}
769
b22ead2a 770/* Apply the coalesced changes from a DAPM sequence */
ce6120cc 771static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
b22ead2a 772 struct list_head *pending)
163cac06 773{
3a45b867 774 struct snd_soc_card *card = dapm->card;
68f89ad8
MB
775 struct snd_soc_dapm_widget *w;
776 int reg, power;
b22ead2a
MB
777 unsigned int value = 0;
778 unsigned int mask = 0;
779 unsigned int cur_mask;
780
781 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
782 power_list)->reg;
783
784 list_for_each_entry(w, pending, power_list) {
785 cur_mask = 1 << w->shift;
786 BUG_ON(reg != w->reg);
787
788 if (w->invert)
789 power = !w->power;
790 else
791 power = w->power;
792
793 mask |= cur_mask;
794 if (power)
795 value |= cur_mask;
796
fd8d3bc0 797 pop_dbg(dapm->dev, card->pop_time,
b22ead2a
MB
798 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
799 w->name, reg, value, mask);
81628103 800
68f89ad8
MB
801 /* Check for events */
802 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
803 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
81628103
MB
804 }
805
806 if (reg >= 0) {
fd8d3bc0 807 pop_dbg(dapm->dev, card->pop_time,
81628103 808 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
3a45b867
JN
809 value, mask, reg, card->pop_time);
810 pop_wait(card->pop_time);
ce6120cc 811 snd_soc_update_bits(dapm->codec, reg, mask, value);
b22ead2a
MB
812 }
813
81628103 814 list_for_each_entry(w, pending, power_list) {
68f89ad8
MB
815 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
816 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
81628103 817 }
b22ead2a 818}
42aa3418 819
b22ead2a
MB
820/* Apply a DAPM power sequence.
821 *
822 * We walk over a pre-sorted list of widgets to apply power to. In
823 * order to minimise the number of writes to the device required
824 * multiple widgets will be updated in a single write where possible.
825 * Currently anything that requires more than a single write is not
826 * handled.
827 */
ce6120cc 828static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
828a842f 829 struct list_head *list, int event, bool power_up)
b22ead2a
MB
830{
831 struct snd_soc_dapm_widget *w, *n;
832 LIST_HEAD(pending);
833 int cur_sort = -1;
20e4859d 834 int cur_subseq = -1;
b22ead2a 835 int cur_reg = SND_SOC_NOPM;
7be31be8 836 struct snd_soc_dapm_context *cur_dapm = NULL;
474b62d6 837 int ret, i;
828a842f
MB
838 int *sort;
839
840 if (power_up)
841 sort = dapm_up_seq;
842 else
843 sort = dapm_down_seq;
163cac06 844
b22ead2a
MB
845 list_for_each_entry_safe(w, n, list, power_list) {
846 ret = 0;
847
848 /* Do we need to apply any queued changes? */
7be31be8 849 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
20e4859d 850 w->dapm != cur_dapm || w->subseq != cur_subseq) {
b22ead2a 851 if (!list_empty(&pending))
7be31be8 852 dapm_seq_run_coalesced(cur_dapm, &pending);
b22ead2a 853
474b62d6
MB
854 if (cur_dapm && cur_dapm->seq_notifier) {
855 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
856 if (sort[i] == cur_sort)
857 cur_dapm->seq_notifier(cur_dapm,
f85a9e0d
MB
858 i,
859 cur_subseq);
474b62d6
MB
860 }
861
b22ead2a
MB
862 INIT_LIST_HEAD(&pending);
863 cur_sort = -1;
20e4859d 864 cur_subseq = -1;
b22ead2a 865 cur_reg = SND_SOC_NOPM;
7be31be8 866 cur_dapm = NULL;
b22ead2a
MB
867 }
868
163cac06
MB
869 switch (w->id) {
870 case snd_soc_dapm_pre:
871 if (!w->event)
b22ead2a
MB
872 list_for_each_entry_safe_continue(w, n, list,
873 power_list);
163cac06 874
b22ead2a 875 if (event == SND_SOC_DAPM_STREAM_START)
163cac06
MB
876 ret = w->event(w,
877 NULL, SND_SOC_DAPM_PRE_PMU);
b22ead2a 878 else if (event == SND_SOC_DAPM_STREAM_STOP)
163cac06
MB
879 ret = w->event(w,
880 NULL, SND_SOC_DAPM_PRE_PMD);
163cac06
MB
881 break;
882
883 case snd_soc_dapm_post:
884 if (!w->event)
b22ead2a
MB
885 list_for_each_entry_safe_continue(w, n, list,
886 power_list);
163cac06 887
b22ead2a 888 if (event == SND_SOC_DAPM_STREAM_START)
163cac06
MB
889 ret = w->event(w,
890 NULL, SND_SOC_DAPM_POST_PMU);
b22ead2a 891 else if (event == SND_SOC_DAPM_STREAM_STOP)
163cac06
MB
892 ret = w->event(w,
893 NULL, SND_SOC_DAPM_POST_PMD);
163cac06
MB
894 break;
895
b22ead2a 896 default:
81628103
MB
897 /* Queue it up for application */
898 cur_sort = sort[w->id];
20e4859d 899 cur_subseq = w->subseq;
81628103 900 cur_reg = w->reg;
7be31be8 901 cur_dapm = w->dapm;
81628103
MB
902 list_move(&w->power_list, &pending);
903 break;
163cac06 904 }
b22ead2a
MB
905
906 if (ret < 0)
f7d41ae8
JN
907 dev_err(w->dapm->dev,
908 "Failed to apply widget power: %d\n", ret);
6ea31b9f 909 }
b22ead2a
MB
910
911 if (!list_empty(&pending))
28e86808 912 dapm_seq_run_coalesced(cur_dapm, &pending);
474b62d6
MB
913
914 if (cur_dapm && cur_dapm->seq_notifier) {
915 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
916 if (sort[i] == cur_sort)
917 cur_dapm->seq_notifier(cur_dapm,
f85a9e0d 918 i, cur_subseq);
474b62d6 919 }
42aa3418
MB
920}
921
97404f2e
MB
922static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
923{
924 struct snd_soc_dapm_update *update = dapm->update;
925 struct snd_soc_dapm_widget *w;
926 int ret;
927
928 if (!update)
929 return;
930
931 w = update->widget;
932
933 if (w->event &&
934 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
935 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
936 if (ret != 0)
937 pr_err("%s DAPM pre-event failed: %d\n",
938 w->name, ret);
939 }
940
941 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
942 update->val);
943 if (ret < 0)
944 pr_err("%s DAPM update failed: %d\n", w->name, ret);
945
946 if (w->event &&
947 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
948 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
949 if (ret != 0)
950 pr_err("%s DAPM post-event failed: %d\n",
951 w->name, ret);
952 }
953}
954
9d0624a7
MB
955/* Async callback run prior to DAPM sequences - brings to _PREPARE if
956 * they're changing state.
957 */
958static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
959{
960 struct snd_soc_dapm_context *d = data;
961 int ret;
962
963 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
964 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
965 if (ret != 0)
966 dev_err(d->dev,
967 "Failed to turn on bias: %d\n", ret);
968 }
969
970 /* If we're changing to all on or all off then prepare */
971 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
972 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
973 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
974 if (ret != 0)
975 dev_err(d->dev,
976 "Failed to prepare bias: %d\n", ret);
977 }
978}
979
980/* Async callback run prior to DAPM sequences - brings to their final
981 * state.
982 */
983static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
984{
985 struct snd_soc_dapm_context *d = data;
986 int ret;
987
988 /* If we just powered the last thing off drop to standby bias */
989 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
990 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
991 if (ret != 0)
992 dev_err(d->dev, "Failed to apply standby bias: %d\n",
993 ret);
994 }
97404f2e 995
9d0624a7
MB
996 /* If we're in standby and can support bias off then do that */
997 if (d->bias_level == SND_SOC_BIAS_STANDBY && d->idle_bias_off) {
998 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
999 if (ret != 0)
1000 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1001 }
1002
1003 /* If we just powered up then move to active bias */
1004 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1005 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1006 if (ret != 0)
1007 dev_err(d->dev, "Failed to apply active bias: %d\n",
1008 ret);
1009 }
1010}
97404f2e 1011
2b97eabc
RP
1012/*
1013 * Scan each dapm widget for complete audio path.
1014 * A complete path is a route that has valid endpoints i.e.:-
1015 *
1016 * o DAC to output pin.
1017 * o Input Pin to ADC.
1018 * o Input pin to Output pin (bypass, sidetone)
1019 * o DAC to ADC (loopback).
1020 */
ce6120cc 1021static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
2b97eabc 1022{
12ea2c78 1023 struct snd_soc_card *card = dapm->card;
2b97eabc 1024 struct snd_soc_dapm_widget *w;
7be31be8 1025 struct snd_soc_dapm_context *d;
291f3bbc
MB
1026 LIST_HEAD(up_list);
1027 LIST_HEAD(down_list);
9d0624a7 1028 LIST_HEAD(async_domain);
38357ab2 1029 int power;
6d3ddc81 1030
84e90930
MB
1031 trace_snd_soc_dapm_start(card);
1032
7be31be8
JN
1033 list_for_each_entry(d, &card->dapm_list, list)
1034 if (d->n_widgets)
1035 d->dev_power = 0;
1036
6d3ddc81
MB
1037 /* Check which widgets we need to power and store them in
1038 * lists indicating if they should be powered up or down.
1039 */
97c866de 1040 list_for_each_entry(w, &card->widgets, list) {
6d3ddc81
MB
1041 switch (w->id) {
1042 case snd_soc_dapm_pre:
828a842f 1043 dapm_seq_insert(w, &down_list, false);
6d3ddc81
MB
1044 break;
1045 case snd_soc_dapm_post:
828a842f 1046 dapm_seq_insert(w, &up_list, true);
6d3ddc81
MB
1047 break;
1048
1049 default:
1050 if (!w->power_check)
1051 continue;
1052
9949788b 1053 if (!w->force)
50b6bce5 1054 power = w->power_check(w);
9949788b
MB
1055 else
1056 power = 1;
1057 if (power)
7be31be8 1058 w->dapm->dev_power = 1;
452c5eaa 1059
6d3ddc81
MB
1060 if (w->power == power)
1061 continue;
1062
84e90930
MB
1063 trace_snd_soc_dapm_widget_power(w, power);
1064
6d3ddc81 1065 if (power)
828a842f 1066 dapm_seq_insert(w, &up_list, true);
6d3ddc81 1067 else
828a842f 1068 dapm_seq_insert(w, &down_list, false);
6d3ddc81
MB
1069
1070 w->power = power;
1071 break;
1072 }
2b97eabc
RP
1073 }
1074
b14b76a5
MB
1075 /* If there are no DAPM widgets then try to figure out power from the
1076 * event type.
1077 */
97c866de 1078 if (!dapm->n_widgets) {
b14b76a5
MB
1079 switch (event) {
1080 case SND_SOC_DAPM_STREAM_START:
1081 case SND_SOC_DAPM_STREAM_RESUME:
7be31be8 1082 dapm->dev_power = 1;
b14b76a5 1083 break;
862af8ad 1084 case SND_SOC_DAPM_STREAM_STOP:
7be31be8 1085 dapm->dev_power = !!dapm->codec->active;
862af8ad 1086 break;
50b6bce5 1087 case SND_SOC_DAPM_STREAM_SUSPEND:
7be31be8 1088 dapm->dev_power = 0;
50b6bce5 1089 break;
b14b76a5 1090 case SND_SOC_DAPM_STREAM_NOP:
ce6120cc 1091 switch (dapm->bias_level) {
a96ca338
MB
1092 case SND_SOC_BIAS_STANDBY:
1093 case SND_SOC_BIAS_OFF:
7be31be8 1094 dapm->dev_power = 0;
a96ca338
MB
1095 break;
1096 default:
7be31be8 1097 dapm->dev_power = 1;
a96ca338
MB
1098 break;
1099 }
50b6bce5 1100 break;
b14b76a5
MB
1101 default:
1102 break;
1103 }
1104 }
1105
52ba67bf
MB
1106 /* Force all contexts in the card to the same bias state */
1107 power = 0;
1108 list_for_each_entry(d, &card->dapm_list, list)
1109 if (d->dev_power)
1110 power = 1;
1111 list_for_each_entry(d, &card->dapm_list, list)
1112 d->dev_power = power;
1113
1114
9d0624a7
MB
1115 /* Run all the bias changes in parallel */
1116 list_for_each_entry(d, &dapm->card->dapm_list, list)
1117 async_schedule_domain(dapm_pre_sequence_async, d,
1118 &async_domain);
1119 async_synchronize_full_domain(&async_domain);
452c5eaa 1120
6d3ddc81 1121 /* Power down widgets first; try to avoid amplifying pops. */
828a842f 1122 dapm_seq_run(dapm, &down_list, event, false);
2b97eabc 1123
97404f2e
MB
1124 dapm_widget_update(dapm);
1125
6d3ddc81 1126 /* Now power up. */
828a842f 1127 dapm_seq_run(dapm, &up_list, event, true);
2b97eabc 1128
9d0624a7
MB
1129 /* Run all the bias changes in parallel */
1130 list_for_each_entry(d, &dapm->card->dapm_list, list)
1131 async_schedule_domain(dapm_post_sequence_async, d,
1132 &async_domain);
1133 async_synchronize_full_domain(&async_domain);
452c5eaa 1134
fd8d3bc0
JN
1135 pop_dbg(dapm->dev, card->pop_time,
1136 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
3a45b867 1137 pop_wait(card->pop_time);
cb507e7e 1138
84e90930
MB
1139 trace_snd_soc_dapm_done(card);
1140
42aa3418 1141 return 0;
2b97eabc
RP
1142}
1143
79fb9387
MB
1144#ifdef CONFIG_DEBUG_FS
1145static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1146{
1147 file->private_data = inode->i_private;
1148 return 0;
1149}
1150
1151static ssize_t dapm_widget_power_read_file(struct file *file,
1152 char __user *user_buf,
1153 size_t count, loff_t *ppos)
1154{
1155 struct snd_soc_dapm_widget *w = file->private_data;
1156 char *buf;
1157 int in, out;
1158 ssize_t ret;
1159 struct snd_soc_dapm_path *p = NULL;
1160
1161 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1162 if (!buf)
1163 return -ENOMEM;
1164
1165 in = is_connected_input_ep(w);
ce6120cc 1166 dapm_clear_walk(w->dapm);
79fb9387 1167 out = is_connected_output_ep(w);
ce6120cc 1168 dapm_clear_walk(w->dapm);
79fb9387 1169
d033c36a 1170 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
79fb9387
MB
1171 w->name, w->power ? "On" : "Off", in, out);
1172
d033c36a
MB
1173 if (w->reg >= 0)
1174 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1175 " - R%d(0x%x) bit %d",
1176 w->reg, w->reg, w->shift);
1177
1178 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1179
3eef08ba
MB
1180 if (w->sname)
1181 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1182 w->sname,
1183 w->active ? "active" : "inactive");
79fb9387
MB
1184
1185 list_for_each_entry(p, &w->sources, list_sink) {
215edda3
MB
1186 if (p->connected && !p->connected(w, p->sink))
1187 continue;
1188
79fb9387
MB
1189 if (p->connect)
1190 ret += snprintf(buf + ret, PAGE_SIZE - ret,
67f5ed6e 1191 " in \"%s\" \"%s\"\n",
79fb9387
MB
1192 p->name ? p->name : "static",
1193 p->source->name);
1194 }
1195 list_for_each_entry(p, &w->sinks, list_source) {
215edda3
MB
1196 if (p->connected && !p->connected(w, p->sink))
1197 continue;
1198
79fb9387
MB
1199 if (p->connect)
1200 ret += snprintf(buf + ret, PAGE_SIZE - ret,
67f5ed6e 1201 " out \"%s\" \"%s\"\n",
79fb9387
MB
1202 p->name ? p->name : "static",
1203 p->sink->name);
1204 }
1205
1206 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1207
1208 kfree(buf);
1209 return ret;
1210}
1211
1212static const struct file_operations dapm_widget_power_fops = {
1213 .open = dapm_widget_power_open_file,
1214 .read = dapm_widget_power_read_file,
6038f373 1215 .llseek = default_llseek,
79fb9387
MB
1216};
1217
ef49e4fa
MB
1218static int dapm_bias_open_file(struct inode *inode, struct file *file)
1219{
1220 file->private_data = inode->i_private;
1221 return 0;
1222}
1223
1224static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1225 size_t count, loff_t *ppos)
1226{
1227 struct snd_soc_dapm_context *dapm = file->private_data;
1228 char *level;
1229
1230 switch (dapm->bias_level) {
1231 case SND_SOC_BIAS_ON:
1232 level = "On\n";
1233 break;
1234 case SND_SOC_BIAS_PREPARE:
1235 level = "Prepare\n";
1236 break;
1237 case SND_SOC_BIAS_STANDBY:
1238 level = "Standby\n";
1239 break;
1240 case SND_SOC_BIAS_OFF:
1241 level = "Off\n";
1242 break;
1243 default:
1244 BUG();
1245 level = "Unknown\n";
1246 break;
1247 }
1248
1249 return simple_read_from_buffer(user_buf, count, ppos, level,
1250 strlen(level));
1251}
1252
1253static const struct file_operations dapm_bias_fops = {
1254 .open = dapm_bias_open_file,
1255 .read = dapm_bias_read_file,
1256 .llseek = default_llseek,
1257};
1258
8eecaf62
LPC
1259void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1260 struct dentry *parent)
79fb9387 1261{
79fb9387
MB
1262 struct dentry *d;
1263
8eecaf62
LPC
1264 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1265
1266 if (!dapm->debugfs_dapm) {
1267 printk(KERN_WARNING
1268 "Failed to create DAPM debugfs directory\n");
79fb9387 1269 return;
8eecaf62 1270 }
79fb9387 1271
ef49e4fa
MB
1272 d = debugfs_create_file("bias_level", 0444,
1273 dapm->debugfs_dapm, dapm,
1274 &dapm_bias_fops);
1275 if (!d)
1276 dev_warn(dapm->dev,
1277 "ASoC: Failed to create bias level debugfs file\n");
d5d1e0be 1278}
ef49e4fa 1279
d5d1e0be
LPC
1280static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1281{
1282 struct snd_soc_dapm_context *dapm = w->dapm;
1283 struct dentry *d;
79fb9387 1284
d5d1e0be
LPC
1285 if (!dapm->debugfs_dapm || !w->name)
1286 return;
1287
1288 d = debugfs_create_file(w->name, 0444,
1289 dapm->debugfs_dapm, w,
1290 &dapm_widget_power_fops);
1291 if (!d)
1292 dev_warn(w->dapm->dev,
1293 "ASoC: Failed to create %s debugfs file\n",
1294 w->name);
79fb9387 1295}
d5d1e0be 1296
6c45e126
LPC
1297static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1298{
1299 debugfs_remove_recursive(dapm->debugfs_dapm);
1300}
1301
79fb9387 1302#else
8eecaf62
LPC
1303void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1304 struct dentry *parent)
79fb9387
MB
1305{
1306}
d5d1e0be
LPC
1307
1308static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1309{
1310}
1311
6c45e126
LPC
1312static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1313{
1314}
1315
79fb9387
MB
1316#endif
1317
2b97eabc 1318/* test and update the power status of a mux widget */
d9c96cf3 1319static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
3a65577d
MB
1320 struct snd_kcontrol *kcontrol, int change,
1321 int mux, struct soc_enum *e)
2b97eabc
RP
1322{
1323 struct snd_soc_dapm_path *path;
1324 int found = 0;
1325
eff317d0 1326 if (widget->id != snd_soc_dapm_mux &&
24ff33ac 1327 widget->id != snd_soc_dapm_virt_mux &&
eff317d0 1328 widget->id != snd_soc_dapm_value_mux)
2b97eabc
RP
1329 return -ENODEV;
1330
3a65577d 1331 if (!change)
2b97eabc
RP
1332 return 0;
1333
1334 /* find dapm widget path assoc with kcontrol */
8ddab3f5 1335 list_for_each_entry(path, &widget->dapm->card->paths, list) {
2b97eabc
RP
1336 if (path->kcontrol != kcontrol)
1337 continue;
1338
cb01e2b9 1339 if (!path->name || !e->texts[mux])
2b97eabc
RP
1340 continue;
1341
1342 found = 1;
1343 /* we now need to match the string in the enum to the path */
cb01e2b9 1344 if (!(strcmp(path->name, e->texts[mux])))
2b97eabc
RP
1345 path->connect = 1; /* new connection */
1346 else
1347 path->connect = 0; /* old connection must be powered down */
1348 }
1349
b91b8fa0 1350 if (found)
ce6120cc 1351 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
2b97eabc
RP
1352
1353 return 0;
1354}
2b97eabc 1355
1b075e3f 1356/* test and update the power status of a mixer or switch widget */
d9c96cf3 1357static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
283375ce 1358 struct snd_kcontrol *kcontrol, int connect)
2b97eabc
RP
1359{
1360 struct snd_soc_dapm_path *path;
1361 int found = 0;
1362
1b075e3f 1363 if (widget->id != snd_soc_dapm_mixer &&
ca9c1aae 1364 widget->id != snd_soc_dapm_mixer_named_ctl &&
1b075e3f 1365 widget->id != snd_soc_dapm_switch)
2b97eabc
RP
1366 return -ENODEV;
1367
2b97eabc 1368 /* find dapm widget path assoc with kcontrol */
8ddab3f5 1369 list_for_each_entry(path, &widget->dapm->card->paths, list) {
2b97eabc
RP
1370 if (path->kcontrol != kcontrol)
1371 continue;
1372
1373 /* found, now check type */
1374 found = 1;
283375ce 1375 path->connect = connect;
2b97eabc
RP
1376 break;
1377 }
1378
b91b8fa0 1379 if (found)
ce6120cc 1380 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
2b97eabc
RP
1381
1382 return 0;
1383}
2b97eabc
RP
1384
1385/* show dapm widget status in sys fs */
1386static ssize_t dapm_widget_show(struct device *dev,
1387 struct device_attribute *attr, char *buf)
1388{
f0fba2ad
LG
1389 struct snd_soc_pcm_runtime *rtd =
1390 container_of(dev, struct snd_soc_pcm_runtime, dev);
1391 struct snd_soc_codec *codec =rtd->codec;
2b97eabc
RP
1392 struct snd_soc_dapm_widget *w;
1393 int count = 0;
1394 char *state = "not set";
1395
97c866de
JN
1396 list_for_each_entry(w, &codec->card->widgets, list) {
1397 if (w->dapm != &codec->dapm)
1398 continue;
2b97eabc
RP
1399
1400 /* only display widgets that burnm power */
1401 switch (w->id) {
1402 case snd_soc_dapm_hp:
1403 case snd_soc_dapm_mic:
1404 case snd_soc_dapm_spk:
1405 case snd_soc_dapm_line:
1406 case snd_soc_dapm_micbias:
1407 case snd_soc_dapm_dac:
1408 case snd_soc_dapm_adc:
1409 case snd_soc_dapm_pga:
d88429a6 1410 case snd_soc_dapm_out_drv:
2b97eabc 1411 case snd_soc_dapm_mixer:
ca9c1aae 1412 case snd_soc_dapm_mixer_named_ctl:
246d0a17 1413 case snd_soc_dapm_supply:
2b97eabc
RP
1414 if (w->name)
1415 count += sprintf(buf + count, "%s: %s\n",
1416 w->name, w->power ? "On":"Off");
1417 break;
1418 default:
1419 break;
1420 }
1421 }
1422
ce6120cc 1423 switch (codec->dapm.bias_level) {
0be9898a
MB
1424 case SND_SOC_BIAS_ON:
1425 state = "On";
2b97eabc 1426 break;
0be9898a
MB
1427 case SND_SOC_BIAS_PREPARE:
1428 state = "Prepare";
2b97eabc 1429 break;
0be9898a
MB
1430 case SND_SOC_BIAS_STANDBY:
1431 state = "Standby";
2b97eabc 1432 break;
0be9898a
MB
1433 case SND_SOC_BIAS_OFF:
1434 state = "Off";
2b97eabc
RP
1435 break;
1436 }
1437 count += sprintf(buf + count, "PM State: %s\n", state);
1438
1439 return count;
1440}
1441
1442static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1443
1444int snd_soc_dapm_sys_add(struct device *dev)
1445{
12ef193d 1446 return device_create_file(dev, &dev_attr_dapm_widget);
2b97eabc
RP
1447}
1448
1449static void snd_soc_dapm_sys_remove(struct device *dev)
1450{
aef90843 1451 device_remove_file(dev, &dev_attr_dapm_widget);
2b97eabc
RP
1452}
1453
1454/* free all dapm widgets and resources */
ce6120cc 1455static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2b97eabc
RP
1456{
1457 struct snd_soc_dapm_widget *w, *next_w;
1458 struct snd_soc_dapm_path *p, *next_p;
1459
97c866de
JN
1460 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1461 if (w->dapm != dapm)
1462 continue;
2b97eabc 1463 list_del(&w->list);
8ddab3f5
JN
1464 /*
1465 * remove source and sink paths associated to this widget.
1466 * While removing the path, remove reference to it from both
1467 * source and sink widgets so that path is removed only once.
1468 */
1469 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1470 list_del(&p->list_sink);
1471 list_del(&p->list_source);
1472 list_del(&p->list);
1473 kfree(p->long_name);
1474 kfree(p);
1475 }
1476 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1477 list_del(&p->list_sink);
1478 list_del(&p->list_source);
1479 list_del(&p->list);
1480 kfree(p->long_name);
1481 kfree(p);
1482 }
ead9b919 1483 kfree(w->name);
2b97eabc
RP
1484 kfree(w);
1485 }
2b97eabc
RP
1486}
1487
91a5fca4
LPC
1488static struct snd_soc_dapm_widget *dapm_find_widget(
1489 struct snd_soc_dapm_context *dapm, const char *pin,
1490 bool search_other_contexts)
a5302181
LG
1491{
1492 struct snd_soc_dapm_widget *w;
91a5fca4 1493 struct snd_soc_dapm_widget *fallback = NULL;
a5302181 1494
97c866de 1495 list_for_each_entry(w, &dapm->card->widgets, list) {
a5302181 1496 if (!strcmp(w->name, pin)) {
91a5fca4
LPC
1497 if (w->dapm == dapm)
1498 return w;
1499 else
1500 fallback = w;
a5302181
LG
1501 }
1502 }
1503
91a5fca4
LPC
1504 if (search_other_contexts)
1505 return fallback;
1506
1507 return NULL;
1508}
1509
1510static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1511 const char *pin, int status)
1512{
1513 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
1514
1515 if (!w) {
1516 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1517 return -EINVAL;
0d86733c
MB
1518 }
1519
91a5fca4
LPC
1520 w->connected = status;
1521 if (status == 0)
1522 w->force = 0;
1523
1524 return 0;
a5302181
LG
1525}
1526
2b97eabc 1527/**
a5302181 1528 * snd_soc_dapm_sync - scan and power dapm paths
ce6120cc 1529 * @dapm: DAPM context
2b97eabc
RP
1530 *
1531 * Walks all dapm audio paths and powers widgets according to their
1532 * stream or path usage.
1533 *
1534 * Returns 0 for success.
1535 */
ce6120cc 1536int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2b97eabc 1537{
ce6120cc 1538 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2b97eabc 1539}
a5302181 1540EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2b97eabc 1541
ce6120cc 1542static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
215edda3 1543 const struct snd_soc_dapm_route *route)
2b97eabc
RP
1544{
1545 struct snd_soc_dapm_path *path;
1546 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
97c866de 1547 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
ead9b919 1548 const char *sink;
215edda3 1549 const char *control = route->control;
ead9b919
JN
1550 const char *source;
1551 char prefixed_sink[80];
1552 char prefixed_source[80];
2b97eabc
RP
1553 int ret = 0;
1554
88e8b9a8 1555 if (dapm->codec && dapm->codec->name_prefix) {
ead9b919
JN
1556 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1557 dapm->codec->name_prefix, route->sink);
1558 sink = prefixed_sink;
1559 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1560 dapm->codec->name_prefix, route->source);
1561 source = prefixed_source;
1562 } else {
1563 sink = route->sink;
1564 source = route->source;
1565 }
1566
97c866de
JN
1567 /*
1568 * find src and dest widgets over all widgets but favor a widget from
1569 * current DAPM context
1570 */
1571 list_for_each_entry(w, &dapm->card->widgets, list) {
2b97eabc 1572 if (!wsink && !(strcmp(w->name, sink))) {
97c866de
JN
1573 wtsink = w;
1574 if (w->dapm == dapm)
1575 wsink = w;
2b97eabc
RP
1576 continue;
1577 }
1578 if (!wsource && !(strcmp(w->name, source))) {
97c866de
JN
1579 wtsource = w;
1580 if (w->dapm == dapm)
1581 wsource = w;
2b97eabc
RP
1582 }
1583 }
97c866de
JN
1584 /* use widget from another DAPM context if not found from this */
1585 if (!wsink)
1586 wsink = wtsink;
1587 if (!wsource)
1588 wsource = wtsource;
2b97eabc
RP
1589
1590 if (wsource == NULL || wsink == NULL)
1591 return -ENODEV;
1592
1593 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1594 if (!path)
1595 return -ENOMEM;
1596
1597 path->source = wsource;
1598 path->sink = wsink;
215edda3 1599 path->connected = route->connected;
2b97eabc
RP
1600 INIT_LIST_HEAD(&path->list);
1601 INIT_LIST_HEAD(&path->list_source);
1602 INIT_LIST_HEAD(&path->list_sink);
1603
1604 /* check for external widgets */
1605 if (wsink->id == snd_soc_dapm_input) {
1606 if (wsource->id == snd_soc_dapm_micbias ||
1607 wsource->id == snd_soc_dapm_mic ||
087d53ab
RC
1608 wsource->id == snd_soc_dapm_line ||
1609 wsource->id == snd_soc_dapm_output)
2b97eabc
RP
1610 wsink->ext = 1;
1611 }
1612 if (wsource->id == snd_soc_dapm_output) {
1613 if (wsink->id == snd_soc_dapm_spk ||
1614 wsink->id == snd_soc_dapm_hp ||
1e39221e
SF
1615 wsink->id == snd_soc_dapm_line ||
1616 wsink->id == snd_soc_dapm_input)
2b97eabc
RP
1617 wsource->ext = 1;
1618 }
1619
1620 /* connect static paths */
1621 if (control == NULL) {
8ddab3f5 1622 list_add(&path->list, &dapm->card->paths);
2b97eabc
RP
1623 list_add(&path->list_sink, &wsink->sources);
1624 list_add(&path->list_source, &wsource->sinks);
1625 path->connect = 1;
1626 return 0;
1627 }
1628
1629 /* connect dynamic paths */
dc2bea61 1630 switch (wsink->id) {
2b97eabc
RP
1631 case snd_soc_dapm_adc:
1632 case snd_soc_dapm_dac:
1633 case snd_soc_dapm_pga:
d88429a6 1634 case snd_soc_dapm_out_drv:
2b97eabc
RP
1635 case snd_soc_dapm_input:
1636 case snd_soc_dapm_output:
1637 case snd_soc_dapm_micbias:
1638 case snd_soc_dapm_vmid:
1639 case snd_soc_dapm_pre:
1640 case snd_soc_dapm_post:
246d0a17 1641 case snd_soc_dapm_supply:
010ff262
MB
1642 case snd_soc_dapm_aif_in:
1643 case snd_soc_dapm_aif_out:
8ddab3f5 1644 list_add(&path->list, &dapm->card->paths);
2b97eabc
RP
1645 list_add(&path->list_sink, &wsink->sources);
1646 list_add(&path->list_source, &wsource->sinks);
1647 path->connect = 1;
1648 return 0;
1649 case snd_soc_dapm_mux:
24ff33ac 1650 case snd_soc_dapm_virt_mux:
74155556 1651 case snd_soc_dapm_value_mux:
ce6120cc 1652 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
82cfecdc 1653 &wsink->kcontrol_news[0]);
2b97eabc
RP
1654 if (ret != 0)
1655 goto err;
1656 break;
1657 case snd_soc_dapm_switch:
1658 case snd_soc_dapm_mixer:
ca9c1aae 1659 case snd_soc_dapm_mixer_named_ctl:
ce6120cc 1660 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2b97eabc
RP
1661 if (ret != 0)
1662 goto err;
1663 break;
1664 case snd_soc_dapm_hp:
1665 case snd_soc_dapm_mic:
1666 case snd_soc_dapm_line:
1667 case snd_soc_dapm_spk:
8ddab3f5 1668 list_add(&path->list, &dapm->card->paths);
2b97eabc
RP
1669 list_add(&path->list_sink, &wsink->sources);
1670 list_add(&path->list_source, &wsource->sinks);
1671 path->connect = 0;
1672 return 0;
1673 }
1674 return 0;
1675
1676err:
f7d41ae8
JN
1677 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1678 source, control, sink);
2b97eabc
RP
1679 kfree(path);
1680 return ret;
1681}
105f1c28 1682
105f1c28
MB
1683/**
1684 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
ce6120cc 1685 * @dapm: DAPM context
105f1c28
MB
1686 * @route: audio routes
1687 * @num: number of routes
1688 *
1689 * Connects 2 dapm widgets together via a named audio path. The sink is
1690 * the widget receiving the audio signal, whilst the source is the sender
1691 * of the audio signal.
1692 *
1693 * Returns 0 for success else error. On error all resources can be freed
1694 * with a call to snd_soc_card_free().
1695 */
ce6120cc 1696int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
105f1c28
MB
1697 const struct snd_soc_dapm_route *route, int num)
1698{
1699 int i, ret;
1700
1701 for (i = 0; i < num; i++) {
ce6120cc 1702 ret = snd_soc_dapm_add_route(dapm, route);
105f1c28 1703 if (ret < 0) {
f7d41ae8
JN
1704 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1705 route->source, route->sink);
105f1c28
MB
1706 return ret;
1707 }
1708 route++;
1709 }
1710
1711 return 0;
1712}
1713EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1714
2b97eabc
RP
1715/**
1716 * snd_soc_dapm_new_widgets - add new dapm widgets
ce6120cc 1717 * @dapm: DAPM context
2b97eabc
RP
1718 *
1719 * Checks the codec for any new dapm widgets and creates them if found.
1720 *
1721 * Returns 0 for success.
1722 */
ce6120cc 1723int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2b97eabc
RP
1724{
1725 struct snd_soc_dapm_widget *w;
b66a70d5 1726 unsigned int val;
2b97eabc 1727
97c866de 1728 list_for_each_entry(w, &dapm->card->widgets, list)
2b97eabc
RP
1729 {
1730 if (w->new)
1731 continue;
1732
1733 switch(w->id) {
1734 case snd_soc_dapm_switch:
1735 case snd_soc_dapm_mixer:
ca9c1aae 1736 case snd_soc_dapm_mixer_named_ctl:
b75576d7 1737 w->power_check = dapm_generic_check_power;
ce6120cc 1738 dapm_new_mixer(dapm, w);
2b97eabc
RP
1739 break;
1740 case snd_soc_dapm_mux:
24ff33ac 1741 case snd_soc_dapm_virt_mux:
2e72f8e3 1742 case snd_soc_dapm_value_mux:
b75576d7 1743 w->power_check = dapm_generic_check_power;
ce6120cc 1744 dapm_new_mux(dapm, w);
2b97eabc
RP
1745 break;
1746 case snd_soc_dapm_adc:
010ff262 1747 case snd_soc_dapm_aif_out:
b75576d7
MB
1748 w->power_check = dapm_adc_check_power;
1749 break;
2b97eabc 1750 case snd_soc_dapm_dac:
010ff262 1751 case snd_soc_dapm_aif_in:
b75576d7
MB
1752 w->power_check = dapm_dac_check_power;
1753 break;
2b97eabc 1754 case snd_soc_dapm_pga:
d88429a6 1755 case snd_soc_dapm_out_drv:
b75576d7 1756 w->power_check = dapm_generic_check_power;
ce6120cc 1757 dapm_new_pga(dapm, w);
2b97eabc
RP
1758 break;
1759 case snd_soc_dapm_input:
1760 case snd_soc_dapm_output:
1761 case snd_soc_dapm_micbias:
1762 case snd_soc_dapm_spk:
1763 case snd_soc_dapm_hp:
1764 case snd_soc_dapm_mic:
1765 case snd_soc_dapm_line:
b75576d7
MB
1766 w->power_check = dapm_generic_check_power;
1767 break;
246d0a17
MB
1768 case snd_soc_dapm_supply:
1769 w->power_check = dapm_supply_check_power;
2b97eabc
RP
1770 case snd_soc_dapm_vmid:
1771 case snd_soc_dapm_pre:
1772 case snd_soc_dapm_post:
1773 break;
1774 }
b66a70d5
MB
1775
1776 /* Read the initial power state from the device */
1777 if (w->reg >= 0) {
1778 val = snd_soc_read(w->codec, w->reg);
1779 val &= 1 << w->shift;
1780 if (w->invert)
1781 val = !val;
1782
1783 if (val)
1784 w->power = 1;
1785 }
1786
2b97eabc 1787 w->new = 1;
d5d1e0be
LPC
1788
1789 dapm_debugfs_add_widget(w);
2b97eabc
RP
1790 }
1791
ce6120cc 1792 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2b97eabc
RP
1793 return 0;
1794}
1795EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1796
1797/**
1798 * snd_soc_dapm_get_volsw - dapm mixer get callback
1799 * @kcontrol: mixer control
ac11a2b3 1800 * @ucontrol: control element information
2b97eabc
RP
1801 *
1802 * Callback to get the value of a dapm mixer control.
1803 *
1804 * Returns 0 for success.
1805 */
1806int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1807 struct snd_ctl_elem_value *ucontrol)
1808{
1809 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
4eaa9819
JS
1810 struct soc_mixer_control *mc =
1811 (struct soc_mixer_control *)kcontrol->private_value;
815ecf8d
JS
1812 unsigned int reg = mc->reg;
1813 unsigned int shift = mc->shift;
1814 unsigned int rshift = mc->rshift;
4eaa9819 1815 int max = mc->max;
815ecf8d
JS
1816 unsigned int invert = mc->invert;
1817 unsigned int mask = (1 << fls(max)) - 1;
2b97eabc 1818
2b97eabc
RP
1819 ucontrol->value.integer.value[0] =
1820 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1821 if (shift != rshift)
1822 ucontrol->value.integer.value[1] =
1823 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1824 if (invert) {
1825 ucontrol->value.integer.value[0] =
a7a4ac86 1826 max - ucontrol->value.integer.value[0];
2b97eabc
RP
1827 if (shift != rshift)
1828 ucontrol->value.integer.value[1] =
a7a4ac86 1829 max - ucontrol->value.integer.value[1];
2b97eabc
RP
1830 }
1831
1832 return 0;
1833}
1834EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1835
1836/**
1837 * snd_soc_dapm_put_volsw - dapm mixer set callback
1838 * @kcontrol: mixer control
ac11a2b3 1839 * @ucontrol: control element information
2b97eabc
RP
1840 *
1841 * Callback to set the value of a dapm mixer control.
1842 *
1843 * Returns 0 for success.
1844 */
1845int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1846 struct snd_ctl_elem_value *ucontrol)
1847{
1848 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
4eaa9819
JS
1849 struct soc_mixer_control *mc =
1850 (struct soc_mixer_control *)kcontrol->private_value;
815ecf8d
JS
1851 unsigned int reg = mc->reg;
1852 unsigned int shift = mc->shift;
4eaa9819 1853 int max = mc->max;
815ecf8d
JS
1854 unsigned int mask = (1 << fls(max)) - 1;
1855 unsigned int invert = mc->invert;
e9cf7049 1856 unsigned int val;
97404f2e
MB
1857 int connect, change;
1858 struct snd_soc_dapm_update update;
2b97eabc
RP
1859
1860 val = (ucontrol->value.integer.value[0] & mask);
1861
1862 if (invert)
a7a4ac86 1863 val = max - val;
e9cf7049 1864 mask = mask << shift;
2b97eabc 1865 val = val << shift;
2b97eabc
RP
1866
1867 mutex_lock(&widget->codec->mutex);
1868 widget->value = val;
1869
e9cf7049 1870 change = snd_soc_test_bits(widget->codec, reg, mask, val);
97404f2e 1871 if (change) {
283375ce
MB
1872 if (val)
1873 /* new connection */
1874 connect = invert ? 0:1;
1875 else
1876 /* old connection must be powered down */
1877 connect = invert ? 1:0;
1878
97404f2e
MB
1879 update.kcontrol = kcontrol;
1880 update.widget = widget;
1881 update.reg = reg;
1882 update.mask = mask;
1883 update.val = val;
1884 widget->dapm->update = &update;
1885
283375ce 1886 dapm_mixer_update_power(widget, kcontrol, connect);
97404f2e
MB
1887
1888 widget->dapm->update = NULL;
283375ce
MB
1889 }
1890
2b97eabc 1891 mutex_unlock(&widget->codec->mutex);
97404f2e 1892 return 0;
2b97eabc
RP
1893}
1894EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1895
1896/**
1897 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1898 * @kcontrol: mixer control
ac11a2b3 1899 * @ucontrol: control element information
2b97eabc
RP
1900 *
1901 * Callback to get the value of a dapm enumerated double mixer control.
1902 *
1903 * Returns 0 for success.
1904 */
1905int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1906 struct snd_ctl_elem_value *ucontrol)
1907{
1908 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1909 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f 1910 unsigned int val, bitmask;
2b97eabc 1911
f8ba0b7b 1912 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2b97eabc
RP
1913 ;
1914 val = snd_soc_read(widget->codec, e->reg);
1915 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1916 if (e->shift_l != e->shift_r)
1917 ucontrol->value.enumerated.item[1] =
1918 (val >> e->shift_r) & (bitmask - 1);
1919
1920 return 0;
1921}
1922EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1923
1924/**
1925 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1926 * @kcontrol: mixer control
ac11a2b3 1927 * @ucontrol: control element information
2b97eabc
RP
1928 *
1929 * Callback to set the value of a dapm enumerated double mixer control.
1930 *
1931 * Returns 0 for success.
1932 */
1933int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1934 struct snd_ctl_elem_value *ucontrol)
1935{
1936 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1937 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3a65577d 1938 unsigned int val, mux, change;
46f5822f 1939 unsigned int mask, bitmask;
97404f2e 1940 struct snd_soc_dapm_update update;
2b97eabc 1941
f8ba0b7b 1942 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2b97eabc 1943 ;
f8ba0b7b 1944 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2b97eabc
RP
1945 return -EINVAL;
1946 mux = ucontrol->value.enumerated.item[0];
1947 val = mux << e->shift_l;
1948 mask = (bitmask - 1) << e->shift_l;
1949 if (e->shift_l != e->shift_r) {
f8ba0b7b 1950 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2b97eabc
RP
1951 return -EINVAL;
1952 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1953 mask |= (bitmask - 1) << e->shift_r;
1954 }
1955
1956 mutex_lock(&widget->codec->mutex);
1957 widget->value = val;
3a65577d 1958 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1642e3d4 1959
97404f2e
MB
1960 update.kcontrol = kcontrol;
1961 update.widget = widget;
1962 update.reg = e->reg;
1963 update.mask = mask;
1964 update.val = val;
1965 widget->dapm->update = &update;
1642e3d4 1966
97404f2e 1967 dapm_mux_update_power(widget, kcontrol, change, mux, e);
1642e3d4 1968
97404f2e 1969 widget->dapm->update = NULL;
2b97eabc 1970
2b97eabc 1971 mutex_unlock(&widget->codec->mutex);
97404f2e 1972 return change;
2b97eabc
RP
1973}
1974EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1975
d2b247a8
MB
1976/**
1977 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1978 * @kcontrol: mixer control
1979 * @ucontrol: control element information
1980 *
1981 * Returns 0 for success.
1982 */
1983int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1984 struct snd_ctl_elem_value *ucontrol)
1985{
1986 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1987
1988 ucontrol->value.enumerated.item[0] = widget->value;
1989
1990 return 0;
1991}
1992EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1993
1994/**
1995 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1996 * @kcontrol: mixer control
1997 * @ucontrol: control element information
1998 *
1999 * Returns 0 for success.
2000 */
2001int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2002 struct snd_ctl_elem_value *ucontrol)
2003{
2004 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
2005 struct soc_enum *e =
2006 (struct soc_enum *)kcontrol->private_value;
2007 int change;
2008 int ret = 0;
2009
2010 if (ucontrol->value.enumerated.item[0] >= e->max)
2011 return -EINVAL;
2012
2013 mutex_lock(&widget->codec->mutex);
2014
2015 change = widget->value != ucontrol->value.enumerated.item[0];
2016 widget->value = ucontrol->value.enumerated.item[0];
2017 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
2018
2019 mutex_unlock(&widget->codec->mutex);
2020 return ret;
2021}
2022EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2023
2e72f8e3
PU
2024/**
2025 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2026 * callback
2027 * @kcontrol: mixer control
2028 * @ucontrol: control element information
2029 *
2030 * Callback to get the value of a dapm semi enumerated double mixer control.
2031 *
2032 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2033 * used for handling bitfield coded enumeration for example.
2034 *
2035 * Returns 0 for success.
2036 */
2037int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2038 struct snd_ctl_elem_value *ucontrol)
2039{
2040 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
74155556 2041 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f 2042 unsigned int reg_val, val, mux;
2e72f8e3
PU
2043
2044 reg_val = snd_soc_read(widget->codec, e->reg);
2045 val = (reg_val >> e->shift_l) & e->mask;
2046 for (mux = 0; mux < e->max; mux++) {
2047 if (val == e->values[mux])
2048 break;
2049 }
2050 ucontrol->value.enumerated.item[0] = mux;
2051 if (e->shift_l != e->shift_r) {
2052 val = (reg_val >> e->shift_r) & e->mask;
2053 for (mux = 0; mux < e->max; mux++) {
2054 if (val == e->values[mux])
2055 break;
2056 }
2057 ucontrol->value.enumerated.item[1] = mux;
2058 }
2059
2060 return 0;
2061}
2062EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2063
2064/**
2065 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2066 * callback
2067 * @kcontrol: mixer control
2068 * @ucontrol: control element information
2069 *
2070 * Callback to set the value of a dapm semi enumerated double mixer control.
2071 *
2072 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2073 * used for handling bitfield coded enumeration for example.
2074 *
2075 * Returns 0 for success.
2076 */
2077int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2078 struct snd_ctl_elem_value *ucontrol)
2079{
2080 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
74155556 2081 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3a65577d 2082 unsigned int val, mux, change;
46f5822f 2083 unsigned int mask;
97404f2e 2084 struct snd_soc_dapm_update update;
2e72f8e3
PU
2085
2086 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2087 return -EINVAL;
2088 mux = ucontrol->value.enumerated.item[0];
2089 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2090 mask = e->mask << e->shift_l;
2091 if (e->shift_l != e->shift_r) {
2092 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2093 return -EINVAL;
2094 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2095 mask |= e->mask << e->shift_r;
2096 }
2097
2098 mutex_lock(&widget->codec->mutex);
2099 widget->value = val;
3a65577d 2100 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1642e3d4 2101
97404f2e
MB
2102 update.kcontrol = kcontrol;
2103 update.widget = widget;
2104 update.reg = e->reg;
2105 update.mask = mask;
2106 update.val = val;
2107 widget->dapm->update = &update;
1642e3d4 2108
97404f2e 2109 dapm_mux_update_power(widget, kcontrol, change, mux, e);
1642e3d4 2110
97404f2e 2111 widget->dapm->update = NULL;
2e72f8e3 2112
2e72f8e3 2113 mutex_unlock(&widget->codec->mutex);
97404f2e 2114 return change;
2e72f8e3
PU
2115}
2116EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2117
8b37dbd2
MB
2118/**
2119 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2120 *
2121 * @kcontrol: mixer control
2122 * @uinfo: control element information
2123 *
2124 * Callback to provide information about a pin switch control.
2125 */
2126int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2127 struct snd_ctl_elem_info *uinfo)
2128{
2129 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2130 uinfo->count = 1;
2131 uinfo->value.integer.min = 0;
2132 uinfo->value.integer.max = 1;
2133
2134 return 0;
2135}
2136EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2137
2138/**
2139 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2140 *
2141 * @kcontrol: mixer control
2142 * @ucontrol: Value
2143 */
2144int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2145 struct snd_ctl_elem_value *ucontrol)
2146{
2147 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2148 const char *pin = (const char *)kcontrol->private_value;
2149
2150 mutex_lock(&codec->mutex);
2151
2152 ucontrol->value.integer.value[0] =
ce6120cc 2153 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
8b37dbd2
MB
2154
2155 mutex_unlock(&codec->mutex);
2156
2157 return 0;
2158}
2159EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2160
2161/**
2162 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2163 *
2164 * @kcontrol: mixer control
2165 * @ucontrol: Value
2166 */
2167int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2168 struct snd_ctl_elem_value *ucontrol)
2169{
2170 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2171 const char *pin = (const char *)kcontrol->private_value;
2172
2173 mutex_lock(&codec->mutex);
2174
2175 if (ucontrol->value.integer.value[0])
ce6120cc 2176 snd_soc_dapm_enable_pin(&codec->dapm, pin);
8b37dbd2 2177 else
ce6120cc 2178 snd_soc_dapm_disable_pin(&codec->dapm, pin);
8b37dbd2 2179
ce6120cc 2180 snd_soc_dapm_sync(&codec->dapm);
8b37dbd2
MB
2181
2182 mutex_unlock(&codec->mutex);
2183
2184 return 0;
2185}
2186EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2187
2b97eabc
RP
2188/**
2189 * snd_soc_dapm_new_control - create new dapm control
ce6120cc 2190 * @dapm: DAPM context
2b97eabc
RP
2191 * @widget: widget template
2192 *
2193 * Creates a new dapm control based upon the template.
2194 *
2195 * Returns 0 for success else error.
2196 */
ce6120cc 2197int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2b97eabc
RP
2198 const struct snd_soc_dapm_widget *widget)
2199{
2200 struct snd_soc_dapm_widget *w;
ead9b919 2201 size_t name_len;
2b97eabc
RP
2202
2203 if ((w = dapm_cnew_widget(widget)) == NULL)
2204 return -ENOMEM;
2205
ead9b919 2206 name_len = strlen(widget->name) + 1;
88e8b9a8 2207 if (dapm->codec && dapm->codec->name_prefix)
ead9b919
JN
2208 name_len += 1 + strlen(dapm->codec->name_prefix);
2209 w->name = kmalloc(name_len, GFP_KERNEL);
2210 if (w->name == NULL) {
2211 kfree(w);
2212 return -ENOMEM;
2213 }
88e8b9a8 2214 if (dapm->codec && dapm->codec->name_prefix)
ead9b919
JN
2215 snprintf(w->name, name_len, "%s %s",
2216 dapm->codec->name_prefix, widget->name);
2217 else
2218 snprintf(w->name, name_len, "%s", widget->name);
2219
97c866de 2220 dapm->n_widgets++;
ce6120cc
LG
2221 w->dapm = dapm;
2222 w->codec = dapm->codec;
2b97eabc
RP
2223 INIT_LIST_HEAD(&w->sources);
2224 INIT_LIST_HEAD(&w->sinks);
2225 INIT_LIST_HEAD(&w->list);
97c866de 2226 list_add(&w->list, &dapm->card->widgets);
2b97eabc
RP
2227
2228 /* machine layer set ups unconnected pins and insertions */
2229 w->connected = 1;
2230 return 0;
2231}
2232EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2233
4ba1327a
MB
2234/**
2235 * snd_soc_dapm_new_controls - create new dapm controls
ce6120cc 2236 * @dapm: DAPM context
4ba1327a
MB
2237 * @widget: widget array
2238 * @num: number of widgets
2239 *
2240 * Creates new DAPM controls based upon the templates.
2241 *
2242 * Returns 0 for success else error.
2243 */
ce6120cc 2244int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
4ba1327a
MB
2245 const struct snd_soc_dapm_widget *widget,
2246 int num)
2247{
2248 int i, ret;
2249
2250 for (i = 0; i < num; i++) {
ce6120cc 2251 ret = snd_soc_dapm_new_control(dapm, widget);
b8b33cb5 2252 if (ret < 0) {
f7d41ae8
JN
2253 dev_err(dapm->dev,
2254 "ASoC: Failed to create DAPM control %s: %d\n",
2255 widget->name, ret);
4ba1327a 2256 return ret;
b8b33cb5 2257 }
4ba1327a
MB
2258 widget++;
2259 }
2260 return 0;
2261}
2262EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2263
ce6120cc 2264static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
f0fba2ad 2265 const char *stream, int event)
2b97eabc
RP
2266{
2267 struct snd_soc_dapm_widget *w;
2268
97c866de 2269 list_for_each_entry(w, &dapm->card->widgets, list)
2b97eabc 2270 {
97c866de 2271 if (!w->sname || w->dapm != dapm)
2b97eabc 2272 continue;
f7d41ae8
JN
2273 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2274 w->name, w->sname, stream, event);
2b97eabc
RP
2275 if (strstr(w->sname, stream)) {
2276 switch(event) {
2277 case SND_SOC_DAPM_STREAM_START:
2278 w->active = 1;
2279 break;
2280 case SND_SOC_DAPM_STREAM_STOP:
2281 w->active = 0;
2282 break;
2283 case SND_SOC_DAPM_STREAM_SUSPEND:
2b97eabc 2284 case SND_SOC_DAPM_STREAM_RESUME:
2b97eabc 2285 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2b97eabc
RP
2286 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2287 break;
2288 }
2289 }
2290 }
2b97eabc 2291
ce6120cc
LG
2292 dapm_power_widgets(dapm, event);
2293}
2294
2295/**
2296 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2297 * @rtd: PCM runtime data
2298 * @stream: stream name
2299 * @event: stream event
2300 *
2301 * Sends a stream event to the dapm core. The core then makes any
2302 * necessary widget power changes.
2303 *
2304 * Returns 0 for success else error.
2305 */
2306int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2307 const char *stream, int event)
2308{
2309 struct snd_soc_codec *codec = rtd->codec;
2310
2311 if (stream == NULL)
2312 return 0;
2313
2314 mutex_lock(&codec->mutex);
2315 soc_dapm_stream_event(&codec->dapm, stream, event);
8e8b2d67 2316 mutex_unlock(&codec->mutex);
2b97eabc
RP
2317 return 0;
2318}
2b97eabc
RP
2319
2320/**
a5302181 2321 * snd_soc_dapm_enable_pin - enable pin.
ce6120cc 2322 * @dapm: DAPM context
a5302181 2323 * @pin: pin name
2b97eabc 2324 *
74b8f955 2325 * Enables input/output pin and its parents or children widgets iff there is
a5302181
LG
2326 * a valid audio route and active audio stream.
2327 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2328 * do any widget power switching.
2b97eabc 2329 */
ce6120cc 2330int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2b97eabc 2331{
ce6120cc 2332 return snd_soc_dapm_set_pin(dapm, pin, 1);
a5302181
LG
2333}
2334EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2b97eabc 2335
da34183e
MB
2336/**
2337 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
ce6120cc 2338 * @dapm: DAPM context
da34183e
MB
2339 * @pin: pin name
2340 *
2341 * Enables input/output pin regardless of any other state. This is
2342 * intended for use with microphone bias supplies used in microphone
2343 * jack detection.
2344 *
2345 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2346 * do any widget power switching.
2347 */
ce6120cc
LG
2348int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2349 const char *pin)
da34183e 2350{
91a5fca4 2351 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
da34183e 2352
91a5fca4
LPC
2353 if (!w) {
2354 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2355 return -EINVAL;
0d86733c
MB
2356 }
2357
91a5fca4
LPC
2358 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2359 w->connected = 1;
2360 w->force = 1;
da34183e 2361
91a5fca4 2362 return 0;
da34183e
MB
2363}
2364EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2365
a5302181
LG
2366/**
2367 * snd_soc_dapm_disable_pin - disable pin.
ce6120cc 2368 * @dapm: DAPM context
a5302181
LG
2369 * @pin: pin name
2370 *
74b8f955 2371 * Disables input/output pin and its parents or children widgets.
a5302181
LG
2372 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2373 * do any widget power switching.
2374 */
ce6120cc
LG
2375int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2376 const char *pin)
a5302181 2377{
ce6120cc 2378 return snd_soc_dapm_set_pin(dapm, pin, 0);
2b97eabc 2379}
a5302181 2380EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2b97eabc 2381
5817b52a
MB
2382/**
2383 * snd_soc_dapm_nc_pin - permanently disable pin.
ce6120cc 2384 * @dapm: DAPM context
5817b52a
MB
2385 * @pin: pin name
2386 *
2387 * Marks the specified pin as being not connected, disabling it along
2388 * any parent or child widgets. At present this is identical to
2389 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2390 * additional things such as disabling controls which only affect
2391 * paths through the pin.
2392 *
2393 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2394 * do any widget power switching.
2395 */
ce6120cc 2396int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
5817b52a 2397{
ce6120cc 2398 return snd_soc_dapm_set_pin(dapm, pin, 0);
5817b52a
MB
2399}
2400EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2401
eeec12bf 2402/**
a5302181 2403 * snd_soc_dapm_get_pin_status - get audio pin status
ce6120cc 2404 * @dapm: DAPM context
a5302181 2405 * @pin: audio signal pin endpoint (or start point)
eeec12bf 2406 *
a5302181 2407 * Get audio pin status - connected or disconnected.
eeec12bf 2408 *
a5302181 2409 * Returns 1 for connected otherwise 0.
eeec12bf 2410 */
ce6120cc
LG
2411int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2412 const char *pin)
eeec12bf 2413{
91a5fca4 2414 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
eeec12bf 2415
91a5fca4
LPC
2416 if (w)
2417 return w->connected;
a68b38ad 2418
eeec12bf
GG
2419 return 0;
2420}
a5302181 2421EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
eeec12bf 2422
1547aba9
MB
2423/**
2424 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
ce6120cc 2425 * @dapm: DAPM context
1547aba9
MB
2426 * @pin: audio signal pin endpoint (or start point)
2427 *
2428 * Mark the given endpoint or pin as ignoring suspend. When the
2429 * system is disabled a path between two endpoints flagged as ignoring
2430 * suspend will not be disabled. The path must already be enabled via
2431 * normal means at suspend time, it will not be turned on if it was not
2432 * already enabled.
2433 */
ce6120cc
LG
2434int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2435 const char *pin)
1547aba9 2436{
91a5fca4 2437 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
1547aba9 2438
91a5fca4
LPC
2439 if (!w) {
2440 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2441 return -EINVAL;
1547aba9
MB
2442 }
2443
91a5fca4
LPC
2444 w->ignore_suspend = 1;
2445
2446 return 0;
1547aba9
MB
2447}
2448EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2449
2b97eabc
RP
2450/**
2451 * snd_soc_dapm_free - free dapm resources
f0fba2ad 2452 * @card: SoC device
2b97eabc
RP
2453 *
2454 * Free all dapm widgets and resources.
2455 */
ce6120cc 2456void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2b97eabc 2457{
ce6120cc 2458 snd_soc_dapm_sys_remove(dapm->dev);
6c45e126 2459 dapm_debugfs_cleanup(dapm);
ce6120cc 2460 dapm_free_widgets(dapm);
7be31be8 2461 list_del(&dapm->list);
2b97eabc
RP
2462}
2463EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2464
ce6120cc 2465static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
51737470 2466{
51737470
MB
2467 struct snd_soc_dapm_widget *w;
2468 LIST_HEAD(down_list);
2469 int powerdown = 0;
2470
97c866de
JN
2471 list_for_each_entry(w, &dapm->card->widgets, list) {
2472 if (w->dapm != dapm)
2473 continue;
51737470 2474 if (w->power) {
828a842f 2475 dapm_seq_insert(w, &down_list, false);
c2caa4da 2476 w->power = 0;
51737470
MB
2477 powerdown = 1;
2478 }
2479 }
2480
2481 /* If there were no widgets to power down we're already in
2482 * standby.
2483 */
2484 if (powerdown) {
ed5a4c47 2485 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
828a842f 2486 dapm_seq_run(dapm, &down_list, 0, false);
ed5a4c47 2487 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
51737470 2488 }
f0fba2ad
LG
2489}
2490
2491/*
2492 * snd_soc_dapm_shutdown - callback for system shutdown
2493 */
2494void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2495{
2496 struct snd_soc_codec *codec;
2497
ce6120cc
LG
2498 list_for_each_entry(codec, &card->codec_dev_list, list) {
2499 soc_dapm_shutdown_codec(&codec->dapm);
ed5a4c47 2500 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
ce6120cc 2501 }
51737470
MB
2502}
2503
2b97eabc 2504/* Module information */
d331124d 2505MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2b97eabc
RP
2506MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2507MODULE_LICENSE("GPL");