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