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