OSDN Git Service

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