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