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