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