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