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