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