ASoC: dapm: Add snd_soc_dapm_switch to the power up/down sequence table
[linux-2.6-block.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/headphone 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 power down of audio subsystem to reduce pops between a quick
22  *      device reopen.
23  *
24  */
25
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
29 #include <linux/async.h>
30 #include <linux/delay.h>
31 #include <linux/pm.h>
32 #include <linux/bitops.h>
33 #include <linux/platform_device.h>
34 #include <linux/jiffies.h>
35 #include <linux/debugfs.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/clk.h>
39 #include <linux/slab.h>
40 #include <sound/core.h>
41 #include <sound/pcm.h>
42 #include <sound/pcm_params.h>
43 #include <sound/soc.h>
44 #include <sound/initval.h>
45
46 #include <trace/events/asoc.h>
47
48 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
49
50 /* dapm power sequences - make this per codec in the future */
51 static int dapm_up_seq[] = {
52         [snd_soc_dapm_pre] = 0,
53         [snd_soc_dapm_supply] = 1,
54         [snd_soc_dapm_regulator_supply] = 1,
55         [snd_soc_dapm_clock_supply] = 1,
56         [snd_soc_dapm_micbias] = 2,
57         [snd_soc_dapm_dai_link] = 2,
58         [snd_soc_dapm_dai] = 3,
59         [snd_soc_dapm_aif_in] = 3,
60         [snd_soc_dapm_aif_out] = 3,
61         [snd_soc_dapm_mic] = 4,
62         [snd_soc_dapm_mux] = 5,
63         [snd_soc_dapm_virt_mux] = 5,
64         [snd_soc_dapm_value_mux] = 5,
65         [snd_soc_dapm_dac] = 6,
66         [snd_soc_dapm_switch] = 7,
67         [snd_soc_dapm_mixer] = 7,
68         [snd_soc_dapm_mixer_named_ctl] = 7,
69         [snd_soc_dapm_pga] = 8,
70         [snd_soc_dapm_adc] = 9,
71         [snd_soc_dapm_out_drv] = 10,
72         [snd_soc_dapm_hp] = 10,
73         [snd_soc_dapm_spk] = 10,
74         [snd_soc_dapm_line] = 10,
75         [snd_soc_dapm_post] = 11,
76 };
77
78 static int dapm_down_seq[] = {
79         [snd_soc_dapm_pre] = 0,
80         [snd_soc_dapm_adc] = 1,
81         [snd_soc_dapm_hp] = 2,
82         [snd_soc_dapm_spk] = 2,
83         [snd_soc_dapm_line] = 2,
84         [snd_soc_dapm_out_drv] = 2,
85         [snd_soc_dapm_pga] = 4,
86         [snd_soc_dapm_switch] = 5,
87         [snd_soc_dapm_mixer_named_ctl] = 5,
88         [snd_soc_dapm_mixer] = 5,
89         [snd_soc_dapm_dac] = 6,
90         [snd_soc_dapm_mic] = 7,
91         [snd_soc_dapm_micbias] = 8,
92         [snd_soc_dapm_mux] = 9,
93         [snd_soc_dapm_virt_mux] = 9,
94         [snd_soc_dapm_value_mux] = 9,
95         [snd_soc_dapm_aif_in] = 10,
96         [snd_soc_dapm_aif_out] = 10,
97         [snd_soc_dapm_dai] = 10,
98         [snd_soc_dapm_dai_link] = 11,
99         [snd_soc_dapm_clock_supply] = 12,
100         [snd_soc_dapm_regulator_supply] = 12,
101         [snd_soc_dapm_supply] = 12,
102         [snd_soc_dapm_post] = 13,
103 };
104
105 static void pop_wait(u32 pop_time)
106 {
107         if (pop_time)
108                 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
109 }
110
111 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
112 {
113         va_list args;
114         char *buf;
115
116         if (!pop_time)
117                 return;
118
119         buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
120         if (buf == NULL)
121                 return;
122
123         va_start(args, fmt);
124         vsnprintf(buf, PAGE_SIZE, fmt, args);
125         dev_info(dev, "%s", buf);
126         va_end(args);
127
128         kfree(buf);
129 }
130
131 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
132 {
133         return !list_empty(&w->dirty);
134 }
135
136 void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
137 {
138         if (!dapm_dirty_widget(w)) {
139                 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
140                          w->name, reason);
141                 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
142         }
143 }
144 EXPORT_SYMBOL_GPL(dapm_mark_dirty);
145
146 void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
147 {
148         struct snd_soc_card *card = dapm->card;
149         struct snd_soc_dapm_widget *w;
150
151         mutex_lock(&card->dapm_mutex);
152
153         list_for_each_entry(w, &card->widgets, list) {
154                 switch (w->id) {
155                 case snd_soc_dapm_input:
156                 case snd_soc_dapm_output:
157                         dapm_mark_dirty(w, "Rechecking inputs and outputs");
158                         break;
159                 default:
160                         break;
161                 }
162         }
163
164         mutex_unlock(&card->dapm_mutex);
165 }
166 EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
167
168 /* create a new dapm widget */
169 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
170         const struct snd_soc_dapm_widget *_widget)
171 {
172         return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
173 }
174
175 /* get snd_card from DAPM context */
176 static inline struct snd_card *dapm_get_snd_card(
177         struct snd_soc_dapm_context *dapm)
178 {
179         if (dapm->codec)
180                 return dapm->codec->card->snd_card;
181         else if (dapm->platform)
182                 return dapm->platform->card->snd_card;
183         else
184                 BUG();
185
186         /* unreachable */
187         return NULL;
188 }
189
190 /* get soc_card from DAPM context */
191 static inline struct snd_soc_card *dapm_get_soc_card(
192                 struct snd_soc_dapm_context *dapm)
193 {
194         if (dapm->codec)
195                 return dapm->codec->card;
196         else if (dapm->platform)
197                 return dapm->platform->card;
198         else
199                 BUG();
200
201         /* unreachable */
202         return NULL;
203 }
204
205 static void dapm_reset(struct snd_soc_card *card)
206 {
207         struct snd_soc_dapm_widget *w;
208
209         memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
210
211         list_for_each_entry(w, &card->widgets, list) {
212                 w->power_checked = false;
213                 w->inputs = -1;
214                 w->outputs = -1;
215         }
216 }
217
218 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
219 {
220         if (w->codec)
221                 return snd_soc_read(w->codec, reg);
222         else if (w->platform)
223                 return snd_soc_platform_read(w->platform, reg);
224
225         dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
226         return -1;
227 }
228
229 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
230 {
231         if (w->codec)
232                 return snd_soc_write(w->codec, reg, val);
233         else if (w->platform)
234                 return snd_soc_platform_write(w->platform, reg, val);
235
236         dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
237         return -1;
238 }
239
240 static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
241 {
242         if (w->codec && !w->codec->using_regmap)
243                 mutex_lock(&w->codec->mutex);
244         else if (w->platform)
245                 mutex_lock(&w->platform->mutex);
246 }
247
248 static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
249 {
250         if (w->codec && !w->codec->using_regmap)
251                 mutex_unlock(&w->codec->mutex);
252         else if (w->platform)
253                 mutex_unlock(&w->platform->mutex);
254 }
255
256 static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
257         unsigned short reg, unsigned int mask, unsigned int value)
258 {
259         bool change;
260         unsigned int old, new;
261         int ret;
262
263         if (w->codec && w->codec->using_regmap) {
264                 ret = regmap_update_bits_check(w->codec->control_data,
265                                                reg, mask, value, &change);
266                 if (ret != 0)
267                         return ret;
268         } else {
269                 soc_widget_lock(w);
270                 ret = soc_widget_read(w, reg);
271                 if (ret < 0) {
272                         soc_widget_unlock(w);
273                         return ret;
274                 }
275
276                 old = ret;
277                 new = (old & ~mask) | (value & mask);
278                 change = old != new;
279                 if (change) {
280                         ret = soc_widget_write(w, reg, new);
281                         if (ret < 0) {
282                                 soc_widget_unlock(w);
283                                 return ret;
284                         }
285                 }
286                 soc_widget_unlock(w);
287         }
288
289         return change;
290 }
291
292 /**
293  * snd_soc_dapm_set_bias_level - set the bias level for the system
294  * @dapm: DAPM context
295  * @level: level to configure
296  *
297  * Configure the bias (power) levels for the SoC audio device.
298  *
299  * Returns 0 for success else error.
300  */
301 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
302                                        enum snd_soc_bias_level level)
303 {
304         struct snd_soc_card *card = dapm->card;
305         int ret = 0;
306
307         trace_snd_soc_bias_level_start(card, level);
308
309         if (card && card->set_bias_level)
310                 ret = card->set_bias_level(card, dapm, level);
311         if (ret != 0)
312                 goto out;
313
314         if (dapm->codec) {
315                 if (dapm->codec->driver->set_bias_level)
316                         ret = dapm->codec->driver->set_bias_level(dapm->codec,
317                                                                   level);
318                 else
319                         dapm->bias_level = level;
320         } else if (!card || dapm != &card->dapm) {
321                 dapm->bias_level = level;
322         }
323
324         if (ret != 0)
325                 goto out;
326
327         if (card && card->set_bias_level_post)
328                 ret = card->set_bias_level_post(card, dapm, level);
329 out:
330         trace_snd_soc_bias_level_done(card, level);
331
332         return ret;
333 }
334
335 /* set up initial codec paths */
336 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
337         struct snd_soc_dapm_path *p, int i)
338 {
339         switch (w->id) {
340         case snd_soc_dapm_switch:
341         case snd_soc_dapm_mixer:
342         case snd_soc_dapm_mixer_named_ctl: {
343                 int val;
344                 struct soc_mixer_control *mc = (struct soc_mixer_control *)
345                         w->kcontrol_news[i].private_value;
346                 unsigned int reg = mc->reg;
347                 unsigned int shift = mc->shift;
348                 int max = mc->max;
349                 unsigned int mask = (1 << fls(max)) - 1;
350                 unsigned int invert = mc->invert;
351
352                 val = soc_widget_read(w, reg);
353                 val = (val >> shift) & mask;
354                 if (invert)
355                         val = max - val;
356
357                 p->connect = !!val;
358         }
359         break;
360         case snd_soc_dapm_mux: {
361                 struct soc_enum *e = (struct soc_enum *)
362                         w->kcontrol_news[i].private_value;
363                 int val, item;
364
365                 val = soc_widget_read(w, e->reg);
366                 item = (val >> e->shift_l) & e->mask;
367
368                 p->connect = 0;
369                 for (i = 0; i < e->max; i++) {
370                         if (!(strcmp(p->name, e->texts[i])) && item == i)
371                                 p->connect = 1;
372                 }
373         }
374         break;
375         case snd_soc_dapm_virt_mux: {
376                 struct soc_enum *e = (struct soc_enum *)
377                         w->kcontrol_news[i].private_value;
378
379                 p->connect = 0;
380                 /* since a virtual mux has no backing registers to
381                  * decide which path to connect, it will try to match
382                  * with the first enumeration.  This is to ensure
383                  * that the default mux choice (the first) will be
384                  * correctly powered up during initialization.
385                  */
386                 if (!strcmp(p->name, e->texts[0]))
387                         p->connect = 1;
388         }
389         break;
390         case snd_soc_dapm_value_mux: {
391                 struct soc_enum *e = (struct soc_enum *)
392                         w->kcontrol_news[i].private_value;
393                 int val, item;
394
395                 val = soc_widget_read(w, e->reg);
396                 val = (val >> e->shift_l) & e->mask;
397                 for (item = 0; item < e->max; item++) {
398                         if (val == e->values[item])
399                                 break;
400                 }
401
402                 p->connect = 0;
403                 for (i = 0; i < e->max; i++) {
404                         if (!(strcmp(p->name, e->texts[i])) && item == i)
405                                 p->connect = 1;
406                 }
407         }
408         break;
409         /* does not affect routing - always connected */
410         case snd_soc_dapm_pga:
411         case snd_soc_dapm_out_drv:
412         case snd_soc_dapm_output:
413         case snd_soc_dapm_adc:
414         case snd_soc_dapm_input:
415         case snd_soc_dapm_siggen:
416         case snd_soc_dapm_dac:
417         case snd_soc_dapm_micbias:
418         case snd_soc_dapm_vmid:
419         case snd_soc_dapm_supply:
420         case snd_soc_dapm_regulator_supply:
421         case snd_soc_dapm_clock_supply:
422         case snd_soc_dapm_aif_in:
423         case snd_soc_dapm_aif_out:
424         case snd_soc_dapm_dai:
425         case snd_soc_dapm_hp:
426         case snd_soc_dapm_mic:
427         case snd_soc_dapm_spk:
428         case snd_soc_dapm_line:
429         case snd_soc_dapm_dai_link:
430                 p->connect = 1;
431         break;
432         /* does affect routing - dynamically connected */
433         case snd_soc_dapm_pre:
434         case snd_soc_dapm_post:
435                 p->connect = 0;
436         break;
437         }
438 }
439
440 /* connect mux widget to its interconnecting audio paths */
441 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
442         struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
443         struct snd_soc_dapm_path *path, const char *control_name,
444         const struct snd_kcontrol_new *kcontrol)
445 {
446         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
447         int i;
448
449         for (i = 0; i < e->max; i++) {
450                 if (!(strcmp(control_name, e->texts[i]))) {
451                         list_add(&path->list, &dapm->card->paths);
452                         list_add(&path->list_sink, &dest->sources);
453                         list_add(&path->list_source, &src->sinks);
454                         path->name = (char*)e->texts[i];
455                         dapm_set_path_status(dest, path, 0);
456                         return 0;
457                 }
458         }
459
460         return -ENODEV;
461 }
462
463 /* connect mixer widget to its interconnecting audio paths */
464 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
465         struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
466         struct snd_soc_dapm_path *path, const char *control_name)
467 {
468         int i;
469
470         /* search for mixer kcontrol */
471         for (i = 0; i < dest->num_kcontrols; i++) {
472                 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
473                         list_add(&path->list, &dapm->card->paths);
474                         list_add(&path->list_sink, &dest->sources);
475                         list_add(&path->list_source, &src->sinks);
476                         path->name = dest->kcontrol_news[i].name;
477                         dapm_set_path_status(dest, path, i);
478                         return 0;
479                 }
480         }
481         return -ENODEV;
482 }
483
484 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
485         struct snd_soc_dapm_widget *kcontrolw,
486         const struct snd_kcontrol_new *kcontrol_new,
487         struct snd_kcontrol **kcontrol)
488 {
489         struct snd_soc_dapm_widget *w;
490         int i;
491
492         *kcontrol = NULL;
493
494         list_for_each_entry(w, &dapm->card->widgets, list) {
495                 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
496                         continue;
497                 for (i = 0; i < w->num_kcontrols; i++) {
498                         if (&w->kcontrol_news[i] == kcontrol_new) {
499                                 if (w->kcontrols)
500                                         *kcontrol = w->kcontrols[i];
501                                 return 1;
502                         }
503                 }
504         }
505
506         return 0;
507 }
508
509 /*
510  * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
511  * create it. Either way, add the widget into the control's widget list
512  */
513 static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
514         int kci, struct snd_soc_dapm_path *path)
515 {
516         struct snd_soc_dapm_context *dapm = w->dapm;
517         struct snd_card *card = dapm->card->snd_card;
518         const char *prefix;
519         size_t prefix_len;
520         int shared;
521         struct snd_kcontrol *kcontrol;
522         struct snd_soc_dapm_widget_list *wlist;
523         int wlistentries;
524         size_t wlistsize;
525         bool wname_in_long_name, kcname_in_long_name;
526         size_t name_len;
527         char *long_name;
528         const char *name;
529         int ret;
530
531         if (dapm->codec)
532                 prefix = dapm->codec->name_prefix;
533         else
534                 prefix = NULL;
535
536         if (prefix)
537                 prefix_len = strlen(prefix) + 1;
538         else
539                 prefix_len = 0;
540
541         shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
542                                          &kcontrol);
543
544         if (kcontrol) {
545                 wlist = kcontrol->private_data;
546                 wlistentries = wlist->num_widgets + 1;
547         } else {
548                 wlist = NULL;
549                 wlistentries = 1;
550         }
551
552         wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
553                         wlistentries * sizeof(struct snd_soc_dapm_widget *);
554         wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
555         if (wlist == NULL) {
556                 dev_err(dapm->dev, "ASoC: can't allocate widget list for %s\n",
557                         w->name);
558                 return -ENOMEM;
559         }
560         wlist->num_widgets = wlistentries;
561         wlist->widgets[wlistentries - 1] = w;
562
563         if (!kcontrol) {
564                 if (shared) {
565                         wname_in_long_name = false;
566                         kcname_in_long_name = true;
567                 } else {
568                         switch (w->id) {
569                         case snd_soc_dapm_switch:
570                         case snd_soc_dapm_mixer:
571                                 wname_in_long_name = true;
572                                 kcname_in_long_name = true;
573                                 break;
574                         case snd_soc_dapm_mixer_named_ctl:
575                                 wname_in_long_name = false;
576                                 kcname_in_long_name = true;
577                                 break;
578                         case snd_soc_dapm_mux:
579                         case snd_soc_dapm_virt_mux:
580                         case snd_soc_dapm_value_mux:
581                                 wname_in_long_name = true;
582                                 kcname_in_long_name = false;
583                                 break;
584                         default:
585                                 kfree(wlist);
586                                 return -EINVAL;
587                         }
588                 }
589
590                 if (wname_in_long_name && kcname_in_long_name) {
591                         name_len = strlen(w->name) - prefix_len + 1 +
592                                    strlen(w->kcontrol_news[kci].name) + 1;
593
594                         long_name = kmalloc(name_len, GFP_KERNEL);
595                         if (long_name == NULL) {
596                                 kfree(wlist);
597                                 return -ENOMEM;
598                         }
599
600                         /*
601                          * The control will get a prefix from the control
602                          * creation process but we're also using the same
603                          * prefix for widgets so cut the prefix off the
604                          * front of the widget name.
605                          */
606                         snprintf(long_name, name_len, "%s %s",
607                                  w->name + prefix_len,
608                                  w->kcontrol_news[kci].name);
609                         long_name[name_len - 1] = '\0';
610
611                         name = long_name;
612                 } else if (wname_in_long_name) {
613                         long_name = NULL;
614                         name = w->name + prefix_len;
615                 } else {
616                         long_name = NULL;
617                         name = w->kcontrol_news[kci].name;
618                 }
619
620                 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], wlist, name,
621                                         prefix);
622                 ret = snd_ctl_add(card, kcontrol);
623                 if (ret < 0) {
624                         dev_err(dapm->dev,
625                                 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
626                                 w->name, name, ret);
627                         kfree(wlist);
628                         kfree(long_name);
629                         return ret;
630                 }
631
632                 path->long_name = long_name;
633         }
634
635         kcontrol->private_data = wlist;
636         w->kcontrols[kci] = kcontrol;
637         path->kcontrol = kcontrol;
638
639         return 0;
640 }
641
642 /* create new dapm mixer control */
643 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
644 {
645         int i, ret;
646         struct snd_soc_dapm_path *path;
647
648         /* add kcontrol */
649         for (i = 0; i < w->num_kcontrols; i++) {
650                 /* match name */
651                 list_for_each_entry(path, &w->sources, list_sink) {
652                         /* mixer/mux paths name must match control name */
653                         if (path->name != (char *)w->kcontrol_news[i].name)
654                                 continue;
655
656                         if (w->kcontrols[i]) {
657                                 path->kcontrol = w->kcontrols[i];
658                                 continue;
659                         }
660
661                         ret = dapm_create_or_share_mixmux_kcontrol(w, i, path);
662                         if (ret < 0)
663                                 return ret;
664                 }
665         }
666
667         return 0;
668 }
669
670 /* create new dapm mux control */
671 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
672 {
673         struct snd_soc_dapm_context *dapm = w->dapm;
674         struct snd_soc_dapm_path *path;
675         int ret;
676
677         if (w->num_kcontrols != 1) {
678                 dev_err(dapm->dev,
679                         "ASoC: mux %s has incorrect number of controls\n",
680                         w->name);
681                 return -EINVAL;
682         }
683
684         path = list_first_entry(&w->sources, struct snd_soc_dapm_path,
685                                 list_sink);
686         if (!path) {
687                 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
688                 return -EINVAL;
689         }
690
691         ret = dapm_create_or_share_mixmux_kcontrol(w, 0, path);
692         if (ret < 0)
693                 return ret;
694
695         list_for_each_entry(path, &w->sources, list_sink)
696                 path->kcontrol = w->kcontrols[0];
697
698         return 0;
699 }
700
701 /* create new dapm volume control */
702 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
703 {
704         if (w->num_kcontrols)
705                 dev_err(w->dapm->dev,
706                         "ASoC: PGA controls not supported: '%s'\n", w->name);
707
708         return 0;
709 }
710
711 /* reset 'walked' bit for each dapm path */
712 static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
713                                    struct list_head *sink)
714 {
715         struct snd_soc_dapm_path *p;
716
717         list_for_each_entry(p, sink, list_source) {
718                 if (p->walked) {
719                         p->walked = 0;
720                         dapm_clear_walk_output(dapm, &p->sink->sinks);
721                 }
722         }
723 }
724
725 static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
726                                   struct list_head *source)
727 {
728         struct snd_soc_dapm_path *p;
729
730         list_for_each_entry(p, source, list_sink) {
731                 if (p->walked) {
732                         p->walked = 0;
733                         dapm_clear_walk_input(dapm, &p->source->sources);
734                 }
735         }
736 }
737
738
739 /* We implement power down on suspend by checking the power state of
740  * the ALSA card - when we are suspending the ALSA state for the card
741  * is set to D3.
742  */
743 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
744 {
745         int level = snd_power_get_state(widget->dapm->card->snd_card);
746
747         switch (level) {
748         case SNDRV_CTL_POWER_D3hot:
749         case SNDRV_CTL_POWER_D3cold:
750                 if (widget->ignore_suspend)
751                         dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
752                                 widget->name);
753                 return widget->ignore_suspend;
754         default:
755                 return 1;
756         }
757 }
758
759 /* add widget to list if it's not already in the list */
760 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
761         struct snd_soc_dapm_widget *w)
762 {
763         struct snd_soc_dapm_widget_list *wlist;
764         int wlistsize, wlistentries, i;
765
766         if (*list == NULL)
767                 return -EINVAL;
768
769         wlist = *list;
770
771         /* is this widget already in the list */
772         for (i = 0; i < wlist->num_widgets; i++) {
773                 if (wlist->widgets[i] == w)
774                         return 0;
775         }
776
777         /* allocate some new space */
778         wlistentries = wlist->num_widgets + 1;
779         wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
780                         wlistentries * sizeof(struct snd_soc_dapm_widget *);
781         *list = krealloc(wlist, wlistsize, GFP_KERNEL);
782         if (*list == NULL) {
783                 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
784                         w->name);
785                 return -ENOMEM;
786         }
787         wlist = *list;
788
789         /* insert the widget */
790         dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
791                         w->name, wlist->num_widgets);
792
793         wlist->widgets[wlist->num_widgets] = w;
794         wlist->num_widgets++;
795         return 1;
796 }
797
798 /*
799  * Recursively check for a completed path to an active or physically connected
800  * output widget. Returns number of complete paths.
801  */
802 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
803         struct snd_soc_dapm_widget_list **list)
804 {
805         struct snd_soc_dapm_path *path;
806         int con = 0;
807
808         if (widget->outputs >= 0)
809                 return widget->outputs;
810
811         DAPM_UPDATE_STAT(widget, path_checks);
812
813         switch (widget->id) {
814         case snd_soc_dapm_supply:
815         case snd_soc_dapm_regulator_supply:
816         case snd_soc_dapm_clock_supply:
817                 return 0;
818         default:
819                 break;
820         }
821
822         switch (widget->id) {
823         case snd_soc_dapm_adc:
824         case snd_soc_dapm_aif_out:
825         case snd_soc_dapm_dai:
826                 if (widget->active) {
827                         widget->outputs = snd_soc_dapm_suspend_check(widget);
828                         return widget->outputs;
829                 }
830         default:
831                 break;
832         }
833
834         if (widget->connected) {
835                 /* connected pin ? */
836                 if (widget->id == snd_soc_dapm_output && !widget->ext) {
837                         widget->outputs = snd_soc_dapm_suspend_check(widget);
838                         return widget->outputs;
839                 }
840
841                 /* connected jack or spk ? */
842                 if (widget->id == snd_soc_dapm_hp ||
843                     widget->id == snd_soc_dapm_spk ||
844                     (widget->id == snd_soc_dapm_line &&
845                      !list_empty(&widget->sources))) {
846                         widget->outputs = snd_soc_dapm_suspend_check(widget);
847                         return widget->outputs;
848                 }
849         }
850
851         list_for_each_entry(path, &widget->sinks, list_source) {
852                 DAPM_UPDATE_STAT(widget, neighbour_checks);
853
854                 if (path->weak)
855                         continue;
856
857                 if (path->walking)
858                         return 1;
859
860                 if (path->walked)
861                         continue;
862
863                 trace_snd_soc_dapm_output_path(widget, path);
864
865                 if (path->sink && path->connect) {
866                         path->walked = 1;
867                         path->walking = 1;
868
869                         /* do we need to add this widget to the list ? */
870                         if (list) {
871                                 int err;
872                                 err = dapm_list_add_widget(list, path->sink);
873                                 if (err < 0) {
874                                         dev_err(widget->dapm->dev,
875                                                 "ASoC: could not add widget %s\n",
876                                                 widget->name);
877                                         path->walking = 0;
878                                         return con;
879                                 }
880                         }
881
882                         con += is_connected_output_ep(path->sink, list);
883
884                         path->walking = 0;
885                 }
886         }
887
888         widget->outputs = con;
889
890         return con;
891 }
892
893 /*
894  * Recursively check for a completed path to an active or physically connected
895  * input widget. Returns number of complete paths.
896  */
897 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
898         struct snd_soc_dapm_widget_list **list)
899 {
900         struct snd_soc_dapm_path *path;
901         int con = 0;
902
903         if (widget->inputs >= 0)
904                 return widget->inputs;
905
906         DAPM_UPDATE_STAT(widget, path_checks);
907
908         switch (widget->id) {
909         case snd_soc_dapm_supply:
910         case snd_soc_dapm_regulator_supply:
911         case snd_soc_dapm_clock_supply:
912                 return 0;
913         default:
914                 break;
915         }
916
917         /* active stream ? */
918         switch (widget->id) {
919         case snd_soc_dapm_dac:
920         case snd_soc_dapm_aif_in:
921         case snd_soc_dapm_dai:
922                 if (widget->active) {
923                         widget->inputs = snd_soc_dapm_suspend_check(widget);
924                         return widget->inputs;
925                 }
926         default:
927                 break;
928         }
929
930         if (widget->connected) {
931                 /* connected pin ? */
932                 if (widget->id == snd_soc_dapm_input && !widget->ext) {
933                         widget->inputs = snd_soc_dapm_suspend_check(widget);
934                         return widget->inputs;
935                 }
936
937                 /* connected VMID/Bias for lower pops */
938                 if (widget->id == snd_soc_dapm_vmid) {
939                         widget->inputs = snd_soc_dapm_suspend_check(widget);
940                         return widget->inputs;
941                 }
942
943                 /* connected jack ? */
944                 if (widget->id == snd_soc_dapm_mic ||
945                     (widget->id == snd_soc_dapm_line &&
946                      !list_empty(&widget->sinks))) {
947                         widget->inputs = snd_soc_dapm_suspend_check(widget);
948                         return widget->inputs;
949                 }
950
951                 /* signal generator */
952                 if (widget->id == snd_soc_dapm_siggen) {
953                         widget->inputs = snd_soc_dapm_suspend_check(widget);
954                         return widget->inputs;
955                 }
956         }
957
958         list_for_each_entry(path, &widget->sources, list_sink) {
959                 DAPM_UPDATE_STAT(widget, neighbour_checks);
960
961                 if (path->weak)
962                         continue;
963
964                 if (path->walking)
965                         return 1;
966
967                 if (path->walked)
968                         continue;
969
970                 trace_snd_soc_dapm_input_path(widget, path);
971
972                 if (path->source && path->connect) {
973                         path->walked = 1;
974                         path->walking = 1;
975
976                         /* do we need to add this widget to the list ? */
977                         if (list) {
978                                 int err;
979                                 err = dapm_list_add_widget(list, path->source);
980                                 if (err < 0) {
981                                         dev_err(widget->dapm->dev,
982                                                 "ASoC: could not add widget %s\n",
983                                                 widget->name);
984                                         path->walking = 0;
985                                         return con;
986                                 }
987                         }
988
989                         con += is_connected_input_ep(path->source, list);
990
991                         path->walking = 0;
992                 }
993         }
994
995         widget->inputs = con;
996
997         return con;
998 }
999
1000 /**
1001  * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1002  * @dai: the soc DAI.
1003  * @stream: stream direction.
1004  * @list: list of active widgets for this stream.
1005  *
1006  * Queries DAPM graph as to whether an valid audio stream path exists for
1007  * the initial stream specified by name. This takes into account
1008  * current mixer and mux kcontrol settings. Creates list of valid widgets.
1009  *
1010  * Returns the number of valid paths or negative error.
1011  */
1012 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1013         struct snd_soc_dapm_widget_list **list)
1014 {
1015         struct snd_soc_card *card = dai->card;
1016         int paths;
1017
1018         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1019         dapm_reset(card);
1020
1021         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1022                 paths = is_connected_output_ep(dai->playback_widget, list);
1023                 dapm_clear_walk_output(&card->dapm,
1024                                        &dai->playback_widget->sinks);
1025         } else {
1026                 paths = is_connected_input_ep(dai->capture_widget, list);
1027                 dapm_clear_walk_input(&card->dapm,
1028                                       &dai->capture_widget->sources);
1029         }
1030
1031         trace_snd_soc_dapm_connected(paths, stream);
1032         mutex_unlock(&card->dapm_mutex);
1033
1034         return paths;
1035 }
1036
1037 /*
1038  * Handler for generic register modifier widget.
1039  */
1040 int dapm_reg_event(struct snd_soc_dapm_widget *w,
1041                    struct snd_kcontrol *kcontrol, int event)
1042 {
1043         unsigned int val;
1044
1045         if (SND_SOC_DAPM_EVENT_ON(event))
1046                 val = w->on_val;
1047         else
1048                 val = w->off_val;
1049
1050         soc_widget_update_bits_locked(w, -(w->reg + 1),
1051                             w->mask << w->shift, val << w->shift);
1052
1053         return 0;
1054 }
1055 EXPORT_SYMBOL_GPL(dapm_reg_event);
1056
1057 /*
1058  * Handler for regulator supply widget.
1059  */
1060 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1061                    struct snd_kcontrol *kcontrol, int event)
1062 {
1063         int ret;
1064
1065         if (SND_SOC_DAPM_EVENT_ON(event)) {
1066                 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
1067                         ret = regulator_allow_bypass(w->regulator, false);
1068                         if (ret != 0)
1069                                 dev_warn(w->dapm->dev,
1070                                          "ASoC: Failed to bypass %s: %d\n",
1071                                          w->name, ret);
1072                 }
1073
1074                 return regulator_enable(w->regulator);
1075         } else {
1076                 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
1077                         ret = regulator_allow_bypass(w->regulator, true);
1078                         if (ret != 0)
1079                                 dev_warn(w->dapm->dev,
1080                                          "ASoC: Failed to unbypass %s: %d\n",
1081                                          w->name, ret);
1082                 }
1083
1084                 return regulator_disable_deferred(w->regulator, w->shift);
1085         }
1086 }
1087 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1088
1089 /*
1090  * Handler for clock supply widget.
1091  */
1092 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1093                    struct snd_kcontrol *kcontrol, int event)
1094 {
1095         if (!w->clk)
1096                 return -EIO;
1097
1098 #ifdef CONFIG_HAVE_CLK
1099         if (SND_SOC_DAPM_EVENT_ON(event)) {
1100                 return clk_prepare_enable(w->clk);
1101         } else {
1102                 clk_disable_unprepare(w->clk);
1103                 return 0;
1104         }
1105 #endif
1106         return 0;
1107 }
1108 EXPORT_SYMBOL_GPL(dapm_clock_event);
1109
1110 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1111 {
1112         if (w->power_checked)
1113                 return w->new_power;
1114
1115         if (w->force)
1116                 w->new_power = 1;
1117         else
1118                 w->new_power = w->power_check(w);
1119
1120         w->power_checked = true;
1121
1122         return w->new_power;
1123 }
1124
1125 /* Generic check to see if a widget should be powered.
1126  */
1127 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1128 {
1129         int in, out;
1130
1131         DAPM_UPDATE_STAT(w, power_checks);
1132
1133         in = is_connected_input_ep(w, NULL);
1134         dapm_clear_walk_input(w->dapm, &w->sources);
1135         out = is_connected_output_ep(w, NULL);
1136         dapm_clear_walk_output(w->dapm, &w->sinks);
1137         return out != 0 && in != 0;
1138 }
1139
1140 static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
1141 {
1142         DAPM_UPDATE_STAT(w, power_checks);
1143
1144         if (w->active)
1145                 return w->active;
1146
1147         return dapm_generic_check_power(w);
1148 }
1149
1150 /* Check to see if an ADC has power */
1151 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1152 {
1153         int in;
1154
1155         DAPM_UPDATE_STAT(w, power_checks);
1156
1157         if (w->active) {
1158                 in = is_connected_input_ep(w, NULL);
1159                 dapm_clear_walk_input(w->dapm, &w->sources);
1160                 return in != 0;
1161         } else {
1162                 return dapm_generic_check_power(w);
1163         }
1164 }
1165
1166 /* Check to see if a DAC has power */
1167 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1168 {
1169         int out;
1170
1171         DAPM_UPDATE_STAT(w, power_checks);
1172
1173         if (w->active) {
1174                 out = is_connected_output_ep(w, NULL);
1175                 dapm_clear_walk_output(w->dapm, &w->sinks);
1176                 return out != 0;
1177         } else {
1178                 return dapm_generic_check_power(w);
1179         }
1180 }
1181
1182 /* Check to see if a power supply is needed */
1183 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1184 {
1185         struct snd_soc_dapm_path *path;
1186
1187         DAPM_UPDATE_STAT(w, power_checks);
1188
1189         /* Check if one of our outputs is connected */
1190         list_for_each_entry(path, &w->sinks, list_source) {
1191                 DAPM_UPDATE_STAT(w, neighbour_checks);
1192
1193                 if (path->weak)
1194                         continue;
1195
1196                 if (path->connected &&
1197                     !path->connected(path->source, path->sink))
1198                         continue;
1199
1200                 if (!path->sink)
1201                         continue;
1202
1203                 if (dapm_widget_power_check(path->sink))
1204                         return 1;
1205         }
1206
1207         return 0;
1208 }
1209
1210 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1211 {
1212         return 1;
1213 }
1214
1215 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1216                             struct snd_soc_dapm_widget *b,
1217                             bool power_up)
1218 {
1219         int *sort;
1220
1221         if (power_up)
1222                 sort = dapm_up_seq;
1223         else
1224                 sort = dapm_down_seq;
1225
1226         if (sort[a->id] != sort[b->id])
1227                 return sort[a->id] - sort[b->id];
1228         if (a->subseq != b->subseq) {
1229                 if (power_up)
1230                         return a->subseq - b->subseq;
1231                 else
1232                         return b->subseq - a->subseq;
1233         }
1234         if (a->reg != b->reg)
1235                 return a->reg - b->reg;
1236         if (a->dapm != b->dapm)
1237                 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1238
1239         return 0;
1240 }
1241
1242 /* Insert a widget in order into a DAPM power sequence. */
1243 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1244                             struct list_head *list,
1245                             bool power_up)
1246 {
1247         struct snd_soc_dapm_widget *w;
1248
1249         list_for_each_entry(w, list, power_list)
1250                 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1251                         list_add_tail(&new_widget->power_list, &w->power_list);
1252                         return;
1253                 }
1254
1255         list_add_tail(&new_widget->power_list, list);
1256 }
1257
1258 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1259                                  struct snd_soc_dapm_widget *w, int event)
1260 {
1261         struct snd_soc_card *card = dapm->card;
1262         const char *ev_name;
1263         int power, ret;
1264
1265         switch (event) {
1266         case SND_SOC_DAPM_PRE_PMU:
1267                 ev_name = "PRE_PMU";
1268                 power = 1;
1269                 break;
1270         case SND_SOC_DAPM_POST_PMU:
1271                 ev_name = "POST_PMU";
1272                 power = 1;
1273                 break;
1274         case SND_SOC_DAPM_PRE_PMD:
1275                 ev_name = "PRE_PMD";
1276                 power = 0;
1277                 break;
1278         case SND_SOC_DAPM_POST_PMD:
1279                 ev_name = "POST_PMD";
1280                 power = 0;
1281                 break;
1282         default:
1283                 BUG();
1284                 return;
1285         }
1286
1287         if (w->power != power)
1288                 return;
1289
1290         if (w->event && (w->event_flags & event)) {
1291                 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1292                         w->name, ev_name);
1293                 trace_snd_soc_dapm_widget_event_start(w, event);
1294                 ret = w->event(w, NULL, event);
1295                 trace_snd_soc_dapm_widget_event_done(w, event);
1296                 if (ret < 0)
1297                         dev_err(dapm->dev, "ASoC: %s: %s event failed: %d\n",
1298                                ev_name, w->name, ret);
1299         }
1300 }
1301
1302 /* Apply the coalesced changes from a DAPM sequence */
1303 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
1304                                    struct list_head *pending)
1305 {
1306         struct snd_soc_card *card = dapm->card;
1307         struct snd_soc_dapm_widget *w;
1308         int reg, power;
1309         unsigned int value = 0;
1310         unsigned int mask = 0;
1311         unsigned int cur_mask;
1312
1313         reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1314                                power_list)->reg;
1315
1316         list_for_each_entry(w, pending, power_list) {
1317                 cur_mask = 1 << w->shift;
1318                 BUG_ON(reg != w->reg);
1319
1320                 if (w->invert)
1321                         power = !w->power;
1322                 else
1323                         power = w->power;
1324
1325                 mask |= cur_mask;
1326                 if (power)
1327                         value |= cur_mask;
1328
1329                 pop_dbg(dapm->dev, card->pop_time,
1330                         "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1331                         w->name, reg, value, mask);
1332
1333                 /* Check for events */
1334                 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1335                 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1336         }
1337
1338         if (reg >= 0) {
1339                 /* Any widget will do, they should all be updating the
1340                  * same register.
1341                  */
1342                 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1343                                      power_list);
1344
1345                 pop_dbg(dapm->dev, card->pop_time,
1346                         "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1347                         value, mask, reg, card->pop_time);
1348                 pop_wait(card->pop_time);
1349                 soc_widget_update_bits_locked(w, reg, mask, value);
1350         }
1351
1352         list_for_each_entry(w, pending, power_list) {
1353                 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1354                 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1355         }
1356 }
1357
1358 /* Apply a DAPM power sequence.
1359  *
1360  * We walk over a pre-sorted list of widgets to apply power to.  In
1361  * order to minimise the number of writes to the device required
1362  * multiple widgets will be updated in a single write where possible.
1363  * Currently anything that requires more than a single write is not
1364  * handled.
1365  */
1366 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1367                          struct list_head *list, int event, bool power_up)
1368 {
1369         struct snd_soc_dapm_widget *w, *n;
1370         LIST_HEAD(pending);
1371         int cur_sort = -1;
1372         int cur_subseq = -1;
1373         int cur_reg = SND_SOC_NOPM;
1374         struct snd_soc_dapm_context *cur_dapm = NULL;
1375         int ret, i;
1376         int *sort;
1377
1378         if (power_up)
1379                 sort = dapm_up_seq;
1380         else
1381                 sort = dapm_down_seq;
1382
1383         list_for_each_entry_safe(w, n, list, power_list) {
1384                 ret = 0;
1385
1386                 /* Do we need to apply any queued changes? */
1387                 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1388                     w->dapm != cur_dapm || w->subseq != cur_subseq) {
1389                         if (!list_empty(&pending))
1390                                 dapm_seq_run_coalesced(cur_dapm, &pending);
1391
1392                         if (cur_dapm && cur_dapm->seq_notifier) {
1393                                 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1394                                         if (sort[i] == cur_sort)
1395                                                 cur_dapm->seq_notifier(cur_dapm,
1396                                                                        i,
1397                                                                        cur_subseq);
1398                         }
1399
1400                         INIT_LIST_HEAD(&pending);
1401                         cur_sort = -1;
1402                         cur_subseq = INT_MIN;
1403                         cur_reg = SND_SOC_NOPM;
1404                         cur_dapm = NULL;
1405                 }
1406
1407                 switch (w->id) {
1408                 case snd_soc_dapm_pre:
1409                         if (!w->event)
1410                                 list_for_each_entry_safe_continue(w, n, list,
1411                                                                   power_list);
1412
1413                         if (event == SND_SOC_DAPM_STREAM_START)
1414                                 ret = w->event(w,
1415                                                NULL, SND_SOC_DAPM_PRE_PMU);
1416                         else if (event == SND_SOC_DAPM_STREAM_STOP)
1417                                 ret = w->event(w,
1418                                                NULL, SND_SOC_DAPM_PRE_PMD);
1419                         break;
1420
1421                 case snd_soc_dapm_post:
1422                         if (!w->event)
1423                                 list_for_each_entry_safe_continue(w, n, list,
1424                                                                   power_list);
1425
1426                         if (event == SND_SOC_DAPM_STREAM_START)
1427                                 ret = w->event(w,
1428                                                NULL, SND_SOC_DAPM_POST_PMU);
1429                         else if (event == SND_SOC_DAPM_STREAM_STOP)
1430                                 ret = w->event(w,
1431                                                NULL, SND_SOC_DAPM_POST_PMD);
1432                         break;
1433
1434                 default:
1435                         /* Queue it up for application */
1436                         cur_sort = sort[w->id];
1437                         cur_subseq = w->subseq;
1438                         cur_reg = w->reg;
1439                         cur_dapm = w->dapm;
1440                         list_move(&w->power_list, &pending);
1441                         break;
1442                 }
1443
1444                 if (ret < 0)
1445                         dev_err(w->dapm->dev,
1446                                 "ASoC: Failed to apply widget power: %d\n", ret);
1447         }
1448
1449         if (!list_empty(&pending))
1450                 dapm_seq_run_coalesced(cur_dapm, &pending);
1451
1452         if (cur_dapm && cur_dapm->seq_notifier) {
1453                 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1454                         if (sort[i] == cur_sort)
1455                                 cur_dapm->seq_notifier(cur_dapm,
1456                                                        i, cur_subseq);
1457         }
1458 }
1459
1460 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1461 {
1462         struct snd_soc_dapm_update *update = dapm->update;
1463         struct snd_soc_dapm_widget *w;
1464         int ret;
1465
1466         if (!update)
1467                 return;
1468
1469         w = update->widget;
1470
1471         if (w->event &&
1472             (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1473                 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1474                 if (ret != 0)
1475                         dev_err(dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1476                                w->name, ret);
1477         }
1478
1479         ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
1480                                   update->val);
1481         if (ret < 0)
1482                 dev_err(dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1483                         w->name, ret);
1484
1485         if (w->event &&
1486             (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1487                 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1488                 if (ret != 0)
1489                         dev_err(dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1490                                w->name, ret);
1491         }
1492 }
1493
1494 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1495  * they're changing state.
1496  */
1497 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1498 {
1499         struct snd_soc_dapm_context *d = data;
1500         int ret;
1501
1502         /* If we're off and we're not supposed to be go into STANDBY */
1503         if (d->bias_level == SND_SOC_BIAS_OFF &&
1504             d->target_bias_level != SND_SOC_BIAS_OFF) {
1505                 if (d->dev)
1506                         pm_runtime_get_sync(d->dev);
1507
1508                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1509                 if (ret != 0)
1510                         dev_err(d->dev,
1511                                 "ASoC: Failed to turn on bias: %d\n", ret);
1512         }
1513
1514         /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1515         if (d->bias_level != d->target_bias_level) {
1516                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1517                 if (ret != 0)
1518                         dev_err(d->dev,
1519                                 "ASoC: Failed to prepare bias: %d\n", ret);
1520         }
1521 }
1522
1523 /* Async callback run prior to DAPM sequences - brings to their final
1524  * state.
1525  */
1526 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1527 {
1528         struct snd_soc_dapm_context *d = data;
1529         int ret;
1530
1531         /* If we just powered the last thing off drop to standby bias */
1532         if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1533             (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1534              d->target_bias_level == SND_SOC_BIAS_OFF)) {
1535                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1536                 if (ret != 0)
1537                         dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1538                                 ret);
1539         }
1540
1541         /* If we're in standby and can support bias off then do that */
1542         if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1543             d->target_bias_level == SND_SOC_BIAS_OFF) {
1544                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1545                 if (ret != 0)
1546                         dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1547                                 ret);
1548
1549                 if (d->dev)
1550                         pm_runtime_put(d->dev);
1551         }
1552
1553         /* If we just powered up then move to active bias */
1554         if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1555             d->target_bias_level == SND_SOC_BIAS_ON) {
1556                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1557                 if (ret != 0)
1558                         dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1559                                 ret);
1560         }
1561 }
1562
1563 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1564                                        bool power, bool connect)
1565 {
1566         /* If a connection is being made or broken then that update
1567          * will have marked the peer dirty, otherwise the widgets are
1568          * not connected and this update has no impact. */
1569         if (!connect)
1570                 return;
1571
1572         /* If the peer is already in the state we're moving to then we
1573          * won't have an impact on it. */
1574         if (power != peer->power)
1575                 dapm_mark_dirty(peer, "peer state change");
1576 }
1577
1578 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1579                                   struct list_head *up_list,
1580                                   struct list_head *down_list)
1581 {
1582         struct snd_soc_dapm_path *path;
1583
1584         if (w->power == power)
1585                 return;
1586
1587         trace_snd_soc_dapm_widget_power(w, power);
1588
1589         /* If we changed our power state perhaps our neigbours changed
1590          * also.
1591          */
1592         list_for_each_entry(path, &w->sources, list_sink) {
1593                 if (path->source) {
1594                         dapm_widget_set_peer_power(path->source, power,
1595                                                    path->connect);
1596                 }
1597         }
1598         switch (w->id) {
1599         case snd_soc_dapm_supply:
1600         case snd_soc_dapm_regulator_supply:
1601         case snd_soc_dapm_clock_supply:
1602                 /* Supplies can't affect their outputs, only their inputs */
1603                 break;
1604         default:
1605                 list_for_each_entry(path, &w->sinks, list_source) {
1606                         if (path->sink) {
1607                                 dapm_widget_set_peer_power(path->sink, power,
1608                                                            path->connect);
1609                         }
1610                 }
1611                 break;
1612         }
1613
1614         if (power)
1615                 dapm_seq_insert(w, up_list, true);
1616         else
1617                 dapm_seq_insert(w, down_list, false);
1618
1619         w->power = power;
1620 }
1621
1622 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1623                                   struct list_head *up_list,
1624                                   struct list_head *down_list)
1625 {
1626         int power;
1627
1628         switch (w->id) {
1629         case snd_soc_dapm_pre:
1630                 dapm_seq_insert(w, down_list, false);
1631                 break;
1632         case snd_soc_dapm_post:
1633                 dapm_seq_insert(w, up_list, true);
1634                 break;
1635
1636         default:
1637                 power = dapm_widget_power_check(w);
1638
1639                 dapm_widget_set_power(w, power, up_list, down_list);
1640                 break;
1641         }
1642 }
1643
1644 /*
1645  * Scan each dapm widget for complete audio path.
1646  * A complete path is a route that has valid endpoints i.e.:-
1647  *
1648  *  o DAC to output pin.
1649  *  o Input Pin to ADC.
1650  *  o Input pin to Output pin (bypass, sidetone)
1651  *  o DAC to ADC (loopback).
1652  */
1653 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1654 {
1655         struct snd_soc_card *card = dapm->card;
1656         struct snd_soc_dapm_widget *w;
1657         struct snd_soc_dapm_context *d;
1658         LIST_HEAD(up_list);
1659         LIST_HEAD(down_list);
1660         ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1661         enum snd_soc_bias_level bias;
1662
1663         trace_snd_soc_dapm_start(card);
1664
1665         list_for_each_entry(d, &card->dapm_list, list) {
1666                 if (d->idle_bias_off)
1667                         d->target_bias_level = SND_SOC_BIAS_OFF;
1668                 else
1669                         d->target_bias_level = SND_SOC_BIAS_STANDBY;
1670         }
1671
1672         dapm_reset(card);
1673
1674         /* Check which widgets we need to power and store them in
1675          * lists indicating if they should be powered up or down.  We
1676          * only check widgets that have been flagged as dirty but note
1677          * that new widgets may be added to the dirty list while we
1678          * iterate.
1679          */
1680         list_for_each_entry(w, &card->dapm_dirty, dirty) {
1681                 dapm_power_one_widget(w, &up_list, &down_list);
1682         }
1683
1684         list_for_each_entry(w, &card->widgets, list) {
1685                 switch (w->id) {
1686                 case snd_soc_dapm_pre:
1687                 case snd_soc_dapm_post:
1688                         /* These widgets always need to be powered */
1689                         break;
1690                 default:
1691                         list_del_init(&w->dirty);
1692                         break;
1693                 }
1694
1695                 if (w->power) {
1696                         d = w->dapm;
1697
1698                         /* Supplies and micbiases only bring the
1699                          * context up to STANDBY as unless something
1700                          * else is active and passing audio they
1701                          * generally don't require full power.  Signal
1702                          * generators are virtual pins and have no
1703                          * power impact themselves.
1704                          */
1705                         switch (w->id) {
1706                         case snd_soc_dapm_siggen:
1707                                 break;
1708                         case snd_soc_dapm_supply:
1709                         case snd_soc_dapm_regulator_supply:
1710                         case snd_soc_dapm_clock_supply:
1711                         case snd_soc_dapm_micbias:
1712                                 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1713                                         d->target_bias_level = SND_SOC_BIAS_STANDBY;
1714                                 break;
1715                         default:
1716                                 d->target_bias_level = SND_SOC_BIAS_ON;
1717                                 break;
1718                         }
1719                 }
1720
1721         }
1722
1723         /* Force all contexts in the card to the same bias state if
1724          * they're not ground referenced.
1725          */
1726         bias = SND_SOC_BIAS_OFF;
1727         list_for_each_entry(d, &card->dapm_list, list)
1728                 if (d->target_bias_level > bias)
1729                         bias = d->target_bias_level;
1730         list_for_each_entry(d, &card->dapm_list, list)
1731                 if (!d->idle_bias_off)
1732                         d->target_bias_level = bias;
1733
1734         trace_snd_soc_dapm_walk_done(card);
1735
1736         /* Run all the bias changes in parallel */
1737         list_for_each_entry(d, &dapm->card->dapm_list, list)
1738                 async_schedule_domain(dapm_pre_sequence_async, d,
1739                                         &async_domain);
1740         async_synchronize_full_domain(&async_domain);
1741
1742         /* Power down widgets first; try to avoid amplifying pops. */
1743         dapm_seq_run(dapm, &down_list, event, false);
1744
1745         dapm_widget_update(dapm);
1746
1747         /* Now power up. */
1748         dapm_seq_run(dapm, &up_list, event, true);
1749
1750         /* Run all the bias changes in parallel */
1751         list_for_each_entry(d, &dapm->card->dapm_list, list)
1752                 async_schedule_domain(dapm_post_sequence_async, d,
1753                                         &async_domain);
1754         async_synchronize_full_domain(&async_domain);
1755
1756         /* do we need to notify any clients that DAPM event is complete */
1757         list_for_each_entry(d, &card->dapm_list, list) {
1758                 if (d->stream_event)
1759                         d->stream_event(d, event);
1760         }
1761
1762         pop_dbg(dapm->dev, card->pop_time,
1763                 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1764         pop_wait(card->pop_time);
1765
1766         trace_snd_soc_dapm_done(card);
1767
1768         return 0;
1769 }
1770
1771 #ifdef CONFIG_DEBUG_FS
1772 static ssize_t dapm_widget_power_read_file(struct file *file,
1773                                            char __user *user_buf,
1774                                            size_t count, loff_t *ppos)
1775 {
1776         struct snd_soc_dapm_widget *w = file->private_data;
1777         char *buf;
1778         int in, out;
1779         ssize_t ret;
1780         struct snd_soc_dapm_path *p = NULL;
1781
1782         buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1783         if (!buf)
1784                 return -ENOMEM;
1785
1786         in = is_connected_input_ep(w, NULL);
1787         dapm_clear_walk_input(w->dapm, &w->sources);
1788         out = is_connected_output_ep(w, NULL);
1789         dapm_clear_walk_output(w->dapm, &w->sinks);
1790
1791         ret = snprintf(buf, PAGE_SIZE, "%s: %s%s  in %d out %d",
1792                        w->name, w->power ? "On" : "Off",
1793                        w->force ? " (forced)" : "", in, out);
1794
1795         if (w->reg >= 0)
1796                 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1797                                 " - R%d(0x%x) bit %d",
1798                                 w->reg, w->reg, w->shift);
1799
1800         ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1801
1802         if (w->sname)
1803                 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1804                                 w->sname,
1805                                 w->active ? "active" : "inactive");
1806
1807         list_for_each_entry(p, &w->sources, list_sink) {
1808                 if (p->connected && !p->connected(w, p->sink))
1809                         continue;
1810
1811                 if (p->connect)
1812                         ret += snprintf(buf + ret, PAGE_SIZE - ret,
1813                                         " in  \"%s\" \"%s\"\n",
1814                                         p->name ? p->name : "static",
1815                                         p->source->name);
1816         }
1817         list_for_each_entry(p, &w->sinks, list_source) {
1818                 if (p->connected && !p->connected(w, p->sink))
1819                         continue;
1820
1821                 if (p->connect)
1822                         ret += snprintf(buf + ret, PAGE_SIZE - ret,
1823                                         " out \"%s\" \"%s\"\n",
1824                                         p->name ? p->name : "static",
1825                                         p->sink->name);
1826         }
1827
1828         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1829
1830         kfree(buf);
1831         return ret;
1832 }
1833
1834 static const struct file_operations dapm_widget_power_fops = {
1835         .open = simple_open,
1836         .read = dapm_widget_power_read_file,
1837         .llseek = default_llseek,
1838 };
1839
1840 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1841                                    size_t count, loff_t *ppos)
1842 {
1843         struct snd_soc_dapm_context *dapm = file->private_data;
1844         char *level;
1845
1846         switch (dapm->bias_level) {
1847         case SND_SOC_BIAS_ON:
1848                 level = "On\n";
1849                 break;
1850         case SND_SOC_BIAS_PREPARE:
1851                 level = "Prepare\n";
1852                 break;
1853         case SND_SOC_BIAS_STANDBY:
1854                 level = "Standby\n";
1855                 break;
1856         case SND_SOC_BIAS_OFF:
1857                 level = "Off\n";
1858                 break;
1859         default:
1860                 BUG();
1861                 level = "Unknown\n";
1862                 break;
1863         }
1864
1865         return simple_read_from_buffer(user_buf, count, ppos, level,
1866                                        strlen(level));
1867 }
1868
1869 static const struct file_operations dapm_bias_fops = {
1870         .open = simple_open,
1871         .read = dapm_bias_read_file,
1872         .llseek = default_llseek,
1873 };
1874
1875 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1876         struct dentry *parent)
1877 {
1878         struct dentry *d;
1879
1880         dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1881
1882         if (!dapm->debugfs_dapm) {
1883                 dev_warn(dapm->dev,
1884                        "ASoC: Failed to create DAPM debugfs directory\n");
1885                 return;
1886         }
1887
1888         d = debugfs_create_file("bias_level", 0444,
1889                                 dapm->debugfs_dapm, dapm,
1890                                 &dapm_bias_fops);
1891         if (!d)
1892                 dev_warn(dapm->dev,
1893                          "ASoC: Failed to create bias level debugfs file\n");
1894 }
1895
1896 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1897 {
1898         struct snd_soc_dapm_context *dapm = w->dapm;
1899         struct dentry *d;
1900
1901         if (!dapm->debugfs_dapm || !w->name)
1902                 return;
1903
1904         d = debugfs_create_file(w->name, 0444,
1905                                 dapm->debugfs_dapm, w,
1906                                 &dapm_widget_power_fops);
1907         if (!d)
1908                 dev_warn(w->dapm->dev,
1909                         "ASoC: Failed to create %s debugfs file\n",
1910                         w->name);
1911 }
1912
1913 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1914 {
1915         debugfs_remove_recursive(dapm->debugfs_dapm);
1916 }
1917
1918 #else
1919 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1920         struct dentry *parent)
1921 {
1922 }
1923
1924 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1925 {
1926 }
1927
1928 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1929 {
1930 }
1931
1932 #endif
1933
1934 /* test and update the power status of a mux widget */
1935 static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1936                                  struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1937 {
1938         struct snd_soc_dapm_path *path;
1939         int found = 0;
1940
1941         if (widget->id != snd_soc_dapm_mux &&
1942             widget->id != snd_soc_dapm_virt_mux &&
1943             widget->id != snd_soc_dapm_value_mux)
1944                 return -ENODEV;
1945
1946         /* find dapm widget path assoc with kcontrol */
1947         list_for_each_entry(path, &widget->dapm->card->paths, list) {
1948                 if (path->kcontrol != kcontrol)
1949                         continue;
1950
1951                 if (!path->name || !e->texts[mux])
1952                         continue;
1953
1954                 found = 1;
1955                 /* we now need to match the string in the enum to the path */
1956                 if (!(strcmp(path->name, e->texts[mux]))) {
1957                         path->connect = 1; /* new connection */
1958                         dapm_mark_dirty(path->source, "mux connection");
1959                 } else {
1960                         if (path->connect)
1961                                 dapm_mark_dirty(path->source,
1962                                                 "mux disconnection");
1963                         path->connect = 0; /* old connection must be powered down */
1964                 }
1965         }
1966
1967         if (found) {
1968                 dapm_mark_dirty(widget, "mux change");
1969                 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1970         }
1971
1972         return found;
1973 }
1974
1975 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1976                 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1977 {
1978         struct snd_soc_card *card = widget->dapm->card;
1979         int ret;
1980
1981         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1982         ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1983         mutex_unlock(&card->dapm_mutex);
1984         if (ret > 0)
1985                 soc_dpcm_runtime_update(widget);
1986         return ret;
1987 }
1988 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
1989
1990 /* test and update the power status of a mixer or switch widget */
1991 static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1992                                    struct snd_kcontrol *kcontrol, int connect)
1993 {
1994         struct snd_soc_dapm_path *path;
1995         int found = 0;
1996
1997         if (widget->id != snd_soc_dapm_mixer &&
1998             widget->id != snd_soc_dapm_mixer_named_ctl &&
1999             widget->id != snd_soc_dapm_switch)
2000                 return -ENODEV;
2001
2002         /* find dapm widget path assoc with kcontrol */
2003         list_for_each_entry(path, &widget->dapm->card->paths, list) {
2004                 if (path->kcontrol != kcontrol)
2005                         continue;
2006
2007                 /* found, now check type */
2008                 found = 1;
2009                 path->connect = connect;
2010                 dapm_mark_dirty(path->source, "mixer connection");
2011         }
2012
2013         if (found) {
2014                 dapm_mark_dirty(widget, "mixer update");
2015                 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
2016         }
2017
2018         return found;
2019 }
2020
2021 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
2022                                 struct snd_kcontrol *kcontrol, int connect)
2023 {
2024         struct snd_soc_card *card = widget->dapm->card;
2025         int ret;
2026
2027         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2028         ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
2029         mutex_unlock(&card->dapm_mutex);
2030         if (ret > 0)
2031                 soc_dpcm_runtime_update(widget);
2032         return ret;
2033 }
2034 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2035
2036 /* show dapm widget status in sys fs */
2037 static ssize_t dapm_widget_show(struct device *dev,
2038         struct device_attribute *attr, char *buf)
2039 {
2040         struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2041         struct snd_soc_codec *codec =rtd->codec;
2042         struct snd_soc_dapm_widget *w;
2043         int count = 0;
2044         char *state = "not set";
2045
2046         list_for_each_entry(w, &codec->card->widgets, list) {
2047                 if (w->dapm != &codec->dapm)
2048                         continue;
2049
2050                 /* only display widgets that burnm power */
2051                 switch (w->id) {
2052                 case snd_soc_dapm_hp:
2053                 case snd_soc_dapm_mic:
2054                 case snd_soc_dapm_spk:
2055                 case snd_soc_dapm_line:
2056                 case snd_soc_dapm_micbias:
2057                 case snd_soc_dapm_dac:
2058                 case snd_soc_dapm_adc:
2059                 case snd_soc_dapm_pga:
2060                 case snd_soc_dapm_out_drv:
2061                 case snd_soc_dapm_mixer:
2062                 case snd_soc_dapm_mixer_named_ctl:
2063                 case snd_soc_dapm_supply:
2064                 case snd_soc_dapm_regulator_supply:
2065                 case snd_soc_dapm_clock_supply:
2066                         if (w->name)
2067                                 count += sprintf(buf + count, "%s: %s\n",
2068                                         w->name, w->power ? "On":"Off");
2069                 break;
2070                 default:
2071                 break;
2072                 }
2073         }
2074
2075         switch (codec->dapm.bias_level) {
2076         case SND_SOC_BIAS_ON:
2077                 state = "On";
2078                 break;
2079         case SND_SOC_BIAS_PREPARE:
2080                 state = "Prepare";
2081                 break;
2082         case SND_SOC_BIAS_STANDBY:
2083                 state = "Standby";
2084                 break;
2085         case SND_SOC_BIAS_OFF:
2086                 state = "Off";
2087                 break;
2088         }
2089         count += sprintf(buf + count, "PM State: %s\n", state);
2090
2091         return count;
2092 }
2093
2094 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2095
2096 int snd_soc_dapm_sys_add(struct device *dev)
2097 {
2098         return device_create_file(dev, &dev_attr_dapm_widget);
2099 }
2100
2101 static void snd_soc_dapm_sys_remove(struct device *dev)
2102 {
2103         device_remove_file(dev, &dev_attr_dapm_widget);
2104 }
2105
2106 /* free all dapm widgets and resources */
2107 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2108 {
2109         struct snd_soc_dapm_widget *w, *next_w;
2110         struct snd_soc_dapm_path *p, *next_p;
2111
2112         list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2113                 if (w->dapm != dapm)
2114                         continue;
2115                 list_del(&w->list);
2116                 /*
2117                  * remove source and sink paths associated to this widget.
2118                  * While removing the path, remove reference to it from both
2119                  * source and sink widgets so that path is removed only once.
2120                  */
2121                 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
2122                         list_del(&p->list_sink);
2123                         list_del(&p->list_source);
2124                         list_del(&p->list);
2125                         kfree(p->long_name);
2126                         kfree(p);
2127                 }
2128                 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
2129                         list_del(&p->list_sink);
2130                         list_del(&p->list_source);
2131                         list_del(&p->list);
2132                         kfree(p->long_name);
2133                         kfree(p);
2134                 }
2135                 kfree(w->kcontrols);
2136                 kfree(w->name);
2137                 kfree(w);
2138         }
2139 }
2140
2141 static struct snd_soc_dapm_widget *dapm_find_widget(
2142                         struct snd_soc_dapm_context *dapm, const char *pin,
2143                         bool search_other_contexts)
2144 {
2145         struct snd_soc_dapm_widget *w;
2146         struct snd_soc_dapm_widget *fallback = NULL;
2147
2148         list_for_each_entry(w, &dapm->card->widgets, list) {
2149                 if (!strcmp(w->name, pin)) {
2150                         if (w->dapm == dapm)
2151                                 return w;
2152                         else
2153                                 fallback = w;
2154                 }
2155         }
2156
2157         if (search_other_contexts)
2158                 return fallback;
2159
2160         return NULL;
2161 }
2162
2163 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2164                                 const char *pin, int status)
2165 {
2166         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2167
2168         if (!w) {
2169                 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2170                 return -EINVAL;
2171         }
2172
2173         if (w->connected != status)
2174                 dapm_mark_dirty(w, "pin configuration");
2175
2176         w->connected = status;
2177         if (status == 0)
2178                 w->force = 0;
2179
2180         return 0;
2181 }
2182
2183 /**
2184  * snd_soc_dapm_sync - scan and power dapm paths
2185  * @dapm: DAPM context
2186  *
2187  * Walks all dapm audio paths and powers widgets according to their
2188  * stream or path usage.
2189  *
2190  * Returns 0 for success.
2191  */
2192 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2193 {
2194         int ret;
2195
2196         /*
2197          * Suppress early reports (eg, jacks syncing their state) to avoid
2198          * silly DAPM runs during card startup.
2199          */
2200         if (!dapm->card || !dapm->card->instantiated)
2201                 return 0;
2202
2203         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2204         ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2205         mutex_unlock(&dapm->card->dapm_mutex);
2206         return ret;
2207 }
2208 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2209
2210 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2211                                   const struct snd_soc_dapm_route *route)
2212 {
2213         struct snd_soc_dapm_path *path;
2214         struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2215         struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2216         const char *sink;
2217         const char *control = route->control;
2218         const char *source;
2219         char prefixed_sink[80];
2220         char prefixed_source[80];
2221         int ret = 0;
2222
2223         if (dapm->codec && dapm->codec->name_prefix) {
2224                 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2225                          dapm->codec->name_prefix, route->sink);
2226                 sink = prefixed_sink;
2227                 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2228                          dapm->codec->name_prefix, route->source);
2229                 source = prefixed_source;
2230         } else {
2231                 sink = route->sink;
2232                 source = route->source;
2233         }
2234
2235         /*
2236          * find src and dest widgets over all widgets but favor a widget from
2237          * current DAPM context
2238          */
2239         list_for_each_entry(w, &dapm->card->widgets, list) {
2240                 if (!wsink && !(strcmp(w->name, sink))) {
2241                         wtsink = w;
2242                         if (w->dapm == dapm)
2243                                 wsink = w;
2244                         continue;
2245                 }
2246                 if (!wsource && !(strcmp(w->name, source))) {
2247                         wtsource = w;
2248                         if (w->dapm == dapm)
2249                                 wsource = w;
2250                 }
2251         }
2252         /* use widget from another DAPM context if not found from this */
2253         if (!wsink)
2254                 wsink = wtsink;
2255         if (!wsource)
2256                 wsource = wtsource;
2257
2258         if (wsource == NULL) {
2259                 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2260                         route->source);
2261                 return -ENODEV;
2262         }
2263         if (wsink == NULL) {
2264                 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2265                         route->sink);
2266                 return -ENODEV;
2267         }
2268
2269         path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2270         if (!path)
2271                 return -ENOMEM;
2272
2273         path->source = wsource;
2274         path->sink = wsink;
2275         path->connected = route->connected;
2276         INIT_LIST_HEAD(&path->list);
2277         INIT_LIST_HEAD(&path->list_source);
2278         INIT_LIST_HEAD(&path->list_sink);
2279
2280         /* check for external widgets */
2281         if (wsink->id == snd_soc_dapm_input) {
2282                 if (wsource->id == snd_soc_dapm_micbias ||
2283                         wsource->id == snd_soc_dapm_mic ||
2284                         wsource->id == snd_soc_dapm_line ||
2285                         wsource->id == snd_soc_dapm_output)
2286                         wsink->ext = 1;
2287         }
2288         if (wsource->id == snd_soc_dapm_output) {
2289                 if (wsink->id == snd_soc_dapm_spk ||
2290                         wsink->id == snd_soc_dapm_hp ||
2291                         wsink->id == snd_soc_dapm_line ||
2292                         wsink->id == snd_soc_dapm_input)
2293                         wsource->ext = 1;
2294         }
2295
2296         /* connect static paths */
2297         if (control == NULL) {
2298                 list_add(&path->list, &dapm->card->paths);
2299                 list_add(&path->list_sink, &wsink->sources);
2300                 list_add(&path->list_source, &wsource->sinks);
2301                 path->connect = 1;
2302                 return 0;
2303         }
2304
2305         /* connect dynamic paths */
2306         switch (wsink->id) {
2307         case snd_soc_dapm_adc:
2308         case snd_soc_dapm_dac:
2309         case snd_soc_dapm_pga:
2310         case snd_soc_dapm_out_drv:
2311         case snd_soc_dapm_input:
2312         case snd_soc_dapm_output:
2313         case snd_soc_dapm_siggen:
2314         case snd_soc_dapm_micbias:
2315         case snd_soc_dapm_vmid:
2316         case snd_soc_dapm_pre:
2317         case snd_soc_dapm_post:
2318         case snd_soc_dapm_supply:
2319         case snd_soc_dapm_regulator_supply:
2320         case snd_soc_dapm_clock_supply:
2321         case snd_soc_dapm_aif_in:
2322         case snd_soc_dapm_aif_out:
2323         case snd_soc_dapm_dai:
2324         case snd_soc_dapm_dai_link:
2325                 list_add(&path->list, &dapm->card->paths);
2326                 list_add(&path->list_sink, &wsink->sources);
2327                 list_add(&path->list_source, &wsource->sinks);
2328                 path->connect = 1;
2329                 return 0;
2330         case snd_soc_dapm_mux:
2331         case snd_soc_dapm_virt_mux:
2332         case snd_soc_dapm_value_mux:
2333                 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2334                         &wsink->kcontrol_news[0]);
2335                 if (ret != 0)
2336                         goto err;
2337                 break;
2338         case snd_soc_dapm_switch:
2339         case snd_soc_dapm_mixer:
2340         case snd_soc_dapm_mixer_named_ctl:
2341                 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2342                 if (ret != 0)
2343                         goto err;
2344                 break;
2345         case snd_soc_dapm_hp:
2346         case snd_soc_dapm_mic:
2347         case snd_soc_dapm_line:
2348         case snd_soc_dapm_spk:
2349                 list_add(&path->list, &dapm->card->paths);
2350                 list_add(&path->list_sink, &wsink->sources);
2351                 list_add(&path->list_source, &wsource->sinks);
2352                 path->connect = 0;
2353                 return 0;
2354         }
2355
2356         dapm_mark_dirty(wsource, "Route added");
2357         dapm_mark_dirty(wsink, "Route added");
2358
2359         return 0;
2360
2361 err:
2362         dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2363                  source, control, sink);
2364         kfree(path);
2365         return ret;
2366 }
2367
2368 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2369                                   const struct snd_soc_dapm_route *route)
2370 {
2371         struct snd_soc_dapm_path *path, *p;
2372         const char *sink;
2373         const char *source;
2374         char prefixed_sink[80];
2375         char prefixed_source[80];
2376
2377         if (route->control) {
2378                 dev_err(dapm->dev,
2379                         "ASoC: Removal of routes with controls not supported\n");
2380                 return -EINVAL;
2381         }
2382
2383         if (dapm->codec && dapm->codec->name_prefix) {
2384                 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2385                          dapm->codec->name_prefix, route->sink);
2386                 sink = prefixed_sink;
2387                 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2388                          dapm->codec->name_prefix, route->source);
2389                 source = prefixed_source;
2390         } else {
2391                 sink = route->sink;
2392                 source = route->source;
2393         }
2394
2395         path = NULL;
2396         list_for_each_entry(p, &dapm->card->paths, list) {
2397                 if (strcmp(p->source->name, source) != 0)
2398                         continue;
2399                 if (strcmp(p->sink->name, sink) != 0)
2400                         continue;
2401                 path = p;
2402                 break;
2403         }
2404
2405         if (path) {
2406                 dapm_mark_dirty(path->source, "Route removed");
2407                 dapm_mark_dirty(path->sink, "Route removed");
2408
2409                 list_del(&path->list);
2410                 list_del(&path->list_sink);
2411                 list_del(&path->list_source);
2412                 kfree(path);
2413         } else {
2414                 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2415                          source, sink);
2416         }
2417
2418         return 0;
2419 }
2420
2421 /**
2422  * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2423  * @dapm: DAPM context
2424  * @route: audio routes
2425  * @num: number of routes
2426  *
2427  * Connects 2 dapm widgets together via a named audio path. The sink is
2428  * the widget receiving the audio signal, whilst the source is the sender
2429  * of the audio signal.
2430  *
2431  * Returns 0 for success else error. On error all resources can be freed
2432  * with a call to snd_soc_card_free().
2433  */
2434 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2435                             const struct snd_soc_dapm_route *route, int num)
2436 {
2437         int i, r, ret = 0;
2438
2439         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2440         for (i = 0; i < num; i++) {
2441                 r = snd_soc_dapm_add_route(dapm, route);
2442                 if (r < 0) {
2443                         dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2444                                 route->source,
2445                                 route->control ? route->control : "direct",
2446                                 route->sink);
2447                         ret = r;
2448                 }
2449                 route++;
2450         }
2451         mutex_unlock(&dapm->card->dapm_mutex);
2452
2453         return ret;
2454 }
2455 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2456
2457 /**
2458  * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2459  * @dapm: DAPM context
2460  * @route: audio routes
2461  * @num: number of routes
2462  *
2463  * Removes routes from the DAPM context.
2464  */
2465 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2466                             const struct snd_soc_dapm_route *route, int num)
2467 {
2468         int i, ret = 0;
2469
2470         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2471         for (i = 0; i < num; i++) {
2472                 snd_soc_dapm_del_route(dapm, route);
2473                 route++;
2474         }
2475         mutex_unlock(&dapm->card->dapm_mutex);
2476
2477         return ret;
2478 }
2479 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2480
2481 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2482                                    const struct snd_soc_dapm_route *route)
2483 {
2484         struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2485                                                               route->source,
2486                                                               true);
2487         struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2488                                                             route->sink,
2489                                                             true);
2490         struct snd_soc_dapm_path *path;
2491         int count = 0;
2492
2493         if (!source) {
2494                 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2495                         route->source);
2496                 return -ENODEV;
2497         }
2498
2499         if (!sink) {
2500                 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2501                         route->sink);
2502                 return -ENODEV;
2503         }
2504
2505         if (route->control || route->connected)
2506                 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2507                          route->source, route->sink);
2508
2509         list_for_each_entry(path, &source->sinks, list_source) {
2510                 if (path->sink == sink) {
2511                         path->weak = 1;
2512                         count++;
2513                 }
2514         }
2515
2516         if (count == 0)
2517                 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2518                         route->source, route->sink);
2519         if (count > 1)
2520                 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2521                          count, route->source, route->sink);
2522
2523         return 0;
2524 }
2525
2526 /**
2527  * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2528  * @dapm: DAPM context
2529  * @route: audio routes
2530  * @num: number of routes
2531  *
2532  * Mark existing routes matching those specified in the passed array
2533  * as being weak, meaning that they are ignored for the purpose of
2534  * power decisions.  The main intended use case is for sidetone paths
2535  * which couple audio between other independent paths if they are both
2536  * active in order to make the combination work better at the user
2537  * level but which aren't intended to be "used".
2538  *
2539  * Note that CODEC drivers should not use this as sidetone type paths
2540  * can frequently also be used as bypass paths.
2541  */
2542 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2543                              const struct snd_soc_dapm_route *route, int num)
2544 {
2545         int i, err;
2546         int ret = 0;
2547
2548         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2549         for (i = 0; i < num; i++) {
2550                 err = snd_soc_dapm_weak_route(dapm, route);
2551                 if (err)
2552                         ret = err;
2553                 route++;
2554         }
2555         mutex_unlock(&dapm->card->dapm_mutex);
2556
2557         return ret;
2558 }
2559 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2560
2561 /**
2562  * snd_soc_dapm_new_widgets - add new dapm widgets
2563  * @dapm: DAPM context
2564  *
2565  * Checks the codec for any new dapm widgets and creates them if found.
2566  *
2567  * Returns 0 for success.
2568  */
2569 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2570 {
2571         struct snd_soc_dapm_widget *w;
2572         unsigned int val;
2573
2574         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2575
2576         list_for_each_entry(w, &dapm->card->widgets, list)
2577         {
2578                 if (w->new)
2579                         continue;
2580
2581                 if (w->num_kcontrols) {
2582                         w->kcontrols = kzalloc(w->num_kcontrols *
2583                                                 sizeof(struct snd_kcontrol *),
2584                                                 GFP_KERNEL);
2585                         if (!w->kcontrols) {
2586                                 mutex_unlock(&dapm->card->dapm_mutex);
2587                                 return -ENOMEM;
2588                         }
2589                 }
2590
2591                 switch(w->id) {
2592                 case snd_soc_dapm_switch:
2593                 case snd_soc_dapm_mixer:
2594                 case snd_soc_dapm_mixer_named_ctl:
2595                         dapm_new_mixer(w);
2596                         break;
2597                 case snd_soc_dapm_mux:
2598                 case snd_soc_dapm_virt_mux:
2599                 case snd_soc_dapm_value_mux:
2600                         dapm_new_mux(w);
2601                         break;
2602                 case snd_soc_dapm_pga:
2603                 case snd_soc_dapm_out_drv:
2604                         dapm_new_pga(w);
2605                         break;
2606                 default:
2607                         break;
2608                 }
2609
2610                 /* Read the initial power state from the device */
2611                 if (w->reg >= 0) {
2612                         val = soc_widget_read(w, w->reg);
2613                         val &= 1 << w->shift;
2614                         if (w->invert)
2615                                 val = !val;
2616
2617                         if (val)
2618                                 w->power = 1;
2619                 }
2620
2621                 w->new = 1;
2622
2623                 dapm_mark_dirty(w, "new widget");
2624                 dapm_debugfs_add_widget(w);
2625         }
2626
2627         dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2628         mutex_unlock(&dapm->card->dapm_mutex);
2629         return 0;
2630 }
2631 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2632
2633 /**
2634  * snd_soc_dapm_get_volsw - dapm mixer get callback
2635  * @kcontrol: mixer control
2636  * @ucontrol: control element information
2637  *
2638  * Callback to get the value of a dapm mixer control.
2639  *
2640  * Returns 0 for success.
2641  */
2642 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2643         struct snd_ctl_elem_value *ucontrol)
2644 {
2645         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2646         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2647         struct soc_mixer_control *mc =
2648                 (struct soc_mixer_control *)kcontrol->private_value;
2649         unsigned int reg = mc->reg;
2650         unsigned int shift = mc->shift;
2651         int max = mc->max;
2652         unsigned int mask = (1 << fls(max)) - 1;
2653         unsigned int invert = mc->invert;
2654
2655         if (snd_soc_volsw_is_stereo(mc))
2656                 dev_warn(widget->dapm->dev,
2657                          "ASoC: Control '%s' is stereo, which is not supported\n",
2658                          kcontrol->id.name);
2659
2660         ucontrol->value.integer.value[0] =
2661                 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2662         if (invert)
2663                 ucontrol->value.integer.value[0] =
2664                         max - ucontrol->value.integer.value[0];
2665
2666         return 0;
2667 }
2668 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2669
2670 /**
2671  * snd_soc_dapm_put_volsw - dapm mixer set callback
2672  * @kcontrol: mixer control
2673  * @ucontrol: control element information
2674  *
2675  * Callback to set the value of a dapm mixer control.
2676  *
2677  * Returns 0 for success.
2678  */
2679 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2680         struct snd_ctl_elem_value *ucontrol)
2681 {
2682         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2683         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2684         struct snd_soc_codec *codec = widget->codec;
2685         struct snd_soc_card *card = codec->card;
2686         struct soc_mixer_control *mc =
2687                 (struct soc_mixer_control *)kcontrol->private_value;
2688         unsigned int reg = mc->reg;
2689         unsigned int shift = mc->shift;
2690         int max = mc->max;
2691         unsigned int mask = (1 << fls(max)) - 1;
2692         unsigned int invert = mc->invert;
2693         unsigned int val;
2694         int connect, change;
2695         struct snd_soc_dapm_update update;
2696         int wi;
2697
2698         if (snd_soc_volsw_is_stereo(mc))
2699                 dev_warn(widget->dapm->dev,
2700                          "ASoC: Control '%s' is stereo, which is not supported\n",
2701                          kcontrol->id.name);
2702
2703         val = (ucontrol->value.integer.value[0] & mask);
2704         connect = !!val;
2705
2706         if (invert)
2707                 val = max - val;
2708         mask = mask << shift;
2709         val = val << shift;
2710
2711         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2712
2713         change = snd_soc_test_bits(widget->codec, reg, mask, val);
2714         if (change) {
2715                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2716                         widget = wlist->widgets[wi];
2717
2718                         widget->value = val;
2719
2720                         update.kcontrol = kcontrol;
2721                         update.widget = widget;
2722                         update.reg = reg;
2723                         update.mask = mask;
2724                         update.val = val;
2725                         widget->dapm->update = &update;
2726
2727                         soc_dapm_mixer_update_power(widget, kcontrol, connect);
2728
2729                         widget->dapm->update = NULL;
2730                 }
2731         }
2732
2733         mutex_unlock(&card->dapm_mutex);
2734         return 0;
2735 }
2736 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2737
2738 /**
2739  * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2740  * @kcontrol: mixer control
2741  * @ucontrol: control element information
2742  *
2743  * Callback to get the value of a dapm enumerated double mixer control.
2744  *
2745  * Returns 0 for success.
2746  */
2747 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2748         struct snd_ctl_elem_value *ucontrol)
2749 {
2750         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2751         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2752         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2753         unsigned int val;
2754
2755         val = snd_soc_read(widget->codec, e->reg);
2756         ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
2757         if (e->shift_l != e->shift_r)
2758                 ucontrol->value.enumerated.item[1] =
2759                         (val >> e->shift_r) & e->mask;
2760
2761         return 0;
2762 }
2763 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2764
2765 /**
2766  * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2767  * @kcontrol: mixer control
2768  * @ucontrol: control element information
2769  *
2770  * Callback to set the value of a dapm enumerated double mixer control.
2771  *
2772  * Returns 0 for success.
2773  */
2774 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2775         struct snd_ctl_elem_value *ucontrol)
2776 {
2777         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2778         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2779         struct snd_soc_codec *codec = widget->codec;
2780         struct snd_soc_card *card = codec->card;
2781         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2782         unsigned int val, mux, change;
2783         unsigned int mask;
2784         struct snd_soc_dapm_update update;
2785         int wi;
2786
2787         if (ucontrol->value.enumerated.item[0] > e->max - 1)
2788                 return -EINVAL;
2789         mux = ucontrol->value.enumerated.item[0];
2790         val = mux << e->shift_l;
2791         mask = e->mask << e->shift_l;
2792         if (e->shift_l != e->shift_r) {
2793                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2794                         return -EINVAL;
2795                 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2796                 mask |= e->mask << e->shift_r;
2797         }
2798
2799         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2800
2801         change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2802         if (change) {
2803                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2804                         widget = wlist->widgets[wi];
2805
2806                         widget->value = val;
2807
2808                         update.kcontrol = kcontrol;
2809                         update.widget = widget;
2810                         update.reg = e->reg;
2811                         update.mask = mask;
2812                         update.val = val;
2813                         widget->dapm->update = &update;
2814
2815                         soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2816
2817                         widget->dapm->update = NULL;
2818                 }
2819         }
2820
2821         mutex_unlock(&card->dapm_mutex);
2822         return change;
2823 }
2824 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2825
2826 /**
2827  * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2828  * @kcontrol: mixer control
2829  * @ucontrol: control element information
2830  *
2831  * Returns 0 for success.
2832  */
2833 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2834                                struct snd_ctl_elem_value *ucontrol)
2835 {
2836         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2837         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2838
2839         ucontrol->value.enumerated.item[0] = widget->value;
2840
2841         return 0;
2842 }
2843 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2844
2845 /**
2846  * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2847  * @kcontrol: mixer control
2848  * @ucontrol: control element information
2849  *
2850  * Returns 0 for success.
2851  */
2852 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2853                                struct snd_ctl_elem_value *ucontrol)
2854 {
2855         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2856         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2857         struct snd_soc_codec *codec = widget->codec;
2858         struct snd_soc_card *card = codec->card;
2859         struct soc_enum *e =
2860                 (struct soc_enum *)kcontrol->private_value;
2861         int change;
2862         int ret = 0;
2863         int wi;
2864
2865         if (ucontrol->value.enumerated.item[0] >= e->max)
2866                 return -EINVAL;
2867
2868         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2869
2870         change = widget->value != ucontrol->value.enumerated.item[0];
2871         if (change) {
2872                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2873                         widget = wlist->widgets[wi];
2874
2875                         widget->value = ucontrol->value.enumerated.item[0];
2876
2877                         soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
2878                 }
2879         }
2880
2881         mutex_unlock(&card->dapm_mutex);
2882         return ret;
2883 }
2884 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2885
2886 /**
2887  * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2888  *                                      callback
2889  * @kcontrol: mixer control
2890  * @ucontrol: control element information
2891  *
2892  * Callback to get the value of a dapm semi enumerated double mixer control.
2893  *
2894  * Semi enumerated mixer: the enumerated items are referred as values. Can be
2895  * used for handling bitfield coded enumeration for example.
2896  *
2897  * Returns 0 for success.
2898  */
2899 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2900         struct snd_ctl_elem_value *ucontrol)
2901 {
2902         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2903         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2904         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2905         unsigned int reg_val, val, mux;
2906
2907         reg_val = snd_soc_read(widget->codec, e->reg);
2908         val = (reg_val >> e->shift_l) & e->mask;
2909         for (mux = 0; mux < e->max; mux++) {
2910                 if (val == e->values[mux])
2911                         break;
2912         }
2913         ucontrol->value.enumerated.item[0] = mux;
2914         if (e->shift_l != e->shift_r) {
2915                 val = (reg_val >> e->shift_r) & e->mask;
2916                 for (mux = 0; mux < e->max; mux++) {
2917                         if (val == e->values[mux])
2918                                 break;
2919                 }
2920                 ucontrol->value.enumerated.item[1] = mux;
2921         }
2922
2923         return 0;
2924 }
2925 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2926
2927 /**
2928  * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2929  *                                      callback
2930  * @kcontrol: mixer control
2931  * @ucontrol: control element information
2932  *
2933  * Callback to set the value of a dapm semi enumerated double mixer control.
2934  *
2935  * Semi enumerated mixer: the enumerated items are referred as values. Can be
2936  * used for handling bitfield coded enumeration for example.
2937  *
2938  * Returns 0 for success.
2939  */
2940 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2941         struct snd_ctl_elem_value *ucontrol)
2942 {
2943         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2944         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2945         struct snd_soc_codec *codec = widget->codec;
2946         struct snd_soc_card *card = codec->card;
2947         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2948         unsigned int val, mux, change;
2949         unsigned int mask;
2950         struct snd_soc_dapm_update update;
2951         int wi;
2952
2953         if (ucontrol->value.enumerated.item[0] > e->max - 1)
2954                 return -EINVAL;
2955         mux = ucontrol->value.enumerated.item[0];
2956         val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2957         mask = e->mask << e->shift_l;
2958         if (e->shift_l != e->shift_r) {
2959                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2960                         return -EINVAL;
2961                 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2962                 mask |= e->mask << e->shift_r;
2963         }
2964
2965         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2966
2967         change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2968         if (change) {
2969                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2970                         widget = wlist->widgets[wi];
2971
2972                         widget->value = val;
2973
2974                         update.kcontrol = kcontrol;
2975                         update.widget = widget;
2976                         update.reg = e->reg;
2977                         update.mask = mask;
2978                         update.val = val;
2979                         widget->dapm->update = &update;
2980
2981                         soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2982
2983                         widget->dapm->update = NULL;
2984                 }
2985         }
2986
2987         mutex_unlock(&card->dapm_mutex);
2988         return change;
2989 }
2990 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2991
2992 /**
2993  * snd_soc_dapm_info_pin_switch - Info for a pin switch
2994  *
2995  * @kcontrol: mixer control
2996  * @uinfo: control element information
2997  *
2998  * Callback to provide information about a pin switch control.
2999  */
3000 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3001                                  struct snd_ctl_elem_info *uinfo)
3002 {
3003         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3004         uinfo->count = 1;
3005         uinfo->value.integer.min = 0;
3006         uinfo->value.integer.max = 1;
3007
3008         return 0;
3009 }
3010 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3011
3012 /**
3013  * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3014  *
3015  * @kcontrol: mixer control
3016  * @ucontrol: Value
3017  */
3018 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3019                                 struct snd_ctl_elem_value *ucontrol)
3020 {
3021         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3022         const char *pin = (const char *)kcontrol->private_value;
3023
3024         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3025
3026         ucontrol->value.integer.value[0] =
3027                 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3028
3029         mutex_unlock(&card->dapm_mutex);
3030
3031         return 0;
3032 }
3033 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3034
3035 /**
3036  * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3037  *
3038  * @kcontrol: mixer control
3039  * @ucontrol: Value
3040  */
3041 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3042                                 struct snd_ctl_elem_value *ucontrol)
3043 {
3044         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3045         const char *pin = (const char *)kcontrol->private_value;
3046
3047         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3048
3049         if (ucontrol->value.integer.value[0])
3050                 snd_soc_dapm_enable_pin(&card->dapm, pin);
3051         else
3052                 snd_soc_dapm_disable_pin(&card->dapm, pin);
3053
3054         mutex_unlock(&card->dapm_mutex);
3055
3056         snd_soc_dapm_sync(&card->dapm);
3057         return 0;
3058 }
3059 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3060
3061 static struct snd_soc_dapm_widget *
3062 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3063                          const struct snd_soc_dapm_widget *widget)
3064 {
3065         struct snd_soc_dapm_widget *w;
3066         size_t name_len;
3067         int ret;
3068
3069         if ((w = dapm_cnew_widget(widget)) == NULL)
3070                 return NULL;
3071
3072         switch (w->id) {
3073         case snd_soc_dapm_regulator_supply:
3074                 w->regulator = devm_regulator_get(dapm->dev, w->name);
3075                 if (IS_ERR(w->regulator)) {
3076                         ret = PTR_ERR(w->regulator);
3077                         dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3078                                 w->name, ret);
3079                         return NULL;
3080                 }
3081
3082                 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
3083                         ret = regulator_allow_bypass(w->regulator, true);
3084                         if (ret != 0)
3085                                 dev_warn(w->dapm->dev,
3086                                          "ASoC: Failed to unbypass %s: %d\n",
3087                                          w->name, ret);
3088                 }
3089                 break;
3090         case snd_soc_dapm_clock_supply:
3091 #ifdef CONFIG_CLKDEV_LOOKUP
3092                 w->clk = devm_clk_get(dapm->dev, w->name);
3093                 if (IS_ERR(w->clk)) {
3094                         ret = PTR_ERR(w->clk);
3095                         dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3096                                 w->name, ret);
3097                         return NULL;
3098                 }
3099 #else
3100                 return NULL;
3101 #endif
3102                 break;
3103         default:
3104                 break;
3105         }
3106
3107         name_len = strlen(widget->name) + 1;
3108         if (dapm->codec && dapm->codec->name_prefix)
3109                 name_len += 1 + strlen(dapm->codec->name_prefix);
3110         w->name = kmalloc(name_len, GFP_KERNEL);
3111         if (w->name == NULL) {
3112                 kfree(w);
3113                 return NULL;
3114         }
3115         if (dapm->codec && dapm->codec->name_prefix)
3116                 snprintf((char *)w->name, name_len, "%s %s",
3117                         dapm->codec->name_prefix, widget->name);
3118         else
3119                 snprintf((char *)w->name, name_len, "%s", widget->name);
3120
3121         switch (w->id) {
3122         case snd_soc_dapm_switch:
3123         case snd_soc_dapm_mixer:
3124         case snd_soc_dapm_mixer_named_ctl:
3125                 w->power_check = dapm_generic_check_power;
3126                 break;
3127         case snd_soc_dapm_mux:
3128         case snd_soc_dapm_virt_mux:
3129         case snd_soc_dapm_value_mux:
3130                 w->power_check = dapm_generic_check_power;
3131                 break;
3132         case snd_soc_dapm_adc:
3133         case snd_soc_dapm_aif_out:
3134                 w->power_check = dapm_adc_check_power;
3135                 break;
3136         case snd_soc_dapm_dac:
3137         case snd_soc_dapm_aif_in:
3138                 w->power_check = dapm_dac_check_power;
3139                 break;
3140         case snd_soc_dapm_pga:
3141         case snd_soc_dapm_out_drv:
3142         case snd_soc_dapm_input:
3143         case snd_soc_dapm_output:
3144         case snd_soc_dapm_micbias:
3145         case snd_soc_dapm_spk:
3146         case snd_soc_dapm_hp:
3147         case snd_soc_dapm_mic:
3148         case snd_soc_dapm_line:
3149         case snd_soc_dapm_dai_link:
3150                 w->power_check = dapm_generic_check_power;
3151                 break;
3152         case snd_soc_dapm_supply:
3153         case snd_soc_dapm_regulator_supply:
3154         case snd_soc_dapm_clock_supply:
3155                 w->power_check = dapm_supply_check_power;
3156                 break;
3157         case snd_soc_dapm_dai:
3158                 w->power_check = dapm_dai_check_power;
3159                 break;
3160         default:
3161                 w->power_check = dapm_always_on_check_power;
3162                 break;
3163         }
3164
3165         w->dapm = dapm;
3166         w->codec = dapm->codec;
3167         w->platform = dapm->platform;
3168         INIT_LIST_HEAD(&w->sources);
3169         INIT_LIST_HEAD(&w->sinks);
3170         INIT_LIST_HEAD(&w->list);
3171         INIT_LIST_HEAD(&w->dirty);
3172         list_add(&w->list, &dapm->card->widgets);
3173
3174         /* machine layer set ups unconnected pins and insertions */
3175         w->connected = 1;
3176         return w;
3177 }
3178
3179 /**
3180  * snd_soc_dapm_new_controls - create new dapm controls
3181  * @dapm: DAPM context
3182  * @widget: widget array
3183  * @num: number of widgets
3184  *
3185  * Creates new DAPM controls based upon the templates.
3186  *
3187  * Returns 0 for success else error.
3188  */
3189 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3190         const struct snd_soc_dapm_widget *widget,
3191         int num)
3192 {
3193         struct snd_soc_dapm_widget *w;
3194         int i;
3195         int ret = 0;
3196
3197         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3198         for (i = 0; i < num; i++) {
3199                 w = snd_soc_dapm_new_control(dapm, widget);
3200                 if (!w) {
3201                         dev_err(dapm->dev,
3202                                 "ASoC: Failed to create DAPM control %s\n",
3203                                 widget->name);
3204                         ret = -ENOMEM;
3205                         break;
3206                 }
3207                 widget++;
3208         }
3209         mutex_unlock(&dapm->card->dapm_mutex);
3210         return ret;
3211 }
3212 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3213
3214 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3215                                   struct snd_kcontrol *kcontrol, int event)
3216 {
3217         struct snd_soc_dapm_path *source_p, *sink_p;
3218         struct snd_soc_dai *source, *sink;
3219         const struct snd_soc_pcm_stream *config = w->params;
3220         struct snd_pcm_substream substream;
3221         struct snd_pcm_hw_params *params = NULL;
3222         u64 fmt;
3223         int ret;
3224
3225         BUG_ON(!config);
3226         BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3227
3228         /* We only support a single source and sink, pick the first */
3229         source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3230                                     list_sink);
3231         sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3232                                   list_source);
3233
3234         BUG_ON(!source_p || !sink_p);
3235         BUG_ON(!sink_p->source || !source_p->sink);
3236         BUG_ON(!source_p->source || !sink_p->sink);
3237
3238         source = source_p->source->priv;
3239         sink = sink_p->sink->priv;
3240
3241         /* Be a little careful as we don't want to overflow the mask array */
3242         if (config->formats) {
3243                 fmt = ffs(config->formats) - 1;
3244         } else {
3245                 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3246                          config->formats);
3247                 fmt = 0;
3248         }
3249
3250         /* Currently very limited parameter selection */
3251         params = kzalloc(sizeof(*params), GFP_KERNEL);
3252         if (!params) {
3253                 ret = -ENOMEM;
3254                 goto out;
3255         }
3256         snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3257
3258         hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3259                 config->rate_min;
3260         hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3261                 config->rate_max;
3262
3263         hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3264                 = config->channels_min;
3265         hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3266                 = config->channels_max;
3267
3268         memset(&substream, 0, sizeof(substream));
3269
3270         switch (event) {
3271         case SND_SOC_DAPM_PRE_PMU:
3272                 if (source->driver->ops && source->driver->ops->hw_params) {
3273                         substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3274                         ret = source->driver->ops->hw_params(&substream,
3275                                                              params, source);
3276                         if (ret != 0) {
3277                                 dev_err(source->dev,
3278                                         "ASoC: hw_params() failed: %d\n", ret);
3279                                 goto out;
3280                         }
3281                 }
3282
3283                 if (sink->driver->ops && sink->driver->ops->hw_params) {
3284                         substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3285                         ret = sink->driver->ops->hw_params(&substream, params,
3286                                                            sink);
3287                         if (ret != 0) {
3288                                 dev_err(sink->dev,
3289                                         "ASoC: hw_params() failed: %d\n", ret);
3290                                 goto out;
3291                         }
3292                 }
3293                 break;
3294
3295         case SND_SOC_DAPM_POST_PMU:
3296                 ret = snd_soc_dai_digital_mute(sink, 0,
3297                                                SNDRV_PCM_STREAM_PLAYBACK);
3298                 if (ret != 0 && ret != -ENOTSUPP)
3299                         dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3300                 ret = 0;
3301                 break;
3302
3303         case SND_SOC_DAPM_PRE_PMD:
3304                 ret = snd_soc_dai_digital_mute(sink, 1,
3305                                                SNDRV_PCM_STREAM_PLAYBACK);
3306                 if (ret != 0 && ret != -ENOTSUPP)
3307                         dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3308                 ret = 0;
3309                 break;
3310
3311         default:
3312                 BUG();
3313                 return -EINVAL;
3314         }
3315
3316 out:
3317         kfree(params);
3318         return ret;
3319 }
3320
3321 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3322                          const struct snd_soc_pcm_stream *params,
3323                          struct snd_soc_dapm_widget *source,
3324                          struct snd_soc_dapm_widget *sink)
3325 {
3326         struct snd_soc_dapm_route routes[2];
3327         struct snd_soc_dapm_widget template;
3328         struct snd_soc_dapm_widget *w;
3329         size_t len;
3330         char *link_name;
3331
3332         len = strlen(source->name) + strlen(sink->name) + 2;
3333         link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3334         if (!link_name)
3335                 return -ENOMEM;
3336         snprintf(link_name, len, "%s-%s", source->name, sink->name);
3337
3338         memset(&template, 0, sizeof(template));
3339         template.reg = SND_SOC_NOPM;
3340         template.id = snd_soc_dapm_dai_link;
3341         template.name = link_name;
3342         template.event = snd_soc_dai_link_event;
3343         template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3344                 SND_SOC_DAPM_PRE_PMD;
3345
3346         dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3347
3348         w = snd_soc_dapm_new_control(&card->dapm, &template);
3349         if (!w) {
3350                 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3351                         link_name);
3352                 return -ENOMEM;
3353         }
3354
3355         w->params = params;
3356
3357         memset(&routes, 0, sizeof(routes));
3358
3359         routes[0].source = source->name;
3360         routes[0].sink = link_name;
3361         routes[1].source = link_name;
3362         routes[1].sink = sink->name;
3363
3364         return snd_soc_dapm_add_routes(&card->dapm, routes,
3365                                        ARRAY_SIZE(routes));
3366 }
3367
3368 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3369                                  struct snd_soc_dai *dai)
3370 {
3371         struct snd_soc_dapm_widget template;
3372         struct snd_soc_dapm_widget *w;
3373
3374         WARN_ON(dapm->dev != dai->dev);
3375
3376         memset(&template, 0, sizeof(template));
3377         template.reg = SND_SOC_NOPM;
3378
3379         if (dai->driver->playback.stream_name) {
3380                 template.id = snd_soc_dapm_dai;
3381                 template.name = dai->driver->playback.stream_name;
3382                 template.sname = dai->driver->playback.stream_name;
3383
3384                 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3385                         template.name);
3386
3387                 w = snd_soc_dapm_new_control(dapm, &template);
3388                 if (!w) {
3389                         dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3390                                 dai->driver->playback.stream_name);
3391                 }
3392
3393                 w->priv = dai;
3394                 dai->playback_widget = w;
3395         }
3396
3397         if (dai->driver->capture.stream_name) {
3398                 template.id = snd_soc_dapm_dai;
3399                 template.name = dai->driver->capture.stream_name;
3400                 template.sname = dai->driver->capture.stream_name;
3401
3402                 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3403                         template.name);
3404
3405                 w = snd_soc_dapm_new_control(dapm, &template);
3406                 if (!w) {
3407                         dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3408                                 dai->driver->capture.stream_name);
3409                 }
3410
3411                 w->priv = dai;
3412                 dai->capture_widget = w;
3413         }
3414
3415         return 0;
3416 }
3417
3418 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3419 {
3420         struct snd_soc_dapm_widget *dai_w, *w;
3421         struct snd_soc_dai *dai;
3422         struct snd_soc_dapm_route r;
3423
3424         memset(&r, 0, sizeof(r));
3425
3426         /* For each DAI widget... */
3427         list_for_each_entry(dai_w, &card->widgets, list) {
3428                 if (dai_w->id != snd_soc_dapm_dai)
3429                         continue;
3430
3431                 dai = dai_w->priv;
3432
3433                 /* ...find all widgets with the same stream and link them */
3434                 list_for_each_entry(w, &card->widgets, list) {
3435                         if (w->dapm != dai_w->dapm)
3436                                 continue;
3437
3438                         if (w->id == snd_soc_dapm_dai)
3439                                 continue;
3440
3441                         if (!w->sname)
3442                                 continue;
3443
3444                         if (dai->driver->playback.stream_name &&
3445                             strstr(w->sname,
3446                                    dai->driver->playback.stream_name)) {
3447                                 r.source = dai->playback_widget->name;
3448                                 r.sink = w->name;
3449                                 dev_dbg(dai->dev, "%s -> %s\n",
3450                                          r.source, r.sink);
3451
3452                                 snd_soc_dapm_add_route(w->dapm, &r);
3453                         }
3454
3455                         if (dai->driver->capture.stream_name &&
3456                             strstr(w->sname,
3457                                    dai->driver->capture.stream_name)) {
3458                                 r.source = w->name;
3459                                 r.sink = dai->capture_widget->name;
3460                                 dev_dbg(dai->dev, "%s -> %s\n",
3461                                         r.source, r.sink);
3462
3463                                 snd_soc_dapm_add_route(w->dapm, &r);
3464                         }
3465                 }
3466         }
3467
3468         return 0;
3469 }
3470
3471 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3472         int event)
3473 {
3474
3475         struct snd_soc_dapm_widget *w_cpu, *w_codec;
3476         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3477         struct snd_soc_dai *codec_dai = rtd->codec_dai;
3478
3479         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3480                 w_cpu = cpu_dai->playback_widget;
3481                 w_codec = codec_dai->playback_widget;
3482         } else {
3483                 w_cpu = cpu_dai->capture_widget;
3484                 w_codec = codec_dai->capture_widget;
3485         }
3486
3487         if (w_cpu) {
3488
3489                 dapm_mark_dirty(w_cpu, "stream event");
3490
3491                 switch (event) {
3492                 case SND_SOC_DAPM_STREAM_START:
3493                         w_cpu->active = 1;
3494                         break;
3495                 case SND_SOC_DAPM_STREAM_STOP:
3496                         w_cpu->active = 0;
3497                         break;
3498                 case SND_SOC_DAPM_STREAM_SUSPEND:
3499                 case SND_SOC_DAPM_STREAM_RESUME:
3500                 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3501                 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3502                         break;
3503                 }
3504         }
3505
3506         if (w_codec) {
3507
3508                 dapm_mark_dirty(w_codec, "stream event");
3509
3510                 switch (event) {
3511                 case SND_SOC_DAPM_STREAM_START:
3512                         w_codec->active = 1;
3513                         break;
3514                 case SND_SOC_DAPM_STREAM_STOP:
3515                         w_codec->active = 0;
3516                         break;
3517                 case SND_SOC_DAPM_STREAM_SUSPEND:
3518                 case SND_SOC_DAPM_STREAM_RESUME:
3519                 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3520                 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3521                         break;
3522                 }
3523         }
3524
3525         dapm_power_widgets(&rtd->card->dapm, event);
3526 }
3527
3528 /**
3529  * snd_soc_dapm_stream_event - send a stream event to the dapm core
3530  * @rtd: PCM runtime data
3531  * @stream: stream name
3532  * @event: stream event
3533  *
3534  * Sends a stream event to the dapm core. The core then makes any
3535  * necessary widget power changes.
3536  *
3537  * Returns 0 for success else error.
3538  */
3539 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3540                               int event)
3541 {
3542         struct snd_soc_card *card = rtd->card;
3543
3544         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3545         soc_dapm_stream_event(rtd, stream, event);
3546         mutex_unlock(&card->dapm_mutex);
3547 }
3548
3549 /**
3550  * snd_soc_dapm_enable_pin - enable pin.
3551  * @dapm: DAPM context
3552  * @pin: pin name
3553  *
3554  * Enables input/output pin and its parents or children widgets iff there is
3555  * a valid audio route and active audio stream.
3556  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3557  * do any widget power switching.
3558  */
3559 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3560 {
3561         return snd_soc_dapm_set_pin(dapm, pin, 1);
3562 }
3563 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3564
3565 /**
3566  * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3567  * @dapm: DAPM context
3568  * @pin: pin name
3569  *
3570  * Enables input/output pin regardless of any other state.  This is
3571  * intended for use with microphone bias supplies used in microphone
3572  * jack detection.
3573  *
3574  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3575  * do any widget power switching.
3576  */
3577 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3578                                   const char *pin)
3579 {
3580         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3581
3582         if (!w) {
3583                 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3584                 return -EINVAL;
3585         }
3586
3587         dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3588         w->connected = 1;
3589         w->force = 1;
3590         dapm_mark_dirty(w, "force enable");
3591
3592         return 0;
3593 }
3594 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3595
3596 /**
3597  * snd_soc_dapm_disable_pin - disable pin.
3598  * @dapm: DAPM context
3599  * @pin: pin name
3600  *
3601  * Disables input/output pin and its parents or children widgets.
3602  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3603  * do any widget power switching.
3604  */
3605 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3606                              const char *pin)
3607 {
3608         return snd_soc_dapm_set_pin(dapm, pin, 0);
3609 }
3610 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3611
3612 /**
3613  * snd_soc_dapm_nc_pin - permanently disable pin.
3614  * @dapm: DAPM context
3615  * @pin: pin name
3616  *
3617  * Marks the specified pin as being not connected, disabling it along
3618  * any parent or child widgets.  At present this is identical to
3619  * snd_soc_dapm_disable_pin() but in future it will be extended to do
3620  * additional things such as disabling controls which only affect
3621  * paths through the pin.
3622  *
3623  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3624  * do any widget power switching.
3625  */
3626 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3627 {
3628         return snd_soc_dapm_set_pin(dapm, pin, 0);
3629 }
3630 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3631
3632 /**
3633  * snd_soc_dapm_get_pin_status - get audio pin status
3634  * @dapm: DAPM context
3635  * @pin: audio signal pin endpoint (or start point)
3636  *
3637  * Get audio pin status - connected or disconnected.
3638  *
3639  * Returns 1 for connected otherwise 0.
3640  */
3641 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3642                                 const char *pin)
3643 {
3644         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3645
3646         if (w)
3647                 return w->connected;
3648
3649         return 0;
3650 }
3651 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3652
3653 /**
3654  * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3655  * @dapm: DAPM context
3656  * @pin: audio signal pin endpoint (or start point)
3657  *
3658  * Mark the given endpoint or pin as ignoring suspend.  When the
3659  * system is disabled a path between two endpoints flagged as ignoring
3660  * suspend will not be disabled.  The path must already be enabled via
3661  * normal means at suspend time, it will not be turned on if it was not
3662  * already enabled.
3663  */
3664 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3665                                 const char *pin)
3666 {
3667         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3668
3669         if (!w) {
3670                 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3671                 return -EINVAL;
3672         }
3673
3674         w->ignore_suspend = 1;
3675
3676         return 0;
3677 }
3678 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3679
3680 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3681                                               struct snd_soc_dapm_widget *w)
3682 {
3683         struct snd_soc_dapm_path *p;
3684
3685         list_for_each_entry(p, &card->paths, list) {
3686                 if ((p->source == w) || (p->sink == w)) {
3687                         dev_dbg(card->dev,
3688                             "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3689                             p->source->name, p->source->id, p->source->dapm,
3690                             p->sink->name, p->sink->id, p->sink->dapm);
3691
3692                         /* Connected to something other than the codec */
3693                         if (p->source->dapm != p->sink->dapm)
3694                                 return true;
3695                         /*
3696                          * Loopback connection from codec external pin to
3697                          * codec external pin
3698                          */
3699                         if (p->sink->id == snd_soc_dapm_input) {
3700                                 switch (p->source->id) {
3701                                 case snd_soc_dapm_output:
3702                                 case snd_soc_dapm_micbias:
3703                                         return true;
3704                                 default:
3705                                         break;
3706                                 }
3707                         }
3708                 }
3709         }
3710
3711         return false;
3712 }
3713
3714 /**
3715  * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3716  * @codec: The codec whose pins should be processed
3717  *
3718  * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3719  * which are unused. Pins are used if they are connected externally to the
3720  * codec, whether that be to some other device, or a loop-back connection to
3721  * the codec itself.
3722  */
3723 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3724 {
3725         struct snd_soc_card *card = codec->card;
3726         struct snd_soc_dapm_context *dapm = &codec->dapm;
3727         struct snd_soc_dapm_widget *w;
3728
3729         dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
3730                 &card->dapm, &codec->dapm);
3731
3732         list_for_each_entry(w, &card->widgets, list) {
3733                 if (w->dapm != dapm)
3734                         continue;
3735                 switch (w->id) {
3736                 case snd_soc_dapm_input:
3737                 case snd_soc_dapm_output:
3738                 case snd_soc_dapm_micbias:
3739                         dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
3740                                 w->name);
3741                         if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3742                                 dev_dbg(codec->dev,
3743                                         "... Not in map; disabling\n");
3744                                 snd_soc_dapm_nc_pin(dapm, w->name);
3745                         }
3746                         break;
3747                 default:
3748                         break;
3749                 }
3750         }
3751 }
3752
3753 /**
3754  * snd_soc_dapm_free - free dapm resources
3755  * @dapm: DAPM context
3756  *
3757  * Free all dapm widgets and resources.
3758  */
3759 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3760 {
3761         snd_soc_dapm_sys_remove(dapm->dev);
3762         dapm_debugfs_cleanup(dapm);
3763         dapm_free_widgets(dapm);
3764         list_del(&dapm->list);
3765 }
3766 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3767
3768 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
3769 {
3770         struct snd_soc_card *card = dapm->card;
3771         struct snd_soc_dapm_widget *w;
3772         LIST_HEAD(down_list);
3773         int powerdown = 0;
3774
3775         mutex_lock(&card->dapm_mutex);
3776
3777         list_for_each_entry(w, &dapm->card->widgets, list) {
3778                 if (w->dapm != dapm)
3779                         continue;
3780                 if (w->power) {
3781                         dapm_seq_insert(w, &down_list, false);
3782                         w->power = 0;
3783                         powerdown = 1;
3784                 }
3785         }
3786
3787         /* If there were no widgets to power down we're already in
3788          * standby.
3789          */
3790         if (powerdown) {
3791                 if (dapm->bias_level == SND_SOC_BIAS_ON)
3792                         snd_soc_dapm_set_bias_level(dapm,
3793                                                     SND_SOC_BIAS_PREPARE);
3794                 dapm_seq_run(dapm, &down_list, 0, false);
3795                 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3796                         snd_soc_dapm_set_bias_level(dapm,
3797                                                     SND_SOC_BIAS_STANDBY);
3798         }
3799
3800         mutex_unlock(&card->dapm_mutex);
3801 }
3802
3803 /*
3804  * snd_soc_dapm_shutdown - callback for system shutdown
3805  */
3806 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3807 {
3808         struct snd_soc_codec *codec;
3809
3810         list_for_each_entry(codec, &card->codec_dev_list, card_list) {
3811                 soc_dapm_shutdown_codec(&codec->dapm);
3812                 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3813                         snd_soc_dapm_set_bias_level(&codec->dapm,
3814                                                     SND_SOC_BIAS_OFF);
3815         }
3816 }
3817
3818 /* Module information */
3819 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3820 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3821 MODULE_LICENSE("GPL");