root/sound/soc/soc-dapm.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. dapm_assert_locked
  2. pop_wait
  3. __printf
  4. dapm_dirty_widget
  5. dapm_mark_dirty
  6. dapm_widget_invalidate_paths
  7. dapm_widget_invalidate_input_paths
  8. dapm_widget_invalidate_output_paths
  9. dapm_path_invalidate
  10. dapm_mark_endpoints_dirty
  11. dapm_cnew_widget
  12. dapm_kcontrol_data_alloc
  13. dapm_kcontrol_free
  14. dapm_kcontrol_get_wlist
  15. dapm_kcontrol_add_widget
  16. dapm_kcontrol_add_path
  17. dapm_kcontrol_is_powered
  18. dapm_kcontrol_get_path_list
  19. dapm_kcontrol_get_value
  20. dapm_kcontrol_set_value
  21. snd_soc_dapm_kcontrol_widget
  22. snd_soc_dapm_kcontrol_dapm
  23. dapm_reset
  24. soc_dapm_prefix
  25. soc_dapm_read
  26. soc_dapm_update_bits
  27. soc_dapm_test_bits
  28. soc_dapm_async_complete
  29. dapm_wcache_lookup
  30. dapm_wcache_update
  31. snd_soc_dapm_force_bias_level
  32. snd_soc_dapm_set_bias_level
  33. dapm_connect_mux
  34. dapm_set_mixer_path_status
  35. dapm_connect_mixer
  36. dapm_is_shared_kcontrol
  37. dapm_create_or_share_kcontrol
  38. dapm_new_mixer
  39. dapm_new_mux
  40. dapm_new_pga
  41. dapm_new_dai_link
  42. snd_soc_dapm_suspend_check
  43. dapm_widget_list_create
  44. invalidate_paths_ep
  45. is_connected_ep
  46. is_connected_output_ep
  47. is_connected_input_ep
  48. snd_soc_dapm_dai_get_connected_widgets
  49. dapm_regulator_event
  50. dapm_pinctrl_event
  51. dapm_clock_event
  52. dapm_widget_power_check
  53. dapm_generic_check_power
  54. dapm_supply_check_power
  55. dapm_always_on_check_power
  56. dapm_seq_compare
  57. dapm_seq_insert
  58. dapm_seq_check_event
  59. dapm_seq_run_coalesced
  60. dapm_seq_run
  61. dapm_widget_update
  62. dapm_pre_sequence_async
  63. dapm_post_sequence_async
  64. dapm_widget_set_peer_power
  65. dapm_widget_set_power
  66. dapm_power_one_widget
  67. dapm_idle_bias_off
  68. dapm_power_widgets
  69. dapm_widget_power_read_file
  70. dapm_bias_read_file
  71. snd_soc_dapm_debugfs_init
  72. dapm_debugfs_add_widget
  73. dapm_debugfs_cleanup
  74. snd_soc_dapm_debugfs_init
  75. dapm_debugfs_add_widget
  76. dapm_debugfs_cleanup
  77. soc_dapm_connect_path
  78. soc_dapm_mux_update_power
  79. snd_soc_dapm_mux_update_power
  80. soc_dapm_mixer_update_power
  81. snd_soc_dapm_mixer_update_power
  82. dapm_widget_show_component
  83. dapm_widget_show
  84. dapm_free_path
  85. snd_soc_dapm_free_widget
  86. snd_soc_dapm_reset_cache
  87. dapm_free_widgets
  88. dapm_find_widget
  89. snd_soc_dapm_set_pin
  90. snd_soc_dapm_sync_unlocked
  91. snd_soc_dapm_sync
  92. dapm_update_dai_chan
  93. dapm_update_dai_unlocked
  94. snd_soc_dapm_update_dai
  95. dapm_update_widget_flags
  96. snd_soc_dapm_check_dynamic_path
  97. snd_soc_dapm_add_path
  98. snd_soc_dapm_add_route
  99. snd_soc_dapm_del_route
  100. snd_soc_dapm_add_routes
  101. snd_soc_dapm_del_routes
  102. snd_soc_dapm_weak_route
  103. snd_soc_dapm_weak_routes
  104. snd_soc_dapm_new_widgets
  105. snd_soc_dapm_get_volsw
  106. snd_soc_dapm_put_volsw
  107. snd_soc_dapm_get_enum_double
  108. snd_soc_dapm_put_enum_double
  109. snd_soc_dapm_info_pin_switch
  110. snd_soc_dapm_get_pin_switch
  111. snd_soc_dapm_put_pin_switch
  112. snd_soc_dapm_new_control_unlocked
  113. snd_soc_dapm_new_control
  114. snd_soc_dapm_new_controls
  115. snd_soc_dai_link_event_pre_pmu
  116. snd_soc_dai_link_event
  117. snd_soc_dapm_dai_link_get
  118. snd_soc_dapm_dai_link_put
  119. snd_soc_dapm_free_kcontrol
  120. snd_soc_dapm_alloc_kcontrol
  121. snd_soc_dapm_new_dai
  122. snd_soc_dapm_new_dai_widgets
  123. snd_soc_dapm_link_dai_widgets
  124. dapm_connect_dai_link_widgets
  125. soc_dapm_dai_stream_event
  126. snd_soc_dapm_connect_dai_link_widgets
  127. soc_dapm_stream_event
  128. snd_soc_dapm_stream_event
  129. snd_soc_dapm_enable_pin_unlocked
  130. snd_soc_dapm_enable_pin
  131. snd_soc_dapm_force_enable_pin_unlocked
  132. snd_soc_dapm_force_enable_pin
  133. snd_soc_dapm_disable_pin_unlocked
  134. snd_soc_dapm_disable_pin
  135. snd_soc_dapm_nc_pin_unlocked
  136. snd_soc_dapm_nc_pin
  137. snd_soc_dapm_get_pin_status
  138. snd_soc_dapm_ignore_suspend
  139. snd_soc_dapm_free
  140. snd_soc_dapm_init
  141. soc_dapm_shutdown_dapm
  142. snd_soc_dapm_shutdown

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

/* [<][>][^][v][top][bottom][index][help] */