2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/headphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed power down of audio subsystem to reduce pops between a quick
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
29 #include <linux/async.h>
30 #include <linux/delay.h>
32 #include <linux/bitops.h>
33 #include <linux/platform_device.h>
34 #include <linux/jiffies.h>
35 #include <linux/debugfs.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/clk.h>
39 #include <linux/slab.h>
40 #include <sound/core.h>
41 #include <sound/pcm.h>
42 #include <sound/pcm_params.h>
43 #include <sound/soc.h>
44 #include <sound/initval.h>
46 #include <trace/events/asoc.h>
48 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
50 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
51 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
53 #define snd_soc_dapm_for_each_direction(dir) \
54 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
57 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context
*dapm
,
58 struct snd_soc_dapm_widget
*wsource
, struct snd_soc_dapm_widget
*wsink
,
60 int (*connected
)(struct snd_soc_dapm_widget
*source
,
61 struct snd_soc_dapm_widget
*sink
));
63 struct snd_soc_dapm_widget
*
64 snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
65 const struct snd_soc_dapm_widget
*widget
);
67 struct snd_soc_dapm_widget
*
68 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context
*dapm
,
69 const struct snd_soc_dapm_widget
*widget
);
71 /* dapm power sequences - make this per codec in the future */
72 static int dapm_up_seq
[] = {
73 [snd_soc_dapm_pre
] = 0,
74 [snd_soc_dapm_regulator_supply
] = 1,
75 [snd_soc_dapm_clock_supply
] = 1,
76 [snd_soc_dapm_supply
] = 2,
77 [snd_soc_dapm_micbias
] = 3,
78 [snd_soc_dapm_dai_link
] = 2,
79 [snd_soc_dapm_dai_in
] = 4,
80 [snd_soc_dapm_dai_out
] = 4,
81 [snd_soc_dapm_aif_in
] = 4,
82 [snd_soc_dapm_aif_out
] = 4,
83 [snd_soc_dapm_mic
] = 5,
84 [snd_soc_dapm_mux
] = 6,
85 [snd_soc_dapm_demux
] = 6,
86 [snd_soc_dapm_dac
] = 7,
87 [snd_soc_dapm_switch
] = 8,
88 [snd_soc_dapm_mixer
] = 8,
89 [snd_soc_dapm_mixer_named_ctl
] = 8,
90 [snd_soc_dapm_pga
] = 9,
91 [snd_soc_dapm_adc
] = 10,
92 [snd_soc_dapm_out_drv
] = 11,
93 [snd_soc_dapm_hp
] = 11,
94 [snd_soc_dapm_spk
] = 11,
95 [snd_soc_dapm_line
] = 11,
96 [snd_soc_dapm_kcontrol
] = 12,
97 [snd_soc_dapm_post
] = 13,
100 static int dapm_down_seq
[] = {
101 [snd_soc_dapm_pre
] = 0,
102 [snd_soc_dapm_kcontrol
] = 1,
103 [snd_soc_dapm_adc
] = 2,
104 [snd_soc_dapm_hp
] = 3,
105 [snd_soc_dapm_spk
] = 3,
106 [snd_soc_dapm_line
] = 3,
107 [snd_soc_dapm_out_drv
] = 3,
108 [snd_soc_dapm_pga
] = 4,
109 [snd_soc_dapm_switch
] = 5,
110 [snd_soc_dapm_mixer_named_ctl
] = 5,
111 [snd_soc_dapm_mixer
] = 5,
112 [snd_soc_dapm_dac
] = 6,
113 [snd_soc_dapm_mic
] = 7,
114 [snd_soc_dapm_micbias
] = 8,
115 [snd_soc_dapm_mux
] = 9,
116 [snd_soc_dapm_demux
] = 9,
117 [snd_soc_dapm_aif_in
] = 10,
118 [snd_soc_dapm_aif_out
] = 10,
119 [snd_soc_dapm_dai_in
] = 10,
120 [snd_soc_dapm_dai_out
] = 10,
121 [snd_soc_dapm_dai_link
] = 11,
122 [snd_soc_dapm_supply
] = 12,
123 [snd_soc_dapm_clock_supply
] = 13,
124 [snd_soc_dapm_regulator_supply
] = 13,
125 [snd_soc_dapm_post
] = 14,
128 static void dapm_assert_locked(struct snd_soc_dapm_context
*dapm
)
130 if (dapm
->card
&& dapm
->card
->instantiated
)
131 lockdep_assert_held(&dapm
->card
->dapm_mutex
);
134 static void pop_wait(u32 pop_time
)
137 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time
));
140 static void pop_dbg(struct device
*dev
, u32 pop_time
, const char *fmt
, ...)
148 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
153 vsnprintf(buf
, PAGE_SIZE
, fmt
, args
);
154 dev_info(dev
, "%s", buf
);
160 static bool dapm_dirty_widget(struct snd_soc_dapm_widget
*w
)
162 return !list_empty(&w
->dirty
);
165 static void dapm_mark_dirty(struct snd_soc_dapm_widget
*w
, const char *reason
)
167 dapm_assert_locked(w
->dapm
);
169 if (!dapm_dirty_widget(w
)) {
170 dev_vdbg(w
->dapm
->dev
, "Marking %s dirty due to %s\n",
172 list_add_tail(&w
->dirty
, &w
->dapm
->card
->dapm_dirty
);
177 * Common implementation for dapm_widget_invalidate_input_paths() and
178 * dapm_widget_invalidate_output_paths(). The function is inlined since the
179 * combined size of the two specialized functions is only marginally larger then
180 * the size of the generic function and at the same time the fast path of the
181 * specialized functions is significantly smaller than the generic function.
183 static __always_inline
void dapm_widget_invalidate_paths(
184 struct snd_soc_dapm_widget
*w
, enum snd_soc_dapm_direction dir
)
186 enum snd_soc_dapm_direction rdir
= SND_SOC_DAPM_DIR_REVERSE(dir
);
187 struct snd_soc_dapm_widget
*node
;
188 struct snd_soc_dapm_path
*p
;
191 dapm_assert_locked(w
->dapm
);
193 if (w
->endpoints
[dir
] == -1)
196 list_add_tail(&w
->work_list
, &list
);
197 w
->endpoints
[dir
] = -1;
199 list_for_each_entry(w
, &list
, work_list
) {
200 snd_soc_dapm_widget_for_each_path(w
, dir
, p
) {
201 if (p
->is_supply
|| p
->weak
|| !p
->connect
)
203 node
= p
->node
[rdir
];
204 if (node
->endpoints
[dir
] != -1) {
205 node
->endpoints
[dir
] = -1;
206 list_add_tail(&node
->work_list
, &list
);
213 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
215 * @w: The widget for which to invalidate the cached number of input paths
217 * Resets the cached number of inputs for the specified widget and all widgets
218 * that can be reached via outcoming paths from the widget.
220 * This function must be called if the number of output paths for a widget might
221 * have changed. E.g. if the source state of a widget changes or a path is added
222 * or activated with the widget as the sink.
224 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget
*w
)
226 dapm_widget_invalidate_paths(w
, SND_SOC_DAPM_DIR_IN
);
230 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
232 * @w: The widget for which to invalidate the cached number of output paths
234 * Resets the cached number of outputs for the specified widget and all widgets
235 * that can be reached via incoming paths from the widget.
237 * This function must be called if the number of output paths for a widget might
238 * have changed. E.g. if the sink state of a widget changes or a path is added
239 * or activated with the widget as the source.
241 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget
*w
)
243 dapm_widget_invalidate_paths(w
, SND_SOC_DAPM_DIR_OUT
);
247 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
248 * for the widgets connected to a path
249 * @p: The path to invalidate
251 * Resets the cached number of inputs for the sink of the path and the cached
252 * number of outputs for the source of the path.
254 * This function must be called when a path is added, removed or the connected
257 static void dapm_path_invalidate(struct snd_soc_dapm_path
*p
)
260 * Weak paths or supply paths do not influence the number of input or
261 * output paths of their neighbors.
263 if (p
->weak
|| p
->is_supply
)
267 * The number of connected endpoints is the sum of the number of
268 * connected endpoints of all neighbors. If a node with 0 connected
269 * endpoints is either connected or disconnected that sum won't change,
270 * so there is no need to re-check the path.
272 if (p
->source
->endpoints
[SND_SOC_DAPM_DIR_IN
] != 0)
273 dapm_widget_invalidate_input_paths(p
->sink
);
274 if (p
->sink
->endpoints
[SND_SOC_DAPM_DIR_OUT
] != 0)
275 dapm_widget_invalidate_output_paths(p
->source
);
278 void dapm_mark_endpoints_dirty(struct snd_soc_card
*card
)
280 struct snd_soc_dapm_widget
*w
;
282 mutex_lock(&card
->dapm_mutex
);
284 list_for_each_entry(w
, &card
->widgets
, list
) {
286 dapm_mark_dirty(w
, "Rechecking endpoints");
287 if (w
->is_ep
& SND_SOC_DAPM_EP_SINK
)
288 dapm_widget_invalidate_output_paths(w
);
289 if (w
->is_ep
& SND_SOC_DAPM_EP_SOURCE
)
290 dapm_widget_invalidate_input_paths(w
);
294 mutex_unlock(&card
->dapm_mutex
);
296 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty
);
298 /* create a new dapm widget */
299 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
300 const struct snd_soc_dapm_widget
*_widget
)
302 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
305 struct dapm_kcontrol_data
{
307 struct snd_soc_dapm_widget
*widget
;
308 struct list_head paths
;
309 struct snd_soc_dapm_widget_list
*wlist
;
312 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget
*widget
,
313 struct snd_kcontrol
*kcontrol
, const char *ctrl_name
)
315 struct dapm_kcontrol_data
*data
;
316 struct soc_mixer_control
*mc
;
321 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
325 INIT_LIST_HEAD(&data
->paths
);
327 switch (widget
->id
) {
328 case snd_soc_dapm_switch
:
329 case snd_soc_dapm_mixer
:
330 case snd_soc_dapm_mixer_named_ctl
:
331 mc
= (struct soc_mixer_control
*)kcontrol
->private_value
;
333 if (mc
->autodisable
&& snd_soc_volsw_is_stereo(mc
))
334 dev_warn(widget
->dapm
->dev
,
335 "ASoC: Unsupported stereo autodisable control '%s'\n",
338 if (mc
->autodisable
) {
339 struct snd_soc_dapm_widget
template;
341 name
= kasprintf(GFP_KERNEL
, "%s %s", ctrl_name
,
348 memset(&template, 0, sizeof(template));
349 template.reg
= mc
->reg
;
350 template.mask
= (1 << fls(mc
->max
)) - 1;
351 template.shift
= mc
->shift
;
353 template.off_val
= mc
->max
;
355 template.off_val
= 0;
356 template.on_val
= template.off_val
;
357 template.id
= snd_soc_dapm_kcontrol
;
358 template.name
= name
;
360 data
->value
= template.on_val
;
363 snd_soc_dapm_new_control_unlocked(widget
->dapm
,
366 if (IS_ERR(data
->widget
)) {
367 ret
= PTR_ERR(data
->widget
);
376 case snd_soc_dapm_demux
:
377 case snd_soc_dapm_mux
:
378 e
= (struct soc_enum
*)kcontrol
->private_value
;
380 if (e
->autodisable
) {
381 struct snd_soc_dapm_widget
template;
383 name
= kasprintf(GFP_KERNEL
, "%s %s", ctrl_name
,
390 memset(&template, 0, sizeof(template));
391 template.reg
= e
->reg
;
392 template.mask
= e
->mask
<< e
->shift_l
;
393 template.shift
= e
->shift_l
;
394 template.off_val
= snd_soc_enum_item_to_val(e
, 0);
395 template.on_val
= template.off_val
;
396 template.id
= snd_soc_dapm_kcontrol
;
397 template.name
= name
;
399 data
->value
= template.on_val
;
401 data
->widget
= snd_soc_dapm_new_control_unlocked(
402 widget
->dapm
, &template);
404 if (IS_ERR(data
->widget
)) {
405 ret
= PTR_ERR(data
->widget
);
413 snd_soc_dapm_add_path(widget
->dapm
, data
->widget
,
421 kcontrol
->private_data
= data
;
430 static void dapm_kcontrol_free(struct snd_kcontrol
*kctl
)
432 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kctl
);
434 list_del(&data
->paths
);
439 static struct snd_soc_dapm_widget_list
*dapm_kcontrol_get_wlist(
440 const struct snd_kcontrol
*kcontrol
)
442 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
447 static int dapm_kcontrol_add_widget(struct snd_kcontrol
*kcontrol
,
448 struct snd_soc_dapm_widget
*widget
)
450 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
451 struct snd_soc_dapm_widget_list
*new_wlist
;
455 n
= data
->wlist
->num_widgets
+ 1;
459 new_wlist
= krealloc(data
->wlist
,
460 sizeof(*new_wlist
) + sizeof(widget
) * n
, GFP_KERNEL
);
464 new_wlist
->widgets
[n
- 1] = widget
;
465 new_wlist
->num_widgets
= n
;
467 data
->wlist
= new_wlist
;
472 static void dapm_kcontrol_add_path(const struct snd_kcontrol
*kcontrol
,
473 struct snd_soc_dapm_path
*path
)
475 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
477 list_add_tail(&path
->list_kcontrol
, &data
->paths
);
480 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol
*kcontrol
)
482 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
487 return data
->widget
->power
;
490 static struct list_head
*dapm_kcontrol_get_path_list(
491 const struct snd_kcontrol
*kcontrol
)
493 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
498 #define dapm_kcontrol_for_each_path(path, kcontrol) \
499 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
502 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol
*kcontrol
)
504 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
508 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value
);
510 static bool dapm_kcontrol_set_value(const struct snd_kcontrol
*kcontrol
,
513 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
515 if (data
->value
== value
)
519 data
->widget
->on_val
= value
;
527 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
529 * @kcontrol: The kcontrol
531 struct snd_soc_dapm_widget
*snd_soc_dapm_kcontrol_widget(
532 struct snd_kcontrol
*kcontrol
)
534 return dapm_kcontrol_get_wlist(kcontrol
)->widgets
[0];
536 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget
);
539 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
541 * @kcontrol: The kcontrol
543 * Note: This function must only be used on kcontrols that are known to have
544 * been registered for a CODEC. Otherwise the behaviour is undefined.
546 struct snd_soc_dapm_context
*snd_soc_dapm_kcontrol_dapm(
547 struct snd_kcontrol
*kcontrol
)
549 return dapm_kcontrol_get_wlist(kcontrol
)->widgets
[0]->dapm
;
551 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm
);
553 static void dapm_reset(struct snd_soc_card
*card
)
555 struct snd_soc_dapm_widget
*w
;
557 lockdep_assert_held(&card
->dapm_mutex
);
559 memset(&card
->dapm_stats
, 0, sizeof(card
->dapm_stats
));
561 list_for_each_entry(w
, &card
->widgets
, list
) {
562 w
->new_power
= w
->power
;
563 w
->power_checked
= false;
567 static const char *soc_dapm_prefix(struct snd_soc_dapm_context
*dapm
)
569 if (!dapm
->component
)
571 return dapm
->component
->name_prefix
;
574 static int soc_dapm_read(struct snd_soc_dapm_context
*dapm
, int reg
,
577 if (!dapm
->component
)
579 return snd_soc_component_read(dapm
->component
, reg
, value
);
582 static int soc_dapm_update_bits(struct snd_soc_dapm_context
*dapm
,
583 int reg
, unsigned int mask
, unsigned int value
)
585 if (!dapm
->component
)
587 return snd_soc_component_update_bits(dapm
->component
, reg
,
591 static int soc_dapm_test_bits(struct snd_soc_dapm_context
*dapm
,
592 int reg
, unsigned int mask
, unsigned int value
)
594 if (!dapm
->component
)
596 return snd_soc_component_test_bits(dapm
->component
, reg
, mask
, value
);
599 static void soc_dapm_async_complete(struct snd_soc_dapm_context
*dapm
)
602 snd_soc_component_async_complete(dapm
->component
);
605 static struct snd_soc_dapm_widget
*
606 dapm_wcache_lookup(struct snd_soc_dapm_wcache
*wcache
, const char *name
)
608 struct snd_soc_dapm_widget
*w
= wcache
->widget
;
609 struct list_head
*wlist
;
614 wlist
= &w
->dapm
->card
->widgets
;
616 list_for_each_entry_from(w
, wlist
, list
) {
617 if (!strcmp(name
, w
->name
))
628 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache
*wcache
,
629 struct snd_soc_dapm_widget
*w
)
635 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
636 * @dapm: The DAPM context for which to set the level
637 * @level: The level to set
639 * Forces the DAPM bias level to a specific state. It will call the bias level
640 * callback of DAPM context with the specified level. This will even happen if
641 * the context is already at the same level. Furthermore it will not go through
642 * the normal bias level sequencing, meaning any intermediate states between the
643 * current and the target state will not be entered.
645 * Note that the change in bias level is only temporary and the next time
646 * snd_soc_dapm_sync() is called the state will be set to the level as
647 * determined by the DAPM core. The function is mainly intended to be used to
648 * used during probe or resume from suspend to power up the device so
649 * initialization can be done, before the DAPM core takes over.
651 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context
*dapm
,
652 enum snd_soc_bias_level level
)
656 if (dapm
->set_bias_level
)
657 ret
= dapm
->set_bias_level(dapm
, level
);
660 dapm
->bias_level
= level
;
664 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level
);
667 * snd_soc_dapm_set_bias_level - set the bias level for the system
668 * @dapm: DAPM context
669 * @level: level to configure
671 * Configure the bias (power) levels for the SoC audio device.
673 * Returns 0 for success else error.
675 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context
*dapm
,
676 enum snd_soc_bias_level level
)
678 struct snd_soc_card
*card
= dapm
->card
;
681 trace_snd_soc_bias_level_start(card
, level
);
683 if (card
&& card
->set_bias_level
)
684 ret
= card
->set_bias_level(card
, dapm
, level
);
688 if (!card
|| dapm
!= &card
->dapm
)
689 ret
= snd_soc_dapm_force_bias_level(dapm
, level
);
694 if (card
&& card
->set_bias_level_post
)
695 ret
= card
->set_bias_level_post(card
, dapm
, level
);
697 trace_snd_soc_bias_level_done(card
, level
);
702 /* connect mux widget to its interconnecting audio paths */
703 static int dapm_connect_mux(struct snd_soc_dapm_context
*dapm
,
704 struct snd_soc_dapm_path
*path
, const char *control_name
,
705 struct snd_soc_dapm_widget
*w
)
707 const struct snd_kcontrol_new
*kcontrol
= &w
->kcontrol_news
[0];
708 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
709 unsigned int val
, item
;
712 if (e
->reg
!= SND_SOC_NOPM
) {
713 soc_dapm_read(dapm
, e
->reg
, &val
);
714 val
= (val
>> e
->shift_l
) & e
->mask
;
715 item
= snd_soc_enum_val_to_item(e
, val
);
717 /* since a virtual mux has no backing registers to
718 * decide which path to connect, it will try to match
719 * with the first enumeration. This is to ensure
720 * that the default mux choice (the first) will be
721 * correctly powered up during initialization.
726 for (i
= 0; i
< e
->items
; i
++) {
727 if (!(strcmp(control_name
, e
->texts
[i
]))) {
728 path
->name
= e
->texts
[i
];
740 /* set up initial codec paths */
741 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path
*p
, int i
,
744 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
745 p
->sink
->kcontrol_news
[i
].private_value
;
746 unsigned int reg
= mc
->reg
;
747 unsigned int shift
= mc
->shift
;
748 unsigned int max
= mc
->max
;
749 unsigned int mask
= (1 << fls(max
)) - 1;
750 unsigned int invert
= mc
->invert
;
753 if (reg
!= SND_SOC_NOPM
) {
754 soc_dapm_read(p
->sink
->dapm
, reg
, &val
);
756 * The nth_path argument allows this function to know
757 * which path of a kcontrol it is setting the initial
758 * status for. Ideally this would support any number
759 * of paths and channels. But since kcontrols only come
760 * in mono and stereo variants, we are limited to 2
763 * The following code assumes for stereo controls the
764 * first path is the left channel, and all remaining
765 * paths are the right channel.
767 if (snd_soc_volsw_is_stereo(mc
) && nth_path
> 0) {
769 soc_dapm_read(p
->sink
->dapm
, mc
->rreg
, &val
);
770 val
= (val
>> mc
->rshift
) & mask
;
772 val
= (val
>> shift
) & mask
;
782 /* connect mixer widget to its interconnecting audio paths */
783 static int dapm_connect_mixer(struct snd_soc_dapm_context
*dapm
,
784 struct snd_soc_dapm_path
*path
, const char *control_name
)
788 /* search for mixer kcontrol */
789 for (i
= 0; i
< path
->sink
->num_kcontrols
; i
++) {
790 if (!strcmp(control_name
, path
->sink
->kcontrol_news
[i
].name
)) {
791 path
->name
= path
->sink
->kcontrol_news
[i
].name
;
792 dapm_set_mixer_path_status(path
, i
, nth_path
++);
799 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context
*dapm
,
800 struct snd_soc_dapm_widget
*kcontrolw
,
801 const struct snd_kcontrol_new
*kcontrol_new
,
802 struct snd_kcontrol
**kcontrol
)
804 struct snd_soc_dapm_widget
*w
;
809 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
810 if (w
== kcontrolw
|| w
->dapm
!= kcontrolw
->dapm
)
812 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
813 if (&w
->kcontrol_news
[i
] == kcontrol_new
) {
815 *kcontrol
= w
->kcontrols
[i
];
825 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
826 * create it. Either way, add the widget into the control's widget list
828 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget
*w
,
831 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
832 struct snd_card
*card
= dapm
->card
->snd_card
;
836 struct snd_kcontrol
*kcontrol
;
837 bool wname_in_long_name
, kcname_in_long_name
;
838 char *long_name
= NULL
;
842 prefix
= soc_dapm_prefix(dapm
);
844 prefix_len
= strlen(prefix
) + 1;
848 shared
= dapm_is_shared_kcontrol(dapm
, w
, &w
->kcontrol_news
[kci
],
853 wname_in_long_name
= false;
854 kcname_in_long_name
= true;
857 case snd_soc_dapm_switch
:
858 case snd_soc_dapm_mixer
:
859 case snd_soc_dapm_pga
:
860 case snd_soc_dapm_out_drv
:
861 wname_in_long_name
= true;
862 kcname_in_long_name
= true;
864 case snd_soc_dapm_mixer_named_ctl
:
865 wname_in_long_name
= false;
866 kcname_in_long_name
= true;
868 case snd_soc_dapm_demux
:
869 case snd_soc_dapm_mux
:
870 wname_in_long_name
= true;
871 kcname_in_long_name
= false;
878 if (wname_in_long_name
&& kcname_in_long_name
) {
880 * The control will get a prefix from the control
881 * creation process but we're also using the same
882 * prefix for widgets so cut the prefix off the
883 * front of the widget name.
885 long_name
= kasprintf(GFP_KERNEL
, "%s %s",
886 w
->name
+ prefix_len
,
887 w
->kcontrol_news
[kci
].name
);
888 if (long_name
== NULL
)
892 } else if (wname_in_long_name
) {
894 name
= w
->name
+ prefix_len
;
897 name
= w
->kcontrol_news
[kci
].name
;
900 kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[kci
], NULL
, name
,
907 kcontrol
->private_free
= dapm_kcontrol_free
;
909 ret
= dapm_kcontrol_data_alloc(w
, kcontrol
, name
);
911 snd_ctl_free_one(kcontrol
);
915 ret
= snd_ctl_add(card
, kcontrol
);
918 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
924 ret
= dapm_kcontrol_add_widget(kcontrol
, w
);
926 w
->kcontrols
[kci
] = kcontrol
;
934 /* create new dapm mixer control */
935 static int dapm_new_mixer(struct snd_soc_dapm_widget
*w
)
938 struct snd_soc_dapm_path
*path
;
939 struct dapm_kcontrol_data
*data
;
942 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
944 snd_soc_dapm_widget_for_each_source_path(w
, path
) {
945 /* mixer/mux paths name must match control name */
946 if (path
->name
!= (char *)w
->kcontrol_news
[i
].name
)
949 if (!w
->kcontrols
[i
]) {
950 ret
= dapm_create_or_share_kcontrol(w
, i
);
955 dapm_kcontrol_add_path(w
->kcontrols
[i
], path
);
957 data
= snd_kcontrol_chip(w
->kcontrols
[i
]);
959 snd_soc_dapm_add_path(data
->widget
->dapm
,
969 /* create new dapm mux control */
970 static int dapm_new_mux(struct snd_soc_dapm_widget
*w
)
972 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
973 enum snd_soc_dapm_direction dir
;
974 struct snd_soc_dapm_path
*path
;
979 case snd_soc_dapm_mux
:
980 dir
= SND_SOC_DAPM_DIR_OUT
;
983 case snd_soc_dapm_demux
:
984 dir
= SND_SOC_DAPM_DIR_IN
;
991 if (w
->num_kcontrols
!= 1) {
993 "ASoC: %s %s has incorrect number of controls\n", type
,
998 if (list_empty(&w
->edges
[dir
])) {
999 dev_err(dapm
->dev
, "ASoC: %s %s has no paths\n", type
, w
->name
);
1003 ret
= dapm_create_or_share_kcontrol(w
, 0);
1007 snd_soc_dapm_widget_for_each_path(w
, dir
, path
) {
1009 dapm_kcontrol_add_path(w
->kcontrols
[0], path
);
1015 /* create new dapm volume control */
1016 static int dapm_new_pga(struct snd_soc_dapm_widget
*w
)
1020 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
1021 ret
= dapm_create_or_share_kcontrol(w
, i
);
1029 /* create new dapm dai link control */
1030 static int dapm_new_dai_link(struct snd_soc_dapm_widget
*w
)
1033 struct snd_kcontrol
*kcontrol
;
1034 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
1035 struct snd_card
*card
= dapm
->card
->snd_card
;
1037 /* create control for links with > 1 config */
1038 if (w
->num_params
<= 1)
1042 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
1043 kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[i
], w
,
1045 ret
= snd_ctl_add(card
, kcontrol
);
1048 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1049 w
->name
, w
->kcontrol_news
[i
].name
, ret
);
1052 kcontrol
->private_data
= w
;
1053 w
->kcontrols
[i
] = kcontrol
;
1059 /* We implement power down on suspend by checking the power state of
1060 * the ALSA card - when we are suspending the ALSA state for the card
1063 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
1065 int level
= snd_power_get_state(widget
->dapm
->card
->snd_card
);
1068 case SNDRV_CTL_POWER_D3hot
:
1069 case SNDRV_CTL_POWER_D3cold
:
1070 if (widget
->ignore_suspend
)
1071 dev_dbg(widget
->dapm
->dev
, "ASoC: %s ignoring suspend\n",
1073 return widget
->ignore_suspend
;
1079 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list
**list
,
1080 struct list_head
*widgets
)
1082 struct snd_soc_dapm_widget
*w
;
1083 struct list_head
*it
;
1084 unsigned int size
= 0;
1087 list_for_each(it
, widgets
)
1090 *list
= kzalloc(sizeof(**list
) + size
* sizeof(*w
), GFP_KERNEL
);
1094 list_for_each_entry(w
, widgets
, work_list
)
1095 (*list
)->widgets
[i
++] = w
;
1097 (*list
)->num_widgets
= i
;
1103 * Common implementation for is_connected_output_ep() and
1104 * is_connected_input_ep(). The function is inlined since the combined size of
1105 * the two specialized functions is only marginally larger then the size of the
1106 * generic function and at the same time the fast path of the specialized
1107 * functions is significantly smaller than the generic function.
1109 static __always_inline
int is_connected_ep(struct snd_soc_dapm_widget
*widget
,
1110 struct list_head
*list
, enum snd_soc_dapm_direction dir
,
1111 int (*fn
)(struct snd_soc_dapm_widget
*, struct list_head
*,
1112 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*,
1113 enum snd_soc_dapm_direction
)),
1114 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*,
1115 enum snd_soc_dapm_direction
))
1117 enum snd_soc_dapm_direction rdir
= SND_SOC_DAPM_DIR_REVERSE(dir
);
1118 struct snd_soc_dapm_path
*path
;
1121 if (widget
->endpoints
[dir
] >= 0)
1122 return widget
->endpoints
[dir
];
1124 DAPM_UPDATE_STAT(widget
, path_checks
);
1126 /* do we need to add this widget to the list ? */
1128 list_add_tail(&widget
->work_list
, list
);
1130 if (custom_stop_condition
&& custom_stop_condition(widget
, dir
)) {
1131 widget
->endpoints
[dir
] = 1;
1132 return widget
->endpoints
[dir
];
1135 if ((widget
->is_ep
& SND_SOC_DAPM_DIR_TO_EP(dir
)) && widget
->connected
) {
1136 widget
->endpoints
[dir
] = snd_soc_dapm_suspend_check(widget
);
1137 return widget
->endpoints
[dir
];
1140 snd_soc_dapm_widget_for_each_path(widget
, rdir
, path
) {
1141 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
1143 if (path
->weak
|| path
->is_supply
)
1149 trace_snd_soc_dapm_path(widget
, dir
, path
);
1151 if (path
->connect
) {
1153 con
+= fn(path
->node
[dir
], list
, custom_stop_condition
);
1158 widget
->endpoints
[dir
] = con
;
1164 * Recursively check for a completed path to an active or physically connected
1165 * output widget. Returns number of complete paths.
1167 * Optionally, can be supplied with a function acting as a stopping condition.
1168 * This function takes the dapm widget currently being examined and the walk
1169 * direction as an arguments, it should return true if the walk should be
1170 * stopped and false otherwise.
1172 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
,
1173 struct list_head
*list
,
1174 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*i
,
1175 enum snd_soc_dapm_direction
))
1177 return is_connected_ep(widget
, list
, SND_SOC_DAPM_DIR_OUT
,
1178 is_connected_output_ep
, custom_stop_condition
);
1182 * Recursively check for a completed path to an active or physically connected
1183 * input widget. Returns number of complete paths.
1185 * Optionally, can be supplied with a function acting as a stopping condition.
1186 * This function takes the dapm widget currently being examined and the walk
1187 * direction as an arguments, it should return true if the walk should be
1188 * stopped and false otherwise.
1190 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
,
1191 struct list_head
*list
,
1192 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*i
,
1193 enum snd_soc_dapm_direction
))
1195 return is_connected_ep(widget
, list
, SND_SOC_DAPM_DIR_IN
,
1196 is_connected_input_ep
, custom_stop_condition
);
1199 int snd_soc_dapm_connected_output_ep(struct snd_soc_dapm_widget
*widget
,
1200 struct list_head
*list
)
1202 return is_connected_output_ep(widget
, list
, NULL
);
1205 int snd_soc_dapm_connected_input_ep(struct snd_soc_dapm_widget
*widget
,
1206 struct list_head
*list
)
1208 return is_connected_input_ep(widget
, list
, NULL
);
1212 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1213 * @dai: the soc DAI.
1214 * @stream: stream direction.
1215 * @list: list of active widgets for this stream.
1216 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1217 * walk based on custom logic.
1219 * Queries DAPM graph as to whether a valid audio stream path exists for
1220 * the initial stream specified by name. This takes into account
1221 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1223 * Optionally, can be supplied with a function acting as a stopping condition.
1224 * This function takes the dapm widget currently being examined and the walk
1225 * direction as an arguments, it should return true if the walk should be
1226 * stopped and false otherwise.
1228 * Returns the number of valid paths or negative error.
1230 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai
*dai
, int stream
,
1231 struct snd_soc_dapm_widget_list
**list
,
1232 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*,
1233 enum snd_soc_dapm_direction
))
1235 struct snd_soc_card
*card
= dai
->component
->card
;
1236 struct snd_soc_dapm_widget
*w
;
1241 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
1244 * For is_connected_{output,input}_ep fully discover the graph we need
1245 * to reset the cached number of inputs and outputs.
1247 list_for_each_entry(w
, &card
->widgets
, list
) {
1248 w
->endpoints
[SND_SOC_DAPM_DIR_IN
] = -1;
1249 w
->endpoints
[SND_SOC_DAPM_DIR_OUT
] = -1;
1252 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1253 paths
= is_connected_output_ep(dai
->playback_widget
, &widgets
,
1254 custom_stop_condition
);
1256 paths
= is_connected_input_ep(dai
->capture_widget
, &widgets
,
1257 custom_stop_condition
);
1259 /* Drop starting point */
1260 list_del(widgets
.next
);
1262 ret
= dapm_widget_list_create(list
, &widgets
);
1266 trace_snd_soc_dapm_connected(paths
, stream
);
1267 mutex_unlock(&card
->dapm_mutex
);
1273 * Handler for regulator supply widget.
1275 int dapm_regulator_event(struct snd_soc_dapm_widget
*w
,
1276 struct snd_kcontrol
*kcontrol
, int event
)
1280 soc_dapm_async_complete(w
->dapm
);
1282 if (SND_SOC_DAPM_EVENT_ON(event
)) {
1283 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
1284 ret
= regulator_allow_bypass(w
->regulator
, false);
1286 dev_warn(w
->dapm
->dev
,
1287 "ASoC: Failed to unbypass %s: %d\n",
1291 return regulator_enable(w
->regulator
);
1293 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
1294 ret
= regulator_allow_bypass(w
->regulator
, true);
1296 dev_warn(w
->dapm
->dev
,
1297 "ASoC: Failed to bypass %s: %d\n",
1301 return regulator_disable_deferred(w
->regulator
, w
->shift
);
1304 EXPORT_SYMBOL_GPL(dapm_regulator_event
);
1307 * Handler for clock supply widget.
1309 int dapm_clock_event(struct snd_soc_dapm_widget
*w
,
1310 struct snd_kcontrol
*kcontrol
, int event
)
1315 soc_dapm_async_complete(w
->dapm
);
1317 #ifdef CONFIG_HAVE_CLK
1318 if (SND_SOC_DAPM_EVENT_ON(event
)) {
1319 return clk_prepare_enable(w
->clk
);
1321 clk_disable_unprepare(w
->clk
);
1327 EXPORT_SYMBOL_GPL(dapm_clock_event
);
1329 static int dapm_widget_power_check(struct snd_soc_dapm_widget
*w
)
1331 if (w
->power_checked
)
1332 return w
->new_power
;
1337 w
->new_power
= w
->power_check(w
);
1339 w
->power_checked
= true;
1341 return w
->new_power
;
1344 /* Generic check to see if a widget should be powered. */
1345 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
1349 DAPM_UPDATE_STAT(w
, power_checks
);
1351 in
= is_connected_input_ep(w
, NULL
, NULL
);
1352 out
= is_connected_output_ep(w
, NULL
, NULL
);
1353 return out
!= 0 && in
!= 0;
1356 /* Check to see if a power supply is needed */
1357 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
1359 struct snd_soc_dapm_path
*path
;
1361 DAPM_UPDATE_STAT(w
, power_checks
);
1363 /* Check if one of our outputs is connected */
1364 snd_soc_dapm_widget_for_each_sink_path(w
, path
) {
1365 DAPM_UPDATE_STAT(w
, neighbour_checks
);
1370 if (path
->connected
&&
1371 !path
->connected(path
->source
, path
->sink
))
1374 if (dapm_widget_power_check(path
->sink
))
1381 static int dapm_always_on_check_power(struct snd_soc_dapm_widget
*w
)
1383 return w
->connected
;
1386 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
1387 struct snd_soc_dapm_widget
*b
,
1395 sort
= dapm_down_seq
;
1397 if (sort
[a
->id
] != sort
[b
->id
])
1398 return sort
[a
->id
] - sort
[b
->id
];
1399 if (a
->subseq
!= b
->subseq
) {
1401 return a
->subseq
- b
->subseq
;
1403 return b
->subseq
- a
->subseq
;
1405 if (a
->reg
!= b
->reg
)
1406 return a
->reg
- b
->reg
;
1407 if (a
->dapm
!= b
->dapm
)
1408 return (unsigned long)a
->dapm
- (unsigned long)b
->dapm
;
1413 /* Insert a widget in order into a DAPM power sequence. */
1414 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
1415 struct list_head
*list
,
1418 struct snd_soc_dapm_widget
*w
;
1420 list_for_each_entry(w
, list
, power_list
)
1421 if (dapm_seq_compare(new_widget
, w
, power_up
) < 0) {
1422 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
1426 list_add_tail(&new_widget
->power_list
, list
);
1429 static void dapm_seq_check_event(struct snd_soc_card
*card
,
1430 struct snd_soc_dapm_widget
*w
, int event
)
1432 const char *ev_name
;
1436 case SND_SOC_DAPM_PRE_PMU
:
1437 ev_name
= "PRE_PMU";
1440 case SND_SOC_DAPM_POST_PMU
:
1441 ev_name
= "POST_PMU";
1444 case SND_SOC_DAPM_PRE_PMD
:
1445 ev_name
= "PRE_PMD";
1448 case SND_SOC_DAPM_POST_PMD
:
1449 ev_name
= "POST_PMD";
1452 case SND_SOC_DAPM_WILL_PMU
:
1453 ev_name
= "WILL_PMU";
1456 case SND_SOC_DAPM_WILL_PMD
:
1457 ev_name
= "WILL_PMD";
1461 WARN(1, "Unknown event %d\n", event
);
1465 if (w
->new_power
!= power
)
1468 if (w
->event
&& (w
->event_flags
& event
)) {
1469 pop_dbg(w
->dapm
->dev
, card
->pop_time
, "pop test : %s %s\n",
1471 soc_dapm_async_complete(w
->dapm
);
1472 trace_snd_soc_dapm_widget_event_start(w
, event
);
1473 ret
= w
->event(w
, NULL
, event
);
1474 trace_snd_soc_dapm_widget_event_done(w
, event
);
1476 dev_err(w
->dapm
->dev
, "ASoC: %s: %s event failed: %d\n",
1477 ev_name
, w
->name
, ret
);
1481 /* Apply the coalesced changes from a DAPM sequence */
1482 static void dapm_seq_run_coalesced(struct snd_soc_card
*card
,
1483 struct list_head
*pending
)
1485 struct snd_soc_dapm_context
*dapm
;
1486 struct snd_soc_dapm_widget
*w
;
1488 unsigned int value
= 0;
1489 unsigned int mask
= 0;
1491 w
= list_first_entry(pending
, struct snd_soc_dapm_widget
, power_list
);
1495 list_for_each_entry(w
, pending
, power_list
) {
1496 WARN_ON(reg
!= w
->reg
|| dapm
!= w
->dapm
);
1497 w
->power
= w
->new_power
;
1499 mask
|= w
->mask
<< w
->shift
;
1501 value
|= w
->on_val
<< w
->shift
;
1503 value
|= w
->off_val
<< w
->shift
;
1505 pop_dbg(dapm
->dev
, card
->pop_time
,
1506 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1507 w
->name
, reg
, value
, mask
);
1509 /* Check for events */
1510 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_PRE_PMU
);
1511 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_PRE_PMD
);
1515 /* Any widget will do, they should all be updating the
1519 pop_dbg(dapm
->dev
, card
->pop_time
,
1520 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1521 value
, mask
, reg
, card
->pop_time
);
1522 pop_wait(card
->pop_time
);
1523 soc_dapm_update_bits(dapm
, reg
, mask
, value
);
1526 list_for_each_entry(w
, pending
, power_list
) {
1527 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_POST_PMU
);
1528 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_POST_PMD
);
1532 /* Apply a DAPM power sequence.
1534 * We walk over a pre-sorted list of widgets to apply power to. In
1535 * order to minimise the number of writes to the device required
1536 * multiple widgets will be updated in a single write where possible.
1537 * Currently anything that requires more than a single write is not
1540 static void dapm_seq_run(struct snd_soc_card
*card
,
1541 struct list_head
*list
, int event
, bool power_up
)
1543 struct snd_soc_dapm_widget
*w
, *n
;
1544 struct snd_soc_dapm_context
*d
;
1547 int cur_subseq
= -1;
1548 int cur_reg
= SND_SOC_NOPM
;
1549 struct snd_soc_dapm_context
*cur_dapm
= NULL
;
1556 sort
= dapm_down_seq
;
1558 list_for_each_entry_safe(w
, n
, list
, power_list
) {
1561 /* Do we need to apply any queued changes? */
1562 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
||
1563 w
->dapm
!= cur_dapm
|| w
->subseq
!= cur_subseq
) {
1564 if (!list_empty(&pending
))
1565 dapm_seq_run_coalesced(card
, &pending
);
1567 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1568 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1569 if (sort
[i
] == cur_sort
)
1570 cur_dapm
->seq_notifier(cur_dapm
,
1575 if (cur_dapm
&& w
->dapm
!= cur_dapm
)
1576 soc_dapm_async_complete(cur_dapm
);
1578 INIT_LIST_HEAD(&pending
);
1580 cur_subseq
= INT_MIN
;
1581 cur_reg
= SND_SOC_NOPM
;
1586 case snd_soc_dapm_pre
:
1588 list_for_each_entry_safe_continue(w
, n
, list
,
1591 if (event
== SND_SOC_DAPM_STREAM_START
)
1593 NULL
, SND_SOC_DAPM_PRE_PMU
);
1594 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1596 NULL
, SND_SOC_DAPM_PRE_PMD
);
1599 case snd_soc_dapm_post
:
1601 list_for_each_entry_safe_continue(w
, n
, list
,
1604 if (event
== SND_SOC_DAPM_STREAM_START
)
1606 NULL
, SND_SOC_DAPM_POST_PMU
);
1607 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1609 NULL
, SND_SOC_DAPM_POST_PMD
);
1613 /* Queue it up for application */
1614 cur_sort
= sort
[w
->id
];
1615 cur_subseq
= w
->subseq
;
1618 list_move(&w
->power_list
, &pending
);
1623 dev_err(w
->dapm
->dev
,
1624 "ASoC: Failed to apply widget power: %d\n", ret
);
1627 if (!list_empty(&pending
))
1628 dapm_seq_run_coalesced(card
, &pending
);
1630 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1631 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1632 if (sort
[i
] == cur_sort
)
1633 cur_dapm
->seq_notifier(cur_dapm
,
1637 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1638 soc_dapm_async_complete(d
);
1642 static void dapm_widget_update(struct snd_soc_card
*card
)
1644 struct snd_soc_dapm_update
*update
= card
->update
;
1645 struct snd_soc_dapm_widget_list
*wlist
;
1646 struct snd_soc_dapm_widget
*w
= NULL
;
1650 if (!update
|| !dapm_kcontrol_is_powered(update
->kcontrol
))
1653 wlist
= dapm_kcontrol_get_wlist(update
->kcontrol
);
1655 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
1656 w
= wlist
->widgets
[wi
];
1658 if (w
->event
&& (w
->event_flags
& SND_SOC_DAPM_PRE_REG
)) {
1659 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_PRE_REG
);
1661 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM pre-event failed: %d\n",
1669 ret
= soc_dapm_update_bits(w
->dapm
, update
->reg
, update
->mask
,
1672 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM update failed: %d\n",
1675 if (update
->has_second_set
) {
1676 ret
= soc_dapm_update_bits(w
->dapm
, update
->reg2
,
1677 update
->mask2
, update
->val2
);
1679 dev_err(w
->dapm
->dev
,
1680 "ASoC: %s DAPM update failed: %d\n",
1684 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
1685 w
= wlist
->widgets
[wi
];
1687 if (w
->event
&& (w
->event_flags
& SND_SOC_DAPM_POST_REG
)) {
1688 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_POST_REG
);
1690 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM post-event failed: %d\n",
1696 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1697 * they're changing state.
1699 static void dapm_pre_sequence_async(void *data
, async_cookie_t cookie
)
1701 struct snd_soc_dapm_context
*d
= data
;
1704 /* If we're off and we're not supposed to go into STANDBY */
1705 if (d
->bias_level
== SND_SOC_BIAS_OFF
&&
1706 d
->target_bias_level
!= SND_SOC_BIAS_OFF
) {
1708 pm_runtime_get_sync(d
->dev
);
1710 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1713 "ASoC: Failed to turn on bias: %d\n", ret
);
1716 /* Prepare for a transition to ON or away from ON */
1717 if ((d
->target_bias_level
== SND_SOC_BIAS_ON
&&
1718 d
->bias_level
!= SND_SOC_BIAS_ON
) ||
1719 (d
->target_bias_level
!= SND_SOC_BIAS_ON
&&
1720 d
->bias_level
== SND_SOC_BIAS_ON
)) {
1721 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_PREPARE
);
1724 "ASoC: Failed to prepare bias: %d\n", ret
);
1728 /* Async callback run prior to DAPM sequences - brings to their final
1731 static void dapm_post_sequence_async(void *data
, async_cookie_t cookie
)
1733 struct snd_soc_dapm_context
*d
= data
;
1736 /* If we just powered the last thing off drop to standby bias */
1737 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1738 (d
->target_bias_level
== SND_SOC_BIAS_STANDBY
||
1739 d
->target_bias_level
== SND_SOC_BIAS_OFF
)) {
1740 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1742 dev_err(d
->dev
, "ASoC: Failed to apply standby bias: %d\n",
1746 /* If we're in standby and can support bias off then do that */
1747 if (d
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1748 d
->target_bias_level
== SND_SOC_BIAS_OFF
) {
1749 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_OFF
);
1751 dev_err(d
->dev
, "ASoC: Failed to turn off bias: %d\n",
1755 pm_runtime_put(d
->dev
);
1758 /* If we just powered up then move to active bias */
1759 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1760 d
->target_bias_level
== SND_SOC_BIAS_ON
) {
1761 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_ON
);
1763 dev_err(d
->dev
, "ASoC: Failed to apply active bias: %d\n",
1768 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget
*peer
,
1769 bool power
, bool connect
)
1771 /* If a connection is being made or broken then that update
1772 * will have marked the peer dirty, otherwise the widgets are
1773 * not connected and this update has no impact. */
1777 /* If the peer is already in the state we're moving to then we
1778 * won't have an impact on it. */
1779 if (power
!= peer
->power
)
1780 dapm_mark_dirty(peer
, "peer state change");
1783 static void dapm_widget_set_power(struct snd_soc_dapm_widget
*w
, bool power
,
1784 struct list_head
*up_list
,
1785 struct list_head
*down_list
)
1787 struct snd_soc_dapm_path
*path
;
1789 if (w
->power
== power
)
1792 trace_snd_soc_dapm_widget_power(w
, power
);
1794 /* If we changed our power state perhaps our neigbours changed
1797 snd_soc_dapm_widget_for_each_source_path(w
, path
)
1798 dapm_widget_set_peer_power(path
->source
, power
, path
->connect
);
1800 /* Supplies can't affect their outputs, only their inputs */
1801 if (!w
->is_supply
) {
1802 snd_soc_dapm_widget_for_each_sink_path(w
, path
)
1803 dapm_widget_set_peer_power(path
->sink
, power
,
1808 dapm_seq_insert(w
, up_list
, true);
1810 dapm_seq_insert(w
, down_list
, false);
1813 static void dapm_power_one_widget(struct snd_soc_dapm_widget
*w
,
1814 struct list_head
*up_list
,
1815 struct list_head
*down_list
)
1820 case snd_soc_dapm_pre
:
1821 dapm_seq_insert(w
, down_list
, false);
1823 case snd_soc_dapm_post
:
1824 dapm_seq_insert(w
, up_list
, true);
1828 power
= dapm_widget_power_check(w
);
1830 dapm_widget_set_power(w
, power
, up_list
, down_list
);
1835 static bool dapm_idle_bias_off(struct snd_soc_dapm_context
*dapm
)
1837 if (dapm
->idle_bias_off
)
1840 switch (snd_power_get_state(dapm
->card
->snd_card
)) {
1841 case SNDRV_CTL_POWER_D3hot
:
1842 case SNDRV_CTL_POWER_D3cold
:
1843 return dapm
->suspend_bias_off
;
1852 * Scan each dapm widget for complete audio path.
1853 * A complete path is a route that has valid endpoints i.e.:-
1855 * o DAC to output pin.
1856 * o Input pin to ADC.
1857 * o Input pin to Output pin (bypass, sidetone)
1858 * o DAC to ADC (loopback).
1860 static int dapm_power_widgets(struct snd_soc_card
*card
, int event
)
1862 struct snd_soc_dapm_widget
*w
;
1863 struct snd_soc_dapm_context
*d
;
1865 LIST_HEAD(down_list
);
1866 ASYNC_DOMAIN_EXCLUSIVE(async_domain
);
1867 enum snd_soc_bias_level bias
;
1869 lockdep_assert_held(&card
->dapm_mutex
);
1871 trace_snd_soc_dapm_start(card
);
1873 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1874 if (dapm_idle_bias_off(d
))
1875 d
->target_bias_level
= SND_SOC_BIAS_OFF
;
1877 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1882 /* Check which widgets we need to power and store them in
1883 * lists indicating if they should be powered up or down. We
1884 * only check widgets that have been flagged as dirty but note
1885 * that new widgets may be added to the dirty list while we
1888 list_for_each_entry(w
, &card
->dapm_dirty
, dirty
) {
1889 dapm_power_one_widget(w
, &up_list
, &down_list
);
1892 list_for_each_entry(w
, &card
->widgets
, list
) {
1894 case snd_soc_dapm_pre
:
1895 case snd_soc_dapm_post
:
1896 /* These widgets always need to be powered */
1899 list_del_init(&w
->dirty
);
1906 /* Supplies and micbiases only bring the
1907 * context up to STANDBY as unless something
1908 * else is active and passing audio they
1909 * generally don't require full power. Signal
1910 * generators are virtual pins and have no
1911 * power impact themselves.
1914 case snd_soc_dapm_siggen
:
1915 case snd_soc_dapm_vmid
:
1917 case snd_soc_dapm_supply
:
1918 case snd_soc_dapm_regulator_supply
:
1919 case snd_soc_dapm_clock_supply
:
1920 case snd_soc_dapm_micbias
:
1921 if (d
->target_bias_level
< SND_SOC_BIAS_STANDBY
)
1922 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1925 d
->target_bias_level
= SND_SOC_BIAS_ON
;
1932 /* Force all contexts in the card to the same bias state if
1933 * they're not ground referenced.
1935 bias
= SND_SOC_BIAS_OFF
;
1936 list_for_each_entry(d
, &card
->dapm_list
, list
)
1937 if (d
->target_bias_level
> bias
)
1938 bias
= d
->target_bias_level
;
1939 list_for_each_entry(d
, &card
->dapm_list
, list
)
1940 if (!dapm_idle_bias_off(d
))
1941 d
->target_bias_level
= bias
;
1943 trace_snd_soc_dapm_walk_done(card
);
1945 /* Run card bias changes at first */
1946 dapm_pre_sequence_async(&card
->dapm
, 0);
1947 /* Run other bias changes in parallel */
1948 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1949 if (d
!= &card
->dapm
)
1950 async_schedule_domain(dapm_pre_sequence_async
, d
,
1953 async_synchronize_full_domain(&async_domain
);
1955 list_for_each_entry(w
, &down_list
, power_list
) {
1956 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_WILL_PMD
);
1959 list_for_each_entry(w
, &up_list
, power_list
) {
1960 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_WILL_PMU
);
1963 /* Power down widgets first; try to avoid amplifying pops. */
1964 dapm_seq_run(card
, &down_list
, event
, false);
1966 dapm_widget_update(card
);
1969 dapm_seq_run(card
, &up_list
, event
, true);
1971 /* Run all the bias changes in parallel */
1972 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1973 if (d
!= &card
->dapm
)
1974 async_schedule_domain(dapm_post_sequence_async
, d
,
1977 async_synchronize_full_domain(&async_domain
);
1978 /* Run card bias changes at last */
1979 dapm_post_sequence_async(&card
->dapm
, 0);
1981 /* do we need to notify any clients that DAPM event is complete */
1982 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1983 if (d
->stream_event
)
1984 d
->stream_event(d
, event
);
1987 pop_dbg(card
->dev
, card
->pop_time
,
1988 "DAPM sequencing finished, waiting %dms\n", card
->pop_time
);
1989 pop_wait(card
->pop_time
);
1991 trace_snd_soc_dapm_done(card
);
1996 #ifdef CONFIG_DEBUG_FS
1997 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1998 char __user
*user_buf
,
1999 size_t count
, loff_t
*ppos
)
2001 struct snd_soc_dapm_widget
*w
= file
->private_data
;
2002 struct snd_soc_card
*card
= w
->dapm
->card
;
2003 enum snd_soc_dapm_direction dir
, rdir
;
2007 struct snd_soc_dapm_path
*p
= NULL
;
2009 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
2013 mutex_lock(&card
->dapm_mutex
);
2015 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2020 in
= is_connected_input_ep(w
, NULL
, NULL
);
2021 out
= is_connected_output_ep(w
, NULL
, NULL
);
2024 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s%s in %d out %d",
2025 w
->name
, w
->power
? "On" : "Off",
2026 w
->force
? " (forced)" : "", in
, out
);
2029 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
2030 " - R%d(0x%x) mask 0x%x",
2031 w
->reg
, w
->reg
, w
->mask
<< w
->shift
);
2033 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
2036 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
2038 w
->active
? "active" : "inactive");
2040 snd_soc_dapm_for_each_direction(dir
) {
2041 rdir
= SND_SOC_DAPM_DIR_REVERSE(dir
);
2042 snd_soc_dapm_widget_for_each_path(w
, dir
, p
) {
2043 if (p
->connected
&& !p
->connected(w
, p
->node
[rdir
]))
2049 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
2050 " %s \"%s\" \"%s\"\n",
2051 (rdir
== SND_SOC_DAPM_DIR_IN
) ? "in" : "out",
2052 p
->name
? p
->name
: "static",
2053 p
->node
[rdir
]->name
);
2057 mutex_unlock(&card
->dapm_mutex
);
2059 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
2065 static const struct file_operations dapm_widget_power_fops
= {
2066 .open
= simple_open
,
2067 .read
= dapm_widget_power_read_file
,
2068 .llseek
= default_llseek
,
2071 static ssize_t
dapm_bias_read_file(struct file
*file
, char __user
*user_buf
,
2072 size_t count
, loff_t
*ppos
)
2074 struct snd_soc_dapm_context
*dapm
= file
->private_data
;
2077 switch (dapm
->bias_level
) {
2078 case SND_SOC_BIAS_ON
:
2081 case SND_SOC_BIAS_PREPARE
:
2082 level
= "Prepare\n";
2084 case SND_SOC_BIAS_STANDBY
:
2085 level
= "Standby\n";
2087 case SND_SOC_BIAS_OFF
:
2091 WARN(1, "Unknown bias_level %d\n", dapm
->bias_level
);
2092 level
= "Unknown\n";
2096 return simple_read_from_buffer(user_buf
, count
, ppos
, level
,
2100 static const struct file_operations dapm_bias_fops
= {
2101 .open
= simple_open
,
2102 .read
= dapm_bias_read_file
,
2103 .llseek
= default_llseek
,
2106 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
2107 struct dentry
*parent
)
2114 dapm
->debugfs_dapm
= debugfs_create_dir("dapm", parent
);
2116 if (!dapm
->debugfs_dapm
) {
2118 "ASoC: Failed to create DAPM debugfs directory\n");
2122 d
= debugfs_create_file("bias_level", 0444,
2123 dapm
->debugfs_dapm
, dapm
,
2127 "ASoC: Failed to create bias level debugfs file\n");
2130 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
2132 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
2135 if (!dapm
->debugfs_dapm
|| !w
->name
)
2138 d
= debugfs_create_file(w
->name
, 0444,
2139 dapm
->debugfs_dapm
, w
,
2140 &dapm_widget_power_fops
);
2142 dev_warn(w
->dapm
->dev
,
2143 "ASoC: Failed to create %s debugfs file\n",
2147 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
2149 debugfs_remove_recursive(dapm
->debugfs_dapm
);
2153 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
2154 struct dentry
*parent
)
2158 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
2162 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
2169 * soc_dapm_connect_path() - Connects or disconnects a path
2170 * @path: The path to update
2171 * @connect: The new connect state of the path. True if the path is connected,
2172 * false if it is disconnected.
2173 * @reason: The reason why the path changed (for debugging only)
2175 static void soc_dapm_connect_path(struct snd_soc_dapm_path
*path
,
2176 bool connect
, const char *reason
)
2178 if (path
->connect
== connect
)
2181 path
->connect
= connect
;
2182 dapm_mark_dirty(path
->source
, reason
);
2183 dapm_mark_dirty(path
->sink
, reason
);
2184 dapm_path_invalidate(path
);
2187 /* test and update the power status of a mux widget */
2188 static int soc_dapm_mux_update_power(struct snd_soc_card
*card
,
2189 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
)
2191 struct snd_soc_dapm_path
*path
;
2195 lockdep_assert_held(&card
->dapm_mutex
);
2197 /* find dapm widget path assoc with kcontrol */
2198 dapm_kcontrol_for_each_path(path
, kcontrol
) {
2200 /* we now need to match the string in the enum to the path */
2201 if (!(strcmp(path
->name
, e
->texts
[mux
])))
2206 soc_dapm_connect_path(path
, connect
, "mux update");
2210 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2215 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context
*dapm
,
2216 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
,
2217 struct snd_soc_dapm_update
*update
)
2219 struct snd_soc_card
*card
= dapm
->card
;
2222 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2223 card
->update
= update
;
2224 ret
= soc_dapm_mux_update_power(card
, kcontrol
, mux
, e
);
2225 card
->update
= NULL
;
2226 mutex_unlock(&card
->dapm_mutex
);
2228 soc_dpcm_runtime_update(card
);
2231 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power
);
2233 /* test and update the power status of a mixer or switch widget */
2234 static int soc_dapm_mixer_update_power(struct snd_soc_card
*card
,
2235 struct snd_kcontrol
*kcontrol
,
2236 int connect
, int rconnect
)
2238 struct snd_soc_dapm_path
*path
;
2241 lockdep_assert_held(&card
->dapm_mutex
);
2243 /* find dapm widget path assoc with kcontrol */
2244 dapm_kcontrol_for_each_path(path
, kcontrol
) {
2246 * Ideally this function should support any number of
2247 * paths and channels. But since kcontrols only come
2248 * in mono and stereo variants, we are limited to 2
2251 * The following code assumes for stereo controls the
2252 * first path (when 'found == 0') is the left channel,
2253 * and all remaining paths (when 'found == 1') are the
2256 * A stereo control is signified by a valid 'rconnect'
2257 * value, either 0 for unconnected, or >= 0 for connected.
2258 * This is chosen instead of using snd_soc_volsw_is_stereo,
2259 * so that the behavior of snd_soc_dapm_mixer_update_power
2260 * doesn't change even when the kcontrol passed in is
2263 * It passes 'connect' as the path connect status for
2264 * the left channel, and 'rconnect' for the right
2267 if (found
&& rconnect
>= 0)
2268 soc_dapm_connect_path(path
, rconnect
, "mixer update");
2270 soc_dapm_connect_path(path
, connect
, "mixer update");
2275 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2280 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context
*dapm
,
2281 struct snd_kcontrol
*kcontrol
, int connect
,
2282 struct snd_soc_dapm_update
*update
)
2284 struct snd_soc_card
*card
= dapm
->card
;
2287 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2288 card
->update
= update
;
2289 ret
= soc_dapm_mixer_update_power(card
, kcontrol
, connect
, -1);
2290 card
->update
= NULL
;
2291 mutex_unlock(&card
->dapm_mutex
);
2293 soc_dpcm_runtime_update(card
);
2296 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power
);
2298 static ssize_t
dapm_widget_show_component(struct snd_soc_component
*cmpnt
,
2301 struct snd_soc_dapm_context
*dapm
= snd_soc_component_get_dapm(cmpnt
);
2302 struct snd_soc_dapm_widget
*w
;
2304 char *state
= "not set";
2306 /* card won't be set for the dummy component, as a spot fix
2307 * we're checking for that case specifically here but in future
2308 * we will ensure that the dummy component looks like others.
2313 list_for_each_entry(w
, &cmpnt
->card
->widgets
, list
) {
2314 if (w
->dapm
!= dapm
)
2317 /* only display widgets that burn power */
2319 case snd_soc_dapm_hp
:
2320 case snd_soc_dapm_mic
:
2321 case snd_soc_dapm_spk
:
2322 case snd_soc_dapm_line
:
2323 case snd_soc_dapm_micbias
:
2324 case snd_soc_dapm_dac
:
2325 case snd_soc_dapm_adc
:
2326 case snd_soc_dapm_pga
:
2327 case snd_soc_dapm_out_drv
:
2328 case snd_soc_dapm_mixer
:
2329 case snd_soc_dapm_mixer_named_ctl
:
2330 case snd_soc_dapm_supply
:
2331 case snd_soc_dapm_regulator_supply
:
2332 case snd_soc_dapm_clock_supply
:
2334 count
+= sprintf(buf
+ count
, "%s: %s\n",
2335 w
->name
, w
->power
? "On":"Off");
2342 switch (snd_soc_dapm_get_bias_level(dapm
)) {
2343 case SND_SOC_BIAS_ON
:
2346 case SND_SOC_BIAS_PREPARE
:
2349 case SND_SOC_BIAS_STANDBY
:
2352 case SND_SOC_BIAS_OFF
:
2356 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
2361 /* show dapm widget status in sys fs */
2362 static ssize_t
dapm_widget_show(struct device
*dev
,
2363 struct device_attribute
*attr
, char *buf
)
2365 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
2368 mutex_lock(&rtd
->card
->dapm_mutex
);
2370 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
2371 struct snd_soc_component
*cmpnt
= rtd
->codec_dais
[i
]->component
;
2373 count
+= dapm_widget_show_component(cmpnt
, buf
+ count
);
2376 mutex_unlock(&rtd
->card
->dapm_mutex
);
2381 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
2383 struct attribute
*soc_dapm_dev_attrs
[] = {
2384 &dev_attr_dapm_widget
.attr
,
2388 static void dapm_free_path(struct snd_soc_dapm_path
*path
)
2390 list_del(&path
->list_node
[SND_SOC_DAPM_DIR_IN
]);
2391 list_del(&path
->list_node
[SND_SOC_DAPM_DIR_OUT
]);
2392 list_del(&path
->list_kcontrol
);
2393 list_del(&path
->list
);
2397 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget
*w
)
2399 struct snd_soc_dapm_path
*p
, *next_p
;
2400 enum snd_soc_dapm_direction dir
;
2404 * remove source and sink paths associated to this widget.
2405 * While removing the path, remove reference to it from both
2406 * source and sink widgets so that path is removed only once.
2408 snd_soc_dapm_for_each_direction(dir
) {
2409 snd_soc_dapm_widget_for_each_path_safe(w
, dir
, p
, next_p
)
2413 kfree(w
->kcontrols
);
2414 kfree_const(w
->name
);
2418 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context
*dapm
)
2420 dapm
->path_sink_cache
.widget
= NULL
;
2421 dapm
->path_source_cache
.widget
= NULL
;
2424 /* free all dapm widgets and resources */
2425 static void dapm_free_widgets(struct snd_soc_dapm_context
*dapm
)
2427 struct snd_soc_dapm_widget
*w
, *next_w
;
2429 list_for_each_entry_safe(w
, next_w
, &dapm
->card
->widgets
, list
) {
2430 if (w
->dapm
!= dapm
)
2432 snd_soc_dapm_free_widget(w
);
2434 snd_soc_dapm_reset_cache(dapm
);
2437 static struct snd_soc_dapm_widget
*dapm_find_widget(
2438 struct snd_soc_dapm_context
*dapm
, const char *pin
,
2439 bool search_other_contexts
)
2441 struct snd_soc_dapm_widget
*w
;
2442 struct snd_soc_dapm_widget
*fallback
= NULL
;
2444 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
2445 if (!strcmp(w
->name
, pin
)) {
2446 if (w
->dapm
== dapm
)
2453 if (search_other_contexts
)
2459 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context
*dapm
,
2460 const char *pin
, int status
)
2462 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
2464 dapm_assert_locked(dapm
);
2467 dev_err(dapm
->dev
, "ASoC: DAPM unknown pin %s\n", pin
);
2471 if (w
->connected
!= status
) {
2472 dapm_mark_dirty(w
, "pin configuration");
2473 dapm_widget_invalidate_input_paths(w
);
2474 dapm_widget_invalidate_output_paths(w
);
2477 w
->connected
= status
;
2485 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2486 * @dapm: DAPM context
2488 * Walks all dapm audio paths and powers widgets according to their
2489 * stream or path usage.
2491 * Requires external locking.
2493 * Returns 0 for success.
2495 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context
*dapm
)
2498 * Suppress early reports (eg, jacks syncing their state) to avoid
2499 * silly DAPM runs during card startup.
2501 if (!dapm
->card
|| !dapm
->card
->instantiated
)
2504 return dapm_power_widgets(dapm
->card
, SND_SOC_DAPM_STREAM_NOP
);
2506 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked
);
2509 * snd_soc_dapm_sync - scan and power dapm paths
2510 * @dapm: DAPM context
2512 * Walks all dapm audio paths and powers widgets according to their
2513 * stream or path usage.
2515 * Returns 0 for success.
2517 int snd_soc_dapm_sync(struct snd_soc_dapm_context
*dapm
)
2521 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2522 ret
= snd_soc_dapm_sync_unlocked(dapm
);
2523 mutex_unlock(&dapm
->card
->dapm_mutex
);
2526 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
2529 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2530 * @w: The widget for which to update the flags
2532 * Some widgets have a dynamic category which depends on which neighbors they
2533 * are connected to. This function update the category for these widgets.
2535 * This function must be called whenever a path is added or removed to a widget.
2537 static void dapm_update_widget_flags(struct snd_soc_dapm_widget
*w
)
2539 enum snd_soc_dapm_direction dir
;
2540 struct snd_soc_dapm_path
*p
;
2544 case snd_soc_dapm_input
:
2545 /* On a fully routed card an input is never a source */
2546 if (w
->dapm
->card
->fully_routed
)
2548 ep
= SND_SOC_DAPM_EP_SOURCE
;
2549 snd_soc_dapm_widget_for_each_source_path(w
, p
) {
2550 if (p
->source
->id
== snd_soc_dapm_micbias
||
2551 p
->source
->id
== snd_soc_dapm_mic
||
2552 p
->source
->id
== snd_soc_dapm_line
||
2553 p
->source
->id
== snd_soc_dapm_output
) {
2559 case snd_soc_dapm_output
:
2560 /* On a fully routed card a output is never a sink */
2561 if (w
->dapm
->card
->fully_routed
)
2563 ep
= SND_SOC_DAPM_EP_SINK
;
2564 snd_soc_dapm_widget_for_each_sink_path(w
, p
) {
2565 if (p
->sink
->id
== snd_soc_dapm_spk
||
2566 p
->sink
->id
== snd_soc_dapm_hp
||
2567 p
->sink
->id
== snd_soc_dapm_line
||
2568 p
->sink
->id
== snd_soc_dapm_input
) {
2574 case snd_soc_dapm_line
:
2576 snd_soc_dapm_for_each_direction(dir
) {
2577 if (!list_empty(&w
->edges
[dir
]))
2578 ep
|= SND_SOC_DAPM_DIR_TO_EP(dir
);
2588 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context
*dapm
,
2589 struct snd_soc_dapm_widget
*source
, struct snd_soc_dapm_widget
*sink
,
2590 const char *control
)
2592 bool dynamic_source
= false;
2593 bool dynamic_sink
= false;
2598 switch (source
->id
) {
2599 case snd_soc_dapm_demux
:
2600 dynamic_source
= true;
2607 case snd_soc_dapm_mux
:
2608 case snd_soc_dapm_switch
:
2609 case snd_soc_dapm_mixer
:
2610 case snd_soc_dapm_mixer_named_ctl
:
2611 dynamic_sink
= true;
2617 if (dynamic_source
&& dynamic_sink
) {
2619 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2620 source
->name
, control
, sink
->name
);
2622 } else if (!dynamic_source
&& !dynamic_sink
) {
2624 "Control not supported for path %s -> [%s] -> %s\n",
2625 source
->name
, control
, sink
->name
);
2632 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context
*dapm
,
2633 struct snd_soc_dapm_widget
*wsource
, struct snd_soc_dapm_widget
*wsink
,
2634 const char *control
,
2635 int (*connected
)(struct snd_soc_dapm_widget
*source
,
2636 struct snd_soc_dapm_widget
*sink
))
2638 struct snd_soc_dapm_widget
*widgets
[2];
2639 enum snd_soc_dapm_direction dir
;
2640 struct snd_soc_dapm_path
*path
;
2643 if (wsink
->is_supply
&& !wsource
->is_supply
) {
2645 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2646 wsource
->name
, wsink
->name
);
2650 if (connected
&& !wsource
->is_supply
) {
2652 "connected() callback only supported for supply widgets (%s -> %s)\n",
2653 wsource
->name
, wsink
->name
);
2657 if (wsource
->is_supply
&& control
) {
2659 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2660 wsource
->name
, control
, wsink
->name
);
2664 ret
= snd_soc_dapm_check_dynamic_path(dapm
, wsource
, wsink
, control
);
2668 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
2672 path
->node
[SND_SOC_DAPM_DIR_IN
] = wsource
;
2673 path
->node
[SND_SOC_DAPM_DIR_OUT
] = wsink
;
2674 widgets
[SND_SOC_DAPM_DIR_IN
] = wsource
;
2675 widgets
[SND_SOC_DAPM_DIR_OUT
] = wsink
;
2677 path
->connected
= connected
;
2678 INIT_LIST_HEAD(&path
->list
);
2679 INIT_LIST_HEAD(&path
->list_kcontrol
);
2681 if (wsource
->is_supply
|| wsink
->is_supply
)
2682 path
->is_supply
= 1;
2684 /* connect static paths */
2685 if (control
== NULL
) {
2688 switch (wsource
->id
) {
2689 case snd_soc_dapm_demux
:
2690 ret
= dapm_connect_mux(dapm
, path
, control
, wsource
);
2698 switch (wsink
->id
) {
2699 case snd_soc_dapm_mux
:
2700 ret
= dapm_connect_mux(dapm
, path
, control
, wsink
);
2704 case snd_soc_dapm_switch
:
2705 case snd_soc_dapm_mixer
:
2706 case snd_soc_dapm_mixer_named_ctl
:
2707 ret
= dapm_connect_mixer(dapm
, path
, control
);
2716 list_add(&path
->list
, &dapm
->card
->paths
);
2717 snd_soc_dapm_for_each_direction(dir
)
2718 list_add(&path
->list_node
[dir
], &widgets
[dir
]->edges
[dir
]);
2720 snd_soc_dapm_for_each_direction(dir
) {
2721 dapm_update_widget_flags(widgets
[dir
]);
2722 dapm_mark_dirty(widgets
[dir
], "Route added");
2725 if (dapm
->card
->instantiated
&& path
->connect
)
2726 dapm_path_invalidate(path
);
2734 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context
*dapm
,
2735 const struct snd_soc_dapm_route
*route
)
2737 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
2738 struct snd_soc_dapm_widget
*wtsource
= NULL
, *wtsink
= NULL
;
2741 char prefixed_sink
[80];
2742 char prefixed_source
[80];
2746 prefix
= soc_dapm_prefix(dapm
);
2748 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
2749 prefix
, route
->sink
);
2750 sink
= prefixed_sink
;
2751 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
2752 prefix
, route
->source
);
2753 source
= prefixed_source
;
2756 source
= route
->source
;
2759 wsource
= dapm_wcache_lookup(&dapm
->path_source_cache
, source
);
2760 wsink
= dapm_wcache_lookup(&dapm
->path_sink_cache
, sink
);
2762 if (wsink
&& wsource
)
2766 * find src and dest widgets over all widgets but favor a widget from
2767 * current DAPM context
2769 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
2770 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
2772 if (w
->dapm
== dapm
) {
2779 if (!wsource
&& !(strcmp(w
->name
, source
))) {
2781 if (w
->dapm
== dapm
) {
2788 /* use widget from another DAPM context if not found from this */
2794 if (wsource
== NULL
) {
2795 dev_err(dapm
->dev
, "ASoC: no source widget found for %s\n",
2799 if (wsink
== NULL
) {
2800 dev_err(dapm
->dev
, "ASoC: no sink widget found for %s\n",
2806 dapm_wcache_update(&dapm
->path_sink_cache
, wsink
);
2807 dapm_wcache_update(&dapm
->path_source_cache
, wsource
);
2809 ret
= snd_soc_dapm_add_path(dapm
, wsource
, wsink
, route
->control
,
2816 dev_warn(dapm
->dev
, "ASoC: no dapm match for %s --> %s --> %s\n",
2817 source
, route
->control
, sink
);
2821 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context
*dapm
,
2822 const struct snd_soc_dapm_route
*route
)
2824 struct snd_soc_dapm_widget
*wsource
, *wsink
;
2825 struct snd_soc_dapm_path
*path
, *p
;
2828 char prefixed_sink
[80];
2829 char prefixed_source
[80];
2832 if (route
->control
) {
2834 "ASoC: Removal of routes with controls not supported\n");
2838 prefix
= soc_dapm_prefix(dapm
);
2840 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
2841 prefix
, route
->sink
);
2842 sink
= prefixed_sink
;
2843 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
2844 prefix
, route
->source
);
2845 source
= prefixed_source
;
2848 source
= route
->source
;
2852 list_for_each_entry(p
, &dapm
->card
->paths
, list
) {
2853 if (strcmp(p
->source
->name
, source
) != 0)
2855 if (strcmp(p
->sink
->name
, sink
) != 0)
2862 wsource
= path
->source
;
2865 dapm_mark_dirty(wsource
, "Route removed");
2866 dapm_mark_dirty(wsink
, "Route removed");
2868 dapm_path_invalidate(path
);
2870 dapm_free_path(path
);
2872 /* Update any path related flags */
2873 dapm_update_widget_flags(wsource
);
2874 dapm_update_widget_flags(wsink
);
2876 dev_warn(dapm
->dev
, "ASoC: Route %s->%s does not exist\n",
2884 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2885 * @dapm: DAPM context
2886 * @route: audio routes
2887 * @num: number of routes
2889 * Connects 2 dapm widgets together via a named audio path. The sink is
2890 * the widget receiving the audio signal, whilst the source is the sender
2891 * of the audio signal.
2893 * Returns 0 for success else error. On error all resources can be freed
2894 * with a call to snd_soc_card_free().
2896 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context
*dapm
,
2897 const struct snd_soc_dapm_route
*route
, int num
)
2901 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2902 for (i
= 0; i
< num
; i
++) {
2903 r
= snd_soc_dapm_add_route(dapm
, route
);
2905 dev_err(dapm
->dev
, "ASoC: Failed to add route %s -> %s -> %s\n",
2907 route
->control
? route
->control
: "direct",
2913 mutex_unlock(&dapm
->card
->dapm_mutex
);
2917 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
2920 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2921 * @dapm: DAPM context
2922 * @route: audio routes
2923 * @num: number of routes
2925 * Removes routes from the DAPM context.
2927 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context
*dapm
,
2928 const struct snd_soc_dapm_route
*route
, int num
)
2932 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2933 for (i
= 0; i
< num
; i
++) {
2934 snd_soc_dapm_del_route(dapm
, route
);
2937 mutex_unlock(&dapm
->card
->dapm_mutex
);
2941 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes
);
2943 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context
*dapm
,
2944 const struct snd_soc_dapm_route
*route
)
2946 struct snd_soc_dapm_widget
*source
= dapm_find_widget(dapm
,
2949 struct snd_soc_dapm_widget
*sink
= dapm_find_widget(dapm
,
2952 struct snd_soc_dapm_path
*path
;
2956 dev_err(dapm
->dev
, "ASoC: Unable to find source %s for weak route\n",
2962 dev_err(dapm
->dev
, "ASoC: Unable to find sink %s for weak route\n",
2967 if (route
->control
|| route
->connected
)
2968 dev_warn(dapm
->dev
, "ASoC: Ignoring control for weak route %s->%s\n",
2969 route
->source
, route
->sink
);
2971 snd_soc_dapm_widget_for_each_sink_path(source
, path
) {
2972 if (path
->sink
== sink
) {
2979 dev_err(dapm
->dev
, "ASoC: No path found for weak route %s->%s\n",
2980 route
->source
, route
->sink
);
2982 dev_warn(dapm
->dev
, "ASoC: %d paths found for weak route %s->%s\n",
2983 count
, route
->source
, route
->sink
);
2989 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2990 * @dapm: DAPM context
2991 * @route: audio routes
2992 * @num: number of routes
2994 * Mark existing routes matching those specified in the passed array
2995 * as being weak, meaning that they are ignored for the purpose of
2996 * power decisions. The main intended use case is for sidetone paths
2997 * which couple audio between other independent paths if they are both
2998 * active in order to make the combination work better at the user
2999 * level but which aren't intended to be "used".
3001 * Note that CODEC drivers should not use this as sidetone type paths
3002 * can frequently also be used as bypass paths.
3004 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context
*dapm
,
3005 const struct snd_soc_dapm_route
*route
, int num
)
3010 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
3011 for (i
= 0; i
< num
; i
++) {
3012 err
= snd_soc_dapm_weak_route(dapm
, route
);
3017 mutex_unlock(&dapm
->card
->dapm_mutex
);
3021 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes
);
3024 * snd_soc_dapm_new_widgets - add new dapm widgets
3025 * @card: card to be checked for new dapm widgets
3027 * Checks the codec for any new dapm widgets and creates them if found.
3029 * Returns 0 for success.
3031 int snd_soc_dapm_new_widgets(struct snd_soc_card
*card
)
3033 struct snd_soc_dapm_widget
*w
;
3036 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
3038 list_for_each_entry(w
, &card
->widgets
, list
)
3043 if (w
->num_kcontrols
) {
3044 w
->kcontrols
= kzalloc(w
->num_kcontrols
*
3045 sizeof(struct snd_kcontrol
*),
3047 if (!w
->kcontrols
) {
3048 mutex_unlock(&card
->dapm_mutex
);
3054 case snd_soc_dapm_switch
:
3055 case snd_soc_dapm_mixer
:
3056 case snd_soc_dapm_mixer_named_ctl
:
3059 case snd_soc_dapm_mux
:
3060 case snd_soc_dapm_demux
:
3063 case snd_soc_dapm_pga
:
3064 case snd_soc_dapm_out_drv
:
3067 case snd_soc_dapm_dai_link
:
3068 dapm_new_dai_link(w
);
3074 /* Read the initial power state from the device */
3076 soc_dapm_read(w
->dapm
, w
->reg
, &val
);
3077 val
= val
>> w
->shift
;
3079 if (val
== w
->on_val
)
3085 dapm_mark_dirty(w
, "new widget");
3086 dapm_debugfs_add_widget(w
);
3089 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
3090 mutex_unlock(&card
->dapm_mutex
);
3093 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
3096 * snd_soc_dapm_get_volsw - dapm mixer get callback
3097 * @kcontrol: mixer control
3098 * @ucontrol: control element information
3100 * Callback to get the value of a dapm mixer control.
3102 * Returns 0 for success.
3104 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
3105 struct snd_ctl_elem_value
*ucontrol
)
3107 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
3108 struct snd_soc_card
*card
= dapm
->card
;
3109 struct soc_mixer_control
*mc
=
3110 (struct soc_mixer_control
*)kcontrol
->private_value
;
3112 unsigned int shift
= mc
->shift
;
3114 unsigned int width
= fls(max
);
3115 unsigned int mask
= (1 << fls(max
)) - 1;
3116 unsigned int invert
= mc
->invert
;
3117 unsigned int reg_val
, val
, rval
= 0;
3120 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3121 if (dapm_kcontrol_is_powered(kcontrol
) && reg
!= SND_SOC_NOPM
) {
3122 ret
= soc_dapm_read(dapm
, reg
, ®_val
);
3123 val
= (reg_val
>> shift
) & mask
;
3125 if (ret
== 0 && reg
!= mc
->rreg
)
3126 ret
= soc_dapm_read(dapm
, mc
->rreg
, ®_val
);
3128 if (snd_soc_volsw_is_stereo(mc
))
3129 rval
= (reg_val
>> mc
->rshift
) & mask
;
3131 reg_val
= dapm_kcontrol_get_value(kcontrol
);
3132 val
= reg_val
& mask
;
3134 if (snd_soc_volsw_is_stereo(mc
))
3135 rval
= (reg_val
>> width
) & mask
;
3137 mutex_unlock(&card
->dapm_mutex
);
3143 ucontrol
->value
.integer
.value
[0] = max
- val
;
3145 ucontrol
->value
.integer
.value
[0] = val
;
3147 if (snd_soc_volsw_is_stereo(mc
)) {
3149 ucontrol
->value
.integer
.value
[1] = max
- rval
;
3151 ucontrol
->value
.integer
.value
[1] = rval
;
3156 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
3159 * snd_soc_dapm_put_volsw - dapm mixer set callback
3160 * @kcontrol: mixer control
3161 * @ucontrol: control element information
3163 * Callback to set the value of a dapm mixer control.
3165 * Returns 0 for success.
3167 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
3168 struct snd_ctl_elem_value
*ucontrol
)
3170 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
3171 struct snd_soc_card
*card
= dapm
->card
;
3172 struct soc_mixer_control
*mc
=
3173 (struct soc_mixer_control
*)kcontrol
->private_value
;
3175 unsigned int shift
= mc
->shift
;
3177 unsigned int width
= fls(max
);
3178 unsigned int mask
= (1 << width
) - 1;
3179 unsigned int invert
= mc
->invert
;
3180 unsigned int val
, rval
= 0;
3181 int connect
, rconnect
= -1, change
, reg_change
= 0;
3182 struct snd_soc_dapm_update update
= { NULL
};
3185 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
3191 if (snd_soc_volsw_is_stereo(mc
)) {
3192 rval
= (ucontrol
->value
.integer
.value
[1] & mask
);
3198 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3200 /* This assumes field width < (bits in unsigned int / 2) */
3201 if (width
> sizeof(unsigned int) * 8 / 2)
3203 "ASoC: control %s field width limit exceeded\n",
3205 change
= dapm_kcontrol_set_value(kcontrol
, val
| (rval
<< width
));
3207 if (reg
!= SND_SOC_NOPM
) {
3209 rval
= rval
<< mc
->rshift
;
3211 reg_change
= soc_dapm_test_bits(dapm
, reg
, mask
<< shift
, val
);
3213 if (snd_soc_volsw_is_stereo(mc
))
3214 reg_change
|= soc_dapm_test_bits(dapm
, mc
->rreg
,
3219 if (change
|| reg_change
) {
3221 if (snd_soc_volsw_is_stereo(mc
)) {
3222 update
.has_second_set
= true;
3223 update
.reg2
= mc
->rreg
;
3224 update
.mask2
= mask
<< mc
->rshift
;
3227 update
.kcontrol
= kcontrol
;
3229 update
.mask
= mask
<< shift
;
3231 card
->update
= &update
;
3233 change
|= reg_change
;
3235 ret
= soc_dapm_mixer_update_power(card
, kcontrol
, connect
,
3238 card
->update
= NULL
;
3241 mutex_unlock(&card
->dapm_mutex
);
3244 soc_dpcm_runtime_update(card
);
3248 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
3251 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3252 * @kcontrol: mixer control
3253 * @ucontrol: control element information
3255 * Callback to get the value of a dapm enumerated double mixer control.
3257 * Returns 0 for success.
3259 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
3260 struct snd_ctl_elem_value
*ucontrol
)
3262 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
3263 struct snd_soc_card
*card
= dapm
->card
;
3264 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
3265 unsigned int reg_val
, val
;
3267 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3268 if (e
->reg
!= SND_SOC_NOPM
&& dapm_kcontrol_is_powered(kcontrol
)) {
3269 int ret
= soc_dapm_read(dapm
, e
->reg
, ®_val
);
3271 mutex_unlock(&card
->dapm_mutex
);
3275 reg_val
= dapm_kcontrol_get_value(kcontrol
);
3277 mutex_unlock(&card
->dapm_mutex
);
3279 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
3280 ucontrol
->value
.enumerated
.item
[0] = snd_soc_enum_val_to_item(e
, val
);
3281 if (e
->shift_l
!= e
->shift_r
) {
3282 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
3283 val
= snd_soc_enum_val_to_item(e
, val
);
3284 ucontrol
->value
.enumerated
.item
[1] = val
;
3289 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
3292 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3293 * @kcontrol: mixer control
3294 * @ucontrol: control element information
3296 * Callback to set the value of a dapm enumerated double mixer control.
3298 * Returns 0 for success.
3300 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
3301 struct snd_ctl_elem_value
*ucontrol
)
3303 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
3304 struct snd_soc_card
*card
= dapm
->card
;
3305 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
3306 unsigned int *item
= ucontrol
->value
.enumerated
.item
;
3307 unsigned int val
, change
, reg_change
= 0;
3309 struct snd_soc_dapm_update update
= { NULL
};
3312 if (item
[0] >= e
->items
)
3315 val
= snd_soc_enum_item_to_val(e
, item
[0]) << e
->shift_l
;
3316 mask
= e
->mask
<< e
->shift_l
;
3317 if (e
->shift_l
!= e
->shift_r
) {
3318 if (item
[1] > e
->items
)
3320 val
|= snd_soc_enum_item_to_val(e
, item
[1]) << e
->shift_r
;
3321 mask
|= e
->mask
<< e
->shift_r
;
3324 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3326 change
= dapm_kcontrol_set_value(kcontrol
, val
);
3328 if (e
->reg
!= SND_SOC_NOPM
)
3329 reg_change
= soc_dapm_test_bits(dapm
, e
->reg
, mask
, val
);
3331 if (change
|| reg_change
) {
3333 update
.kcontrol
= kcontrol
;
3334 update
.reg
= e
->reg
;
3337 card
->update
= &update
;
3339 change
|= reg_change
;
3341 ret
= soc_dapm_mux_update_power(card
, kcontrol
, item
[0], e
);
3343 card
->update
= NULL
;
3346 mutex_unlock(&card
->dapm_mutex
);
3349 soc_dpcm_runtime_update(card
);
3353 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
3356 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3358 * @kcontrol: mixer control
3359 * @uinfo: control element information
3361 * Callback to provide information about a pin switch control.
3363 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
3364 struct snd_ctl_elem_info
*uinfo
)
3366 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
3368 uinfo
->value
.integer
.min
= 0;
3369 uinfo
->value
.integer
.max
= 1;
3373 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
3376 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3378 * @kcontrol: mixer control
3381 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
3382 struct snd_ctl_elem_value
*ucontrol
)
3384 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
3385 const char *pin
= (const char *)kcontrol
->private_value
;
3387 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3389 ucontrol
->value
.integer
.value
[0] =
3390 snd_soc_dapm_get_pin_status(&card
->dapm
, pin
);
3392 mutex_unlock(&card
->dapm_mutex
);
3396 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
3399 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3401 * @kcontrol: mixer control
3404 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
3405 struct snd_ctl_elem_value
*ucontrol
)
3407 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
3408 const char *pin
= (const char *)kcontrol
->private_value
;
3410 if (ucontrol
->value
.integer
.value
[0])
3411 snd_soc_dapm_enable_pin(&card
->dapm
, pin
);
3413 snd_soc_dapm_disable_pin(&card
->dapm
, pin
);
3415 snd_soc_dapm_sync(&card
->dapm
);
3418 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
3420 struct snd_soc_dapm_widget
*
3421 snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
3422 const struct snd_soc_dapm_widget
*widget
)
3424 struct snd_soc_dapm_widget
*w
;
3426 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3427 w
= snd_soc_dapm_new_control_unlocked(dapm
, widget
);
3428 /* Do not nag about probe deferrals */
3430 int ret
= PTR_ERR(w
);
3432 if (ret
!= -EPROBE_DEFER
)
3434 "ASoC: Failed to create DAPM control %s (%d)\n",
3440 "ASoC: Failed to create DAPM control %s\n",
3444 mutex_unlock(&dapm
->card
->dapm_mutex
);
3447 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
3449 struct snd_soc_dapm_widget
*
3450 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context
*dapm
,
3451 const struct snd_soc_dapm_widget
*widget
)
3453 enum snd_soc_dapm_direction dir
;
3454 struct snd_soc_dapm_widget
*w
;
3458 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
3462 case snd_soc_dapm_regulator_supply
:
3463 w
->regulator
= devm_regulator_get(dapm
->dev
, w
->name
);
3464 if (IS_ERR(w
->regulator
)) {
3465 ret
= PTR_ERR(w
->regulator
);
3466 if (ret
== -EPROBE_DEFER
)
3467 return ERR_PTR(ret
);
3468 dev_err(dapm
->dev
, "ASoC: Failed to request %s: %d\n",
3473 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
3474 ret
= regulator_allow_bypass(w
->regulator
, true);
3476 dev_warn(w
->dapm
->dev
,
3477 "ASoC: Failed to bypass %s: %d\n",
3481 case snd_soc_dapm_clock_supply
:
3482 #ifdef CONFIG_CLKDEV_LOOKUP
3483 w
->clk
= devm_clk_get(dapm
->dev
, w
->name
);
3484 if (IS_ERR(w
->clk
)) {
3485 ret
= PTR_ERR(w
->clk
);
3486 if (ret
== -EPROBE_DEFER
)
3487 return ERR_PTR(ret
);
3488 dev_err(dapm
->dev
, "ASoC: Failed to request %s: %d\n",
3500 prefix
= soc_dapm_prefix(dapm
);
3502 w
->name
= kasprintf(GFP_KERNEL
, "%s %s", prefix
, widget
->name
);
3504 w
->name
= kstrdup_const(widget
->name
, GFP_KERNEL
);
3505 if (w
->name
== NULL
) {
3511 case snd_soc_dapm_mic
:
3512 w
->is_ep
= SND_SOC_DAPM_EP_SOURCE
;
3513 w
->power_check
= dapm_generic_check_power
;
3515 case snd_soc_dapm_input
:
3516 if (!dapm
->card
->fully_routed
)
3517 w
->is_ep
= SND_SOC_DAPM_EP_SOURCE
;
3518 w
->power_check
= dapm_generic_check_power
;
3520 case snd_soc_dapm_spk
:
3521 case snd_soc_dapm_hp
:
3522 w
->is_ep
= SND_SOC_DAPM_EP_SINK
;
3523 w
->power_check
= dapm_generic_check_power
;
3525 case snd_soc_dapm_output
:
3526 if (!dapm
->card
->fully_routed
)
3527 w
->is_ep
= SND_SOC_DAPM_EP_SINK
;
3528 w
->power_check
= dapm_generic_check_power
;
3530 case snd_soc_dapm_vmid
:
3531 case snd_soc_dapm_siggen
:
3532 w
->is_ep
= SND_SOC_DAPM_EP_SOURCE
;
3533 w
->power_check
= dapm_always_on_check_power
;
3535 case snd_soc_dapm_sink
:
3536 w
->is_ep
= SND_SOC_DAPM_EP_SINK
;
3537 w
->power_check
= dapm_always_on_check_power
;
3540 case snd_soc_dapm_mux
:
3541 case snd_soc_dapm_demux
:
3542 case snd_soc_dapm_switch
:
3543 case snd_soc_dapm_mixer
:
3544 case snd_soc_dapm_mixer_named_ctl
:
3545 case snd_soc_dapm_adc
:
3546 case snd_soc_dapm_aif_out
:
3547 case snd_soc_dapm_dac
:
3548 case snd_soc_dapm_aif_in
:
3549 case snd_soc_dapm_pga
:
3550 case snd_soc_dapm_out_drv
:
3551 case snd_soc_dapm_micbias
:
3552 case snd_soc_dapm_line
:
3553 case snd_soc_dapm_dai_link
:
3554 case snd_soc_dapm_dai_out
:
3555 case snd_soc_dapm_dai_in
:
3556 w
->power_check
= dapm_generic_check_power
;
3558 case snd_soc_dapm_supply
:
3559 case snd_soc_dapm_regulator_supply
:
3560 case snd_soc_dapm_clock_supply
:
3561 case snd_soc_dapm_kcontrol
:
3563 w
->power_check
= dapm_supply_check_power
;
3566 w
->power_check
= dapm_always_on_check_power
;
3571 INIT_LIST_HEAD(&w
->list
);
3572 INIT_LIST_HEAD(&w
->dirty
);
3573 list_add_tail(&w
->list
, &dapm
->card
->widgets
);
3575 snd_soc_dapm_for_each_direction(dir
) {
3576 INIT_LIST_HEAD(&w
->edges
[dir
]);
3577 w
->endpoints
[dir
] = -1;
3580 /* machine layer sets up unconnected pins and insertions */
3586 * snd_soc_dapm_new_controls - create new dapm controls
3587 * @dapm: DAPM context
3588 * @widget: widget array
3589 * @num: number of widgets
3591 * Creates new DAPM controls based upon the templates.
3593 * Returns 0 for success else error.
3595 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context
*dapm
,
3596 const struct snd_soc_dapm_widget
*widget
,
3599 struct snd_soc_dapm_widget
*w
;
3603 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
3604 for (i
= 0; i
< num
; i
++) {
3605 w
= snd_soc_dapm_new_control_unlocked(dapm
, widget
);
3608 /* Do not nag about probe deferrals */
3609 if (ret
== -EPROBE_DEFER
)
3612 "ASoC: Failed to create DAPM control %s (%d)\n",
3618 "ASoC: Failed to create DAPM control %s\n",
3625 mutex_unlock(&dapm
->card
->dapm_mutex
);
3628 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
3630 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget
*w
,
3631 struct snd_kcontrol
*kcontrol
, int event
)
3633 struct snd_soc_dapm_path
*source_p
, *sink_p
;
3634 struct snd_soc_dai
*source
, *sink
;
3635 const struct snd_soc_pcm_stream
*config
= w
->params
+ w
->params_select
;
3636 struct snd_pcm_substream substream
;
3637 struct snd_pcm_hw_params
*params
= NULL
;
3638 struct snd_pcm_runtime
*runtime
= NULL
;
3642 if (WARN_ON(!config
) ||
3643 WARN_ON(list_empty(&w
->edges
[SND_SOC_DAPM_DIR_OUT
]) ||
3644 list_empty(&w
->edges
[SND_SOC_DAPM_DIR_IN
])))
3647 /* We only support a single source and sink, pick the first */
3648 source_p
= list_first_entry(&w
->edges
[SND_SOC_DAPM_DIR_OUT
],
3649 struct snd_soc_dapm_path
,
3650 list_node
[SND_SOC_DAPM_DIR_OUT
]);
3651 sink_p
= list_first_entry(&w
->edges
[SND_SOC_DAPM_DIR_IN
],
3652 struct snd_soc_dapm_path
,
3653 list_node
[SND_SOC_DAPM_DIR_IN
]);
3655 source
= source_p
->source
->priv
;
3656 sink
= sink_p
->sink
->priv
;
3658 /* Be a little careful as we don't want to overflow the mask array */
3659 if (config
->formats
) {
3660 fmt
= ffs(config
->formats
) - 1;
3662 dev_warn(w
->dapm
->dev
, "ASoC: Invalid format %llx specified\n",
3667 /* Currently very limited parameter selection */
3668 params
= kzalloc(sizeof(*params
), GFP_KERNEL
);
3673 snd_mask_set(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), fmt
);
3675 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
)->min
=
3677 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
)->max
=
3680 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
)->min
3681 = config
->channels_min
;
3682 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
)->max
3683 = config
->channels_max
;
3685 memset(&substream
, 0, sizeof(substream
));
3687 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3688 runtime
= kzalloc(sizeof(*runtime
), GFP_KERNEL
);
3693 substream
.runtime
= runtime
;
3696 case SND_SOC_DAPM_PRE_PMU
:
3697 substream
.stream
= SNDRV_PCM_STREAM_CAPTURE
;
3698 if (source
->driver
->ops
&& source
->driver
->ops
->startup
) {
3699 ret
= source
->driver
->ops
->startup(&substream
, source
);
3701 dev_err(source
->dev
,
3702 "ASoC: startup() failed: %d\n", ret
);
3707 ret
= soc_dai_hw_params(&substream
, params
, source
);
3711 substream
.stream
= SNDRV_PCM_STREAM_PLAYBACK
;
3712 if (sink
->driver
->ops
&& sink
->driver
->ops
->startup
) {
3713 ret
= sink
->driver
->ops
->startup(&substream
, sink
);
3716 "ASoC: startup() failed: %d\n", ret
);
3721 ret
= soc_dai_hw_params(&substream
, params
, sink
);
3726 case SND_SOC_DAPM_POST_PMU
:
3727 ret
= snd_soc_dai_digital_mute(sink
, 0,
3728 SNDRV_PCM_STREAM_PLAYBACK
);
3729 if (ret
!= 0 && ret
!= -ENOTSUPP
)
3730 dev_warn(sink
->dev
, "ASoC: Failed to unmute: %d\n", ret
);
3734 case SND_SOC_DAPM_PRE_PMD
:
3735 ret
= snd_soc_dai_digital_mute(sink
, 1,
3736 SNDRV_PCM_STREAM_PLAYBACK
);
3737 if (ret
!= 0 && ret
!= -ENOTSUPP
)
3738 dev_warn(sink
->dev
, "ASoC: Failed to mute: %d\n", ret
);
3742 if (source
->driver
->ops
&& source
->driver
->ops
->shutdown
) {
3743 substream
.stream
= SNDRV_PCM_STREAM_CAPTURE
;
3744 source
->driver
->ops
->shutdown(&substream
, source
);
3748 if (sink
->driver
->ops
&& sink
->driver
->ops
->shutdown
) {
3749 substream
.stream
= SNDRV_PCM_STREAM_PLAYBACK
;
3750 sink
->driver
->ops
->shutdown(&substream
, sink
);
3755 WARN(1, "Unknown event %d\n", event
);
3765 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol
*kcontrol
,
3766 struct snd_ctl_elem_value
*ucontrol
)
3768 struct snd_soc_dapm_widget
*w
= snd_kcontrol_chip(kcontrol
);
3770 ucontrol
->value
.enumerated
.item
[0] = w
->params_select
;
3775 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol
*kcontrol
,
3776 struct snd_ctl_elem_value
*ucontrol
)
3778 struct snd_soc_dapm_widget
*w
= snd_kcontrol_chip(kcontrol
);
3780 /* Can't change the config when widget is already powered */
3784 if (ucontrol
->value
.enumerated
.item
[0] == w
->params_select
)
3787 if (ucontrol
->value
.enumerated
.item
[0] >= w
->num_params
)
3790 w
->params_select
= ucontrol
->value
.enumerated
.item
[0];
3795 int snd_soc_dapm_new_pcm(struct snd_soc_card
*card
,
3796 const struct snd_soc_pcm_stream
*params
,
3797 unsigned int num_params
,
3798 struct snd_soc_dapm_widget
*source
,
3799 struct snd_soc_dapm_widget
*sink
)
3801 struct snd_soc_dapm_widget
template;
3802 struct snd_soc_dapm_widget
*w
;
3805 unsigned long private_value
;
3806 const char **w_param_text
;
3807 struct soc_enum w_param_enum
[] = {
3808 SOC_ENUM_SINGLE(0, 0, 0, NULL
),
3810 struct snd_kcontrol_new kcontrol_dai_link
[] = {
3811 SOC_ENUM_EXT(NULL
, w_param_enum
[0],
3812 snd_soc_dapm_dai_link_get
,
3813 snd_soc_dapm_dai_link_put
),
3815 const struct snd_soc_pcm_stream
*config
= params
;
3817 w_param_text
= devm_kcalloc(card
->dev
, num_params
,
3818 sizeof(char *), GFP_KERNEL
);
3822 link_name
= devm_kasprintf(card
->dev
, GFP_KERNEL
, "%s-%s",
3823 source
->name
, sink
->name
);
3826 goto outfree_w_param
;
3829 for (count
= 0 ; count
< num_params
; count
++) {
3830 if (!config
->stream_name
) {
3831 dev_warn(card
->dapm
.dev
,
3832 "ASoC: anonymous config %d for dai link %s\n",
3834 w_param_text
[count
] =
3835 devm_kasprintf(card
->dev
, GFP_KERNEL
,
3836 "Anonymous Configuration %d",
3838 if (!w_param_text
[count
]) {
3840 goto outfree_link_name
;
3843 w_param_text
[count
] = devm_kmemdup(card
->dev
,
3844 config
->stream_name
,
3845 strlen(config
->stream_name
) + 1,
3847 if (!w_param_text
[count
]) {
3849 goto outfree_link_name
;
3854 w_param_enum
[0].items
= num_params
;
3855 w_param_enum
[0].texts
= w_param_text
;
3857 memset(&template, 0, sizeof(template));
3858 template.reg
= SND_SOC_NOPM
;
3859 template.id
= snd_soc_dapm_dai_link
;
3860 template.name
= link_name
;
3861 template.event
= snd_soc_dai_link_event
;
3862 template.event_flags
= SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMU
|
3863 SND_SOC_DAPM_PRE_PMD
;
3864 template.num_kcontrols
= 1;
3865 /* duplicate w_param_enum on heap so that memory persists */
3867 (unsigned long) devm_kmemdup(card
->dev
,
3868 (void *)(kcontrol_dai_link
[0].private_value
),
3869 sizeof(struct soc_enum
), GFP_KERNEL
);
3870 if (!private_value
) {
3871 dev_err(card
->dev
, "ASoC: Failed to create control for %s widget\n",
3874 goto outfree_link_name
;
3876 kcontrol_dai_link
[0].private_value
= private_value
;
3877 /* duplicate kcontrol_dai_link on heap so that memory persists */
3878 template.kcontrol_news
=
3879 devm_kmemdup(card
->dev
, &kcontrol_dai_link
[0],
3880 sizeof(struct snd_kcontrol_new
),
3882 if (!template.kcontrol_news
) {
3883 dev_err(card
->dev
, "ASoC: Failed to create control for %s widget\n",
3886 goto outfree_private_value
;
3889 dev_dbg(card
->dev
, "ASoC: adding %s widget\n", link_name
);
3891 w
= snd_soc_dapm_new_control_unlocked(&card
->dapm
, &template);
3894 /* Do not nag about probe deferrals */
3895 if (ret
!= -EPROBE_DEFER
)
3897 "ASoC: Failed to create %s widget (%d)\n",
3899 goto outfree_kcontrol_news
;
3902 dev_err(card
->dev
, "ASoC: Failed to create %s widget\n",
3905 goto outfree_kcontrol_news
;
3909 w
->num_params
= num_params
;
3911 ret
= snd_soc_dapm_add_path(&card
->dapm
, source
, w
, NULL
, NULL
);
3914 return snd_soc_dapm_add_path(&card
->dapm
, w
, sink
, NULL
, NULL
);
3917 devm_kfree(card
->dev
, w
);
3918 outfree_kcontrol_news
:
3919 devm_kfree(card
->dev
, (void *)template.kcontrol_news
);
3920 outfree_private_value
:
3921 devm_kfree(card
->dev
, (void *)private_value
);
3923 devm_kfree(card
->dev
, link_name
);
3925 for (count
= 0 ; count
< num_params
; count
++)
3926 devm_kfree(card
->dev
, (void *)w_param_text
[count
]);
3927 devm_kfree(card
->dev
, w_param_text
);
3932 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context
*dapm
,
3933 struct snd_soc_dai
*dai
)
3935 struct snd_soc_dapm_widget
template;
3936 struct snd_soc_dapm_widget
*w
;
3938 WARN_ON(dapm
->dev
!= dai
->dev
);
3940 memset(&template, 0, sizeof(template));
3941 template.reg
= SND_SOC_NOPM
;
3943 if (dai
->driver
->playback
.stream_name
) {
3944 template.id
= snd_soc_dapm_dai_in
;
3945 template.name
= dai
->driver
->playback
.stream_name
;
3946 template.sname
= dai
->driver
->playback
.stream_name
;
3948 dev_dbg(dai
->dev
, "ASoC: adding %s widget\n",
3951 w
= snd_soc_dapm_new_control_unlocked(dapm
, &template);
3953 int ret
= PTR_ERR(w
);
3955 /* Do not nag about probe deferrals */
3956 if (ret
!= -EPROBE_DEFER
)
3958 "ASoC: Failed to create %s widget (%d)\n",
3959 dai
->driver
->playback
.stream_name
, ret
);
3963 dev_err(dapm
->dev
, "ASoC: Failed to create %s widget\n",
3964 dai
->driver
->playback
.stream_name
);
3969 dai
->playback_widget
= w
;
3972 if (dai
->driver
->capture
.stream_name
) {
3973 template.id
= snd_soc_dapm_dai_out
;
3974 template.name
= dai
->driver
->capture
.stream_name
;
3975 template.sname
= dai
->driver
->capture
.stream_name
;
3977 dev_dbg(dai
->dev
, "ASoC: adding %s widget\n",
3980 w
= snd_soc_dapm_new_control_unlocked(dapm
, &template);
3982 int ret
= PTR_ERR(w
);
3984 /* Do not nag about probe deferrals */
3985 if (ret
!= -EPROBE_DEFER
)
3987 "ASoC: Failed to create %s widget (%d)\n",
3988 dai
->driver
->playback
.stream_name
, ret
);
3992 dev_err(dapm
->dev
, "ASoC: Failed to create %s widget\n",
3993 dai
->driver
->capture
.stream_name
);
3998 dai
->capture_widget
= w
;
4004 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card
*card
)
4006 struct snd_soc_dapm_widget
*dai_w
, *w
;
4007 struct snd_soc_dapm_widget
*src
, *sink
;
4008 struct snd_soc_dai
*dai
;
4010 /* For each DAI widget... */
4011 list_for_each_entry(dai_w
, &card
->widgets
, list
) {
4012 switch (dai_w
->id
) {
4013 case snd_soc_dapm_dai_in
:
4014 case snd_soc_dapm_dai_out
:
4022 /* ...find all widgets with the same stream and link them */
4023 list_for_each_entry(w
, &card
->widgets
, list
) {
4024 if (w
->dapm
!= dai_w
->dapm
)
4028 case snd_soc_dapm_dai_in
:
4029 case snd_soc_dapm_dai_out
:
4035 if (!w
->sname
|| !strstr(w
->sname
, dai_w
->sname
))
4038 if (dai_w
->id
== snd_soc_dapm_dai_in
) {
4045 dev_dbg(dai
->dev
, "%s -> %s\n", src
->name
, sink
->name
);
4046 snd_soc_dapm_add_path(w
->dapm
, src
, sink
, NULL
, NULL
);
4053 static void dapm_connect_dai_link_widgets(struct snd_soc_card
*card
,
4054 struct snd_soc_pcm_runtime
*rtd
)
4056 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
4057 struct snd_soc_dapm_widget
*sink
, *source
;
4060 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
4061 struct snd_soc_dai
*codec_dai
= rtd
->codec_dais
[i
];
4063 /* connect BE DAI playback if widgets are valid */
4064 if (codec_dai
->playback_widget
&& cpu_dai
->playback_widget
) {
4065 source
= cpu_dai
->playback_widget
;
4066 sink
= codec_dai
->playback_widget
;
4067 dev_dbg(rtd
->dev
, "connected DAI link %s:%s -> %s:%s\n",
4068 cpu_dai
->component
->name
, source
->name
,
4069 codec_dai
->component
->name
, sink
->name
);
4071 snd_soc_dapm_add_path(&card
->dapm
, source
, sink
,
4075 /* connect BE DAI capture if widgets are valid */
4076 if (codec_dai
->capture_widget
&& cpu_dai
->capture_widget
) {
4077 source
= codec_dai
->capture_widget
;
4078 sink
= cpu_dai
->capture_widget
;
4079 dev_dbg(rtd
->dev
, "connected DAI link %s:%s -> %s:%s\n",
4080 codec_dai
->component
->name
, source
->name
,
4081 cpu_dai
->component
->name
, sink
->name
);
4083 snd_soc_dapm_add_path(&card
->dapm
, source
, sink
,
4089 static void soc_dapm_dai_stream_event(struct snd_soc_dai
*dai
, int stream
,
4092 struct snd_soc_dapm_widget
*w
;
4095 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
4096 w
= dai
->playback_widget
;
4098 w
= dai
->capture_widget
;
4101 dapm_mark_dirty(w
, "stream event");
4103 if (w
->id
== snd_soc_dapm_dai_in
) {
4104 ep
= SND_SOC_DAPM_EP_SOURCE
;
4105 dapm_widget_invalidate_input_paths(w
);
4107 ep
= SND_SOC_DAPM_EP_SINK
;
4108 dapm_widget_invalidate_output_paths(w
);
4112 case SND_SOC_DAPM_STREAM_START
:
4116 case SND_SOC_DAPM_STREAM_STOP
:
4120 case SND_SOC_DAPM_STREAM_SUSPEND
:
4121 case SND_SOC_DAPM_STREAM_RESUME
:
4122 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
4123 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
4129 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card
*card
)
4131 struct snd_soc_pcm_runtime
*rtd
;
4133 /* for each BE DAI link... */
4134 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
4136 * dynamic FE links have no fixed DAI mapping.
4137 * CODEC<->CODEC links have no direct connection.
4139 if (rtd
->dai_link
->dynamic
|| rtd
->dai_link
->params
)
4142 dapm_connect_dai_link_widgets(card
, rtd
);
4146 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
, int stream
,
4151 soc_dapm_dai_stream_event(rtd
->cpu_dai
, stream
, event
);
4152 for (i
= 0; i
< rtd
->num_codecs
; i
++)
4153 soc_dapm_dai_stream_event(rtd
->codec_dais
[i
], stream
, event
);
4155 dapm_power_widgets(rtd
->card
, event
);
4159 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4160 * @rtd: PCM runtime data
4161 * @stream: stream name
4162 * @event: stream event
4164 * Sends a stream event to the dapm core. The core then makes any
4165 * necessary widget power changes.
4167 * Returns 0 for success else error.
4169 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
, int stream
,
4172 struct snd_soc_card
*card
= rtd
->card
;
4174 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4175 soc_dapm_stream_event(rtd
, stream
, event
);
4176 mutex_unlock(&card
->dapm_mutex
);
4180 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4181 * @dapm: DAPM context
4184 * Enables input/output pin and its parents or children widgets iff there is
4185 * a valid audio route and active audio stream.
4187 * Requires external locking.
4189 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4190 * do any widget power switching.
4192 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
4195 return snd_soc_dapm_set_pin(dapm
, pin
, 1);
4197 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked
);
4200 * snd_soc_dapm_enable_pin - enable pin.
4201 * @dapm: DAPM context
4204 * Enables input/output pin and its parents or children widgets iff there is
4205 * a valid audio route and active audio stream.
4207 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4208 * do any widget power switching.
4210 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
4214 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4216 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 1);
4218 mutex_unlock(&dapm
->card
->dapm_mutex
);
4222 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
4225 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4226 * @dapm: DAPM context
4229 * Enables input/output pin regardless of any other state. This is
4230 * intended for use with microphone bias supplies used in microphone
4233 * Requires external locking.
4235 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4236 * do any widget power switching.
4238 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
4241 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
4244 dev_err(dapm
->dev
, "ASoC: unknown pin %s\n", pin
);
4248 dev_dbg(w
->dapm
->dev
, "ASoC: force enable pin %s\n", pin
);
4249 if (!w
->connected
) {
4251 * w->force does not affect the number of input or output paths,
4252 * so we only have to recheck if w->connected is changed
4254 dapm_widget_invalidate_input_paths(w
);
4255 dapm_widget_invalidate_output_paths(w
);
4259 dapm_mark_dirty(w
, "force enable");
4263 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked
);
4266 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4267 * @dapm: DAPM context
4270 * Enables input/output pin regardless of any other state. This is
4271 * intended for use with microphone bias supplies used in microphone
4274 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4275 * do any widget power switching.
4277 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context
*dapm
,
4282 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4284 ret
= snd_soc_dapm_force_enable_pin_unlocked(dapm
, pin
);
4286 mutex_unlock(&dapm
->card
->dapm_mutex
);
4290 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
4293 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4294 * @dapm: DAPM context
4297 * Disables input/output pin and its parents or children widgets.
4299 * Requires external locking.
4301 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4302 * do any widget power switching.
4304 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
4307 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
4309 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked
);
4312 * snd_soc_dapm_disable_pin - disable pin.
4313 * @dapm: DAPM context
4316 * Disables input/output pin and its parents or children widgets.
4318 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4319 * do any widget power switching.
4321 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context
*dapm
,
4326 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4328 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 0);
4330 mutex_unlock(&dapm
->card
->dapm_mutex
);
4334 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
4337 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4338 * @dapm: DAPM context
4341 * Marks the specified pin as being not connected, disabling it along
4342 * any parent or child widgets. At present this is identical to
4343 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4344 * additional things such as disabling controls which only affect
4345 * paths through the pin.
4347 * Requires external locking.
4349 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4350 * do any widget power switching.
4352 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
4355 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
4357 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked
);
4360 * snd_soc_dapm_nc_pin - permanently disable pin.
4361 * @dapm: DAPM context
4364 * Marks the specified pin as being not connected, disabling it along
4365 * any parent or child widgets. At present this is identical to
4366 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4367 * additional things such as disabling controls which only affect
4368 * paths through the pin.
4370 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4371 * do any widget power switching.
4373 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
4377 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4379 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 0);
4381 mutex_unlock(&dapm
->card
->dapm_mutex
);
4385 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
4388 * snd_soc_dapm_get_pin_status - get audio pin status
4389 * @dapm: DAPM context
4390 * @pin: audio signal pin endpoint (or start point)
4392 * Get audio pin status - connected or disconnected.
4394 * Returns 1 for connected otherwise 0.
4396 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context
*dapm
,
4399 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
4402 return w
->connected
;
4406 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
4409 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4410 * @dapm: DAPM context
4411 * @pin: audio signal pin endpoint (or start point)
4413 * Mark the given endpoint or pin as ignoring suspend. When the
4414 * system is disabled a path between two endpoints flagged as ignoring
4415 * suspend will not be disabled. The path must already be enabled via
4416 * normal means at suspend time, it will not be turned on if it was not
4419 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context
*dapm
,
4422 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, false);
4425 dev_err(dapm
->dev
, "ASoC: unknown pin %s\n", pin
);
4429 w
->ignore_suspend
= 1;
4433 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
4436 * snd_soc_dapm_free - free dapm resources
4437 * @dapm: DAPM context
4439 * Free all dapm widgets and resources.
4441 void snd_soc_dapm_free(struct snd_soc_dapm_context
*dapm
)
4443 dapm_debugfs_cleanup(dapm
);
4444 dapm_free_widgets(dapm
);
4445 list_del(&dapm
->list
);
4447 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
4449 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context
*dapm
)
4451 struct snd_soc_card
*card
= dapm
->card
;
4452 struct snd_soc_dapm_widget
*w
;
4453 LIST_HEAD(down_list
);
4456 mutex_lock(&card
->dapm_mutex
);
4458 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
4459 if (w
->dapm
!= dapm
)
4462 dapm_seq_insert(w
, &down_list
, false);
4468 /* If there were no widgets to power down we're already in
4472 if (dapm
->bias_level
== SND_SOC_BIAS_ON
)
4473 snd_soc_dapm_set_bias_level(dapm
,
4474 SND_SOC_BIAS_PREPARE
);
4475 dapm_seq_run(card
, &down_list
, 0, false);
4476 if (dapm
->bias_level
== SND_SOC_BIAS_PREPARE
)
4477 snd_soc_dapm_set_bias_level(dapm
,
4478 SND_SOC_BIAS_STANDBY
);
4481 mutex_unlock(&card
->dapm_mutex
);
4485 * snd_soc_dapm_shutdown - callback for system shutdown
4487 void snd_soc_dapm_shutdown(struct snd_soc_card
*card
)
4489 struct snd_soc_dapm_context
*dapm
;
4491 list_for_each_entry(dapm
, &card
->dapm_list
, list
) {
4492 if (dapm
!= &card
->dapm
) {
4493 soc_dapm_shutdown_dapm(dapm
);
4494 if (dapm
->bias_level
== SND_SOC_BIAS_STANDBY
)
4495 snd_soc_dapm_set_bias_level(dapm
,
4500 soc_dapm_shutdown_dapm(&card
->dapm
);
4501 if (card
->dapm
.bias_level
== SND_SOC_BIAS_STANDBY
)
4502 snd_soc_dapm_set_bias_level(&card
->dapm
,
4506 /* Module information */
4507 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4508 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4509 MODULE_LICENSE("GPL");