GNU Linux-libre 4.14.262-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                                 list_for_each_entry_safe_continue(w, n, list,
1621                                                                   power_list);
1622
1623                         if (event == SND_SOC_DAPM_STREAM_START)
1624                                 ret = w->event(w,
1625                                                NULL, SND_SOC_DAPM_PRE_PMU);
1626                         else if (event == SND_SOC_DAPM_STREAM_STOP)
1627                                 ret = w->event(w,
1628                                                NULL, SND_SOC_DAPM_PRE_PMD);
1629                         break;
1630
1631                 case snd_soc_dapm_post:
1632                         if (!w->event)
1633                                 list_for_each_entry_safe_continue(w, n, list,
1634                                                                   power_list);
1635
1636                         if (event == SND_SOC_DAPM_STREAM_START)
1637                                 ret = w->event(w,
1638                                                NULL, SND_SOC_DAPM_POST_PMU);
1639                         else if (event == SND_SOC_DAPM_STREAM_STOP)
1640                                 ret = w->event(w,
1641                                                NULL, SND_SOC_DAPM_POST_PMD);
1642                         break;
1643
1644                 default:
1645                         /* Queue it up for application */
1646                         cur_sort = sort[w->id];
1647                         cur_subseq = w->subseq;
1648                         cur_reg = w->reg;
1649                         cur_dapm = w->dapm;
1650                         list_move(&w->power_list, &pending);
1651                         break;
1652                 }
1653
1654                 if (ret < 0)
1655                         dev_err(w->dapm->dev,
1656                                 "ASoC: Failed to apply widget power: %d\n", ret);
1657         }
1658
1659         if (!list_empty(&pending))
1660                 dapm_seq_run_coalesced(card, &pending);
1661
1662         if (cur_dapm && cur_dapm->seq_notifier) {
1663                 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1664                         if (sort[i] == cur_sort)
1665                                 cur_dapm->seq_notifier(cur_dapm,
1666                                                        i, cur_subseq);
1667         }
1668
1669         list_for_each_entry(d, &card->dapm_list, list) {
1670                 soc_dapm_async_complete(d);
1671         }
1672 }
1673
1674 static void dapm_widget_update(struct snd_soc_card *card)
1675 {
1676         struct snd_soc_dapm_update *update = card->update;
1677         struct snd_soc_dapm_widget_list *wlist;
1678         struct snd_soc_dapm_widget *w = NULL;
1679         unsigned int wi;
1680         int ret;
1681
1682         if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1683                 return;
1684
1685         wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1686
1687         for (wi = 0; wi < wlist->num_widgets; wi++) {
1688                 w = wlist->widgets[wi];
1689
1690                 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1691                         ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1692                         if (ret != 0)
1693                                 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1694                                            w->name, ret);
1695                 }
1696         }
1697
1698         if (!w)
1699                 return;
1700
1701         ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1702                 update->val);
1703         if (ret < 0)
1704                 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1705                         w->name, ret);
1706
1707         if (update->has_second_set) {
1708                 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1709                                            update->mask2, update->val2);
1710                 if (ret < 0)
1711                         dev_err(w->dapm->dev,
1712                                 "ASoC: %s DAPM update failed: %d\n",
1713                                 w->name, ret);
1714         }
1715
1716         for (wi = 0; wi < wlist->num_widgets; wi++) {
1717                 w = wlist->widgets[wi];
1718
1719                 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1720                         ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1721                         if (ret != 0)
1722                                 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1723                                            w->name, ret);
1724                 }
1725         }
1726 }
1727
1728 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1729  * they're changing state.
1730  */
1731 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1732 {
1733         struct snd_soc_dapm_context *d = data;
1734         int ret;
1735
1736         /* If we're off and we're not supposed to go into STANDBY */
1737         if (d->bias_level == SND_SOC_BIAS_OFF &&
1738             d->target_bias_level != SND_SOC_BIAS_OFF) {
1739                 if (d->dev)
1740                         pm_runtime_get_sync(d->dev);
1741
1742                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1743                 if (ret != 0)
1744                         dev_err(d->dev,
1745                                 "ASoC: Failed to turn on bias: %d\n", ret);
1746         }
1747
1748         /* Prepare for a transition to ON or away from ON */
1749         if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1750              d->bias_level != SND_SOC_BIAS_ON) ||
1751             (d->target_bias_level != SND_SOC_BIAS_ON &&
1752              d->bias_level == SND_SOC_BIAS_ON)) {
1753                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1754                 if (ret != 0)
1755                         dev_err(d->dev,
1756                                 "ASoC: Failed to prepare bias: %d\n", ret);
1757         }
1758 }
1759
1760 /* Async callback run prior to DAPM sequences - brings to their final
1761  * state.
1762  */
1763 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1764 {
1765         struct snd_soc_dapm_context *d = data;
1766         int ret;
1767
1768         /* If we just powered the last thing off drop to standby bias */
1769         if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1770             (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1771              d->target_bias_level == SND_SOC_BIAS_OFF)) {
1772                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1773                 if (ret != 0)
1774                         dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1775                                 ret);
1776         }
1777
1778         /* If we're in standby and can support bias off then do that */
1779         if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1780             d->target_bias_level == SND_SOC_BIAS_OFF) {
1781                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1782                 if (ret != 0)
1783                         dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1784                                 ret);
1785
1786                 if (d->dev)
1787                         pm_runtime_put(d->dev);
1788         }
1789
1790         /* If we just powered up then move to active bias */
1791         if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1792             d->target_bias_level == SND_SOC_BIAS_ON) {
1793                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1794                 if (ret != 0)
1795                         dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1796                                 ret);
1797         }
1798 }
1799
1800 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1801                                        bool power, bool connect)
1802 {
1803         /* If a connection is being made or broken then that update
1804          * will have marked the peer dirty, otherwise the widgets are
1805          * not connected and this update has no impact. */
1806         if (!connect)
1807                 return;
1808
1809         /* If the peer is already in the state we're moving to then we
1810          * won't have an impact on it. */
1811         if (power != peer->power)
1812                 dapm_mark_dirty(peer, "peer state change");
1813 }
1814
1815 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1816                                   struct list_head *up_list,
1817                                   struct list_head *down_list)
1818 {
1819         struct snd_soc_dapm_path *path;
1820
1821         if (w->power == power)
1822                 return;
1823
1824         trace_snd_soc_dapm_widget_power(w, power);
1825
1826         /* If we changed our power state perhaps our neigbours changed
1827          * also.
1828          */
1829         snd_soc_dapm_widget_for_each_source_path(w, path)
1830                 dapm_widget_set_peer_power(path->source, power, path->connect);
1831
1832         /* Supplies can't affect their outputs, only their inputs */
1833         if (!w->is_supply) {
1834                 snd_soc_dapm_widget_for_each_sink_path(w, path)
1835                         dapm_widget_set_peer_power(path->sink, power,
1836                                                    path->connect);
1837         }
1838
1839         if (power)
1840                 dapm_seq_insert(w, up_list, true);
1841         else
1842                 dapm_seq_insert(w, down_list, false);
1843 }
1844
1845 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1846                                   struct list_head *up_list,
1847                                   struct list_head *down_list)
1848 {
1849         int power;
1850
1851         switch (w->id) {
1852         case snd_soc_dapm_pre:
1853                 dapm_seq_insert(w, down_list, false);
1854                 break;
1855         case snd_soc_dapm_post:
1856                 dapm_seq_insert(w, up_list, true);
1857                 break;
1858
1859         default:
1860                 power = dapm_widget_power_check(w);
1861
1862                 dapm_widget_set_power(w, power, up_list, down_list);
1863                 break;
1864         }
1865 }
1866
1867 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1868 {
1869         if (dapm->idle_bias_off)
1870                 return true;
1871
1872         switch (snd_power_get_state(dapm->card->snd_card)) {
1873         case SNDRV_CTL_POWER_D3hot:
1874         case SNDRV_CTL_POWER_D3cold:
1875                 return dapm->suspend_bias_off;
1876         default:
1877                 break;
1878         }
1879
1880         return false;
1881 }
1882
1883 /*
1884  * Scan each dapm widget for complete audio path.
1885  * A complete path is a route that has valid endpoints i.e.:-
1886  *
1887  *  o DAC to output pin.
1888  *  o Input pin to ADC.
1889  *  o Input pin to Output pin (bypass, sidetone)
1890  *  o DAC to ADC (loopback).
1891  */
1892 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1893 {
1894         struct snd_soc_dapm_widget *w;
1895         struct snd_soc_dapm_context *d;
1896         LIST_HEAD(up_list);
1897         LIST_HEAD(down_list);
1898         ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1899         enum snd_soc_bias_level bias;
1900
1901         lockdep_assert_held(&card->dapm_mutex);
1902
1903         trace_snd_soc_dapm_start(card);
1904
1905         list_for_each_entry(d, &card->dapm_list, list) {
1906                 if (dapm_idle_bias_off(d))
1907                         d->target_bias_level = SND_SOC_BIAS_OFF;
1908                 else
1909                         d->target_bias_level = SND_SOC_BIAS_STANDBY;
1910         }
1911
1912         dapm_reset(card);
1913
1914         /* Check which widgets we need to power and store them in
1915          * lists indicating if they should be powered up or down.  We
1916          * only check widgets that have been flagged as dirty but note
1917          * that new widgets may be added to the dirty list while we
1918          * iterate.
1919          */
1920         list_for_each_entry(w, &card->dapm_dirty, dirty) {
1921                 dapm_power_one_widget(w, &up_list, &down_list);
1922         }
1923
1924         list_for_each_entry(w, &card->widgets, list) {
1925                 switch (w->id) {
1926                 case snd_soc_dapm_pre:
1927                 case snd_soc_dapm_post:
1928                         /* These widgets always need to be powered */
1929                         break;
1930                 default:
1931                         list_del_init(&w->dirty);
1932                         break;
1933                 }
1934
1935                 if (w->new_power) {
1936                         d = w->dapm;
1937
1938                         /* Supplies and micbiases only bring the
1939                          * context up to STANDBY as unless something
1940                          * else is active and passing audio they
1941                          * generally don't require full power.  Signal
1942                          * generators are virtual pins and have no
1943                          * power impact themselves.
1944                          */
1945                         switch (w->id) {
1946                         case snd_soc_dapm_siggen:
1947                         case snd_soc_dapm_vmid:
1948                                 break;
1949                         case snd_soc_dapm_supply:
1950                         case snd_soc_dapm_regulator_supply:
1951                         case snd_soc_dapm_clock_supply:
1952                         case snd_soc_dapm_micbias:
1953                                 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1954                                         d->target_bias_level = SND_SOC_BIAS_STANDBY;
1955                                 break;
1956                         default:
1957                                 d->target_bias_level = SND_SOC_BIAS_ON;
1958                                 break;
1959                         }
1960                 }
1961
1962         }
1963
1964         /* Force all contexts in the card to the same bias state if
1965          * they're not ground referenced.
1966          */
1967         bias = SND_SOC_BIAS_OFF;
1968         list_for_each_entry(d, &card->dapm_list, list)
1969                 if (d->target_bias_level > bias)
1970                         bias = d->target_bias_level;
1971         list_for_each_entry(d, &card->dapm_list, list)
1972                 if (!dapm_idle_bias_off(d))
1973                         d->target_bias_level = bias;
1974
1975         trace_snd_soc_dapm_walk_done(card);
1976
1977         /* Run card bias changes at first */
1978         dapm_pre_sequence_async(&card->dapm, 0);
1979         /* Run other bias changes in parallel */
1980         list_for_each_entry(d, &card->dapm_list, list) {
1981                 if (d != &card->dapm)
1982                         async_schedule_domain(dapm_pre_sequence_async, d,
1983                                                 &async_domain);
1984         }
1985         async_synchronize_full_domain(&async_domain);
1986
1987         list_for_each_entry(w, &down_list, power_list) {
1988                 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1989         }
1990
1991         list_for_each_entry(w, &up_list, power_list) {
1992                 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1993         }
1994
1995         /* Power down widgets first; try to avoid amplifying pops. */
1996         dapm_seq_run(card, &down_list, event, false);
1997
1998         dapm_widget_update(card);
1999
2000         /* Now power up. */
2001         dapm_seq_run(card, &up_list, event, true);
2002
2003         /* Run all the bias changes in parallel */
2004         list_for_each_entry(d, &card->dapm_list, list) {
2005                 if (d != &card->dapm)
2006                         async_schedule_domain(dapm_post_sequence_async, d,
2007                                                 &async_domain);
2008         }
2009         async_synchronize_full_domain(&async_domain);
2010         /* Run card bias changes at last */
2011         dapm_post_sequence_async(&card->dapm, 0);
2012
2013         /* do we need to notify any clients that DAPM event is complete */
2014         list_for_each_entry(d, &card->dapm_list, list) {
2015                 if (d->stream_event)
2016                         d->stream_event(d, event);
2017         }
2018
2019         pop_dbg(card->dev, card->pop_time,
2020                 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2021         pop_wait(card->pop_time);
2022
2023         trace_snd_soc_dapm_done(card);
2024
2025         return 0;
2026 }
2027
2028 #ifdef CONFIG_DEBUG_FS
2029 static ssize_t dapm_widget_power_read_file(struct file *file,
2030                                            char __user *user_buf,
2031                                            size_t count, loff_t *ppos)
2032 {
2033         struct snd_soc_dapm_widget *w = file->private_data;
2034         struct snd_soc_card *card = w->dapm->card;
2035         enum snd_soc_dapm_direction dir, rdir;
2036         char *buf;
2037         int in, out;
2038         ssize_t ret;
2039         struct snd_soc_dapm_path *p = NULL;
2040
2041         buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2042         if (!buf)
2043                 return -ENOMEM;
2044
2045         mutex_lock(&card->dapm_mutex);
2046
2047         /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2048         if (w->is_supply) {
2049                 in = 0;
2050                 out = 0;
2051         } else {
2052                 in = is_connected_input_ep(w, NULL, NULL);
2053                 out = is_connected_output_ep(w, NULL, NULL);
2054         }
2055
2056         ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s  in %d out %d",
2057                        w->name, w->power ? "On" : "Off",
2058                        w->force ? " (forced)" : "", in, out);
2059
2060         if (w->reg >= 0)
2061                 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2062                                 " - R%d(0x%x) mask 0x%x",
2063                                 w->reg, w->reg, w->mask << w->shift);
2064
2065         ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2066
2067         if (w->sname)
2068                 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2069                                 w->sname,
2070                                 w->active ? "active" : "inactive");
2071
2072         snd_soc_dapm_for_each_direction(dir) {
2073                 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2074                 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2075                         if (p->connected && !p->connected(w, p->node[rdir]))
2076                                 continue;
2077
2078                         if (!p->connect)
2079                                 continue;
2080
2081                         ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2082                                         " %s  \"%s\" \"%s\"\n",
2083                                         (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2084                                         p->name ? p->name : "static",
2085                                         p->node[rdir]->name);
2086                 }
2087         }
2088
2089         mutex_unlock(&card->dapm_mutex);
2090
2091         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2092
2093         kfree(buf);
2094         return ret;
2095 }
2096
2097 static const struct file_operations dapm_widget_power_fops = {
2098         .open = simple_open,
2099         .read = dapm_widget_power_read_file,
2100         .llseek = default_llseek,
2101 };
2102
2103 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2104                                    size_t count, loff_t *ppos)
2105 {
2106         struct snd_soc_dapm_context *dapm = file->private_data;
2107         char *level;
2108
2109         switch (dapm->bias_level) {
2110         case SND_SOC_BIAS_ON:
2111                 level = "On\n";
2112                 break;
2113         case SND_SOC_BIAS_PREPARE:
2114                 level = "Prepare\n";
2115                 break;
2116         case SND_SOC_BIAS_STANDBY:
2117                 level = "Standby\n";
2118                 break;
2119         case SND_SOC_BIAS_OFF:
2120                 level = "Off\n";
2121                 break;
2122         default:
2123                 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2124                 level = "Unknown\n";
2125                 break;
2126         }
2127
2128         return simple_read_from_buffer(user_buf, count, ppos, level,
2129                                        strlen(level));
2130 }
2131
2132 static const struct file_operations dapm_bias_fops = {
2133         .open = simple_open,
2134         .read = dapm_bias_read_file,
2135         .llseek = default_llseek,
2136 };
2137
2138 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2139         struct dentry *parent)
2140 {
2141         struct dentry *d;
2142
2143         if (!parent || IS_ERR(parent))
2144                 return;
2145
2146         dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2147
2148         if (IS_ERR(dapm->debugfs_dapm)) {
2149                 dev_warn(dapm->dev,
2150                          "ASoC: Failed to create DAPM debugfs directory %ld\n",
2151                          PTR_ERR(dapm->debugfs_dapm));
2152                 return;
2153         }
2154
2155         d = debugfs_create_file("bias_level", 0444,
2156                                 dapm->debugfs_dapm, dapm,
2157                                 &dapm_bias_fops);
2158         if (IS_ERR(d))
2159                 dev_warn(dapm->dev,
2160                          "ASoC: Failed to create bias level debugfs file: %ld\n",
2161                          PTR_ERR(d));
2162 }
2163
2164 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2165 {
2166         struct snd_soc_dapm_context *dapm = w->dapm;
2167         struct dentry *d;
2168
2169         if (!dapm->debugfs_dapm || !w->name)
2170                 return;
2171
2172         d = debugfs_create_file(w->name, 0444,
2173                                 dapm->debugfs_dapm, w,
2174                                 &dapm_widget_power_fops);
2175         if (IS_ERR(d))
2176                 dev_warn(w->dapm->dev,
2177                          "ASoC: Failed to create %s debugfs file: %ld\n",
2178                          w->name, PTR_ERR(d));
2179 }
2180
2181 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2182 {
2183         debugfs_remove_recursive(dapm->debugfs_dapm);
2184 }
2185
2186 #else
2187 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2188         struct dentry *parent)
2189 {
2190 }
2191
2192 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2193 {
2194 }
2195
2196 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2197 {
2198 }
2199
2200 #endif
2201
2202 /*
2203  * soc_dapm_connect_path() - Connects or disconnects a path
2204  * @path: The path to update
2205  * @connect: The new connect state of the path. True if the path is connected,
2206  *  false if it is disconnected.
2207  * @reason: The reason why the path changed (for debugging only)
2208  */
2209 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2210         bool connect, const char *reason)
2211 {
2212         if (path->connect == connect)
2213                 return;
2214
2215         path->connect = connect;
2216         dapm_mark_dirty(path->source, reason);
2217         dapm_mark_dirty(path->sink, reason);
2218         dapm_path_invalidate(path);
2219 }
2220
2221 /* test and update the power status of a mux widget */
2222 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2223                                  struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2224 {
2225         struct snd_soc_dapm_path *path;
2226         int found = 0;
2227         bool connect;
2228
2229         lockdep_assert_held(&card->dapm_mutex);
2230
2231         /* find dapm widget path assoc with kcontrol */
2232         dapm_kcontrol_for_each_path(path, kcontrol) {
2233                 found = 1;
2234                 /* we now need to match the string in the enum to the path */
2235                 if (!(strcmp(path->name, e->texts[mux])))
2236                         connect = true;
2237                 else
2238                         connect = false;
2239
2240                 soc_dapm_connect_path(path, connect, "mux update");
2241         }
2242
2243         if (found)
2244                 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2245
2246         return found;
2247 }
2248
2249 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2250         struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2251         struct snd_soc_dapm_update *update)
2252 {
2253         struct snd_soc_card *card = dapm->card;
2254         int ret;
2255
2256         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2257         card->update = update;
2258         ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2259         card->update = NULL;
2260         mutex_unlock(&card->dapm_mutex);
2261         if (ret > 0)
2262                 soc_dpcm_runtime_update(card);
2263         return ret;
2264 }
2265 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2266
2267 /* test and update the power status of a mixer or switch widget */
2268 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2269                                        struct snd_kcontrol *kcontrol,
2270                                        int connect, int rconnect)
2271 {
2272         struct snd_soc_dapm_path *path;
2273         int found = 0;
2274
2275         lockdep_assert_held(&card->dapm_mutex);
2276
2277         /* find dapm widget path assoc with kcontrol */
2278         dapm_kcontrol_for_each_path(path, kcontrol) {
2279                 /*
2280                  * Ideally this function should support any number of
2281                  * paths and channels. But since kcontrols only come
2282                  * in mono and stereo variants, we are limited to 2
2283                  * channels.
2284                  *
2285                  * The following code assumes for stereo controls the
2286                  * first path (when 'found == 0') is the left channel,
2287                  * and all remaining paths (when 'found == 1') are the
2288                  * right channel.
2289                  *
2290                  * A stereo control is signified by a valid 'rconnect'
2291                  * value, either 0 for unconnected, or >= 0 for connected.
2292                  * This is chosen instead of using snd_soc_volsw_is_stereo,
2293                  * so that the behavior of snd_soc_dapm_mixer_update_power
2294                  * doesn't change even when the kcontrol passed in is
2295                  * stereo.
2296                  *
2297                  * It passes 'connect' as the path connect status for
2298                  * the left channel, and 'rconnect' for the right
2299                  * channel.
2300                  */
2301                 if (found && rconnect >= 0)
2302                         soc_dapm_connect_path(path, rconnect, "mixer update");
2303                 else
2304                         soc_dapm_connect_path(path, connect, "mixer update");
2305                 found = 1;
2306         }
2307
2308         if (found)
2309                 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2310
2311         return found;
2312 }
2313
2314 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2315         struct snd_kcontrol *kcontrol, int connect,
2316         struct snd_soc_dapm_update *update)
2317 {
2318         struct snd_soc_card *card = dapm->card;
2319         int ret;
2320
2321         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2322         card->update = update;
2323         ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2324         card->update = NULL;
2325         mutex_unlock(&card->dapm_mutex);
2326         if (ret > 0)
2327                 soc_dpcm_runtime_update(card);
2328         return ret;
2329 }
2330 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2331
2332 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2333         char *buf)
2334 {
2335         struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2336         struct snd_soc_dapm_widget *w;
2337         int count = 0;
2338         char *state = "not set";
2339
2340         /* card won't be set for the dummy component, as a spot fix
2341          * we're checking for that case specifically here but in future
2342          * we will ensure that the dummy component looks like others.
2343          */
2344         if (!cmpnt->card)
2345                 return 0;
2346
2347         list_for_each_entry(w, &cmpnt->card->widgets, list) {
2348                 if (w->dapm != dapm)
2349                         continue;
2350
2351                 /* only display widgets that burn power */
2352                 switch (w->id) {
2353                 case snd_soc_dapm_hp:
2354                 case snd_soc_dapm_mic:
2355                 case snd_soc_dapm_spk:
2356                 case snd_soc_dapm_line:
2357                 case snd_soc_dapm_micbias:
2358                 case snd_soc_dapm_dac:
2359                 case snd_soc_dapm_adc:
2360                 case snd_soc_dapm_pga:
2361                 case snd_soc_dapm_out_drv:
2362                 case snd_soc_dapm_mixer:
2363                 case snd_soc_dapm_mixer_named_ctl:
2364                 case snd_soc_dapm_supply:
2365                 case snd_soc_dapm_regulator_supply:
2366                 case snd_soc_dapm_clock_supply:
2367                         if (w->name)
2368                                 count += sprintf(buf + count, "%s: %s\n",
2369                                         w->name, w->power ? "On":"Off");
2370                 break;
2371                 default:
2372                 break;
2373                 }
2374         }
2375
2376         switch (snd_soc_dapm_get_bias_level(dapm)) {
2377         case SND_SOC_BIAS_ON:
2378                 state = "On";
2379                 break;
2380         case SND_SOC_BIAS_PREPARE:
2381                 state = "Prepare";
2382                 break;
2383         case SND_SOC_BIAS_STANDBY:
2384                 state = "Standby";
2385                 break;
2386         case SND_SOC_BIAS_OFF:
2387                 state = "Off";
2388                 break;
2389         }
2390         count += sprintf(buf + count, "PM State: %s\n", state);
2391
2392         return count;
2393 }
2394
2395 /* show dapm widget status in sys fs */
2396 static ssize_t dapm_widget_show(struct device *dev,
2397         struct device_attribute *attr, char *buf)
2398 {
2399         struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2400         int i, count = 0;
2401
2402         mutex_lock(&rtd->card->dapm_mutex);
2403
2404         for (i = 0; i < rtd->num_codecs; i++) {
2405                 struct snd_soc_component *cmpnt = rtd->codec_dais[i]->component;
2406
2407                 count += dapm_widget_show_component(cmpnt, buf + count);
2408         }
2409
2410         mutex_unlock(&rtd->card->dapm_mutex);
2411
2412         return count;
2413 }
2414
2415 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2416
2417 struct attribute *soc_dapm_dev_attrs[] = {
2418         &dev_attr_dapm_widget.attr,
2419         NULL
2420 };
2421
2422 static void dapm_free_path(struct snd_soc_dapm_path *path)
2423 {
2424         list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2425         list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2426         list_del(&path->list_kcontrol);
2427         list_del(&path->list);
2428         kfree(path);
2429 }
2430
2431 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2432 {
2433         struct snd_soc_dapm_path *p, *next_p;
2434         enum snd_soc_dapm_direction dir;
2435
2436         list_del(&w->list);
2437         list_del(&w->dirty);
2438         /*
2439          * remove source and sink paths associated to this widget.
2440          * While removing the path, remove reference to it from both
2441          * source and sink widgets so that path is removed only once.
2442          */
2443         snd_soc_dapm_for_each_direction(dir) {
2444                 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2445                         dapm_free_path(p);
2446         }
2447
2448         kfree(w->kcontrols);
2449         kfree_const(w->name);
2450         kfree(w);
2451 }
2452
2453 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2454 {
2455         dapm->path_sink_cache.widget = NULL;
2456         dapm->path_source_cache.widget = NULL;
2457 }
2458
2459 /* free all dapm widgets and resources */
2460 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2461 {
2462         struct snd_soc_dapm_widget *w, *next_w;
2463
2464         list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2465                 if (w->dapm != dapm)
2466                         continue;
2467                 snd_soc_dapm_free_widget(w);
2468         }
2469         snd_soc_dapm_reset_cache(dapm);
2470 }
2471
2472 static struct snd_soc_dapm_widget *dapm_find_widget(
2473                         struct snd_soc_dapm_context *dapm, const char *pin,
2474                         bool search_other_contexts)
2475 {
2476         struct snd_soc_dapm_widget *w;
2477         struct snd_soc_dapm_widget *fallback = NULL;
2478
2479         list_for_each_entry(w, &dapm->card->widgets, list) {
2480                 if (!strcmp(w->name, pin)) {
2481                         if (w->dapm == dapm)
2482                                 return w;
2483                         else
2484                                 fallback = w;
2485                 }
2486         }
2487
2488         if (search_other_contexts)
2489                 return fallback;
2490
2491         return NULL;
2492 }
2493
2494 /*
2495  * set the DAPM pin status:
2496  * returns 1 when the value has been updated, 0 when unchanged, or a negative
2497  * error code; called from kcontrol put callback
2498  */
2499 static int __snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2500                                   const char *pin, int status)
2501 {
2502         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2503         int ret = 0;
2504
2505         dapm_assert_locked(dapm);
2506
2507         if (!w) {
2508                 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2509                 return -EINVAL;
2510         }
2511
2512         if (w->connected != status) {
2513                 dapm_mark_dirty(w, "pin configuration");
2514                 dapm_widget_invalidate_input_paths(w);
2515                 dapm_widget_invalidate_output_paths(w);
2516                 ret = 1;
2517         }
2518
2519         w->connected = status;
2520         if (status == 0)
2521                 w->force = 0;
2522
2523         return ret;
2524 }
2525
2526 /*
2527  * similar as __snd_soc_dapm_set_pin(), but returns 0 when successful;
2528  * called from several API functions below
2529  */
2530 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2531                                 const char *pin, int status)
2532 {
2533         int ret = __snd_soc_dapm_set_pin(dapm, pin, status);
2534
2535         return ret < 0 ? ret : 0;
2536 }
2537
2538 /**
2539  * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2540  * @dapm: DAPM context
2541  *
2542  * Walks all dapm audio paths and powers widgets according to their
2543  * stream or path usage.
2544  *
2545  * Requires external locking.
2546  *
2547  * Returns 0 for success.
2548  */
2549 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2550 {
2551         /*
2552          * Suppress early reports (eg, jacks syncing their state) to avoid
2553          * silly DAPM runs during card startup.
2554          */
2555         if (!dapm->card || !dapm->card->instantiated)
2556                 return 0;
2557
2558         return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2559 }
2560 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2561
2562 /**
2563  * snd_soc_dapm_sync - scan and power dapm paths
2564  * @dapm: DAPM context
2565  *
2566  * Walks all dapm audio paths and powers widgets according to their
2567  * stream or path usage.
2568  *
2569  * Returns 0 for success.
2570  */
2571 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2572 {
2573         int ret;
2574
2575         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2576         ret = snd_soc_dapm_sync_unlocked(dapm);
2577         mutex_unlock(&dapm->card->dapm_mutex);
2578         return ret;
2579 }
2580 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2581
2582 /*
2583  * dapm_update_widget_flags() - Re-compute widget sink and source flags
2584  * @w: The widget for which to update the flags
2585  *
2586  * Some widgets have a dynamic category which depends on which neighbors they
2587  * are connected to. This function update the category for these widgets.
2588  *
2589  * This function must be called whenever a path is added or removed to a widget.
2590  */
2591 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2592 {
2593         enum snd_soc_dapm_direction dir;
2594         struct snd_soc_dapm_path *p;
2595         unsigned int ep;
2596
2597         switch (w->id) {
2598         case snd_soc_dapm_input:
2599                 /* On a fully routed card an input is never a source */
2600                 if (w->dapm->card->fully_routed)
2601                         return;
2602                 ep = SND_SOC_DAPM_EP_SOURCE;
2603                 snd_soc_dapm_widget_for_each_source_path(w, p) {
2604                         if (p->source->id == snd_soc_dapm_micbias ||
2605                                 p->source->id == snd_soc_dapm_mic ||
2606                                 p->source->id == snd_soc_dapm_line ||
2607                                 p->source->id == snd_soc_dapm_output) {
2608                                         ep = 0;
2609                                         break;
2610                         }
2611                 }
2612                 break;
2613         case snd_soc_dapm_output:
2614                 /* On a fully routed card a output is never a sink */
2615                 if (w->dapm->card->fully_routed)
2616                         return;
2617                 ep = SND_SOC_DAPM_EP_SINK;
2618                 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2619                         if (p->sink->id == snd_soc_dapm_spk ||
2620                                 p->sink->id == snd_soc_dapm_hp ||
2621                                 p->sink->id == snd_soc_dapm_line ||
2622                                 p->sink->id == snd_soc_dapm_input) {
2623                                         ep = 0;
2624                                         break;
2625                         }
2626                 }
2627                 break;
2628         case snd_soc_dapm_line:
2629                 ep = 0;
2630                 snd_soc_dapm_for_each_direction(dir) {
2631                         if (!list_empty(&w->edges[dir]))
2632                                 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2633                 }
2634                 break;
2635         default:
2636                 return;
2637         }
2638
2639         w->is_ep = ep;
2640 }
2641
2642 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2643         struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2644         const char *control)
2645 {
2646         bool dynamic_source = false;
2647         bool dynamic_sink = false;
2648
2649         if (!control)
2650                 return 0;
2651
2652         switch (source->id) {
2653         case snd_soc_dapm_demux:
2654                 dynamic_source = true;
2655                 break;
2656         default:
2657                 break;
2658         }
2659
2660         switch (sink->id) {
2661         case snd_soc_dapm_mux:
2662         case snd_soc_dapm_switch:
2663         case snd_soc_dapm_mixer:
2664         case snd_soc_dapm_mixer_named_ctl:
2665                 dynamic_sink = true;
2666                 break;
2667         default:
2668                 break;
2669         }
2670
2671         if (dynamic_source && dynamic_sink) {
2672                 dev_err(dapm->dev,
2673                         "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2674                         source->name, control, sink->name);
2675                 return -EINVAL;
2676         } else if (!dynamic_source && !dynamic_sink) {
2677                 dev_err(dapm->dev,
2678                         "Control not supported for path %s -> [%s] -> %s\n",
2679                         source->name, control, sink->name);
2680                 return -EINVAL;
2681         }
2682
2683         return 0;
2684 }
2685
2686 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2687         struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2688         const char *control,
2689         int (*connected)(struct snd_soc_dapm_widget *source,
2690                          struct snd_soc_dapm_widget *sink))
2691 {
2692         struct snd_soc_dapm_widget *widgets[2];
2693         enum snd_soc_dapm_direction dir;
2694         struct snd_soc_dapm_path *path;
2695         int ret;
2696
2697         if (wsink->is_supply && !wsource->is_supply) {
2698                 dev_err(dapm->dev,
2699                         "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2700                         wsource->name, wsink->name);
2701                 return -EINVAL;
2702         }
2703
2704         if (connected && !wsource->is_supply) {
2705                 dev_err(dapm->dev,
2706                         "connected() callback only supported for supply widgets (%s -> %s)\n",
2707                         wsource->name, wsink->name);
2708                 return -EINVAL;
2709         }
2710
2711         if (wsource->is_supply && control) {
2712                 dev_err(dapm->dev,
2713                         "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2714                         wsource->name, control, wsink->name);
2715                 return -EINVAL;
2716         }
2717
2718         ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2719         if (ret)
2720                 return ret;
2721
2722         path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2723         if (!path)
2724                 return -ENOMEM;
2725
2726         path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2727         path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2728         widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2729         widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2730
2731         path->connected = connected;
2732         INIT_LIST_HEAD(&path->list);
2733         INIT_LIST_HEAD(&path->list_kcontrol);
2734
2735         if (wsource->is_supply || wsink->is_supply)
2736                 path->is_supply = 1;
2737
2738         /* connect static paths */
2739         if (control == NULL) {
2740                 path->connect = 1;
2741         } else {
2742                 switch (wsource->id) {
2743                 case snd_soc_dapm_demux:
2744                         ret = dapm_connect_mux(dapm, path, control, wsource);
2745                         if (ret)
2746                                 goto err;
2747                         break;
2748                 default:
2749                         break;
2750                 }
2751
2752                 switch (wsink->id) {
2753                 case snd_soc_dapm_mux:
2754                         ret = dapm_connect_mux(dapm, path, control, wsink);
2755                         if (ret != 0)
2756                                 goto err;
2757                         break;
2758                 case snd_soc_dapm_switch:
2759                 case snd_soc_dapm_mixer:
2760                 case snd_soc_dapm_mixer_named_ctl:
2761                         ret = dapm_connect_mixer(dapm, path, control);
2762                         if (ret != 0)
2763                                 goto err;
2764                         break;
2765                 default:
2766                         break;
2767                 }
2768         }
2769
2770         list_add(&path->list, &dapm->card->paths);
2771         snd_soc_dapm_for_each_direction(dir)
2772                 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2773
2774         snd_soc_dapm_for_each_direction(dir) {
2775                 dapm_update_widget_flags(widgets[dir]);
2776                 dapm_mark_dirty(widgets[dir], "Route added");
2777         }
2778
2779         if (dapm->card->instantiated && path->connect)
2780                 dapm_path_invalidate(path);
2781
2782         return 0;
2783 err:
2784         kfree(path);
2785         return ret;
2786 }
2787
2788 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2789                                   const struct snd_soc_dapm_route *route)
2790 {
2791         struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2792         struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2793         const char *sink;
2794         const char *source;
2795         char prefixed_sink[80];
2796         char prefixed_source[80];
2797         const char *prefix;
2798         int ret;
2799
2800         prefix = soc_dapm_prefix(dapm);
2801         if (prefix) {
2802                 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2803                          prefix, route->sink);
2804                 sink = prefixed_sink;
2805                 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2806                          prefix, route->source);
2807                 source = prefixed_source;
2808         } else {
2809                 sink = route->sink;
2810                 source = route->source;
2811         }
2812
2813         wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2814         wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2815
2816         if (wsink && wsource)
2817                 goto skip_search;
2818
2819         /*
2820          * find src and dest widgets over all widgets but favor a widget from
2821          * current DAPM context
2822          */
2823         list_for_each_entry(w, &dapm->card->widgets, list) {
2824                 if (!wsink && !(strcmp(w->name, sink))) {
2825                         wtsink = w;
2826                         if (w->dapm == dapm) {
2827                                 wsink = w;
2828                                 if (wsource)
2829                                         break;
2830                         }
2831                         continue;
2832                 }
2833                 if (!wsource && !(strcmp(w->name, source))) {
2834                         wtsource = w;
2835                         if (w->dapm == dapm) {
2836                                 wsource = w;
2837                                 if (wsink)
2838                                         break;
2839                         }
2840                 }
2841         }
2842         /* use widget from another DAPM context if not found from this */
2843         if (!wsink)
2844                 wsink = wtsink;
2845         if (!wsource)
2846                 wsource = wtsource;
2847
2848         if (wsource == NULL) {
2849                 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2850                         route->source);
2851                 return -ENODEV;
2852         }
2853         if (wsink == NULL) {
2854                 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2855                         route->sink);
2856                 return -ENODEV;
2857         }
2858
2859 skip_search:
2860         dapm_wcache_update(&dapm->path_sink_cache, wsink);
2861         dapm_wcache_update(&dapm->path_source_cache, wsource);
2862
2863         ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2864                 route->connected);
2865         if (ret)
2866                 goto err;
2867
2868         return 0;
2869 err:
2870         dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2871                  source, route->control, sink);
2872         return ret;
2873 }
2874
2875 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2876                                   const struct snd_soc_dapm_route *route)
2877 {
2878         struct snd_soc_dapm_widget *wsource, *wsink;
2879         struct snd_soc_dapm_path *path, *p;
2880         const char *sink;
2881         const char *source;
2882         char prefixed_sink[80];
2883         char prefixed_source[80];
2884         const char *prefix;
2885
2886         if (route->control) {
2887                 dev_err(dapm->dev,
2888                         "ASoC: Removal of routes with controls not supported\n");
2889                 return -EINVAL;
2890         }
2891
2892         prefix = soc_dapm_prefix(dapm);
2893         if (prefix) {
2894                 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2895                          prefix, route->sink);
2896                 sink = prefixed_sink;
2897                 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2898                          prefix, route->source);
2899                 source = prefixed_source;
2900         } else {
2901                 sink = route->sink;
2902                 source = route->source;
2903         }
2904
2905         path = NULL;
2906         list_for_each_entry(p, &dapm->card->paths, list) {
2907                 if (strcmp(p->source->name, source) != 0)
2908                         continue;
2909                 if (strcmp(p->sink->name, sink) != 0)
2910                         continue;
2911                 path = p;
2912                 break;
2913         }
2914
2915         if (path) {
2916                 wsource = path->source;
2917                 wsink = path->sink;
2918
2919                 dapm_mark_dirty(wsource, "Route removed");
2920                 dapm_mark_dirty(wsink, "Route removed");
2921                 if (path->connect)
2922                         dapm_path_invalidate(path);
2923
2924                 dapm_free_path(path);
2925
2926                 /* Update any path related flags */
2927                 dapm_update_widget_flags(wsource);
2928                 dapm_update_widget_flags(wsink);
2929         } else {
2930                 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2931                          source, sink);
2932         }
2933
2934         return 0;
2935 }
2936
2937 /**
2938  * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2939  * @dapm: DAPM context
2940  * @route: audio routes
2941  * @num: number of routes
2942  *
2943  * Connects 2 dapm widgets together via a named audio path. The sink is
2944  * the widget receiving the audio signal, whilst the source is the sender
2945  * of the audio signal.
2946  *
2947  * Returns 0 for success else error. On error all resources can be freed
2948  * with a call to snd_soc_card_free().
2949  */
2950 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2951                             const struct snd_soc_dapm_route *route, int num)
2952 {
2953         int i, r, ret = 0;
2954
2955         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2956         for (i = 0; i < num; i++) {
2957                 r = snd_soc_dapm_add_route(dapm, route);
2958                 if (r < 0) {
2959                         dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2960                                 route->source,
2961                                 route->control ? route->control : "direct",
2962                                 route->sink);
2963                         ret = r;
2964                 }
2965                 route++;
2966         }
2967         mutex_unlock(&dapm->card->dapm_mutex);
2968
2969         return ret;
2970 }
2971 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2972
2973 /**
2974  * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2975  * @dapm: DAPM context
2976  * @route: audio routes
2977  * @num: number of routes
2978  *
2979  * Removes routes from the DAPM context.
2980  */
2981 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2982                             const struct snd_soc_dapm_route *route, int num)
2983 {
2984         int i;
2985
2986         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2987         for (i = 0; i < num; i++) {
2988                 snd_soc_dapm_del_route(dapm, route);
2989                 route++;
2990         }
2991         mutex_unlock(&dapm->card->dapm_mutex);
2992
2993         return 0;
2994 }
2995 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2996
2997 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2998                                    const struct snd_soc_dapm_route *route)
2999 {
3000         struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3001                                                               route->source,
3002                                                               true);
3003         struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3004                                                             route->sink,
3005                                                             true);
3006         struct snd_soc_dapm_path *path;
3007         int count = 0;
3008
3009         if (!source) {
3010                 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3011                         route->source);
3012                 return -ENODEV;
3013         }
3014
3015         if (!sink) {
3016                 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3017                         route->sink);
3018                 return -ENODEV;
3019         }
3020
3021         if (route->control || route->connected)
3022                 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3023                          route->source, route->sink);
3024
3025         snd_soc_dapm_widget_for_each_sink_path(source, path) {
3026                 if (path->sink == sink) {
3027                         path->weak = 1;
3028                         count++;
3029                 }
3030         }
3031
3032         if (count == 0)
3033                 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3034                         route->source, route->sink);
3035         if (count > 1)
3036                 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3037                          count, route->source, route->sink);
3038
3039         return 0;
3040 }
3041
3042 /**
3043  * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3044  * @dapm: DAPM context
3045  * @route: audio routes
3046  * @num: number of routes
3047  *
3048  * Mark existing routes matching those specified in the passed array
3049  * as being weak, meaning that they are ignored for the purpose of
3050  * power decisions.  The main intended use case is for sidetone paths
3051  * which couple audio between other independent paths if they are both
3052  * active in order to make the combination work better at the user
3053  * level but which aren't intended to be "used".
3054  *
3055  * Note that CODEC drivers should not use this as sidetone type paths
3056  * can frequently also be used as bypass paths.
3057  */
3058 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3059                              const struct snd_soc_dapm_route *route, int num)
3060 {
3061         int i, err;
3062         int ret = 0;
3063
3064         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3065         for (i = 0; i < num; i++) {
3066                 err = snd_soc_dapm_weak_route(dapm, route);
3067                 if (err)
3068                         ret = err;
3069                 route++;
3070         }
3071         mutex_unlock(&dapm->card->dapm_mutex);
3072
3073         return ret;
3074 }
3075 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3076
3077 /**
3078  * snd_soc_dapm_new_widgets - add new dapm widgets
3079  * @card: card to be checked for new dapm widgets
3080  *
3081  * Checks the codec for any new dapm widgets and creates them if found.
3082  *
3083  * Returns 0 for success.
3084  */
3085 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3086 {
3087         struct snd_soc_dapm_widget *w;
3088         unsigned int val;
3089
3090         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3091
3092         list_for_each_entry(w, &card->widgets, list)
3093         {
3094                 if (w->new)
3095                         continue;
3096
3097                 if (w->num_kcontrols) {
3098                         w->kcontrols = kzalloc(w->num_kcontrols *
3099                                                 sizeof(struct snd_kcontrol *),
3100                                                 GFP_KERNEL);
3101                         if (!w->kcontrols) {
3102                                 mutex_unlock(&card->dapm_mutex);
3103                                 return -ENOMEM;
3104                         }
3105                 }
3106
3107                 switch(w->id) {
3108                 case snd_soc_dapm_switch:
3109                 case snd_soc_dapm_mixer:
3110                 case snd_soc_dapm_mixer_named_ctl:
3111                         dapm_new_mixer(w);
3112                         break;
3113                 case snd_soc_dapm_mux:
3114                 case snd_soc_dapm_demux:
3115                         dapm_new_mux(w);
3116                         break;
3117                 case snd_soc_dapm_pga:
3118                 case snd_soc_dapm_out_drv:
3119                         dapm_new_pga(w);
3120                         break;
3121                 case snd_soc_dapm_dai_link:
3122                         dapm_new_dai_link(w);
3123                         break;
3124                 default:
3125                         break;
3126                 }
3127
3128                 /* Read the initial power state from the device */
3129                 if (w->reg >= 0) {
3130                         soc_dapm_read(w->dapm, w->reg, &val);
3131                         val = val >> w->shift;
3132                         val &= w->mask;
3133                         if (val == w->on_val)
3134                                 w->power = 1;
3135                 }
3136
3137                 w->new = 1;
3138
3139                 dapm_mark_dirty(w, "new widget");
3140                 dapm_debugfs_add_widget(w);
3141         }
3142
3143         dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3144         mutex_unlock(&card->dapm_mutex);
3145         return 0;
3146 }
3147 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3148
3149 /**
3150  * snd_soc_dapm_get_volsw - dapm mixer get callback
3151  * @kcontrol: mixer control
3152  * @ucontrol: control element information
3153  *
3154  * Callback to get the value of a dapm mixer control.
3155  *
3156  * Returns 0 for success.
3157  */
3158 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3159         struct snd_ctl_elem_value *ucontrol)
3160 {
3161         struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3162         struct snd_soc_card *card = dapm->card;
3163         struct soc_mixer_control *mc =
3164                 (struct soc_mixer_control *)kcontrol->private_value;
3165         int reg = mc->reg;
3166         unsigned int shift = mc->shift;
3167         int max = mc->max;
3168         unsigned int width = fls(max);
3169         unsigned int mask = (1 << fls(max)) - 1;
3170         unsigned int invert = mc->invert;
3171         unsigned int reg_val, val, rval = 0;
3172         int ret = 0;
3173
3174         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3175         if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3176                 ret = soc_dapm_read(dapm, reg, &reg_val);
3177                 val = (reg_val >> shift) & mask;
3178
3179                 if (ret == 0 && reg != mc->rreg)
3180                         ret = soc_dapm_read(dapm, mc->rreg, &reg_val);
3181
3182                 if (snd_soc_volsw_is_stereo(mc))
3183                         rval = (reg_val >> mc->rshift) & mask;
3184         } else {
3185                 reg_val = dapm_kcontrol_get_value(kcontrol);
3186                 val = reg_val & mask;
3187
3188                 if (snd_soc_volsw_is_stereo(mc))
3189                         rval = (reg_val >> width) & mask;
3190         }
3191         mutex_unlock(&card->dapm_mutex);
3192
3193         if (ret)
3194                 return ret;
3195
3196         if (invert)
3197                 ucontrol->value.integer.value[0] = max - val;
3198         else
3199                 ucontrol->value.integer.value[0] = val;
3200
3201         if (snd_soc_volsw_is_stereo(mc)) {
3202                 if (invert)
3203                         ucontrol->value.integer.value[1] = max - rval;
3204                 else
3205                         ucontrol->value.integer.value[1] = rval;
3206         }
3207
3208         return ret;
3209 }
3210 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3211
3212 /**
3213  * snd_soc_dapm_put_volsw - dapm mixer set callback
3214  * @kcontrol: mixer control
3215  * @ucontrol: control element information
3216  *
3217  * Callback to set the value of a dapm mixer control.
3218  *
3219  * Returns 0 for success.
3220  */
3221 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3222         struct snd_ctl_elem_value *ucontrol)
3223 {
3224         struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3225         struct snd_soc_card *card = dapm->card;
3226         struct soc_mixer_control *mc =
3227                 (struct soc_mixer_control *)kcontrol->private_value;
3228         int reg = mc->reg;
3229         unsigned int shift = mc->shift;
3230         int max = mc->max;
3231         unsigned int width = fls(max);
3232         unsigned int mask = (1 << width) - 1;
3233         unsigned int invert = mc->invert;
3234         unsigned int val, rval = 0;
3235         int connect, rconnect = -1, change, reg_change = 0;
3236         struct snd_soc_dapm_update update = { NULL };
3237         int ret = 0;
3238
3239         val = (ucontrol->value.integer.value[0] & mask);
3240         connect = !!val;
3241
3242         if (invert)
3243                 val = max - val;
3244
3245         if (snd_soc_volsw_is_stereo(mc)) {
3246                 rval = (ucontrol->value.integer.value[1] & mask);
3247                 rconnect = !!rval;
3248                 if (invert)
3249                         rval = max - rval;
3250         }
3251
3252         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3253
3254         /* This assumes field width < (bits in unsigned int / 2) */
3255         if (width > sizeof(unsigned int) * 8 / 2)
3256                 dev_warn(dapm->dev,
3257                          "ASoC: control %s field width limit exceeded\n",
3258                          kcontrol->id.name);
3259         change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3260
3261         if (reg != SND_SOC_NOPM) {
3262                 val = val << shift;
3263                 rval = rval << mc->rshift;
3264
3265                 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3266
3267                 if (snd_soc_volsw_is_stereo(mc))
3268                         reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3269                                                          mask << mc->rshift,
3270                                                          rval);
3271         }
3272
3273         if (change || reg_change) {
3274                 if (reg_change) {
3275                         if (snd_soc_volsw_is_stereo(mc)) {
3276                                 update.has_second_set = true;
3277                                 update.reg2 = mc->rreg;
3278                                 update.mask2 = mask << mc->rshift;
3279                                 update.val2 = rval;
3280                         }
3281                         update.kcontrol = kcontrol;
3282                         update.reg = reg;
3283                         update.mask = mask << shift;
3284                         update.val = val;
3285                         card->update = &update;
3286                 }
3287                 change |= reg_change;
3288
3289                 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3290                                                   rconnect);
3291
3292                 card->update = NULL;
3293         }
3294
3295         mutex_unlock(&card->dapm_mutex);
3296
3297         if (ret > 0)
3298                 soc_dpcm_runtime_update(card);
3299
3300         return change;
3301 }
3302 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3303
3304 /**
3305  * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3306  * @kcontrol: mixer control
3307  * @ucontrol: control element information
3308  *
3309  * Callback to get the value of a dapm enumerated double mixer control.
3310  *
3311  * Returns 0 for success.
3312  */
3313 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3314         struct snd_ctl_elem_value *ucontrol)
3315 {
3316         struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3317         struct snd_soc_card *card = dapm->card;
3318         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3319         unsigned int reg_val, val;
3320
3321         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3322         if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3323                 int ret = soc_dapm_read(dapm, e->reg, &reg_val);
3324                 if (ret) {
3325                         mutex_unlock(&card->dapm_mutex);
3326                         return ret;
3327                 }
3328         } else {
3329                 reg_val = dapm_kcontrol_get_value(kcontrol);
3330         }
3331         mutex_unlock(&card->dapm_mutex);
3332
3333         val = (reg_val >> e->shift_l) & e->mask;
3334         ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3335         if (e->shift_l != e->shift_r) {
3336                 val = (reg_val >> e->shift_r) & e->mask;
3337                 val = snd_soc_enum_val_to_item(e, val);
3338                 ucontrol->value.enumerated.item[1] = val;
3339         }
3340
3341         return 0;
3342 }
3343 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3344
3345 /**
3346  * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3347  * @kcontrol: mixer control
3348  * @ucontrol: control element information
3349  *
3350  * Callback to set the value of a dapm enumerated double mixer control.
3351  *
3352  * Returns 0 for success.
3353  */
3354 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3355         struct snd_ctl_elem_value *ucontrol)
3356 {
3357         struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3358         struct snd_soc_card *card = dapm->card;
3359         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3360         unsigned int *item = ucontrol->value.enumerated.item;
3361         unsigned int val, change, reg_change = 0;
3362         unsigned int mask;
3363         struct snd_soc_dapm_update update = { NULL };
3364         int ret = 0;
3365
3366         if (item[0] >= e->items)
3367                 return -EINVAL;
3368
3369         val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3370         mask = e->mask << e->shift_l;
3371         if (e->shift_l != e->shift_r) {
3372                 if (item[1] > e->items)
3373                         return -EINVAL;
3374                 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3375                 mask |= e->mask << e->shift_r;
3376         }
3377
3378         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3379
3380         change = dapm_kcontrol_set_value(kcontrol, val);
3381
3382         if (e->reg != SND_SOC_NOPM)
3383                 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3384
3385         if (change || reg_change) {
3386                 if (reg_change) {
3387                         update.kcontrol = kcontrol;
3388                         update.reg = e->reg;
3389                         update.mask = mask;
3390                         update.val = val;
3391                         card->update = &update;
3392                 }
3393                 change |= reg_change;
3394
3395                 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3396
3397                 card->update = NULL;
3398         }
3399
3400         mutex_unlock(&card->dapm_mutex);
3401
3402         if (ret > 0)
3403                 soc_dpcm_runtime_update(card);
3404
3405         return change;
3406 }
3407 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3408
3409 /**
3410  * snd_soc_dapm_info_pin_switch - Info for a pin switch
3411  *
3412  * @kcontrol: mixer control
3413  * @uinfo: control element information
3414  *
3415  * Callback to provide information about a pin switch control.
3416  */
3417 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3418                                  struct snd_ctl_elem_info *uinfo)
3419 {
3420         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3421         uinfo->count = 1;
3422         uinfo->value.integer.min = 0;
3423         uinfo->value.integer.max = 1;
3424
3425         return 0;
3426 }
3427 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3428
3429 /**
3430  * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3431  *
3432  * @kcontrol: mixer control
3433  * @ucontrol: Value
3434  */
3435 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3436                                 struct snd_ctl_elem_value *ucontrol)
3437 {
3438         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3439         const char *pin = (const char *)kcontrol->private_value;
3440
3441         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3442
3443         ucontrol->value.integer.value[0] =
3444                 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3445
3446         mutex_unlock(&card->dapm_mutex);
3447
3448         return 0;
3449 }
3450 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3451
3452 /**
3453  * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3454  *
3455  * @kcontrol: mixer control
3456  * @ucontrol: Value
3457  */
3458 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3459                                 struct snd_ctl_elem_value *ucontrol)
3460 {
3461         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3462         const char *pin = (const char *)kcontrol->private_value;
3463         int ret;
3464
3465         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3466         ret = __snd_soc_dapm_set_pin(&card->dapm, pin,
3467                                      !!ucontrol->value.integer.value[0]);
3468         mutex_unlock(&card->dapm_mutex);
3469
3470         snd_soc_dapm_sync(&card->dapm);
3471         return ret;
3472 }
3473 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3474
3475 struct snd_soc_dapm_widget *
3476 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3477         const struct snd_soc_dapm_widget *widget)
3478 {
3479         struct snd_soc_dapm_widget *w;
3480
3481         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3482         w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3483         /* Do not nag about probe deferrals */
3484         if (IS_ERR(w)) {
3485                 int ret = PTR_ERR(w);
3486
3487                 if (ret != -EPROBE_DEFER)
3488                         dev_err(dapm->dev,
3489                                 "ASoC: Failed to create DAPM control %s (%d)\n",
3490                                 widget->name, ret);
3491                 goto out_unlock;
3492         }
3493         if (!w)
3494                 dev_err(dapm->dev,
3495                         "ASoC: Failed to create DAPM control %s\n",
3496                         widget->name);
3497
3498 out_unlock:
3499         mutex_unlock(&dapm->card->dapm_mutex);
3500         return w;
3501 }
3502 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3503
3504 struct snd_soc_dapm_widget *
3505 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3506                          const struct snd_soc_dapm_widget *widget)
3507 {
3508         enum snd_soc_dapm_direction dir;
3509         struct snd_soc_dapm_widget *w;
3510         const char *prefix;
3511         int ret;
3512
3513         if ((w = dapm_cnew_widget(widget)) == NULL)
3514                 return NULL;
3515
3516         switch (w->id) {
3517         case snd_soc_dapm_regulator_supply:
3518                 w->regulator = devm_regulator_get(dapm->dev, w->name);
3519                 if (IS_ERR(w->regulator)) {
3520                         ret = PTR_ERR(w->regulator);
3521                         if (ret == -EPROBE_DEFER)
3522                                 return ERR_PTR(ret);
3523                         dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3524                                 w->name, ret);
3525                         return NULL;
3526                 }
3527
3528                 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3529                         ret = regulator_allow_bypass(w->regulator, true);
3530                         if (ret != 0)
3531                                 dev_warn(w->dapm->dev,
3532                                          "ASoC: Failed to bypass %s: %d\n",
3533                                          w->name, ret);
3534                 }
3535                 break;
3536         case snd_soc_dapm_clock_supply:
3537 #ifdef CONFIG_CLKDEV_LOOKUP
3538                 w->clk = devm_clk_get(dapm->dev, w->name);
3539                 if (IS_ERR(w->clk)) {
3540                         ret = PTR_ERR(w->clk);
3541                         if (ret == -EPROBE_DEFER)
3542                                 return ERR_PTR(ret);
3543                         dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3544                                 w->name, ret);
3545                         return NULL;
3546                 }
3547 #else
3548                 return NULL;
3549 #endif
3550                 break;
3551         default:
3552                 break;
3553         }
3554
3555         prefix = soc_dapm_prefix(dapm);
3556         if (prefix)
3557                 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3558         else
3559                 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3560         if (w->name == NULL) {
3561                 kfree(w);
3562                 return NULL;
3563         }
3564
3565         switch (w->id) {
3566         case snd_soc_dapm_mic:
3567                 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3568                 w->power_check = dapm_generic_check_power;
3569                 break;
3570         case snd_soc_dapm_input:
3571                 if (!dapm->card->fully_routed)
3572                         w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3573                 w->power_check = dapm_generic_check_power;
3574                 break;
3575         case snd_soc_dapm_spk:
3576         case snd_soc_dapm_hp:
3577                 w->is_ep = SND_SOC_DAPM_EP_SINK;
3578                 w->power_check = dapm_generic_check_power;
3579                 break;
3580         case snd_soc_dapm_output:
3581                 if (!dapm->card->fully_routed)
3582                         w->is_ep = SND_SOC_DAPM_EP_SINK;
3583                 w->power_check = dapm_generic_check_power;
3584                 break;
3585         case snd_soc_dapm_vmid:
3586         case snd_soc_dapm_siggen:
3587                 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3588                 w->power_check = dapm_always_on_check_power;
3589                 break;
3590         case snd_soc_dapm_sink:
3591                 w->is_ep = SND_SOC_DAPM_EP_SINK;
3592                 w->power_check = dapm_always_on_check_power;
3593                 break;
3594
3595         case snd_soc_dapm_mux:
3596         case snd_soc_dapm_demux:
3597         case snd_soc_dapm_switch:
3598         case snd_soc_dapm_mixer:
3599         case snd_soc_dapm_mixer_named_ctl:
3600         case snd_soc_dapm_adc:
3601         case snd_soc_dapm_aif_out:
3602         case snd_soc_dapm_dac:
3603         case snd_soc_dapm_aif_in:
3604         case snd_soc_dapm_pga:
3605         case snd_soc_dapm_out_drv:
3606         case snd_soc_dapm_micbias:
3607         case snd_soc_dapm_line:
3608         case snd_soc_dapm_dai_link:
3609         case snd_soc_dapm_dai_out:
3610         case snd_soc_dapm_dai_in:
3611                 w->power_check = dapm_generic_check_power;
3612                 break;
3613         case snd_soc_dapm_supply:
3614         case snd_soc_dapm_regulator_supply:
3615         case snd_soc_dapm_clock_supply:
3616         case snd_soc_dapm_kcontrol:
3617                 w->is_supply = 1;
3618                 w->power_check = dapm_supply_check_power;
3619                 break;
3620         default:
3621                 w->power_check = dapm_always_on_check_power;
3622                 break;
3623         }
3624
3625         w->dapm = dapm;
3626         INIT_LIST_HEAD(&w->list);
3627         INIT_LIST_HEAD(&w->dirty);
3628         list_add_tail(&w->list, &dapm->card->widgets);
3629
3630         snd_soc_dapm_for_each_direction(dir) {
3631                 INIT_LIST_HEAD(&w->edges[dir]);
3632                 w->endpoints[dir] = -1;
3633         }
3634
3635         /* machine layer sets up unconnected pins and insertions */
3636         w->connected = 1;
3637         return w;
3638 }
3639
3640 /**
3641  * snd_soc_dapm_new_controls - create new dapm controls
3642  * @dapm: DAPM context
3643  * @widget: widget array
3644  * @num: number of widgets
3645  *
3646  * Creates new DAPM controls based upon the templates.
3647  *
3648  * Returns 0 for success else error.
3649  */
3650 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3651         const struct snd_soc_dapm_widget *widget,
3652         int num)
3653 {
3654         struct snd_soc_dapm_widget *w;
3655         int i;
3656         int ret = 0;
3657
3658         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3659         for (i = 0; i < num; i++) {
3660                 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3661                 if (IS_ERR(w)) {
3662                         ret = PTR_ERR(w);
3663                         /* Do not nag about probe deferrals */
3664                         if (ret == -EPROBE_DEFER)
3665                                 break;
3666                         dev_err(dapm->dev,
3667                                 "ASoC: Failed to create DAPM control %s (%d)\n",
3668                                 widget->name, ret);
3669                         break;
3670                 }
3671                 if (!w) {
3672                         dev_err(dapm->dev,
3673                                 "ASoC: Failed to create DAPM control %s\n",
3674                                 widget->name);
3675                         ret = -ENOMEM;
3676                         break;
3677                 }
3678                 widget++;
3679         }
3680         mutex_unlock(&dapm->card->dapm_mutex);
3681         return ret;
3682 }
3683 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3684
3685 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3686                                   struct snd_kcontrol *kcontrol, int event)
3687 {
3688         struct snd_soc_dapm_path *source_p, *sink_p;
3689         struct snd_soc_dai *source, *sink;
3690         const struct snd_soc_pcm_stream *config = w->params + w->params_select;
3691         struct snd_pcm_substream substream;
3692         struct snd_pcm_hw_params *params = NULL;
3693         struct snd_pcm_runtime *runtime = NULL;
3694         u64 fmt;
3695         int ret;
3696
3697         if (WARN_ON(!config) ||
3698             WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3699                     list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3700                 return -EINVAL;
3701
3702         /* We only support a single source and sink, pick the first */
3703         source_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_OUT],
3704                                     struct snd_soc_dapm_path,
3705                                     list_node[SND_SOC_DAPM_DIR_OUT]);
3706         sink_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_IN],
3707                                     struct snd_soc_dapm_path,
3708                                     list_node[SND_SOC_DAPM_DIR_IN]);
3709
3710         source = source_p->source->priv;
3711         sink = sink_p->sink->priv;
3712
3713         /* Be a little careful as we don't want to overflow the mask array */
3714         if (config->formats) {
3715                 fmt = ffs(config->formats) - 1;
3716         } else {
3717                 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3718                          config->formats);
3719                 fmt = 0;
3720         }
3721
3722         /* Currently very limited parameter selection */
3723         params = kzalloc(sizeof(*params), GFP_KERNEL);
3724         if (!params) {
3725                 ret = -ENOMEM;
3726                 goto out;
3727         }
3728         snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3729
3730         hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3731                 config->rate_min;
3732         hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3733                 config->rate_max;
3734
3735         hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3736                 = config->channels_min;
3737         hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3738                 = config->channels_max;
3739
3740         memset(&substream, 0, sizeof(substream));
3741
3742         /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3743         runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3744         if (!runtime) {
3745                 ret = -ENOMEM;
3746                 goto out;
3747         }
3748         substream.runtime = runtime;
3749
3750         switch (event) {
3751         case SND_SOC_DAPM_PRE_PMU:
3752                 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3753                 if (source->driver->ops && source->driver->ops->startup) {
3754                         ret = source->driver->ops->startup(&substream, source);
3755                         if (ret < 0) {
3756                                 dev_err(source->dev,
3757                                         "ASoC: startup() failed: %d\n", ret);
3758                                 goto out;
3759                         }
3760                         source->active++;
3761                 }
3762                 ret = soc_dai_hw_params(&substream, params, source);
3763                 if (ret < 0)
3764                         goto out;
3765
3766                 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3767                 if (sink->driver->ops && sink->driver->ops->startup) {
3768                         ret = sink->driver->ops->startup(&substream, sink);
3769                         if (ret < 0) {
3770                                 dev_err(sink->dev,
3771                                         "ASoC: startup() failed: %d\n", ret);
3772                                 goto out;
3773                         }
3774                         sink->active++;
3775                 }
3776                 ret = soc_dai_hw_params(&substream, params, sink);
3777                 if (ret < 0)
3778                         goto out;
3779                 break;
3780
3781         case SND_SOC_DAPM_POST_PMU:
3782                 ret = snd_soc_dai_digital_mute(sink, 0,
3783                                                SNDRV_PCM_STREAM_PLAYBACK);
3784                 if (ret != 0 && ret != -ENOTSUPP)
3785                         dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3786                 ret = 0;
3787                 break;
3788
3789         case SND_SOC_DAPM_PRE_PMD:
3790                 ret = snd_soc_dai_digital_mute(sink, 1,
3791                                                SNDRV_PCM_STREAM_PLAYBACK);
3792                 if (ret != 0 && ret != -ENOTSUPP)
3793                         dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3794                 ret = 0;
3795
3796                 source->active--;
3797                 if (source->driver->ops && source->driver->ops->shutdown) {
3798                         substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3799                         source->driver->ops->shutdown(&substream, source);
3800                 }
3801
3802                 sink->active--;
3803                 if (sink->driver->ops && sink->driver->ops->shutdown) {
3804                         substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3805                         sink->driver->ops->shutdown(&substream, sink);
3806                 }
3807                 break;
3808
3809         default:
3810                 WARN(1, "Unknown event %d\n", event);
3811                 ret = -EINVAL;
3812         }
3813
3814 out:
3815         kfree(runtime);
3816         kfree(params);
3817         return ret;
3818 }
3819
3820 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3821                           struct snd_ctl_elem_value *ucontrol)
3822 {
3823         struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3824
3825         ucontrol->value.enumerated.item[0] = w->params_select;
3826
3827         return 0;
3828 }
3829
3830 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3831                           struct snd_ctl_elem_value *ucontrol)
3832 {
3833         struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3834
3835         /* Can't change the config when widget is already powered */
3836         if (w->power)
3837                 return -EBUSY;
3838
3839         if (ucontrol->value.enumerated.item[0] == w->params_select)
3840                 return 0;
3841
3842         if (ucontrol->value.enumerated.item[0] >= w->num_params)
3843                 return -EINVAL;
3844
3845         w->params_select = ucontrol->value.enumerated.item[0];
3846
3847         return 1;
3848 }
3849
3850 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3851                          const struct snd_soc_pcm_stream *params,
3852                          unsigned int num_params,
3853                          struct snd_soc_dapm_widget *source,
3854                          struct snd_soc_dapm_widget *sink)
3855 {
3856         struct snd_soc_dapm_widget template;
3857         struct snd_soc_dapm_widget *w;
3858         char *link_name;
3859         int ret, count;
3860         unsigned long private_value;
3861         const char **w_param_text;
3862         struct soc_enum w_param_enum[] = {
3863                 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3864         };
3865         struct snd_kcontrol_new kcontrol_dai_link[] = {
3866                 SOC_ENUM_EXT(NULL, w_param_enum[0],
3867                              snd_soc_dapm_dai_link_get,
3868                              snd_soc_dapm_dai_link_put),
3869         };
3870         const struct snd_soc_pcm_stream *config = params;
3871
3872         w_param_text = devm_kcalloc(card->dev, num_params,
3873                                         sizeof(char *), GFP_KERNEL);
3874         if (!w_param_text)
3875                 return -ENOMEM;
3876
3877         link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3878                                    source->name, sink->name);
3879         if (!link_name) {
3880                 ret = -ENOMEM;
3881                 goto outfree_w_param;
3882         }
3883
3884         for (count = 0 ; count < num_params; count++) {
3885                 if (!config->stream_name) {
3886                         dev_warn(card->dapm.dev,
3887                                 "ASoC: anonymous config %d for dai link %s\n",
3888                                 count, link_name);
3889                         w_param_text[count] =
3890                                 devm_kasprintf(card->dev, GFP_KERNEL,
3891                                                "Anonymous Configuration %d",
3892                                                count);
3893                         if (!w_param_text[count]) {
3894                                 ret = -ENOMEM;
3895                                 goto outfree_link_name;
3896                         }
3897                 } else {
3898                         w_param_text[count] = devm_kmemdup(card->dev,
3899                                                 config->stream_name,
3900                                                 strlen(config->stream_name) + 1,
3901                                                 GFP_KERNEL);
3902                         if (!w_param_text[count]) {
3903                                 ret = -ENOMEM;
3904                                 goto outfree_link_name;
3905                         }
3906                 }
3907                 config++;
3908         }
3909         w_param_enum[0].items = num_params;
3910         w_param_enum[0].texts = w_param_text;
3911
3912         memset(&template, 0, sizeof(template));
3913         template.reg = SND_SOC_NOPM;
3914         template.id = snd_soc_dapm_dai_link;
3915         template.name = link_name;
3916         template.event = snd_soc_dai_link_event;
3917         template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3918                 SND_SOC_DAPM_PRE_PMD;
3919         template.num_kcontrols = 1;
3920         /* duplicate w_param_enum on heap so that memory persists */
3921         private_value =
3922                 (unsigned long) devm_kmemdup(card->dev,
3923                         (void *)(kcontrol_dai_link[0].private_value),
3924                         sizeof(struct soc_enum), GFP_KERNEL);
3925         if (!private_value) {
3926                 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3927                         link_name);
3928                 ret = -ENOMEM;
3929                 goto outfree_link_name;
3930         }
3931         kcontrol_dai_link[0].private_value = private_value;
3932         /* duplicate kcontrol_dai_link on heap so that memory persists */
3933         template.kcontrol_news =
3934                                 devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3935                                         sizeof(struct snd_kcontrol_new),
3936                                         GFP_KERNEL);
3937         if (!template.kcontrol_news) {
3938                 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3939                         link_name);
3940                 ret = -ENOMEM;
3941                 goto outfree_private_value;
3942         }
3943
3944         dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3945
3946         w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
3947         if (IS_ERR(w)) {
3948                 ret = PTR_ERR(w);
3949                 /* Do not nag about probe deferrals */
3950                 if (ret != -EPROBE_DEFER)
3951                         dev_err(card->dev,
3952                                 "ASoC: Failed to create %s widget (%d)\n",
3953                                 link_name, ret);
3954                 goto outfree_kcontrol_news;
3955         }
3956         if (!w) {
3957                 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3958                         link_name);
3959                 ret = -ENOMEM;
3960                 goto outfree_kcontrol_news;
3961         }
3962
3963         w->params = params;
3964         w->num_params = num_params;
3965
3966         ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3967         if (ret)
3968                 goto outfree_w;
3969         return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
3970
3971 outfree_w:
3972         devm_kfree(card->dev, w);
3973 outfree_kcontrol_news:
3974         devm_kfree(card->dev, (void *)template.kcontrol_news);
3975 outfree_private_value:
3976         devm_kfree(card->dev, (void *)private_value);
3977 outfree_link_name:
3978         devm_kfree(card->dev, link_name);
3979 outfree_w_param:
3980         for (count = 0 ; count < num_params; count++)
3981                 devm_kfree(card->dev, (void *)w_param_text[count]);
3982         devm_kfree(card->dev, w_param_text);
3983
3984         return ret;
3985 }
3986
3987 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3988                                  struct snd_soc_dai *dai)
3989 {
3990         struct snd_soc_dapm_widget template;
3991         struct snd_soc_dapm_widget *w;
3992
3993         WARN_ON(dapm->dev != dai->dev);
3994
3995         memset(&template, 0, sizeof(template));
3996         template.reg = SND_SOC_NOPM;
3997
3998         if (dai->driver->playback.stream_name) {
3999                 template.id = snd_soc_dapm_dai_in;
4000                 template.name = dai->driver->playback.stream_name;
4001                 template.sname = dai->driver->playback.stream_name;
4002
4003                 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4004                         template.name);
4005
4006                 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4007                 if (IS_ERR(w)) {
4008                         int ret = PTR_ERR(w);
4009
4010                         /* Do not nag about probe deferrals */
4011                         if (ret != -EPROBE_DEFER)
4012                                 dev_err(dapm->dev,
4013                                 "ASoC: Failed to create %s widget (%d)\n",
4014                                 dai->driver->playback.stream_name, ret);
4015                         return ret;
4016                 }
4017                 if (!w) {
4018                         dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
4019                                 dai->driver->playback.stream_name);
4020                         return -ENOMEM;
4021                 }
4022
4023                 w->priv = dai;
4024                 dai->playback_widget = w;
4025         }
4026
4027         if (dai->driver->capture.stream_name) {
4028                 template.id = snd_soc_dapm_dai_out;
4029                 template.name = dai->driver->capture.stream_name;
4030                 template.sname = dai->driver->capture.stream_name;
4031
4032                 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4033                         template.name);
4034
4035                 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4036                 if (IS_ERR(w)) {
4037                         int ret = PTR_ERR(w);
4038
4039                         /* Do not nag about probe deferrals */
4040                         if (ret != -EPROBE_DEFER)
4041                                 dev_err(dapm->dev,
4042                                 "ASoC: Failed to create %s widget (%d)\n",
4043                                 dai->driver->playback.stream_name, ret);
4044                         return ret;
4045                 }
4046                 if (!w) {
4047                         dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
4048                                 dai->driver->capture.stream_name);
4049                         return -ENOMEM;
4050                 }
4051
4052                 w->priv = dai;
4053                 dai->capture_widget = w;
4054         }
4055
4056         return 0;
4057 }
4058
4059 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4060 {
4061         struct snd_soc_dapm_widget *dai_w, *w;
4062         struct snd_soc_dapm_widget *src, *sink;
4063         struct snd_soc_dai *dai;
4064
4065         /* For each DAI widget... */
4066         list_for_each_entry(dai_w, &card->widgets, list) {
4067                 switch (dai_w->id) {
4068                 case snd_soc_dapm_dai_in:
4069                 case snd_soc_dapm_dai_out:
4070                         break;
4071                 default:
4072                         continue;
4073                 }
4074
4075                 /* let users know there is no DAI to link */
4076                 if (!dai_w->priv) {
4077                         dev_dbg(card->dev, "dai widget %s has no DAI\n",
4078                                 dai_w->name);
4079                         continue;
4080                 }
4081
4082                 dai = dai_w->priv;
4083
4084                 /* ...find all widgets with the same stream and link them */
4085                 list_for_each_entry(w, &card->widgets, list) {
4086                         if (w->dapm != dai_w->dapm)
4087                                 continue;
4088
4089                         switch (w->id) {
4090                         case snd_soc_dapm_dai_in:
4091                         case snd_soc_dapm_dai_out:
4092                                 continue;
4093                         default:
4094                                 break;
4095                         }
4096
4097                         if (!w->sname || !strstr(w->sname, dai_w->sname))
4098                                 continue;
4099
4100                         if (dai_w->id == snd_soc_dapm_dai_in) {
4101                                 src = dai_w;
4102                                 sink = w;
4103                         } else {
4104                                 src = w;
4105                                 sink = dai_w;
4106                         }
4107                         dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4108                         snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4109                 }
4110         }
4111
4112         return 0;
4113 }
4114
4115 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4116                                           struct snd_soc_pcm_runtime *rtd)
4117 {
4118         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4119         struct snd_soc_dapm_widget *sink, *source;
4120         int i;
4121
4122         for (i = 0; i < rtd->num_codecs; i++) {
4123                 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
4124
4125                 /* connect BE DAI playback if widgets are valid */
4126                 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
4127                         source = cpu_dai->playback_widget;
4128                         sink = codec_dai->playback_widget;
4129                         dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4130                                 cpu_dai->component->name, source->name,
4131                                 codec_dai->component->name, sink->name);
4132
4133                         snd_soc_dapm_add_path(&card->dapm, source, sink,
4134                                 NULL, NULL);
4135                 }
4136
4137                 /* connect BE DAI capture if widgets are valid */
4138                 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
4139                         source = codec_dai->capture_widget;
4140                         sink = cpu_dai->capture_widget;
4141                         dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4142                                 codec_dai->component->name, source->name,
4143                                 cpu_dai->component->name, sink->name);
4144
4145                         snd_soc_dapm_add_path(&card->dapm, source, sink,
4146                                 NULL, NULL);
4147                 }
4148         }
4149 }
4150
4151 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4152         int event)
4153 {
4154         struct snd_soc_dapm_widget *w;
4155         unsigned int ep;
4156
4157         if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4158                 w = dai->playback_widget;
4159         else
4160                 w = dai->capture_widget;
4161
4162         if (w) {
4163                 dapm_mark_dirty(w, "stream event");
4164
4165                 if (w->id == snd_soc_dapm_dai_in) {
4166                         ep = SND_SOC_DAPM_EP_SOURCE;
4167                         dapm_widget_invalidate_input_paths(w);
4168                 } else {
4169                         ep = SND_SOC_DAPM_EP_SINK;
4170                         dapm_widget_invalidate_output_paths(w);
4171                 }
4172
4173                 switch (event) {
4174                 case SND_SOC_DAPM_STREAM_START:
4175                         w->active = 1;
4176                         w->is_ep = ep;
4177                         break;
4178                 case SND_SOC_DAPM_STREAM_STOP:
4179                         w->active = 0;
4180                         w->is_ep = 0;
4181                         break;
4182                 case SND_SOC_DAPM_STREAM_SUSPEND:
4183                 case SND_SOC_DAPM_STREAM_RESUME:
4184                 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4185                 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4186                         break;
4187                 }
4188         }
4189 }
4190
4191 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4192 {
4193         struct snd_soc_pcm_runtime *rtd;
4194
4195         /* for each BE DAI link... */
4196         list_for_each_entry(rtd, &card->rtd_list, list)  {
4197                 /*
4198                  * dynamic FE links have no fixed DAI mapping.
4199                  * CODEC<->CODEC links have no direct connection.
4200                  */
4201                 if (rtd->dai_link->dynamic || rtd->dai_link->params)
4202                         continue;
4203
4204                 dapm_connect_dai_link_widgets(card, rtd);
4205         }
4206 }
4207
4208 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4209         int event)
4210 {
4211         int i;
4212
4213         soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
4214         for (i = 0; i < rtd->num_codecs; i++)
4215                 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
4216
4217         dapm_power_widgets(rtd->card, event);
4218 }
4219
4220 /**
4221  * snd_soc_dapm_stream_event - send a stream event to the dapm core
4222  * @rtd: PCM runtime data
4223  * @stream: stream name
4224  * @event: stream event
4225  *
4226  * Sends a stream event to the dapm core. The core then makes any
4227  * necessary widget power changes.
4228  *
4229  * Returns 0 for success else error.
4230  */
4231 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4232                               int event)
4233 {
4234         struct snd_soc_card *card = rtd->card;
4235
4236         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4237         soc_dapm_stream_event(rtd, stream, event);
4238         mutex_unlock(&card->dapm_mutex);
4239 }
4240
4241 /**
4242  * snd_soc_dapm_enable_pin_unlocked - enable pin.
4243  * @dapm: DAPM context
4244  * @pin: pin name
4245  *
4246  * Enables input/output pin and its parents or children widgets iff there is
4247  * a valid audio route and active audio stream.
4248  *
4249  * Requires external locking.
4250  *
4251  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4252  * do any widget power switching.
4253  */
4254 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4255                                    const char *pin)
4256 {
4257         return snd_soc_dapm_set_pin(dapm, pin, 1);
4258 }
4259 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4260
4261 /**
4262  * snd_soc_dapm_enable_pin - enable pin.
4263  * @dapm: DAPM context
4264  * @pin: pin name
4265  *
4266  * Enables input/output pin and its parents or children widgets iff there is
4267  * a valid audio route and active audio stream.
4268  *
4269  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4270  * do any widget power switching.
4271  */
4272 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4273 {
4274         int ret;
4275
4276         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4277
4278         ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4279
4280         mutex_unlock(&dapm->card->dapm_mutex);
4281
4282         return ret;
4283 }
4284 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4285
4286 /**
4287  * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4288  * @dapm: DAPM context
4289  * @pin: pin name
4290  *
4291  * Enables input/output pin regardless of any other state.  This is
4292  * intended for use with microphone bias supplies used in microphone
4293  * jack detection.
4294  *
4295  * Requires external locking.
4296  *
4297  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4298  * do any widget power switching.
4299  */
4300 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4301                                          const char *pin)
4302 {
4303         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4304
4305         if (!w) {
4306                 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4307                 return -EINVAL;
4308         }
4309
4310         dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4311         if (!w->connected) {
4312                 /*
4313                  * w->force does not affect the number of input or output paths,
4314                  * so we only have to recheck if w->connected is changed
4315                  */
4316                 dapm_widget_invalidate_input_paths(w);
4317                 dapm_widget_invalidate_output_paths(w);
4318                 w->connected = 1;
4319         }
4320         w->force = 1;
4321         dapm_mark_dirty(w, "force enable");
4322
4323         return 0;
4324 }
4325 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4326
4327 /**
4328  * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4329  * @dapm: DAPM context
4330  * @pin: pin name
4331  *
4332  * Enables input/output pin regardless of any other state.  This is
4333  * intended for use with microphone bias supplies used in microphone
4334  * jack detection.
4335  *
4336  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4337  * do any widget power switching.
4338  */
4339 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4340                                   const char *pin)
4341 {
4342         int ret;
4343
4344         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4345
4346         ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4347
4348         mutex_unlock(&dapm->card->dapm_mutex);
4349
4350         return ret;
4351 }
4352 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4353
4354 /**
4355  * snd_soc_dapm_disable_pin_unlocked - disable pin.
4356  * @dapm: DAPM context
4357  * @pin: pin name
4358  *
4359  * Disables input/output pin and its parents or children widgets.
4360  *
4361  * Requires external locking.
4362  *
4363  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4364  * do any widget power switching.
4365  */
4366 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4367                                     const char *pin)
4368 {
4369         return snd_soc_dapm_set_pin(dapm, pin, 0);
4370 }
4371 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4372
4373 /**
4374  * snd_soc_dapm_disable_pin - disable pin.
4375  * @dapm: DAPM context
4376  * @pin: pin name
4377  *
4378  * Disables input/output pin and its parents or children widgets.
4379  *
4380  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4381  * do any widget power switching.
4382  */
4383 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4384                              const char *pin)
4385 {
4386         int ret;
4387
4388         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4389
4390         ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4391
4392         mutex_unlock(&dapm->card->dapm_mutex);
4393
4394         return ret;
4395 }
4396 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4397
4398 /**
4399  * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4400  * @dapm: DAPM context
4401  * @pin: pin name
4402  *
4403  * Marks the specified pin as being not connected, disabling it along
4404  * any parent or child widgets.  At present this is identical to
4405  * snd_soc_dapm_disable_pin() but in future it will be extended to do
4406  * additional things such as disabling controls which only affect
4407  * paths through the pin.
4408  *
4409  * Requires external locking.
4410  *
4411  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4412  * do any widget power switching.
4413  */
4414 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4415                                const char *pin)
4416 {
4417         return snd_soc_dapm_set_pin(dapm, pin, 0);
4418 }
4419 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4420
4421 /**
4422  * snd_soc_dapm_nc_pin - permanently disable pin.
4423  * @dapm: DAPM context
4424  * @pin: pin name
4425  *
4426  * Marks the specified pin as being not connected, disabling it along
4427  * any parent or child widgets.  At present this is identical to
4428  * snd_soc_dapm_disable_pin() but in future it will be extended to do
4429  * additional things such as disabling controls which only affect
4430  * paths through the pin.
4431  *
4432  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4433  * do any widget power switching.
4434  */
4435 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4436 {
4437         int ret;
4438
4439         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4440
4441         ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4442
4443         mutex_unlock(&dapm->card->dapm_mutex);
4444
4445         return ret;
4446 }
4447 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4448
4449 /**
4450  * snd_soc_dapm_get_pin_status - get audio pin status
4451  * @dapm: DAPM context
4452  * @pin: audio signal pin endpoint (or start point)
4453  *
4454  * Get audio pin status - connected or disconnected.
4455  *
4456  * Returns 1 for connected otherwise 0.
4457  */
4458 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4459                                 const char *pin)
4460 {
4461         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4462
4463         if (w)
4464                 return w->connected;
4465
4466         return 0;
4467 }
4468 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4469
4470 /**
4471  * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4472  * @dapm: DAPM context
4473  * @pin: audio signal pin endpoint (or start point)
4474  *
4475  * Mark the given endpoint or pin as ignoring suspend.  When the
4476  * system is disabled a path between two endpoints flagged as ignoring
4477  * suspend will not be disabled.  The path must already be enabled via
4478  * normal means at suspend time, it will not be turned on if it was not
4479  * already enabled.
4480  */
4481 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4482                                 const char *pin)
4483 {
4484         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4485
4486         if (!w) {
4487                 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4488                 return -EINVAL;
4489         }
4490
4491         w->ignore_suspend = 1;
4492
4493         return 0;
4494 }
4495 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4496
4497 /**
4498  * snd_soc_dapm_free - free dapm resources
4499  * @dapm: DAPM context
4500  *
4501  * Free all dapm widgets and resources.
4502  */
4503 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4504 {
4505         dapm_debugfs_cleanup(dapm);
4506         dapm_free_widgets(dapm);
4507         list_del(&dapm->list);
4508 }
4509 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4510
4511 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4512 {
4513         struct snd_soc_card *card = dapm->card;
4514         struct snd_soc_dapm_widget *w;
4515         LIST_HEAD(down_list);
4516         int powerdown = 0;
4517
4518         mutex_lock(&card->dapm_mutex);
4519
4520         list_for_each_entry(w, &dapm->card->widgets, list) {
4521                 if (w->dapm != dapm)
4522                         continue;
4523                 if (w->power) {
4524                         dapm_seq_insert(w, &down_list, false);
4525                         w->new_power = 0;
4526                         powerdown = 1;
4527                 }
4528         }
4529
4530         /* If there were no widgets to power down we're already in
4531          * standby.
4532          */
4533         if (powerdown) {
4534                 if (dapm->bias_level == SND_SOC_BIAS_ON)
4535                         snd_soc_dapm_set_bias_level(dapm,
4536                                                     SND_SOC_BIAS_PREPARE);
4537                 dapm_seq_run(card, &down_list, 0, false);
4538                 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4539                         snd_soc_dapm_set_bias_level(dapm,
4540                                                     SND_SOC_BIAS_STANDBY);
4541         }
4542
4543         mutex_unlock(&card->dapm_mutex);
4544 }
4545
4546 /*
4547  * snd_soc_dapm_shutdown - callback for system shutdown
4548  */
4549 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4550 {
4551         struct snd_soc_dapm_context *dapm;
4552
4553         list_for_each_entry(dapm, &card->dapm_list, list) {
4554                 if (dapm != &card->dapm) {
4555                         soc_dapm_shutdown_dapm(dapm);
4556                         if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4557                                 snd_soc_dapm_set_bias_level(dapm,
4558                                                             SND_SOC_BIAS_OFF);
4559                 }
4560         }
4561
4562         soc_dapm_shutdown_dapm(&card->dapm);
4563         if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4564                 snd_soc_dapm_set_bias_level(&card->dapm,
4565                                             SND_SOC_BIAS_OFF);
4566 }
4567
4568 /* Module information */
4569 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4570 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4571 MODULE_LICENSE("GPL");