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