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