~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/soc/soc-dapm.c

Version: ~ [ linux-5.10-rc5 ] ~ [ linux-5.9.10 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.79 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.159 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.208 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.245 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.245 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp