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