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