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