]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - sound/soc/soc-dapm.c
ASoC: Allow setting codec register with debugfs filesystem
[linux-2.6-omap-h63xx.git] / sound / soc / soc-dapm.c
1 /*
2  * soc-dapm.c  --  ALSA SoC Dynamic Audio Power Management
3  *
4  * Copyright 2005 Wolfson Microelectronics PLC.
5  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
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  *
12  *  Features:
13  *    o Changes power status of internal codec blocks depending on the
14  *      dynamic configuration of codec internal audio paths and active
15  *      DAC's/ADC's.
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
21  *    o Delayed powerdown of audio susbsystem to reduce pops between a quick
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>
40 #include <sound/core.h>
41 #include <sound/pcm.h>
42 #include <sound/pcm_params.h>
43 #include <sound/soc-dapm.h>
44 #include <sound/initval.h>
45
46 /* debug */
47 #ifdef DEBUG
48 #define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
49 #else
50 #define dump_dapm(codec, action)
51 #endif
52
53 /* dapm power sequences - make this per codec in the future */
54 static int dapm_up_seq[] = {
55         snd_soc_dapm_pre, snd_soc_dapm_micbias, snd_soc_dapm_mic,
56         snd_soc_dapm_mux, snd_soc_dapm_dac, snd_soc_dapm_mixer, snd_soc_dapm_pga,
57         snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk, snd_soc_dapm_post
58 };
59 static int dapm_down_seq[] = {
60         snd_soc_dapm_pre, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
61         snd_soc_dapm_pga, snd_soc_dapm_mixer, snd_soc_dapm_dac, snd_soc_dapm_mic,
62         snd_soc_dapm_micbias, snd_soc_dapm_mux, snd_soc_dapm_post
63 };
64
65 static int dapm_status = 1;
66 module_param(dapm_status, int, 0);
67 MODULE_PARM_DESC(dapm_status, "enable DPM sysfs entries");
68
69 static void pop_wait(u32 pop_time)
70 {
71         if (pop_time)
72                 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
73 }
74
75 static void pop_dbg(u32 pop_time, const char *fmt, ...)
76 {
77         va_list args;
78
79         va_start(args, fmt);
80
81         if (pop_time) {
82                 vprintk(fmt, args);
83                 pop_wait(pop_time);
84         }
85
86         va_end(args);
87 }
88
89 /* create a new dapm widget */
90 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
91         const struct snd_soc_dapm_widget *_widget)
92 {
93         return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
94 }
95
96 /* set up initial codec paths */
97 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
98         struct snd_soc_dapm_path *p, int i)
99 {
100         switch (w->id) {
101         case snd_soc_dapm_switch:
102         case snd_soc_dapm_mixer: {
103                 int val;
104                 struct soc_mixer_control *mc = (struct soc_mixer_control *)
105                         w->kcontrols[i].private_value;
106                 unsigned int reg = mc->reg;
107                 unsigned int shift = mc->shift;
108                 int max = mc->max;
109                 unsigned int mask = (1 << fls(max)) - 1;
110                 unsigned int invert = mc->invert;
111
112                 val = snd_soc_read(w->codec, reg);
113                 val = (val >> shift) & mask;
114
115                 if ((invert && !val) || (!invert && val))
116                         p->connect = 1;
117                 else
118                         p->connect = 0;
119         }
120         break;
121         case snd_soc_dapm_mux: {
122                 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
123                 int val, item, bitmask;
124
125                 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
126                 ;
127                 val = snd_soc_read(w->codec, e->reg);
128                 item = (val >> e->shift_l) & (bitmask - 1);
129
130                 p->connect = 0;
131                 for (i = 0; i < e->max; i++) {
132                         if (!(strcmp(p->name, e->texts[i])) && item == i)
133                                 p->connect = 1;
134                 }
135         }
136         break;
137         /* does not effect routing - always connected */
138         case snd_soc_dapm_pga:
139         case snd_soc_dapm_output:
140         case snd_soc_dapm_adc:
141         case snd_soc_dapm_input:
142         case snd_soc_dapm_dac:
143         case snd_soc_dapm_micbias:
144         case snd_soc_dapm_vmid:
145                 p->connect = 1;
146         break;
147         /* does effect routing - dynamically connected */
148         case snd_soc_dapm_hp:
149         case snd_soc_dapm_mic:
150         case snd_soc_dapm_spk:
151         case snd_soc_dapm_line:
152         case snd_soc_dapm_pre:
153         case snd_soc_dapm_post:
154                 p->connect = 0;
155         break;
156         }
157 }
158
159 /* connect mux widget to it's interconnecting audio paths */
160 static int dapm_connect_mux(struct snd_soc_codec *codec,
161         struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
162         struct snd_soc_dapm_path *path, const char *control_name,
163         const struct snd_kcontrol_new *kcontrol)
164 {
165         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
166         int i;
167
168         for (i = 0; i < e->max; i++) {
169                 if (!(strcmp(control_name, e->texts[i]))) {
170                         list_add(&path->list, &codec->dapm_paths);
171                         list_add(&path->list_sink, &dest->sources);
172                         list_add(&path->list_source, &src->sinks);
173                         path->name = (char*)e->texts[i];
174                         dapm_set_path_status(dest, path, 0);
175                         return 0;
176                 }
177         }
178
179         return -ENODEV;
180 }
181
182 /* connect mixer widget to it's interconnecting audio paths */
183 static int dapm_connect_mixer(struct snd_soc_codec *codec,
184         struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
185         struct snd_soc_dapm_path *path, const char *control_name)
186 {
187         int i;
188
189         /* search for mixer kcontrol */
190         for (i = 0; i < dest->num_kcontrols; i++) {
191                 if (!strcmp(control_name, dest->kcontrols[i].name)) {
192                         list_add(&path->list, &codec->dapm_paths);
193                         list_add(&path->list_sink, &dest->sources);
194                         list_add(&path->list_source, &src->sinks);
195                         path->name = dest->kcontrols[i].name;
196                         dapm_set_path_status(dest, path, i);
197                         return 0;
198                 }
199         }
200         return -ENODEV;
201 }
202
203 /* update dapm codec register bits */
204 static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
205 {
206         int change, power;
207         unsigned short old, new;
208         struct snd_soc_codec *codec = widget->codec;
209
210         /* check for valid widgets */
211         if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
212                 widget->id == snd_soc_dapm_output ||
213                 widget->id == snd_soc_dapm_hp ||
214                 widget->id == snd_soc_dapm_mic ||
215                 widget->id == snd_soc_dapm_line ||
216                 widget->id == snd_soc_dapm_spk)
217                 return 0;
218
219         power = widget->power;
220         if (widget->invert)
221                 power = (power ? 0:1);
222
223         old = snd_soc_read(codec, widget->reg);
224         new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
225
226         change = old != new;
227         if (change) {
228                 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
229                         widget->name, widget->power ? "on" : "off",
230                         codec->pop_time);
231                 snd_soc_write(codec, widget->reg, new);
232                 pop_wait(codec->pop_time);
233         }
234         pr_debug("reg %x old %x new %x change %d\n", widget->reg,
235                  old, new, change);
236         return change;
237 }
238
239 /* ramps the volume up or down to minimise pops before or after a
240  * DAPM power event */
241 static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
242 {
243         const struct snd_kcontrol_new *k = widget->kcontrols;
244
245         if (widget->muted && !power)
246                 return 0;
247         if (!widget->muted && power)
248                 return 0;
249
250         if (widget->num_kcontrols && k) {
251                 struct soc_mixer_control *mc =
252                         (struct soc_mixer_control *)k->private_value;
253                 unsigned int reg = mc->reg;
254                 unsigned int shift = mc->shift;
255                 int max = mc->max;
256                 unsigned int mask = (1 << fls(max)) - 1;
257                 unsigned int invert = mc->invert;
258
259                 if (power) {
260                         int i;
261                         /* power up has happended, increase volume to last level */
262                         if (invert) {
263                                 for (i = max; i > widget->saved_value; i--)
264                                         snd_soc_update_bits(widget->codec, reg, mask, i);
265                         } else {
266                                 for (i = 0; i < widget->saved_value; i++)
267                                         snd_soc_update_bits(widget->codec, reg, mask, i);
268                         }
269                         widget->muted = 0;
270                 } else {
271                         /* power down is about to occur, decrease volume to mute */
272                         int val = snd_soc_read(widget->codec, reg);
273                         int i = widget->saved_value = (val >> shift) & mask;
274                         if (invert) {
275                                 for (; i < mask; i++)
276                                         snd_soc_update_bits(widget->codec, reg, mask, i);
277                         } else {
278                                 for (; i > 0; i--)
279                                         snd_soc_update_bits(widget->codec, reg, mask, i);
280                         }
281                         widget->muted = 1;
282                 }
283         }
284         return 0;
285 }
286
287 /* create new dapm mixer control */
288 static int dapm_new_mixer(struct snd_soc_codec *codec,
289         struct snd_soc_dapm_widget *w)
290 {
291         int i, ret = 0;
292         char name[32];
293         struct snd_soc_dapm_path *path;
294
295         /* add kcontrol */
296         for (i = 0; i < w->num_kcontrols; i++) {
297
298                 /* match name */
299                 list_for_each_entry(path, &w->sources, list_sink) {
300
301                         /* mixer/mux paths name must match control name */
302                         if (path->name != (char*)w->kcontrols[i].name)
303                                 continue;
304
305                         /* add dapm control with long name */
306                         snprintf(name, 32, "%s %s", w->name, w->kcontrols[i].name);
307                         path->long_name = kstrdup (name, GFP_KERNEL);
308                         if (path->long_name == NULL)
309                                 return -ENOMEM;
310
311                         path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
312                                 path->long_name);
313                         ret = snd_ctl_add(codec->card, path->kcontrol);
314                         if (ret < 0) {
315                                 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s\n",
316                                                 path->long_name);
317                                 kfree(path->long_name);
318                                 path->long_name = NULL;
319                                 return ret;
320                         }
321                 }
322         }
323         return ret;
324 }
325
326 /* create new dapm mux control */
327 static int dapm_new_mux(struct snd_soc_codec *codec,
328         struct snd_soc_dapm_widget *w)
329 {
330         struct snd_soc_dapm_path *path = NULL;
331         struct snd_kcontrol *kcontrol;
332         int ret = 0;
333
334         if (!w->num_kcontrols) {
335                 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
336                 return -EINVAL;
337         }
338
339         kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
340         ret = snd_ctl_add(codec->card, kcontrol);
341         if (ret < 0)
342                 goto err;
343
344         list_for_each_entry(path, &w->sources, list_sink)
345                 path->kcontrol = kcontrol;
346
347         return ret;
348
349 err:
350         printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
351         return ret;
352 }
353
354 /* create new dapm volume control */
355 static int dapm_new_pga(struct snd_soc_codec *codec,
356         struct snd_soc_dapm_widget *w)
357 {
358         struct snd_kcontrol *kcontrol;
359         int ret = 0;
360
361         if (!w->num_kcontrols)
362                 return -EINVAL;
363
364         kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
365         ret = snd_ctl_add(codec->card, kcontrol);
366         if (ret < 0) {
367                 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
368                 return ret;
369         }
370
371         return ret;
372 }
373
374 /* reset 'walked' bit for each dapm path */
375 static inline void dapm_clear_walk(struct snd_soc_codec *codec)
376 {
377         struct snd_soc_dapm_path *p;
378
379         list_for_each_entry(p, &codec->dapm_paths, list)
380                 p->walked = 0;
381 }
382
383 /*
384  * Recursively check for a completed path to an active or physically connected
385  * output widget. Returns number of complete paths.
386  */
387 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
388 {
389         struct snd_soc_dapm_path *path;
390         int con = 0;
391
392         if (widget->id == snd_soc_dapm_adc && widget->active)
393                 return 1;
394
395         if (widget->connected) {
396                 /* connected pin ? */
397                 if (widget->id == snd_soc_dapm_output && !widget->ext)
398                         return 1;
399
400                 /* connected jack or spk ? */
401                 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
402                         widget->id == snd_soc_dapm_line)
403                         return 1;
404         }
405
406         list_for_each_entry(path, &widget->sinks, list_source) {
407                 if (path->walked)
408                         continue;
409
410                 if (path->sink && path->connect) {
411                         path->walked = 1;
412                         con += is_connected_output_ep(path->sink);
413                 }
414         }
415
416         return con;
417 }
418
419 /*
420  * Recursively check for a completed path to an active or physically connected
421  * input widget. Returns number of complete paths.
422  */
423 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
424 {
425         struct snd_soc_dapm_path *path;
426         int con = 0;
427
428         /* active stream ? */
429         if (widget->id == snd_soc_dapm_dac && widget->active)
430                 return 1;
431
432         if (widget->connected) {
433                 /* connected pin ? */
434                 if (widget->id == snd_soc_dapm_input && !widget->ext)
435                         return 1;
436
437                 /* connected VMID/Bias for lower pops */
438                 if (widget->id == snd_soc_dapm_vmid)
439                         return 1;
440
441                 /* connected jack ? */
442                 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
443                         return 1;
444         }
445
446         list_for_each_entry(path, &widget->sources, list_sink) {
447                 if (path->walked)
448                         continue;
449
450                 if (path->source && path->connect) {
451                         path->walked = 1;
452                         con += is_connected_input_ep(path->source);
453                 }
454         }
455
456         return con;
457 }
458
459 /*
460  * Handler for generic register modifier widget.
461  */
462 int dapm_reg_event(struct snd_soc_dapm_widget *w,
463                    struct snd_kcontrol *kcontrol, int event)
464 {
465         unsigned int val;
466
467         if (SND_SOC_DAPM_EVENT_ON(event))
468                 val = w->on_val;
469         else
470                 val = w->off_val;
471
472         snd_soc_update_bits(w->codec, -(w->reg + 1),
473                             w->mask << w->shift, val << w->shift);
474
475         return 0;
476 }
477 EXPORT_SYMBOL_GPL(dapm_reg_event);
478
479 /*
480  * Scan each dapm widget for complete audio path.
481  * A complete path is a route that has valid endpoints i.e.:-
482  *
483  *  o DAC to output pin.
484  *  o Input Pin to ADC.
485  *  o Input pin to Output pin (bypass, sidetone)
486  *  o DAC to ADC (loopback).
487  */
488 static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
489 {
490         struct snd_soc_dapm_widget *w;
491         int in, out, i, c = 1, *seq = NULL, ret = 0, power_change, power;
492
493         /* do we have a sequenced stream event */
494         if (event == SND_SOC_DAPM_STREAM_START) {
495                 c = ARRAY_SIZE(dapm_up_seq);
496                 seq = dapm_up_seq;
497         } else if (event == SND_SOC_DAPM_STREAM_STOP) {
498                 c = ARRAY_SIZE(dapm_down_seq);
499                 seq = dapm_down_seq;
500         }
501
502         for(i = 0; i < c; i++) {
503                 list_for_each_entry(w, &codec->dapm_widgets, list) {
504
505                         /* is widget in stream order */
506                         if (seq && seq[i] && w->id != seq[i])
507                                 continue;
508
509                         /* vmid - no action */
510                         if (w->id == snd_soc_dapm_vmid)
511                                 continue;
512
513                         /* active ADC */
514                         if (w->id == snd_soc_dapm_adc && w->active) {
515                                 in = is_connected_input_ep(w);
516                                 dapm_clear_walk(w->codec);
517                                 w->power = (in != 0) ? 1 : 0;
518                                 dapm_update_bits(w);
519                                 continue;
520                         }
521
522                         /* active DAC */
523                         if (w->id == snd_soc_dapm_dac && w->active) {
524                                 out = is_connected_output_ep(w);
525                                 dapm_clear_walk(w->codec);
526                                 w->power = (out != 0) ? 1 : 0;
527                                 dapm_update_bits(w);
528                                 continue;
529                         }
530
531                         /* pre and post event widgets */
532                         if (w->id == snd_soc_dapm_pre) {
533                                 if (!w->event)
534                                         continue;
535
536                                 if (event == SND_SOC_DAPM_STREAM_START) {
537                                         ret = w->event(w,
538                                                 NULL, SND_SOC_DAPM_PRE_PMU);
539                                         if (ret < 0)
540                                                 return ret;
541                                 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
542                                         ret = w->event(w,
543                                                 NULL, SND_SOC_DAPM_PRE_PMD);
544                                         if (ret < 0)
545                                                 return ret;
546                                 }
547                                 continue;
548                         }
549                         if (w->id == snd_soc_dapm_post) {
550                                 if (!w->event)
551                                         continue;
552
553                                 if (event == SND_SOC_DAPM_STREAM_START) {
554                                         ret = w->event(w,
555                                                 NULL, SND_SOC_DAPM_POST_PMU);
556                                         if (ret < 0)
557                                                 return ret;
558                                 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
559                                         ret = w->event(w,
560                                                 NULL, SND_SOC_DAPM_POST_PMD);
561                                         if (ret < 0)
562                                                 return ret;
563                                 }
564                                 continue;
565                         }
566
567                         /* all other widgets */
568                         in = is_connected_input_ep(w);
569                         dapm_clear_walk(w->codec);
570                         out = is_connected_output_ep(w);
571                         dapm_clear_walk(w->codec);
572                         power = (out != 0 && in != 0) ? 1 : 0;
573                         power_change = (w->power == power) ? 0: 1;
574                         w->power = power;
575
576                         if (!power_change)
577                                 continue;
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 (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 (!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
601                         /* Lower PGA volume to reduce pops */
602                         if (w->id == snd_soc_dapm_pga && !power)
603                                 dapm_set_pga(w, power);
604
605                         dapm_update_bits(w);
606
607                         /* Raise PGA volume to reduce pops */
608                         if (w->id == snd_soc_dapm_pga && power)
609                                 dapm_set_pga(w, power);
610
611                         /* power up post event */
612                         if (power && w->event &&
613                             (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
614                                 ret = w->event(w,
615                                                NULL, SND_SOC_DAPM_POST_PMU);
616                                 if (ret < 0)
617                                         return ret;
618                         }
619
620                         /* power down post event */
621                         if (!power && w->event &&
622                             (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
623                                 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
624                                 if (ret < 0)
625                                         return ret;
626                         }
627                 }
628         }
629
630         return ret;
631 }
632
633 #ifdef DEBUG
634 static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
635 {
636         struct snd_soc_dapm_widget *w;
637         struct snd_soc_dapm_path *p = NULL;
638         int in, out;
639
640         printk("DAPM %s %s\n", codec->name, action);
641
642         list_for_each_entry(w, &codec->dapm_widgets, list) {
643
644                 /* only display widgets that effect routing */
645                 switch (w->id) {
646                 case snd_soc_dapm_pre:
647                 case snd_soc_dapm_post:
648                 case snd_soc_dapm_vmid:
649                         continue;
650                 case snd_soc_dapm_mux:
651                 case snd_soc_dapm_output:
652                 case snd_soc_dapm_input:
653                 case snd_soc_dapm_switch:
654                 case snd_soc_dapm_hp:
655                 case snd_soc_dapm_mic:
656                 case snd_soc_dapm_spk:
657                 case snd_soc_dapm_line:
658                 case snd_soc_dapm_micbias:
659                 case snd_soc_dapm_dac:
660                 case snd_soc_dapm_adc:
661                 case snd_soc_dapm_pga:
662                 case snd_soc_dapm_mixer:
663                         if (w->name) {
664                                 in = is_connected_input_ep(w);
665                                 dapm_clear_walk(w->codec);
666                                 out = is_connected_output_ep(w);
667                                 dapm_clear_walk(w->codec);
668                                 printk("%s: %s  in %d out %d\n", w->name,
669                                         w->power ? "On":"Off",in, out);
670
671                                 list_for_each_entry(p, &w->sources, list_sink) {
672                                         if (p->connect)
673                                                 printk(" in  %s %s\n", p->name ? p->name : "static",
674                                                         p->source->name);
675                                 }
676                                 list_for_each_entry(p, &w->sinks, list_source) {
677                                         if (p->connect)
678                                                 printk(" out %s %s\n", p->name ? p->name : "static",
679                                                         p->sink->name);
680                                 }
681                         }
682                 break;
683                 }
684         }
685 }
686 #endif
687
688 /* test and update the power status of a mux widget */
689 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
690                                  struct snd_kcontrol *kcontrol, int mask,
691                                  int mux, int val, struct soc_enum *e)
692 {
693         struct snd_soc_dapm_path *path;
694         int found = 0;
695
696         if (widget->id != snd_soc_dapm_mux)
697                 return -ENODEV;
698
699         if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
700                 return 0;
701
702         /* find dapm widget path assoc with kcontrol */
703         list_for_each_entry(path, &widget->codec->dapm_paths, list) {
704                 if (path->kcontrol != kcontrol)
705                         continue;
706
707                 if (!path->name || !e->texts[mux])
708                         continue;
709
710                 found = 1;
711                 /* we now need to match the string in the enum to the path */
712                 if (!(strcmp(path->name, e->texts[mux])))
713                         path->connect = 1; /* new connection */
714                 else
715                         path->connect = 0; /* old connection must be powered down */
716         }
717
718         if (found) {
719                 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
720                 dump_dapm(widget->codec, "mux power update");
721         }
722
723         return 0;
724 }
725
726 /* test and update the power status of a mixer or switch widget */
727 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
728                                    struct snd_kcontrol *kcontrol, int reg,
729                                    int val_mask, int val, int invert)
730 {
731         struct snd_soc_dapm_path *path;
732         int found = 0;
733
734         if (widget->id != snd_soc_dapm_mixer &&
735             widget->id != snd_soc_dapm_switch)
736                 return -ENODEV;
737
738         if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
739                 return 0;
740
741         /* find dapm widget path assoc with kcontrol */
742         list_for_each_entry(path, &widget->codec->dapm_paths, list) {
743                 if (path->kcontrol != kcontrol)
744                         continue;
745
746                 /* found, now check type */
747                 found = 1;
748                 if (val)
749                         /* new connection */
750                         path->connect = invert ? 0:1;
751                 else
752                         /* old connection must be powered down */
753                         path->connect = invert ? 1:0;
754                 break;
755         }
756
757         if (found) {
758                 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
759                 dump_dapm(widget->codec, "mixer power update");
760         }
761
762         return 0;
763 }
764
765 /* show dapm widget status in sys fs */
766 static ssize_t dapm_widget_show(struct device *dev,
767         struct device_attribute *attr, char *buf)
768 {
769         struct snd_soc_device *devdata = dev_get_drvdata(dev);
770         struct snd_soc_codec *codec = devdata->codec;
771         struct snd_soc_dapm_widget *w;
772         int count = 0;
773         char *state = "not set";
774
775         list_for_each_entry(w, &codec->dapm_widgets, list) {
776
777                 /* only display widgets that burnm power */
778                 switch (w->id) {
779                 case snd_soc_dapm_hp:
780                 case snd_soc_dapm_mic:
781                 case snd_soc_dapm_spk:
782                 case snd_soc_dapm_line:
783                 case snd_soc_dapm_micbias:
784                 case snd_soc_dapm_dac:
785                 case snd_soc_dapm_adc:
786                 case snd_soc_dapm_pga:
787                 case snd_soc_dapm_mixer:
788                         if (w->name)
789                                 count += sprintf(buf + count, "%s: %s\n",
790                                         w->name, w->power ? "On":"Off");
791                 break;
792                 default:
793                 break;
794                 }
795         }
796
797         switch (codec->bias_level) {
798         case SND_SOC_BIAS_ON:
799                 state = "On";
800                 break;
801         case SND_SOC_BIAS_PREPARE:
802                 state = "Prepare";
803                 break;
804         case SND_SOC_BIAS_STANDBY:
805                 state = "Standby";
806                 break;
807         case SND_SOC_BIAS_OFF:
808                 state = "Off";
809                 break;
810         }
811         count += sprintf(buf + count, "PM State: %s\n", state);
812
813         return count;
814 }
815
816 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
817
818 int snd_soc_dapm_sys_add(struct device *dev)
819 {
820         if (!dapm_status)
821                 return 0;
822
823         ret = device_create_file(dev, &dev_attr_dapm_widget);
824         if (ret != 0)
825                 return ret;
826         return device_create_file(dev, &dev_attr_dapm_widget);
827 }
828
829 static void snd_soc_dapm_sys_remove(struct device *dev)
830 {
831         if (dapm_status) {
832                 device_remove_file(dev, &dev_attr_dapm_widget);
833         }
834 }
835
836 /* free all dapm widgets and resources */
837 static void dapm_free_widgets(struct snd_soc_codec *codec)
838 {
839         struct snd_soc_dapm_widget *w, *next_w;
840         struct snd_soc_dapm_path *p, *next_p;
841
842         list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
843                 list_del(&w->list);
844                 kfree(w);
845         }
846
847         list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
848                 list_del(&p->list);
849                 kfree(p->long_name);
850                 kfree(p);
851         }
852 }
853
854 static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
855         char *pin, int status)
856 {
857         struct snd_soc_dapm_widget *w;
858
859         list_for_each_entry(w, &codec->dapm_widgets, list) {
860                 if (!strcmp(w->name, pin)) {
861                         pr_debug("dapm: %s: pin %s\n", codec->name, pin);
862                         w->connected = status;
863                         return 0;
864                 }
865         }
866
867         pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
868         return -EINVAL;
869 }
870
871 /**
872  * snd_soc_dapm_sync - scan and power dapm paths
873  * @codec: audio codec
874  *
875  * Walks all dapm audio paths and powers widgets according to their
876  * stream or path usage.
877  *
878  * Returns 0 for success.
879  */
880 int snd_soc_dapm_sync(struct snd_soc_codec *codec)
881 {
882         int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
883         dump_dapm(codec, "sync");
884         return ret;
885 }
886 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
887
888 static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
889         const char *sink, const char *control, const char *source)
890 {
891         struct snd_soc_dapm_path *path;
892         struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
893         int ret = 0;
894
895         /* find src and dest widgets */
896         list_for_each_entry(w, &codec->dapm_widgets, list) {
897
898                 if (!wsink && !(strcmp(w->name, sink))) {
899                         wsink = w;
900                         continue;
901                 }
902                 if (!wsource && !(strcmp(w->name, source))) {
903                         wsource = w;
904                 }
905         }
906
907         if (wsource == NULL || wsink == NULL)
908                 return -ENODEV;
909
910         path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
911         if (!path)
912                 return -ENOMEM;
913
914         path->source = wsource;
915         path->sink = wsink;
916         INIT_LIST_HEAD(&path->list);
917         INIT_LIST_HEAD(&path->list_source);
918         INIT_LIST_HEAD(&path->list_sink);
919
920         /* check for external widgets */
921         if (wsink->id == snd_soc_dapm_input) {
922                 if (wsource->id == snd_soc_dapm_micbias ||
923                         wsource->id == snd_soc_dapm_mic ||
924                         wsink->id == snd_soc_dapm_line ||
925                         wsink->id == snd_soc_dapm_output)
926                         wsink->ext = 1;
927         }
928         if (wsource->id == snd_soc_dapm_output) {
929                 if (wsink->id == snd_soc_dapm_spk ||
930                         wsink->id == snd_soc_dapm_hp ||
931                         wsink->id == snd_soc_dapm_line ||
932                         wsink->id == snd_soc_dapm_input)
933                         wsource->ext = 1;
934         }
935
936         /* connect static paths */
937         if (control == NULL) {
938                 list_add(&path->list, &codec->dapm_paths);
939                 list_add(&path->list_sink, &wsink->sources);
940                 list_add(&path->list_source, &wsource->sinks);
941                 path->connect = 1;
942                 return 0;
943         }
944
945         /* connect dynamic paths */
946         switch(wsink->id) {
947         case snd_soc_dapm_adc:
948         case snd_soc_dapm_dac:
949         case snd_soc_dapm_pga:
950         case snd_soc_dapm_input:
951         case snd_soc_dapm_output:
952         case snd_soc_dapm_micbias:
953         case snd_soc_dapm_vmid:
954         case snd_soc_dapm_pre:
955         case snd_soc_dapm_post:
956                 list_add(&path->list, &codec->dapm_paths);
957                 list_add(&path->list_sink, &wsink->sources);
958                 list_add(&path->list_source, &wsource->sinks);
959                 path->connect = 1;
960                 return 0;
961         case snd_soc_dapm_mux:
962                 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
963                         &wsink->kcontrols[0]);
964                 if (ret != 0)
965                         goto err;
966                 break;
967         case snd_soc_dapm_switch:
968         case snd_soc_dapm_mixer:
969                 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
970                 if (ret != 0)
971                         goto err;
972                 break;
973         case snd_soc_dapm_hp:
974         case snd_soc_dapm_mic:
975         case snd_soc_dapm_line:
976         case snd_soc_dapm_spk:
977                 list_add(&path->list, &codec->dapm_paths);
978                 list_add(&path->list_sink, &wsink->sources);
979                 list_add(&path->list_source, &wsource->sinks);
980                 path->connect = 0;
981                 return 0;
982         }
983         return 0;
984
985 err:
986         printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
987                 control, sink);
988         kfree(path);
989         return ret;
990 }
991
992 /**
993  * snd_soc_dapm_add_routes - Add routes between DAPM widgets
994  * @codec: codec
995  * @route: audio routes
996  * @num: number of routes
997  *
998  * Connects 2 dapm widgets together via a named audio path. The sink is
999  * the widget receiving the audio signal, whilst the source is the sender
1000  * of the audio signal.
1001  *
1002  * Returns 0 for success else error. On error all resources can be freed
1003  * with a call to snd_soc_card_free().
1004  */
1005 int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1006                             const struct snd_soc_dapm_route *route, int num)
1007 {
1008         int i, ret;
1009
1010         for (i = 0; i < num; i++) {
1011                 ret = snd_soc_dapm_add_route(codec, route->sink,
1012                                              route->control, route->source);
1013                 if (ret < 0) {
1014                         printk(KERN_ERR "Failed to add route %s->%s\n",
1015                                route->source,
1016                                route->sink);
1017                         return ret;
1018                 }
1019                 route++;
1020         }
1021
1022         return 0;
1023 }
1024 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1025
1026 /**
1027  * snd_soc_dapm_new_widgets - add new dapm widgets
1028  * @codec: audio codec
1029  *
1030  * Checks the codec for any new dapm widgets and creates them if found.
1031  *
1032  * Returns 0 for success.
1033  */
1034 int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1035 {
1036         struct snd_soc_dapm_widget *w;
1037
1038         list_for_each_entry(w, &codec->dapm_widgets, list)
1039         {
1040                 if (w->new)
1041                         continue;
1042
1043                 switch(w->id) {
1044                 case snd_soc_dapm_switch:
1045                 case snd_soc_dapm_mixer:
1046                         dapm_new_mixer(codec, w);
1047                         break;
1048                 case snd_soc_dapm_mux:
1049                         dapm_new_mux(codec, w);
1050                         break;
1051                 case snd_soc_dapm_adc:
1052                 case snd_soc_dapm_dac:
1053                 case snd_soc_dapm_pga:
1054                         dapm_new_pga(codec, w);
1055                         break;
1056                 case snd_soc_dapm_input:
1057                 case snd_soc_dapm_output:
1058                 case snd_soc_dapm_micbias:
1059                 case snd_soc_dapm_spk:
1060                 case snd_soc_dapm_hp:
1061                 case snd_soc_dapm_mic:
1062                 case snd_soc_dapm_line:
1063                 case snd_soc_dapm_vmid:
1064                 case snd_soc_dapm_pre:
1065                 case snd_soc_dapm_post:
1066                         break;
1067                 }
1068                 w->new = 1;
1069         }
1070
1071         dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1072         return 0;
1073 }
1074 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1075
1076 /**
1077  * snd_soc_dapm_get_volsw - dapm mixer get callback
1078  * @kcontrol: mixer control
1079  * @uinfo: control element information
1080  *
1081  * Callback to get the value of a dapm mixer control.
1082  *
1083  * Returns 0 for success.
1084  */
1085 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1086         struct snd_ctl_elem_value *ucontrol)
1087 {
1088         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1089         struct soc_mixer_control *mc =
1090                 (struct soc_mixer_control *)kcontrol->private_value;
1091         unsigned int reg = mc->reg;
1092         unsigned int shift = mc->shift;
1093         unsigned int rshift = mc->rshift;
1094         int max = mc->max;
1095         unsigned int invert = mc->invert;
1096         unsigned int mask = (1 << fls(max)) - 1;
1097
1098         /* return the saved value if we are powered down */
1099         if (widget->id == snd_soc_dapm_pga && !widget->power) {
1100                 ucontrol->value.integer.value[0] = widget->saved_value;
1101                 return 0;
1102         }
1103
1104         ucontrol->value.integer.value[0] =
1105                 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1106         if (shift != rshift)
1107                 ucontrol->value.integer.value[1] =
1108                         (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1109         if (invert) {
1110                 ucontrol->value.integer.value[0] =
1111                         max - ucontrol->value.integer.value[0];
1112                 if (shift != rshift)
1113                         ucontrol->value.integer.value[1] =
1114                                 max - ucontrol->value.integer.value[1];
1115         }
1116
1117         return 0;
1118 }
1119 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1120
1121 /**
1122  * snd_soc_dapm_put_volsw - dapm mixer set callback
1123  * @kcontrol: mixer control
1124  * @uinfo: control element information
1125  *
1126  * Callback to set the value of a dapm mixer control.
1127  *
1128  * Returns 0 for success.
1129  */
1130 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1131         struct snd_ctl_elem_value *ucontrol)
1132 {
1133         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1134         struct soc_mixer_control *mc =
1135                 (struct soc_mixer_control *)kcontrol->private_value;
1136         unsigned int reg = mc->reg;
1137         unsigned int shift = mc->shift;
1138         unsigned int rshift = mc->rshift;
1139         int max = mc->max;
1140         unsigned int mask = (1 << fls(max)) - 1;
1141         unsigned int invert = mc->invert;
1142         unsigned short val, val2, val_mask;
1143         int ret;
1144
1145         val = (ucontrol->value.integer.value[0] & mask);
1146
1147         if (invert)
1148                 val = max - val;
1149         val_mask = mask << shift;
1150         val = val << shift;
1151         if (shift != rshift) {
1152                 val2 = (ucontrol->value.integer.value[1] & mask);
1153                 if (invert)
1154                         val2 = max - val2;
1155                 val_mask |= mask << rshift;
1156                 val |= val2 << rshift;
1157         }
1158
1159         mutex_lock(&widget->codec->mutex);
1160         widget->value = val;
1161
1162         /* save volume value if the widget is powered down */
1163         if (widget->id == snd_soc_dapm_pga && !widget->power) {
1164                 widget->saved_value = val;
1165                 mutex_unlock(&widget->codec->mutex);
1166                 return 1;
1167         }
1168
1169         dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1170         if (widget->event) {
1171                 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1172                         ret = widget->event(widget, kcontrol,
1173                                                 SND_SOC_DAPM_PRE_REG);
1174                         if (ret < 0) {
1175                                 ret = 1;
1176                                 goto out;
1177                         }
1178                 }
1179                 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1180                 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1181                         ret = widget->event(widget, kcontrol,
1182                                                 SND_SOC_DAPM_POST_REG);
1183         } else
1184                 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1185
1186 out:
1187         mutex_unlock(&widget->codec->mutex);
1188         return ret;
1189 }
1190 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1191
1192 /**
1193  * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1194  * @kcontrol: mixer control
1195  * @uinfo: control element information
1196  *
1197  * Callback to get the value of a dapm enumerated double mixer control.
1198  *
1199  * Returns 0 for success.
1200  */
1201 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1202         struct snd_ctl_elem_value *ucontrol)
1203 {
1204         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1205         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1206         unsigned short val, bitmask;
1207
1208         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1209                 ;
1210         val = snd_soc_read(widget->codec, e->reg);
1211         ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1212         if (e->shift_l != e->shift_r)
1213                 ucontrol->value.enumerated.item[1] =
1214                         (val >> e->shift_r) & (bitmask - 1);
1215
1216         return 0;
1217 }
1218 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1219
1220 /**
1221  * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1222  * @kcontrol: mixer control
1223  * @uinfo: control element information
1224  *
1225  * Callback to set the value of a dapm enumerated double mixer control.
1226  *
1227  * Returns 0 for success.
1228  */
1229 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1230         struct snd_ctl_elem_value *ucontrol)
1231 {
1232         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1233         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1234         unsigned short val, mux;
1235         unsigned short mask, bitmask;
1236         int ret = 0;
1237
1238         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1239                 ;
1240         if (ucontrol->value.enumerated.item[0] > e->max - 1)
1241                 return -EINVAL;
1242         mux = ucontrol->value.enumerated.item[0];
1243         val = mux << e->shift_l;
1244         mask = (bitmask - 1) << e->shift_l;
1245         if (e->shift_l != e->shift_r) {
1246                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1247                         return -EINVAL;
1248                 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1249                 mask |= (bitmask - 1) << e->shift_r;
1250         }
1251
1252         mutex_lock(&widget->codec->mutex);
1253         widget->value = val;
1254         dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
1255         if (widget->event) {
1256                 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1257                         ret = widget->event(widget,
1258                                 kcontrol, SND_SOC_DAPM_PRE_REG);
1259                         if (ret < 0)
1260                                 goto out;
1261                 }
1262                 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1263                 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1264                         ret = widget->event(widget,
1265                                 kcontrol, SND_SOC_DAPM_POST_REG);
1266         } else
1267                 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1268
1269 out:
1270         mutex_unlock(&widget->codec->mutex);
1271         return ret;
1272 }
1273 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1274
1275 /**
1276  * snd_soc_dapm_new_control - create new dapm control
1277  * @codec: audio codec
1278  * @widget: widget template
1279  *
1280  * Creates a new dapm control based upon the template.
1281  *
1282  * Returns 0 for success else error.
1283  */
1284 int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1285         const struct snd_soc_dapm_widget *widget)
1286 {
1287         struct snd_soc_dapm_widget *w;
1288
1289         if ((w = dapm_cnew_widget(widget)) == NULL)
1290                 return -ENOMEM;
1291
1292         w->codec = codec;
1293         INIT_LIST_HEAD(&w->sources);
1294         INIT_LIST_HEAD(&w->sinks);
1295         INIT_LIST_HEAD(&w->list);
1296         list_add(&w->list, &codec->dapm_widgets);
1297
1298         /* machine layer set ups unconnected pins and insertions */
1299         w->connected = 1;
1300         return 0;
1301 }
1302 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1303
1304 /**
1305  * snd_soc_dapm_new_controls - create new dapm controls
1306  * @codec: audio codec
1307  * @widget: widget array
1308  * @num: number of widgets
1309  *
1310  * Creates new DAPM controls based upon the templates.
1311  *
1312  * Returns 0 for success else error.
1313  */
1314 int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1315         const struct snd_soc_dapm_widget *widget,
1316         int num)
1317 {
1318         int i, ret;
1319
1320         for (i = 0; i < num; i++) {
1321                 ret = snd_soc_dapm_new_control(codec, widget);
1322                 if (ret < 0)
1323                         return ret;
1324                 widget++;
1325         }
1326         return 0;
1327 }
1328 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1329
1330
1331 /**
1332  * snd_soc_dapm_stream_event - send a stream event to the dapm core
1333  * @codec: audio codec
1334  * @stream: stream name
1335  * @event: stream event
1336  *
1337  * Sends a stream event to the dapm core. The core then makes any
1338  * necessary widget power changes.
1339  *
1340  * Returns 0 for success else error.
1341  */
1342 int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1343         char *stream, int event)
1344 {
1345         struct snd_soc_dapm_widget *w;
1346
1347         if (stream == NULL)
1348                 return 0;
1349
1350         mutex_lock(&codec->mutex);
1351         list_for_each_entry(w, &codec->dapm_widgets, list)
1352         {
1353                 if (!w->sname)
1354                         continue;
1355                 pr_debug("widget %s\n %s stream %s event %d\n",
1356                          w->name, w->sname, stream, event);
1357                 if (strstr(w->sname, stream)) {
1358                         switch(event) {
1359                         case SND_SOC_DAPM_STREAM_START:
1360                                 w->active = 1;
1361                                 break;
1362                         case SND_SOC_DAPM_STREAM_STOP:
1363                                 w->active = 0;
1364                                 break;
1365                         case SND_SOC_DAPM_STREAM_SUSPEND:
1366                                 if (w->active)
1367                                         w->suspend = 1;
1368                                 w->active = 0;
1369                                 break;
1370                         case SND_SOC_DAPM_STREAM_RESUME:
1371                                 if (w->suspend) {
1372                                         w->active = 1;
1373                                         w->suspend = 0;
1374                                 }
1375                                 break;
1376                         case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1377                                 break;
1378                         case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1379                                 break;
1380                         }
1381                 }
1382         }
1383         mutex_unlock(&codec->mutex);
1384
1385         dapm_power_widgets(codec, event);
1386         dump_dapm(codec, __func__);
1387         return 0;
1388 }
1389 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1390
1391 /**
1392  * snd_soc_dapm_set_bias_level - set the bias level for the system
1393  * @socdev: audio device
1394  * @level: level to configure
1395  *
1396  * Configure the bias (power) levels for the SoC audio device.
1397  *
1398  * Returns 0 for success else error.
1399  */
1400 int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
1401                                 enum snd_soc_bias_level level)
1402 {
1403         struct snd_soc_codec *codec = socdev->codec;
1404         struct snd_soc_machine *machine = socdev->machine;
1405         int ret = 0;
1406
1407         if (machine->set_bias_level)
1408                 ret = machine->set_bias_level(machine, level);
1409         if (ret == 0 && codec->set_bias_level)
1410                 ret = codec->set_bias_level(codec, level);
1411
1412         return ret;
1413 }
1414
1415 /**
1416  * snd_soc_dapm_enable_pin - enable pin.
1417  * @snd_soc_codec: SoC codec
1418  * @pin: pin name
1419  *
1420  * Enables input/output pin and it's parents or children widgets iff there is
1421  * a valid audio route and active audio stream.
1422  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1423  * do any widget power switching.
1424  */
1425 int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, char *pin)
1426 {
1427         return snd_soc_dapm_set_pin(codec, pin, 1);
1428 }
1429 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
1430
1431 /**
1432  * snd_soc_dapm_disable_pin - disable pin.
1433  * @codec: SoC codec
1434  * @pin: pin name
1435  *
1436  * Disables input/output pin and it's parents or children widgets.
1437  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1438  * do any widget power switching.
1439  */
1440 int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, char *pin)
1441 {
1442         return snd_soc_dapm_set_pin(codec, pin, 0);
1443 }
1444 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
1445
1446 /**
1447  * snd_soc_dapm_nc_pin - permanently disable pin.
1448  * @codec: SoC codec
1449  * @pin: pin name
1450  *
1451  * Marks the specified pin as being not connected, disabling it along
1452  * any parent or child widgets.  At present this is identical to
1453  * snd_soc_dapm_disable_pin() but in future it will be extended to do
1454  * additional things such as disabling controls which only affect
1455  * paths through the pin.
1456  *
1457  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1458  * do any widget power switching.
1459  */
1460 int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, char *pin)
1461 {
1462         return snd_soc_dapm_set_pin(codec, pin, 0);
1463 }
1464 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
1465
1466 /**
1467  * snd_soc_dapm_get_pin_status - get audio pin status
1468  * @codec: audio codec
1469  * @pin: audio signal pin endpoint (or start point)
1470  *
1471  * Get audio pin status - connected or disconnected.
1472  *
1473  * Returns 1 for connected otherwise 0.
1474  */
1475 int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, char *pin)
1476 {
1477         struct snd_soc_dapm_widget *w;
1478
1479         list_for_each_entry(w, &codec->dapm_widgets, list) {
1480                 if (!strcmp(w->name, pin))
1481                         return w->connected;
1482         }
1483
1484         return 0;
1485 }
1486 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
1487
1488 /**
1489  * snd_soc_dapm_free - free dapm resources
1490  * @socdev: SoC device
1491  *
1492  * Free all dapm widgets and resources.
1493  */
1494 void snd_soc_dapm_free(struct snd_soc_device *socdev)
1495 {
1496         struct snd_soc_codec *codec = socdev->codec;
1497
1498         snd_soc_dapm_sys_remove(socdev->dev);
1499         dapm_free_widgets(codec);
1500 }
1501 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1502
1503 /* Module information */
1504 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
1505 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1506 MODULE_LICENSE("GPL");