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