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