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