]> Pileus Git - ~andy/linux/blob - sound/soc/soc-dapm.c
ASoC: Assign power_check when we allocate DAPM widgets
[~andy/linux] / 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/async.h>
36 #include <linux/delay.h>
37 #include <linux/pm.h>
38 #include <linux/bitops.h>
39 #include <linux/platform_device.h>
40 #include <linux/jiffies.h>
41 #include <linux/debugfs.h>
42 #include <linux/slab.h>
43 #include <sound/core.h>
44 #include <sound/pcm.h>
45 #include <sound/pcm_params.h>
46 #include <sound/soc.h>
47 #include <sound/initval.h>
48
49 #include <trace/events/asoc.h>
50
51 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
52
53 /* dapm power sequences - make this per codec in the future */
54 static int dapm_up_seq[] = {
55         [snd_soc_dapm_pre] = 0,
56         [snd_soc_dapm_supply] = 1,
57         [snd_soc_dapm_micbias] = 2,
58         [snd_soc_dapm_aif_in] = 3,
59         [snd_soc_dapm_aif_out] = 3,
60         [snd_soc_dapm_mic] = 4,
61         [snd_soc_dapm_mux] = 5,
62         [snd_soc_dapm_virt_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_out_drv] = 10,
70         [snd_soc_dapm_hp] = 10,
71         [snd_soc_dapm_spk] = 10,
72         [snd_soc_dapm_post] = 11,
73 };
74
75 static int dapm_down_seq[] = {
76         [snd_soc_dapm_pre] = 0,
77         [snd_soc_dapm_adc] = 1,
78         [snd_soc_dapm_hp] = 2,
79         [snd_soc_dapm_spk] = 2,
80         [snd_soc_dapm_out_drv] = 2,
81         [snd_soc_dapm_pga] = 4,
82         [snd_soc_dapm_mixer_named_ctl] = 5,
83         [snd_soc_dapm_mixer] = 5,
84         [snd_soc_dapm_dac] = 6,
85         [snd_soc_dapm_mic] = 7,
86         [snd_soc_dapm_micbias] = 8,
87         [snd_soc_dapm_mux] = 9,
88         [snd_soc_dapm_virt_mux] = 9,
89         [snd_soc_dapm_value_mux] = 9,
90         [snd_soc_dapm_aif_in] = 10,
91         [snd_soc_dapm_aif_out] = 10,
92         [snd_soc_dapm_supply] = 11,
93         [snd_soc_dapm_post] = 12,
94 };
95
96 static void pop_wait(u32 pop_time)
97 {
98         if (pop_time)
99                 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
100 }
101
102 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
103 {
104         va_list args;
105         char *buf;
106
107         if (!pop_time)
108                 return;
109
110         buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
111         if (buf == NULL)
112                 return;
113
114         va_start(args, fmt);
115         vsnprintf(buf, PAGE_SIZE, fmt, args);
116         dev_info(dev, "%s", buf);
117         va_end(args);
118
119         kfree(buf);
120 }
121
122 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
123 {
124         return !list_empty(&w->dirty);
125 }
126
127 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
128 {
129         if (!dapm_dirty_widget(w)) {
130                 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
131                          w->name, reason);
132                 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
133         }
134 }
135
136 /* create a new dapm widget */
137 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
138         const struct snd_soc_dapm_widget *_widget)
139 {
140         return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
141 }
142
143 /* get snd_card from DAPM context */
144 static inline struct snd_card *dapm_get_snd_card(
145         struct snd_soc_dapm_context *dapm)
146 {
147         if (dapm->codec)
148                 return dapm->codec->card->snd_card;
149         else if (dapm->platform)
150                 return dapm->platform->card->snd_card;
151         else
152                 BUG();
153
154         /* unreachable */
155         return NULL;
156 }
157
158 /* get soc_card from DAPM context */
159 static inline struct snd_soc_card *dapm_get_soc_card(
160                 struct snd_soc_dapm_context *dapm)
161 {
162         if (dapm->codec)
163                 return dapm->codec->card;
164         else if (dapm->platform)
165                 return dapm->platform->card;
166         else
167                 BUG();
168
169         /* unreachable */
170         return NULL;
171 }
172
173 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
174 {
175         if (w->codec)
176                 return snd_soc_read(w->codec, reg);
177         else if (w->platform)
178                 return snd_soc_platform_read(w->platform, reg);
179
180         dev_err(w->dapm->dev, "no valid widget read method\n");
181         return -1;
182 }
183
184 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
185 {
186         if (w->codec)
187                 return snd_soc_write(w->codec, reg, val);
188         else if (w->platform)
189                 return snd_soc_platform_write(w->platform, reg, val);
190
191         dev_err(w->dapm->dev, "no valid widget write method\n");
192         return -1;
193 }
194
195 static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
196         unsigned short reg, unsigned int mask, unsigned int value)
197 {
198         int change;
199         unsigned int old, new;
200         int ret;
201
202         ret = soc_widget_read(w, reg);
203         if (ret < 0)
204                 return ret;
205
206         old = ret;
207         new = (old & ~mask) | (value & mask);
208         change = old != new;
209         if (change) {
210                 ret = soc_widget_write(w, reg, new);
211                 if (ret < 0)
212                         return ret;
213         }
214
215         return change;
216 }
217
218 /**
219  * snd_soc_dapm_set_bias_level - set the bias level for the system
220  * @dapm: DAPM context
221  * @level: level to configure
222  *
223  * Configure the bias (power) levels for the SoC audio device.
224  *
225  * Returns 0 for success else error.
226  */
227 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
228                                        enum snd_soc_bias_level level)
229 {
230         struct snd_soc_card *card = dapm->card;
231         int ret = 0;
232
233         trace_snd_soc_bias_level_start(card, level);
234
235         if (card && card->set_bias_level)
236                 ret = card->set_bias_level(card, dapm, level);
237         if (ret != 0)
238                 goto out;
239
240         if (dapm->codec) {
241                 if (dapm->codec->driver->set_bias_level)
242                         ret = dapm->codec->driver->set_bias_level(dapm->codec,
243                                                                   level);
244                 else
245                         dapm->bias_level = level;
246         }
247         if (ret != 0)
248                 goto out;
249
250         if (card && card->set_bias_level_post)
251                 ret = card->set_bias_level_post(card, dapm, level);
252 out:
253         trace_snd_soc_bias_level_done(card, level);
254
255         return ret;
256 }
257
258 /* set up initial codec paths */
259 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
260         struct snd_soc_dapm_path *p, int i)
261 {
262         switch (w->id) {
263         case snd_soc_dapm_switch:
264         case snd_soc_dapm_mixer:
265         case snd_soc_dapm_mixer_named_ctl: {
266                 int val;
267                 struct soc_mixer_control *mc = (struct soc_mixer_control *)
268                         w->kcontrol_news[i].private_value;
269                 unsigned int reg = mc->reg;
270                 unsigned int shift = mc->shift;
271                 int max = mc->max;
272                 unsigned int mask = (1 << fls(max)) - 1;
273                 unsigned int invert = mc->invert;
274
275                 val = soc_widget_read(w, reg);
276                 val = (val >> shift) & mask;
277
278                 if ((invert && !val) || (!invert && val))
279                         p->connect = 1;
280                 else
281                         p->connect = 0;
282         }
283         break;
284         case snd_soc_dapm_mux: {
285                 struct soc_enum *e = (struct soc_enum *)
286                         w->kcontrol_news[i].private_value;
287                 int val, item, bitmask;
288
289                 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
290                         ;
291                 val = soc_widget_read(w, e->reg);
292                 item = (val >> e->shift_l) & (bitmask - 1);
293
294                 p->connect = 0;
295                 for (i = 0; i < e->max; i++) {
296                         if (!(strcmp(p->name, e->texts[i])) && item == i)
297                                 p->connect = 1;
298                 }
299         }
300         break;
301         case snd_soc_dapm_virt_mux: {
302                 struct soc_enum *e = (struct soc_enum *)
303                         w->kcontrol_news[i].private_value;
304
305                 p->connect = 0;
306                 /* since a virtual mux has no backing registers to
307                  * decide which path to connect, it will try to match
308                  * with the first enumeration.  This is to ensure
309                  * that the default mux choice (the first) will be
310                  * correctly powered up during initialization.
311                  */
312                 if (!strcmp(p->name, e->texts[0]))
313                         p->connect = 1;
314         }
315         break;
316         case snd_soc_dapm_value_mux: {
317                 struct soc_enum *e = (struct soc_enum *)
318                         w->kcontrol_news[i].private_value;
319                 int val, item;
320
321                 val = soc_widget_read(w, e->reg);
322                 val = (val >> e->shift_l) & e->mask;
323                 for (item = 0; item < e->max; item++) {
324                         if (val == e->values[item])
325                                 break;
326                 }
327
328                 p->connect = 0;
329                 for (i = 0; i < e->max; i++) {
330                         if (!(strcmp(p->name, e->texts[i])) && item == i)
331                                 p->connect = 1;
332                 }
333         }
334         break;
335         /* does not affect routing - always connected */
336         case snd_soc_dapm_pga:
337         case snd_soc_dapm_out_drv:
338         case snd_soc_dapm_output:
339         case snd_soc_dapm_adc:
340         case snd_soc_dapm_input:
341         case snd_soc_dapm_dac:
342         case snd_soc_dapm_micbias:
343         case snd_soc_dapm_vmid:
344         case snd_soc_dapm_supply:
345         case snd_soc_dapm_aif_in:
346         case snd_soc_dapm_aif_out:
347         case snd_soc_dapm_hp:
348         case snd_soc_dapm_mic:
349         case snd_soc_dapm_spk:
350         case snd_soc_dapm_line:
351                 p->connect = 1;
352         break;
353         /* does affect routing - dynamically connected */
354         case snd_soc_dapm_pre:
355         case snd_soc_dapm_post:
356                 p->connect = 0;
357         break;
358         }
359 }
360
361 /* connect mux widget to its interconnecting audio paths */
362 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
363         struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
364         struct snd_soc_dapm_path *path, const char *control_name,
365         const struct snd_kcontrol_new *kcontrol)
366 {
367         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
368         int i;
369
370         for (i = 0; i < e->max; i++) {
371                 if (!(strcmp(control_name, e->texts[i]))) {
372                         list_add(&path->list, &dapm->card->paths);
373                         list_add(&path->list_sink, &dest->sources);
374                         list_add(&path->list_source, &src->sinks);
375                         path->name = (char*)e->texts[i];
376                         dapm_set_path_status(dest, path, 0);
377                         return 0;
378                 }
379         }
380
381         return -ENODEV;
382 }
383
384 /* connect mixer widget to its interconnecting audio paths */
385 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
386         struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
387         struct snd_soc_dapm_path *path, const char *control_name)
388 {
389         int i;
390
391         /* search for mixer kcontrol */
392         for (i = 0; i < dest->num_kcontrols; i++) {
393                 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
394                         list_add(&path->list, &dapm->card->paths);
395                         list_add(&path->list_sink, &dest->sources);
396                         list_add(&path->list_source, &src->sinks);
397                         path->name = dest->kcontrol_news[i].name;
398                         dapm_set_path_status(dest, path, i);
399                         return 0;
400                 }
401         }
402         return -ENODEV;
403 }
404
405 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
406         struct snd_soc_dapm_widget *kcontrolw,
407         const struct snd_kcontrol_new *kcontrol_new,
408         struct snd_kcontrol **kcontrol)
409 {
410         struct snd_soc_dapm_widget *w;
411         int i;
412
413         *kcontrol = NULL;
414
415         list_for_each_entry(w, &dapm->card->widgets, list) {
416                 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
417                         continue;
418                 for (i = 0; i < w->num_kcontrols; i++) {
419                         if (&w->kcontrol_news[i] == kcontrol_new) {
420                                 if (w->kcontrols)
421                                         *kcontrol = w->kcontrols[i];
422                                 return 1;
423                         }
424                 }
425         }
426
427         return 0;
428 }
429
430 /* create new dapm mixer control */
431 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
432 {
433         struct snd_soc_dapm_context *dapm = w->dapm;
434         int i, ret = 0;
435         size_t name_len, prefix_len;
436         struct snd_soc_dapm_path *path;
437         struct snd_card *card = dapm->card->snd_card;
438         const char *prefix;
439         struct snd_soc_dapm_widget_list *wlist;
440         size_t wlistsize;
441
442         if (dapm->codec)
443                 prefix = dapm->codec->name_prefix;
444         else
445                 prefix = NULL;
446
447         if (prefix)
448                 prefix_len = strlen(prefix) + 1;
449         else
450                 prefix_len = 0;
451
452         /* add kcontrol */
453         for (i = 0; i < w->num_kcontrols; i++) {
454
455                 /* match name */
456                 list_for_each_entry(path, &w->sources, list_sink) {
457
458                         /* mixer/mux paths name must match control name */
459                         if (path->name != (char *)w->kcontrol_news[i].name)
460                                 continue;
461
462                         if (w->kcontrols[i]) {
463                                 path->kcontrol = w->kcontrols[i];
464                                 continue;
465                         }
466
467                         wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
468                                     sizeof(struct snd_soc_dapm_widget *),
469                         wlist = kzalloc(wlistsize, GFP_KERNEL);
470                         if (wlist == NULL) {
471                                 dev_err(dapm->dev,
472                                         "asoc: can't allocate widget list for %s\n",
473                                         w->name);
474                                 return -ENOMEM;
475                         }
476                         wlist->num_widgets = 1;
477                         wlist->widgets[0] = w;
478
479                         /* add dapm control with long name.
480                          * for dapm_mixer this is the concatenation of the
481                          * mixer and kcontrol name.
482                          * for dapm_mixer_named_ctl this is simply the
483                          * kcontrol name.
484                          */
485                         name_len = strlen(w->kcontrol_news[i].name) + 1;
486                         if (w->id != snd_soc_dapm_mixer_named_ctl)
487                                 name_len += 1 + strlen(w->name);
488
489                         path->long_name = kmalloc(name_len, GFP_KERNEL);
490
491                         if (path->long_name == NULL) {
492                                 kfree(wlist);
493                                 return -ENOMEM;
494                         }
495
496                         switch (w->id) {
497                         default:
498                                 /* The control will get a prefix from
499                                  * the control creation process but
500                                  * we're also using the same prefix
501                                  * for widgets so cut the prefix off
502                                  * the front of the widget name.
503                                  */
504                                 snprintf(path->long_name, name_len, "%s %s",
505                                          w->name + prefix_len,
506                                          w->kcontrol_news[i].name);
507                                 break;
508                         case snd_soc_dapm_mixer_named_ctl:
509                                 snprintf(path->long_name, name_len, "%s",
510                                          w->kcontrol_news[i].name);
511                                 break;
512                         }
513
514                         path->long_name[name_len - 1] = '\0';
515
516                         path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
517                                                       wlist, path->long_name,
518                                                       prefix);
519                         ret = snd_ctl_add(card, path->kcontrol);
520                         if (ret < 0) {
521                                 dev_err(dapm->dev,
522                                         "asoc: failed to add dapm kcontrol %s: %d\n",
523                                         path->long_name, ret);
524                                 kfree(wlist);
525                                 kfree(path->long_name);
526                                 path->long_name = NULL;
527                                 return ret;
528                         }
529                         w->kcontrols[i] = path->kcontrol;
530                 }
531         }
532         return ret;
533 }
534
535 /* create new dapm mux control */
536 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
537 {
538         struct snd_soc_dapm_context *dapm = w->dapm;
539         struct snd_soc_dapm_path *path = NULL;
540         struct snd_kcontrol *kcontrol;
541         struct snd_card *card = dapm->card->snd_card;
542         const char *prefix;
543         size_t prefix_len;
544         int ret;
545         struct snd_soc_dapm_widget_list *wlist;
546         int shared, wlistentries;
547         size_t wlistsize;
548         char *name;
549
550         if (w->num_kcontrols != 1) {
551                 dev_err(dapm->dev,
552                         "asoc: mux %s has incorrect number of controls\n",
553                         w->name);
554                 return -EINVAL;
555         }
556
557         shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
558                                          &kcontrol);
559         if (kcontrol) {
560                 wlist = kcontrol->private_data;
561                 wlistentries = wlist->num_widgets + 1;
562         } else {
563                 wlist = NULL;
564                 wlistentries = 1;
565         }
566         wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
567                 wlistentries * sizeof(struct snd_soc_dapm_widget *),
568         wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
569         if (wlist == NULL) {
570                 dev_err(dapm->dev,
571                         "asoc: can't allocate widget list for %s\n", w->name);
572                 return -ENOMEM;
573         }
574         wlist->num_widgets = wlistentries;
575         wlist->widgets[wlistentries - 1] = w;
576
577         if (!kcontrol) {
578                 if (dapm->codec)
579                         prefix = dapm->codec->name_prefix;
580                 else
581                         prefix = NULL;
582
583                 if (shared) {
584                         name = w->kcontrol_news[0].name;
585                         prefix_len = 0;
586                 } else {
587                         name = w->name;
588                         if (prefix)
589                                 prefix_len = strlen(prefix) + 1;
590                         else
591                                 prefix_len = 0;
592                 }
593
594                 /*
595                  * The control will get a prefix from the control creation
596                  * process but we're also using the same prefix for widgets so
597                  * cut the prefix off the front of the widget name.
598                  */
599                 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
600                                         name + prefix_len, prefix);
601                 ret = snd_ctl_add(card, kcontrol);
602                 if (ret < 0) {
603                         dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
604                                 w->name, ret);
605                         kfree(wlist);
606                         return ret;
607                 }
608         }
609
610         kcontrol->private_data = wlist;
611
612         w->kcontrols[0] = kcontrol;
613
614         list_for_each_entry(path, &w->sources, list_sink)
615                 path->kcontrol = kcontrol;
616
617         return 0;
618 }
619
620 /* create new dapm volume control */
621 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
622 {
623         if (w->num_kcontrols)
624                 dev_err(w->dapm->dev,
625                         "asoc: PGA controls not supported: '%s'\n", w->name);
626
627         return 0;
628 }
629
630 /* reset 'walked' bit for each dapm path */
631 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
632 {
633         struct snd_soc_dapm_path *p;
634
635         list_for_each_entry(p, &dapm->card->paths, list)
636                 p->walked = 0;
637 }
638
639 /* We implement power down on suspend by checking the power state of
640  * the ALSA card - when we are suspending the ALSA state for the card
641  * is set to D3.
642  */
643 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
644 {
645         int level = snd_power_get_state(widget->dapm->card->snd_card);
646
647         switch (level) {
648         case SNDRV_CTL_POWER_D3hot:
649         case SNDRV_CTL_POWER_D3cold:
650                 if (widget->ignore_suspend)
651                         dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
652                                 widget->name);
653                 return widget->ignore_suspend;
654         default:
655                 return 1;
656         }
657 }
658
659 /*
660  * Recursively check for a completed path to an active or physically connected
661  * output widget. Returns number of complete paths.
662  */
663 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
664 {
665         struct snd_soc_dapm_path *path;
666         int con = 0;
667
668         DAPM_UPDATE_STAT(widget, path_checks);
669
670         if (widget->id == snd_soc_dapm_supply)
671                 return 0;
672
673         switch (widget->id) {
674         case snd_soc_dapm_adc:
675         case snd_soc_dapm_aif_out:
676                 if (widget->active)
677                         return snd_soc_dapm_suspend_check(widget);
678         default:
679                 break;
680         }
681
682         if (widget->connected) {
683                 /* connected pin ? */
684                 if (widget->id == snd_soc_dapm_output && !widget->ext)
685                         return snd_soc_dapm_suspend_check(widget);
686
687                 /* connected jack or spk ? */
688                 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
689                     (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
690                         return snd_soc_dapm_suspend_check(widget);
691         }
692
693         list_for_each_entry(path, &widget->sinks, list_source) {
694                 DAPM_UPDATE_STAT(widget, neighbour_checks);
695
696                 if (path->weak)
697                         continue;
698
699                 if (path->walked)
700                         continue;
701
702                 if (path->sink && path->connect) {
703                         path->walked = 1;
704                         con += is_connected_output_ep(path->sink);
705                 }
706         }
707
708         return con;
709 }
710
711 /*
712  * Recursively check for a completed path to an active or physically connected
713  * input widget. Returns number of complete paths.
714  */
715 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
716 {
717         struct snd_soc_dapm_path *path;
718         int con = 0;
719
720         DAPM_UPDATE_STAT(widget, path_checks);
721
722         if (widget->id == snd_soc_dapm_supply)
723                 return 0;
724
725         /* active stream ? */
726         switch (widget->id) {
727         case snd_soc_dapm_dac:
728         case snd_soc_dapm_aif_in:
729                 if (widget->active)
730                         return snd_soc_dapm_suspend_check(widget);
731         default:
732                 break;
733         }
734
735         if (widget->connected) {
736                 /* connected pin ? */
737                 if (widget->id == snd_soc_dapm_input && !widget->ext)
738                         return snd_soc_dapm_suspend_check(widget);
739
740                 /* connected VMID/Bias for lower pops */
741                 if (widget->id == snd_soc_dapm_vmid)
742                         return snd_soc_dapm_suspend_check(widget);
743
744                 /* connected jack ? */
745                 if (widget->id == snd_soc_dapm_mic ||
746                     (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
747                         return snd_soc_dapm_suspend_check(widget);
748         }
749
750         list_for_each_entry(path, &widget->sources, list_sink) {
751                 DAPM_UPDATE_STAT(widget, neighbour_checks);
752
753                 if (path->weak)
754                         continue;
755
756                 if (path->walked)
757                         continue;
758
759                 if (path->source && path->connect) {
760                         path->walked = 1;
761                         con += is_connected_input_ep(path->source);
762                 }
763         }
764
765         return con;
766 }
767
768 /*
769  * Handler for generic register modifier widget.
770  */
771 int dapm_reg_event(struct snd_soc_dapm_widget *w,
772                    struct snd_kcontrol *kcontrol, int event)
773 {
774         unsigned int val;
775
776         if (SND_SOC_DAPM_EVENT_ON(event))
777                 val = w->on_val;
778         else
779                 val = w->off_val;
780
781         soc_widget_update_bits(w, -(w->reg + 1),
782                             w->mask << w->shift, val << w->shift);
783
784         return 0;
785 }
786 EXPORT_SYMBOL_GPL(dapm_reg_event);
787
788 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
789 {
790         if (w->power_checked)
791                 return w->new_power;
792
793         if (w->force)
794                 w->new_power = 1;
795         else
796                 w->new_power = w->power_check(w);
797
798         w->power_checked = true;
799
800         return w->new_power;
801 }
802
803 /* Generic check to see if a widget should be powered.
804  */
805 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
806 {
807         int in, out;
808
809         DAPM_UPDATE_STAT(w, power_checks);
810
811         in = is_connected_input_ep(w);
812         dapm_clear_walk(w->dapm);
813         out = is_connected_output_ep(w);
814         dapm_clear_walk(w->dapm);
815         return out != 0 && in != 0;
816 }
817
818 /* Check to see if an ADC has power */
819 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
820 {
821         int in;
822
823         DAPM_UPDATE_STAT(w, power_checks);
824
825         if (w->active) {
826                 in = is_connected_input_ep(w);
827                 dapm_clear_walk(w->dapm);
828                 return in != 0;
829         } else {
830                 return dapm_generic_check_power(w);
831         }
832 }
833
834 /* Check to see if a DAC has power */
835 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
836 {
837         int out;
838
839         DAPM_UPDATE_STAT(w, power_checks);
840
841         if (w->active) {
842                 out = is_connected_output_ep(w);
843                 dapm_clear_walk(w->dapm);
844                 return out != 0;
845         } else {
846                 return dapm_generic_check_power(w);
847         }
848 }
849
850 /* Check to see if a power supply is needed */
851 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
852 {
853         struct snd_soc_dapm_path *path;
854
855         DAPM_UPDATE_STAT(w, power_checks);
856
857         /* Check if one of our outputs is connected */
858         list_for_each_entry(path, &w->sinks, list_source) {
859                 DAPM_UPDATE_STAT(w, neighbour_checks);
860
861                 if (path->weak)
862                         continue;
863
864                 if (path->connected &&
865                     !path->connected(path->source, path->sink))
866                         continue;
867
868                 if (!path->sink)
869                         continue;
870
871                 if (dapm_widget_power_check(path->sink))
872                         return 1;
873         }
874
875         dapm_clear_walk(w->dapm);
876
877         return 0;
878 }
879
880 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
881 {
882         return 1;
883 }
884
885 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
886                             struct snd_soc_dapm_widget *b,
887                             bool power_up)
888 {
889         int *sort;
890
891         if (power_up)
892                 sort = dapm_up_seq;
893         else
894                 sort = dapm_down_seq;
895
896         if (sort[a->id] != sort[b->id])
897                 return sort[a->id] - sort[b->id];
898         if (a->subseq != b->subseq) {
899                 if (power_up)
900                         return a->subseq - b->subseq;
901                 else
902                         return b->subseq - a->subseq;
903         }
904         if (a->reg != b->reg)
905                 return a->reg - b->reg;
906         if (a->dapm != b->dapm)
907                 return (unsigned long)a->dapm - (unsigned long)b->dapm;
908
909         return 0;
910 }
911
912 /* Insert a widget in order into a DAPM power sequence. */
913 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
914                             struct list_head *list,
915                             bool power_up)
916 {
917         struct snd_soc_dapm_widget *w;
918
919         list_for_each_entry(w, list, power_list)
920                 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
921                         list_add_tail(&new_widget->power_list, &w->power_list);
922                         return;
923                 }
924
925         list_add_tail(&new_widget->power_list, list);
926 }
927
928 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
929                                  struct snd_soc_dapm_widget *w, int event)
930 {
931         struct snd_soc_card *card = dapm->card;
932         const char *ev_name;
933         int power, ret;
934
935         switch (event) {
936         case SND_SOC_DAPM_PRE_PMU:
937                 ev_name = "PRE_PMU";
938                 power = 1;
939                 break;
940         case SND_SOC_DAPM_POST_PMU:
941                 ev_name = "POST_PMU";
942                 power = 1;
943                 break;
944         case SND_SOC_DAPM_PRE_PMD:
945                 ev_name = "PRE_PMD";
946                 power = 0;
947                 break;
948         case SND_SOC_DAPM_POST_PMD:
949                 ev_name = "POST_PMD";
950                 power = 0;
951                 break;
952         default:
953                 BUG();
954                 return;
955         }
956
957         if (w->power != power)
958                 return;
959
960         if (w->event && (w->event_flags & event)) {
961                 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
962                         w->name, ev_name);
963                 trace_snd_soc_dapm_widget_event_start(w, event);
964                 ret = w->event(w, NULL, event);
965                 trace_snd_soc_dapm_widget_event_done(w, event);
966                 if (ret < 0)
967                         pr_err("%s: %s event failed: %d\n",
968                                ev_name, w->name, ret);
969         }
970 }
971
972 /* Apply the coalesced changes from a DAPM sequence */
973 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
974                                    struct list_head *pending)
975 {
976         struct snd_soc_card *card = dapm->card;
977         struct snd_soc_dapm_widget *w;
978         int reg, power;
979         unsigned int value = 0;
980         unsigned int mask = 0;
981         unsigned int cur_mask;
982
983         reg = list_first_entry(pending, struct snd_soc_dapm_widget,
984                                power_list)->reg;
985
986         list_for_each_entry(w, pending, power_list) {
987                 cur_mask = 1 << w->shift;
988                 BUG_ON(reg != w->reg);
989
990                 if (w->invert)
991                         power = !w->power;
992                 else
993                         power = w->power;
994
995                 mask |= cur_mask;
996                 if (power)
997                         value |= cur_mask;
998
999                 pop_dbg(dapm->dev, card->pop_time,
1000                         "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1001                         w->name, reg, value, mask);
1002
1003                 /* Check for events */
1004                 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1005                 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1006         }
1007
1008         if (reg >= 0) {
1009                 /* Any widget will do, they should all be updating the
1010                  * same register.
1011                  */
1012                 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1013                                      power_list);
1014
1015                 pop_dbg(dapm->dev, card->pop_time,
1016                         "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1017                         value, mask, reg, card->pop_time);
1018                 pop_wait(card->pop_time);
1019                 soc_widget_update_bits(w, reg, mask, value);
1020         }
1021
1022         list_for_each_entry(w, pending, power_list) {
1023                 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1024                 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1025         }
1026 }
1027
1028 /* Apply a DAPM power sequence.
1029  *
1030  * We walk over a pre-sorted list of widgets to apply power to.  In
1031  * order to minimise the number of writes to the device required
1032  * multiple widgets will be updated in a single write where possible.
1033  * Currently anything that requires more than a single write is not
1034  * handled.
1035  */
1036 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1037                          struct list_head *list, int event, bool power_up)
1038 {
1039         struct snd_soc_dapm_widget *w, *n;
1040         LIST_HEAD(pending);
1041         int cur_sort = -1;
1042         int cur_subseq = -1;
1043         int cur_reg = SND_SOC_NOPM;
1044         struct snd_soc_dapm_context *cur_dapm = NULL;
1045         int ret, i;
1046         int *sort;
1047
1048         if (power_up)
1049                 sort = dapm_up_seq;
1050         else
1051                 sort = dapm_down_seq;
1052
1053         list_for_each_entry_safe(w, n, list, power_list) {
1054                 ret = 0;
1055
1056                 /* Do we need to apply any queued changes? */
1057                 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1058                     w->dapm != cur_dapm || w->subseq != cur_subseq) {
1059                         if (!list_empty(&pending))
1060                                 dapm_seq_run_coalesced(cur_dapm, &pending);
1061
1062                         if (cur_dapm && cur_dapm->seq_notifier) {
1063                                 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1064                                         if (sort[i] == cur_sort)
1065                                                 cur_dapm->seq_notifier(cur_dapm,
1066                                                                        i,
1067                                                                        cur_subseq);
1068                         }
1069
1070                         INIT_LIST_HEAD(&pending);
1071                         cur_sort = -1;
1072                         cur_subseq = INT_MIN;
1073                         cur_reg = SND_SOC_NOPM;
1074                         cur_dapm = NULL;
1075                 }
1076
1077                 switch (w->id) {
1078                 case snd_soc_dapm_pre:
1079                         if (!w->event)
1080                                 list_for_each_entry_safe_continue(w, n, list,
1081                                                                   power_list);
1082
1083                         if (event == SND_SOC_DAPM_STREAM_START)
1084                                 ret = w->event(w,
1085                                                NULL, SND_SOC_DAPM_PRE_PMU);
1086                         else if (event == SND_SOC_DAPM_STREAM_STOP)
1087                                 ret = w->event(w,
1088                                                NULL, SND_SOC_DAPM_PRE_PMD);
1089                         break;
1090
1091                 case snd_soc_dapm_post:
1092                         if (!w->event)
1093                                 list_for_each_entry_safe_continue(w, n, list,
1094                                                                   power_list);
1095
1096                         if (event == SND_SOC_DAPM_STREAM_START)
1097                                 ret = w->event(w,
1098                                                NULL, SND_SOC_DAPM_POST_PMU);
1099                         else if (event == SND_SOC_DAPM_STREAM_STOP)
1100                                 ret = w->event(w,
1101                                                NULL, SND_SOC_DAPM_POST_PMD);
1102                         break;
1103
1104                 default:
1105                         /* Queue it up for application */
1106                         cur_sort = sort[w->id];
1107                         cur_subseq = w->subseq;
1108                         cur_reg = w->reg;
1109                         cur_dapm = w->dapm;
1110                         list_move(&w->power_list, &pending);
1111                         break;
1112                 }
1113
1114                 if (ret < 0)
1115                         dev_err(w->dapm->dev,
1116                                 "Failed to apply widget power: %d\n", ret);
1117         }
1118
1119         if (!list_empty(&pending))
1120                 dapm_seq_run_coalesced(cur_dapm, &pending);
1121
1122         if (cur_dapm && cur_dapm->seq_notifier) {
1123                 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1124                         if (sort[i] == cur_sort)
1125                                 cur_dapm->seq_notifier(cur_dapm,
1126                                                        i, cur_subseq);
1127         }
1128 }
1129
1130 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1131 {
1132         struct snd_soc_dapm_update *update = dapm->update;
1133         struct snd_soc_dapm_widget *w;
1134         int ret;
1135
1136         if (!update)
1137                 return;
1138
1139         w = update->widget;
1140
1141         if (w->event &&
1142             (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1143                 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1144                 if (ret != 0)
1145                         pr_err("%s DAPM pre-event failed: %d\n",
1146                                w->name, ret);
1147         }
1148
1149         ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1150                                   update->val);
1151         if (ret < 0)
1152                 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1153
1154         if (w->event &&
1155             (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1156                 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1157                 if (ret != 0)
1158                         pr_err("%s DAPM post-event failed: %d\n",
1159                                w->name, ret);
1160         }
1161 }
1162
1163 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1164  * they're changing state.
1165  */
1166 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1167 {
1168         struct snd_soc_dapm_context *d = data;
1169         int ret;
1170
1171         /* If we're off and we're not supposed to be go into STANDBY */
1172         if (d->bias_level == SND_SOC_BIAS_OFF &&
1173             d->target_bias_level != SND_SOC_BIAS_OFF) {
1174                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1175                 if (ret != 0)
1176                         dev_err(d->dev,
1177                                 "Failed to turn on bias: %d\n", ret);
1178         }
1179
1180         /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1181         if (d->bias_level != d->target_bias_level) {
1182                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1183                 if (ret != 0)
1184                         dev_err(d->dev,
1185                                 "Failed to prepare bias: %d\n", ret);
1186         }
1187 }
1188
1189 /* Async callback run prior to DAPM sequences - brings to their final
1190  * state.
1191  */
1192 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1193 {
1194         struct snd_soc_dapm_context *d = data;
1195         int ret;
1196
1197         /* If we just powered the last thing off drop to standby bias */
1198         if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1199             (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1200              d->target_bias_level == SND_SOC_BIAS_OFF)) {
1201                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1202                 if (ret != 0)
1203                         dev_err(d->dev, "Failed to apply standby bias: %d\n",
1204                                 ret);
1205         }
1206
1207         /* If we're in standby and can support bias off then do that */
1208         if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1209             d->target_bias_level == SND_SOC_BIAS_OFF) {
1210                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1211                 if (ret != 0)
1212                         dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1213         }
1214
1215         /* If we just powered up then move to active bias */
1216         if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1217             d->target_bias_level == SND_SOC_BIAS_ON) {
1218                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1219                 if (ret != 0)
1220                         dev_err(d->dev, "Failed to apply active bias: %d\n",
1221                                 ret);
1222         }
1223 }
1224
1225 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1226                                        bool power, bool connect)
1227 {
1228         /* If a connection is being made or broken then that update
1229          * will have marked the peer dirty, otherwise the widgets are
1230          * not connected and this update has no impact. */
1231         if (!connect)
1232                 return;
1233
1234         /* If the peer is already in the state we're moving to then we
1235          * won't have an impact on it. */
1236         if (power != peer->power)
1237                 dapm_mark_dirty(peer, "peer state change");
1238 }
1239
1240 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1241                                   struct list_head *up_list,
1242                                   struct list_head *down_list)
1243 {
1244         struct snd_soc_dapm_path *path;
1245
1246         if (w->power == power)
1247                 return;
1248
1249         trace_snd_soc_dapm_widget_power(w, power);
1250
1251         /* If we changed our power state perhaps our neigbours changed
1252          * also.
1253          */
1254         list_for_each_entry(path, &w->sources, list_sink) {
1255                 if (path->source) {
1256                         dapm_widget_set_peer_power(path->source, power,
1257                                                    path->connect);
1258                 }
1259         }
1260         switch (w->id) {
1261         case snd_soc_dapm_supply:
1262                 /* Supplies can't affect their outputs, only their inputs */
1263                 break;
1264         default:
1265                 list_for_each_entry(path, &w->sinks, list_source) {
1266                         if (path->sink) {
1267                                 dapm_widget_set_peer_power(path->sink, power,
1268                                                            path->connect);
1269                         }
1270                 }
1271                 break;
1272         }
1273
1274         if (power)
1275                 dapm_seq_insert(w, up_list, true);
1276         else
1277                 dapm_seq_insert(w, down_list, false);
1278
1279         w->power = power;
1280 }
1281
1282 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1283                                   struct list_head *up_list,
1284                                   struct list_head *down_list)
1285 {
1286         int power;
1287
1288         switch (w->id) {
1289         case snd_soc_dapm_pre:
1290                 dapm_seq_insert(w, down_list, false);
1291                 break;
1292         case snd_soc_dapm_post:
1293                 dapm_seq_insert(w, up_list, true);
1294                 break;
1295
1296         default:
1297                 power = dapm_widget_power_check(w);
1298
1299                 dapm_widget_set_power(w, power, up_list, down_list);
1300                 break;
1301         }
1302 }
1303
1304 /*
1305  * Scan each dapm widget for complete audio path.
1306  * A complete path is a route that has valid endpoints i.e.:-
1307  *
1308  *  o DAC to output pin.
1309  *  o Input Pin to ADC.
1310  *  o Input pin to Output pin (bypass, sidetone)
1311  *  o DAC to ADC (loopback).
1312  */
1313 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1314 {
1315         struct snd_soc_card *card = dapm->card;
1316         struct snd_soc_dapm_widget *w;
1317         struct snd_soc_dapm_context *d;
1318         LIST_HEAD(up_list);
1319         LIST_HEAD(down_list);
1320         LIST_HEAD(async_domain);
1321         enum snd_soc_bias_level bias;
1322
1323         trace_snd_soc_dapm_start(card);
1324
1325         list_for_each_entry(d, &card->dapm_list, list) {
1326                 if (d->n_widgets || d->codec == NULL) {
1327                         if (d->idle_bias_off)
1328                                 d->target_bias_level = SND_SOC_BIAS_OFF;
1329                         else
1330                                 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1331                 }
1332         }
1333
1334         memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
1335
1336         list_for_each_entry(w, &card->widgets, list) {
1337                 w->power_checked = false;
1338         }
1339
1340         /* Check which widgets we need to power and store them in
1341          * lists indicating if they should be powered up or down.  We
1342          * only check widgets that have been flagged as dirty but note
1343          * that new widgets may be added to the dirty list while we
1344          * iterate.
1345          */
1346         list_for_each_entry(w, &card->dapm_dirty, dirty) {
1347                 dapm_power_one_widget(w, &up_list, &down_list);
1348         }
1349
1350         list_for_each_entry(w, &card->widgets, list) {
1351                 list_del_init(&w->dirty);
1352
1353                 if (w->power) {
1354                         d = w->dapm;
1355
1356                         /* Supplies and micbiases only bring the
1357                          * context up to STANDBY as unless something
1358                          * else is active and passing audio they
1359                          * generally don't require full power.
1360                          */
1361                         switch (w->id) {
1362                         case snd_soc_dapm_supply:
1363                         case snd_soc_dapm_micbias:
1364                                 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1365                                         d->target_bias_level = SND_SOC_BIAS_STANDBY;
1366                                 break;
1367                         default:
1368                                 d->target_bias_level = SND_SOC_BIAS_ON;
1369                                 break;
1370                         }
1371                 }
1372
1373         }
1374
1375         /* If there are no DAPM widgets then try to figure out power from the
1376          * event type.
1377          */
1378         if (!dapm->n_widgets) {
1379                 switch (event) {
1380                 case SND_SOC_DAPM_STREAM_START:
1381                 case SND_SOC_DAPM_STREAM_RESUME:
1382                         dapm->target_bias_level = SND_SOC_BIAS_ON;
1383                         break;
1384                 case SND_SOC_DAPM_STREAM_STOP:
1385                         if (dapm->codec->active)
1386                                 dapm->target_bias_level = SND_SOC_BIAS_ON;
1387                         else
1388                                 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1389                         break;
1390                 case SND_SOC_DAPM_STREAM_SUSPEND:
1391                         dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1392                         break;
1393                 case SND_SOC_DAPM_STREAM_NOP:
1394                         dapm->target_bias_level = dapm->bias_level;
1395                         break;
1396                 default:
1397                         break;
1398                 }
1399         }
1400
1401         /* Force all contexts in the card to the same bias state if
1402          * they're not ground referenced.
1403          */
1404         bias = SND_SOC_BIAS_OFF;
1405         list_for_each_entry(d, &card->dapm_list, list)
1406                 if (d->target_bias_level > bias)
1407                         bias = d->target_bias_level;
1408         list_for_each_entry(d, &card->dapm_list, list)
1409                 if (!d->idle_bias_off)
1410                         d->target_bias_level = bias;
1411
1412         trace_snd_soc_dapm_walk_done(card);
1413
1414         /* Run all the bias changes in parallel */
1415         list_for_each_entry(d, &dapm->card->dapm_list, list)
1416                 async_schedule_domain(dapm_pre_sequence_async, d,
1417                                         &async_domain);
1418         async_synchronize_full_domain(&async_domain);
1419
1420         /* Power down widgets first; try to avoid amplifying pops. */
1421         dapm_seq_run(dapm, &down_list, event, false);
1422
1423         dapm_widget_update(dapm);
1424
1425         /* Now power up. */
1426         dapm_seq_run(dapm, &up_list, event, true);
1427
1428         /* Run all the bias changes in parallel */
1429         list_for_each_entry(d, &dapm->card->dapm_list, list)
1430                 async_schedule_domain(dapm_post_sequence_async, d,
1431                                         &async_domain);
1432         async_synchronize_full_domain(&async_domain);
1433
1434         pop_dbg(dapm->dev, card->pop_time,
1435                 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1436         pop_wait(card->pop_time);
1437
1438         trace_snd_soc_dapm_done(card);
1439
1440         return 0;
1441 }
1442
1443 #ifdef CONFIG_DEBUG_FS
1444 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1445 {
1446         file->private_data = inode->i_private;
1447         return 0;
1448 }
1449
1450 static ssize_t dapm_widget_power_read_file(struct file *file,
1451                                            char __user *user_buf,
1452                                            size_t count, loff_t *ppos)
1453 {
1454         struct snd_soc_dapm_widget *w = file->private_data;
1455         char *buf;
1456         int in, out;
1457         ssize_t ret;
1458         struct snd_soc_dapm_path *p = NULL;
1459
1460         buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1461         if (!buf)
1462                 return -ENOMEM;
1463
1464         in = is_connected_input_ep(w);
1465         dapm_clear_walk(w->dapm);
1466         out = is_connected_output_ep(w);
1467         dapm_clear_walk(w->dapm);
1468
1469         ret = snprintf(buf, PAGE_SIZE, "%s: %s  in %d out %d",
1470                        w->name, w->power ? "On" : "Off", in, out);
1471
1472         if (w->reg >= 0)
1473                 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1474                                 " - R%d(0x%x) bit %d",
1475                                 w->reg, w->reg, w->shift);
1476
1477         ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1478
1479         if (w->sname)
1480                 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1481                                 w->sname,
1482                                 w->active ? "active" : "inactive");
1483
1484         list_for_each_entry(p, &w->sources, list_sink) {
1485                 if (p->connected && !p->connected(w, p->sink))
1486                         continue;
1487
1488                 if (p->connect)
1489                         ret += snprintf(buf + ret, PAGE_SIZE - ret,
1490                                         " in  \"%s\" \"%s\"\n",
1491                                         p->name ? p->name : "static",
1492                                         p->source->name);
1493         }
1494         list_for_each_entry(p, &w->sinks, list_source) {
1495                 if (p->connected && !p->connected(w, p->sink))
1496                         continue;
1497
1498                 if (p->connect)
1499                         ret += snprintf(buf + ret, PAGE_SIZE - ret,
1500                                         " out \"%s\" \"%s\"\n",
1501                                         p->name ? p->name : "static",
1502                                         p->sink->name);
1503         }
1504
1505         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1506
1507         kfree(buf);
1508         return ret;
1509 }
1510
1511 static const struct file_operations dapm_widget_power_fops = {
1512         .open = dapm_widget_power_open_file,
1513         .read = dapm_widget_power_read_file,
1514         .llseek = default_llseek,
1515 };
1516
1517 static int dapm_bias_open_file(struct inode *inode, struct file *file)
1518 {
1519         file->private_data = inode->i_private;
1520         return 0;
1521 }
1522
1523 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1524                                    size_t count, loff_t *ppos)
1525 {
1526         struct snd_soc_dapm_context *dapm = file->private_data;
1527         char *level;
1528
1529         switch (dapm->bias_level) {
1530         case SND_SOC_BIAS_ON:
1531                 level = "On\n";
1532                 break;
1533         case SND_SOC_BIAS_PREPARE:
1534                 level = "Prepare\n";
1535                 break;
1536         case SND_SOC_BIAS_STANDBY:
1537                 level = "Standby\n";
1538                 break;
1539         case SND_SOC_BIAS_OFF:
1540                 level = "Off\n";
1541                 break;
1542         default:
1543                 BUG();
1544                 level = "Unknown\n";
1545                 break;
1546         }
1547
1548         return simple_read_from_buffer(user_buf, count, ppos, level,
1549                                        strlen(level));
1550 }
1551
1552 static const struct file_operations dapm_bias_fops = {
1553         .open = dapm_bias_open_file,
1554         .read = dapm_bias_read_file,
1555         .llseek = default_llseek,
1556 };
1557
1558 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1559         struct dentry *parent)
1560 {
1561         struct dentry *d;
1562
1563         dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1564
1565         if (!dapm->debugfs_dapm) {
1566                 printk(KERN_WARNING
1567                        "Failed to create DAPM debugfs directory\n");
1568                 return;
1569         }
1570
1571         d = debugfs_create_file("bias_level", 0444,
1572                                 dapm->debugfs_dapm, dapm,
1573                                 &dapm_bias_fops);
1574         if (!d)
1575                 dev_warn(dapm->dev,
1576                          "ASoC: Failed to create bias level debugfs file\n");
1577 }
1578
1579 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1580 {
1581         struct snd_soc_dapm_context *dapm = w->dapm;
1582         struct dentry *d;
1583
1584         if (!dapm->debugfs_dapm || !w->name)
1585                 return;
1586
1587         d = debugfs_create_file(w->name, 0444,
1588                                 dapm->debugfs_dapm, w,
1589                                 &dapm_widget_power_fops);
1590         if (!d)
1591                 dev_warn(w->dapm->dev,
1592                         "ASoC: Failed to create %s debugfs file\n",
1593                         w->name);
1594 }
1595
1596 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1597 {
1598         debugfs_remove_recursive(dapm->debugfs_dapm);
1599 }
1600
1601 #else
1602 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1603         struct dentry *parent)
1604 {
1605 }
1606
1607 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1608 {
1609 }
1610
1611 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1612 {
1613 }
1614
1615 #endif
1616
1617 /* test and update the power status of a mux widget */
1618 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1619                                  struct snd_kcontrol *kcontrol, int change,
1620                                  int mux, struct soc_enum *e)
1621 {
1622         struct snd_soc_dapm_path *path;
1623         int found = 0;
1624
1625         if (widget->id != snd_soc_dapm_mux &&
1626             widget->id != snd_soc_dapm_virt_mux &&
1627             widget->id != snd_soc_dapm_value_mux)
1628                 return -ENODEV;
1629
1630         if (!change)
1631                 return 0;
1632
1633         /* find dapm widget path assoc with kcontrol */
1634         list_for_each_entry(path, &widget->dapm->card->paths, list) {
1635                 if (path->kcontrol != kcontrol)
1636                         continue;
1637
1638                 if (!path->name || !e->texts[mux])
1639                         continue;
1640
1641                 found = 1;
1642                 /* we now need to match the string in the enum to the path */
1643                 if (!(strcmp(path->name, e->texts[mux]))) {
1644                         path->connect = 1; /* new connection */
1645                         dapm_mark_dirty(path->source, "mux connection");
1646                 } else {
1647                         if (path->connect)
1648                                 dapm_mark_dirty(path->source,
1649                                                 "mux disconnection");
1650                         path->connect = 0; /* old connection must be powered down */
1651                 }
1652         }
1653
1654         if (found) {
1655                 dapm_mark_dirty(widget, "mux change");
1656                 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1657         }
1658
1659         return 0;
1660 }
1661
1662 /* test and update the power status of a mixer or switch widget */
1663 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1664                                    struct snd_kcontrol *kcontrol, int connect)
1665 {
1666         struct snd_soc_dapm_path *path;
1667         int found = 0;
1668
1669         if (widget->id != snd_soc_dapm_mixer &&
1670             widget->id != snd_soc_dapm_mixer_named_ctl &&
1671             widget->id != snd_soc_dapm_switch)
1672                 return -ENODEV;
1673
1674         /* find dapm widget path assoc with kcontrol */
1675         list_for_each_entry(path, &widget->dapm->card->paths, list) {
1676                 if (path->kcontrol != kcontrol)
1677                         continue;
1678
1679                 /* found, now check type */
1680                 found = 1;
1681                 path->connect = connect;
1682                 dapm_mark_dirty(path->source, "mixer connection");
1683         }
1684
1685         if (found) {
1686                 dapm_mark_dirty(widget, "mixer update");
1687                 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1688         }
1689
1690         return 0;
1691 }
1692
1693 /* show dapm widget status in sys fs */
1694 static ssize_t dapm_widget_show(struct device *dev,
1695         struct device_attribute *attr, char *buf)
1696 {
1697         struct snd_soc_pcm_runtime *rtd =
1698                         container_of(dev, struct snd_soc_pcm_runtime, dev);
1699         struct snd_soc_codec *codec =rtd->codec;
1700         struct snd_soc_dapm_widget *w;
1701         int count = 0;
1702         char *state = "not set";
1703
1704         list_for_each_entry(w, &codec->card->widgets, list) {
1705                 if (w->dapm != &codec->dapm)
1706                         continue;
1707
1708                 /* only display widgets that burnm power */
1709                 switch (w->id) {
1710                 case snd_soc_dapm_hp:
1711                 case snd_soc_dapm_mic:
1712                 case snd_soc_dapm_spk:
1713                 case snd_soc_dapm_line:
1714                 case snd_soc_dapm_micbias:
1715                 case snd_soc_dapm_dac:
1716                 case snd_soc_dapm_adc:
1717                 case snd_soc_dapm_pga:
1718                 case snd_soc_dapm_out_drv:
1719                 case snd_soc_dapm_mixer:
1720                 case snd_soc_dapm_mixer_named_ctl:
1721                 case snd_soc_dapm_supply:
1722                         if (w->name)
1723                                 count += sprintf(buf + count, "%s: %s\n",
1724                                         w->name, w->power ? "On":"Off");
1725                 break;
1726                 default:
1727                 break;
1728                 }
1729         }
1730
1731         switch (codec->dapm.bias_level) {
1732         case SND_SOC_BIAS_ON:
1733                 state = "On";
1734                 break;
1735         case SND_SOC_BIAS_PREPARE:
1736                 state = "Prepare";
1737                 break;
1738         case SND_SOC_BIAS_STANDBY:
1739                 state = "Standby";
1740                 break;
1741         case SND_SOC_BIAS_OFF:
1742                 state = "Off";
1743                 break;
1744         }
1745         count += sprintf(buf + count, "PM State: %s\n", state);
1746
1747         return count;
1748 }
1749
1750 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1751
1752 int snd_soc_dapm_sys_add(struct device *dev)
1753 {
1754         return device_create_file(dev, &dev_attr_dapm_widget);
1755 }
1756
1757 static void snd_soc_dapm_sys_remove(struct device *dev)
1758 {
1759         device_remove_file(dev, &dev_attr_dapm_widget);
1760 }
1761
1762 /* free all dapm widgets and resources */
1763 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1764 {
1765         struct snd_soc_dapm_widget *w, *next_w;
1766         struct snd_soc_dapm_path *p, *next_p;
1767
1768         list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1769                 if (w->dapm != dapm)
1770                         continue;
1771                 list_del(&w->list);
1772                 /*
1773                  * remove source and sink paths associated to this widget.
1774                  * While removing the path, remove reference to it from both
1775                  * source and sink widgets so that path is removed only once.
1776                  */
1777                 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1778                         list_del(&p->list_sink);
1779                         list_del(&p->list_source);
1780                         list_del(&p->list);
1781                         kfree(p->long_name);
1782                         kfree(p);
1783                 }
1784                 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1785                         list_del(&p->list_sink);
1786                         list_del(&p->list_source);
1787                         list_del(&p->list);
1788                         kfree(p->long_name);
1789                         kfree(p);
1790                 }
1791                 kfree(w->kcontrols);
1792                 kfree(w->name);
1793                 kfree(w);
1794         }
1795 }
1796
1797 static struct snd_soc_dapm_widget *dapm_find_widget(
1798                         struct snd_soc_dapm_context *dapm, const char *pin,
1799                         bool search_other_contexts)
1800 {
1801         struct snd_soc_dapm_widget *w;
1802         struct snd_soc_dapm_widget *fallback = NULL;
1803
1804         list_for_each_entry(w, &dapm->card->widgets, list) {
1805                 if (!strcmp(w->name, pin)) {
1806                         if (w->dapm == dapm)
1807                                 return w;
1808                         else
1809                                 fallback = w;
1810                 }
1811         }
1812
1813         if (search_other_contexts)
1814                 return fallback;
1815
1816         return NULL;
1817 }
1818
1819 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1820                                 const char *pin, int status)
1821 {
1822         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
1823
1824         if (!w) {
1825                 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1826                 return -EINVAL;
1827         }
1828
1829         w->connected = status;
1830         if (status == 0)
1831                 w->force = 0;
1832         dapm_mark_dirty(w, "pin configuration");
1833
1834         return 0;
1835 }
1836
1837 /**
1838  * snd_soc_dapm_sync - scan and power dapm paths
1839  * @dapm: DAPM context
1840  *
1841  * Walks all dapm audio paths and powers widgets according to their
1842  * stream or path usage.
1843  *
1844  * Returns 0 for success.
1845  */
1846 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1847 {
1848         /*
1849          * Suppress early reports (eg, jacks syncing their state) to avoid
1850          * silly DAPM runs during card startup.
1851          */
1852         if (!dapm->card || !dapm->card->instantiated)
1853                 return 0;
1854
1855         return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1856 }
1857 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1858
1859 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1860                                   const struct snd_soc_dapm_route *route)
1861 {
1862         struct snd_soc_dapm_path *path;
1863         struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1864         struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1865         const char *sink;
1866         const char *control = route->control;
1867         const char *source;
1868         char prefixed_sink[80];
1869         char prefixed_source[80];
1870         int ret = 0;
1871
1872         if (dapm->codec && dapm->codec->name_prefix) {
1873                 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1874                          dapm->codec->name_prefix, route->sink);
1875                 sink = prefixed_sink;
1876                 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1877                          dapm->codec->name_prefix, route->source);
1878                 source = prefixed_source;
1879         } else {
1880                 sink = route->sink;
1881                 source = route->source;
1882         }
1883
1884         /*
1885          * find src and dest widgets over all widgets but favor a widget from
1886          * current DAPM context
1887          */
1888         list_for_each_entry(w, &dapm->card->widgets, list) {
1889                 if (!wsink && !(strcmp(w->name, sink))) {
1890                         wtsink = w;
1891                         if (w->dapm == dapm)
1892                                 wsink = w;
1893                         continue;
1894                 }
1895                 if (!wsource && !(strcmp(w->name, source))) {
1896                         wtsource = w;
1897                         if (w->dapm == dapm)
1898                                 wsource = w;
1899                 }
1900         }
1901         /* use widget from another DAPM context if not found from this */
1902         if (!wsink)
1903                 wsink = wtsink;
1904         if (!wsource)
1905                 wsource = wtsource;
1906
1907         if (wsource == NULL || wsink == NULL)
1908                 return -ENODEV;
1909
1910         path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1911         if (!path)
1912                 return -ENOMEM;
1913
1914         path->source = wsource;
1915         path->sink = wsink;
1916         path->connected = route->connected;
1917         INIT_LIST_HEAD(&path->list);
1918         INIT_LIST_HEAD(&path->list_source);
1919         INIT_LIST_HEAD(&path->list_sink);
1920
1921         /* check for external widgets */
1922         if (wsink->id == snd_soc_dapm_input) {
1923                 if (wsource->id == snd_soc_dapm_micbias ||
1924                         wsource->id == snd_soc_dapm_mic ||
1925                         wsource->id == snd_soc_dapm_line ||
1926                         wsource->id == snd_soc_dapm_output)
1927                         wsink->ext = 1;
1928         }
1929         if (wsource->id == snd_soc_dapm_output) {
1930                 if (wsink->id == snd_soc_dapm_spk ||
1931                         wsink->id == snd_soc_dapm_hp ||
1932                         wsink->id == snd_soc_dapm_line ||
1933                         wsink->id == snd_soc_dapm_input)
1934                         wsource->ext = 1;
1935         }
1936
1937         /* connect static paths */
1938         if (control == NULL) {
1939                 list_add(&path->list, &dapm->card->paths);
1940                 list_add(&path->list_sink, &wsink->sources);
1941                 list_add(&path->list_source, &wsource->sinks);
1942                 path->connect = 1;
1943                 return 0;
1944         }
1945
1946         /* connect dynamic paths */
1947         switch (wsink->id) {
1948         case snd_soc_dapm_adc:
1949         case snd_soc_dapm_dac:
1950         case snd_soc_dapm_pga:
1951         case snd_soc_dapm_out_drv:
1952         case snd_soc_dapm_input:
1953         case snd_soc_dapm_output:
1954         case snd_soc_dapm_micbias:
1955         case snd_soc_dapm_vmid:
1956         case snd_soc_dapm_pre:
1957         case snd_soc_dapm_post:
1958         case snd_soc_dapm_supply:
1959         case snd_soc_dapm_aif_in:
1960         case snd_soc_dapm_aif_out:
1961                 list_add(&path->list, &dapm->card->paths);
1962                 list_add(&path->list_sink, &wsink->sources);
1963                 list_add(&path->list_source, &wsource->sinks);
1964                 path->connect = 1;
1965                 return 0;
1966         case snd_soc_dapm_mux:
1967         case snd_soc_dapm_virt_mux:
1968         case snd_soc_dapm_value_mux:
1969                 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
1970                         &wsink->kcontrol_news[0]);
1971                 if (ret != 0)
1972                         goto err;
1973                 break;
1974         case snd_soc_dapm_switch:
1975         case snd_soc_dapm_mixer:
1976         case snd_soc_dapm_mixer_named_ctl:
1977                 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
1978                 if (ret != 0)
1979                         goto err;
1980                 break;
1981         case snd_soc_dapm_hp:
1982         case snd_soc_dapm_mic:
1983         case snd_soc_dapm_line:
1984         case snd_soc_dapm_spk:
1985                 list_add(&path->list, &dapm->card->paths);
1986                 list_add(&path->list_sink, &wsink->sources);
1987                 list_add(&path->list_source, &wsource->sinks);
1988                 path->connect = 0;
1989                 return 0;
1990         }
1991         return 0;
1992
1993 err:
1994         dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1995                  source, control, sink);
1996         kfree(path);
1997         return ret;
1998 }
1999
2000 /**
2001  * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2002  * @dapm: DAPM context
2003  * @route: audio routes
2004  * @num: number of routes
2005  *
2006  * Connects 2 dapm widgets together via a named audio path. The sink is
2007  * the widget receiving the audio signal, whilst the source is the sender
2008  * of the audio signal.
2009  *
2010  * Returns 0 for success else error. On error all resources can be freed
2011  * with a call to snd_soc_card_free().
2012  */
2013 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2014                             const struct snd_soc_dapm_route *route, int num)
2015 {
2016         int i, ret;
2017
2018         for (i = 0; i < num; i++) {
2019                 ret = snd_soc_dapm_add_route(dapm, route);
2020                 if (ret < 0) {
2021                         dev_err(dapm->dev, "Failed to add route %s->%s\n",
2022                                 route->source, route->sink);
2023                         return ret;
2024                 }
2025                 route++;
2026         }
2027
2028         return 0;
2029 }
2030 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2031
2032 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2033                                    const struct snd_soc_dapm_route *route)
2034 {
2035         struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2036                                                               route->source,
2037                                                               true);
2038         struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2039                                                             route->sink,
2040                                                             true);
2041         struct snd_soc_dapm_path *path;
2042         int count = 0;
2043
2044         if (!source) {
2045                 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2046                         route->source);
2047                 return -ENODEV;
2048         }
2049
2050         if (!sink) {
2051                 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2052                         route->sink);
2053                 return -ENODEV;
2054         }
2055
2056         if (route->control || route->connected)
2057                 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2058                          route->source, route->sink);
2059
2060         list_for_each_entry(path, &source->sinks, list_source) {
2061                 if (path->sink == sink) {
2062                         path->weak = 1;
2063                         count++;
2064                 }
2065         }
2066
2067         if (count == 0)
2068                 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2069                         route->source, route->sink);
2070         if (count > 1)
2071                 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2072                          count, route->source, route->sink);
2073
2074         return 0;
2075 }
2076
2077 /**
2078  * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2079  * @dapm: DAPM context
2080  * @route: audio routes
2081  * @num: number of routes
2082  *
2083  * Mark existing routes matching those specified in the passed array
2084  * as being weak, meaning that they are ignored for the purpose of
2085  * power decisions.  The main intended use case is for sidetone paths
2086  * which couple audio between other independent paths if they are both
2087  * active in order to make the combination work better at the user
2088  * level but which aren't intended to be "used".
2089  *
2090  * Note that CODEC drivers should not use this as sidetone type paths
2091  * can frequently also be used as bypass paths.
2092  */
2093 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2094                              const struct snd_soc_dapm_route *route, int num)
2095 {
2096         int i, err;
2097         int ret = 0;
2098
2099         for (i = 0; i < num; i++) {
2100                 err = snd_soc_dapm_weak_route(dapm, route);
2101                 if (err)
2102                         ret = err;
2103                 route++;
2104         }
2105
2106         return ret;
2107 }
2108 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2109
2110 /**
2111  * snd_soc_dapm_new_widgets - add new dapm widgets
2112  * @dapm: DAPM context
2113  *
2114  * Checks the codec for any new dapm widgets and creates them if found.
2115  *
2116  * Returns 0 for success.
2117  */
2118 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2119 {
2120         struct snd_soc_dapm_widget *w;
2121         unsigned int val;
2122
2123         list_for_each_entry(w, &dapm->card->widgets, list)
2124         {
2125                 if (w->new)
2126                         continue;
2127
2128                 if (w->num_kcontrols) {
2129                         w->kcontrols = kzalloc(w->num_kcontrols *
2130                                                 sizeof(struct snd_kcontrol *),
2131                                                 GFP_KERNEL);
2132                         if (!w->kcontrols)
2133                                 return -ENOMEM;
2134                 }
2135
2136                 switch(w->id) {
2137                 case snd_soc_dapm_switch:
2138                 case snd_soc_dapm_mixer:
2139                 case snd_soc_dapm_mixer_named_ctl:
2140                         dapm_new_mixer(w);
2141                         break;
2142                 case snd_soc_dapm_mux:
2143                 case snd_soc_dapm_virt_mux:
2144                 case snd_soc_dapm_value_mux:
2145                         dapm_new_mux(w);
2146                         break;
2147                 case snd_soc_dapm_pga:
2148                 case snd_soc_dapm_out_drv:
2149                         dapm_new_pga(w);
2150                         break;
2151                 default:
2152                         break;
2153                 }
2154
2155                 /* Read the initial power state from the device */
2156                 if (w->reg >= 0) {
2157                         val = soc_widget_read(w, w->reg);
2158                         val &= 1 << w->shift;
2159                         if (w->invert)
2160                                 val = !val;
2161
2162                         if (val)
2163                                 w->power = 1;
2164                 }
2165
2166                 w->new = 1;
2167
2168                 dapm_mark_dirty(w, "new widget");
2169                 dapm_debugfs_add_widget(w);
2170         }
2171
2172         dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2173         return 0;
2174 }
2175 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2176
2177 /**
2178  * snd_soc_dapm_get_volsw - dapm mixer get callback
2179  * @kcontrol: mixer control
2180  * @ucontrol: control element information
2181  *
2182  * Callback to get the value of a dapm mixer control.
2183  *
2184  * Returns 0 for success.
2185  */
2186 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2187         struct snd_ctl_elem_value *ucontrol)
2188 {
2189         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2190         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2191         struct soc_mixer_control *mc =
2192                 (struct soc_mixer_control *)kcontrol->private_value;
2193         unsigned int reg = mc->reg;
2194         unsigned int shift = mc->shift;
2195         unsigned int rshift = mc->rshift;
2196         int max = mc->max;
2197         unsigned int invert = mc->invert;
2198         unsigned int mask = (1 << fls(max)) - 1;
2199
2200         ucontrol->value.integer.value[0] =
2201                 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2202         if (shift != rshift)
2203                 ucontrol->value.integer.value[1] =
2204                         (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2205         if (invert) {
2206                 ucontrol->value.integer.value[0] =
2207                         max - ucontrol->value.integer.value[0];
2208                 if (shift != rshift)
2209                         ucontrol->value.integer.value[1] =
2210                                 max - ucontrol->value.integer.value[1];
2211         }
2212
2213         return 0;
2214 }
2215 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2216
2217 /**
2218  * snd_soc_dapm_put_volsw - dapm mixer set callback
2219  * @kcontrol: mixer control
2220  * @ucontrol: control element information
2221  *
2222  * Callback to set the value of a dapm mixer control.
2223  *
2224  * Returns 0 for success.
2225  */
2226 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2227         struct snd_ctl_elem_value *ucontrol)
2228 {
2229         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2230         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2231         struct snd_soc_codec *codec = widget->codec;
2232         struct soc_mixer_control *mc =
2233                 (struct soc_mixer_control *)kcontrol->private_value;
2234         unsigned int reg = mc->reg;
2235         unsigned int shift = mc->shift;
2236         int max = mc->max;
2237         unsigned int mask = (1 << fls(max)) - 1;
2238         unsigned int invert = mc->invert;
2239         unsigned int val;
2240         int connect, change;
2241         struct snd_soc_dapm_update update;
2242         int wi;
2243
2244         val = (ucontrol->value.integer.value[0] & mask);
2245
2246         if (invert)
2247                 val = max - val;
2248         mask = mask << shift;
2249         val = val << shift;
2250
2251         if (val)
2252                 /* new connection */
2253                 connect = invert ? 0 : 1;
2254         else
2255                 /* old connection must be powered down */
2256                 connect = invert ? 1 : 0;
2257
2258         mutex_lock(&codec->mutex);
2259
2260         change = snd_soc_test_bits(widget->codec, reg, mask, val);
2261         if (change) {
2262                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2263                         widget = wlist->widgets[wi];
2264
2265                         widget->value = val;
2266
2267                         update.kcontrol = kcontrol;
2268                         update.widget = widget;
2269                         update.reg = reg;
2270                         update.mask = mask;
2271                         update.val = val;
2272                         widget->dapm->update = &update;
2273
2274                         dapm_mixer_update_power(widget, kcontrol, connect);
2275
2276                         widget->dapm->update = NULL;
2277                 }
2278         }
2279
2280         mutex_unlock(&codec->mutex);
2281         return 0;
2282 }
2283 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2284
2285 /**
2286  * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2287  * @kcontrol: mixer control
2288  * @ucontrol: control element information
2289  *
2290  * Callback to get the value of a dapm enumerated double mixer control.
2291  *
2292  * Returns 0 for success.
2293  */
2294 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2295         struct snd_ctl_elem_value *ucontrol)
2296 {
2297         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2298         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2299         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2300         unsigned int val, bitmask;
2301
2302         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2303                 ;
2304         val = snd_soc_read(widget->codec, e->reg);
2305         ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2306         if (e->shift_l != e->shift_r)
2307                 ucontrol->value.enumerated.item[1] =
2308                         (val >> e->shift_r) & (bitmask - 1);
2309
2310         return 0;
2311 }
2312 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2313
2314 /**
2315  * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2316  * @kcontrol: mixer control
2317  * @ucontrol: control element information
2318  *
2319  * Callback to set the value of a dapm enumerated double mixer control.
2320  *
2321  * Returns 0 for success.
2322  */
2323 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2324         struct snd_ctl_elem_value *ucontrol)
2325 {
2326         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2327         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2328         struct snd_soc_codec *codec = widget->codec;
2329         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2330         unsigned int val, mux, change;
2331         unsigned int mask, bitmask;
2332         struct snd_soc_dapm_update update;
2333         int wi;
2334
2335         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2336                 ;
2337         if (ucontrol->value.enumerated.item[0] > e->max - 1)
2338                 return -EINVAL;
2339         mux = ucontrol->value.enumerated.item[0];
2340         val = mux << e->shift_l;
2341         mask = (bitmask - 1) << e->shift_l;
2342         if (e->shift_l != e->shift_r) {
2343                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2344                         return -EINVAL;
2345                 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2346                 mask |= (bitmask - 1) << e->shift_r;
2347         }
2348
2349         mutex_lock(&codec->mutex);
2350
2351         change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2352         if (change) {
2353                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2354                         widget = wlist->widgets[wi];
2355
2356                         widget->value = val;
2357
2358                         update.kcontrol = kcontrol;
2359                         update.widget = widget;
2360                         update.reg = e->reg;
2361                         update.mask = mask;
2362                         update.val = val;
2363                         widget->dapm->update = &update;
2364
2365                         dapm_mux_update_power(widget, kcontrol, change, mux, e);
2366
2367                         widget->dapm->update = NULL;
2368                 }
2369         }
2370
2371         mutex_unlock(&codec->mutex);
2372         return change;
2373 }
2374 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2375
2376 /**
2377  * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2378  * @kcontrol: mixer control
2379  * @ucontrol: control element information
2380  *
2381  * Returns 0 for success.
2382  */
2383 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2384                                struct snd_ctl_elem_value *ucontrol)
2385 {
2386         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2387         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2388
2389         ucontrol->value.enumerated.item[0] = widget->value;
2390
2391         return 0;
2392 }
2393 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2394
2395 /**
2396  * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2397  * @kcontrol: mixer control
2398  * @ucontrol: control element information
2399  *
2400  * Returns 0 for success.
2401  */
2402 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2403                                struct snd_ctl_elem_value *ucontrol)
2404 {
2405         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2406         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2407         struct snd_soc_codec *codec = widget->codec;
2408         struct soc_enum *e =
2409                 (struct soc_enum *)kcontrol->private_value;
2410         int change;
2411         int ret = 0;
2412         int wi;
2413
2414         if (ucontrol->value.enumerated.item[0] >= e->max)
2415                 return -EINVAL;
2416
2417         mutex_lock(&codec->mutex);
2418
2419         change = widget->value != ucontrol->value.enumerated.item[0];
2420         if (change) {
2421                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2422                         widget = wlist->widgets[wi];
2423
2424                         widget->value = ucontrol->value.enumerated.item[0];
2425
2426                         dapm_mux_update_power(widget, kcontrol, change,
2427                                               widget->value, e);
2428                 }
2429         }
2430
2431         mutex_unlock(&codec->mutex);
2432         return ret;
2433 }
2434 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2435
2436 /**
2437  * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2438  *                                      callback
2439  * @kcontrol: mixer control
2440  * @ucontrol: control element information
2441  *
2442  * Callback to get the value of a dapm semi enumerated double mixer control.
2443  *
2444  * Semi enumerated mixer: the enumerated items are referred as values. Can be
2445  * used for handling bitfield coded enumeration for example.
2446  *
2447  * Returns 0 for success.
2448  */
2449 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2450         struct snd_ctl_elem_value *ucontrol)
2451 {
2452         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2453         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2454         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2455         unsigned int reg_val, val, mux;
2456
2457         reg_val = snd_soc_read(widget->codec, e->reg);
2458         val = (reg_val >> e->shift_l) & e->mask;
2459         for (mux = 0; mux < e->max; mux++) {
2460                 if (val == e->values[mux])
2461                         break;
2462         }
2463         ucontrol->value.enumerated.item[0] = mux;
2464         if (e->shift_l != e->shift_r) {
2465                 val = (reg_val >> e->shift_r) & e->mask;
2466                 for (mux = 0; mux < e->max; mux++) {
2467                         if (val == e->values[mux])
2468                                 break;
2469                 }
2470                 ucontrol->value.enumerated.item[1] = mux;
2471         }
2472
2473         return 0;
2474 }
2475 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2476
2477 /**
2478  * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2479  *                                      callback
2480  * @kcontrol: mixer control
2481  * @ucontrol: control element information
2482  *
2483  * Callback to set the value of a dapm semi enumerated double mixer control.
2484  *
2485  * Semi enumerated mixer: the enumerated items are referred as values. Can be
2486  * used for handling bitfield coded enumeration for example.
2487  *
2488  * Returns 0 for success.
2489  */
2490 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2491         struct snd_ctl_elem_value *ucontrol)
2492 {
2493         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2494         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2495         struct snd_soc_codec *codec = widget->codec;
2496         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2497         unsigned int val, mux, change;
2498         unsigned int mask;
2499         struct snd_soc_dapm_update update;
2500         int wi;
2501
2502         if (ucontrol->value.enumerated.item[0] > e->max - 1)
2503                 return -EINVAL;
2504         mux = ucontrol->value.enumerated.item[0];
2505         val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2506         mask = e->mask << e->shift_l;
2507         if (e->shift_l != e->shift_r) {
2508                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2509                         return -EINVAL;
2510                 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2511                 mask |= e->mask << e->shift_r;
2512         }
2513
2514         mutex_lock(&codec->mutex);
2515
2516         change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2517         if (change) {
2518                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2519                         widget = wlist->widgets[wi];
2520
2521                         widget->value = val;
2522
2523                         update.kcontrol = kcontrol;
2524                         update.widget = widget;
2525                         update.reg = e->reg;
2526                         update.mask = mask;
2527                         update.val = val;
2528                         widget->dapm->update = &update;
2529
2530                         dapm_mux_update_power(widget, kcontrol, change, mux, e);
2531
2532                         widget->dapm->update = NULL;
2533                 }
2534         }
2535
2536         mutex_unlock(&codec->mutex);
2537         return change;
2538 }
2539 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2540
2541 /**
2542  * snd_soc_dapm_info_pin_switch - Info for a pin switch
2543  *
2544  * @kcontrol: mixer control
2545  * @uinfo: control element information
2546  *
2547  * Callback to provide information about a pin switch control.
2548  */
2549 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2550                                  struct snd_ctl_elem_info *uinfo)
2551 {
2552         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2553         uinfo->count = 1;
2554         uinfo->value.integer.min = 0;
2555         uinfo->value.integer.max = 1;
2556
2557         return 0;
2558 }
2559 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2560
2561 /**
2562  * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2563  *
2564  * @kcontrol: mixer control
2565  * @ucontrol: Value
2566  */
2567 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2568                                 struct snd_ctl_elem_value *ucontrol)
2569 {
2570         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2571         const char *pin = (const char *)kcontrol->private_value;
2572
2573         mutex_lock(&codec->mutex);
2574
2575         ucontrol->value.integer.value[0] =
2576                 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2577
2578         mutex_unlock(&codec->mutex);
2579
2580         return 0;
2581 }
2582 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2583
2584 /**
2585  * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2586  *
2587  * @kcontrol: mixer control
2588  * @ucontrol: Value
2589  */
2590 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2591                                 struct snd_ctl_elem_value *ucontrol)
2592 {
2593         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2594         const char *pin = (const char *)kcontrol->private_value;
2595
2596         mutex_lock(&codec->mutex);
2597
2598         if (ucontrol->value.integer.value[0])
2599                 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2600         else
2601                 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2602
2603         snd_soc_dapm_sync(&codec->dapm);
2604
2605         mutex_unlock(&codec->mutex);
2606
2607         return 0;
2608 }
2609 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2610
2611 /**
2612  * snd_soc_dapm_new_control - create new dapm control
2613  * @dapm: DAPM context
2614  * @widget: widget template
2615  *
2616  * Creates a new dapm control based upon the template.
2617  *
2618  * Returns 0 for success else error.
2619  */
2620 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2621         const struct snd_soc_dapm_widget *widget)
2622 {
2623         struct snd_soc_dapm_widget *w;
2624         size_t name_len;
2625
2626         if ((w = dapm_cnew_widget(widget)) == NULL)
2627                 return -ENOMEM;
2628
2629         name_len = strlen(widget->name) + 1;
2630         if (dapm->codec && dapm->codec->name_prefix)
2631                 name_len += 1 + strlen(dapm->codec->name_prefix);
2632         w->name = kmalloc(name_len, GFP_KERNEL);
2633         if (w->name == NULL) {
2634                 kfree(w);
2635                 return -ENOMEM;
2636         }
2637         if (dapm->codec && dapm->codec->name_prefix)
2638                 snprintf(w->name, name_len, "%s %s",
2639                         dapm->codec->name_prefix, widget->name);
2640         else
2641                 snprintf(w->name, name_len, "%s", widget->name);
2642
2643         switch (w->id) {
2644         case snd_soc_dapm_switch:
2645         case snd_soc_dapm_mixer:
2646         case snd_soc_dapm_mixer_named_ctl:
2647                 w->power_check = dapm_generic_check_power;
2648                 break;
2649         case snd_soc_dapm_mux:
2650         case snd_soc_dapm_virt_mux:
2651         case snd_soc_dapm_value_mux:
2652                 w->power_check = dapm_generic_check_power;
2653                 break;
2654         case snd_soc_dapm_adc:
2655         case snd_soc_dapm_aif_out:
2656                 w->power_check = dapm_adc_check_power;
2657                 break;
2658         case snd_soc_dapm_dac:
2659         case snd_soc_dapm_aif_in:
2660                 w->power_check = dapm_dac_check_power;
2661                 break;
2662         case snd_soc_dapm_pga:
2663         case snd_soc_dapm_out_drv:
2664         case snd_soc_dapm_input:
2665         case snd_soc_dapm_output:
2666         case snd_soc_dapm_micbias:
2667         case snd_soc_dapm_spk:
2668         case snd_soc_dapm_hp:
2669         case snd_soc_dapm_mic:
2670         case snd_soc_dapm_line:
2671                 w->power_check = dapm_generic_check_power;
2672                 break;
2673         case snd_soc_dapm_supply:
2674                 w->power_check = dapm_supply_check_power;
2675                 break;
2676         default:
2677                 w->power_check = dapm_always_on_check_power;
2678                 break;
2679         }
2680
2681         dapm->n_widgets++;
2682         w->dapm = dapm;
2683         w->codec = dapm->codec;
2684         w->platform = dapm->platform;
2685         INIT_LIST_HEAD(&w->sources);
2686         INIT_LIST_HEAD(&w->sinks);
2687         INIT_LIST_HEAD(&w->list);
2688         INIT_LIST_HEAD(&w->dirty);
2689         list_add(&w->list, &dapm->card->widgets);
2690
2691         /* machine layer set ups unconnected pins and insertions */
2692         w->connected = 1;
2693         return 0;
2694 }
2695 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2696
2697 /**
2698  * snd_soc_dapm_new_controls - create new dapm controls
2699  * @dapm: DAPM context
2700  * @widget: widget array
2701  * @num: number of widgets
2702  *
2703  * Creates new DAPM controls based upon the templates.
2704  *
2705  * Returns 0 for success else error.
2706  */
2707 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2708         const struct snd_soc_dapm_widget *widget,
2709         int num)
2710 {
2711         int i, ret;
2712
2713         for (i = 0; i < num; i++) {
2714                 ret = snd_soc_dapm_new_control(dapm, widget);
2715                 if (ret < 0) {
2716                         dev_err(dapm->dev,
2717                                 "ASoC: Failed to create DAPM control %s: %d\n",
2718                                 widget->name, ret);
2719                         return ret;
2720                 }
2721                 widget++;
2722         }
2723         return 0;
2724 }
2725 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2726
2727 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2728         const char *stream, int event)
2729 {
2730         struct snd_soc_dapm_widget *w;
2731
2732         list_for_each_entry(w, &dapm->card->widgets, list)
2733         {
2734                 if (!w->sname || w->dapm != dapm)
2735                         continue;
2736                 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2737                         w->name, w->sname, stream, event);
2738                 if (strstr(w->sname, stream)) {
2739                         dapm_mark_dirty(w, "stream event");
2740                         switch(event) {
2741                         case SND_SOC_DAPM_STREAM_START:
2742                                 w->active = 1;
2743                                 break;
2744                         case SND_SOC_DAPM_STREAM_STOP:
2745                                 w->active = 0;
2746                                 break;
2747                         case SND_SOC_DAPM_STREAM_SUSPEND:
2748                         case SND_SOC_DAPM_STREAM_RESUME:
2749                         case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2750                         case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2751                                 break;
2752                         }
2753                 }
2754         }
2755
2756         dapm_power_widgets(dapm, event);
2757
2758         /* do we need to notify any clients that DAPM stream is complete */
2759         if (dapm->stream_event)
2760                 dapm->stream_event(dapm, event);
2761 }
2762
2763 /**
2764  * snd_soc_dapm_stream_event - send a stream event to the dapm core
2765  * @rtd: PCM runtime data
2766  * @stream: stream name
2767  * @event: stream event
2768  *
2769  * Sends a stream event to the dapm core. The core then makes any
2770  * necessary widget power changes.
2771  *
2772  * Returns 0 for success else error.
2773  */
2774 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2775         const char *stream, int event)
2776 {
2777         struct snd_soc_codec *codec = rtd->codec;
2778
2779         if (stream == NULL)
2780                 return 0;
2781
2782         mutex_lock(&codec->mutex);
2783         soc_dapm_stream_event(&codec->dapm, stream, event);
2784         mutex_unlock(&codec->mutex);
2785         return 0;
2786 }
2787
2788 /**
2789  * snd_soc_dapm_enable_pin - enable pin.
2790  * @dapm: DAPM context
2791  * @pin: pin name
2792  *
2793  * Enables input/output pin and its parents or children widgets iff there is
2794  * a valid audio route and active audio stream.
2795  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2796  * do any widget power switching.
2797  */
2798 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2799 {
2800         return snd_soc_dapm_set_pin(dapm, pin, 1);
2801 }
2802 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2803
2804 /**
2805  * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2806  * @dapm: DAPM context
2807  * @pin: pin name
2808  *
2809  * Enables input/output pin regardless of any other state.  This is
2810  * intended for use with microphone bias supplies used in microphone
2811  * jack detection.
2812  *
2813  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2814  * do any widget power switching.
2815  */
2816 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2817                                   const char *pin)
2818 {
2819         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2820
2821         if (!w) {
2822                 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2823                 return -EINVAL;
2824         }
2825
2826         dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2827         w->connected = 1;
2828         w->force = 1;
2829         dapm_mark_dirty(w, "force enable");
2830
2831         return 0;
2832 }
2833 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2834
2835 /**
2836  * snd_soc_dapm_disable_pin - disable pin.
2837  * @dapm: DAPM context
2838  * @pin: pin name
2839  *
2840  * Disables input/output pin and its parents or children widgets.
2841  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2842  * do any widget power switching.
2843  */
2844 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2845                              const char *pin)
2846 {
2847         return snd_soc_dapm_set_pin(dapm, pin, 0);
2848 }
2849 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2850
2851 /**
2852  * snd_soc_dapm_nc_pin - permanently disable pin.
2853  * @dapm: DAPM context
2854  * @pin: pin name
2855  *
2856  * Marks the specified pin as being not connected, disabling it along
2857  * any parent or child widgets.  At present this is identical to
2858  * snd_soc_dapm_disable_pin() but in future it will be extended to do
2859  * additional things such as disabling controls which only affect
2860  * paths through the pin.
2861  *
2862  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2863  * do any widget power switching.
2864  */
2865 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2866 {
2867         return snd_soc_dapm_set_pin(dapm, pin, 0);
2868 }
2869 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2870
2871 /**
2872  * snd_soc_dapm_get_pin_status - get audio pin status
2873  * @dapm: DAPM context
2874  * @pin: audio signal pin endpoint (or start point)
2875  *
2876  * Get audio pin status - connected or disconnected.
2877  *
2878  * Returns 1 for connected otherwise 0.
2879  */
2880 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2881                                 const char *pin)
2882 {
2883         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2884
2885         if (w)
2886                 return w->connected;
2887
2888         return 0;
2889 }
2890 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2891
2892 /**
2893  * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2894  * @dapm: DAPM context
2895  * @pin: audio signal pin endpoint (or start point)
2896  *
2897  * Mark the given endpoint or pin as ignoring suspend.  When the
2898  * system is disabled a path between two endpoints flagged as ignoring
2899  * suspend will not be disabled.  The path must already be enabled via
2900  * normal means at suspend time, it will not be turned on if it was not
2901  * already enabled.
2902  */
2903 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2904                                 const char *pin)
2905 {
2906         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
2907
2908         if (!w) {
2909                 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2910                 return -EINVAL;
2911         }
2912
2913         w->ignore_suspend = 1;
2914
2915         return 0;
2916 }
2917 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2918
2919 /**
2920  * snd_soc_dapm_free - free dapm resources
2921  * @dapm: DAPM context
2922  *
2923  * Free all dapm widgets and resources.
2924  */
2925 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2926 {
2927         snd_soc_dapm_sys_remove(dapm->dev);
2928         dapm_debugfs_cleanup(dapm);
2929         dapm_free_widgets(dapm);
2930         list_del(&dapm->list);
2931 }
2932 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2933
2934 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
2935 {
2936         struct snd_soc_dapm_widget *w;
2937         LIST_HEAD(down_list);
2938         int powerdown = 0;
2939
2940         list_for_each_entry(w, &dapm->card->widgets, list) {
2941                 if (w->dapm != dapm)
2942                         continue;
2943                 if (w->power) {
2944                         dapm_seq_insert(w, &down_list, false);
2945                         w->power = 0;
2946                         powerdown = 1;
2947                 }
2948         }
2949
2950         /* If there were no widgets to power down we're already in
2951          * standby.
2952          */
2953         if (powerdown) {
2954                 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
2955                 dapm_seq_run(dapm, &down_list, 0, false);
2956                 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
2957         }
2958 }
2959
2960 /*
2961  * snd_soc_dapm_shutdown - callback for system shutdown
2962  */
2963 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2964 {
2965         struct snd_soc_codec *codec;
2966
2967         list_for_each_entry(codec, &card->codec_dev_list, list) {
2968                 soc_dapm_shutdown_codec(&codec->dapm);
2969                 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
2970         }
2971 }
2972
2973 /* Module information */
2974 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2975 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2976 MODULE_LICENSE("GPL");