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