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