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