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