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