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