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