]> Pileus Git - ~andy/linux/blob - sound/soc/soc-dapm.c
ASoC: Don't mark the outputs of supplies as dirty on state changes
[~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         int power = 0;
855
856         DAPM_UPDATE_STAT(w, power_checks);
857
858         /* Check if one of our outputs is connected */
859         list_for_each_entry(path, &w->sinks, list_source) {
860                 DAPM_UPDATE_STAT(w, neighbour_checks);
861
862                 if (path->weak)
863                         continue;
864
865                 if (path->connected &&
866                     !path->connected(path->source, path->sink))
867                         continue;
868
869                 if (!path->sink)
870                         continue;
871
872                 if (dapm_widget_power_check(path->sink)) {
873                         power = 1;
874                         break;
875                 }
876         }
877
878         dapm_clear_walk(w->dapm);
879
880         return power;
881 }
882
883 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
884 {
885         return 1;
886 }
887
888 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
889                             struct snd_soc_dapm_widget *b,
890                             bool power_up)
891 {
892         int *sort;
893
894         if (power_up)
895                 sort = dapm_up_seq;
896         else
897                 sort = dapm_down_seq;
898
899         if (sort[a->id] != sort[b->id])
900                 return sort[a->id] - sort[b->id];
901         if (a->subseq != b->subseq) {
902                 if (power_up)
903                         return a->subseq - b->subseq;
904                 else
905                         return b->subseq - a->subseq;
906         }
907         if (a->reg != b->reg)
908                 return a->reg - b->reg;
909         if (a->dapm != b->dapm)
910                 return (unsigned long)a->dapm - (unsigned long)b->dapm;
911
912         return 0;
913 }
914
915 /* Insert a widget in order into a DAPM power sequence. */
916 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
917                             struct list_head *list,
918                             bool power_up)
919 {
920         struct snd_soc_dapm_widget *w;
921
922         list_for_each_entry(w, list, power_list)
923                 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
924                         list_add_tail(&new_widget->power_list, &w->power_list);
925                         return;
926                 }
927
928         list_add_tail(&new_widget->power_list, list);
929 }
930
931 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
932                                  struct snd_soc_dapm_widget *w, int event)
933 {
934         struct snd_soc_card *card = dapm->card;
935         const char *ev_name;
936         int power, ret;
937
938         switch (event) {
939         case SND_SOC_DAPM_PRE_PMU:
940                 ev_name = "PRE_PMU";
941                 power = 1;
942                 break;
943         case SND_SOC_DAPM_POST_PMU:
944                 ev_name = "POST_PMU";
945                 power = 1;
946                 break;
947         case SND_SOC_DAPM_PRE_PMD:
948                 ev_name = "PRE_PMD";
949                 power = 0;
950                 break;
951         case SND_SOC_DAPM_POST_PMD:
952                 ev_name = "POST_PMD";
953                 power = 0;
954                 break;
955         default:
956                 BUG();
957                 return;
958         }
959
960         if (w->power != power)
961                 return;
962
963         if (w->event && (w->event_flags & event)) {
964                 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
965                         w->name, ev_name);
966                 trace_snd_soc_dapm_widget_event_start(w, event);
967                 ret = w->event(w, NULL, event);
968                 trace_snd_soc_dapm_widget_event_done(w, event);
969                 if (ret < 0)
970                         pr_err("%s: %s event failed: %d\n",
971                                ev_name, w->name, ret);
972         }
973 }
974
975 /* Apply the coalesced changes from a DAPM sequence */
976 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
977                                    struct list_head *pending)
978 {
979         struct snd_soc_card *card = dapm->card;
980         struct snd_soc_dapm_widget *w;
981         int reg, power;
982         unsigned int value = 0;
983         unsigned int mask = 0;
984         unsigned int cur_mask;
985
986         reg = list_first_entry(pending, struct snd_soc_dapm_widget,
987                                power_list)->reg;
988
989         list_for_each_entry(w, pending, power_list) {
990                 cur_mask = 1 << w->shift;
991                 BUG_ON(reg != w->reg);
992
993                 if (w->invert)
994                         power = !w->power;
995                 else
996                         power = w->power;
997
998                 mask |= cur_mask;
999                 if (power)
1000                         value |= cur_mask;
1001
1002                 pop_dbg(dapm->dev, card->pop_time,
1003                         "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1004                         w->name, reg, value, mask);
1005
1006                 /* Check for events */
1007                 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1008                 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1009         }
1010
1011         if (reg >= 0) {
1012                 /* Any widget will do, they should all be updating the
1013                  * same register.
1014                  */
1015                 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1016                                      power_list);
1017
1018                 pop_dbg(dapm->dev, card->pop_time,
1019                         "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1020                         value, mask, reg, card->pop_time);
1021                 pop_wait(card->pop_time);
1022                 soc_widget_update_bits(w, reg, mask, value);
1023         }
1024
1025         list_for_each_entry(w, pending, power_list) {
1026                 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1027                 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1028         }
1029 }
1030
1031 /* Apply a DAPM power sequence.
1032  *
1033  * We walk over a pre-sorted list of widgets to apply power to.  In
1034  * order to minimise the number of writes to the device required
1035  * multiple widgets will be updated in a single write where possible.
1036  * Currently anything that requires more than a single write is not
1037  * handled.
1038  */
1039 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1040                          struct list_head *list, int event, bool power_up)
1041 {
1042         struct snd_soc_dapm_widget *w, *n;
1043         LIST_HEAD(pending);
1044         int cur_sort = -1;
1045         int cur_subseq = -1;
1046         int cur_reg = SND_SOC_NOPM;
1047         struct snd_soc_dapm_context *cur_dapm = NULL;
1048         int ret, i;
1049         int *sort;
1050
1051         if (power_up)
1052                 sort = dapm_up_seq;
1053         else
1054                 sort = dapm_down_seq;
1055
1056         list_for_each_entry_safe(w, n, list, power_list) {
1057                 ret = 0;
1058
1059                 /* Do we need to apply any queued changes? */
1060                 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1061                     w->dapm != cur_dapm || w->subseq != cur_subseq) {
1062                         if (!list_empty(&pending))
1063                                 dapm_seq_run_coalesced(cur_dapm, &pending);
1064
1065                         if (cur_dapm && cur_dapm->seq_notifier) {
1066                                 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1067                                         if (sort[i] == cur_sort)
1068                                                 cur_dapm->seq_notifier(cur_dapm,
1069                                                                        i,
1070                                                                        cur_subseq);
1071                         }
1072
1073                         INIT_LIST_HEAD(&pending);
1074                         cur_sort = -1;
1075                         cur_subseq = INT_MIN;
1076                         cur_reg = SND_SOC_NOPM;
1077                         cur_dapm = NULL;
1078                 }
1079
1080                 switch (w->id) {
1081                 case snd_soc_dapm_pre:
1082                         if (!w->event)
1083                                 list_for_each_entry_safe_continue(w, n, list,
1084                                                                   power_list);
1085
1086                         if (event == SND_SOC_DAPM_STREAM_START)
1087                                 ret = w->event(w,
1088                                                NULL, SND_SOC_DAPM_PRE_PMU);
1089                         else if (event == SND_SOC_DAPM_STREAM_STOP)
1090                                 ret = w->event(w,
1091                                                NULL, SND_SOC_DAPM_PRE_PMD);
1092                         break;
1093
1094                 case snd_soc_dapm_post:
1095                         if (!w->event)
1096                                 list_for_each_entry_safe_continue(w, n, list,
1097                                                                   power_list);
1098
1099                         if (event == SND_SOC_DAPM_STREAM_START)
1100                                 ret = w->event(w,
1101                                                NULL, SND_SOC_DAPM_POST_PMU);
1102                         else if (event == SND_SOC_DAPM_STREAM_STOP)
1103                                 ret = w->event(w,
1104                                                NULL, SND_SOC_DAPM_POST_PMD);
1105                         break;
1106
1107                 default:
1108                         /* Queue it up for application */
1109                         cur_sort = sort[w->id];
1110                         cur_subseq = w->subseq;
1111                         cur_reg = w->reg;
1112                         cur_dapm = w->dapm;
1113                         list_move(&w->power_list, &pending);
1114                         break;
1115                 }
1116
1117                 if (ret < 0)
1118                         dev_err(w->dapm->dev,
1119                                 "Failed to apply widget power: %d\n", ret);
1120         }
1121
1122         if (!list_empty(&pending))
1123                 dapm_seq_run_coalesced(cur_dapm, &pending);
1124
1125         if (cur_dapm && cur_dapm->seq_notifier) {
1126                 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1127                         if (sort[i] == cur_sort)
1128                                 cur_dapm->seq_notifier(cur_dapm,
1129                                                        i, cur_subseq);
1130         }
1131 }
1132
1133 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1134 {
1135         struct snd_soc_dapm_update *update = dapm->update;
1136         struct snd_soc_dapm_widget *w;
1137         int ret;
1138
1139         if (!update)
1140                 return;
1141
1142         w = update->widget;
1143
1144         if (w->event &&
1145             (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1146                 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1147                 if (ret != 0)
1148                         pr_err("%s DAPM pre-event failed: %d\n",
1149                                w->name, ret);
1150         }
1151
1152         ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1153                                   update->val);
1154         if (ret < 0)
1155                 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1156
1157         if (w->event &&
1158             (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1159                 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1160                 if (ret != 0)
1161                         pr_err("%s DAPM post-event failed: %d\n",
1162                                w->name, ret);
1163         }
1164 }
1165
1166 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1167  * they're changing state.
1168  */
1169 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1170 {
1171         struct snd_soc_dapm_context *d = data;
1172         int ret;
1173
1174         /* If we're off and we're not supposed to be go into STANDBY */
1175         if (d->bias_level == SND_SOC_BIAS_OFF &&
1176             d->target_bias_level != SND_SOC_BIAS_OFF) {
1177                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1178                 if (ret != 0)
1179                         dev_err(d->dev,
1180                                 "Failed to turn on bias: %d\n", ret);
1181         }
1182
1183         /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1184         if (d->bias_level != d->target_bias_level) {
1185                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1186                 if (ret != 0)
1187                         dev_err(d->dev,
1188                                 "Failed to prepare bias: %d\n", ret);
1189         }
1190 }
1191
1192 /* Async callback run prior to DAPM sequences - brings to their final
1193  * state.
1194  */
1195 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1196 {
1197         struct snd_soc_dapm_context *d = data;
1198         int ret;
1199
1200         /* If we just powered the last thing off drop to standby bias */
1201         if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1202             (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1203              d->target_bias_level == SND_SOC_BIAS_OFF)) {
1204                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1205                 if (ret != 0)
1206                         dev_err(d->dev, "Failed to apply standby bias: %d\n",
1207                                 ret);
1208         }
1209
1210         /* If we're in standby and can support bias off then do that */
1211         if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1212             d->target_bias_level == SND_SOC_BIAS_OFF) {
1213                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1214                 if (ret != 0)
1215                         dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1216         }
1217
1218         /* If we just powered up then move to active bias */
1219         if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1220             d->target_bias_level == SND_SOC_BIAS_ON) {
1221                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1222                 if (ret != 0)
1223                         dev_err(d->dev, "Failed to apply active bias: %d\n",
1224                                 ret);
1225         }
1226 }
1227
1228 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1229                                        bool power, bool connect)
1230 {
1231         /* If a connection is being made or broken then that update
1232          * will have marked the peer dirty, otherwise the widgets are
1233          * not connected and this update has no impact. */
1234         if (!connect)
1235                 return;
1236
1237         /* If the peer is already in the state we're moving to then we
1238          * won't have an impact on it. */
1239         if (power != peer->power)
1240                 dapm_mark_dirty(peer, "peer state change");
1241 }
1242
1243 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1244                                   struct list_head *up_list,
1245                                   struct list_head *down_list)
1246 {
1247         struct snd_soc_dapm_path *path;
1248
1249         if (w->power == power)
1250                 return;
1251
1252         trace_snd_soc_dapm_widget_power(w, power);
1253
1254         /* If we changed our power state perhaps our neigbours changed
1255          * also.
1256          */
1257         list_for_each_entry(path, &w->sources, list_sink) {
1258                 if (path->source) {
1259                         dapm_widget_set_peer_power(path->source, power,
1260                                                    path->connect);
1261                 }
1262         }
1263         switch (w->id) {
1264         case snd_soc_dapm_supply:
1265                 /* Supplies can't affect their outputs, only their inputs */
1266                 break;
1267         default:
1268                 list_for_each_entry(path, &w->sinks, list_source) {
1269                         if (path->sink) {
1270                                 dapm_widget_set_peer_power(path->sink, power,
1271                                                            path->connect);
1272                         }
1273                 }
1274                 break;
1275         }
1276
1277         if (power)
1278                 dapm_seq_insert(w, up_list, true);
1279         else
1280                 dapm_seq_insert(w, down_list, false);
1281
1282         w->power = power;
1283 }
1284
1285 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1286                                   struct list_head *up_list,
1287                                   struct list_head *down_list)
1288 {
1289         int power;
1290
1291         switch (w->id) {
1292         case snd_soc_dapm_pre:
1293                 dapm_seq_insert(w, down_list, false);
1294                 break;
1295         case snd_soc_dapm_post:
1296                 dapm_seq_insert(w, up_list, true);
1297                 break;
1298
1299         default:
1300                 power = dapm_widget_power_check(w);
1301
1302                 dapm_widget_set_power(w, power, up_list, down_list);
1303                 break;
1304         }
1305 }
1306
1307 /*
1308  * Scan each dapm widget for complete audio path.
1309  * A complete path is a route that has valid endpoints i.e.:-
1310  *
1311  *  o DAC to output pin.
1312  *  o Input Pin to ADC.
1313  *  o Input pin to Output pin (bypass, sidetone)
1314  *  o DAC to ADC (loopback).
1315  */
1316 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1317 {
1318         struct snd_soc_card *card = dapm->card;
1319         struct snd_soc_dapm_widget *w;
1320         struct snd_soc_dapm_context *d;
1321         LIST_HEAD(up_list);
1322         LIST_HEAD(down_list);
1323         LIST_HEAD(async_domain);
1324         enum snd_soc_bias_level bias;
1325
1326         trace_snd_soc_dapm_start(card);
1327
1328         list_for_each_entry(d, &card->dapm_list, list) {
1329                 if (d->n_widgets || d->codec == NULL) {
1330                         if (d->idle_bias_off)
1331                                 d->target_bias_level = SND_SOC_BIAS_OFF;
1332                         else
1333                                 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1334                 }
1335         }
1336
1337         memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
1338
1339         list_for_each_entry(w, &card->widgets, list) {
1340                 w->power_checked = false;
1341         }
1342
1343         /* Check which widgets we need to power and store them in
1344          * lists indicating if they should be powered up or down.  We
1345          * only check widgets that have been flagged as dirty but note
1346          * that new widgets may be added to the dirty list while we
1347          * iterate.
1348          */
1349         list_for_each_entry(w, &card->dapm_dirty, dirty) {
1350                 dapm_power_one_widget(w, &up_list, &down_list);
1351         }
1352
1353         list_for_each_entry(w, &card->widgets, list) {
1354                 list_del_init(&w->dirty);
1355
1356                 if (w->power) {
1357                         d = w->dapm;
1358
1359                         /* Supplies and micbiases only bring the
1360                          * context up to STANDBY as unless something
1361                          * else is active and passing audio they
1362                          * generally don't require full power.
1363                          */
1364                         switch (w->id) {
1365                         case snd_soc_dapm_supply:
1366                         case snd_soc_dapm_micbias:
1367                                 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1368                                         d->target_bias_level = SND_SOC_BIAS_STANDBY;
1369                                 break;
1370                         default:
1371                                 d->target_bias_level = SND_SOC_BIAS_ON;
1372                                 break;
1373                         }
1374                 }
1375
1376         }
1377
1378         /* If there are no DAPM widgets then try to figure out power from the
1379          * event type.
1380          */
1381         if (!dapm->n_widgets) {
1382                 switch (event) {
1383                 case SND_SOC_DAPM_STREAM_START:
1384                 case SND_SOC_DAPM_STREAM_RESUME:
1385                         dapm->target_bias_level = SND_SOC_BIAS_ON;
1386                         break;
1387                 case SND_SOC_DAPM_STREAM_STOP:
1388                         if (dapm->codec->active)
1389                                 dapm->target_bias_level = SND_SOC_BIAS_ON;
1390                         else
1391                                 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1392                         break;
1393                 case SND_SOC_DAPM_STREAM_SUSPEND:
1394                         dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1395                         break;
1396                 case SND_SOC_DAPM_STREAM_NOP:
1397                         dapm->target_bias_level = dapm->bias_level;
1398                         break;
1399                 default:
1400                         break;
1401                 }
1402         }
1403
1404         /* Force all contexts in the card to the same bias state if
1405          * they're not ground referenced.
1406          */
1407         bias = SND_SOC_BIAS_OFF;
1408         list_for_each_entry(d, &card->dapm_list, list)
1409                 if (d->target_bias_level > bias)
1410                         bias = d->target_bias_level;
1411         list_for_each_entry(d, &card->dapm_list, list)
1412                 if (!d->idle_bias_off)
1413                         d->target_bias_level = bias;
1414
1415         trace_snd_soc_dapm_walk_done(card);
1416
1417         /* Run all the bias changes in parallel */
1418         list_for_each_entry(d, &dapm->card->dapm_list, list)
1419                 async_schedule_domain(dapm_pre_sequence_async, d,
1420                                         &async_domain);
1421         async_synchronize_full_domain(&async_domain);
1422
1423         /* Power down widgets first; try to avoid amplifying pops. */
1424         dapm_seq_run(dapm, &down_list, event, false);
1425
1426         dapm_widget_update(dapm);
1427
1428         /* Now power up. */
1429         dapm_seq_run(dapm, &up_list, event, true);
1430
1431         /* Run all the bias changes in parallel */
1432         list_for_each_entry(d, &dapm->card->dapm_list, list)
1433                 async_schedule_domain(dapm_post_sequence_async, d,
1434                                         &async_domain);
1435         async_synchronize_full_domain(&async_domain);
1436
1437         pop_dbg(dapm->dev, card->pop_time,
1438                 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1439         pop_wait(card->pop_time);
1440
1441         trace_snd_soc_dapm_done(card);
1442
1443         return 0;
1444 }
1445
1446 #ifdef CONFIG_DEBUG_FS
1447 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1448 {
1449         file->private_data = inode->i_private;
1450         return 0;
1451 }
1452
1453 static ssize_t dapm_widget_power_read_file(struct file *file,
1454                                            char __user *user_buf,
1455                                            size_t count, loff_t *ppos)
1456 {
1457         struct snd_soc_dapm_widget *w = file->private_data;
1458         char *buf;
1459         int in, out;
1460         ssize_t ret;
1461         struct snd_soc_dapm_path *p = NULL;
1462
1463         buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1464         if (!buf)
1465                 return -ENOMEM;
1466
1467         in = is_connected_input_ep(w);
1468         dapm_clear_walk(w->dapm);
1469         out = is_connected_output_ep(w);
1470         dapm_clear_walk(w->dapm);
1471
1472         ret = snprintf(buf, PAGE_SIZE, "%s: %s  in %d out %d",
1473                        w->name, w->power ? "On" : "Off", in, out);
1474
1475         if (w->reg >= 0)
1476                 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1477                                 " - R%d(0x%x) bit %d",
1478                                 w->reg, w->reg, w->shift);
1479
1480         ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1481
1482         if (w->sname)
1483                 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1484                                 w->sname,
1485                                 w->active ? "active" : "inactive");
1486
1487         list_for_each_entry(p, &w->sources, list_sink) {
1488                 if (p->connected && !p->connected(w, p->sink))
1489                         continue;
1490
1491                 if (p->connect)
1492                         ret += snprintf(buf + ret, PAGE_SIZE - ret,
1493                                         " in  \"%s\" \"%s\"\n",
1494                                         p->name ? p->name : "static",
1495                                         p->source->name);
1496         }
1497         list_for_each_entry(p, &w->sinks, list_source) {
1498                 if (p->connected && !p->connected(w, p->sink))
1499                         continue;
1500
1501                 if (p->connect)
1502                         ret += snprintf(buf + ret, PAGE_SIZE - ret,
1503                                         " out \"%s\" \"%s\"\n",
1504                                         p->name ? p->name : "static",
1505                                         p->sink->name);
1506         }
1507
1508         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1509
1510         kfree(buf);
1511         return ret;
1512 }
1513
1514 static const struct file_operations dapm_widget_power_fops = {
1515         .open = dapm_widget_power_open_file,
1516         .read = dapm_widget_power_read_file,
1517         .llseek = default_llseek,
1518 };
1519
1520 static int dapm_bias_open_file(struct inode *inode, struct file *file)
1521 {
1522         file->private_data = inode->i_private;
1523         return 0;
1524 }
1525
1526 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1527                                    size_t count, loff_t *ppos)
1528 {
1529         struct snd_soc_dapm_context *dapm = file->private_data;
1530         char *level;
1531
1532         switch (dapm->bias_level) {
1533         case SND_SOC_BIAS_ON:
1534                 level = "On\n";
1535                 break;
1536         case SND_SOC_BIAS_PREPARE:
1537                 level = "Prepare\n";
1538                 break;
1539         case SND_SOC_BIAS_STANDBY:
1540                 level = "Standby\n";
1541                 break;
1542         case SND_SOC_BIAS_OFF:
1543                 level = "Off\n";
1544                 break;
1545         default:
1546                 BUG();
1547                 level = "Unknown\n";
1548                 break;
1549         }
1550
1551         return simple_read_from_buffer(user_buf, count, ppos, level,
1552                                        strlen(level));
1553 }
1554
1555 static const struct file_operations dapm_bias_fops = {
1556         .open = dapm_bias_open_file,
1557         .read = dapm_bias_read_file,
1558         .llseek = default_llseek,
1559 };
1560
1561 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1562         struct dentry *parent)
1563 {
1564         struct dentry *d;
1565
1566         dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1567
1568         if (!dapm->debugfs_dapm) {
1569                 printk(KERN_WARNING
1570                        "Failed to create DAPM debugfs directory\n");
1571                 return;
1572         }
1573
1574         d = debugfs_create_file("bias_level", 0444,
1575                                 dapm->debugfs_dapm, dapm,
1576                                 &dapm_bias_fops);
1577         if (!d)
1578                 dev_warn(dapm->dev,
1579                          "ASoC: Failed to create bias level debugfs file\n");
1580 }
1581
1582 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1583 {
1584         struct snd_soc_dapm_context *dapm = w->dapm;
1585         struct dentry *d;
1586
1587         if (!dapm->debugfs_dapm || !w->name)
1588                 return;
1589
1590         d = debugfs_create_file(w->name, 0444,
1591                                 dapm->debugfs_dapm, w,
1592                                 &dapm_widget_power_fops);
1593         if (!d)
1594                 dev_warn(w->dapm->dev,
1595                         "ASoC: Failed to create %s debugfs file\n",
1596                         w->name);
1597 }
1598
1599 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1600 {
1601         debugfs_remove_recursive(dapm->debugfs_dapm);
1602 }
1603
1604 #else
1605 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1606         struct dentry *parent)
1607 {
1608 }
1609
1610 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1611 {
1612 }
1613
1614 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1615 {
1616 }
1617
1618 #endif
1619
1620 /* test and update the power status of a mux widget */
1621 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1622                                  struct snd_kcontrol *kcontrol, int change,
1623                                  int mux, struct soc_enum *e)
1624 {
1625         struct snd_soc_dapm_path *path;
1626         int found = 0;
1627
1628         if (widget->id != snd_soc_dapm_mux &&
1629             widget->id != snd_soc_dapm_virt_mux &&
1630             widget->id != snd_soc_dapm_value_mux)
1631                 return -ENODEV;
1632
1633         if (!change)
1634                 return 0;
1635
1636         /* find dapm widget path assoc with kcontrol */
1637         list_for_each_entry(path, &widget->dapm->card->paths, list) {
1638                 if (path->kcontrol != kcontrol)
1639                         continue;
1640
1641                 if (!path->name || !e->texts[mux])
1642                         continue;
1643
1644                 found = 1;
1645                 /* we now need to match the string in the enum to the path */
1646                 if (!(strcmp(path->name, e->texts[mux]))) {
1647                         path->connect = 1; /* new connection */
1648                         dapm_mark_dirty(path->source, "mux connection");
1649                 } else {
1650                         if (path->connect)
1651                                 dapm_mark_dirty(path->source,
1652                                                 "mux disconnection");
1653                         path->connect = 0; /* old connection must be powered down */
1654                 }
1655         }
1656
1657         if (found) {
1658                 dapm_mark_dirty(widget, "mux change");
1659                 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1660         }
1661
1662         return 0;
1663 }
1664
1665 /* test and update the power status of a mixer or switch widget */
1666 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1667                                    struct snd_kcontrol *kcontrol, int connect)
1668 {
1669         struct snd_soc_dapm_path *path;
1670         int found = 0;
1671
1672         if (widget->id != snd_soc_dapm_mixer &&
1673             widget->id != snd_soc_dapm_mixer_named_ctl &&
1674             widget->id != snd_soc_dapm_switch)
1675                 return -ENODEV;
1676
1677         /* find dapm widget path assoc with kcontrol */
1678         list_for_each_entry(path, &widget->dapm->card->paths, list) {
1679                 if (path->kcontrol != kcontrol)
1680                         continue;
1681
1682                 /* found, now check type */
1683                 found = 1;
1684                 path->connect = connect;
1685                 dapm_mark_dirty(path->source, "mixer connection");
1686         }
1687
1688         if (found) {
1689                 dapm_mark_dirty(widget, "mixer update");
1690                 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1691         }
1692
1693         return 0;
1694 }
1695
1696 /* show dapm widget status in sys fs */
1697 static ssize_t dapm_widget_show(struct device *dev,
1698         struct device_attribute *attr, char *buf)
1699 {
1700         struct snd_soc_pcm_runtime *rtd =
1701                         container_of(dev, struct snd_soc_pcm_runtime, dev);
1702         struct snd_soc_codec *codec =rtd->codec;
1703         struct snd_soc_dapm_widget *w;
1704         int count = 0;
1705         char *state = "not set";
1706
1707         list_for_each_entry(w, &codec->card->widgets, list) {
1708                 if (w->dapm != &codec->dapm)
1709                         continue;
1710
1711                 /* only display widgets that burnm power */
1712                 switch (w->id) {
1713                 case snd_soc_dapm_hp:
1714                 case snd_soc_dapm_mic:
1715                 case snd_soc_dapm_spk:
1716                 case snd_soc_dapm_line:
1717                 case snd_soc_dapm_micbias:
1718                 case snd_soc_dapm_dac:
1719                 case snd_soc_dapm_adc:
1720                 case snd_soc_dapm_pga:
1721                 case snd_soc_dapm_out_drv:
1722                 case snd_soc_dapm_mixer:
1723                 case snd_soc_dapm_mixer_named_ctl:
1724                 case snd_soc_dapm_supply:
1725                         if (w->name)
1726                                 count += sprintf(buf + count, "%s: %s\n",
1727                                         w->name, w->power ? "On":"Off");
1728                 break;
1729                 default:
1730                 break;
1731                 }
1732         }
1733
1734         switch (codec->dapm.bias_level) {
1735         case SND_SOC_BIAS_ON:
1736                 state = "On";
1737                 break;
1738         case SND_SOC_BIAS_PREPARE:
1739                 state = "Prepare";
1740                 break;
1741         case SND_SOC_BIAS_STANDBY:
1742                 state = "Standby";
1743                 break;
1744         case SND_SOC_BIAS_OFF:
1745                 state = "Off";
1746                 break;
1747         }
1748         count += sprintf(buf + count, "PM State: %s\n", state);
1749
1750         return count;
1751 }
1752
1753 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1754
1755 int snd_soc_dapm_sys_add(struct device *dev)
1756 {
1757         return device_create_file(dev, &dev_attr_dapm_widget);
1758 }
1759
1760 static void snd_soc_dapm_sys_remove(struct device *dev)
1761 {
1762         device_remove_file(dev, &dev_attr_dapm_widget);
1763 }
1764
1765 /* free all dapm widgets and resources */
1766 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1767 {
1768         struct snd_soc_dapm_widget *w, *next_w;
1769         struct snd_soc_dapm_path *p, *next_p;
1770
1771         list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1772                 if (w->dapm != dapm)
1773                         continue;
1774                 list_del(&w->list);
1775                 /*
1776                  * remove source and sink paths associated to this widget.
1777                  * While removing the path, remove reference to it from both
1778                  * source and sink widgets so that path is removed only once.
1779                  */
1780                 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1781                         list_del(&p->list_sink);
1782                         list_del(&p->list_source);
1783                         list_del(&p->list);
1784                         kfree(p->long_name);
1785                         kfree(p);
1786                 }
1787                 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1788                         list_del(&p->list_sink);
1789                         list_del(&p->list_source);
1790                         list_del(&p->list);
1791                         kfree(p->long_name);
1792                         kfree(p);
1793                 }
1794                 kfree(w->kcontrols);
1795                 kfree(w->name);
1796                 kfree(w);
1797         }
1798 }
1799
1800 static struct snd_soc_dapm_widget *dapm_find_widget(
1801                         struct snd_soc_dapm_context *dapm, const char *pin,
1802                         bool search_other_contexts)
1803 {
1804         struct snd_soc_dapm_widget *w;
1805         struct snd_soc_dapm_widget *fallback = NULL;
1806
1807         list_for_each_entry(w, &dapm->card->widgets, list) {
1808                 if (!strcmp(w->name, pin)) {
1809                         if (w->dapm == dapm)
1810                                 return w;
1811                         else
1812                                 fallback = w;
1813                 }
1814         }
1815
1816         if (search_other_contexts)
1817                 return fallback;
1818
1819         return NULL;
1820 }
1821
1822 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1823                                 const char *pin, int status)
1824 {
1825         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
1826
1827         if (!w) {
1828                 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1829                 return -EINVAL;
1830         }
1831
1832         w->connected = status;
1833         if (status == 0)
1834                 w->force = 0;
1835         dapm_mark_dirty(w, "pin configuration");
1836
1837         return 0;
1838 }
1839
1840 /**
1841  * snd_soc_dapm_sync - scan and power dapm paths
1842  * @dapm: DAPM context
1843  *
1844  * Walks all dapm audio paths and powers widgets according to their
1845  * stream or path usage.
1846  *
1847  * Returns 0 for success.
1848  */
1849 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1850 {
1851         return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1852 }
1853 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1854
1855 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1856                                   const struct snd_soc_dapm_route *route)
1857 {
1858         struct snd_soc_dapm_path *path;
1859         struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1860         struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1861         const char *sink;
1862         const char *control = route->control;
1863         const char *source;
1864         char prefixed_sink[80];
1865         char prefixed_source[80];
1866         int ret = 0;
1867
1868         if (dapm->codec && dapm->codec->name_prefix) {
1869                 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1870                          dapm->codec->name_prefix, route->sink);
1871                 sink = prefixed_sink;
1872                 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1873                          dapm->codec->name_prefix, route->source);
1874                 source = prefixed_source;
1875         } else {
1876                 sink = route->sink;
1877                 source = route->source;
1878         }
1879
1880         /*
1881          * find src and dest widgets over all widgets but favor a widget from
1882          * current DAPM context
1883          */
1884         list_for_each_entry(w, &dapm->card->widgets, list) {
1885                 if (!wsink && !(strcmp(w->name, sink))) {
1886                         wtsink = w;
1887                         if (w->dapm == dapm)
1888                                 wsink = w;
1889                         continue;
1890                 }
1891                 if (!wsource && !(strcmp(w->name, source))) {
1892                         wtsource = w;
1893                         if (w->dapm == dapm)
1894                                 wsource = w;
1895                 }
1896         }
1897         /* use widget from another DAPM context if not found from this */
1898         if (!wsink)
1899                 wsink = wtsink;
1900         if (!wsource)
1901                 wsource = wtsource;
1902
1903         if (wsource == NULL || wsink == NULL)
1904                 return -ENODEV;
1905
1906         path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1907         if (!path)
1908                 return -ENOMEM;
1909
1910         path->source = wsource;
1911         path->sink = wsink;
1912         path->connected = route->connected;
1913         INIT_LIST_HEAD(&path->list);
1914         INIT_LIST_HEAD(&path->list_source);
1915         INIT_LIST_HEAD(&path->list_sink);
1916
1917         /* check for external widgets */
1918         if (wsink->id == snd_soc_dapm_input) {
1919                 if (wsource->id == snd_soc_dapm_micbias ||
1920                         wsource->id == snd_soc_dapm_mic ||
1921                         wsource->id == snd_soc_dapm_line ||
1922                         wsource->id == snd_soc_dapm_output)
1923                         wsink->ext = 1;
1924         }
1925         if (wsource->id == snd_soc_dapm_output) {
1926                 if (wsink->id == snd_soc_dapm_spk ||
1927                         wsink->id == snd_soc_dapm_hp ||
1928                         wsink->id == snd_soc_dapm_line ||
1929                         wsink->id == snd_soc_dapm_input)
1930                         wsource->ext = 1;
1931         }
1932
1933         /* connect static paths */
1934         if (control == NULL) {
1935                 list_add(&path->list, &dapm->card->paths);
1936                 list_add(&path->list_sink, &wsink->sources);
1937                 list_add(&path->list_source, &wsource->sinks);
1938                 path->connect = 1;
1939                 return 0;
1940         }
1941
1942         /* connect dynamic paths */
1943         switch (wsink->id) {
1944         case snd_soc_dapm_adc:
1945         case snd_soc_dapm_dac:
1946         case snd_soc_dapm_pga:
1947         case snd_soc_dapm_out_drv:
1948         case snd_soc_dapm_input:
1949         case snd_soc_dapm_output:
1950         case snd_soc_dapm_micbias:
1951         case snd_soc_dapm_vmid:
1952         case snd_soc_dapm_pre:
1953         case snd_soc_dapm_post:
1954         case snd_soc_dapm_supply:
1955         case snd_soc_dapm_aif_in:
1956         case snd_soc_dapm_aif_out:
1957                 list_add(&path->list, &dapm->card->paths);
1958                 list_add(&path->list_sink, &wsink->sources);
1959                 list_add(&path->list_source, &wsource->sinks);
1960                 path->connect = 1;
1961                 return 0;
1962         case snd_soc_dapm_mux:
1963         case snd_soc_dapm_virt_mux:
1964         case snd_soc_dapm_value_mux:
1965                 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
1966                         &wsink->kcontrol_news[0]);
1967                 if (ret != 0)
1968                         goto err;
1969                 break;
1970         case snd_soc_dapm_switch:
1971         case snd_soc_dapm_mixer:
1972         case snd_soc_dapm_mixer_named_ctl:
1973                 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
1974                 if (ret != 0)
1975                         goto err;
1976                 break;
1977         case snd_soc_dapm_hp:
1978         case snd_soc_dapm_mic:
1979         case snd_soc_dapm_line:
1980         case snd_soc_dapm_spk:
1981                 list_add(&path->list, &dapm->card->paths);
1982                 list_add(&path->list_sink, &wsink->sources);
1983                 list_add(&path->list_source, &wsource->sinks);
1984                 path->connect = 0;
1985                 return 0;
1986         }
1987         return 0;
1988
1989 err:
1990         dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1991                  source, control, sink);
1992         kfree(path);
1993         return ret;
1994 }
1995
1996 /**
1997  * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1998  * @dapm: DAPM context
1999  * @route: audio routes
2000  * @num: number of routes
2001  *
2002  * Connects 2 dapm widgets together via a named audio path. The sink is
2003  * the widget receiving the audio signal, whilst the source is the sender
2004  * of the audio signal.
2005  *
2006  * Returns 0 for success else error. On error all resources can be freed
2007  * with a call to snd_soc_card_free().
2008  */
2009 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2010                             const struct snd_soc_dapm_route *route, int num)
2011 {
2012         int i, ret;
2013
2014         for (i = 0; i < num; i++) {
2015                 ret = snd_soc_dapm_add_route(dapm, route);
2016                 if (ret < 0) {
2017                         dev_err(dapm->dev, "Failed to add route %s->%s\n",
2018                                 route->source, route->sink);
2019                         return ret;
2020                 }
2021                 route++;
2022         }
2023
2024         return 0;
2025 }
2026 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2027
2028 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2029                                    const struct snd_soc_dapm_route *route)
2030 {
2031         struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2032                                                               route->source,
2033                                                               true);
2034         struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2035                                                             route->sink,
2036                                                             true);
2037         struct snd_soc_dapm_path *path;
2038         int count = 0;
2039
2040         if (!source) {
2041                 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2042                         route->source);
2043                 return -ENODEV;
2044         }
2045
2046         if (!sink) {
2047                 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2048                         route->sink);
2049                 return -ENODEV;
2050         }
2051
2052         if (route->control || route->connected)
2053                 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2054                          route->source, route->sink);
2055
2056         list_for_each_entry(path, &source->sinks, list_source) {
2057                 if (path->sink == sink) {
2058                         path->weak = 1;
2059                         count++;
2060                 }
2061         }
2062
2063         if (count == 0)
2064                 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2065                         route->source, route->sink);
2066         if (count > 1)
2067                 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2068                          count, route->source, route->sink);
2069
2070         return 0;
2071 }
2072
2073 /**
2074  * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2075  * @dapm: DAPM context
2076  * @route: audio routes
2077  * @num: number of routes
2078  *
2079  * Mark existing routes matching those specified in the passed array
2080  * as being weak, meaning that they are ignored for the purpose of
2081  * power decisions.  The main intended use case is for sidetone paths
2082  * which couple audio between other independent paths if they are both
2083  * active in order to make the combination work better at the user
2084  * level but which aren't intended to be "used".
2085  *
2086  * Note that CODEC drivers should not use this as sidetone type paths
2087  * can frequently also be used as bypass paths.
2088  */
2089 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2090                              const struct snd_soc_dapm_route *route, int num)
2091 {
2092         int i, err;
2093         int ret = 0;
2094
2095         for (i = 0; i < num; i++) {
2096                 err = snd_soc_dapm_weak_route(dapm, route);
2097                 if (err)
2098                         ret = err;
2099                 route++;
2100         }
2101
2102         return ret;
2103 }
2104 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2105
2106 /**
2107  * snd_soc_dapm_new_widgets - add new dapm widgets
2108  * @dapm: DAPM context
2109  *
2110  * Checks the codec for any new dapm widgets and creates them if found.
2111  *
2112  * Returns 0 for success.
2113  */
2114 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2115 {
2116         struct snd_soc_dapm_widget *w;
2117         unsigned int val;
2118
2119         list_for_each_entry(w, &dapm->card->widgets, list)
2120         {
2121                 if (w->new)
2122                         continue;
2123
2124                 if (w->num_kcontrols) {
2125                         w->kcontrols = kzalloc(w->num_kcontrols *
2126                                                 sizeof(struct snd_kcontrol *),
2127                                                 GFP_KERNEL);
2128                         if (!w->kcontrols)
2129                                 return -ENOMEM;
2130                 }
2131
2132                 switch(w->id) {
2133                 case snd_soc_dapm_switch:
2134                 case snd_soc_dapm_mixer:
2135                 case snd_soc_dapm_mixer_named_ctl:
2136                         w->power_check = dapm_generic_check_power;
2137                         dapm_new_mixer(w);
2138                         break;
2139                 case snd_soc_dapm_mux:
2140                 case snd_soc_dapm_virt_mux:
2141                 case snd_soc_dapm_value_mux:
2142                         w->power_check = dapm_generic_check_power;
2143                         dapm_new_mux(w);
2144                         break;
2145                 case snd_soc_dapm_adc:
2146                 case snd_soc_dapm_aif_out:
2147                         w->power_check = dapm_adc_check_power;
2148                         break;
2149                 case snd_soc_dapm_dac:
2150                 case snd_soc_dapm_aif_in:
2151                         w->power_check = dapm_dac_check_power;
2152                         break;
2153                 case snd_soc_dapm_pga:
2154                 case snd_soc_dapm_out_drv:
2155                         w->power_check = dapm_generic_check_power;
2156                         dapm_new_pga(w);
2157                         break;
2158                 case snd_soc_dapm_input:
2159                 case snd_soc_dapm_output:
2160                 case snd_soc_dapm_micbias:
2161                 case snd_soc_dapm_spk:
2162                 case snd_soc_dapm_hp:
2163                 case snd_soc_dapm_mic:
2164                 case snd_soc_dapm_line:
2165                         w->power_check = dapm_generic_check_power;
2166                         break;
2167                 case snd_soc_dapm_supply:
2168                         w->power_check = dapm_supply_check_power;
2169                 case snd_soc_dapm_vmid:
2170                 case snd_soc_dapm_pre:
2171                 case snd_soc_dapm_post:
2172                         break;
2173                 }
2174
2175                 if (!w->power_check)
2176                         w->power_check = dapm_always_on_check_power;
2177
2178                 /* Read the initial power state from the device */
2179                 if (w->reg >= 0) {
2180                         val = soc_widget_read(w, w->reg);
2181                         val &= 1 << w->shift;
2182                         if (w->invert)
2183                                 val = !val;
2184
2185                         if (val)
2186                                 w->power = 1;
2187                 }
2188
2189                 w->new = 1;
2190
2191                 list_add(&w->dirty, &(w->dapm->card->dapm_dirty));
2192                 dapm_debugfs_add_widget(w);
2193         }
2194
2195         dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2196         return 0;
2197 }
2198 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2199
2200 /**
2201  * snd_soc_dapm_get_volsw - dapm mixer get callback
2202  * @kcontrol: mixer control
2203  * @ucontrol: control element information
2204  *
2205  * Callback to get the value of a dapm mixer control.
2206  *
2207  * Returns 0 for success.
2208  */
2209 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2210         struct snd_ctl_elem_value *ucontrol)
2211 {
2212         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2213         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2214         struct soc_mixer_control *mc =
2215                 (struct soc_mixer_control *)kcontrol->private_value;
2216         unsigned int reg = mc->reg;
2217         unsigned int shift = mc->shift;
2218         unsigned int rshift = mc->rshift;
2219         int max = mc->max;
2220         unsigned int invert = mc->invert;
2221         unsigned int mask = (1 << fls(max)) - 1;
2222
2223         ucontrol->value.integer.value[0] =
2224                 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2225         if (shift != rshift)
2226                 ucontrol->value.integer.value[1] =
2227                         (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2228         if (invert) {
2229                 ucontrol->value.integer.value[0] =
2230                         max - ucontrol->value.integer.value[0];
2231                 if (shift != rshift)
2232                         ucontrol->value.integer.value[1] =
2233                                 max - ucontrol->value.integer.value[1];
2234         }
2235
2236         return 0;
2237 }
2238 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2239
2240 /**
2241  * snd_soc_dapm_put_volsw - dapm mixer set callback
2242  * @kcontrol: mixer control
2243  * @ucontrol: control element information
2244  *
2245  * Callback to set the value of a dapm mixer control.
2246  *
2247  * Returns 0 for success.
2248  */
2249 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2250         struct snd_ctl_elem_value *ucontrol)
2251 {
2252         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2253         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2254         struct snd_soc_codec *codec = widget->codec;
2255         struct soc_mixer_control *mc =
2256                 (struct soc_mixer_control *)kcontrol->private_value;
2257         unsigned int reg = mc->reg;
2258         unsigned int shift = mc->shift;
2259         int max = mc->max;
2260         unsigned int mask = (1 << fls(max)) - 1;
2261         unsigned int invert = mc->invert;
2262         unsigned int val;
2263         int connect, change;
2264         struct snd_soc_dapm_update update;
2265         int wi;
2266
2267         val = (ucontrol->value.integer.value[0] & mask);
2268
2269         if (invert)
2270                 val = max - val;
2271         mask = mask << shift;
2272         val = val << shift;
2273
2274         if (val)
2275                 /* new connection */
2276                 connect = invert ? 0 : 1;
2277         else
2278                 /* old connection must be powered down */
2279                 connect = invert ? 1 : 0;
2280
2281         mutex_lock(&codec->mutex);
2282
2283         change = snd_soc_test_bits(widget->codec, reg, mask, val);
2284         if (change) {
2285                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2286                         widget = wlist->widgets[wi];
2287
2288                         widget->value = val;
2289
2290                         update.kcontrol = kcontrol;
2291                         update.widget = widget;
2292                         update.reg = reg;
2293                         update.mask = mask;
2294                         update.val = val;
2295                         widget->dapm->update = &update;
2296
2297                         dapm_mixer_update_power(widget, kcontrol, connect);
2298
2299                         widget->dapm->update = NULL;
2300                 }
2301         }
2302
2303         mutex_unlock(&codec->mutex);
2304         return 0;
2305 }
2306 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2307
2308 /**
2309  * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2310  * @kcontrol: mixer control
2311  * @ucontrol: control element information
2312  *
2313  * Callback to get the value of a dapm enumerated double mixer control.
2314  *
2315  * Returns 0 for success.
2316  */
2317 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2318         struct snd_ctl_elem_value *ucontrol)
2319 {
2320         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2321         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2322         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2323         unsigned int val, bitmask;
2324
2325         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2326                 ;
2327         val = snd_soc_read(widget->codec, e->reg);
2328         ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2329         if (e->shift_l != e->shift_r)
2330                 ucontrol->value.enumerated.item[1] =
2331                         (val >> e->shift_r) & (bitmask - 1);
2332
2333         return 0;
2334 }
2335 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2336
2337 /**
2338  * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2339  * @kcontrol: mixer control
2340  * @ucontrol: control element information
2341  *
2342  * Callback to set the value of a dapm enumerated double mixer control.
2343  *
2344  * Returns 0 for success.
2345  */
2346 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2347         struct snd_ctl_elem_value *ucontrol)
2348 {
2349         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2350         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2351         struct snd_soc_codec *codec = widget->codec;
2352         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2353         unsigned int val, mux, change;
2354         unsigned int mask, bitmask;
2355         struct snd_soc_dapm_update update;
2356         int wi;
2357
2358         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2359                 ;
2360         if (ucontrol->value.enumerated.item[0] > e->max - 1)
2361                 return -EINVAL;
2362         mux = ucontrol->value.enumerated.item[0];
2363         val = mux << e->shift_l;
2364         mask = (bitmask - 1) << e->shift_l;
2365         if (e->shift_l != e->shift_r) {
2366                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2367                         return -EINVAL;
2368                 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2369                 mask |= (bitmask - 1) << e->shift_r;
2370         }
2371
2372         mutex_lock(&codec->mutex);
2373
2374         change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2375         if (change) {
2376                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2377                         widget = wlist->widgets[wi];
2378
2379                         widget->value = val;
2380
2381                         update.kcontrol = kcontrol;
2382                         update.widget = widget;
2383                         update.reg = e->reg;
2384                         update.mask = mask;
2385                         update.val = val;
2386                         widget->dapm->update = &update;
2387
2388                         dapm_mux_update_power(widget, kcontrol, change, mux, e);
2389
2390                         widget->dapm->update = NULL;
2391                 }
2392         }
2393
2394         mutex_unlock(&codec->mutex);
2395         return change;
2396 }
2397 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2398
2399 /**
2400  * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2401  * @kcontrol: mixer control
2402  * @ucontrol: control element information
2403  *
2404  * Returns 0 for success.
2405  */
2406 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2407                                struct snd_ctl_elem_value *ucontrol)
2408 {
2409         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2410         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2411
2412         ucontrol->value.enumerated.item[0] = widget->value;
2413
2414         return 0;
2415 }
2416 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2417
2418 /**
2419  * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2420  * @kcontrol: mixer control
2421  * @ucontrol: control element information
2422  *
2423  * Returns 0 for success.
2424  */
2425 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2426                                struct snd_ctl_elem_value *ucontrol)
2427 {
2428         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2429         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2430         struct snd_soc_codec *codec = widget->codec;
2431         struct soc_enum *e =
2432                 (struct soc_enum *)kcontrol->private_value;
2433         int change;
2434         int ret = 0;
2435         int wi;
2436
2437         if (ucontrol->value.enumerated.item[0] >= e->max)
2438                 return -EINVAL;
2439
2440         mutex_lock(&codec->mutex);
2441
2442         change = widget->value != ucontrol->value.enumerated.item[0];
2443         if (change) {
2444                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2445                         widget = wlist->widgets[wi];
2446
2447                         widget->value = ucontrol->value.enumerated.item[0];
2448
2449                         dapm_mux_update_power(widget, kcontrol, change,
2450                                               widget->value, e);
2451                 }
2452         }
2453
2454         mutex_unlock(&codec->mutex);
2455         return ret;
2456 }
2457 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2458
2459 /**
2460  * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2461  *                                      callback
2462  * @kcontrol: mixer control
2463  * @ucontrol: control element information
2464  *
2465  * Callback to get the value of a dapm semi enumerated double mixer control.
2466  *
2467  * Semi enumerated mixer: the enumerated items are referred as values. Can be
2468  * used for handling bitfield coded enumeration for example.
2469  *
2470  * Returns 0 for success.
2471  */
2472 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2473         struct snd_ctl_elem_value *ucontrol)
2474 {
2475         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2476         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2477         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2478         unsigned int reg_val, val, mux;
2479
2480         reg_val = snd_soc_read(widget->codec, e->reg);
2481         val = (reg_val >> e->shift_l) & e->mask;
2482         for (mux = 0; mux < e->max; mux++) {
2483                 if (val == e->values[mux])
2484                         break;
2485         }
2486         ucontrol->value.enumerated.item[0] = mux;
2487         if (e->shift_l != e->shift_r) {
2488                 val = (reg_val >> e->shift_r) & e->mask;
2489                 for (mux = 0; mux < e->max; mux++) {
2490                         if (val == e->values[mux])
2491                                 break;
2492                 }
2493                 ucontrol->value.enumerated.item[1] = mux;
2494         }
2495
2496         return 0;
2497 }
2498 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2499
2500 /**
2501  * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2502  *                                      callback
2503  * @kcontrol: mixer control
2504  * @ucontrol: control element information
2505  *
2506  * Callback to set the value of a dapm semi enumerated double mixer control.
2507  *
2508  * Semi enumerated mixer: the enumerated items are referred as values. Can be
2509  * used for handling bitfield coded enumeration for example.
2510  *
2511  * Returns 0 for success.
2512  */
2513 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2514         struct snd_ctl_elem_value *ucontrol)
2515 {
2516         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2517         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2518         struct snd_soc_codec *codec = widget->codec;
2519         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2520         unsigned int val, mux, change;
2521         unsigned int mask;
2522         struct snd_soc_dapm_update update;
2523         int wi;
2524
2525         if (ucontrol->value.enumerated.item[0] > e->max - 1)
2526                 return -EINVAL;
2527         mux = ucontrol->value.enumerated.item[0];
2528         val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2529         mask = e->mask << e->shift_l;
2530         if (e->shift_l != e->shift_r) {
2531                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2532                         return -EINVAL;
2533                 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2534                 mask |= e->mask << e->shift_r;
2535         }
2536
2537         mutex_lock(&codec->mutex);
2538
2539         change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2540         if (change) {
2541                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2542                         widget = wlist->widgets[wi];
2543
2544                         widget->value = val;
2545
2546                         update.kcontrol = kcontrol;
2547                         update.widget = widget;
2548                         update.reg = e->reg;
2549                         update.mask = mask;
2550                         update.val = val;
2551                         widget->dapm->update = &update;
2552
2553                         dapm_mux_update_power(widget, kcontrol, change, mux, e);
2554
2555                         widget->dapm->update = NULL;
2556                 }
2557         }
2558
2559         mutex_unlock(&codec->mutex);
2560         return change;
2561 }
2562 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2563
2564 /**
2565  * snd_soc_dapm_info_pin_switch - Info for a pin switch
2566  *
2567  * @kcontrol: mixer control
2568  * @uinfo: control element information
2569  *
2570  * Callback to provide information about a pin switch control.
2571  */
2572 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2573                                  struct snd_ctl_elem_info *uinfo)
2574 {
2575         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2576         uinfo->count = 1;
2577         uinfo->value.integer.min = 0;
2578         uinfo->value.integer.max = 1;
2579
2580         return 0;
2581 }
2582 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2583
2584 /**
2585  * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2586  *
2587  * @kcontrol: mixer control
2588  * @ucontrol: Value
2589  */
2590 int snd_soc_dapm_get_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         ucontrol->value.integer.value[0] =
2599                 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2600
2601         mutex_unlock(&codec->mutex);
2602
2603         return 0;
2604 }
2605 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2606
2607 /**
2608  * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2609  *
2610  * @kcontrol: mixer control
2611  * @ucontrol: Value
2612  */
2613 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2614                                 struct snd_ctl_elem_value *ucontrol)
2615 {
2616         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2617         const char *pin = (const char *)kcontrol->private_value;
2618
2619         mutex_lock(&codec->mutex);
2620
2621         if (ucontrol->value.integer.value[0])
2622                 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2623         else
2624                 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2625
2626         snd_soc_dapm_sync(&codec->dapm);
2627
2628         mutex_unlock(&codec->mutex);
2629
2630         return 0;
2631 }
2632 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2633
2634 /**
2635  * snd_soc_dapm_new_control - create new dapm control
2636  * @dapm: DAPM context
2637  * @widget: widget template
2638  *
2639  * Creates a new dapm control based upon the template.
2640  *
2641  * Returns 0 for success else error.
2642  */
2643 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2644         const struct snd_soc_dapm_widget *widget)
2645 {
2646         struct snd_soc_dapm_widget *w;
2647         size_t name_len;
2648
2649         if ((w = dapm_cnew_widget(widget)) == NULL)
2650                 return -ENOMEM;
2651
2652         name_len = strlen(widget->name) + 1;
2653         if (dapm->codec && dapm->codec->name_prefix)
2654                 name_len += 1 + strlen(dapm->codec->name_prefix);
2655         w->name = kmalloc(name_len, GFP_KERNEL);
2656         if (w->name == NULL) {
2657                 kfree(w);
2658                 return -ENOMEM;
2659         }
2660         if (dapm->codec && dapm->codec->name_prefix)
2661                 snprintf(w->name, name_len, "%s %s",
2662                         dapm->codec->name_prefix, widget->name);
2663         else
2664                 snprintf(w->name, name_len, "%s", widget->name);
2665
2666         dapm->n_widgets++;
2667         w->dapm = dapm;
2668         w->codec = dapm->codec;
2669         w->platform = dapm->platform;
2670         INIT_LIST_HEAD(&w->sources);
2671         INIT_LIST_HEAD(&w->sinks);
2672         INIT_LIST_HEAD(&w->list);
2673         INIT_LIST_HEAD(&w->dirty);
2674         list_add(&w->list, &dapm->card->widgets);
2675
2676         /* machine layer set ups unconnected pins and insertions */
2677         w->connected = 1;
2678         return 0;
2679 }
2680 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2681
2682 /**
2683  * snd_soc_dapm_new_controls - create new dapm controls
2684  * @dapm: DAPM context
2685  * @widget: widget array
2686  * @num: number of widgets
2687  *
2688  * Creates new DAPM controls based upon the templates.
2689  *
2690  * Returns 0 for success else error.
2691  */
2692 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2693         const struct snd_soc_dapm_widget *widget,
2694         int num)
2695 {
2696         int i, ret;
2697
2698         for (i = 0; i < num; i++) {
2699                 ret = snd_soc_dapm_new_control(dapm, widget);
2700                 if (ret < 0) {
2701                         dev_err(dapm->dev,
2702                                 "ASoC: Failed to create DAPM control %s: %d\n",
2703                                 widget->name, ret);
2704                         return ret;
2705                 }
2706                 widget++;
2707         }
2708         return 0;
2709 }
2710 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2711
2712 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2713         const char *stream, int event)
2714 {
2715         struct snd_soc_dapm_widget *w;
2716
2717         list_for_each_entry(w, &dapm->card->widgets, list)
2718         {
2719                 if (!w->sname || w->dapm != dapm)
2720                         continue;
2721                 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2722                         w->name, w->sname, stream, event);
2723                 if (strstr(w->sname, stream)) {
2724                         dapm_mark_dirty(w, "stream event");
2725                         switch(event) {
2726                         case SND_SOC_DAPM_STREAM_START:
2727                                 w->active = 1;
2728                                 break;
2729                         case SND_SOC_DAPM_STREAM_STOP:
2730                                 w->active = 0;
2731                                 break;
2732                         case SND_SOC_DAPM_STREAM_SUSPEND:
2733                         case SND_SOC_DAPM_STREAM_RESUME:
2734                         case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2735                         case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2736                                 break;
2737                         }
2738                 }
2739         }
2740
2741         dapm_power_widgets(dapm, event);
2742
2743         /* do we need to notify any clients that DAPM stream is complete */
2744         if (dapm->stream_event)
2745                 dapm->stream_event(dapm, event);
2746 }
2747
2748 /**
2749  * snd_soc_dapm_stream_event - send a stream event to the dapm core
2750  * @rtd: PCM runtime data
2751  * @stream: stream name
2752  * @event: stream event
2753  *
2754  * Sends a stream event to the dapm core. The core then makes any
2755  * necessary widget power changes.
2756  *
2757  * Returns 0 for success else error.
2758  */
2759 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2760         const char *stream, int event)
2761 {
2762         struct snd_soc_codec *codec = rtd->codec;
2763
2764         if (stream == NULL)
2765                 return 0;
2766
2767         mutex_lock(&codec->mutex);
2768         soc_dapm_stream_event(&codec->dapm, stream, event);
2769         mutex_unlock(&codec->mutex);
2770         return 0;
2771 }
2772
2773 /**
2774  * snd_soc_dapm_enable_pin - enable pin.
2775  * @dapm: DAPM context
2776  * @pin: pin name
2777  *
2778  * Enables input/output pin and its parents or children widgets iff there is
2779  * a valid audio route and active audio stream.
2780  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2781  * do any widget power switching.
2782  */
2783 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2784 {
2785         return snd_soc_dapm_set_pin(dapm, pin, 1);
2786 }
2787 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2788
2789 /**
2790  * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2791  * @dapm: DAPM context
2792  * @pin: pin name
2793  *
2794  * Enables input/output pin regardless of any other state.  This is
2795  * intended for use with microphone bias supplies used in microphone
2796  * jack detection.
2797  *
2798  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2799  * do any widget power switching.
2800  */
2801 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2802                                   const char *pin)
2803 {
2804         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2805
2806         if (!w) {
2807                 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2808                 return -EINVAL;
2809         }
2810
2811         dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2812         w->connected = 1;
2813         w->force = 1;
2814         dapm_mark_dirty(w, "force enable");
2815
2816         return 0;
2817 }
2818 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2819
2820 /**
2821  * snd_soc_dapm_disable_pin - disable pin.
2822  * @dapm: DAPM context
2823  * @pin: pin name
2824  *
2825  * Disables input/output pin and its parents or children widgets.
2826  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2827  * do any widget power switching.
2828  */
2829 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2830                              const char *pin)
2831 {
2832         return snd_soc_dapm_set_pin(dapm, pin, 0);
2833 }
2834 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2835
2836 /**
2837  * snd_soc_dapm_nc_pin - permanently disable pin.
2838  * @dapm: DAPM context
2839  * @pin: pin name
2840  *
2841  * Marks the specified pin as being not connected, disabling it along
2842  * any parent or child widgets.  At present this is identical to
2843  * snd_soc_dapm_disable_pin() but in future it will be extended to do
2844  * additional things such as disabling controls which only affect
2845  * paths through the pin.
2846  *
2847  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2848  * do any widget power switching.
2849  */
2850 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2851 {
2852         return snd_soc_dapm_set_pin(dapm, pin, 0);
2853 }
2854 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2855
2856 /**
2857  * snd_soc_dapm_get_pin_status - get audio pin status
2858  * @dapm: DAPM context
2859  * @pin: audio signal pin endpoint (or start point)
2860  *
2861  * Get audio pin status - connected or disconnected.
2862  *
2863  * Returns 1 for connected otherwise 0.
2864  */
2865 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2866                                 const char *pin)
2867 {
2868         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2869
2870         if (w)
2871                 return w->connected;
2872
2873         return 0;
2874 }
2875 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2876
2877 /**
2878  * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2879  * @dapm: DAPM context
2880  * @pin: audio signal pin endpoint (or start point)
2881  *
2882  * Mark the given endpoint or pin as ignoring suspend.  When the
2883  * system is disabled a path between two endpoints flagged as ignoring
2884  * suspend will not be disabled.  The path must already be enabled via
2885  * normal means at suspend time, it will not be turned on if it was not
2886  * already enabled.
2887  */
2888 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2889                                 const char *pin)
2890 {
2891         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
2892
2893         if (!w) {
2894                 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2895                 return -EINVAL;
2896         }
2897
2898         w->ignore_suspend = 1;
2899
2900         return 0;
2901 }
2902 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2903
2904 /**
2905  * snd_soc_dapm_free - free dapm resources
2906  * @dapm: DAPM context
2907  *
2908  * Free all dapm widgets and resources.
2909  */
2910 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2911 {
2912         snd_soc_dapm_sys_remove(dapm->dev);
2913         dapm_debugfs_cleanup(dapm);
2914         dapm_free_widgets(dapm);
2915         list_del(&dapm->list);
2916 }
2917 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2918
2919 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
2920 {
2921         struct snd_soc_dapm_widget *w;
2922         LIST_HEAD(down_list);
2923         int powerdown = 0;
2924
2925         list_for_each_entry(w, &dapm->card->widgets, list) {
2926                 if (w->dapm != dapm)
2927                         continue;
2928                 if (w->power) {
2929                         dapm_seq_insert(w, &down_list, false);
2930                         w->power = 0;
2931                         powerdown = 1;
2932                 }
2933         }
2934
2935         /* If there were no widgets to power down we're already in
2936          * standby.
2937          */
2938         if (powerdown) {
2939                 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
2940                 dapm_seq_run(dapm, &down_list, 0, false);
2941                 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
2942         }
2943 }
2944
2945 /*
2946  * snd_soc_dapm_shutdown - callback for system shutdown
2947  */
2948 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2949 {
2950         struct snd_soc_codec *codec;
2951
2952         list_for_each_entry(codec, &card->codec_dev_list, list) {
2953                 soc_dapm_shutdown_codec(&codec->dapm);
2954                 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
2955         }
2956 }
2957
2958 /* Module information */
2959 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2960 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2961 MODULE_LICENSE("GPL");