]> Pileus Git - ~andy/linux/blob - sound/soc/soc-dapm.c
ASoC: dapm: Add API call to query valid DAPM paths
[~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 0;
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         return ret;
1862 }
1863 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
1864
1865 /* test and update the power status of a mixer or switch widget */
1866 static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1867                                    struct snd_kcontrol *kcontrol, int connect)
1868 {
1869         struct snd_soc_dapm_path *path;
1870         int found = 0;
1871
1872         if (widget->id != snd_soc_dapm_mixer &&
1873             widget->id != snd_soc_dapm_mixer_named_ctl &&
1874             widget->id != snd_soc_dapm_switch)
1875                 return -ENODEV;
1876
1877         /* find dapm widget path assoc with kcontrol */
1878         list_for_each_entry(path, &widget->dapm->card->paths, list) {
1879                 if (path->kcontrol != kcontrol)
1880                         continue;
1881
1882                 /* found, now check type */
1883                 found = 1;
1884                 path->connect = connect;
1885                 dapm_mark_dirty(path->source, "mixer connection");
1886         }
1887
1888         if (found) {
1889                 dapm_mark_dirty(widget, "mixer update");
1890                 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1891         }
1892
1893         return 0;
1894 }
1895
1896 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1897                                 struct snd_kcontrol *kcontrol, int connect)
1898 {
1899         struct snd_soc_card *card = widget->dapm->card;
1900         int ret;
1901
1902         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1903         ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
1904         mutex_unlock(&card->dapm_mutex);
1905         return ret;
1906 }
1907 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
1908
1909 /* show dapm widget status in sys fs */
1910 static ssize_t dapm_widget_show(struct device *dev,
1911         struct device_attribute *attr, char *buf)
1912 {
1913         struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
1914         struct snd_soc_codec *codec =rtd->codec;
1915         struct snd_soc_dapm_widget *w;
1916         int count = 0;
1917         char *state = "not set";
1918
1919         list_for_each_entry(w, &codec->card->widgets, list) {
1920                 if (w->dapm != &codec->dapm)
1921                         continue;
1922
1923                 /* only display widgets that burnm power */
1924                 switch (w->id) {
1925                 case snd_soc_dapm_hp:
1926                 case snd_soc_dapm_mic:
1927                 case snd_soc_dapm_spk:
1928                 case snd_soc_dapm_line:
1929                 case snd_soc_dapm_micbias:
1930                 case snd_soc_dapm_dac:
1931                 case snd_soc_dapm_adc:
1932                 case snd_soc_dapm_pga:
1933                 case snd_soc_dapm_out_drv:
1934                 case snd_soc_dapm_mixer:
1935                 case snd_soc_dapm_mixer_named_ctl:
1936                 case snd_soc_dapm_supply:
1937                 case snd_soc_dapm_regulator_supply:
1938                         if (w->name)
1939                                 count += sprintf(buf + count, "%s: %s\n",
1940                                         w->name, w->power ? "On":"Off");
1941                 break;
1942                 default:
1943                 break;
1944                 }
1945         }
1946
1947         switch (codec->dapm.bias_level) {
1948         case SND_SOC_BIAS_ON:
1949                 state = "On";
1950                 break;
1951         case SND_SOC_BIAS_PREPARE:
1952                 state = "Prepare";
1953                 break;
1954         case SND_SOC_BIAS_STANDBY:
1955                 state = "Standby";
1956                 break;
1957         case SND_SOC_BIAS_OFF:
1958                 state = "Off";
1959                 break;
1960         }
1961         count += sprintf(buf + count, "PM State: %s\n", state);
1962
1963         return count;
1964 }
1965
1966 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1967
1968 int snd_soc_dapm_sys_add(struct device *dev)
1969 {
1970         return device_create_file(dev, &dev_attr_dapm_widget);
1971 }
1972
1973 static void snd_soc_dapm_sys_remove(struct device *dev)
1974 {
1975         device_remove_file(dev, &dev_attr_dapm_widget);
1976 }
1977
1978 /* free all dapm widgets and resources */
1979 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1980 {
1981         struct snd_soc_dapm_widget *w, *next_w;
1982         struct snd_soc_dapm_path *p, *next_p;
1983
1984         list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1985                 if (w->dapm != dapm)
1986                         continue;
1987                 list_del(&w->list);
1988                 /*
1989                  * remove source and sink paths associated to this widget.
1990                  * While removing the path, remove reference to it from both
1991                  * source and sink widgets so that path is removed only once.
1992                  */
1993                 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1994                         list_del(&p->list_sink);
1995                         list_del(&p->list_source);
1996                         list_del(&p->list);
1997                         kfree(p->long_name);
1998                         kfree(p);
1999                 }
2000                 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
2001                         list_del(&p->list_sink);
2002                         list_del(&p->list_source);
2003                         list_del(&p->list);
2004                         kfree(p->long_name);
2005                         kfree(p);
2006                 }
2007                 kfree(w->kcontrols);
2008                 kfree(w->name);
2009                 kfree(w);
2010         }
2011 }
2012
2013 static struct snd_soc_dapm_widget *dapm_find_widget(
2014                         struct snd_soc_dapm_context *dapm, const char *pin,
2015                         bool search_other_contexts)
2016 {
2017         struct snd_soc_dapm_widget *w;
2018         struct snd_soc_dapm_widget *fallback = NULL;
2019
2020         list_for_each_entry(w, &dapm->card->widgets, list) {
2021                 if (!strcmp(w->name, pin)) {
2022                         if (w->dapm == dapm)
2023                                 return w;
2024                         else
2025                                 fallback = w;
2026                 }
2027         }
2028
2029         if (search_other_contexts)
2030                 return fallback;
2031
2032         return NULL;
2033 }
2034
2035 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2036                                 const char *pin, int status)
2037 {
2038         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2039
2040         if (!w) {
2041                 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2042                 return -EINVAL;
2043         }
2044
2045         if (w->connected != status)
2046                 dapm_mark_dirty(w, "pin configuration");
2047
2048         w->connected = status;
2049         if (status == 0)
2050                 w->force = 0;
2051
2052         return 0;
2053 }
2054
2055 /**
2056  * snd_soc_dapm_sync - scan and power dapm paths
2057  * @dapm: DAPM context
2058  *
2059  * Walks all dapm audio paths and powers widgets according to their
2060  * stream or path usage.
2061  *
2062  * Returns 0 for success.
2063  */
2064 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2065 {
2066         int ret;
2067
2068         /*
2069          * Suppress early reports (eg, jacks syncing their state) to avoid
2070          * silly DAPM runs during card startup.
2071          */
2072         if (!dapm->card || !dapm->card->instantiated)
2073                 return 0;
2074
2075         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2076         ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2077         mutex_unlock(&dapm->card->dapm_mutex);
2078         return ret;
2079 }
2080 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2081
2082 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2083                                   const struct snd_soc_dapm_route *route)
2084 {
2085         struct snd_soc_dapm_path *path;
2086         struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2087         struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2088         const char *sink;
2089         const char *control = route->control;
2090         const char *source;
2091         char prefixed_sink[80];
2092         char prefixed_source[80];
2093         int ret = 0;
2094
2095         if (dapm->codec && dapm->codec->name_prefix) {
2096                 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2097                          dapm->codec->name_prefix, route->sink);
2098                 sink = prefixed_sink;
2099                 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2100                          dapm->codec->name_prefix, route->source);
2101                 source = prefixed_source;
2102         } else {
2103                 sink = route->sink;
2104                 source = route->source;
2105         }
2106
2107         /*
2108          * find src and dest widgets over all widgets but favor a widget from
2109          * current DAPM context
2110          */
2111         list_for_each_entry(w, &dapm->card->widgets, list) {
2112                 if (!wsink && !(strcmp(w->name, sink))) {
2113                         wtsink = w;
2114                         if (w->dapm == dapm)
2115                                 wsink = w;
2116                         continue;
2117                 }
2118                 if (!wsource && !(strcmp(w->name, source))) {
2119                         wtsource = w;
2120                         if (w->dapm == dapm)
2121                                 wsource = w;
2122                 }
2123         }
2124         /* use widget from another DAPM context if not found from this */
2125         if (!wsink)
2126                 wsink = wtsink;
2127         if (!wsource)
2128                 wsource = wtsource;
2129
2130         if (wsource == NULL || wsink == NULL)
2131                 return -ENODEV;
2132
2133         path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2134         if (!path)
2135                 return -ENOMEM;
2136
2137         path->source = wsource;
2138         path->sink = wsink;
2139         path->connected = route->connected;
2140         INIT_LIST_HEAD(&path->list);
2141         INIT_LIST_HEAD(&path->list_source);
2142         INIT_LIST_HEAD(&path->list_sink);
2143
2144         /* check for external widgets */
2145         if (wsink->id == snd_soc_dapm_input) {
2146                 if (wsource->id == snd_soc_dapm_micbias ||
2147                         wsource->id == snd_soc_dapm_mic ||
2148                         wsource->id == snd_soc_dapm_line ||
2149                         wsource->id == snd_soc_dapm_output)
2150                         wsink->ext = 1;
2151         }
2152         if (wsource->id == snd_soc_dapm_output) {
2153                 if (wsink->id == snd_soc_dapm_spk ||
2154                         wsink->id == snd_soc_dapm_hp ||
2155                         wsink->id == snd_soc_dapm_line ||
2156                         wsink->id == snd_soc_dapm_input)
2157                         wsource->ext = 1;
2158         }
2159
2160         /* connect static paths */
2161         if (control == NULL) {
2162                 list_add(&path->list, &dapm->card->paths);
2163                 list_add(&path->list_sink, &wsink->sources);
2164                 list_add(&path->list_source, &wsource->sinks);
2165                 path->connect = 1;
2166                 return 0;
2167         }
2168
2169         /* connect dynamic paths */
2170         switch (wsink->id) {
2171         case snd_soc_dapm_adc:
2172         case snd_soc_dapm_dac:
2173         case snd_soc_dapm_pga:
2174         case snd_soc_dapm_out_drv:
2175         case snd_soc_dapm_input:
2176         case snd_soc_dapm_output:
2177         case snd_soc_dapm_siggen:
2178         case snd_soc_dapm_micbias:
2179         case snd_soc_dapm_vmid:
2180         case snd_soc_dapm_pre:
2181         case snd_soc_dapm_post:
2182         case snd_soc_dapm_supply:
2183         case snd_soc_dapm_regulator_supply:
2184         case snd_soc_dapm_aif_in:
2185         case snd_soc_dapm_aif_out:
2186         case snd_soc_dapm_dai:
2187         case snd_soc_dapm_dai_link:
2188                 list_add(&path->list, &dapm->card->paths);
2189                 list_add(&path->list_sink, &wsink->sources);
2190                 list_add(&path->list_source, &wsource->sinks);
2191                 path->connect = 1;
2192                 return 0;
2193         case snd_soc_dapm_mux:
2194         case snd_soc_dapm_virt_mux:
2195         case snd_soc_dapm_value_mux:
2196                 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2197                         &wsink->kcontrol_news[0]);
2198                 if (ret != 0)
2199                         goto err;
2200                 break;
2201         case snd_soc_dapm_switch:
2202         case snd_soc_dapm_mixer:
2203         case snd_soc_dapm_mixer_named_ctl:
2204                 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2205                 if (ret != 0)
2206                         goto err;
2207                 break;
2208         case snd_soc_dapm_hp:
2209         case snd_soc_dapm_mic:
2210         case snd_soc_dapm_line:
2211         case snd_soc_dapm_spk:
2212                 list_add(&path->list, &dapm->card->paths);
2213                 list_add(&path->list_sink, &wsink->sources);
2214                 list_add(&path->list_source, &wsource->sinks);
2215                 path->connect = 0;
2216                 return 0;
2217         }
2218         return 0;
2219
2220 err:
2221         dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2222                  source, control, sink);
2223         kfree(path);
2224         return ret;
2225 }
2226
2227 /**
2228  * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2229  * @dapm: DAPM context
2230  * @route: audio routes
2231  * @num: number of routes
2232  *
2233  * Connects 2 dapm widgets together via a named audio path. The sink is
2234  * the widget receiving the audio signal, whilst the source is the sender
2235  * of the audio signal.
2236  *
2237  * Returns 0 for success else error. On error all resources can be freed
2238  * with a call to snd_soc_card_free().
2239  */
2240 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2241                             const struct snd_soc_dapm_route *route, int num)
2242 {
2243         int i, ret = 0;
2244
2245         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2246         for (i = 0; i < num; i++) {
2247                 ret = snd_soc_dapm_add_route(dapm, route);
2248                 if (ret < 0) {
2249                         dev_err(dapm->dev, "Failed to add route %s->%s\n",
2250                                 route->source, route->sink);
2251                         break;
2252                 }
2253                 route++;
2254         }
2255         mutex_unlock(&dapm->card->dapm_mutex);
2256
2257         return ret;
2258 }
2259 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2260
2261 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2262                                    const struct snd_soc_dapm_route *route)
2263 {
2264         struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2265                                                               route->source,
2266                                                               true);
2267         struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2268                                                             route->sink,
2269                                                             true);
2270         struct snd_soc_dapm_path *path;
2271         int count = 0;
2272
2273         if (!source) {
2274                 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2275                         route->source);
2276                 return -ENODEV;
2277         }
2278
2279         if (!sink) {
2280                 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2281                         route->sink);
2282                 return -ENODEV;
2283         }
2284
2285         if (route->control || route->connected)
2286                 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2287                          route->source, route->sink);
2288
2289         list_for_each_entry(path, &source->sinks, list_source) {
2290                 if (path->sink == sink) {
2291                         path->weak = 1;
2292                         count++;
2293                 }
2294         }
2295
2296         if (count == 0)
2297                 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2298                         route->source, route->sink);
2299         if (count > 1)
2300                 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2301                          count, route->source, route->sink);
2302
2303         return 0;
2304 }
2305
2306 /**
2307  * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2308  * @dapm: DAPM context
2309  * @route: audio routes
2310  * @num: number of routes
2311  *
2312  * Mark existing routes matching those specified in the passed array
2313  * as being weak, meaning that they are ignored for the purpose of
2314  * power decisions.  The main intended use case is for sidetone paths
2315  * which couple audio between other independent paths if they are both
2316  * active in order to make the combination work better at the user
2317  * level but which aren't intended to be "used".
2318  *
2319  * Note that CODEC drivers should not use this as sidetone type paths
2320  * can frequently also be used as bypass paths.
2321  */
2322 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2323                              const struct snd_soc_dapm_route *route, int num)
2324 {
2325         int i, err;
2326         int ret = 0;
2327
2328         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2329         for (i = 0; i < num; i++) {
2330                 err = snd_soc_dapm_weak_route(dapm, route);
2331                 if (err)
2332                         ret = err;
2333                 route++;
2334         }
2335         mutex_unlock(&dapm->card->dapm_mutex);
2336
2337         return ret;
2338 }
2339 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2340
2341 /**
2342  * snd_soc_dapm_new_widgets - add new dapm widgets
2343  * @dapm: DAPM context
2344  *
2345  * Checks the codec for any new dapm widgets and creates them if found.
2346  *
2347  * Returns 0 for success.
2348  */
2349 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2350 {
2351         struct snd_soc_dapm_widget *w;
2352         unsigned int val;
2353
2354         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2355
2356         list_for_each_entry(w, &dapm->card->widgets, list)
2357         {
2358                 if (w->new)
2359                         continue;
2360
2361                 if (w->num_kcontrols) {
2362                         w->kcontrols = kzalloc(w->num_kcontrols *
2363                                                 sizeof(struct snd_kcontrol *),
2364                                                 GFP_KERNEL);
2365                         if (!w->kcontrols) {
2366                                 mutex_unlock(&dapm->card->dapm_mutex);
2367                                 return -ENOMEM;
2368                         }
2369                 }
2370
2371                 switch(w->id) {
2372                 case snd_soc_dapm_switch:
2373                 case snd_soc_dapm_mixer:
2374                 case snd_soc_dapm_mixer_named_ctl:
2375                         dapm_new_mixer(w);
2376                         break;
2377                 case snd_soc_dapm_mux:
2378                 case snd_soc_dapm_virt_mux:
2379                 case snd_soc_dapm_value_mux:
2380                         dapm_new_mux(w);
2381                         break;
2382                 case snd_soc_dapm_pga:
2383                 case snd_soc_dapm_out_drv:
2384                         dapm_new_pga(w);
2385                         break;
2386                 default:
2387                         break;
2388                 }
2389
2390                 /* Read the initial power state from the device */
2391                 if (w->reg >= 0) {
2392                         val = soc_widget_read(w, w->reg);
2393                         val &= 1 << w->shift;
2394                         if (w->invert)
2395                                 val = !val;
2396
2397                         if (val)
2398                                 w->power = 1;
2399                 }
2400
2401                 w->new = 1;
2402
2403                 dapm_mark_dirty(w, "new widget");
2404                 dapm_debugfs_add_widget(w);
2405         }
2406
2407         dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2408         mutex_unlock(&dapm->card->dapm_mutex);
2409         return 0;
2410 }
2411 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2412
2413 /**
2414  * snd_soc_dapm_get_volsw - dapm mixer get callback
2415  * @kcontrol: mixer control
2416  * @ucontrol: control element information
2417  *
2418  * Callback to get the value of a dapm mixer control.
2419  *
2420  * Returns 0 for success.
2421  */
2422 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2423         struct snd_ctl_elem_value *ucontrol)
2424 {
2425         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2426         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2427         struct soc_mixer_control *mc =
2428                 (struct soc_mixer_control *)kcontrol->private_value;
2429         unsigned int reg = mc->reg;
2430         unsigned int shift = mc->shift;
2431         unsigned int rshift = mc->rshift;
2432         int max = mc->max;
2433         unsigned int invert = mc->invert;
2434         unsigned int mask = (1 << fls(max)) - 1;
2435
2436         ucontrol->value.integer.value[0] =
2437                 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2438         if (shift != rshift)
2439                 ucontrol->value.integer.value[1] =
2440                         (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2441         if (invert) {
2442                 ucontrol->value.integer.value[0] =
2443                         max - ucontrol->value.integer.value[0];
2444                 if (shift != rshift)
2445                         ucontrol->value.integer.value[1] =
2446                                 max - ucontrol->value.integer.value[1];
2447         }
2448
2449         return 0;
2450 }
2451 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2452
2453 /**
2454  * snd_soc_dapm_put_volsw - dapm mixer set callback
2455  * @kcontrol: mixer control
2456  * @ucontrol: control element information
2457  *
2458  * Callback to set the value of a dapm mixer control.
2459  *
2460  * Returns 0 for success.
2461  */
2462 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2463         struct snd_ctl_elem_value *ucontrol)
2464 {
2465         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2466         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2467         struct snd_soc_codec *codec = widget->codec;
2468         struct snd_soc_card *card = codec->card;
2469         struct soc_mixer_control *mc =
2470                 (struct soc_mixer_control *)kcontrol->private_value;
2471         unsigned int reg = mc->reg;
2472         unsigned int shift = mc->shift;
2473         int max = mc->max;
2474         unsigned int mask = (1 << fls(max)) - 1;
2475         unsigned int invert = mc->invert;
2476         unsigned int val;
2477         int connect, change;
2478         struct snd_soc_dapm_update update;
2479         int wi;
2480
2481         val = (ucontrol->value.integer.value[0] & mask);
2482
2483         if (invert)
2484                 val = max - val;
2485         mask = mask << shift;
2486         val = val << shift;
2487
2488         if (val)
2489                 /* new connection */
2490                 connect = invert ? 0 : 1;
2491         else
2492                 /* old connection must be powered down */
2493                 connect = invert ? 1 : 0;
2494
2495         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2496
2497         change = snd_soc_test_bits(widget->codec, reg, mask, val);
2498         if (change) {
2499                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2500                         widget = wlist->widgets[wi];
2501
2502                         widget->value = val;
2503
2504                         update.kcontrol = kcontrol;
2505                         update.widget = widget;
2506                         update.reg = reg;
2507                         update.mask = mask;
2508                         update.val = val;
2509                         widget->dapm->update = &update;
2510
2511                         soc_dapm_mixer_update_power(widget, kcontrol, connect);
2512
2513                         widget->dapm->update = NULL;
2514                 }
2515         }
2516
2517         mutex_unlock(&card->dapm_mutex);
2518         return 0;
2519 }
2520 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2521
2522 /**
2523  * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2524  * @kcontrol: mixer control
2525  * @ucontrol: control element information
2526  *
2527  * Callback to get the value of a dapm enumerated double mixer control.
2528  *
2529  * Returns 0 for success.
2530  */
2531 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2532         struct snd_ctl_elem_value *ucontrol)
2533 {
2534         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2535         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2536         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2537         unsigned int val, bitmask;
2538
2539         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2540                 ;
2541         val = snd_soc_read(widget->codec, e->reg);
2542         ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2543         if (e->shift_l != e->shift_r)
2544                 ucontrol->value.enumerated.item[1] =
2545                         (val >> e->shift_r) & (bitmask - 1);
2546
2547         return 0;
2548 }
2549 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2550
2551 /**
2552  * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2553  * @kcontrol: mixer control
2554  * @ucontrol: control element information
2555  *
2556  * Callback to set the value of a dapm enumerated double mixer control.
2557  *
2558  * Returns 0 for success.
2559  */
2560 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2561         struct snd_ctl_elem_value *ucontrol)
2562 {
2563         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2564         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2565         struct snd_soc_codec *codec = widget->codec;
2566         struct snd_soc_card *card = codec->card;
2567         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2568         unsigned int val, mux, change;
2569         unsigned int mask, bitmask;
2570         struct snd_soc_dapm_update update;
2571         int wi;
2572
2573         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2574                 ;
2575         if (ucontrol->value.enumerated.item[0] > e->max - 1)
2576                 return -EINVAL;
2577         mux = ucontrol->value.enumerated.item[0];
2578         val = mux << e->shift_l;
2579         mask = (bitmask - 1) << e->shift_l;
2580         if (e->shift_l != e->shift_r) {
2581                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2582                         return -EINVAL;
2583                 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2584                 mask |= (bitmask - 1) << e->shift_r;
2585         }
2586
2587         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2588
2589         change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2590         if (change) {
2591                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2592                         widget = wlist->widgets[wi];
2593
2594                         widget->value = val;
2595
2596                         update.kcontrol = kcontrol;
2597                         update.widget = widget;
2598                         update.reg = e->reg;
2599                         update.mask = mask;
2600                         update.val = val;
2601                         widget->dapm->update = &update;
2602
2603                         soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2604
2605                         widget->dapm->update = NULL;
2606                 }
2607         }
2608
2609         mutex_unlock(&card->dapm_mutex);
2610         return change;
2611 }
2612 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2613
2614 /**
2615  * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2616  * @kcontrol: mixer control
2617  * @ucontrol: control element information
2618  *
2619  * Returns 0 for success.
2620  */
2621 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2622                                struct snd_ctl_elem_value *ucontrol)
2623 {
2624         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2625         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2626
2627         ucontrol->value.enumerated.item[0] = widget->value;
2628
2629         return 0;
2630 }
2631 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2632
2633 /**
2634  * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2635  * @kcontrol: mixer control
2636  * @ucontrol: control element information
2637  *
2638  * Returns 0 for success.
2639  */
2640 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2641                                struct snd_ctl_elem_value *ucontrol)
2642 {
2643         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2644         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2645         struct snd_soc_codec *codec = widget->codec;
2646         struct snd_soc_card *card = codec->card;
2647         struct soc_enum *e =
2648                 (struct soc_enum *)kcontrol->private_value;
2649         int change;
2650         int ret = 0;
2651         int wi;
2652
2653         if (ucontrol->value.enumerated.item[0] >= e->max)
2654                 return -EINVAL;
2655
2656         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2657
2658         change = widget->value != ucontrol->value.enumerated.item[0];
2659         if (change) {
2660                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2661                         widget = wlist->widgets[wi];
2662
2663                         widget->value = ucontrol->value.enumerated.item[0];
2664
2665                         soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
2666                 }
2667         }
2668
2669         mutex_unlock(&card->dapm_mutex);
2670         return ret;
2671 }
2672 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2673
2674 /**
2675  * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2676  *                                      callback
2677  * @kcontrol: mixer control
2678  * @ucontrol: control element information
2679  *
2680  * Callback to get the value of a dapm semi enumerated double mixer control.
2681  *
2682  * Semi enumerated mixer: the enumerated items are referred as values. Can be
2683  * used for handling bitfield coded enumeration for example.
2684  *
2685  * Returns 0 for success.
2686  */
2687 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2688         struct snd_ctl_elem_value *ucontrol)
2689 {
2690         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2691         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2692         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2693         unsigned int reg_val, val, mux;
2694
2695         reg_val = snd_soc_read(widget->codec, e->reg);
2696         val = (reg_val >> e->shift_l) & e->mask;
2697         for (mux = 0; mux < e->max; mux++) {
2698                 if (val == e->values[mux])
2699                         break;
2700         }
2701         ucontrol->value.enumerated.item[0] = mux;
2702         if (e->shift_l != e->shift_r) {
2703                 val = (reg_val >> e->shift_r) & e->mask;
2704                 for (mux = 0; mux < e->max; mux++) {
2705                         if (val == e->values[mux])
2706                                 break;
2707                 }
2708                 ucontrol->value.enumerated.item[1] = mux;
2709         }
2710
2711         return 0;
2712 }
2713 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2714
2715 /**
2716  * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2717  *                                      callback
2718  * @kcontrol: mixer control
2719  * @ucontrol: control element information
2720  *
2721  * Callback to set the value of a dapm semi enumerated double mixer control.
2722  *
2723  * Semi enumerated mixer: the enumerated items are referred as values. Can be
2724  * used for handling bitfield coded enumeration for example.
2725  *
2726  * Returns 0 for success.
2727  */
2728 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2729         struct snd_ctl_elem_value *ucontrol)
2730 {
2731         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2732         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2733         struct snd_soc_codec *codec = widget->codec;
2734         struct snd_soc_card *card = codec->card;
2735         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2736         unsigned int val, mux, change;
2737         unsigned int mask;
2738         struct snd_soc_dapm_update update;
2739         int wi;
2740
2741         if (ucontrol->value.enumerated.item[0] > e->max - 1)
2742                 return -EINVAL;
2743         mux = ucontrol->value.enumerated.item[0];
2744         val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2745         mask = e->mask << e->shift_l;
2746         if (e->shift_l != e->shift_r) {
2747                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2748                         return -EINVAL;
2749                 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2750                 mask |= e->mask << e->shift_r;
2751         }
2752
2753         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2754
2755         change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2756         if (change) {
2757                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2758                         widget = wlist->widgets[wi];
2759
2760                         widget->value = val;
2761
2762                         update.kcontrol = kcontrol;
2763                         update.widget = widget;
2764                         update.reg = e->reg;
2765                         update.mask = mask;
2766                         update.val = val;
2767                         widget->dapm->update = &update;
2768
2769                         soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2770
2771                         widget->dapm->update = NULL;
2772                 }
2773         }
2774
2775         mutex_unlock(&card->dapm_mutex);
2776         return change;
2777 }
2778 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2779
2780 /**
2781  * snd_soc_dapm_info_pin_switch - Info for a pin switch
2782  *
2783  * @kcontrol: mixer control
2784  * @uinfo: control element information
2785  *
2786  * Callback to provide information about a pin switch control.
2787  */
2788 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2789                                  struct snd_ctl_elem_info *uinfo)
2790 {
2791         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2792         uinfo->count = 1;
2793         uinfo->value.integer.min = 0;
2794         uinfo->value.integer.max = 1;
2795
2796         return 0;
2797 }
2798 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2799
2800 /**
2801  * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2802  *
2803  * @kcontrol: mixer control
2804  * @ucontrol: Value
2805  */
2806 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2807                                 struct snd_ctl_elem_value *ucontrol)
2808 {
2809         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2810         const char *pin = (const char *)kcontrol->private_value;
2811
2812         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2813
2814         ucontrol->value.integer.value[0] =
2815                 snd_soc_dapm_get_pin_status(&card->dapm, pin);
2816
2817         mutex_unlock(&card->dapm_mutex);
2818
2819         return 0;
2820 }
2821 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2822
2823 /**
2824  * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2825  *
2826  * @kcontrol: mixer control
2827  * @ucontrol: Value
2828  */
2829 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2830                                 struct snd_ctl_elem_value *ucontrol)
2831 {
2832         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2833         const char *pin = (const char *)kcontrol->private_value;
2834
2835         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2836
2837         if (ucontrol->value.integer.value[0])
2838                 snd_soc_dapm_enable_pin(&card->dapm, pin);
2839         else
2840                 snd_soc_dapm_disable_pin(&card->dapm, pin);
2841
2842         mutex_unlock(&card->dapm_mutex);
2843
2844         snd_soc_dapm_sync(&card->dapm);
2845         return 0;
2846 }
2847 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2848
2849 static struct snd_soc_dapm_widget *
2850 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2851                          const struct snd_soc_dapm_widget *widget)
2852 {
2853         struct snd_soc_dapm_widget *w;
2854         size_t name_len;
2855         int ret;
2856
2857         if ((w = dapm_cnew_widget(widget)) == NULL)
2858                 return NULL;
2859
2860         switch (w->id) {
2861         case snd_soc_dapm_regulator_supply:
2862                 w->regulator = devm_regulator_get(dapm->dev, w->name);
2863                 if (IS_ERR(w->regulator)) {
2864                         ret = PTR_ERR(w->regulator);
2865                         dev_err(dapm->dev, "Failed to request %s: %d\n",
2866                                 w->name, ret);
2867                         return NULL;
2868                 }
2869                 break;
2870         default:
2871                 break;
2872         }
2873
2874         name_len = strlen(widget->name) + 1;
2875         if (dapm->codec && dapm->codec->name_prefix)
2876                 name_len += 1 + strlen(dapm->codec->name_prefix);
2877         w->name = kmalloc(name_len, GFP_KERNEL);
2878         if (w->name == NULL) {
2879                 kfree(w);
2880                 return NULL;
2881         }
2882         if (dapm->codec && dapm->codec->name_prefix)
2883                 snprintf((char *)w->name, name_len, "%s %s",
2884                         dapm->codec->name_prefix, widget->name);
2885         else
2886                 snprintf((char *)w->name, name_len, "%s", widget->name);
2887
2888         switch (w->id) {
2889         case snd_soc_dapm_switch:
2890         case snd_soc_dapm_mixer:
2891         case snd_soc_dapm_mixer_named_ctl:
2892                 w->power_check = dapm_generic_check_power;
2893                 break;
2894         case snd_soc_dapm_mux:
2895         case snd_soc_dapm_virt_mux:
2896         case snd_soc_dapm_value_mux:
2897                 w->power_check = dapm_generic_check_power;
2898                 break;
2899         case snd_soc_dapm_adc:
2900         case snd_soc_dapm_aif_out:
2901                 w->power_check = dapm_adc_check_power;
2902                 break;
2903         case snd_soc_dapm_dac:
2904         case snd_soc_dapm_aif_in:
2905                 w->power_check = dapm_dac_check_power;
2906                 break;
2907         case snd_soc_dapm_pga:
2908         case snd_soc_dapm_out_drv:
2909         case snd_soc_dapm_input:
2910         case snd_soc_dapm_output:
2911         case snd_soc_dapm_micbias:
2912         case snd_soc_dapm_spk:
2913         case snd_soc_dapm_hp:
2914         case snd_soc_dapm_mic:
2915         case snd_soc_dapm_line:
2916         case snd_soc_dapm_dai_link:
2917                 w->power_check = dapm_generic_check_power;
2918                 break;
2919         case snd_soc_dapm_supply:
2920         case snd_soc_dapm_regulator_supply:
2921                 w->power_check = dapm_supply_check_power;
2922                 break;
2923         case snd_soc_dapm_dai:
2924                 w->power_check = dapm_dai_check_power;
2925                 break;
2926         default:
2927                 w->power_check = dapm_always_on_check_power;
2928                 break;
2929         }
2930
2931         dapm->n_widgets++;
2932         w->dapm = dapm;
2933         w->codec = dapm->codec;
2934         w->platform = dapm->platform;
2935         INIT_LIST_HEAD(&w->sources);
2936         INIT_LIST_HEAD(&w->sinks);
2937         INIT_LIST_HEAD(&w->list);
2938         INIT_LIST_HEAD(&w->dirty);
2939         list_add(&w->list, &dapm->card->widgets);
2940
2941         /* machine layer set ups unconnected pins and insertions */
2942         w->connected = 1;
2943         return w;
2944 }
2945
2946 /**
2947  * snd_soc_dapm_new_controls - create new dapm controls
2948  * @dapm: DAPM context
2949  * @widget: widget array
2950  * @num: number of widgets
2951  *
2952  * Creates new DAPM controls based upon the templates.
2953  *
2954  * Returns 0 for success else error.
2955  */
2956 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2957         const struct snd_soc_dapm_widget *widget,
2958         int num)
2959 {
2960         struct snd_soc_dapm_widget *w;
2961         int i;
2962         int ret = 0;
2963
2964         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2965         for (i = 0; i < num; i++) {
2966                 w = snd_soc_dapm_new_control(dapm, widget);
2967                 if (!w) {
2968                         dev_err(dapm->dev,
2969                                 "ASoC: Failed to create DAPM control %s\n",
2970                                 widget->name);
2971                         ret = -ENOMEM;
2972                         break;
2973                 }
2974                 widget++;
2975         }
2976         mutex_unlock(&dapm->card->dapm_mutex);
2977         return ret;
2978 }
2979 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2980
2981 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
2982                                   struct snd_kcontrol *kcontrol, int event)
2983 {
2984         struct snd_soc_dapm_path *source_p, *sink_p;
2985         struct snd_soc_dai *source, *sink;
2986         const struct snd_soc_pcm_stream *config = w->params;
2987         struct snd_pcm_substream substream;
2988         struct snd_pcm_hw_params params;
2989         u64 fmt;
2990         int ret;
2991
2992         BUG_ON(!config);
2993         BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
2994
2995         /* We only support a single source and sink, pick the first */
2996         source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
2997                                     list_sink);
2998         sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
2999                                   list_source);
3000
3001         BUG_ON(!source_p || !sink_p);
3002         BUG_ON(!sink_p->source || !source_p->sink);
3003         BUG_ON(!source_p->source || !sink_p->sink);
3004
3005         source = source_p->source->priv;
3006         sink = sink_p->sink->priv;
3007
3008         /* Be a little careful as we don't want to overflow the mask array */
3009         if (config->formats) {
3010                 fmt = ffs(config->formats) - 1;
3011         } else {
3012                 dev_warn(w->dapm->dev, "Invalid format %llx specified\n",
3013                          config->formats);
3014                 fmt = 0;
3015         }
3016
3017         /* Currently very limited parameter selection */
3018         memset(&params, 0, sizeof(params));
3019         snd_mask_set(hw_param_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3020
3021         hw_param_interval(&params, SNDRV_PCM_HW_PARAM_RATE)->min =
3022                 config->rate_min;
3023         hw_param_interval(&params, SNDRV_PCM_HW_PARAM_RATE)->max =
3024                 config->rate_max;
3025
3026         hw_param_interval(&params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3027                 = config->channels_min;
3028         hw_param_interval(&params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3029                 = config->channels_max;
3030
3031         memset(&substream, 0, sizeof(substream));
3032
3033         switch (event) {
3034         case SND_SOC_DAPM_PRE_PMU:
3035                 if (source->driver->ops && source->driver->ops->hw_params) {
3036                         substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3037                         ret = source->driver->ops->hw_params(&substream,
3038                                                              &params, source);
3039                         if (ret != 0) {
3040                                 dev_err(source->dev,
3041                                         "hw_params() failed: %d\n", ret);
3042                                 return ret;
3043                         }
3044                 }
3045
3046                 if (sink->driver->ops && sink->driver->ops->hw_params) {
3047                         substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3048                         ret = sink->driver->ops->hw_params(&substream, &params,
3049                                                            sink);
3050                         if (ret != 0) {
3051                                 dev_err(sink->dev,
3052                                         "hw_params() failed: %d\n", ret);
3053                                 return ret;
3054                         }
3055                 }
3056                 break;
3057
3058         case SND_SOC_DAPM_POST_PMU:
3059                 ret = snd_soc_dai_digital_mute(sink, 0);
3060                 if (ret != 0 && ret != -ENOTSUPP)
3061                         dev_warn(sink->dev, "Failed to unmute: %d\n", ret);
3062                 break;
3063
3064         case SND_SOC_DAPM_PRE_PMD:
3065                 ret = snd_soc_dai_digital_mute(sink, 1);
3066                 if (ret != 0 && ret != -ENOTSUPP)
3067                         dev_warn(sink->dev, "Failed to mute: %d\n", ret);
3068                 break;
3069
3070         default:
3071                 BUG();
3072                 return -EINVAL;
3073         }
3074
3075         return 0;
3076 }
3077
3078 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3079                          const struct snd_soc_pcm_stream *params,
3080                          struct snd_soc_dapm_widget *source,
3081                          struct snd_soc_dapm_widget *sink)
3082 {
3083         struct snd_soc_dapm_route routes[2];
3084         struct snd_soc_dapm_widget template;
3085         struct snd_soc_dapm_widget *w;
3086         size_t len;
3087         char *link_name;
3088
3089         len = strlen(source->name) + strlen(sink->name) + 2;
3090         link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3091         if (!link_name)
3092                 return -ENOMEM;
3093         snprintf(link_name, len, "%s-%s", source->name, sink->name);
3094
3095         memset(&template, 0, sizeof(template));
3096         template.reg = SND_SOC_NOPM;
3097         template.id = snd_soc_dapm_dai_link;
3098         template.name = link_name;
3099         template.event = snd_soc_dai_link_event;
3100         template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3101                 SND_SOC_DAPM_PRE_PMD;
3102
3103         dev_dbg(card->dev, "adding %s widget\n", link_name);
3104
3105         w = snd_soc_dapm_new_control(&card->dapm, &template);
3106         if (!w) {
3107                 dev_err(card->dev, "Failed to create %s widget\n",
3108                         link_name);
3109                 return -ENOMEM;
3110         }
3111
3112         w->params = params;
3113
3114         memset(&routes, 0, sizeof(routes));
3115
3116         routes[0].source = source->name;
3117         routes[0].sink = link_name;
3118         routes[1].source = link_name;
3119         routes[1].sink = sink->name;
3120
3121         return snd_soc_dapm_add_routes(&card->dapm, routes,
3122                                        ARRAY_SIZE(routes));
3123 }
3124
3125 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3126                                  struct snd_soc_dai *dai)
3127 {
3128         struct snd_soc_dapm_widget template;
3129         struct snd_soc_dapm_widget *w;
3130
3131         WARN_ON(dapm->dev != dai->dev);
3132
3133         memset(&template, 0, sizeof(template));
3134         template.reg = SND_SOC_NOPM;
3135
3136         if (dai->driver->playback.stream_name) {
3137                 template.id = snd_soc_dapm_dai;
3138                 template.name = dai->driver->playback.stream_name;
3139                 template.sname = dai->driver->playback.stream_name;
3140
3141                 dev_dbg(dai->dev, "adding %s widget\n",
3142                         template.name);
3143
3144                 w = snd_soc_dapm_new_control(dapm, &template);
3145                 if (!w) {
3146                         dev_err(dapm->dev, "Failed to create %s widget\n",
3147                                 dai->driver->playback.stream_name);
3148                 }
3149
3150                 w->priv = dai;
3151                 dai->playback_widget = w;
3152         }
3153
3154         if (dai->driver->capture.stream_name) {
3155                 template.id = snd_soc_dapm_dai;
3156                 template.name = dai->driver->capture.stream_name;
3157                 template.sname = dai->driver->capture.stream_name;
3158
3159                 dev_dbg(dai->dev, "adding %s widget\n",
3160                         template.name);
3161
3162                 w = snd_soc_dapm_new_control(dapm, &template);
3163                 if (!w) {
3164                         dev_err(dapm->dev, "Failed to create %s widget\n",
3165                                 dai->driver->capture.stream_name);
3166                 }
3167
3168                 w->priv = dai;
3169                 dai->capture_widget = w;
3170         }
3171
3172         return 0;
3173 }
3174
3175 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3176 {
3177         struct snd_soc_dapm_widget *dai_w, *w;
3178         struct snd_soc_dai *dai;
3179         struct snd_soc_dapm_route r;
3180
3181         memset(&r, 0, sizeof(r));
3182
3183         /* For each DAI widget... */
3184         list_for_each_entry(dai_w, &card->widgets, list) {
3185                 if (dai_w->id != snd_soc_dapm_dai)
3186                         continue;
3187
3188                 dai = dai_w->priv;
3189
3190                 /* ...find all widgets with the same stream and link them */
3191                 list_for_each_entry(w, &card->widgets, list) {
3192                         if (w->dapm != dai_w->dapm)
3193                                 continue;
3194
3195                         if (w->id == snd_soc_dapm_dai)
3196                                 continue;
3197
3198                         if (!w->sname)
3199                                 continue;
3200
3201                         if (dai->driver->playback.stream_name &&
3202                             strstr(w->sname,
3203                                    dai->driver->playback.stream_name)) {
3204                                 r.source = dai->playback_widget->name;
3205                                 r.sink = w->name;
3206                                 dev_dbg(dai->dev, "%s -> %s\n",
3207                                          r.source, r.sink);
3208
3209                                 snd_soc_dapm_add_route(w->dapm, &r);
3210                         }
3211
3212                         if (dai->driver->capture.stream_name &&
3213                             strstr(w->sname,
3214                                    dai->driver->capture.stream_name)) {
3215                                 r.source = w->name;
3216                                 r.sink = dai->capture_widget->name;
3217                                 dev_dbg(dai->dev, "%s -> %s\n",
3218                                         r.source, r.sink);
3219
3220                                 snd_soc_dapm_add_route(w->dapm, &r);
3221                         }
3222                 }
3223         }
3224
3225         return 0;
3226 }
3227
3228 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3229         int event)
3230 {
3231
3232         struct snd_soc_dapm_widget *w_cpu, *w_codec;
3233         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3234         struct snd_soc_dai *codec_dai = rtd->codec_dai;
3235
3236         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3237                 w_cpu = cpu_dai->playback_widget;
3238                 w_codec = codec_dai->playback_widget;
3239         } else {
3240                 w_cpu = cpu_dai->capture_widget;
3241                 w_codec = codec_dai->capture_widget;
3242         }
3243
3244         if (w_cpu) {
3245
3246                 dapm_mark_dirty(w_cpu, "stream event");
3247
3248                 switch (event) {
3249                 case SND_SOC_DAPM_STREAM_START:
3250                         w_cpu->active = 1;
3251                         break;
3252                 case SND_SOC_DAPM_STREAM_STOP:
3253                         w_cpu->active = 0;
3254                         break;
3255                 case SND_SOC_DAPM_STREAM_SUSPEND:
3256                 case SND_SOC_DAPM_STREAM_RESUME:
3257                 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3258                 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3259                         break;
3260                 }
3261         }
3262
3263         if (w_codec) {
3264
3265                 dapm_mark_dirty(w_codec, "stream event");
3266
3267                 switch (event) {
3268                 case SND_SOC_DAPM_STREAM_START:
3269                         w_codec->active = 1;
3270                         break;
3271                 case SND_SOC_DAPM_STREAM_STOP:
3272                         w_codec->active = 0;
3273                         break;
3274                 case SND_SOC_DAPM_STREAM_SUSPEND:
3275                 case SND_SOC_DAPM_STREAM_RESUME:
3276                 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3277                 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3278                         break;
3279                 }
3280         }
3281
3282         dapm_power_widgets(&rtd->card->dapm, event);
3283 }
3284
3285 /**
3286  * snd_soc_dapm_stream_event - send a stream event to the dapm core
3287  * @rtd: PCM runtime data
3288  * @stream: stream name
3289  * @event: stream event
3290  *
3291  * Sends a stream event to the dapm core. The core then makes any
3292  * necessary widget power changes.
3293  *
3294  * Returns 0 for success else error.
3295  */
3296 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3297                               int event)
3298 {
3299         struct snd_soc_card *card = rtd->card;
3300
3301         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3302         soc_dapm_stream_event(rtd, stream, event);
3303         mutex_unlock(&card->dapm_mutex);
3304 }
3305
3306 /**
3307  * snd_soc_dapm_enable_pin - enable pin.
3308  * @dapm: DAPM context
3309  * @pin: pin name
3310  *
3311  * Enables input/output pin and its parents or children widgets iff there is
3312  * a valid audio route and active audio stream.
3313  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3314  * do any widget power switching.
3315  */
3316 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3317 {
3318         return snd_soc_dapm_set_pin(dapm, pin, 1);
3319 }
3320 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3321
3322 /**
3323  * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3324  * @dapm: DAPM context
3325  * @pin: pin name
3326  *
3327  * Enables input/output pin regardless of any other state.  This is
3328  * intended for use with microphone bias supplies used in microphone
3329  * jack detection.
3330  *
3331  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3332  * do any widget power switching.
3333  */
3334 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3335                                   const char *pin)
3336 {
3337         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3338
3339         if (!w) {
3340                 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3341                 return -EINVAL;
3342         }
3343
3344         dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
3345         w->connected = 1;
3346         w->force = 1;
3347         dapm_mark_dirty(w, "force enable");
3348
3349         return 0;
3350 }
3351 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3352
3353 /**
3354  * snd_soc_dapm_disable_pin - disable pin.
3355  * @dapm: DAPM context
3356  * @pin: pin name
3357  *
3358  * Disables input/output pin and its parents or children widgets.
3359  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3360  * do any widget power switching.
3361  */
3362 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3363                              const char *pin)
3364 {
3365         return snd_soc_dapm_set_pin(dapm, pin, 0);
3366 }
3367 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3368
3369 /**
3370  * snd_soc_dapm_nc_pin - permanently disable pin.
3371  * @dapm: DAPM context
3372  * @pin: pin name
3373  *
3374  * Marks the specified pin as being not connected, disabling it along
3375  * any parent or child widgets.  At present this is identical to
3376  * snd_soc_dapm_disable_pin() but in future it will be extended to do
3377  * additional things such as disabling controls which only affect
3378  * paths through the pin.
3379  *
3380  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3381  * do any widget power switching.
3382  */
3383 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3384 {
3385         return snd_soc_dapm_set_pin(dapm, pin, 0);
3386 }
3387 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3388
3389 /**
3390  * snd_soc_dapm_get_pin_status - get audio pin status
3391  * @dapm: DAPM context
3392  * @pin: audio signal pin endpoint (or start point)
3393  *
3394  * Get audio pin status - connected or disconnected.
3395  *
3396  * Returns 1 for connected otherwise 0.
3397  */
3398 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3399                                 const char *pin)
3400 {
3401         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3402
3403         if (w)
3404                 return w->connected;
3405
3406         return 0;
3407 }
3408 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3409
3410 /**
3411  * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3412  * @dapm: DAPM context
3413  * @pin: audio signal pin endpoint (or start point)
3414  *
3415  * Mark the given endpoint or pin as ignoring suspend.  When the
3416  * system is disabled a path between two endpoints flagged as ignoring
3417  * suspend will not be disabled.  The path must already be enabled via
3418  * normal means at suspend time, it will not be turned on if it was not
3419  * already enabled.
3420  */
3421 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3422                                 const char *pin)
3423 {
3424         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3425
3426         if (!w) {
3427                 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3428                 return -EINVAL;
3429         }
3430
3431         w->ignore_suspend = 1;
3432
3433         return 0;
3434 }
3435 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3436
3437 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3438                                               struct snd_soc_dapm_widget *w)
3439 {
3440         struct snd_soc_dapm_path *p;
3441
3442         list_for_each_entry(p, &card->paths, list) {
3443                 if ((p->source == w) || (p->sink == w)) {
3444                         dev_dbg(card->dev,
3445                             "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3446                             p->source->name, p->source->id, p->source->dapm,
3447                             p->sink->name, p->sink->id, p->sink->dapm);
3448
3449                         /* Connected to something other than the codec */
3450                         if (p->source->dapm != p->sink->dapm)
3451                                 return true;
3452                         /*
3453                          * Loopback connection from codec external pin to
3454                          * codec external pin
3455                          */
3456                         if (p->sink->id == snd_soc_dapm_input) {
3457                                 switch (p->source->id) {
3458                                 case snd_soc_dapm_output:
3459                                 case snd_soc_dapm_micbias:
3460                                         return true;
3461                                 default:
3462                                         break;
3463                                 }
3464                         }
3465                 }
3466         }
3467
3468         return false;
3469 }
3470
3471 /**
3472  * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3473  * @codec: The codec whose pins should be processed
3474  *
3475  * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3476  * which are unused. Pins are used if they are connected externally to the
3477  * codec, whether that be to some other device, or a loop-back connection to
3478  * the codec itself.
3479  */
3480 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3481 {
3482         struct snd_soc_card *card = codec->card;
3483         struct snd_soc_dapm_context *dapm = &codec->dapm;
3484         struct snd_soc_dapm_widget *w;
3485
3486         dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
3487                 &card->dapm, &codec->dapm);
3488
3489         list_for_each_entry(w, &card->widgets, list) {
3490                 if (w->dapm != dapm)
3491                         continue;
3492                 switch (w->id) {
3493                 case snd_soc_dapm_input:
3494                 case snd_soc_dapm_output:
3495                 case snd_soc_dapm_micbias:
3496                         dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
3497                                 w->name);
3498                         if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3499                                 dev_dbg(codec->dev,
3500                                         "... Not in map; disabling\n");
3501                                 snd_soc_dapm_nc_pin(dapm, w->name);
3502                         }
3503                         break;
3504                 default:
3505                         break;
3506                 }
3507         }
3508 }
3509
3510 /**
3511  * snd_soc_dapm_free - free dapm resources
3512  * @dapm: DAPM context
3513  *
3514  * Free all dapm widgets and resources.
3515  */
3516 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3517 {
3518         snd_soc_dapm_sys_remove(dapm->dev);
3519         dapm_debugfs_cleanup(dapm);
3520         dapm_free_widgets(dapm);
3521         list_del(&dapm->list);
3522 }
3523 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3524
3525 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
3526 {
3527         struct snd_soc_dapm_widget *w;
3528         LIST_HEAD(down_list);
3529         int powerdown = 0;
3530
3531         list_for_each_entry(w, &dapm->card->widgets, list) {
3532                 if (w->dapm != dapm)
3533                         continue;
3534                 if (w->power) {
3535                         dapm_seq_insert(w, &down_list, false);
3536                         w->power = 0;
3537                         powerdown = 1;
3538                 }
3539         }
3540
3541         /* If there were no widgets to power down we're already in
3542          * standby.
3543          */
3544         if (powerdown) {
3545                 if (dapm->bias_level == SND_SOC_BIAS_ON)
3546                         snd_soc_dapm_set_bias_level(dapm,
3547                                                     SND_SOC_BIAS_PREPARE);
3548                 dapm_seq_run(dapm, &down_list, 0, false);
3549                 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3550                         snd_soc_dapm_set_bias_level(dapm,
3551                                                     SND_SOC_BIAS_STANDBY);
3552         }
3553 }
3554
3555 /*
3556  * snd_soc_dapm_shutdown - callback for system shutdown
3557  */
3558 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3559 {
3560         struct snd_soc_codec *codec;
3561
3562         list_for_each_entry(codec, &card->codec_dev_list, list) {
3563                 soc_dapm_shutdown_codec(&codec->dapm);
3564                 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3565                         snd_soc_dapm_set_bias_level(&codec->dapm,
3566                                                     SND_SOC_BIAS_OFF);
3567         }
3568 }
3569
3570 /* Module information */
3571 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3572 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3573 MODULE_LICENSE("GPL");