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