Merge tag 'powerpc-5.11-3' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux/fpc-iii.git] / sound / soc / soc-dapm.c
blob9f0c86cbdcca2160d4e75c0db226f36b5b86a9aa
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.
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>
40 #include <trace/events/asoc.h>
42 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
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)
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)++)
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));
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);
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);
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,
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,
148 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
150 if (dapm->card && dapm->card->instantiated)
151 lockdep_assert_held(&dapm->card->dapm_mutex);
154 static void pop_wait(u32 pop_time)
156 if (pop_time)
157 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
160 __printf(3, 4)
161 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
163 va_list args;
164 char *buf;
166 if (!pop_time)
167 return;
169 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
170 if (buf == NULL)
171 return;
173 va_start(args, fmt);
174 vsnprintf(buf, PAGE_SIZE, fmt, args);
175 dev_info(dev, "%s", buf);
176 va_end(args);
178 kfree(buf);
181 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
183 return !list_empty(&w->dirty);
186 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
188 dapm_assert_locked(w->dapm);
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);
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.
204 static __always_inline void dapm_widget_invalidate_paths(
205 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
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);
212 dapm_assert_locked(w->dapm);
214 if (w->endpoints[dir] == -1)
215 return;
217 list_add_tail(&w->work_list, &list);
218 w->endpoints[dir] = -1;
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);
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
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.
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.
245 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
247 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
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
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.
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.
262 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
264 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
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
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.
275 * This function must be called when a path is added, removed or the connected
276 * state changes.
278 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
281 * Weak paths or supply paths do not influence the number of input or
282 * output paths of their neighbors.
284 if (p->weak || p->is_supply)
285 return;
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.
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);
299 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
301 struct snd_soc_dapm_widget *w;
303 mutex_lock(&card->dapm_mutex);
305 for_each_card_widgets(card, w) {
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);
315 mutex_unlock(&card->dapm_mutex);
317 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
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)
323 struct snd_soc_dapm_widget *w;
325 w = kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
326 if (!w)
327 return NULL;
330 * w->name is duplicated in caller, but w->sname isn't.
331 * Duplicate it here if defined
333 if (_widget->sname) {
334 w->sname = kstrdup_const(_widget->sname, GFP_KERNEL);
335 if (!w->sname) {
336 kfree(w);
337 return NULL;
340 return w;
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;
350 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
351 struct snd_kcontrol *kcontrol, const char *ctrl_name)
353 struct dapm_kcontrol_data *data;
354 struct soc_mixer_control *mc;
355 struct soc_enum *e;
356 const char *name;
357 int ret;
359 data = kzalloc(sizeof(*data), GFP_KERNEL);
360 if (!data)
361 return -ENOMEM;
363 INIT_LIST_HEAD(&data->paths);
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;
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);
376 if (mc->autodisable) {
377 struct snd_soc_dapm_widget template;
379 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
380 "Autodisable");
381 if (!name) {
382 ret = -ENOMEM;
383 goto err_data;
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;
398 data->value = template.on_val;
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;
409 break;
410 case snd_soc_dapm_demux:
411 case snd_soc_dapm_mux:
412 e = (struct soc_enum *)kcontrol->private_value;
414 if (e->autodisable) {
415 struct snd_soc_dapm_widget template;
417 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
418 "Autodisable");
419 if (!name) {
420 ret = -ENOMEM;
421 goto err_data;
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;
433 data->value = template.on_val;
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;
443 snd_soc_dapm_add_path(widget->dapm, data->widget,
444 widget, NULL, NULL);
446 break;
447 default:
448 break;
451 kcontrol->private_data = data;
453 return 0;
455 err_data:
456 kfree(data);
457 return ret;
460 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
462 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
464 list_del(&data->paths);
465 kfree(data->wlist);
466 kfree(data);
469 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
470 const struct snd_kcontrol *kcontrol)
472 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
474 return data->wlist;
477 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
478 struct snd_soc_dapm_widget *widget)
480 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
481 struct snd_soc_dapm_widget_list *new_wlist;
482 unsigned int n;
484 if (data->wlist)
485 n = data->wlist->num_widgets + 1;
486 else
487 n = 1;
489 new_wlist = krealloc(data->wlist,
490 struct_size(new_wlist, widgets, n),
491 GFP_KERNEL);
492 if (!new_wlist)
493 return -ENOMEM;
495 new_wlist->widgets[n - 1] = widget;
496 new_wlist->num_widgets = n;
498 data->wlist = new_wlist;
500 return 0;
503 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
504 struct snd_soc_dapm_path *path)
506 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
508 list_add_tail(&path->list_kcontrol, &data->paths);
511 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
513 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
515 if (!data->widget)
516 return true;
518 return data->widget->power;
521 static struct list_head *dapm_kcontrol_get_path_list(
522 const struct snd_kcontrol *kcontrol)
524 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
526 return &data->paths;
529 #define dapm_kcontrol_for_each_path(path, kcontrol) \
530 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
531 list_kcontrol)
533 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
535 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
537 return data->value;
539 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
541 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
542 unsigned int value)
544 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
546 if (data->value == value)
547 return false;
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;
566 data->value = value;
568 return true;
572 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
573 * kcontrol
574 * @kcontrol: The kcontrol
576 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
577 struct snd_kcontrol *kcontrol)
579 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
581 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
584 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
585 * kcontrol
586 * @kcontrol: The kcontrol
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.
591 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
592 struct snd_kcontrol *kcontrol)
594 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
596 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
598 static void dapm_reset(struct snd_soc_card *card)
600 struct snd_soc_dapm_widget *w;
602 lockdep_assert_held(&card->dapm_mutex);
604 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
606 for_each_card_widgets(card, w) {
607 w->new_power = w->power;
608 w->power_checked = false;
612 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
614 if (!dapm->component)
615 return NULL;
616 return dapm->component->name_prefix;
619 static unsigned int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg)
621 if (!dapm->component)
622 return -EIO;
623 return snd_soc_component_read(dapm->component, reg);
626 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
627 int reg, unsigned int mask, unsigned int value)
629 if (!dapm->component)
630 return -EIO;
631 return snd_soc_component_update_bits(dapm->component, reg,
632 mask, value);
635 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
636 int reg, unsigned int mask, unsigned int value)
638 if (!dapm->component)
639 return -EIO;
640 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
643 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
645 if (dapm->component)
646 snd_soc_component_async_complete(dapm->component);
649 static struct snd_soc_dapm_widget *
650 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
652 struct snd_soc_dapm_widget *w = wcache->widget;
653 struct list_head *wlist;
654 const int depth = 2;
655 int i = 0;
657 if (w) {
658 wlist = &w->dapm->card->widgets;
660 list_for_each_entry_from(w, wlist, list) {
661 if (!strcmp(name, w->name))
662 return w;
664 if (++i == depth)
665 break;
669 return NULL;
672 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
673 struct snd_soc_dapm_widget *w)
675 wcache->widget = w;
679 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
680 * @dapm: The DAPM context for which to set the level
681 * @level: The level to set
683 * Forces the DAPM bias level to a specific state. It will call the bias level
684 * callback of DAPM context with the specified level. This will even happen if
685 * the context is already at the same level. Furthermore it will not go through
686 * the normal bias level sequencing, meaning any intermediate states between the
687 * current and the target state will not be entered.
689 * Note that the change in bias level is only temporary and the next time
690 * snd_soc_dapm_sync() is called the state will be set to the level as
691 * determined by the DAPM core. The function is mainly intended to be used to
692 * used during probe or resume from suspend to power up the device so
693 * initialization can be done, before the DAPM core takes over.
695 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
696 enum snd_soc_bias_level level)
698 int ret = 0;
700 if (dapm->component)
701 ret = snd_soc_component_set_bias_level(dapm->component, level);
703 if (ret == 0)
704 dapm->bias_level = level;
706 return ret;
708 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
711 * snd_soc_dapm_set_bias_level - set the bias level for the system
712 * @dapm: DAPM context
713 * @level: level to configure
715 * Configure the bias (power) levels for the SoC audio device.
717 * Returns 0 for success else error.
719 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
720 enum snd_soc_bias_level level)
722 struct snd_soc_card *card = dapm->card;
723 int ret = 0;
725 trace_snd_soc_bias_level_start(card, level);
727 ret = snd_soc_card_set_bias_level(card, dapm, level);
728 if (ret != 0)
729 goto out;
731 if (!card || dapm != &card->dapm)
732 ret = snd_soc_dapm_force_bias_level(dapm, level);
734 if (ret != 0)
735 goto out;
737 ret = snd_soc_card_set_bias_level_post(card, dapm, level);
738 out:
739 trace_snd_soc_bias_level_done(card, level);
741 return ret;
744 /* connect mux widget to its interconnecting audio paths */
745 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
746 struct snd_soc_dapm_path *path, const char *control_name,
747 struct snd_soc_dapm_widget *w)
749 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
750 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
751 unsigned int val, item;
752 int i;
754 if (e->reg != SND_SOC_NOPM) {
755 val = soc_dapm_read(dapm, e->reg);
756 val = (val >> e->shift_l) & e->mask;
757 item = snd_soc_enum_val_to_item(e, val);
758 } else {
759 /* since a virtual mux has no backing registers to
760 * decide which path to connect, it will try to match
761 * with the first enumeration. This is to ensure
762 * that the default mux choice (the first) will be
763 * correctly powered up during initialization.
765 item = 0;
768 i = match_string(e->texts, e->items, control_name);
769 if (i < 0)
770 return -ENODEV;
772 path->name = e->texts[i];
773 path->connect = (i == item);
774 return 0;
778 /* set up initial codec paths */
779 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
780 int nth_path)
782 struct soc_mixer_control *mc = (struct soc_mixer_control *)
783 p->sink->kcontrol_news[i].private_value;
784 unsigned int reg = mc->reg;
785 unsigned int shift = mc->shift;
786 unsigned int max = mc->max;
787 unsigned int mask = (1 << fls(max)) - 1;
788 unsigned int invert = mc->invert;
789 unsigned int val;
791 if (reg != SND_SOC_NOPM) {
792 val = soc_dapm_read(p->sink->dapm, reg);
794 * The nth_path argument allows this function to know
795 * which path of a kcontrol it is setting the initial
796 * status for. Ideally this would support any number
797 * of paths and channels. But since kcontrols only come
798 * in mono and stereo variants, we are limited to 2
799 * channels.
801 * The following code assumes for stereo controls the
802 * first path is the left channel, and all remaining
803 * paths are the right channel.
805 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
806 if (reg != mc->rreg)
807 val = soc_dapm_read(p->sink->dapm, mc->rreg);
808 val = (val >> mc->rshift) & mask;
809 } else {
810 val = (val >> shift) & mask;
812 if (invert)
813 val = max - val;
814 p->connect = !!val;
815 } else {
816 /* since a virtual mixer has no backing registers to
817 * decide which path to connect, it will try to match
818 * with initial state. This is to ensure
819 * that the default mixer choice will be
820 * correctly powered up during initialization.
822 p->connect = invert;
826 /* connect mixer widget to its interconnecting audio paths */
827 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
828 struct snd_soc_dapm_path *path, const char *control_name)
830 int i, nth_path = 0;
832 /* search for mixer kcontrol */
833 for (i = 0; i < path->sink->num_kcontrols; i++) {
834 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
835 path->name = path->sink->kcontrol_news[i].name;
836 dapm_set_mixer_path_status(path, i, nth_path++);
837 return 0;
840 return -ENODEV;
843 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
844 struct snd_soc_dapm_widget *kcontrolw,
845 const struct snd_kcontrol_new *kcontrol_new,
846 struct snd_kcontrol **kcontrol)
848 struct snd_soc_dapm_widget *w;
849 int i;
851 *kcontrol = NULL;
853 for_each_card_widgets(dapm->card, w) {
854 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
855 continue;
856 for (i = 0; i < w->num_kcontrols; i++) {
857 if (&w->kcontrol_news[i] == kcontrol_new) {
858 if (w->kcontrols)
859 *kcontrol = w->kcontrols[i];
860 return 1;
865 return 0;
869 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
870 * create it. Either way, add the widget into the control's widget list
872 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
873 int kci)
875 struct snd_soc_dapm_context *dapm = w->dapm;
876 struct snd_card *card = dapm->card->snd_card;
877 const char *prefix;
878 size_t prefix_len;
879 int shared;
880 struct snd_kcontrol *kcontrol;
881 bool wname_in_long_name, kcname_in_long_name;
882 char *long_name = NULL;
883 const char *name;
884 int ret = 0;
886 prefix = soc_dapm_prefix(dapm);
887 if (prefix)
888 prefix_len = strlen(prefix) + 1;
889 else
890 prefix_len = 0;
892 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
893 &kcontrol);
895 if (!kcontrol) {
896 if (shared) {
897 wname_in_long_name = false;
898 kcname_in_long_name = true;
899 } else {
900 switch (w->id) {
901 case snd_soc_dapm_switch:
902 case snd_soc_dapm_mixer:
903 case snd_soc_dapm_pga:
904 case snd_soc_dapm_effect:
905 case snd_soc_dapm_out_drv:
906 wname_in_long_name = true;
907 kcname_in_long_name = true;
908 break;
909 case snd_soc_dapm_mixer_named_ctl:
910 wname_in_long_name = false;
911 kcname_in_long_name = true;
912 break;
913 case snd_soc_dapm_demux:
914 case snd_soc_dapm_mux:
915 wname_in_long_name = true;
916 kcname_in_long_name = false;
917 break;
918 default:
919 return -EINVAL;
923 if (wname_in_long_name && kcname_in_long_name) {
925 * The control will get a prefix from the control
926 * creation process but we're also using the same
927 * prefix for widgets so cut the prefix off the
928 * front of the widget name.
930 long_name = kasprintf(GFP_KERNEL, "%s %s",
931 w->name + prefix_len,
932 w->kcontrol_news[kci].name);
933 if (long_name == NULL)
934 return -ENOMEM;
936 name = long_name;
937 } else if (wname_in_long_name) {
938 long_name = NULL;
939 name = w->name + prefix_len;
940 } else {
941 long_name = NULL;
942 name = w->kcontrol_news[kci].name;
945 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
946 prefix);
947 if (!kcontrol) {
948 ret = -ENOMEM;
949 goto exit_free;
952 kcontrol->private_free = dapm_kcontrol_free;
954 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
955 if (ret) {
956 snd_ctl_free_one(kcontrol);
957 goto exit_free;
960 ret = snd_ctl_add(card, kcontrol);
961 if (ret < 0) {
962 dev_err(dapm->dev,
963 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
964 w->name, name, ret);
965 goto exit_free;
969 ret = dapm_kcontrol_add_widget(kcontrol, w);
970 if (ret == 0)
971 w->kcontrols[kci] = kcontrol;
973 exit_free:
974 kfree(long_name);
976 return ret;
979 /* create new dapm mixer control */
980 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
982 int i, ret;
983 struct snd_soc_dapm_path *path;
984 struct dapm_kcontrol_data *data;
986 /* add kcontrol */
987 for (i = 0; i < w->num_kcontrols; i++) {
988 /* match name */
989 snd_soc_dapm_widget_for_each_source_path(w, path) {
990 /* mixer/mux paths name must match control name */
991 if (path->name != (char *)w->kcontrol_news[i].name)
992 continue;
994 if (!w->kcontrols[i]) {
995 ret = dapm_create_or_share_kcontrol(w, i);
996 if (ret < 0)
997 return ret;
1000 dapm_kcontrol_add_path(w->kcontrols[i], path);
1002 data = snd_kcontrol_chip(w->kcontrols[i]);
1003 if (data->widget)
1004 snd_soc_dapm_add_path(data->widget->dapm,
1005 data->widget,
1006 path->source,
1007 NULL, NULL);
1011 return 0;
1014 /* create new dapm mux control */
1015 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
1017 struct snd_soc_dapm_context *dapm = w->dapm;
1018 enum snd_soc_dapm_direction dir;
1019 struct snd_soc_dapm_path *path;
1020 const char *type;
1021 int ret;
1023 switch (w->id) {
1024 case snd_soc_dapm_mux:
1025 dir = SND_SOC_DAPM_DIR_OUT;
1026 type = "mux";
1027 break;
1028 case snd_soc_dapm_demux:
1029 dir = SND_SOC_DAPM_DIR_IN;
1030 type = "demux";
1031 break;
1032 default:
1033 return -EINVAL;
1036 if (w->num_kcontrols != 1) {
1037 dev_err(dapm->dev,
1038 "ASoC: %s %s has incorrect number of controls\n", type,
1039 w->name);
1040 return -EINVAL;
1043 if (list_empty(&w->edges[dir])) {
1044 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1045 return -EINVAL;
1048 ret = dapm_create_or_share_kcontrol(w, 0);
1049 if (ret < 0)
1050 return ret;
1052 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1053 if (path->name)
1054 dapm_kcontrol_add_path(w->kcontrols[0], path);
1057 return 0;
1060 /* create new dapm volume control */
1061 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1063 int i, ret;
1065 for (i = 0; i < w->num_kcontrols; i++) {
1066 ret = dapm_create_or_share_kcontrol(w, i);
1067 if (ret < 0)
1068 return ret;
1071 return 0;
1074 /* create new dapm dai link control */
1075 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1077 int i, ret;
1078 struct snd_kcontrol *kcontrol;
1079 struct snd_soc_dapm_context *dapm = w->dapm;
1080 struct snd_card *card = dapm->card->snd_card;
1081 struct snd_soc_pcm_runtime *rtd = w->priv;
1083 /* create control for links with > 1 config */
1084 if (rtd->dai_link->num_params <= 1)
1085 return 0;
1087 /* add kcontrol */
1088 for (i = 0; i < w->num_kcontrols; i++) {
1089 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1090 w->name, NULL);
1091 ret = snd_ctl_add(card, kcontrol);
1092 if (ret < 0) {
1093 dev_err(dapm->dev,
1094 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1095 w->name, w->kcontrol_news[i].name, ret);
1096 return ret;
1098 kcontrol->private_data = w;
1099 w->kcontrols[i] = kcontrol;
1102 return 0;
1105 /* We implement power down on suspend by checking the power state of
1106 * the ALSA card - when we are suspending the ALSA state for the card
1107 * is set to D3.
1109 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1111 int level = snd_power_get_state(widget->dapm->card->snd_card);
1113 switch (level) {
1114 case SNDRV_CTL_POWER_D3hot:
1115 case SNDRV_CTL_POWER_D3cold:
1116 if (widget->ignore_suspend)
1117 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1118 widget->name);
1119 return widget->ignore_suspend;
1120 default:
1121 return 1;
1125 static void dapm_widget_list_free(struct snd_soc_dapm_widget_list **list)
1127 kfree(*list);
1130 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1131 struct list_head *widgets)
1133 struct snd_soc_dapm_widget *w;
1134 struct list_head *it;
1135 unsigned int size = 0;
1136 unsigned int i = 0;
1138 list_for_each(it, widgets)
1139 size++;
1141 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1142 if (*list == NULL)
1143 return -ENOMEM;
1145 list_for_each_entry(w, widgets, work_list)
1146 (*list)->widgets[i++] = w;
1148 (*list)->num_widgets = i;
1150 return 0;
1154 * Recursively reset the cached number of inputs or outputs for the specified
1155 * widget and all widgets that can be reached via incoming or outcoming paths
1156 * from the widget.
1158 static void invalidate_paths_ep(struct snd_soc_dapm_widget *widget,
1159 enum snd_soc_dapm_direction dir)
1161 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1162 struct snd_soc_dapm_path *path;
1164 widget->endpoints[dir] = -1;
1166 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1167 if (path->weak || path->is_supply)
1168 continue;
1170 if (path->walking)
1171 return;
1173 if (path->connect) {
1174 path->walking = 1;
1175 invalidate_paths_ep(path->node[dir], dir);
1176 path->walking = 0;
1182 * Common implementation for is_connected_output_ep() and
1183 * is_connected_input_ep(). The function is inlined since the combined size of
1184 * the two specialized functions is only marginally larger then the size of the
1185 * generic function and at the same time the fast path of the specialized
1186 * functions is significantly smaller than the generic function.
1188 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1189 struct list_head *list, enum snd_soc_dapm_direction dir,
1190 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1191 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1192 enum snd_soc_dapm_direction)),
1193 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1194 enum snd_soc_dapm_direction))
1196 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1197 struct snd_soc_dapm_path *path;
1198 int con = 0;
1200 if (widget->endpoints[dir] >= 0)
1201 return widget->endpoints[dir];
1203 DAPM_UPDATE_STAT(widget, path_checks);
1205 /* do we need to add this widget to the list ? */
1206 if (list)
1207 list_add_tail(&widget->work_list, list);
1209 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1210 list = NULL;
1211 custom_stop_condition = NULL;
1214 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1215 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1216 return widget->endpoints[dir];
1219 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1220 DAPM_UPDATE_STAT(widget, neighbour_checks);
1222 if (path->weak || path->is_supply)
1223 continue;
1225 if (path->walking)
1226 return 1;
1228 trace_snd_soc_dapm_path(widget, dir, path);
1230 if (path->connect) {
1231 path->walking = 1;
1232 con += fn(path->node[dir], list, custom_stop_condition);
1233 path->walking = 0;
1237 widget->endpoints[dir] = con;
1239 return con;
1243 * Recursively check for a completed path to an active or physically connected
1244 * output widget. Returns number of complete paths.
1246 * Optionally, can be supplied with a function acting as a stopping condition.
1247 * This function takes the dapm widget currently being examined and the walk
1248 * direction as an arguments, it should return true if widgets from that point
1249 * in the graph onwards should not be added to the widget list.
1251 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1252 struct list_head *list,
1253 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1254 enum snd_soc_dapm_direction))
1256 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1257 is_connected_output_ep, custom_stop_condition);
1261 * Recursively check for a completed path to an active or physically connected
1262 * input widget. Returns number of complete paths.
1264 * Optionally, can be supplied with a function acting as a stopping condition.
1265 * This function takes the dapm widget currently being examined and the walk
1266 * direction as an arguments, it should return true if the walk should be
1267 * stopped and false otherwise.
1269 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1270 struct list_head *list,
1271 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1272 enum snd_soc_dapm_direction))
1274 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1275 is_connected_input_ep, custom_stop_condition);
1279 * snd_soc_dapm_dai_get_connected_widgets - query audio path and it's widgets.
1280 * @dai: the soc DAI.
1281 * @stream: stream direction.
1282 * @list: list of active widgets for this stream.
1283 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1284 * walk based on custom logic.
1286 * Queries DAPM graph as to whether a valid audio stream path exists for
1287 * the initial stream specified by name. This takes into account
1288 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1290 * Optionally, can be supplied with a function acting as a stopping condition.
1291 * This function takes the dapm widget currently being examined and the walk
1292 * direction as an arguments, it should return true if the walk should be
1293 * stopped and false otherwise.
1295 * Returns the number of valid paths or negative error.
1297 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1298 struct snd_soc_dapm_widget_list **list,
1299 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1300 enum snd_soc_dapm_direction))
1302 struct snd_soc_card *card = dai->component->card;
1303 struct snd_soc_dapm_widget *w;
1304 LIST_HEAD(widgets);
1305 int paths;
1306 int ret;
1308 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1310 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1311 w = dai->playback_widget;
1312 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_OUT);
1313 paths = is_connected_output_ep(w, &widgets,
1314 custom_stop_condition);
1315 } else {
1316 w = dai->capture_widget;
1317 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_IN);
1318 paths = is_connected_input_ep(w, &widgets,
1319 custom_stop_condition);
1322 /* Drop starting point */
1323 list_del(widgets.next);
1325 ret = dapm_widget_list_create(list, &widgets);
1326 if (ret)
1327 paths = ret;
1329 trace_snd_soc_dapm_connected(paths, stream);
1330 mutex_unlock(&card->dapm_mutex);
1332 return paths;
1335 void snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list **list)
1337 dapm_widget_list_free(list);
1341 * Handler for regulator supply widget.
1343 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1344 struct snd_kcontrol *kcontrol, int event)
1346 int ret;
1348 soc_dapm_async_complete(w->dapm);
1350 if (SND_SOC_DAPM_EVENT_ON(event)) {
1351 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1352 ret = regulator_allow_bypass(w->regulator, false);
1353 if (ret != 0)
1354 dev_warn(w->dapm->dev,
1355 "ASoC: Failed to unbypass %s: %d\n",
1356 w->name, ret);
1359 return regulator_enable(w->regulator);
1360 } else {
1361 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1362 ret = regulator_allow_bypass(w->regulator, true);
1363 if (ret != 0)
1364 dev_warn(w->dapm->dev,
1365 "ASoC: Failed to bypass %s: %d\n",
1366 w->name, ret);
1369 return regulator_disable_deferred(w->regulator, w->shift);
1372 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1375 * Handler for pinctrl widget.
1377 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1378 struct snd_kcontrol *kcontrol, int event)
1380 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1381 struct pinctrl *p = w->pinctrl;
1382 struct pinctrl_state *s;
1384 if (!p || !priv)
1385 return -EIO;
1387 if (SND_SOC_DAPM_EVENT_ON(event))
1388 s = pinctrl_lookup_state(p, priv->active_state);
1389 else
1390 s = pinctrl_lookup_state(p, priv->sleep_state);
1392 if (IS_ERR(s))
1393 return PTR_ERR(s);
1395 return pinctrl_select_state(p, s);
1397 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1400 * Handler for clock supply widget.
1402 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1403 struct snd_kcontrol *kcontrol, int event)
1405 if (!w->clk)
1406 return -EIO;
1408 soc_dapm_async_complete(w->dapm);
1410 if (SND_SOC_DAPM_EVENT_ON(event)) {
1411 return clk_prepare_enable(w->clk);
1412 } else {
1413 clk_disable_unprepare(w->clk);
1414 return 0;
1417 return 0;
1419 EXPORT_SYMBOL_GPL(dapm_clock_event);
1421 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1423 if (w->power_checked)
1424 return w->new_power;
1426 if (w->force)
1427 w->new_power = 1;
1428 else
1429 w->new_power = w->power_check(w);
1431 w->power_checked = true;
1433 return w->new_power;
1436 /* Generic check to see if a widget should be powered. */
1437 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1439 int in, out;
1441 DAPM_UPDATE_STAT(w, power_checks);
1443 in = is_connected_input_ep(w, NULL, NULL);
1444 out = is_connected_output_ep(w, NULL, NULL);
1445 return out != 0 && in != 0;
1448 /* Check to see if a power supply is needed */
1449 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1451 struct snd_soc_dapm_path *path;
1453 DAPM_UPDATE_STAT(w, power_checks);
1455 /* Check if one of our outputs is connected */
1456 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1457 DAPM_UPDATE_STAT(w, neighbour_checks);
1459 if (path->weak)
1460 continue;
1462 if (path->connected &&
1463 !path->connected(path->source, path->sink))
1464 continue;
1466 if (dapm_widget_power_check(path->sink))
1467 return 1;
1470 return 0;
1473 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1475 return w->connected;
1478 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1479 struct snd_soc_dapm_widget *b,
1480 bool power_up)
1482 int *sort;
1484 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1485 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1487 if (power_up)
1488 sort = dapm_up_seq;
1489 else
1490 sort = dapm_down_seq;
1492 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1493 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1495 if (sort[a->id] != sort[b->id])
1496 return sort[a->id] - sort[b->id];
1497 if (a->subseq != b->subseq) {
1498 if (power_up)
1499 return a->subseq - b->subseq;
1500 else
1501 return b->subseq - a->subseq;
1503 if (a->reg != b->reg)
1504 return a->reg - b->reg;
1505 if (a->dapm != b->dapm)
1506 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1508 return 0;
1511 /* Insert a widget in order into a DAPM power sequence. */
1512 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1513 struct list_head *list,
1514 bool power_up)
1516 struct snd_soc_dapm_widget *w;
1518 list_for_each_entry(w, list, power_list)
1519 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1520 list_add_tail(&new_widget->power_list, &w->power_list);
1521 return;
1524 list_add_tail(&new_widget->power_list, list);
1527 static void dapm_seq_check_event(struct snd_soc_card *card,
1528 struct snd_soc_dapm_widget *w, int event)
1530 const char *ev_name;
1531 int power, ret;
1533 switch (event) {
1534 case SND_SOC_DAPM_PRE_PMU:
1535 ev_name = "PRE_PMU";
1536 power = 1;
1537 break;
1538 case SND_SOC_DAPM_POST_PMU:
1539 ev_name = "POST_PMU";
1540 power = 1;
1541 break;
1542 case SND_SOC_DAPM_PRE_PMD:
1543 ev_name = "PRE_PMD";
1544 power = 0;
1545 break;
1546 case SND_SOC_DAPM_POST_PMD:
1547 ev_name = "POST_PMD";
1548 power = 0;
1549 break;
1550 case SND_SOC_DAPM_WILL_PMU:
1551 ev_name = "WILL_PMU";
1552 power = 1;
1553 break;
1554 case SND_SOC_DAPM_WILL_PMD:
1555 ev_name = "WILL_PMD";
1556 power = 0;
1557 break;
1558 default:
1559 WARN(1, "Unknown event %d\n", event);
1560 return;
1563 if (w->new_power != power)
1564 return;
1566 if (w->event && (w->event_flags & event)) {
1567 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1568 w->name, ev_name);
1569 soc_dapm_async_complete(w->dapm);
1570 trace_snd_soc_dapm_widget_event_start(w, event);
1571 ret = w->event(w, NULL, event);
1572 trace_snd_soc_dapm_widget_event_done(w, event);
1573 if (ret < 0)
1574 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1575 ev_name, w->name, ret);
1579 /* Apply the coalesced changes from a DAPM sequence */
1580 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1581 struct list_head *pending)
1583 struct snd_soc_dapm_context *dapm;
1584 struct snd_soc_dapm_widget *w;
1585 int reg;
1586 unsigned int value = 0;
1587 unsigned int mask = 0;
1589 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1590 reg = w->reg;
1591 dapm = w->dapm;
1593 list_for_each_entry(w, pending, power_list) {
1594 WARN_ON(reg != w->reg || dapm != w->dapm);
1595 w->power = w->new_power;
1597 mask |= w->mask << w->shift;
1598 if (w->power)
1599 value |= w->on_val << w->shift;
1600 else
1601 value |= w->off_val << w->shift;
1603 pop_dbg(dapm->dev, card->pop_time,
1604 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1605 w->name, reg, value, mask);
1607 /* Check for events */
1608 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1609 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1612 if (reg >= 0) {
1613 /* Any widget will do, they should all be updating the
1614 * same register.
1617 pop_dbg(dapm->dev, card->pop_time,
1618 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1619 value, mask, reg, card->pop_time);
1620 pop_wait(card->pop_time);
1621 soc_dapm_update_bits(dapm, reg, mask, value);
1624 list_for_each_entry(w, pending, power_list) {
1625 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1626 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1630 /* Apply a DAPM power sequence.
1632 * We walk over a pre-sorted list of widgets to apply power to. In
1633 * order to minimise the number of writes to the device required
1634 * multiple widgets will be updated in a single write where possible.
1635 * Currently anything that requires more than a single write is not
1636 * handled.
1638 static void dapm_seq_run(struct snd_soc_card *card,
1639 struct list_head *list, int event, bool power_up)
1641 struct snd_soc_dapm_widget *w, *n;
1642 struct snd_soc_dapm_context *d;
1643 LIST_HEAD(pending);
1644 int cur_sort = -1;
1645 int cur_subseq = -1;
1646 int cur_reg = SND_SOC_NOPM;
1647 struct snd_soc_dapm_context *cur_dapm = NULL;
1648 int ret, i;
1649 int *sort;
1651 if (power_up)
1652 sort = dapm_up_seq;
1653 else
1654 sort = dapm_down_seq;
1656 list_for_each_entry_safe(w, n, list, power_list) {
1657 ret = 0;
1659 /* Do we need to apply any queued changes? */
1660 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1661 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1662 if (!list_empty(&pending))
1663 dapm_seq_run_coalesced(card, &pending);
1665 if (cur_dapm && cur_dapm->component) {
1666 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1667 if (sort[i] == cur_sort)
1668 snd_soc_component_seq_notifier(
1669 cur_dapm->component,
1670 i, cur_subseq);
1673 if (cur_dapm && w->dapm != cur_dapm)
1674 soc_dapm_async_complete(cur_dapm);
1676 INIT_LIST_HEAD(&pending);
1677 cur_sort = -1;
1678 cur_subseq = INT_MIN;
1679 cur_reg = SND_SOC_NOPM;
1680 cur_dapm = NULL;
1683 switch (w->id) {
1684 case snd_soc_dapm_pre:
1685 if (!w->event)
1686 list_for_each_entry_safe_continue(w, n, list,
1687 power_list);
1689 if (event == SND_SOC_DAPM_STREAM_START)
1690 ret = w->event(w,
1691 NULL, SND_SOC_DAPM_PRE_PMU);
1692 else if (event == SND_SOC_DAPM_STREAM_STOP)
1693 ret = w->event(w,
1694 NULL, SND_SOC_DAPM_PRE_PMD);
1695 break;
1697 case snd_soc_dapm_post:
1698 if (!w->event)
1699 list_for_each_entry_safe_continue(w, n, list,
1700 power_list);
1702 if (event == SND_SOC_DAPM_STREAM_START)
1703 ret = w->event(w,
1704 NULL, SND_SOC_DAPM_POST_PMU);
1705 else if (event == SND_SOC_DAPM_STREAM_STOP)
1706 ret = w->event(w,
1707 NULL, SND_SOC_DAPM_POST_PMD);
1708 break;
1710 default:
1711 /* Queue it up for application */
1712 cur_sort = sort[w->id];
1713 cur_subseq = w->subseq;
1714 cur_reg = w->reg;
1715 cur_dapm = w->dapm;
1716 list_move(&w->power_list, &pending);
1717 break;
1720 if (ret < 0)
1721 dev_err(w->dapm->dev,
1722 "ASoC: Failed to apply widget power: %d\n", ret);
1725 if (!list_empty(&pending))
1726 dapm_seq_run_coalesced(card, &pending);
1728 if (cur_dapm && cur_dapm->component) {
1729 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1730 if (sort[i] == cur_sort)
1731 snd_soc_component_seq_notifier(
1732 cur_dapm->component,
1733 i, cur_subseq);
1736 for_each_card_dapms(card, d)
1737 soc_dapm_async_complete(d);
1740 static void dapm_widget_update(struct snd_soc_card *card)
1742 struct snd_soc_dapm_update *update = card->update;
1743 struct snd_soc_dapm_widget_list *wlist;
1744 struct snd_soc_dapm_widget *w = NULL;
1745 unsigned int wi;
1746 int ret;
1748 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1749 return;
1751 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1753 for_each_dapm_widgets(wlist, wi, w) {
1754 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1755 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1756 if (ret != 0)
1757 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1758 w->name, ret);
1762 if (!w)
1763 return;
1765 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1766 update->val);
1767 if (ret < 0)
1768 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1769 w->name, ret);
1771 if (update->has_second_set) {
1772 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1773 update->mask2, update->val2);
1774 if (ret < 0)
1775 dev_err(w->dapm->dev,
1776 "ASoC: %s DAPM update failed: %d\n",
1777 w->name, ret);
1780 for_each_dapm_widgets(wlist, wi, w) {
1781 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1782 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1783 if (ret != 0)
1784 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1785 w->name, ret);
1790 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1791 * they're changing state.
1793 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1795 struct snd_soc_dapm_context *d = data;
1796 int ret;
1798 /* If we're off and we're not supposed to go into STANDBY */
1799 if (d->bias_level == SND_SOC_BIAS_OFF &&
1800 d->target_bias_level != SND_SOC_BIAS_OFF) {
1801 if (d->dev && cookie)
1802 pm_runtime_get_sync(d->dev);
1804 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1805 if (ret != 0)
1806 dev_err(d->dev,
1807 "ASoC: Failed to turn on bias: %d\n", ret);
1810 /* Prepare for a transition to ON or away from ON */
1811 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1812 d->bias_level != SND_SOC_BIAS_ON) ||
1813 (d->target_bias_level != SND_SOC_BIAS_ON &&
1814 d->bias_level == SND_SOC_BIAS_ON)) {
1815 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1816 if (ret != 0)
1817 dev_err(d->dev,
1818 "ASoC: Failed to prepare bias: %d\n", ret);
1822 /* Async callback run prior to DAPM sequences - brings to their final
1823 * state.
1825 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1827 struct snd_soc_dapm_context *d = data;
1828 int ret;
1830 /* If we just powered the last thing off drop to standby bias */
1831 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1832 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1833 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1834 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1835 if (ret != 0)
1836 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1837 ret);
1840 /* If we're in standby and can support bias off then do that */
1841 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1842 d->target_bias_level == SND_SOC_BIAS_OFF) {
1843 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1844 if (ret != 0)
1845 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1846 ret);
1848 if (d->dev && cookie)
1849 pm_runtime_put(d->dev);
1852 /* If we just powered up then move to active bias */
1853 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1854 d->target_bias_level == SND_SOC_BIAS_ON) {
1855 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1856 if (ret != 0)
1857 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1858 ret);
1862 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1863 bool power, bool connect)
1865 /* If a connection is being made or broken then that update
1866 * will have marked the peer dirty, otherwise the widgets are
1867 * not connected and this update has no impact. */
1868 if (!connect)
1869 return;
1871 /* If the peer is already in the state we're moving to then we
1872 * won't have an impact on it. */
1873 if (power != peer->power)
1874 dapm_mark_dirty(peer, "peer state change");
1877 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1878 struct list_head *up_list,
1879 struct list_head *down_list)
1881 struct snd_soc_dapm_path *path;
1883 if (w->power == power)
1884 return;
1886 trace_snd_soc_dapm_widget_power(w, power);
1888 /* If we changed our power state perhaps our neigbours changed
1889 * also.
1891 snd_soc_dapm_widget_for_each_source_path(w, path)
1892 dapm_widget_set_peer_power(path->source, power, path->connect);
1894 /* Supplies can't affect their outputs, only their inputs */
1895 if (!w->is_supply) {
1896 snd_soc_dapm_widget_for_each_sink_path(w, path)
1897 dapm_widget_set_peer_power(path->sink, power,
1898 path->connect);
1901 if (power)
1902 dapm_seq_insert(w, up_list, true);
1903 else
1904 dapm_seq_insert(w, down_list, false);
1907 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1908 struct list_head *up_list,
1909 struct list_head *down_list)
1911 int power;
1913 switch (w->id) {
1914 case snd_soc_dapm_pre:
1915 dapm_seq_insert(w, down_list, false);
1916 break;
1917 case snd_soc_dapm_post:
1918 dapm_seq_insert(w, up_list, true);
1919 break;
1921 default:
1922 power = dapm_widget_power_check(w);
1924 dapm_widget_set_power(w, power, up_list, down_list);
1925 break;
1929 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1931 if (dapm->idle_bias_off)
1932 return true;
1934 switch (snd_power_get_state(dapm->card->snd_card)) {
1935 case SNDRV_CTL_POWER_D3hot:
1936 case SNDRV_CTL_POWER_D3cold:
1937 return dapm->suspend_bias_off;
1938 default:
1939 break;
1942 return false;
1946 * Scan each dapm widget for complete audio path.
1947 * A complete path is a route that has valid endpoints i.e.:-
1949 * o DAC to output pin.
1950 * o Input pin to ADC.
1951 * o Input pin to Output pin (bypass, sidetone)
1952 * o DAC to ADC (loopback).
1954 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1956 struct snd_soc_dapm_widget *w;
1957 struct snd_soc_dapm_context *d;
1958 LIST_HEAD(up_list);
1959 LIST_HEAD(down_list);
1960 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1961 enum snd_soc_bias_level bias;
1962 int ret;
1964 lockdep_assert_held(&card->dapm_mutex);
1966 trace_snd_soc_dapm_start(card);
1968 for_each_card_dapms(card, d) {
1969 if (dapm_idle_bias_off(d))
1970 d->target_bias_level = SND_SOC_BIAS_OFF;
1971 else
1972 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1975 dapm_reset(card);
1977 /* Check which widgets we need to power and store them in
1978 * lists indicating if they should be powered up or down. We
1979 * only check widgets that have been flagged as dirty but note
1980 * that new widgets may be added to the dirty list while we
1981 * iterate.
1983 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1984 dapm_power_one_widget(w, &up_list, &down_list);
1987 for_each_card_widgets(card, w) {
1988 switch (w->id) {
1989 case snd_soc_dapm_pre:
1990 case snd_soc_dapm_post:
1991 /* These widgets always need to be powered */
1992 break;
1993 default:
1994 list_del_init(&w->dirty);
1995 break;
1998 if (w->new_power) {
1999 d = w->dapm;
2001 /* Supplies and micbiases only bring the
2002 * context up to STANDBY as unless something
2003 * else is active and passing audio they
2004 * generally don't require full power. Signal
2005 * generators are virtual pins and have no
2006 * power impact themselves.
2008 switch (w->id) {
2009 case snd_soc_dapm_siggen:
2010 case snd_soc_dapm_vmid:
2011 break;
2012 case snd_soc_dapm_supply:
2013 case snd_soc_dapm_regulator_supply:
2014 case snd_soc_dapm_pinctrl:
2015 case snd_soc_dapm_clock_supply:
2016 case snd_soc_dapm_micbias:
2017 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
2018 d->target_bias_level = SND_SOC_BIAS_STANDBY;
2019 break;
2020 default:
2021 d->target_bias_level = SND_SOC_BIAS_ON;
2022 break;
2028 /* Force all contexts in the card to the same bias state if
2029 * they're not ground referenced.
2031 bias = SND_SOC_BIAS_OFF;
2032 for_each_card_dapms(card, d)
2033 if (d->target_bias_level > bias)
2034 bias = d->target_bias_level;
2035 for_each_card_dapms(card, d)
2036 if (!dapm_idle_bias_off(d))
2037 d->target_bias_level = bias;
2039 trace_snd_soc_dapm_walk_done(card);
2041 /* Run card bias changes at first */
2042 dapm_pre_sequence_async(&card->dapm, 0);
2043 /* Run other bias changes in parallel */
2044 for_each_card_dapms(card, d) {
2045 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2046 async_schedule_domain(dapm_pre_sequence_async, d,
2047 &async_domain);
2049 async_synchronize_full_domain(&async_domain);
2051 list_for_each_entry(w, &down_list, power_list) {
2052 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
2055 list_for_each_entry(w, &up_list, power_list) {
2056 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
2059 /* Power down widgets first; try to avoid amplifying pops. */
2060 dapm_seq_run(card, &down_list, event, false);
2062 dapm_widget_update(card);
2064 /* Now power up. */
2065 dapm_seq_run(card, &up_list, event, true);
2067 /* Run all the bias changes in parallel */
2068 for_each_card_dapms(card, d) {
2069 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2070 async_schedule_domain(dapm_post_sequence_async, d,
2071 &async_domain);
2073 async_synchronize_full_domain(&async_domain);
2074 /* Run card bias changes at last */
2075 dapm_post_sequence_async(&card->dapm, 0);
2077 /* do we need to notify any clients that DAPM event is complete */
2078 for_each_card_dapms(card, d) {
2079 if (!d->component)
2080 continue;
2082 ret = snd_soc_component_stream_event(d->component, event);
2083 if (ret < 0)
2084 return ret;
2087 pop_dbg(card->dev, card->pop_time,
2088 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2089 pop_wait(card->pop_time);
2091 trace_snd_soc_dapm_done(card);
2093 return 0;
2096 #ifdef CONFIG_DEBUG_FS
2097 static ssize_t dapm_widget_power_read_file(struct file *file,
2098 char __user *user_buf,
2099 size_t count, loff_t *ppos)
2101 struct snd_soc_dapm_widget *w = file->private_data;
2102 struct snd_soc_card *card = w->dapm->card;
2103 enum snd_soc_dapm_direction dir, rdir;
2104 char *buf;
2105 int in, out;
2106 ssize_t ret;
2107 struct snd_soc_dapm_path *p = NULL;
2109 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2110 if (!buf)
2111 return -ENOMEM;
2113 mutex_lock(&card->dapm_mutex);
2115 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2116 if (w->is_supply) {
2117 in = 0;
2118 out = 0;
2119 } else {
2120 in = is_connected_input_ep(w, NULL, NULL);
2121 out = is_connected_output_ep(w, NULL, NULL);
2124 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2125 w->name, w->power ? "On" : "Off",
2126 w->force ? " (forced)" : "", in, out);
2128 if (w->reg >= 0)
2129 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2130 " - R%d(0x%x) mask 0x%x",
2131 w->reg, w->reg, w->mask << w->shift);
2133 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2135 if (w->sname)
2136 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2137 w->sname,
2138 w->active ? "active" : "inactive");
2140 snd_soc_dapm_for_each_direction(dir) {
2141 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2142 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2143 if (p->connected && !p->connected(p->source, p->sink))
2144 continue;
2146 if (!p->connect)
2147 continue;
2149 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2150 " %s \"%s\" \"%s\"\n",
2151 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2152 p->name ? p->name : "static",
2153 p->node[rdir]->name);
2157 mutex_unlock(&card->dapm_mutex);
2159 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2161 kfree(buf);
2162 return ret;
2165 static const struct file_operations dapm_widget_power_fops = {
2166 .open = simple_open,
2167 .read = dapm_widget_power_read_file,
2168 .llseek = default_llseek,
2171 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2172 size_t count, loff_t *ppos)
2174 struct snd_soc_dapm_context *dapm = file->private_data;
2175 char *level;
2177 switch (dapm->bias_level) {
2178 case SND_SOC_BIAS_ON:
2179 level = "On\n";
2180 break;
2181 case SND_SOC_BIAS_PREPARE:
2182 level = "Prepare\n";
2183 break;
2184 case SND_SOC_BIAS_STANDBY:
2185 level = "Standby\n";
2186 break;
2187 case SND_SOC_BIAS_OFF:
2188 level = "Off\n";
2189 break;
2190 default:
2191 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2192 level = "Unknown\n";
2193 break;
2196 return simple_read_from_buffer(user_buf, count, ppos, level,
2197 strlen(level));
2200 static const struct file_operations dapm_bias_fops = {
2201 .open = simple_open,
2202 .read = dapm_bias_read_file,
2203 .llseek = default_llseek,
2206 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2207 struct dentry *parent)
2209 if (!parent || IS_ERR(parent))
2210 return;
2212 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2214 debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
2215 &dapm_bias_fops);
2218 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2220 struct snd_soc_dapm_context *dapm = w->dapm;
2222 if (!dapm->debugfs_dapm || !w->name)
2223 return;
2225 debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
2226 &dapm_widget_power_fops);
2229 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2231 debugfs_remove_recursive(dapm->debugfs_dapm);
2232 dapm->debugfs_dapm = NULL;
2235 #else
2236 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2237 struct dentry *parent)
2241 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2245 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2249 #endif
2252 * soc_dapm_connect_path() - Connects or disconnects a path
2253 * @path: The path to update
2254 * @connect: The new connect state of the path. True if the path is connected,
2255 * false if it is disconnected.
2256 * @reason: The reason why the path changed (for debugging only)
2258 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2259 bool connect, const char *reason)
2261 if (path->connect == connect)
2262 return;
2264 path->connect = connect;
2265 dapm_mark_dirty(path->source, reason);
2266 dapm_mark_dirty(path->sink, reason);
2267 dapm_path_invalidate(path);
2270 /* test and update the power status of a mux widget */
2271 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2272 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2274 struct snd_soc_dapm_path *path;
2275 int found = 0;
2276 bool connect;
2278 lockdep_assert_held(&card->dapm_mutex);
2280 /* find dapm widget path assoc with kcontrol */
2281 dapm_kcontrol_for_each_path(path, kcontrol) {
2282 found = 1;
2283 /* we now need to match the string in the enum to the path */
2284 if (e && !(strcmp(path->name, e->texts[mux])))
2285 connect = true;
2286 else
2287 connect = false;
2289 soc_dapm_connect_path(path, connect, "mux update");
2292 if (found)
2293 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2295 return found;
2298 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2299 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2300 struct snd_soc_dapm_update *update)
2302 struct snd_soc_card *card = dapm->card;
2303 int ret;
2305 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2306 card->update = update;
2307 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2308 card->update = NULL;
2309 mutex_unlock(&card->dapm_mutex);
2310 if (ret > 0)
2311 snd_soc_dpcm_runtime_update(card);
2312 return ret;
2314 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2316 /* test and update the power status of a mixer or switch widget */
2317 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2318 struct snd_kcontrol *kcontrol,
2319 int connect, int rconnect)
2321 struct snd_soc_dapm_path *path;
2322 int found = 0;
2324 lockdep_assert_held(&card->dapm_mutex);
2326 /* find dapm widget path assoc with kcontrol */
2327 dapm_kcontrol_for_each_path(path, kcontrol) {
2329 * Ideally this function should support any number of
2330 * paths and channels. But since kcontrols only come
2331 * in mono and stereo variants, we are limited to 2
2332 * channels.
2334 * The following code assumes for stereo controls the
2335 * first path (when 'found == 0') is the left channel,
2336 * and all remaining paths (when 'found == 1') are the
2337 * right channel.
2339 * A stereo control is signified by a valid 'rconnect'
2340 * value, either 0 for unconnected, or >= 0 for connected.
2341 * This is chosen instead of using snd_soc_volsw_is_stereo,
2342 * so that the behavior of snd_soc_dapm_mixer_update_power
2343 * doesn't change even when the kcontrol passed in is
2344 * stereo.
2346 * It passes 'connect' as the path connect status for
2347 * the left channel, and 'rconnect' for the right
2348 * channel.
2350 if (found && rconnect >= 0)
2351 soc_dapm_connect_path(path, rconnect, "mixer update");
2352 else
2353 soc_dapm_connect_path(path, connect, "mixer update");
2354 found = 1;
2357 if (found)
2358 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2360 return found;
2363 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2364 struct snd_kcontrol *kcontrol, int connect,
2365 struct snd_soc_dapm_update *update)
2367 struct snd_soc_card *card = dapm->card;
2368 int ret;
2370 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2371 card->update = update;
2372 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2373 card->update = NULL;
2374 mutex_unlock(&card->dapm_mutex);
2375 if (ret > 0)
2376 snd_soc_dpcm_runtime_update(card);
2377 return ret;
2379 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2381 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2382 char *buf)
2384 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2385 struct snd_soc_dapm_widget *w;
2386 int count = 0;
2387 char *state = "not set";
2389 /* card won't be set for the dummy component, as a spot fix
2390 * we're checking for that case specifically here but in future
2391 * we will ensure that the dummy component looks like others.
2393 if (!cmpnt->card)
2394 return 0;
2396 for_each_card_widgets(cmpnt->card, w) {
2397 if (w->dapm != dapm)
2398 continue;
2400 /* only display widgets that burn power */
2401 switch (w->id) {
2402 case snd_soc_dapm_hp:
2403 case snd_soc_dapm_mic:
2404 case snd_soc_dapm_spk:
2405 case snd_soc_dapm_line:
2406 case snd_soc_dapm_micbias:
2407 case snd_soc_dapm_dac:
2408 case snd_soc_dapm_adc:
2409 case snd_soc_dapm_pga:
2410 case snd_soc_dapm_effect:
2411 case snd_soc_dapm_out_drv:
2412 case snd_soc_dapm_mixer:
2413 case snd_soc_dapm_mixer_named_ctl:
2414 case snd_soc_dapm_supply:
2415 case snd_soc_dapm_regulator_supply:
2416 case snd_soc_dapm_pinctrl:
2417 case snd_soc_dapm_clock_supply:
2418 if (w->name)
2419 count += sprintf(buf + count, "%s: %s\n",
2420 w->name, w->power ? "On":"Off");
2421 break;
2422 default:
2423 break;
2427 switch (snd_soc_dapm_get_bias_level(dapm)) {
2428 case SND_SOC_BIAS_ON:
2429 state = "On";
2430 break;
2431 case SND_SOC_BIAS_PREPARE:
2432 state = "Prepare";
2433 break;
2434 case SND_SOC_BIAS_STANDBY:
2435 state = "Standby";
2436 break;
2437 case SND_SOC_BIAS_OFF:
2438 state = "Off";
2439 break;
2441 count += sprintf(buf + count, "PM State: %s\n", state);
2443 return count;
2446 /* show dapm widget status in sys fs */
2447 static ssize_t dapm_widget_show(struct device *dev,
2448 struct device_attribute *attr, char *buf)
2450 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2451 struct snd_soc_dai *codec_dai;
2452 int i, count = 0;
2454 mutex_lock(&rtd->card->dapm_mutex);
2456 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2457 struct snd_soc_component *cmpnt = codec_dai->component;
2459 count += dapm_widget_show_component(cmpnt, buf + count);
2462 mutex_unlock(&rtd->card->dapm_mutex);
2464 return count;
2467 static DEVICE_ATTR_RO(dapm_widget);
2469 struct attribute *soc_dapm_dev_attrs[] = {
2470 &dev_attr_dapm_widget.attr,
2471 NULL
2474 static void dapm_free_path(struct snd_soc_dapm_path *path)
2476 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2477 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2478 list_del(&path->list_kcontrol);
2479 list_del(&path->list);
2480 kfree(path);
2483 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2485 struct snd_soc_dapm_path *p, *next_p;
2486 enum snd_soc_dapm_direction dir;
2488 list_del(&w->list);
2490 * remove source and sink paths associated to this widget.
2491 * While removing the path, remove reference to it from both
2492 * source and sink widgets so that path is removed only once.
2494 snd_soc_dapm_for_each_direction(dir) {
2495 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2496 dapm_free_path(p);
2499 kfree(w->kcontrols);
2500 kfree_const(w->name);
2501 kfree_const(w->sname);
2502 kfree(w);
2505 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2507 dapm->path_sink_cache.widget = NULL;
2508 dapm->path_source_cache.widget = NULL;
2511 /* free all dapm widgets and resources */
2512 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2514 struct snd_soc_dapm_widget *w, *next_w;
2516 for_each_card_widgets_safe(dapm->card, w, next_w) {
2517 if (w->dapm != dapm)
2518 continue;
2519 snd_soc_dapm_free_widget(w);
2521 snd_soc_dapm_reset_cache(dapm);
2524 static struct snd_soc_dapm_widget *dapm_find_widget(
2525 struct snd_soc_dapm_context *dapm, const char *pin,
2526 bool search_other_contexts)
2528 struct snd_soc_dapm_widget *w;
2529 struct snd_soc_dapm_widget *fallback = NULL;
2531 for_each_card_widgets(dapm->card, w) {
2532 if (!strcmp(w->name, pin)) {
2533 if (w->dapm == dapm)
2534 return w;
2535 else
2536 fallback = w;
2540 if (search_other_contexts)
2541 return fallback;
2543 return NULL;
2546 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2547 const char *pin, int status)
2549 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2551 dapm_assert_locked(dapm);
2553 if (!w) {
2554 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2555 return -EINVAL;
2558 if (w->connected != status) {
2559 dapm_mark_dirty(w, "pin configuration");
2560 dapm_widget_invalidate_input_paths(w);
2561 dapm_widget_invalidate_output_paths(w);
2564 w->connected = status;
2565 if (status == 0)
2566 w->force = 0;
2568 return 0;
2572 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2573 * @dapm: DAPM context
2575 * Walks all dapm audio paths and powers widgets according to their
2576 * stream or path usage.
2578 * Requires external locking.
2580 * Returns 0 for success.
2582 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2585 * Suppress early reports (eg, jacks syncing their state) to avoid
2586 * silly DAPM runs during card startup.
2588 if (!dapm->card || !dapm->card->instantiated)
2589 return 0;
2591 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2593 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2596 * snd_soc_dapm_sync - scan and power dapm paths
2597 * @dapm: DAPM context
2599 * Walks all dapm audio paths and powers widgets according to their
2600 * stream or path usage.
2602 * Returns 0 for success.
2604 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2606 int ret;
2608 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2609 ret = snd_soc_dapm_sync_unlocked(dapm);
2610 mutex_unlock(&dapm->card->dapm_mutex);
2611 return ret;
2613 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2615 static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2616 struct snd_soc_dapm_widget *w,
2617 int channels)
2619 switch (w->id) {
2620 case snd_soc_dapm_aif_out:
2621 case snd_soc_dapm_aif_in:
2622 break;
2623 default:
2624 return 0;
2627 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2628 w->channel < channels ? "Connecting" : "Disconnecting",
2629 p->source->name, p->sink->name);
2631 if (w->channel < channels)
2632 soc_dapm_connect_path(p, true, "dai update");
2633 else
2634 soc_dapm_connect_path(p, false, "dai update");
2636 return 0;
2639 static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2640 struct snd_pcm_hw_params *params,
2641 struct snd_soc_dai *dai)
2643 int dir = substream->stream;
2644 int channels = params_channels(params);
2645 struct snd_soc_dapm_path *p;
2646 struct snd_soc_dapm_widget *w;
2647 int ret;
2649 w = snd_soc_dai_get_widget(dai, dir);
2651 if (!w)
2652 return 0;
2654 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2655 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2657 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2658 ret = dapm_update_dai_chan(p, p->sink, channels);
2659 if (ret < 0)
2660 return ret;
2663 snd_soc_dapm_widget_for_each_source_path(w, p) {
2664 ret = dapm_update_dai_chan(p, p->source, channels);
2665 if (ret < 0)
2666 return ret;
2669 return 0;
2672 int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2673 struct snd_pcm_hw_params *params,
2674 struct snd_soc_dai *dai)
2676 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
2677 int ret;
2679 mutex_lock_nested(&rtd->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2680 ret = dapm_update_dai_unlocked(substream, params, dai);
2681 mutex_unlock(&rtd->card->dapm_mutex);
2683 return ret;
2685 EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2688 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2689 * @w: The widget for which to update the flags
2691 * Some widgets have a dynamic category which depends on which neighbors they
2692 * are connected to. This function update the category for these widgets.
2694 * This function must be called whenever a path is added or removed to a widget.
2696 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2698 enum snd_soc_dapm_direction dir;
2699 struct snd_soc_dapm_path *p;
2700 unsigned int ep;
2702 switch (w->id) {
2703 case snd_soc_dapm_input:
2704 /* On a fully routed card an input is never a source */
2705 if (w->dapm->card->fully_routed)
2706 return;
2707 ep = SND_SOC_DAPM_EP_SOURCE;
2708 snd_soc_dapm_widget_for_each_source_path(w, p) {
2709 if (p->source->id == snd_soc_dapm_micbias ||
2710 p->source->id == snd_soc_dapm_mic ||
2711 p->source->id == snd_soc_dapm_line ||
2712 p->source->id == snd_soc_dapm_output) {
2713 ep = 0;
2714 break;
2717 break;
2718 case snd_soc_dapm_output:
2719 /* On a fully routed card a output is never a sink */
2720 if (w->dapm->card->fully_routed)
2721 return;
2722 ep = SND_SOC_DAPM_EP_SINK;
2723 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2724 if (p->sink->id == snd_soc_dapm_spk ||
2725 p->sink->id == snd_soc_dapm_hp ||
2726 p->sink->id == snd_soc_dapm_line ||
2727 p->sink->id == snd_soc_dapm_input) {
2728 ep = 0;
2729 break;
2732 break;
2733 case snd_soc_dapm_line:
2734 ep = 0;
2735 snd_soc_dapm_for_each_direction(dir) {
2736 if (!list_empty(&w->edges[dir]))
2737 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2739 break;
2740 default:
2741 return;
2744 w->is_ep = ep;
2747 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2748 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2749 const char *control)
2751 bool dynamic_source = false;
2752 bool dynamic_sink = false;
2754 if (!control)
2755 return 0;
2757 switch (source->id) {
2758 case snd_soc_dapm_demux:
2759 dynamic_source = true;
2760 break;
2761 default:
2762 break;
2765 switch (sink->id) {
2766 case snd_soc_dapm_mux:
2767 case snd_soc_dapm_switch:
2768 case snd_soc_dapm_mixer:
2769 case snd_soc_dapm_mixer_named_ctl:
2770 dynamic_sink = true;
2771 break;
2772 default:
2773 break;
2776 if (dynamic_source && dynamic_sink) {
2777 dev_err(dapm->dev,
2778 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2779 source->name, control, sink->name);
2780 return -EINVAL;
2781 } else if (!dynamic_source && !dynamic_sink) {
2782 dev_err(dapm->dev,
2783 "Control not supported for path %s -> [%s] -> %s\n",
2784 source->name, control, sink->name);
2785 return -EINVAL;
2788 return 0;
2791 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2792 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2793 const char *control,
2794 int (*connected)(struct snd_soc_dapm_widget *source,
2795 struct snd_soc_dapm_widget *sink))
2797 struct snd_soc_dapm_widget *widgets[2];
2798 enum snd_soc_dapm_direction dir;
2799 struct snd_soc_dapm_path *path;
2800 int ret;
2802 if (wsink->is_supply && !wsource->is_supply) {
2803 dev_err(dapm->dev,
2804 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2805 wsource->name, wsink->name);
2806 return -EINVAL;
2809 if (connected && !wsource->is_supply) {
2810 dev_err(dapm->dev,
2811 "connected() callback only supported for supply widgets (%s -> %s)\n",
2812 wsource->name, wsink->name);
2813 return -EINVAL;
2816 if (wsource->is_supply && control) {
2817 dev_err(dapm->dev,
2818 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2819 wsource->name, control, wsink->name);
2820 return -EINVAL;
2823 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2824 if (ret)
2825 return ret;
2827 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2828 if (!path)
2829 return -ENOMEM;
2831 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2832 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2833 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2834 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2836 path->connected = connected;
2837 INIT_LIST_HEAD(&path->list);
2838 INIT_LIST_HEAD(&path->list_kcontrol);
2840 if (wsource->is_supply || wsink->is_supply)
2841 path->is_supply = 1;
2843 /* connect static paths */
2844 if (control == NULL) {
2845 path->connect = 1;
2846 } else {
2847 switch (wsource->id) {
2848 case snd_soc_dapm_demux:
2849 ret = dapm_connect_mux(dapm, path, control, wsource);
2850 if (ret)
2851 goto err;
2852 break;
2853 default:
2854 break;
2857 switch (wsink->id) {
2858 case snd_soc_dapm_mux:
2859 ret = dapm_connect_mux(dapm, path, control, wsink);
2860 if (ret != 0)
2861 goto err;
2862 break;
2863 case snd_soc_dapm_switch:
2864 case snd_soc_dapm_mixer:
2865 case snd_soc_dapm_mixer_named_ctl:
2866 ret = dapm_connect_mixer(dapm, path, control);
2867 if (ret != 0)
2868 goto err;
2869 break;
2870 default:
2871 break;
2875 list_add(&path->list, &dapm->card->paths);
2876 snd_soc_dapm_for_each_direction(dir)
2877 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2879 snd_soc_dapm_for_each_direction(dir) {
2880 dapm_update_widget_flags(widgets[dir]);
2881 dapm_mark_dirty(widgets[dir], "Route added");
2884 if (dapm->card->instantiated && path->connect)
2885 dapm_path_invalidate(path);
2887 return 0;
2888 err:
2889 kfree(path);
2890 return ret;
2893 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2894 const struct snd_soc_dapm_route *route)
2896 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2897 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2898 const char *sink;
2899 const char *source;
2900 char prefixed_sink[80];
2901 char prefixed_source[80];
2902 const char *prefix;
2903 unsigned int sink_ref = 0;
2904 unsigned int source_ref = 0;
2905 int ret;
2907 prefix = soc_dapm_prefix(dapm);
2908 if (prefix) {
2909 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2910 prefix, route->sink);
2911 sink = prefixed_sink;
2912 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2913 prefix, route->source);
2914 source = prefixed_source;
2915 } else {
2916 sink = route->sink;
2917 source = route->source;
2920 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2921 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2923 if (wsink && wsource)
2924 goto skip_search;
2927 * find src and dest widgets over all widgets but favor a widget from
2928 * current DAPM context
2930 for_each_card_widgets(dapm->card, w) {
2931 if (!wsink && !(strcmp(w->name, sink))) {
2932 wtsink = w;
2933 if (w->dapm == dapm) {
2934 wsink = w;
2935 if (wsource)
2936 break;
2938 sink_ref++;
2939 if (sink_ref > 1)
2940 dev_warn(dapm->dev,
2941 "ASoC: sink widget %s overwritten\n",
2942 w->name);
2943 continue;
2945 if (!wsource && !(strcmp(w->name, source))) {
2946 wtsource = w;
2947 if (w->dapm == dapm) {
2948 wsource = w;
2949 if (wsink)
2950 break;
2952 source_ref++;
2953 if (source_ref > 1)
2954 dev_warn(dapm->dev,
2955 "ASoC: source widget %s overwritten\n",
2956 w->name);
2959 /* use widget from another DAPM context if not found from this */
2960 if (!wsink)
2961 wsink = wtsink;
2962 if (!wsource)
2963 wsource = wtsource;
2965 if (wsource == NULL) {
2966 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2967 route->source);
2968 return -ENODEV;
2970 if (wsink == NULL) {
2971 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2972 route->sink);
2973 return -ENODEV;
2976 skip_search:
2977 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2978 dapm_wcache_update(&dapm->path_source_cache, wsource);
2980 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2981 route->connected);
2982 if (ret)
2983 goto err;
2985 return 0;
2986 err:
2987 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2988 source, route->control, sink);
2989 return ret;
2992 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2993 const struct snd_soc_dapm_route *route)
2995 struct snd_soc_dapm_widget *wsource, *wsink;
2996 struct snd_soc_dapm_path *path, *p;
2997 const char *sink;
2998 const char *source;
2999 char prefixed_sink[80];
3000 char prefixed_source[80];
3001 const char *prefix;
3003 if (route->control) {
3004 dev_err(dapm->dev,
3005 "ASoC: Removal of routes with controls not supported\n");
3006 return -EINVAL;
3009 prefix = soc_dapm_prefix(dapm);
3010 if (prefix) {
3011 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
3012 prefix, route->sink);
3013 sink = prefixed_sink;
3014 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
3015 prefix, route->source);
3016 source = prefixed_source;
3017 } else {
3018 sink = route->sink;
3019 source = route->source;
3022 path = NULL;
3023 list_for_each_entry(p, &dapm->card->paths, list) {
3024 if (strcmp(p->source->name, source) != 0)
3025 continue;
3026 if (strcmp(p->sink->name, sink) != 0)
3027 continue;
3028 path = p;
3029 break;
3032 if (path) {
3033 wsource = path->source;
3034 wsink = path->sink;
3036 dapm_mark_dirty(wsource, "Route removed");
3037 dapm_mark_dirty(wsink, "Route removed");
3038 if (path->connect)
3039 dapm_path_invalidate(path);
3041 dapm_free_path(path);
3043 /* Update any path related flags */
3044 dapm_update_widget_flags(wsource);
3045 dapm_update_widget_flags(wsink);
3046 } else {
3047 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
3048 source, sink);
3051 return 0;
3055 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
3056 * @dapm: DAPM context
3057 * @route: audio routes
3058 * @num: number of routes
3060 * Connects 2 dapm widgets together via a named audio path. The sink is
3061 * the widget receiving the audio signal, whilst the source is the sender
3062 * of the audio signal.
3064 * Returns 0 for success else error. On error all resources can be freed
3065 * with a call to snd_soc_card_free().
3067 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
3068 const struct snd_soc_dapm_route *route, int num)
3070 int i, r, ret = 0;
3072 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3073 for (i = 0; i < num; i++) {
3074 r = snd_soc_dapm_add_route(dapm, route);
3075 if (r < 0) {
3076 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
3077 route->source,
3078 route->control ? route->control : "direct",
3079 route->sink);
3080 ret = r;
3082 route++;
3084 mutex_unlock(&dapm->card->dapm_mutex);
3086 return ret;
3088 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3091 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3092 * @dapm: DAPM context
3093 * @route: audio routes
3094 * @num: number of routes
3096 * Removes routes from the DAPM context.
3098 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3099 const struct snd_soc_dapm_route *route, int num)
3101 int i;
3103 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3104 for (i = 0; i < num; i++) {
3105 snd_soc_dapm_del_route(dapm, route);
3106 route++;
3108 mutex_unlock(&dapm->card->dapm_mutex);
3110 return 0;
3112 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3114 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3115 const struct snd_soc_dapm_route *route)
3117 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3118 route->source,
3119 true);
3120 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3121 route->sink,
3122 true);
3123 struct snd_soc_dapm_path *path;
3124 int count = 0;
3126 if (!source) {
3127 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3128 route->source);
3129 return -ENODEV;
3132 if (!sink) {
3133 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3134 route->sink);
3135 return -ENODEV;
3138 if (route->control || route->connected)
3139 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3140 route->source, route->sink);
3142 snd_soc_dapm_widget_for_each_sink_path(source, path) {
3143 if (path->sink == sink) {
3144 path->weak = 1;
3145 count++;
3149 if (count == 0)
3150 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3151 route->source, route->sink);
3152 if (count > 1)
3153 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3154 count, route->source, route->sink);
3156 return 0;
3160 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3161 * @dapm: DAPM context
3162 * @route: audio routes
3163 * @num: number of routes
3165 * Mark existing routes matching those specified in the passed array
3166 * as being weak, meaning that they are ignored for the purpose of
3167 * power decisions. The main intended use case is for sidetone paths
3168 * which couple audio between other independent paths if they are both
3169 * active in order to make the combination work better at the user
3170 * level but which aren't intended to be "used".
3172 * Note that CODEC drivers should not use this as sidetone type paths
3173 * can frequently also be used as bypass paths.
3175 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3176 const struct snd_soc_dapm_route *route, int num)
3178 int i, err;
3179 int ret = 0;
3181 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3182 for (i = 0; i < num; i++) {
3183 err = snd_soc_dapm_weak_route(dapm, route);
3184 if (err)
3185 ret = err;
3186 route++;
3188 mutex_unlock(&dapm->card->dapm_mutex);
3190 return ret;
3192 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3195 * snd_soc_dapm_new_widgets - add new dapm widgets
3196 * @card: card to be checked for new dapm widgets
3198 * Checks the codec for any new dapm widgets and creates them if found.
3200 * Returns 0 for success.
3202 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3204 struct snd_soc_dapm_widget *w;
3205 unsigned int val;
3207 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3209 for_each_card_widgets(card, w)
3211 if (w->new)
3212 continue;
3214 if (w->num_kcontrols) {
3215 w->kcontrols = kcalloc(w->num_kcontrols,
3216 sizeof(struct snd_kcontrol *),
3217 GFP_KERNEL);
3218 if (!w->kcontrols) {
3219 mutex_unlock(&card->dapm_mutex);
3220 return -ENOMEM;
3224 switch(w->id) {
3225 case snd_soc_dapm_switch:
3226 case snd_soc_dapm_mixer:
3227 case snd_soc_dapm_mixer_named_ctl:
3228 dapm_new_mixer(w);
3229 break;
3230 case snd_soc_dapm_mux:
3231 case snd_soc_dapm_demux:
3232 dapm_new_mux(w);
3233 break;
3234 case snd_soc_dapm_pga:
3235 case snd_soc_dapm_effect:
3236 case snd_soc_dapm_out_drv:
3237 dapm_new_pga(w);
3238 break;
3239 case snd_soc_dapm_dai_link:
3240 dapm_new_dai_link(w);
3241 break;
3242 default:
3243 break;
3246 /* Read the initial power state from the device */
3247 if (w->reg >= 0) {
3248 val = soc_dapm_read(w->dapm, w->reg);
3249 val = val >> w->shift;
3250 val &= w->mask;
3251 if (val == w->on_val)
3252 w->power = 1;
3255 w->new = 1;
3257 dapm_mark_dirty(w, "new widget");
3258 dapm_debugfs_add_widget(w);
3261 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3262 mutex_unlock(&card->dapm_mutex);
3263 return 0;
3265 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3268 * snd_soc_dapm_get_volsw - dapm mixer get callback
3269 * @kcontrol: mixer control
3270 * @ucontrol: control element information
3272 * Callback to get the value of a dapm mixer control.
3274 * Returns 0 for success.
3276 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3277 struct snd_ctl_elem_value *ucontrol)
3279 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3280 struct snd_soc_card *card = dapm->card;
3281 struct soc_mixer_control *mc =
3282 (struct soc_mixer_control *)kcontrol->private_value;
3283 int reg = mc->reg;
3284 unsigned int shift = mc->shift;
3285 int max = mc->max;
3286 unsigned int width = fls(max);
3287 unsigned int mask = (1 << fls(max)) - 1;
3288 unsigned int invert = mc->invert;
3289 unsigned int reg_val, val, rval = 0;
3291 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3292 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3293 reg_val = soc_dapm_read(dapm, reg);
3294 val = (reg_val >> shift) & mask;
3296 if (reg != mc->rreg)
3297 reg_val = soc_dapm_read(dapm, mc->rreg);
3299 if (snd_soc_volsw_is_stereo(mc))
3300 rval = (reg_val >> mc->rshift) & mask;
3301 } else {
3302 reg_val = dapm_kcontrol_get_value(kcontrol);
3303 val = reg_val & mask;
3305 if (snd_soc_volsw_is_stereo(mc))
3306 rval = (reg_val >> width) & mask;
3308 mutex_unlock(&card->dapm_mutex);
3310 if (invert)
3311 ucontrol->value.integer.value[0] = max - val;
3312 else
3313 ucontrol->value.integer.value[0] = val;
3315 if (snd_soc_volsw_is_stereo(mc)) {
3316 if (invert)
3317 ucontrol->value.integer.value[1] = max - rval;
3318 else
3319 ucontrol->value.integer.value[1] = rval;
3322 return 0;
3324 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3327 * snd_soc_dapm_put_volsw - dapm mixer set callback
3328 * @kcontrol: mixer control
3329 * @ucontrol: control element information
3331 * Callback to set the value of a dapm mixer control.
3333 * Returns 0 for success.
3335 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3336 struct snd_ctl_elem_value *ucontrol)
3338 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3339 struct snd_soc_card *card = dapm->card;
3340 struct soc_mixer_control *mc =
3341 (struct soc_mixer_control *)kcontrol->private_value;
3342 int reg = mc->reg;
3343 unsigned int shift = mc->shift;
3344 int max = mc->max;
3345 unsigned int width = fls(max);
3346 unsigned int mask = (1 << width) - 1;
3347 unsigned int invert = mc->invert;
3348 unsigned int val, rval = 0;
3349 int connect, rconnect = -1, change, reg_change = 0;
3350 struct snd_soc_dapm_update update = {};
3351 int ret = 0;
3353 val = (ucontrol->value.integer.value[0] & mask);
3354 connect = !!val;
3356 if (invert)
3357 val = max - val;
3359 if (snd_soc_volsw_is_stereo(mc)) {
3360 rval = (ucontrol->value.integer.value[1] & mask);
3361 rconnect = !!rval;
3362 if (invert)
3363 rval = max - rval;
3366 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3368 /* This assumes field width < (bits in unsigned int / 2) */
3369 if (width > sizeof(unsigned int) * 8 / 2)
3370 dev_warn(dapm->dev,
3371 "ASoC: control %s field width limit exceeded\n",
3372 kcontrol->id.name);
3373 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3375 if (reg != SND_SOC_NOPM) {
3376 val = val << shift;
3377 rval = rval << mc->rshift;
3379 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3381 if (snd_soc_volsw_is_stereo(mc))
3382 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3383 mask << mc->rshift,
3384 rval);
3387 if (change || reg_change) {
3388 if (reg_change) {
3389 if (snd_soc_volsw_is_stereo(mc)) {
3390 update.has_second_set = true;
3391 update.reg2 = mc->rreg;
3392 update.mask2 = mask << mc->rshift;
3393 update.val2 = rval;
3395 update.kcontrol = kcontrol;
3396 update.reg = reg;
3397 update.mask = mask << shift;
3398 update.val = val;
3399 card->update = &update;
3401 change |= reg_change;
3403 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3404 rconnect);
3406 card->update = NULL;
3409 mutex_unlock(&card->dapm_mutex);
3411 if (ret > 0)
3412 snd_soc_dpcm_runtime_update(card);
3414 return change;
3416 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3419 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3420 * @kcontrol: mixer control
3421 * @ucontrol: control element information
3423 * Callback to get the value of a dapm enumerated double mixer control.
3425 * Returns 0 for success.
3427 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3428 struct snd_ctl_elem_value *ucontrol)
3430 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3431 struct snd_soc_card *card = dapm->card;
3432 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3433 unsigned int reg_val, val;
3435 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3436 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3437 reg_val = soc_dapm_read(dapm, e->reg);
3438 } else {
3439 reg_val = dapm_kcontrol_get_value(kcontrol);
3441 mutex_unlock(&card->dapm_mutex);
3443 val = (reg_val >> e->shift_l) & e->mask;
3444 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3445 if (e->shift_l != e->shift_r) {
3446 val = (reg_val >> e->shift_r) & e->mask;
3447 val = snd_soc_enum_val_to_item(e, val);
3448 ucontrol->value.enumerated.item[1] = val;
3451 return 0;
3453 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3456 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3457 * @kcontrol: mixer control
3458 * @ucontrol: control element information
3460 * Callback to set the value of a dapm enumerated double mixer control.
3462 * Returns 0 for success.
3464 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3465 struct snd_ctl_elem_value *ucontrol)
3467 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3468 struct snd_soc_card *card = dapm->card;
3469 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3470 unsigned int *item = ucontrol->value.enumerated.item;
3471 unsigned int val, change, reg_change = 0;
3472 unsigned int mask;
3473 struct snd_soc_dapm_update update = {};
3474 int ret = 0;
3476 if (item[0] >= e->items)
3477 return -EINVAL;
3479 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3480 mask = e->mask << e->shift_l;
3481 if (e->shift_l != e->shift_r) {
3482 if (item[1] > e->items)
3483 return -EINVAL;
3484 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3485 mask |= e->mask << e->shift_r;
3488 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3490 change = dapm_kcontrol_set_value(kcontrol, val);
3492 if (e->reg != SND_SOC_NOPM)
3493 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3495 if (change || reg_change) {
3496 if (reg_change) {
3497 update.kcontrol = kcontrol;
3498 update.reg = e->reg;
3499 update.mask = mask;
3500 update.val = val;
3501 card->update = &update;
3503 change |= reg_change;
3505 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3507 card->update = NULL;
3510 mutex_unlock(&card->dapm_mutex);
3512 if (ret > 0)
3513 snd_soc_dpcm_runtime_update(card);
3515 return change;
3517 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3520 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3522 * @kcontrol: mixer control
3523 * @uinfo: control element information
3525 * Callback to provide information about a pin switch control.
3527 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3528 struct snd_ctl_elem_info *uinfo)
3530 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3531 uinfo->count = 1;
3532 uinfo->value.integer.min = 0;
3533 uinfo->value.integer.max = 1;
3535 return 0;
3537 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3540 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3542 * @kcontrol: mixer control
3543 * @ucontrol: Value
3545 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3546 struct snd_ctl_elem_value *ucontrol)
3548 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3549 const char *pin = (const char *)kcontrol->private_value;
3551 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3553 ucontrol->value.integer.value[0] =
3554 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3556 mutex_unlock(&card->dapm_mutex);
3558 return 0;
3560 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3563 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3565 * @kcontrol: mixer control
3566 * @ucontrol: Value
3568 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3569 struct snd_ctl_elem_value *ucontrol)
3571 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3572 const char *pin = (const char *)kcontrol->private_value;
3574 if (ucontrol->value.integer.value[0])
3575 snd_soc_dapm_enable_pin(&card->dapm, pin);
3576 else
3577 snd_soc_dapm_disable_pin(&card->dapm, pin);
3579 snd_soc_dapm_sync(&card->dapm);
3580 return 0;
3582 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3584 struct snd_soc_dapm_widget *
3585 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3586 const struct snd_soc_dapm_widget *widget)
3588 enum snd_soc_dapm_direction dir;
3589 struct snd_soc_dapm_widget *w;
3590 const char *prefix;
3591 int ret;
3593 if ((w = dapm_cnew_widget(widget)) == NULL)
3594 return ERR_PTR(-ENOMEM);
3596 switch (w->id) {
3597 case snd_soc_dapm_regulator_supply:
3598 w->regulator = devm_regulator_get(dapm->dev, w->name);
3599 if (IS_ERR(w->regulator)) {
3600 ret = PTR_ERR(w->regulator);
3601 goto request_failed;
3604 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3605 ret = regulator_allow_bypass(w->regulator, true);
3606 if (ret != 0)
3607 dev_warn(dapm->dev,
3608 "ASoC: Failed to bypass %s: %d\n",
3609 w->name, ret);
3611 break;
3612 case snd_soc_dapm_pinctrl:
3613 w->pinctrl = devm_pinctrl_get(dapm->dev);
3614 if (IS_ERR(w->pinctrl)) {
3615 ret = PTR_ERR(w->pinctrl);
3616 goto request_failed;
3619 /* set to sleep_state when initializing */
3620 dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD);
3621 break;
3622 case snd_soc_dapm_clock_supply:
3623 w->clk = devm_clk_get(dapm->dev, w->name);
3624 if (IS_ERR(w->clk)) {
3625 ret = PTR_ERR(w->clk);
3626 goto request_failed;
3628 break;
3629 default:
3630 break;
3633 prefix = soc_dapm_prefix(dapm);
3634 if (prefix)
3635 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3636 else
3637 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3638 if (w->name == NULL) {
3639 kfree_const(w->sname);
3640 kfree(w);
3641 return ERR_PTR(-ENOMEM);
3644 switch (w->id) {
3645 case snd_soc_dapm_mic:
3646 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3647 w->power_check = dapm_generic_check_power;
3648 break;
3649 case snd_soc_dapm_input:
3650 if (!dapm->card->fully_routed)
3651 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3652 w->power_check = dapm_generic_check_power;
3653 break;
3654 case snd_soc_dapm_spk:
3655 case snd_soc_dapm_hp:
3656 w->is_ep = SND_SOC_DAPM_EP_SINK;
3657 w->power_check = dapm_generic_check_power;
3658 break;
3659 case snd_soc_dapm_output:
3660 if (!dapm->card->fully_routed)
3661 w->is_ep = SND_SOC_DAPM_EP_SINK;
3662 w->power_check = dapm_generic_check_power;
3663 break;
3664 case snd_soc_dapm_vmid:
3665 case snd_soc_dapm_siggen:
3666 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3667 w->power_check = dapm_always_on_check_power;
3668 break;
3669 case snd_soc_dapm_sink:
3670 w->is_ep = SND_SOC_DAPM_EP_SINK;
3671 w->power_check = dapm_always_on_check_power;
3672 break;
3674 case snd_soc_dapm_mux:
3675 case snd_soc_dapm_demux:
3676 case snd_soc_dapm_switch:
3677 case snd_soc_dapm_mixer:
3678 case snd_soc_dapm_mixer_named_ctl:
3679 case snd_soc_dapm_adc:
3680 case snd_soc_dapm_aif_out:
3681 case snd_soc_dapm_dac:
3682 case snd_soc_dapm_aif_in:
3683 case snd_soc_dapm_pga:
3684 case snd_soc_dapm_buffer:
3685 case snd_soc_dapm_scheduler:
3686 case snd_soc_dapm_effect:
3687 case snd_soc_dapm_src:
3688 case snd_soc_dapm_asrc:
3689 case snd_soc_dapm_encoder:
3690 case snd_soc_dapm_decoder:
3691 case snd_soc_dapm_out_drv:
3692 case snd_soc_dapm_micbias:
3693 case snd_soc_dapm_line:
3694 case snd_soc_dapm_dai_link:
3695 case snd_soc_dapm_dai_out:
3696 case snd_soc_dapm_dai_in:
3697 w->power_check = dapm_generic_check_power;
3698 break;
3699 case snd_soc_dapm_supply:
3700 case snd_soc_dapm_regulator_supply:
3701 case snd_soc_dapm_pinctrl:
3702 case snd_soc_dapm_clock_supply:
3703 case snd_soc_dapm_kcontrol:
3704 w->is_supply = 1;
3705 w->power_check = dapm_supply_check_power;
3706 break;
3707 default:
3708 w->power_check = dapm_always_on_check_power;
3709 break;
3712 w->dapm = dapm;
3713 INIT_LIST_HEAD(&w->list);
3714 INIT_LIST_HEAD(&w->dirty);
3715 /* see for_each_card_widgets */
3716 list_add_tail(&w->list, &dapm->card->widgets);
3718 snd_soc_dapm_for_each_direction(dir) {
3719 INIT_LIST_HEAD(&w->edges[dir]);
3720 w->endpoints[dir] = -1;
3723 /* machine layer sets up unconnected pins and insertions */
3724 w->connected = 1;
3725 return w;
3727 request_failed:
3728 if (ret != -EPROBE_DEFER)
3729 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3730 w->name, ret);
3732 kfree_const(w->sname);
3733 kfree(w);
3734 return ERR_PTR(ret);
3738 * snd_soc_dapm_new_control - create new dapm control
3739 * @dapm: DAPM context
3740 * @widget: widget template
3742 * Creates new DAPM control based upon a template.
3744 * Returns a widget pointer on success or an error pointer on failure
3746 struct snd_soc_dapm_widget *
3747 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3748 const struct snd_soc_dapm_widget *widget)
3750 struct snd_soc_dapm_widget *w;
3752 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3753 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3754 mutex_unlock(&dapm->card->dapm_mutex);
3756 return w;
3758 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3761 * snd_soc_dapm_new_controls - create new dapm controls
3762 * @dapm: DAPM context
3763 * @widget: widget array
3764 * @num: number of widgets
3766 * Creates new DAPM controls based upon the templates.
3768 * Returns 0 for success else error.
3770 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3771 const struct snd_soc_dapm_widget *widget,
3772 int num)
3774 struct snd_soc_dapm_widget *w;
3775 int i;
3776 int ret = 0;
3778 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3779 for (i = 0; i < num; i++) {
3780 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3781 if (IS_ERR(w)) {
3782 ret = PTR_ERR(w);
3783 break;
3785 widget++;
3787 mutex_unlock(&dapm->card->dapm_mutex);
3788 return ret;
3790 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3792 static int
3793 snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3794 struct snd_pcm_substream *substream)
3796 struct snd_soc_dapm_path *path;
3797 struct snd_soc_dai *source, *sink;
3798 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
3799 struct snd_pcm_hw_params *params = NULL;
3800 const struct snd_soc_pcm_stream *config = NULL;
3801 struct snd_pcm_runtime *runtime = NULL;
3802 unsigned int fmt;
3803 int ret = 0;
3805 params = kzalloc(sizeof(*params), GFP_KERNEL);
3806 if (!params)
3807 return -ENOMEM;
3809 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3810 if (!runtime) {
3811 ret = -ENOMEM;
3812 goto out;
3815 substream->runtime = runtime;
3817 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3818 snd_soc_dapm_widget_for_each_source_path(w, path) {
3819 source = path->source->priv;
3821 ret = snd_soc_dai_startup(source, substream);
3822 if (ret < 0) {
3823 dev_err(source->dev,
3824 "ASoC: startup() failed: %d\n", ret);
3825 goto out;
3827 snd_soc_dai_activate(source, substream->stream);
3830 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3831 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3832 sink = path->sink->priv;
3834 ret = snd_soc_dai_startup(sink, substream);
3835 if (ret < 0) {
3836 dev_err(sink->dev,
3837 "ASoC: startup() failed: %d\n", ret);
3838 goto out;
3840 snd_soc_dai_activate(sink, substream->stream);
3843 substream->hw_opened = 1;
3846 * Note: getting the config after .startup() gives a chance to
3847 * either party on the link to alter the configuration if
3848 * necessary
3850 config = rtd->dai_link->params + rtd->params_select;
3851 if (WARN_ON(!config)) {
3852 dev_err(w->dapm->dev, "ASoC: link config missing\n");
3853 ret = -EINVAL;
3854 goto out;
3857 /* Be a little careful as we don't want to overflow the mask array */
3858 if (config->formats) {
3859 fmt = ffs(config->formats) - 1;
3860 } else {
3861 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3862 config->formats);
3864 ret = -EINVAL;
3865 goto out;
3868 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3869 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3870 config->rate_min;
3871 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3872 config->rate_max;
3873 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3874 = config->channels_min;
3875 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3876 = config->channels_max;
3878 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3879 snd_soc_dapm_widget_for_each_source_path(w, path) {
3880 source = path->source->priv;
3882 ret = snd_soc_dai_hw_params(source, substream, params);
3883 if (ret < 0)
3884 goto out;
3886 dapm_update_dai_unlocked(substream, params, source);
3889 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3890 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3891 sink = path->sink->priv;
3893 ret = snd_soc_dai_hw_params(sink, substream, params);
3894 if (ret < 0)
3895 goto out;
3897 dapm_update_dai_unlocked(substream, params, sink);
3900 runtime->format = params_format(params);
3901 runtime->subformat = params_subformat(params);
3902 runtime->channels = params_channels(params);
3903 runtime->rate = params_rate(params);
3905 out:
3906 kfree(params);
3907 return ret;
3910 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3911 struct snd_kcontrol *kcontrol, int event)
3913 struct snd_soc_dapm_path *path;
3914 struct snd_soc_dai *source, *sink;
3915 struct snd_pcm_substream *substream = w->priv;
3916 int ret = 0, saved_stream = substream->stream;
3918 if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3919 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3920 return -EINVAL;
3922 switch (event) {
3923 case SND_SOC_DAPM_PRE_PMU:
3924 ret = snd_soc_dai_link_event_pre_pmu(w, substream);
3925 if (ret < 0)
3926 goto out;
3928 break;
3930 case SND_SOC_DAPM_POST_PMU:
3931 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3932 sink = path->sink->priv;
3934 ret = snd_soc_dai_digital_mute(sink, 0,
3935 SNDRV_PCM_STREAM_PLAYBACK);
3936 if (ret != 0 && ret != -ENOTSUPP)
3937 dev_warn(sink->dev,
3938 "ASoC: Failed to unmute: %d\n", ret);
3939 ret = 0;
3941 break;
3943 case SND_SOC_DAPM_PRE_PMD:
3944 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3945 sink = path->sink->priv;
3947 ret = snd_soc_dai_digital_mute(sink, 1,
3948 SNDRV_PCM_STREAM_PLAYBACK);
3949 if (ret != 0 && ret != -ENOTSUPP)
3950 dev_warn(sink->dev,
3951 "ASoC: Failed to mute: %d\n", ret);
3952 ret = 0;
3955 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3956 snd_soc_dapm_widget_for_each_source_path(w, path) {
3957 source = path->source->priv;
3958 snd_soc_dai_hw_free(source, substream, 0);
3961 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3962 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3963 sink = path->sink->priv;
3964 snd_soc_dai_hw_free(sink, substream, 0);
3967 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3968 snd_soc_dapm_widget_for_each_source_path(w, path) {
3969 source = path->source->priv;
3970 snd_soc_dai_deactivate(source, substream->stream);
3971 snd_soc_dai_shutdown(source, substream, 0);
3974 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3975 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3976 sink = path->sink->priv;
3977 snd_soc_dai_deactivate(sink, substream->stream);
3978 snd_soc_dai_shutdown(sink, substream, 0);
3980 break;
3982 case SND_SOC_DAPM_POST_PMD:
3983 kfree(substream->runtime);
3984 break;
3986 default:
3987 WARN(1, "Unknown event %d\n", event);
3988 ret = -EINVAL;
3991 out:
3992 /* Restore the substream direction */
3993 substream->stream = saved_stream;
3994 return ret;
3997 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3998 struct snd_ctl_elem_value *ucontrol)
4000 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4001 struct snd_soc_pcm_runtime *rtd = w->priv;
4003 ucontrol->value.enumerated.item[0] = rtd->params_select;
4005 return 0;
4008 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
4009 struct snd_ctl_elem_value *ucontrol)
4011 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4012 struct snd_soc_pcm_runtime *rtd = w->priv;
4014 /* Can't change the config when widget is already powered */
4015 if (w->power)
4016 return -EBUSY;
4018 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
4019 return 0;
4021 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
4022 return -EINVAL;
4024 rtd->params_select = ucontrol->value.enumerated.item[0];
4026 return 0;
4029 static void
4030 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
4031 unsigned long *private_value,
4032 int num_params,
4033 const char **w_param_text)
4035 int count;
4037 devm_kfree(card->dev, (void *)*private_value);
4039 if (!w_param_text)
4040 return;
4042 for (count = 0 ; count < num_params; count++)
4043 devm_kfree(card->dev, (void *)w_param_text[count]);
4044 devm_kfree(card->dev, w_param_text);
4047 static struct snd_kcontrol_new *
4048 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
4049 char *link_name,
4050 const struct snd_soc_pcm_stream *params,
4051 int num_params, const char **w_param_text,
4052 unsigned long *private_value)
4054 struct soc_enum w_param_enum[] = {
4055 SOC_ENUM_SINGLE(0, 0, 0, NULL),
4057 struct snd_kcontrol_new kcontrol_dai_link[] = {
4058 SOC_ENUM_EXT(NULL, w_param_enum[0],
4059 snd_soc_dapm_dai_link_get,
4060 snd_soc_dapm_dai_link_put),
4062 struct snd_kcontrol_new *kcontrol_news;
4063 const struct snd_soc_pcm_stream *config = params;
4064 int count;
4066 for (count = 0 ; count < num_params; count++) {
4067 if (!config->stream_name) {
4068 dev_warn(card->dapm.dev,
4069 "ASoC: anonymous config %d for dai link %s\n",
4070 count, link_name);
4071 w_param_text[count] =
4072 devm_kasprintf(card->dev, GFP_KERNEL,
4073 "Anonymous Configuration %d",
4074 count);
4075 } else {
4076 w_param_text[count] = devm_kmemdup(card->dev,
4077 config->stream_name,
4078 strlen(config->stream_name) + 1,
4079 GFP_KERNEL);
4081 if (!w_param_text[count])
4082 goto outfree_w_param;
4083 config++;
4086 w_param_enum[0].items = num_params;
4087 w_param_enum[0].texts = w_param_text;
4089 *private_value =
4090 (unsigned long) devm_kmemdup(card->dev,
4091 (void *)(kcontrol_dai_link[0].private_value),
4092 sizeof(struct soc_enum), GFP_KERNEL);
4093 if (!*private_value) {
4094 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4095 link_name);
4096 goto outfree_w_param;
4098 kcontrol_dai_link[0].private_value = *private_value;
4099 /* duplicate kcontrol_dai_link on heap so that memory persists */
4100 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4101 sizeof(struct snd_kcontrol_new),
4102 GFP_KERNEL);
4103 if (!kcontrol_news) {
4104 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4105 link_name);
4106 goto outfree_w_param;
4108 return kcontrol_news;
4110 outfree_w_param:
4111 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
4112 return NULL;
4115 static struct snd_soc_dapm_widget *
4116 snd_soc_dapm_new_dai(struct snd_soc_card *card,
4117 struct snd_pcm_substream *substream,
4118 char *id)
4120 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
4121 struct snd_soc_dapm_widget template;
4122 struct snd_soc_dapm_widget *w;
4123 const char **w_param_text;
4124 unsigned long private_value = 0;
4125 char *link_name;
4126 int ret;
4128 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
4129 rtd->dai_link->name, id);
4130 if (!link_name)
4131 return ERR_PTR(-ENOMEM);
4133 memset(&template, 0, sizeof(template));
4134 template.reg = SND_SOC_NOPM;
4135 template.id = snd_soc_dapm_dai_link;
4136 template.name = link_name;
4137 template.event = snd_soc_dai_link_event;
4138 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
4139 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD;
4140 template.kcontrol_news = NULL;
4142 /* allocate memory for control, only in case of multiple configs */
4143 if (rtd->dai_link->num_params > 1) {
4144 w_param_text = devm_kcalloc(card->dev,
4145 rtd->dai_link->num_params,
4146 sizeof(char *), GFP_KERNEL);
4147 if (!w_param_text) {
4148 ret = -ENOMEM;
4149 goto param_fail;
4152 template.num_kcontrols = 1;
4153 template.kcontrol_news =
4154 snd_soc_dapm_alloc_kcontrol(card,
4155 link_name,
4156 rtd->dai_link->params,
4157 rtd->dai_link->num_params,
4158 w_param_text, &private_value);
4159 if (!template.kcontrol_news) {
4160 ret = -ENOMEM;
4161 goto param_fail;
4163 } else {
4164 w_param_text = NULL;
4166 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
4168 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
4169 if (IS_ERR(w)) {
4170 ret = PTR_ERR(w);
4171 dev_err(rtd->dev, "ASoC: Failed to create %s widget: %d\n",
4172 link_name, ret);
4173 goto outfree_kcontrol_news;
4176 w->priv = substream;
4178 return w;
4180 outfree_kcontrol_news:
4181 devm_kfree(card->dev, (void *)template.kcontrol_news);
4182 snd_soc_dapm_free_kcontrol(card, &private_value,
4183 rtd->dai_link->num_params, w_param_text);
4184 param_fail:
4185 devm_kfree(card->dev, link_name);
4186 return ERR_PTR(ret);
4189 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4190 struct snd_soc_dai *dai)
4192 struct snd_soc_dapm_widget template;
4193 struct snd_soc_dapm_widget *w;
4195 WARN_ON(dapm->dev != dai->dev);
4197 memset(&template, 0, sizeof(template));
4198 template.reg = SND_SOC_NOPM;
4200 if (dai->driver->playback.stream_name) {
4201 template.id = snd_soc_dapm_dai_in;
4202 template.name = dai->driver->playback.stream_name;
4203 template.sname = dai->driver->playback.stream_name;
4205 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4206 template.name);
4208 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4209 if (IS_ERR(w))
4210 return PTR_ERR(w);
4212 w->priv = dai;
4213 dai->playback_widget = w;
4216 if (dai->driver->capture.stream_name) {
4217 template.id = snd_soc_dapm_dai_out;
4218 template.name = dai->driver->capture.stream_name;
4219 template.sname = dai->driver->capture.stream_name;
4221 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4222 template.name);
4224 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4225 if (IS_ERR(w))
4226 return PTR_ERR(w);
4228 w->priv = dai;
4229 dai->capture_widget = w;
4232 return 0;
4235 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4237 struct snd_soc_dapm_widget *dai_w, *w;
4238 struct snd_soc_dapm_widget *src, *sink;
4239 struct snd_soc_dai *dai;
4241 /* For each DAI widget... */
4242 for_each_card_widgets(card, dai_w) {
4243 switch (dai_w->id) {
4244 case snd_soc_dapm_dai_in:
4245 case snd_soc_dapm_dai_out:
4246 break;
4247 default:
4248 continue;
4251 /* let users know there is no DAI to link */
4252 if (!dai_w->priv) {
4253 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4254 dai_w->name);
4255 continue;
4258 dai = dai_w->priv;
4260 /* ...find all widgets with the same stream and link them */
4261 for_each_card_widgets(card, w) {
4262 if (w->dapm != dai_w->dapm)
4263 continue;
4265 switch (w->id) {
4266 case snd_soc_dapm_dai_in:
4267 case snd_soc_dapm_dai_out:
4268 continue;
4269 default:
4270 break;
4273 if (!w->sname || !strstr(w->sname, dai_w->sname))
4274 continue;
4276 if (dai_w->id == snd_soc_dapm_dai_in) {
4277 src = dai_w;
4278 sink = w;
4279 } else {
4280 src = w;
4281 sink = dai_w;
4283 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4284 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4288 return 0;
4291 static void dapm_connect_dai_routes(struct snd_soc_dapm_context *dapm,
4292 struct snd_soc_dai *src_dai,
4293 struct snd_soc_dapm_widget *src,
4294 struct snd_soc_dapm_widget *dai,
4295 struct snd_soc_dai *sink_dai,
4296 struct snd_soc_dapm_widget *sink)
4298 dev_dbg(dapm->dev, "connected DAI link %s:%s -> %s:%s\n",
4299 src_dai->component->name, src->name,
4300 sink_dai->component->name, sink->name);
4302 if (dai) {
4303 snd_soc_dapm_add_path(dapm, src, dai, NULL, NULL);
4304 src = dai;
4307 snd_soc_dapm_add_path(dapm, src, sink, NULL, NULL);
4310 static void dapm_connect_dai_pair(struct snd_soc_card *card,
4311 struct snd_soc_pcm_runtime *rtd,
4312 struct snd_soc_dai *codec_dai,
4313 struct snd_soc_dai *cpu_dai)
4315 struct snd_soc_dai_link *dai_link = rtd->dai_link;
4316 struct snd_soc_dapm_widget *dai, *codec, *playback_cpu, *capture_cpu;
4317 struct snd_pcm_substream *substream;
4318 struct snd_pcm_str *streams = rtd->pcm->streams;
4320 if (dai_link->params) {
4321 playback_cpu = cpu_dai->capture_widget;
4322 capture_cpu = cpu_dai->playback_widget;
4323 } else {
4324 playback_cpu = cpu_dai->playback_widget;
4325 capture_cpu = cpu_dai->capture_widget;
4328 /* connect BE DAI playback if widgets are valid */
4329 codec = codec_dai->playback_widget;
4331 if (playback_cpu && codec) {
4332 if (dai_link->params && !rtd->playback_widget) {
4333 substream = streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
4334 dai = snd_soc_dapm_new_dai(card, substream, "playback");
4335 if (IS_ERR(dai))
4336 goto capture;
4337 rtd->playback_widget = dai;
4340 dapm_connect_dai_routes(&card->dapm, cpu_dai, playback_cpu,
4341 rtd->playback_widget,
4342 codec_dai, codec);
4345 capture:
4346 /* connect BE DAI capture if widgets are valid */
4347 codec = codec_dai->capture_widget;
4349 if (codec && capture_cpu) {
4350 if (dai_link->params && !rtd->capture_widget) {
4351 substream = streams[SNDRV_PCM_STREAM_CAPTURE].substream;
4352 dai = snd_soc_dapm_new_dai(card, substream, "capture");
4353 if (IS_ERR(dai))
4354 return;
4355 rtd->capture_widget = dai;
4358 dapm_connect_dai_routes(&card->dapm, codec_dai, codec,
4359 rtd->capture_widget,
4360 cpu_dai, capture_cpu);
4364 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4365 int event)
4367 struct snd_soc_dapm_widget *w;
4368 unsigned int ep;
4370 w = snd_soc_dai_get_widget(dai, stream);
4372 if (w) {
4373 dapm_mark_dirty(w, "stream event");
4375 if (w->id == snd_soc_dapm_dai_in) {
4376 ep = SND_SOC_DAPM_EP_SOURCE;
4377 dapm_widget_invalidate_input_paths(w);
4378 } else {
4379 ep = SND_SOC_DAPM_EP_SINK;
4380 dapm_widget_invalidate_output_paths(w);
4383 switch (event) {
4384 case SND_SOC_DAPM_STREAM_START:
4385 w->active = 1;
4386 w->is_ep = ep;
4387 break;
4388 case SND_SOC_DAPM_STREAM_STOP:
4389 w->active = 0;
4390 w->is_ep = 0;
4391 break;
4392 case SND_SOC_DAPM_STREAM_SUSPEND:
4393 case SND_SOC_DAPM_STREAM_RESUME:
4394 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4395 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4396 break;
4401 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4403 struct snd_soc_pcm_runtime *rtd;
4404 struct snd_soc_dai *codec_dai;
4405 int i;
4407 /* for each BE DAI link... */
4408 for_each_card_rtds(card, rtd) {
4410 * dynamic FE links have no fixed DAI mapping.
4411 * CODEC<->CODEC links have no direct connection.
4413 if (rtd->dai_link->dynamic)
4414 continue;
4416 if (rtd->num_cpus == 1) {
4417 for_each_rtd_codec_dais(rtd, i, codec_dai)
4418 dapm_connect_dai_pair(card, rtd, codec_dai,
4419 asoc_rtd_to_cpu(rtd, 0));
4420 } else if (rtd->num_codecs == rtd->num_cpus) {
4421 for_each_rtd_codec_dais(rtd, i, codec_dai)
4422 dapm_connect_dai_pair(card, rtd, codec_dai,
4423 asoc_rtd_to_cpu(rtd, i));
4424 } else {
4425 dev_err(card->dev,
4426 "N cpus to M codecs link is not supported yet\n");
4431 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4432 int event)
4434 struct snd_soc_dai *dai;
4435 int i;
4437 for_each_rtd_dais(rtd, i, dai)
4438 soc_dapm_dai_stream_event(dai, stream, event);
4440 dapm_power_widgets(rtd->card, event);
4444 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4445 * @rtd: PCM runtime data
4446 * @stream: stream name
4447 * @event: stream event
4449 * Sends a stream event to the dapm core. The core then makes any
4450 * necessary widget power changes.
4452 * Returns 0 for success else error.
4454 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4455 int event)
4457 struct snd_soc_card *card = rtd->card;
4459 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4460 soc_dapm_stream_event(rtd, stream, event);
4461 mutex_unlock(&card->dapm_mutex);
4464 void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream)
4466 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
4467 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
4468 /* powered down playback stream now */
4469 snd_soc_dapm_stream_event(rtd,
4470 SNDRV_PCM_STREAM_PLAYBACK,
4471 SND_SOC_DAPM_STREAM_STOP);
4472 } else {
4473 /* start delayed pop wq here for playback streams */
4474 rtd->pop_wait = 1;
4475 queue_delayed_work(system_power_efficient_wq,
4476 &rtd->delayed_work,
4477 msecs_to_jiffies(rtd->pmdown_time));
4479 } else {
4480 /* capture streams can be powered down now */
4481 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
4482 SND_SOC_DAPM_STREAM_STOP);
4485 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop);
4488 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4489 * @dapm: DAPM context
4490 * @pin: pin name
4492 * Enables input/output pin and its parents or children widgets iff there is
4493 * a valid audio route and active audio stream.
4495 * Requires external locking.
4497 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4498 * do any widget power switching.
4500 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4501 const char *pin)
4503 return snd_soc_dapm_set_pin(dapm, pin, 1);
4505 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4508 * snd_soc_dapm_enable_pin - enable pin.
4509 * @dapm: DAPM context
4510 * @pin: pin name
4512 * Enables input/output pin and its parents or children widgets iff there is
4513 * a valid audio route and active audio stream.
4515 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4516 * do any widget power switching.
4518 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4520 int ret;
4522 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4524 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4526 mutex_unlock(&dapm->card->dapm_mutex);
4528 return ret;
4530 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4533 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4534 * @dapm: DAPM context
4535 * @pin: pin name
4537 * Enables input/output pin regardless of any other state. This is
4538 * intended for use with microphone bias supplies used in microphone
4539 * jack detection.
4541 * Requires external locking.
4543 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4544 * do any widget power switching.
4546 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4547 const char *pin)
4549 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4551 if (!w) {
4552 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4553 return -EINVAL;
4556 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4557 if (!w->connected) {
4559 * w->force does not affect the number of input or output paths,
4560 * so we only have to recheck if w->connected is changed
4562 dapm_widget_invalidate_input_paths(w);
4563 dapm_widget_invalidate_output_paths(w);
4564 w->connected = 1;
4566 w->force = 1;
4567 dapm_mark_dirty(w, "force enable");
4569 return 0;
4571 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4574 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4575 * @dapm: DAPM context
4576 * @pin: pin name
4578 * Enables input/output pin regardless of any other state. This is
4579 * intended for use with microphone bias supplies used in microphone
4580 * jack detection.
4582 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4583 * do any widget power switching.
4585 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4586 const char *pin)
4588 int ret;
4590 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4592 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4594 mutex_unlock(&dapm->card->dapm_mutex);
4596 return ret;
4598 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4601 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4602 * @dapm: DAPM context
4603 * @pin: pin name
4605 * Disables input/output pin and its parents or children widgets.
4607 * Requires external locking.
4609 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4610 * do any widget power switching.
4612 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4613 const char *pin)
4615 return snd_soc_dapm_set_pin(dapm, pin, 0);
4617 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4620 * snd_soc_dapm_disable_pin - disable pin.
4621 * @dapm: DAPM context
4622 * @pin: pin name
4624 * Disables input/output pin and its parents or children widgets.
4626 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4627 * do any widget power switching.
4629 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4630 const char *pin)
4632 int ret;
4634 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4636 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4638 mutex_unlock(&dapm->card->dapm_mutex);
4640 return ret;
4642 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4645 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4646 * @dapm: DAPM context
4647 * @pin: pin name
4649 * Marks the specified pin as being not connected, disabling it along
4650 * any parent or child widgets. At present this is identical to
4651 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4652 * additional things such as disabling controls which only affect
4653 * paths through the pin.
4655 * Requires external locking.
4657 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4658 * do any widget power switching.
4660 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4661 const char *pin)
4663 return snd_soc_dapm_set_pin(dapm, pin, 0);
4665 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4668 * snd_soc_dapm_nc_pin - permanently disable pin.
4669 * @dapm: DAPM context
4670 * @pin: pin name
4672 * Marks the specified pin as being not connected, disabling it along
4673 * any parent or child widgets. At present this is identical to
4674 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4675 * additional things such as disabling controls which only affect
4676 * paths through the pin.
4678 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4679 * do any widget power switching.
4681 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4683 int ret;
4685 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4687 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4689 mutex_unlock(&dapm->card->dapm_mutex);
4691 return ret;
4693 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4696 * snd_soc_dapm_get_pin_status - get audio pin status
4697 * @dapm: DAPM context
4698 * @pin: audio signal pin endpoint (or start point)
4700 * Get audio pin status - connected or disconnected.
4702 * Returns 1 for connected otherwise 0.
4704 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4705 const char *pin)
4707 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4709 if (w)
4710 return w->connected;
4712 return 0;
4714 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4717 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4718 * @dapm: DAPM context
4719 * @pin: audio signal pin endpoint (or start point)
4721 * Mark the given endpoint or pin as ignoring suspend. When the
4722 * system is disabled a path between two endpoints flagged as ignoring
4723 * suspend will not be disabled. The path must already be enabled via
4724 * normal means at suspend time, it will not be turned on if it was not
4725 * already enabled.
4727 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4728 const char *pin)
4730 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4732 if (!w) {
4733 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4734 return -EINVAL;
4737 w->ignore_suspend = 1;
4739 return 0;
4741 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4744 * snd_soc_dapm_free - free dapm resources
4745 * @dapm: DAPM context
4747 * Free all dapm widgets and resources.
4749 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4751 dapm_debugfs_cleanup(dapm);
4752 dapm_free_widgets(dapm);
4753 list_del(&dapm->list);
4755 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4757 void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
4758 struct snd_soc_card *card,
4759 struct snd_soc_component *component)
4761 dapm->card = card;
4762 dapm->component = component;
4763 dapm->bias_level = SND_SOC_BIAS_OFF;
4765 if (component) {
4766 dapm->dev = component->dev;
4767 dapm->idle_bias_off = !component->driver->idle_bias_on;
4768 dapm->suspend_bias_off = component->driver->suspend_bias_off;
4769 } else {
4770 dapm->dev = card->dev;
4773 INIT_LIST_HEAD(&dapm->list);
4774 /* see for_each_card_dapms */
4775 list_add(&dapm->list, &card->dapm_list);
4777 EXPORT_SYMBOL_GPL(snd_soc_dapm_init);
4779 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4781 struct snd_soc_card *card = dapm->card;
4782 struct snd_soc_dapm_widget *w;
4783 LIST_HEAD(down_list);
4784 int powerdown = 0;
4786 mutex_lock(&card->dapm_mutex);
4788 for_each_card_widgets(dapm->card, w) {
4789 if (w->dapm != dapm)
4790 continue;
4791 if (w->power) {
4792 dapm_seq_insert(w, &down_list, false);
4793 w->new_power = 0;
4794 powerdown = 1;
4798 /* If there were no widgets to power down we're already in
4799 * standby.
4801 if (powerdown) {
4802 if (dapm->bias_level == SND_SOC_BIAS_ON)
4803 snd_soc_dapm_set_bias_level(dapm,
4804 SND_SOC_BIAS_PREPARE);
4805 dapm_seq_run(card, &down_list, 0, false);
4806 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4807 snd_soc_dapm_set_bias_level(dapm,
4808 SND_SOC_BIAS_STANDBY);
4811 mutex_unlock(&card->dapm_mutex);
4815 * snd_soc_dapm_shutdown - callback for system shutdown
4817 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4819 struct snd_soc_dapm_context *dapm;
4821 for_each_card_dapms(card, dapm) {
4822 if (dapm != &card->dapm) {
4823 soc_dapm_shutdown_dapm(dapm);
4824 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4825 snd_soc_dapm_set_bias_level(dapm,
4826 SND_SOC_BIAS_OFF);
4830 soc_dapm_shutdown_dapm(&card->dapm);
4831 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4832 snd_soc_dapm_set_bias_level(&card->dapm,
4833 SND_SOC_BIAS_OFF);
4836 /* Module information */
4837 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4838 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4839 MODULE_LICENSE("GPL");