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