2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/headphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed power down of audio subsystem to reduce pops between a quick
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
29 #include <linux/async.h>
30 #include <linux/delay.h>
32 #include <linux/bitops.h>
33 #include <linux/platform_device.h>
34 #include <linux/jiffies.h>
35 #include <linux/debugfs.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/clk.h>
39 #include <linux/slab.h>
40 #include <sound/core.h>
41 #include <sound/pcm.h>
42 #include <sound/pcm_params.h>
43 #include <sound/soc.h>
44 #include <sound/initval.h>
46 #include <trace/events/asoc.h>
48 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
50 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
51 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
53 #define snd_soc_dapm_for_each_direction(dir) \
54 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
57 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context
*dapm
,
58 struct snd_soc_dapm_widget
*wsource
, struct snd_soc_dapm_widget
*wsink
,
60 int (*connected
)(struct snd_soc_dapm_widget
*source
,
61 struct snd_soc_dapm_widget
*sink
));
63 struct snd_soc_dapm_widget
*
64 snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
65 const struct snd_soc_dapm_widget
*widget
);
67 struct snd_soc_dapm_widget
*
68 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context
*dapm
,
69 const struct snd_soc_dapm_widget
*widget
);
71 /* dapm power sequences - make this per codec in the future */
72 static int dapm_up_seq
[] = {
73 [snd_soc_dapm_pre
] = 0,
74 [snd_soc_dapm_regulator_supply
] = 1,
75 [snd_soc_dapm_clock_supply
] = 1,
76 [snd_soc_dapm_supply
] = 2,
77 [snd_soc_dapm_micbias
] = 3,
78 [snd_soc_dapm_dai_link
] = 2,
79 [snd_soc_dapm_dai_in
] = 4,
80 [snd_soc_dapm_dai_out
] = 4,
81 [snd_soc_dapm_aif_in
] = 4,
82 [snd_soc_dapm_aif_out
] = 4,
83 [snd_soc_dapm_mic
] = 5,
84 [snd_soc_dapm_mux
] = 6,
85 [snd_soc_dapm_demux
] = 6,
86 [snd_soc_dapm_dac
] = 7,
87 [snd_soc_dapm_switch
] = 8,
88 [snd_soc_dapm_mixer
] = 8,
89 [snd_soc_dapm_mixer_named_ctl
] = 8,
90 [snd_soc_dapm_pga
] = 9,
91 [snd_soc_dapm_adc
] = 10,
92 [snd_soc_dapm_out_drv
] = 11,
93 [snd_soc_dapm_hp
] = 11,
94 [snd_soc_dapm_spk
] = 11,
95 [snd_soc_dapm_line
] = 11,
96 [snd_soc_dapm_kcontrol
] = 12,
97 [snd_soc_dapm_post
] = 13,
100 static int dapm_down_seq
[] = {
101 [snd_soc_dapm_pre
] = 0,
102 [snd_soc_dapm_kcontrol
] = 1,
103 [snd_soc_dapm_adc
] = 2,
104 [snd_soc_dapm_hp
] = 3,
105 [snd_soc_dapm_spk
] = 3,
106 [snd_soc_dapm_line
] = 3,
107 [snd_soc_dapm_out_drv
] = 3,
108 [snd_soc_dapm_pga
] = 4,
109 [snd_soc_dapm_switch
] = 5,
110 [snd_soc_dapm_mixer_named_ctl
] = 5,
111 [snd_soc_dapm_mixer
] = 5,
112 [snd_soc_dapm_dac
] = 6,
113 [snd_soc_dapm_mic
] = 7,
114 [snd_soc_dapm_micbias
] = 8,
115 [snd_soc_dapm_mux
] = 9,
116 [snd_soc_dapm_demux
] = 9,
117 [snd_soc_dapm_aif_in
] = 10,
118 [snd_soc_dapm_aif_out
] = 10,
119 [snd_soc_dapm_dai_in
] = 10,
120 [snd_soc_dapm_dai_out
] = 10,
121 [snd_soc_dapm_dai_link
] = 11,
122 [snd_soc_dapm_supply
] = 12,
123 [snd_soc_dapm_clock_supply
] = 13,
124 [snd_soc_dapm_regulator_supply
] = 13,
125 [snd_soc_dapm_post
] = 14,
128 static void dapm_assert_locked(struct snd_soc_dapm_context
*dapm
)
130 if (dapm
->card
&& dapm
->card
->instantiated
)
131 lockdep_assert_held(&dapm
->card
->dapm_mutex
);
134 static void pop_wait(u32 pop_time
)
137 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time
));
140 static void pop_dbg(struct device
*dev
, u32 pop_time
, const char *fmt
, ...)
148 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
153 vsnprintf(buf
, PAGE_SIZE
, fmt
, args
);
154 dev_info(dev
, "%s", buf
);
160 static bool dapm_dirty_widget(struct snd_soc_dapm_widget
*w
)
162 return !list_empty(&w
->dirty
);
165 static void dapm_mark_dirty(struct snd_soc_dapm_widget
*w
, const char *reason
)
167 dapm_assert_locked(w
->dapm
);
169 if (!dapm_dirty_widget(w
)) {
170 dev_vdbg(w
->dapm
->dev
, "Marking %s dirty due to %s\n",
172 list_add_tail(&w
->dirty
, &w
->dapm
->card
->dapm_dirty
);
177 * Common implementation for dapm_widget_invalidate_input_paths() and
178 * dapm_widget_invalidate_output_paths(). The function is inlined since the
179 * combined size of the two specialized functions is only marginally larger then
180 * the size of the generic function and at the same time the fast path of the
181 * specialized functions is significantly smaller than the generic function.
183 static __always_inline
void dapm_widget_invalidate_paths(
184 struct snd_soc_dapm_widget
*w
, enum snd_soc_dapm_direction dir
)
186 enum snd_soc_dapm_direction rdir
= SND_SOC_DAPM_DIR_REVERSE(dir
);
187 struct snd_soc_dapm_widget
*node
;
188 struct snd_soc_dapm_path
*p
;
191 dapm_assert_locked(w
->dapm
);
193 if (w
->endpoints
[dir
] == -1)
196 list_add_tail(&w
->work_list
, &list
);
197 w
->endpoints
[dir
] = -1;
199 list_for_each_entry(w
, &list
, work_list
) {
200 snd_soc_dapm_widget_for_each_path(w
, dir
, p
) {
201 if (p
->is_supply
|| p
->weak
|| !p
->connect
)
203 node
= p
->node
[rdir
];
204 if (node
->endpoints
[dir
] != -1) {
205 node
->endpoints
[dir
] = -1;
206 list_add_tail(&node
->work_list
, &list
);
213 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
215 * @w: The widget for which to invalidate the cached number of input paths
217 * Resets the cached number of inputs for the specified widget and all widgets
218 * that can be reached via outcoming paths from the widget.
220 * This function must be called if the number of output paths for a widget might
221 * have changed. E.g. if the source state of a widget changes or a path is added
222 * or activated with the widget as the sink.
224 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget
*w
)
226 dapm_widget_invalidate_paths(w
, SND_SOC_DAPM_DIR_IN
);
230 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
232 * @w: The widget for which to invalidate the cached number of output paths
234 * Resets the cached number of outputs for the specified widget and all widgets
235 * that can be reached via incoming paths from the widget.
237 * This function must be called if the number of output paths for a widget might
238 * have changed. E.g. if the sink state of a widget changes or a path is added
239 * or activated with the widget as the source.
241 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget
*w
)
243 dapm_widget_invalidate_paths(w
, SND_SOC_DAPM_DIR_OUT
);
247 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
248 * for the widgets connected to a path
249 * @p: The path to invalidate
251 * Resets the cached number of inputs for the sink of the path and the cached
252 * number of outputs for the source of the path.
254 * This function must be called when a path is added, removed or the connected
257 static void dapm_path_invalidate(struct snd_soc_dapm_path
*p
)
260 * Weak paths or supply paths do not influence the number of input or
261 * output paths of their neighbors.
263 if (p
->weak
|| p
->is_supply
)
267 * The number of connected endpoints is the sum of the number of
268 * connected endpoints of all neighbors. If a node with 0 connected
269 * endpoints is either connected or disconnected that sum won't change,
270 * so there is no need to re-check the path.
272 if (p
->source
->endpoints
[SND_SOC_DAPM_DIR_IN
] != 0)
273 dapm_widget_invalidate_input_paths(p
->sink
);
274 if (p
->sink
->endpoints
[SND_SOC_DAPM_DIR_OUT
] != 0)
275 dapm_widget_invalidate_output_paths(p
->source
);
278 void dapm_mark_endpoints_dirty(struct snd_soc_card
*card
)
280 struct snd_soc_dapm_widget
*w
;
282 mutex_lock(&card
->dapm_mutex
);
284 list_for_each_entry(w
, &card
->widgets
, list
) {
286 dapm_mark_dirty(w
, "Rechecking endpoints");
287 if (w
->is_ep
& SND_SOC_DAPM_EP_SINK
)
288 dapm_widget_invalidate_output_paths(w
);
289 if (w
->is_ep
& SND_SOC_DAPM_EP_SOURCE
)
290 dapm_widget_invalidate_input_paths(w
);
294 mutex_unlock(&card
->dapm_mutex
);
296 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty
);
298 /* create a new dapm widget */
299 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
300 const struct snd_soc_dapm_widget
*_widget
)
302 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
305 struct dapm_kcontrol_data
{
307 struct snd_soc_dapm_widget
*widget
;
308 struct list_head paths
;
309 struct snd_soc_dapm_widget_list
*wlist
;
312 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget
*widget
,
313 struct snd_kcontrol
*kcontrol
, const char *ctrl_name
)
315 struct dapm_kcontrol_data
*data
;
316 struct soc_mixer_control
*mc
;
321 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
325 INIT_LIST_HEAD(&data
->paths
);
327 switch (widget
->id
) {
328 case snd_soc_dapm_switch
:
329 case snd_soc_dapm_mixer
:
330 case snd_soc_dapm_mixer_named_ctl
:
331 mc
= (struct soc_mixer_control
*)kcontrol
->private_value
;
333 if (mc
->autodisable
&& snd_soc_volsw_is_stereo(mc
))
334 dev_warn(widget
->dapm
->dev
,
335 "ASoC: Unsupported stereo autodisable control '%s'\n",
338 if (mc
->autodisable
) {
339 struct snd_soc_dapm_widget
template;
341 name
= kasprintf(GFP_KERNEL
, "%s %s", ctrl_name
,
348 memset(&template, 0, sizeof(template));
349 template.reg
= mc
->reg
;
350 template.mask
= (1 << fls(mc
->max
)) - 1;
351 template.shift
= mc
->shift
;
353 template.off_val
= mc
->max
;
355 template.off_val
= 0;
356 template.on_val
= template.off_val
;
357 template.id
= snd_soc_dapm_kcontrol
;
358 template.name
= name
;
360 data
->value
= template.on_val
;
363 snd_soc_dapm_new_control_unlocked(widget
->dapm
,
372 case snd_soc_dapm_demux
:
373 case snd_soc_dapm_mux
:
374 e
= (struct soc_enum
*)kcontrol
->private_value
;
376 if (e
->autodisable
) {
377 struct snd_soc_dapm_widget
template;
379 name
= kasprintf(GFP_KERNEL
, "%s %s", ctrl_name
,
386 memset(&template, 0, sizeof(template));
387 template.reg
= e
->reg
;
388 template.mask
= e
->mask
<< e
->shift_l
;
389 template.shift
= e
->shift_l
;
390 template.off_val
= snd_soc_enum_item_to_val(e
, 0);
391 template.on_val
= template.off_val
;
392 template.id
= snd_soc_dapm_kcontrol
;
393 template.name
= name
;
395 data
->value
= template.on_val
;
397 data
->widget
= snd_soc_dapm_new_control_unlocked(
398 widget
->dapm
, &template);
405 snd_soc_dapm_add_path(widget
->dapm
, data
->widget
,
413 kcontrol
->private_data
= data
;
422 static void dapm_kcontrol_free(struct snd_kcontrol
*kctl
)
424 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kctl
);
429 static struct snd_soc_dapm_widget_list
*dapm_kcontrol_get_wlist(
430 const struct snd_kcontrol
*kcontrol
)
432 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
437 static int dapm_kcontrol_add_widget(struct snd_kcontrol
*kcontrol
,
438 struct snd_soc_dapm_widget
*widget
)
440 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
441 struct snd_soc_dapm_widget_list
*new_wlist
;
445 n
= data
->wlist
->num_widgets
+ 1;
449 new_wlist
= krealloc(data
->wlist
,
450 sizeof(*new_wlist
) + sizeof(widget
) * n
, GFP_KERNEL
);
454 new_wlist
->widgets
[n
- 1] = widget
;
455 new_wlist
->num_widgets
= n
;
457 data
->wlist
= new_wlist
;
462 static void dapm_kcontrol_add_path(const struct snd_kcontrol
*kcontrol
,
463 struct snd_soc_dapm_path
*path
)
465 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
467 list_add_tail(&path
->list_kcontrol
, &data
->paths
);
470 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol
*kcontrol
)
472 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
477 return data
->widget
->power
;
480 static struct list_head
*dapm_kcontrol_get_path_list(
481 const struct snd_kcontrol
*kcontrol
)
483 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
488 #define dapm_kcontrol_for_each_path(path, kcontrol) \
489 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
492 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol
*kcontrol
)
494 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
498 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value
);
500 static bool dapm_kcontrol_set_value(const struct snd_kcontrol
*kcontrol
,
503 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
505 if (data
->value
== value
)
509 data
->widget
->on_val
= value
;
517 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
519 * @kcontrol: The kcontrol
521 struct snd_soc_dapm_widget
*snd_soc_dapm_kcontrol_widget(
522 struct snd_kcontrol
*kcontrol
)
524 return dapm_kcontrol_get_wlist(kcontrol
)->widgets
[0];
526 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget
);
529 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
531 * @kcontrol: The kcontrol
533 * Note: This function must only be used on kcontrols that are known to have
534 * been registered for a CODEC. Otherwise the behaviour is undefined.
536 struct snd_soc_dapm_context
*snd_soc_dapm_kcontrol_dapm(
537 struct snd_kcontrol
*kcontrol
)
539 return dapm_kcontrol_get_wlist(kcontrol
)->widgets
[0]->dapm
;
541 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm
);
543 static void dapm_reset(struct snd_soc_card
*card
)
545 struct snd_soc_dapm_widget
*w
;
547 lockdep_assert_held(&card
->dapm_mutex
);
549 memset(&card
->dapm_stats
, 0, sizeof(card
->dapm_stats
));
551 list_for_each_entry(w
, &card
->widgets
, list
) {
552 w
->new_power
= w
->power
;
553 w
->power_checked
= false;
557 static const char *soc_dapm_prefix(struct snd_soc_dapm_context
*dapm
)
559 if (!dapm
->component
)
561 return dapm
->component
->name_prefix
;
564 static int soc_dapm_read(struct snd_soc_dapm_context
*dapm
, int reg
,
567 if (!dapm
->component
)
569 return snd_soc_component_read(dapm
->component
, reg
, value
);
572 static int soc_dapm_update_bits(struct snd_soc_dapm_context
*dapm
,
573 int reg
, unsigned int mask
, unsigned int value
)
575 if (!dapm
->component
)
577 return snd_soc_component_update_bits(dapm
->component
, reg
,
581 static int soc_dapm_test_bits(struct snd_soc_dapm_context
*dapm
,
582 int reg
, unsigned int mask
, unsigned int value
)
584 if (!dapm
->component
)
586 return snd_soc_component_test_bits(dapm
->component
, reg
, mask
, value
);
589 static void soc_dapm_async_complete(struct snd_soc_dapm_context
*dapm
)
592 snd_soc_component_async_complete(dapm
->component
);
595 static struct snd_soc_dapm_widget
*
596 dapm_wcache_lookup(struct snd_soc_dapm_wcache
*wcache
, const char *name
)
598 struct snd_soc_dapm_widget
*w
= wcache
->widget
;
599 struct list_head
*wlist
;
604 wlist
= &w
->dapm
->card
->widgets
;
606 list_for_each_entry_from(w
, wlist
, list
) {
607 if (!strcmp(name
, w
->name
))
618 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache
*wcache
,
619 struct snd_soc_dapm_widget
*w
)
625 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
626 * @dapm: The DAPM context for which to set the level
627 * @level: The level to set
629 * Forces the DAPM bias level to a specific state. It will call the bias level
630 * callback of DAPM context with the specified level. This will even happen if
631 * the context is already at the same level. Furthermore it will not go through
632 * the normal bias level sequencing, meaning any intermediate states between the
633 * current and the target state will not be entered.
635 * Note that the change in bias level is only temporary and the next time
636 * snd_soc_dapm_sync() is called the state will be set to the level as
637 * determined by the DAPM core. The function is mainly intended to be used to
638 * used during probe or resume from suspend to power up the device so
639 * initialization can be done, before the DAPM core takes over.
641 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context
*dapm
,
642 enum snd_soc_bias_level level
)
646 if (dapm
->set_bias_level
)
647 ret
= dapm
->set_bias_level(dapm
, level
);
650 dapm
->bias_level
= level
;
654 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level
);
657 * snd_soc_dapm_set_bias_level - set the bias level for the system
658 * @dapm: DAPM context
659 * @level: level to configure
661 * Configure the bias (power) levels for the SoC audio device.
663 * Returns 0 for success else error.
665 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context
*dapm
,
666 enum snd_soc_bias_level level
)
668 struct snd_soc_card
*card
= dapm
->card
;
671 trace_snd_soc_bias_level_start(card
, level
);
673 if (card
&& card
->set_bias_level
)
674 ret
= card
->set_bias_level(card
, dapm
, level
);
678 if (!card
|| dapm
!= &card
->dapm
)
679 ret
= snd_soc_dapm_force_bias_level(dapm
, level
);
684 if (card
&& card
->set_bias_level_post
)
685 ret
= card
->set_bias_level_post(card
, dapm
, level
);
687 trace_snd_soc_bias_level_done(card
, level
);
692 /* connect mux widget to its interconnecting audio paths */
693 static int dapm_connect_mux(struct snd_soc_dapm_context
*dapm
,
694 struct snd_soc_dapm_path
*path
, const char *control_name
,
695 struct snd_soc_dapm_widget
*w
)
697 const struct snd_kcontrol_new
*kcontrol
= &w
->kcontrol_news
[0];
698 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
699 unsigned int val
, item
;
702 if (e
->reg
!= SND_SOC_NOPM
) {
703 soc_dapm_read(dapm
, e
->reg
, &val
);
704 val
= (val
>> e
->shift_l
) & e
->mask
;
705 item
= snd_soc_enum_val_to_item(e
, val
);
707 /* since a virtual mux has no backing registers to
708 * decide which path to connect, it will try to match
709 * with the first enumeration. This is to ensure
710 * that the default mux choice (the first) will be
711 * correctly powered up during initialization.
716 for (i
= 0; i
< e
->items
; i
++) {
717 if (!(strcmp(control_name
, e
->texts
[i
]))) {
718 path
->name
= e
->texts
[i
];
730 /* set up initial codec paths */
731 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path
*p
, int i
,
734 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
735 p
->sink
->kcontrol_news
[i
].private_value
;
736 unsigned int reg
= mc
->reg
;
737 unsigned int shift
= mc
->shift
;
738 unsigned int max
= mc
->max
;
739 unsigned int mask
= (1 << fls(max
)) - 1;
740 unsigned int invert
= mc
->invert
;
743 if (reg
!= SND_SOC_NOPM
) {
744 soc_dapm_read(p
->sink
->dapm
, reg
, &val
);
746 * The nth_path argument allows this function to know
747 * which path of a kcontrol it is setting the initial
748 * status for. Ideally this would support any number
749 * of paths and channels. But since kcontrols only come
750 * in mono and stereo variants, we are limited to 2
753 * The following code assumes for stereo controls the
754 * first path is the left channel, and all remaining
755 * paths are the right channel.
757 if (snd_soc_volsw_is_stereo(mc
) && nth_path
> 0) {
759 soc_dapm_read(p
->sink
->dapm
, mc
->rreg
, &val
);
760 val
= (val
>> mc
->rshift
) & mask
;
762 val
= (val
>> shift
) & mask
;
772 /* connect mixer widget to its interconnecting audio paths */
773 static int dapm_connect_mixer(struct snd_soc_dapm_context
*dapm
,
774 struct snd_soc_dapm_path
*path
, const char *control_name
)
778 /* search for mixer kcontrol */
779 for (i
= 0; i
< path
->sink
->num_kcontrols
; i
++) {
780 if (!strcmp(control_name
, path
->sink
->kcontrol_news
[i
].name
)) {
781 path
->name
= path
->sink
->kcontrol_news
[i
].name
;
782 dapm_set_mixer_path_status(path
, i
, nth_path
++);
789 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context
*dapm
,
790 struct snd_soc_dapm_widget
*kcontrolw
,
791 const struct snd_kcontrol_new
*kcontrol_new
,
792 struct snd_kcontrol
**kcontrol
)
794 struct snd_soc_dapm_widget
*w
;
799 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
800 if (w
== kcontrolw
|| w
->dapm
!= kcontrolw
->dapm
)
802 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
803 if (&w
->kcontrol_news
[i
] == kcontrol_new
) {
805 *kcontrol
= w
->kcontrols
[i
];
815 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
816 * create it. Either way, add the widget into the control's widget list
818 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget
*w
,
821 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
822 struct snd_card
*card
= dapm
->card
->snd_card
;
826 struct snd_kcontrol
*kcontrol
;
827 bool wname_in_long_name
, kcname_in_long_name
;
828 char *long_name
= NULL
;
832 prefix
= soc_dapm_prefix(dapm
);
834 prefix_len
= strlen(prefix
) + 1;
838 shared
= dapm_is_shared_kcontrol(dapm
, w
, &w
->kcontrol_news
[kci
],
843 wname_in_long_name
= false;
844 kcname_in_long_name
= true;
847 case snd_soc_dapm_switch
:
848 case snd_soc_dapm_mixer
:
849 case snd_soc_dapm_pga
:
850 case snd_soc_dapm_out_drv
:
851 wname_in_long_name
= true;
852 kcname_in_long_name
= true;
854 case snd_soc_dapm_mixer_named_ctl
:
855 wname_in_long_name
= false;
856 kcname_in_long_name
= true;
858 case snd_soc_dapm_demux
:
859 case snd_soc_dapm_mux
:
860 wname_in_long_name
= true;
861 kcname_in_long_name
= false;
868 if (wname_in_long_name
&& kcname_in_long_name
) {
870 * The control will get a prefix from the control
871 * creation process but we're also using the same
872 * prefix for widgets so cut the prefix off the
873 * front of the widget name.
875 long_name
= kasprintf(GFP_KERNEL
, "%s %s",
876 w
->name
+ prefix_len
,
877 w
->kcontrol_news
[kci
].name
);
878 if (long_name
== NULL
)
882 } else if (wname_in_long_name
) {
884 name
= w
->name
+ prefix_len
;
887 name
= w
->kcontrol_news
[kci
].name
;
890 kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[kci
], NULL
, name
,
897 kcontrol
->private_free
= dapm_kcontrol_free
;
899 ret
= dapm_kcontrol_data_alloc(w
, kcontrol
, name
);
901 snd_ctl_free_one(kcontrol
);
905 ret
= snd_ctl_add(card
, kcontrol
);
908 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
914 ret
= dapm_kcontrol_add_widget(kcontrol
, w
);
916 w
->kcontrols
[kci
] = kcontrol
;
924 /* create new dapm mixer control */
925 static int dapm_new_mixer(struct snd_soc_dapm_widget
*w
)
928 struct snd_soc_dapm_path
*path
;
929 struct dapm_kcontrol_data
*data
;
932 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
934 snd_soc_dapm_widget_for_each_source_path(w
, path
) {
935 /* mixer/mux paths name must match control name */
936 if (path
->name
!= (char *)w
->kcontrol_news
[i
].name
)
939 if (!w
->kcontrols
[i
]) {
940 ret
= dapm_create_or_share_kcontrol(w
, i
);
945 dapm_kcontrol_add_path(w
->kcontrols
[i
], path
);
947 data
= snd_kcontrol_chip(w
->kcontrols
[i
]);
949 snd_soc_dapm_add_path(data
->widget
->dapm
,
959 /* create new dapm mux control */
960 static int dapm_new_mux(struct snd_soc_dapm_widget
*w
)
962 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
963 enum snd_soc_dapm_direction dir
;
964 struct snd_soc_dapm_path
*path
;
969 case snd_soc_dapm_mux
:
970 dir
= SND_SOC_DAPM_DIR_OUT
;
973 case snd_soc_dapm_demux
:
974 dir
= SND_SOC_DAPM_DIR_IN
;
981 if (w
->num_kcontrols
!= 1) {
983 "ASoC: %s %s has incorrect number of controls\n", type
,
988 if (list_empty(&w
->edges
[dir
])) {
989 dev_err(dapm
->dev
, "ASoC: %s %s has no paths\n", type
, w
->name
);
993 ret
= dapm_create_or_share_kcontrol(w
, 0);
997 snd_soc_dapm_widget_for_each_path(w
, dir
, path
) {
999 dapm_kcontrol_add_path(w
->kcontrols
[0], path
);
1005 /* create new dapm volume control */
1006 static int dapm_new_pga(struct snd_soc_dapm_widget
*w
)
1010 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
1011 ret
= dapm_create_or_share_kcontrol(w
, i
);
1019 /* create new dapm dai link control */
1020 static int dapm_new_dai_link(struct snd_soc_dapm_widget
*w
)
1023 struct snd_kcontrol
*kcontrol
;
1024 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
1025 struct snd_card
*card
= dapm
->card
->snd_card
;
1027 /* create control for links with > 1 config */
1028 if (w
->num_params
<= 1)
1032 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
1033 kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[i
], w
,
1035 ret
= snd_ctl_add(card
, kcontrol
);
1038 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1039 w
->name
, w
->kcontrol_news
[i
].name
, ret
);
1042 kcontrol
->private_data
= w
;
1043 w
->kcontrols
[i
] = kcontrol
;
1049 /* We implement power down on suspend by checking the power state of
1050 * the ALSA card - when we are suspending the ALSA state for the card
1053 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
1055 int level
= snd_power_get_state(widget
->dapm
->card
->snd_card
);
1058 case SNDRV_CTL_POWER_D3hot
:
1059 case SNDRV_CTL_POWER_D3cold
:
1060 if (widget
->ignore_suspend
)
1061 dev_dbg(widget
->dapm
->dev
, "ASoC: %s ignoring suspend\n",
1063 return widget
->ignore_suspend
;
1069 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list
**list
,
1070 struct list_head
*widgets
)
1072 struct snd_soc_dapm_widget
*w
;
1073 struct list_head
*it
;
1074 unsigned int size
= 0;
1077 list_for_each(it
, widgets
)
1080 *list
= kzalloc(sizeof(**list
) + size
* sizeof(*w
), GFP_KERNEL
);
1084 list_for_each_entry(w
, widgets
, work_list
)
1085 (*list
)->widgets
[i
++] = w
;
1087 (*list
)->num_widgets
= i
;
1093 * Common implementation for is_connected_output_ep() and
1094 * is_connected_input_ep(). The function is inlined since the combined size of
1095 * the two specialized functions is only marginally larger then the size of the
1096 * generic function and at the same time the fast path of the specialized
1097 * functions is significantly smaller than the generic function.
1099 static __always_inline
int is_connected_ep(struct snd_soc_dapm_widget
*widget
,
1100 struct list_head
*list
, enum snd_soc_dapm_direction dir
,
1101 int (*fn
)(struct snd_soc_dapm_widget
*, struct list_head
*,
1102 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*,
1103 enum snd_soc_dapm_direction
)),
1104 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*,
1105 enum snd_soc_dapm_direction
))
1107 enum snd_soc_dapm_direction rdir
= SND_SOC_DAPM_DIR_REVERSE(dir
);
1108 struct snd_soc_dapm_path
*path
;
1111 if (widget
->endpoints
[dir
] >= 0)
1112 return widget
->endpoints
[dir
];
1114 DAPM_UPDATE_STAT(widget
, path_checks
);
1116 /* do we need to add this widget to the list ? */
1118 list_add_tail(&widget
->work_list
, list
);
1120 if (custom_stop_condition
&& custom_stop_condition(widget
, dir
)) {
1121 widget
->endpoints
[dir
] = 1;
1122 return widget
->endpoints
[dir
];
1125 if ((widget
->is_ep
& SND_SOC_DAPM_DIR_TO_EP(dir
)) && widget
->connected
) {
1126 widget
->endpoints
[dir
] = snd_soc_dapm_suspend_check(widget
);
1127 return widget
->endpoints
[dir
];
1130 snd_soc_dapm_widget_for_each_path(widget
, rdir
, path
) {
1131 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
1133 if (path
->weak
|| path
->is_supply
)
1139 trace_snd_soc_dapm_path(widget
, dir
, path
);
1141 if (path
->connect
) {
1143 con
+= fn(path
->node
[dir
], list
, custom_stop_condition
);
1148 widget
->endpoints
[dir
] = con
;
1154 * Recursively check for a completed path to an active or physically connected
1155 * output widget. Returns number of complete paths.
1157 * Optionally, can be supplied with a function acting as a stopping condition.
1158 * This function takes the dapm widget currently being examined and the walk
1159 * direction as an arguments, it should return true if the walk should be
1160 * stopped and false otherwise.
1162 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
,
1163 struct list_head
*list
,
1164 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*i
,
1165 enum snd_soc_dapm_direction
))
1167 return is_connected_ep(widget
, list
, SND_SOC_DAPM_DIR_OUT
,
1168 is_connected_output_ep
, custom_stop_condition
);
1172 * Recursively check for a completed path to an active or physically connected
1173 * input widget. Returns number of complete paths.
1175 * Optionally, can be supplied with a function acting as a stopping condition.
1176 * This function takes the dapm widget currently being examined and the walk
1177 * direction as an arguments, it should return true if the walk should be
1178 * stopped and false otherwise.
1180 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
,
1181 struct list_head
*list
,
1182 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*i
,
1183 enum snd_soc_dapm_direction
))
1185 return is_connected_ep(widget
, list
, SND_SOC_DAPM_DIR_IN
,
1186 is_connected_input_ep
, custom_stop_condition
);
1190 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1191 * @dai: the soc DAI.
1192 * @stream: stream direction.
1193 * @list: list of active widgets for this stream.
1194 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1195 * walk based on custom logic.
1197 * Queries DAPM graph as to whether a valid audio stream path exists for
1198 * the initial stream specified by name. This takes into account
1199 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1201 * Optionally, can be supplied with a function acting as a stopping condition.
1202 * This function takes the dapm widget currently being examined and the walk
1203 * direction as an arguments, it should return true if the walk should be
1204 * stopped and false otherwise.
1206 * Returns the number of valid paths or negative error.
1208 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai
*dai
, int stream
,
1209 struct snd_soc_dapm_widget_list
**list
,
1210 bool (*custom_stop_condition
)(struct snd_soc_dapm_widget
*,
1211 enum snd_soc_dapm_direction
))
1213 struct snd_soc_card
*card
= dai
->component
->card
;
1214 struct snd_soc_dapm_widget
*w
;
1219 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
1222 * For is_connected_{output,input}_ep fully discover the graph we need
1223 * to reset the cached number of inputs and outputs.
1225 list_for_each_entry(w
, &card
->widgets
, list
) {
1226 w
->endpoints
[SND_SOC_DAPM_DIR_IN
] = -1;
1227 w
->endpoints
[SND_SOC_DAPM_DIR_OUT
] = -1;
1230 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1231 paths
= is_connected_output_ep(dai
->playback_widget
, &widgets
,
1232 custom_stop_condition
);
1234 paths
= is_connected_input_ep(dai
->capture_widget
, &widgets
,
1235 custom_stop_condition
);
1237 /* Drop starting point */
1238 list_del(widgets
.next
);
1240 ret
= dapm_widget_list_create(list
, &widgets
);
1244 trace_snd_soc_dapm_connected(paths
, stream
);
1245 mutex_unlock(&card
->dapm_mutex
);
1251 * Handler for regulator supply widget.
1253 int dapm_regulator_event(struct snd_soc_dapm_widget
*w
,
1254 struct snd_kcontrol
*kcontrol
, int event
)
1258 soc_dapm_async_complete(w
->dapm
);
1260 if (SND_SOC_DAPM_EVENT_ON(event
)) {
1261 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
1262 ret
= regulator_allow_bypass(w
->regulator
, false);
1264 dev_warn(w
->dapm
->dev
,
1265 "ASoC: Failed to unbypass %s: %d\n",
1269 return regulator_enable(w
->regulator
);
1271 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
1272 ret
= regulator_allow_bypass(w
->regulator
, true);
1274 dev_warn(w
->dapm
->dev
,
1275 "ASoC: Failed to bypass %s: %d\n",
1279 return regulator_disable_deferred(w
->regulator
, w
->shift
);
1282 EXPORT_SYMBOL_GPL(dapm_regulator_event
);
1285 * Handler for clock supply widget.
1287 int dapm_clock_event(struct snd_soc_dapm_widget
*w
,
1288 struct snd_kcontrol
*kcontrol
, int event
)
1293 soc_dapm_async_complete(w
->dapm
);
1295 #ifdef CONFIG_HAVE_CLK
1296 if (SND_SOC_DAPM_EVENT_ON(event
)) {
1297 return clk_prepare_enable(w
->clk
);
1299 clk_disable_unprepare(w
->clk
);
1305 EXPORT_SYMBOL_GPL(dapm_clock_event
);
1307 static int dapm_widget_power_check(struct snd_soc_dapm_widget
*w
)
1309 if (w
->power_checked
)
1310 return w
->new_power
;
1315 w
->new_power
= w
->power_check(w
);
1317 w
->power_checked
= true;
1319 return w
->new_power
;
1322 /* Generic check to see if a widget should be powered. */
1323 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
1327 DAPM_UPDATE_STAT(w
, power_checks
);
1329 in
= is_connected_input_ep(w
, NULL
, NULL
);
1330 out
= is_connected_output_ep(w
, NULL
, NULL
);
1331 return out
!= 0 && in
!= 0;
1334 /* Check to see if a power supply is needed */
1335 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
1337 struct snd_soc_dapm_path
*path
;
1339 DAPM_UPDATE_STAT(w
, power_checks
);
1341 /* Check if one of our outputs is connected */
1342 snd_soc_dapm_widget_for_each_sink_path(w
, path
) {
1343 DAPM_UPDATE_STAT(w
, neighbour_checks
);
1348 if (path
->connected
&&
1349 !path
->connected(path
->source
, path
->sink
))
1352 if (dapm_widget_power_check(path
->sink
))
1359 static int dapm_always_on_check_power(struct snd_soc_dapm_widget
*w
)
1361 return w
->connected
;
1364 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
1365 struct snd_soc_dapm_widget
*b
,
1373 sort
= dapm_down_seq
;
1375 if (sort
[a
->id
] != sort
[b
->id
])
1376 return sort
[a
->id
] - sort
[b
->id
];
1377 if (a
->subseq
!= b
->subseq
) {
1379 return a
->subseq
- b
->subseq
;
1381 return b
->subseq
- a
->subseq
;
1383 if (a
->reg
!= b
->reg
)
1384 return a
->reg
- b
->reg
;
1385 if (a
->dapm
!= b
->dapm
)
1386 return (unsigned long)a
->dapm
- (unsigned long)b
->dapm
;
1391 /* Insert a widget in order into a DAPM power sequence. */
1392 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
1393 struct list_head
*list
,
1396 struct snd_soc_dapm_widget
*w
;
1398 list_for_each_entry(w
, list
, power_list
)
1399 if (dapm_seq_compare(new_widget
, w
, power_up
) < 0) {
1400 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
1404 list_add_tail(&new_widget
->power_list
, list
);
1407 static void dapm_seq_check_event(struct snd_soc_card
*card
,
1408 struct snd_soc_dapm_widget
*w
, int event
)
1410 const char *ev_name
;
1414 case SND_SOC_DAPM_PRE_PMU
:
1415 ev_name
= "PRE_PMU";
1418 case SND_SOC_DAPM_POST_PMU
:
1419 ev_name
= "POST_PMU";
1422 case SND_SOC_DAPM_PRE_PMD
:
1423 ev_name
= "PRE_PMD";
1426 case SND_SOC_DAPM_POST_PMD
:
1427 ev_name
= "POST_PMD";
1430 case SND_SOC_DAPM_WILL_PMU
:
1431 ev_name
= "WILL_PMU";
1434 case SND_SOC_DAPM_WILL_PMD
:
1435 ev_name
= "WILL_PMD";
1439 WARN(1, "Unknown event %d\n", event
);
1443 if (w
->new_power
!= power
)
1446 if (w
->event
&& (w
->event_flags
& event
)) {
1447 pop_dbg(w
->dapm
->dev
, card
->pop_time
, "pop test : %s %s\n",
1449 soc_dapm_async_complete(w
->dapm
);
1450 trace_snd_soc_dapm_widget_event_start(w
, event
);
1451 ret
= w
->event(w
, NULL
, event
);
1452 trace_snd_soc_dapm_widget_event_done(w
, event
);
1454 dev_err(w
->dapm
->dev
, "ASoC: %s: %s event failed: %d\n",
1455 ev_name
, w
->name
, ret
);
1459 /* Apply the coalesced changes from a DAPM sequence */
1460 static void dapm_seq_run_coalesced(struct snd_soc_card
*card
,
1461 struct list_head
*pending
)
1463 struct snd_soc_dapm_context
*dapm
;
1464 struct snd_soc_dapm_widget
*w
;
1466 unsigned int value
= 0;
1467 unsigned int mask
= 0;
1469 w
= list_first_entry(pending
, struct snd_soc_dapm_widget
, power_list
);
1473 list_for_each_entry(w
, pending
, power_list
) {
1474 WARN_ON(reg
!= w
->reg
|| dapm
!= w
->dapm
);
1475 w
->power
= w
->new_power
;
1477 mask
|= w
->mask
<< w
->shift
;
1479 value
|= w
->on_val
<< w
->shift
;
1481 value
|= w
->off_val
<< w
->shift
;
1483 pop_dbg(dapm
->dev
, card
->pop_time
,
1484 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1485 w
->name
, reg
, value
, mask
);
1487 /* Check for events */
1488 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_PRE_PMU
);
1489 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_PRE_PMD
);
1493 /* Any widget will do, they should all be updating the
1497 pop_dbg(dapm
->dev
, card
->pop_time
,
1498 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1499 value
, mask
, reg
, card
->pop_time
);
1500 pop_wait(card
->pop_time
);
1501 soc_dapm_update_bits(dapm
, reg
, mask
, value
);
1504 list_for_each_entry(w
, pending
, power_list
) {
1505 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_POST_PMU
);
1506 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_POST_PMD
);
1510 /* Apply a DAPM power sequence.
1512 * We walk over a pre-sorted list of widgets to apply power to. In
1513 * order to minimise the number of writes to the device required
1514 * multiple widgets will be updated in a single write where possible.
1515 * Currently anything that requires more than a single write is not
1518 static void dapm_seq_run(struct snd_soc_card
*card
,
1519 struct list_head
*list
, int event
, bool power_up
)
1521 struct snd_soc_dapm_widget
*w
, *n
;
1522 struct snd_soc_dapm_context
*d
;
1525 int cur_subseq
= -1;
1526 int cur_reg
= SND_SOC_NOPM
;
1527 struct snd_soc_dapm_context
*cur_dapm
= NULL
;
1534 sort
= dapm_down_seq
;
1536 list_for_each_entry_safe(w
, n
, list
, power_list
) {
1539 /* Do we need to apply any queued changes? */
1540 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
||
1541 w
->dapm
!= cur_dapm
|| w
->subseq
!= cur_subseq
) {
1542 if (!list_empty(&pending
))
1543 dapm_seq_run_coalesced(card
, &pending
);
1545 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1546 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1547 if (sort
[i
] == cur_sort
)
1548 cur_dapm
->seq_notifier(cur_dapm
,
1553 if (cur_dapm
&& w
->dapm
!= cur_dapm
)
1554 soc_dapm_async_complete(cur_dapm
);
1556 INIT_LIST_HEAD(&pending
);
1558 cur_subseq
= INT_MIN
;
1559 cur_reg
= SND_SOC_NOPM
;
1564 case snd_soc_dapm_pre
:
1566 list_for_each_entry_safe_continue(w
, n
, list
,
1569 if (event
== SND_SOC_DAPM_STREAM_START
)
1571 NULL
, SND_SOC_DAPM_PRE_PMU
);
1572 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1574 NULL
, SND_SOC_DAPM_PRE_PMD
);
1577 case snd_soc_dapm_post
:
1579 list_for_each_entry_safe_continue(w
, n
, list
,
1582 if (event
== SND_SOC_DAPM_STREAM_START
)
1584 NULL
, SND_SOC_DAPM_POST_PMU
);
1585 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1587 NULL
, SND_SOC_DAPM_POST_PMD
);
1591 /* Queue it up for application */
1592 cur_sort
= sort
[w
->id
];
1593 cur_subseq
= w
->subseq
;
1596 list_move(&w
->power_list
, &pending
);
1601 dev_err(w
->dapm
->dev
,
1602 "ASoC: Failed to apply widget power: %d\n", ret
);
1605 if (!list_empty(&pending
))
1606 dapm_seq_run_coalesced(card
, &pending
);
1608 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1609 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1610 if (sort
[i
] == cur_sort
)
1611 cur_dapm
->seq_notifier(cur_dapm
,
1615 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1616 soc_dapm_async_complete(d
);
1620 static void dapm_widget_update(struct snd_soc_card
*card
)
1622 struct snd_soc_dapm_update
*update
= card
->update
;
1623 struct snd_soc_dapm_widget_list
*wlist
;
1624 struct snd_soc_dapm_widget
*w
= NULL
;
1628 if (!update
|| !dapm_kcontrol_is_powered(update
->kcontrol
))
1631 wlist
= dapm_kcontrol_get_wlist(update
->kcontrol
);
1633 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
1634 w
= wlist
->widgets
[wi
];
1636 if (w
->event
&& (w
->event_flags
& SND_SOC_DAPM_PRE_REG
)) {
1637 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_PRE_REG
);
1639 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM pre-event failed: %d\n",
1647 ret
= soc_dapm_update_bits(w
->dapm
, update
->reg
, update
->mask
,
1650 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM update failed: %d\n",
1653 if (update
->has_second_set
) {
1654 ret
= soc_dapm_update_bits(w
->dapm
, update
->reg2
,
1655 update
->mask2
, update
->val2
);
1657 dev_err(w
->dapm
->dev
,
1658 "ASoC: %s DAPM update failed: %d\n",
1662 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
1663 w
= wlist
->widgets
[wi
];
1665 if (w
->event
&& (w
->event_flags
& SND_SOC_DAPM_POST_REG
)) {
1666 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_POST_REG
);
1668 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM post-event failed: %d\n",
1674 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1675 * they're changing state.
1677 static void dapm_pre_sequence_async(void *data
, async_cookie_t cookie
)
1679 struct snd_soc_dapm_context
*d
= data
;
1682 /* If we're off and we're not supposed to go into STANDBY */
1683 if (d
->bias_level
== SND_SOC_BIAS_OFF
&&
1684 d
->target_bias_level
!= SND_SOC_BIAS_OFF
) {
1686 pm_runtime_get_sync(d
->dev
);
1688 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1691 "ASoC: Failed to turn on bias: %d\n", ret
);
1694 /* Prepare for a transition to ON or away from ON */
1695 if ((d
->target_bias_level
== SND_SOC_BIAS_ON
&&
1696 d
->bias_level
!= SND_SOC_BIAS_ON
) ||
1697 (d
->target_bias_level
!= SND_SOC_BIAS_ON
&&
1698 d
->bias_level
== SND_SOC_BIAS_ON
)) {
1699 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_PREPARE
);
1702 "ASoC: Failed to prepare bias: %d\n", ret
);
1706 /* Async callback run prior to DAPM sequences - brings to their final
1709 static void dapm_post_sequence_async(void *data
, async_cookie_t cookie
)
1711 struct snd_soc_dapm_context
*d
= data
;
1714 /* If we just powered the last thing off drop to standby bias */
1715 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1716 (d
->target_bias_level
== SND_SOC_BIAS_STANDBY
||
1717 d
->target_bias_level
== SND_SOC_BIAS_OFF
)) {
1718 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1720 dev_err(d
->dev
, "ASoC: Failed to apply standby bias: %d\n",
1724 /* If we're in standby and can support bias off then do that */
1725 if (d
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1726 d
->target_bias_level
== SND_SOC_BIAS_OFF
) {
1727 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_OFF
);
1729 dev_err(d
->dev
, "ASoC: Failed to turn off bias: %d\n",
1733 pm_runtime_put(d
->dev
);
1736 /* If we just powered up then move to active bias */
1737 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1738 d
->target_bias_level
== SND_SOC_BIAS_ON
) {
1739 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_ON
);
1741 dev_err(d
->dev
, "ASoC: Failed to apply active bias: %d\n",
1746 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget
*peer
,
1747 bool power
, bool connect
)
1749 /* If a connection is being made or broken then that update
1750 * will have marked the peer dirty, otherwise the widgets are
1751 * not connected and this update has no impact. */
1755 /* If the peer is already in the state we're moving to then we
1756 * won't have an impact on it. */
1757 if (power
!= peer
->power
)
1758 dapm_mark_dirty(peer
, "peer state change");
1761 static void dapm_widget_set_power(struct snd_soc_dapm_widget
*w
, bool power
,
1762 struct list_head
*up_list
,
1763 struct list_head
*down_list
)
1765 struct snd_soc_dapm_path
*path
;
1767 if (w
->power
== power
)
1770 trace_snd_soc_dapm_widget_power(w
, power
);
1772 /* If we changed our power state perhaps our neigbours changed
1775 snd_soc_dapm_widget_for_each_source_path(w
, path
)
1776 dapm_widget_set_peer_power(path
->source
, power
, path
->connect
);
1778 /* Supplies can't affect their outputs, only their inputs */
1779 if (!w
->is_supply
) {
1780 snd_soc_dapm_widget_for_each_sink_path(w
, path
)
1781 dapm_widget_set_peer_power(path
->sink
, power
,
1786 dapm_seq_insert(w
, up_list
, true);
1788 dapm_seq_insert(w
, down_list
, false);
1791 static void dapm_power_one_widget(struct snd_soc_dapm_widget
*w
,
1792 struct list_head
*up_list
,
1793 struct list_head
*down_list
)
1798 case snd_soc_dapm_pre
:
1799 dapm_seq_insert(w
, down_list
, false);
1801 case snd_soc_dapm_post
:
1802 dapm_seq_insert(w
, up_list
, true);
1806 power
= dapm_widget_power_check(w
);
1808 dapm_widget_set_power(w
, power
, up_list
, down_list
);
1813 static bool dapm_idle_bias_off(struct snd_soc_dapm_context
*dapm
)
1815 if (dapm
->idle_bias_off
)
1818 switch (snd_power_get_state(dapm
->card
->snd_card
)) {
1819 case SNDRV_CTL_POWER_D3hot
:
1820 case SNDRV_CTL_POWER_D3cold
:
1821 return dapm
->suspend_bias_off
;
1830 * Scan each dapm widget for complete audio path.
1831 * A complete path is a route that has valid endpoints i.e.:-
1833 * o DAC to output pin.
1834 * o Input pin to ADC.
1835 * o Input pin to Output pin (bypass, sidetone)
1836 * o DAC to ADC (loopback).
1838 static int dapm_power_widgets(struct snd_soc_card
*card
, int event
)
1840 struct snd_soc_dapm_widget
*w
;
1841 struct snd_soc_dapm_context
*d
;
1843 LIST_HEAD(down_list
);
1844 ASYNC_DOMAIN_EXCLUSIVE(async_domain
);
1845 enum snd_soc_bias_level bias
;
1847 lockdep_assert_held(&card
->dapm_mutex
);
1849 trace_snd_soc_dapm_start(card
);
1851 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1852 if (dapm_idle_bias_off(d
))
1853 d
->target_bias_level
= SND_SOC_BIAS_OFF
;
1855 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1860 /* Check which widgets we need to power and store them in
1861 * lists indicating if they should be powered up or down. We
1862 * only check widgets that have been flagged as dirty but note
1863 * that new widgets may be added to the dirty list while we
1866 list_for_each_entry(w
, &card
->dapm_dirty
, dirty
) {
1867 dapm_power_one_widget(w
, &up_list
, &down_list
);
1870 list_for_each_entry(w
, &card
->widgets
, list
) {
1872 case snd_soc_dapm_pre
:
1873 case snd_soc_dapm_post
:
1874 /* These widgets always need to be powered */
1877 list_del_init(&w
->dirty
);
1884 /* Supplies and micbiases only bring the
1885 * context up to STANDBY as unless something
1886 * else is active and passing audio they
1887 * generally don't require full power. Signal
1888 * generators are virtual pins and have no
1889 * power impact themselves.
1892 case snd_soc_dapm_siggen
:
1893 case snd_soc_dapm_vmid
:
1895 case snd_soc_dapm_supply
:
1896 case snd_soc_dapm_regulator_supply
:
1897 case snd_soc_dapm_clock_supply
:
1898 case snd_soc_dapm_micbias
:
1899 if (d
->target_bias_level
< SND_SOC_BIAS_STANDBY
)
1900 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1903 d
->target_bias_level
= SND_SOC_BIAS_ON
;
1910 /* Force all contexts in the card to the same bias state if
1911 * they're not ground referenced.
1913 bias
= SND_SOC_BIAS_OFF
;
1914 list_for_each_entry(d
, &card
->dapm_list
, list
)
1915 if (d
->target_bias_level
> bias
)
1916 bias
= d
->target_bias_level
;
1917 list_for_each_entry(d
, &card
->dapm_list
, list
)
1918 if (!dapm_idle_bias_off(d
))
1919 d
->target_bias_level
= bias
;
1921 trace_snd_soc_dapm_walk_done(card
);
1923 /* Run card bias changes at first */
1924 dapm_pre_sequence_async(&card
->dapm
, 0);
1925 /* Run other bias changes in parallel */
1926 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1927 if (d
!= &card
->dapm
)
1928 async_schedule_domain(dapm_pre_sequence_async
, d
,
1931 async_synchronize_full_domain(&async_domain
);
1933 list_for_each_entry(w
, &down_list
, power_list
) {
1934 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_WILL_PMD
);
1937 list_for_each_entry(w
, &up_list
, power_list
) {
1938 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_WILL_PMU
);
1941 /* Power down widgets first; try to avoid amplifying pops. */
1942 dapm_seq_run(card
, &down_list
, event
, false);
1944 dapm_widget_update(card
);
1947 dapm_seq_run(card
, &up_list
, event
, true);
1949 /* Run all the bias changes in parallel */
1950 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1951 if (d
!= &card
->dapm
)
1952 async_schedule_domain(dapm_post_sequence_async
, d
,
1955 async_synchronize_full_domain(&async_domain
);
1956 /* Run card bias changes at last */
1957 dapm_post_sequence_async(&card
->dapm
, 0);
1959 /* do we need to notify any clients that DAPM event is complete */
1960 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1961 if (d
->stream_event
)
1962 d
->stream_event(d
, event
);
1965 pop_dbg(card
->dev
, card
->pop_time
,
1966 "DAPM sequencing finished, waiting %dms\n", card
->pop_time
);
1967 pop_wait(card
->pop_time
);
1969 trace_snd_soc_dapm_done(card
);
1974 #ifdef CONFIG_DEBUG_FS
1975 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1976 char __user
*user_buf
,
1977 size_t count
, loff_t
*ppos
)
1979 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1980 struct snd_soc_card
*card
= w
->dapm
->card
;
1981 enum snd_soc_dapm_direction dir
, rdir
;
1985 struct snd_soc_dapm_path
*p
= NULL
;
1987 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1991 mutex_lock(&card
->dapm_mutex
);
1993 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
1998 in
= is_connected_input_ep(w
, NULL
, NULL
);
1999 out
= is_connected_output_ep(w
, NULL
, NULL
);
2002 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s%s in %d out %d",
2003 w
->name
, w
->power
? "On" : "Off",
2004 w
->force
? " (forced)" : "", in
, out
);
2007 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
2008 " - R%d(0x%x) mask 0x%x",
2009 w
->reg
, w
->reg
, w
->mask
<< w
->shift
);
2011 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
2014 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
2016 w
->active
? "active" : "inactive");
2018 snd_soc_dapm_for_each_direction(dir
) {
2019 rdir
= SND_SOC_DAPM_DIR_REVERSE(dir
);
2020 snd_soc_dapm_widget_for_each_path(w
, dir
, p
) {
2021 if (p
->connected
&& !p
->connected(w
, p
->node
[rdir
]))
2027 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
2028 " %s \"%s\" \"%s\"\n",
2029 (rdir
== SND_SOC_DAPM_DIR_IN
) ? "in" : "out",
2030 p
->name
? p
->name
: "static",
2031 p
->node
[rdir
]->name
);
2035 mutex_unlock(&card
->dapm_mutex
);
2037 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
2043 static const struct file_operations dapm_widget_power_fops
= {
2044 .open
= simple_open
,
2045 .read
= dapm_widget_power_read_file
,
2046 .llseek
= default_llseek
,
2049 static ssize_t
dapm_bias_read_file(struct file
*file
, char __user
*user_buf
,
2050 size_t count
, loff_t
*ppos
)
2052 struct snd_soc_dapm_context
*dapm
= file
->private_data
;
2055 switch (dapm
->bias_level
) {
2056 case SND_SOC_BIAS_ON
:
2059 case SND_SOC_BIAS_PREPARE
:
2060 level
= "Prepare\n";
2062 case SND_SOC_BIAS_STANDBY
:
2063 level
= "Standby\n";
2065 case SND_SOC_BIAS_OFF
:
2069 WARN(1, "Unknown bias_level %d\n", dapm
->bias_level
);
2070 level
= "Unknown\n";
2074 return simple_read_from_buffer(user_buf
, count
, ppos
, level
,
2078 static const struct file_operations dapm_bias_fops
= {
2079 .open
= simple_open
,
2080 .read
= dapm_bias_read_file
,
2081 .llseek
= default_llseek
,
2084 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
2085 struct dentry
*parent
)
2092 dapm
->debugfs_dapm
= debugfs_create_dir("dapm", parent
);
2094 if (!dapm
->debugfs_dapm
) {
2096 "ASoC: Failed to create DAPM debugfs directory\n");
2100 d
= debugfs_create_file("bias_level", 0444,
2101 dapm
->debugfs_dapm
, dapm
,
2105 "ASoC: Failed to create bias level debugfs file\n");
2108 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
2110 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
2113 if (!dapm
->debugfs_dapm
|| !w
->name
)
2116 d
= debugfs_create_file(w
->name
, 0444,
2117 dapm
->debugfs_dapm
, w
,
2118 &dapm_widget_power_fops
);
2120 dev_warn(w
->dapm
->dev
,
2121 "ASoC: Failed to create %s debugfs file\n",
2125 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
2127 debugfs_remove_recursive(dapm
->debugfs_dapm
);
2131 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
2132 struct dentry
*parent
)
2136 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
2140 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
2147 * soc_dapm_connect_path() - Connects or disconnects a path
2148 * @path: The path to update
2149 * @connect: The new connect state of the path. True if the path is connected,
2150 * false if it is disconnected.
2151 * @reason: The reason why the path changed (for debugging only)
2153 static void soc_dapm_connect_path(struct snd_soc_dapm_path
*path
,
2154 bool connect
, const char *reason
)
2156 if (path
->connect
== connect
)
2159 path
->connect
= connect
;
2160 dapm_mark_dirty(path
->source
, reason
);
2161 dapm_mark_dirty(path
->sink
, reason
);
2162 dapm_path_invalidate(path
);
2165 /* test and update the power status of a mux widget */
2166 static int soc_dapm_mux_update_power(struct snd_soc_card
*card
,
2167 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
)
2169 struct snd_soc_dapm_path
*path
;
2173 lockdep_assert_held(&card
->dapm_mutex
);
2175 /* find dapm widget path assoc with kcontrol */
2176 dapm_kcontrol_for_each_path(path
, kcontrol
) {
2178 /* we now need to match the string in the enum to the path */
2179 if (!(strcmp(path
->name
, e
->texts
[mux
])))
2184 soc_dapm_connect_path(path
, connect
, "mux update");
2188 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2193 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context
*dapm
,
2194 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
,
2195 struct snd_soc_dapm_update
*update
)
2197 struct snd_soc_card
*card
= dapm
->card
;
2200 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2201 card
->update
= update
;
2202 ret
= soc_dapm_mux_update_power(card
, kcontrol
, mux
, e
);
2203 card
->update
= NULL
;
2204 mutex_unlock(&card
->dapm_mutex
);
2206 soc_dpcm_runtime_update(card
);
2209 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power
);
2211 /* test and update the power status of a mixer or switch widget */
2212 static int soc_dapm_mixer_update_power(struct snd_soc_card
*card
,
2213 struct snd_kcontrol
*kcontrol
,
2214 int connect
, int rconnect
)
2216 struct snd_soc_dapm_path
*path
;
2219 lockdep_assert_held(&card
->dapm_mutex
);
2221 /* find dapm widget path assoc with kcontrol */
2222 dapm_kcontrol_for_each_path(path
, kcontrol
) {
2224 * Ideally this function should support any number of
2225 * paths and channels. But since kcontrols only come
2226 * in mono and stereo variants, we are limited to 2
2229 * The following code assumes for stereo controls the
2230 * first path (when 'found == 0') is the left channel,
2231 * and all remaining paths (when 'found == 1') are the
2234 * A stereo control is signified by a valid 'rconnect'
2235 * value, either 0 for unconnected, or >= 0 for connected.
2236 * This is chosen instead of using snd_soc_volsw_is_stereo,
2237 * so that the behavior of snd_soc_dapm_mixer_update_power
2238 * doesn't change even when the kcontrol passed in is
2241 * It passes 'connect' as the path connect status for
2242 * the left channel, and 'rconnect' for the right
2245 if (found
&& rconnect
>= 0)
2246 soc_dapm_connect_path(path
, rconnect
, "mixer update");
2248 soc_dapm_connect_path(path
, connect
, "mixer update");
2253 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2258 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context
*dapm
,
2259 struct snd_kcontrol
*kcontrol
, int connect
,
2260 struct snd_soc_dapm_update
*update
)
2262 struct snd_soc_card
*card
= dapm
->card
;
2265 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2266 card
->update
= update
;
2267 ret
= soc_dapm_mixer_update_power(card
, kcontrol
, connect
, -1);
2268 card
->update
= NULL
;
2269 mutex_unlock(&card
->dapm_mutex
);
2271 soc_dpcm_runtime_update(card
);
2274 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power
);
2276 static ssize_t
dapm_widget_show_component(struct snd_soc_component
*cmpnt
,
2279 struct snd_soc_dapm_context
*dapm
= snd_soc_component_get_dapm(cmpnt
);
2280 struct snd_soc_dapm_widget
*w
;
2282 char *state
= "not set";
2284 /* card won't be set for the dummy component, as a spot fix
2285 * we're checking for that case specifically here but in future
2286 * we will ensure that the dummy component looks like others.
2291 list_for_each_entry(w
, &cmpnt
->card
->widgets
, list
) {
2292 if (w
->dapm
!= dapm
)
2295 /* only display widgets that burn power */
2297 case snd_soc_dapm_hp
:
2298 case snd_soc_dapm_mic
:
2299 case snd_soc_dapm_spk
:
2300 case snd_soc_dapm_line
:
2301 case snd_soc_dapm_micbias
:
2302 case snd_soc_dapm_dac
:
2303 case snd_soc_dapm_adc
:
2304 case snd_soc_dapm_pga
:
2305 case snd_soc_dapm_out_drv
:
2306 case snd_soc_dapm_mixer
:
2307 case snd_soc_dapm_mixer_named_ctl
:
2308 case snd_soc_dapm_supply
:
2309 case snd_soc_dapm_regulator_supply
:
2310 case snd_soc_dapm_clock_supply
:
2312 count
+= sprintf(buf
+ count
, "%s: %s\n",
2313 w
->name
, w
->power
? "On":"Off");
2320 switch (snd_soc_dapm_get_bias_level(dapm
)) {
2321 case SND_SOC_BIAS_ON
:
2324 case SND_SOC_BIAS_PREPARE
:
2327 case SND_SOC_BIAS_STANDBY
:
2330 case SND_SOC_BIAS_OFF
:
2334 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
2339 /* show dapm widget status in sys fs */
2340 static ssize_t
dapm_widget_show(struct device
*dev
,
2341 struct device_attribute
*attr
, char *buf
)
2343 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
2346 mutex_lock(&rtd
->card
->dapm_mutex
);
2348 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
2349 struct snd_soc_component
*cmpnt
= rtd
->codec_dais
[i
]->component
;
2351 count
+= dapm_widget_show_component(cmpnt
, buf
+ count
);
2354 mutex_unlock(&rtd
->card
->dapm_mutex
);
2359 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
2361 struct attribute
*soc_dapm_dev_attrs
[] = {
2362 &dev_attr_dapm_widget
.attr
,
2366 static void dapm_free_path(struct snd_soc_dapm_path
*path
)
2368 list_del(&path
->list_node
[SND_SOC_DAPM_DIR_IN
]);
2369 list_del(&path
->list_node
[SND_SOC_DAPM_DIR_OUT
]);
2370 list_del(&path
->list_kcontrol
);
2371 list_del(&path
->list
);
2375 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget
*w
)
2377 struct snd_soc_dapm_path
*p
, *next_p
;
2378 enum snd_soc_dapm_direction dir
;
2382 * remove source and sink paths associated to this widget.
2383 * While removing the path, remove reference to it from both
2384 * source and sink widgets so that path is removed only once.
2386 snd_soc_dapm_for_each_direction(dir
) {
2387 snd_soc_dapm_widget_for_each_path_safe(w
, dir
, p
, next_p
)
2391 kfree(w
->kcontrols
);
2392 kfree_const(w
->name
);
2396 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context
*dapm
)
2398 dapm
->path_sink_cache
.widget
= NULL
;
2399 dapm
->path_source_cache
.widget
= NULL
;
2402 /* free all dapm widgets and resources */
2403 static void dapm_free_widgets(struct snd_soc_dapm_context
*dapm
)
2405 struct snd_soc_dapm_widget
*w
, *next_w
;
2407 list_for_each_entry_safe(w
, next_w
, &dapm
->card
->widgets
, list
) {
2408 if (w
->dapm
!= dapm
)
2410 snd_soc_dapm_free_widget(w
);
2412 snd_soc_dapm_reset_cache(dapm
);
2415 static struct snd_soc_dapm_widget
*dapm_find_widget(
2416 struct snd_soc_dapm_context
*dapm
, const char *pin
,
2417 bool search_other_contexts
)
2419 struct snd_soc_dapm_widget
*w
;
2420 struct snd_soc_dapm_widget
*fallback
= NULL
;
2422 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
2423 if (!strcmp(w
->name
, pin
)) {
2424 if (w
->dapm
== dapm
)
2431 if (search_other_contexts
)
2437 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context
*dapm
,
2438 const char *pin
, int status
)
2440 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
2442 dapm_assert_locked(dapm
);
2445 dev_err(dapm
->dev
, "ASoC: DAPM unknown pin %s\n", pin
);
2449 if (w
->connected
!= status
) {
2450 dapm_mark_dirty(w
, "pin configuration");
2451 dapm_widget_invalidate_input_paths(w
);
2452 dapm_widget_invalidate_output_paths(w
);
2455 w
->connected
= status
;
2463 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2464 * @dapm: DAPM context
2466 * Walks all dapm audio paths and powers widgets according to their
2467 * stream or path usage.
2469 * Requires external locking.
2471 * Returns 0 for success.
2473 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context
*dapm
)
2476 * Suppress early reports (eg, jacks syncing their state) to avoid
2477 * silly DAPM runs during card startup.
2479 if (!dapm
->card
|| !dapm
->card
->instantiated
)
2482 return dapm_power_widgets(dapm
->card
, SND_SOC_DAPM_STREAM_NOP
);
2484 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked
);
2487 * snd_soc_dapm_sync - scan and power dapm paths
2488 * @dapm: DAPM context
2490 * Walks all dapm audio paths and powers widgets according to their
2491 * stream or path usage.
2493 * Returns 0 for success.
2495 int snd_soc_dapm_sync(struct snd_soc_dapm_context
*dapm
)
2499 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2500 ret
= snd_soc_dapm_sync_unlocked(dapm
);
2501 mutex_unlock(&dapm
->card
->dapm_mutex
);
2504 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
2507 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2508 * @w: The widget for which to update the flags
2510 * Some widgets have a dynamic category which depends on which neighbors they
2511 * are connected to. This function update the category for these widgets.
2513 * This function must be called whenever a path is added or removed to a widget.
2515 static void dapm_update_widget_flags(struct snd_soc_dapm_widget
*w
)
2517 enum snd_soc_dapm_direction dir
;
2518 struct snd_soc_dapm_path
*p
;
2522 case snd_soc_dapm_input
:
2523 /* On a fully routed card an input is never a source */
2524 if (w
->dapm
->card
->fully_routed
)
2526 ep
= SND_SOC_DAPM_EP_SOURCE
;
2527 snd_soc_dapm_widget_for_each_source_path(w
, p
) {
2528 if (p
->source
->id
== snd_soc_dapm_micbias
||
2529 p
->source
->id
== snd_soc_dapm_mic
||
2530 p
->source
->id
== snd_soc_dapm_line
||
2531 p
->source
->id
== snd_soc_dapm_output
) {
2537 case snd_soc_dapm_output
:
2538 /* On a fully routed card a output is never a sink */
2539 if (w
->dapm
->card
->fully_routed
)
2541 ep
= SND_SOC_DAPM_EP_SINK
;
2542 snd_soc_dapm_widget_for_each_sink_path(w
, p
) {
2543 if (p
->sink
->id
== snd_soc_dapm_spk
||
2544 p
->sink
->id
== snd_soc_dapm_hp
||
2545 p
->sink
->id
== snd_soc_dapm_line
||
2546 p
->sink
->id
== snd_soc_dapm_input
) {
2552 case snd_soc_dapm_line
:
2554 snd_soc_dapm_for_each_direction(dir
) {
2555 if (!list_empty(&w
->edges
[dir
]))
2556 ep
|= SND_SOC_DAPM_DIR_TO_EP(dir
);
2566 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context
*dapm
,
2567 struct snd_soc_dapm_widget
*source
, struct snd_soc_dapm_widget
*sink
,
2568 const char *control
)
2570 bool dynamic_source
= false;
2571 bool dynamic_sink
= false;
2576 switch (source
->id
) {
2577 case snd_soc_dapm_demux
:
2578 dynamic_source
= true;
2585 case snd_soc_dapm_mux
:
2586 case snd_soc_dapm_switch
:
2587 case snd_soc_dapm_mixer
:
2588 case snd_soc_dapm_mixer_named_ctl
:
2589 dynamic_sink
= true;
2595 if (dynamic_source
&& dynamic_sink
) {
2597 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2598 source
->name
, control
, sink
->name
);
2600 } else if (!dynamic_source
&& !dynamic_sink
) {
2602 "Control not supported for path %s -> [%s] -> %s\n",
2603 source
->name
, control
, sink
->name
);
2610 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context
*dapm
,
2611 struct snd_soc_dapm_widget
*wsource
, struct snd_soc_dapm_widget
*wsink
,
2612 const char *control
,
2613 int (*connected
)(struct snd_soc_dapm_widget
*source
,
2614 struct snd_soc_dapm_widget
*sink
))
2616 struct snd_soc_dapm_widget
*widgets
[2];
2617 enum snd_soc_dapm_direction dir
;
2618 struct snd_soc_dapm_path
*path
;
2621 if (wsink
->is_supply
&& !wsource
->is_supply
) {
2623 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2624 wsource
->name
, wsink
->name
);
2628 if (connected
&& !wsource
->is_supply
) {
2630 "connected() callback only supported for supply widgets (%s -> %s)\n",
2631 wsource
->name
, wsink
->name
);
2635 if (wsource
->is_supply
&& control
) {
2637 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2638 wsource
->name
, control
, wsink
->name
);
2642 ret
= snd_soc_dapm_check_dynamic_path(dapm
, wsource
, wsink
, control
);
2646 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
2650 path
->node
[SND_SOC_DAPM_DIR_IN
] = wsource
;
2651 path
->node
[SND_SOC_DAPM_DIR_OUT
] = wsink
;
2652 widgets
[SND_SOC_DAPM_DIR_IN
] = wsource
;
2653 widgets
[SND_SOC_DAPM_DIR_OUT
] = wsink
;
2655 path
->connected
= connected
;
2656 INIT_LIST_HEAD(&path
->list
);
2657 INIT_LIST_HEAD(&path
->list_kcontrol
);
2659 if (wsource
->is_supply
|| wsink
->is_supply
)
2660 path
->is_supply
= 1;
2662 /* connect static paths */
2663 if (control
== NULL
) {
2666 switch (wsource
->id
) {
2667 case snd_soc_dapm_demux
:
2668 ret
= dapm_connect_mux(dapm
, path
, control
, wsource
);
2676 switch (wsink
->id
) {
2677 case snd_soc_dapm_mux
:
2678 ret
= dapm_connect_mux(dapm
, path
, control
, wsink
);
2682 case snd_soc_dapm_switch
:
2683 case snd_soc_dapm_mixer
:
2684 case snd_soc_dapm_mixer_named_ctl
:
2685 ret
= dapm_connect_mixer(dapm
, path
, control
);
2694 list_add(&path
->list
, &dapm
->card
->paths
);
2695 snd_soc_dapm_for_each_direction(dir
)
2696 list_add(&path
->list_node
[dir
], &widgets
[dir
]->edges
[dir
]);
2698 snd_soc_dapm_for_each_direction(dir
) {
2699 dapm_update_widget_flags(widgets
[dir
]);
2700 dapm_mark_dirty(widgets
[dir
], "Route added");
2703 if (dapm
->card
->instantiated
&& path
->connect
)
2704 dapm_path_invalidate(path
);
2712 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context
*dapm
,
2713 const struct snd_soc_dapm_route
*route
)
2715 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
2716 struct snd_soc_dapm_widget
*wtsource
= NULL
, *wtsink
= NULL
;
2719 char prefixed_sink
[80];
2720 char prefixed_source
[80];
2724 prefix
= soc_dapm_prefix(dapm
);
2726 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
2727 prefix
, route
->sink
);
2728 sink
= prefixed_sink
;
2729 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
2730 prefix
, route
->source
);
2731 source
= prefixed_source
;
2734 source
= route
->source
;
2737 wsource
= dapm_wcache_lookup(&dapm
->path_source_cache
, source
);
2738 wsink
= dapm_wcache_lookup(&dapm
->path_sink_cache
, sink
);
2740 if (wsink
&& wsource
)
2744 * find src and dest widgets over all widgets but favor a widget from
2745 * current DAPM context
2747 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
2748 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
2750 if (w
->dapm
== dapm
) {
2757 if (!wsource
&& !(strcmp(w
->name
, source
))) {
2759 if (w
->dapm
== dapm
) {
2766 /* use widget from another DAPM context if not found from this */
2772 if (wsource
== NULL
) {
2773 dev_err(dapm
->dev
, "ASoC: no source widget found for %s\n",
2777 if (wsink
== NULL
) {
2778 dev_err(dapm
->dev
, "ASoC: no sink widget found for %s\n",
2784 dapm_wcache_update(&dapm
->path_sink_cache
, wsink
);
2785 dapm_wcache_update(&dapm
->path_source_cache
, wsource
);
2787 ret
= snd_soc_dapm_add_path(dapm
, wsource
, wsink
, route
->control
,
2794 dev_warn(dapm
->dev
, "ASoC: no dapm match for %s --> %s --> %s\n",
2795 source
, route
->control
, sink
);
2799 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context
*dapm
,
2800 const struct snd_soc_dapm_route
*route
)
2802 struct snd_soc_dapm_widget
*wsource
, *wsink
;
2803 struct snd_soc_dapm_path
*path
, *p
;
2806 char prefixed_sink
[80];
2807 char prefixed_source
[80];
2810 if (route
->control
) {
2812 "ASoC: Removal of routes with controls not supported\n");
2816 prefix
= soc_dapm_prefix(dapm
);
2818 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
2819 prefix
, route
->sink
);
2820 sink
= prefixed_sink
;
2821 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
2822 prefix
, route
->source
);
2823 source
= prefixed_source
;
2826 source
= route
->source
;
2830 list_for_each_entry(p
, &dapm
->card
->paths
, list
) {
2831 if (strcmp(p
->source
->name
, source
) != 0)
2833 if (strcmp(p
->sink
->name
, sink
) != 0)
2840 wsource
= path
->source
;
2843 dapm_mark_dirty(wsource
, "Route removed");
2844 dapm_mark_dirty(wsink
, "Route removed");
2846 dapm_path_invalidate(path
);
2848 dapm_free_path(path
);
2850 /* Update any path related flags */
2851 dapm_update_widget_flags(wsource
);
2852 dapm_update_widget_flags(wsink
);
2854 dev_warn(dapm
->dev
, "ASoC: Route %s->%s does not exist\n",
2862 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2863 * @dapm: DAPM context
2864 * @route: audio routes
2865 * @num: number of routes
2867 * Connects 2 dapm widgets together via a named audio path. The sink is
2868 * the widget receiving the audio signal, whilst the source is the sender
2869 * of the audio signal.
2871 * Returns 0 for success else error. On error all resources can be freed
2872 * with a call to snd_soc_card_free().
2874 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context
*dapm
,
2875 const struct snd_soc_dapm_route
*route
, int num
)
2879 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2880 for (i
= 0; i
< num
; i
++) {
2881 r
= snd_soc_dapm_add_route(dapm
, route
);
2883 dev_err(dapm
->dev
, "ASoC: Failed to add route %s -> %s -> %s\n",
2885 route
->control
? route
->control
: "direct",
2891 mutex_unlock(&dapm
->card
->dapm_mutex
);
2895 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
2898 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2899 * @dapm: DAPM context
2900 * @route: audio routes
2901 * @num: number of routes
2903 * Removes routes from the DAPM context.
2905 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context
*dapm
,
2906 const struct snd_soc_dapm_route
*route
, int num
)
2910 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2911 for (i
= 0; i
< num
; i
++) {
2912 snd_soc_dapm_del_route(dapm
, route
);
2915 mutex_unlock(&dapm
->card
->dapm_mutex
);
2919 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes
);
2921 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context
*dapm
,
2922 const struct snd_soc_dapm_route
*route
)
2924 struct snd_soc_dapm_widget
*source
= dapm_find_widget(dapm
,
2927 struct snd_soc_dapm_widget
*sink
= dapm_find_widget(dapm
,
2930 struct snd_soc_dapm_path
*path
;
2934 dev_err(dapm
->dev
, "ASoC: Unable to find source %s for weak route\n",
2940 dev_err(dapm
->dev
, "ASoC: Unable to find sink %s for weak route\n",
2945 if (route
->control
|| route
->connected
)
2946 dev_warn(dapm
->dev
, "ASoC: Ignoring control for weak route %s->%s\n",
2947 route
->source
, route
->sink
);
2949 snd_soc_dapm_widget_for_each_sink_path(source
, path
) {
2950 if (path
->sink
== sink
) {
2957 dev_err(dapm
->dev
, "ASoC: No path found for weak route %s->%s\n",
2958 route
->source
, route
->sink
);
2960 dev_warn(dapm
->dev
, "ASoC: %d paths found for weak route %s->%s\n",
2961 count
, route
->source
, route
->sink
);
2967 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2968 * @dapm: DAPM context
2969 * @route: audio routes
2970 * @num: number of routes
2972 * Mark existing routes matching those specified in the passed array
2973 * as being weak, meaning that they are ignored for the purpose of
2974 * power decisions. The main intended use case is for sidetone paths
2975 * which couple audio between other independent paths if they are both
2976 * active in order to make the combination work better at the user
2977 * level but which aren't intended to be "used".
2979 * Note that CODEC drivers should not use this as sidetone type paths
2980 * can frequently also be used as bypass paths.
2982 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context
*dapm
,
2983 const struct snd_soc_dapm_route
*route
, int num
)
2988 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2989 for (i
= 0; i
< num
; i
++) {
2990 err
= snd_soc_dapm_weak_route(dapm
, route
);
2995 mutex_unlock(&dapm
->card
->dapm_mutex
);
2999 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes
);
3002 * snd_soc_dapm_new_widgets - add new dapm widgets
3003 * @card: card to be checked for new dapm widgets
3005 * Checks the codec for any new dapm widgets and creates them if found.
3007 * Returns 0 for success.
3009 int snd_soc_dapm_new_widgets(struct snd_soc_card
*card
)
3011 struct snd_soc_dapm_widget
*w
;
3014 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
3016 list_for_each_entry(w
, &card
->widgets
, list
)
3021 if (w
->num_kcontrols
) {
3022 w
->kcontrols
= kzalloc(w
->num_kcontrols
*
3023 sizeof(struct snd_kcontrol
*),
3025 if (!w
->kcontrols
) {
3026 mutex_unlock(&card
->dapm_mutex
);
3032 case snd_soc_dapm_switch
:
3033 case snd_soc_dapm_mixer
:
3034 case snd_soc_dapm_mixer_named_ctl
:
3037 case snd_soc_dapm_mux
:
3038 case snd_soc_dapm_demux
:
3041 case snd_soc_dapm_pga
:
3042 case snd_soc_dapm_out_drv
:
3045 case snd_soc_dapm_dai_link
:
3046 dapm_new_dai_link(w
);
3052 /* Read the initial power state from the device */
3054 soc_dapm_read(w
->dapm
, w
->reg
, &val
);
3055 val
= val
>> w
->shift
;
3057 if (val
== w
->on_val
)
3063 dapm_mark_dirty(w
, "new widget");
3064 dapm_debugfs_add_widget(w
);
3067 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
3068 mutex_unlock(&card
->dapm_mutex
);
3071 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
3074 * snd_soc_dapm_get_volsw - dapm mixer get callback
3075 * @kcontrol: mixer control
3076 * @ucontrol: control element information
3078 * Callback to get the value of a dapm mixer control.
3080 * Returns 0 for success.
3082 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
3083 struct snd_ctl_elem_value
*ucontrol
)
3085 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
3086 struct snd_soc_card
*card
= dapm
->card
;
3087 struct soc_mixer_control
*mc
=
3088 (struct soc_mixer_control
*)kcontrol
->private_value
;
3090 unsigned int shift
= mc
->shift
;
3092 unsigned int width
= fls(max
);
3093 unsigned int mask
= (1 << fls(max
)) - 1;
3094 unsigned int invert
= mc
->invert
;
3095 unsigned int reg_val
, val
, rval
= 0;
3098 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3099 if (dapm_kcontrol_is_powered(kcontrol
) && reg
!= SND_SOC_NOPM
) {
3100 ret
= soc_dapm_read(dapm
, reg
, ®_val
);
3101 val
= (reg_val
>> shift
) & mask
;
3103 if (ret
== 0 && reg
!= mc
->rreg
)
3104 ret
= soc_dapm_read(dapm
, mc
->rreg
, ®_val
);
3106 if (snd_soc_volsw_is_stereo(mc
))
3107 rval
= (reg_val
>> mc
->rshift
) & mask
;
3109 reg_val
= dapm_kcontrol_get_value(kcontrol
);
3110 val
= reg_val
& mask
;
3112 if (snd_soc_volsw_is_stereo(mc
))
3113 rval
= (reg_val
>> width
) & mask
;
3115 mutex_unlock(&card
->dapm_mutex
);
3121 ucontrol
->value
.integer
.value
[0] = max
- val
;
3123 ucontrol
->value
.integer
.value
[0] = val
;
3125 if (snd_soc_volsw_is_stereo(mc
)) {
3127 ucontrol
->value
.integer
.value
[1] = max
- rval
;
3129 ucontrol
->value
.integer
.value
[1] = rval
;
3134 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
3137 * snd_soc_dapm_put_volsw - dapm mixer set callback
3138 * @kcontrol: mixer control
3139 * @ucontrol: control element information
3141 * Callback to set the value of a dapm mixer control.
3143 * Returns 0 for success.
3145 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
3146 struct snd_ctl_elem_value
*ucontrol
)
3148 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
3149 struct snd_soc_card
*card
= dapm
->card
;
3150 struct soc_mixer_control
*mc
=
3151 (struct soc_mixer_control
*)kcontrol
->private_value
;
3153 unsigned int shift
= mc
->shift
;
3155 unsigned int width
= fls(max
);
3156 unsigned int mask
= (1 << width
) - 1;
3157 unsigned int invert
= mc
->invert
;
3158 unsigned int val
, rval
= 0;
3159 int connect
, rconnect
= -1, change
, reg_change
= 0;
3160 struct snd_soc_dapm_update update
= { NULL
};
3163 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
3169 if (snd_soc_volsw_is_stereo(mc
)) {
3170 rval
= (ucontrol
->value
.integer
.value
[1] & mask
);
3176 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3178 /* This assumes field width < (bits in unsigned int / 2) */
3179 if (width
> sizeof(unsigned int) * 8 / 2)
3181 "ASoC: control %s field width limit exceeded\n",
3183 change
= dapm_kcontrol_set_value(kcontrol
, val
| (rval
<< width
));
3185 if (reg
!= SND_SOC_NOPM
) {
3187 rval
= rval
<< mc
->rshift
;
3189 reg_change
= soc_dapm_test_bits(dapm
, reg
, mask
<< shift
, val
);
3191 if (snd_soc_volsw_is_stereo(mc
))
3192 reg_change
|= soc_dapm_test_bits(dapm
, mc
->rreg
,
3197 if (change
|| reg_change
) {
3199 if (snd_soc_volsw_is_stereo(mc
)) {
3200 update
.has_second_set
= true;
3201 update
.reg2
= mc
->rreg
;
3202 update
.mask2
= mask
<< mc
->rshift
;
3205 update
.kcontrol
= kcontrol
;
3207 update
.mask
= mask
<< shift
;
3209 card
->update
= &update
;
3211 change
|= reg_change
;
3213 ret
= soc_dapm_mixer_update_power(card
, kcontrol
, connect
,
3216 card
->update
= NULL
;
3219 mutex_unlock(&card
->dapm_mutex
);
3222 soc_dpcm_runtime_update(card
);
3226 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
3229 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3230 * @kcontrol: mixer control
3231 * @ucontrol: control element information
3233 * Callback to get the value of a dapm enumerated double mixer control.
3235 * Returns 0 for success.
3237 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
3238 struct snd_ctl_elem_value
*ucontrol
)
3240 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
3241 struct snd_soc_card
*card
= dapm
->card
;
3242 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
3243 unsigned int reg_val
, val
;
3245 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3246 if (e
->reg
!= SND_SOC_NOPM
&& dapm_kcontrol_is_powered(kcontrol
)) {
3247 int ret
= soc_dapm_read(dapm
, e
->reg
, ®_val
);
3249 mutex_unlock(&card
->dapm_mutex
);
3253 reg_val
= dapm_kcontrol_get_value(kcontrol
);
3255 mutex_unlock(&card
->dapm_mutex
);
3257 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
3258 ucontrol
->value
.enumerated
.item
[0] = snd_soc_enum_val_to_item(e
, val
);
3259 if (e
->shift_l
!= e
->shift_r
) {
3260 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
3261 val
= snd_soc_enum_val_to_item(e
, val
);
3262 ucontrol
->value
.enumerated
.item
[1] = val
;
3267 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
3270 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3271 * @kcontrol: mixer control
3272 * @ucontrol: control element information
3274 * Callback to set the value of a dapm enumerated double mixer control.
3276 * Returns 0 for success.
3278 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
3279 struct snd_ctl_elem_value
*ucontrol
)
3281 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
3282 struct snd_soc_card
*card
= dapm
->card
;
3283 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
3284 unsigned int *item
= ucontrol
->value
.enumerated
.item
;
3285 unsigned int val
, change
, reg_change
= 0;
3287 struct snd_soc_dapm_update update
= { NULL
};
3290 if (item
[0] >= e
->items
)
3293 val
= snd_soc_enum_item_to_val(e
, item
[0]) << e
->shift_l
;
3294 mask
= e
->mask
<< e
->shift_l
;
3295 if (e
->shift_l
!= e
->shift_r
) {
3296 if (item
[1] > e
->items
)
3298 val
|= snd_soc_enum_item_to_val(e
, item
[1]) << e
->shift_r
;
3299 mask
|= e
->mask
<< e
->shift_r
;
3302 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3304 change
= dapm_kcontrol_set_value(kcontrol
, val
);
3306 if (e
->reg
!= SND_SOC_NOPM
)
3307 reg_change
= soc_dapm_test_bits(dapm
, e
->reg
, mask
, val
);
3309 if (change
|| reg_change
) {
3311 update
.kcontrol
= kcontrol
;
3312 update
.reg
= e
->reg
;
3315 card
->update
= &update
;
3317 change
|= reg_change
;
3319 ret
= soc_dapm_mux_update_power(card
, kcontrol
, item
[0], e
);
3321 card
->update
= NULL
;
3324 mutex_unlock(&card
->dapm_mutex
);
3327 soc_dpcm_runtime_update(card
);
3331 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
3334 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3336 * @kcontrol: mixer control
3337 * @uinfo: control element information
3339 * Callback to provide information about a pin switch control.
3341 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
3342 struct snd_ctl_elem_info
*uinfo
)
3344 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
3346 uinfo
->value
.integer
.min
= 0;
3347 uinfo
->value
.integer
.max
= 1;
3351 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
3354 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3356 * @kcontrol: mixer control
3359 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
3360 struct snd_ctl_elem_value
*ucontrol
)
3362 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
3363 const char *pin
= (const char *)kcontrol
->private_value
;
3365 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3367 ucontrol
->value
.integer
.value
[0] =
3368 snd_soc_dapm_get_pin_status(&card
->dapm
, pin
);
3370 mutex_unlock(&card
->dapm_mutex
);
3374 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
3377 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3379 * @kcontrol: mixer control
3382 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
3383 struct snd_ctl_elem_value
*ucontrol
)
3385 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
3386 const char *pin
= (const char *)kcontrol
->private_value
;
3388 if (ucontrol
->value
.integer
.value
[0])
3389 snd_soc_dapm_enable_pin(&card
->dapm
, pin
);
3391 snd_soc_dapm_disable_pin(&card
->dapm
, pin
);
3393 snd_soc_dapm_sync(&card
->dapm
);
3396 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
3398 struct snd_soc_dapm_widget
*
3399 snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
3400 const struct snd_soc_dapm_widget
*widget
)
3402 struct snd_soc_dapm_widget
*w
;
3404 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3405 w
= snd_soc_dapm_new_control_unlocked(dapm
, widget
);
3408 "ASoC: Failed to create DAPM control %s\n",
3411 mutex_unlock(&dapm
->card
->dapm_mutex
);
3414 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
3416 struct snd_soc_dapm_widget
*
3417 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context
*dapm
,
3418 const struct snd_soc_dapm_widget
*widget
)
3420 enum snd_soc_dapm_direction dir
;
3421 struct snd_soc_dapm_widget
*w
;
3425 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
3429 case snd_soc_dapm_regulator_supply
:
3430 w
->regulator
= devm_regulator_get(dapm
->dev
, w
->name
);
3431 if (IS_ERR(w
->regulator
)) {
3432 ret
= PTR_ERR(w
->regulator
);
3433 dev_err(dapm
->dev
, "ASoC: Failed to request %s: %d\n",
3438 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
3439 ret
= regulator_allow_bypass(w
->regulator
, true);
3441 dev_warn(w
->dapm
->dev
,
3442 "ASoC: Failed to bypass %s: %d\n",
3446 case snd_soc_dapm_clock_supply
:
3447 #ifdef CONFIG_CLKDEV_LOOKUP
3448 w
->clk
= devm_clk_get(dapm
->dev
, w
->name
);
3449 if (IS_ERR(w
->clk
)) {
3450 ret
= PTR_ERR(w
->clk
);
3451 dev_err(dapm
->dev
, "ASoC: Failed to request %s: %d\n",
3463 prefix
= soc_dapm_prefix(dapm
);
3465 w
->name
= kasprintf(GFP_KERNEL
, "%s %s", prefix
, widget
->name
);
3467 w
->name
= kstrdup_const(widget
->name
, GFP_KERNEL
);
3468 if (w
->name
== NULL
) {
3474 case snd_soc_dapm_mic
:
3475 w
->is_ep
= SND_SOC_DAPM_EP_SOURCE
;
3476 w
->power_check
= dapm_generic_check_power
;
3478 case snd_soc_dapm_input
:
3479 if (!dapm
->card
->fully_routed
)
3480 w
->is_ep
= SND_SOC_DAPM_EP_SOURCE
;
3481 w
->power_check
= dapm_generic_check_power
;
3483 case snd_soc_dapm_spk
:
3484 case snd_soc_dapm_hp
:
3485 w
->is_ep
= SND_SOC_DAPM_EP_SINK
;
3486 w
->power_check
= dapm_generic_check_power
;
3488 case snd_soc_dapm_output
:
3489 if (!dapm
->card
->fully_routed
)
3490 w
->is_ep
= SND_SOC_DAPM_EP_SINK
;
3491 w
->power_check
= dapm_generic_check_power
;
3493 case snd_soc_dapm_vmid
:
3494 case snd_soc_dapm_siggen
:
3495 w
->is_ep
= SND_SOC_DAPM_EP_SOURCE
;
3496 w
->power_check
= dapm_always_on_check_power
;
3498 case snd_soc_dapm_sink
:
3499 w
->is_ep
= SND_SOC_DAPM_EP_SINK
;
3500 w
->power_check
= dapm_always_on_check_power
;
3503 case snd_soc_dapm_mux
:
3504 case snd_soc_dapm_demux
:
3505 case snd_soc_dapm_switch
:
3506 case snd_soc_dapm_mixer
:
3507 case snd_soc_dapm_mixer_named_ctl
:
3508 case snd_soc_dapm_adc
:
3509 case snd_soc_dapm_aif_out
:
3510 case snd_soc_dapm_dac
:
3511 case snd_soc_dapm_aif_in
:
3512 case snd_soc_dapm_pga
:
3513 case snd_soc_dapm_out_drv
:
3514 case snd_soc_dapm_micbias
:
3515 case snd_soc_dapm_line
:
3516 case snd_soc_dapm_dai_link
:
3517 case snd_soc_dapm_dai_out
:
3518 case snd_soc_dapm_dai_in
:
3519 w
->power_check
= dapm_generic_check_power
;
3521 case snd_soc_dapm_supply
:
3522 case snd_soc_dapm_regulator_supply
:
3523 case snd_soc_dapm_clock_supply
:
3524 case snd_soc_dapm_kcontrol
:
3526 w
->power_check
= dapm_supply_check_power
;
3529 w
->power_check
= dapm_always_on_check_power
;
3534 INIT_LIST_HEAD(&w
->list
);
3535 INIT_LIST_HEAD(&w
->dirty
);
3536 list_add_tail(&w
->list
, &dapm
->card
->widgets
);
3538 snd_soc_dapm_for_each_direction(dir
) {
3539 INIT_LIST_HEAD(&w
->edges
[dir
]);
3540 w
->endpoints
[dir
] = -1;
3543 /* machine layer sets up unconnected pins and insertions */
3549 * snd_soc_dapm_new_controls - create new dapm controls
3550 * @dapm: DAPM context
3551 * @widget: widget array
3552 * @num: number of widgets
3554 * Creates new DAPM controls based upon the templates.
3556 * Returns 0 for success else error.
3558 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context
*dapm
,
3559 const struct snd_soc_dapm_widget
*widget
,
3562 struct snd_soc_dapm_widget
*w
;
3566 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
3567 for (i
= 0; i
< num
; i
++) {
3568 w
= snd_soc_dapm_new_control_unlocked(dapm
, widget
);
3571 "ASoC: Failed to create DAPM control %s\n",
3578 mutex_unlock(&dapm
->card
->dapm_mutex
);
3581 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
3583 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget
*w
,
3584 struct snd_kcontrol
*kcontrol
, int event
)
3586 struct snd_soc_dapm_path
*source_p
, *sink_p
;
3587 struct snd_soc_dai
*source
, *sink
;
3588 const struct snd_soc_pcm_stream
*config
= w
->params
+ w
->params_select
;
3589 struct snd_pcm_substream substream
;
3590 struct snd_pcm_hw_params
*params
= NULL
;
3591 struct snd_pcm_runtime
*runtime
= NULL
;
3595 if (WARN_ON(!config
) ||
3596 WARN_ON(list_empty(&w
->edges
[SND_SOC_DAPM_DIR_OUT
]) ||
3597 list_empty(&w
->edges
[SND_SOC_DAPM_DIR_IN
])))
3600 /* We only support a single source and sink, pick the first */
3601 source_p
= list_first_entry(&w
->edges
[SND_SOC_DAPM_DIR_OUT
],
3602 struct snd_soc_dapm_path
,
3603 list_node
[SND_SOC_DAPM_DIR_OUT
]);
3604 sink_p
= list_first_entry(&w
->edges
[SND_SOC_DAPM_DIR_IN
],
3605 struct snd_soc_dapm_path
,
3606 list_node
[SND_SOC_DAPM_DIR_IN
]);
3608 source
= source_p
->source
->priv
;
3609 sink
= sink_p
->sink
->priv
;
3611 /* Be a little careful as we don't want to overflow the mask array */
3612 if (config
->formats
) {
3613 fmt
= ffs(config
->formats
) - 1;
3615 dev_warn(w
->dapm
->dev
, "ASoC: Invalid format %llx specified\n",
3620 /* Currently very limited parameter selection */
3621 params
= kzalloc(sizeof(*params
), GFP_KERNEL
);
3626 snd_mask_set(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), fmt
);
3628 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
)->min
=
3630 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
)->max
=
3633 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
)->min
3634 = config
->channels_min
;
3635 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
)->max
3636 = config
->channels_max
;
3638 memset(&substream
, 0, sizeof(substream
));
3640 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3641 runtime
= kzalloc(sizeof(*runtime
), GFP_KERNEL
);
3646 substream
.runtime
= runtime
;
3649 case SND_SOC_DAPM_PRE_PMU
:
3650 substream
.stream
= SNDRV_PCM_STREAM_CAPTURE
;
3651 if (source
->driver
->ops
&& source
->driver
->ops
->startup
) {
3652 ret
= source
->driver
->ops
->startup(&substream
, source
);
3654 dev_err(source
->dev
,
3655 "ASoC: startup() failed: %d\n", ret
);
3660 ret
= soc_dai_hw_params(&substream
, params
, source
);
3664 substream
.stream
= SNDRV_PCM_STREAM_PLAYBACK
;
3665 if (sink
->driver
->ops
&& sink
->driver
->ops
->startup
) {
3666 ret
= sink
->driver
->ops
->startup(&substream
, sink
);
3669 "ASoC: startup() failed: %d\n", ret
);
3674 ret
= soc_dai_hw_params(&substream
, params
, sink
);
3679 case SND_SOC_DAPM_POST_PMU
:
3680 ret
= snd_soc_dai_digital_mute(sink
, 0,
3681 SNDRV_PCM_STREAM_PLAYBACK
);
3682 if (ret
!= 0 && ret
!= -ENOTSUPP
)
3683 dev_warn(sink
->dev
, "ASoC: Failed to unmute: %d\n", ret
);
3687 case SND_SOC_DAPM_PRE_PMD
:
3688 ret
= snd_soc_dai_digital_mute(sink
, 1,
3689 SNDRV_PCM_STREAM_PLAYBACK
);
3690 if (ret
!= 0 && ret
!= -ENOTSUPP
)
3691 dev_warn(sink
->dev
, "ASoC: Failed to mute: %d\n", ret
);
3695 if (source
->driver
->ops
&& source
->driver
->ops
->shutdown
) {
3696 substream
.stream
= SNDRV_PCM_STREAM_CAPTURE
;
3697 source
->driver
->ops
->shutdown(&substream
, source
);
3701 if (sink
->driver
->ops
&& sink
->driver
->ops
->shutdown
) {
3702 substream
.stream
= SNDRV_PCM_STREAM_PLAYBACK
;
3703 sink
->driver
->ops
->shutdown(&substream
, sink
);
3708 WARN(1, "Unknown event %d\n", event
);
3718 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol
*kcontrol
,
3719 struct snd_ctl_elem_value
*ucontrol
)
3721 struct snd_soc_dapm_widget
*w
= snd_kcontrol_chip(kcontrol
);
3723 ucontrol
->value
.enumerated
.item
[0] = w
->params_select
;
3728 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol
*kcontrol
,
3729 struct snd_ctl_elem_value
*ucontrol
)
3731 struct snd_soc_dapm_widget
*w
= snd_kcontrol_chip(kcontrol
);
3733 /* Can't change the config when widget is already powered */
3737 if (ucontrol
->value
.enumerated
.item
[0] == w
->params_select
)
3740 if (ucontrol
->value
.enumerated
.item
[0] >= w
->num_params
)
3743 w
->params_select
= ucontrol
->value
.enumerated
.item
[0];
3748 int snd_soc_dapm_new_pcm(struct snd_soc_card
*card
,
3749 const struct snd_soc_pcm_stream
*params
,
3750 unsigned int num_params
,
3751 struct snd_soc_dapm_widget
*source
,
3752 struct snd_soc_dapm_widget
*sink
)
3754 struct snd_soc_dapm_widget
template;
3755 struct snd_soc_dapm_widget
*w
;
3758 unsigned long private_value
;
3759 const char **w_param_text
;
3760 struct soc_enum w_param_enum
[] = {
3761 SOC_ENUM_SINGLE(0, 0, 0, NULL
),
3763 struct snd_kcontrol_new kcontrol_dai_link
[] = {
3764 SOC_ENUM_EXT(NULL
, w_param_enum
[0],
3765 snd_soc_dapm_dai_link_get
,
3766 snd_soc_dapm_dai_link_put
),
3768 const struct snd_soc_pcm_stream
*config
= params
;
3770 w_param_text
= devm_kcalloc(card
->dev
, num_params
,
3771 sizeof(char *), GFP_KERNEL
);
3775 link_name
= devm_kasprintf(card
->dev
, GFP_KERNEL
, "%s-%s",
3776 source
->name
, sink
->name
);
3779 goto outfree_w_param
;
3782 for (count
= 0 ; count
< num_params
; count
++) {
3783 if (!config
->stream_name
) {
3784 dev_warn(card
->dapm
.dev
,
3785 "ASoC: anonymous config %d for dai link %s\n",
3787 w_param_text
[count
] =
3788 devm_kasprintf(card
->dev
, GFP_KERNEL
,
3789 "Anonymous Configuration %d",
3791 if (!w_param_text
[count
]) {
3793 goto outfree_link_name
;
3796 w_param_text
[count
] = devm_kmemdup(card
->dev
,
3797 config
->stream_name
,
3798 strlen(config
->stream_name
) + 1,
3800 if (!w_param_text
[count
]) {
3802 goto outfree_link_name
;
3807 w_param_enum
[0].items
= num_params
;
3808 w_param_enum
[0].texts
= w_param_text
;
3810 memset(&template, 0, sizeof(template));
3811 template.reg
= SND_SOC_NOPM
;
3812 template.id
= snd_soc_dapm_dai_link
;
3813 template.name
= link_name
;
3814 template.event
= snd_soc_dai_link_event
;
3815 template.event_flags
= SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMU
|
3816 SND_SOC_DAPM_PRE_PMD
;
3817 template.num_kcontrols
= 1;
3818 /* duplicate w_param_enum on heap so that memory persists */
3820 (unsigned long) devm_kmemdup(card
->dev
,
3821 (void *)(kcontrol_dai_link
[0].private_value
),
3822 sizeof(struct soc_enum
), GFP_KERNEL
);
3823 if (!private_value
) {
3824 dev_err(card
->dev
, "ASoC: Failed to create control for %s widget\n",
3827 goto outfree_link_name
;
3829 kcontrol_dai_link
[0].private_value
= private_value
;
3830 /* duplicate kcontrol_dai_link on heap so that memory persists */
3831 template.kcontrol_news
=
3832 devm_kmemdup(card
->dev
, &kcontrol_dai_link
[0],
3833 sizeof(struct snd_kcontrol_new
),
3835 if (!template.kcontrol_news
) {
3836 dev_err(card
->dev
, "ASoC: Failed to create control for %s widget\n",
3839 goto outfree_private_value
;
3842 dev_dbg(card
->dev
, "ASoC: adding %s widget\n", link_name
);
3844 w
= snd_soc_dapm_new_control_unlocked(&card
->dapm
, &template);
3846 dev_err(card
->dev
, "ASoC: Failed to create %s widget\n",
3849 goto outfree_kcontrol_news
;
3853 w
->num_params
= num_params
;
3855 ret
= snd_soc_dapm_add_path(&card
->dapm
, source
, w
, NULL
, NULL
);
3858 return snd_soc_dapm_add_path(&card
->dapm
, w
, sink
, NULL
, NULL
);
3861 devm_kfree(card
->dev
, w
);
3862 outfree_kcontrol_news
:
3863 devm_kfree(card
->dev
, (void *)template.kcontrol_news
);
3864 outfree_private_value
:
3865 devm_kfree(card
->dev
, (void *)private_value
);
3867 devm_kfree(card
->dev
, link_name
);
3869 for (count
= 0 ; count
< num_params
; count
++)
3870 devm_kfree(card
->dev
, (void *)w_param_text
[count
]);
3871 devm_kfree(card
->dev
, w_param_text
);
3876 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context
*dapm
,
3877 struct snd_soc_dai
*dai
)
3879 struct snd_soc_dapm_widget
template;
3880 struct snd_soc_dapm_widget
*w
;
3882 WARN_ON(dapm
->dev
!= dai
->dev
);
3884 memset(&template, 0, sizeof(template));
3885 template.reg
= SND_SOC_NOPM
;
3887 if (dai
->driver
->playback
.stream_name
) {
3888 template.id
= snd_soc_dapm_dai_in
;
3889 template.name
= dai
->driver
->playback
.stream_name
;
3890 template.sname
= dai
->driver
->playback
.stream_name
;
3892 dev_dbg(dai
->dev
, "ASoC: adding %s widget\n",
3895 w
= snd_soc_dapm_new_control_unlocked(dapm
, &template);
3897 dev_err(dapm
->dev
, "ASoC: Failed to create %s widget\n",
3898 dai
->driver
->playback
.stream_name
);
3903 dai
->playback_widget
= w
;
3906 if (dai
->driver
->capture
.stream_name
) {
3907 template.id
= snd_soc_dapm_dai_out
;
3908 template.name
= dai
->driver
->capture
.stream_name
;
3909 template.sname
= dai
->driver
->capture
.stream_name
;
3911 dev_dbg(dai
->dev
, "ASoC: adding %s widget\n",
3914 w
= snd_soc_dapm_new_control_unlocked(dapm
, &template);
3916 dev_err(dapm
->dev
, "ASoC: Failed to create %s widget\n",
3917 dai
->driver
->capture
.stream_name
);
3922 dai
->capture_widget
= w
;
3928 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card
*card
)
3930 struct snd_soc_dapm_widget
*dai_w
, *w
;
3931 struct snd_soc_dapm_widget
*src
, *sink
;
3932 struct snd_soc_dai
*dai
;
3934 /* For each DAI widget... */
3935 list_for_each_entry(dai_w
, &card
->widgets
, list
) {
3936 switch (dai_w
->id
) {
3937 case snd_soc_dapm_dai_in
:
3938 case snd_soc_dapm_dai_out
:
3946 /* ...find all widgets with the same stream and link them */
3947 list_for_each_entry(w
, &card
->widgets
, list
) {
3948 if (w
->dapm
!= dai_w
->dapm
)
3952 case snd_soc_dapm_dai_in
:
3953 case snd_soc_dapm_dai_out
:
3959 if (!w
->sname
|| !strstr(w
->sname
, dai_w
->sname
))
3962 if (dai_w
->id
== snd_soc_dapm_dai_in
) {
3969 dev_dbg(dai
->dev
, "%s -> %s\n", src
->name
, sink
->name
);
3970 snd_soc_dapm_add_path(w
->dapm
, src
, sink
, NULL
, NULL
);
3977 static void dapm_connect_dai_link_widgets(struct snd_soc_card
*card
,
3978 struct snd_soc_pcm_runtime
*rtd
)
3980 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
3981 struct snd_soc_dapm_widget
*sink
, *source
;
3984 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
3985 struct snd_soc_dai
*codec_dai
= rtd
->codec_dais
[i
];
3987 /* connect BE DAI playback if widgets are valid */
3988 if (codec_dai
->playback_widget
&& cpu_dai
->playback_widget
) {
3989 source
= cpu_dai
->playback_widget
;
3990 sink
= codec_dai
->playback_widget
;
3991 dev_dbg(rtd
->dev
, "connected DAI link %s:%s -> %s:%s\n",
3992 cpu_dai
->component
->name
, source
->name
,
3993 codec_dai
->component
->name
, sink
->name
);
3995 snd_soc_dapm_add_path(&card
->dapm
, source
, sink
,
3999 /* connect BE DAI capture if widgets are valid */
4000 if (codec_dai
->capture_widget
&& cpu_dai
->capture_widget
) {
4001 source
= codec_dai
->capture_widget
;
4002 sink
= cpu_dai
->capture_widget
;
4003 dev_dbg(rtd
->dev
, "connected DAI link %s:%s -> %s:%s\n",
4004 codec_dai
->component
->name
, source
->name
,
4005 cpu_dai
->component
->name
, sink
->name
);
4007 snd_soc_dapm_add_path(&card
->dapm
, source
, sink
,
4013 static void soc_dapm_dai_stream_event(struct snd_soc_dai
*dai
, int stream
,
4016 struct snd_soc_dapm_widget
*w
;
4019 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
4020 w
= dai
->playback_widget
;
4022 w
= dai
->capture_widget
;
4025 dapm_mark_dirty(w
, "stream event");
4027 if (w
->id
== snd_soc_dapm_dai_in
) {
4028 ep
= SND_SOC_DAPM_EP_SOURCE
;
4029 dapm_widget_invalidate_input_paths(w
);
4031 ep
= SND_SOC_DAPM_EP_SINK
;
4032 dapm_widget_invalidate_output_paths(w
);
4036 case SND_SOC_DAPM_STREAM_START
:
4040 case SND_SOC_DAPM_STREAM_STOP
:
4044 case SND_SOC_DAPM_STREAM_SUSPEND
:
4045 case SND_SOC_DAPM_STREAM_RESUME
:
4046 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
4047 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
4053 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card
*card
)
4055 struct snd_soc_pcm_runtime
*rtd
;
4057 /* for each BE DAI link... */
4058 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
4060 * dynamic FE links have no fixed DAI mapping.
4061 * CODEC<->CODEC links have no direct connection.
4063 if (rtd
->dai_link
->dynamic
|| rtd
->dai_link
->params
)
4066 dapm_connect_dai_link_widgets(card
, rtd
);
4070 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
, int stream
,
4075 soc_dapm_dai_stream_event(rtd
->cpu_dai
, stream
, event
);
4076 for (i
= 0; i
< rtd
->num_codecs
; i
++)
4077 soc_dapm_dai_stream_event(rtd
->codec_dais
[i
], stream
, event
);
4079 dapm_power_widgets(rtd
->card
, event
);
4083 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4084 * @rtd: PCM runtime data
4085 * @stream: stream name
4086 * @event: stream event
4088 * Sends a stream event to the dapm core. The core then makes any
4089 * necessary widget power changes.
4091 * Returns 0 for success else error.
4093 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
, int stream
,
4096 struct snd_soc_card
*card
= rtd
->card
;
4098 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4099 soc_dapm_stream_event(rtd
, stream
, event
);
4100 mutex_unlock(&card
->dapm_mutex
);
4104 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4105 * @dapm: DAPM context
4108 * Enables input/output pin and its parents or children widgets iff there is
4109 * a valid audio route and active audio stream.
4111 * Requires external locking.
4113 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4114 * do any widget power switching.
4116 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
4119 return snd_soc_dapm_set_pin(dapm
, pin
, 1);
4121 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked
);
4124 * snd_soc_dapm_enable_pin - enable pin.
4125 * @dapm: DAPM context
4128 * Enables input/output pin and its parents or children widgets iff there is
4129 * a valid audio route and active audio stream.
4131 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4132 * do any widget power switching.
4134 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
4138 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4140 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 1);
4142 mutex_unlock(&dapm
->card
->dapm_mutex
);
4146 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
4149 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4150 * @dapm: DAPM context
4153 * Enables input/output pin regardless of any other state. This is
4154 * intended for use with microphone bias supplies used in microphone
4157 * Requires external locking.
4159 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4160 * do any widget power switching.
4162 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
4165 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
4168 dev_err(dapm
->dev
, "ASoC: unknown pin %s\n", pin
);
4172 dev_dbg(w
->dapm
->dev
, "ASoC: force enable pin %s\n", pin
);
4173 if (!w
->connected
) {
4175 * w->force does not affect the number of input or output paths,
4176 * so we only have to recheck if w->connected is changed
4178 dapm_widget_invalidate_input_paths(w
);
4179 dapm_widget_invalidate_output_paths(w
);
4183 dapm_mark_dirty(w
, "force enable");
4187 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked
);
4190 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4191 * @dapm: DAPM context
4194 * Enables input/output pin regardless of any other state. This is
4195 * intended for use with microphone bias supplies used in microphone
4198 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4199 * do any widget power switching.
4201 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context
*dapm
,
4206 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4208 ret
= snd_soc_dapm_force_enable_pin_unlocked(dapm
, pin
);
4210 mutex_unlock(&dapm
->card
->dapm_mutex
);
4214 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
4217 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4218 * @dapm: DAPM context
4221 * Disables input/output pin and its parents or children widgets.
4223 * Requires external locking.
4225 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4226 * do any widget power switching.
4228 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
4231 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
4233 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked
);
4236 * snd_soc_dapm_disable_pin - disable pin.
4237 * @dapm: DAPM context
4240 * Disables input/output pin and its parents or children widgets.
4242 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4243 * do any widget power switching.
4245 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context
*dapm
,
4250 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4252 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 0);
4254 mutex_unlock(&dapm
->card
->dapm_mutex
);
4258 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
4261 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4262 * @dapm: DAPM context
4265 * Marks the specified pin as being not connected, disabling it along
4266 * any parent or child widgets. At present this is identical to
4267 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4268 * additional things such as disabling controls which only affect
4269 * paths through the pin.
4271 * Requires external locking.
4273 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4274 * do any widget power switching.
4276 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
4279 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
4281 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked
);
4284 * snd_soc_dapm_nc_pin - permanently disable pin.
4285 * @dapm: DAPM context
4288 * Marks the specified pin as being not connected, disabling it along
4289 * any parent or child widgets. At present this is identical to
4290 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4291 * additional things such as disabling controls which only affect
4292 * paths through the pin.
4294 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4295 * do any widget power switching.
4297 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
4301 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
4303 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 0);
4305 mutex_unlock(&dapm
->card
->dapm_mutex
);
4309 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
4312 * snd_soc_dapm_get_pin_status - get audio pin status
4313 * @dapm: DAPM context
4314 * @pin: audio signal pin endpoint (or start point)
4316 * Get audio pin status - connected or disconnected.
4318 * Returns 1 for connected otherwise 0.
4320 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context
*dapm
,
4323 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
4326 return w
->connected
;
4330 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
4333 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4334 * @dapm: DAPM context
4335 * @pin: audio signal pin endpoint (or start point)
4337 * Mark the given endpoint or pin as ignoring suspend. When the
4338 * system is disabled a path between two endpoints flagged as ignoring
4339 * suspend will not be disabled. The path must already be enabled via
4340 * normal means at suspend time, it will not be turned on if it was not
4343 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context
*dapm
,
4346 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, false);
4349 dev_err(dapm
->dev
, "ASoC: unknown pin %s\n", pin
);
4353 w
->ignore_suspend
= 1;
4357 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
4360 * snd_soc_dapm_free - free dapm resources
4361 * @dapm: DAPM context
4363 * Free all dapm widgets and resources.
4365 void snd_soc_dapm_free(struct snd_soc_dapm_context
*dapm
)
4367 dapm_debugfs_cleanup(dapm
);
4368 dapm_free_widgets(dapm
);
4369 list_del(&dapm
->list
);
4371 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
4373 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context
*dapm
)
4375 struct snd_soc_card
*card
= dapm
->card
;
4376 struct snd_soc_dapm_widget
*w
;
4377 LIST_HEAD(down_list
);
4380 mutex_lock(&card
->dapm_mutex
);
4382 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
4383 if (w
->dapm
!= dapm
)
4386 dapm_seq_insert(w
, &down_list
, false);
4392 /* If there were no widgets to power down we're already in
4396 if (dapm
->bias_level
== SND_SOC_BIAS_ON
)
4397 snd_soc_dapm_set_bias_level(dapm
,
4398 SND_SOC_BIAS_PREPARE
);
4399 dapm_seq_run(card
, &down_list
, 0, false);
4400 if (dapm
->bias_level
== SND_SOC_BIAS_PREPARE
)
4401 snd_soc_dapm_set_bias_level(dapm
,
4402 SND_SOC_BIAS_STANDBY
);
4405 mutex_unlock(&card
->dapm_mutex
);
4409 * snd_soc_dapm_shutdown - callback for system shutdown
4411 void snd_soc_dapm_shutdown(struct snd_soc_card
*card
)
4413 struct snd_soc_dapm_context
*dapm
;
4415 list_for_each_entry(dapm
, &card
->dapm_list
, list
) {
4416 if (dapm
!= &card
->dapm
) {
4417 soc_dapm_shutdown_dapm(dapm
);
4418 if (dapm
->bias_level
== SND_SOC_BIAS_STANDBY
)
4419 snd_soc_dapm_set_bias_level(dapm
,
4424 soc_dapm_shutdown_dapm(&card
->dapm
);
4425 if (card
->dapm
.bias_level
== SND_SOC_BIAS_STANDBY
)
4426 snd_soc_dapm_set_bias_level(&card
->dapm
,
4430 /* Module information */
4431 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4432 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4433 MODULE_LICENSE("GPL");