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