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