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
);
437 static struct snd_soc_dapm_widget_list
*dapm_kcontrol_get_wlist(
438 const struct snd_kcontrol
*kcontrol
)
440 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
445 static int dapm_kcontrol_add_widget(struct snd_kcontrol
*kcontrol
,
446 struct snd_soc_dapm_widget
*widget
)
448 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
449 struct snd_soc_dapm_widget_list
*new_wlist
;
453 n
= data
->wlist
->num_widgets
+ 1;
457 new_wlist
= krealloc(data
->wlist
,
458 sizeof(*new_wlist
) + sizeof(widget
) * n
, GFP_KERNEL
);
462 new_wlist
->widgets
[n
- 1] = widget
;
463 new_wlist
->num_widgets
= n
;
465 data
->wlist
= new_wlist
;
470 static void dapm_kcontrol_add_path(const struct snd_kcontrol
*kcontrol
,
471 struct snd_soc_dapm_path
*path
)
473 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
475 list_add_tail(&path
->list_kcontrol
, &data
->paths
);
478 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol
*kcontrol
)
480 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
485 return data
->widget
->power
;
488 static struct list_head
*dapm_kcontrol_get_path_list(
489 const struct snd_kcontrol
*kcontrol
)
491 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
496 #define dapm_kcontrol_for_each_path(path, kcontrol) \
497 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
500 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol
*kcontrol
)
502 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
506 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value
);
508 static bool dapm_kcontrol_set_value(const struct snd_kcontrol
*kcontrol
,
511 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
513 if (data
->value
== value
)
517 data
->widget
->on_val
= value
;
525 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
527 * @kcontrol: The kcontrol
529 struct snd_soc_dapm_widget
*snd_soc_dapm_kcontrol_widget(
530 struct snd_kcontrol
*kcontrol
)
532 return dapm_kcontrol_get_wlist(kcontrol
)->widgets
[0];
534 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget
);
537 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
539 * @kcontrol: The kcontrol
541 * Note: This function must only be used on kcontrols that are known to have
542 * been registered for a CODEC. Otherwise the behaviour is undefined.
544 struct snd_soc_dapm_context
*snd_soc_dapm_kcontrol_dapm(
545 struct snd_kcontrol
*kcontrol
)
547 return dapm_kcontrol_get_wlist(kcontrol
)->widgets
[0]->dapm
;
549 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm
);
551 static void dapm_reset(struct snd_soc_card
*card
)
553 struct snd_soc_dapm_widget
*w
;
555 lockdep_assert_held(&card
->dapm_mutex
);
557 memset(&card
->dapm_stats
, 0, sizeof(card
->dapm_stats
));
559 list_for_each_entry(w
, &card
->widgets
, list
) {
560 w
->new_power
= w
->power
;
561 w
->power_checked
= false;
565 static const char *soc_dapm_prefix(struct snd_soc_dapm_context
*dapm
)
567 if (!dapm
->component
)
569 return dapm
->component
->name_prefix
;
572 static int soc_dapm_read(struct snd_soc_dapm_context
*dapm
, int reg
,
575 if (!dapm
->component
)
577 return snd_soc_component_read(dapm
->component
, reg
, value
);
580 static int soc_dapm_update_bits(struct snd_soc_dapm_context
*dapm
,
581 int reg
, unsigned int mask
, unsigned int value
)
583 if (!dapm
->component
)
585 return snd_soc_component_update_bits(dapm
->component
, reg
,
589 static int soc_dapm_test_bits(struct snd_soc_dapm_context
*dapm
,
590 int reg
, unsigned int mask
, unsigned int value
)
592 if (!dapm
->component
)
594 return snd_soc_component_test_bits(dapm
->component
, reg
, mask
, value
);
597 static void soc_dapm_async_complete(struct snd_soc_dapm_context
*dapm
)
600 snd_soc_component_async_complete(dapm
->component
);
603 static struct snd_soc_dapm_widget
*
604 dapm_wcache_lookup(struct snd_soc_dapm_wcache
*wcache
, const char *name
)
606 struct snd_soc_dapm_widget
*w
= wcache
->widget
;
607 struct list_head
*wlist
;
612 wlist
= &w
->dapm
->card
->widgets
;
614 list_for_each_entry_from(w
, wlist
, list
) {
615 if (!strcmp(name
, w
->name
))
626 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache
*wcache
,
627 struct snd_soc_dapm_widget
*w
)
633 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
634 * @dapm: The DAPM context for which to set the level
635 * @level: The level to set
637 * Forces the DAPM bias level to a specific state. It will call the bias level
638 * callback of DAPM context with the specified level. This will even happen if
639 * the context is already at the same level. Furthermore it will not go through
640 * the normal bias level sequencing, meaning any intermediate states between the
641 * current and the target state will not be entered.
643 * Note that the change in bias level is only temporary and the next time
644 * snd_soc_dapm_sync() is called the state will be set to the level as
645 * determined by the DAPM core. The function is mainly intended to be used to
646 * used during probe or resume from suspend to power up the device so
647 * initialization can be done, before the DAPM core takes over.
649 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context
*dapm
,
650 enum snd_soc_bias_level level
)
654 if (dapm
->set_bias_level
)
655 ret
= dapm
->set_bias_level(dapm
, level
);
658 dapm
->bias_level
= level
;
662 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level
);
665 * snd_soc_dapm_set_bias_level - set the bias level for the system
666 * @dapm: DAPM context
667 * @level: level to configure
669 * Configure the bias (power) levels for the SoC audio device.
671 * Returns 0 for success else error.
673 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context
*dapm
,
674 enum snd_soc_bias_level level
)
676 struct snd_soc_card
*card
= dapm
->card
;
679 trace_snd_soc_bias_level_start(card
, level
);
681 if (card
&& card
->set_bias_level
)
682 ret
= card
->set_bias_level(card
, dapm
, level
);
686 if (!card
|| dapm
!= &card
->dapm
)
687 ret
= snd_soc_dapm_force_bias_level(dapm
, level
);
692 if (card
&& card
->set_bias_level_post
)
693 ret
= card
->set_bias_level_post(card
, dapm
, level
);
695 trace_snd_soc_bias_level_done(card
, level
);
700 /* connect mux widget to its interconnecting audio paths */
701 static int dapm_connect_mux(struct snd_soc_dapm_context
*dapm
,
702 struct snd_soc_dapm_path
*path
, const char *control_name
,
703 struct snd_soc_dapm_widget
*w
)
705 const struct snd_kcontrol_new
*kcontrol
= &w
->kcontrol_news
[0];
706 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
707 unsigned int val
, item
;
710 if (e
->reg
!= SND_SOC_NOPM
) {
711 soc_dapm_read(dapm
, e
->reg
, &val
);
712 val
= (val
>> e
->shift_l
) & e
->mask
;
713 item
= snd_soc_enum_val_to_item(e
, val
);
715 /* since a virtual mux has no backing registers to
716 * decide which path to connect, it will try to match
717 * with the first enumeration. This is to ensure
718 * that the default mux choice (the first) will be
719 * correctly powered up during initialization.
724 for (i
= 0; i
< e
->items
; i
++) {
725 if (!(strcmp(control_name
, e
->texts
[i
]))) {
726 path
->name
= e
->texts
[i
];
738 /* set up initial codec paths */
739 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path
*p
, int i
,
742 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
743 p
->sink
->kcontrol_news
[i
].private_value
;
744 unsigned int reg
= mc
->reg
;
745 unsigned int shift
= mc
->shift
;
746 unsigned int max
= mc
->max
;
747 unsigned int mask
= (1 << fls(max
)) - 1;
748 unsigned int invert
= mc
->invert
;
751 if (reg
!= SND_SOC_NOPM
) {
752 soc_dapm_read(p
->sink
->dapm
, reg
, &val
);
754 * The nth_path argument allows this function to know
755 * which path of a kcontrol it is setting the initial
756 * status for. Ideally this would support any number
757 * of paths and channels. But since kcontrols only come
758 * in mono and stereo variants, we are limited to 2
761 * The following code assumes for stereo controls the
762 * first path is the left channel, and all remaining
763 * paths are the right channel.
765 if (snd_soc_volsw_is_stereo(mc
) && nth_path
> 0) {
767 soc_dapm_read(p
->sink
->dapm
, mc
->rreg
, &val
);
768 val
= (val
>> mc
->rshift
) & mask
;
770 val
= (val
>> shift
) & mask
;
780 /* connect mixer widget to its interconnecting audio paths */
781 static int dapm_connect_mixer(struct snd_soc_dapm_context
*dapm
,
782 struct snd_soc_dapm_path
*path
, const char *control_name
)
786 /* search for mixer kcontrol */
787 for (i
= 0; i
< path
->sink
->num_kcontrols
; i
++) {
788 if (!strcmp(control_name
, path
->sink
->kcontrol_news
[i
].name
)) {
789 path
->name
= path
->sink
->kcontrol_news
[i
].name
;
790 dapm_set_mixer_path_status(path
, i
, nth_path
++);
797 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context
*dapm
,
798 struct snd_soc_dapm_widget
*kcontrolw
,
799 const struct snd_kcontrol_new
*kcontrol_new
,
800 struct snd_kcontrol
**kcontrol
)
802 struct snd_soc_dapm_widget
*w
;
807 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
808 if (w
== kcontrolw
|| w
->dapm
!= kcontrolw
->dapm
)
810 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
811 if (&w
->kcontrol_news
[i
] == kcontrol_new
) {
813 *kcontrol
= w
->kcontrols
[i
];
823 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
824 * create it. Either way, add the widget into the control's widget list
826 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget
*w
,
829 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
830 struct snd_card
*card
= dapm
->card
->snd_card
;
834 struct snd_kcontrol
*kcontrol
;
835 bool wname_in_long_name
, kcname_in_long_name
;
836 char *long_name
= NULL
;
840 prefix
= soc_dapm_prefix(dapm
);
842 prefix_len
= strlen(prefix
) + 1;
846 shared
= dapm_is_shared_kcontrol(dapm
, w
, &w
->kcontrol_news
[kci
],
851 wname_in_long_name
= false;
852 kcname_in_long_name
= true;
855 case snd_soc_dapm_switch
:
856 case snd_soc_dapm_mixer
:
857 case snd_soc_dapm_pga
:
858 case snd_soc_dapm_out_drv
:
859 wname_in_long_name
= true;
860 kcname_in_long_name
= true;
862 case snd_soc_dapm_mixer_named_ctl
:
863 wname_in_long_name
= false;
864 kcname_in_long_name
= true;
866 case snd_soc_dapm_demux
:
867 case snd_soc_dapm_mux
:
868 wname_in_long_name
= true;
869 kcname_in_long_name
= false;
876 if (wname_in_long_name
&& kcname_in_long_name
) {
878 * The control will get a prefix from the control
879 * creation process but we're also using the same
880 * prefix for widgets so cut the prefix off the
881 * front of the widget name.
883 long_name
= kasprintf(GFP_KERNEL
, "%s %s",
884 w
->name
+ prefix_len
,
885 w
->kcontrol_news
[kci
].name
);
886 if (long_name
== NULL
)
890 } else if (wname_in_long_name
) {
892 name
= w
->name
+ prefix_len
;
895 name
= w
->kcontrol_news
[kci
].name
;
898 kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[kci
], NULL
, name
,
905 kcontrol
->private_free
= dapm_kcontrol_free
;
907 ret
= dapm_kcontrol_data_alloc(w
, kcontrol
, name
);
909 snd_ctl_free_one(kcontrol
);
913 ret
= snd_ctl_add(card
, kcontrol
);
916 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
922 ret
= dapm_kcontrol_add_widget(kcontrol
, w
);
924 w
->kcontrols
[kci
] = kcontrol
;
932 /* create new dapm mixer control */
933 static int dapm_new_mixer(struct snd_soc_dapm_widget
*w
)
936 struct snd_soc_dapm_path
*path
;
937 struct dapm_kcontrol_data
*data
;
940 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
942 snd_soc_dapm_widget_for_each_source_path(w
, path
) {
943 /* mixer/mux paths name must match control name */
944 if (path
->name
!= (char *)w
->kcontrol_news
[i
].name
)
947 if (!w
->kcontrols
[i
]) {
948 ret
= dapm_create_or_share_kcontrol(w
, i
);
953 dapm_kcontrol_add_path(w
->kcontrols
[i
], path
);
955 data
= snd_kcontrol_chip(w
->kcontrols
[i
]);
957 snd_soc_dapm_add_path(data
->widget
->dapm
,
967 /* create new dapm mux control */
968 static int dapm_new_mux(struct snd_soc_dapm_widget
*w
)
970 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
971 enum snd_soc_dapm_direction dir
;
972 struct snd_soc_dapm_path
*path
;
977 case snd_soc_dapm_mux
:
978 dir
= SND_SOC_DAPM_DIR_OUT
;
981 case snd_soc_dapm_demux
:
982 dir
= SND_SOC_DAPM_DIR_IN
;
989 if (w
->num_kcontrols
!= 1) {
991 "ASoC: %s %s has incorrect number of controls\n", type
,
996 if (list_empty(&w
->edges
[dir
])) {
997 dev_err(dapm
->dev
, "ASoC: %s %s has no paths\n", type
, w
->name
);
1001 ret
= dapm_create_or_share_kcontrol(w
, 0);
1005 snd_soc_dapm_widget_for_each_path(w
, dir
, path
) {
1007 dapm_kcontrol_add_path(w
->kcontrols
[0], path
);
1013 /* create new dapm volume control */
1014 static int dapm_new_pga(struct snd_soc_dapm_widget
*w
)
1018 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
1019 ret
= dapm_create_or_share_kcontrol(w
, i
);
1027 /* create new dapm dai link control */
1028 static int dapm_new_dai_link(struct snd_soc_dapm_widget
*w
)
1031 struct snd_kcontrol
*kcontrol
;
1032 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
1033 struct snd_card
*card
= dapm
->card
->snd_card
;
1035 /* create control for links with > 1 config */
1036 if (w
->num_params
<= 1)
1040 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
1041 kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[i
], w
,
1043 ret
= snd_ctl_add(card
, kcontrol
);
1046 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1047 w
->name
, w
->kcontrol_news
[i
].name
, ret
);
1050 kcontrol
->private_data
= w
;
1051 w
->kcontrols
[i
] = kcontrol
;
1057 /* We implement power down on suspend by checking the power state of
1058 * the ALSA card - when we are suspending the ALSA state for the card
1061 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
1063 int level
= snd_power_get_state(widget
->dapm
->card
->snd_card
);
1066 case SNDRV_CTL_POWER_D3hot
:
1067 case SNDRV_CTL_POWER_D3cold
:
1068 if (widget
->ignore_suspend
)
1069 dev_dbg(widget
->dapm
->dev
, "ASoC: %s ignoring suspend\n",
1071 return widget
->ignore_suspend
;
1077 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list
**list
,
1078 struct list_head
*widgets
)
1080 struct snd_soc_dapm_widget
*w
;
1081 struct list_head
*it
;
1082 unsigned int size
= 0;
1085 list_for_each(it
, widgets
)
1088 *list
= kzalloc(sizeof(**list
) + size
* sizeof(*w
), GFP_KERNEL
);
1092 list_for_each_entry(w
, widgets
, work_list
)
1093 (*list
)->widgets
[i
++] = w
;
1095 (*list
)->num_widgets
= i
;
1101 * Common implementation for is_connected_output_ep() and
1102 * is_connected_input_ep(). The function is inlined since the combined size of
1103 * the two specialized functions is only marginally larger then the size of the
1104 * generic function and at the same time the fast path of the specialized
1105 * functions is significantly smaller than the generic function.
1107 static __always_inline
int is_connected_ep(struct snd_soc_dapm_widget
*widget
,
1108 struct list_head
*list
, enum snd_soc_dapm_direction dir
,
1109 int (*fn
)(struct snd_soc_dapm_widget
*, struct list_head
*,
1110 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*,
1111 enum snd_soc_dapm_direction
)),
1112 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*,
1113 enum snd_soc_dapm_direction
))
1115 enum snd_soc_dapm_direction rdir
= SND_SOC_DAPM_DIR_REVERSE(dir
);
1116 struct snd_soc_dapm_path
*path
;
1119 if (widget
->endpoints
[dir
] >= 0)
1120 return widget
->endpoints
[dir
];
1122 DAPM_UPDATE_STAT(widget
, path_checks
);
1124 /* do we need to add this widget to the list ? */
1126 list_add_tail(&widget
->work_list
, list
);
1128 if (custom_stop_condition
&& custom_stop_condition(widget
, dir
)) {
1129 widget
->endpoints
[dir
] = 1;
1130 return widget
->endpoints
[dir
];
1133 if ((widget
->is_ep
& SND_SOC_DAPM_DIR_TO_EP(dir
)) && widget
->connected
) {
1134 widget
->endpoints
[dir
] = snd_soc_dapm_suspend_check(widget
);
1135 return widget
->endpoints
[dir
];
1138 snd_soc_dapm_widget_for_each_path(widget
, rdir
, path
) {
1139 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
1141 if (path
->weak
|| path
->is_supply
)
1147 trace_snd_soc_dapm_path(widget
, dir
, path
);
1149 if (path
->connect
) {
1151 con
+= fn(path
->node
[dir
], list
, custom_stop_condition
);
1156 widget
->endpoints
[dir
] = con
;
1162 * Recursively check for a completed path to an active or physically connected
1163 * output widget. Returns number of complete paths.
1165 * Optionally, can be supplied with a function acting as a stopping condition.
1166 * This function takes the dapm widget currently being examined and the walk
1167 * direction as an arguments, it should return true if the walk should be
1168 * stopped and false otherwise.
1170 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
,
1171 struct list_head
*list
,
1172 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*i
,
1173 enum snd_soc_dapm_direction
))
1175 return is_connected_ep(widget
, list
, SND_SOC_DAPM_DIR_OUT
,
1176 is_connected_output_ep
, custom_stop_condition
);
1180 * Recursively check for a completed path to an active or physically connected
1181 * input widget. Returns number of complete paths.
1183 * Optionally, can be supplied with a function acting as a stopping condition.
1184 * This function takes the dapm widget currently being examined and the walk
1185 * direction as an arguments, it should return true if the walk should be
1186 * stopped and false otherwise.
1188 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
,
1189 struct list_head
*list
,
1190 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*i
,
1191 enum snd_soc_dapm_direction
))
1193 return is_connected_ep(widget
, list
, SND_SOC_DAPM_DIR_IN
,
1194 is_connected_input_ep
, custom_stop_condition
);
1198 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1199 * @dai: the soc DAI.
1200 * @stream: stream direction.
1201 * @list: list of active widgets for this stream.
1202 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1203 * walk based on custom logic.
1205 * Queries DAPM graph as to whether a valid audio stream path exists for
1206 * the initial stream specified by name. This takes into account
1207 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1209 * Optionally, can be supplied with a function acting as a stopping condition.
1210 * This function takes the dapm widget currently being examined and the walk
1211 * direction as an arguments, it should return true if the walk should be
1212 * stopped and false otherwise.
1214 * Returns the number of valid paths or negative error.
1216 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai
*dai
, int stream
,
1217 struct snd_soc_dapm_widget_list
**list
,
1218 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*,
1219 enum snd_soc_dapm_direction
))
1221 struct snd_soc_card
*card
= dai
->component
->card
;
1222 struct snd_soc_dapm_widget
*w
;
1227 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
1230 * For is_connected_{output,input}_ep fully discover the graph we need
1231 * to reset the cached number of inputs and outputs.
1233 list_for_each_entry(w
, &card
->widgets
, list
) {
1234 w
->endpoints
[SND_SOC_DAPM_DIR_IN
] = -1;
1235 w
->endpoints
[SND_SOC_DAPM_DIR_OUT
] = -1;
1238 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1239 paths
= is_connected_output_ep(dai
->playback_widget
, &widgets
,
1240 custom_stop_condition
);
1242 paths
= is_connected_input_ep(dai
->capture_widget
, &widgets
,
1243 custom_stop_condition
);
1245 /* Drop starting point */
1246 list_del(widgets
.next
);
1248 ret
= dapm_widget_list_create(list
, &widgets
);
1252 trace_snd_soc_dapm_connected(paths
, stream
);
1253 mutex_unlock(&card
->dapm_mutex
);
1259 * Handler for regulator supply widget.
1261 int dapm_regulator_event(struct snd_soc_dapm_widget
*w
,
1262 struct snd_kcontrol
*kcontrol
, int event
)
1266 soc_dapm_async_complete(w
->dapm
);
1268 if (SND_SOC_DAPM_EVENT_ON(event
)) {
1269 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
1270 ret
= regulator_allow_bypass(w
->regulator
, false);
1272 dev_warn(w
->dapm
->dev
,
1273 "ASoC: Failed to unbypass %s: %d\n",
1277 return regulator_enable(w
->regulator
);
1279 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
1280 ret
= regulator_allow_bypass(w
->regulator
, true);
1282 dev_warn(w
->dapm
->dev
,
1283 "ASoC: Failed to bypass %s: %d\n",
1287 return regulator_disable_deferred(w
->regulator
, w
->shift
);
1290 EXPORT_SYMBOL_GPL(dapm_regulator_event
);
1293 * Handler for clock supply widget.
1295 int dapm_clock_event(struct snd_soc_dapm_widget
*w
,
1296 struct snd_kcontrol
*kcontrol
, int event
)
1301 soc_dapm_async_complete(w
->dapm
);
1303 #ifdef CONFIG_HAVE_CLK
1304 if (SND_SOC_DAPM_EVENT_ON(event
)) {
1305 return clk_prepare_enable(w
->clk
);
1307 clk_disable_unprepare(w
->clk
);
1313 EXPORT_SYMBOL_GPL(dapm_clock_event
);
1315 static int dapm_widget_power_check(struct snd_soc_dapm_widget
*w
)
1317 if (w
->power_checked
)
1318 return w
->new_power
;
1323 w
->new_power
= w
->power_check(w
);
1325 w
->power_checked
= true;
1327 return w
->new_power
;
1330 /* Generic check to see if a widget should be powered. */
1331 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
1335 DAPM_UPDATE_STAT(w
, power_checks
);
1337 in
= is_connected_input_ep(w
, NULL
, NULL
);
1338 out
= is_connected_output_ep(w
, NULL
, NULL
);
1339 return out
!= 0 && in
!= 0;
1342 /* Check to see if a power supply is needed */
1343 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
1345 struct snd_soc_dapm_path
*path
;
1347 DAPM_UPDATE_STAT(w
, power_checks
);
1349 /* Check if one of our outputs is connected */
1350 snd_soc_dapm_widget_for_each_sink_path(w
, path
) {
1351 DAPM_UPDATE_STAT(w
, neighbour_checks
);
1356 if (path
->connected
&&
1357 !path
->connected(path
->source
, path
->sink
))
1360 if (dapm_widget_power_check(path
->sink
))
1367 static int dapm_always_on_check_power(struct snd_soc_dapm_widget
*w
)
1369 return w
->connected
;
1372 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
1373 struct snd_soc_dapm_widget
*b
,
1381 sort
= dapm_down_seq
;
1383 if (sort
[a
->id
] != sort
[b
->id
])
1384 return sort
[a
->id
] - sort
[b
->id
];
1385 if (a
->subseq
!= b
->subseq
) {
1387 return a
->subseq
- b
->subseq
;
1389 return b
->subseq
- a
->subseq
;
1391 if (a
->reg
!= b
->reg
)
1392 return a
->reg
- b
->reg
;
1393 if (a
->dapm
!= b
->dapm
)
1394 return (unsigned long)a
->dapm
- (unsigned long)b
->dapm
;
1399 /* Insert a widget in order into a DAPM power sequence. */
1400 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
1401 struct list_head
*list
,
1404 struct snd_soc_dapm_widget
*w
;
1406 list_for_each_entry(w
, list
, power_list
)
1407 if (dapm_seq_compare(new_widget
, w
, power_up
) < 0) {
1408 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
1412 list_add_tail(&new_widget
->power_list
, list
);
1415 static void dapm_seq_check_event(struct snd_soc_card
*card
,
1416 struct snd_soc_dapm_widget
*w
, int event
)
1418 const char *ev_name
;
1422 case SND_SOC_DAPM_PRE_PMU
:
1423 ev_name
= "PRE_PMU";
1426 case SND_SOC_DAPM_POST_PMU
:
1427 ev_name
= "POST_PMU";
1430 case SND_SOC_DAPM_PRE_PMD
:
1431 ev_name
= "PRE_PMD";
1434 case SND_SOC_DAPM_POST_PMD
:
1435 ev_name
= "POST_PMD";
1438 case SND_SOC_DAPM_WILL_PMU
:
1439 ev_name
= "WILL_PMU";
1442 case SND_SOC_DAPM_WILL_PMD
:
1443 ev_name
= "WILL_PMD";
1447 WARN(1, "Unknown event %d\n", event
);
1451 if (w
->new_power
!= power
)
1454 if (w
->event
&& (w
->event_flags
& event
)) {
1455 pop_dbg(w
->dapm
->dev
, card
->pop_time
, "pop test : %s %s\n",
1457 soc_dapm_async_complete(w
->dapm
);
1458 trace_snd_soc_dapm_widget_event_start(w
, event
);
1459 ret
= w
->event(w
, NULL
, event
);
1460 trace_snd_soc_dapm_widget_event_done(w
, event
);
1462 dev_err(w
->dapm
->dev
, "ASoC: %s: %s event failed: %d\n",
1463 ev_name
, w
->name
, ret
);
1467 /* Apply the coalesced changes from a DAPM sequence */
1468 static void dapm_seq_run_coalesced(struct snd_soc_card
*card
,
1469 struct list_head
*pending
)
1471 struct snd_soc_dapm_context
*dapm
;
1472 struct snd_soc_dapm_widget
*w
;
1474 unsigned int value
= 0;
1475 unsigned int mask
= 0;
1477 w
= list_first_entry(pending
, struct snd_soc_dapm_widget
, power_list
);
1481 list_for_each_entry(w
, pending
, power_list
) {
1482 WARN_ON(reg
!= w
->reg
|| dapm
!= w
->dapm
);
1483 w
->power
= w
->new_power
;
1485 mask
|= w
->mask
<< w
->shift
;
1487 value
|= w
->on_val
<< w
->shift
;
1489 value
|= w
->off_val
<< w
->shift
;
1491 pop_dbg(dapm
->dev
, card
->pop_time
,
1492 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1493 w
->name
, reg
, value
, mask
);
1495 /* Check for events */
1496 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_PRE_PMU
);
1497 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_PRE_PMD
);
1501 /* Any widget will do, they should all be updating the
1505 pop_dbg(dapm
->dev
, card
->pop_time
,
1506 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1507 value
, mask
, reg
, card
->pop_time
);
1508 pop_wait(card
->pop_time
);
1509 soc_dapm_update_bits(dapm
, reg
, mask
, value
);
1512 list_for_each_entry(w
, pending
, power_list
) {
1513 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_POST_PMU
);
1514 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_POST_PMD
);
1518 /* Apply a DAPM power sequence.
1520 * We walk over a pre-sorted list of widgets to apply power to. In
1521 * order to minimise the number of writes to the device required
1522 * multiple widgets will be updated in a single write where possible.
1523 * Currently anything that requires more than a single write is not
1526 static void dapm_seq_run(struct snd_soc_card
*card
,
1527 struct list_head
*list
, int event
, bool power_up
)
1529 struct snd_soc_dapm_widget
*w
, *n
;
1530 struct snd_soc_dapm_context
*d
;
1533 int cur_subseq
= -1;
1534 int cur_reg
= SND_SOC_NOPM
;
1535 struct snd_soc_dapm_context
*cur_dapm
= NULL
;
1542 sort
= dapm_down_seq
;
1544 list_for_each_entry_safe(w
, n
, list
, power_list
) {
1547 /* Do we need to apply any queued changes? */
1548 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
||
1549 w
->dapm
!= cur_dapm
|| w
->subseq
!= cur_subseq
) {
1550 if (!list_empty(&pending
))
1551 dapm_seq_run_coalesced(card
, &pending
);
1553 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1554 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1555 if (sort
[i
] == cur_sort
)
1556 cur_dapm
->seq_notifier(cur_dapm
,
1561 if (cur_dapm
&& w
->dapm
!= cur_dapm
)
1562 soc_dapm_async_complete(cur_dapm
);
1564 INIT_LIST_HEAD(&pending
);
1566 cur_subseq
= INT_MIN
;
1567 cur_reg
= SND_SOC_NOPM
;
1572 case snd_soc_dapm_pre
:
1574 list_for_each_entry_safe_continue(w
, n
, list
,
1577 if (event
== SND_SOC_DAPM_STREAM_START
)
1579 NULL
, SND_SOC_DAPM_PRE_PMU
);
1580 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1582 NULL
, SND_SOC_DAPM_PRE_PMD
);
1585 case snd_soc_dapm_post
:
1587 list_for_each_entry_safe_continue(w
, n
, list
,
1590 if (event
== SND_SOC_DAPM_STREAM_START
)
1592 NULL
, SND_SOC_DAPM_POST_PMU
);
1593 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1595 NULL
, SND_SOC_DAPM_POST_PMD
);
1599 /* Queue it up for application */
1600 cur_sort
= sort
[w
->id
];
1601 cur_subseq
= w
->subseq
;
1604 list_move(&w
->power_list
, &pending
);
1609 dev_err(w
->dapm
->dev
,
1610 "ASoC: Failed to apply widget power: %d\n", ret
);
1613 if (!list_empty(&pending
))
1614 dapm_seq_run_coalesced(card
, &pending
);
1616 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1617 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1618 if (sort
[i
] == cur_sort
)
1619 cur_dapm
->seq_notifier(cur_dapm
,
1623 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1624 soc_dapm_async_complete(d
);
1628 static void dapm_widget_update(struct snd_soc_card
*card
)
1630 struct snd_soc_dapm_update
*update
= card
->update
;
1631 struct snd_soc_dapm_widget_list
*wlist
;
1632 struct snd_soc_dapm_widget
*w
= NULL
;
1636 if (!update
|| !dapm_kcontrol_is_powered(update
->kcontrol
))
1639 wlist
= dapm_kcontrol_get_wlist(update
->kcontrol
);
1641 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
1642 w
= wlist
->widgets
[wi
];
1644 if (w
->event
&& (w
->event_flags
& SND_SOC_DAPM_PRE_REG
)) {
1645 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_PRE_REG
);
1647 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM pre-event failed: %d\n",
1655 ret
= soc_dapm_update_bits(w
->dapm
, update
->reg
, update
->mask
,
1658 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM update failed: %d\n",
1661 if (update
->has_second_set
) {
1662 ret
= soc_dapm_update_bits(w
->dapm
, update
->reg2
,
1663 update
->mask2
, update
->val2
);
1665 dev_err(w
->dapm
->dev
,
1666 "ASoC: %s DAPM update failed: %d\n",
1670 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
1671 w
= wlist
->widgets
[wi
];
1673 if (w
->event
&& (w
->event_flags
& SND_SOC_DAPM_POST_REG
)) {
1674 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_POST_REG
);
1676 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM post-event failed: %d\n",
1682 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1683 * they're changing state.
1685 static void dapm_pre_sequence_async(void *data
, async_cookie_t cookie
)
1687 struct snd_soc_dapm_context
*d
= data
;
1690 /* If we're off and we're not supposed to go into STANDBY */
1691 if (d
->bias_level
== SND_SOC_BIAS_OFF
&&
1692 d
->target_bias_level
!= SND_SOC_BIAS_OFF
) {
1694 pm_runtime_get_sync(d
->dev
);
1696 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1699 "ASoC: Failed to turn on bias: %d\n", ret
);
1702 /* Prepare for a transition to ON or away from ON */
1703 if ((d
->target_bias_level
== SND_SOC_BIAS_ON
&&
1704 d
->bias_level
!= SND_SOC_BIAS_ON
) ||
1705 (d
->target_bias_level
!= SND_SOC_BIAS_ON
&&
1706 d
->bias_level
== SND_SOC_BIAS_ON
)) {
1707 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_PREPARE
);
1710 "ASoC: Failed to prepare bias: %d\n", ret
);
1714 /* Async callback run prior to DAPM sequences - brings to their final
1717 static void dapm_post_sequence_async(void *data
, async_cookie_t cookie
)
1719 struct snd_soc_dapm_context
*d
= data
;
1722 /* If we just powered the last thing off drop to standby bias */
1723 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1724 (d
->target_bias_level
== SND_SOC_BIAS_STANDBY
||
1725 d
->target_bias_level
== SND_SOC_BIAS_OFF
)) {
1726 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1728 dev_err(d
->dev
, "ASoC: Failed to apply standby bias: %d\n",
1732 /* If we're in standby and can support bias off then do that */
1733 if (d
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1734 d
->target_bias_level
== SND_SOC_BIAS_OFF
) {
1735 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_OFF
);
1737 dev_err(d
->dev
, "ASoC: Failed to turn off bias: %d\n",
1741 pm_runtime_put(d
->dev
);
1744 /* If we just powered up then move to active bias */
1745 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1746 d
->target_bias_level
== SND_SOC_BIAS_ON
) {
1747 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_ON
);
1749 dev_err(d
->dev
, "ASoC: Failed to apply active bias: %d\n",
1754 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget
*peer
,
1755 bool power
, bool connect
)
1757 /* If a connection is being made or broken then that update
1758 * will have marked the peer dirty, otherwise the widgets are
1759 * not connected and this update has no impact. */
1763 /* If the peer is already in the state we're moving to then we
1764 * won't have an impact on it. */
1765 if (power
!= peer
->power
)
1766 dapm_mark_dirty(peer
, "peer state change");
1769 static void dapm_widget_set_power(struct snd_soc_dapm_widget
*w
, bool power
,
1770 struct list_head
*up_list
,
1771 struct list_head
*down_list
)
1773 struct snd_soc_dapm_path
*path
;
1775 if (w
->power
== power
)
1778 trace_snd_soc_dapm_widget_power(w
, power
);
1780 /* If we changed our power state perhaps our neigbours changed
1783 snd_soc_dapm_widget_for_each_source_path(w
, path
)
1784 dapm_widget_set_peer_power(path
->source
, power
, path
->connect
);
1786 /* Supplies can't affect their outputs, only their inputs */
1787 if (!w
->is_supply
) {
1788 snd_soc_dapm_widget_for_each_sink_path(w
, path
)
1789 dapm_widget_set_peer_power(path
->sink
, power
,
1794 dapm_seq_insert(w
, up_list
, true);
1796 dapm_seq_insert(w
, down_list
, false);
1799 static void dapm_power_one_widget(struct snd_soc_dapm_widget
*w
,
1800 struct list_head
*up_list
,
1801 struct list_head
*down_list
)
1806 case snd_soc_dapm_pre
:
1807 dapm_seq_insert(w
, down_list
, false);
1809 case snd_soc_dapm_post
:
1810 dapm_seq_insert(w
, up_list
, true);
1814 power
= dapm_widget_power_check(w
);
1816 dapm_widget_set_power(w
, power
, up_list
, down_list
);
1821 static bool dapm_idle_bias_off(struct snd_soc_dapm_context
*dapm
)
1823 if (dapm
->idle_bias_off
)
1826 switch (snd_power_get_state(dapm
->card
->snd_card
)) {
1827 case SNDRV_CTL_POWER_D3hot
:
1828 case SNDRV_CTL_POWER_D3cold
:
1829 return dapm
->suspend_bias_off
;
1838 * Scan each dapm widget for complete audio path.
1839 * A complete path is a route that has valid endpoints i.e.:-
1841 * o DAC to output pin.
1842 * o Input pin to ADC.
1843 * o Input pin to Output pin (bypass, sidetone)
1844 * o DAC to ADC (loopback).
1846 static int dapm_power_widgets(struct snd_soc_card
*card
, int event
)
1848 struct snd_soc_dapm_widget
*w
;
1849 struct snd_soc_dapm_context
*d
;
1851 LIST_HEAD(down_list
);
1852 ASYNC_DOMAIN_EXCLUSIVE(async_domain
);
1853 enum snd_soc_bias_level bias
;
1855 lockdep_assert_held(&card
->dapm_mutex
);
1857 trace_snd_soc_dapm_start(card
);
1859 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1860 if (dapm_idle_bias_off(d
))
1861 d
->target_bias_level
= SND_SOC_BIAS_OFF
;
1863 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1868 /* Check which widgets we need to power and store them in
1869 * lists indicating if they should be powered up or down. We
1870 * only check widgets that have been flagged as dirty but note
1871 * that new widgets may be added to the dirty list while we
1874 list_for_each_entry(w
, &card
->dapm_dirty
, dirty
) {
1875 dapm_power_one_widget(w
, &up_list
, &down_list
);
1878 list_for_each_entry(w
, &card
->widgets
, list
) {
1880 case snd_soc_dapm_pre
:
1881 case snd_soc_dapm_post
:
1882 /* These widgets always need to be powered */
1885 list_del_init(&w
->dirty
);
1892 /* Supplies and micbiases only bring the
1893 * context up to STANDBY as unless something
1894 * else is active and passing audio they
1895 * generally don't require full power. Signal
1896 * generators are virtual pins and have no
1897 * power impact themselves.
1900 case snd_soc_dapm_siggen
:
1901 case snd_soc_dapm_vmid
:
1903 case snd_soc_dapm_supply
:
1904 case snd_soc_dapm_regulator_supply
:
1905 case snd_soc_dapm_clock_supply
:
1906 case snd_soc_dapm_micbias
:
1907 if (d
->target_bias_level
< SND_SOC_BIAS_STANDBY
)
1908 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1911 d
->target_bias_level
= SND_SOC_BIAS_ON
;
1918 /* Force all contexts in the card to the same bias state if
1919 * they're not ground referenced.
1921 bias
= SND_SOC_BIAS_OFF
;
1922 list_for_each_entry(d
, &card
->dapm_list
, list
)
1923 if (d
->target_bias_level
> bias
)
1924 bias
= d
->target_bias_level
;
1925 list_for_each_entry(d
, &card
->dapm_list
, list
)
1926 if (!dapm_idle_bias_off(d
))
1927 d
->target_bias_level
= bias
;
1929 trace_snd_soc_dapm_walk_done(card
);
1931 /* Run card bias changes at first */
1932 dapm_pre_sequence_async(&card
->dapm
, 0);
1933 /* Run other bias changes in parallel */
1934 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1935 if (d
!= &card
->dapm
)
1936 async_schedule_domain(dapm_pre_sequence_async
, d
,
1939 async_synchronize_full_domain(&async_domain
);
1941 list_for_each_entry(w
, &down_list
, power_list
) {
1942 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_WILL_PMD
);
1945 list_for_each_entry(w
, &up_list
, power_list
) {
1946 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_WILL_PMU
);
1949 /* Power down widgets first; try to avoid amplifying pops. */
1950 dapm_seq_run(card
, &down_list
, event
, false);
1952 dapm_widget_update(card
);
1955 dapm_seq_run(card
, &up_list
, event
, true);
1957 /* Run all the bias changes in parallel */
1958 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1959 if (d
!= &card
->dapm
)
1960 async_schedule_domain(dapm_post_sequence_async
, d
,
1963 async_synchronize_full_domain(&async_domain
);
1964 /* Run card bias changes at last */
1965 dapm_post_sequence_async(&card
->dapm
, 0);
1967 /* do we need to notify any clients that DAPM event is complete */
1968 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1969 if (d
->stream_event
)
1970 d
->stream_event(d
, event
);
1973 pop_dbg(card
->dev
, card
->pop_time
,
1974 "DAPM sequencing finished, waiting %dms\n", card
->pop_time
);
1975 pop_wait(card
->pop_time
);
1977 trace_snd_soc_dapm_done(card
);
1982 #ifdef CONFIG_DEBUG_FS
1983 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1984 char __user
*user_buf
,
1985 size_t count
, loff_t
*ppos
)
1987 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1988 struct snd_soc_card
*card
= w
->dapm
->card
;
1989 enum snd_soc_dapm_direction dir
, rdir
;
1993 struct snd_soc_dapm_path
*p
= NULL
;
1995 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1999 mutex_lock(&card
->dapm_mutex
);
2001 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2006 in
= is_connected_input_ep(w
, NULL
, NULL
);
2007 out
= is_connected_output_ep(w
, NULL
, NULL
);
2010 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s%s in %d out %d",
2011 w
->name
, w
->power
? "On" : "Off",
2012 w
->force
? " (forced)" : "", in
, out
);
2015 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
2016 " - R%d(0x%x) mask 0x%x",
2017 w
->reg
, w
->reg
, w
->mask
<< w
->shift
);
2019 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
2022 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
2024 w
->active
? "active" : "inactive");
2026 snd_soc_dapm_for_each_direction(dir
) {
2027 rdir
= SND_SOC_DAPM_DIR_REVERSE(dir
);
2028 snd_soc_dapm_widget_for_each_path(w
, dir
, p
) {
2029 if (p
->connected
&& !p
->connected(w
, p
->node
[rdir
]))
2035 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
2036 " %s \"%s\" \"%s\"\n",
2037 (rdir
== SND_SOC_DAPM_DIR_IN
) ? "in" : "out",
2038 p
->name
? p
->name
: "static",
2039 p
->node
[rdir
]->name
);
2043 mutex_unlock(&card
->dapm_mutex
);
2045 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
2051 static const struct file_operations dapm_widget_power_fops
= {
2052 .open
= simple_open
,
2053 .read
= dapm_widget_power_read_file
,
2054 .llseek
= default_llseek
,
2057 static ssize_t
dapm_bias_read_file(struct file
*file
, char __user
*user_buf
,
2058 size_t count
, loff_t
*ppos
)
2060 struct snd_soc_dapm_context
*dapm
= file
->private_data
;
2063 switch (dapm
->bias_level
) {
2064 case SND_SOC_BIAS_ON
:
2067 case SND_SOC_BIAS_PREPARE
:
2068 level
= "Prepare\n";
2070 case SND_SOC_BIAS_STANDBY
:
2071 level
= "Standby\n";
2073 case SND_SOC_BIAS_OFF
:
2077 WARN(1, "Unknown bias_level %d\n", dapm
->bias_level
);
2078 level
= "Unknown\n";
2082 return simple_read_from_buffer(user_buf
, count
, ppos
, level
,
2086 static const struct file_operations dapm_bias_fops
= {
2087 .open
= simple_open
,
2088 .read
= dapm_bias_read_file
,
2089 .llseek
= default_llseek
,
2092 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
2093 struct dentry
*parent
)
2100 dapm
->debugfs_dapm
= debugfs_create_dir("dapm", parent
);
2102 if (!dapm
->debugfs_dapm
) {
2104 "ASoC: Failed to create DAPM debugfs directory\n");
2108 d
= debugfs_create_file("bias_level", 0444,
2109 dapm
->debugfs_dapm
, dapm
,
2113 "ASoC: Failed to create bias level debugfs file\n");
2116 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
2118 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
2121 if (!dapm
->debugfs_dapm
|| !w
->name
)
2124 d
= debugfs_create_file(w
->name
, 0444,
2125 dapm
->debugfs_dapm
, w
,
2126 &dapm_widget_power_fops
);
2128 dev_warn(w
->dapm
->dev
,
2129 "ASoC: Failed to create %s debugfs file\n",
2133 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
2135 debugfs_remove_recursive(dapm
->debugfs_dapm
);
2139 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
2140 struct dentry
*parent
)
2144 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
2148 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
2155 * soc_dapm_connect_path() - Connects or disconnects a path
2156 * @path: The path to update
2157 * @connect: The new connect state of the path. True if the path is connected,
2158 * false if it is disconnected.
2159 * @reason: The reason why the path changed (for debugging only)
2161 static void soc_dapm_connect_path(struct snd_soc_dapm_path
*path
,
2162 bool connect
, const char *reason
)
2164 if (path
->connect
== connect
)
2167 path
->connect
= connect
;
2168 dapm_mark_dirty(path
->source
, reason
);
2169 dapm_mark_dirty(path
->sink
, reason
);
2170 dapm_path_invalidate(path
);
2173 /* test and update the power status of a mux widget */
2174 static int soc_dapm_mux_update_power(struct snd_soc_card
*card
,
2175 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
)
2177 struct snd_soc_dapm_path
*path
;
2181 lockdep_assert_held(&card
->dapm_mutex
);
2183 /* find dapm widget path assoc with kcontrol */
2184 dapm_kcontrol_for_each_path(path
, kcontrol
) {
2186 /* we now need to match the string in the enum to the path */
2187 if (!(strcmp(path
->name
, e
->texts
[mux
])))
2192 soc_dapm_connect_path(path
, connect
, "mux update");
2196 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2201 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context
*dapm
,
2202 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
,
2203 struct snd_soc_dapm_update
*update
)
2205 struct snd_soc_card
*card
= dapm
->card
;
2208 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2209 card
->update
= update
;
2210 ret
= soc_dapm_mux_update_power(card
, kcontrol
, mux
, e
);
2211 card
->update
= NULL
;
2212 mutex_unlock(&card
->dapm_mutex
);
2214 soc_dpcm_runtime_update(card
);
2217 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power
);
2219 /* test and update the power status of a mixer or switch widget */
2220 static int soc_dapm_mixer_update_power(struct snd_soc_card
*card
,
2221 struct snd_kcontrol
*kcontrol
,
2222 int connect
, int rconnect
)
2224 struct snd_soc_dapm_path
*path
;
2227 lockdep_assert_held(&card
->dapm_mutex
);
2229 /* find dapm widget path assoc with kcontrol */
2230 dapm_kcontrol_for_each_path(path
, kcontrol
) {
2232 * Ideally this function should support any number of
2233 * paths and channels. But since kcontrols only come
2234 * in mono and stereo variants, we are limited to 2
2237 * The following code assumes for stereo controls the
2238 * first path (when 'found == 0') is the left channel,
2239 * and all remaining paths (when 'found == 1') are the
2242 * A stereo control is signified by a valid 'rconnect'
2243 * value, either 0 for unconnected, or >= 0 for connected.
2244 * This is chosen instead of using snd_soc_volsw_is_stereo,
2245 * so that the behavior of snd_soc_dapm_mixer_update_power
2246 * doesn't change even when the kcontrol passed in is
2249 * It passes 'connect' as the path connect status for
2250 * the left channel, and 'rconnect' for the right
2253 if (found
&& rconnect
>= 0)
2254 soc_dapm_connect_path(path
, rconnect
, "mixer update");
2256 soc_dapm_connect_path(path
, connect
, "mixer update");
2261 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2266 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context
*dapm
,
2267 struct snd_kcontrol
*kcontrol
, int connect
,
2268 struct snd_soc_dapm_update
*update
)
2270 struct snd_soc_card
*card
= dapm
->card
;
2273 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2274 card
->update
= update
;
2275 ret
= soc_dapm_mixer_update_power(card
, kcontrol
, connect
, -1);
2276 card
->update
= NULL
;
2277 mutex_unlock(&card
->dapm_mutex
);
2279 soc_dpcm_runtime_update(card
);
2282 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power
);
2284 static ssize_t
dapm_widget_show_component(struct snd_soc_component
*cmpnt
,
2287 struct snd_soc_dapm_context
*dapm
= snd_soc_component_get_dapm(cmpnt
);
2288 struct snd_soc_dapm_widget
*w
;
2290 char *state
= "not set";
2292 /* card won't be set for the dummy component, as a spot fix
2293 * we're checking for that case specifically here but in future
2294 * we will ensure that the dummy component looks like others.
2299 list_for_each_entry(w
, &cmpnt
->card
->widgets
, list
) {
2300 if (w
->dapm
!= dapm
)
2303 /* only display widgets that burn power */
2305 case snd_soc_dapm_hp
:
2306 case snd_soc_dapm_mic
:
2307 case snd_soc_dapm_spk
:
2308 case snd_soc_dapm_line
:
2309 case snd_soc_dapm_micbias
:
2310 case snd_soc_dapm_dac
:
2311 case snd_soc_dapm_adc
:
2312 case snd_soc_dapm_pga
:
2313 case snd_soc_dapm_out_drv
:
2314 case snd_soc_dapm_mixer
:
2315 case snd_soc_dapm_mixer_named_ctl
:
2316 case snd_soc_dapm_supply
:
2317 case snd_soc_dapm_regulator_supply
:
2318 case snd_soc_dapm_clock_supply
:
2320 count
+= sprintf(buf
+ count
, "%s: %s\n",
2321 w
->name
, w
->power
? "On":"Off");
2328 switch (snd_soc_dapm_get_bias_level(dapm
)) {
2329 case SND_SOC_BIAS_ON
:
2332 case SND_SOC_BIAS_PREPARE
:
2335 case SND_SOC_BIAS_STANDBY
:
2338 case SND_SOC_BIAS_OFF
:
2342 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
2347 /* show dapm widget status in sys fs */
2348 static ssize_t
dapm_widget_show(struct device
*dev
,
2349 struct device_attribute
*attr
, char *buf
)
2351 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
2354 mutex_lock(&rtd
->card
->dapm_mutex
);
2356 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
2357 struct snd_soc_component
*cmpnt
= rtd
->codec_dais
[i
]->component
;
2359 count
+= dapm_widget_show_component(cmpnt
, buf
+ count
);
2362 mutex_unlock(&rtd
->card
->dapm_mutex
);
2367 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
2369 struct attribute
*soc_dapm_dev_attrs
[] = {
2370 &dev_attr_dapm_widget
.attr
,
2374 static void dapm_free_path(struct snd_soc_dapm_path
*path
)
2376 list_del(&path
->list_node
[SND_SOC_DAPM_DIR_IN
]);
2377 list_del(&path
->list_node
[SND_SOC_DAPM_DIR_OUT
]);
2378 list_del(&path
->list_kcontrol
);
2379 list_del(&path
->list
);
2383 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget
*w
)
2385 struct snd_soc_dapm_path
*p
, *next_p
;
2386 enum snd_soc_dapm_direction dir
;
2390 * remove source and sink paths associated to this widget.
2391 * While removing the path, remove reference to it from both
2392 * source and sink widgets so that path is removed only once.
2394 snd_soc_dapm_for_each_direction(dir
) {
2395 snd_soc_dapm_widget_for_each_path_safe(w
, dir
, p
, next_p
)
2399 kfree(w
->kcontrols
);
2400 kfree_const(w
->name
);
2404 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context
*dapm
)
2406 dapm
->path_sink_cache
.widget
= NULL
;
2407 dapm
->path_source_cache
.widget
= NULL
;
2410 /* free all dapm widgets and resources */
2411 static void dapm_free_widgets(struct snd_soc_dapm_context
*dapm
)
2413 struct snd_soc_dapm_widget
*w
, *next_w
;
2415 list_for_each_entry_safe(w
, next_w
, &dapm
->card
->widgets
, list
) {
2416 if (w
->dapm
!= dapm
)
2418 snd_soc_dapm_free_widget(w
);
2420 snd_soc_dapm_reset_cache(dapm
);
2423 static struct snd_soc_dapm_widget
*dapm_find_widget(
2424 struct snd_soc_dapm_context
*dapm
, const char *pin
,
2425 bool search_other_contexts
)
2427 struct snd_soc_dapm_widget
*w
;
2428 struct snd_soc_dapm_widget
*fallback
= NULL
;
2430 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
2431 if (!strcmp(w
->name
, pin
)) {
2432 if (w
->dapm
== dapm
)
2439 if (search_other_contexts
)
2445 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context
*dapm
,
2446 const char *pin
, int status
)
2448 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
2450 dapm_assert_locked(dapm
);
2453 dev_err(dapm
->dev
, "ASoC: DAPM unknown pin %s\n", pin
);
2457 if (w
->connected
!= status
) {
2458 dapm_mark_dirty(w
, "pin configuration");
2459 dapm_widget_invalidate_input_paths(w
);
2460 dapm_widget_invalidate_output_paths(w
);
2463 w
->connected
= status
;
2471 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2472 * @dapm: DAPM context
2474 * Walks all dapm audio paths and powers widgets according to their
2475 * stream or path usage.
2477 * Requires external locking.
2479 * Returns 0 for success.
2481 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context
*dapm
)
2484 * Suppress early reports (eg, jacks syncing their state) to avoid
2485 * silly DAPM runs during card startup.
2487 if (!dapm
->card
|| !dapm
->card
->instantiated
)
2490 return dapm_power_widgets(dapm
->card
, SND_SOC_DAPM_STREAM_NOP
);
2492 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked
);
2495 * snd_soc_dapm_sync - scan and power dapm paths
2496 * @dapm: DAPM context
2498 * Walks all dapm audio paths and powers widgets according to their
2499 * stream or path usage.
2501 * Returns 0 for success.
2503 int snd_soc_dapm_sync(struct snd_soc_dapm_context
*dapm
)
2507 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2508 ret
= snd_soc_dapm_sync_unlocked(dapm
);
2509 mutex_unlock(&dapm
->card
->dapm_mutex
);
2512 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
2515 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2516 * @w: The widget for which to update the flags
2518 * Some widgets have a dynamic category which depends on which neighbors they
2519 * are connected to. This function update the category for these widgets.
2521 * This function must be called whenever a path is added or removed to a widget.
2523 static void dapm_update_widget_flags(struct snd_soc_dapm_widget
*w
)
2525 enum snd_soc_dapm_direction dir
;
2526 struct snd_soc_dapm_path
*p
;
2530 case snd_soc_dapm_input
:
2531 /* On a fully routed card an input is never a source */
2532 if (w
->dapm
->card
->fully_routed
)
2534 ep
= SND_SOC_DAPM_EP_SOURCE
;
2535 snd_soc_dapm_widget_for_each_source_path(w
, p
) {
2536 if (p
->source
->id
== snd_soc_dapm_micbias
||
2537 p
->source
->id
== snd_soc_dapm_mic
||
2538 p
->source
->id
== snd_soc_dapm_line
||
2539 p
->source
->id
== snd_soc_dapm_output
) {
2545 case snd_soc_dapm_output
:
2546 /* On a fully routed card a output is never a sink */
2547 if (w
->dapm
->card
->fully_routed
)
2549 ep
= SND_SOC_DAPM_EP_SINK
;
2550 snd_soc_dapm_widget_for_each_sink_path(w
, p
) {
2551 if (p
->sink
->id
== snd_soc_dapm_spk
||
2552 p
->sink
->id
== snd_soc_dapm_hp
||
2553 p
->sink
->id
== snd_soc_dapm_line
||
2554 p
->sink
->id
== snd_soc_dapm_input
) {
2560 case snd_soc_dapm_line
:
2562 snd_soc_dapm_for_each_direction(dir
) {
2563 if (!list_empty(&w
->edges
[dir
]))
2564 ep
|= SND_SOC_DAPM_DIR_TO_EP(dir
);
2574 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context
*dapm
,
2575 struct snd_soc_dapm_widget
*source
, struct snd_soc_dapm_widget
*sink
,
2576 const char *control
)
2578 bool dynamic_source
= false;
2579 bool dynamic_sink
= false;
2584 switch (source
->id
) {
2585 case snd_soc_dapm_demux
:
2586 dynamic_source
= true;
2593 case snd_soc_dapm_mux
:
2594 case snd_soc_dapm_switch
:
2595 case snd_soc_dapm_mixer
:
2596 case snd_soc_dapm_mixer_named_ctl
:
2597 dynamic_sink
= true;
2603 if (dynamic_source
&& dynamic_sink
) {
2605 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2606 source
->name
, control
, sink
->name
);
2608 } else if (!dynamic_source
&& !dynamic_sink
) {
2610 "Control not supported for path %s -> [%s] -> %s\n",
2611 source
->name
, control
, sink
->name
);
2618 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context
*dapm
,
2619 struct snd_soc_dapm_widget
*wsource
, struct snd_soc_dapm_widget
*wsink
,
2620 const char *control
,
2621 int (*connected
)(struct snd_soc_dapm_widget
*source
,
2622 struct snd_soc_dapm_widget
*sink
))
2624 struct snd_soc_dapm_widget
*widgets
[2];
2625 enum snd_soc_dapm_direction dir
;
2626 struct snd_soc_dapm_path
*path
;
2629 if (wsink
->is_supply
&& !wsource
->is_supply
) {
2631 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2632 wsource
->name
, wsink
->name
);
2636 if (connected
&& !wsource
->is_supply
) {
2638 "connected() callback only supported for supply widgets (%s -> %s)\n",
2639 wsource
->name
, wsink
->name
);
2643 if (wsource
->is_supply
&& control
) {
2645 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2646 wsource
->name
, control
, wsink
->name
);
2650 ret
= snd_soc_dapm_check_dynamic_path(dapm
, wsource
, wsink
, control
);
2654 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
2658 path
->node
[SND_SOC_DAPM_DIR_IN
] = wsource
;
2659 path
->node
[SND_SOC_DAPM_DIR_OUT
] = wsink
;
2660 widgets
[SND_SOC_DAPM_DIR_IN
] = wsource
;
2661 widgets
[SND_SOC_DAPM_DIR_OUT
] = wsink
;
2663 path
->connected
= connected
;
2664 INIT_LIST_HEAD(&path
->list
);
2665 INIT_LIST_HEAD(&path
->list_kcontrol
);
2667 if (wsource
->is_supply
|| wsink
->is_supply
)
2668 path
->is_supply
= 1;
2670 /* connect static paths */
2671 if (control
== NULL
) {
2674 switch (wsource
->id
) {
2675 case snd_soc_dapm_demux
:
2676 ret
= dapm_connect_mux(dapm
, path
, control
, wsource
);
2684 switch (wsink
->id
) {
2685 case snd_soc_dapm_mux
:
2686 ret
= dapm_connect_mux(dapm
, path
, control
, wsink
);
2690 case snd_soc_dapm_switch
:
2691 case snd_soc_dapm_mixer
:
2692 case snd_soc_dapm_mixer_named_ctl
:
2693 ret
= dapm_connect_mixer(dapm
, path
, control
);
2702 list_add(&path
->list
, &dapm
->card
->paths
);
2703 snd_soc_dapm_for_each_direction(dir
)
2704 list_add(&path
->list_node
[dir
], &widgets
[dir
]->edges
[dir
]);
2706 snd_soc_dapm_for_each_direction(dir
) {
2707 dapm_update_widget_flags(widgets
[dir
]);
2708 dapm_mark_dirty(widgets
[dir
], "Route added");
2711 if (dapm
->card
->instantiated
&& path
->connect
)
2712 dapm_path_invalidate(path
);
2720 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context
*dapm
,
2721 const struct snd_soc_dapm_route
*route
)
2723 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
2724 struct snd_soc_dapm_widget
*wtsource
= NULL
, *wtsink
= NULL
;
2727 char prefixed_sink
[80];
2728 char prefixed_source
[80];
2732 prefix
= soc_dapm_prefix(dapm
);
2734 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
2735 prefix
, route
->sink
);
2736 sink
= prefixed_sink
;
2737 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
2738 prefix
, route
->source
);
2739 source
= prefixed_source
;
2742 source
= route
->source
;
2745 wsource
= dapm_wcache_lookup(&dapm
->path_source_cache
, source
);
2746 wsink
= dapm_wcache_lookup(&dapm
->path_sink_cache
, sink
);
2748 if (wsink
&& wsource
)
2752 * find src and dest widgets over all widgets but favor a widget from
2753 * current DAPM context
2755 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
2756 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
2758 if (w
->dapm
== dapm
) {
2765 if (!wsource
&& !(strcmp(w
->name
, source
))) {
2767 if (w
->dapm
== dapm
) {
2774 /* use widget from another DAPM context if not found from this */
2780 if (wsource
== NULL
) {
2781 dev_err(dapm
->dev
, "ASoC: no source widget found for %s\n",
2785 if (wsink
== NULL
) {
2786 dev_err(dapm
->dev
, "ASoC: no sink widget found for %s\n",
2792 dapm_wcache_update(&dapm
->path_sink_cache
, wsink
);
2793 dapm_wcache_update(&dapm
->path_source_cache
, wsource
);
2795 ret
= snd_soc_dapm_add_path(dapm
, wsource
, wsink
, route
->control
,
2802 dev_warn(dapm
->dev
, "ASoC: no dapm match for %s --> %s --> %s\n",
2803 source
, route
->control
, sink
);
2807 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context
*dapm
,
2808 const struct snd_soc_dapm_route
*route
)
2810 struct snd_soc_dapm_widget
*wsource
, *wsink
;
2811 struct snd_soc_dapm_path
*path
, *p
;
2814 char prefixed_sink
[80];
2815 char prefixed_source
[80];
2818 if (route
->control
) {
2820 "ASoC: Removal of routes with controls not supported\n");
2824 prefix
= soc_dapm_prefix(dapm
);
2826 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
2827 prefix
, route
->sink
);
2828 sink
= prefixed_sink
;
2829 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
2830 prefix
, route
->source
);
2831 source
= prefixed_source
;
2834 source
= route
->source
;
2838 list_for_each_entry(p
, &dapm
->card
->paths
, list
) {
2839 if (strcmp(p
->source
->name
, source
) != 0)
2841 if (strcmp(p
->sink
->name
, sink
) != 0)
2848 wsource
= path
->source
;
2851 dapm_mark_dirty(wsource
, "Route removed");
2852 dapm_mark_dirty(wsink
, "Route removed");
2854 dapm_path_invalidate(path
);
2856 dapm_free_path(path
);
2858 /* Update any path related flags */
2859 dapm_update_widget_flags(wsource
);
2860 dapm_update_widget_flags(wsink
);
2862 dev_warn(dapm
->dev
, "ASoC: Route %s->%s does not exist\n",
2870 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2871 * @dapm: DAPM context
2872 * @route: audio routes
2873 * @num: number of routes
2875 * Connects 2 dapm widgets together via a named audio path. The sink is
2876 * the widget receiving the audio signal, whilst the source is the sender
2877 * of the audio signal.
2879 * Returns 0 for success else error. On error all resources can be freed
2880 * with a call to snd_soc_card_free().
2882 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context
*dapm
,
2883 const struct snd_soc_dapm_route
*route
, int num
)
2887 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2888 for (i
= 0; i
< num
; i
++) {
2889 r
= snd_soc_dapm_add_route(dapm
, route
);
2891 dev_err(dapm
->dev
, "ASoC: Failed to add route %s -> %s -> %s\n",
2893 route
->control
? route
->control
: "direct",
2899 mutex_unlock(&dapm
->card
->dapm_mutex
);
2903 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
2906 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2907 * @dapm: DAPM context
2908 * @route: audio routes
2909 * @num: number of routes
2911 * Removes routes from the DAPM context.
2913 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context
*dapm
,
2914 const struct snd_soc_dapm_route
*route
, int num
)
2918 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2919 for (i
= 0; i
< num
; i
++) {
2920 snd_soc_dapm_del_route(dapm
, route
);
2923 mutex_unlock(&dapm
->card
->dapm_mutex
);
2927 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes
);
2929 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context
*dapm
,
2930 const struct snd_soc_dapm_route
*route
)
2932 struct snd_soc_dapm_widget
*source
= dapm_find_widget(dapm
,
2935 struct snd_soc_dapm_widget
*sink
= dapm_find_widget(dapm
,
2938 struct snd_soc_dapm_path
*path
;
2942 dev_err(dapm
->dev
, "ASoC: Unable to find source %s for weak route\n",
2948 dev_err(dapm
->dev
, "ASoC: Unable to find sink %s for weak route\n",
2953 if (route
->control
|| route
->connected
)
2954 dev_warn(dapm
->dev
, "ASoC: Ignoring control for weak route %s->%s\n",
2955 route
->source
, route
->sink
);
2957 snd_soc_dapm_widget_for_each_sink_path(source
, path
) {
2958 if (path
->sink
== sink
) {
2965 dev_err(dapm
->dev
, "ASoC: No path found for weak route %s->%s\n",
2966 route
->source
, route
->sink
);
2968 dev_warn(dapm
->dev
, "ASoC: %d paths found for weak route %s->%s\n",
2969 count
, route
->source
, route
->sink
);
2975 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2976 * @dapm: DAPM context
2977 * @route: audio routes
2978 * @num: number of routes
2980 * Mark existing routes matching those specified in the passed array
2981 * as being weak, meaning that they are ignored for the purpose of
2982 * power decisions. The main intended use case is for sidetone paths
2983 * which couple audio between other independent paths if they are both
2984 * active in order to make the combination work better at the user
2985 * level but which aren't intended to be "used".
2987 * Note that CODEC drivers should not use this as sidetone type paths
2988 * can frequently also be used as bypass paths.
2990 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context
*dapm
,
2991 const struct snd_soc_dapm_route
*route
, int num
)
2996 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2997 for (i
= 0; i
< num
; i
++) {
2998 err
= snd_soc_dapm_weak_route(dapm
, route
);
3003 mutex_unlock(&dapm
->card
->dapm_mutex
);
3007 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes
);
3010 * snd_soc_dapm_new_widgets - add new dapm widgets
3011 * @card: card to be checked for new dapm widgets
3013 * Checks the codec for any new dapm widgets and creates them if found.
3015 * Returns 0 for success.
3017 int snd_soc_dapm_new_widgets(struct snd_soc_card
*card
)
3019 struct snd_soc_dapm_widget
*w
;
3022 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
3024 list_for_each_entry(w
, &card
->widgets
, list
)
3029 if (w
->num_kcontrols
) {
3030 w
->kcontrols
= kzalloc(w
->num_kcontrols
*
3031 sizeof(struct snd_kcontrol
*),
3033 if (!w
->kcontrols
) {
3034 mutex_unlock(&card
->dapm_mutex
);
3040 case snd_soc_dapm_switch
:
3041 case snd_soc_dapm_mixer
:
3042 case snd_soc_dapm_mixer_named_ctl
:
3045 case snd_soc_dapm_mux
:
3046 case snd_soc_dapm_demux
:
3049 case snd_soc_dapm_pga
:
3050 case snd_soc_dapm_out_drv
:
3053 case snd_soc_dapm_dai_link
:
3054 dapm_new_dai_link(w
);
3060 /* Read the initial power state from the device */
3062 soc_dapm_read(w
->dapm
, w
->reg
, &val
);
3063 val
= val
>> w
->shift
;
3065 if (val
== w
->on_val
)
3071 dapm_mark_dirty(w
, "new widget");
3072 dapm_debugfs_add_widget(w
);
3075 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
3076 mutex_unlock(&card
->dapm_mutex
);
3079 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
3082 * snd_soc_dapm_get_volsw - dapm mixer get callback
3083 * @kcontrol: mixer control
3084 * @ucontrol: control element information
3086 * Callback to get the value of a dapm mixer control.
3088 * Returns 0 for success.
3090 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
3091 struct snd_ctl_elem_value
*ucontrol
)
3093 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
3094 struct snd_soc_card
*card
= dapm
->card
;
3095 struct soc_mixer_control
*mc
=
3096 (struct soc_mixer_control
*)kcontrol
->private_value
;
3098 unsigned int shift
= mc
->shift
;
3100 unsigned int width
= fls(max
);
3101 unsigned int mask
= (1 << fls(max
)) - 1;
3102 unsigned int invert
= mc
->invert
;
3103 unsigned int reg_val
, val
, rval
= 0;
3106 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3107 if (dapm_kcontrol_is_powered(kcontrol
) && reg
!= SND_SOC_NOPM
) {
3108 ret
= soc_dapm_read(dapm
, reg
, ®_val
);
3109 val
= (reg_val
>> shift
) & mask
;
3111 if (ret
== 0 && reg
!= mc
->rreg
)
3112 ret
= soc_dapm_read(dapm
, mc
->rreg
, ®_val
);
3114 if (snd_soc_volsw_is_stereo(mc
))
3115 rval
= (reg_val
>> mc
->rshift
) & mask
;
3117 reg_val
= dapm_kcontrol_get_value(kcontrol
);
3118 val
= reg_val
& mask
;
3120 if (snd_soc_volsw_is_stereo(mc
))
3121 rval
= (reg_val
>> width
) & mask
;
3123 mutex_unlock(&card
->dapm_mutex
);
3129 ucontrol
->value
.integer
.value
[0] = max
- val
;
3131 ucontrol
->value
.integer
.value
[0] = val
;
3133 if (snd_soc_volsw_is_stereo(mc
)) {
3135 ucontrol
->value
.integer
.value
[1] = max
- rval
;
3137 ucontrol
->value
.integer
.value
[1] = rval
;
3142 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
3145 * snd_soc_dapm_put_volsw - dapm mixer set callback
3146 * @kcontrol: mixer control
3147 * @ucontrol: control element information
3149 * Callback to set the value of a dapm mixer control.
3151 * Returns 0 for success.
3153 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
3154 struct snd_ctl_elem_value
*ucontrol
)
3156 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
3157 struct snd_soc_card
*card
= dapm
->card
;
3158 struct soc_mixer_control
*mc
=
3159 (struct soc_mixer_control
*)kcontrol
->private_value
;
3161 unsigned int shift
= mc
->shift
;
3163 unsigned int width
= fls(max
);
3164 unsigned int mask
= (1 << width
) - 1;
3165 unsigned int invert
= mc
->invert
;
3166 unsigned int val
, rval
= 0;
3167 int connect
, rconnect
= -1, change
, reg_change
= 0;
3168 struct snd_soc_dapm_update update
= { NULL
};
3171 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
3177 if (snd_soc_volsw_is_stereo(mc
)) {
3178 rval
= (ucontrol
->value
.integer
.value
[1] & mask
);
3184 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3186 /* This assumes field width < (bits in unsigned int / 2) */
3187 if (width
> sizeof(unsigned int) * 8 / 2)
3189 "ASoC: control %s field width limit exceeded\n",
3191 change
= dapm_kcontrol_set_value(kcontrol
, val
| (rval
<< width
));
3193 if (reg
!= SND_SOC_NOPM
) {
3195 rval
= rval
<< mc
->rshift
;
3197 reg_change
= soc_dapm_test_bits(dapm
, reg
, mask
<< shift
, val
);
3199 if (snd_soc_volsw_is_stereo(mc
))
3200 reg_change
|= soc_dapm_test_bits(dapm
, mc
->rreg
,
3205 if (change
|| reg_change
) {
3207 if (snd_soc_volsw_is_stereo(mc
)) {
3208 update
.has_second_set
= true;
3209 update
.reg2
= mc
->rreg
;
3210 update
.mask2
= mask
<< mc
->rshift
;
3213 update
.kcontrol
= kcontrol
;
3215 update
.mask
= mask
<< shift
;
3217 card
->update
= &update
;
3219 change
|= reg_change
;
3221 ret
= soc_dapm_mixer_update_power(card
, kcontrol
, connect
,
3224 card
->update
= NULL
;
3227 mutex_unlock(&card
->dapm_mutex
);
3230 soc_dpcm_runtime_update(card
);
3234 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
3237 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3238 * @kcontrol: mixer control
3239 * @ucontrol: control element information
3241 * Callback to get the value of a dapm enumerated double mixer control.
3243 * Returns 0 for success.
3245 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
3246 struct snd_ctl_elem_value
*ucontrol
)
3248 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
3249 struct snd_soc_card
*card
= dapm
->card
;
3250 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
3251 unsigned int reg_val
, val
;
3253 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3254 if (e
->reg
!= SND_SOC_NOPM
&& dapm_kcontrol_is_powered(kcontrol
)) {
3255 int ret
= soc_dapm_read(dapm
, e
->reg
, ®_val
);
3257 mutex_unlock(&card
->dapm_mutex
);
3261 reg_val
= dapm_kcontrol_get_value(kcontrol
);
3263 mutex_unlock(&card
->dapm_mutex
);
3265 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
3266 ucontrol
->value
.enumerated
.item
[0] = snd_soc_enum_val_to_item(e
, val
);
3267 if (e
->shift_l
!= e
->shift_r
) {
3268 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
3269 val
= snd_soc_enum_val_to_item(e
, val
);
3270 ucontrol
->value
.enumerated
.item
[1] = val
;
3275 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
3278 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3279 * @kcontrol: mixer control
3280 * @ucontrol: control element information
3282 * Callback to set the value of a dapm enumerated double mixer control.
3284 * Returns 0 for success.
3286 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
3287 struct snd_ctl_elem_value
*ucontrol
)
3289 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
3290 struct snd_soc_card
*card
= dapm
->card
;
3291 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
3292 unsigned int *item
= ucontrol
->value
.enumerated
.item
;
3293 unsigned int val
, change
, reg_change
= 0;
3295 struct snd_soc_dapm_update update
= { NULL
};
3298 if (item
[0] >= e
->items
)
3301 val
= snd_soc_enum_item_to_val(e
, item
[0]) << e
->shift_l
;
3302 mask
= e
->mask
<< e
->shift_l
;
3303 if (e
->shift_l
!= e
->shift_r
) {
3304 if (item
[1] > e
->items
)
3306 val
|= snd_soc_enum_item_to_val(e
, item
[1]) << e
->shift_r
;
3307 mask
|= e
->mask
<< e
->shift_r
;
3310 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3312 change
= dapm_kcontrol_set_value(kcontrol
, val
);
3314 if (e
->reg
!= SND_SOC_NOPM
)
3315 reg_change
= soc_dapm_test_bits(dapm
, e
->reg
, mask
, val
);
3317 if (change
|| reg_change
) {
3319 update
.kcontrol
= kcontrol
;
3320 update
.reg
= e
->reg
;
3323 card
->update
= &update
;
3325 change
|= reg_change
;
3327 ret
= soc_dapm_mux_update_power(card
, kcontrol
, item
[0], e
);
3329 card
->update
= NULL
;
3332 mutex_unlock(&card
->dapm_mutex
);
3335 soc_dpcm_runtime_update(card
);
3339 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
3342 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3344 * @kcontrol: mixer control
3345 * @uinfo: control element information
3347 * Callback to provide information about a pin switch control.
3349 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
3350 struct snd_ctl_elem_info
*uinfo
)
3352 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
3354 uinfo
->value
.integer
.min
= 0;
3355 uinfo
->value
.integer
.max
= 1;
3359 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
3362 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3364 * @kcontrol: mixer control
3367 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
3368 struct snd_ctl_elem_value
*ucontrol
)
3370 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
3371 const char *pin
= (const char *)kcontrol
->private_value
;
3373 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3375 ucontrol
->value
.integer
.value
[0] =
3376 snd_soc_dapm_get_pin_status(&card
->dapm
, pin
);
3378 mutex_unlock(&card
->dapm_mutex
);
3382 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
3385 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3387 * @kcontrol: mixer control
3390 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
3391 struct snd_ctl_elem_value
*ucontrol
)
3393 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
3394 const char *pin
= (const char *)kcontrol
->private_value
;
3396 if (ucontrol
->value
.integer
.value
[0])
3397 snd_soc_dapm_enable_pin(&card
->dapm
, pin
);
3399 snd_soc_dapm_disable_pin(&card
->dapm
, pin
);
3401 snd_soc_dapm_sync(&card
->dapm
);
3404 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
3406 struct snd_soc_dapm_widget
*
3407 snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
3408 const struct snd_soc_dapm_widget
*widget
)
3410 struct snd_soc_dapm_widget
*w
;
3412 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3413 w
= snd_soc_dapm_new_control_unlocked(dapm
, widget
);
3414 /* Do not nag about probe deferrals */
3416 int ret
= PTR_ERR(w
);
3418 if (ret
!= -EPROBE_DEFER
)
3420 "ASoC: Failed to create DAPM control %s (%d)\n",
3426 "ASoC: Failed to create DAPM control %s\n",
3430 mutex_unlock(&dapm
->card
->dapm_mutex
);
3433 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
3435 struct snd_soc_dapm_widget
*
3436 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context
*dapm
,
3437 const struct snd_soc_dapm_widget
*widget
)
3439 enum snd_soc_dapm_direction dir
;
3440 struct snd_soc_dapm_widget
*w
;
3444 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
3448 case snd_soc_dapm_regulator_supply
:
3449 w
->regulator
= devm_regulator_get(dapm
->dev
, w
->name
);
3450 if (IS_ERR(w
->regulator
)) {
3451 ret
= PTR_ERR(w
->regulator
);
3452 if (ret
== -EPROBE_DEFER
)
3453 return ERR_PTR(ret
);
3454 dev_err(dapm
->dev
, "ASoC: Failed to request %s: %d\n",
3459 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
3460 ret
= regulator_allow_bypass(w
->regulator
, true);
3462 dev_warn(w
->dapm
->dev
,
3463 "ASoC: Failed to bypass %s: %d\n",
3467 case snd_soc_dapm_clock_supply
:
3468 #ifdef CONFIG_CLKDEV_LOOKUP
3469 w
->clk
= devm_clk_get(dapm
->dev
, w
->name
);
3470 if (IS_ERR(w
->clk
)) {
3471 ret
= PTR_ERR(w
->clk
);
3472 if (ret
== -EPROBE_DEFER
)
3473 return ERR_PTR(ret
);
3474 dev_err(dapm
->dev
, "ASoC: Failed to request %s: %d\n",
3486 prefix
= soc_dapm_prefix(dapm
);
3488 w
->name
= kasprintf(GFP_KERNEL
, "%s %s", prefix
, widget
->name
);
3490 w
->name
= kstrdup_const(widget
->name
, GFP_KERNEL
);
3491 if (w
->name
== NULL
) {
3497 case snd_soc_dapm_mic
:
3498 w
->is_ep
= SND_SOC_DAPM_EP_SOURCE
;
3499 w
->power_check
= dapm_generic_check_power
;
3501 case snd_soc_dapm_input
:
3502 if (!dapm
->card
->fully_routed
)
3503 w
->is_ep
= SND_SOC_DAPM_EP_SOURCE
;
3504 w
->power_check
= dapm_generic_check_power
;
3506 case snd_soc_dapm_spk
:
3507 case snd_soc_dapm_hp
:
3508 w
->is_ep
= SND_SOC_DAPM_EP_SINK
;
3509 w
->power_check
= dapm_generic_check_power
;
3511 case snd_soc_dapm_output
:
3512 if (!dapm
->card
->fully_routed
)
3513 w
->is_ep
= SND_SOC_DAPM_EP_SINK
;
3514 w
->power_check
= dapm_generic_check_power
;
3516 case snd_soc_dapm_vmid
:
3517 case snd_soc_dapm_siggen
:
3518 w
->is_ep
= SND_SOC_DAPM_EP_SOURCE
;
3519 w
->power_check
= dapm_always_on_check_power
;
3521 case snd_soc_dapm_sink
:
3522 w
->is_ep
= SND_SOC_DAPM_EP_SINK
;
3523 w
->power_check
= dapm_always_on_check_power
;
3526 case snd_soc_dapm_mux
:
3527 case snd_soc_dapm_demux
:
3528 case snd_soc_dapm_switch
:
3529 case snd_soc_dapm_mixer
:
3530 case snd_soc_dapm_mixer_named_ctl
:
3531 case snd_soc_dapm_adc
:
3532 case snd_soc_dapm_aif_out
:
3533 case snd_soc_dapm_dac
:
3534 case snd_soc_dapm_aif_in
:
3535 case snd_soc_dapm_pga
:
3536 case snd_soc_dapm_out_drv
:
3537 case snd_soc_dapm_micbias
:
3538 case snd_soc_dapm_line
:
3539 case snd_soc_dapm_dai_link
:
3540 case snd_soc_dapm_dai_out
:
3541 case snd_soc_dapm_dai_in
:
3542 w
->power_check
= dapm_generic_check_power
;
3544 case snd_soc_dapm_supply
:
3545 case snd_soc_dapm_regulator_supply
:
3546 case snd_soc_dapm_clock_supply
:
3547 case snd_soc_dapm_kcontrol
:
3549 w
->power_check
= dapm_supply_check_power
;
3552 w
->power_check
= dapm_always_on_check_power
;
3557 INIT_LIST_HEAD(&w
->list
);
3558 INIT_LIST_HEAD(&w
->dirty
);
3559 list_add_tail(&w
->list
, &dapm
->card
->widgets
);
3561 snd_soc_dapm_for_each_direction(dir
) {
3562 INIT_LIST_HEAD(&w
->edges
[dir
]);
3563 w
->endpoints
[dir
] = -1;
3566 /* machine layer sets up unconnected pins and insertions */
3572 * snd_soc_dapm_new_controls - create new dapm controls
3573 * @dapm: DAPM context
3574 * @widget: widget array
3575 * @num: number of widgets
3577 * Creates new DAPM controls based upon the templates.
3579 * Returns 0 for success else error.
3581 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context
*dapm
,
3582 const struct snd_soc_dapm_widget
*widget
,
3585 struct snd_soc_dapm_widget
*w
;
3589 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
3590 for (i
= 0; i
< num
; i
++) {
3591 w
= snd_soc_dapm_new_control_unlocked(dapm
, widget
);
3594 /* Do not nag about probe deferrals */
3595 if (ret
== -EPROBE_DEFER
)
3598 "ASoC: Failed to create DAPM control %s (%d)\n",
3604 "ASoC: Failed to create DAPM control %s\n",
3611 mutex_unlock(&dapm
->card
->dapm_mutex
);
3614 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
3616 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget
*w
,
3617 struct snd_kcontrol
*kcontrol
, int event
)
3619 struct snd_soc_dapm_path
*source_p
, *sink_p
;
3620 struct snd_soc_dai
*source
, *sink
;
3621 const struct snd_soc_pcm_stream
*config
= w
->params
+ w
->params_select
;
3622 struct snd_pcm_substream substream
;
3623 struct snd_pcm_hw_params
*params
= NULL
;
3624 struct snd_pcm_runtime
*runtime
= NULL
;
3628 if (WARN_ON(!config
) ||
3629 WARN_ON(list_empty(&w
->edges
[SND_SOC_DAPM_DIR_OUT
]) ||
3630 list_empty(&w
->edges
[SND_SOC_DAPM_DIR_IN
])))
3633 /* We only support a single source and sink, pick the first */
3634 source_p
= list_first_entry(&w
->edges
[SND_SOC_DAPM_DIR_OUT
],
3635 struct snd_soc_dapm_path
,
3636 list_node
[SND_SOC_DAPM_DIR_OUT
]);
3637 sink_p
= list_first_entry(&w
->edges
[SND_SOC_DAPM_DIR_IN
],
3638 struct snd_soc_dapm_path
,
3639 list_node
[SND_SOC_DAPM_DIR_IN
]);
3641 source
= source_p
->source
->priv
;
3642 sink
= sink_p
->sink
->priv
;
3644 /* Be a little careful as we don't want to overflow the mask array */
3645 if (config
->formats
) {
3646 fmt
= ffs(config
->formats
) - 1;
3648 dev_warn(w
->dapm
->dev
, "ASoC: Invalid format %llx specified\n",
3653 /* Currently very limited parameter selection */
3654 params
= kzalloc(sizeof(*params
), GFP_KERNEL
);
3659 snd_mask_set(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), fmt
);
3661 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
)->min
=
3663 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
)->max
=
3666 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
)->min
3667 = config
->channels_min
;
3668 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
)->max
3669 = config
->channels_max
;
3671 memset(&substream
, 0, sizeof(substream
));
3673 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3674 runtime
= kzalloc(sizeof(*runtime
), GFP_KERNEL
);
3679 substream
.runtime
= runtime
;
3682 case SND_SOC_DAPM_PRE_PMU
:
3683 substream
.stream
= SNDRV_PCM_STREAM_CAPTURE
;
3684 if (source
->driver
->ops
->startup
) {
3685 ret
= source
->driver
->ops
->startup(&substream
, source
);
3687 dev_err(source
->dev
,
3688 "ASoC: startup() failed: %d\n", ret
);
3693 ret
= soc_dai_hw_params(&substream
, params
, source
);
3697 substream
.stream
= SNDRV_PCM_STREAM_PLAYBACK
;
3698 if (sink
->driver
->ops
->startup
) {
3699 ret
= sink
->driver
->ops
->startup(&substream
, sink
);
3702 "ASoC: startup() failed: %d\n", ret
);
3707 ret
= soc_dai_hw_params(&substream
, params
, sink
);
3712 case SND_SOC_DAPM_POST_PMU
:
3713 ret
= snd_soc_dai_digital_mute(sink
, 0,
3714 SNDRV_PCM_STREAM_PLAYBACK
);
3715 if (ret
!= 0 && ret
!= -ENOTSUPP
)
3716 dev_warn(sink
->dev
, "ASoC: Failed to unmute: %d\n", ret
);
3720 case SND_SOC_DAPM_PRE_PMD
:
3721 ret
= snd_soc_dai_digital_mute(sink
, 1,
3722 SNDRV_PCM_STREAM_PLAYBACK
);
3723 if (ret
!= 0 && ret
!= -ENOTSUPP
)
3724 dev_warn(sink
->dev
, "ASoC: Failed to mute: %d\n", ret
);
3728 if (source
->driver
->ops
->shutdown
) {
3729 substream
.stream
= SNDRV_PCM_STREAM_CAPTURE
;
3730 source
->driver
->ops
->shutdown(&substream
, source
);
3734 if (sink
->driver
->ops
->shutdown
) {
3735 substream
.stream
= SNDRV_PCM_STREAM_PLAYBACK
;
3736 sink
->driver
->ops
->shutdown(&substream
, sink
);
3741 WARN(1, "Unknown event %d\n", event
);
3751 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol
*kcontrol
,
3752 struct snd_ctl_elem_value
*ucontrol
)
3754 struct snd_soc_dapm_widget
*w
= snd_kcontrol_chip(kcontrol
);
3756 ucontrol
->value
.enumerated
.item
[0] = w
->params_select
;
3761 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol
*kcontrol
,
3762 struct snd_ctl_elem_value
*ucontrol
)
3764 struct snd_soc_dapm_widget
*w
= snd_kcontrol_chip(kcontrol
);
3766 /* Can't change the config when widget is already powered */
3770 if (ucontrol
->value
.enumerated
.item
[0] == w
->params_select
)
3773 if (ucontrol
->value
.enumerated
.item
[0] >= w
->num_params
)
3776 w
->params_select
= ucontrol
->value
.enumerated
.item
[0];
3782 snd_soc_dapm_free_kcontrol(struct snd_soc_card
*card
,
3783 unsigned long *private_value
,
3785 const char **w_param_text
)
3789 devm_kfree(card
->dev
, (void *)*private_value
);
3790 for (count
= 0 ; count
< num_params
; count
++)
3791 devm_kfree(card
->dev
, (void *)w_param_text
[count
]);
3792 devm_kfree(card
->dev
, w_param_text
);
3795 static struct snd_kcontrol_new
*
3796 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card
*card
,
3798 const struct snd_soc_pcm_stream
*params
,
3799 int num_params
, const char **w_param_text
,
3800 unsigned long *private_value
)
3802 struct soc_enum w_param_enum
[] = {
3803 SOC_ENUM_SINGLE(0, 0, 0, NULL
),
3805 struct snd_kcontrol_new kcontrol_dai_link
[] = {
3806 SOC_ENUM_EXT(NULL
, w_param_enum
[0],
3807 snd_soc_dapm_dai_link_get
,
3808 snd_soc_dapm_dai_link_put
),
3810 struct snd_kcontrol_new
*kcontrol_news
;
3811 const struct snd_soc_pcm_stream
*config
= params
;
3814 for (count
= 0 ; count
< num_params
; count
++) {
3815 if (!config
->stream_name
) {
3816 dev_warn(card
->dapm
.dev
,
3817 "ASoC: anonymous config %d for dai link %s\n",
3819 w_param_text
[count
] =
3820 devm_kasprintf(card
->dev
, GFP_KERNEL
,
3821 "Anonymous Configuration %d",
3824 w_param_text
[count
] = devm_kmemdup(card
->dev
,
3825 config
->stream_name
,
3826 strlen(config
->stream_name
) + 1,
3829 if (!w_param_text
[count
])
3830 goto outfree_w_param
;
3834 w_param_enum
[0].items
= num_params
;
3835 w_param_enum
[0].texts
= w_param_text
;
3838 (unsigned long) devm_kmemdup(card
->dev
,
3839 (void *)(kcontrol_dai_link
[0].private_value
),
3840 sizeof(struct soc_enum
), GFP_KERNEL
);
3841 if (!*private_value
) {
3842 dev_err(card
->dev
, "ASoC: Failed to create control for %s widget\n",
3844 goto outfree_w_param
;
3846 kcontrol_dai_link
[0].private_value
= *private_value
;
3847 /* duplicate kcontrol_dai_link on heap so that memory persists */
3848 kcontrol_news
= devm_kmemdup(card
->dev
, &kcontrol_dai_link
[0],
3849 sizeof(struct snd_kcontrol_new
),
3851 if (!kcontrol_news
) {
3852 dev_err(card
->dev
, "ASoC: Failed to create control for %s widget\n",
3854 goto outfree_w_param
;
3856 return kcontrol_news
;
3859 snd_soc_dapm_free_kcontrol(card
, private_value
, num_params
, w_param_text
);
3863 int snd_soc_dapm_new_pcm(struct snd_soc_card
*card
,
3864 const struct snd_soc_pcm_stream
*params
,
3865 unsigned int num_params
,
3866 struct snd_soc_dapm_widget
*source
,
3867 struct snd_soc_dapm_widget
*sink
)
3869 struct snd_soc_dapm_widget
template;
3870 struct snd_soc_dapm_widget
*w
;
3871 const char **w_param_text
;
3872 unsigned long private_value
;
3876 link_name
= devm_kasprintf(card
->dev
, GFP_KERNEL
, "%s-%s",
3877 source
->name
, sink
->name
);
3881 memset(&template, 0, sizeof(template));
3882 template.reg
= SND_SOC_NOPM
;
3883 template.id
= snd_soc_dapm_dai_link
;
3884 template.name
= link_name
;
3885 template.event
= snd_soc_dai_link_event
;
3886 template.event_flags
= SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMU
|
3887 SND_SOC_DAPM_PRE_PMD
;
3888 template.kcontrol_news
= NULL
;
3890 /* allocate memory for control, only in case of multiple configs */
3891 if (num_params
> 1) {
3892 w_param_text
= devm_kcalloc(card
->dev
, num_params
,
3893 sizeof(char *), GFP_KERNEL
);
3894 if (!w_param_text
) {
3899 template.num_kcontrols
= 1;
3900 template.kcontrol_news
=
3901 snd_soc_dapm_alloc_kcontrol(card
,
3902 link_name
, params
, num_params
,
3903 w_param_text
, &private_value
);
3904 if (!template.kcontrol_news
) {
3909 w_param_text
= NULL
;
3911 dev_dbg(card
->dev
, "ASoC: adding %s widget\n", link_name
);
3913 w
= snd_soc_dapm_new_control_unlocked(&card
->dapm
, &template);
3916 /* Do not nag about probe deferrals */
3917 if (ret
!= -EPROBE_DEFER
)
3919 "ASoC: Failed to create %s widget (%d)\n",
3921 goto outfree_kcontrol_news
;
3924 dev_err(card
->dev
, "ASoC: Failed to create %s widget\n",
3927 goto outfree_kcontrol_news
;
3931 w
->num_params
= num_params
;
3933 ret
= snd_soc_dapm_add_path(&card
->dapm
, source
, w
, NULL
, NULL
);
3936 return snd_soc_dapm_add_path(&card
->dapm
, w
, sink
, NULL
, NULL
);
3939 devm_kfree(card
->dev
, w
);
3940 outfree_kcontrol_news
:
3941 devm_kfree(card
->dev
, (void *)template.kcontrol_news
);
3942 snd_soc_dapm_free_kcontrol(card
, &private_value
, num_params
, w_param_text
);
3944 devm_kfree(card
->dev
, link_name
);
3948 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context
*dapm
,
3949 struct snd_soc_dai
*dai
)
3951 struct snd_soc_dapm_widget
template;
3952 struct snd_soc_dapm_widget
*w
;
3954 WARN_ON(dapm
->dev
!= dai
->dev
);
3956 memset(&template, 0, sizeof(template));
3957 template.reg
= SND_SOC_NOPM
;
3959 if (dai
->driver
->playback
.stream_name
) {
3960 template.id
= snd_soc_dapm_dai_in
;
3961 template.name
= dai
->driver
->playback
.stream_name
;
3962 template.sname
= dai
->driver
->playback
.stream_name
;
3964 dev_dbg(dai
->dev
, "ASoC: adding %s widget\n",
3967 w
= snd_soc_dapm_new_control_unlocked(dapm
, &template);
3969 int ret
= PTR_ERR(w
);
3971 /* Do not nag about probe deferrals */
3972 if (ret
!= -EPROBE_DEFER
)
3974 "ASoC: Failed to create %s widget (%d)\n",
3975 dai
->driver
->playback
.stream_name
, ret
);
3979 dev_err(dapm
->dev
, "ASoC: Failed to create %s widget\n",
3980 dai
->driver
->playback
.stream_name
);
3985 dai
->playback_widget
= w
;
3988 if (dai
->driver
->capture
.stream_name
) {
3989 template.id
= snd_soc_dapm_dai_out
;
3990 template.name
= dai
->driver
->capture
.stream_name
;
3991 template.sname
= dai
->driver
->capture
.stream_name
;
3993 dev_dbg(dai
->dev
, "ASoC: adding %s widget\n",
3996 w
= snd_soc_dapm_new_control_unlocked(dapm
, &template);
3998 int ret
= PTR_ERR(w
);
4000 /* Do not nag about probe deferrals */
4001 if (ret
!= -EPROBE_DEFER
)
4003 "ASoC: Failed to create %s widget (%d)\n",
4004 dai
->driver
->playback
.stream_name
, ret
);
4008 dev_err(dapm
->dev
, "ASoC: Failed to create %s widget\n",
4009 dai
->driver
->capture
.stream_name
);
4014 dai
->capture_widget
= w
;
4020 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card
*card
)
4022 struct snd_soc_dapm_widget
*dai_w
, *w
;
4023 struct snd_soc_dapm_widget
*src
, *sink
;
4024 struct snd_soc_dai
*dai
;
4026 /* For each DAI widget... */
4027 list_for_each_entry(dai_w
, &card
->widgets
, list
) {
4028 switch (dai_w
->id
) {
4029 case snd_soc_dapm_dai_in
:
4030 case snd_soc_dapm_dai_out
:
4038 /* ...find all widgets with the same stream and link them */
4039 list_for_each_entry(w
, &card
->widgets
, list
) {
4040 if (w
->dapm
!= dai_w
->dapm
)
4044 case snd_soc_dapm_dai_in
:
4045 case snd_soc_dapm_dai_out
:
4051 if (!w
->sname
|| !strstr(w
->sname
, dai_w
->sname
))
4054 if (dai_w
->id
== snd_soc_dapm_dai_in
) {
4061 dev_dbg(dai
->dev
, "%s -> %s\n", src
->name
, sink
->name
);
4062 snd_soc_dapm_add_path(w
->dapm
, src
, sink
, NULL
, NULL
);
4069 static void dapm_connect_dai_link_widgets(struct snd_soc_card
*card
,
4070 struct snd_soc_pcm_runtime
*rtd
)
4072 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
4073 struct snd_soc_dapm_widget
*sink
, *source
;
4076 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
4077 struct snd_soc_dai
*codec_dai
= rtd
->codec_dais
[i
];
4079 /* connect BE DAI playback if widgets are valid */
4080 if (codec_dai
->playback_widget
&& cpu_dai
->playback_widget
) {
4081 source
= cpu_dai
->playback_widget
;
4082 sink
= codec_dai
->playback_widget
;
4083 dev_dbg(rtd
->dev
, "connected DAI link %s:%s -> %s:%s\n",
4084 cpu_dai
->component
->name
, source
->name
,
4085 codec_dai
->component
->name
, sink
->name
);
4087 snd_soc_dapm_add_path(&card
->dapm
, source
, sink
,
4091 /* connect BE DAI capture if widgets are valid */
4092 if (codec_dai
->capture_widget
&& cpu_dai
->capture_widget
) {
4093 source
= codec_dai
->capture_widget
;
4094 sink
= cpu_dai
->capture_widget
;
4095 dev_dbg(rtd
->dev
, "connected DAI link %s:%s -> %s:%s\n",
4096 codec_dai
->component
->name
, source
->name
,
4097 cpu_dai
->component
->name
, sink
->name
);
4099 snd_soc_dapm_add_path(&card
->dapm
, source
, sink
,
4105 static void soc_dapm_dai_stream_event(struct snd_soc_dai
*dai
, int stream
,
4108 struct snd_soc_dapm_widget
*w
;
4111 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
4112 w
= dai
->playback_widget
;
4114 w
= dai
->capture_widget
;
4117 dapm_mark_dirty(w
, "stream event");
4119 if (w
->id
== snd_soc_dapm_dai_in
) {
4120 ep
= SND_SOC_DAPM_EP_SOURCE
;
4121 dapm_widget_invalidate_input_paths(w
);
4123 ep
= SND_SOC_DAPM_EP_SINK
;
4124 dapm_widget_invalidate_output_paths(w
);
4128 case SND_SOC_DAPM_STREAM_START
:
4132 case SND_SOC_DAPM_STREAM_STOP
:
4136 case SND_SOC_DAPM_STREAM_SUSPEND
:
4137 case SND_SOC_DAPM_STREAM_RESUME
:
4138 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
4139 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
4145 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card
*card
)
4147 struct snd_soc_pcm_runtime
*rtd
;
4149 /* for each BE DAI link... */
4150 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
4152 * dynamic FE links have no fixed DAI mapping.
4153 * CODEC<->CODEC links have no direct connection.
4155 if (rtd
->dai_link
->dynamic
|| rtd
->dai_link
->params
)
4158 dapm_connect_dai_link_widgets(card
, rtd
);
4162 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
, int stream
,
4167 soc_dapm_dai_stream_event(rtd
->cpu_dai
, stream
, event
);
4168 for (i
= 0; i
< rtd
->num_codecs
; i
++)
4169 soc_dapm_dai_stream_event(rtd
->codec_dais
[i
], stream
, event
);
4171 dapm_power_widgets(rtd
->card
, event
);
4175 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4176 * @rtd: PCM runtime data
4177 * @stream: stream name
4178 * @event: stream event
4180 * Sends a stream event to the dapm core. The core then makes any
4181 * necessary widget power changes.
4183 * Returns 0 for success else error.
4185 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
, int stream
,
4188 struct snd_soc_card
*card
= rtd
->card
;
4190 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4191 soc_dapm_stream_event(rtd
, stream
, event
);
4192 mutex_unlock(&card
->dapm_mutex
);
4196 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4197 * @dapm: DAPM context
4200 * Enables input/output pin and its parents or children widgets iff there is
4201 * a valid audio route and active audio stream.
4203 * Requires external locking.
4205 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4206 * do any widget power switching.
4208 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
4211 return snd_soc_dapm_set_pin(dapm
, pin
, 1);
4213 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked
);
4216 * snd_soc_dapm_enable_pin - enable pin.
4217 * @dapm: DAPM context
4220 * Enables input/output pin and its parents or children widgets iff there is
4221 * a valid audio route and active audio stream.
4223 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4224 * do any widget power switching.
4226 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
4230 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4232 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 1);
4234 mutex_unlock(&dapm
->card
->dapm_mutex
);
4238 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
4241 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4242 * @dapm: DAPM context
4245 * Enables input/output pin regardless of any other state. This is
4246 * intended for use with microphone bias supplies used in microphone
4249 * Requires external locking.
4251 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4252 * do any widget power switching.
4254 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
4257 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
4260 dev_err(dapm
->dev
, "ASoC: unknown pin %s\n", pin
);
4264 dev_dbg(w
->dapm
->dev
, "ASoC: force enable pin %s\n", pin
);
4265 if (!w
->connected
) {
4267 * w->force does not affect the number of input or output paths,
4268 * so we only have to recheck if w->connected is changed
4270 dapm_widget_invalidate_input_paths(w
);
4271 dapm_widget_invalidate_output_paths(w
);
4275 dapm_mark_dirty(w
, "force enable");
4279 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked
);
4282 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4283 * @dapm: DAPM context
4286 * Enables input/output pin regardless of any other state. This is
4287 * intended for use with microphone bias supplies used in microphone
4290 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4291 * do any widget power switching.
4293 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context
*dapm
,
4298 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4300 ret
= snd_soc_dapm_force_enable_pin_unlocked(dapm
, pin
);
4302 mutex_unlock(&dapm
->card
->dapm_mutex
);
4306 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
4309 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4310 * @dapm: DAPM context
4313 * Disables input/output pin and its parents or children widgets.
4315 * Requires external locking.
4317 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4318 * do any widget power switching.
4320 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
4323 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
4325 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked
);
4328 * snd_soc_dapm_disable_pin - disable pin.
4329 * @dapm: DAPM context
4332 * Disables input/output pin and its parents or children widgets.
4334 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4335 * do any widget power switching.
4337 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context
*dapm
,
4342 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4344 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 0);
4346 mutex_unlock(&dapm
->card
->dapm_mutex
);
4350 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
4353 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4354 * @dapm: DAPM context
4357 * Marks the specified pin as being not connected, disabling it along
4358 * any parent or child widgets. At present this is identical to
4359 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4360 * additional things such as disabling controls which only affect
4361 * paths through the pin.
4363 * Requires external locking.
4365 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4366 * do any widget power switching.
4368 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
4371 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
4373 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked
);
4376 * snd_soc_dapm_nc_pin - permanently disable pin.
4377 * @dapm: DAPM context
4380 * Marks the specified pin as being not connected, disabling it along
4381 * any parent or child widgets. At present this is identical to
4382 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4383 * additional things such as disabling controls which only affect
4384 * paths through the pin.
4386 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4387 * do any widget power switching.
4389 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
4393 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4395 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 0);
4397 mutex_unlock(&dapm
->card
->dapm_mutex
);
4401 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
4404 * snd_soc_dapm_get_pin_status - get audio pin status
4405 * @dapm: DAPM context
4406 * @pin: audio signal pin endpoint (or start point)
4408 * Get audio pin status - connected or disconnected.
4410 * Returns 1 for connected otherwise 0.
4412 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context
*dapm
,
4415 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
4418 return w
->connected
;
4422 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
4425 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4426 * @dapm: DAPM context
4427 * @pin: audio signal pin endpoint (or start point)
4429 * Mark the given endpoint or pin as ignoring suspend. When the
4430 * system is disabled a path between two endpoints flagged as ignoring
4431 * suspend will not be disabled. The path must already be enabled via
4432 * normal means at suspend time, it will not be turned on if it was not
4435 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context
*dapm
,
4438 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, false);
4441 dev_err(dapm
->dev
, "ASoC: unknown pin %s\n", pin
);
4445 w
->ignore_suspend
= 1;
4449 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
4452 * snd_soc_dapm_free - free dapm resources
4453 * @dapm: DAPM context
4455 * Free all dapm widgets and resources.
4457 void snd_soc_dapm_free(struct snd_soc_dapm_context
*dapm
)
4459 dapm_debugfs_cleanup(dapm
);
4460 dapm_free_widgets(dapm
);
4461 list_del(&dapm
->list
);
4463 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
4465 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context
*dapm
)
4467 struct snd_soc_card
*card
= dapm
->card
;
4468 struct snd_soc_dapm_widget
*w
;
4469 LIST_HEAD(down_list
);
4472 mutex_lock(&card
->dapm_mutex
);
4474 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
4475 if (w
->dapm
!= dapm
)
4478 dapm_seq_insert(w
, &down_list
, false);
4484 /* If there were no widgets to power down we're already in
4488 if (dapm
->bias_level
== SND_SOC_BIAS_ON
)
4489 snd_soc_dapm_set_bias_level(dapm
,
4490 SND_SOC_BIAS_PREPARE
);
4491 dapm_seq_run(card
, &down_list
, 0, false);
4492 if (dapm
->bias_level
== SND_SOC_BIAS_PREPARE
)
4493 snd_soc_dapm_set_bias_level(dapm
,
4494 SND_SOC_BIAS_STANDBY
);
4497 mutex_unlock(&card
->dapm_mutex
);
4501 * snd_soc_dapm_shutdown - callback for system shutdown
4503 void snd_soc_dapm_shutdown(struct snd_soc_card
*card
)
4505 struct snd_soc_dapm_context
*dapm
;
4507 list_for_each_entry(dapm
, &card
->dapm_list
, list
) {
4508 if (dapm
!= &card
->dapm
) {
4509 soc_dapm_shutdown_dapm(dapm
);
4510 if (dapm
->bias_level
== SND_SOC_BIAS_STANDBY
)
4511 snd_soc_dapm_set_bias_level(dapm
,
4516 soc_dapm_shutdown_dapm(&card
->dapm
);
4517 if (card
->dapm
.bias_level
== SND_SOC_BIAS_STANDBY
)
4518 snd_soc_dapm_set_bias_level(&card
->dapm
,
4522 /* Module information */
4523 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4524 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4525 MODULE_LICENSE("GPL");