drm/panthor: Don't add write fences to the shared BOs
[drm/drm-misc.git] / sound / soc / soc-dapm.c
blob9330f1a3f7589dc467c04238830f2009a619a998
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/cleanup.h>
24 #include <linux/delay.h>
25 #include <linux/pm.h>
26 #include <linux/bitops.h>
27 #include <linux/platform_device.h>
28 #include <linux/jiffies.h>
29 #include <linux/debugfs.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/pinctrl/consumer.h>
33 #include <linux/clk.h>
34 #include <linux/slab.h>
35 #include <sound/core.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
39 #include <sound/initval.h>
41 #include <trace/events/asoc.h>
43 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
45 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
46 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
48 #define snd_soc_dapm_for_each_direction(dir) \
49 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
50 (dir)++)
52 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
53 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
54 const char *control,
55 int (*connected)(struct snd_soc_dapm_widget *source,
56 struct snd_soc_dapm_widget *sink));
58 struct snd_soc_dapm_widget *
59 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
60 const struct snd_soc_dapm_widget *widget);
62 struct snd_soc_dapm_widget *
63 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
64 const struct snd_soc_dapm_widget *widget);
66 static unsigned int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg);
68 /* dapm power sequences - make this per codec in the future */
69 static int dapm_up_seq[] = {
70 [snd_soc_dapm_pre] = 1,
71 [snd_soc_dapm_regulator_supply] = 2,
72 [snd_soc_dapm_pinctrl] = 2,
73 [snd_soc_dapm_clock_supply] = 2,
74 [snd_soc_dapm_supply] = 3,
75 [snd_soc_dapm_dai_link] = 3,
76 [snd_soc_dapm_micbias] = 4,
77 [snd_soc_dapm_vmid] = 4,
78 [snd_soc_dapm_dai_in] = 5,
79 [snd_soc_dapm_dai_out] = 5,
80 [snd_soc_dapm_aif_in] = 5,
81 [snd_soc_dapm_aif_out] = 5,
82 [snd_soc_dapm_mic] = 6,
83 [snd_soc_dapm_siggen] = 6,
84 [snd_soc_dapm_input] = 6,
85 [snd_soc_dapm_output] = 6,
86 [snd_soc_dapm_mux] = 7,
87 [snd_soc_dapm_demux] = 7,
88 [snd_soc_dapm_dac] = 8,
89 [snd_soc_dapm_switch] = 9,
90 [snd_soc_dapm_mixer] = 9,
91 [snd_soc_dapm_mixer_named_ctl] = 9,
92 [snd_soc_dapm_pga] = 10,
93 [snd_soc_dapm_buffer] = 10,
94 [snd_soc_dapm_scheduler] = 10,
95 [snd_soc_dapm_effect] = 10,
96 [snd_soc_dapm_src] = 10,
97 [snd_soc_dapm_asrc] = 10,
98 [snd_soc_dapm_encoder] = 10,
99 [snd_soc_dapm_decoder] = 10,
100 [snd_soc_dapm_adc] = 11,
101 [snd_soc_dapm_out_drv] = 12,
102 [snd_soc_dapm_hp] = 12,
103 [snd_soc_dapm_line] = 12,
104 [snd_soc_dapm_sink] = 12,
105 [snd_soc_dapm_spk] = 13,
106 [snd_soc_dapm_kcontrol] = 14,
107 [snd_soc_dapm_post] = 15,
110 static int dapm_down_seq[] = {
111 [snd_soc_dapm_pre] = 1,
112 [snd_soc_dapm_kcontrol] = 2,
113 [snd_soc_dapm_adc] = 3,
114 [snd_soc_dapm_spk] = 4,
115 [snd_soc_dapm_hp] = 5,
116 [snd_soc_dapm_line] = 5,
117 [snd_soc_dapm_out_drv] = 5,
118 [snd_soc_dapm_sink] = 6,
119 [snd_soc_dapm_pga] = 6,
120 [snd_soc_dapm_buffer] = 6,
121 [snd_soc_dapm_scheduler] = 6,
122 [snd_soc_dapm_effect] = 6,
123 [snd_soc_dapm_src] = 6,
124 [snd_soc_dapm_asrc] = 6,
125 [snd_soc_dapm_encoder] = 6,
126 [snd_soc_dapm_decoder] = 6,
127 [snd_soc_dapm_switch] = 7,
128 [snd_soc_dapm_mixer_named_ctl] = 7,
129 [snd_soc_dapm_mixer] = 7,
130 [snd_soc_dapm_dac] = 8,
131 [snd_soc_dapm_mic] = 9,
132 [snd_soc_dapm_siggen] = 9,
133 [snd_soc_dapm_input] = 9,
134 [snd_soc_dapm_output] = 9,
135 [snd_soc_dapm_micbias] = 10,
136 [snd_soc_dapm_vmid] = 10,
137 [snd_soc_dapm_mux] = 11,
138 [snd_soc_dapm_demux] = 11,
139 [snd_soc_dapm_aif_in] = 12,
140 [snd_soc_dapm_aif_out] = 12,
141 [snd_soc_dapm_dai_in] = 12,
142 [snd_soc_dapm_dai_out] = 12,
143 [snd_soc_dapm_dai_link] = 13,
144 [snd_soc_dapm_supply] = 14,
145 [snd_soc_dapm_clock_supply] = 15,
146 [snd_soc_dapm_pinctrl] = 15,
147 [snd_soc_dapm_regulator_supply] = 15,
148 [snd_soc_dapm_post] = 16,
151 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
153 if (snd_soc_card_is_instantiated(dapm->card))
154 snd_soc_dapm_mutex_assert_held(dapm);
157 static void pop_wait(u32 pop_time)
159 if (pop_time)
160 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
163 __printf(3, 4)
164 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
166 va_list args;
167 char *buf;
169 if (!pop_time)
170 return;
172 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
173 if (buf == NULL)
174 return;
176 va_start(args, fmt);
177 vsnprintf(buf, PAGE_SIZE, fmt, args);
178 dev_info(dev, "%s", buf);
179 va_end(args);
181 kfree(buf);
184 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
186 return !list_empty(&w->dirty);
189 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
191 dapm_assert_locked(w->dapm);
193 if (!dapm_dirty_widget(w)) {
194 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
195 w->name, reason);
196 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
201 * Common implementation for dapm_widget_invalidate_input_paths() and
202 * dapm_widget_invalidate_output_paths(). The function is inlined since the
203 * combined size of the two specialized functions is only marginally larger then
204 * the size of the generic function and at the same time the fast path of the
205 * specialized functions is significantly smaller than the generic function.
207 static __always_inline void dapm_widget_invalidate_paths(
208 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
210 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
211 struct snd_soc_dapm_widget *node;
212 struct snd_soc_dapm_path *p;
213 LIST_HEAD(list);
215 dapm_assert_locked(w->dapm);
217 if (w->endpoints[dir] == -1)
218 return;
220 list_add_tail(&w->work_list, &list);
221 w->endpoints[dir] = -1;
223 list_for_each_entry(w, &list, work_list) {
224 snd_soc_dapm_widget_for_each_path(w, dir, p) {
225 if (p->is_supply || p->weak || !p->connect)
226 continue;
227 node = p->node[rdir];
228 if (node->endpoints[dir] != -1) {
229 node->endpoints[dir] = -1;
230 list_add_tail(&node->work_list, &list);
237 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
238 * input paths
239 * @w: The widget for which to invalidate the cached number of input paths
241 * Resets the cached number of inputs for the specified widget and all widgets
242 * that can be reached via outcoming paths from the widget.
244 * This function must be called if the number of output paths for a widget might
245 * have changed. E.g. if the source state of a widget changes or a path is added
246 * or activated with the widget as the sink.
248 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
250 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
254 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
255 * output paths
256 * @w: The widget for which to invalidate the cached number of output paths
258 * Resets the cached number of outputs for the specified widget and all widgets
259 * that can be reached via incoming paths from the widget.
261 * This function must be called if the number of output paths for a widget might
262 * have changed. E.g. if the sink state of a widget changes or a path is added
263 * or activated with the widget as the source.
265 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
267 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
271 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
272 * for the widgets connected to a path
273 * @p: The path to invalidate
275 * Resets the cached number of inputs for the sink of the path and the cached
276 * number of outputs for the source of the path.
278 * This function must be called when a path is added, removed or the connected
279 * state changes.
281 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
284 * Weak paths or supply paths do not influence the number of input or
285 * output paths of their neighbors.
287 if (p->weak || p->is_supply)
288 return;
291 * The number of connected endpoints is the sum of the number of
292 * connected endpoints of all neighbors. If a node with 0 connected
293 * endpoints is either connected or disconnected that sum won't change,
294 * so there is no need to re-check the path.
296 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
297 dapm_widget_invalidate_input_paths(p->sink);
298 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
299 dapm_widget_invalidate_output_paths(p->source);
302 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
304 struct snd_soc_dapm_widget *w;
306 snd_soc_dapm_mutex_lock_root(card);
308 for_each_card_widgets(card, w) {
309 if (w->is_ep) {
310 dapm_mark_dirty(w, "Rechecking endpoints");
311 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
312 dapm_widget_invalidate_output_paths(w);
313 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
314 dapm_widget_invalidate_input_paths(w);
318 snd_soc_dapm_mutex_unlock(card);
320 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
322 /* create a new dapm widget */
323 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
324 const struct snd_soc_dapm_widget *_widget,
325 const char *prefix)
327 struct snd_soc_dapm_widget *w __free(kfree) = kmemdup(_widget,
328 sizeof(*_widget),
329 GFP_KERNEL);
330 if (!w)
331 return NULL;
333 if (prefix)
334 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, _widget->name);
335 else
336 w->name = kstrdup_const(_widget->name, GFP_KERNEL);
337 if (!w->name)
338 return NULL;
340 if (_widget->sname) {
341 w->sname = kstrdup_const(_widget->sname, GFP_KERNEL);
342 if (!w->sname) {
343 kfree_const(w->name);
344 return NULL;
348 return_ptr(w);
351 struct dapm_kcontrol_data {
352 unsigned int value;
353 struct snd_soc_dapm_widget *widget;
354 struct list_head paths;
355 struct snd_soc_dapm_widget_list *wlist;
358 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
359 struct snd_kcontrol *kcontrol, const char *ctrl_name)
361 struct dapm_kcontrol_data *data;
362 struct soc_mixer_control *mc;
363 struct soc_enum *e;
364 const char *name;
365 int ret;
367 data = kzalloc(sizeof(*data), GFP_KERNEL);
368 if (!data)
369 return -ENOMEM;
371 INIT_LIST_HEAD(&data->paths);
373 switch (widget->id) {
374 case snd_soc_dapm_switch:
375 case snd_soc_dapm_mixer:
376 case snd_soc_dapm_mixer_named_ctl:
377 mc = (struct soc_mixer_control *)kcontrol->private_value;
379 if (mc->autodisable) {
380 struct snd_soc_dapm_widget template;
382 if (snd_soc_volsw_is_stereo(mc))
383 dev_warn(widget->dapm->dev,
384 "ASoC: Unsupported stereo autodisable control '%s'\n",
385 ctrl_name);
387 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
388 "Autodisable");
389 if (!name) {
390 ret = -ENOMEM;
391 goto err_data;
394 memset(&template, 0, sizeof(template));
395 template.reg = mc->reg;
396 template.mask = (1 << fls(mc->max)) - 1;
397 template.shift = mc->shift;
398 if (mc->invert)
399 template.off_val = mc->max;
400 else
401 template.off_val = 0;
402 template.on_val = template.off_val;
403 template.id = snd_soc_dapm_kcontrol;
404 template.name = name;
406 data->value = template.on_val;
408 data->widget =
409 snd_soc_dapm_new_control_unlocked(widget->dapm,
410 &template);
411 kfree(name);
412 if (IS_ERR(data->widget)) {
413 ret = PTR_ERR(data->widget);
414 goto err_data;
417 break;
418 case snd_soc_dapm_demux:
419 case snd_soc_dapm_mux:
420 e = (struct soc_enum *)kcontrol->private_value;
422 if (e->autodisable) {
423 struct snd_soc_dapm_widget template;
425 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
426 "Autodisable");
427 if (!name) {
428 ret = -ENOMEM;
429 goto err_data;
432 memset(&template, 0, sizeof(template));
433 template.reg = e->reg;
434 template.mask = e->mask;
435 template.shift = e->shift_l;
436 template.off_val = snd_soc_enum_item_to_val(e, 0);
437 template.on_val = template.off_val;
438 template.id = snd_soc_dapm_kcontrol;
439 template.name = name;
441 data->value = template.on_val;
443 data->widget = snd_soc_dapm_new_control_unlocked(
444 widget->dapm, &template);
445 kfree(name);
446 if (IS_ERR(data->widget)) {
447 ret = PTR_ERR(data->widget);
448 goto err_data;
451 snd_soc_dapm_add_path(widget->dapm, data->widget,
452 widget, NULL, NULL);
453 } else if (e->reg != SND_SOC_NOPM) {
454 data->value = soc_dapm_read(widget->dapm, e->reg) &
455 (e->mask << e->shift_l);
457 break;
458 default:
459 break;
462 kcontrol->private_data = data;
464 return 0;
466 err_data:
467 kfree(data);
468 return ret;
471 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
473 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
475 list_del(&data->paths);
476 kfree(data->wlist);
477 kfree(data);
480 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
481 const struct snd_kcontrol *kcontrol)
483 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
485 return data->wlist;
488 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
489 struct snd_soc_dapm_widget *widget)
491 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
492 struct snd_soc_dapm_widget_list *new_wlist;
493 unsigned int n;
495 if (data->wlist)
496 n = data->wlist->num_widgets + 1;
497 else
498 n = 1;
500 new_wlist = krealloc(data->wlist,
501 struct_size(new_wlist, widgets, n),
502 GFP_KERNEL);
503 if (!new_wlist)
504 return -ENOMEM;
506 new_wlist->num_widgets = n;
507 new_wlist->widgets[n - 1] = widget;
509 data->wlist = new_wlist;
511 return 0;
514 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
515 struct snd_soc_dapm_path *path)
517 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
519 list_add_tail(&path->list_kcontrol, &data->paths);
522 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
524 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
526 if (!data->widget)
527 return true;
529 return data->widget->power;
532 static struct list_head *dapm_kcontrol_get_path_list(
533 const struct snd_kcontrol *kcontrol)
535 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
537 return &data->paths;
540 #define dapm_kcontrol_for_each_path(path, kcontrol) \
541 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
542 list_kcontrol)
544 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
546 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
548 return data->value;
550 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
552 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
553 unsigned int value)
555 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
557 if (data->value == value)
558 return false;
560 if (data->widget) {
561 switch (dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->id) {
562 case snd_soc_dapm_switch:
563 case snd_soc_dapm_mixer:
564 case snd_soc_dapm_mixer_named_ctl:
565 data->widget->on_val = value & data->widget->mask;
566 break;
567 case snd_soc_dapm_demux:
568 case snd_soc_dapm_mux:
569 data->widget->on_val = value >> data->widget->shift;
570 break;
571 default:
572 data->widget->on_val = value;
573 break;
577 data->value = value;
579 return true;
583 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
584 * kcontrol
585 * @kcontrol: The kcontrol
587 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
588 struct snd_kcontrol *kcontrol)
590 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
592 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
595 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
596 * kcontrol
597 * @kcontrol: The kcontrol
599 * Note: This function must only be used on kcontrols that are known to have
600 * been registered for a CODEC. Otherwise the behaviour is undefined.
602 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
603 struct snd_kcontrol *kcontrol)
605 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
607 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
609 static void dapm_reset(struct snd_soc_card *card)
611 struct snd_soc_dapm_widget *w;
613 snd_soc_dapm_mutex_assert_held(card);
615 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
617 for_each_card_widgets(card, w) {
618 w->new_power = w->power;
619 w->power_checked = false;
623 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
625 if (!dapm->component)
626 return NULL;
627 return dapm->component->name_prefix;
630 static unsigned int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg)
632 if (!dapm->component)
633 return -EIO;
634 return snd_soc_component_read(dapm->component, reg);
637 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
638 int reg, unsigned int mask, unsigned int value)
640 if (!dapm->component)
641 return -EIO;
642 return snd_soc_component_update_bits(dapm->component, reg,
643 mask, value);
646 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
647 int reg, unsigned int mask, unsigned int value)
649 if (!dapm->component)
650 return -EIO;
651 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
654 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
656 if (dapm->component)
657 snd_soc_component_async_complete(dapm->component);
660 static struct snd_soc_dapm_widget *
661 dapm_wcache_lookup(struct snd_soc_dapm_widget *w, const char *name)
663 if (w) {
664 struct list_head *wlist = &w->dapm->card->widgets;
665 const int depth = 2;
666 int i = 0;
668 list_for_each_entry_from(w, wlist, list) {
669 if (!strcmp(name, w->name))
670 return w;
672 if (++i == depth)
673 break;
677 return NULL;
681 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
682 * @dapm: The DAPM context for which to set the level
683 * @level: The level to set
685 * Forces the DAPM bias level to a specific state. It will call the bias level
686 * callback of DAPM context with the specified level. This will even happen if
687 * the context is already at the same level. Furthermore it will not go through
688 * the normal bias level sequencing, meaning any intermediate states between the
689 * current and the target state will not be entered.
691 * Note that the change in bias level is only temporary and the next time
692 * snd_soc_dapm_sync() is called the state will be set to the level as
693 * determined by the DAPM core. The function is mainly intended to be used to
694 * used during probe or resume from suspend to power up the device so
695 * initialization can be done, before the DAPM core takes over.
697 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
698 enum snd_soc_bias_level level)
700 int ret = 0;
702 if (dapm->component)
703 ret = snd_soc_component_set_bias_level(dapm->component, level);
705 if (ret == 0)
706 dapm->bias_level = level;
708 return ret;
710 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
713 * snd_soc_dapm_set_bias_level - set the bias level for the system
714 * @dapm: DAPM context
715 * @level: level to configure
717 * Configure the bias (power) levels for the SoC audio device.
719 * Returns 0 for success else error.
721 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
722 enum snd_soc_bias_level level)
724 struct snd_soc_card *card = dapm->card;
725 int ret = 0;
727 trace_snd_soc_bias_level_start(dapm, level);
729 ret = snd_soc_card_set_bias_level(card, dapm, level);
730 if (ret != 0)
731 goto out;
733 if (!card || dapm != &card->dapm)
734 ret = snd_soc_dapm_force_bias_level(dapm, level);
736 if (ret != 0)
737 goto out;
739 ret = snd_soc_card_set_bias_level_post(card, dapm, level);
740 out:
741 trace_snd_soc_bias_level_done(dapm, level);
743 return ret;
746 /* connect mux widget to its interconnecting audio paths */
747 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
748 struct snd_soc_dapm_path *path, const char *control_name,
749 struct snd_soc_dapm_widget *w)
751 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
752 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
753 unsigned int item;
754 int i;
756 if (e->reg != SND_SOC_NOPM) {
757 unsigned int val;
758 val = soc_dapm_read(dapm, e->reg);
759 val = (val >> e->shift_l) & e->mask;
760 item = snd_soc_enum_val_to_item(e, val);
761 } else {
762 /* since a virtual mux has no backing registers to
763 * decide which path to connect, it will try to match
764 * with the first enumeration. This is to ensure
765 * that the default mux choice (the first) will be
766 * correctly powered up during initialization.
768 item = 0;
771 i = match_string(e->texts, e->items, control_name);
772 if (i < 0)
773 return -ENODEV;
775 path->name = e->texts[i];
776 path->connect = (i == item);
777 return 0;
781 /* set up initial codec paths */
782 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
783 int nth_path)
785 struct soc_mixer_control *mc = (struct soc_mixer_control *)
786 p->sink->kcontrol_news[i].private_value;
787 unsigned int reg = mc->reg;
788 unsigned int invert = mc->invert;
790 if (reg != SND_SOC_NOPM) {
791 unsigned int shift = mc->shift;
792 unsigned int max = mc->max;
793 unsigned int mask = (1 << fls(max)) - 1;
794 unsigned int val = soc_dapm_read(p->sink->dapm, reg);
797 * The nth_path argument allows this function to know
798 * which path of a kcontrol it is setting the initial
799 * status for. Ideally this would support any number
800 * of paths and channels. But since kcontrols only come
801 * in mono and stereo variants, we are limited to 2
802 * channels.
804 * The following code assumes for stereo controls the
805 * first path is the left channel, and all remaining
806 * paths are the right channel.
808 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
809 if (reg != mc->rreg)
810 val = soc_dapm_read(p->sink->dapm, mc->rreg);
811 val = (val >> mc->rshift) & mask;
812 } else {
813 val = (val >> shift) & mask;
815 if (invert)
816 val = max - val;
817 p->connect = !!val;
818 } else {
819 /* since a virtual mixer has no backing registers to
820 * decide which path to connect, it will try to match
821 * with initial state. This is to ensure
822 * that the default mixer choice will be
823 * correctly powered up during initialization.
825 p->connect = invert;
829 /* connect mixer widget to its interconnecting audio paths */
830 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
831 struct snd_soc_dapm_path *path, const char *control_name)
833 int i, nth_path = 0;
835 /* search for mixer kcontrol */
836 for (i = 0; i < path->sink->num_kcontrols; i++) {
837 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
838 path->name = path->sink->kcontrol_news[i].name;
839 dapm_set_mixer_path_status(path, i, nth_path++);
840 return 0;
843 return -ENODEV;
846 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
847 struct snd_soc_dapm_widget *kcontrolw,
848 const struct snd_kcontrol_new *kcontrol_new,
849 struct snd_kcontrol **kcontrol)
851 struct snd_soc_dapm_widget *w;
852 int i;
854 *kcontrol = NULL;
856 for_each_card_widgets(dapm->card, w) {
857 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
858 continue;
859 for (i = 0; i < w->num_kcontrols; i++) {
860 if (&w->kcontrol_news[i] == kcontrol_new) {
861 if (w->kcontrols)
862 *kcontrol = w->kcontrols[i];
863 return 1;
868 return 0;
872 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
873 * create it. Either way, add the widget into the control's widget list
875 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
876 int kci)
878 struct snd_soc_dapm_context *dapm = w->dapm;
879 struct snd_card *card = dapm->card->snd_card;
880 const char *prefix;
881 size_t prefix_len;
882 int shared;
883 struct snd_kcontrol *kcontrol;
884 bool wname_in_long_name, kcname_in_long_name;
885 char *long_name = NULL;
886 const char *name;
887 int ret = 0;
889 prefix = soc_dapm_prefix(dapm);
890 if (prefix)
891 prefix_len = strlen(prefix) + 1;
892 else
893 prefix_len = 0;
895 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
896 &kcontrol);
898 if (!kcontrol) {
899 if (shared) {
900 wname_in_long_name = false;
901 kcname_in_long_name = true;
902 } else {
903 switch (w->id) {
904 case snd_soc_dapm_switch:
905 case snd_soc_dapm_mixer:
906 case snd_soc_dapm_pga:
907 case snd_soc_dapm_effect:
908 case snd_soc_dapm_out_drv:
909 wname_in_long_name = true;
910 kcname_in_long_name = true;
911 break;
912 case snd_soc_dapm_mixer_named_ctl:
913 wname_in_long_name = false;
914 kcname_in_long_name = true;
915 break;
916 case snd_soc_dapm_demux:
917 case snd_soc_dapm_mux:
918 wname_in_long_name = true;
919 kcname_in_long_name = false;
920 break;
921 default:
922 return -EINVAL;
925 if (w->no_wname_in_kcontrol_name)
926 wname_in_long_name = false;
928 if (wname_in_long_name && kcname_in_long_name) {
930 * The control will get a prefix from the control
931 * creation process but we're also using the same
932 * prefix for widgets so cut the prefix off the
933 * front of the widget name.
935 long_name = kasprintf(GFP_KERNEL, "%s %s",
936 w->name + prefix_len,
937 w->kcontrol_news[kci].name);
938 if (long_name == NULL)
939 return -ENOMEM;
941 name = long_name;
942 } else if (wname_in_long_name) {
943 long_name = NULL;
944 name = w->name + prefix_len;
945 } else {
946 long_name = NULL;
947 name = w->kcontrol_news[kci].name;
950 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
951 prefix);
952 if (!kcontrol) {
953 ret = -ENOMEM;
954 goto exit_free;
957 kcontrol->private_free = dapm_kcontrol_free;
959 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
960 if (ret) {
961 snd_ctl_free_one(kcontrol);
962 goto exit_free;
965 ret = snd_ctl_add(card, kcontrol);
966 if (ret < 0) {
967 dev_err(dapm->dev,
968 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
969 w->name, name, ret);
970 goto exit_free;
974 ret = dapm_kcontrol_add_widget(kcontrol, w);
975 if (ret == 0)
976 w->kcontrols[kci] = kcontrol;
978 exit_free:
979 kfree(long_name);
981 return ret;
984 /* create new dapm mixer control */
985 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
987 int i, ret;
988 struct snd_soc_dapm_path *path;
989 struct dapm_kcontrol_data *data;
991 /* add kcontrol */
992 for (i = 0; i < w->num_kcontrols; i++) {
993 /* match name */
994 snd_soc_dapm_widget_for_each_source_path(w, path) {
995 /* mixer/mux paths name must match control name */
996 if (path->name != (char *)w->kcontrol_news[i].name)
997 continue;
999 if (!w->kcontrols[i]) {
1000 ret = dapm_create_or_share_kcontrol(w, i);
1001 if (ret < 0)
1002 return ret;
1005 dapm_kcontrol_add_path(w->kcontrols[i], path);
1007 data = snd_kcontrol_chip(w->kcontrols[i]);
1008 if (data->widget)
1009 snd_soc_dapm_add_path(data->widget->dapm,
1010 data->widget,
1011 path->source,
1012 NULL, NULL);
1016 return 0;
1019 /* create new dapm mux control */
1020 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
1022 struct snd_soc_dapm_context *dapm = w->dapm;
1023 enum snd_soc_dapm_direction dir;
1024 struct snd_soc_dapm_path *path;
1025 const char *type;
1026 int ret;
1028 switch (w->id) {
1029 case snd_soc_dapm_mux:
1030 dir = SND_SOC_DAPM_DIR_OUT;
1031 type = "mux";
1032 break;
1033 case snd_soc_dapm_demux:
1034 dir = SND_SOC_DAPM_DIR_IN;
1035 type = "demux";
1036 break;
1037 default:
1038 return -EINVAL;
1041 if (w->num_kcontrols != 1) {
1042 dev_err(dapm->dev,
1043 "ASoC: %s %s has incorrect number of controls\n", type,
1044 w->name);
1045 return -EINVAL;
1048 if (list_empty(&w->edges[dir])) {
1049 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1050 return -EINVAL;
1053 ret = dapm_create_or_share_kcontrol(w, 0);
1054 if (ret < 0)
1055 return ret;
1057 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1058 if (path->name)
1059 dapm_kcontrol_add_path(w->kcontrols[0], path);
1062 return 0;
1065 /* create new dapm volume control */
1066 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1068 int i;
1070 for (i = 0; i < w->num_kcontrols; i++) {
1071 int ret = dapm_create_or_share_kcontrol(w, i);
1072 if (ret < 0)
1073 return ret;
1076 return 0;
1079 /* create new dapm dai link control */
1080 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1082 int i;
1083 struct snd_soc_pcm_runtime *rtd = w->priv;
1085 /* create control for links with > 1 config */
1086 if (rtd->dai_link->num_c2c_params <= 1)
1087 return 0;
1089 /* add kcontrol */
1090 for (i = 0; i < w->num_kcontrols; i++) {
1091 struct snd_soc_dapm_context *dapm = w->dapm;
1092 struct snd_card *card = dapm->card->snd_card;
1093 struct snd_kcontrol *kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
1094 w, w->name, NULL);
1095 int ret = snd_ctl_add(card, kcontrol);
1097 if (ret < 0) {
1098 dev_err(dapm->dev,
1099 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1100 w->name, w->kcontrol_news[i].name, ret);
1101 return ret;
1103 kcontrol->private_data = w;
1104 w->kcontrols[i] = kcontrol;
1107 return 0;
1110 /* We implement power down on suspend by checking the power state of
1111 * the ALSA card - when we are suspending the ALSA state for the card
1112 * is set to D3.
1114 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1116 int level = snd_power_get_state(widget->dapm->card->snd_card);
1118 switch (level) {
1119 case SNDRV_CTL_POWER_D3hot:
1120 case SNDRV_CTL_POWER_D3cold:
1121 if (widget->ignore_suspend)
1122 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1123 widget->name);
1124 return widget->ignore_suspend;
1125 default:
1126 return 1;
1130 static void dapm_widget_list_free(struct snd_soc_dapm_widget_list **list)
1132 kfree(*list);
1135 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1136 struct list_head *widgets)
1138 struct snd_soc_dapm_widget *w;
1139 struct list_head *it;
1140 unsigned int size = 0;
1141 unsigned int i = 0;
1143 list_for_each(it, widgets)
1144 size++;
1146 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1147 if (*list == NULL)
1148 return -ENOMEM;
1150 list_for_each_entry(w, widgets, work_list)
1151 (*list)->widgets[i++] = w;
1153 (*list)->num_widgets = i;
1155 return 0;
1159 * Recursively reset the cached number of inputs or outputs for the specified
1160 * widget and all widgets that can be reached via incoming or outcoming paths
1161 * from the widget.
1163 static void invalidate_paths_ep(struct snd_soc_dapm_widget *widget,
1164 enum snd_soc_dapm_direction dir)
1166 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1167 struct snd_soc_dapm_path *path;
1169 widget->endpoints[dir] = -1;
1171 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1172 if (path->weak || path->is_supply)
1173 continue;
1175 if (path->walking)
1176 return;
1178 if (path->connect) {
1179 path->walking = 1;
1180 invalidate_paths_ep(path->node[dir], dir);
1181 path->walking = 0;
1187 * Common implementation for is_connected_output_ep() and
1188 * is_connected_input_ep(). The function is inlined since the combined size of
1189 * the two specialized functions is only marginally larger then the size of the
1190 * generic function and at the same time the fast path of the specialized
1191 * functions is significantly smaller than the generic function.
1193 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1194 struct list_head *list, enum snd_soc_dapm_direction dir,
1195 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1196 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1197 enum snd_soc_dapm_direction)),
1198 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1199 enum snd_soc_dapm_direction))
1201 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1202 struct snd_soc_dapm_path *path;
1203 int con = 0;
1205 if (widget->endpoints[dir] >= 0)
1206 return widget->endpoints[dir];
1208 DAPM_UPDATE_STAT(widget, path_checks);
1210 /* do we need to add this widget to the list ? */
1211 if (list)
1212 list_add_tail(&widget->work_list, list);
1214 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1215 list = NULL;
1216 custom_stop_condition = NULL;
1219 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1220 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1221 return widget->endpoints[dir];
1224 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1225 DAPM_UPDATE_STAT(widget, neighbour_checks);
1227 if (path->weak || path->is_supply)
1228 continue;
1230 if (path->walking)
1231 return 1;
1233 trace_snd_soc_dapm_path(widget, dir, path);
1235 if (path->connect) {
1236 path->walking = 1;
1237 con += fn(path->node[dir], list, custom_stop_condition);
1238 path->walking = 0;
1242 widget->endpoints[dir] = con;
1244 return con;
1248 * Recursively check for a completed path to an active or physically connected
1249 * output widget. Returns number of complete paths.
1251 * Optionally, can be supplied with a function acting as a stopping condition.
1252 * This function takes the dapm widget currently being examined and the walk
1253 * direction as an arguments, it should return true if widgets from that point
1254 * in the graph onwards should not be added to the widget list.
1256 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1257 struct list_head *list,
1258 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1259 enum snd_soc_dapm_direction))
1261 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1262 is_connected_output_ep, custom_stop_condition);
1266 * Recursively check for a completed path to an active or physically connected
1267 * input widget. Returns number of complete paths.
1269 * Optionally, can be supplied with a function acting as a stopping condition.
1270 * This function takes the dapm widget currently being examined and the walk
1271 * direction as an arguments, it should return true if the walk should be
1272 * stopped and false otherwise.
1274 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1275 struct list_head *list,
1276 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1277 enum snd_soc_dapm_direction))
1279 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1280 is_connected_input_ep, custom_stop_condition);
1284 * snd_soc_dapm_dai_get_connected_widgets - query audio path and it's widgets.
1285 * @dai: the soc DAI.
1286 * @stream: stream direction.
1287 * @list: list of active widgets for this stream.
1288 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1289 * walk based on custom logic.
1291 * Queries DAPM graph as to whether a valid audio stream path exists for
1292 * the initial stream specified by name. This takes into account
1293 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1295 * Optionally, can be supplied with a function acting as a stopping condition.
1296 * This function takes the dapm widget currently being examined and the walk
1297 * direction as an arguments, it should return true if the walk should be
1298 * stopped and false otherwise.
1300 * Returns the number of valid paths or negative error.
1302 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1303 struct snd_soc_dapm_widget_list **list,
1304 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1305 enum snd_soc_dapm_direction))
1307 struct snd_soc_card *card = dai->component->card;
1308 struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(dai, stream);
1309 LIST_HEAD(widgets);
1310 int paths;
1311 int ret;
1313 snd_soc_dapm_mutex_lock(card);
1315 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1316 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_OUT);
1317 paths = is_connected_output_ep(w, &widgets,
1318 custom_stop_condition);
1319 } else {
1320 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_IN);
1321 paths = is_connected_input_ep(w, &widgets,
1322 custom_stop_condition);
1325 /* Drop starting point */
1326 list_del(widgets.next);
1328 ret = dapm_widget_list_create(list, &widgets);
1329 if (ret)
1330 paths = ret;
1332 trace_snd_soc_dapm_connected(paths, stream);
1333 snd_soc_dapm_mutex_unlock(card);
1335 return paths;
1337 EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_get_connected_widgets);
1339 void snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list **list)
1341 dapm_widget_list_free(list);
1343 EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_free_widgets);
1346 * Handler for regulator supply widget.
1348 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1349 struct snd_kcontrol *kcontrol, int event)
1351 int ret;
1353 soc_dapm_async_complete(w->dapm);
1355 if (SND_SOC_DAPM_EVENT_ON(event)) {
1356 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1357 ret = regulator_allow_bypass(w->regulator, false);
1358 if (ret != 0)
1359 dev_warn(w->dapm->dev,
1360 "ASoC: Failed to unbypass %s: %d\n",
1361 w->name, ret);
1364 return regulator_enable(w->regulator);
1365 } else {
1366 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1367 ret = regulator_allow_bypass(w->regulator, true);
1368 if (ret != 0)
1369 dev_warn(w->dapm->dev,
1370 "ASoC: Failed to bypass %s: %d\n",
1371 w->name, ret);
1374 return regulator_disable_deferred(w->regulator, w->shift);
1377 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1380 * Handler for pinctrl widget.
1382 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1383 struct snd_kcontrol *kcontrol, int event)
1385 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1386 struct pinctrl *p = w->pinctrl;
1387 struct pinctrl_state *s;
1389 if (!p || !priv)
1390 return -EIO;
1392 if (SND_SOC_DAPM_EVENT_ON(event))
1393 s = pinctrl_lookup_state(p, priv->active_state);
1394 else
1395 s = pinctrl_lookup_state(p, priv->sleep_state);
1397 if (IS_ERR(s))
1398 return PTR_ERR(s);
1400 return pinctrl_select_state(p, s);
1402 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1405 * Handler for clock supply widget.
1407 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1408 struct snd_kcontrol *kcontrol, int event)
1410 if (!w->clk)
1411 return -EIO;
1413 soc_dapm_async_complete(w->dapm);
1415 if (SND_SOC_DAPM_EVENT_ON(event)) {
1416 return clk_prepare_enable(w->clk);
1417 } else {
1418 clk_disable_unprepare(w->clk);
1419 return 0;
1422 return 0;
1424 EXPORT_SYMBOL_GPL(dapm_clock_event);
1426 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1428 if (w->power_checked)
1429 return w->new_power;
1431 if (w->force)
1432 w->new_power = 1;
1433 else
1434 w->new_power = w->power_check(w);
1436 w->power_checked = true;
1438 return w->new_power;
1441 /* Generic check to see if a widget should be powered. */
1442 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1444 int in, out;
1446 DAPM_UPDATE_STAT(w, power_checks);
1448 in = is_connected_input_ep(w, NULL, NULL);
1449 out = is_connected_output_ep(w, NULL, NULL);
1450 return out != 0 && in != 0;
1453 /* Check to see if a power supply is needed */
1454 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1456 struct snd_soc_dapm_path *path;
1458 DAPM_UPDATE_STAT(w, power_checks);
1460 /* Check if one of our outputs is connected */
1461 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1462 DAPM_UPDATE_STAT(w, neighbour_checks);
1464 if (path->weak)
1465 continue;
1467 if (path->connected &&
1468 !path->connected(path->source, path->sink))
1469 continue;
1471 if (dapm_widget_power_check(path->sink))
1472 return 1;
1475 return 0;
1478 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1480 return w->connected;
1483 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1484 struct snd_soc_dapm_widget *b,
1485 bool power_up)
1487 int *sort;
1489 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1490 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1492 if (power_up)
1493 sort = dapm_up_seq;
1494 else
1495 sort = dapm_down_seq;
1497 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1498 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1500 if (sort[a->id] != sort[b->id])
1501 return sort[a->id] - sort[b->id];
1502 if (a->subseq != b->subseq) {
1503 if (power_up)
1504 return a->subseq - b->subseq;
1505 else
1506 return b->subseq - a->subseq;
1508 if (a->reg != b->reg)
1509 return a->reg - b->reg;
1510 if (a->dapm != b->dapm)
1511 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1513 return 0;
1516 /* Insert a widget in order into a DAPM power sequence. */
1517 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1518 struct list_head *list,
1519 bool power_up)
1521 struct snd_soc_dapm_widget *w;
1523 list_for_each_entry(w, list, power_list)
1524 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1525 list_add_tail(&new_widget->power_list, &w->power_list);
1526 return;
1529 list_add_tail(&new_widget->power_list, list);
1532 static void dapm_seq_check_event(struct snd_soc_card *card,
1533 struct snd_soc_dapm_widget *w, int event)
1535 const char *ev_name;
1536 int power;
1538 switch (event) {
1539 case SND_SOC_DAPM_PRE_PMU:
1540 ev_name = "PRE_PMU";
1541 power = 1;
1542 break;
1543 case SND_SOC_DAPM_POST_PMU:
1544 ev_name = "POST_PMU";
1545 power = 1;
1546 break;
1547 case SND_SOC_DAPM_PRE_PMD:
1548 ev_name = "PRE_PMD";
1549 power = 0;
1550 break;
1551 case SND_SOC_DAPM_POST_PMD:
1552 ev_name = "POST_PMD";
1553 power = 0;
1554 break;
1555 case SND_SOC_DAPM_WILL_PMU:
1556 ev_name = "WILL_PMU";
1557 power = 1;
1558 break;
1559 case SND_SOC_DAPM_WILL_PMD:
1560 ev_name = "WILL_PMD";
1561 power = 0;
1562 break;
1563 default:
1564 WARN(1, "Unknown event %d\n", event);
1565 return;
1568 if (w->new_power != power)
1569 return;
1571 if (w->event && (w->event_flags & event)) {
1572 int ret;
1574 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1575 w->name, ev_name);
1576 soc_dapm_async_complete(w->dapm);
1577 trace_snd_soc_dapm_widget_event_start(w, event);
1578 ret = w->event(w, NULL, event);
1579 trace_snd_soc_dapm_widget_event_done(w, event);
1580 if (ret < 0)
1581 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1582 ev_name, w->name, ret);
1586 /* Apply the coalesced changes from a DAPM sequence */
1587 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1588 struct list_head *pending)
1590 struct snd_soc_dapm_context *dapm;
1591 struct snd_soc_dapm_widget *w;
1592 int reg;
1593 unsigned int value = 0;
1594 unsigned int mask = 0;
1596 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1597 reg = w->reg;
1598 dapm = w->dapm;
1600 list_for_each_entry(w, pending, power_list) {
1601 WARN_ON(reg != w->reg || dapm != w->dapm);
1602 w->power = w->new_power;
1604 mask |= w->mask << w->shift;
1605 if (w->power)
1606 value |= w->on_val << w->shift;
1607 else
1608 value |= w->off_val << w->shift;
1610 pop_dbg(dapm->dev, card->pop_time,
1611 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1612 w->name, reg, value, mask);
1614 /* Check for events */
1615 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1616 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1619 if (reg >= 0) {
1620 /* Any widget will do, they should all be updating the
1621 * same register.
1624 pop_dbg(dapm->dev, card->pop_time,
1625 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1626 value, mask, reg, card->pop_time);
1627 pop_wait(card->pop_time);
1628 soc_dapm_update_bits(dapm, reg, mask, value);
1631 list_for_each_entry(w, pending, power_list) {
1632 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1633 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1637 /* Apply a DAPM power sequence.
1639 * We walk over a pre-sorted list of widgets to apply power to. In
1640 * order to minimise the number of writes to the device required
1641 * multiple widgets will be updated in a single write where possible.
1642 * Currently anything that requires more than a single write is not
1643 * handled.
1645 static void dapm_seq_run(struct snd_soc_card *card,
1646 struct list_head *list, int event, bool power_up)
1648 struct snd_soc_dapm_widget *w, *n;
1649 struct snd_soc_dapm_context *d;
1650 LIST_HEAD(pending);
1651 int cur_sort = -1;
1652 int cur_subseq = -1;
1653 int cur_reg = SND_SOC_NOPM;
1654 struct snd_soc_dapm_context *cur_dapm = NULL;
1655 int i;
1656 int *sort;
1658 if (power_up)
1659 sort = dapm_up_seq;
1660 else
1661 sort = dapm_down_seq;
1663 list_for_each_entry_safe(w, n, list, power_list) {
1664 int ret = 0;
1666 /* Do we need to apply any queued changes? */
1667 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1668 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1669 if (!list_empty(&pending))
1670 dapm_seq_run_coalesced(card, &pending);
1672 if (cur_dapm && cur_dapm->component) {
1673 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1674 if (sort[i] == cur_sort)
1675 snd_soc_component_seq_notifier(
1676 cur_dapm->component,
1677 i, cur_subseq);
1680 if (cur_dapm && w->dapm != cur_dapm)
1681 soc_dapm_async_complete(cur_dapm);
1683 INIT_LIST_HEAD(&pending);
1684 cur_sort = -1;
1685 cur_subseq = INT_MIN;
1686 cur_reg = SND_SOC_NOPM;
1687 cur_dapm = NULL;
1690 switch (w->id) {
1691 case snd_soc_dapm_pre:
1692 if (!w->event)
1693 continue;
1695 if (event == SND_SOC_DAPM_STREAM_START)
1696 ret = w->event(w,
1697 NULL, SND_SOC_DAPM_PRE_PMU);
1698 else if (event == SND_SOC_DAPM_STREAM_STOP)
1699 ret = w->event(w,
1700 NULL, SND_SOC_DAPM_PRE_PMD);
1701 break;
1703 case snd_soc_dapm_post:
1704 if (!w->event)
1705 continue;
1707 if (event == SND_SOC_DAPM_STREAM_START)
1708 ret = w->event(w,
1709 NULL, SND_SOC_DAPM_POST_PMU);
1710 else if (event == SND_SOC_DAPM_STREAM_STOP)
1711 ret = w->event(w,
1712 NULL, SND_SOC_DAPM_POST_PMD);
1713 break;
1715 default:
1716 /* Queue it up for application */
1717 cur_sort = sort[w->id];
1718 cur_subseq = w->subseq;
1719 cur_reg = w->reg;
1720 cur_dapm = w->dapm;
1721 list_move(&w->power_list, &pending);
1722 break;
1725 if (ret < 0)
1726 dev_err(w->dapm->dev,
1727 "ASoC: Failed to apply widget power: %d\n", ret);
1730 if (!list_empty(&pending))
1731 dapm_seq_run_coalesced(card, &pending);
1733 if (cur_dapm && cur_dapm->component) {
1734 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1735 if (sort[i] == cur_sort)
1736 snd_soc_component_seq_notifier(
1737 cur_dapm->component,
1738 i, cur_subseq);
1741 for_each_card_dapms(card, d)
1742 soc_dapm_async_complete(d);
1745 static void dapm_widget_update(struct snd_soc_card *card)
1747 struct snd_soc_dapm_update *update = card->update;
1748 struct snd_soc_dapm_widget_list *wlist;
1749 struct snd_soc_dapm_widget *w = NULL;
1750 unsigned int wi;
1751 int ret;
1753 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1754 return;
1756 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1758 for_each_dapm_widgets(wlist, wi, w) {
1759 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1760 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1761 if (ret != 0)
1762 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1763 w->name, ret);
1767 if (!w)
1768 return;
1770 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1771 update->val);
1772 if (ret < 0)
1773 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1774 w->name, ret);
1776 if (update->has_second_set) {
1777 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1778 update->mask2, update->val2);
1779 if (ret < 0)
1780 dev_err(w->dapm->dev,
1781 "ASoC: %s DAPM update failed: %d\n",
1782 w->name, ret);
1785 for_each_dapm_widgets(wlist, wi, w) {
1786 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1787 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1788 if (ret != 0)
1789 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1790 w->name, ret);
1795 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1796 * they're changing state.
1798 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1800 struct snd_soc_dapm_context *d = data;
1801 int ret;
1803 /* If we're off and we're not supposed to go into STANDBY */
1804 if (d->bias_level == SND_SOC_BIAS_OFF &&
1805 d->target_bias_level != SND_SOC_BIAS_OFF) {
1806 if (d->dev && cookie)
1807 pm_runtime_get_sync(d->dev);
1809 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1810 if (ret != 0)
1811 dev_err(d->dev,
1812 "ASoC: Failed to turn on bias: %d\n", ret);
1815 /* Prepare for a transition to ON or away from ON */
1816 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1817 d->bias_level != SND_SOC_BIAS_ON) ||
1818 (d->target_bias_level != SND_SOC_BIAS_ON &&
1819 d->bias_level == SND_SOC_BIAS_ON)) {
1820 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1821 if (ret != 0)
1822 dev_err(d->dev,
1823 "ASoC: Failed to prepare bias: %d\n", ret);
1827 /* Async callback run prior to DAPM sequences - brings to their final
1828 * state.
1830 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1832 struct snd_soc_dapm_context *d = data;
1833 int ret;
1835 /* If we just powered the last thing off drop to standby bias */
1836 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1837 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1838 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1839 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1840 if (ret != 0)
1841 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1842 ret);
1845 /* If we're in standby and can support bias off then do that */
1846 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1847 d->target_bias_level == SND_SOC_BIAS_OFF) {
1848 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1849 if (ret != 0)
1850 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1851 ret);
1853 if (d->dev && cookie)
1854 pm_runtime_put(d->dev);
1857 /* If we just powered up then move to active bias */
1858 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1859 d->target_bias_level == SND_SOC_BIAS_ON) {
1860 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1861 if (ret != 0)
1862 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1863 ret);
1867 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1868 bool power, bool connect)
1870 /* If a connection is being made or broken then that update
1871 * will have marked the peer dirty, otherwise the widgets are
1872 * not connected and this update has no impact. */
1873 if (!connect)
1874 return;
1876 /* If the peer is already in the state we're moving to then we
1877 * won't have an impact on it. */
1878 if (power != peer->power)
1879 dapm_mark_dirty(peer, "peer state change");
1882 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1883 struct list_head *up_list,
1884 struct list_head *down_list)
1886 struct snd_soc_dapm_path *path;
1887 int power;
1889 switch (w->id) {
1890 case snd_soc_dapm_pre:
1891 power = 0;
1892 goto end;
1893 case snd_soc_dapm_post:
1894 power = 1;
1895 goto end;
1896 default:
1897 break;
1900 power = dapm_widget_power_check(w);
1902 if (w->power == power)
1903 return;
1905 trace_snd_soc_dapm_widget_power(w, power);
1908 * If we changed our power state perhaps our neigbours
1909 * changed also.
1911 snd_soc_dapm_widget_for_each_source_path(w, path)
1912 dapm_widget_set_peer_power(path->source, power, path->connect);
1915 * Supplies can't affect their outputs, only their inputs
1917 if (!w->is_supply)
1918 snd_soc_dapm_widget_for_each_sink_path(w, path)
1919 dapm_widget_set_peer_power(path->sink, power, path->connect);
1921 end:
1922 if (power)
1923 dapm_seq_insert(w, up_list, true);
1924 else
1925 dapm_seq_insert(w, down_list, false);
1928 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1930 if (dapm->idle_bias_off)
1931 return true;
1933 switch (snd_power_get_state(dapm->card->snd_card)) {
1934 case SNDRV_CTL_POWER_D3hot:
1935 case SNDRV_CTL_POWER_D3cold:
1936 return dapm->suspend_bias_off;
1937 default:
1938 break;
1941 return false;
1945 * Scan each dapm widget for complete audio path.
1946 * A complete path is a route that has valid endpoints i.e.:-
1948 * o DAC to output pin.
1949 * o Input pin to ADC.
1950 * o Input pin to Output pin (bypass, sidetone)
1951 * o DAC to ADC (loopback).
1953 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1955 struct snd_soc_dapm_widget *w;
1956 struct snd_soc_dapm_context *d;
1957 LIST_HEAD(up_list);
1958 LIST_HEAD(down_list);
1959 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1960 enum snd_soc_bias_level bias;
1961 int ret;
1963 snd_soc_dapm_mutex_assert_held(card);
1965 trace_snd_soc_dapm_start(card, event);
1967 for_each_card_dapms(card, d) {
1968 if (dapm_idle_bias_off(d))
1969 d->target_bias_level = SND_SOC_BIAS_OFF;
1970 else
1971 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1974 dapm_reset(card);
1976 /* Check which widgets we need to power and store them in
1977 * lists indicating if they should be powered up or down. We
1978 * only check widgets that have been flagged as dirty but note
1979 * that new widgets may be added to the dirty list while we
1980 * iterate.
1982 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1983 dapm_power_one_widget(w, &up_list, &down_list);
1986 for_each_card_widgets(card, w) {
1987 switch (w->id) {
1988 case snd_soc_dapm_pre:
1989 case snd_soc_dapm_post:
1990 /* These widgets always need to be powered */
1991 break;
1992 default:
1993 list_del_init(&w->dirty);
1994 break;
1997 if (w->new_power) {
1998 d = w->dapm;
2000 /* Supplies and micbiases only bring the
2001 * context up to STANDBY as unless something
2002 * else is active and passing audio they
2003 * generally don't require full power. Signal
2004 * generators are virtual pins and have no
2005 * power impact themselves.
2007 switch (w->id) {
2008 case snd_soc_dapm_siggen:
2009 case snd_soc_dapm_vmid:
2010 break;
2011 case snd_soc_dapm_supply:
2012 case snd_soc_dapm_regulator_supply:
2013 case snd_soc_dapm_pinctrl:
2014 case snd_soc_dapm_clock_supply:
2015 case snd_soc_dapm_micbias:
2016 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
2017 d->target_bias_level = SND_SOC_BIAS_STANDBY;
2018 break;
2019 default:
2020 d->target_bias_level = SND_SOC_BIAS_ON;
2021 break;
2027 /* Force all contexts in the card to the same bias state if
2028 * they're not ground referenced.
2030 bias = SND_SOC_BIAS_OFF;
2031 for_each_card_dapms(card, d)
2032 if (d->target_bias_level > bias)
2033 bias = d->target_bias_level;
2034 for_each_card_dapms(card, d)
2035 if (!dapm_idle_bias_off(d))
2036 d->target_bias_level = bias;
2038 trace_snd_soc_dapm_walk_done(card);
2040 /* Run card bias changes at first */
2041 dapm_pre_sequence_async(&card->dapm, 0);
2042 /* Run other bias changes in parallel */
2043 for_each_card_dapms(card, d) {
2044 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2045 async_schedule_domain(dapm_pre_sequence_async, d,
2046 &async_domain);
2048 async_synchronize_full_domain(&async_domain);
2050 list_for_each_entry(w, &down_list, power_list) {
2051 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
2054 list_for_each_entry(w, &up_list, power_list) {
2055 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
2058 /* Power down widgets first; try to avoid amplifying pops. */
2059 dapm_seq_run(card, &down_list, event, false);
2061 dapm_widget_update(card);
2063 /* Now power up. */
2064 dapm_seq_run(card, &up_list, event, true);
2066 /* Run all the bias changes in parallel */
2067 for_each_card_dapms(card, d) {
2068 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2069 async_schedule_domain(dapm_post_sequence_async, d,
2070 &async_domain);
2072 async_synchronize_full_domain(&async_domain);
2073 /* Run card bias changes at last */
2074 dapm_post_sequence_async(&card->dapm, 0);
2076 /* do we need to notify any clients that DAPM event is complete */
2077 for_each_card_dapms(card, d) {
2078 if (!d->component)
2079 continue;
2081 ret = snd_soc_component_stream_event(d->component, event);
2082 if (ret < 0)
2083 return ret;
2086 pop_dbg(card->dev, card->pop_time,
2087 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2088 pop_wait(card->pop_time);
2090 trace_snd_soc_dapm_done(card, event);
2092 return 0;
2095 #ifdef CONFIG_DEBUG_FS
2097 static const char * const snd_soc_dapm_type_name[] = {
2098 [snd_soc_dapm_input] = "input",
2099 [snd_soc_dapm_output] = "output",
2100 [snd_soc_dapm_mux] = "mux",
2101 [snd_soc_dapm_demux] = "demux",
2102 [snd_soc_dapm_mixer] = "mixer",
2103 [snd_soc_dapm_mixer_named_ctl] = "mixer_named_ctl",
2104 [snd_soc_dapm_pga] = "pga",
2105 [snd_soc_dapm_out_drv] = "out_drv",
2106 [snd_soc_dapm_adc] = "adc",
2107 [snd_soc_dapm_dac] = "dac",
2108 [snd_soc_dapm_micbias] = "micbias",
2109 [snd_soc_dapm_mic] = "mic",
2110 [snd_soc_dapm_hp] = "hp",
2111 [snd_soc_dapm_spk] = "spk",
2112 [snd_soc_dapm_line] = "line",
2113 [snd_soc_dapm_switch] = "switch",
2114 [snd_soc_dapm_vmid] = "vmid",
2115 [snd_soc_dapm_pre] = "pre",
2116 [snd_soc_dapm_post] = "post",
2117 [snd_soc_dapm_supply] = "supply",
2118 [snd_soc_dapm_pinctrl] = "pinctrl",
2119 [snd_soc_dapm_regulator_supply] = "regulator_supply",
2120 [snd_soc_dapm_clock_supply] = "clock_supply",
2121 [snd_soc_dapm_aif_in] = "aif_in",
2122 [snd_soc_dapm_aif_out] = "aif_out",
2123 [snd_soc_dapm_siggen] = "siggen",
2124 [snd_soc_dapm_sink] = "sink",
2125 [snd_soc_dapm_dai_in] = "dai_in",
2126 [snd_soc_dapm_dai_out] = "dai_out",
2127 [snd_soc_dapm_dai_link] = "dai_link",
2128 [snd_soc_dapm_kcontrol] = "kcontrol",
2129 [snd_soc_dapm_buffer] = "buffer",
2130 [snd_soc_dapm_scheduler] = "scheduler",
2131 [snd_soc_dapm_effect] = "effect",
2132 [snd_soc_dapm_src] = "src",
2133 [snd_soc_dapm_asrc] = "asrc",
2134 [snd_soc_dapm_encoder] = "encoder",
2135 [snd_soc_dapm_decoder] = "decoder",
2138 static ssize_t dapm_widget_power_read_file(struct file *file,
2139 char __user *user_buf,
2140 size_t count, loff_t *ppos)
2142 struct snd_soc_dapm_widget *w = file->private_data;
2143 enum snd_soc_dapm_direction dir, rdir;
2144 char *buf;
2145 int in, out;
2146 ssize_t ret;
2147 struct snd_soc_dapm_path *p = NULL;
2148 const char *c_name;
2150 BUILD_BUG_ON(ARRAY_SIZE(snd_soc_dapm_type_name) != SND_SOC_DAPM_TYPE_COUNT);
2152 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2153 if (!buf)
2154 return -ENOMEM;
2156 snd_soc_dapm_mutex_lock_root(w->dapm);
2158 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2159 if (w->is_supply) {
2160 in = 0;
2161 out = 0;
2162 } else {
2163 in = is_connected_input_ep(w, NULL, NULL);
2164 out = is_connected_output_ep(w, NULL, NULL);
2167 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2168 w->name, w->power ? "On" : "Off",
2169 w->force ? " (forced)" : "", in, out);
2171 if (w->reg >= 0)
2172 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2173 " - R%d(0x%x) mask 0x%x",
2174 w->reg, w->reg, w->mask << w->shift);
2176 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2178 if (w->sname)
2179 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2180 w->sname,
2181 w->active ? "active" : "inactive");
2183 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " widget-type %s\n",
2184 snd_soc_dapm_type_name[w->id]);
2186 snd_soc_dapm_for_each_direction(dir) {
2187 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2188 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2189 if (p->connected && !p->connected(p->source, p->sink))
2190 continue;
2192 if (!p->connect)
2193 continue;
2195 c_name = p->node[rdir]->dapm->component ?
2196 p->node[rdir]->dapm->component->name : NULL;
2197 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2198 " %s \"%s\" \"%s\" \"%s\"\n",
2199 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2200 p->name ? p->name : "static",
2201 p->node[rdir]->name, c_name);
2205 snd_soc_dapm_mutex_unlock(w->dapm);
2207 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2209 kfree(buf);
2210 return ret;
2213 static const struct file_operations dapm_widget_power_fops = {
2214 .open = simple_open,
2215 .read = dapm_widget_power_read_file,
2216 .llseek = default_llseek,
2219 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2220 size_t count, loff_t *ppos)
2222 struct snd_soc_dapm_context *dapm = file->private_data;
2223 char *level;
2225 switch (dapm->bias_level) {
2226 case SND_SOC_BIAS_ON:
2227 level = "On\n";
2228 break;
2229 case SND_SOC_BIAS_PREPARE:
2230 level = "Prepare\n";
2231 break;
2232 case SND_SOC_BIAS_STANDBY:
2233 level = "Standby\n";
2234 break;
2235 case SND_SOC_BIAS_OFF:
2236 level = "Off\n";
2237 break;
2238 default:
2239 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2240 level = "Unknown\n";
2241 break;
2244 return simple_read_from_buffer(user_buf, count, ppos, level,
2245 strlen(level));
2248 static const struct file_operations dapm_bias_fops = {
2249 .open = simple_open,
2250 .read = dapm_bias_read_file,
2251 .llseek = default_llseek,
2254 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2255 struct dentry *parent)
2257 if (IS_ERR_OR_NULL(parent))
2258 return;
2260 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2262 debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
2263 &dapm_bias_fops);
2266 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2268 struct snd_soc_dapm_context *dapm = w->dapm;
2270 if (!dapm->debugfs_dapm || !w->name)
2271 return;
2273 debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
2274 &dapm_widget_power_fops);
2277 static void dapm_debugfs_free_widget(struct snd_soc_dapm_widget *w)
2279 struct snd_soc_dapm_context *dapm = w->dapm;
2281 if (!dapm->debugfs_dapm || !w->name)
2282 return;
2284 debugfs_lookup_and_remove(w->name, dapm->debugfs_dapm);
2287 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2289 debugfs_remove_recursive(dapm->debugfs_dapm);
2290 dapm->debugfs_dapm = NULL;
2293 #else
2294 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2295 struct dentry *parent)
2299 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2303 static inline void dapm_debugfs_free_widget(struct snd_soc_dapm_widget *w)
2307 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2311 #endif
2314 * soc_dapm_connect_path() - Connects or disconnects a path
2315 * @path: The path to update
2316 * @connect: The new connect state of the path. True if the path is connected,
2317 * false if it is disconnected.
2318 * @reason: The reason why the path changed (for debugging only)
2320 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2321 bool connect, const char *reason)
2323 if (path->connect == connect)
2324 return;
2326 path->connect = connect;
2327 dapm_mark_dirty(path->source, reason);
2328 dapm_mark_dirty(path->sink, reason);
2329 dapm_path_invalidate(path);
2332 /* test and update the power status of a mux widget */
2333 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2334 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2336 struct snd_soc_dapm_path *path;
2337 int found = 0;
2338 bool connect;
2340 snd_soc_dapm_mutex_assert_held(card);
2342 /* find dapm widget path assoc with kcontrol */
2343 dapm_kcontrol_for_each_path(path, kcontrol) {
2344 found = 1;
2345 /* we now need to match the string in the enum to the path */
2346 if (e && !(strcmp(path->name, e->texts[mux])))
2347 connect = true;
2348 else
2349 connect = false;
2351 soc_dapm_connect_path(path, connect, "mux update");
2354 if (found)
2355 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2357 return found;
2360 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2361 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2362 struct snd_soc_dapm_update *update)
2364 struct snd_soc_card *card = dapm->card;
2365 int ret;
2367 snd_soc_dapm_mutex_lock(card);
2368 card->update = update;
2369 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2370 card->update = NULL;
2371 snd_soc_dapm_mutex_unlock(card);
2372 if (ret > 0)
2373 snd_soc_dpcm_runtime_update(card);
2374 return ret;
2376 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2378 /* test and update the power status of a mixer or switch widget */
2379 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2380 struct snd_kcontrol *kcontrol,
2381 int connect, int rconnect)
2383 struct snd_soc_dapm_path *path;
2384 int found = 0;
2386 snd_soc_dapm_mutex_assert_held(card);
2388 /* find dapm widget path assoc with kcontrol */
2389 dapm_kcontrol_for_each_path(path, kcontrol) {
2391 * Ideally this function should support any number of
2392 * paths and channels. But since kcontrols only come
2393 * in mono and stereo variants, we are limited to 2
2394 * channels.
2396 * The following code assumes for stereo controls the
2397 * first path (when 'found == 0') is the left channel,
2398 * and all remaining paths (when 'found == 1') are the
2399 * right channel.
2401 * A stereo control is signified by a valid 'rconnect'
2402 * value, either 0 for unconnected, or >= 0 for connected.
2403 * This is chosen instead of using snd_soc_volsw_is_stereo,
2404 * so that the behavior of snd_soc_dapm_mixer_update_power
2405 * doesn't change even when the kcontrol passed in is
2406 * stereo.
2408 * It passes 'connect' as the path connect status for
2409 * the left channel, and 'rconnect' for the right
2410 * channel.
2412 if (found && rconnect >= 0)
2413 soc_dapm_connect_path(path, rconnect, "mixer update");
2414 else
2415 soc_dapm_connect_path(path, connect, "mixer update");
2416 found = 1;
2419 if (found)
2420 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2422 return found;
2425 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2426 struct snd_kcontrol *kcontrol, int connect,
2427 struct snd_soc_dapm_update *update)
2429 struct snd_soc_card *card = dapm->card;
2430 int ret;
2432 snd_soc_dapm_mutex_lock(card);
2433 card->update = update;
2434 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2435 card->update = NULL;
2436 snd_soc_dapm_mutex_unlock(card);
2437 if (ret > 0)
2438 snd_soc_dpcm_runtime_update(card);
2439 return ret;
2441 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2443 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2444 char *buf, int count)
2446 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2447 struct snd_soc_dapm_widget *w;
2448 char *state = "not set";
2450 /* card won't be set for the dummy component, as a spot fix
2451 * we're checking for that case specifically here but in future
2452 * we will ensure that the dummy component looks like others.
2454 if (!cmpnt->card)
2455 return 0;
2457 for_each_card_widgets(cmpnt->card, w) {
2458 if (w->dapm != dapm)
2459 continue;
2461 /* only display widgets that burn power */
2462 switch (w->id) {
2463 case snd_soc_dapm_hp:
2464 case snd_soc_dapm_mic:
2465 case snd_soc_dapm_spk:
2466 case snd_soc_dapm_line:
2467 case snd_soc_dapm_micbias:
2468 case snd_soc_dapm_dac:
2469 case snd_soc_dapm_adc:
2470 case snd_soc_dapm_pga:
2471 case snd_soc_dapm_effect:
2472 case snd_soc_dapm_out_drv:
2473 case snd_soc_dapm_mixer:
2474 case snd_soc_dapm_mixer_named_ctl:
2475 case snd_soc_dapm_supply:
2476 case snd_soc_dapm_regulator_supply:
2477 case snd_soc_dapm_pinctrl:
2478 case snd_soc_dapm_clock_supply:
2479 if (w->name)
2480 count += sysfs_emit_at(buf, count, "%s: %s\n",
2481 w->name, w->power ? "On":"Off");
2482 break;
2483 default:
2484 break;
2488 switch (snd_soc_dapm_get_bias_level(dapm)) {
2489 case SND_SOC_BIAS_ON:
2490 state = "On";
2491 break;
2492 case SND_SOC_BIAS_PREPARE:
2493 state = "Prepare";
2494 break;
2495 case SND_SOC_BIAS_STANDBY:
2496 state = "Standby";
2497 break;
2498 case SND_SOC_BIAS_OFF:
2499 state = "Off";
2500 break;
2502 count += sysfs_emit_at(buf, count, "PM State: %s\n", state);
2504 return count;
2507 /* show dapm widget status in sys fs */
2508 static ssize_t dapm_widget_show(struct device *dev,
2509 struct device_attribute *attr, char *buf)
2511 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2512 struct snd_soc_dai *codec_dai;
2513 int i, count = 0;
2515 snd_soc_dapm_mutex_lock_root(rtd->card);
2517 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2518 struct snd_soc_component *cmpnt = codec_dai->component;
2520 count = dapm_widget_show_component(cmpnt, buf, count);
2523 snd_soc_dapm_mutex_unlock(rtd->card);
2525 return count;
2528 static DEVICE_ATTR_RO(dapm_widget);
2530 struct attribute *soc_dapm_dev_attrs[] = {
2531 &dev_attr_dapm_widget.attr,
2532 NULL
2535 static void dapm_free_path(struct snd_soc_dapm_path *path)
2537 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2538 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2539 list_del(&path->list_kcontrol);
2540 list_del(&path->list);
2541 kfree(path);
2545 * snd_soc_dapm_free_widget - Free specified widget
2546 * @w: widget to free
2548 * Removes widget from all paths and frees memory occupied by it.
2550 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2552 struct snd_soc_dapm_path *p, *next_p;
2553 enum snd_soc_dapm_direction dir;
2555 if (!w)
2556 return;
2558 list_del(&w->list);
2559 list_del(&w->dirty);
2561 * remove source and sink paths associated to this widget.
2562 * While removing the path, remove reference to it from both
2563 * source and sink widgets so that path is removed only once.
2565 snd_soc_dapm_for_each_direction(dir) {
2566 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2567 dapm_free_path(p);
2570 dapm_debugfs_free_widget(w);
2572 kfree(w->kcontrols);
2573 kfree_const(w->name);
2574 kfree_const(w->sname);
2575 kfree(w);
2577 EXPORT_SYMBOL_GPL(snd_soc_dapm_free_widget);
2579 /* free all dapm widgets and resources */
2580 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2582 struct snd_soc_dapm_widget *w, *next_w;
2584 for_each_card_widgets_safe(dapm->card, w, next_w) {
2585 if (w->dapm != dapm)
2586 continue;
2587 snd_soc_dapm_free_widget(w);
2590 dapm->wcache_sink = NULL;
2591 dapm->wcache_source = NULL;
2594 static struct snd_soc_dapm_widget *dapm_find_widget(
2595 struct snd_soc_dapm_context *dapm, const char *pin,
2596 bool search_other_contexts)
2598 struct snd_soc_dapm_widget *w;
2599 struct snd_soc_dapm_widget *fallback = NULL;
2600 char prefixed_pin[80];
2601 const char *pin_name;
2602 const char *prefix = soc_dapm_prefix(dapm);
2604 if (prefix) {
2605 snprintf(prefixed_pin, sizeof(prefixed_pin), "%s %s",
2606 prefix, pin);
2607 pin_name = prefixed_pin;
2608 } else {
2609 pin_name = pin;
2612 for_each_card_widgets(dapm->card, w) {
2613 if (!strcmp(w->name, pin_name)) {
2614 if (w->dapm == dapm)
2615 return w;
2616 else
2617 fallback = w;
2621 if (search_other_contexts)
2622 return fallback;
2624 return NULL;
2628 * set the DAPM pin status:
2629 * returns 1 when the value has been updated, 0 when unchanged, or a negative
2630 * error code; called from kcontrol put callback
2632 static int __snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2633 const char *pin, int status)
2635 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2636 int ret = 0;
2638 dapm_assert_locked(dapm);
2640 if (!w) {
2641 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2642 return -EINVAL;
2645 if (w->connected != status) {
2646 dapm_mark_dirty(w, "pin configuration");
2647 dapm_widget_invalidate_input_paths(w);
2648 dapm_widget_invalidate_output_paths(w);
2649 ret = 1;
2652 w->connected = status;
2653 if (status == 0)
2654 w->force = 0;
2656 return ret;
2660 * similar as __snd_soc_dapm_set_pin(), but returns 0 when successful;
2661 * called from several API functions below
2663 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2664 const char *pin, int status)
2666 int ret = __snd_soc_dapm_set_pin(dapm, pin, status);
2668 return ret < 0 ? ret : 0;
2672 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2673 * @dapm: DAPM context
2675 * Walks all dapm audio paths and powers widgets according to their
2676 * stream or path usage.
2678 * Requires external locking.
2680 * Returns 0 for success.
2682 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2685 * Suppress early reports (eg, jacks syncing their state) to avoid
2686 * silly DAPM runs during card startup.
2688 if (!snd_soc_card_is_instantiated(dapm->card))
2689 return 0;
2691 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2693 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2696 * snd_soc_dapm_sync - scan and power dapm paths
2697 * @dapm: DAPM context
2699 * Walks all dapm audio paths and powers widgets according to their
2700 * stream or path usage.
2702 * Returns 0 for success.
2704 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2706 int ret;
2708 snd_soc_dapm_mutex_lock(dapm);
2709 ret = snd_soc_dapm_sync_unlocked(dapm);
2710 snd_soc_dapm_mutex_unlock(dapm);
2711 return ret;
2713 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2715 static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2716 struct snd_soc_dapm_widget *w,
2717 int channels)
2719 switch (w->id) {
2720 case snd_soc_dapm_aif_out:
2721 case snd_soc_dapm_aif_in:
2722 break;
2723 default:
2724 return 0;
2727 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2728 w->channel < channels ? "Connecting" : "Disconnecting",
2729 p->source->name, p->sink->name);
2731 if (w->channel < channels)
2732 soc_dapm_connect_path(p, true, "dai update");
2733 else
2734 soc_dapm_connect_path(p, false, "dai update");
2736 return 0;
2739 static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2740 struct snd_pcm_hw_params *params,
2741 struct snd_soc_dai *dai)
2743 int dir = substream->stream;
2744 int channels = params_channels(params);
2745 struct snd_soc_dapm_path *p;
2746 struct snd_soc_dapm_widget *w;
2747 int ret;
2749 w = snd_soc_dai_get_widget(dai, dir);
2751 if (!w)
2752 return 0;
2754 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name, snd_pcm_direction_name(dir));
2756 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2757 ret = dapm_update_dai_chan(p, p->sink, channels);
2758 if (ret < 0)
2759 return ret;
2762 snd_soc_dapm_widget_for_each_source_path(w, p) {
2763 ret = dapm_update_dai_chan(p, p->source, channels);
2764 if (ret < 0)
2765 return ret;
2768 return 0;
2771 int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2772 struct snd_pcm_hw_params *params,
2773 struct snd_soc_dai *dai)
2775 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
2776 int ret;
2778 snd_soc_dapm_mutex_lock(rtd->card);
2779 ret = dapm_update_dai_unlocked(substream, params, dai);
2780 snd_soc_dapm_mutex_unlock(rtd->card);
2782 return ret;
2784 EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2786 int snd_soc_dapm_widget_name_cmp(struct snd_soc_dapm_widget *widget, const char *s)
2788 struct snd_soc_component *component = snd_soc_dapm_to_component(widget->dapm);
2789 const char *wname = widget->name;
2791 if (component->name_prefix)
2792 wname += strlen(component->name_prefix) + 1; /* plus space */
2794 return strcmp(wname, s);
2796 EXPORT_SYMBOL_GPL(snd_soc_dapm_widget_name_cmp);
2799 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2800 * @w: The widget for which to update the flags
2802 * Some widgets have a dynamic category which depends on which neighbors they
2803 * are connected to. This function update the category for these widgets.
2805 * This function must be called whenever a path is added or removed to a widget.
2807 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2809 enum snd_soc_dapm_direction dir;
2810 struct snd_soc_dapm_path *p;
2811 unsigned int ep;
2813 switch (w->id) {
2814 case snd_soc_dapm_input:
2815 /* On a fully routed card an input is never a source */
2816 if (w->dapm->card->fully_routed)
2817 return;
2818 ep = SND_SOC_DAPM_EP_SOURCE;
2819 snd_soc_dapm_widget_for_each_source_path(w, p) {
2820 if (p->source->id == snd_soc_dapm_micbias ||
2821 p->source->id == snd_soc_dapm_mic ||
2822 p->source->id == snd_soc_dapm_line ||
2823 p->source->id == snd_soc_dapm_output) {
2824 ep = 0;
2825 break;
2828 break;
2829 case snd_soc_dapm_output:
2830 /* On a fully routed card a output is never a sink */
2831 if (w->dapm->card->fully_routed)
2832 return;
2833 ep = SND_SOC_DAPM_EP_SINK;
2834 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2835 if (p->sink->id == snd_soc_dapm_spk ||
2836 p->sink->id == snd_soc_dapm_hp ||
2837 p->sink->id == snd_soc_dapm_line ||
2838 p->sink->id == snd_soc_dapm_input) {
2839 ep = 0;
2840 break;
2843 break;
2844 case snd_soc_dapm_line:
2845 ep = 0;
2846 snd_soc_dapm_for_each_direction(dir) {
2847 if (!list_empty(&w->edges[dir]))
2848 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2850 break;
2851 default:
2852 return;
2855 w->is_ep = ep;
2858 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2859 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2860 const char *control)
2862 bool dynamic_source = false;
2863 bool dynamic_sink = false;
2865 if (!control)
2866 return 0;
2868 switch (source->id) {
2869 case snd_soc_dapm_demux:
2870 dynamic_source = true;
2871 break;
2872 default:
2873 break;
2876 switch (sink->id) {
2877 case snd_soc_dapm_mux:
2878 case snd_soc_dapm_switch:
2879 case snd_soc_dapm_mixer:
2880 case snd_soc_dapm_mixer_named_ctl:
2881 dynamic_sink = true;
2882 break;
2883 default:
2884 break;
2887 if (dynamic_source && dynamic_sink) {
2888 dev_err(dapm->dev,
2889 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2890 source->name, control, sink->name);
2891 return -EINVAL;
2892 } else if (!dynamic_source && !dynamic_sink) {
2893 dev_err(dapm->dev,
2894 "Control not supported for path %s -> [%s] -> %s\n",
2895 source->name, control, sink->name);
2896 return -EINVAL;
2899 return 0;
2902 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2903 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2904 const char *control,
2905 int (*connected)(struct snd_soc_dapm_widget *source,
2906 struct snd_soc_dapm_widget *sink))
2908 enum snd_soc_dapm_direction dir;
2909 struct snd_soc_dapm_path *path;
2910 int ret;
2912 if (wsink->is_supply && !wsource->is_supply) {
2913 dev_err(dapm->dev,
2914 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2915 wsource->name, wsink->name);
2916 return -EINVAL;
2919 if (connected && !wsource->is_supply) {
2920 dev_err(dapm->dev,
2921 "connected() callback only supported for supply widgets (%s -> %s)\n",
2922 wsource->name, wsink->name);
2923 return -EINVAL;
2926 if (wsource->is_supply && control) {
2927 dev_err(dapm->dev,
2928 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2929 wsource->name, control, wsink->name);
2930 return -EINVAL;
2933 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2934 if (ret)
2935 return ret;
2937 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2938 if (!path)
2939 return -ENOMEM;
2941 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2942 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2944 path->connected = connected;
2945 INIT_LIST_HEAD(&path->list);
2946 INIT_LIST_HEAD(&path->list_kcontrol);
2948 if (wsource->is_supply || wsink->is_supply)
2949 path->is_supply = 1;
2951 /* connect static paths */
2952 if (control == NULL) {
2953 path->connect = 1;
2954 } else {
2955 switch (wsource->id) {
2956 case snd_soc_dapm_demux:
2957 ret = dapm_connect_mux(dapm, path, control, wsource);
2958 if (ret)
2959 goto err;
2960 break;
2961 default:
2962 break;
2965 switch (wsink->id) {
2966 case snd_soc_dapm_mux:
2967 ret = dapm_connect_mux(dapm, path, control, wsink);
2968 if (ret != 0)
2969 goto err;
2970 break;
2971 case snd_soc_dapm_switch:
2972 case snd_soc_dapm_mixer:
2973 case snd_soc_dapm_mixer_named_ctl:
2974 ret = dapm_connect_mixer(dapm, path, control);
2975 if (ret != 0)
2976 goto err;
2977 break;
2978 default:
2979 break;
2983 list_add(&path->list, &dapm->card->paths);
2985 snd_soc_dapm_for_each_direction(dir)
2986 list_add(&path->list_node[dir], &path->node[dir]->edges[dir]);
2988 snd_soc_dapm_for_each_direction(dir) {
2989 dapm_update_widget_flags(path->node[dir]);
2990 dapm_mark_dirty(path->node[dir], "Route added");
2993 if (snd_soc_card_is_instantiated(dapm->card) && path->connect)
2994 dapm_path_invalidate(path);
2996 return 0;
2997 err:
2998 kfree(path);
2999 return ret;
3002 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
3003 const struct snd_soc_dapm_route *route)
3005 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
3006 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
3007 const char *sink;
3008 const char *source;
3009 char prefixed_sink[80];
3010 char prefixed_source[80];
3011 const char *prefix;
3012 unsigned int sink_ref = 0;
3013 unsigned int source_ref = 0;
3014 int ret;
3016 prefix = soc_dapm_prefix(dapm);
3017 if (prefix) {
3018 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
3019 prefix, route->sink);
3020 sink = prefixed_sink;
3021 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
3022 prefix, route->source);
3023 source = prefixed_source;
3024 } else {
3025 sink = route->sink;
3026 source = route->source;
3029 wsource = dapm_wcache_lookup(dapm->wcache_source, source);
3030 wsink = dapm_wcache_lookup(dapm->wcache_sink, sink);
3032 if (wsink && wsource)
3033 goto skip_search;
3036 * find src and dest widgets over all widgets but favor a widget from
3037 * current DAPM context
3039 for_each_card_widgets(dapm->card, w) {
3040 if (!wsink && !(strcmp(w->name, sink))) {
3041 wtsink = w;
3042 if (w->dapm == dapm) {
3043 wsink = w;
3044 if (wsource)
3045 break;
3047 sink_ref++;
3048 if (sink_ref > 1)
3049 dev_warn(dapm->dev,
3050 "ASoC: sink widget %s overwritten\n",
3051 w->name);
3052 continue;
3054 if (!wsource && !(strcmp(w->name, source))) {
3055 wtsource = w;
3056 if (w->dapm == dapm) {
3057 wsource = w;
3058 if (wsink)
3059 break;
3061 source_ref++;
3062 if (source_ref > 1)
3063 dev_warn(dapm->dev,
3064 "ASoC: source widget %s overwritten\n",
3065 w->name);
3068 /* use widget from another DAPM context if not found from this */
3069 if (!wsink)
3070 wsink = wtsink;
3071 if (!wsource)
3072 wsource = wtsource;
3074 ret = -ENODEV;
3075 if (!wsource)
3076 goto err;
3077 if (!wsink)
3078 goto err;
3080 skip_search:
3081 /* update cache */
3082 dapm->wcache_sink = wsink;
3083 dapm->wcache_source = wsource;
3085 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
3086 route->connected);
3087 err:
3088 if (ret)
3089 dev_err(dapm->dev, "ASoC: Failed to add route %s%s -%s%s%s> %s%s\n",
3090 source, !wsource ? "(*)" : "",
3091 !route->control ? "" : "> [",
3092 !route->control ? "" : route->control,
3093 !route->control ? "" : "] -",
3094 sink, !wsink ? "(*)" : "");
3095 return ret;
3098 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
3099 const struct snd_soc_dapm_route *route)
3101 struct snd_soc_dapm_path *path, *p;
3102 const char *sink;
3103 const char *source;
3104 char prefixed_sink[80];
3105 char prefixed_source[80];
3106 const char *prefix;
3108 if (route->control) {
3109 dev_err(dapm->dev,
3110 "ASoC: Removal of routes with controls not supported\n");
3111 return -EINVAL;
3114 prefix = soc_dapm_prefix(dapm);
3115 if (prefix) {
3116 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
3117 prefix, route->sink);
3118 sink = prefixed_sink;
3119 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
3120 prefix, route->source);
3121 source = prefixed_source;
3122 } else {
3123 sink = route->sink;
3124 source = route->source;
3127 path = NULL;
3128 list_for_each_entry(p, &dapm->card->paths, list) {
3129 if (strcmp(p->source->name, source) != 0)
3130 continue;
3131 if (strcmp(p->sink->name, sink) != 0)
3132 continue;
3133 path = p;
3134 break;
3137 if (path) {
3138 struct snd_soc_dapm_widget *wsource = path->source;
3139 struct snd_soc_dapm_widget *wsink = path->sink;
3141 dapm_mark_dirty(wsource, "Route removed");
3142 dapm_mark_dirty(wsink, "Route removed");
3143 if (path->connect)
3144 dapm_path_invalidate(path);
3146 dapm_free_path(path);
3148 /* Update any path related flags */
3149 dapm_update_widget_flags(wsource);
3150 dapm_update_widget_flags(wsink);
3151 } else {
3152 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
3153 source, sink);
3156 return 0;
3160 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
3161 * @dapm: DAPM context
3162 * @route: audio routes
3163 * @num: number of routes
3165 * Connects 2 dapm widgets together via a named audio path. The sink is
3166 * the widget receiving the audio signal, whilst the source is the sender
3167 * of the audio signal.
3169 * Returns 0 for success else error. On error all resources can be freed
3170 * with a call to snd_soc_card_free().
3172 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
3173 const struct snd_soc_dapm_route *route, int num)
3175 int i, ret = 0;
3177 snd_soc_dapm_mutex_lock(dapm);
3178 for (i = 0; i < num; i++) {
3179 int r = snd_soc_dapm_add_route(dapm, route);
3180 if (r < 0)
3181 ret = r;
3182 route++;
3184 snd_soc_dapm_mutex_unlock(dapm);
3186 return ret;
3188 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3191 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3192 * @dapm: DAPM context
3193 * @route: audio routes
3194 * @num: number of routes
3196 * Removes routes from the DAPM context.
3198 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3199 const struct snd_soc_dapm_route *route, int num)
3201 int i;
3203 snd_soc_dapm_mutex_lock(dapm);
3204 for (i = 0; i < num; i++) {
3205 snd_soc_dapm_del_route(dapm, route);
3206 route++;
3208 snd_soc_dapm_mutex_unlock(dapm);
3210 return 0;
3212 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3214 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3215 const struct snd_soc_dapm_route *route)
3217 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3218 route->source,
3219 true);
3220 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3221 route->sink,
3222 true);
3223 struct snd_soc_dapm_path *path;
3224 int count = 0;
3226 if (!source) {
3227 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3228 route->source);
3229 return -ENODEV;
3232 if (!sink) {
3233 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3234 route->sink);
3235 return -ENODEV;
3238 if (route->control || route->connected)
3239 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3240 route->source, route->sink);
3242 snd_soc_dapm_widget_for_each_sink_path(source, path) {
3243 if (path->sink == sink) {
3244 path->weak = 1;
3245 count++;
3249 if (count == 0)
3250 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3251 route->source, route->sink);
3252 if (count > 1)
3253 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3254 count, route->source, route->sink);
3256 return 0;
3260 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3261 * @dapm: DAPM context
3262 * @route: audio routes
3263 * @num: number of routes
3265 * Mark existing routes matching those specified in the passed array
3266 * as being weak, meaning that they are ignored for the purpose of
3267 * power decisions. The main intended use case is for sidetone paths
3268 * which couple audio between other independent paths if they are both
3269 * active in order to make the combination work better at the user
3270 * level but which aren't intended to be "used".
3272 * Note that CODEC drivers should not use this as sidetone type paths
3273 * can frequently also be used as bypass paths.
3275 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3276 const struct snd_soc_dapm_route *route, int num)
3278 int i;
3279 int ret = 0;
3281 snd_soc_dapm_mutex_lock_root(dapm);
3282 for (i = 0; i < num; i++) {
3283 int err = snd_soc_dapm_weak_route(dapm, route);
3284 if (err)
3285 ret = err;
3286 route++;
3288 snd_soc_dapm_mutex_unlock(dapm);
3290 return ret;
3292 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3295 * snd_soc_dapm_new_widgets - add new dapm widgets
3296 * @card: card to be checked for new dapm widgets
3298 * Checks the codec for any new dapm widgets and creates them if found.
3300 * Returns 0 for success.
3302 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3304 struct snd_soc_dapm_widget *w;
3305 unsigned int val;
3307 snd_soc_dapm_mutex_lock_root(card);
3309 for_each_card_widgets(card, w)
3311 if (w->new)
3312 continue;
3314 if (w->num_kcontrols) {
3315 w->kcontrols = kcalloc(w->num_kcontrols,
3316 sizeof(struct snd_kcontrol *),
3317 GFP_KERNEL);
3318 if (!w->kcontrols) {
3319 snd_soc_dapm_mutex_unlock(card);
3320 return -ENOMEM;
3324 switch(w->id) {
3325 case snd_soc_dapm_switch:
3326 case snd_soc_dapm_mixer:
3327 case snd_soc_dapm_mixer_named_ctl:
3328 dapm_new_mixer(w);
3329 break;
3330 case snd_soc_dapm_mux:
3331 case snd_soc_dapm_demux:
3332 dapm_new_mux(w);
3333 break;
3334 case snd_soc_dapm_pga:
3335 case snd_soc_dapm_effect:
3336 case snd_soc_dapm_out_drv:
3337 dapm_new_pga(w);
3338 break;
3339 case snd_soc_dapm_dai_link:
3340 dapm_new_dai_link(w);
3341 break;
3342 default:
3343 break;
3346 /* Read the initial power state from the device */
3347 if (w->reg >= 0) {
3348 val = soc_dapm_read(w->dapm, w->reg);
3349 val = val >> w->shift;
3350 val &= w->mask;
3351 if (val == w->on_val)
3352 w->power = 1;
3355 w->new = 1;
3357 dapm_mark_dirty(w, "new widget");
3358 dapm_debugfs_add_widget(w);
3361 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3362 snd_soc_dapm_mutex_unlock(card);
3363 return 0;
3365 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3368 * snd_soc_dapm_get_volsw - dapm mixer get callback
3369 * @kcontrol: mixer control
3370 * @ucontrol: control element information
3372 * Callback to get the value of a dapm mixer control.
3374 * Returns 0 for success.
3376 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3377 struct snd_ctl_elem_value *ucontrol)
3379 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3380 struct soc_mixer_control *mc =
3381 (struct soc_mixer_control *)kcontrol->private_value;
3382 int reg = mc->reg;
3383 unsigned int shift = mc->shift;
3384 int max = mc->max;
3385 unsigned int width = fls(max);
3386 unsigned int mask = (1 << fls(max)) - 1;
3387 unsigned int invert = mc->invert;
3388 unsigned int reg_val, val, rval = 0;
3390 snd_soc_dapm_mutex_lock(dapm);
3391 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3392 reg_val = soc_dapm_read(dapm, reg);
3393 val = (reg_val >> shift) & mask;
3395 if (reg != mc->rreg)
3396 reg_val = soc_dapm_read(dapm, mc->rreg);
3398 if (snd_soc_volsw_is_stereo(mc))
3399 rval = (reg_val >> mc->rshift) & mask;
3400 } else {
3401 reg_val = dapm_kcontrol_get_value(kcontrol);
3402 val = reg_val & mask;
3404 if (snd_soc_volsw_is_stereo(mc))
3405 rval = (reg_val >> width) & mask;
3407 snd_soc_dapm_mutex_unlock(dapm);
3409 if (invert)
3410 ucontrol->value.integer.value[0] = max - val;
3411 else
3412 ucontrol->value.integer.value[0] = val;
3414 if (snd_soc_volsw_is_stereo(mc)) {
3415 if (invert)
3416 ucontrol->value.integer.value[1] = max - rval;
3417 else
3418 ucontrol->value.integer.value[1] = rval;
3421 return 0;
3423 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3426 * snd_soc_dapm_put_volsw - dapm mixer set callback
3427 * @kcontrol: mixer control
3428 * @ucontrol: control element information
3430 * Callback to set the value of a dapm mixer control.
3432 * Returns 0 for success.
3434 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3435 struct snd_ctl_elem_value *ucontrol)
3437 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3438 struct snd_soc_card *card = dapm->card;
3439 struct soc_mixer_control *mc =
3440 (struct soc_mixer_control *)kcontrol->private_value;
3441 int reg = mc->reg;
3442 unsigned int shift = mc->shift;
3443 int max = mc->max;
3444 unsigned int width = fls(max);
3445 unsigned int mask = (1 << width) - 1;
3446 unsigned int invert = mc->invert;
3447 unsigned int val, rval = 0;
3448 int connect, rconnect = -1, change, reg_change = 0;
3449 struct snd_soc_dapm_update update = {};
3450 int ret = 0;
3452 val = (ucontrol->value.integer.value[0] & mask);
3453 connect = !!val;
3455 if (invert)
3456 val = max - val;
3458 if (snd_soc_volsw_is_stereo(mc)) {
3459 rval = (ucontrol->value.integer.value[1] & mask);
3460 rconnect = !!rval;
3461 if (invert)
3462 rval = max - rval;
3465 snd_soc_dapm_mutex_lock(card);
3467 /* This assumes field width < (bits in unsigned int / 2) */
3468 if (width > sizeof(unsigned int) * 8 / 2)
3469 dev_warn(dapm->dev,
3470 "ASoC: control %s field width limit exceeded\n",
3471 kcontrol->id.name);
3472 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3474 if (reg != SND_SOC_NOPM) {
3475 val = val << shift;
3476 rval = rval << mc->rshift;
3478 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3480 if (snd_soc_volsw_is_stereo(mc))
3481 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3482 mask << mc->rshift,
3483 rval);
3486 if (change || reg_change) {
3487 if (reg_change) {
3488 if (snd_soc_volsw_is_stereo(mc)) {
3489 update.has_second_set = true;
3490 update.reg2 = mc->rreg;
3491 update.mask2 = mask << mc->rshift;
3492 update.val2 = rval;
3494 update.kcontrol = kcontrol;
3495 update.reg = reg;
3496 update.mask = mask << shift;
3497 update.val = val;
3498 card->update = &update;
3501 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3502 rconnect);
3504 card->update = NULL;
3507 snd_soc_dapm_mutex_unlock(card);
3509 if (ret > 0)
3510 snd_soc_dpcm_runtime_update(card);
3512 return change;
3514 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3517 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3518 * @kcontrol: mixer control
3519 * @ucontrol: control element information
3521 * Callback to get the value of a dapm enumerated double mixer control.
3523 * Returns 0 for success.
3525 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3526 struct snd_ctl_elem_value *ucontrol)
3528 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3529 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3530 unsigned int reg_val, val;
3532 snd_soc_dapm_mutex_lock(dapm);
3533 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3534 reg_val = soc_dapm_read(dapm, e->reg);
3535 } else {
3536 reg_val = dapm_kcontrol_get_value(kcontrol);
3538 snd_soc_dapm_mutex_unlock(dapm);
3540 val = (reg_val >> e->shift_l) & e->mask;
3541 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3542 if (e->shift_l != e->shift_r) {
3543 val = (reg_val >> e->shift_r) & e->mask;
3544 val = snd_soc_enum_val_to_item(e, val);
3545 ucontrol->value.enumerated.item[1] = val;
3548 return 0;
3550 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3553 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3554 * @kcontrol: mixer control
3555 * @ucontrol: control element information
3557 * Callback to set the value of a dapm enumerated double mixer control.
3559 * Returns 0 for success.
3561 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3562 struct snd_ctl_elem_value *ucontrol)
3564 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3565 struct snd_soc_card *card = dapm->card;
3566 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3567 unsigned int *item = ucontrol->value.enumerated.item;
3568 unsigned int val, change, reg_change = 0;
3569 unsigned int mask;
3570 struct snd_soc_dapm_update update = {};
3571 int ret = 0;
3573 if (item[0] >= e->items)
3574 return -EINVAL;
3576 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3577 mask = e->mask << e->shift_l;
3578 if (e->shift_l != e->shift_r) {
3579 if (item[1] > e->items)
3580 return -EINVAL;
3581 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3582 mask |= e->mask << e->shift_r;
3585 snd_soc_dapm_mutex_lock(card);
3587 change = dapm_kcontrol_set_value(kcontrol, val);
3589 if (e->reg != SND_SOC_NOPM)
3590 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3592 if (change || reg_change) {
3593 if (reg_change) {
3594 update.kcontrol = kcontrol;
3595 update.reg = e->reg;
3596 update.mask = mask;
3597 update.val = val;
3598 card->update = &update;
3601 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3603 card->update = NULL;
3606 snd_soc_dapm_mutex_unlock(card);
3608 if (ret > 0)
3609 snd_soc_dpcm_runtime_update(card);
3611 return change;
3613 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3616 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3618 * @kcontrol: mixer control
3619 * @uinfo: control element information
3621 * Callback to provide information about a pin switch control.
3623 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3624 struct snd_ctl_elem_info *uinfo)
3626 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3627 uinfo->count = 1;
3628 uinfo->value.integer.min = 0;
3629 uinfo->value.integer.max = 1;
3631 return 0;
3633 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3636 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3638 * @kcontrol: mixer control
3639 * @ucontrol: Value
3641 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3642 struct snd_ctl_elem_value *ucontrol)
3644 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3645 const char *pin = (const char *)kcontrol->private_value;
3647 snd_soc_dapm_mutex_lock(card);
3649 ucontrol->value.integer.value[0] =
3650 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3652 snd_soc_dapm_mutex_unlock(card);
3654 return 0;
3656 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3659 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3661 * @kcontrol: mixer control
3662 * @ucontrol: Value
3664 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3665 struct snd_ctl_elem_value *ucontrol)
3667 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3668 const char *pin = (const char *)kcontrol->private_value;
3669 int ret;
3671 snd_soc_dapm_mutex_lock(card);
3672 ret = __snd_soc_dapm_set_pin(&card->dapm, pin,
3673 !!ucontrol->value.integer.value[0]);
3674 snd_soc_dapm_mutex_unlock(card);
3676 snd_soc_dapm_sync(&card->dapm);
3677 return ret;
3679 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3681 struct snd_soc_dapm_widget *
3682 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3683 const struct snd_soc_dapm_widget *widget)
3685 enum snd_soc_dapm_direction dir;
3686 struct snd_soc_dapm_widget *w;
3687 int ret = -ENOMEM;
3689 w = dapm_cnew_widget(widget, soc_dapm_prefix(dapm));
3690 if (!w)
3691 goto cnew_failed;
3693 switch (w->id) {
3694 case snd_soc_dapm_regulator_supply:
3695 w->regulator = devm_regulator_get(dapm->dev, widget->name);
3696 if (IS_ERR(w->regulator)) {
3697 ret = PTR_ERR(w->regulator);
3698 goto request_failed;
3701 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3702 ret = regulator_allow_bypass(w->regulator, true);
3703 if (ret != 0)
3704 dev_warn(dapm->dev,
3705 "ASoC: Failed to bypass %s: %d\n",
3706 w->name, ret);
3708 break;
3709 case snd_soc_dapm_pinctrl:
3710 w->pinctrl = devm_pinctrl_get(dapm->dev);
3711 if (IS_ERR(w->pinctrl)) {
3712 ret = PTR_ERR(w->pinctrl);
3713 goto request_failed;
3716 /* set to sleep_state when initializing */
3717 dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD);
3718 break;
3719 case snd_soc_dapm_clock_supply:
3720 w->clk = devm_clk_get(dapm->dev, widget->name);
3721 if (IS_ERR(w->clk)) {
3722 ret = PTR_ERR(w->clk);
3723 goto request_failed;
3725 break;
3726 default:
3727 break;
3730 switch (w->id) {
3731 case snd_soc_dapm_mic:
3732 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3733 w->power_check = dapm_generic_check_power;
3734 break;
3735 case snd_soc_dapm_input:
3736 if (!dapm->card->fully_routed)
3737 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3738 w->power_check = dapm_generic_check_power;
3739 break;
3740 case snd_soc_dapm_spk:
3741 case snd_soc_dapm_hp:
3742 w->is_ep = SND_SOC_DAPM_EP_SINK;
3743 w->power_check = dapm_generic_check_power;
3744 break;
3745 case snd_soc_dapm_output:
3746 if (!dapm->card->fully_routed)
3747 w->is_ep = SND_SOC_DAPM_EP_SINK;
3748 w->power_check = dapm_generic_check_power;
3749 break;
3750 case snd_soc_dapm_vmid:
3751 case snd_soc_dapm_siggen:
3752 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3753 w->power_check = dapm_always_on_check_power;
3754 break;
3755 case snd_soc_dapm_sink:
3756 w->is_ep = SND_SOC_DAPM_EP_SINK;
3757 w->power_check = dapm_always_on_check_power;
3758 break;
3760 case snd_soc_dapm_mux:
3761 case snd_soc_dapm_demux:
3762 case snd_soc_dapm_switch:
3763 case snd_soc_dapm_mixer:
3764 case snd_soc_dapm_mixer_named_ctl:
3765 case snd_soc_dapm_adc:
3766 case snd_soc_dapm_aif_out:
3767 case snd_soc_dapm_dac:
3768 case snd_soc_dapm_aif_in:
3769 case snd_soc_dapm_pga:
3770 case snd_soc_dapm_buffer:
3771 case snd_soc_dapm_scheduler:
3772 case snd_soc_dapm_effect:
3773 case snd_soc_dapm_src:
3774 case snd_soc_dapm_asrc:
3775 case snd_soc_dapm_encoder:
3776 case snd_soc_dapm_decoder:
3777 case snd_soc_dapm_out_drv:
3778 case snd_soc_dapm_micbias:
3779 case snd_soc_dapm_line:
3780 case snd_soc_dapm_dai_link:
3781 case snd_soc_dapm_dai_out:
3782 case snd_soc_dapm_dai_in:
3783 w->power_check = dapm_generic_check_power;
3784 break;
3785 case snd_soc_dapm_supply:
3786 case snd_soc_dapm_regulator_supply:
3787 case snd_soc_dapm_pinctrl:
3788 case snd_soc_dapm_clock_supply:
3789 case snd_soc_dapm_kcontrol:
3790 w->is_supply = 1;
3791 w->power_check = dapm_supply_check_power;
3792 break;
3793 default:
3794 w->power_check = dapm_always_on_check_power;
3795 break;
3798 w->dapm = dapm;
3799 INIT_LIST_HEAD(&w->list);
3800 INIT_LIST_HEAD(&w->dirty);
3801 /* see for_each_card_widgets */
3802 list_add_tail(&w->list, &dapm->card->widgets);
3804 snd_soc_dapm_for_each_direction(dir) {
3805 INIT_LIST_HEAD(&w->edges[dir]);
3806 w->endpoints[dir] = -1;
3809 /* machine layer sets up unconnected pins and insertions */
3810 w->connected = 1;
3811 return w;
3813 request_failed:
3814 dev_err_probe(dapm->dev, ret, "ASoC: Failed to request %s\n",
3815 w->name);
3816 kfree_const(w->name);
3817 kfree_const(w->sname);
3818 kfree(w);
3819 cnew_failed:
3820 return ERR_PTR(ret);
3824 * snd_soc_dapm_new_control - create new dapm control
3825 * @dapm: DAPM context
3826 * @widget: widget template
3828 * Creates new DAPM control based upon a template.
3830 * Returns a widget pointer on success or an error pointer on failure
3832 struct snd_soc_dapm_widget *
3833 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3834 const struct snd_soc_dapm_widget *widget)
3836 struct snd_soc_dapm_widget *w;
3838 snd_soc_dapm_mutex_lock(dapm);
3839 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3840 snd_soc_dapm_mutex_unlock(dapm);
3842 return w;
3844 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3847 * snd_soc_dapm_new_controls - create new dapm controls
3848 * @dapm: DAPM context
3849 * @widget: widget array
3850 * @num: number of widgets
3852 * Creates new DAPM controls based upon the templates.
3854 * Returns 0 for success else error.
3856 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3857 const struct snd_soc_dapm_widget *widget,
3858 unsigned int num)
3860 int i;
3861 int ret = 0;
3863 snd_soc_dapm_mutex_lock_root(dapm);
3864 for (i = 0; i < num; i++) {
3865 struct snd_soc_dapm_widget *w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3866 if (IS_ERR(w)) {
3867 ret = PTR_ERR(w);
3868 break;
3870 widget++;
3872 snd_soc_dapm_mutex_unlock(dapm);
3873 return ret;
3875 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3877 static int
3878 snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3879 struct snd_pcm_substream *substream)
3881 struct snd_soc_dapm_path *path;
3882 struct snd_soc_dai *source, *sink;
3883 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
3884 const struct snd_soc_pcm_stream *config = NULL;
3885 struct snd_pcm_runtime *runtime = NULL;
3886 unsigned int fmt;
3887 int ret;
3890 * NOTE
3892 * snd_pcm_hw_params is quite large (608 bytes on arm64) and is
3893 * starting to get a bit excessive for allocation on the stack,
3894 * especially when you're building with some of the KASAN type
3895 * stuff that increases stack usage.
3896 * So, we use kzalloc()/kfree() for params in this function.
3898 struct snd_pcm_hw_params *params __free(kfree) = kzalloc(sizeof(*params),
3899 GFP_KERNEL);
3900 if (!params)
3901 return -ENOMEM;
3903 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3904 if (!runtime)
3905 return -ENOMEM;
3907 substream->runtime = runtime;
3909 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3910 snd_soc_dapm_widget_for_each_source_path(w, path) {
3911 source = path->source->priv;
3913 ret = snd_soc_dai_startup(source, substream);
3914 if (ret < 0)
3915 return ret;
3917 snd_soc_dai_activate(source, substream->stream);
3920 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3921 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3922 sink = path->sink->priv;
3924 ret = snd_soc_dai_startup(sink, substream);
3925 if (ret < 0)
3926 return ret;
3928 snd_soc_dai_activate(sink, substream->stream);
3931 substream->hw_opened = 1;
3934 * Note: getting the config after .startup() gives a chance to
3935 * either party on the link to alter the configuration if
3936 * necessary
3938 config = rtd->dai_link->c2c_params + rtd->c2c_params_select;
3939 if (!config) {
3940 dev_err(w->dapm->dev, "ASoC: link config missing\n");
3941 return -EINVAL;
3944 /* Be a little careful as we don't want to overflow the mask array */
3945 if (!config->formats) {
3946 dev_warn(w->dapm->dev, "ASoC: Invalid format was specified\n");
3948 return -EINVAL;
3951 fmt = ffs(config->formats) - 1;
3953 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3954 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3955 config->rate_min;
3956 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3957 config->rate_max;
3958 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3959 = config->channels_min;
3960 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3961 = config->channels_max;
3963 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3964 snd_soc_dapm_widget_for_each_source_path(w, path) {
3965 source = path->source->priv;
3967 ret = snd_soc_dai_hw_params(source, substream, params);
3968 if (ret < 0)
3969 return ret;
3971 dapm_update_dai_unlocked(substream, params, source);
3974 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3975 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3976 sink = path->sink->priv;
3978 ret = snd_soc_dai_hw_params(sink, substream, params);
3979 if (ret < 0)
3980 return ret;
3982 dapm_update_dai_unlocked(substream, params, sink);
3985 runtime->format = params_format(params);
3986 runtime->subformat = params_subformat(params);
3987 runtime->channels = params_channels(params);
3988 runtime->rate = params_rate(params);
3990 return 0;
3993 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3994 struct snd_kcontrol *kcontrol, int event)
3996 struct snd_soc_dapm_path *path;
3997 struct snd_soc_dai *source, *sink;
3998 struct snd_pcm_substream *substream = w->priv;
3999 int ret = 0, saved_stream = substream->stream;
4001 if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
4002 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
4003 return -EINVAL;
4005 switch (event) {
4006 case SND_SOC_DAPM_PRE_PMU:
4007 ret = snd_soc_dai_link_event_pre_pmu(w, substream);
4008 if (ret < 0)
4009 goto out;
4011 break;
4013 case SND_SOC_DAPM_POST_PMU:
4014 snd_soc_dapm_widget_for_each_sink_path(w, path) {
4015 sink = path->sink->priv;
4017 snd_soc_dai_digital_mute(sink, 0, SNDRV_PCM_STREAM_PLAYBACK);
4018 ret = 0;
4020 break;
4022 case SND_SOC_DAPM_PRE_PMD:
4023 snd_soc_dapm_widget_for_each_sink_path(w, path) {
4024 sink = path->sink->priv;
4026 snd_soc_dai_digital_mute(sink, 1, SNDRV_PCM_STREAM_PLAYBACK);
4027 ret = 0;
4030 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
4031 snd_soc_dapm_widget_for_each_source_path(w, path) {
4032 source = path->source->priv;
4033 snd_soc_dai_hw_free(source, substream, 0);
4036 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
4037 snd_soc_dapm_widget_for_each_sink_path(w, path) {
4038 sink = path->sink->priv;
4039 snd_soc_dai_hw_free(sink, substream, 0);
4042 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
4043 snd_soc_dapm_widget_for_each_source_path(w, path) {
4044 source = path->source->priv;
4045 snd_soc_dai_deactivate(source, substream->stream);
4046 snd_soc_dai_shutdown(source, substream, 0);
4049 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
4050 snd_soc_dapm_widget_for_each_sink_path(w, path) {
4051 sink = path->sink->priv;
4052 snd_soc_dai_deactivate(sink, substream->stream);
4053 snd_soc_dai_shutdown(sink, substream, 0);
4055 break;
4057 case SND_SOC_DAPM_POST_PMD:
4058 kfree(substream->runtime);
4059 substream->runtime = NULL;
4060 break;
4062 default:
4063 WARN(1, "Unknown event %d\n", event);
4064 ret = -EINVAL;
4067 out:
4068 /* Restore the substream direction */
4069 substream->stream = saved_stream;
4070 return ret;
4073 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
4074 struct snd_ctl_elem_value *ucontrol)
4076 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4077 struct snd_soc_pcm_runtime *rtd = w->priv;
4079 ucontrol->value.enumerated.item[0] = rtd->c2c_params_select;
4081 return 0;
4084 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
4085 struct snd_ctl_elem_value *ucontrol)
4087 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4088 struct snd_soc_pcm_runtime *rtd = w->priv;
4090 /* Can't change the config when widget is already powered */
4091 if (w->power)
4092 return -EBUSY;
4094 if (ucontrol->value.enumerated.item[0] == rtd->c2c_params_select)
4095 return 0;
4097 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_c2c_params)
4098 return -EINVAL;
4100 rtd->c2c_params_select = ucontrol->value.enumerated.item[0];
4102 return 1;
4105 static void
4106 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
4107 unsigned long *private_value,
4108 int num_c2c_params,
4109 const char **w_param_text)
4111 int count;
4113 devm_kfree(card->dev, (void *)*private_value);
4115 if (!w_param_text)
4116 return;
4118 for (count = 0 ; count < num_c2c_params; count++)
4119 devm_kfree(card->dev, (void *)w_param_text[count]);
4120 devm_kfree(card->dev, w_param_text);
4123 static struct snd_kcontrol_new *
4124 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
4125 char *link_name,
4126 const struct snd_soc_pcm_stream *c2c_params,
4127 int num_c2c_params, const char **w_param_text,
4128 unsigned long *private_value)
4130 struct soc_enum w_param_enum[] = {
4131 SOC_ENUM_SINGLE(0, 0, 0, NULL),
4133 struct snd_kcontrol_new kcontrol_dai_link[] = {
4134 SOC_ENUM_EXT(NULL, w_param_enum[0],
4135 snd_soc_dapm_dai_link_get,
4136 snd_soc_dapm_dai_link_put),
4138 struct snd_kcontrol_new *kcontrol_news;
4139 const struct snd_soc_pcm_stream *config = c2c_params;
4140 int count;
4142 for (count = 0 ; count < num_c2c_params; count++) {
4143 if (!config->stream_name) {
4144 dev_warn(card->dapm.dev,
4145 "ASoC: anonymous config %d for dai link %s\n",
4146 count, link_name);
4147 w_param_text[count] =
4148 devm_kasprintf(card->dev, GFP_KERNEL,
4149 "Anonymous Configuration %d",
4150 count);
4151 } else {
4152 w_param_text[count] = devm_kmemdup(card->dev,
4153 config->stream_name,
4154 strlen(config->stream_name) + 1,
4155 GFP_KERNEL);
4157 if (!w_param_text[count])
4158 goto outfree_w_param;
4159 config++;
4162 w_param_enum[0].items = num_c2c_params;
4163 w_param_enum[0].texts = w_param_text;
4165 *private_value =
4166 (unsigned long) devm_kmemdup(card->dev,
4167 (void *)(kcontrol_dai_link[0].private_value),
4168 sizeof(struct soc_enum), GFP_KERNEL);
4169 if (!*private_value) {
4170 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4171 link_name);
4172 goto outfree_w_param;
4174 kcontrol_dai_link[0].private_value = *private_value;
4175 /* duplicate kcontrol_dai_link on heap so that memory persists */
4176 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4177 sizeof(struct snd_kcontrol_new),
4178 GFP_KERNEL);
4179 if (!kcontrol_news) {
4180 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4181 link_name);
4182 goto outfree_w_param;
4184 return kcontrol_news;
4186 outfree_w_param:
4187 snd_soc_dapm_free_kcontrol(card, private_value, num_c2c_params, w_param_text);
4188 return NULL;
4191 static struct snd_soc_dapm_widget *
4192 snd_soc_dapm_new_dai(struct snd_soc_card *card,
4193 struct snd_pcm_substream *substream,
4194 char *id)
4196 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
4197 struct snd_soc_dapm_widget template;
4198 struct snd_soc_dapm_widget *w;
4199 const struct snd_kcontrol_new *kcontrol_news;
4200 int num_kcontrols;
4201 const char **w_param_text;
4202 unsigned long private_value = 0;
4203 char *link_name;
4204 int ret = -ENOMEM;
4206 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
4207 rtd->dai_link->name, id);
4208 if (!link_name)
4209 goto name_fail;
4211 /* allocate memory for control, only in case of multiple configs */
4212 w_param_text = NULL;
4213 kcontrol_news = NULL;
4214 num_kcontrols = 0;
4215 if (rtd->dai_link->num_c2c_params > 1) {
4216 w_param_text = devm_kcalloc(card->dev,
4217 rtd->dai_link->num_c2c_params,
4218 sizeof(char *), GFP_KERNEL);
4219 if (!w_param_text)
4220 goto param_fail;
4222 num_kcontrols = 1;
4223 kcontrol_news = snd_soc_dapm_alloc_kcontrol(card, link_name,
4224 rtd->dai_link->c2c_params,
4225 rtd->dai_link->num_c2c_params,
4226 w_param_text, &private_value);
4227 if (!kcontrol_news)
4228 goto param_fail;
4231 memset(&template, 0, sizeof(template));
4232 template.reg = SND_SOC_NOPM;
4233 template.id = snd_soc_dapm_dai_link;
4234 template.name = link_name;
4235 template.event = snd_soc_dai_link_event;
4236 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
4237 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD;
4238 template.kcontrol_news = kcontrol_news;
4239 template.num_kcontrols = num_kcontrols;
4241 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
4243 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
4244 if (IS_ERR(w)) {
4245 ret = PTR_ERR(w);
4246 goto outfree_kcontrol_news;
4249 w->priv = substream;
4251 return w;
4253 outfree_kcontrol_news:
4254 devm_kfree(card->dev, (void *)template.kcontrol_news);
4255 snd_soc_dapm_free_kcontrol(card, &private_value,
4256 rtd->dai_link->num_c2c_params, w_param_text);
4257 param_fail:
4258 devm_kfree(card->dev, link_name);
4259 name_fail:
4260 dev_err(rtd->dev, "ASoC: Failed to create %s-%s widget: %d\n",
4261 rtd->dai_link->name, id, ret);
4262 return ERR_PTR(ret);
4266 * snd_soc_dapm_new_dai_widgets - Create new DAPM widgets
4267 * @dapm: DAPM context
4268 * @dai: parent DAI
4270 * Returns 0 on success, error code otherwise.
4272 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4273 struct snd_soc_dai *dai)
4275 struct snd_soc_dapm_widget template;
4276 struct snd_soc_dapm_widget *w;
4278 WARN_ON(dapm->dev != dai->dev);
4280 memset(&template, 0, sizeof(template));
4281 template.reg = SND_SOC_NOPM;
4283 if (dai->driver->playback.stream_name) {
4284 template.id = snd_soc_dapm_dai_in;
4285 template.name = dai->driver->playback.stream_name;
4286 template.sname = dai->driver->playback.stream_name;
4288 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4289 template.name);
4291 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4292 if (IS_ERR(w))
4293 return PTR_ERR(w);
4295 w->priv = dai;
4296 snd_soc_dai_set_widget_playback(dai, w);
4299 if (dai->driver->capture.stream_name) {
4300 template.id = snd_soc_dapm_dai_out;
4301 template.name = dai->driver->capture.stream_name;
4302 template.sname = dai->driver->capture.stream_name;
4304 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4305 template.name);
4307 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4308 if (IS_ERR(w))
4309 return PTR_ERR(w);
4311 w->priv = dai;
4312 snd_soc_dai_set_widget_capture(dai, w);
4315 return 0;
4317 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_dai_widgets);
4319 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4321 struct snd_soc_dapm_widget *dai_w, *w;
4322 struct snd_soc_dapm_widget *src, *sink;
4323 struct snd_soc_dai *dai;
4325 /* For each DAI widget... */
4326 for_each_card_widgets(card, dai_w) {
4327 switch (dai_w->id) {
4328 case snd_soc_dapm_dai_in:
4329 case snd_soc_dapm_dai_out:
4330 break;
4331 default:
4332 continue;
4335 /* let users know there is no DAI to link */
4336 if (!dai_w->priv) {
4337 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4338 dai_w->name);
4339 continue;
4342 dai = dai_w->priv;
4344 /* ...find all widgets with the same stream and link them */
4345 for_each_card_widgets(card, w) {
4346 if (w->dapm != dai_w->dapm)
4347 continue;
4349 switch (w->id) {
4350 case snd_soc_dapm_dai_in:
4351 case snd_soc_dapm_dai_out:
4352 continue;
4353 default:
4354 break;
4357 if (!w->sname || !strstr(w->sname, dai_w->sname))
4358 continue;
4360 if (dai_w->id == snd_soc_dapm_dai_in) {
4361 src = dai_w;
4362 sink = w;
4363 } else {
4364 src = w;
4365 sink = dai_w;
4367 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4368 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4372 return 0;
4375 static void dapm_connect_dai_routes(struct snd_soc_dapm_context *dapm,
4376 struct snd_soc_dai *src_dai,
4377 struct snd_soc_dapm_widget *src,
4378 struct snd_soc_dapm_widget *dai,
4379 struct snd_soc_dai *sink_dai,
4380 struct snd_soc_dapm_widget *sink)
4382 dev_dbg(dapm->dev, "connected DAI link %s:%s -> %s:%s\n",
4383 src_dai->component->name, src->name,
4384 sink_dai->component->name, sink->name);
4386 if (dai) {
4387 snd_soc_dapm_add_path(dapm, src, dai, NULL, NULL);
4388 src = dai;
4391 snd_soc_dapm_add_path(dapm, src, sink, NULL, NULL);
4394 static void dapm_connect_dai_pair(struct snd_soc_card *card,
4395 struct snd_soc_pcm_runtime *rtd,
4396 struct snd_soc_dai *codec_dai,
4397 struct snd_soc_dai *cpu_dai)
4399 struct snd_soc_dai_link *dai_link = rtd->dai_link;
4400 struct snd_soc_dapm_widget *codec, *cpu;
4401 struct snd_soc_dai *src_dai[] = { cpu_dai, codec_dai };
4402 struct snd_soc_dai *sink_dai[] = { codec_dai, cpu_dai };
4403 struct snd_soc_dapm_widget **src[] = { &cpu, &codec };
4404 struct snd_soc_dapm_widget **sink[] = { &codec, &cpu };
4405 char *widget_name[] = { "playback", "capture" };
4406 int stream;
4408 for_each_pcm_streams(stream) {
4409 int stream_cpu, stream_codec;
4411 stream_cpu = snd_soc_get_stream_cpu(dai_link, stream);
4412 stream_codec = stream;
4414 /* connect BE DAI playback if widgets are valid */
4415 cpu = snd_soc_dai_get_widget(cpu_dai, stream_cpu);
4416 codec = snd_soc_dai_get_widget(codec_dai, stream_codec);
4418 if (!cpu || !codec)
4419 continue;
4421 /* special handling for [Codec2Codec] */
4422 if (dai_link->c2c_params && !rtd->c2c_widget[stream]) {
4423 struct snd_pcm_substream *substream = rtd->pcm->streams[stream].substream;
4424 struct snd_soc_dapm_widget *dai = snd_soc_dapm_new_dai(card, substream,
4425 widget_name[stream]);
4427 if (IS_ERR(dai))
4428 continue;
4430 rtd->c2c_widget[stream] = dai;
4433 dapm_connect_dai_routes(&card->dapm, src_dai[stream], *src[stream],
4434 rtd->c2c_widget[stream],
4435 sink_dai[stream], *sink[stream]);
4439 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4440 int event)
4442 struct snd_soc_dapm_widget *w;
4444 w = snd_soc_dai_get_widget(dai, stream);
4446 if (w) {
4447 unsigned int ep;
4449 dapm_mark_dirty(w, "stream event");
4451 if (w->id == snd_soc_dapm_dai_in) {
4452 ep = SND_SOC_DAPM_EP_SOURCE;
4453 dapm_widget_invalidate_input_paths(w);
4454 } else {
4455 ep = SND_SOC_DAPM_EP_SINK;
4456 dapm_widget_invalidate_output_paths(w);
4459 switch (event) {
4460 case SND_SOC_DAPM_STREAM_START:
4461 w->active = 1;
4462 w->is_ep = ep;
4463 break;
4464 case SND_SOC_DAPM_STREAM_STOP:
4465 w->active = 0;
4466 w->is_ep = 0;
4467 break;
4468 case SND_SOC_DAPM_STREAM_SUSPEND:
4469 case SND_SOC_DAPM_STREAM_RESUME:
4470 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4471 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4472 break;
4477 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4479 struct snd_soc_pcm_runtime *rtd;
4480 struct snd_soc_dai *cpu_dai;
4481 struct snd_soc_dai *codec_dai;
4483 /* for each BE DAI link... */
4484 for_each_card_rtds(card, rtd) {
4485 struct snd_soc_dai_link_ch_map *ch_maps;
4486 int i;
4489 * dynamic FE links have no fixed DAI mapping.
4490 * CODEC<->CODEC links have no direct connection.
4492 if (rtd->dai_link->dynamic)
4493 continue;
4496 * see
4497 * soc.h :: [dai_link->ch_maps Image sample]
4499 for_each_rtd_ch_maps(rtd, i, ch_maps) {
4500 cpu_dai = snd_soc_rtd_to_cpu(rtd, ch_maps->cpu);
4501 codec_dai = snd_soc_rtd_to_codec(rtd, ch_maps->codec);
4503 dapm_connect_dai_pair(card, rtd, codec_dai, cpu_dai);
4508 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4509 int event)
4511 struct snd_soc_dai *dai;
4512 int i;
4514 for_each_rtd_dais(rtd, i, dai)
4515 soc_dapm_dai_stream_event(dai, stream, event);
4517 dapm_power_widgets(rtd->card, event);
4521 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4522 * @rtd: PCM runtime data
4523 * @stream: stream name
4524 * @event: stream event
4526 * Sends a stream event to the dapm core. The core then makes any
4527 * necessary widget power changes.
4529 * Returns 0 for success else error.
4531 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4532 int event)
4534 struct snd_soc_card *card = rtd->card;
4536 snd_soc_dapm_mutex_lock(card);
4537 soc_dapm_stream_event(rtd, stream, event);
4538 snd_soc_dapm_mutex_unlock(card);
4541 void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream)
4543 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
4544 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
4545 /* powered down playback stream now */
4546 snd_soc_dapm_stream_event(rtd,
4547 SNDRV_PCM_STREAM_PLAYBACK,
4548 SND_SOC_DAPM_STREAM_STOP);
4549 } else {
4550 /* start delayed pop wq here for playback streams */
4551 rtd->pop_wait = 1;
4552 queue_delayed_work(system_power_efficient_wq,
4553 &rtd->delayed_work,
4554 msecs_to_jiffies(rtd->pmdown_time));
4556 } else {
4557 /* capture streams can be powered down now */
4558 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
4559 SND_SOC_DAPM_STREAM_STOP);
4562 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop);
4565 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4566 * @dapm: DAPM context
4567 * @pin: pin name
4569 * Enables input/output pin and its parents or children widgets iff there is
4570 * a valid audio route and active audio stream.
4572 * Requires external locking.
4574 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4575 * do any widget power switching.
4577 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4578 const char *pin)
4580 return snd_soc_dapm_set_pin(dapm, pin, 1);
4582 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4585 * snd_soc_dapm_enable_pin - enable pin.
4586 * @dapm: DAPM context
4587 * @pin: pin name
4589 * Enables input/output pin and its parents or children widgets iff there is
4590 * a valid audio route and active audio stream.
4592 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4593 * do any widget power switching.
4595 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4597 int ret;
4599 snd_soc_dapm_mutex_lock(dapm);
4601 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4603 snd_soc_dapm_mutex_unlock(dapm);
4605 return ret;
4607 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4610 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4611 * @dapm: DAPM context
4612 * @pin: pin name
4614 * Enables input/output pin regardless of any other state. This is
4615 * intended for use with microphone bias supplies used in microphone
4616 * jack detection.
4618 * Requires external locking.
4620 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4621 * do any widget power switching.
4623 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4624 const char *pin)
4626 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4628 if (!w) {
4629 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4630 return -EINVAL;
4633 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4634 if (!w->connected) {
4636 * w->force does not affect the number of input or output paths,
4637 * so we only have to recheck if w->connected is changed
4639 dapm_widget_invalidate_input_paths(w);
4640 dapm_widget_invalidate_output_paths(w);
4641 w->connected = 1;
4643 w->force = 1;
4644 dapm_mark_dirty(w, "force enable");
4646 return 0;
4648 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4651 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4652 * @dapm: DAPM context
4653 * @pin: pin name
4655 * Enables input/output pin regardless of any other state. This is
4656 * intended for use with microphone bias supplies used in microphone
4657 * jack detection.
4659 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4660 * do any widget power switching.
4662 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4663 const char *pin)
4665 int ret;
4667 snd_soc_dapm_mutex_lock(dapm);
4669 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4671 snd_soc_dapm_mutex_unlock(dapm);
4673 return ret;
4675 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4678 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4679 * @dapm: DAPM context
4680 * @pin: pin name
4682 * Disables input/output pin and its parents or children widgets.
4684 * Requires external locking.
4686 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4687 * do any widget power switching.
4689 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4690 const char *pin)
4692 return snd_soc_dapm_set_pin(dapm, pin, 0);
4694 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4697 * snd_soc_dapm_disable_pin - disable pin.
4698 * @dapm: DAPM context
4699 * @pin: pin name
4701 * Disables input/output pin and its parents or children widgets.
4703 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4704 * do any widget power switching.
4706 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4707 const char *pin)
4709 int ret;
4711 snd_soc_dapm_mutex_lock(dapm);
4713 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4715 snd_soc_dapm_mutex_unlock(dapm);
4717 return ret;
4719 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4722 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4723 * @dapm: DAPM context
4724 * @pin: pin name
4726 * Marks the specified pin as being not connected, disabling it along
4727 * any parent or child widgets. At present this is identical to
4728 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4729 * additional things such as disabling controls which only affect
4730 * paths through the pin.
4732 * Requires external locking.
4734 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4735 * do any widget power switching.
4737 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4738 const char *pin)
4740 return snd_soc_dapm_set_pin(dapm, pin, 0);
4742 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4745 * snd_soc_dapm_nc_pin - permanently disable pin.
4746 * @dapm: DAPM context
4747 * @pin: pin name
4749 * Marks the specified pin as being not connected, disabling it along
4750 * any parent or child widgets. At present this is identical to
4751 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4752 * additional things such as disabling controls which only affect
4753 * paths through the pin.
4755 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4756 * do any widget power switching.
4758 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4760 int ret;
4762 snd_soc_dapm_mutex_lock(dapm);
4764 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4766 snd_soc_dapm_mutex_unlock(dapm);
4768 return ret;
4770 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4773 * snd_soc_dapm_get_pin_status - get audio pin status
4774 * @dapm: DAPM context
4775 * @pin: audio signal pin endpoint (or start point)
4777 * Get audio pin status - connected or disconnected.
4779 * Returns 1 for connected otherwise 0.
4781 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4782 const char *pin)
4784 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4786 if (w)
4787 return w->connected;
4789 return 0;
4791 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4794 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4795 * @dapm: DAPM context
4796 * @pin: audio signal pin endpoint (or start point)
4798 * Mark the given endpoint or pin as ignoring suspend. When the
4799 * system is disabled a path between two endpoints flagged as ignoring
4800 * suspend will not be disabled. The path must already be enabled via
4801 * normal means at suspend time, it will not be turned on if it was not
4802 * already enabled.
4804 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4805 const char *pin)
4807 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4809 if (!w) {
4810 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4811 return -EINVAL;
4814 w->ignore_suspend = 1;
4816 return 0;
4818 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4821 * snd_soc_dapm_free - free dapm resources
4822 * @dapm: DAPM context
4824 * Free all dapm widgets and resources.
4826 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4828 dapm_debugfs_cleanup(dapm);
4829 dapm_free_widgets(dapm);
4830 list_del(&dapm->list);
4832 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4834 void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
4835 struct snd_soc_card *card,
4836 struct snd_soc_component *component)
4838 dapm->card = card;
4839 dapm->component = component;
4840 dapm->bias_level = SND_SOC_BIAS_OFF;
4842 if (component) {
4843 dapm->dev = component->dev;
4844 dapm->idle_bias_off = !component->driver->idle_bias_on;
4845 dapm->suspend_bias_off = component->driver->suspend_bias_off;
4846 } else {
4847 dapm->dev = card->dev;
4850 INIT_LIST_HEAD(&dapm->list);
4851 /* see for_each_card_dapms */
4852 list_add(&dapm->list, &card->dapm_list);
4854 EXPORT_SYMBOL_GPL(snd_soc_dapm_init);
4856 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4858 struct snd_soc_card *card = dapm->card;
4859 struct snd_soc_dapm_widget *w;
4860 LIST_HEAD(down_list);
4861 int powerdown = 0;
4863 snd_soc_dapm_mutex_lock_root(card);
4865 for_each_card_widgets(dapm->card, w) {
4866 if (w->dapm != dapm)
4867 continue;
4868 if (w->power) {
4869 dapm_seq_insert(w, &down_list, false);
4870 w->new_power = 0;
4871 powerdown = 1;
4875 /* If there were no widgets to power down we're already in
4876 * standby.
4878 if (powerdown) {
4879 if (dapm->bias_level == SND_SOC_BIAS_ON)
4880 snd_soc_dapm_set_bias_level(dapm,
4881 SND_SOC_BIAS_PREPARE);
4882 dapm_seq_run(card, &down_list, 0, false);
4883 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4884 snd_soc_dapm_set_bias_level(dapm,
4885 SND_SOC_BIAS_STANDBY);
4888 snd_soc_dapm_mutex_unlock(card);
4892 * snd_soc_dapm_shutdown - callback for system shutdown
4894 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4896 struct snd_soc_dapm_context *dapm;
4898 for_each_card_dapms(card, dapm) {
4899 if (dapm != &card->dapm) {
4900 soc_dapm_shutdown_dapm(dapm);
4901 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4902 snd_soc_dapm_set_bias_level(dapm,
4903 SND_SOC_BIAS_OFF);
4907 soc_dapm_shutdown_dapm(&card->dapm);
4908 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4909 snd_soc_dapm_set_bias_level(&card->dapm,
4910 SND_SOC_BIAS_OFF);
4913 /* Module information */
4914 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4915 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4916 MODULE_LICENSE("GPL");