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