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