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 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context
*dapm
,
51 struct snd_soc_dapm_widget
*wsource
, struct snd_soc_dapm_widget
*wsink
,
53 int (*connected
)(struct snd_soc_dapm_widget
*source
,
54 struct snd_soc_dapm_widget
*sink
));
55 static struct snd_soc_dapm_widget
*
56 snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
57 const struct snd_soc_dapm_widget
*widget
);
59 /* dapm power sequences - make this per codec in the future */
60 static int dapm_up_seq
[] = {
61 [snd_soc_dapm_pre
] = 0,
62 [snd_soc_dapm_regulator_supply
] = 1,
63 [snd_soc_dapm_clock_supply
] = 1,
64 [snd_soc_dapm_supply
] = 2,
65 [snd_soc_dapm_micbias
] = 3,
66 [snd_soc_dapm_dai_link
] = 2,
67 [snd_soc_dapm_dai_in
] = 4,
68 [snd_soc_dapm_dai_out
] = 4,
69 [snd_soc_dapm_aif_in
] = 4,
70 [snd_soc_dapm_aif_out
] = 4,
71 [snd_soc_dapm_mic
] = 5,
72 [snd_soc_dapm_mux
] = 6,
73 [snd_soc_dapm_dac
] = 7,
74 [snd_soc_dapm_switch
] = 8,
75 [snd_soc_dapm_mixer
] = 8,
76 [snd_soc_dapm_mixer_named_ctl
] = 8,
77 [snd_soc_dapm_pga
] = 9,
78 [snd_soc_dapm_adc
] = 10,
79 [snd_soc_dapm_out_drv
] = 11,
80 [snd_soc_dapm_hp
] = 11,
81 [snd_soc_dapm_spk
] = 11,
82 [snd_soc_dapm_line
] = 11,
83 [snd_soc_dapm_kcontrol
] = 12,
84 [snd_soc_dapm_post
] = 13,
87 static int dapm_down_seq
[] = {
88 [snd_soc_dapm_pre
] = 0,
89 [snd_soc_dapm_kcontrol
] = 1,
90 [snd_soc_dapm_adc
] = 2,
91 [snd_soc_dapm_hp
] = 3,
92 [snd_soc_dapm_spk
] = 3,
93 [snd_soc_dapm_line
] = 3,
94 [snd_soc_dapm_out_drv
] = 3,
95 [snd_soc_dapm_pga
] = 4,
96 [snd_soc_dapm_switch
] = 5,
97 [snd_soc_dapm_mixer_named_ctl
] = 5,
98 [snd_soc_dapm_mixer
] = 5,
99 [snd_soc_dapm_dac
] = 6,
100 [snd_soc_dapm_mic
] = 7,
101 [snd_soc_dapm_micbias
] = 8,
102 [snd_soc_dapm_mux
] = 9,
103 [snd_soc_dapm_aif_in
] = 10,
104 [snd_soc_dapm_aif_out
] = 10,
105 [snd_soc_dapm_dai_in
] = 10,
106 [snd_soc_dapm_dai_out
] = 10,
107 [snd_soc_dapm_dai_link
] = 11,
108 [snd_soc_dapm_supply
] = 12,
109 [snd_soc_dapm_clock_supply
] = 13,
110 [snd_soc_dapm_regulator_supply
] = 13,
111 [snd_soc_dapm_post
] = 14,
114 static void dapm_assert_locked(struct snd_soc_dapm_context
*dapm
)
116 if (dapm
->card
&& dapm
->card
->instantiated
)
117 lockdep_assert_held(&dapm
->card
->dapm_mutex
);
120 static void pop_wait(u32 pop_time
)
123 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time
));
126 static void pop_dbg(struct device
*dev
, u32 pop_time
, const char *fmt
, ...)
134 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
139 vsnprintf(buf
, PAGE_SIZE
, fmt
, args
);
140 dev_info(dev
, "%s", buf
);
146 static bool dapm_dirty_widget(struct snd_soc_dapm_widget
*w
)
148 return !list_empty(&w
->dirty
);
151 static void dapm_mark_dirty(struct snd_soc_dapm_widget
*w
, const char *reason
)
153 dapm_assert_locked(w
->dapm
);
155 if (!dapm_dirty_widget(w
)) {
156 dev_vdbg(w
->dapm
->dev
, "Marking %s dirty due to %s\n",
158 list_add_tail(&w
->dirty
, &w
->dapm
->card
->dapm_dirty
);
162 void dapm_mark_io_dirty(struct snd_soc_dapm_context
*dapm
)
164 struct snd_soc_card
*card
= dapm
->card
;
165 struct snd_soc_dapm_widget
*w
;
167 mutex_lock(&card
->dapm_mutex
);
169 list_for_each_entry(w
, &card
->widgets
, list
) {
171 case snd_soc_dapm_input
:
172 case snd_soc_dapm_output
:
173 dapm_mark_dirty(w
, "Rechecking inputs and outputs");
180 mutex_unlock(&card
->dapm_mutex
);
182 EXPORT_SYMBOL_GPL(dapm_mark_io_dirty
);
184 /* create a new dapm widget */
185 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
186 const struct snd_soc_dapm_widget
*_widget
)
188 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
191 struct dapm_kcontrol_data
{
193 struct snd_soc_dapm_widget
*widget
;
194 struct list_head paths
;
195 struct snd_soc_dapm_widget_list
*wlist
;
198 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget
*widget
,
199 struct snd_kcontrol
*kcontrol
)
201 struct dapm_kcontrol_data
*data
;
202 struct soc_mixer_control
*mc
;
204 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
206 dev_err(widget
->dapm
->dev
,
207 "ASoC: can't allocate kcontrol data for %s\n",
212 INIT_LIST_HEAD(&data
->paths
);
214 switch (widget
->id
) {
215 case snd_soc_dapm_switch
:
216 case snd_soc_dapm_mixer
:
217 case snd_soc_dapm_mixer_named_ctl
:
218 mc
= (struct soc_mixer_control
*)kcontrol
->private_value
;
220 if (mc
->autodisable
) {
221 struct snd_soc_dapm_widget
template;
223 memset(&template, 0, sizeof(template));
224 template.reg
= mc
->reg
;
225 template.mask
= (1 << fls(mc
->max
)) - 1;
226 template.shift
= mc
->shift
;
228 template.off_val
= mc
->max
;
230 template.off_val
= 0;
231 template.on_val
= template.off_val
;
232 template.id
= snd_soc_dapm_kcontrol
;
233 template.name
= kcontrol
->id
.name
;
235 data
->value
= template.on_val
;
237 data
->widget
= snd_soc_dapm_new_control(widget
->dapm
,
249 kcontrol
->private_data
= data
;
254 static void dapm_kcontrol_free(struct snd_kcontrol
*kctl
)
256 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kctl
);
261 static struct snd_soc_dapm_widget_list
*dapm_kcontrol_get_wlist(
262 const struct snd_kcontrol
*kcontrol
)
264 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
269 static int dapm_kcontrol_add_widget(struct snd_kcontrol
*kcontrol
,
270 struct snd_soc_dapm_widget
*widget
)
272 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
273 struct snd_soc_dapm_widget_list
*new_wlist
;
277 n
= data
->wlist
->num_widgets
+ 1;
281 new_wlist
= krealloc(data
->wlist
,
282 sizeof(*new_wlist
) + sizeof(widget
) * n
, GFP_KERNEL
);
286 new_wlist
->widgets
[n
- 1] = widget
;
287 new_wlist
->num_widgets
= n
;
289 data
->wlist
= new_wlist
;
294 static void dapm_kcontrol_add_path(const struct snd_kcontrol
*kcontrol
,
295 struct snd_soc_dapm_path
*path
)
297 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
299 list_add_tail(&path
->list_kcontrol
, &data
->paths
);
302 snd_soc_dapm_add_path(data
->widget
->dapm
, data
->widget
,
303 path
->source
, NULL
, NULL
);
307 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol
*kcontrol
)
309 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
314 return data
->widget
->power
;
317 static struct list_head
*dapm_kcontrol_get_path_list(
318 const struct snd_kcontrol
*kcontrol
)
320 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
325 #define dapm_kcontrol_for_each_path(path, kcontrol) \
326 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
329 static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol
*kcontrol
)
331 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
336 static bool dapm_kcontrol_set_value(const struct snd_kcontrol
*kcontrol
,
339 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
341 if (data
->value
== value
)
345 data
->widget
->on_val
= value
;
353 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
354 * @kcontrol: The kcontrol
356 struct snd_soc_codec
*snd_soc_dapm_kcontrol_codec(struct snd_kcontrol
*kcontrol
)
358 return dapm_kcontrol_get_wlist(kcontrol
)->widgets
[0]->codec
;
360 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec
);
362 static void dapm_reset(struct snd_soc_card
*card
)
364 struct snd_soc_dapm_widget
*w
;
366 lockdep_assert_held(&card
->dapm_mutex
);
368 memset(&card
->dapm_stats
, 0, sizeof(card
->dapm_stats
));
370 list_for_each_entry(w
, &card
->widgets
, list
) {
371 w
->new_power
= w
->power
;
372 w
->power_checked
= false;
378 static const char *soc_dapm_prefix(struct snd_soc_dapm_context
*dapm
)
380 if (!dapm
->component
)
382 return dapm
->component
->name_prefix
;
385 static int soc_widget_read(struct snd_soc_dapm_widget
*w
, int reg
,
388 if (!w
->dapm
->component
)
390 return snd_soc_component_read(w
->dapm
->component
, reg
, value
);
393 static int soc_widget_update_bits(struct snd_soc_dapm_widget
*w
,
394 int reg
, unsigned int mask
, unsigned int value
)
396 if (!w
->dapm
->component
)
398 return snd_soc_component_update_bits_async(w
->dapm
->component
, reg
,
402 static void soc_dapm_async_complete(struct snd_soc_dapm_context
*dapm
)
405 snd_soc_component_async_complete(dapm
->component
);
409 * snd_soc_dapm_set_bias_level - set the bias level for the system
410 * @dapm: DAPM context
411 * @level: level to configure
413 * Configure the bias (power) levels for the SoC audio device.
415 * Returns 0 for success else error.
417 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context
*dapm
,
418 enum snd_soc_bias_level level
)
420 struct snd_soc_card
*card
= dapm
->card
;
423 trace_snd_soc_bias_level_start(card
, level
);
425 if (card
&& card
->set_bias_level
)
426 ret
= card
->set_bias_level(card
, dapm
, level
);
430 if (dapm
->set_bias_level
)
431 ret
= dapm
->set_bias_level(dapm
, level
);
432 else if (!card
|| dapm
!= &card
->dapm
)
433 dapm
->bias_level
= level
;
438 if (card
&& card
->set_bias_level_post
)
439 ret
= card
->set_bias_level_post(card
, dapm
, level
);
441 trace_snd_soc_bias_level_done(card
, level
);
446 /* connect mux widget to its interconnecting audio paths */
447 static int dapm_connect_mux(struct snd_soc_dapm_context
*dapm
,
448 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
449 struct snd_soc_dapm_path
*path
, const char *control_name
,
450 const struct snd_kcontrol_new
*kcontrol
)
452 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
453 unsigned int val
, item
;
456 if (e
->reg
!= SND_SOC_NOPM
) {
457 soc_widget_read(dest
, e
->reg
, &val
);
458 val
= (val
>> e
->shift_l
) & e
->mask
;
459 item
= snd_soc_enum_val_to_item(e
, val
);
461 /* since a virtual mux has no backing registers to
462 * decide which path to connect, it will try to match
463 * with the first enumeration. This is to ensure
464 * that the default mux choice (the first) will be
465 * correctly powered up during initialization.
470 for (i
= 0; i
< e
->items
; i
++) {
471 if (!(strcmp(control_name
, e
->texts
[i
]))) {
472 list_add(&path
->list
, &dapm
->card
->paths
);
473 list_add(&path
->list_sink
, &dest
->sources
);
474 list_add(&path
->list_source
, &src
->sinks
);
475 path
->name
= (char*)e
->texts
[i
];
487 /* set up initial codec paths */
488 static void dapm_set_mixer_path_status(struct snd_soc_dapm_widget
*w
,
489 struct snd_soc_dapm_path
*p
, int i
)
491 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
492 w
->kcontrol_news
[i
].private_value
;
493 unsigned int reg
= mc
->reg
;
494 unsigned int shift
= mc
->shift
;
495 unsigned int max
= mc
->max
;
496 unsigned int mask
= (1 << fls(max
)) - 1;
497 unsigned int invert
= mc
->invert
;
500 if (reg
!= SND_SOC_NOPM
) {
501 soc_widget_read(w
, reg
, &val
);
502 val
= (val
>> shift
) & mask
;
511 /* connect mixer widget to its interconnecting audio paths */
512 static int dapm_connect_mixer(struct snd_soc_dapm_context
*dapm
,
513 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
514 struct snd_soc_dapm_path
*path
, const char *control_name
)
518 /* search for mixer kcontrol */
519 for (i
= 0; i
< dest
->num_kcontrols
; i
++) {
520 if (!strcmp(control_name
, dest
->kcontrol_news
[i
].name
)) {
521 list_add(&path
->list
, &dapm
->card
->paths
);
522 list_add(&path
->list_sink
, &dest
->sources
);
523 list_add(&path
->list_source
, &src
->sinks
);
524 path
->name
= dest
->kcontrol_news
[i
].name
;
525 dapm_set_mixer_path_status(dest
, path
, i
);
532 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context
*dapm
,
533 struct snd_soc_dapm_widget
*kcontrolw
,
534 const struct snd_kcontrol_new
*kcontrol_new
,
535 struct snd_kcontrol
**kcontrol
)
537 struct snd_soc_dapm_widget
*w
;
542 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
543 if (w
== kcontrolw
|| w
->dapm
!= kcontrolw
->dapm
)
545 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
546 if (&w
->kcontrol_news
[i
] == kcontrol_new
) {
548 *kcontrol
= w
->kcontrols
[i
];
558 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
559 * create it. Either way, add the widget into the control's widget list
561 static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget
*w
,
564 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
565 struct snd_card
*card
= dapm
->card
->snd_card
;
569 struct snd_kcontrol
*kcontrol
;
570 bool wname_in_long_name
, kcname_in_long_name
;
575 prefix
= soc_dapm_prefix(dapm
);
577 prefix_len
= strlen(prefix
) + 1;
581 shared
= dapm_is_shared_kcontrol(dapm
, w
, &w
->kcontrol_news
[kci
],
586 wname_in_long_name
= false;
587 kcname_in_long_name
= true;
590 case snd_soc_dapm_switch
:
591 case snd_soc_dapm_mixer
:
592 wname_in_long_name
= true;
593 kcname_in_long_name
= true;
595 case snd_soc_dapm_mixer_named_ctl
:
596 wname_in_long_name
= false;
597 kcname_in_long_name
= true;
599 case snd_soc_dapm_mux
:
600 wname_in_long_name
= true;
601 kcname_in_long_name
= false;
608 if (wname_in_long_name
&& kcname_in_long_name
) {
610 * The control will get a prefix from the control
611 * creation process but we're also using the same
612 * prefix for widgets so cut the prefix off the
613 * front of the widget name.
615 long_name
= kasprintf(GFP_KERNEL
, "%s %s",
616 w
->name
+ prefix_len
,
617 w
->kcontrol_news
[kci
].name
);
618 if (long_name
== NULL
)
622 } else if (wname_in_long_name
) {
624 name
= w
->name
+ prefix_len
;
627 name
= w
->kcontrol_news
[kci
].name
;
630 kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[kci
], NULL
, name
,
635 kcontrol
->private_free
= dapm_kcontrol_free
;
637 ret
= dapm_kcontrol_data_alloc(w
, kcontrol
);
639 snd_ctl_free_one(kcontrol
);
643 ret
= snd_ctl_add(card
, kcontrol
);
646 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
652 ret
= dapm_kcontrol_add_widget(kcontrol
, w
);
656 w
->kcontrols
[kci
] = kcontrol
;
661 /* create new dapm mixer control */
662 static int dapm_new_mixer(struct snd_soc_dapm_widget
*w
)
665 struct snd_soc_dapm_path
*path
;
668 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
670 list_for_each_entry(path
, &w
->sources
, list_sink
) {
671 /* mixer/mux paths name must match control name */
672 if (path
->name
!= (char *)w
->kcontrol_news
[i
].name
)
675 if (w
->kcontrols
[i
]) {
676 dapm_kcontrol_add_path(w
->kcontrols
[i
], path
);
680 ret
= dapm_create_or_share_mixmux_kcontrol(w
, i
);
684 dapm_kcontrol_add_path(w
->kcontrols
[i
], path
);
691 /* create new dapm mux control */
692 static int dapm_new_mux(struct snd_soc_dapm_widget
*w
)
694 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
695 struct snd_soc_dapm_path
*path
;
698 if (w
->num_kcontrols
!= 1) {
700 "ASoC: mux %s has incorrect number of controls\n",
705 if (list_empty(&w
->sources
)) {
706 dev_err(dapm
->dev
, "ASoC: mux %s has no paths\n", w
->name
);
710 ret
= dapm_create_or_share_mixmux_kcontrol(w
, 0);
714 list_for_each_entry(path
, &w
->sources
, list_sink
)
715 dapm_kcontrol_add_path(w
->kcontrols
[0], path
);
720 /* create new dapm volume control */
721 static int dapm_new_pga(struct snd_soc_dapm_widget
*w
)
723 if (w
->num_kcontrols
)
724 dev_err(w
->dapm
->dev
,
725 "ASoC: PGA controls not supported: '%s'\n", w
->name
);
730 /* reset 'walked' bit for each dapm path */
731 static void dapm_clear_walk_output(struct snd_soc_dapm_context
*dapm
,
732 struct list_head
*sink
)
734 struct snd_soc_dapm_path
*p
;
736 list_for_each_entry(p
, sink
, list_source
) {
739 dapm_clear_walk_output(dapm
, &p
->sink
->sinks
);
744 static void dapm_clear_walk_input(struct snd_soc_dapm_context
*dapm
,
745 struct list_head
*source
)
747 struct snd_soc_dapm_path
*p
;
749 list_for_each_entry(p
, source
, list_sink
) {
752 dapm_clear_walk_input(dapm
, &p
->source
->sources
);
758 /* We implement power down on suspend by checking the power state of
759 * the ALSA card - when we are suspending the ALSA state for the card
762 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
764 int level
= snd_power_get_state(widget
->dapm
->card
->snd_card
);
767 case SNDRV_CTL_POWER_D3hot
:
768 case SNDRV_CTL_POWER_D3cold
:
769 if (widget
->ignore_suspend
)
770 dev_dbg(widget
->dapm
->dev
, "ASoC: %s ignoring suspend\n",
772 return widget
->ignore_suspend
;
778 /* add widget to list if it's not already in the list */
779 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list
**list
,
780 struct snd_soc_dapm_widget
*w
)
782 struct snd_soc_dapm_widget_list
*wlist
;
783 int wlistsize
, wlistentries
, i
;
790 /* is this widget already in the list */
791 for (i
= 0; i
< wlist
->num_widgets
; i
++) {
792 if (wlist
->widgets
[i
] == w
)
796 /* allocate some new space */
797 wlistentries
= wlist
->num_widgets
+ 1;
798 wlistsize
= sizeof(struct snd_soc_dapm_widget_list
) +
799 wlistentries
* sizeof(struct snd_soc_dapm_widget
*);
800 *list
= krealloc(wlist
, wlistsize
, GFP_KERNEL
);
802 dev_err(w
->dapm
->dev
, "ASoC: can't allocate widget list for %s\n",
808 /* insert the widget */
809 dev_dbg(w
->dapm
->dev
, "ASoC: added %s in widget list pos %d\n",
810 w
->name
, wlist
->num_widgets
);
812 wlist
->widgets
[wlist
->num_widgets
] = w
;
813 wlist
->num_widgets
++;
818 * Recursively check for a completed path to an active or physically connected
819 * output widget. Returns number of complete paths.
821 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
,
822 struct snd_soc_dapm_widget_list
**list
)
824 struct snd_soc_dapm_path
*path
;
827 if (widget
->outputs
>= 0)
828 return widget
->outputs
;
830 DAPM_UPDATE_STAT(widget
, path_checks
);
832 switch (widget
->id
) {
833 case snd_soc_dapm_supply
:
834 case snd_soc_dapm_regulator_supply
:
835 case snd_soc_dapm_clock_supply
:
836 case snd_soc_dapm_kcontrol
:
842 switch (widget
->id
) {
843 case snd_soc_dapm_adc
:
844 case snd_soc_dapm_aif_out
:
845 case snd_soc_dapm_dai_out
:
846 if (widget
->active
) {
847 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
848 return widget
->outputs
;
854 if (widget
->connected
) {
855 /* connected pin ? */
856 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
) {
857 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
858 return widget
->outputs
;
861 /* connected jack or spk ? */
862 if (widget
->id
== snd_soc_dapm_hp
||
863 widget
->id
== snd_soc_dapm_spk
||
864 (widget
->id
== snd_soc_dapm_line
&&
865 !list_empty(&widget
->sources
))) {
866 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
867 return widget
->outputs
;
871 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
872 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
883 trace_snd_soc_dapm_output_path(widget
, path
);
885 if (path
->sink
&& path
->connect
) {
889 /* do we need to add this widget to the list ? */
892 err
= dapm_list_add_widget(list
, path
->sink
);
894 dev_err(widget
->dapm
->dev
,
895 "ASoC: could not add widget %s\n",
902 con
+= is_connected_output_ep(path
->sink
, list
);
908 widget
->outputs
= con
;
914 * Recursively check for a completed path to an active or physically connected
915 * input widget. Returns number of complete paths.
917 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
,
918 struct snd_soc_dapm_widget_list
**list
)
920 struct snd_soc_dapm_path
*path
;
923 if (widget
->inputs
>= 0)
924 return widget
->inputs
;
926 DAPM_UPDATE_STAT(widget
, path_checks
);
928 switch (widget
->id
) {
929 case snd_soc_dapm_supply
:
930 case snd_soc_dapm_regulator_supply
:
931 case snd_soc_dapm_clock_supply
:
932 case snd_soc_dapm_kcontrol
:
938 /* active stream ? */
939 switch (widget
->id
) {
940 case snd_soc_dapm_dac
:
941 case snd_soc_dapm_aif_in
:
942 case snd_soc_dapm_dai_in
:
943 if (widget
->active
) {
944 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
945 return widget
->inputs
;
951 if (widget
->connected
) {
952 /* connected pin ? */
953 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
) {
954 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
955 return widget
->inputs
;
958 /* connected VMID/Bias for lower pops */
959 if (widget
->id
== snd_soc_dapm_vmid
) {
960 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
961 return widget
->inputs
;
964 /* connected jack ? */
965 if (widget
->id
== snd_soc_dapm_mic
||
966 (widget
->id
== snd_soc_dapm_line
&&
967 !list_empty(&widget
->sinks
))) {
968 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
969 return widget
->inputs
;
972 /* signal generator */
973 if (widget
->id
== snd_soc_dapm_siggen
) {
974 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
975 return widget
->inputs
;
979 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
980 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
991 trace_snd_soc_dapm_input_path(widget
, path
);
993 if (path
->source
&& path
->connect
) {
997 /* do we need to add this widget to the list ? */
1000 err
= dapm_list_add_widget(list
, path
->source
);
1002 dev_err(widget
->dapm
->dev
,
1003 "ASoC: could not add widget %s\n",
1010 con
+= is_connected_input_ep(path
->source
, list
);
1016 widget
->inputs
= con
;
1022 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1023 * @dai: the soc DAI.
1024 * @stream: stream direction.
1025 * @list: list of active widgets for this stream.
1027 * Queries DAPM graph as to whether an valid audio stream path exists for
1028 * the initial stream specified by name. This takes into account
1029 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1031 * Returns the number of valid paths or negative error.
1033 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai
*dai
, int stream
,
1034 struct snd_soc_dapm_widget_list
**list
)
1036 struct snd_soc_card
*card
= dai
->card
;
1039 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
1042 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1043 paths
= is_connected_output_ep(dai
->playback_widget
, list
);
1044 dapm_clear_walk_output(&card
->dapm
,
1045 &dai
->playback_widget
->sinks
);
1047 paths
= is_connected_input_ep(dai
->capture_widget
, list
);
1048 dapm_clear_walk_input(&card
->dapm
,
1049 &dai
->capture_widget
->sources
);
1052 trace_snd_soc_dapm_connected(paths
, stream
);
1053 mutex_unlock(&card
->dapm_mutex
);
1059 * Handler for regulator supply widget.
1061 int dapm_regulator_event(struct snd_soc_dapm_widget
*w
,
1062 struct snd_kcontrol
*kcontrol
, int event
)
1066 soc_dapm_async_complete(w
->dapm
);
1068 if (SND_SOC_DAPM_EVENT_ON(event
)) {
1069 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
1070 ret
= regulator_allow_bypass(w
->regulator
, false);
1072 dev_warn(w
->dapm
->dev
,
1073 "ASoC: Failed to unbypass %s: %d\n",
1077 return regulator_enable(w
->regulator
);
1079 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
1080 ret
= regulator_allow_bypass(w
->regulator
, true);
1082 dev_warn(w
->dapm
->dev
,
1083 "ASoC: Failed to bypass %s: %d\n",
1087 return regulator_disable_deferred(w
->regulator
, w
->shift
);
1090 EXPORT_SYMBOL_GPL(dapm_regulator_event
);
1093 * Handler for clock supply widget.
1095 int dapm_clock_event(struct snd_soc_dapm_widget
*w
,
1096 struct snd_kcontrol
*kcontrol
, int event
)
1101 soc_dapm_async_complete(w
->dapm
);
1103 #ifdef CONFIG_HAVE_CLK
1104 if (SND_SOC_DAPM_EVENT_ON(event
)) {
1105 return clk_prepare_enable(w
->clk
);
1107 clk_disable_unprepare(w
->clk
);
1113 EXPORT_SYMBOL_GPL(dapm_clock_event
);
1115 static int dapm_widget_power_check(struct snd_soc_dapm_widget
*w
)
1117 if (w
->power_checked
)
1118 return w
->new_power
;
1123 w
->new_power
= w
->power_check(w
);
1125 w
->power_checked
= true;
1127 return w
->new_power
;
1130 /* Generic check to see if a widget should be powered.
1132 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
1136 DAPM_UPDATE_STAT(w
, power_checks
);
1138 in
= is_connected_input_ep(w
, NULL
);
1139 dapm_clear_walk_input(w
->dapm
, &w
->sources
);
1140 out
= is_connected_output_ep(w
, NULL
);
1141 dapm_clear_walk_output(w
->dapm
, &w
->sinks
);
1142 return out
!= 0 && in
!= 0;
1145 /* Check to see if an ADC has power */
1146 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
1150 DAPM_UPDATE_STAT(w
, power_checks
);
1153 in
= is_connected_input_ep(w
, NULL
);
1154 dapm_clear_walk_input(w
->dapm
, &w
->sources
);
1157 return dapm_generic_check_power(w
);
1161 /* Check to see if a DAC has power */
1162 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
1166 DAPM_UPDATE_STAT(w
, power_checks
);
1169 out
= is_connected_output_ep(w
, NULL
);
1170 dapm_clear_walk_output(w
->dapm
, &w
->sinks
);
1173 return dapm_generic_check_power(w
);
1177 /* Check to see if a power supply is needed */
1178 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
1180 struct snd_soc_dapm_path
*path
;
1182 DAPM_UPDATE_STAT(w
, power_checks
);
1184 /* Check if one of our outputs is connected */
1185 list_for_each_entry(path
, &w
->sinks
, list_source
) {
1186 DAPM_UPDATE_STAT(w
, neighbour_checks
);
1191 if (path
->connected
&&
1192 !path
->connected(path
->source
, path
->sink
))
1198 if (dapm_widget_power_check(path
->sink
))
1205 static int dapm_always_on_check_power(struct snd_soc_dapm_widget
*w
)
1210 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
1211 struct snd_soc_dapm_widget
*b
,
1219 sort
= dapm_down_seq
;
1221 if (sort
[a
->id
] != sort
[b
->id
])
1222 return sort
[a
->id
] - sort
[b
->id
];
1223 if (a
->subseq
!= b
->subseq
) {
1225 return a
->subseq
- b
->subseq
;
1227 return b
->subseq
- a
->subseq
;
1229 if (a
->reg
!= b
->reg
)
1230 return a
->reg
- b
->reg
;
1231 if (a
->dapm
!= b
->dapm
)
1232 return (unsigned long)a
->dapm
- (unsigned long)b
->dapm
;
1237 /* Insert a widget in order into a DAPM power sequence. */
1238 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
1239 struct list_head
*list
,
1242 struct snd_soc_dapm_widget
*w
;
1244 list_for_each_entry(w
, list
, power_list
)
1245 if (dapm_seq_compare(new_widget
, w
, power_up
) < 0) {
1246 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
1250 list_add_tail(&new_widget
->power_list
, list
);
1253 static void dapm_seq_check_event(struct snd_soc_card
*card
,
1254 struct snd_soc_dapm_widget
*w
, int event
)
1256 const char *ev_name
;
1260 case SND_SOC_DAPM_PRE_PMU
:
1261 ev_name
= "PRE_PMU";
1264 case SND_SOC_DAPM_POST_PMU
:
1265 ev_name
= "POST_PMU";
1268 case SND_SOC_DAPM_PRE_PMD
:
1269 ev_name
= "PRE_PMD";
1272 case SND_SOC_DAPM_POST_PMD
:
1273 ev_name
= "POST_PMD";
1276 case SND_SOC_DAPM_WILL_PMU
:
1277 ev_name
= "WILL_PMU";
1280 case SND_SOC_DAPM_WILL_PMD
:
1281 ev_name
= "WILL_PMD";
1285 WARN(1, "Unknown event %d\n", event
);
1289 if (w
->new_power
!= power
)
1292 if (w
->event
&& (w
->event_flags
& event
)) {
1293 pop_dbg(w
->dapm
->dev
, card
->pop_time
, "pop test : %s %s\n",
1295 soc_dapm_async_complete(w
->dapm
);
1296 trace_snd_soc_dapm_widget_event_start(w
, event
);
1297 ret
= w
->event(w
, NULL
, event
);
1298 trace_snd_soc_dapm_widget_event_done(w
, event
);
1300 dev_err(w
->dapm
->dev
, "ASoC: %s: %s event failed: %d\n",
1301 ev_name
, w
->name
, ret
);
1305 /* Apply the coalesced changes from a DAPM sequence */
1306 static void dapm_seq_run_coalesced(struct snd_soc_card
*card
,
1307 struct list_head
*pending
)
1309 struct snd_soc_dapm_widget
*w
;
1311 unsigned int value
= 0;
1312 unsigned int mask
= 0;
1314 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
1317 list_for_each_entry(w
, pending
, power_list
) {
1318 WARN_ON(reg
!= w
->reg
);
1319 w
->power
= w
->new_power
;
1321 mask
|= w
->mask
<< w
->shift
;
1323 value
|= w
->on_val
<< w
->shift
;
1325 value
|= w
->off_val
<< w
->shift
;
1327 pop_dbg(w
->dapm
->dev
, card
->pop_time
,
1328 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1329 w
->name
, reg
, value
, mask
);
1331 /* Check for events */
1332 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_PRE_PMU
);
1333 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_PRE_PMD
);
1337 /* Any widget will do, they should all be updating the
1340 w
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
1343 pop_dbg(w
->dapm
->dev
, card
->pop_time
,
1344 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1345 value
, mask
, reg
, card
->pop_time
);
1346 pop_wait(card
->pop_time
);
1347 soc_widget_update_bits(w
, reg
, mask
, value
);
1350 list_for_each_entry(w
, pending
, power_list
) {
1351 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_POST_PMU
);
1352 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_POST_PMD
);
1356 /* Apply a DAPM power sequence.
1358 * We walk over a pre-sorted list of widgets to apply power to. In
1359 * order to minimise the number of writes to the device required
1360 * multiple widgets will be updated in a single write where possible.
1361 * Currently anything that requires more than a single write is not
1364 static void dapm_seq_run(struct snd_soc_card
*card
,
1365 struct list_head
*list
, int event
, bool power_up
)
1367 struct snd_soc_dapm_widget
*w
, *n
;
1368 struct snd_soc_dapm_context
*d
;
1371 int cur_subseq
= -1;
1372 int cur_reg
= SND_SOC_NOPM
;
1373 struct snd_soc_dapm_context
*cur_dapm
= NULL
;
1380 sort
= dapm_down_seq
;
1382 list_for_each_entry_safe(w
, n
, list
, power_list
) {
1385 /* Do we need to apply any queued changes? */
1386 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
||
1387 w
->dapm
!= cur_dapm
|| w
->subseq
!= cur_subseq
) {
1388 if (!list_empty(&pending
))
1389 dapm_seq_run_coalesced(card
, &pending
);
1391 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1392 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1393 if (sort
[i
] == cur_sort
)
1394 cur_dapm
->seq_notifier(cur_dapm
,
1399 if (cur_dapm
&& w
->dapm
!= cur_dapm
)
1400 soc_dapm_async_complete(cur_dapm
);
1402 INIT_LIST_HEAD(&pending
);
1404 cur_subseq
= INT_MIN
;
1405 cur_reg
= SND_SOC_NOPM
;
1410 case snd_soc_dapm_pre
:
1412 list_for_each_entry_safe_continue(w
, n
, list
,
1415 if (event
== SND_SOC_DAPM_STREAM_START
)
1417 NULL
, SND_SOC_DAPM_PRE_PMU
);
1418 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1420 NULL
, SND_SOC_DAPM_PRE_PMD
);
1423 case snd_soc_dapm_post
:
1425 list_for_each_entry_safe_continue(w
, n
, list
,
1428 if (event
== SND_SOC_DAPM_STREAM_START
)
1430 NULL
, SND_SOC_DAPM_POST_PMU
);
1431 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1433 NULL
, SND_SOC_DAPM_POST_PMD
);
1437 /* Queue it up for application */
1438 cur_sort
= sort
[w
->id
];
1439 cur_subseq
= w
->subseq
;
1442 list_move(&w
->power_list
, &pending
);
1447 dev_err(w
->dapm
->dev
,
1448 "ASoC: Failed to apply widget power: %d\n", ret
);
1451 if (!list_empty(&pending
))
1452 dapm_seq_run_coalesced(card
, &pending
);
1454 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1455 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1456 if (sort
[i
] == cur_sort
)
1457 cur_dapm
->seq_notifier(cur_dapm
,
1461 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1462 soc_dapm_async_complete(d
);
1466 static void dapm_widget_update(struct snd_soc_card
*card
)
1468 struct snd_soc_dapm_update
*update
= card
->update
;
1469 struct snd_soc_dapm_widget_list
*wlist
;
1470 struct snd_soc_dapm_widget
*w
= NULL
;
1474 if (!update
|| !dapm_kcontrol_is_powered(update
->kcontrol
))
1477 wlist
= dapm_kcontrol_get_wlist(update
->kcontrol
);
1479 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
1480 w
= wlist
->widgets
[wi
];
1482 if (w
->event
&& (w
->event_flags
& SND_SOC_DAPM_PRE_REG
)) {
1483 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_PRE_REG
);
1485 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM pre-event failed: %d\n",
1493 ret
= soc_widget_update_bits(w
, update
->reg
, update
->mask
, update
->val
);
1495 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM update failed: %d\n",
1498 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
1499 w
= wlist
->widgets
[wi
];
1501 if (w
->event
&& (w
->event_flags
& SND_SOC_DAPM_POST_REG
)) {
1502 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_POST_REG
);
1504 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM post-event failed: %d\n",
1510 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1511 * they're changing state.
1513 static void dapm_pre_sequence_async(void *data
, async_cookie_t cookie
)
1515 struct snd_soc_dapm_context
*d
= data
;
1518 /* If we're off and we're not supposed to be go into STANDBY */
1519 if (d
->bias_level
== SND_SOC_BIAS_OFF
&&
1520 d
->target_bias_level
!= SND_SOC_BIAS_OFF
) {
1522 pm_runtime_get_sync(d
->dev
);
1524 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1527 "ASoC: Failed to turn on bias: %d\n", ret
);
1530 /* Prepare for a transition to ON or away from ON */
1531 if ((d
->target_bias_level
== SND_SOC_BIAS_ON
&&
1532 d
->bias_level
!= SND_SOC_BIAS_ON
) ||
1533 (d
->target_bias_level
!= SND_SOC_BIAS_ON
&&
1534 d
->bias_level
== SND_SOC_BIAS_ON
)) {
1535 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_PREPARE
);
1538 "ASoC: Failed to prepare bias: %d\n", ret
);
1542 /* Async callback run prior to DAPM sequences - brings to their final
1545 static void dapm_post_sequence_async(void *data
, async_cookie_t cookie
)
1547 struct snd_soc_dapm_context
*d
= data
;
1550 /* If we just powered the last thing off drop to standby bias */
1551 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1552 (d
->target_bias_level
== SND_SOC_BIAS_STANDBY
||
1553 d
->target_bias_level
== SND_SOC_BIAS_OFF
)) {
1554 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1556 dev_err(d
->dev
, "ASoC: Failed to apply standby bias: %d\n",
1560 /* If we're in standby and can support bias off then do that */
1561 if (d
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1562 d
->target_bias_level
== SND_SOC_BIAS_OFF
) {
1563 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_OFF
);
1565 dev_err(d
->dev
, "ASoC: Failed to turn off bias: %d\n",
1569 pm_runtime_put(d
->dev
);
1572 /* If we just powered up then move to active bias */
1573 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1574 d
->target_bias_level
== SND_SOC_BIAS_ON
) {
1575 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_ON
);
1577 dev_err(d
->dev
, "ASoC: Failed to apply active bias: %d\n",
1582 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget
*peer
,
1583 bool power
, bool connect
)
1585 /* If a connection is being made or broken then that update
1586 * will have marked the peer dirty, otherwise the widgets are
1587 * not connected and this update has no impact. */
1591 /* If the peer is already in the state we're moving to then we
1592 * won't have an impact on it. */
1593 if (power
!= peer
->power
)
1594 dapm_mark_dirty(peer
, "peer state change");
1597 static void dapm_widget_set_power(struct snd_soc_dapm_widget
*w
, bool power
,
1598 struct list_head
*up_list
,
1599 struct list_head
*down_list
)
1601 struct snd_soc_dapm_path
*path
;
1603 if (w
->power
== power
)
1606 trace_snd_soc_dapm_widget_power(w
, power
);
1608 /* If we changed our power state perhaps our neigbours changed
1611 list_for_each_entry(path
, &w
->sources
, list_sink
) {
1613 dapm_widget_set_peer_power(path
->source
, power
,
1618 case snd_soc_dapm_supply
:
1619 case snd_soc_dapm_regulator_supply
:
1620 case snd_soc_dapm_clock_supply
:
1621 case snd_soc_dapm_kcontrol
:
1622 /* Supplies can't affect their outputs, only their inputs */
1625 list_for_each_entry(path
, &w
->sinks
, list_source
) {
1627 dapm_widget_set_peer_power(path
->sink
, power
,
1635 dapm_seq_insert(w
, up_list
, true);
1637 dapm_seq_insert(w
, down_list
, false);
1640 static void dapm_power_one_widget(struct snd_soc_dapm_widget
*w
,
1641 struct list_head
*up_list
,
1642 struct list_head
*down_list
)
1647 case snd_soc_dapm_pre
:
1648 dapm_seq_insert(w
, down_list
, false);
1650 case snd_soc_dapm_post
:
1651 dapm_seq_insert(w
, up_list
, true);
1655 power
= dapm_widget_power_check(w
);
1657 dapm_widget_set_power(w
, power
, up_list
, down_list
);
1663 * Scan each dapm widget for complete audio path.
1664 * A complete path is a route that has valid endpoints i.e.:-
1666 * o DAC to output pin.
1667 * o Input Pin to ADC.
1668 * o Input pin to Output pin (bypass, sidetone)
1669 * o DAC to ADC (loopback).
1671 static int dapm_power_widgets(struct snd_soc_card
*card
, int event
)
1673 struct snd_soc_dapm_widget
*w
;
1674 struct snd_soc_dapm_context
*d
;
1676 LIST_HEAD(down_list
);
1677 ASYNC_DOMAIN_EXCLUSIVE(async_domain
);
1678 enum snd_soc_bias_level bias
;
1680 lockdep_assert_held(&card
->dapm_mutex
);
1682 trace_snd_soc_dapm_start(card
);
1684 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1685 if (d
->idle_bias_off
)
1686 d
->target_bias_level
= SND_SOC_BIAS_OFF
;
1688 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1693 /* Check which widgets we need to power and store them in
1694 * lists indicating if they should be powered up or down. We
1695 * only check widgets that have been flagged as dirty but note
1696 * that new widgets may be added to the dirty list while we
1699 list_for_each_entry(w
, &card
->dapm_dirty
, dirty
) {
1700 dapm_power_one_widget(w
, &up_list
, &down_list
);
1703 list_for_each_entry(w
, &card
->widgets
, list
) {
1705 case snd_soc_dapm_pre
:
1706 case snd_soc_dapm_post
:
1707 /* These widgets always need to be powered */
1710 list_del_init(&w
->dirty
);
1717 /* Supplies and micbiases only bring the
1718 * context up to STANDBY as unless something
1719 * else is active and passing audio they
1720 * generally don't require full power. Signal
1721 * generators are virtual pins and have no
1722 * power impact themselves.
1725 case snd_soc_dapm_siggen
:
1726 case snd_soc_dapm_vmid
:
1728 case snd_soc_dapm_supply
:
1729 case snd_soc_dapm_regulator_supply
:
1730 case snd_soc_dapm_clock_supply
:
1731 case snd_soc_dapm_micbias
:
1732 if (d
->target_bias_level
< SND_SOC_BIAS_STANDBY
)
1733 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1736 d
->target_bias_level
= SND_SOC_BIAS_ON
;
1743 /* Force all contexts in the card to the same bias state if
1744 * they're not ground referenced.
1746 bias
= SND_SOC_BIAS_OFF
;
1747 list_for_each_entry(d
, &card
->dapm_list
, list
)
1748 if (d
->target_bias_level
> bias
)
1749 bias
= d
->target_bias_level
;
1750 list_for_each_entry(d
, &card
->dapm_list
, list
)
1751 if (!d
->idle_bias_off
)
1752 d
->target_bias_level
= bias
;
1754 trace_snd_soc_dapm_walk_done(card
);
1756 /* Run card bias changes at first */
1757 dapm_pre_sequence_async(&card
->dapm
, 0);
1758 /* Run other bias changes in parallel */
1759 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1760 if (d
!= &card
->dapm
)
1761 async_schedule_domain(dapm_pre_sequence_async
, d
,
1764 async_synchronize_full_domain(&async_domain
);
1766 list_for_each_entry(w
, &down_list
, power_list
) {
1767 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_WILL_PMD
);
1770 list_for_each_entry(w
, &up_list
, power_list
) {
1771 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_WILL_PMU
);
1774 /* Power down widgets first; try to avoid amplifying pops. */
1775 dapm_seq_run(card
, &down_list
, event
, false);
1777 dapm_widget_update(card
);
1780 dapm_seq_run(card
, &up_list
, event
, true);
1782 /* Run all the bias changes in parallel */
1783 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1784 if (d
!= &card
->dapm
)
1785 async_schedule_domain(dapm_post_sequence_async
, d
,
1788 async_synchronize_full_domain(&async_domain
);
1789 /* Run card bias changes at last */
1790 dapm_post_sequence_async(&card
->dapm
, 0);
1792 /* do we need to notify any clients that DAPM event is complete */
1793 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1794 if (d
->stream_event
)
1795 d
->stream_event(d
, event
);
1798 pop_dbg(card
->dev
, card
->pop_time
,
1799 "DAPM sequencing finished, waiting %dms\n", card
->pop_time
);
1800 pop_wait(card
->pop_time
);
1802 trace_snd_soc_dapm_done(card
);
1807 #ifdef CONFIG_DEBUG_FS
1808 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1809 char __user
*user_buf
,
1810 size_t count
, loff_t
*ppos
)
1812 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1816 struct snd_soc_dapm_path
*p
= NULL
;
1818 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1822 in
= is_connected_input_ep(w
, NULL
);
1823 dapm_clear_walk_input(w
->dapm
, &w
->sources
);
1824 out
= is_connected_output_ep(w
, NULL
);
1825 dapm_clear_walk_output(w
->dapm
, &w
->sinks
);
1827 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s%s in %d out %d",
1828 w
->name
, w
->power
? "On" : "Off",
1829 w
->force
? " (forced)" : "", in
, out
);
1832 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1833 " - R%d(0x%x) mask 0x%x",
1834 w
->reg
, w
->reg
, w
->mask
<< w
->shift
);
1836 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
1839 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1841 w
->active
? "active" : "inactive");
1843 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1844 if (p
->connected
&& !p
->connected(w
, p
->source
))
1848 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1849 " in \"%s\" \"%s\"\n",
1850 p
->name
? p
->name
: "static",
1853 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1854 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1858 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1859 " out \"%s\" \"%s\"\n",
1860 p
->name
? p
->name
: "static",
1864 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1870 static const struct file_operations dapm_widget_power_fops
= {
1871 .open
= simple_open
,
1872 .read
= dapm_widget_power_read_file
,
1873 .llseek
= default_llseek
,
1876 static ssize_t
dapm_bias_read_file(struct file
*file
, char __user
*user_buf
,
1877 size_t count
, loff_t
*ppos
)
1879 struct snd_soc_dapm_context
*dapm
= file
->private_data
;
1882 switch (dapm
->bias_level
) {
1883 case SND_SOC_BIAS_ON
:
1886 case SND_SOC_BIAS_PREPARE
:
1887 level
= "Prepare\n";
1889 case SND_SOC_BIAS_STANDBY
:
1890 level
= "Standby\n";
1892 case SND_SOC_BIAS_OFF
:
1896 WARN(1, "Unknown bias_level %d\n", dapm
->bias_level
);
1897 level
= "Unknown\n";
1901 return simple_read_from_buffer(user_buf
, count
, ppos
, level
,
1905 static const struct file_operations dapm_bias_fops
= {
1906 .open
= simple_open
,
1907 .read
= dapm_bias_read_file
,
1908 .llseek
= default_llseek
,
1911 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
1912 struct dentry
*parent
)
1916 dapm
->debugfs_dapm
= debugfs_create_dir("dapm", parent
);
1918 if (!dapm
->debugfs_dapm
) {
1920 "ASoC: Failed to create DAPM debugfs directory\n");
1924 d
= debugfs_create_file("bias_level", 0444,
1925 dapm
->debugfs_dapm
, dapm
,
1929 "ASoC: Failed to create bias level debugfs file\n");
1932 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
1934 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
1937 if (!dapm
->debugfs_dapm
|| !w
->name
)
1940 d
= debugfs_create_file(w
->name
, 0444,
1941 dapm
->debugfs_dapm
, w
,
1942 &dapm_widget_power_fops
);
1944 dev_warn(w
->dapm
->dev
,
1945 "ASoC: Failed to create %s debugfs file\n",
1949 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
1951 debugfs_remove_recursive(dapm
->debugfs_dapm
);
1955 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
1956 struct dentry
*parent
)
1960 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
1964 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
1970 /* test and update the power status of a mux widget */
1971 static int soc_dapm_mux_update_power(struct snd_soc_card
*card
,
1972 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
)
1974 struct snd_soc_dapm_path
*path
;
1977 lockdep_assert_held(&card
->dapm_mutex
);
1979 /* find dapm widget path assoc with kcontrol */
1980 dapm_kcontrol_for_each_path(path
, kcontrol
) {
1981 if (!path
->name
|| !e
->texts
[mux
])
1985 /* we now need to match the string in the enum to the path */
1986 if (!(strcmp(path
->name
, e
->texts
[mux
]))) {
1987 path
->connect
= 1; /* new connection */
1988 dapm_mark_dirty(path
->source
, "mux connection");
1991 dapm_mark_dirty(path
->source
,
1992 "mux disconnection");
1993 path
->connect
= 0; /* old connection must be powered down */
1995 dapm_mark_dirty(path
->sink
, "mux change");
1999 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2004 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context
*dapm
,
2005 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
,
2006 struct snd_soc_dapm_update
*update
)
2008 struct snd_soc_card
*card
= dapm
->card
;
2011 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2012 card
->update
= update
;
2013 ret
= soc_dapm_mux_update_power(card
, kcontrol
, mux
, e
);
2014 card
->update
= NULL
;
2015 mutex_unlock(&card
->dapm_mutex
);
2017 soc_dpcm_runtime_update(card
);
2020 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power
);
2022 /* test and update the power status of a mixer or switch widget */
2023 static int soc_dapm_mixer_update_power(struct snd_soc_card
*card
,
2024 struct snd_kcontrol
*kcontrol
, int connect
)
2026 struct snd_soc_dapm_path
*path
;
2029 lockdep_assert_held(&card
->dapm_mutex
);
2031 /* find dapm widget path assoc with kcontrol */
2032 dapm_kcontrol_for_each_path(path
, kcontrol
) {
2034 path
->connect
= connect
;
2035 dapm_mark_dirty(path
->source
, "mixer connection");
2036 dapm_mark_dirty(path
->sink
, "mixer update");
2040 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2045 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context
*dapm
,
2046 struct snd_kcontrol
*kcontrol
, int connect
,
2047 struct snd_soc_dapm_update
*update
)
2049 struct snd_soc_card
*card
= dapm
->card
;
2052 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2053 card
->update
= update
;
2054 ret
= soc_dapm_mixer_update_power(card
, kcontrol
, connect
);
2055 card
->update
= NULL
;
2056 mutex_unlock(&card
->dapm_mutex
);
2058 soc_dpcm_runtime_update(card
);
2061 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power
);
2063 /* show dapm widget status in sys fs */
2064 static ssize_t
dapm_widget_show(struct device
*dev
,
2065 struct device_attribute
*attr
, char *buf
)
2067 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
2068 struct snd_soc_codec
*codec
=rtd
->codec
;
2069 struct snd_soc_dapm_widget
*w
;
2071 char *state
= "not set";
2073 list_for_each_entry(w
, &codec
->card
->widgets
, list
) {
2074 if (w
->dapm
!= &codec
->dapm
)
2077 /* only display widgets that burnm power */
2079 case snd_soc_dapm_hp
:
2080 case snd_soc_dapm_mic
:
2081 case snd_soc_dapm_spk
:
2082 case snd_soc_dapm_line
:
2083 case snd_soc_dapm_micbias
:
2084 case snd_soc_dapm_dac
:
2085 case snd_soc_dapm_adc
:
2086 case snd_soc_dapm_pga
:
2087 case snd_soc_dapm_out_drv
:
2088 case snd_soc_dapm_mixer
:
2089 case snd_soc_dapm_mixer_named_ctl
:
2090 case snd_soc_dapm_supply
:
2091 case snd_soc_dapm_regulator_supply
:
2092 case snd_soc_dapm_clock_supply
:
2094 count
+= sprintf(buf
+ count
, "%s: %s\n",
2095 w
->name
, w
->power
? "On":"Off");
2102 switch (codec
->dapm
.bias_level
) {
2103 case SND_SOC_BIAS_ON
:
2106 case SND_SOC_BIAS_PREPARE
:
2109 case SND_SOC_BIAS_STANDBY
:
2112 case SND_SOC_BIAS_OFF
:
2116 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
2121 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
2123 int snd_soc_dapm_sys_add(struct device
*dev
)
2125 return device_create_file(dev
, &dev_attr_dapm_widget
);
2128 static void snd_soc_dapm_sys_remove(struct device
*dev
)
2130 device_remove_file(dev
, &dev_attr_dapm_widget
);
2133 static void dapm_free_path(struct snd_soc_dapm_path
*path
)
2135 list_del(&path
->list_sink
);
2136 list_del(&path
->list_source
);
2137 list_del(&path
->list_kcontrol
);
2138 list_del(&path
->list
);
2142 /* free all dapm widgets and resources */
2143 static void dapm_free_widgets(struct snd_soc_dapm_context
*dapm
)
2145 struct snd_soc_dapm_widget
*w
, *next_w
;
2146 struct snd_soc_dapm_path
*p
, *next_p
;
2148 list_for_each_entry_safe(w
, next_w
, &dapm
->card
->widgets
, list
) {
2149 if (w
->dapm
!= dapm
)
2153 * remove source and sink paths associated to this widget.
2154 * While removing the path, remove reference to it from both
2155 * source and sink widgets so that path is removed only once.
2157 list_for_each_entry_safe(p
, next_p
, &w
->sources
, list_sink
)
2160 list_for_each_entry_safe(p
, next_p
, &w
->sinks
, list_source
)
2163 kfree(w
->kcontrols
);
2169 static struct snd_soc_dapm_widget
*dapm_find_widget(
2170 struct snd_soc_dapm_context
*dapm
, const char *pin
,
2171 bool search_other_contexts
)
2173 struct snd_soc_dapm_widget
*w
;
2174 struct snd_soc_dapm_widget
*fallback
= NULL
;
2176 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
2177 if (!strcmp(w
->name
, pin
)) {
2178 if (w
->dapm
== dapm
)
2185 if (search_other_contexts
)
2191 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context
*dapm
,
2192 const char *pin
, int status
)
2194 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
2196 dapm_assert_locked(dapm
);
2199 dev_err(dapm
->dev
, "ASoC: DAPM unknown pin %s\n", pin
);
2203 if (w
->connected
!= status
)
2204 dapm_mark_dirty(w
, "pin configuration");
2206 w
->connected
= status
;
2214 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2215 * @dapm: DAPM context
2217 * Walks all dapm audio paths and powers widgets according to their
2218 * stream or path usage.
2220 * Requires external locking.
2222 * Returns 0 for success.
2224 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context
*dapm
)
2227 * Suppress early reports (eg, jacks syncing their state) to avoid
2228 * silly DAPM runs during card startup.
2230 if (!dapm
->card
|| !dapm
->card
->instantiated
)
2233 return dapm_power_widgets(dapm
->card
, SND_SOC_DAPM_STREAM_NOP
);
2235 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked
);
2238 * snd_soc_dapm_sync - scan and power dapm paths
2239 * @dapm: DAPM context
2241 * Walks all dapm audio paths and powers widgets according to their
2242 * stream or path usage.
2244 * Returns 0 for success.
2246 int snd_soc_dapm_sync(struct snd_soc_dapm_context
*dapm
)
2250 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2251 ret
= snd_soc_dapm_sync_unlocked(dapm
);
2252 mutex_unlock(&dapm
->card
->dapm_mutex
);
2255 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
2257 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context
*dapm
,
2258 struct snd_soc_dapm_widget
*wsource
, struct snd_soc_dapm_widget
*wsink
,
2259 const char *control
,
2260 int (*connected
)(struct snd_soc_dapm_widget
*source
,
2261 struct snd_soc_dapm_widget
*sink
))
2263 struct snd_soc_dapm_path
*path
;
2266 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
2270 path
->source
= wsource
;
2272 path
->connected
= connected
;
2273 INIT_LIST_HEAD(&path
->list
);
2274 INIT_LIST_HEAD(&path
->list_kcontrol
);
2275 INIT_LIST_HEAD(&path
->list_source
);
2276 INIT_LIST_HEAD(&path
->list_sink
);
2278 /* check for external widgets */
2279 if (wsink
->id
== snd_soc_dapm_input
) {
2280 if (wsource
->id
== snd_soc_dapm_micbias
||
2281 wsource
->id
== snd_soc_dapm_mic
||
2282 wsource
->id
== snd_soc_dapm_line
||
2283 wsource
->id
== snd_soc_dapm_output
)
2286 if (wsource
->id
== snd_soc_dapm_output
) {
2287 if (wsink
->id
== snd_soc_dapm_spk
||
2288 wsink
->id
== snd_soc_dapm_hp
||
2289 wsink
->id
== snd_soc_dapm_line
||
2290 wsink
->id
== snd_soc_dapm_input
)
2294 dapm_mark_dirty(wsource
, "Route added");
2295 dapm_mark_dirty(wsink
, "Route added");
2297 /* connect static paths */
2298 if (control
== NULL
) {
2299 list_add(&path
->list
, &dapm
->card
->paths
);
2300 list_add(&path
->list_sink
, &wsink
->sources
);
2301 list_add(&path
->list_source
, &wsource
->sinks
);
2306 /* connect dynamic paths */
2307 switch (wsink
->id
) {
2308 case snd_soc_dapm_adc
:
2309 case snd_soc_dapm_dac
:
2310 case snd_soc_dapm_pga
:
2311 case snd_soc_dapm_out_drv
:
2312 case snd_soc_dapm_input
:
2313 case snd_soc_dapm_output
:
2314 case snd_soc_dapm_siggen
:
2315 case snd_soc_dapm_micbias
:
2316 case snd_soc_dapm_vmid
:
2317 case snd_soc_dapm_pre
:
2318 case snd_soc_dapm_post
:
2319 case snd_soc_dapm_supply
:
2320 case snd_soc_dapm_regulator_supply
:
2321 case snd_soc_dapm_clock_supply
:
2322 case snd_soc_dapm_aif_in
:
2323 case snd_soc_dapm_aif_out
:
2324 case snd_soc_dapm_dai_in
:
2325 case snd_soc_dapm_dai_out
:
2326 case snd_soc_dapm_dai_link
:
2327 case snd_soc_dapm_kcontrol
:
2328 list_add(&path
->list
, &dapm
->card
->paths
);
2329 list_add(&path
->list_sink
, &wsink
->sources
);
2330 list_add(&path
->list_source
, &wsource
->sinks
);
2333 case snd_soc_dapm_mux
:
2334 ret
= dapm_connect_mux(dapm
, wsource
, wsink
, path
, control
,
2335 &wsink
->kcontrol_news
[0]);
2339 case snd_soc_dapm_switch
:
2340 case snd_soc_dapm_mixer
:
2341 case snd_soc_dapm_mixer_named_ctl
:
2342 ret
= dapm_connect_mixer(dapm
, wsource
, wsink
, path
, control
);
2346 case snd_soc_dapm_hp
:
2347 case snd_soc_dapm_mic
:
2348 case snd_soc_dapm_line
:
2349 case snd_soc_dapm_spk
:
2350 list_add(&path
->list
, &dapm
->card
->paths
);
2351 list_add(&path
->list_sink
, &wsink
->sources
);
2352 list_add(&path
->list_source
, &wsource
->sinks
);
2363 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context
*dapm
,
2364 const struct snd_soc_dapm_route
*route
)
2366 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
2367 struct snd_soc_dapm_widget
*wtsource
= NULL
, *wtsink
= NULL
;
2370 char prefixed_sink
[80];
2371 char prefixed_source
[80];
2375 prefix
= soc_dapm_prefix(dapm
);
2377 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
2378 prefix
, route
->sink
);
2379 sink
= prefixed_sink
;
2380 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
2381 prefix
, route
->source
);
2382 source
= prefixed_source
;
2385 source
= route
->source
;
2389 * find src and dest widgets over all widgets but favor a widget from
2390 * current DAPM context
2392 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
2393 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
2395 if (w
->dapm
== dapm
)
2399 if (!wsource
&& !(strcmp(w
->name
, source
))) {
2401 if (w
->dapm
== dapm
)
2405 /* use widget from another DAPM context if not found from this */
2411 if (wsource
== NULL
) {
2412 dev_err(dapm
->dev
, "ASoC: no source widget found for %s\n",
2416 if (wsink
== NULL
) {
2417 dev_err(dapm
->dev
, "ASoC: no sink widget found for %s\n",
2422 ret
= snd_soc_dapm_add_path(dapm
, wsource
, wsink
, route
->control
,
2429 dev_warn(dapm
->dev
, "ASoC: no dapm match for %s --> %s --> %s\n",
2430 source
, route
->control
, sink
);
2434 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context
*dapm
,
2435 const struct snd_soc_dapm_route
*route
)
2437 struct snd_soc_dapm_path
*path
, *p
;
2440 char prefixed_sink
[80];
2441 char prefixed_source
[80];
2444 if (route
->control
) {
2446 "ASoC: Removal of routes with controls not supported\n");
2450 prefix
= soc_dapm_prefix(dapm
);
2452 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
2453 prefix
, route
->sink
);
2454 sink
= prefixed_sink
;
2455 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
2456 prefix
, route
->source
);
2457 source
= prefixed_source
;
2460 source
= route
->source
;
2464 list_for_each_entry(p
, &dapm
->card
->paths
, list
) {
2465 if (strcmp(p
->source
->name
, source
) != 0)
2467 if (strcmp(p
->sink
->name
, sink
) != 0)
2474 dapm_mark_dirty(path
->source
, "Route removed");
2475 dapm_mark_dirty(path
->sink
, "Route removed");
2477 dapm_free_path(path
);
2479 dev_warn(dapm
->dev
, "ASoC: Route %s->%s does not exist\n",
2487 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2488 * @dapm: DAPM context
2489 * @route: audio routes
2490 * @num: number of routes
2492 * Connects 2 dapm widgets together via a named audio path. The sink is
2493 * the widget receiving the audio signal, whilst the source is the sender
2494 * of the audio signal.
2496 * Returns 0 for success else error. On error all resources can be freed
2497 * with a call to snd_soc_card_free().
2499 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context
*dapm
,
2500 const struct snd_soc_dapm_route
*route
, int num
)
2504 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2505 for (i
= 0; i
< num
; i
++) {
2506 r
= snd_soc_dapm_add_route(dapm
, route
);
2508 dev_err(dapm
->dev
, "ASoC: Failed to add route %s -> %s -> %s\n",
2510 route
->control
? route
->control
: "direct",
2516 mutex_unlock(&dapm
->card
->dapm_mutex
);
2520 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
2523 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2524 * @dapm: DAPM context
2525 * @route: audio routes
2526 * @num: number of routes
2528 * Removes routes from the DAPM context.
2530 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context
*dapm
,
2531 const struct snd_soc_dapm_route
*route
, int num
)
2535 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2536 for (i
= 0; i
< num
; i
++) {
2537 snd_soc_dapm_del_route(dapm
, route
);
2540 mutex_unlock(&dapm
->card
->dapm_mutex
);
2544 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes
);
2546 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context
*dapm
,
2547 const struct snd_soc_dapm_route
*route
)
2549 struct snd_soc_dapm_widget
*source
= dapm_find_widget(dapm
,
2552 struct snd_soc_dapm_widget
*sink
= dapm_find_widget(dapm
,
2555 struct snd_soc_dapm_path
*path
;
2559 dev_err(dapm
->dev
, "ASoC: Unable to find source %s for weak route\n",
2565 dev_err(dapm
->dev
, "ASoC: Unable to find sink %s for weak route\n",
2570 if (route
->control
|| route
->connected
)
2571 dev_warn(dapm
->dev
, "ASoC: Ignoring control for weak route %s->%s\n",
2572 route
->source
, route
->sink
);
2574 list_for_each_entry(path
, &source
->sinks
, list_source
) {
2575 if (path
->sink
== sink
) {
2582 dev_err(dapm
->dev
, "ASoC: No path found for weak route %s->%s\n",
2583 route
->source
, route
->sink
);
2585 dev_warn(dapm
->dev
, "ASoC: %d paths found for weak route %s->%s\n",
2586 count
, route
->source
, route
->sink
);
2592 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2593 * @dapm: DAPM context
2594 * @route: audio routes
2595 * @num: number of routes
2597 * Mark existing routes matching those specified in the passed array
2598 * as being weak, meaning that they are ignored for the purpose of
2599 * power decisions. The main intended use case is for sidetone paths
2600 * which couple audio between other independent paths if they are both
2601 * active in order to make the combination work better at the user
2602 * level but which aren't intended to be "used".
2604 * Note that CODEC drivers should not use this as sidetone type paths
2605 * can frequently also be used as bypass paths.
2607 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context
*dapm
,
2608 const struct snd_soc_dapm_route
*route
, int num
)
2613 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2614 for (i
= 0; i
< num
; i
++) {
2615 err
= snd_soc_dapm_weak_route(dapm
, route
);
2620 mutex_unlock(&dapm
->card
->dapm_mutex
);
2624 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes
);
2627 * snd_soc_dapm_new_widgets - add new dapm widgets
2628 * @dapm: DAPM context
2630 * Checks the codec for any new dapm widgets and creates them if found.
2632 * Returns 0 for success.
2634 int snd_soc_dapm_new_widgets(struct snd_soc_card
*card
)
2636 struct snd_soc_dapm_widget
*w
;
2639 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2641 list_for_each_entry(w
, &card
->widgets
, list
)
2646 if (w
->num_kcontrols
) {
2647 w
->kcontrols
= kzalloc(w
->num_kcontrols
*
2648 sizeof(struct snd_kcontrol
*),
2650 if (!w
->kcontrols
) {
2651 mutex_unlock(&card
->dapm_mutex
);
2657 case snd_soc_dapm_switch
:
2658 case snd_soc_dapm_mixer
:
2659 case snd_soc_dapm_mixer_named_ctl
:
2662 case snd_soc_dapm_mux
:
2665 case snd_soc_dapm_pga
:
2666 case snd_soc_dapm_out_drv
:
2673 /* Read the initial power state from the device */
2675 soc_widget_read(w
, w
->reg
, &val
);
2676 val
= val
>> w
->shift
;
2678 if (val
== w
->on_val
)
2684 dapm_mark_dirty(w
, "new widget");
2685 dapm_debugfs_add_widget(w
);
2688 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2689 mutex_unlock(&card
->dapm_mutex
);
2692 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
2695 * snd_soc_dapm_get_volsw - dapm mixer get callback
2696 * @kcontrol: mixer control
2697 * @ucontrol: control element information
2699 * Callback to get the value of a dapm mixer control.
2701 * Returns 0 for success.
2703 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
2704 struct snd_ctl_elem_value
*ucontrol
)
2706 struct snd_soc_codec
*codec
= snd_soc_dapm_kcontrol_codec(kcontrol
);
2707 struct snd_soc_card
*card
= codec
->card
;
2708 struct soc_mixer_control
*mc
=
2709 (struct soc_mixer_control
*)kcontrol
->private_value
;
2711 unsigned int shift
= mc
->shift
;
2713 unsigned int mask
= (1 << fls(max
)) - 1;
2714 unsigned int invert
= mc
->invert
;
2717 if (snd_soc_volsw_is_stereo(mc
))
2718 dev_warn(codec
->dapm
.dev
,
2719 "ASoC: Control '%s' is stereo, which is not supported\n",
2722 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2723 if (dapm_kcontrol_is_powered(kcontrol
) && reg
!= SND_SOC_NOPM
)
2724 val
= (snd_soc_read(codec
, reg
) >> shift
) & mask
;
2726 val
= dapm_kcontrol_get_value(kcontrol
);
2727 mutex_unlock(&card
->dapm_mutex
);
2730 ucontrol
->value
.integer
.value
[0] = max
- val
;
2732 ucontrol
->value
.integer
.value
[0] = val
;
2736 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
2739 * snd_soc_dapm_put_volsw - dapm mixer set callback
2740 * @kcontrol: mixer control
2741 * @ucontrol: control element information
2743 * Callback to set the value of a dapm mixer control.
2745 * Returns 0 for success.
2747 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
2748 struct snd_ctl_elem_value
*ucontrol
)
2750 struct snd_soc_codec
*codec
= snd_soc_dapm_kcontrol_codec(kcontrol
);
2751 struct snd_soc_card
*card
= codec
->card
;
2752 struct soc_mixer_control
*mc
=
2753 (struct soc_mixer_control
*)kcontrol
->private_value
;
2755 unsigned int shift
= mc
->shift
;
2757 unsigned int mask
= (1 << fls(max
)) - 1;
2758 unsigned int invert
= mc
->invert
;
2760 int connect
, change
, reg_change
= 0;
2761 struct snd_soc_dapm_update update
;
2764 if (snd_soc_volsw_is_stereo(mc
))
2765 dev_warn(codec
->dapm
.dev
,
2766 "ASoC: Control '%s' is stereo, which is not supported\n",
2769 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2775 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2777 change
= dapm_kcontrol_set_value(kcontrol
, val
);
2779 if (reg
!= SND_SOC_NOPM
) {
2780 mask
= mask
<< shift
;
2783 reg_change
= snd_soc_test_bits(codec
, reg
, mask
, val
);
2786 if (change
|| reg_change
) {
2788 update
.kcontrol
= kcontrol
;
2792 card
->update
= &update
;
2794 change
|= reg_change
;
2796 ret
= soc_dapm_mixer_update_power(card
, kcontrol
, connect
);
2798 card
->update
= NULL
;
2801 mutex_unlock(&card
->dapm_mutex
);
2804 soc_dpcm_runtime_update(card
);
2808 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
2811 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2812 * @kcontrol: mixer control
2813 * @ucontrol: control element information
2815 * Callback to get the value of a dapm enumerated double mixer control.
2817 * Returns 0 for success.
2819 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
2820 struct snd_ctl_elem_value
*ucontrol
)
2822 struct snd_soc_codec
*codec
= snd_soc_dapm_kcontrol_codec(kcontrol
);
2823 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2824 unsigned int reg_val
, val
;
2826 if (e
->reg
!= SND_SOC_NOPM
)
2827 reg_val
= snd_soc_read(codec
, e
->reg
);
2829 reg_val
= dapm_kcontrol_get_value(kcontrol
);
2831 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
2832 ucontrol
->value
.enumerated
.item
[0] = snd_soc_enum_val_to_item(e
, val
);
2833 if (e
->shift_l
!= e
->shift_r
) {
2834 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
2835 val
= snd_soc_enum_val_to_item(e
, val
);
2836 ucontrol
->value
.enumerated
.item
[1] = val
;
2841 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
2844 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2845 * @kcontrol: mixer control
2846 * @ucontrol: control element information
2848 * Callback to set the value of a dapm enumerated double mixer control.
2850 * Returns 0 for success.
2852 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
2853 struct snd_ctl_elem_value
*ucontrol
)
2855 struct snd_soc_codec
*codec
= snd_soc_dapm_kcontrol_codec(kcontrol
);
2856 struct snd_soc_card
*card
= codec
->card
;
2857 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2858 unsigned int *item
= ucontrol
->value
.enumerated
.item
;
2859 unsigned int val
, change
;
2861 struct snd_soc_dapm_update update
;
2864 if (item
[0] >= e
->items
)
2867 val
= snd_soc_enum_item_to_val(e
, item
[0]) << e
->shift_l
;
2868 mask
= e
->mask
<< e
->shift_l
;
2869 if (e
->shift_l
!= e
->shift_r
) {
2870 if (item
[1] > e
->items
)
2872 val
|= snd_soc_enum_item_to_val(e
, item
[1]) << e
->shift_l
;
2873 mask
|= e
->mask
<< e
->shift_r
;
2876 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2878 if (e
->reg
!= SND_SOC_NOPM
)
2879 change
= snd_soc_test_bits(codec
, e
->reg
, mask
, val
);
2881 change
= dapm_kcontrol_set_value(kcontrol
, val
);
2884 if (e
->reg
!= SND_SOC_NOPM
) {
2885 update
.kcontrol
= kcontrol
;
2886 update
.reg
= e
->reg
;
2889 card
->update
= &update
;
2892 ret
= soc_dapm_mux_update_power(card
, kcontrol
, item
[0], e
);
2894 card
->update
= NULL
;
2897 mutex_unlock(&card
->dapm_mutex
);
2900 soc_dpcm_runtime_update(card
);
2904 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
2907 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2909 * @kcontrol: mixer control
2910 * @uinfo: control element information
2912 * Callback to provide information about a pin switch control.
2914 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
2915 struct snd_ctl_elem_info
*uinfo
)
2917 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2919 uinfo
->value
.integer
.min
= 0;
2920 uinfo
->value
.integer
.max
= 1;
2924 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
2927 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2929 * @kcontrol: mixer control
2932 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
2933 struct snd_ctl_elem_value
*ucontrol
)
2935 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
2936 const char *pin
= (const char *)kcontrol
->private_value
;
2938 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2940 ucontrol
->value
.integer
.value
[0] =
2941 snd_soc_dapm_get_pin_status(&card
->dapm
, pin
);
2943 mutex_unlock(&card
->dapm_mutex
);
2947 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
2950 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2952 * @kcontrol: mixer control
2955 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
2956 struct snd_ctl_elem_value
*ucontrol
)
2958 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
2959 const char *pin
= (const char *)kcontrol
->private_value
;
2961 if (ucontrol
->value
.integer
.value
[0])
2962 snd_soc_dapm_enable_pin(&card
->dapm
, pin
);
2964 snd_soc_dapm_disable_pin(&card
->dapm
, pin
);
2966 snd_soc_dapm_sync(&card
->dapm
);
2969 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
2971 static struct snd_soc_dapm_widget
*
2972 snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
2973 const struct snd_soc_dapm_widget
*widget
)
2975 struct snd_soc_dapm_widget
*w
;
2979 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
2983 case snd_soc_dapm_regulator_supply
:
2984 w
->regulator
= devm_regulator_get(dapm
->dev
, w
->name
);
2985 if (IS_ERR(w
->regulator
)) {
2986 ret
= PTR_ERR(w
->regulator
);
2987 dev_err(dapm
->dev
, "ASoC: Failed to request %s: %d\n",
2992 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
2993 ret
= regulator_allow_bypass(w
->regulator
, true);
2995 dev_warn(w
->dapm
->dev
,
2996 "ASoC: Failed to bypass %s: %d\n",
3000 case snd_soc_dapm_clock_supply
:
3001 #ifdef CONFIG_CLKDEV_LOOKUP
3002 w
->clk
= devm_clk_get(dapm
->dev
, w
->name
);
3003 if (IS_ERR(w
->clk
)) {
3004 ret
= PTR_ERR(w
->clk
);
3005 dev_err(dapm
->dev
, "ASoC: Failed to request %s: %d\n",
3017 prefix
= soc_dapm_prefix(dapm
);
3019 w
->name
= kasprintf(GFP_KERNEL
, "%s %s", prefix
, widget
->name
);
3021 w
->name
= kasprintf(GFP_KERNEL
, "%s", widget
->name
);
3023 if (w
->name
== NULL
) {
3029 case snd_soc_dapm_switch
:
3030 case snd_soc_dapm_mixer
:
3031 case snd_soc_dapm_mixer_named_ctl
:
3032 w
->power_check
= dapm_generic_check_power
;
3034 case snd_soc_dapm_mux
:
3035 w
->power_check
= dapm_generic_check_power
;
3037 case snd_soc_dapm_dai_out
:
3038 w
->power_check
= dapm_adc_check_power
;
3040 case snd_soc_dapm_dai_in
:
3041 w
->power_check
= dapm_dac_check_power
;
3043 case snd_soc_dapm_adc
:
3044 case snd_soc_dapm_aif_out
:
3045 case snd_soc_dapm_dac
:
3046 case snd_soc_dapm_aif_in
:
3047 case snd_soc_dapm_pga
:
3048 case snd_soc_dapm_out_drv
:
3049 case snd_soc_dapm_input
:
3050 case snd_soc_dapm_output
:
3051 case snd_soc_dapm_micbias
:
3052 case snd_soc_dapm_spk
:
3053 case snd_soc_dapm_hp
:
3054 case snd_soc_dapm_mic
:
3055 case snd_soc_dapm_line
:
3056 case snd_soc_dapm_dai_link
:
3057 w
->power_check
= dapm_generic_check_power
;
3059 case snd_soc_dapm_supply
:
3060 case snd_soc_dapm_regulator_supply
:
3061 case snd_soc_dapm_clock_supply
:
3062 case snd_soc_dapm_kcontrol
:
3063 w
->power_check
= dapm_supply_check_power
;
3066 w
->power_check
= dapm_always_on_check_power
;
3071 w
->codec
= dapm
->codec
;
3072 w
->platform
= dapm
->platform
;
3073 INIT_LIST_HEAD(&w
->sources
);
3074 INIT_LIST_HEAD(&w
->sinks
);
3075 INIT_LIST_HEAD(&w
->list
);
3076 INIT_LIST_HEAD(&w
->dirty
);
3077 list_add(&w
->list
, &dapm
->card
->widgets
);
3079 /* machine layer set ups unconnected pins and insertions */
3085 * snd_soc_dapm_new_controls - create new dapm controls
3086 * @dapm: DAPM context
3087 * @widget: widget array
3088 * @num: number of widgets
3090 * Creates new DAPM controls based upon the templates.
3092 * Returns 0 for success else error.
3094 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context
*dapm
,
3095 const struct snd_soc_dapm_widget
*widget
,
3098 struct snd_soc_dapm_widget
*w
;
3102 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
3103 for (i
= 0; i
< num
; i
++) {
3104 w
= snd_soc_dapm_new_control(dapm
, widget
);
3107 "ASoC: Failed to create DAPM control %s\n",
3114 mutex_unlock(&dapm
->card
->dapm_mutex
);
3117 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
3119 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget
*w
,
3120 struct snd_kcontrol
*kcontrol
, int event
)
3122 struct snd_soc_dapm_path
*source_p
, *sink_p
;
3123 struct snd_soc_dai
*source
, *sink
;
3124 const struct snd_soc_pcm_stream
*config
= w
->params
;
3125 struct snd_pcm_substream substream
;
3126 struct snd_pcm_hw_params
*params
= NULL
;
3130 if (WARN_ON(!config
) ||
3131 WARN_ON(list_empty(&w
->sources
) || list_empty(&w
->sinks
)))
3134 /* We only support a single source and sink, pick the first */
3135 source_p
= list_first_entry(&w
->sources
, struct snd_soc_dapm_path
,
3137 sink_p
= list_first_entry(&w
->sinks
, struct snd_soc_dapm_path
,
3140 if (WARN_ON(!source_p
|| !sink_p
) ||
3141 WARN_ON(!sink_p
->source
|| !source_p
->sink
) ||
3142 WARN_ON(!source_p
->source
|| !sink_p
->sink
))
3145 source
= source_p
->source
->priv
;
3146 sink
= sink_p
->sink
->priv
;
3148 /* Be a little careful as we don't want to overflow the mask array */
3149 if (config
->formats
) {
3150 fmt
= ffs(config
->formats
) - 1;
3152 dev_warn(w
->dapm
->dev
, "ASoC: Invalid format %llx specified\n",
3157 /* Currently very limited parameter selection */
3158 params
= kzalloc(sizeof(*params
), GFP_KERNEL
);
3163 snd_mask_set(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), fmt
);
3165 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
)->min
=
3167 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
)->max
=
3170 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
)->min
3171 = config
->channels_min
;
3172 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
)->max
3173 = config
->channels_max
;
3175 memset(&substream
, 0, sizeof(substream
));
3178 case SND_SOC_DAPM_PRE_PMU
:
3179 if (source
->driver
->ops
&& source
->driver
->ops
->hw_params
) {
3180 substream
.stream
= SNDRV_PCM_STREAM_CAPTURE
;
3181 ret
= source
->driver
->ops
->hw_params(&substream
,
3184 dev_err(source
->dev
,
3185 "ASoC: hw_params() failed: %d\n", ret
);
3190 if (sink
->driver
->ops
&& sink
->driver
->ops
->hw_params
) {
3191 substream
.stream
= SNDRV_PCM_STREAM_PLAYBACK
;
3192 ret
= sink
->driver
->ops
->hw_params(&substream
, params
,
3196 "ASoC: hw_params() failed: %d\n", ret
);
3202 case SND_SOC_DAPM_POST_PMU
:
3203 ret
= snd_soc_dai_digital_mute(sink
, 0,
3204 SNDRV_PCM_STREAM_PLAYBACK
);
3205 if (ret
!= 0 && ret
!= -ENOTSUPP
)
3206 dev_warn(sink
->dev
, "ASoC: Failed to unmute: %d\n", ret
);
3210 case SND_SOC_DAPM_PRE_PMD
:
3211 ret
= snd_soc_dai_digital_mute(sink
, 1,
3212 SNDRV_PCM_STREAM_PLAYBACK
);
3213 if (ret
!= 0 && ret
!= -ENOTSUPP
)
3214 dev_warn(sink
->dev
, "ASoC: Failed to mute: %d\n", ret
);
3219 WARN(1, "Unknown event %d\n", event
);
3228 int snd_soc_dapm_new_pcm(struct snd_soc_card
*card
,
3229 const struct snd_soc_pcm_stream
*params
,
3230 struct snd_soc_dapm_widget
*source
,
3231 struct snd_soc_dapm_widget
*sink
)
3233 struct snd_soc_dapm_widget
template;
3234 struct snd_soc_dapm_widget
*w
;
3239 len
= strlen(source
->name
) + strlen(sink
->name
) + 2;
3240 link_name
= devm_kzalloc(card
->dev
, len
, GFP_KERNEL
);
3243 snprintf(link_name
, len
, "%s-%s", source
->name
, sink
->name
);
3245 memset(&template, 0, sizeof(template));
3246 template.reg
= SND_SOC_NOPM
;
3247 template.id
= snd_soc_dapm_dai_link
;
3248 template.name
= link_name
;
3249 template.event
= snd_soc_dai_link_event
;
3250 template.event_flags
= SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMU
|
3251 SND_SOC_DAPM_PRE_PMD
;
3253 dev_dbg(card
->dev
, "ASoC: adding %s widget\n", link_name
);
3255 w
= snd_soc_dapm_new_control(&card
->dapm
, &template);
3257 dev_err(card
->dev
, "ASoC: Failed to create %s widget\n",
3264 ret
= snd_soc_dapm_add_path(&card
->dapm
, source
, w
, NULL
, NULL
);
3267 return snd_soc_dapm_add_path(&card
->dapm
, w
, sink
, NULL
, NULL
);
3270 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context
*dapm
,
3271 struct snd_soc_dai
*dai
)
3273 struct snd_soc_dapm_widget
template;
3274 struct snd_soc_dapm_widget
*w
;
3276 WARN_ON(dapm
->dev
!= dai
->dev
);
3278 memset(&template, 0, sizeof(template));
3279 template.reg
= SND_SOC_NOPM
;
3281 if (dai
->driver
->playback
.stream_name
) {
3282 template.id
= snd_soc_dapm_dai_in
;
3283 template.name
= dai
->driver
->playback
.stream_name
;
3284 template.sname
= dai
->driver
->playback
.stream_name
;
3286 dev_dbg(dai
->dev
, "ASoC: adding %s widget\n",
3289 w
= snd_soc_dapm_new_control(dapm
, &template);
3291 dev_err(dapm
->dev
, "ASoC: Failed to create %s widget\n",
3292 dai
->driver
->playback
.stream_name
);
3297 dai
->playback_widget
= w
;
3300 if (dai
->driver
->capture
.stream_name
) {
3301 template.id
= snd_soc_dapm_dai_out
;
3302 template.name
= dai
->driver
->capture
.stream_name
;
3303 template.sname
= dai
->driver
->capture
.stream_name
;
3305 dev_dbg(dai
->dev
, "ASoC: adding %s widget\n",
3308 w
= snd_soc_dapm_new_control(dapm
, &template);
3310 dev_err(dapm
->dev
, "ASoC: Failed to create %s widget\n",
3311 dai
->driver
->capture
.stream_name
);
3316 dai
->capture_widget
= w
;
3322 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card
*card
)
3324 struct snd_soc_dapm_widget
*dai_w
, *w
;
3325 struct snd_soc_dapm_widget
*src
, *sink
;
3326 struct snd_soc_dai
*dai
;
3328 /* For each DAI widget... */
3329 list_for_each_entry(dai_w
, &card
->widgets
, list
) {
3330 switch (dai_w
->id
) {
3331 case snd_soc_dapm_dai_in
:
3332 case snd_soc_dapm_dai_out
:
3340 /* ...find all widgets with the same stream and link them */
3341 list_for_each_entry(w
, &card
->widgets
, list
) {
3342 if (w
->dapm
!= dai_w
->dapm
)
3346 case snd_soc_dapm_dai_in
:
3347 case snd_soc_dapm_dai_out
:
3353 if (!w
->sname
|| !strstr(w
->sname
, dai_w
->name
))
3356 if (dai_w
->id
== snd_soc_dapm_dai_in
) {
3363 dev_dbg(dai
->dev
, "%s -> %s\n", src
->name
, sink
->name
);
3364 snd_soc_dapm_add_path(w
->dapm
, src
, sink
, NULL
, NULL
);
3371 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card
*card
)
3373 struct snd_soc_pcm_runtime
*rtd
= card
->rtd
;
3374 struct snd_soc_dapm_widget
*sink
, *source
;
3375 struct snd_soc_dai
*cpu_dai
, *codec_dai
;
3378 /* for each BE DAI link... */
3379 for (i
= 0; i
< card
->num_rtd
; i
++) {
3380 rtd
= &card
->rtd
[i
];
3381 cpu_dai
= rtd
->cpu_dai
;
3382 codec_dai
= rtd
->codec_dai
;
3385 * dynamic FE links have no fixed DAI mapping.
3386 * CODEC<->CODEC links have no direct connection.
3388 if (rtd
->dai_link
->dynamic
|| rtd
->dai_link
->params
)
3391 /* there is no point in connecting BE DAI links with dummies */
3392 if (snd_soc_dai_is_dummy(codec_dai
) ||
3393 snd_soc_dai_is_dummy(cpu_dai
))
3396 /* connect BE DAI playback if widgets are valid */
3397 if (codec_dai
->playback_widget
&& cpu_dai
->playback_widget
) {
3398 source
= cpu_dai
->playback_widget
;
3399 sink
= codec_dai
->playback_widget
;
3400 dev_dbg(rtd
->dev
, "connected DAI link %s:%s -> %s:%s\n",
3401 cpu_dai
->component
->name
, source
->name
,
3402 codec_dai
->component
->name
, sink
->name
);
3404 snd_soc_dapm_add_path(&card
->dapm
, source
, sink
,
3408 /* connect BE DAI capture if widgets are valid */
3409 if (codec_dai
->capture_widget
&& cpu_dai
->capture_widget
) {
3410 source
= codec_dai
->capture_widget
;
3411 sink
= cpu_dai
->capture_widget
;
3412 dev_dbg(rtd
->dev
, "connected DAI link %s:%s -> %s:%s\n",
3413 codec_dai
->component
->name
, source
->name
,
3414 cpu_dai
->component
->name
, sink
->name
);
3416 snd_soc_dapm_add_path(&card
->dapm
, source
, sink
,
3422 static void soc_dapm_dai_stream_event(struct snd_soc_dai
*dai
, int stream
,
3425 struct snd_soc_dapm_widget
*w
;
3427 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
3428 w
= dai
->playback_widget
;
3430 w
= dai
->capture_widget
;
3433 dapm_mark_dirty(w
, "stream event");
3436 case SND_SOC_DAPM_STREAM_START
:
3439 case SND_SOC_DAPM_STREAM_STOP
:
3442 case SND_SOC_DAPM_STREAM_SUSPEND
:
3443 case SND_SOC_DAPM_STREAM_RESUME
:
3444 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
3445 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
3451 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
, int stream
,
3454 soc_dapm_dai_stream_event(rtd
->cpu_dai
, stream
, event
);
3455 soc_dapm_dai_stream_event(rtd
->codec_dai
, stream
, event
);
3457 dapm_power_widgets(rtd
->card
, event
);
3461 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3462 * @rtd: PCM runtime data
3463 * @stream: stream name
3464 * @event: stream event
3466 * Sends a stream event to the dapm core. The core then makes any
3467 * necessary widget power changes.
3469 * Returns 0 for success else error.
3471 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
, int stream
,
3474 struct snd_soc_card
*card
= rtd
->card
;
3476 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3477 soc_dapm_stream_event(rtd
, stream
, event
);
3478 mutex_unlock(&card
->dapm_mutex
);
3482 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3483 * @dapm: DAPM context
3486 * Enables input/output pin and its parents or children widgets iff there is
3487 * a valid audio route and active audio stream.
3489 * Requires external locking.
3491 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3492 * do any widget power switching.
3494 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
3497 return snd_soc_dapm_set_pin(dapm
, pin
, 1);
3499 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked
);
3502 * snd_soc_dapm_enable_pin - enable pin.
3503 * @dapm: DAPM context
3506 * Enables input/output pin and its parents or children widgets iff there is
3507 * a valid audio route and active audio stream.
3509 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3510 * do any widget power switching.
3512 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
3516 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3518 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 1);
3520 mutex_unlock(&dapm
->card
->dapm_mutex
);
3524 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
3527 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3528 * @dapm: DAPM context
3531 * Enables input/output pin regardless of any other state. This is
3532 * intended for use with microphone bias supplies used in microphone
3535 * Requires external locking.
3537 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3538 * do any widget power switching.
3540 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
3543 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
3546 dev_err(dapm
->dev
, "ASoC: unknown pin %s\n", pin
);
3550 dev_dbg(w
->dapm
->dev
, "ASoC: force enable pin %s\n", pin
);
3553 dapm_mark_dirty(w
, "force enable");
3557 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked
);
3560 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3561 * @dapm: DAPM context
3564 * Enables input/output pin regardless of any other state. This is
3565 * intended for use with microphone bias supplies used in microphone
3568 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3569 * do any widget power switching.
3571 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context
*dapm
,
3576 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3578 ret
= snd_soc_dapm_force_enable_pin_unlocked(dapm
, pin
);
3580 mutex_unlock(&dapm
->card
->dapm_mutex
);
3584 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
3587 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3588 * @dapm: DAPM context
3591 * Disables input/output pin and its parents or children widgets.
3593 * Requires external locking.
3595 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3596 * do any widget power switching.
3598 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
3601 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
3603 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked
);
3606 * snd_soc_dapm_disable_pin - disable pin.
3607 * @dapm: DAPM context
3610 * Disables input/output pin and its parents or children widgets.
3612 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3613 * do any widget power switching.
3615 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context
*dapm
,
3620 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3622 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 0);
3624 mutex_unlock(&dapm
->card
->dapm_mutex
);
3628 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
3631 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3632 * @dapm: DAPM context
3635 * Marks the specified pin as being not connected, disabling it along
3636 * any parent or child widgets. At present this is identical to
3637 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3638 * additional things such as disabling controls which only affect
3639 * paths through the pin.
3641 * Requires external locking.
3643 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3644 * do any widget power switching.
3646 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
3649 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
3651 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked
);
3654 * snd_soc_dapm_nc_pin - permanently disable pin.
3655 * @dapm: DAPM context
3658 * Marks the specified pin as being not connected, disabling it along
3659 * any parent or child widgets. At present this is identical to
3660 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3661 * additional things such as disabling controls which only affect
3662 * paths through the pin.
3664 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3665 * do any widget power switching.
3667 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
3671 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3673 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 0);
3675 mutex_unlock(&dapm
->card
->dapm_mutex
);
3679 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
3682 * snd_soc_dapm_get_pin_status - get audio pin status
3683 * @dapm: DAPM context
3684 * @pin: audio signal pin endpoint (or start point)
3686 * Get audio pin status - connected or disconnected.
3688 * Returns 1 for connected otherwise 0.
3690 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context
*dapm
,
3693 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
3696 return w
->connected
;
3700 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
3703 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3704 * @dapm: DAPM context
3705 * @pin: audio signal pin endpoint (or start point)
3707 * Mark the given endpoint or pin as ignoring suspend. When the
3708 * system is disabled a path between two endpoints flagged as ignoring
3709 * suspend will not be disabled. The path must already be enabled via
3710 * normal means at suspend time, it will not be turned on if it was not
3713 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context
*dapm
,
3716 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, false);
3719 dev_err(dapm
->dev
, "ASoC: unknown pin %s\n", pin
);
3723 w
->ignore_suspend
= 1;
3727 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
3729 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card
*card
,
3730 struct snd_soc_dapm_widget
*w
)
3732 struct snd_soc_dapm_path
*p
;
3734 list_for_each_entry(p
, &card
->paths
, list
) {
3735 if ((p
->source
== w
) || (p
->sink
== w
)) {
3737 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3738 p
->source
->name
, p
->source
->id
, p
->source
->dapm
,
3739 p
->sink
->name
, p
->sink
->id
, p
->sink
->dapm
);
3741 /* Connected to something other than the codec */
3742 if (p
->source
->dapm
!= p
->sink
->dapm
)
3745 * Loopback connection from codec external pin to
3746 * codec external pin
3748 if (p
->sink
->id
== snd_soc_dapm_input
) {
3749 switch (p
->source
->id
) {
3750 case snd_soc_dapm_output
:
3751 case snd_soc_dapm_micbias
:
3764 * snd_soc_dapm_auto_nc_pins - call snd_soc_dapm_nc_pin for unused pins
3765 * @card: The card whose pins should be processed
3767 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the card
3768 * which are unused. Pins are used if they are connected externally to a
3769 * component, whether that be to some other device, or a loop-back connection to
3770 * the component itself.
3772 void snd_soc_dapm_auto_nc_pins(struct snd_soc_card
*card
)
3774 struct snd_soc_dapm_widget
*w
;
3776 dev_dbg(card
->dev
, "ASoC: Auto NC: DAPMs: card:%p\n", &card
->dapm
);
3778 list_for_each_entry(w
, &card
->widgets
, list
) {
3780 case snd_soc_dapm_input
:
3781 case snd_soc_dapm_output
:
3782 case snd_soc_dapm_micbias
:
3783 dev_dbg(card
->dev
, "ASoC: Auto NC: Checking widget %s\n",
3785 if (!snd_soc_dapm_widget_in_card_paths(card
, w
)) {
3787 "... Not in map; disabling\n");
3788 snd_soc_dapm_nc_pin(w
->dapm
, w
->name
);
3798 * snd_soc_dapm_free - free dapm resources
3799 * @dapm: DAPM context
3801 * Free all dapm widgets and resources.
3803 void snd_soc_dapm_free(struct snd_soc_dapm_context
*dapm
)
3805 snd_soc_dapm_sys_remove(dapm
->dev
);
3806 dapm_debugfs_cleanup(dapm
);
3807 dapm_free_widgets(dapm
);
3808 list_del(&dapm
->list
);
3810 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
3812 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context
*dapm
)
3814 struct snd_soc_card
*card
= dapm
->card
;
3815 struct snd_soc_dapm_widget
*w
;
3816 LIST_HEAD(down_list
);
3819 mutex_lock(&card
->dapm_mutex
);
3821 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
3822 if (w
->dapm
!= dapm
)
3825 dapm_seq_insert(w
, &down_list
, false);
3831 /* If there were no widgets to power down we're already in
3835 if (dapm
->bias_level
== SND_SOC_BIAS_ON
)
3836 snd_soc_dapm_set_bias_level(dapm
,
3837 SND_SOC_BIAS_PREPARE
);
3838 dapm_seq_run(card
, &down_list
, 0, false);
3839 if (dapm
->bias_level
== SND_SOC_BIAS_PREPARE
)
3840 snd_soc_dapm_set_bias_level(dapm
,
3841 SND_SOC_BIAS_STANDBY
);
3844 mutex_unlock(&card
->dapm_mutex
);
3848 * snd_soc_dapm_shutdown - callback for system shutdown
3850 void snd_soc_dapm_shutdown(struct snd_soc_card
*card
)
3852 struct snd_soc_dapm_context
*dapm
;
3854 list_for_each_entry(dapm
, &card
->dapm_list
, list
) {
3855 if (dapm
!= &card
->dapm
) {
3856 soc_dapm_shutdown_dapm(dapm
);
3857 if (dapm
->bias_level
== SND_SOC_BIAS_STANDBY
)
3858 snd_soc_dapm_set_bias_level(dapm
,
3863 soc_dapm_shutdown_dapm(&card
->dapm
);
3864 if (card
->dapm
.bias_level
== SND_SOC_BIAS_STANDBY
)
3865 snd_soc_dapm_set_bias_level(&card
->dapm
,
3869 /* Module information */
3870 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3871 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3872 MODULE_LICENSE("GPL");