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