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