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