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