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