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/pinctrl/consumer.h>
39 #include <linux/clk.h>
40 #include <linux/slab.h>
41 #include <sound/core.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/soc.h>
45 #include <sound/initval.h>
47 #include <trace/events/asoc.h>
49 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
51 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
52 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
54 #define snd_soc_dapm_for_each_direction(dir) \
55 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
58 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context
*dapm
,
59 struct snd_soc_dapm_widget
*wsource
, struct snd_soc_dapm_widget
*wsink
,
61 int (*connected
)(struct snd_soc_dapm_widget
*source
,
62 struct snd_soc_dapm_widget
*sink
));
64 struct snd_soc_dapm_widget
*
65 snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
66 const struct snd_soc_dapm_widget
*widget
);
68 struct snd_soc_dapm_widget
*
69 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context
*dapm
,
70 const struct snd_soc_dapm_widget
*widget
);
72 /* dapm power sequences - make this per codec in the future */
73 static int dapm_up_seq
[] = {
74 [snd_soc_dapm_pre
] = 0,
75 [snd_soc_dapm_regulator_supply
] = 1,
76 [snd_soc_dapm_pinctrl
] = 1,
77 [snd_soc_dapm_clock_supply
] = 1,
78 [snd_soc_dapm_supply
] = 2,
79 [snd_soc_dapm_micbias
] = 3,
80 [snd_soc_dapm_dai_link
] = 2,
81 [snd_soc_dapm_dai_in
] = 4,
82 [snd_soc_dapm_dai_out
] = 4,
83 [snd_soc_dapm_aif_in
] = 4,
84 [snd_soc_dapm_aif_out
] = 4,
85 [snd_soc_dapm_mic
] = 5,
86 [snd_soc_dapm_mux
] = 6,
87 [snd_soc_dapm_demux
] = 6,
88 [snd_soc_dapm_dac
] = 7,
89 [snd_soc_dapm_switch
] = 8,
90 [snd_soc_dapm_mixer
] = 8,
91 [snd_soc_dapm_mixer_named_ctl
] = 8,
92 [snd_soc_dapm_pga
] = 9,
93 [snd_soc_dapm_adc
] = 10,
94 [snd_soc_dapm_out_drv
] = 11,
95 [snd_soc_dapm_hp
] = 11,
96 [snd_soc_dapm_spk
] = 11,
97 [snd_soc_dapm_line
] = 11,
98 [snd_soc_dapm_kcontrol
] = 12,
99 [snd_soc_dapm_post
] = 13,
102 static int dapm_down_seq
[] = {
103 [snd_soc_dapm_pre
] = 0,
104 [snd_soc_dapm_kcontrol
] = 1,
105 [snd_soc_dapm_adc
] = 2,
106 [snd_soc_dapm_hp
] = 3,
107 [snd_soc_dapm_spk
] = 3,
108 [snd_soc_dapm_line
] = 3,
109 [snd_soc_dapm_out_drv
] = 3,
110 [snd_soc_dapm_pga
] = 4,
111 [snd_soc_dapm_switch
] = 5,
112 [snd_soc_dapm_mixer_named_ctl
] = 5,
113 [snd_soc_dapm_mixer
] = 5,
114 [snd_soc_dapm_dac
] = 6,
115 [snd_soc_dapm_mic
] = 7,
116 [snd_soc_dapm_micbias
] = 8,
117 [snd_soc_dapm_mux
] = 9,
118 [snd_soc_dapm_demux
] = 9,
119 [snd_soc_dapm_aif_in
] = 10,
120 [snd_soc_dapm_aif_out
] = 10,
121 [snd_soc_dapm_dai_in
] = 10,
122 [snd_soc_dapm_dai_out
] = 10,
123 [snd_soc_dapm_dai_link
] = 11,
124 [snd_soc_dapm_supply
] = 12,
125 [snd_soc_dapm_clock_supply
] = 13,
126 [snd_soc_dapm_pinctrl
] = 13,
127 [snd_soc_dapm_regulator_supply
] = 13,
128 [snd_soc_dapm_post
] = 14,
131 static void dapm_assert_locked(struct snd_soc_dapm_context
*dapm
)
133 if (dapm
->card
&& dapm
->card
->instantiated
)
134 lockdep_assert_held(&dapm
->card
->dapm_mutex
);
137 static void pop_wait(u32 pop_time
)
140 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time
));
143 static void pop_dbg(struct device
*dev
, u32 pop_time
, const char *fmt
, ...)
151 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
156 vsnprintf(buf
, PAGE_SIZE
, fmt
, args
);
157 dev_info(dev
, "%s", buf
);
163 static bool dapm_dirty_widget(struct snd_soc_dapm_widget
*w
)
165 return !list_empty(&w
->dirty
);
168 static void dapm_mark_dirty(struct snd_soc_dapm_widget
*w
, const char *reason
)
170 dapm_assert_locked(w
->dapm
);
172 if (!dapm_dirty_widget(w
)) {
173 dev_vdbg(w
->dapm
->dev
, "Marking %s dirty due to %s\n",
175 list_add_tail(&w
->dirty
, &w
->dapm
->card
->dapm_dirty
);
180 * Common implementation for dapm_widget_invalidate_input_paths() and
181 * dapm_widget_invalidate_output_paths(). The function is inlined since the
182 * combined size of the two specialized functions is only marginally larger then
183 * the size of the generic function and at the same time the fast path of the
184 * specialized functions is significantly smaller than the generic function.
186 static __always_inline
void dapm_widget_invalidate_paths(
187 struct snd_soc_dapm_widget
*w
, enum snd_soc_dapm_direction dir
)
189 enum snd_soc_dapm_direction rdir
= SND_SOC_DAPM_DIR_REVERSE(dir
);
190 struct snd_soc_dapm_widget
*node
;
191 struct snd_soc_dapm_path
*p
;
194 dapm_assert_locked(w
->dapm
);
196 if (w
->endpoints
[dir
] == -1)
199 list_add_tail(&w
->work_list
, &list
);
200 w
->endpoints
[dir
] = -1;
202 list_for_each_entry(w
, &list
, work_list
) {
203 snd_soc_dapm_widget_for_each_path(w
, dir
, p
) {
204 if (p
->is_supply
|| p
->weak
|| !p
->connect
)
206 node
= p
->node
[rdir
];
207 if (node
->endpoints
[dir
] != -1) {
208 node
->endpoints
[dir
] = -1;
209 list_add_tail(&node
->work_list
, &list
);
216 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
218 * @w: The widget for which to invalidate the cached number of input paths
220 * Resets the cached number of inputs for the specified widget and all widgets
221 * that can be reached via outcoming paths from the widget.
223 * This function must be called if the number of output paths for a widget might
224 * have changed. E.g. if the source state of a widget changes or a path is added
225 * or activated with the widget as the sink.
227 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget
*w
)
229 dapm_widget_invalidate_paths(w
, SND_SOC_DAPM_DIR_IN
);
233 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
235 * @w: The widget for which to invalidate the cached number of output paths
237 * Resets the cached number of outputs for the specified widget and all widgets
238 * that can be reached via incoming paths from the widget.
240 * This function must be called if the number of output paths for a widget might
241 * have changed. E.g. if the sink state of a widget changes or a path is added
242 * or activated with the widget as the source.
244 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget
*w
)
246 dapm_widget_invalidate_paths(w
, SND_SOC_DAPM_DIR_OUT
);
250 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
251 * for the widgets connected to a path
252 * @p: The path to invalidate
254 * Resets the cached number of inputs for the sink of the path and the cached
255 * number of outputs for the source of the path.
257 * This function must be called when a path is added, removed or the connected
260 static void dapm_path_invalidate(struct snd_soc_dapm_path
*p
)
263 * Weak paths or supply paths do not influence the number of input or
264 * output paths of their neighbors.
266 if (p
->weak
|| p
->is_supply
)
270 * The number of connected endpoints is the sum of the number of
271 * connected endpoints of all neighbors. If a node with 0 connected
272 * endpoints is either connected or disconnected that sum won't change,
273 * so there is no need to re-check the path.
275 if (p
->source
->endpoints
[SND_SOC_DAPM_DIR_IN
] != 0)
276 dapm_widget_invalidate_input_paths(p
->sink
);
277 if (p
->sink
->endpoints
[SND_SOC_DAPM_DIR_OUT
] != 0)
278 dapm_widget_invalidate_output_paths(p
->source
);
281 void dapm_mark_endpoints_dirty(struct snd_soc_card
*card
)
283 struct snd_soc_dapm_widget
*w
;
285 mutex_lock(&card
->dapm_mutex
);
287 list_for_each_entry(w
, &card
->widgets
, list
) {
289 dapm_mark_dirty(w
, "Rechecking endpoints");
290 if (w
->is_ep
& SND_SOC_DAPM_EP_SINK
)
291 dapm_widget_invalidate_output_paths(w
);
292 if (w
->is_ep
& SND_SOC_DAPM_EP_SOURCE
)
293 dapm_widget_invalidate_input_paths(w
);
297 mutex_unlock(&card
->dapm_mutex
);
299 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty
);
301 /* create a new dapm widget */
302 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
303 const struct snd_soc_dapm_widget
*_widget
)
305 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
308 struct dapm_kcontrol_data
{
310 struct snd_soc_dapm_widget
*widget
;
311 struct list_head paths
;
312 struct snd_soc_dapm_widget_list
*wlist
;
315 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget
*widget
,
316 struct snd_kcontrol
*kcontrol
, const char *ctrl_name
)
318 struct dapm_kcontrol_data
*data
;
319 struct soc_mixer_control
*mc
;
324 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
328 INIT_LIST_HEAD(&data
->paths
);
330 switch (widget
->id
) {
331 case snd_soc_dapm_switch
:
332 case snd_soc_dapm_mixer
:
333 case snd_soc_dapm_mixer_named_ctl
:
334 mc
= (struct soc_mixer_control
*)kcontrol
->private_value
;
336 if (mc
->autodisable
&& snd_soc_volsw_is_stereo(mc
))
337 dev_warn(widget
->dapm
->dev
,
338 "ASoC: Unsupported stereo autodisable control '%s'\n",
341 if (mc
->autodisable
) {
342 struct snd_soc_dapm_widget
template;
344 name
= kasprintf(GFP_KERNEL
, "%s %s", ctrl_name
,
351 memset(&template, 0, sizeof(template));
352 template.reg
= mc
->reg
;
353 template.mask
= (1 << fls(mc
->max
)) - 1;
354 template.shift
= mc
->shift
;
356 template.off_val
= mc
->max
;
358 template.off_val
= 0;
359 template.on_val
= template.off_val
;
360 template.id
= snd_soc_dapm_kcontrol
;
361 template.name
= name
;
363 data
->value
= template.on_val
;
366 snd_soc_dapm_new_control_unlocked(widget
->dapm
,
369 if (IS_ERR(data
->widget
)) {
370 ret
= PTR_ERR(data
->widget
);
379 case snd_soc_dapm_demux
:
380 case snd_soc_dapm_mux
:
381 e
= (struct soc_enum
*)kcontrol
->private_value
;
383 if (e
->autodisable
) {
384 struct snd_soc_dapm_widget
template;
386 name
= kasprintf(GFP_KERNEL
, "%s %s", ctrl_name
,
393 memset(&template, 0, sizeof(template));
394 template.reg
= e
->reg
;
395 template.mask
= e
->mask
<< e
->shift_l
;
396 template.shift
= e
->shift_l
;
397 template.off_val
= snd_soc_enum_item_to_val(e
, 0);
398 template.on_val
= template.off_val
;
399 template.id
= snd_soc_dapm_kcontrol
;
400 template.name
= name
;
402 data
->value
= template.on_val
;
404 data
->widget
= snd_soc_dapm_new_control_unlocked(
405 widget
->dapm
, &template);
407 if (IS_ERR(data
->widget
)) {
408 ret
= PTR_ERR(data
->widget
);
416 snd_soc_dapm_add_path(widget
->dapm
, data
->widget
,
424 kcontrol
->private_data
= data
;
433 static void dapm_kcontrol_free(struct snd_kcontrol
*kctl
)
435 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kctl
);
437 list_del(&data
->paths
);
442 static struct snd_soc_dapm_widget_list
*dapm_kcontrol_get_wlist(
443 const struct snd_kcontrol
*kcontrol
)
445 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
450 static int dapm_kcontrol_add_widget(struct snd_kcontrol
*kcontrol
,
451 struct snd_soc_dapm_widget
*widget
)
453 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
454 struct snd_soc_dapm_widget_list
*new_wlist
;
458 n
= data
->wlist
->num_widgets
+ 1;
462 new_wlist
= krealloc(data
->wlist
,
463 sizeof(*new_wlist
) + sizeof(widget
) * n
, GFP_KERNEL
);
467 new_wlist
->widgets
[n
- 1] = widget
;
468 new_wlist
->num_widgets
= n
;
470 data
->wlist
= new_wlist
;
475 static void dapm_kcontrol_add_path(const struct snd_kcontrol
*kcontrol
,
476 struct snd_soc_dapm_path
*path
)
478 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
480 list_add_tail(&path
->list_kcontrol
, &data
->paths
);
483 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol
*kcontrol
)
485 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
490 return data
->widget
->power
;
493 static struct list_head
*dapm_kcontrol_get_path_list(
494 const struct snd_kcontrol
*kcontrol
)
496 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
501 #define dapm_kcontrol_for_each_path(path, kcontrol) \
502 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
505 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol
*kcontrol
)
507 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
511 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value
);
513 static bool dapm_kcontrol_set_value(const struct snd_kcontrol
*kcontrol
,
516 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
518 if (data
->value
== value
)
522 data
->widget
->on_val
= value
;
530 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
532 * @kcontrol: The kcontrol
534 struct snd_soc_dapm_widget
*snd_soc_dapm_kcontrol_widget(
535 struct snd_kcontrol
*kcontrol
)
537 return dapm_kcontrol_get_wlist(kcontrol
)->widgets
[0];
539 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget
);
542 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
544 * @kcontrol: The kcontrol
546 * Note: This function must only be used on kcontrols that are known to have
547 * been registered for a CODEC. Otherwise the behaviour is undefined.
549 struct snd_soc_dapm_context
*snd_soc_dapm_kcontrol_dapm(
550 struct snd_kcontrol
*kcontrol
)
552 return dapm_kcontrol_get_wlist(kcontrol
)->widgets
[0]->dapm
;
554 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm
);
556 static void dapm_reset(struct snd_soc_card
*card
)
558 struct snd_soc_dapm_widget
*w
;
560 lockdep_assert_held(&card
->dapm_mutex
);
562 memset(&card
->dapm_stats
, 0, sizeof(card
->dapm_stats
));
564 list_for_each_entry(w
, &card
->widgets
, list
) {
565 w
->new_power
= w
->power
;
566 w
->power_checked
= false;
570 static const char *soc_dapm_prefix(struct snd_soc_dapm_context
*dapm
)
572 if (!dapm
->component
)
574 return dapm
->component
->name_prefix
;
577 static int soc_dapm_read(struct snd_soc_dapm_context
*dapm
, int reg
,
580 if (!dapm
->component
)
582 return snd_soc_component_read(dapm
->component
, reg
, value
);
585 static int soc_dapm_update_bits(struct snd_soc_dapm_context
*dapm
,
586 int reg
, unsigned int mask
, unsigned int value
)
588 if (!dapm
->component
)
590 return snd_soc_component_update_bits(dapm
->component
, reg
,
594 static int soc_dapm_test_bits(struct snd_soc_dapm_context
*dapm
,
595 int reg
, unsigned int mask
, unsigned int value
)
597 if (!dapm
->component
)
599 return snd_soc_component_test_bits(dapm
->component
, reg
, mask
, value
);
602 static void soc_dapm_async_complete(struct snd_soc_dapm_context
*dapm
)
605 snd_soc_component_async_complete(dapm
->component
);
608 static struct snd_soc_dapm_widget
*
609 dapm_wcache_lookup(struct snd_soc_dapm_wcache
*wcache
, const char *name
)
611 struct snd_soc_dapm_widget
*w
= wcache
->widget
;
612 struct list_head
*wlist
;
617 wlist
= &w
->dapm
->card
->widgets
;
619 list_for_each_entry_from(w
, wlist
, list
) {
620 if (!strcmp(name
, w
->name
))
631 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache
*wcache
,
632 struct snd_soc_dapm_widget
*w
)
638 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
639 * @dapm: The DAPM context for which to set the level
640 * @level: The level to set
642 * Forces the DAPM bias level to a specific state. It will call the bias level
643 * callback of DAPM context with the specified level. This will even happen if
644 * the context is already at the same level. Furthermore it will not go through
645 * the normal bias level sequencing, meaning any intermediate states between the
646 * current and the target state will not be entered.
648 * Note that the change in bias level is only temporary and the next time
649 * snd_soc_dapm_sync() is called the state will be set to the level as
650 * determined by the DAPM core. The function is mainly intended to be used to
651 * used during probe or resume from suspend to power up the device so
652 * initialization can be done, before the DAPM core takes over.
654 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context
*dapm
,
655 enum snd_soc_bias_level level
)
659 if (dapm
->set_bias_level
)
660 ret
= dapm
->set_bias_level(dapm
, level
);
663 dapm
->bias_level
= level
;
667 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level
);
670 * snd_soc_dapm_set_bias_level - set the bias level for the system
671 * @dapm: DAPM context
672 * @level: level to configure
674 * Configure the bias (power) levels for the SoC audio device.
676 * Returns 0 for success else error.
678 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context
*dapm
,
679 enum snd_soc_bias_level level
)
681 struct snd_soc_card
*card
= dapm
->card
;
684 trace_snd_soc_bias_level_start(card
, level
);
686 if (card
&& card
->set_bias_level
)
687 ret
= card
->set_bias_level(card
, dapm
, level
);
691 if (!card
|| dapm
!= &card
->dapm
)
692 ret
= snd_soc_dapm_force_bias_level(dapm
, level
);
697 if (card
&& card
->set_bias_level_post
)
698 ret
= card
->set_bias_level_post(card
, dapm
, level
);
700 trace_snd_soc_bias_level_done(card
, level
);
705 /* connect mux widget to its interconnecting audio paths */
706 static int dapm_connect_mux(struct snd_soc_dapm_context
*dapm
,
707 struct snd_soc_dapm_path
*path
, const char *control_name
,
708 struct snd_soc_dapm_widget
*w
)
710 const struct snd_kcontrol_new
*kcontrol
= &w
->kcontrol_news
[0];
711 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
712 unsigned int val
, item
;
715 if (e
->reg
!= SND_SOC_NOPM
) {
716 soc_dapm_read(dapm
, e
->reg
, &val
);
717 val
= (val
>> e
->shift_l
) & e
->mask
;
718 item
= snd_soc_enum_val_to_item(e
, val
);
720 /* since a virtual mux has no backing registers to
721 * decide which path to connect, it will try to match
722 * with the first enumeration. This is to ensure
723 * that the default mux choice (the first) will be
724 * correctly powered up during initialization.
729 i
= match_string(e
->texts
, e
->items
, control_name
);
733 path
->name
= e
->texts
[i
];
734 path
->connect
= (i
== item
);
739 /* set up initial codec paths */
740 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path
*p
, int i
,
743 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
744 p
->sink
->kcontrol_news
[i
].private_value
;
745 unsigned int reg
= mc
->reg
;
746 unsigned int shift
= mc
->shift
;
747 unsigned int max
= mc
->max
;
748 unsigned int mask
= (1 << fls(max
)) - 1;
749 unsigned int invert
= mc
->invert
;
752 if (reg
!= SND_SOC_NOPM
) {
753 soc_dapm_read(p
->sink
->dapm
, reg
, &val
);
755 * The nth_path argument allows this function to know
756 * which path of a kcontrol it is setting the initial
757 * status for. Ideally this would support any number
758 * of paths and channels. But since kcontrols only come
759 * in mono and stereo variants, we are limited to 2
762 * The following code assumes for stereo controls the
763 * first path is the left channel, and all remaining
764 * paths are the right channel.
766 if (snd_soc_volsw_is_stereo(mc
) && nth_path
> 0) {
768 soc_dapm_read(p
->sink
->dapm
, mc
->rreg
, &val
);
769 val
= (val
>> mc
->rshift
) & mask
;
771 val
= (val
>> shift
) & mask
;
781 /* connect mixer widget to its interconnecting audio paths */
782 static int dapm_connect_mixer(struct snd_soc_dapm_context
*dapm
,
783 struct snd_soc_dapm_path
*path
, const char *control_name
)
787 /* search for mixer kcontrol */
788 for (i
= 0; i
< path
->sink
->num_kcontrols
; i
++) {
789 if (!strcmp(control_name
, path
->sink
->kcontrol_news
[i
].name
)) {
790 path
->name
= path
->sink
->kcontrol_news
[i
].name
;
791 dapm_set_mixer_path_status(path
, i
, nth_path
++);
798 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context
*dapm
,
799 struct snd_soc_dapm_widget
*kcontrolw
,
800 const struct snd_kcontrol_new
*kcontrol_new
,
801 struct snd_kcontrol
**kcontrol
)
803 struct snd_soc_dapm_widget
*w
;
808 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
809 if (w
== kcontrolw
|| w
->dapm
!= kcontrolw
->dapm
)
811 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
812 if (&w
->kcontrol_news
[i
] == kcontrol_new
) {
814 *kcontrol
= w
->kcontrols
[i
];
824 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
825 * create it. Either way, add the widget into the control's widget list
827 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget
*w
,
830 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
831 struct snd_card
*card
= dapm
->card
->snd_card
;
835 struct snd_kcontrol
*kcontrol
;
836 bool wname_in_long_name
, kcname_in_long_name
;
837 char *long_name
= NULL
;
841 prefix
= soc_dapm_prefix(dapm
);
843 prefix_len
= strlen(prefix
) + 1;
847 shared
= dapm_is_shared_kcontrol(dapm
, w
, &w
->kcontrol_news
[kci
],
852 wname_in_long_name
= false;
853 kcname_in_long_name
= true;
856 case snd_soc_dapm_switch
:
857 case snd_soc_dapm_mixer
:
858 case snd_soc_dapm_pga
:
859 case snd_soc_dapm_out_drv
:
860 wname_in_long_name
= true;
861 kcname_in_long_name
= true;
863 case snd_soc_dapm_mixer_named_ctl
:
864 wname_in_long_name
= false;
865 kcname_in_long_name
= true;
867 case snd_soc_dapm_demux
:
868 case snd_soc_dapm_mux
:
869 wname_in_long_name
= true;
870 kcname_in_long_name
= false;
877 if (wname_in_long_name
&& kcname_in_long_name
) {
879 * The control will get a prefix from the control
880 * creation process but we're also using the same
881 * prefix for widgets so cut the prefix off the
882 * front of the widget name.
884 long_name
= kasprintf(GFP_KERNEL
, "%s %s",
885 w
->name
+ prefix_len
,
886 w
->kcontrol_news
[kci
].name
);
887 if (long_name
== NULL
)
891 } else if (wname_in_long_name
) {
893 name
= w
->name
+ prefix_len
;
896 name
= w
->kcontrol_news
[kci
].name
;
899 kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[kci
], NULL
, name
,
906 kcontrol
->private_free
= dapm_kcontrol_free
;
908 ret
= dapm_kcontrol_data_alloc(w
, kcontrol
, name
);
910 snd_ctl_free_one(kcontrol
);
914 ret
= snd_ctl_add(card
, kcontrol
);
917 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
923 ret
= dapm_kcontrol_add_widget(kcontrol
, w
);
925 w
->kcontrols
[kci
] = kcontrol
;
933 /* create new dapm mixer control */
934 static int dapm_new_mixer(struct snd_soc_dapm_widget
*w
)
937 struct snd_soc_dapm_path
*path
;
938 struct dapm_kcontrol_data
*data
;
941 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
943 snd_soc_dapm_widget_for_each_source_path(w
, path
) {
944 /* mixer/mux paths name must match control name */
945 if (path
->name
!= (char *)w
->kcontrol_news
[i
].name
)
948 if (!w
->kcontrols
[i
]) {
949 ret
= dapm_create_or_share_kcontrol(w
, i
);
954 dapm_kcontrol_add_path(w
->kcontrols
[i
], path
);
956 data
= snd_kcontrol_chip(w
->kcontrols
[i
]);
958 snd_soc_dapm_add_path(data
->widget
->dapm
,
968 /* create new dapm mux control */
969 static int dapm_new_mux(struct snd_soc_dapm_widget
*w
)
971 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
972 enum snd_soc_dapm_direction dir
;
973 struct snd_soc_dapm_path
*path
;
978 case snd_soc_dapm_mux
:
979 dir
= SND_SOC_DAPM_DIR_OUT
;
982 case snd_soc_dapm_demux
:
983 dir
= SND_SOC_DAPM_DIR_IN
;
990 if (w
->num_kcontrols
!= 1) {
992 "ASoC: %s %s has incorrect number of controls\n", type
,
997 if (list_empty(&w
->edges
[dir
])) {
998 dev_err(dapm
->dev
, "ASoC: %s %s has no paths\n", type
, w
->name
);
1002 ret
= dapm_create_or_share_kcontrol(w
, 0);
1006 snd_soc_dapm_widget_for_each_path(w
, dir
, path
) {
1008 dapm_kcontrol_add_path(w
->kcontrols
[0], path
);
1014 /* create new dapm volume control */
1015 static int dapm_new_pga(struct snd_soc_dapm_widget
*w
)
1019 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
1020 ret
= dapm_create_or_share_kcontrol(w
, i
);
1028 /* create new dapm dai link control */
1029 static int dapm_new_dai_link(struct snd_soc_dapm_widget
*w
)
1032 struct snd_kcontrol
*kcontrol
;
1033 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
1034 struct snd_card
*card
= dapm
->card
->snd_card
;
1036 /* create control for links with > 1 config */
1037 if (w
->num_params
<= 1)
1041 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
1042 kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[i
], w
,
1044 ret
= snd_ctl_add(card
, kcontrol
);
1047 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1048 w
->name
, w
->kcontrol_news
[i
].name
, ret
);
1051 kcontrol
->private_data
= w
;
1052 w
->kcontrols
[i
] = kcontrol
;
1058 /* We implement power down on suspend by checking the power state of
1059 * the ALSA card - when we are suspending the ALSA state for the card
1062 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
1064 int level
= snd_power_get_state(widget
->dapm
->card
->snd_card
);
1067 case SNDRV_CTL_POWER_D3hot
:
1068 case SNDRV_CTL_POWER_D3cold
:
1069 if (widget
->ignore_suspend
)
1070 dev_dbg(widget
->dapm
->dev
, "ASoC: %s ignoring suspend\n",
1072 return widget
->ignore_suspend
;
1078 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list
**list
,
1079 struct list_head
*widgets
)
1081 struct snd_soc_dapm_widget
*w
;
1082 struct list_head
*it
;
1083 unsigned int size
= 0;
1086 list_for_each(it
, widgets
)
1089 *list
= kzalloc(struct_size(*list
, widgets
, size
), GFP_KERNEL
);
1093 list_for_each_entry(w
, widgets
, work_list
)
1094 (*list
)->widgets
[i
++] = w
;
1096 (*list
)->num_widgets
= i
;
1102 * Common implementation for is_connected_output_ep() and
1103 * is_connected_input_ep(). The function is inlined since the combined size of
1104 * the two specialized functions is only marginally larger then the size of the
1105 * generic function and at the same time the fast path of the specialized
1106 * functions is significantly smaller than the generic function.
1108 static __always_inline
int is_connected_ep(struct snd_soc_dapm_widget
*widget
,
1109 struct list_head
*list
, enum snd_soc_dapm_direction dir
,
1110 int (*fn
)(struct snd_soc_dapm_widget
*, struct list_head
*,
1111 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*,
1112 enum snd_soc_dapm_direction
)),
1113 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*,
1114 enum snd_soc_dapm_direction
))
1116 enum snd_soc_dapm_direction rdir
= SND_SOC_DAPM_DIR_REVERSE(dir
);
1117 struct snd_soc_dapm_path
*path
;
1120 if (widget
->endpoints
[dir
] >= 0)
1121 return widget
->endpoints
[dir
];
1123 DAPM_UPDATE_STAT(widget
, path_checks
);
1125 /* do we need to add this widget to the list ? */
1127 list_add_tail(&widget
->work_list
, list
);
1129 if (custom_stop_condition
&& custom_stop_condition(widget
, dir
)) {
1130 widget
->endpoints
[dir
] = 1;
1131 return widget
->endpoints
[dir
];
1134 if ((widget
->is_ep
& SND_SOC_DAPM_DIR_TO_EP(dir
)) && widget
->connected
) {
1135 widget
->endpoints
[dir
] = snd_soc_dapm_suspend_check(widget
);
1136 return widget
->endpoints
[dir
];
1139 snd_soc_dapm_widget_for_each_path(widget
, rdir
, path
) {
1140 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
1142 if (path
->weak
|| path
->is_supply
)
1148 trace_snd_soc_dapm_path(widget
, dir
, path
);
1150 if (path
->connect
) {
1152 con
+= fn(path
->node
[dir
], list
, custom_stop_condition
);
1157 widget
->endpoints
[dir
] = con
;
1163 * Recursively check for a completed path to an active or physically connected
1164 * output widget. Returns number of complete paths.
1166 * Optionally, can be supplied with a function acting as a stopping condition.
1167 * This function takes the dapm widget currently being examined and the walk
1168 * direction as an arguments, it should return true if the walk should be
1169 * stopped and false otherwise.
1171 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
,
1172 struct list_head
*list
,
1173 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*i
,
1174 enum snd_soc_dapm_direction
))
1176 return is_connected_ep(widget
, list
, SND_SOC_DAPM_DIR_OUT
,
1177 is_connected_output_ep
, custom_stop_condition
);
1181 * Recursively check for a completed path to an active or physically connected
1182 * input widget. Returns number of complete paths.
1184 * Optionally, can be supplied with a function acting as a stopping condition.
1185 * This function takes the dapm widget currently being examined and the walk
1186 * direction as an arguments, it should return true if the walk should be
1187 * stopped and false otherwise.
1189 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
,
1190 struct list_head
*list
,
1191 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*i
,
1192 enum snd_soc_dapm_direction
))
1194 return is_connected_ep(widget
, list
, SND_SOC_DAPM_DIR_IN
,
1195 is_connected_input_ep
, custom_stop_condition
);
1199 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1200 * @dai: the soc DAI.
1201 * @stream: stream direction.
1202 * @list: list of active widgets for this stream.
1203 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1204 * walk based on custom logic.
1206 * Queries DAPM graph as to whether a valid audio stream path exists for
1207 * the initial stream specified by name. This takes into account
1208 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1210 * Optionally, can be supplied with a function acting as a stopping condition.
1211 * This function takes the dapm widget currently being examined and the walk
1212 * direction as an arguments, it should return true if the walk should be
1213 * stopped and false otherwise.
1215 * Returns the number of valid paths or negative error.
1217 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai
*dai
, int stream
,
1218 struct snd_soc_dapm_widget_list
**list
,
1219 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*,
1220 enum snd_soc_dapm_direction
))
1222 struct snd_soc_card
*card
= dai
->component
->card
;
1223 struct snd_soc_dapm_widget
*w
;
1228 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
1231 * For is_connected_{output,input}_ep fully discover the graph we need
1232 * to reset the cached number of inputs and outputs.
1234 list_for_each_entry(w
, &card
->widgets
, list
) {
1235 w
->endpoints
[SND_SOC_DAPM_DIR_IN
] = -1;
1236 w
->endpoints
[SND_SOC_DAPM_DIR_OUT
] = -1;
1239 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1240 paths
= is_connected_output_ep(dai
->playback_widget
, &widgets
,
1241 custom_stop_condition
);
1243 paths
= is_connected_input_ep(dai
->capture_widget
, &widgets
,
1244 custom_stop_condition
);
1246 /* Drop starting point */
1247 list_del(widgets
.next
);
1249 ret
= dapm_widget_list_create(list
, &widgets
);
1253 trace_snd_soc_dapm_connected(paths
, stream
);
1254 mutex_unlock(&card
->dapm_mutex
);
1260 * Handler for regulator supply widget.
1262 int dapm_regulator_event(struct snd_soc_dapm_widget
*w
,
1263 struct snd_kcontrol
*kcontrol
, int event
)
1267 soc_dapm_async_complete(w
->dapm
);
1269 if (SND_SOC_DAPM_EVENT_ON(event
)) {
1270 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
1271 ret
= regulator_allow_bypass(w
->regulator
, false);
1273 dev_warn(w
->dapm
->dev
,
1274 "ASoC: Failed to unbypass %s: %d\n",
1278 return regulator_enable(w
->regulator
);
1280 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
1281 ret
= regulator_allow_bypass(w
->regulator
, true);
1283 dev_warn(w
->dapm
->dev
,
1284 "ASoC: Failed to bypass %s: %d\n",
1288 return regulator_disable_deferred(w
->regulator
, w
->shift
);
1291 EXPORT_SYMBOL_GPL(dapm_regulator_event
);
1294 * Handler for pinctrl widget.
1296 int dapm_pinctrl_event(struct snd_soc_dapm_widget
*w
,
1297 struct snd_kcontrol
*kcontrol
, int event
)
1299 struct snd_soc_dapm_pinctrl_priv
*priv
= w
->priv
;
1300 struct pinctrl
*p
= w
->pinctrl
;
1301 struct pinctrl_state
*s
;
1306 if (SND_SOC_DAPM_EVENT_ON(event
))
1307 s
= pinctrl_lookup_state(p
, priv
->active_state
);
1309 s
= pinctrl_lookup_state(p
, priv
->sleep_state
);
1314 return pinctrl_select_state(p
, s
);
1316 EXPORT_SYMBOL_GPL(dapm_pinctrl_event
);
1319 * Handler for clock supply widget.
1321 int dapm_clock_event(struct snd_soc_dapm_widget
*w
,
1322 struct snd_kcontrol
*kcontrol
, int event
)
1327 soc_dapm_async_complete(w
->dapm
);
1329 #ifdef CONFIG_HAVE_CLK
1330 if (SND_SOC_DAPM_EVENT_ON(event
)) {
1331 return clk_prepare_enable(w
->clk
);
1333 clk_disable_unprepare(w
->clk
);
1339 EXPORT_SYMBOL_GPL(dapm_clock_event
);
1341 static int dapm_widget_power_check(struct snd_soc_dapm_widget
*w
)
1343 if (w
->power_checked
)
1344 return w
->new_power
;
1349 w
->new_power
= w
->power_check(w
);
1351 w
->power_checked
= true;
1353 return w
->new_power
;
1356 /* Generic check to see if a widget should be powered. */
1357 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
1361 DAPM_UPDATE_STAT(w
, power_checks
);
1363 in
= is_connected_input_ep(w
, NULL
, NULL
);
1364 out
= is_connected_output_ep(w
, NULL
, NULL
);
1365 return out
!= 0 && in
!= 0;
1368 /* Check to see if a power supply is needed */
1369 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
1371 struct snd_soc_dapm_path
*path
;
1373 DAPM_UPDATE_STAT(w
, power_checks
);
1375 /* Check if one of our outputs is connected */
1376 snd_soc_dapm_widget_for_each_sink_path(w
, path
) {
1377 DAPM_UPDATE_STAT(w
, neighbour_checks
);
1382 if (path
->connected
&&
1383 !path
->connected(path
->source
, path
->sink
))
1386 if (dapm_widget_power_check(path
->sink
))
1393 static int dapm_always_on_check_power(struct snd_soc_dapm_widget
*w
)
1395 return w
->connected
;
1398 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
1399 struct snd_soc_dapm_widget
*b
,
1407 sort
= dapm_down_seq
;
1409 if (sort
[a
->id
] != sort
[b
->id
])
1410 return sort
[a
->id
] - sort
[b
->id
];
1411 if (a
->subseq
!= b
->subseq
) {
1413 return a
->subseq
- b
->subseq
;
1415 return b
->subseq
- a
->subseq
;
1417 if (a
->reg
!= b
->reg
)
1418 return a
->reg
- b
->reg
;
1419 if (a
->dapm
!= b
->dapm
)
1420 return (unsigned long)a
->dapm
- (unsigned long)b
->dapm
;
1425 /* Insert a widget in order into a DAPM power sequence. */
1426 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
1427 struct list_head
*list
,
1430 struct snd_soc_dapm_widget
*w
;
1432 list_for_each_entry(w
, list
, power_list
)
1433 if (dapm_seq_compare(new_widget
, w
, power_up
) < 0) {
1434 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
1438 list_add_tail(&new_widget
->power_list
, list
);
1441 static void dapm_seq_check_event(struct snd_soc_card
*card
,
1442 struct snd_soc_dapm_widget
*w
, int event
)
1444 const char *ev_name
;
1448 case SND_SOC_DAPM_PRE_PMU
:
1449 ev_name
= "PRE_PMU";
1452 case SND_SOC_DAPM_POST_PMU
:
1453 ev_name
= "POST_PMU";
1456 case SND_SOC_DAPM_PRE_PMD
:
1457 ev_name
= "PRE_PMD";
1460 case SND_SOC_DAPM_POST_PMD
:
1461 ev_name
= "POST_PMD";
1464 case SND_SOC_DAPM_WILL_PMU
:
1465 ev_name
= "WILL_PMU";
1468 case SND_SOC_DAPM_WILL_PMD
:
1469 ev_name
= "WILL_PMD";
1473 WARN(1, "Unknown event %d\n", event
);
1477 if (w
->new_power
!= power
)
1480 if (w
->event
&& (w
->event_flags
& event
)) {
1481 pop_dbg(w
->dapm
->dev
, card
->pop_time
, "pop test : %s %s\n",
1483 soc_dapm_async_complete(w
->dapm
);
1484 trace_snd_soc_dapm_widget_event_start(w
, event
);
1485 ret
= w
->event(w
, NULL
, event
);
1486 trace_snd_soc_dapm_widget_event_done(w
, event
);
1488 dev_err(w
->dapm
->dev
, "ASoC: %s: %s event failed: %d\n",
1489 ev_name
, w
->name
, ret
);
1493 /* Apply the coalesced changes from a DAPM sequence */
1494 static void dapm_seq_run_coalesced(struct snd_soc_card
*card
,
1495 struct list_head
*pending
)
1497 struct snd_soc_dapm_context
*dapm
;
1498 struct snd_soc_dapm_widget
*w
;
1500 unsigned int value
= 0;
1501 unsigned int mask
= 0;
1503 w
= list_first_entry(pending
, struct snd_soc_dapm_widget
, power_list
);
1507 list_for_each_entry(w
, pending
, power_list
) {
1508 WARN_ON(reg
!= w
->reg
|| dapm
!= w
->dapm
);
1509 w
->power
= w
->new_power
;
1511 mask
|= w
->mask
<< w
->shift
;
1513 value
|= w
->on_val
<< w
->shift
;
1515 value
|= w
->off_val
<< w
->shift
;
1517 pop_dbg(dapm
->dev
, card
->pop_time
,
1518 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1519 w
->name
, reg
, value
, mask
);
1521 /* Check for events */
1522 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_PRE_PMU
);
1523 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_PRE_PMD
);
1527 /* Any widget will do, they should all be updating the
1531 pop_dbg(dapm
->dev
, card
->pop_time
,
1532 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1533 value
, mask
, reg
, card
->pop_time
);
1534 pop_wait(card
->pop_time
);
1535 soc_dapm_update_bits(dapm
, reg
, mask
, value
);
1538 list_for_each_entry(w
, pending
, power_list
) {
1539 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_POST_PMU
);
1540 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_POST_PMD
);
1544 /* Apply a DAPM power sequence.
1546 * We walk over a pre-sorted list of widgets to apply power to. In
1547 * order to minimise the number of writes to the device required
1548 * multiple widgets will be updated in a single write where possible.
1549 * Currently anything that requires more than a single write is not
1552 static void dapm_seq_run(struct snd_soc_card
*card
,
1553 struct list_head
*list
, int event
, bool power_up
)
1555 struct snd_soc_dapm_widget
*w
, *n
;
1556 struct snd_soc_dapm_context
*d
;
1559 int cur_subseq
= -1;
1560 int cur_reg
= SND_SOC_NOPM
;
1561 struct snd_soc_dapm_context
*cur_dapm
= NULL
;
1568 sort
= dapm_down_seq
;
1570 list_for_each_entry_safe(w
, n
, list
, power_list
) {
1573 /* Do we need to apply any queued changes? */
1574 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
||
1575 w
->dapm
!= cur_dapm
|| w
->subseq
!= cur_subseq
) {
1576 if (!list_empty(&pending
))
1577 dapm_seq_run_coalesced(card
, &pending
);
1579 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1580 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1581 if (sort
[i
] == cur_sort
)
1582 cur_dapm
->seq_notifier(cur_dapm
,
1587 if (cur_dapm
&& w
->dapm
!= cur_dapm
)
1588 soc_dapm_async_complete(cur_dapm
);
1590 INIT_LIST_HEAD(&pending
);
1592 cur_subseq
= INT_MIN
;
1593 cur_reg
= SND_SOC_NOPM
;
1598 case snd_soc_dapm_pre
:
1600 list_for_each_entry_safe_continue(w
, n
, list
,
1603 if (event
== SND_SOC_DAPM_STREAM_START
)
1605 NULL
, SND_SOC_DAPM_PRE_PMU
);
1606 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1608 NULL
, SND_SOC_DAPM_PRE_PMD
);
1611 case snd_soc_dapm_post
:
1613 list_for_each_entry_safe_continue(w
, n
, list
,
1616 if (event
== SND_SOC_DAPM_STREAM_START
)
1618 NULL
, SND_SOC_DAPM_POST_PMU
);
1619 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1621 NULL
, SND_SOC_DAPM_POST_PMD
);
1625 /* Queue it up for application */
1626 cur_sort
= sort
[w
->id
];
1627 cur_subseq
= w
->subseq
;
1630 list_move(&w
->power_list
, &pending
);
1635 dev_err(w
->dapm
->dev
,
1636 "ASoC: Failed to apply widget power: %d\n", ret
);
1639 if (!list_empty(&pending
))
1640 dapm_seq_run_coalesced(card
, &pending
);
1642 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1643 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1644 if (sort
[i
] == cur_sort
)
1645 cur_dapm
->seq_notifier(cur_dapm
,
1649 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1650 soc_dapm_async_complete(d
);
1654 static void dapm_widget_update(struct snd_soc_card
*card
)
1656 struct snd_soc_dapm_update
*update
= card
->update
;
1657 struct snd_soc_dapm_widget_list
*wlist
;
1658 struct snd_soc_dapm_widget
*w
= NULL
;
1662 if (!update
|| !dapm_kcontrol_is_powered(update
->kcontrol
))
1665 wlist
= dapm_kcontrol_get_wlist(update
->kcontrol
);
1667 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
1668 w
= wlist
->widgets
[wi
];
1670 if (w
->event
&& (w
->event_flags
& SND_SOC_DAPM_PRE_REG
)) {
1671 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_PRE_REG
);
1673 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM pre-event failed: %d\n",
1681 ret
= soc_dapm_update_bits(w
->dapm
, update
->reg
, update
->mask
,
1684 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM update failed: %d\n",
1687 if (update
->has_second_set
) {
1688 ret
= soc_dapm_update_bits(w
->dapm
, update
->reg2
,
1689 update
->mask2
, update
->val2
);
1691 dev_err(w
->dapm
->dev
,
1692 "ASoC: %s DAPM update failed: %d\n",
1696 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
1697 w
= wlist
->widgets
[wi
];
1699 if (w
->event
&& (w
->event_flags
& SND_SOC_DAPM_POST_REG
)) {
1700 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_POST_REG
);
1702 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM post-event failed: %d\n",
1708 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1709 * they're changing state.
1711 static void dapm_pre_sequence_async(void *data
, async_cookie_t cookie
)
1713 struct snd_soc_dapm_context
*d
= data
;
1716 /* If we're off and we're not supposed to go into STANDBY */
1717 if (d
->bias_level
== SND_SOC_BIAS_OFF
&&
1718 d
->target_bias_level
!= SND_SOC_BIAS_OFF
) {
1720 pm_runtime_get_sync(d
->dev
);
1722 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1725 "ASoC: Failed to turn on bias: %d\n", ret
);
1728 /* Prepare for a transition to ON or away from ON */
1729 if ((d
->target_bias_level
== SND_SOC_BIAS_ON
&&
1730 d
->bias_level
!= SND_SOC_BIAS_ON
) ||
1731 (d
->target_bias_level
!= SND_SOC_BIAS_ON
&&
1732 d
->bias_level
== SND_SOC_BIAS_ON
)) {
1733 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_PREPARE
);
1736 "ASoC: Failed to prepare bias: %d\n", ret
);
1740 /* Async callback run prior to DAPM sequences - brings to their final
1743 static void dapm_post_sequence_async(void *data
, async_cookie_t cookie
)
1745 struct snd_soc_dapm_context
*d
= data
;
1748 /* If we just powered the last thing off drop to standby bias */
1749 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1750 (d
->target_bias_level
== SND_SOC_BIAS_STANDBY
||
1751 d
->target_bias_level
== SND_SOC_BIAS_OFF
)) {
1752 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1754 dev_err(d
->dev
, "ASoC: Failed to apply standby bias: %d\n",
1758 /* If we're in standby and can support bias off then do that */
1759 if (d
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1760 d
->target_bias_level
== SND_SOC_BIAS_OFF
) {
1761 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_OFF
);
1763 dev_err(d
->dev
, "ASoC: Failed to turn off bias: %d\n",
1767 pm_runtime_put(d
->dev
);
1770 /* If we just powered up then move to active bias */
1771 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1772 d
->target_bias_level
== SND_SOC_BIAS_ON
) {
1773 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_ON
);
1775 dev_err(d
->dev
, "ASoC: Failed to apply active bias: %d\n",
1780 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget
*peer
,
1781 bool power
, bool connect
)
1783 /* If a connection is being made or broken then that update
1784 * will have marked the peer dirty, otherwise the widgets are
1785 * not connected and this update has no impact. */
1789 /* If the peer is already in the state we're moving to then we
1790 * won't have an impact on it. */
1791 if (power
!= peer
->power
)
1792 dapm_mark_dirty(peer
, "peer state change");
1795 static void dapm_widget_set_power(struct snd_soc_dapm_widget
*w
, bool power
,
1796 struct list_head
*up_list
,
1797 struct list_head
*down_list
)
1799 struct snd_soc_dapm_path
*path
;
1801 if (w
->power
== power
)
1804 trace_snd_soc_dapm_widget_power(w
, power
);
1806 /* If we changed our power state perhaps our neigbours changed
1809 snd_soc_dapm_widget_for_each_source_path(w
, path
)
1810 dapm_widget_set_peer_power(path
->source
, power
, path
->connect
);
1812 /* Supplies can't affect their outputs, only their inputs */
1813 if (!w
->is_supply
) {
1814 snd_soc_dapm_widget_for_each_sink_path(w
, path
)
1815 dapm_widget_set_peer_power(path
->sink
, power
,
1820 dapm_seq_insert(w
, up_list
, true);
1822 dapm_seq_insert(w
, down_list
, false);
1825 static void dapm_power_one_widget(struct snd_soc_dapm_widget
*w
,
1826 struct list_head
*up_list
,
1827 struct list_head
*down_list
)
1832 case snd_soc_dapm_pre
:
1833 dapm_seq_insert(w
, down_list
, false);
1835 case snd_soc_dapm_post
:
1836 dapm_seq_insert(w
, up_list
, true);
1840 power
= dapm_widget_power_check(w
);
1842 dapm_widget_set_power(w
, power
, up_list
, down_list
);
1847 static bool dapm_idle_bias_off(struct snd_soc_dapm_context
*dapm
)
1849 if (dapm
->idle_bias_off
)
1852 switch (snd_power_get_state(dapm
->card
->snd_card
)) {
1853 case SNDRV_CTL_POWER_D3hot
:
1854 case SNDRV_CTL_POWER_D3cold
:
1855 return dapm
->suspend_bias_off
;
1864 * Scan each dapm widget for complete audio path.
1865 * A complete path is a route that has valid endpoints i.e.:-
1867 * o DAC to output pin.
1868 * o Input pin to ADC.
1869 * o Input pin to Output pin (bypass, sidetone)
1870 * o DAC to ADC (loopback).
1872 static int dapm_power_widgets(struct snd_soc_card
*card
, int event
)
1874 struct snd_soc_dapm_widget
*w
;
1875 struct snd_soc_dapm_context
*d
;
1877 LIST_HEAD(down_list
);
1878 ASYNC_DOMAIN_EXCLUSIVE(async_domain
);
1879 enum snd_soc_bias_level bias
;
1881 lockdep_assert_held(&card
->dapm_mutex
);
1883 trace_snd_soc_dapm_start(card
);
1885 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1886 if (dapm_idle_bias_off(d
))
1887 d
->target_bias_level
= SND_SOC_BIAS_OFF
;
1889 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1894 /* Check which widgets we need to power and store them in
1895 * lists indicating if they should be powered up or down. We
1896 * only check widgets that have been flagged as dirty but note
1897 * that new widgets may be added to the dirty list while we
1900 list_for_each_entry(w
, &card
->dapm_dirty
, dirty
) {
1901 dapm_power_one_widget(w
, &up_list
, &down_list
);
1904 list_for_each_entry(w
, &card
->widgets
, list
) {
1906 case snd_soc_dapm_pre
:
1907 case snd_soc_dapm_post
:
1908 /* These widgets always need to be powered */
1911 list_del_init(&w
->dirty
);
1918 /* Supplies and micbiases only bring the
1919 * context up to STANDBY as unless something
1920 * else is active and passing audio they
1921 * generally don't require full power. Signal
1922 * generators are virtual pins and have no
1923 * power impact themselves.
1926 case snd_soc_dapm_siggen
:
1927 case snd_soc_dapm_vmid
:
1929 case snd_soc_dapm_supply
:
1930 case snd_soc_dapm_regulator_supply
:
1931 case snd_soc_dapm_pinctrl
:
1932 case snd_soc_dapm_clock_supply
:
1933 case snd_soc_dapm_micbias
:
1934 if (d
->target_bias_level
< SND_SOC_BIAS_STANDBY
)
1935 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1938 d
->target_bias_level
= SND_SOC_BIAS_ON
;
1945 /* Force all contexts in the card to the same bias state if
1946 * they're not ground referenced.
1948 bias
= SND_SOC_BIAS_OFF
;
1949 list_for_each_entry(d
, &card
->dapm_list
, list
)
1950 if (d
->target_bias_level
> bias
)
1951 bias
= d
->target_bias_level
;
1952 list_for_each_entry(d
, &card
->dapm_list
, list
)
1953 if (!dapm_idle_bias_off(d
))
1954 d
->target_bias_level
= bias
;
1956 trace_snd_soc_dapm_walk_done(card
);
1958 /* Run card bias changes at first */
1959 dapm_pre_sequence_async(&card
->dapm
, 0);
1960 /* Run other bias changes in parallel */
1961 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1962 if (d
!= &card
->dapm
)
1963 async_schedule_domain(dapm_pre_sequence_async
, d
,
1966 async_synchronize_full_domain(&async_domain
);
1968 list_for_each_entry(w
, &down_list
, power_list
) {
1969 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_WILL_PMD
);
1972 list_for_each_entry(w
, &up_list
, power_list
) {
1973 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_WILL_PMU
);
1976 /* Power down widgets first; try to avoid amplifying pops. */
1977 dapm_seq_run(card
, &down_list
, event
, false);
1979 dapm_widget_update(card
);
1982 dapm_seq_run(card
, &up_list
, event
, true);
1984 /* Run all the bias changes in parallel */
1985 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1986 if (d
!= &card
->dapm
)
1987 async_schedule_domain(dapm_post_sequence_async
, d
,
1990 async_synchronize_full_domain(&async_domain
);
1991 /* Run card bias changes at last */
1992 dapm_post_sequence_async(&card
->dapm
, 0);
1994 /* do we need to notify any clients that DAPM event is complete */
1995 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1996 if (d
->stream_event
)
1997 d
->stream_event(d
, event
);
2000 pop_dbg(card
->dev
, card
->pop_time
,
2001 "DAPM sequencing finished, waiting %dms\n", card
->pop_time
);
2002 pop_wait(card
->pop_time
);
2004 trace_snd_soc_dapm_done(card
);
2009 #ifdef CONFIG_DEBUG_FS
2010 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
2011 char __user
*user_buf
,
2012 size_t count
, loff_t
*ppos
)
2014 struct snd_soc_dapm_widget
*w
= file
->private_data
;
2015 struct snd_soc_card
*card
= w
->dapm
->card
;
2016 enum snd_soc_dapm_direction dir
, rdir
;
2020 struct snd_soc_dapm_path
*p
= NULL
;
2022 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
2026 mutex_lock(&card
->dapm_mutex
);
2028 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2033 in
= is_connected_input_ep(w
, NULL
, NULL
);
2034 out
= is_connected_output_ep(w
, NULL
, NULL
);
2037 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s%s in %d out %d",
2038 w
->name
, w
->power
? "On" : "Off",
2039 w
->force
? " (forced)" : "", in
, out
);
2042 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
2043 " - R%d(0x%x) mask 0x%x",
2044 w
->reg
, w
->reg
, w
->mask
<< w
->shift
);
2046 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
2049 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
2051 w
->active
? "active" : "inactive");
2053 snd_soc_dapm_for_each_direction(dir
) {
2054 rdir
= SND_SOC_DAPM_DIR_REVERSE(dir
);
2055 snd_soc_dapm_widget_for_each_path(w
, dir
, p
) {
2056 if (p
->connected
&& !p
->connected(p
->source
, p
->sink
))
2062 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
2063 " %s \"%s\" \"%s\"\n",
2064 (rdir
== SND_SOC_DAPM_DIR_IN
) ? "in" : "out",
2065 p
->name
? p
->name
: "static",
2066 p
->node
[rdir
]->name
);
2070 mutex_unlock(&card
->dapm_mutex
);
2072 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
2078 static const struct file_operations dapm_widget_power_fops
= {
2079 .open
= simple_open
,
2080 .read
= dapm_widget_power_read_file
,
2081 .llseek
= default_llseek
,
2084 static ssize_t
dapm_bias_read_file(struct file
*file
, char __user
*user_buf
,
2085 size_t count
, loff_t
*ppos
)
2087 struct snd_soc_dapm_context
*dapm
= file
->private_data
;
2090 switch (dapm
->bias_level
) {
2091 case SND_SOC_BIAS_ON
:
2094 case SND_SOC_BIAS_PREPARE
:
2095 level
= "Prepare\n";
2097 case SND_SOC_BIAS_STANDBY
:
2098 level
= "Standby\n";
2100 case SND_SOC_BIAS_OFF
:
2104 WARN(1, "Unknown bias_level %d\n", dapm
->bias_level
);
2105 level
= "Unknown\n";
2109 return simple_read_from_buffer(user_buf
, count
, ppos
, level
,
2113 static const struct file_operations dapm_bias_fops
= {
2114 .open
= simple_open
,
2115 .read
= dapm_bias_read_file
,
2116 .llseek
= default_llseek
,
2119 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
2120 struct dentry
*parent
)
2127 dapm
->debugfs_dapm
= debugfs_create_dir("dapm", parent
);
2129 if (!dapm
->debugfs_dapm
) {
2131 "ASoC: Failed to create DAPM debugfs directory\n");
2135 d
= debugfs_create_file("bias_level", 0444,
2136 dapm
->debugfs_dapm
, dapm
,
2140 "ASoC: Failed to create bias level debugfs file\n");
2143 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
2145 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
2148 if (!dapm
->debugfs_dapm
|| !w
->name
)
2151 d
= debugfs_create_file(w
->name
, 0444,
2152 dapm
->debugfs_dapm
, w
,
2153 &dapm_widget_power_fops
);
2155 dev_warn(w
->dapm
->dev
,
2156 "ASoC: Failed to create %s debugfs file\n",
2160 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
2162 debugfs_remove_recursive(dapm
->debugfs_dapm
);
2166 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
2167 struct dentry
*parent
)
2171 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
2175 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
2182 * soc_dapm_connect_path() - Connects or disconnects a path
2183 * @path: The path to update
2184 * @connect: The new connect state of the path. True if the path is connected,
2185 * false if it is disconnected.
2186 * @reason: The reason why the path changed (for debugging only)
2188 static void soc_dapm_connect_path(struct snd_soc_dapm_path
*path
,
2189 bool connect
, const char *reason
)
2191 if (path
->connect
== connect
)
2194 path
->connect
= connect
;
2195 dapm_mark_dirty(path
->source
, reason
);
2196 dapm_mark_dirty(path
->sink
, reason
);
2197 dapm_path_invalidate(path
);
2200 /* test and update the power status of a mux widget */
2201 static int soc_dapm_mux_update_power(struct snd_soc_card
*card
,
2202 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
)
2204 struct snd_soc_dapm_path
*path
;
2208 lockdep_assert_held(&card
->dapm_mutex
);
2210 /* find dapm widget path assoc with kcontrol */
2211 dapm_kcontrol_for_each_path(path
, kcontrol
) {
2213 /* we now need to match the string in the enum to the path */
2214 if (!(strcmp(path
->name
, e
->texts
[mux
])))
2219 soc_dapm_connect_path(path
, connect
, "mux update");
2223 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2228 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context
*dapm
,
2229 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
,
2230 struct snd_soc_dapm_update
*update
)
2232 struct snd_soc_card
*card
= dapm
->card
;
2235 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2236 card
->update
= update
;
2237 ret
= soc_dapm_mux_update_power(card
, kcontrol
, mux
, e
);
2238 card
->update
= NULL
;
2239 mutex_unlock(&card
->dapm_mutex
);
2241 soc_dpcm_runtime_update(card
);
2244 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power
);
2246 /* test and update the power status of a mixer or switch widget */
2247 static int soc_dapm_mixer_update_power(struct snd_soc_card
*card
,
2248 struct snd_kcontrol
*kcontrol
,
2249 int connect
, int rconnect
)
2251 struct snd_soc_dapm_path
*path
;
2254 lockdep_assert_held(&card
->dapm_mutex
);
2256 /* find dapm widget path assoc with kcontrol */
2257 dapm_kcontrol_for_each_path(path
, kcontrol
) {
2259 * Ideally this function should support any number of
2260 * paths and channels. But since kcontrols only come
2261 * in mono and stereo variants, we are limited to 2
2264 * The following code assumes for stereo controls the
2265 * first path (when 'found == 0') is the left channel,
2266 * and all remaining paths (when 'found == 1') are the
2269 * A stereo control is signified by a valid 'rconnect'
2270 * value, either 0 for unconnected, or >= 0 for connected.
2271 * This is chosen instead of using snd_soc_volsw_is_stereo,
2272 * so that the behavior of snd_soc_dapm_mixer_update_power
2273 * doesn't change even when the kcontrol passed in is
2276 * It passes 'connect' as the path connect status for
2277 * the left channel, and 'rconnect' for the right
2280 if (found
&& rconnect
>= 0)
2281 soc_dapm_connect_path(path
, rconnect
, "mixer update");
2283 soc_dapm_connect_path(path
, connect
, "mixer update");
2288 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2293 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context
*dapm
,
2294 struct snd_kcontrol
*kcontrol
, int connect
,
2295 struct snd_soc_dapm_update
*update
)
2297 struct snd_soc_card
*card
= dapm
->card
;
2300 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2301 card
->update
= update
;
2302 ret
= soc_dapm_mixer_update_power(card
, kcontrol
, connect
, -1);
2303 card
->update
= NULL
;
2304 mutex_unlock(&card
->dapm_mutex
);
2306 soc_dpcm_runtime_update(card
);
2309 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power
);
2311 static ssize_t
dapm_widget_show_component(struct snd_soc_component
*cmpnt
,
2314 struct snd_soc_dapm_context
*dapm
= snd_soc_component_get_dapm(cmpnt
);
2315 struct snd_soc_dapm_widget
*w
;
2317 char *state
= "not set";
2319 /* card won't be set for the dummy component, as a spot fix
2320 * we're checking for that case specifically here but in future
2321 * we will ensure that the dummy component looks like others.
2326 list_for_each_entry(w
, &cmpnt
->card
->widgets
, list
) {
2327 if (w
->dapm
!= dapm
)
2330 /* only display widgets that burn power */
2332 case snd_soc_dapm_hp
:
2333 case snd_soc_dapm_mic
:
2334 case snd_soc_dapm_spk
:
2335 case snd_soc_dapm_line
:
2336 case snd_soc_dapm_micbias
:
2337 case snd_soc_dapm_dac
:
2338 case snd_soc_dapm_adc
:
2339 case snd_soc_dapm_pga
:
2340 case snd_soc_dapm_out_drv
:
2341 case snd_soc_dapm_mixer
:
2342 case snd_soc_dapm_mixer_named_ctl
:
2343 case snd_soc_dapm_supply
:
2344 case snd_soc_dapm_regulator_supply
:
2345 case snd_soc_dapm_pinctrl
:
2346 case snd_soc_dapm_clock_supply
:
2348 count
+= sprintf(buf
+ count
, "%s: %s\n",
2349 w
->name
, w
->power
? "On":"Off");
2356 switch (snd_soc_dapm_get_bias_level(dapm
)) {
2357 case SND_SOC_BIAS_ON
:
2360 case SND_SOC_BIAS_PREPARE
:
2363 case SND_SOC_BIAS_STANDBY
:
2366 case SND_SOC_BIAS_OFF
:
2370 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
2375 /* show dapm widget status in sys fs */
2376 static ssize_t
dapm_widget_show(struct device
*dev
,
2377 struct device_attribute
*attr
, char *buf
)
2379 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
2382 mutex_lock(&rtd
->card
->dapm_mutex
);
2384 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
2385 struct snd_soc_component
*cmpnt
= rtd
->codec_dais
[i
]->component
;
2387 count
+= dapm_widget_show_component(cmpnt
, buf
+ count
);
2390 mutex_unlock(&rtd
->card
->dapm_mutex
);
2395 static DEVICE_ATTR_RO(dapm_widget
);
2397 struct attribute
*soc_dapm_dev_attrs
[] = {
2398 &dev_attr_dapm_widget
.attr
,
2402 static void dapm_free_path(struct snd_soc_dapm_path
*path
)
2404 list_del(&path
->list_node
[SND_SOC_DAPM_DIR_IN
]);
2405 list_del(&path
->list_node
[SND_SOC_DAPM_DIR_OUT
]);
2406 list_del(&path
->list_kcontrol
);
2407 list_del(&path
->list
);
2411 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget
*w
)
2413 struct snd_soc_dapm_path
*p
, *next_p
;
2414 enum snd_soc_dapm_direction dir
;
2418 * remove source and sink paths associated to this widget.
2419 * While removing the path, remove reference to it from both
2420 * source and sink widgets so that path is removed only once.
2422 snd_soc_dapm_for_each_direction(dir
) {
2423 snd_soc_dapm_widget_for_each_path_safe(w
, dir
, p
, next_p
)
2427 kfree(w
->kcontrols
);
2428 kfree_const(w
->name
);
2432 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context
*dapm
)
2434 dapm
->path_sink_cache
.widget
= NULL
;
2435 dapm
->path_source_cache
.widget
= NULL
;
2438 /* free all dapm widgets and resources */
2439 static void dapm_free_widgets(struct snd_soc_dapm_context
*dapm
)
2441 struct snd_soc_dapm_widget
*w
, *next_w
;
2443 list_for_each_entry_safe(w
, next_w
, &dapm
->card
->widgets
, list
) {
2444 if (w
->dapm
!= dapm
)
2446 snd_soc_dapm_free_widget(w
);
2448 snd_soc_dapm_reset_cache(dapm
);
2451 static struct snd_soc_dapm_widget
*dapm_find_widget(
2452 struct snd_soc_dapm_context
*dapm
, const char *pin
,
2453 bool search_other_contexts
)
2455 struct snd_soc_dapm_widget
*w
;
2456 struct snd_soc_dapm_widget
*fallback
= NULL
;
2458 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
2459 if (!strcmp(w
->name
, pin
)) {
2460 if (w
->dapm
== dapm
)
2467 if (search_other_contexts
)
2473 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context
*dapm
,
2474 const char *pin
, int status
)
2476 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
2478 dapm_assert_locked(dapm
);
2481 dev_err(dapm
->dev
, "ASoC: DAPM unknown pin %s\n", pin
);
2485 if (w
->connected
!= status
) {
2486 dapm_mark_dirty(w
, "pin configuration");
2487 dapm_widget_invalidate_input_paths(w
);
2488 dapm_widget_invalidate_output_paths(w
);
2491 w
->connected
= status
;
2499 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2500 * @dapm: DAPM context
2502 * Walks all dapm audio paths and powers widgets according to their
2503 * stream or path usage.
2505 * Requires external locking.
2507 * Returns 0 for success.
2509 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context
*dapm
)
2512 * Suppress early reports (eg, jacks syncing their state) to avoid
2513 * silly DAPM runs during card startup.
2515 if (!dapm
->card
|| !dapm
->card
->instantiated
)
2518 return dapm_power_widgets(dapm
->card
, SND_SOC_DAPM_STREAM_NOP
);
2520 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked
);
2523 * snd_soc_dapm_sync - scan and power dapm paths
2524 * @dapm: DAPM context
2526 * Walks all dapm audio paths and powers widgets according to their
2527 * stream or path usage.
2529 * Returns 0 for success.
2531 int snd_soc_dapm_sync(struct snd_soc_dapm_context
*dapm
)
2535 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2536 ret
= snd_soc_dapm_sync_unlocked(dapm
);
2537 mutex_unlock(&dapm
->card
->dapm_mutex
);
2540 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
2543 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2544 * @w: The widget for which to update the flags
2546 * Some widgets have a dynamic category which depends on which neighbors they
2547 * are connected to. This function update the category for these widgets.
2549 * This function must be called whenever a path is added or removed to a widget.
2551 static void dapm_update_widget_flags(struct snd_soc_dapm_widget
*w
)
2553 enum snd_soc_dapm_direction dir
;
2554 struct snd_soc_dapm_path
*p
;
2558 case snd_soc_dapm_input
:
2559 /* On a fully routed card an input is never a source */
2560 if (w
->dapm
->card
->fully_routed
)
2562 ep
= SND_SOC_DAPM_EP_SOURCE
;
2563 snd_soc_dapm_widget_for_each_source_path(w
, p
) {
2564 if (p
->source
->id
== snd_soc_dapm_micbias
||
2565 p
->source
->id
== snd_soc_dapm_mic
||
2566 p
->source
->id
== snd_soc_dapm_line
||
2567 p
->source
->id
== snd_soc_dapm_output
) {
2573 case snd_soc_dapm_output
:
2574 /* On a fully routed card a output is never a sink */
2575 if (w
->dapm
->card
->fully_routed
)
2577 ep
= SND_SOC_DAPM_EP_SINK
;
2578 snd_soc_dapm_widget_for_each_sink_path(w
, p
) {
2579 if (p
->sink
->id
== snd_soc_dapm_spk
||
2580 p
->sink
->id
== snd_soc_dapm_hp
||
2581 p
->sink
->id
== snd_soc_dapm_line
||
2582 p
->sink
->id
== snd_soc_dapm_input
) {
2588 case snd_soc_dapm_line
:
2590 snd_soc_dapm_for_each_direction(dir
) {
2591 if (!list_empty(&w
->edges
[dir
]))
2592 ep
|= SND_SOC_DAPM_DIR_TO_EP(dir
);
2602 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context
*dapm
,
2603 struct snd_soc_dapm_widget
*source
, struct snd_soc_dapm_widget
*sink
,
2604 const char *control
)
2606 bool dynamic_source
= false;
2607 bool dynamic_sink
= false;
2612 switch (source
->id
) {
2613 case snd_soc_dapm_demux
:
2614 dynamic_source
= true;
2621 case snd_soc_dapm_mux
:
2622 case snd_soc_dapm_switch
:
2623 case snd_soc_dapm_mixer
:
2624 case snd_soc_dapm_mixer_named_ctl
:
2625 dynamic_sink
= true;
2631 if (dynamic_source
&& dynamic_sink
) {
2633 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2634 source
->name
, control
, sink
->name
);
2636 } else if (!dynamic_source
&& !dynamic_sink
) {
2638 "Control not supported for path %s -> [%s] -> %s\n",
2639 source
->name
, control
, sink
->name
);
2646 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context
*dapm
,
2647 struct snd_soc_dapm_widget
*wsource
, struct snd_soc_dapm_widget
*wsink
,
2648 const char *control
,
2649 int (*connected
)(struct snd_soc_dapm_widget
*source
,
2650 struct snd_soc_dapm_widget
*sink
))
2652 struct snd_soc_dapm_widget
*widgets
[2];
2653 enum snd_soc_dapm_direction dir
;
2654 struct snd_soc_dapm_path
*path
;
2657 if (wsink
->is_supply
&& !wsource
->is_supply
) {
2659 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2660 wsource
->name
, wsink
->name
);
2664 if (connected
&& !wsource
->is_supply
) {
2666 "connected() callback only supported for supply widgets (%s -> %s)\n",
2667 wsource
->name
, wsink
->name
);
2671 if (wsource
->is_supply
&& control
) {
2673 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2674 wsource
->name
, control
, wsink
->name
);
2678 ret
= snd_soc_dapm_check_dynamic_path(dapm
, wsource
, wsink
, control
);
2682 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
2686 path
->node
[SND_SOC_DAPM_DIR_IN
] = wsource
;
2687 path
->node
[SND_SOC_DAPM_DIR_OUT
] = wsink
;
2688 widgets
[SND_SOC_DAPM_DIR_IN
] = wsource
;
2689 widgets
[SND_SOC_DAPM_DIR_OUT
] = wsink
;
2691 path
->connected
= connected
;
2692 INIT_LIST_HEAD(&path
->list
);
2693 INIT_LIST_HEAD(&path
->list_kcontrol
);
2695 if (wsource
->is_supply
|| wsink
->is_supply
)
2696 path
->is_supply
= 1;
2698 /* connect static paths */
2699 if (control
== NULL
) {
2702 switch (wsource
->id
) {
2703 case snd_soc_dapm_demux
:
2704 ret
= dapm_connect_mux(dapm
, path
, control
, wsource
);
2712 switch (wsink
->id
) {
2713 case snd_soc_dapm_mux
:
2714 ret
= dapm_connect_mux(dapm
, path
, control
, wsink
);
2718 case snd_soc_dapm_switch
:
2719 case snd_soc_dapm_mixer
:
2720 case snd_soc_dapm_mixer_named_ctl
:
2721 ret
= dapm_connect_mixer(dapm
, path
, control
);
2730 list_add(&path
->list
, &dapm
->card
->paths
);
2731 snd_soc_dapm_for_each_direction(dir
)
2732 list_add(&path
->list_node
[dir
], &widgets
[dir
]->edges
[dir
]);
2734 snd_soc_dapm_for_each_direction(dir
) {
2735 dapm_update_widget_flags(widgets
[dir
]);
2736 dapm_mark_dirty(widgets
[dir
], "Route added");
2739 if (dapm
->card
->instantiated
&& path
->connect
)
2740 dapm_path_invalidate(path
);
2748 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context
*dapm
,
2749 const struct snd_soc_dapm_route
*route
)
2751 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
2752 struct snd_soc_dapm_widget
*wtsource
= NULL
, *wtsink
= NULL
;
2755 char prefixed_sink
[80];
2756 char prefixed_source
[80];
2760 prefix
= soc_dapm_prefix(dapm
);
2762 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
2763 prefix
, route
->sink
);
2764 sink
= prefixed_sink
;
2765 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
2766 prefix
, route
->source
);
2767 source
= prefixed_source
;
2770 source
= route
->source
;
2773 wsource
= dapm_wcache_lookup(&dapm
->path_source_cache
, source
);
2774 wsink
= dapm_wcache_lookup(&dapm
->path_sink_cache
, sink
);
2776 if (wsink
&& wsource
)
2780 * find src and dest widgets over all widgets but favor a widget from
2781 * current DAPM context
2783 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
2784 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
2786 if (w
->dapm
== dapm
) {
2793 if (!wsource
&& !(strcmp(w
->name
, source
))) {
2795 if (w
->dapm
== dapm
) {
2802 /* use widget from another DAPM context if not found from this */
2808 if (wsource
== NULL
) {
2809 dev_err(dapm
->dev
, "ASoC: no source widget found for %s\n",
2813 if (wsink
== NULL
) {
2814 dev_err(dapm
->dev
, "ASoC: no sink widget found for %s\n",
2820 dapm_wcache_update(&dapm
->path_sink_cache
, wsink
);
2821 dapm_wcache_update(&dapm
->path_source_cache
, wsource
);
2823 ret
= snd_soc_dapm_add_path(dapm
, wsource
, wsink
, route
->control
,
2830 dev_warn(dapm
->dev
, "ASoC: no dapm match for %s --> %s --> %s\n",
2831 source
, route
->control
, sink
);
2835 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context
*dapm
,
2836 const struct snd_soc_dapm_route
*route
)
2838 struct snd_soc_dapm_widget
*wsource
, *wsink
;
2839 struct snd_soc_dapm_path
*path
, *p
;
2842 char prefixed_sink
[80];
2843 char prefixed_source
[80];
2846 if (route
->control
) {
2848 "ASoC: Removal of routes with controls not supported\n");
2852 prefix
= soc_dapm_prefix(dapm
);
2854 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
2855 prefix
, route
->sink
);
2856 sink
= prefixed_sink
;
2857 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
2858 prefix
, route
->source
);
2859 source
= prefixed_source
;
2862 source
= route
->source
;
2866 list_for_each_entry(p
, &dapm
->card
->paths
, list
) {
2867 if (strcmp(p
->source
->name
, source
) != 0)
2869 if (strcmp(p
->sink
->name
, sink
) != 0)
2876 wsource
= path
->source
;
2879 dapm_mark_dirty(wsource
, "Route removed");
2880 dapm_mark_dirty(wsink
, "Route removed");
2882 dapm_path_invalidate(path
);
2884 dapm_free_path(path
);
2886 /* Update any path related flags */
2887 dapm_update_widget_flags(wsource
);
2888 dapm_update_widget_flags(wsink
);
2890 dev_warn(dapm
->dev
, "ASoC: Route %s->%s does not exist\n",
2898 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2899 * @dapm: DAPM context
2900 * @route: audio routes
2901 * @num: number of routes
2903 * Connects 2 dapm widgets together via a named audio path. The sink is
2904 * the widget receiving the audio signal, whilst the source is the sender
2905 * of the audio signal.
2907 * Returns 0 for success else error. On error all resources can be freed
2908 * with a call to snd_soc_card_free().
2910 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context
*dapm
,
2911 const struct snd_soc_dapm_route
*route
, int num
)
2915 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2916 for (i
= 0; i
< num
; i
++) {
2917 r
= snd_soc_dapm_add_route(dapm
, route
);
2919 dev_err(dapm
->dev
, "ASoC: Failed to add route %s -> %s -> %s\n",
2921 route
->control
? route
->control
: "direct",
2927 mutex_unlock(&dapm
->card
->dapm_mutex
);
2931 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
2934 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2935 * @dapm: DAPM context
2936 * @route: audio routes
2937 * @num: number of routes
2939 * Removes routes from the DAPM context.
2941 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context
*dapm
,
2942 const struct snd_soc_dapm_route
*route
, int num
)
2946 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2947 for (i
= 0; i
< num
; i
++) {
2948 snd_soc_dapm_del_route(dapm
, route
);
2951 mutex_unlock(&dapm
->card
->dapm_mutex
);
2955 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes
);
2957 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context
*dapm
,
2958 const struct snd_soc_dapm_route
*route
)
2960 struct snd_soc_dapm_widget
*source
= dapm_find_widget(dapm
,
2963 struct snd_soc_dapm_widget
*sink
= dapm_find_widget(dapm
,
2966 struct snd_soc_dapm_path
*path
;
2970 dev_err(dapm
->dev
, "ASoC: Unable to find source %s for weak route\n",
2976 dev_err(dapm
->dev
, "ASoC: Unable to find sink %s for weak route\n",
2981 if (route
->control
|| route
->connected
)
2982 dev_warn(dapm
->dev
, "ASoC: Ignoring control for weak route %s->%s\n",
2983 route
->source
, route
->sink
);
2985 snd_soc_dapm_widget_for_each_sink_path(source
, path
) {
2986 if (path
->sink
== sink
) {
2993 dev_err(dapm
->dev
, "ASoC: No path found for weak route %s->%s\n",
2994 route
->source
, route
->sink
);
2996 dev_warn(dapm
->dev
, "ASoC: %d paths found for weak route %s->%s\n",
2997 count
, route
->source
, route
->sink
);
3003 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3004 * @dapm: DAPM context
3005 * @route: audio routes
3006 * @num: number of routes
3008 * Mark existing routes matching those specified in the passed array
3009 * as being weak, meaning that they are ignored for the purpose of
3010 * power decisions. The main intended use case is for sidetone paths
3011 * which couple audio between other independent paths if they are both
3012 * active in order to make the combination work better at the user
3013 * level but which aren't intended to be "used".
3015 * Note that CODEC drivers should not use this as sidetone type paths
3016 * can frequently also be used as bypass paths.
3018 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context
*dapm
,
3019 const struct snd_soc_dapm_route
*route
, int num
)
3024 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
3025 for (i
= 0; i
< num
; i
++) {
3026 err
= snd_soc_dapm_weak_route(dapm
, route
);
3031 mutex_unlock(&dapm
->card
->dapm_mutex
);
3035 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes
);
3038 * snd_soc_dapm_new_widgets - add new dapm widgets
3039 * @card: card to be checked for new dapm widgets
3041 * Checks the codec for any new dapm widgets and creates them if found.
3043 * Returns 0 for success.
3045 int snd_soc_dapm_new_widgets(struct snd_soc_card
*card
)
3047 struct snd_soc_dapm_widget
*w
;
3050 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
3052 list_for_each_entry(w
, &card
->widgets
, list
)
3057 if (w
->num_kcontrols
) {
3058 w
->kcontrols
= kcalloc(w
->num_kcontrols
,
3059 sizeof(struct snd_kcontrol
*),
3061 if (!w
->kcontrols
) {
3062 mutex_unlock(&card
->dapm_mutex
);
3068 case snd_soc_dapm_switch
:
3069 case snd_soc_dapm_mixer
:
3070 case snd_soc_dapm_mixer_named_ctl
:
3073 case snd_soc_dapm_mux
:
3074 case snd_soc_dapm_demux
:
3077 case snd_soc_dapm_pga
:
3078 case snd_soc_dapm_out_drv
:
3081 case snd_soc_dapm_dai_link
:
3082 dapm_new_dai_link(w
);
3088 /* Read the initial power state from the device */
3090 soc_dapm_read(w
->dapm
, w
->reg
, &val
);
3091 val
= val
>> w
->shift
;
3093 if (val
== w
->on_val
)
3099 dapm_mark_dirty(w
, "new widget");
3100 dapm_debugfs_add_widget(w
);
3103 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
3104 mutex_unlock(&card
->dapm_mutex
);
3107 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
3110 * snd_soc_dapm_get_volsw - dapm mixer get callback
3111 * @kcontrol: mixer control
3112 * @ucontrol: control element information
3114 * Callback to get the value of a dapm mixer control.
3116 * Returns 0 for success.
3118 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
3119 struct snd_ctl_elem_value
*ucontrol
)
3121 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
3122 struct snd_soc_card
*card
= dapm
->card
;
3123 struct soc_mixer_control
*mc
=
3124 (struct soc_mixer_control
*)kcontrol
->private_value
;
3126 unsigned int shift
= mc
->shift
;
3128 unsigned int width
= fls(max
);
3129 unsigned int mask
= (1 << fls(max
)) - 1;
3130 unsigned int invert
= mc
->invert
;
3131 unsigned int reg_val
, val
, rval
= 0;
3134 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3135 if (dapm_kcontrol_is_powered(kcontrol
) && reg
!= SND_SOC_NOPM
) {
3136 ret
= soc_dapm_read(dapm
, reg
, ®_val
);
3137 val
= (reg_val
>> shift
) & mask
;
3139 if (ret
== 0 && reg
!= mc
->rreg
)
3140 ret
= soc_dapm_read(dapm
, mc
->rreg
, ®_val
);
3142 if (snd_soc_volsw_is_stereo(mc
))
3143 rval
= (reg_val
>> mc
->rshift
) & mask
;
3145 reg_val
= dapm_kcontrol_get_value(kcontrol
);
3146 val
= reg_val
& mask
;
3148 if (snd_soc_volsw_is_stereo(mc
))
3149 rval
= (reg_val
>> width
) & mask
;
3151 mutex_unlock(&card
->dapm_mutex
);
3157 ucontrol
->value
.integer
.value
[0] = max
- val
;
3159 ucontrol
->value
.integer
.value
[0] = val
;
3161 if (snd_soc_volsw_is_stereo(mc
)) {
3163 ucontrol
->value
.integer
.value
[1] = max
- rval
;
3165 ucontrol
->value
.integer
.value
[1] = rval
;
3170 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
3173 * snd_soc_dapm_put_volsw - dapm mixer set callback
3174 * @kcontrol: mixer control
3175 * @ucontrol: control element information
3177 * Callback to set the value of a dapm mixer control.
3179 * Returns 0 for success.
3181 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
3182 struct snd_ctl_elem_value
*ucontrol
)
3184 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
3185 struct snd_soc_card
*card
= dapm
->card
;
3186 struct soc_mixer_control
*mc
=
3187 (struct soc_mixer_control
*)kcontrol
->private_value
;
3189 unsigned int shift
= mc
->shift
;
3191 unsigned int width
= fls(max
);
3192 unsigned int mask
= (1 << width
) - 1;
3193 unsigned int invert
= mc
->invert
;
3194 unsigned int val
, rval
= 0;
3195 int connect
, rconnect
= -1, change
, reg_change
= 0;
3196 struct snd_soc_dapm_update update
= {};
3199 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
3205 if (snd_soc_volsw_is_stereo(mc
)) {
3206 rval
= (ucontrol
->value
.integer
.value
[1] & mask
);
3212 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3214 /* This assumes field width < (bits in unsigned int / 2) */
3215 if (width
> sizeof(unsigned int) * 8 / 2)
3217 "ASoC: control %s field width limit exceeded\n",
3219 change
= dapm_kcontrol_set_value(kcontrol
, val
| (rval
<< width
));
3221 if (reg
!= SND_SOC_NOPM
) {
3223 rval
= rval
<< mc
->rshift
;
3225 reg_change
= soc_dapm_test_bits(dapm
, reg
, mask
<< shift
, val
);
3227 if (snd_soc_volsw_is_stereo(mc
))
3228 reg_change
|= soc_dapm_test_bits(dapm
, mc
->rreg
,
3233 if (change
|| reg_change
) {
3235 if (snd_soc_volsw_is_stereo(mc
)) {
3236 update
.has_second_set
= true;
3237 update
.reg2
= mc
->rreg
;
3238 update
.mask2
= mask
<< mc
->rshift
;
3241 update
.kcontrol
= kcontrol
;
3243 update
.mask
= mask
<< shift
;
3245 card
->update
= &update
;
3247 change
|= reg_change
;
3249 ret
= soc_dapm_mixer_update_power(card
, kcontrol
, connect
,
3252 card
->update
= NULL
;
3255 mutex_unlock(&card
->dapm_mutex
);
3258 soc_dpcm_runtime_update(card
);
3262 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
3265 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3266 * @kcontrol: mixer control
3267 * @ucontrol: control element information
3269 * Callback to get the value of a dapm enumerated double mixer control.
3271 * Returns 0 for success.
3273 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
3274 struct snd_ctl_elem_value
*ucontrol
)
3276 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
3277 struct snd_soc_card
*card
= dapm
->card
;
3278 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
3279 unsigned int reg_val
, val
;
3281 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3282 if (e
->reg
!= SND_SOC_NOPM
&& dapm_kcontrol_is_powered(kcontrol
)) {
3283 int ret
= soc_dapm_read(dapm
, e
->reg
, ®_val
);
3285 mutex_unlock(&card
->dapm_mutex
);
3289 reg_val
= dapm_kcontrol_get_value(kcontrol
);
3291 mutex_unlock(&card
->dapm_mutex
);
3293 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
3294 ucontrol
->value
.enumerated
.item
[0] = snd_soc_enum_val_to_item(e
, val
);
3295 if (e
->shift_l
!= e
->shift_r
) {
3296 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
3297 val
= snd_soc_enum_val_to_item(e
, val
);
3298 ucontrol
->value
.enumerated
.item
[1] = val
;
3303 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
3306 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3307 * @kcontrol: mixer control
3308 * @ucontrol: control element information
3310 * Callback to set the value of a dapm enumerated double mixer control.
3312 * Returns 0 for success.
3314 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
3315 struct snd_ctl_elem_value
*ucontrol
)
3317 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
3318 struct snd_soc_card
*card
= dapm
->card
;
3319 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
3320 unsigned int *item
= ucontrol
->value
.enumerated
.item
;
3321 unsigned int val
, change
, reg_change
= 0;
3323 struct snd_soc_dapm_update update
= {};
3326 if (item
[0] >= e
->items
)
3329 val
= snd_soc_enum_item_to_val(e
, item
[0]) << e
->shift_l
;
3330 mask
= e
->mask
<< e
->shift_l
;
3331 if (e
->shift_l
!= e
->shift_r
) {
3332 if (item
[1] > e
->items
)
3334 val
|= snd_soc_enum_item_to_val(e
, item
[1]) << e
->shift_r
;
3335 mask
|= e
->mask
<< e
->shift_r
;
3338 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3340 change
= dapm_kcontrol_set_value(kcontrol
, val
);
3342 if (e
->reg
!= SND_SOC_NOPM
)
3343 reg_change
= soc_dapm_test_bits(dapm
, e
->reg
, mask
, val
);
3345 if (change
|| reg_change
) {
3347 update
.kcontrol
= kcontrol
;
3348 update
.reg
= e
->reg
;
3351 card
->update
= &update
;
3353 change
|= reg_change
;
3355 ret
= soc_dapm_mux_update_power(card
, kcontrol
, item
[0], e
);
3357 card
->update
= NULL
;
3360 mutex_unlock(&card
->dapm_mutex
);
3363 soc_dpcm_runtime_update(card
);
3367 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
3370 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3372 * @kcontrol: mixer control
3373 * @uinfo: control element information
3375 * Callback to provide information about a pin switch control.
3377 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
3378 struct snd_ctl_elem_info
*uinfo
)
3380 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
3382 uinfo
->value
.integer
.min
= 0;
3383 uinfo
->value
.integer
.max
= 1;
3387 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
3390 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3392 * @kcontrol: mixer control
3395 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
3396 struct snd_ctl_elem_value
*ucontrol
)
3398 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
3399 const char *pin
= (const char *)kcontrol
->private_value
;
3401 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3403 ucontrol
->value
.integer
.value
[0] =
3404 snd_soc_dapm_get_pin_status(&card
->dapm
, pin
);
3406 mutex_unlock(&card
->dapm_mutex
);
3410 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
3413 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3415 * @kcontrol: mixer control
3418 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
3419 struct snd_ctl_elem_value
*ucontrol
)
3421 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
3422 const char *pin
= (const char *)kcontrol
->private_value
;
3424 if (ucontrol
->value
.integer
.value
[0])
3425 snd_soc_dapm_enable_pin(&card
->dapm
, pin
);
3427 snd_soc_dapm_disable_pin(&card
->dapm
, pin
);
3429 snd_soc_dapm_sync(&card
->dapm
);
3432 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
3434 struct snd_soc_dapm_widget
*
3435 snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
3436 const struct snd_soc_dapm_widget
*widget
)
3438 struct snd_soc_dapm_widget
*w
;
3440 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3441 w
= snd_soc_dapm_new_control_unlocked(dapm
, widget
);
3442 /* Do not nag about probe deferrals */
3444 int ret
= PTR_ERR(w
);
3446 if (ret
!= -EPROBE_DEFER
)
3448 "ASoC: Failed to create DAPM control %s (%d)\n",
3454 "ASoC: Failed to create DAPM control %s\n",
3458 mutex_unlock(&dapm
->card
->dapm_mutex
);
3461 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
3463 struct snd_soc_dapm_widget
*
3464 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context
*dapm
,
3465 const struct snd_soc_dapm_widget
*widget
)
3467 enum snd_soc_dapm_direction dir
;
3468 struct snd_soc_dapm_widget
*w
;
3472 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
3476 case snd_soc_dapm_regulator_supply
:
3477 w
->regulator
= devm_regulator_get(dapm
->dev
, w
->name
);
3478 if (IS_ERR(w
->regulator
)) {
3479 ret
= PTR_ERR(w
->regulator
);
3480 if (ret
== -EPROBE_DEFER
)
3481 return ERR_PTR(ret
);
3482 dev_err(dapm
->dev
, "ASoC: Failed to request %s: %d\n",
3487 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
3488 ret
= regulator_allow_bypass(w
->regulator
, true);
3490 dev_warn(w
->dapm
->dev
,
3491 "ASoC: Failed to bypass %s: %d\n",
3495 case snd_soc_dapm_pinctrl
:
3496 w
->pinctrl
= devm_pinctrl_get(dapm
->dev
);
3497 if (IS_ERR_OR_NULL(w
->pinctrl
)) {
3498 ret
= PTR_ERR(w
->pinctrl
);
3499 if (ret
== -EPROBE_DEFER
)
3500 return ERR_PTR(ret
);
3501 dev_err(dapm
->dev
, "ASoC: Failed to request %s: %d\n",
3506 case snd_soc_dapm_clock_supply
:
3507 #ifdef CONFIG_CLKDEV_LOOKUP
3508 w
->clk
= devm_clk_get(dapm
->dev
, w
->name
);
3509 if (IS_ERR(w
->clk
)) {
3510 ret
= PTR_ERR(w
->clk
);
3511 if (ret
== -EPROBE_DEFER
)
3512 return ERR_PTR(ret
);
3513 dev_err(dapm
->dev
, "ASoC: Failed to request %s: %d\n",
3525 prefix
= soc_dapm_prefix(dapm
);
3527 w
->name
= kasprintf(GFP_KERNEL
, "%s %s", prefix
, widget
->name
);
3529 w
->name
= kstrdup_const(widget
->name
, GFP_KERNEL
);
3530 if (w
->name
== NULL
) {
3536 case snd_soc_dapm_mic
:
3537 w
->is_ep
= SND_SOC_DAPM_EP_SOURCE
;
3538 w
->power_check
= dapm_generic_check_power
;
3540 case snd_soc_dapm_input
:
3541 if (!dapm
->card
->fully_routed
)
3542 w
->is_ep
= SND_SOC_DAPM_EP_SOURCE
;
3543 w
->power_check
= dapm_generic_check_power
;
3545 case snd_soc_dapm_spk
:
3546 case snd_soc_dapm_hp
:
3547 w
->is_ep
= SND_SOC_DAPM_EP_SINK
;
3548 w
->power_check
= dapm_generic_check_power
;
3550 case snd_soc_dapm_output
:
3551 if (!dapm
->card
->fully_routed
)
3552 w
->is_ep
= SND_SOC_DAPM_EP_SINK
;
3553 w
->power_check
= dapm_generic_check_power
;
3555 case snd_soc_dapm_vmid
:
3556 case snd_soc_dapm_siggen
:
3557 w
->is_ep
= SND_SOC_DAPM_EP_SOURCE
;
3558 w
->power_check
= dapm_always_on_check_power
;
3560 case snd_soc_dapm_sink
:
3561 w
->is_ep
= SND_SOC_DAPM_EP_SINK
;
3562 w
->power_check
= dapm_always_on_check_power
;
3565 case snd_soc_dapm_mux
:
3566 case snd_soc_dapm_demux
:
3567 case snd_soc_dapm_switch
:
3568 case snd_soc_dapm_mixer
:
3569 case snd_soc_dapm_mixer_named_ctl
:
3570 case snd_soc_dapm_adc
:
3571 case snd_soc_dapm_aif_out
:
3572 case snd_soc_dapm_dac
:
3573 case snd_soc_dapm_aif_in
:
3574 case snd_soc_dapm_pga
:
3575 case snd_soc_dapm_out_drv
:
3576 case snd_soc_dapm_micbias
:
3577 case snd_soc_dapm_line
:
3578 case snd_soc_dapm_dai_link
:
3579 case snd_soc_dapm_dai_out
:
3580 case snd_soc_dapm_dai_in
:
3581 w
->power_check
= dapm_generic_check_power
;
3583 case snd_soc_dapm_supply
:
3584 case snd_soc_dapm_regulator_supply
:
3585 case snd_soc_dapm_pinctrl
:
3586 case snd_soc_dapm_clock_supply
:
3587 case snd_soc_dapm_kcontrol
:
3589 w
->power_check
= dapm_supply_check_power
;
3592 w
->power_check
= dapm_always_on_check_power
;
3597 INIT_LIST_HEAD(&w
->list
);
3598 INIT_LIST_HEAD(&w
->dirty
);
3599 list_add_tail(&w
->list
, &dapm
->card
->widgets
);
3601 snd_soc_dapm_for_each_direction(dir
) {
3602 INIT_LIST_HEAD(&w
->edges
[dir
]);
3603 w
->endpoints
[dir
] = -1;
3606 /* machine layer sets up unconnected pins and insertions */
3612 * snd_soc_dapm_new_controls - create new dapm controls
3613 * @dapm: DAPM context
3614 * @widget: widget array
3615 * @num: number of widgets
3617 * Creates new DAPM controls based upon the templates.
3619 * Returns 0 for success else error.
3621 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context
*dapm
,
3622 const struct snd_soc_dapm_widget
*widget
,
3625 struct snd_soc_dapm_widget
*w
;
3629 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
3630 for (i
= 0; i
< num
; i
++) {
3631 w
= snd_soc_dapm_new_control_unlocked(dapm
, widget
);
3634 /* Do not nag about probe deferrals */
3635 if (ret
== -EPROBE_DEFER
)
3638 "ASoC: Failed to create DAPM control %s (%d)\n",
3644 "ASoC: Failed to create DAPM control %s\n",
3651 mutex_unlock(&dapm
->card
->dapm_mutex
);
3654 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
3656 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget
*w
,
3657 struct snd_kcontrol
*kcontrol
, int event
)
3659 struct snd_soc_dapm_path
*source_p
, *sink_p
;
3660 struct snd_soc_dai
*source
, *sink
;
3661 const struct snd_soc_pcm_stream
*config
= w
->params
+ w
->params_select
;
3662 struct snd_pcm_substream substream
;
3663 struct snd_pcm_hw_params
*params
= NULL
;
3664 struct snd_pcm_runtime
*runtime
= NULL
;
3668 if (WARN_ON(!config
) ||
3669 WARN_ON(list_empty(&w
->edges
[SND_SOC_DAPM_DIR_OUT
]) ||
3670 list_empty(&w
->edges
[SND_SOC_DAPM_DIR_IN
])))
3673 /* We only support a single source and sink, pick the first */
3674 source_p
= list_first_entry(&w
->edges
[SND_SOC_DAPM_DIR_OUT
],
3675 struct snd_soc_dapm_path
,
3676 list_node
[SND_SOC_DAPM_DIR_OUT
]);
3677 sink_p
= list_first_entry(&w
->edges
[SND_SOC_DAPM_DIR_IN
],
3678 struct snd_soc_dapm_path
,
3679 list_node
[SND_SOC_DAPM_DIR_IN
]);
3681 source
= source_p
->source
->priv
;
3682 sink
= sink_p
->sink
->priv
;
3684 /* Be a little careful as we don't want to overflow the mask array */
3685 if (config
->formats
) {
3686 fmt
= ffs(config
->formats
) - 1;
3688 dev_warn(w
->dapm
->dev
, "ASoC: Invalid format %llx specified\n",
3693 /* Currently very limited parameter selection */
3694 params
= kzalloc(sizeof(*params
), GFP_KERNEL
);
3699 snd_mask_set(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), fmt
);
3701 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
)->min
=
3703 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
)->max
=
3706 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
)->min
3707 = config
->channels_min
;
3708 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
)->max
3709 = config
->channels_max
;
3711 memset(&substream
, 0, sizeof(substream
));
3713 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3714 runtime
= kzalloc(sizeof(*runtime
), GFP_KERNEL
);
3719 substream
.runtime
= runtime
;
3722 case SND_SOC_DAPM_PRE_PMU
:
3723 substream
.stream
= SNDRV_PCM_STREAM_CAPTURE
;
3724 if (source
->driver
->ops
->startup
) {
3725 ret
= source
->driver
->ops
->startup(&substream
, source
);
3727 dev_err(source
->dev
,
3728 "ASoC: startup() failed: %d\n", ret
);
3733 ret
= soc_dai_hw_params(&substream
, params
, source
);
3737 substream
.stream
= SNDRV_PCM_STREAM_PLAYBACK
;
3738 if (sink
->driver
->ops
->startup
) {
3739 ret
= sink
->driver
->ops
->startup(&substream
, sink
);
3742 "ASoC: startup() failed: %d\n", ret
);
3747 ret
= soc_dai_hw_params(&substream
, params
, sink
);
3752 case SND_SOC_DAPM_POST_PMU
:
3753 ret
= snd_soc_dai_digital_mute(sink
, 0,
3754 SNDRV_PCM_STREAM_PLAYBACK
);
3755 if (ret
!= 0 && ret
!= -ENOTSUPP
)
3756 dev_warn(sink
->dev
, "ASoC: Failed to unmute: %d\n", ret
);
3760 case SND_SOC_DAPM_PRE_PMD
:
3761 ret
= snd_soc_dai_digital_mute(sink
, 1,
3762 SNDRV_PCM_STREAM_PLAYBACK
);
3763 if (ret
!= 0 && ret
!= -ENOTSUPP
)
3764 dev_warn(sink
->dev
, "ASoC: Failed to mute: %d\n", ret
);
3768 if (source
->driver
->ops
->shutdown
) {
3769 substream
.stream
= SNDRV_PCM_STREAM_CAPTURE
;
3770 source
->driver
->ops
->shutdown(&substream
, source
);
3774 if (sink
->driver
->ops
->shutdown
) {
3775 substream
.stream
= SNDRV_PCM_STREAM_PLAYBACK
;
3776 sink
->driver
->ops
->shutdown(&substream
, sink
);
3781 WARN(1, "Unknown event %d\n", event
);
3791 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol
*kcontrol
,
3792 struct snd_ctl_elem_value
*ucontrol
)
3794 struct snd_soc_dapm_widget
*w
= snd_kcontrol_chip(kcontrol
);
3796 ucontrol
->value
.enumerated
.item
[0] = w
->params_select
;
3801 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol
*kcontrol
,
3802 struct snd_ctl_elem_value
*ucontrol
)
3804 struct snd_soc_dapm_widget
*w
= snd_kcontrol_chip(kcontrol
);
3806 /* Can't change the config when widget is already powered */
3810 if (ucontrol
->value
.enumerated
.item
[0] == w
->params_select
)
3813 if (ucontrol
->value
.enumerated
.item
[0] >= w
->num_params
)
3816 w
->params_select
= ucontrol
->value
.enumerated
.item
[0];
3822 snd_soc_dapm_free_kcontrol(struct snd_soc_card
*card
,
3823 unsigned long *private_value
,
3825 const char **w_param_text
)
3829 devm_kfree(card
->dev
, (void *)*private_value
);
3830 for (count
= 0 ; count
< num_params
; count
++)
3831 devm_kfree(card
->dev
, (void *)w_param_text
[count
]);
3832 devm_kfree(card
->dev
, w_param_text
);
3835 static struct snd_kcontrol_new
*
3836 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card
*card
,
3838 const struct snd_soc_pcm_stream
*params
,
3839 int num_params
, const char **w_param_text
,
3840 unsigned long *private_value
)
3842 struct soc_enum w_param_enum
[] = {
3843 SOC_ENUM_SINGLE(0, 0, 0, NULL
),
3845 struct snd_kcontrol_new kcontrol_dai_link
[] = {
3846 SOC_ENUM_EXT(NULL
, w_param_enum
[0],
3847 snd_soc_dapm_dai_link_get
,
3848 snd_soc_dapm_dai_link_put
),
3850 struct snd_kcontrol_new
*kcontrol_news
;
3851 const struct snd_soc_pcm_stream
*config
= params
;
3854 for (count
= 0 ; count
< num_params
; count
++) {
3855 if (!config
->stream_name
) {
3856 dev_warn(card
->dapm
.dev
,
3857 "ASoC: anonymous config %d for dai link %s\n",
3859 w_param_text
[count
] =
3860 devm_kasprintf(card
->dev
, GFP_KERNEL
,
3861 "Anonymous Configuration %d",
3864 w_param_text
[count
] = devm_kmemdup(card
->dev
,
3865 config
->stream_name
,
3866 strlen(config
->stream_name
) + 1,
3869 if (!w_param_text
[count
])
3870 goto outfree_w_param
;
3874 w_param_enum
[0].items
= num_params
;
3875 w_param_enum
[0].texts
= w_param_text
;
3878 (unsigned long) devm_kmemdup(card
->dev
,
3879 (void *)(kcontrol_dai_link
[0].private_value
),
3880 sizeof(struct soc_enum
), GFP_KERNEL
);
3881 if (!*private_value
) {
3882 dev_err(card
->dev
, "ASoC: Failed to create control for %s widget\n",
3884 goto outfree_w_param
;
3886 kcontrol_dai_link
[0].private_value
= *private_value
;
3887 /* duplicate kcontrol_dai_link on heap so that memory persists */
3888 kcontrol_news
= devm_kmemdup(card
->dev
, &kcontrol_dai_link
[0],
3889 sizeof(struct snd_kcontrol_new
),
3891 if (!kcontrol_news
) {
3892 dev_err(card
->dev
, "ASoC: Failed to create control for %s widget\n",
3894 goto outfree_w_param
;
3896 return kcontrol_news
;
3899 snd_soc_dapm_free_kcontrol(card
, private_value
, num_params
, w_param_text
);
3903 int snd_soc_dapm_new_pcm(struct snd_soc_card
*card
,
3904 const struct snd_soc_pcm_stream
*params
,
3905 unsigned int num_params
,
3906 struct snd_soc_dapm_widget
*source
,
3907 struct snd_soc_dapm_widget
*sink
)
3909 struct snd_soc_dapm_widget
template;
3910 struct snd_soc_dapm_widget
*w
;
3911 const char **w_param_text
;
3912 unsigned long private_value
;
3916 link_name
= devm_kasprintf(card
->dev
, GFP_KERNEL
, "%s-%s",
3917 source
->name
, sink
->name
);
3921 memset(&template, 0, sizeof(template));
3922 template.reg
= SND_SOC_NOPM
;
3923 template.id
= snd_soc_dapm_dai_link
;
3924 template.name
= link_name
;
3925 template.event
= snd_soc_dai_link_event
;
3926 template.event_flags
= SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMU
|
3927 SND_SOC_DAPM_PRE_PMD
;
3928 template.kcontrol_news
= NULL
;
3930 /* allocate memory for control, only in case of multiple configs */
3931 if (num_params
> 1) {
3932 w_param_text
= devm_kcalloc(card
->dev
, num_params
,
3933 sizeof(char *), GFP_KERNEL
);
3934 if (!w_param_text
) {
3939 template.num_kcontrols
= 1;
3940 template.kcontrol_news
=
3941 snd_soc_dapm_alloc_kcontrol(card
,
3942 link_name
, params
, num_params
,
3943 w_param_text
, &private_value
);
3944 if (!template.kcontrol_news
) {
3949 w_param_text
= NULL
;
3951 dev_dbg(card
->dev
, "ASoC: adding %s widget\n", link_name
);
3953 w
= snd_soc_dapm_new_control_unlocked(&card
->dapm
, &template);
3956 /* Do not nag about probe deferrals */
3957 if (ret
!= -EPROBE_DEFER
)
3959 "ASoC: Failed to create %s widget (%d)\n",
3961 goto outfree_kcontrol_news
;
3964 dev_err(card
->dev
, "ASoC: Failed to create %s widget\n",
3967 goto outfree_kcontrol_news
;
3971 w
->num_params
= num_params
;
3973 ret
= snd_soc_dapm_add_path(&card
->dapm
, source
, w
, NULL
, NULL
);
3976 return snd_soc_dapm_add_path(&card
->dapm
, w
, sink
, NULL
, NULL
);
3979 devm_kfree(card
->dev
, w
);
3980 outfree_kcontrol_news
:
3981 devm_kfree(card
->dev
, (void *)template.kcontrol_news
);
3982 snd_soc_dapm_free_kcontrol(card
, &private_value
, num_params
, w_param_text
);
3984 devm_kfree(card
->dev
, link_name
);
3988 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context
*dapm
,
3989 struct snd_soc_dai
*dai
)
3991 struct snd_soc_dapm_widget
template;
3992 struct snd_soc_dapm_widget
*w
;
3994 WARN_ON(dapm
->dev
!= dai
->dev
);
3996 memset(&template, 0, sizeof(template));
3997 template.reg
= SND_SOC_NOPM
;
3999 if (dai
->driver
->playback
.stream_name
) {
4000 template.id
= snd_soc_dapm_dai_in
;
4001 template.name
= dai
->driver
->playback
.stream_name
;
4002 template.sname
= dai
->driver
->playback
.stream_name
;
4004 dev_dbg(dai
->dev
, "ASoC: adding %s widget\n",
4007 w
= snd_soc_dapm_new_control_unlocked(dapm
, &template);
4009 int ret
= PTR_ERR(w
);
4011 /* Do not nag about probe deferrals */
4012 if (ret
!= -EPROBE_DEFER
)
4014 "ASoC: Failed to create %s widget (%d)\n",
4015 dai
->driver
->playback
.stream_name
, ret
);
4019 dev_err(dapm
->dev
, "ASoC: Failed to create %s widget\n",
4020 dai
->driver
->playback
.stream_name
);
4025 dai
->playback_widget
= w
;
4028 if (dai
->driver
->capture
.stream_name
) {
4029 template.id
= snd_soc_dapm_dai_out
;
4030 template.name
= dai
->driver
->capture
.stream_name
;
4031 template.sname
= dai
->driver
->capture
.stream_name
;
4033 dev_dbg(dai
->dev
, "ASoC: adding %s widget\n",
4036 w
= snd_soc_dapm_new_control_unlocked(dapm
, &template);
4038 int ret
= PTR_ERR(w
);
4040 /* Do not nag about probe deferrals */
4041 if (ret
!= -EPROBE_DEFER
)
4043 "ASoC: Failed to create %s widget (%d)\n",
4044 dai
->driver
->playback
.stream_name
, ret
);
4048 dev_err(dapm
->dev
, "ASoC: Failed to create %s widget\n",
4049 dai
->driver
->capture
.stream_name
);
4054 dai
->capture_widget
= w
;
4060 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card
*card
)
4062 struct snd_soc_dapm_widget
*dai_w
, *w
;
4063 struct snd_soc_dapm_widget
*src
, *sink
;
4064 struct snd_soc_dai
*dai
;
4066 /* For each DAI widget... */
4067 list_for_each_entry(dai_w
, &card
->widgets
, list
) {
4068 switch (dai_w
->id
) {
4069 case snd_soc_dapm_dai_in
:
4070 case snd_soc_dapm_dai_out
:
4078 /* ...find all widgets with the same stream and link them */
4079 list_for_each_entry(w
, &card
->widgets
, list
) {
4080 if (w
->dapm
!= dai_w
->dapm
)
4084 case snd_soc_dapm_dai_in
:
4085 case snd_soc_dapm_dai_out
:
4091 if (!w
->sname
|| !strstr(w
->sname
, dai_w
->sname
))
4094 if (dai_w
->id
== snd_soc_dapm_dai_in
) {
4101 dev_dbg(dai
->dev
, "%s -> %s\n", src
->name
, sink
->name
);
4102 snd_soc_dapm_add_path(w
->dapm
, src
, sink
, NULL
, NULL
);
4109 static void dapm_connect_dai_link_widgets(struct snd_soc_card
*card
,
4110 struct snd_soc_pcm_runtime
*rtd
)
4112 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
4113 struct snd_soc_dapm_widget
*sink
, *source
;
4116 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
4117 struct snd_soc_dai
*codec_dai
= rtd
->codec_dais
[i
];
4119 /* connect BE DAI playback if widgets are valid */
4120 if (codec_dai
->playback_widget
&& cpu_dai
->playback_widget
) {
4121 source
= cpu_dai
->playback_widget
;
4122 sink
= codec_dai
->playback_widget
;
4123 dev_dbg(rtd
->dev
, "connected DAI link %s:%s -> %s:%s\n",
4124 cpu_dai
->component
->name
, source
->name
,
4125 codec_dai
->component
->name
, sink
->name
);
4127 snd_soc_dapm_add_path(&card
->dapm
, source
, sink
,
4131 /* connect BE DAI capture if widgets are valid */
4132 if (codec_dai
->capture_widget
&& cpu_dai
->capture_widget
) {
4133 source
= codec_dai
->capture_widget
;
4134 sink
= cpu_dai
->capture_widget
;
4135 dev_dbg(rtd
->dev
, "connected DAI link %s:%s -> %s:%s\n",
4136 codec_dai
->component
->name
, source
->name
,
4137 cpu_dai
->component
->name
, sink
->name
);
4139 snd_soc_dapm_add_path(&card
->dapm
, source
, sink
,
4145 static void soc_dapm_dai_stream_event(struct snd_soc_dai
*dai
, int stream
,
4148 struct snd_soc_dapm_widget
*w
;
4151 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
4152 w
= dai
->playback_widget
;
4154 w
= dai
->capture_widget
;
4157 dapm_mark_dirty(w
, "stream event");
4159 if (w
->id
== snd_soc_dapm_dai_in
) {
4160 ep
= SND_SOC_DAPM_EP_SOURCE
;
4161 dapm_widget_invalidate_input_paths(w
);
4163 ep
= SND_SOC_DAPM_EP_SINK
;
4164 dapm_widget_invalidate_output_paths(w
);
4168 case SND_SOC_DAPM_STREAM_START
:
4172 case SND_SOC_DAPM_STREAM_STOP
:
4176 case SND_SOC_DAPM_STREAM_SUSPEND
:
4177 case SND_SOC_DAPM_STREAM_RESUME
:
4178 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
4179 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
4185 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card
*card
)
4187 struct snd_soc_pcm_runtime
*rtd
;
4189 /* for each BE DAI link... */
4190 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
4192 * dynamic FE links have no fixed DAI mapping.
4193 * CODEC<->CODEC links have no direct connection.
4195 if (rtd
->dai_link
->dynamic
|| rtd
->dai_link
->params
)
4198 dapm_connect_dai_link_widgets(card
, rtd
);
4202 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
, int stream
,
4207 soc_dapm_dai_stream_event(rtd
->cpu_dai
, stream
, event
);
4208 for (i
= 0; i
< rtd
->num_codecs
; i
++)
4209 soc_dapm_dai_stream_event(rtd
->codec_dais
[i
], stream
, event
);
4211 dapm_power_widgets(rtd
->card
, event
);
4215 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4216 * @rtd: PCM runtime data
4217 * @stream: stream name
4218 * @event: stream event
4220 * Sends a stream event to the dapm core. The core then makes any
4221 * necessary widget power changes.
4223 * Returns 0 for success else error.
4225 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
, int stream
,
4228 struct snd_soc_card
*card
= rtd
->card
;
4230 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4231 soc_dapm_stream_event(rtd
, stream
, event
);
4232 mutex_unlock(&card
->dapm_mutex
);
4236 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4237 * @dapm: DAPM context
4240 * Enables input/output pin and its parents or children widgets iff there is
4241 * a valid audio route and active audio stream.
4243 * Requires external locking.
4245 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4246 * do any widget power switching.
4248 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
4251 return snd_soc_dapm_set_pin(dapm
, pin
, 1);
4253 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked
);
4256 * snd_soc_dapm_enable_pin - enable pin.
4257 * @dapm: DAPM context
4260 * Enables input/output pin and its parents or children widgets iff there is
4261 * a valid audio route and active audio stream.
4263 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4264 * do any widget power switching.
4266 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
4270 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4272 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 1);
4274 mutex_unlock(&dapm
->card
->dapm_mutex
);
4278 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
4281 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4282 * @dapm: DAPM context
4285 * Enables input/output pin regardless of any other state. This is
4286 * intended for use with microphone bias supplies used in microphone
4289 * Requires external locking.
4291 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4292 * do any widget power switching.
4294 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
4297 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
4300 dev_err(dapm
->dev
, "ASoC: unknown pin %s\n", pin
);
4304 dev_dbg(w
->dapm
->dev
, "ASoC: force enable pin %s\n", pin
);
4305 if (!w
->connected
) {
4307 * w->force does not affect the number of input or output paths,
4308 * so we only have to recheck if w->connected is changed
4310 dapm_widget_invalidate_input_paths(w
);
4311 dapm_widget_invalidate_output_paths(w
);
4315 dapm_mark_dirty(w
, "force enable");
4319 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked
);
4322 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4323 * @dapm: DAPM context
4326 * Enables input/output pin regardless of any other state. This is
4327 * intended for use with microphone bias supplies used in microphone
4330 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4331 * do any widget power switching.
4333 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context
*dapm
,
4338 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4340 ret
= snd_soc_dapm_force_enable_pin_unlocked(dapm
, pin
);
4342 mutex_unlock(&dapm
->card
->dapm_mutex
);
4346 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
4349 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4350 * @dapm: DAPM context
4353 * Disables input/output pin and its parents or children widgets.
4355 * Requires external locking.
4357 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4358 * do any widget power switching.
4360 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
4363 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
4365 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked
);
4368 * snd_soc_dapm_disable_pin - disable pin.
4369 * @dapm: DAPM context
4372 * Disables input/output pin and its parents or children widgets.
4374 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4375 * do any widget power switching.
4377 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context
*dapm
,
4382 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4384 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 0);
4386 mutex_unlock(&dapm
->card
->dapm_mutex
);
4390 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
4393 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4394 * @dapm: DAPM context
4397 * Marks the specified pin as being not connected, disabling it along
4398 * any parent or child widgets. At present this is identical to
4399 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4400 * additional things such as disabling controls which only affect
4401 * paths through the pin.
4403 * Requires external locking.
4405 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4406 * do any widget power switching.
4408 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
4411 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
4413 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked
);
4416 * snd_soc_dapm_nc_pin - permanently disable pin.
4417 * @dapm: DAPM context
4420 * Marks the specified pin as being not connected, disabling it along
4421 * any parent or child widgets. At present this is identical to
4422 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4423 * additional things such as disabling controls which only affect
4424 * paths through the pin.
4426 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4427 * do any widget power switching.
4429 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
4433 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4435 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 0);
4437 mutex_unlock(&dapm
->card
->dapm_mutex
);
4441 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
4444 * snd_soc_dapm_get_pin_status - get audio pin status
4445 * @dapm: DAPM context
4446 * @pin: audio signal pin endpoint (or start point)
4448 * Get audio pin status - connected or disconnected.
4450 * Returns 1 for connected otherwise 0.
4452 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context
*dapm
,
4455 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
4458 return w
->connected
;
4462 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
4465 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4466 * @dapm: DAPM context
4467 * @pin: audio signal pin endpoint (or start point)
4469 * Mark the given endpoint or pin as ignoring suspend. When the
4470 * system is disabled a path between two endpoints flagged as ignoring
4471 * suspend will not be disabled. The path must already be enabled via
4472 * normal means at suspend time, it will not be turned on if it was not
4475 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context
*dapm
,
4478 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, false);
4481 dev_err(dapm
->dev
, "ASoC: unknown pin %s\n", pin
);
4485 w
->ignore_suspend
= 1;
4489 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
4492 * snd_soc_dapm_free - free dapm resources
4493 * @dapm: DAPM context
4495 * Free all dapm widgets and resources.
4497 void snd_soc_dapm_free(struct snd_soc_dapm_context
*dapm
)
4499 dapm_debugfs_cleanup(dapm
);
4500 dapm_free_widgets(dapm
);
4501 list_del(&dapm
->list
);
4503 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
4505 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context
*dapm
)
4507 struct snd_soc_card
*card
= dapm
->card
;
4508 struct snd_soc_dapm_widget
*w
;
4509 LIST_HEAD(down_list
);
4512 mutex_lock(&card
->dapm_mutex
);
4514 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
4515 if (w
->dapm
!= dapm
)
4518 dapm_seq_insert(w
, &down_list
, false);
4524 /* If there were no widgets to power down we're already in
4528 if (dapm
->bias_level
== SND_SOC_BIAS_ON
)
4529 snd_soc_dapm_set_bias_level(dapm
,
4530 SND_SOC_BIAS_PREPARE
);
4531 dapm_seq_run(card
, &down_list
, 0, false);
4532 if (dapm
->bias_level
== SND_SOC_BIAS_PREPARE
)
4533 snd_soc_dapm_set_bias_level(dapm
,
4534 SND_SOC_BIAS_STANDBY
);
4537 mutex_unlock(&card
->dapm_mutex
);
4541 * snd_soc_dapm_shutdown - callback for system shutdown
4543 void snd_soc_dapm_shutdown(struct snd_soc_card
*card
)
4545 struct snd_soc_dapm_context
*dapm
;
4547 list_for_each_entry(dapm
, &card
->dapm_list
, list
) {
4548 if (dapm
!= &card
->dapm
) {
4549 soc_dapm_shutdown_dapm(dapm
);
4550 if (dapm
->bias_level
== SND_SOC_BIAS_STANDBY
)
4551 snd_soc_dapm_set_bias_level(dapm
,
4556 soc_dapm_shutdown_dapm(&card
->dapm
);
4557 if (card
->dapm
.bias_level
== SND_SOC_BIAS_STANDBY
)
4558 snd_soc_dapm_set_bias_level(&card
->dapm
,
4562 /* Module information */
4563 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4564 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4565 MODULE_LICENSE("GPL");