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