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