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