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