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