ASoC: dapm: Fix snd_soc_dapm_put_volsw() connect
[linux-2.6-microblaze.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 }
1023 EXPORT_SYMBOL_GPL(dapm_clock_event);
1024
1025 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1026 {
1027         if (w->power_checked)
1028                 return w->new_power;
1029
1030         if (w->force)
1031                 w->new_power = 1;
1032         else
1033                 w->new_power = w->power_check(w);
1034
1035         w->power_checked = true;
1036
1037         return w->new_power;
1038 }
1039
1040 /* Generic check to see if a widget should be powered.
1041  */
1042 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1043 {
1044         int in, out;
1045
1046         DAPM_UPDATE_STAT(w, power_checks);
1047
1048         in = is_connected_input_ep(w, NULL);
1049         dapm_clear_walk(w->dapm);
1050         out = is_connected_output_ep(w, NULL);
1051         dapm_clear_walk(w->dapm);
1052         return out != 0 && in != 0;
1053 }
1054
1055 static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
1056 {
1057         DAPM_UPDATE_STAT(w, power_checks);
1058
1059         if (w->active)
1060                 return w->active;
1061
1062         return dapm_generic_check_power(w);
1063 }
1064
1065 /* Check to see if an ADC has power */
1066 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1067 {
1068         int in;
1069
1070         DAPM_UPDATE_STAT(w, power_checks);
1071
1072         if (w->active) {
1073                 in = is_connected_input_ep(w, NULL);
1074                 dapm_clear_walk(w->dapm);
1075                 return in != 0;
1076         } else {
1077                 return dapm_generic_check_power(w);
1078         }
1079 }
1080
1081 /* Check to see if a DAC has power */
1082 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1083 {
1084         int out;
1085
1086         DAPM_UPDATE_STAT(w, power_checks);
1087
1088         if (w->active) {
1089                 out = is_connected_output_ep(w, NULL);
1090                 dapm_clear_walk(w->dapm);
1091                 return out != 0;
1092         } else {
1093                 return dapm_generic_check_power(w);
1094         }
1095 }
1096
1097 /* Check to see if a power supply is needed */
1098 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1099 {
1100         struct snd_soc_dapm_path *path;
1101
1102         DAPM_UPDATE_STAT(w, power_checks);
1103
1104         /* Check if one of our outputs is connected */
1105         list_for_each_entry(path, &w->sinks, list_source) {
1106                 DAPM_UPDATE_STAT(w, neighbour_checks);
1107
1108                 if (path->weak)
1109                         continue;
1110
1111                 if (path->connected &&
1112                     !path->connected(path->source, path->sink))
1113                         continue;
1114
1115                 if (!path->sink)
1116                         continue;
1117
1118                 if (dapm_widget_power_check(path->sink))
1119                         return 1;
1120         }
1121
1122         dapm_clear_walk(w->dapm);
1123
1124         return 0;
1125 }
1126
1127 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1128 {
1129         return 1;
1130 }
1131
1132 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1133                             struct snd_soc_dapm_widget *b,
1134                             bool power_up)
1135 {
1136         int *sort;
1137
1138         if (power_up)
1139                 sort = dapm_up_seq;
1140         else
1141                 sort = dapm_down_seq;
1142
1143         if (sort[a->id] != sort[b->id])
1144                 return sort[a->id] - sort[b->id];
1145         if (a->subseq != b->subseq) {
1146                 if (power_up)
1147                         return a->subseq - b->subseq;
1148                 else
1149                         return b->subseq - a->subseq;
1150         }
1151         if (a->reg != b->reg)
1152                 return a->reg - b->reg;
1153         if (a->dapm != b->dapm)
1154                 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1155
1156         return 0;
1157 }
1158
1159 /* Insert a widget in order into a DAPM power sequence. */
1160 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1161                             struct list_head *list,
1162                             bool power_up)
1163 {
1164         struct snd_soc_dapm_widget *w;
1165
1166         list_for_each_entry(w, list, power_list)
1167                 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1168                         list_add_tail(&new_widget->power_list, &w->power_list);
1169                         return;
1170                 }
1171
1172         list_add_tail(&new_widget->power_list, list);
1173 }
1174
1175 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1176                                  struct snd_soc_dapm_widget *w, int event)
1177 {
1178         struct snd_soc_card *card = dapm->card;
1179         const char *ev_name;
1180         int power, ret;
1181
1182         switch (event) {
1183         case SND_SOC_DAPM_PRE_PMU:
1184                 ev_name = "PRE_PMU";
1185                 power = 1;
1186                 break;
1187         case SND_SOC_DAPM_POST_PMU:
1188                 ev_name = "POST_PMU";
1189                 power = 1;
1190                 break;
1191         case SND_SOC_DAPM_PRE_PMD:
1192                 ev_name = "PRE_PMD";
1193                 power = 0;
1194                 break;
1195         case SND_SOC_DAPM_POST_PMD:
1196                 ev_name = "POST_PMD";
1197                 power = 0;
1198                 break;
1199         default:
1200                 BUG();
1201                 return;
1202         }
1203
1204         if (w->power != power)
1205                 return;
1206
1207         if (w->event && (w->event_flags & event)) {
1208                 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1209                         w->name, ev_name);
1210                 trace_snd_soc_dapm_widget_event_start(w, event);
1211                 ret = w->event(w, NULL, event);
1212                 trace_snd_soc_dapm_widget_event_done(w, event);
1213                 if (ret < 0)
1214                         pr_err("%s: %s event failed: %d\n",
1215                                ev_name, w->name, ret);
1216         }
1217 }
1218
1219 /* Apply the coalesced changes from a DAPM sequence */
1220 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
1221                                    struct list_head *pending)
1222 {
1223         struct snd_soc_card *card = dapm->card;
1224         struct snd_soc_dapm_widget *w;
1225         int reg, power;
1226         unsigned int value = 0;
1227         unsigned int mask = 0;
1228         unsigned int cur_mask;
1229
1230         reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1231                                power_list)->reg;
1232
1233         list_for_each_entry(w, pending, power_list) {
1234                 cur_mask = 1 << w->shift;
1235                 BUG_ON(reg != w->reg);
1236
1237                 if (w->invert)
1238                         power = !w->power;
1239                 else
1240                         power = w->power;
1241
1242                 mask |= cur_mask;
1243                 if (power)
1244                         value |= cur_mask;
1245
1246                 pop_dbg(dapm->dev, card->pop_time,
1247                         "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1248                         w->name, reg, value, mask);
1249
1250                 /* Check for events */
1251                 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1252                 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1253         }
1254
1255         if (reg >= 0) {
1256                 /* Any widget will do, they should all be updating the
1257                  * same register.
1258                  */
1259                 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1260                                      power_list);
1261
1262                 pop_dbg(dapm->dev, card->pop_time,
1263                         "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1264                         value, mask, reg, card->pop_time);
1265                 pop_wait(card->pop_time);
1266                 soc_widget_update_bits_locked(w, reg, mask, value);
1267         }
1268
1269         list_for_each_entry(w, pending, power_list) {
1270                 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1271                 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1272         }
1273 }
1274
1275 /* Apply a DAPM power sequence.
1276  *
1277  * We walk over a pre-sorted list of widgets to apply power to.  In
1278  * order to minimise the number of writes to the device required
1279  * multiple widgets will be updated in a single write where possible.
1280  * Currently anything that requires more than a single write is not
1281  * handled.
1282  */
1283 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1284                          struct list_head *list, int event, bool power_up)
1285 {
1286         struct snd_soc_dapm_widget *w, *n;
1287         LIST_HEAD(pending);
1288         int cur_sort = -1;
1289         int cur_subseq = -1;
1290         int cur_reg = SND_SOC_NOPM;
1291         struct snd_soc_dapm_context *cur_dapm = NULL;
1292         int ret, i;
1293         int *sort;
1294
1295         if (power_up)
1296                 sort = dapm_up_seq;
1297         else
1298                 sort = dapm_down_seq;
1299
1300         list_for_each_entry_safe(w, n, list, power_list) {
1301                 ret = 0;
1302
1303                 /* Do we need to apply any queued changes? */
1304                 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1305                     w->dapm != cur_dapm || w->subseq != cur_subseq) {
1306                         if (!list_empty(&pending))
1307                                 dapm_seq_run_coalesced(cur_dapm, &pending);
1308
1309                         if (cur_dapm && cur_dapm->seq_notifier) {
1310                                 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1311                                         if (sort[i] == cur_sort)
1312                                                 cur_dapm->seq_notifier(cur_dapm,
1313                                                                        i,
1314                                                                        cur_subseq);
1315                         }
1316
1317                         INIT_LIST_HEAD(&pending);
1318                         cur_sort = -1;
1319                         cur_subseq = INT_MIN;
1320                         cur_reg = SND_SOC_NOPM;
1321                         cur_dapm = NULL;
1322                 }
1323
1324                 switch (w->id) {
1325                 case snd_soc_dapm_pre:
1326                         if (!w->event)
1327                                 list_for_each_entry_safe_continue(w, n, list,
1328                                                                   power_list);
1329
1330                         if (event == SND_SOC_DAPM_STREAM_START)
1331                                 ret = w->event(w,
1332                                                NULL, SND_SOC_DAPM_PRE_PMU);
1333                         else if (event == SND_SOC_DAPM_STREAM_STOP)
1334                                 ret = w->event(w,
1335                                                NULL, SND_SOC_DAPM_PRE_PMD);
1336                         break;
1337
1338                 case snd_soc_dapm_post:
1339                         if (!w->event)
1340                                 list_for_each_entry_safe_continue(w, n, list,
1341                                                                   power_list);
1342
1343                         if (event == SND_SOC_DAPM_STREAM_START)
1344                                 ret = w->event(w,
1345                                                NULL, SND_SOC_DAPM_POST_PMU);
1346                         else if (event == SND_SOC_DAPM_STREAM_STOP)
1347                                 ret = w->event(w,
1348                                                NULL, SND_SOC_DAPM_POST_PMD);
1349                         break;
1350
1351                 default:
1352                         /* Queue it up for application */
1353                         cur_sort = sort[w->id];
1354                         cur_subseq = w->subseq;
1355                         cur_reg = w->reg;
1356                         cur_dapm = w->dapm;
1357                         list_move(&w->power_list, &pending);
1358                         break;
1359                 }
1360
1361                 if (ret < 0)
1362                         dev_err(w->dapm->dev,
1363                                 "Failed to apply widget power: %d\n", ret);
1364         }
1365
1366         if (!list_empty(&pending))
1367                 dapm_seq_run_coalesced(cur_dapm, &pending);
1368
1369         if (cur_dapm && cur_dapm->seq_notifier) {
1370                 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1371                         if (sort[i] == cur_sort)
1372                                 cur_dapm->seq_notifier(cur_dapm,
1373                                                        i, cur_subseq);
1374         }
1375 }
1376
1377 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1378 {
1379         struct snd_soc_dapm_update *update = dapm->update;
1380         struct snd_soc_dapm_widget *w;
1381         int ret;
1382
1383         if (!update)
1384                 return;
1385
1386         w = update->widget;
1387
1388         if (w->event &&
1389             (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1390                 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1391                 if (ret != 0)
1392                         pr_err("%s DAPM pre-event failed: %d\n",
1393                                w->name, ret);
1394         }
1395
1396         ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
1397                                   update->val);
1398         if (ret < 0)
1399                 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1400
1401         if (w->event &&
1402             (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1403                 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1404                 if (ret != 0)
1405                         pr_err("%s DAPM post-event failed: %d\n",
1406                                w->name, ret);
1407         }
1408 }
1409
1410 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1411  * they're changing state.
1412  */
1413 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1414 {
1415         struct snd_soc_dapm_context *d = data;
1416         int ret;
1417
1418         /* If we're off and we're not supposed to be go into STANDBY */
1419         if (d->bias_level == SND_SOC_BIAS_OFF &&
1420             d->target_bias_level != SND_SOC_BIAS_OFF) {
1421                 if (d->dev)
1422                         pm_runtime_get_sync(d->dev);
1423
1424                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1425                 if (ret != 0)
1426                         dev_err(d->dev,
1427                                 "Failed to turn on bias: %d\n", ret);
1428         }
1429
1430         /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1431         if (d->bias_level != d->target_bias_level) {
1432                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1433                 if (ret != 0)
1434                         dev_err(d->dev,
1435                                 "Failed to prepare bias: %d\n", ret);
1436         }
1437 }
1438
1439 /* Async callback run prior to DAPM sequences - brings to their final
1440  * state.
1441  */
1442 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1443 {
1444         struct snd_soc_dapm_context *d = data;
1445         int ret;
1446
1447         /* If we just powered the last thing off drop to standby bias */
1448         if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1449             (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1450              d->target_bias_level == SND_SOC_BIAS_OFF)) {
1451                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1452                 if (ret != 0)
1453                         dev_err(d->dev, "Failed to apply standby bias: %d\n",
1454                                 ret);
1455         }
1456
1457         /* If we're in standby and can support bias off then do that */
1458         if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1459             d->target_bias_level == SND_SOC_BIAS_OFF) {
1460                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1461                 if (ret != 0)
1462                         dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1463
1464                 if (d->dev)
1465                         pm_runtime_put(d->dev);
1466         }
1467
1468         /* If we just powered up then move to active bias */
1469         if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1470             d->target_bias_level == SND_SOC_BIAS_ON) {
1471                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1472                 if (ret != 0)
1473                         dev_err(d->dev, "Failed to apply active bias: %d\n",
1474                                 ret);
1475         }
1476 }
1477
1478 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1479                                        bool power, bool connect)
1480 {
1481         /* If a connection is being made or broken then that update
1482          * will have marked the peer dirty, otherwise the widgets are
1483          * not connected and this update has no impact. */
1484         if (!connect)
1485                 return;
1486
1487         /* If the peer is already in the state we're moving to then we
1488          * won't have an impact on it. */
1489         if (power != peer->power)
1490                 dapm_mark_dirty(peer, "peer state change");
1491 }
1492
1493 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1494                                   struct list_head *up_list,
1495                                   struct list_head *down_list)
1496 {
1497         struct snd_soc_dapm_path *path;
1498
1499         if (w->power == power)
1500                 return;
1501
1502         trace_snd_soc_dapm_widget_power(w, power);
1503
1504         /* If we changed our power state perhaps our neigbours changed
1505          * also.
1506          */
1507         list_for_each_entry(path, &w->sources, list_sink) {
1508                 if (path->source) {
1509                         dapm_widget_set_peer_power(path->source, power,
1510                                                    path->connect);
1511                 }
1512         }
1513         switch (w->id) {
1514         case snd_soc_dapm_supply:
1515         case snd_soc_dapm_regulator_supply:
1516         case snd_soc_dapm_clock_supply:
1517                 /* Supplies can't affect their outputs, only their inputs */
1518                 break;
1519         default:
1520                 list_for_each_entry(path, &w->sinks, list_source) {
1521                         if (path->sink) {
1522                                 dapm_widget_set_peer_power(path->sink, power,
1523                                                            path->connect);
1524                         }
1525                 }
1526                 break;
1527         }
1528
1529         if (power)
1530                 dapm_seq_insert(w, up_list, true);
1531         else
1532                 dapm_seq_insert(w, down_list, false);
1533
1534         w->power = power;
1535 }
1536
1537 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1538                                   struct list_head *up_list,
1539                                   struct list_head *down_list)
1540 {
1541         int power;
1542
1543         switch (w->id) {
1544         case snd_soc_dapm_pre:
1545                 dapm_seq_insert(w, down_list, false);
1546                 break;
1547         case snd_soc_dapm_post:
1548                 dapm_seq_insert(w, up_list, true);
1549                 break;
1550
1551         default:
1552                 power = dapm_widget_power_check(w);
1553
1554                 dapm_widget_set_power(w, power, up_list, down_list);
1555                 break;
1556         }
1557 }
1558
1559 /*
1560  * Scan each dapm widget for complete audio path.
1561  * A complete path is a route that has valid endpoints i.e.:-
1562  *
1563  *  o DAC to output pin.
1564  *  o Input Pin to ADC.
1565  *  o Input pin to Output pin (bypass, sidetone)
1566  *  o DAC to ADC (loopback).
1567  */
1568 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1569 {
1570         struct snd_soc_card *card = dapm->card;
1571         struct snd_soc_dapm_widget *w;
1572         struct snd_soc_dapm_context *d;
1573         LIST_HEAD(up_list);
1574         LIST_HEAD(down_list);
1575         LIST_HEAD(async_domain);
1576         enum snd_soc_bias_level bias;
1577
1578         trace_snd_soc_dapm_start(card);
1579
1580         list_for_each_entry(d, &card->dapm_list, list) {
1581                 if (d->idle_bias_off)
1582                         d->target_bias_level = SND_SOC_BIAS_OFF;
1583                 else
1584                         d->target_bias_level = SND_SOC_BIAS_STANDBY;
1585         }
1586
1587         dapm_reset(card);
1588
1589         /* Check which widgets we need to power and store them in
1590          * lists indicating if they should be powered up or down.  We
1591          * only check widgets that have been flagged as dirty but note
1592          * that new widgets may be added to the dirty list while we
1593          * iterate.
1594          */
1595         list_for_each_entry(w, &card->dapm_dirty, dirty) {
1596                 dapm_power_one_widget(w, &up_list, &down_list);
1597         }
1598
1599         list_for_each_entry(w, &card->widgets, list) {
1600                 list_del_init(&w->dirty);
1601
1602                 if (w->power) {
1603                         d = w->dapm;
1604
1605                         /* Supplies and micbiases only bring the
1606                          * context up to STANDBY as unless something
1607                          * else is active and passing audio they
1608                          * generally don't require full power.  Signal
1609                          * generators are virtual pins and have no
1610                          * power impact themselves.
1611                          */
1612                         switch (w->id) {
1613                         case snd_soc_dapm_siggen:
1614                                 break;
1615                         case snd_soc_dapm_supply:
1616                         case snd_soc_dapm_regulator_supply:
1617                         case snd_soc_dapm_clock_supply:
1618                         case snd_soc_dapm_micbias:
1619                                 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1620                                         d->target_bias_level = SND_SOC_BIAS_STANDBY;
1621                                 break;
1622                         default:
1623                                 d->target_bias_level = SND_SOC_BIAS_ON;
1624                                 break;
1625                         }
1626                 }
1627
1628         }
1629
1630         /* Force all contexts in the card to the same bias state if
1631          * they're not ground referenced.
1632          */
1633         bias = SND_SOC_BIAS_OFF;
1634         list_for_each_entry(d, &card->dapm_list, list)
1635                 if (d->target_bias_level > bias)
1636                         bias = d->target_bias_level;
1637         list_for_each_entry(d, &card->dapm_list, list)
1638                 if (!d->idle_bias_off)
1639                         d->target_bias_level = bias;
1640
1641         trace_snd_soc_dapm_walk_done(card);
1642
1643         /* Run all the bias changes in parallel */
1644         list_for_each_entry(d, &dapm->card->dapm_list, list)
1645                 async_schedule_domain(dapm_pre_sequence_async, d,
1646                                         &async_domain);
1647         async_synchronize_full_domain(&async_domain);
1648
1649         /* Power down widgets first; try to avoid amplifying pops. */
1650         dapm_seq_run(dapm, &down_list, event, false);
1651
1652         dapm_widget_update(dapm);
1653
1654         /* Now power up. */
1655         dapm_seq_run(dapm, &up_list, event, true);
1656
1657         /* Run all the bias changes in parallel */
1658         list_for_each_entry(d, &dapm->card->dapm_list, list)
1659                 async_schedule_domain(dapm_post_sequence_async, d,
1660                                         &async_domain);
1661         async_synchronize_full_domain(&async_domain);
1662
1663         /* do we need to notify any clients that DAPM event is complete */
1664         list_for_each_entry(d, &card->dapm_list, list) {
1665                 if (d->stream_event)
1666                         d->stream_event(d, event);
1667         }
1668
1669         pop_dbg(dapm->dev, card->pop_time,
1670                 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1671         pop_wait(card->pop_time);
1672
1673         trace_snd_soc_dapm_done(card);
1674
1675         return 0;
1676 }
1677
1678 #ifdef CONFIG_DEBUG_FS
1679 static ssize_t dapm_widget_power_read_file(struct file *file,
1680                                            char __user *user_buf,
1681                                            size_t count, loff_t *ppos)
1682 {
1683         struct snd_soc_dapm_widget *w = file->private_data;
1684         char *buf;
1685         int in, out;
1686         ssize_t ret;
1687         struct snd_soc_dapm_path *p = NULL;
1688
1689         buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1690         if (!buf)
1691                 return -ENOMEM;
1692
1693         in = is_connected_input_ep(w, NULL);
1694         dapm_clear_walk(w->dapm);
1695         out = is_connected_output_ep(w, NULL);
1696         dapm_clear_walk(w->dapm);
1697
1698         ret = snprintf(buf, PAGE_SIZE, "%s: %s%s  in %d out %d",
1699                        w->name, w->power ? "On" : "Off",
1700                        w->force ? " (forced)" : "", in, out);
1701
1702         if (w->reg >= 0)
1703                 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1704                                 " - R%d(0x%x) bit %d",
1705                                 w->reg, w->reg, w->shift);
1706
1707         ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1708
1709         if (w->sname)
1710                 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1711                                 w->sname,
1712                                 w->active ? "active" : "inactive");
1713
1714         list_for_each_entry(p, &w->sources, list_sink) {
1715                 if (p->connected && !p->connected(w, p->sink))
1716                         continue;
1717
1718                 if (p->connect)
1719                         ret += snprintf(buf + ret, PAGE_SIZE - ret,
1720                                         " in  \"%s\" \"%s\"\n",
1721                                         p->name ? p->name : "static",
1722                                         p->source->name);
1723         }
1724         list_for_each_entry(p, &w->sinks, list_source) {
1725                 if (p->connected && !p->connected(w, p->sink))
1726                         continue;
1727
1728                 if (p->connect)
1729                         ret += snprintf(buf + ret, PAGE_SIZE - ret,
1730                                         " out \"%s\" \"%s\"\n",
1731                                         p->name ? p->name : "static",
1732                                         p->sink->name);
1733         }
1734
1735         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1736
1737         kfree(buf);
1738         return ret;
1739 }
1740
1741 static const struct file_operations dapm_widget_power_fops = {
1742         .open = simple_open,
1743         .read = dapm_widget_power_read_file,
1744         .llseek = default_llseek,
1745 };
1746
1747 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1748                                    size_t count, loff_t *ppos)
1749 {
1750         struct snd_soc_dapm_context *dapm = file->private_data;
1751         char *level;
1752
1753         switch (dapm->bias_level) {
1754         case SND_SOC_BIAS_ON:
1755                 level = "On\n";
1756                 break;
1757         case SND_SOC_BIAS_PREPARE:
1758                 level = "Prepare\n";
1759                 break;
1760         case SND_SOC_BIAS_STANDBY:
1761                 level = "Standby\n";
1762                 break;
1763         case SND_SOC_BIAS_OFF:
1764                 level = "Off\n";
1765                 break;
1766         default:
1767                 BUG();
1768                 level = "Unknown\n";
1769                 break;
1770         }
1771
1772         return simple_read_from_buffer(user_buf, count, ppos, level,
1773                                        strlen(level));
1774 }
1775
1776 static const struct file_operations dapm_bias_fops = {
1777         .open = simple_open,
1778         .read = dapm_bias_read_file,
1779         .llseek = default_llseek,
1780 };
1781
1782 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1783         struct dentry *parent)
1784 {
1785         struct dentry *d;
1786
1787         dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1788
1789         if (!dapm->debugfs_dapm) {
1790                 dev_warn(dapm->dev,
1791                        "Failed to create DAPM debugfs directory\n");
1792                 return;
1793         }
1794
1795         d = debugfs_create_file("bias_level", 0444,
1796                                 dapm->debugfs_dapm, dapm,
1797                                 &dapm_bias_fops);
1798         if (!d)
1799                 dev_warn(dapm->dev,
1800                          "ASoC: Failed to create bias level debugfs file\n");
1801 }
1802
1803 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1804 {
1805         struct snd_soc_dapm_context *dapm = w->dapm;
1806         struct dentry *d;
1807
1808         if (!dapm->debugfs_dapm || !w->name)
1809                 return;
1810
1811         d = debugfs_create_file(w->name, 0444,
1812                                 dapm->debugfs_dapm, w,
1813                                 &dapm_widget_power_fops);
1814         if (!d)
1815                 dev_warn(w->dapm->dev,
1816                         "ASoC: Failed to create %s debugfs file\n",
1817                         w->name);
1818 }
1819
1820 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1821 {
1822         debugfs_remove_recursive(dapm->debugfs_dapm);
1823 }
1824
1825 #else
1826 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1827         struct dentry *parent)
1828 {
1829 }
1830
1831 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1832 {
1833 }
1834
1835 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1836 {
1837 }
1838
1839 #endif
1840
1841 /* test and update the power status of a mux widget */
1842 static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1843                                  struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1844 {
1845         struct snd_soc_dapm_path *path;
1846         int found = 0;
1847
1848         if (widget->id != snd_soc_dapm_mux &&
1849             widget->id != snd_soc_dapm_virt_mux &&
1850             widget->id != snd_soc_dapm_value_mux)
1851                 return -ENODEV;
1852
1853         /* find dapm widget path assoc with kcontrol */
1854         list_for_each_entry(path, &widget->dapm->card->paths, list) {
1855                 if (path->kcontrol != kcontrol)
1856                         continue;
1857
1858                 if (!path->name || !e->texts[mux])
1859                         continue;
1860
1861                 found = 1;
1862                 /* we now need to match the string in the enum to the path */
1863                 if (!(strcmp(path->name, e->texts[mux]))) {
1864                         path->connect = 1; /* new connection */
1865                         dapm_mark_dirty(path->source, "mux connection");
1866                 } else {
1867                         if (path->connect)
1868                                 dapm_mark_dirty(path->source,
1869                                                 "mux disconnection");
1870                         path->connect = 0; /* old connection must be powered down */
1871                 }
1872         }
1873
1874         if (found) {
1875                 dapm_mark_dirty(widget, "mux change");
1876                 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1877         }
1878
1879         return found;
1880 }
1881
1882 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1883                 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1884 {
1885         struct snd_soc_card *card = widget->dapm->card;
1886         int ret;
1887
1888         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1889         ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1890         mutex_unlock(&card->dapm_mutex);
1891         if (ret > 0)
1892                 soc_dpcm_runtime_update(widget);
1893         return ret;
1894 }
1895 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
1896
1897 /* test and update the power status of a mixer or switch widget */
1898 static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1899                                    struct snd_kcontrol *kcontrol, int connect)
1900 {
1901         struct snd_soc_dapm_path *path;
1902         int found = 0;
1903
1904         if (widget->id != snd_soc_dapm_mixer &&
1905             widget->id != snd_soc_dapm_mixer_named_ctl &&
1906             widget->id != snd_soc_dapm_switch)
1907                 return -ENODEV;
1908
1909         /* find dapm widget path assoc with kcontrol */
1910         list_for_each_entry(path, &widget->dapm->card->paths, list) {
1911                 if (path->kcontrol != kcontrol)
1912                         continue;
1913
1914                 /* found, now check type */
1915                 found = 1;
1916                 path->connect = connect;
1917                 dapm_mark_dirty(path->source, "mixer connection");
1918         }
1919
1920         if (found) {
1921                 dapm_mark_dirty(widget, "mixer update");
1922                 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1923         }
1924
1925         return found;
1926 }
1927
1928 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1929                                 struct snd_kcontrol *kcontrol, int connect)
1930 {
1931         struct snd_soc_card *card = widget->dapm->card;
1932         int ret;
1933
1934         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1935         ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
1936         mutex_unlock(&card->dapm_mutex);
1937         if (ret > 0)
1938                 soc_dpcm_runtime_update(widget);
1939         return ret;
1940 }
1941 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
1942
1943 /* show dapm widget status in sys fs */
1944 static ssize_t dapm_widget_show(struct device *dev,
1945         struct device_attribute *attr, char *buf)
1946 {
1947         struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
1948         struct snd_soc_codec *codec =rtd->codec;
1949         struct snd_soc_dapm_widget *w;
1950         int count = 0;
1951         char *state = "not set";
1952
1953         list_for_each_entry(w, &codec->card->widgets, list) {
1954                 if (w->dapm != &codec->dapm)
1955                         continue;
1956
1957                 /* only display widgets that burnm power */
1958                 switch (w->id) {
1959                 case snd_soc_dapm_hp:
1960                 case snd_soc_dapm_mic:
1961                 case snd_soc_dapm_spk:
1962                 case snd_soc_dapm_line:
1963                 case snd_soc_dapm_micbias:
1964                 case snd_soc_dapm_dac:
1965                 case snd_soc_dapm_adc:
1966                 case snd_soc_dapm_pga:
1967                 case snd_soc_dapm_out_drv:
1968                 case snd_soc_dapm_mixer:
1969                 case snd_soc_dapm_mixer_named_ctl:
1970                 case snd_soc_dapm_supply:
1971                 case snd_soc_dapm_regulator_supply:
1972                 case snd_soc_dapm_clock_supply:
1973                         if (w->name)
1974                                 count += sprintf(buf + count, "%s: %s\n",
1975                                         w->name, w->power ? "On":"Off");
1976                 break;
1977                 default:
1978                 break;
1979                 }
1980         }
1981
1982         switch (codec->dapm.bias_level) {
1983         case SND_SOC_BIAS_ON:
1984                 state = "On";
1985                 break;
1986         case SND_SOC_BIAS_PREPARE:
1987                 state = "Prepare";
1988                 break;
1989         case SND_SOC_BIAS_STANDBY:
1990                 state = "Standby";
1991                 break;
1992         case SND_SOC_BIAS_OFF:
1993                 state = "Off";
1994                 break;
1995         }
1996         count += sprintf(buf + count, "PM State: %s\n", state);
1997
1998         return count;
1999 }
2000
2001 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2002
2003 int snd_soc_dapm_sys_add(struct device *dev)
2004 {
2005         return device_create_file(dev, &dev_attr_dapm_widget);
2006 }
2007
2008 static void snd_soc_dapm_sys_remove(struct device *dev)
2009 {
2010         device_remove_file(dev, &dev_attr_dapm_widget);
2011 }
2012
2013 /* free all dapm widgets and resources */
2014 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2015 {
2016         struct snd_soc_dapm_widget *w, *next_w;
2017         struct snd_soc_dapm_path *p, *next_p;
2018
2019         list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2020                 if (w->dapm != dapm)
2021                         continue;
2022                 list_del(&w->list);
2023                 /*
2024                  * remove source and sink paths associated to this widget.
2025                  * While removing the path, remove reference to it from both
2026                  * source and sink widgets so that path is removed only once.
2027                  */
2028                 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
2029                         list_del(&p->list_sink);
2030                         list_del(&p->list_source);
2031                         list_del(&p->list);
2032                         kfree(p->long_name);
2033                         kfree(p);
2034                 }
2035                 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
2036                         list_del(&p->list_sink);
2037                         list_del(&p->list_source);
2038                         list_del(&p->list);
2039                         kfree(p->long_name);
2040                         kfree(p);
2041                 }
2042                 kfree(w->kcontrols);
2043                 kfree(w->name);
2044                 kfree(w);
2045         }
2046 }
2047
2048 static struct snd_soc_dapm_widget *dapm_find_widget(
2049                         struct snd_soc_dapm_context *dapm, const char *pin,
2050                         bool search_other_contexts)
2051 {
2052         struct snd_soc_dapm_widget *w;
2053         struct snd_soc_dapm_widget *fallback = NULL;
2054
2055         list_for_each_entry(w, &dapm->card->widgets, list) {
2056                 if (!strcmp(w->name, pin)) {
2057                         if (w->dapm == dapm)
2058                                 return w;
2059                         else
2060                                 fallback = w;
2061                 }
2062         }
2063
2064         if (search_other_contexts)
2065                 return fallback;
2066
2067         return NULL;
2068 }
2069
2070 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2071                                 const char *pin, int status)
2072 {
2073         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2074
2075         if (!w) {
2076                 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2077                 return -EINVAL;
2078         }
2079
2080         if (w->connected != status)
2081                 dapm_mark_dirty(w, "pin configuration");
2082
2083         w->connected = status;
2084         if (status == 0)
2085                 w->force = 0;
2086
2087         return 0;
2088 }
2089
2090 /**
2091  * snd_soc_dapm_sync - scan and power dapm paths
2092  * @dapm: DAPM context
2093  *
2094  * Walks all dapm audio paths and powers widgets according to their
2095  * stream or path usage.
2096  *
2097  * Returns 0 for success.
2098  */
2099 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2100 {
2101         int ret;
2102
2103         /*
2104          * Suppress early reports (eg, jacks syncing their state) to avoid
2105          * silly DAPM runs during card startup.
2106          */
2107         if (!dapm->card || !dapm->card->instantiated)
2108                 return 0;
2109
2110         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2111         ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2112         mutex_unlock(&dapm->card->dapm_mutex);
2113         return ret;
2114 }
2115 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2116
2117 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2118                                   const struct snd_soc_dapm_route *route)
2119 {
2120         struct snd_soc_dapm_path *path;
2121         struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2122         struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2123         const char *sink;
2124         const char *control = route->control;
2125         const char *source;
2126         char prefixed_sink[80];
2127         char prefixed_source[80];
2128         int ret = 0;
2129
2130         if (dapm->codec && dapm->codec->name_prefix) {
2131                 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2132                          dapm->codec->name_prefix, route->sink);
2133                 sink = prefixed_sink;
2134                 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2135                          dapm->codec->name_prefix, route->source);
2136                 source = prefixed_source;
2137         } else {
2138                 sink = route->sink;
2139                 source = route->source;
2140         }
2141
2142         /*
2143          * find src and dest widgets over all widgets but favor a widget from
2144          * current DAPM context
2145          */
2146         list_for_each_entry(w, &dapm->card->widgets, list) {
2147                 if (!wsink && !(strcmp(w->name, sink))) {
2148                         wtsink = w;
2149                         if (w->dapm == dapm)
2150                                 wsink = w;
2151                         continue;
2152                 }
2153                 if (!wsource && !(strcmp(w->name, source))) {
2154                         wtsource = w;
2155                         if (w->dapm == dapm)
2156                                 wsource = w;
2157                 }
2158         }
2159         /* use widget from another DAPM context if not found from this */
2160         if (!wsink)
2161                 wsink = wtsink;
2162         if (!wsource)
2163                 wsource = wtsource;
2164
2165         if (wsource == NULL || wsink == NULL)
2166                 return -ENODEV;
2167
2168         path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2169         if (!path)
2170                 return -ENOMEM;
2171
2172         path->source = wsource;
2173         path->sink = wsink;
2174         path->connected = route->connected;
2175         INIT_LIST_HEAD(&path->list);
2176         INIT_LIST_HEAD(&path->list_source);
2177         INIT_LIST_HEAD(&path->list_sink);
2178
2179         /* check for external widgets */
2180         if (wsink->id == snd_soc_dapm_input) {
2181                 if (wsource->id == snd_soc_dapm_micbias ||
2182                         wsource->id == snd_soc_dapm_mic ||
2183                         wsource->id == snd_soc_dapm_line ||
2184                         wsource->id == snd_soc_dapm_output)
2185                         wsink->ext = 1;
2186         }
2187         if (wsource->id == snd_soc_dapm_output) {
2188                 if (wsink->id == snd_soc_dapm_spk ||
2189                         wsink->id == snd_soc_dapm_hp ||
2190                         wsink->id == snd_soc_dapm_line ||
2191                         wsink->id == snd_soc_dapm_input)
2192                         wsource->ext = 1;
2193         }
2194
2195         /* connect static paths */
2196         if (control == NULL) {
2197                 list_add(&path->list, &dapm->card->paths);
2198                 list_add(&path->list_sink, &wsink->sources);
2199                 list_add(&path->list_source, &wsource->sinks);
2200                 path->connect = 1;
2201                 return 0;
2202         }
2203
2204         /* connect dynamic paths */
2205         switch (wsink->id) {
2206         case snd_soc_dapm_adc:
2207         case snd_soc_dapm_dac:
2208         case snd_soc_dapm_pga:
2209         case snd_soc_dapm_out_drv:
2210         case snd_soc_dapm_input:
2211         case snd_soc_dapm_output:
2212         case snd_soc_dapm_siggen:
2213         case snd_soc_dapm_micbias:
2214         case snd_soc_dapm_vmid:
2215         case snd_soc_dapm_pre:
2216         case snd_soc_dapm_post:
2217         case snd_soc_dapm_supply:
2218         case snd_soc_dapm_regulator_supply:
2219         case snd_soc_dapm_clock_supply:
2220         case snd_soc_dapm_aif_in:
2221         case snd_soc_dapm_aif_out:
2222         case snd_soc_dapm_dai:
2223         case snd_soc_dapm_dai_link:
2224                 list_add(&path->list, &dapm->card->paths);
2225                 list_add(&path->list_sink, &wsink->sources);
2226                 list_add(&path->list_source, &wsource->sinks);
2227                 path->connect = 1;
2228                 return 0;
2229         case snd_soc_dapm_mux:
2230         case snd_soc_dapm_virt_mux:
2231         case snd_soc_dapm_value_mux:
2232                 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2233                         &wsink->kcontrol_news[0]);
2234                 if (ret != 0)
2235                         goto err;
2236                 break;
2237         case snd_soc_dapm_switch:
2238         case snd_soc_dapm_mixer:
2239         case snd_soc_dapm_mixer_named_ctl:
2240                 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2241                 if (ret != 0)
2242                         goto err;
2243                 break;
2244         case snd_soc_dapm_hp:
2245         case snd_soc_dapm_mic:
2246         case snd_soc_dapm_line:
2247         case snd_soc_dapm_spk:
2248                 list_add(&path->list, &dapm->card->paths);
2249                 list_add(&path->list_sink, &wsink->sources);
2250                 list_add(&path->list_source, &wsource->sinks);
2251                 path->connect = 0;
2252                 return 0;
2253         }
2254         return 0;
2255
2256 err:
2257         dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2258                  source, control, sink);
2259         kfree(path);
2260         return ret;
2261 }
2262
2263 /**
2264  * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2265  * @dapm: DAPM context
2266  * @route: audio routes
2267  * @num: number of routes
2268  *
2269  * Connects 2 dapm widgets together via a named audio path. The sink is
2270  * the widget receiving the audio signal, whilst the source is the sender
2271  * of the audio signal.
2272  *
2273  * Returns 0 for success else error. On error all resources can be freed
2274  * with a call to snd_soc_card_free().
2275  */
2276 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2277                             const struct snd_soc_dapm_route *route, int num)
2278 {
2279         int i, r, ret = 0;
2280
2281         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2282         for (i = 0; i < num; i++) {
2283                 r = snd_soc_dapm_add_route(dapm, route);
2284                 if (r < 0) {
2285                         dev_err(dapm->dev, "Failed to add route %s->%s\n",
2286                                 route->source, route->sink);
2287                         ret = r;
2288                 }
2289                 route++;
2290         }
2291         mutex_unlock(&dapm->card->dapm_mutex);
2292
2293         return ret;
2294 }
2295 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2296
2297 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2298                                    const struct snd_soc_dapm_route *route)
2299 {
2300         struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2301                                                               route->source,
2302                                                               true);
2303         struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2304                                                             route->sink,
2305                                                             true);
2306         struct snd_soc_dapm_path *path;
2307         int count = 0;
2308
2309         if (!source) {
2310                 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2311                         route->source);
2312                 return -ENODEV;
2313         }
2314
2315         if (!sink) {
2316                 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2317                         route->sink);
2318                 return -ENODEV;
2319         }
2320
2321         if (route->control || route->connected)
2322                 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2323                          route->source, route->sink);
2324
2325         list_for_each_entry(path, &source->sinks, list_source) {
2326                 if (path->sink == sink) {
2327                         path->weak = 1;
2328                         count++;
2329                 }
2330         }
2331
2332         if (count == 0)
2333                 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2334                         route->source, route->sink);
2335         if (count > 1)
2336                 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2337                          count, route->source, route->sink);
2338
2339         return 0;
2340 }
2341
2342 /**
2343  * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2344  * @dapm: DAPM context
2345  * @route: audio routes
2346  * @num: number of routes
2347  *
2348  * Mark existing routes matching those specified in the passed array
2349  * as being weak, meaning that they are ignored for the purpose of
2350  * power decisions.  The main intended use case is for sidetone paths
2351  * which couple audio between other independent paths if they are both
2352  * active in order to make the combination work better at the user
2353  * level but which aren't intended to be "used".
2354  *
2355  * Note that CODEC drivers should not use this as sidetone type paths
2356  * can frequently also be used as bypass paths.
2357  */
2358 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2359                              const struct snd_soc_dapm_route *route, int num)
2360 {
2361         int i, err;
2362         int ret = 0;
2363
2364         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2365         for (i = 0; i < num; i++) {
2366                 err = snd_soc_dapm_weak_route(dapm, route);
2367                 if (err)
2368                         ret = err;
2369                 route++;
2370         }
2371         mutex_unlock(&dapm->card->dapm_mutex);
2372
2373         return ret;
2374 }
2375 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2376
2377 /**
2378  * snd_soc_dapm_new_widgets - add new dapm widgets
2379  * @dapm: DAPM context
2380  *
2381  * Checks the codec for any new dapm widgets and creates them if found.
2382  *
2383  * Returns 0 for success.
2384  */
2385 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2386 {
2387         struct snd_soc_dapm_widget *w;
2388         unsigned int val;
2389
2390         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2391
2392         list_for_each_entry(w, &dapm->card->widgets, list)
2393         {
2394                 if (w->new)
2395                         continue;
2396
2397                 if (w->num_kcontrols) {
2398                         w->kcontrols = kzalloc(w->num_kcontrols *
2399                                                 sizeof(struct snd_kcontrol *),
2400                                                 GFP_KERNEL);
2401                         if (!w->kcontrols) {
2402                                 mutex_unlock(&dapm->card->dapm_mutex);
2403                                 return -ENOMEM;
2404                         }
2405                 }
2406
2407                 switch(w->id) {
2408                 case snd_soc_dapm_switch:
2409                 case snd_soc_dapm_mixer:
2410                 case snd_soc_dapm_mixer_named_ctl:
2411                         dapm_new_mixer(w);
2412                         break;
2413                 case snd_soc_dapm_mux:
2414                 case snd_soc_dapm_virt_mux:
2415                 case snd_soc_dapm_value_mux:
2416                         dapm_new_mux(w);
2417                         break;
2418                 case snd_soc_dapm_pga:
2419                 case snd_soc_dapm_out_drv:
2420                         dapm_new_pga(w);
2421                         break;
2422                 default:
2423                         break;
2424                 }
2425
2426                 /* Read the initial power state from the device */
2427                 if (w->reg >= 0) {
2428                         val = soc_widget_read(w, w->reg);
2429                         val &= 1 << w->shift;
2430                         if (w->invert)
2431                                 val = !val;
2432
2433                         if (val)
2434                                 w->power = 1;
2435                 }
2436
2437                 w->new = 1;
2438
2439                 dapm_mark_dirty(w, "new widget");
2440                 dapm_debugfs_add_widget(w);
2441         }
2442
2443         dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2444         mutex_unlock(&dapm->card->dapm_mutex);
2445         return 0;
2446 }
2447 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2448
2449 /**
2450  * snd_soc_dapm_get_volsw - dapm mixer get callback
2451  * @kcontrol: mixer control
2452  * @ucontrol: control element information
2453  *
2454  * Callback to get the value of a dapm mixer control.
2455  *
2456  * Returns 0 for success.
2457  */
2458 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2459         struct snd_ctl_elem_value *ucontrol)
2460 {
2461         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2462         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2463         struct soc_mixer_control *mc =
2464                 (struct soc_mixer_control *)kcontrol->private_value;
2465         unsigned int reg = mc->reg;
2466         unsigned int shift = mc->shift;
2467         unsigned int rshift = mc->rshift;
2468         int max = mc->max;
2469         unsigned int invert = mc->invert;
2470         unsigned int mask = (1 << fls(max)) - 1;
2471
2472         ucontrol->value.integer.value[0] =
2473                 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2474         if (shift != rshift)
2475                 ucontrol->value.integer.value[1] =
2476                         (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2477         if (invert) {
2478                 ucontrol->value.integer.value[0] =
2479                         max - ucontrol->value.integer.value[0];
2480                 if (shift != rshift)
2481                         ucontrol->value.integer.value[1] =
2482                                 max - ucontrol->value.integer.value[1];
2483         }
2484
2485         return 0;
2486 }
2487 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2488
2489 /**
2490  * snd_soc_dapm_put_volsw - dapm mixer set callback
2491  * @kcontrol: mixer control
2492  * @ucontrol: control element information
2493  *
2494  * Callback to set the value of a dapm mixer control.
2495  *
2496  * Returns 0 for success.
2497  */
2498 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2499         struct snd_ctl_elem_value *ucontrol)
2500 {
2501         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2502         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2503         struct snd_soc_codec *codec = widget->codec;
2504         struct snd_soc_card *card = codec->card;
2505         struct soc_mixer_control *mc =
2506                 (struct soc_mixer_control *)kcontrol->private_value;
2507         unsigned int reg = mc->reg;
2508         unsigned int shift = mc->shift;
2509         int max = mc->max;
2510         unsigned int mask = (1 << fls(max)) - 1;
2511         unsigned int invert = mc->invert;
2512         unsigned int val;
2513         int connect, change;
2514         struct snd_soc_dapm_update update;
2515         int wi;
2516
2517         val = (ucontrol->value.integer.value[0] & mask);
2518         connect = !!val;
2519
2520         if (invert)
2521                 val = max - val;
2522         mask = mask << shift;
2523         val = val << shift;
2524
2525         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2526
2527         change = snd_soc_test_bits(widget->codec, reg, mask, val);
2528         if (change) {
2529                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2530                         widget = wlist->widgets[wi];
2531
2532                         widget->value = val;
2533
2534                         update.kcontrol = kcontrol;
2535                         update.widget = widget;
2536                         update.reg = reg;
2537                         update.mask = mask;
2538                         update.val = val;
2539                         widget->dapm->update = &update;
2540
2541                         soc_dapm_mixer_update_power(widget, kcontrol, connect);
2542
2543                         widget->dapm->update = NULL;
2544                 }
2545         }
2546
2547         mutex_unlock(&card->dapm_mutex);
2548         return 0;
2549 }
2550 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2551
2552 /**
2553  * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2554  * @kcontrol: mixer control
2555  * @ucontrol: control element information
2556  *
2557  * Callback to get the value of a dapm enumerated double mixer control.
2558  *
2559  * Returns 0 for success.
2560  */
2561 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2562         struct snd_ctl_elem_value *ucontrol)
2563 {
2564         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2565         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2566         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2567         unsigned int val, bitmask;
2568
2569         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2570                 ;
2571         val = snd_soc_read(widget->codec, e->reg);
2572         ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2573         if (e->shift_l != e->shift_r)
2574                 ucontrol->value.enumerated.item[1] =
2575                         (val >> e->shift_r) & (bitmask - 1);
2576
2577         return 0;
2578 }
2579 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2580
2581 /**
2582  * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2583  * @kcontrol: mixer control
2584  * @ucontrol: control element information
2585  *
2586  * Callback to set the value of a dapm enumerated double mixer control.
2587  *
2588  * Returns 0 for success.
2589  */
2590 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2591         struct snd_ctl_elem_value *ucontrol)
2592 {
2593         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2594         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2595         struct snd_soc_codec *codec = widget->codec;
2596         struct snd_soc_card *card = codec->card;
2597         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2598         unsigned int val, mux, change;
2599         unsigned int mask, bitmask;
2600         struct snd_soc_dapm_update update;
2601         int wi;
2602
2603         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2604                 ;
2605         if (ucontrol->value.enumerated.item[0] > e->max - 1)
2606                 return -EINVAL;
2607         mux = ucontrol->value.enumerated.item[0];
2608         val = mux << e->shift_l;
2609         mask = (bitmask - 1) << e->shift_l;
2610         if (e->shift_l != e->shift_r) {
2611                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2612                         return -EINVAL;
2613                 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2614                 mask |= (bitmask - 1) << e->shift_r;
2615         }
2616
2617         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2618
2619         change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2620         if (change) {
2621                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2622                         widget = wlist->widgets[wi];
2623
2624                         widget->value = val;
2625
2626                         update.kcontrol = kcontrol;
2627                         update.widget = widget;
2628                         update.reg = e->reg;
2629                         update.mask = mask;
2630                         update.val = val;
2631                         widget->dapm->update = &update;
2632
2633                         soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2634
2635                         widget->dapm->update = NULL;
2636                 }
2637         }
2638
2639         mutex_unlock(&card->dapm_mutex);
2640         return change;
2641 }
2642 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2643
2644 /**
2645  * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2646  * @kcontrol: mixer control
2647  * @ucontrol: control element information
2648  *
2649  * Returns 0 for success.
2650  */
2651 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2652                                struct snd_ctl_elem_value *ucontrol)
2653 {
2654         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2655         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2656
2657         ucontrol->value.enumerated.item[0] = widget->value;
2658
2659         return 0;
2660 }
2661 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2662
2663 /**
2664  * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2665  * @kcontrol: mixer control
2666  * @ucontrol: control element information
2667  *
2668  * Returns 0 for success.
2669  */
2670 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2671                                struct snd_ctl_elem_value *ucontrol)
2672 {
2673         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2674         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2675         struct snd_soc_codec *codec = widget->codec;
2676         struct snd_soc_card *card = codec->card;
2677         struct soc_enum *e =
2678                 (struct soc_enum *)kcontrol->private_value;
2679         int change;
2680         int ret = 0;
2681         int wi;
2682
2683         if (ucontrol->value.enumerated.item[0] >= e->max)
2684                 return -EINVAL;
2685
2686         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2687
2688         change = widget->value != ucontrol->value.enumerated.item[0];
2689         if (change) {
2690                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2691                         widget = wlist->widgets[wi];
2692
2693                         widget->value = ucontrol->value.enumerated.item[0];
2694
2695                         soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
2696                 }
2697         }
2698
2699         mutex_unlock(&card->dapm_mutex);
2700         return ret;
2701 }
2702 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2703
2704 /**
2705  * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2706  *                                      callback
2707  * @kcontrol: mixer control
2708  * @ucontrol: control element information
2709  *
2710  * Callback to get the value of a dapm semi enumerated double mixer control.
2711  *
2712  * Semi enumerated mixer: the enumerated items are referred as values. Can be
2713  * used for handling bitfield coded enumeration for example.
2714  *
2715  * Returns 0 for success.
2716  */
2717 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2718         struct snd_ctl_elem_value *ucontrol)
2719 {
2720         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2721         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2722         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2723         unsigned int reg_val, val, mux;
2724
2725         reg_val = snd_soc_read(widget->codec, e->reg);
2726         val = (reg_val >> e->shift_l) & e->mask;
2727         for (mux = 0; mux < e->max; mux++) {
2728                 if (val == e->values[mux])
2729                         break;
2730         }
2731         ucontrol->value.enumerated.item[0] = mux;
2732         if (e->shift_l != e->shift_r) {
2733                 val = (reg_val >> e->shift_r) & e->mask;
2734                 for (mux = 0; mux < e->max; mux++) {
2735                         if (val == e->values[mux])
2736                                 break;
2737                 }
2738                 ucontrol->value.enumerated.item[1] = mux;
2739         }
2740
2741         return 0;
2742 }
2743 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2744
2745 /**
2746  * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2747  *                                      callback
2748  * @kcontrol: mixer control
2749  * @ucontrol: control element information
2750  *
2751  * Callback to set the value of a dapm semi enumerated double mixer control.
2752  *
2753  * Semi enumerated mixer: the enumerated items are referred as values. Can be
2754  * used for handling bitfield coded enumeration for example.
2755  *
2756  * Returns 0 for success.
2757  */
2758 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2759         struct snd_ctl_elem_value *ucontrol)
2760 {
2761         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2762         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2763         struct snd_soc_codec *codec = widget->codec;
2764         struct snd_soc_card *card = codec->card;
2765         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2766         unsigned int val, mux, change;
2767         unsigned int mask;
2768         struct snd_soc_dapm_update update;
2769         int wi;
2770
2771         if (ucontrol->value.enumerated.item[0] > e->max - 1)
2772                 return -EINVAL;
2773         mux = ucontrol->value.enumerated.item[0];
2774         val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2775         mask = e->mask << e->shift_l;
2776         if (e->shift_l != e->shift_r) {
2777                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2778                         return -EINVAL;
2779                 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2780                 mask |= e->mask << e->shift_r;
2781         }
2782
2783         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2784
2785         change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2786         if (change) {
2787                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2788                         widget = wlist->widgets[wi];
2789
2790                         widget->value = val;
2791
2792                         update.kcontrol = kcontrol;
2793                         update.widget = widget;
2794                         update.reg = e->reg;
2795                         update.mask = mask;
2796                         update.val = val;
2797                         widget->dapm->update = &update;
2798
2799                         soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2800
2801                         widget->dapm->update = NULL;
2802                 }
2803         }
2804
2805         mutex_unlock(&card->dapm_mutex);
2806         return change;
2807 }
2808 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2809
2810 /**
2811  * snd_soc_dapm_info_pin_switch - Info for a pin switch
2812  *
2813  * @kcontrol: mixer control
2814  * @uinfo: control element information
2815  *
2816  * Callback to provide information about a pin switch control.
2817  */
2818 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2819                                  struct snd_ctl_elem_info *uinfo)
2820 {
2821         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2822         uinfo->count = 1;
2823         uinfo->value.integer.min = 0;
2824         uinfo->value.integer.max = 1;
2825
2826         return 0;
2827 }
2828 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2829
2830 /**
2831  * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2832  *
2833  * @kcontrol: mixer control
2834  * @ucontrol: Value
2835  */
2836 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2837                                 struct snd_ctl_elem_value *ucontrol)
2838 {
2839         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2840         const char *pin = (const char *)kcontrol->private_value;
2841
2842         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2843
2844         ucontrol->value.integer.value[0] =
2845                 snd_soc_dapm_get_pin_status(&card->dapm, pin);
2846
2847         mutex_unlock(&card->dapm_mutex);
2848
2849         return 0;
2850 }
2851 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2852
2853 /**
2854  * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2855  *
2856  * @kcontrol: mixer control
2857  * @ucontrol: Value
2858  */
2859 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2860                                 struct snd_ctl_elem_value *ucontrol)
2861 {
2862         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2863         const char *pin = (const char *)kcontrol->private_value;
2864
2865         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2866
2867         if (ucontrol->value.integer.value[0])
2868                 snd_soc_dapm_enable_pin(&card->dapm, pin);
2869         else
2870                 snd_soc_dapm_disable_pin(&card->dapm, pin);
2871
2872         mutex_unlock(&card->dapm_mutex);
2873
2874         snd_soc_dapm_sync(&card->dapm);
2875         return 0;
2876 }
2877 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2878
2879 static struct snd_soc_dapm_widget *
2880 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2881                          const struct snd_soc_dapm_widget *widget)
2882 {
2883         struct snd_soc_dapm_widget *w;
2884         size_t name_len;
2885         int ret;
2886
2887         if ((w = dapm_cnew_widget(widget)) == NULL)
2888                 return NULL;
2889
2890         switch (w->id) {
2891         case snd_soc_dapm_regulator_supply:
2892                 w->regulator = devm_regulator_get(dapm->dev, w->name);
2893                 if (IS_ERR(w->regulator)) {
2894                         ret = PTR_ERR(w->regulator);
2895                         dev_err(dapm->dev, "Failed to request %s: %d\n",
2896                                 w->name, ret);
2897                         return NULL;
2898                 }
2899                 break;
2900         case snd_soc_dapm_clock_supply:
2901 #ifdef CONFIG_CLKDEV_LOOKUP
2902                 w->clk = devm_clk_get(dapm->dev, w->name);
2903                 if (IS_ERR(w->clk)) {
2904                         ret = PTR_ERR(w->clk);
2905                         dev_err(dapm->dev, "Failed to request %s: %d\n",
2906                                 w->name, ret);
2907                         return NULL;
2908                 }
2909 #else
2910                 return NULL;
2911 #endif
2912                 break;
2913         default:
2914                 break;
2915         }
2916
2917         name_len = strlen(widget->name) + 1;
2918         if (dapm->codec && dapm->codec->name_prefix)
2919                 name_len += 1 + strlen(dapm->codec->name_prefix);
2920         w->name = kmalloc(name_len, GFP_KERNEL);
2921         if (w->name == NULL) {
2922                 kfree(w);
2923                 return NULL;
2924         }
2925         if (dapm->codec && dapm->codec->name_prefix)
2926                 snprintf((char *)w->name, name_len, "%s %s",
2927                         dapm->codec->name_prefix, widget->name);
2928         else
2929                 snprintf((char *)w->name, name_len, "%s", widget->name);
2930
2931         switch (w->id) {
2932         case snd_soc_dapm_switch:
2933         case snd_soc_dapm_mixer:
2934         case snd_soc_dapm_mixer_named_ctl:
2935                 w->power_check = dapm_generic_check_power;
2936                 break;
2937         case snd_soc_dapm_mux:
2938         case snd_soc_dapm_virt_mux:
2939         case snd_soc_dapm_value_mux:
2940                 w->power_check = dapm_generic_check_power;
2941                 break;
2942         case snd_soc_dapm_adc:
2943         case snd_soc_dapm_aif_out:
2944                 w->power_check = dapm_adc_check_power;
2945                 break;
2946         case snd_soc_dapm_dac:
2947         case snd_soc_dapm_aif_in:
2948                 w->power_check = dapm_dac_check_power;
2949                 break;
2950         case snd_soc_dapm_pga:
2951         case snd_soc_dapm_out_drv:
2952         case snd_soc_dapm_input:
2953         case snd_soc_dapm_output:
2954         case snd_soc_dapm_micbias:
2955         case snd_soc_dapm_spk:
2956         case snd_soc_dapm_hp:
2957         case snd_soc_dapm_mic:
2958         case snd_soc_dapm_line:
2959         case snd_soc_dapm_dai_link:
2960                 w->power_check = dapm_generic_check_power;
2961                 break;
2962         case snd_soc_dapm_supply:
2963         case snd_soc_dapm_regulator_supply:
2964         case snd_soc_dapm_clock_supply:
2965                 w->power_check = dapm_supply_check_power;
2966                 break;
2967         case snd_soc_dapm_dai:
2968                 w->power_check = dapm_dai_check_power;
2969                 break;
2970         default:
2971                 w->power_check = dapm_always_on_check_power;
2972                 break;
2973         }
2974
2975         dapm->n_widgets++;
2976         w->dapm = dapm;
2977         w->codec = dapm->codec;
2978         w->platform = dapm->platform;
2979         INIT_LIST_HEAD(&w->sources);
2980         INIT_LIST_HEAD(&w->sinks);
2981         INIT_LIST_HEAD(&w->list);
2982         INIT_LIST_HEAD(&w->dirty);
2983         list_add(&w->list, &dapm->card->widgets);
2984
2985         /* machine layer set ups unconnected pins and insertions */
2986         w->connected = 1;
2987         return w;
2988 }
2989
2990 /**
2991  * snd_soc_dapm_new_controls - create new dapm controls
2992  * @dapm: DAPM context
2993  * @widget: widget array
2994  * @num: number of widgets
2995  *
2996  * Creates new DAPM controls based upon the templates.
2997  *
2998  * Returns 0 for success else error.
2999  */
3000 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3001         const struct snd_soc_dapm_widget *widget,
3002         int num)
3003 {
3004         struct snd_soc_dapm_widget *w;
3005         int i;
3006         int ret = 0;
3007
3008         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3009         for (i = 0; i < num; i++) {
3010                 w = snd_soc_dapm_new_control(dapm, widget);
3011                 if (!w) {
3012                         dev_err(dapm->dev,
3013                                 "ASoC: Failed to create DAPM control %s\n",
3014                                 widget->name);
3015                         ret = -ENOMEM;
3016                         break;
3017                 }
3018                 widget++;
3019         }
3020         mutex_unlock(&dapm->card->dapm_mutex);
3021         return ret;
3022 }
3023 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3024
3025 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3026                                   struct snd_kcontrol *kcontrol, int event)
3027 {
3028         struct snd_soc_dapm_path *source_p, *sink_p;
3029         struct snd_soc_dai *source, *sink;
3030         const struct snd_soc_pcm_stream *config = w->params;
3031         struct snd_pcm_substream substream;
3032         struct snd_pcm_hw_params *params = NULL;
3033         u64 fmt;
3034         int ret;
3035
3036         BUG_ON(!config);
3037         BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3038
3039         /* We only support a single source and sink, pick the first */
3040         source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3041                                     list_sink);
3042         sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3043                                   list_source);
3044
3045         BUG_ON(!source_p || !sink_p);
3046         BUG_ON(!sink_p->source || !source_p->sink);
3047         BUG_ON(!source_p->source || !sink_p->sink);
3048
3049         source = source_p->source->priv;
3050         sink = sink_p->sink->priv;
3051
3052         /* Be a little careful as we don't want to overflow the mask array */
3053         if (config->formats) {
3054                 fmt = ffs(config->formats) - 1;
3055         } else {
3056                 dev_warn(w->dapm->dev, "Invalid format %llx specified\n",
3057                          config->formats);
3058                 fmt = 0;
3059         }
3060
3061         /* Currently very limited parameter selection */
3062         params = kzalloc(sizeof(*params), GFP_KERNEL);
3063         if (!params) {
3064                 ret = -ENOMEM;
3065                 goto out;
3066         }
3067         snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3068
3069         hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3070                 config->rate_min;
3071         hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3072                 config->rate_max;
3073
3074         hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3075                 = config->channels_min;
3076         hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3077                 = config->channels_max;
3078
3079         memset(&substream, 0, sizeof(substream));
3080
3081         switch (event) {
3082         case SND_SOC_DAPM_PRE_PMU:
3083                 if (source->driver->ops && source->driver->ops->hw_params) {
3084                         substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3085                         ret = source->driver->ops->hw_params(&substream,
3086                                                              params, source);
3087                         if (ret != 0) {
3088                                 dev_err(source->dev,
3089                                         "hw_params() failed: %d\n", ret);
3090                                 goto out;
3091                         }
3092                 }
3093
3094                 if (sink->driver->ops && sink->driver->ops->hw_params) {
3095                         substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3096                         ret = sink->driver->ops->hw_params(&substream, params,
3097                                                            sink);
3098                         if (ret != 0) {
3099                                 dev_err(sink->dev,
3100                                         "hw_params() failed: %d\n", ret);
3101                                 goto out;
3102                         }
3103                 }
3104                 break;
3105
3106         case SND_SOC_DAPM_POST_PMU:
3107                 ret = snd_soc_dai_digital_mute(sink, 0);
3108                 if (ret != 0 && ret != -ENOTSUPP)
3109                         dev_warn(sink->dev, "Failed to unmute: %d\n", ret);
3110                 ret = 0;
3111                 break;
3112
3113         case SND_SOC_DAPM_PRE_PMD:
3114                 ret = snd_soc_dai_digital_mute(sink, 1);
3115                 if (ret != 0 && ret != -ENOTSUPP)
3116                         dev_warn(sink->dev, "Failed to mute: %d\n", ret);
3117                 ret = 0;
3118                 break;
3119
3120         default:
3121                 BUG();
3122                 return -EINVAL;
3123         }
3124
3125 out:
3126         kfree(params);
3127         return ret;
3128 }
3129
3130 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3131                          const struct snd_soc_pcm_stream *params,
3132                          struct snd_soc_dapm_widget *source,
3133                          struct snd_soc_dapm_widget *sink)
3134 {
3135         struct snd_soc_dapm_route routes[2];
3136         struct snd_soc_dapm_widget template;
3137         struct snd_soc_dapm_widget *w;
3138         size_t len;
3139         char *link_name;
3140
3141         len = strlen(source->name) + strlen(sink->name) + 2;
3142         link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3143         if (!link_name)
3144                 return -ENOMEM;
3145         snprintf(link_name, len, "%s-%s", source->name, sink->name);
3146
3147         memset(&template, 0, sizeof(template));
3148         template.reg = SND_SOC_NOPM;
3149         template.id = snd_soc_dapm_dai_link;
3150         template.name = link_name;
3151         template.event = snd_soc_dai_link_event;
3152         template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3153                 SND_SOC_DAPM_PRE_PMD;
3154
3155         dev_dbg(card->dev, "adding %s widget\n", link_name);
3156
3157         w = snd_soc_dapm_new_control(&card->dapm, &template);
3158         if (!w) {
3159                 dev_err(card->dev, "Failed to create %s widget\n",
3160                         link_name);
3161                 return -ENOMEM;
3162         }
3163
3164         w->params = params;
3165
3166         memset(&routes, 0, sizeof(routes));
3167
3168         routes[0].source = source->name;
3169         routes[0].sink = link_name;
3170         routes[1].source = link_name;
3171         routes[1].sink = sink->name;
3172
3173         return snd_soc_dapm_add_routes(&card->dapm, routes,
3174                                        ARRAY_SIZE(routes));
3175 }
3176
3177 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3178                                  struct snd_soc_dai *dai)
3179 {
3180         struct snd_soc_dapm_widget template;
3181         struct snd_soc_dapm_widget *w;
3182
3183         WARN_ON(dapm->dev != dai->dev);
3184
3185         memset(&template, 0, sizeof(template));
3186         template.reg = SND_SOC_NOPM;
3187
3188         if (dai->driver->playback.stream_name) {
3189                 template.id = snd_soc_dapm_dai;
3190                 template.name = dai->driver->playback.stream_name;
3191                 template.sname = dai->driver->playback.stream_name;
3192
3193                 dev_dbg(dai->dev, "adding %s widget\n",
3194                         template.name);
3195
3196                 w = snd_soc_dapm_new_control(dapm, &template);
3197                 if (!w) {
3198                         dev_err(dapm->dev, "Failed to create %s widget\n",
3199                                 dai->driver->playback.stream_name);
3200                 }
3201
3202                 w->priv = dai;
3203                 dai->playback_widget = w;
3204         }
3205
3206         if (dai->driver->capture.stream_name) {
3207                 template.id = snd_soc_dapm_dai;
3208                 template.name = dai->driver->capture.stream_name;
3209                 template.sname = dai->driver->capture.stream_name;
3210
3211                 dev_dbg(dai->dev, "adding %s widget\n",
3212                         template.name);
3213
3214                 w = snd_soc_dapm_new_control(dapm, &template);
3215                 if (!w) {
3216                         dev_err(dapm->dev, "Failed to create %s widget\n",
3217                                 dai->driver->capture.stream_name);
3218                 }
3219
3220                 w->priv = dai;
3221                 dai->capture_widget = w;
3222         }
3223
3224         return 0;
3225 }
3226
3227 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3228 {
3229         struct snd_soc_dapm_widget *dai_w, *w;
3230         struct snd_soc_dai *dai;
3231         struct snd_soc_dapm_route r;
3232
3233         memset(&r, 0, sizeof(r));
3234
3235         /* For each DAI widget... */
3236         list_for_each_entry(dai_w, &card->widgets, list) {
3237                 if (dai_w->id != snd_soc_dapm_dai)
3238                         continue;
3239
3240                 dai = dai_w->priv;
3241
3242                 /* ...find all widgets with the same stream and link them */
3243                 list_for_each_entry(w, &card->widgets, list) {
3244                         if (w->dapm != dai_w->dapm)
3245                                 continue;
3246
3247                         if (w->id == snd_soc_dapm_dai)
3248                                 continue;
3249
3250                         if (!w->sname)
3251                                 continue;
3252
3253                         if (dai->driver->playback.stream_name &&
3254                             strstr(w->sname,
3255                                    dai->driver->playback.stream_name)) {
3256                                 r.source = dai->playback_widget->name;
3257                                 r.sink = w->name;
3258                                 dev_dbg(dai->dev, "%s -> %s\n",
3259                                          r.source, r.sink);
3260
3261                                 snd_soc_dapm_add_route(w->dapm, &r);
3262                         }
3263
3264                         if (dai->driver->capture.stream_name &&
3265                             strstr(w->sname,
3266                                    dai->driver->capture.stream_name)) {
3267                                 r.source = w->name;
3268                                 r.sink = dai->capture_widget->name;
3269                                 dev_dbg(dai->dev, "%s -> %s\n",
3270                                         r.source, r.sink);
3271
3272                                 snd_soc_dapm_add_route(w->dapm, &r);
3273                         }
3274                 }
3275         }
3276
3277         return 0;
3278 }
3279
3280 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3281         int event)
3282 {
3283
3284         struct snd_soc_dapm_widget *w_cpu, *w_codec;
3285         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3286         struct snd_soc_dai *codec_dai = rtd->codec_dai;
3287
3288         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3289                 w_cpu = cpu_dai->playback_widget;
3290                 w_codec = codec_dai->playback_widget;
3291         } else {
3292                 w_cpu = cpu_dai->capture_widget;
3293                 w_codec = codec_dai->capture_widget;
3294         }
3295
3296         if (w_cpu) {
3297
3298                 dapm_mark_dirty(w_cpu, "stream event");
3299
3300                 switch (event) {
3301                 case SND_SOC_DAPM_STREAM_START:
3302                         w_cpu->active = 1;
3303                         break;
3304                 case SND_SOC_DAPM_STREAM_STOP:
3305                         w_cpu->active = 0;
3306                         break;
3307                 case SND_SOC_DAPM_STREAM_SUSPEND:
3308                 case SND_SOC_DAPM_STREAM_RESUME:
3309                 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3310                 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3311                         break;
3312                 }
3313         }
3314
3315         if (w_codec) {
3316
3317                 dapm_mark_dirty(w_codec, "stream event");
3318
3319                 switch (event) {
3320                 case SND_SOC_DAPM_STREAM_START:
3321                         w_codec->active = 1;
3322                         break;
3323                 case SND_SOC_DAPM_STREAM_STOP:
3324                         w_codec->active = 0;
3325                         break;
3326                 case SND_SOC_DAPM_STREAM_SUSPEND:
3327                 case SND_SOC_DAPM_STREAM_RESUME:
3328                 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3329                 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3330                         break;
3331                 }
3332         }
3333
3334         dapm_power_widgets(&rtd->card->dapm, event);
3335 }
3336
3337 /**
3338  * snd_soc_dapm_stream_event - send a stream event to the dapm core
3339  * @rtd: PCM runtime data
3340  * @stream: stream name
3341  * @event: stream event
3342  *
3343  * Sends a stream event to the dapm core. The core then makes any
3344  * necessary widget power changes.
3345  *
3346  * Returns 0 for success else error.
3347  */
3348 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3349                               int event)
3350 {
3351         struct snd_soc_card *card = rtd->card;
3352
3353         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3354         soc_dapm_stream_event(rtd, stream, event);
3355         mutex_unlock(&card->dapm_mutex);
3356 }
3357
3358 /**
3359  * snd_soc_dapm_enable_pin - enable pin.
3360  * @dapm: DAPM context
3361  * @pin: pin name
3362  *
3363  * Enables input/output pin and its parents or children widgets iff there is
3364  * a valid audio route and active audio stream.
3365  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3366  * do any widget power switching.
3367  */
3368 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3369 {
3370         return snd_soc_dapm_set_pin(dapm, pin, 1);
3371 }
3372 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3373
3374 /**
3375  * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3376  * @dapm: DAPM context
3377  * @pin: pin name
3378  *
3379  * Enables input/output pin regardless of any other state.  This is
3380  * intended for use with microphone bias supplies used in microphone
3381  * jack detection.
3382  *
3383  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3384  * do any widget power switching.
3385  */
3386 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3387                                   const char *pin)
3388 {
3389         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3390
3391         if (!w) {
3392                 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3393                 return -EINVAL;
3394         }
3395
3396         dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
3397         w->connected = 1;
3398         w->force = 1;
3399         dapm_mark_dirty(w, "force enable");
3400
3401         return 0;
3402 }
3403 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3404
3405 /**
3406  * snd_soc_dapm_disable_pin - disable pin.
3407  * @dapm: DAPM context
3408  * @pin: pin name
3409  *
3410  * Disables input/output pin and its parents or children widgets.
3411  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3412  * do any widget power switching.
3413  */
3414 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3415                              const char *pin)
3416 {
3417         return snd_soc_dapm_set_pin(dapm, pin, 0);
3418 }
3419 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3420
3421 /**
3422  * snd_soc_dapm_nc_pin - permanently disable pin.
3423  * @dapm: DAPM context
3424  * @pin: pin name
3425  *
3426  * Marks the specified pin as being not connected, disabling it along
3427  * any parent or child widgets.  At present this is identical to
3428  * snd_soc_dapm_disable_pin() but in future it will be extended to do
3429  * additional things such as disabling controls which only affect
3430  * paths through the pin.
3431  *
3432  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3433  * do any widget power switching.
3434  */
3435 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3436 {
3437         return snd_soc_dapm_set_pin(dapm, pin, 0);
3438 }
3439 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3440
3441 /**
3442  * snd_soc_dapm_get_pin_status - get audio pin status
3443  * @dapm: DAPM context
3444  * @pin: audio signal pin endpoint (or start point)
3445  *
3446  * Get audio pin status - connected or disconnected.
3447  *
3448  * Returns 1 for connected otherwise 0.
3449  */
3450 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3451                                 const char *pin)
3452 {
3453         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3454
3455         if (w)
3456                 return w->connected;
3457
3458         return 0;
3459 }
3460 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3461
3462 /**
3463  * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3464  * @dapm: DAPM context
3465  * @pin: audio signal pin endpoint (or start point)
3466  *
3467  * Mark the given endpoint or pin as ignoring suspend.  When the
3468  * system is disabled a path between two endpoints flagged as ignoring
3469  * suspend will not be disabled.  The path must already be enabled via
3470  * normal means at suspend time, it will not be turned on if it was not
3471  * already enabled.
3472  */
3473 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3474                                 const char *pin)
3475 {
3476         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3477
3478         if (!w) {
3479                 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3480                 return -EINVAL;
3481         }
3482
3483         w->ignore_suspend = 1;
3484
3485         return 0;
3486 }
3487 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3488
3489 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3490                                               struct snd_soc_dapm_widget *w)
3491 {
3492         struct snd_soc_dapm_path *p;
3493
3494         list_for_each_entry(p, &card->paths, list) {
3495                 if ((p->source == w) || (p->sink == w)) {
3496                         dev_dbg(card->dev,
3497                             "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3498                             p->source->name, p->source->id, p->source->dapm,
3499                             p->sink->name, p->sink->id, p->sink->dapm);
3500
3501                         /* Connected to something other than the codec */
3502                         if (p->source->dapm != p->sink->dapm)
3503                                 return true;
3504                         /*
3505                          * Loopback connection from codec external pin to
3506                          * codec external pin
3507                          */
3508                         if (p->sink->id == snd_soc_dapm_input) {
3509                                 switch (p->source->id) {
3510                                 case snd_soc_dapm_output:
3511                                 case snd_soc_dapm_micbias:
3512                                         return true;
3513                                 default:
3514                                         break;
3515                                 }
3516                         }
3517                 }
3518         }
3519
3520         return false;
3521 }
3522
3523 /**
3524  * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3525  * @codec: The codec whose pins should be processed
3526  *
3527  * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3528  * which are unused. Pins are used if they are connected externally to the
3529  * codec, whether that be to some other device, or a loop-back connection to
3530  * the codec itself.
3531  */
3532 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3533 {
3534         struct snd_soc_card *card = codec->card;
3535         struct snd_soc_dapm_context *dapm = &codec->dapm;
3536         struct snd_soc_dapm_widget *w;
3537
3538         dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
3539                 &card->dapm, &codec->dapm);
3540
3541         list_for_each_entry(w, &card->widgets, list) {
3542                 if (w->dapm != dapm)
3543                         continue;
3544                 switch (w->id) {
3545                 case snd_soc_dapm_input:
3546                 case snd_soc_dapm_output:
3547                 case snd_soc_dapm_micbias:
3548                         dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
3549                                 w->name);
3550                         if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3551                                 dev_dbg(codec->dev,
3552                                         "... Not in map; disabling\n");
3553                                 snd_soc_dapm_nc_pin(dapm, w->name);
3554                         }
3555                         break;
3556                 default:
3557                         break;
3558                 }
3559         }
3560 }
3561
3562 /**
3563  * snd_soc_dapm_free - free dapm resources
3564  * @dapm: DAPM context
3565  *
3566  * Free all dapm widgets and resources.
3567  */
3568 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3569 {
3570         snd_soc_dapm_sys_remove(dapm->dev);
3571         dapm_debugfs_cleanup(dapm);
3572         dapm_free_widgets(dapm);
3573         list_del(&dapm->list);
3574 }
3575 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3576
3577 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
3578 {
3579         struct snd_soc_dapm_widget *w;
3580         LIST_HEAD(down_list);
3581         int powerdown = 0;
3582
3583         list_for_each_entry(w, &dapm->card->widgets, list) {
3584                 if (w->dapm != dapm)
3585                         continue;
3586                 if (w->power) {
3587                         dapm_seq_insert(w, &down_list, false);
3588                         w->power = 0;
3589                         powerdown = 1;
3590                 }
3591         }
3592
3593         /* If there were no widgets to power down we're already in
3594          * standby.
3595          */
3596         if (powerdown) {
3597                 if (dapm->bias_level == SND_SOC_BIAS_ON)
3598                         snd_soc_dapm_set_bias_level(dapm,
3599                                                     SND_SOC_BIAS_PREPARE);
3600                 dapm_seq_run(dapm, &down_list, 0, false);
3601                 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3602                         snd_soc_dapm_set_bias_level(dapm,
3603                                                     SND_SOC_BIAS_STANDBY);
3604         }
3605 }
3606
3607 /*
3608  * snd_soc_dapm_shutdown - callback for system shutdown
3609  */
3610 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3611 {
3612         struct snd_soc_codec *codec;
3613
3614         list_for_each_entry(codec, &card->codec_dev_list, list) {
3615                 soc_dapm_shutdown_codec(&codec->dapm);
3616                 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3617                         snd_soc_dapm_set_bias_level(&codec->dapm,
3618                                                     SND_SOC_BIAS_OFF);
3619         }
3620 }
3621
3622 /* Module information */
3623 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3624 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3625 MODULE_LICENSE("GPL");