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