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
);
163 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of input
165 * @w: The widget for which to invalidate the cached number of input paths
167 * The function resets the cached number of inputs for the specified widget and
168 * all widgets that can be reached via outgoing paths from the widget.
170 * This function must be called if the number of input paths for a widget might
171 * have changed. E.g. if the source state of a widget changes or a path is added
172 * or activated with the widget as the sink.
174 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget
*w
)
176 struct snd_soc_dapm_widget
*sink
;
177 struct snd_soc_dapm_path
*p
;
180 dapm_assert_locked(w
->dapm
);
186 list_add_tail(&w
->work_list
, &list
);
188 list_for_each_entry(w
, &list
, work_list
) {
189 list_for_each_entry(p
, &w
->sinks
, list_source
) {
190 if (p
->is_supply
|| p
->weak
|| !p
->connect
)
193 if (sink
->inputs
!= -1) {
195 list_add_tail(&sink
->work_list
, &list
);
202 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
204 * @w: The widget for which to invalidate the cached number of output paths
206 * Resets the cached number of outputs for the specified widget and all widgets
207 * that can be reached via incoming paths from the widget.
209 * This function must be called if the number of output paths for a widget might
210 * have changed. E.g. if the sink state of a widget changes or a path is added
211 * or activated with the widget as the source.
213 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget
*w
)
215 struct snd_soc_dapm_widget
*source
;
216 struct snd_soc_dapm_path
*p
;
219 dapm_assert_locked(w
->dapm
);
221 if (w
->outputs
== -1)
225 list_add_tail(&w
->work_list
, &list
);
227 list_for_each_entry(w
, &list
, work_list
) {
228 list_for_each_entry(p
, &w
->sources
, list_sink
) {
229 if (p
->is_supply
|| p
->weak
|| !p
->connect
)
232 if (source
->outputs
!= -1) {
233 source
->outputs
= -1;
234 list_add_tail(&source
->work_list
, &list
);
241 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
242 * for the widgets connected to a path
243 * @p: The path to invalidate
245 * Resets the cached number of inputs for the sink of the path and the cached
246 * number of outputs for the source of the path.
248 * This function must be called when a path is added, removed or the connected
251 static void dapm_path_invalidate(struct snd_soc_dapm_path
*p
)
254 * Weak paths or supply paths do not influence the number of input or
255 * output paths of their neighbors.
257 if (p
->weak
|| p
->is_supply
)
261 * The number of connected endpoints is the sum of the number of
262 * connected endpoints of all neighbors. If a node with 0 connected
263 * endpoints is either connected or disconnected that sum won't change,
264 * so there is no need to re-check the path.
266 if (p
->source
->inputs
!= 0)
267 dapm_widget_invalidate_input_paths(p
->sink
);
268 if (p
->sink
->outputs
!= 0)
269 dapm_widget_invalidate_output_paths(p
->source
);
272 void dapm_mark_endpoints_dirty(struct snd_soc_card
*card
)
274 struct snd_soc_dapm_widget
*w
;
276 mutex_lock(&card
->dapm_mutex
);
278 list_for_each_entry(w
, &card
->widgets
, list
) {
279 if (w
->is_sink
|| w
->is_source
) {
280 dapm_mark_dirty(w
, "Rechecking endpoints");
282 dapm_widget_invalidate_output_paths(w
);
284 dapm_widget_invalidate_input_paths(w
);
288 mutex_unlock(&card
->dapm_mutex
);
290 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty
);
292 /* create a new dapm widget */
293 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
294 const struct snd_soc_dapm_widget
*_widget
)
296 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
299 struct dapm_kcontrol_data
{
301 struct snd_soc_dapm_widget
*widget
;
302 struct list_head paths
;
303 struct snd_soc_dapm_widget_list
*wlist
;
306 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget
*widget
,
307 struct snd_kcontrol
*kcontrol
)
309 struct dapm_kcontrol_data
*data
;
310 struct soc_mixer_control
*mc
;
312 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
314 dev_err(widget
->dapm
->dev
,
315 "ASoC: can't allocate kcontrol data for %s\n",
320 INIT_LIST_HEAD(&data
->paths
);
322 switch (widget
->id
) {
323 case snd_soc_dapm_switch
:
324 case snd_soc_dapm_mixer
:
325 case snd_soc_dapm_mixer_named_ctl
:
326 mc
= (struct soc_mixer_control
*)kcontrol
->private_value
;
328 if (mc
->autodisable
) {
329 struct snd_soc_dapm_widget
template;
331 memset(&template, 0, sizeof(template));
332 template.reg
= mc
->reg
;
333 template.mask
= (1 << fls(mc
->max
)) - 1;
334 template.shift
= mc
->shift
;
336 template.off_val
= mc
->max
;
338 template.off_val
= 0;
339 template.on_val
= template.off_val
;
340 template.id
= snd_soc_dapm_kcontrol
;
341 template.name
= kcontrol
->id
.name
;
343 data
->value
= template.on_val
;
345 data
->widget
= snd_soc_dapm_new_control(widget
->dapm
,
357 kcontrol
->private_data
= data
;
362 static void dapm_kcontrol_free(struct snd_kcontrol
*kctl
)
364 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kctl
);
369 static struct snd_soc_dapm_widget_list
*dapm_kcontrol_get_wlist(
370 const struct snd_kcontrol
*kcontrol
)
372 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
377 static int dapm_kcontrol_add_widget(struct snd_kcontrol
*kcontrol
,
378 struct snd_soc_dapm_widget
*widget
)
380 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
381 struct snd_soc_dapm_widget_list
*new_wlist
;
385 n
= data
->wlist
->num_widgets
+ 1;
389 new_wlist
= krealloc(data
->wlist
,
390 sizeof(*new_wlist
) + sizeof(widget
) * n
, GFP_KERNEL
);
394 new_wlist
->widgets
[n
- 1] = widget
;
395 new_wlist
->num_widgets
= n
;
397 data
->wlist
= new_wlist
;
402 static void dapm_kcontrol_add_path(const struct snd_kcontrol
*kcontrol
,
403 struct snd_soc_dapm_path
*path
)
405 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
407 list_add_tail(&path
->list_kcontrol
, &data
->paths
);
410 snd_soc_dapm_add_path(data
->widget
->dapm
, data
->widget
,
411 path
->source
, NULL
, NULL
);
415 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol
*kcontrol
)
417 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
422 return data
->widget
->power
;
425 static struct list_head
*dapm_kcontrol_get_path_list(
426 const struct snd_kcontrol
*kcontrol
)
428 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
433 #define dapm_kcontrol_for_each_path(path, kcontrol) \
434 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
437 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol
*kcontrol
)
439 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
443 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value
);
445 static bool dapm_kcontrol_set_value(const struct snd_kcontrol
*kcontrol
,
448 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
450 if (data
->value
== value
)
454 data
->widget
->on_val
= value
;
462 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
464 * @kcontrol: The kcontrol
466 * Note: This function must only be used on kcontrols that are known to have
467 * been registered for a CODEC. Otherwise the behaviour is undefined.
469 struct snd_soc_dapm_context
*snd_soc_dapm_kcontrol_dapm(
470 struct snd_kcontrol
*kcontrol
)
472 return dapm_kcontrol_get_wlist(kcontrol
)->widgets
[0]->dapm
;
474 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm
);
477 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
478 * @kcontrol: The kcontrol
480 struct snd_soc_codec
*snd_soc_dapm_kcontrol_codec(struct snd_kcontrol
*kcontrol
)
482 return snd_soc_dapm_to_codec(snd_soc_dapm_kcontrol_dapm(kcontrol
));
484 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec
);
486 static void dapm_reset(struct snd_soc_card
*card
)
488 struct snd_soc_dapm_widget
*w
;
490 lockdep_assert_held(&card
->dapm_mutex
);
492 memset(&card
->dapm_stats
, 0, sizeof(card
->dapm_stats
));
494 list_for_each_entry(w
, &card
->widgets
, list
) {
495 w
->new_power
= w
->power
;
496 w
->power_checked
= false;
500 static const char *soc_dapm_prefix(struct snd_soc_dapm_context
*dapm
)
502 if (!dapm
->component
)
504 return dapm
->component
->name_prefix
;
507 static int soc_dapm_read(struct snd_soc_dapm_context
*dapm
, int reg
,
510 if (!dapm
->component
)
512 return snd_soc_component_read(dapm
->component
, reg
, value
);
515 static int soc_dapm_update_bits(struct snd_soc_dapm_context
*dapm
,
516 int reg
, unsigned int mask
, unsigned int value
)
518 if (!dapm
->component
)
520 return snd_soc_component_update_bits(dapm
->component
, reg
,
524 static int soc_dapm_test_bits(struct snd_soc_dapm_context
*dapm
,
525 int reg
, unsigned int mask
, unsigned int value
)
527 if (!dapm
->component
)
529 return snd_soc_component_test_bits(dapm
->component
, reg
, mask
, value
);
532 static void soc_dapm_async_complete(struct snd_soc_dapm_context
*dapm
)
535 snd_soc_component_async_complete(dapm
->component
);
539 * snd_soc_dapm_set_bias_level - set the bias level for the system
540 * @dapm: DAPM context
541 * @level: level to configure
543 * Configure the bias (power) levels for the SoC audio device.
545 * Returns 0 for success else error.
547 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context
*dapm
,
548 enum snd_soc_bias_level level
)
550 struct snd_soc_card
*card
= dapm
->card
;
553 trace_snd_soc_bias_level_start(card
, level
);
555 if (card
&& card
->set_bias_level
)
556 ret
= card
->set_bias_level(card
, dapm
, level
);
560 if (dapm
->set_bias_level
)
561 ret
= dapm
->set_bias_level(dapm
, level
);
562 else if (!card
|| dapm
!= &card
->dapm
)
563 dapm
->bias_level
= level
;
568 if (card
&& card
->set_bias_level_post
)
569 ret
= card
->set_bias_level_post(card
, dapm
, level
);
571 trace_snd_soc_bias_level_done(card
, level
);
576 /* connect mux widget to its interconnecting audio paths */
577 static int dapm_connect_mux(struct snd_soc_dapm_context
*dapm
,
578 struct snd_soc_dapm_path
*path
, const char *control_name
)
580 const struct snd_kcontrol_new
*kcontrol
= &path
->sink
->kcontrol_news
[0];
581 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
582 unsigned int val
, item
;
585 if (e
->reg
!= SND_SOC_NOPM
) {
586 soc_dapm_read(dapm
, e
->reg
, &val
);
587 val
= (val
>> e
->shift_l
) & e
->mask
;
588 item
= snd_soc_enum_val_to_item(e
, val
);
590 /* since a virtual mux has no backing registers to
591 * decide which path to connect, it will try to match
592 * with the first enumeration. This is to ensure
593 * that the default mux choice (the first) will be
594 * correctly powered up during initialization.
599 for (i
= 0; i
< e
->items
; i
++) {
600 if (!(strcmp(control_name
, e
->texts
[i
]))) {
601 path
->name
= e
->texts
[i
];
613 /* set up initial codec paths */
614 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path
*p
, int i
)
616 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
617 p
->sink
->kcontrol_news
[i
].private_value
;
618 unsigned int reg
= mc
->reg
;
619 unsigned int shift
= mc
->shift
;
620 unsigned int max
= mc
->max
;
621 unsigned int mask
= (1 << fls(max
)) - 1;
622 unsigned int invert
= mc
->invert
;
625 if (reg
!= SND_SOC_NOPM
) {
626 soc_dapm_read(p
->sink
->dapm
, reg
, &val
);
627 val
= (val
>> shift
) & mask
;
636 /* connect mixer widget to its interconnecting audio paths */
637 static int dapm_connect_mixer(struct snd_soc_dapm_context
*dapm
,
638 struct snd_soc_dapm_path
*path
, const char *control_name
)
642 /* search for mixer kcontrol */
643 for (i
= 0; i
< path
->sink
->num_kcontrols
; i
++) {
644 if (!strcmp(control_name
, path
->sink
->kcontrol_news
[i
].name
)) {
645 path
->name
= path
->sink
->kcontrol_news
[i
].name
;
646 dapm_set_mixer_path_status(path
, i
);
653 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context
*dapm
,
654 struct snd_soc_dapm_widget
*kcontrolw
,
655 const struct snd_kcontrol_new
*kcontrol_new
,
656 struct snd_kcontrol
**kcontrol
)
658 struct snd_soc_dapm_widget
*w
;
663 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
664 if (w
== kcontrolw
|| w
->dapm
!= kcontrolw
->dapm
)
666 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
667 if (&w
->kcontrol_news
[i
] == kcontrol_new
) {
669 *kcontrol
= w
->kcontrols
[i
];
679 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
680 * create it. Either way, add the widget into the control's widget list
682 static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget
*w
,
685 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
686 struct snd_card
*card
= dapm
->card
->snd_card
;
690 struct snd_kcontrol
*kcontrol
;
691 bool wname_in_long_name
, kcname_in_long_name
;
692 char *long_name
= NULL
;
696 prefix
= soc_dapm_prefix(dapm
);
698 prefix_len
= strlen(prefix
) + 1;
702 shared
= dapm_is_shared_kcontrol(dapm
, w
, &w
->kcontrol_news
[kci
],
707 wname_in_long_name
= false;
708 kcname_in_long_name
= true;
711 case snd_soc_dapm_switch
:
712 case snd_soc_dapm_mixer
:
713 wname_in_long_name
= true;
714 kcname_in_long_name
= true;
716 case snd_soc_dapm_mixer_named_ctl
:
717 wname_in_long_name
= false;
718 kcname_in_long_name
= true;
720 case snd_soc_dapm_mux
:
721 wname_in_long_name
= true;
722 kcname_in_long_name
= false;
729 if (wname_in_long_name
&& kcname_in_long_name
) {
731 * The control will get a prefix from the control
732 * creation process but we're also using the same
733 * prefix for widgets so cut the prefix off the
734 * front of the widget name.
736 long_name
= kasprintf(GFP_KERNEL
, "%s %s",
737 w
->name
+ prefix_len
,
738 w
->kcontrol_news
[kci
].name
);
739 if (long_name
== NULL
)
743 } else if (wname_in_long_name
) {
745 name
= w
->name
+ prefix_len
;
748 name
= w
->kcontrol_news
[kci
].name
;
751 kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[kci
], NULL
, name
,
758 kcontrol
->private_free
= dapm_kcontrol_free
;
760 ret
= dapm_kcontrol_data_alloc(w
, kcontrol
);
762 snd_ctl_free_one(kcontrol
);
766 ret
= snd_ctl_add(card
, kcontrol
);
769 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
775 ret
= dapm_kcontrol_add_widget(kcontrol
, w
);
777 w
->kcontrols
[kci
] = kcontrol
;
785 /* create new dapm mixer control */
786 static int dapm_new_mixer(struct snd_soc_dapm_widget
*w
)
789 struct snd_soc_dapm_path
*path
;
792 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
794 list_for_each_entry(path
, &w
->sources
, list_sink
) {
795 /* mixer/mux paths name must match control name */
796 if (path
->name
!= (char *)w
->kcontrol_news
[i
].name
)
799 if (w
->kcontrols
[i
]) {
800 dapm_kcontrol_add_path(w
->kcontrols
[i
], path
);
804 ret
= dapm_create_or_share_mixmux_kcontrol(w
, i
);
808 dapm_kcontrol_add_path(w
->kcontrols
[i
], path
);
815 /* create new dapm mux control */
816 static int dapm_new_mux(struct snd_soc_dapm_widget
*w
)
818 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
819 struct snd_soc_dapm_path
*path
;
822 if (w
->num_kcontrols
!= 1) {
824 "ASoC: mux %s has incorrect number of controls\n",
829 if (list_empty(&w
->sources
)) {
830 dev_err(dapm
->dev
, "ASoC: mux %s has no paths\n", w
->name
);
834 ret
= dapm_create_or_share_mixmux_kcontrol(w
, 0);
838 list_for_each_entry(path
, &w
->sources
, list_sink
) {
840 dapm_kcontrol_add_path(w
->kcontrols
[0], path
);
846 /* create new dapm volume control */
847 static int dapm_new_pga(struct snd_soc_dapm_widget
*w
)
849 if (w
->num_kcontrols
)
850 dev_err(w
->dapm
->dev
,
851 "ASoC: PGA controls not supported: '%s'\n", w
->name
);
856 /* We implement power down on suspend by checking the power state of
857 * the ALSA card - when we are suspending the ALSA state for the card
860 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
862 int level
= snd_power_get_state(widget
->dapm
->card
->snd_card
);
865 case SNDRV_CTL_POWER_D3hot
:
866 case SNDRV_CTL_POWER_D3cold
:
867 if (widget
->ignore_suspend
)
868 dev_dbg(widget
->dapm
->dev
, "ASoC: %s ignoring suspend\n",
870 return widget
->ignore_suspend
;
876 /* add widget to list if it's not already in the list */
877 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list
**list
,
878 struct snd_soc_dapm_widget
*w
)
880 struct snd_soc_dapm_widget_list
*wlist
;
881 int wlistsize
, wlistentries
, i
;
888 /* is this widget already in the list */
889 for (i
= 0; i
< wlist
->num_widgets
; i
++) {
890 if (wlist
->widgets
[i
] == w
)
894 /* allocate some new space */
895 wlistentries
= wlist
->num_widgets
+ 1;
896 wlistsize
= sizeof(struct snd_soc_dapm_widget_list
) +
897 wlistentries
* sizeof(struct snd_soc_dapm_widget
*);
898 *list
= krealloc(wlist
, wlistsize
, GFP_KERNEL
);
900 dev_err(w
->dapm
->dev
, "ASoC: can't allocate widget list for %s\n",
906 /* insert the widget */
907 dev_dbg(w
->dapm
->dev
, "ASoC: added %s in widget list pos %d\n",
908 w
->name
, wlist
->num_widgets
);
910 wlist
->widgets
[wlist
->num_widgets
] = w
;
911 wlist
->num_widgets
++;
916 * Recursively check for a completed path to an active or physically connected
917 * output widget. Returns number of complete paths.
919 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
,
920 struct snd_soc_dapm_widget_list
**list
)
922 struct snd_soc_dapm_path
*path
;
925 if (widget
->outputs
>= 0)
926 return widget
->outputs
;
928 DAPM_UPDATE_STAT(widget
, path_checks
);
930 if (widget
->is_sink
&& widget
->connected
) {
931 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
932 return widget
->outputs
;
935 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
936 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
938 if (path
->weak
|| path
->is_supply
)
944 trace_snd_soc_dapm_output_path(widget
, path
);
949 /* do we need to add this widget to the list ? */
952 err
= dapm_list_add_widget(list
, path
->sink
);
954 dev_err(widget
->dapm
->dev
,
955 "ASoC: could not add widget %s\n",
962 con
+= is_connected_output_ep(path
->sink
, list
);
968 widget
->outputs
= con
;
974 * Recursively check for a completed path to an active or physically connected
975 * input widget. Returns number of complete paths.
977 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
,
978 struct snd_soc_dapm_widget_list
**list
)
980 struct snd_soc_dapm_path
*path
;
983 if (widget
->inputs
>= 0)
984 return widget
->inputs
;
986 DAPM_UPDATE_STAT(widget
, path_checks
);
988 if (widget
->is_source
&& widget
->connected
) {
989 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
990 return widget
->inputs
;
993 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
994 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
996 if (path
->weak
|| path
->is_supply
)
1002 trace_snd_soc_dapm_input_path(widget
, path
);
1004 if (path
->connect
) {
1007 /* do we need to add this widget to the list ? */
1010 err
= dapm_list_add_widget(list
, path
->source
);
1012 dev_err(widget
->dapm
->dev
,
1013 "ASoC: could not add widget %s\n",
1020 con
+= is_connected_input_ep(path
->source
, list
);
1026 widget
->inputs
= con
;
1032 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1033 * @dai: the soc DAI.
1034 * @stream: stream direction.
1035 * @list: list of active widgets for this stream.
1037 * Queries DAPM graph as to whether an valid audio stream path exists for
1038 * the initial stream specified by name. This takes into account
1039 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1041 * Returns the number of valid paths or negative error.
1043 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai
*dai
, int stream
,
1044 struct snd_soc_dapm_widget_list
**list
)
1046 struct snd_soc_card
*card
= dai
->component
->card
;
1047 struct snd_soc_dapm_widget
*w
;
1050 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
1053 * For is_connected_{output,input}_ep fully discover the graph we need
1054 * to reset the cached number of inputs and outputs.
1056 list_for_each_entry(w
, &card
->widgets
, list
) {
1061 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1062 paths
= is_connected_output_ep(dai
->playback_widget
, list
);
1064 paths
= is_connected_input_ep(dai
->capture_widget
, list
);
1066 trace_snd_soc_dapm_connected(paths
, stream
);
1067 mutex_unlock(&card
->dapm_mutex
);
1073 * Handler for regulator supply widget.
1075 int dapm_regulator_event(struct snd_soc_dapm_widget
*w
,
1076 struct snd_kcontrol
*kcontrol
, int event
)
1080 soc_dapm_async_complete(w
->dapm
);
1082 if (SND_SOC_DAPM_EVENT_ON(event
)) {
1083 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
1084 ret
= regulator_allow_bypass(w
->regulator
, false);
1086 dev_warn(w
->dapm
->dev
,
1087 "ASoC: Failed to unbypass %s: %d\n",
1091 return regulator_enable(w
->regulator
);
1093 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
1094 ret
= regulator_allow_bypass(w
->regulator
, true);
1096 dev_warn(w
->dapm
->dev
,
1097 "ASoC: Failed to bypass %s: %d\n",
1101 return regulator_disable_deferred(w
->regulator
, w
->shift
);
1104 EXPORT_SYMBOL_GPL(dapm_regulator_event
);
1107 * Handler for clock supply widget.
1109 int dapm_clock_event(struct snd_soc_dapm_widget
*w
,
1110 struct snd_kcontrol
*kcontrol
, int event
)
1115 soc_dapm_async_complete(w
->dapm
);
1117 #ifdef CONFIG_HAVE_CLK
1118 if (SND_SOC_DAPM_EVENT_ON(event
)) {
1119 return clk_prepare_enable(w
->clk
);
1121 clk_disable_unprepare(w
->clk
);
1127 EXPORT_SYMBOL_GPL(dapm_clock_event
);
1129 static int dapm_widget_power_check(struct snd_soc_dapm_widget
*w
)
1131 if (w
->power_checked
)
1132 return w
->new_power
;
1137 w
->new_power
= w
->power_check(w
);
1139 w
->power_checked
= true;
1141 return w
->new_power
;
1144 /* Generic check to see if a widget should be powered.
1146 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
1150 DAPM_UPDATE_STAT(w
, power_checks
);
1152 in
= is_connected_input_ep(w
, NULL
);
1153 out
= is_connected_output_ep(w
, NULL
);
1154 return out
!= 0 && in
!= 0;
1157 /* Check to see if a power supply is needed */
1158 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
1160 struct snd_soc_dapm_path
*path
;
1162 DAPM_UPDATE_STAT(w
, power_checks
);
1164 /* Check if one of our outputs is connected */
1165 list_for_each_entry(path
, &w
->sinks
, list_source
) {
1166 DAPM_UPDATE_STAT(w
, neighbour_checks
);
1171 if (path
->connected
&&
1172 !path
->connected(path
->source
, path
->sink
))
1175 if (dapm_widget_power_check(path
->sink
))
1182 static int dapm_always_on_check_power(struct snd_soc_dapm_widget
*w
)
1187 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
1188 struct snd_soc_dapm_widget
*b
,
1196 sort
= dapm_down_seq
;
1198 if (sort
[a
->id
] != sort
[b
->id
])
1199 return sort
[a
->id
] - sort
[b
->id
];
1200 if (a
->subseq
!= b
->subseq
) {
1202 return a
->subseq
- b
->subseq
;
1204 return b
->subseq
- a
->subseq
;
1206 if (a
->reg
!= b
->reg
)
1207 return a
->reg
- b
->reg
;
1208 if (a
->dapm
!= b
->dapm
)
1209 return (unsigned long)a
->dapm
- (unsigned long)b
->dapm
;
1214 /* Insert a widget in order into a DAPM power sequence. */
1215 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
1216 struct list_head
*list
,
1219 struct snd_soc_dapm_widget
*w
;
1221 list_for_each_entry(w
, list
, power_list
)
1222 if (dapm_seq_compare(new_widget
, w
, power_up
) < 0) {
1223 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
1227 list_add_tail(&new_widget
->power_list
, list
);
1230 static void dapm_seq_check_event(struct snd_soc_card
*card
,
1231 struct snd_soc_dapm_widget
*w
, int event
)
1233 const char *ev_name
;
1237 case SND_SOC_DAPM_PRE_PMU
:
1238 ev_name
= "PRE_PMU";
1241 case SND_SOC_DAPM_POST_PMU
:
1242 ev_name
= "POST_PMU";
1245 case SND_SOC_DAPM_PRE_PMD
:
1246 ev_name
= "PRE_PMD";
1249 case SND_SOC_DAPM_POST_PMD
:
1250 ev_name
= "POST_PMD";
1253 case SND_SOC_DAPM_WILL_PMU
:
1254 ev_name
= "WILL_PMU";
1257 case SND_SOC_DAPM_WILL_PMD
:
1258 ev_name
= "WILL_PMD";
1262 WARN(1, "Unknown event %d\n", event
);
1266 if (w
->new_power
!= power
)
1269 if (w
->event
&& (w
->event_flags
& event
)) {
1270 pop_dbg(w
->dapm
->dev
, card
->pop_time
, "pop test : %s %s\n",
1272 soc_dapm_async_complete(w
->dapm
);
1273 trace_snd_soc_dapm_widget_event_start(w
, event
);
1274 ret
= w
->event(w
, NULL
, event
);
1275 trace_snd_soc_dapm_widget_event_done(w
, event
);
1277 dev_err(w
->dapm
->dev
, "ASoC: %s: %s event failed: %d\n",
1278 ev_name
, w
->name
, ret
);
1282 /* Apply the coalesced changes from a DAPM sequence */
1283 static void dapm_seq_run_coalesced(struct snd_soc_card
*card
,
1284 struct list_head
*pending
)
1286 struct snd_soc_dapm_context
*dapm
;
1287 struct snd_soc_dapm_widget
*w
;
1289 unsigned int value
= 0;
1290 unsigned int mask
= 0;
1292 w
= list_first_entry(pending
, struct snd_soc_dapm_widget
, power_list
);
1296 list_for_each_entry(w
, pending
, power_list
) {
1297 WARN_ON(reg
!= w
->reg
|| dapm
!= w
->dapm
);
1298 w
->power
= w
->new_power
;
1300 mask
|= w
->mask
<< w
->shift
;
1302 value
|= w
->on_val
<< w
->shift
;
1304 value
|= w
->off_val
<< w
->shift
;
1306 pop_dbg(dapm
->dev
, card
->pop_time
,
1307 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1308 w
->name
, reg
, value
, mask
);
1310 /* Check for events */
1311 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_PRE_PMU
);
1312 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_PRE_PMD
);
1316 /* Any widget will do, they should all be updating the
1320 pop_dbg(dapm
->dev
, card
->pop_time
,
1321 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1322 value
, mask
, reg
, card
->pop_time
);
1323 pop_wait(card
->pop_time
);
1324 soc_dapm_update_bits(dapm
, reg
, mask
, value
);
1327 list_for_each_entry(w
, pending
, power_list
) {
1328 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_POST_PMU
);
1329 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_POST_PMD
);
1333 /* Apply a DAPM power sequence.
1335 * We walk over a pre-sorted list of widgets to apply power to. In
1336 * order to minimise the number of writes to the device required
1337 * multiple widgets will be updated in a single write where possible.
1338 * Currently anything that requires more than a single write is not
1341 static void dapm_seq_run(struct snd_soc_card
*card
,
1342 struct list_head
*list
, int event
, bool power_up
)
1344 struct snd_soc_dapm_widget
*w
, *n
;
1345 struct snd_soc_dapm_context
*d
;
1348 int cur_subseq
= -1;
1349 int cur_reg
= SND_SOC_NOPM
;
1350 struct snd_soc_dapm_context
*cur_dapm
= NULL
;
1357 sort
= dapm_down_seq
;
1359 list_for_each_entry_safe(w
, n
, list
, power_list
) {
1362 /* Do we need to apply any queued changes? */
1363 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
||
1364 w
->dapm
!= cur_dapm
|| w
->subseq
!= cur_subseq
) {
1365 if (!list_empty(&pending
))
1366 dapm_seq_run_coalesced(card
, &pending
);
1368 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1369 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1370 if (sort
[i
] == cur_sort
)
1371 cur_dapm
->seq_notifier(cur_dapm
,
1376 if (cur_dapm
&& w
->dapm
!= cur_dapm
)
1377 soc_dapm_async_complete(cur_dapm
);
1379 INIT_LIST_HEAD(&pending
);
1381 cur_subseq
= INT_MIN
;
1382 cur_reg
= SND_SOC_NOPM
;
1387 case snd_soc_dapm_pre
:
1389 list_for_each_entry_safe_continue(w
, n
, list
,
1392 if (event
== SND_SOC_DAPM_STREAM_START
)
1394 NULL
, SND_SOC_DAPM_PRE_PMU
);
1395 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1397 NULL
, SND_SOC_DAPM_PRE_PMD
);
1400 case snd_soc_dapm_post
:
1402 list_for_each_entry_safe_continue(w
, n
, list
,
1405 if (event
== SND_SOC_DAPM_STREAM_START
)
1407 NULL
, SND_SOC_DAPM_POST_PMU
);
1408 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1410 NULL
, SND_SOC_DAPM_POST_PMD
);
1414 /* Queue it up for application */
1415 cur_sort
= sort
[w
->id
];
1416 cur_subseq
= w
->subseq
;
1419 list_move(&w
->power_list
, &pending
);
1424 dev_err(w
->dapm
->dev
,
1425 "ASoC: Failed to apply widget power: %d\n", ret
);
1428 if (!list_empty(&pending
))
1429 dapm_seq_run_coalesced(card
, &pending
);
1431 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1432 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1433 if (sort
[i
] == cur_sort
)
1434 cur_dapm
->seq_notifier(cur_dapm
,
1438 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1439 soc_dapm_async_complete(d
);
1443 static void dapm_widget_update(struct snd_soc_card
*card
)
1445 struct snd_soc_dapm_update
*update
= card
->update
;
1446 struct snd_soc_dapm_widget_list
*wlist
;
1447 struct snd_soc_dapm_widget
*w
= NULL
;
1451 if (!update
|| !dapm_kcontrol_is_powered(update
->kcontrol
))
1454 wlist
= dapm_kcontrol_get_wlist(update
->kcontrol
);
1456 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
1457 w
= wlist
->widgets
[wi
];
1459 if (w
->event
&& (w
->event_flags
& SND_SOC_DAPM_PRE_REG
)) {
1460 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_PRE_REG
);
1462 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM pre-event failed: %d\n",
1470 ret
= soc_dapm_update_bits(w
->dapm
, update
->reg
, update
->mask
,
1473 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM update failed: %d\n",
1476 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
1477 w
= wlist
->widgets
[wi
];
1479 if (w
->event
&& (w
->event_flags
& SND_SOC_DAPM_POST_REG
)) {
1480 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_POST_REG
);
1482 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM post-event failed: %d\n",
1488 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1489 * they're changing state.
1491 static void dapm_pre_sequence_async(void *data
, async_cookie_t cookie
)
1493 struct snd_soc_dapm_context
*d
= data
;
1496 /* If we're off and we're not supposed to be go into STANDBY */
1497 if (d
->bias_level
== SND_SOC_BIAS_OFF
&&
1498 d
->target_bias_level
!= SND_SOC_BIAS_OFF
) {
1500 pm_runtime_get_sync(d
->dev
);
1502 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1505 "ASoC: Failed to turn on bias: %d\n", ret
);
1508 /* Prepare for a transition to ON or away from ON */
1509 if ((d
->target_bias_level
== SND_SOC_BIAS_ON
&&
1510 d
->bias_level
!= SND_SOC_BIAS_ON
) ||
1511 (d
->target_bias_level
!= SND_SOC_BIAS_ON
&&
1512 d
->bias_level
== SND_SOC_BIAS_ON
)) {
1513 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_PREPARE
);
1516 "ASoC: Failed to prepare bias: %d\n", ret
);
1520 /* Async callback run prior to DAPM sequences - brings to their final
1523 static void dapm_post_sequence_async(void *data
, async_cookie_t cookie
)
1525 struct snd_soc_dapm_context
*d
= data
;
1528 /* If we just powered the last thing off drop to standby bias */
1529 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1530 (d
->target_bias_level
== SND_SOC_BIAS_STANDBY
||
1531 d
->target_bias_level
== SND_SOC_BIAS_OFF
)) {
1532 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1534 dev_err(d
->dev
, "ASoC: Failed to apply standby bias: %d\n",
1538 /* If we're in standby and can support bias off then do that */
1539 if (d
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1540 d
->target_bias_level
== SND_SOC_BIAS_OFF
) {
1541 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_OFF
);
1543 dev_err(d
->dev
, "ASoC: Failed to turn off bias: %d\n",
1547 pm_runtime_put(d
->dev
);
1550 /* If we just powered up then move to active bias */
1551 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1552 d
->target_bias_level
== SND_SOC_BIAS_ON
) {
1553 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_ON
);
1555 dev_err(d
->dev
, "ASoC: Failed to apply active bias: %d\n",
1560 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget
*peer
,
1561 bool power
, bool connect
)
1563 /* If a connection is being made or broken then that update
1564 * will have marked the peer dirty, otherwise the widgets are
1565 * not connected and this update has no impact. */
1569 /* If the peer is already in the state we're moving to then we
1570 * won't have an impact on it. */
1571 if (power
!= peer
->power
)
1572 dapm_mark_dirty(peer
, "peer state change");
1575 static void dapm_widget_set_power(struct snd_soc_dapm_widget
*w
, bool power
,
1576 struct list_head
*up_list
,
1577 struct list_head
*down_list
)
1579 struct snd_soc_dapm_path
*path
;
1581 if (w
->power
== power
)
1584 trace_snd_soc_dapm_widget_power(w
, power
);
1586 /* If we changed our power state perhaps our neigbours changed
1589 list_for_each_entry(path
, &w
->sources
, list_sink
)
1590 dapm_widget_set_peer_power(path
->source
, power
, path
->connect
);
1592 /* Supplies can't affect their outputs, only their inputs */
1593 if (!w
->is_supply
) {
1594 list_for_each_entry(path
, &w
->sinks
, list_source
)
1595 dapm_widget_set_peer_power(path
->sink
, power
,
1600 dapm_seq_insert(w
, up_list
, true);
1602 dapm_seq_insert(w
, down_list
, false);
1605 static void dapm_power_one_widget(struct snd_soc_dapm_widget
*w
,
1606 struct list_head
*up_list
,
1607 struct list_head
*down_list
)
1612 case snd_soc_dapm_pre
:
1613 dapm_seq_insert(w
, down_list
, false);
1615 case snd_soc_dapm_post
:
1616 dapm_seq_insert(w
, up_list
, true);
1620 power
= dapm_widget_power_check(w
);
1622 dapm_widget_set_power(w
, power
, up_list
, down_list
);
1627 static bool dapm_idle_bias_off(struct snd_soc_dapm_context
*dapm
)
1629 if (dapm
->idle_bias_off
)
1632 switch (snd_power_get_state(dapm
->card
->snd_card
)) {
1633 case SNDRV_CTL_POWER_D3hot
:
1634 case SNDRV_CTL_POWER_D3cold
:
1635 return dapm
->suspend_bias_off
;
1644 * Scan each dapm widget for complete audio path.
1645 * A complete path is a route that has valid endpoints i.e.:-
1647 * o DAC to output pin.
1648 * o Input Pin to ADC.
1649 * o Input pin to Output pin (bypass, sidetone)
1650 * o DAC to ADC (loopback).
1652 static int dapm_power_widgets(struct snd_soc_card
*card
, int event
)
1654 struct snd_soc_dapm_widget
*w
;
1655 struct snd_soc_dapm_context
*d
;
1657 LIST_HEAD(down_list
);
1658 ASYNC_DOMAIN_EXCLUSIVE(async_domain
);
1659 enum snd_soc_bias_level bias
;
1661 lockdep_assert_held(&card
->dapm_mutex
);
1663 trace_snd_soc_dapm_start(card
);
1665 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1666 if (dapm_idle_bias_off(d
))
1667 d
->target_bias_level
= SND_SOC_BIAS_OFF
;
1669 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1674 /* Check which widgets we need to power and store them in
1675 * lists indicating if they should be powered up or down. We
1676 * only check widgets that have been flagged as dirty but note
1677 * that new widgets may be added to the dirty list while we
1680 list_for_each_entry(w
, &card
->dapm_dirty
, dirty
) {
1681 dapm_power_one_widget(w
, &up_list
, &down_list
);
1684 list_for_each_entry(w
, &card
->widgets
, list
) {
1686 case snd_soc_dapm_pre
:
1687 case snd_soc_dapm_post
:
1688 /* These widgets always need to be powered */
1691 list_del_init(&w
->dirty
);
1698 /* Supplies and micbiases only bring the
1699 * context up to STANDBY as unless something
1700 * else is active and passing audio they
1701 * generally don't require full power. Signal
1702 * generators are virtual pins and have no
1703 * power impact themselves.
1706 case snd_soc_dapm_siggen
:
1707 case snd_soc_dapm_vmid
:
1709 case snd_soc_dapm_supply
:
1710 case snd_soc_dapm_regulator_supply
:
1711 case snd_soc_dapm_clock_supply
:
1712 case snd_soc_dapm_micbias
:
1713 if (d
->target_bias_level
< SND_SOC_BIAS_STANDBY
)
1714 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1717 d
->target_bias_level
= SND_SOC_BIAS_ON
;
1724 /* Force all contexts in the card to the same bias state if
1725 * they're not ground referenced.
1727 bias
= SND_SOC_BIAS_OFF
;
1728 list_for_each_entry(d
, &card
->dapm_list
, list
)
1729 if (d
->target_bias_level
> bias
)
1730 bias
= d
->target_bias_level
;
1731 list_for_each_entry(d
, &card
->dapm_list
, list
)
1732 if (!dapm_idle_bias_off(d
))
1733 d
->target_bias_level
= bias
;
1735 trace_snd_soc_dapm_walk_done(card
);
1737 /* Run card bias changes at first */
1738 dapm_pre_sequence_async(&card
->dapm
, 0);
1739 /* Run other bias changes in parallel */
1740 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1741 if (d
!= &card
->dapm
)
1742 async_schedule_domain(dapm_pre_sequence_async
, d
,
1745 async_synchronize_full_domain(&async_domain
);
1747 list_for_each_entry(w
, &down_list
, power_list
) {
1748 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_WILL_PMD
);
1751 list_for_each_entry(w
, &up_list
, power_list
) {
1752 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_WILL_PMU
);
1755 /* Power down widgets first; try to avoid amplifying pops. */
1756 dapm_seq_run(card
, &down_list
, event
, false);
1758 dapm_widget_update(card
);
1761 dapm_seq_run(card
, &up_list
, event
, true);
1763 /* Run all the bias changes in parallel */
1764 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1765 if (d
!= &card
->dapm
)
1766 async_schedule_domain(dapm_post_sequence_async
, d
,
1769 async_synchronize_full_domain(&async_domain
);
1770 /* Run card bias changes at last */
1771 dapm_post_sequence_async(&card
->dapm
, 0);
1773 /* do we need to notify any clients that DAPM event is complete */
1774 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1775 if (d
->stream_event
)
1776 d
->stream_event(d
, event
);
1779 pop_dbg(card
->dev
, card
->pop_time
,
1780 "DAPM sequencing finished, waiting %dms\n", card
->pop_time
);
1781 pop_wait(card
->pop_time
);
1783 trace_snd_soc_dapm_done(card
);
1788 #ifdef CONFIG_DEBUG_FS
1789 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1790 char __user
*user_buf
,
1791 size_t count
, loff_t
*ppos
)
1793 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1797 struct snd_soc_dapm_path
*p
= NULL
;
1799 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1803 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
1808 in
= is_connected_input_ep(w
, NULL
);
1809 out
= is_connected_output_ep(w
, NULL
);
1812 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s%s in %d out %d",
1813 w
->name
, w
->power
? "On" : "Off",
1814 w
->force
? " (forced)" : "", in
, out
);
1817 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1818 " - R%d(0x%x) mask 0x%x",
1819 w
->reg
, w
->reg
, w
->mask
<< w
->shift
);
1821 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
1824 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1826 w
->active
? "active" : "inactive");
1828 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1829 if (p
->connected
&& !p
->connected(w
, p
->source
))
1833 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1834 " in \"%s\" \"%s\"\n",
1835 p
->name
? p
->name
: "static",
1838 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1839 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1843 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1844 " out \"%s\" \"%s\"\n",
1845 p
->name
? p
->name
: "static",
1849 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1855 static const struct file_operations dapm_widget_power_fops
= {
1856 .open
= simple_open
,
1857 .read
= dapm_widget_power_read_file
,
1858 .llseek
= default_llseek
,
1861 static ssize_t
dapm_bias_read_file(struct file
*file
, char __user
*user_buf
,
1862 size_t count
, loff_t
*ppos
)
1864 struct snd_soc_dapm_context
*dapm
= file
->private_data
;
1867 switch (dapm
->bias_level
) {
1868 case SND_SOC_BIAS_ON
:
1871 case SND_SOC_BIAS_PREPARE
:
1872 level
= "Prepare\n";
1874 case SND_SOC_BIAS_STANDBY
:
1875 level
= "Standby\n";
1877 case SND_SOC_BIAS_OFF
:
1881 WARN(1, "Unknown bias_level %d\n", dapm
->bias_level
);
1882 level
= "Unknown\n";
1886 return simple_read_from_buffer(user_buf
, count
, ppos
, level
,
1890 static const struct file_operations dapm_bias_fops
= {
1891 .open
= simple_open
,
1892 .read
= dapm_bias_read_file
,
1893 .llseek
= default_llseek
,
1896 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
1897 struct dentry
*parent
)
1901 dapm
->debugfs_dapm
= debugfs_create_dir("dapm", parent
);
1903 if (!dapm
->debugfs_dapm
) {
1905 "ASoC: Failed to create DAPM debugfs directory\n");
1909 d
= debugfs_create_file("bias_level", 0444,
1910 dapm
->debugfs_dapm
, dapm
,
1914 "ASoC: Failed to create bias level debugfs file\n");
1917 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
1919 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
1922 if (!dapm
->debugfs_dapm
|| !w
->name
)
1925 d
= debugfs_create_file(w
->name
, 0444,
1926 dapm
->debugfs_dapm
, w
,
1927 &dapm_widget_power_fops
);
1929 dev_warn(w
->dapm
->dev
,
1930 "ASoC: Failed to create %s debugfs file\n",
1934 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
1936 debugfs_remove_recursive(dapm
->debugfs_dapm
);
1940 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
1941 struct dentry
*parent
)
1945 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
1949 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
1956 * soc_dapm_connect_path() - Connects or disconnects a path
1957 * @path: The path to update
1958 * @connect: The new connect state of the path. True if the path is connected,
1959 * false if it is disconneted.
1960 * @reason: The reason why the path changed (for debugging only)
1962 static void soc_dapm_connect_path(struct snd_soc_dapm_path
*path
,
1963 bool connect
, const char *reason
)
1965 if (path
->connect
== connect
)
1968 path
->connect
= connect
;
1969 dapm_mark_dirty(path
->source
, reason
);
1970 dapm_mark_dirty(path
->sink
, reason
);
1971 dapm_path_invalidate(path
);
1974 /* test and update the power status of a mux widget */
1975 static int soc_dapm_mux_update_power(struct snd_soc_card
*card
,
1976 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
)
1978 struct snd_soc_dapm_path
*path
;
1982 lockdep_assert_held(&card
->dapm_mutex
);
1984 /* find dapm widget path assoc with kcontrol */
1985 dapm_kcontrol_for_each_path(path
, kcontrol
) {
1987 /* we now need to match the string in the enum to the path */
1988 if (!(strcmp(path
->name
, e
->texts
[mux
])))
1993 soc_dapm_connect_path(path
, connect
, "mux update");
1997 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2002 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context
*dapm
,
2003 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
,
2004 struct snd_soc_dapm_update
*update
)
2006 struct snd_soc_card
*card
= dapm
->card
;
2009 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2010 card
->update
= update
;
2011 ret
= soc_dapm_mux_update_power(card
, kcontrol
, mux
, e
);
2012 card
->update
= NULL
;
2013 mutex_unlock(&card
->dapm_mutex
);
2015 soc_dpcm_runtime_update(card
);
2018 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power
);
2020 /* test and update the power status of a mixer or switch widget */
2021 static int soc_dapm_mixer_update_power(struct snd_soc_card
*card
,
2022 struct snd_kcontrol
*kcontrol
, int connect
)
2024 struct snd_soc_dapm_path
*path
;
2027 lockdep_assert_held(&card
->dapm_mutex
);
2029 /* find dapm widget path assoc with kcontrol */
2030 dapm_kcontrol_for_each_path(path
, kcontrol
) {
2032 soc_dapm_connect_path(path
, connect
, "mixer update");
2036 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2041 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context
*dapm
,
2042 struct snd_kcontrol
*kcontrol
, int connect
,
2043 struct snd_soc_dapm_update
*update
)
2045 struct snd_soc_card
*card
= dapm
->card
;
2048 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2049 card
->update
= update
;
2050 ret
= soc_dapm_mixer_update_power(card
, kcontrol
, connect
);
2051 card
->update
= NULL
;
2052 mutex_unlock(&card
->dapm_mutex
);
2054 soc_dpcm_runtime_update(card
);
2057 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power
);
2059 static ssize_t
dapm_widget_show_codec(struct snd_soc_codec
*codec
, char *buf
)
2061 struct snd_soc_dapm_widget
*w
;
2063 char *state
= "not set";
2065 list_for_each_entry(w
, &codec
->component
.card
->widgets
, list
) {
2066 if (w
->dapm
!= &codec
->dapm
)
2069 /* only display widgets that burnm power */
2071 case snd_soc_dapm_hp
:
2072 case snd_soc_dapm_mic
:
2073 case snd_soc_dapm_spk
:
2074 case snd_soc_dapm_line
:
2075 case snd_soc_dapm_micbias
:
2076 case snd_soc_dapm_dac
:
2077 case snd_soc_dapm_adc
:
2078 case snd_soc_dapm_pga
:
2079 case snd_soc_dapm_out_drv
:
2080 case snd_soc_dapm_mixer
:
2081 case snd_soc_dapm_mixer_named_ctl
:
2082 case snd_soc_dapm_supply
:
2083 case snd_soc_dapm_regulator_supply
:
2084 case snd_soc_dapm_clock_supply
:
2086 count
+= sprintf(buf
+ count
, "%s: %s\n",
2087 w
->name
, w
->power
? "On":"Off");
2094 switch (codec
->dapm
.bias_level
) {
2095 case SND_SOC_BIAS_ON
:
2098 case SND_SOC_BIAS_PREPARE
:
2101 case SND_SOC_BIAS_STANDBY
:
2104 case SND_SOC_BIAS_OFF
:
2108 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
2113 /* show dapm widget status in sys fs */
2114 static ssize_t
dapm_widget_show(struct device
*dev
,
2115 struct device_attribute
*attr
, char *buf
)
2117 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
2120 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
2121 struct snd_soc_codec
*codec
= rtd
->codec_dais
[i
]->codec
;
2122 count
+= dapm_widget_show_codec(codec
, buf
+ count
);
2128 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
2130 struct attribute
*soc_dapm_dev_attrs
[] = {
2131 &dev_attr_dapm_widget
.attr
,
2135 static void dapm_free_path(struct snd_soc_dapm_path
*path
)
2137 list_del(&path
->list_sink
);
2138 list_del(&path
->list_source
);
2139 list_del(&path
->list_kcontrol
);
2140 list_del(&path
->list
);
2144 /* free all dapm widgets and resources */
2145 static void dapm_free_widgets(struct snd_soc_dapm_context
*dapm
)
2147 struct snd_soc_dapm_widget
*w
, *next_w
;
2148 struct snd_soc_dapm_path
*p
, *next_p
;
2150 list_for_each_entry_safe(w
, next_w
, &dapm
->card
->widgets
, list
) {
2151 if (w
->dapm
!= dapm
)
2155 * remove source and sink paths associated to this widget.
2156 * While removing the path, remove reference to it from both
2157 * source and sink widgets so that path is removed only once.
2159 list_for_each_entry_safe(p
, next_p
, &w
->sources
, list_sink
)
2162 list_for_each_entry_safe(p
, next_p
, &w
->sinks
, list_source
)
2165 kfree(w
->kcontrols
);
2171 static struct snd_soc_dapm_widget
*dapm_find_widget(
2172 struct snd_soc_dapm_context
*dapm
, const char *pin
,
2173 bool search_other_contexts
)
2175 struct snd_soc_dapm_widget
*w
;
2176 struct snd_soc_dapm_widget
*fallback
= NULL
;
2178 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
2179 if (!strcmp(w
->name
, pin
)) {
2180 if (w
->dapm
== dapm
)
2187 if (search_other_contexts
)
2193 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context
*dapm
,
2194 const char *pin
, int status
)
2196 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
2198 dapm_assert_locked(dapm
);
2201 dev_err(dapm
->dev
, "ASoC: DAPM unknown pin %s\n", pin
);
2205 if (w
->connected
!= status
) {
2206 dapm_mark_dirty(w
, "pin configuration");
2207 dapm_widget_invalidate_input_paths(w
);
2208 dapm_widget_invalidate_output_paths(w
);
2211 w
->connected
= status
;
2219 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2220 * @dapm: DAPM context
2222 * Walks all dapm audio paths and powers widgets according to their
2223 * stream or path usage.
2225 * Requires external locking.
2227 * Returns 0 for success.
2229 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context
*dapm
)
2232 * Suppress early reports (eg, jacks syncing their state) to avoid
2233 * silly DAPM runs during card startup.
2235 if (!dapm
->card
|| !dapm
->card
->instantiated
)
2238 return dapm_power_widgets(dapm
->card
, SND_SOC_DAPM_STREAM_NOP
);
2240 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked
);
2243 * snd_soc_dapm_sync - scan and power dapm paths
2244 * @dapm: DAPM context
2246 * Walks all dapm audio paths and powers widgets according to their
2247 * stream or path usage.
2249 * Returns 0 for success.
2251 int snd_soc_dapm_sync(struct snd_soc_dapm_context
*dapm
)
2255 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2256 ret
= snd_soc_dapm_sync_unlocked(dapm
);
2257 mutex_unlock(&dapm
->card
->dapm_mutex
);
2260 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
2263 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2264 * @w: The widget for which to update the flags
2266 * Some widgets have a dynamic category which depends on which neighbors they
2267 * are connected to. This function update the category for these widgets.
2269 * This function must be called whenever a path is added or removed to a widget.
2271 static void dapm_update_widget_flags(struct snd_soc_dapm_widget
*w
)
2273 struct snd_soc_dapm_path
*p
;
2276 case snd_soc_dapm_input
:
2277 /* On a fully routed card a input is never a source */
2278 if (w
->dapm
->card
->fully_routed
)
2281 list_for_each_entry(p
, &w
->sources
, list_sink
) {
2282 if (p
->source
->id
== snd_soc_dapm_micbias
||
2283 p
->source
->id
== snd_soc_dapm_mic
||
2284 p
->source
->id
== snd_soc_dapm_line
||
2285 p
->source
->id
== snd_soc_dapm_output
) {
2291 case snd_soc_dapm_output
:
2292 /* On a fully routed card a output is never a sink */
2293 if (w
->dapm
->card
->fully_routed
)
2296 list_for_each_entry(p
, &w
->sinks
, list_source
) {
2297 if (p
->sink
->id
== snd_soc_dapm_spk
||
2298 p
->sink
->id
== snd_soc_dapm_hp
||
2299 p
->sink
->id
== snd_soc_dapm_line
||
2300 p
->sink
->id
== snd_soc_dapm_input
) {
2306 case snd_soc_dapm_line
:
2307 w
->is_sink
= !list_empty(&w
->sources
);
2308 w
->is_source
= !list_empty(&w
->sinks
);
2315 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context
*dapm
,
2316 struct snd_soc_dapm_widget
*wsource
, struct snd_soc_dapm_widget
*wsink
,
2317 const char *control
,
2318 int (*connected
)(struct snd_soc_dapm_widget
*source
,
2319 struct snd_soc_dapm_widget
*sink
))
2321 struct snd_soc_dapm_path
*path
;
2324 if (wsink
->is_supply
&& !wsource
->is_supply
) {
2326 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2327 wsource
->name
, wsink
->name
);
2331 if (connected
&& !wsource
->is_supply
) {
2333 "connected() callback only supported for supply widgets (%s -> %s)\n",
2334 wsource
->name
, wsink
->name
);
2338 if (wsource
->is_supply
&& control
) {
2340 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2341 wsource
->name
, control
, wsink
->name
);
2345 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
2349 path
->source
= wsource
;
2351 path
->connected
= connected
;
2352 INIT_LIST_HEAD(&path
->list
);
2353 INIT_LIST_HEAD(&path
->list_kcontrol
);
2354 INIT_LIST_HEAD(&path
->list_source
);
2355 INIT_LIST_HEAD(&path
->list_sink
);
2357 if (wsource
->is_supply
|| wsink
->is_supply
)
2358 path
->is_supply
= 1;
2360 /* connect static paths */
2361 if (control
== NULL
) {
2364 /* connect dynamic paths */
2365 switch (wsink
->id
) {
2366 case snd_soc_dapm_mux
:
2367 ret
= dapm_connect_mux(dapm
, path
, control
);
2371 case snd_soc_dapm_switch
:
2372 case snd_soc_dapm_mixer
:
2373 case snd_soc_dapm_mixer_named_ctl
:
2374 ret
= dapm_connect_mixer(dapm
, path
, control
);
2380 "Control not supported for path %s -> [%s] -> %s\n",
2381 wsource
->name
, control
, wsink
->name
);
2387 list_add(&path
->list
, &dapm
->card
->paths
);
2388 list_add(&path
->list_sink
, &wsink
->sources
);
2389 list_add(&path
->list_source
, &wsource
->sinks
);
2391 dapm_update_widget_flags(wsource
);
2392 dapm_update_widget_flags(wsink
);
2394 dapm_mark_dirty(wsource
, "Route added");
2395 dapm_mark_dirty(wsink
, "Route added");
2397 if (dapm
->card
->instantiated
&& path
->connect
)
2398 dapm_path_invalidate(path
);
2406 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context
*dapm
,
2407 const struct snd_soc_dapm_route
*route
)
2409 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
2410 struct snd_soc_dapm_widget
*wtsource
= NULL
, *wtsink
= NULL
;
2413 char prefixed_sink
[80];
2414 char prefixed_source
[80];
2418 prefix
= soc_dapm_prefix(dapm
);
2420 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
2421 prefix
, route
->sink
);
2422 sink
= prefixed_sink
;
2423 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
2424 prefix
, route
->source
);
2425 source
= prefixed_source
;
2428 source
= route
->source
;
2432 * find src and dest widgets over all widgets but favor a widget from
2433 * current DAPM context
2435 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
2436 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
2438 if (w
->dapm
== dapm
)
2442 if (!wsource
&& !(strcmp(w
->name
, source
))) {
2444 if (w
->dapm
== dapm
)
2448 /* use widget from another DAPM context if not found from this */
2454 if (wsource
== NULL
) {
2455 dev_err(dapm
->dev
, "ASoC: no source widget found for %s\n",
2459 if (wsink
== NULL
) {
2460 dev_err(dapm
->dev
, "ASoC: no sink widget found for %s\n",
2465 ret
= snd_soc_dapm_add_path(dapm
, wsource
, wsink
, route
->control
,
2472 dev_warn(dapm
->dev
, "ASoC: no dapm match for %s --> %s --> %s\n",
2473 source
, route
->control
, sink
);
2477 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context
*dapm
,
2478 const struct snd_soc_dapm_route
*route
)
2480 struct snd_soc_dapm_widget
*wsource
, *wsink
;
2481 struct snd_soc_dapm_path
*path
, *p
;
2484 char prefixed_sink
[80];
2485 char prefixed_source
[80];
2488 if (route
->control
) {
2490 "ASoC: Removal of routes with controls not supported\n");
2494 prefix
= soc_dapm_prefix(dapm
);
2496 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
2497 prefix
, route
->sink
);
2498 sink
= prefixed_sink
;
2499 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
2500 prefix
, route
->source
);
2501 source
= prefixed_source
;
2504 source
= route
->source
;
2508 list_for_each_entry(p
, &dapm
->card
->paths
, list
) {
2509 if (strcmp(p
->source
->name
, source
) != 0)
2511 if (strcmp(p
->sink
->name
, sink
) != 0)
2518 wsource
= path
->source
;
2521 dapm_mark_dirty(wsource
, "Route removed");
2522 dapm_mark_dirty(wsink
, "Route removed");
2524 dapm_path_invalidate(path
);
2526 dapm_free_path(path
);
2528 /* Update any path related flags */
2529 dapm_update_widget_flags(wsource
);
2530 dapm_update_widget_flags(wsink
);
2532 dev_warn(dapm
->dev
, "ASoC: Route %s->%s does not exist\n",
2540 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2541 * @dapm: DAPM context
2542 * @route: audio routes
2543 * @num: number of routes
2545 * Connects 2 dapm widgets together via a named audio path. The sink is
2546 * the widget receiving the audio signal, whilst the source is the sender
2547 * of the audio signal.
2549 * Returns 0 for success else error. On error all resources can be freed
2550 * with a call to snd_soc_card_free().
2552 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context
*dapm
,
2553 const struct snd_soc_dapm_route
*route
, int num
)
2557 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2558 for (i
= 0; i
< num
; i
++) {
2559 r
= snd_soc_dapm_add_route(dapm
, route
);
2561 dev_err(dapm
->dev
, "ASoC: Failed to add route %s -> %s -> %s\n",
2563 route
->control
? route
->control
: "direct",
2569 mutex_unlock(&dapm
->card
->dapm_mutex
);
2573 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
2576 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2577 * @dapm: DAPM context
2578 * @route: audio routes
2579 * @num: number of routes
2581 * Removes routes from the DAPM context.
2583 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context
*dapm
,
2584 const struct snd_soc_dapm_route
*route
, int num
)
2588 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2589 for (i
= 0; i
< num
; i
++) {
2590 snd_soc_dapm_del_route(dapm
, route
);
2593 mutex_unlock(&dapm
->card
->dapm_mutex
);
2597 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes
);
2599 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context
*dapm
,
2600 const struct snd_soc_dapm_route
*route
)
2602 struct snd_soc_dapm_widget
*source
= dapm_find_widget(dapm
,
2605 struct snd_soc_dapm_widget
*sink
= dapm_find_widget(dapm
,
2608 struct snd_soc_dapm_path
*path
;
2612 dev_err(dapm
->dev
, "ASoC: Unable to find source %s for weak route\n",
2618 dev_err(dapm
->dev
, "ASoC: Unable to find sink %s for weak route\n",
2623 if (route
->control
|| route
->connected
)
2624 dev_warn(dapm
->dev
, "ASoC: Ignoring control for weak route %s->%s\n",
2625 route
->source
, route
->sink
);
2627 list_for_each_entry(path
, &source
->sinks
, list_source
) {
2628 if (path
->sink
== sink
) {
2635 dev_err(dapm
->dev
, "ASoC: No path found for weak route %s->%s\n",
2636 route
->source
, route
->sink
);
2638 dev_warn(dapm
->dev
, "ASoC: %d paths found for weak route %s->%s\n",
2639 count
, route
->source
, route
->sink
);
2645 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2646 * @dapm: DAPM context
2647 * @route: audio routes
2648 * @num: number of routes
2650 * Mark existing routes matching those specified in the passed array
2651 * as being weak, meaning that they are ignored for the purpose of
2652 * power decisions. The main intended use case is for sidetone paths
2653 * which couple audio between other independent paths if they are both
2654 * active in order to make the combination work better at the user
2655 * level but which aren't intended to be "used".
2657 * Note that CODEC drivers should not use this as sidetone type paths
2658 * can frequently also be used as bypass paths.
2660 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context
*dapm
,
2661 const struct snd_soc_dapm_route
*route
, int num
)
2666 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2667 for (i
= 0; i
< num
; i
++) {
2668 err
= snd_soc_dapm_weak_route(dapm
, route
);
2673 mutex_unlock(&dapm
->card
->dapm_mutex
);
2677 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes
);
2680 * snd_soc_dapm_new_widgets - add new dapm widgets
2681 * @dapm: DAPM context
2683 * Checks the codec for any new dapm widgets and creates them if found.
2685 * Returns 0 for success.
2687 int snd_soc_dapm_new_widgets(struct snd_soc_card
*card
)
2689 struct snd_soc_dapm_widget
*w
;
2692 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2694 list_for_each_entry(w
, &card
->widgets
, list
)
2699 if (w
->num_kcontrols
) {
2700 w
->kcontrols
= kzalloc(w
->num_kcontrols
*
2701 sizeof(struct snd_kcontrol
*),
2703 if (!w
->kcontrols
) {
2704 mutex_unlock(&card
->dapm_mutex
);
2710 case snd_soc_dapm_switch
:
2711 case snd_soc_dapm_mixer
:
2712 case snd_soc_dapm_mixer_named_ctl
:
2715 case snd_soc_dapm_mux
:
2718 case snd_soc_dapm_pga
:
2719 case snd_soc_dapm_out_drv
:
2726 /* Read the initial power state from the device */
2728 soc_dapm_read(w
->dapm
, w
->reg
, &val
);
2729 val
= val
>> w
->shift
;
2731 if (val
== w
->on_val
)
2737 dapm_mark_dirty(w
, "new widget");
2738 dapm_debugfs_add_widget(w
);
2741 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2742 mutex_unlock(&card
->dapm_mutex
);
2745 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
2748 * snd_soc_dapm_get_volsw - dapm mixer get callback
2749 * @kcontrol: mixer control
2750 * @ucontrol: control element information
2752 * Callback to get the value of a dapm mixer control.
2754 * Returns 0 for success.
2756 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
2757 struct snd_ctl_elem_value
*ucontrol
)
2759 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
2760 struct snd_soc_card
*card
= dapm
->card
;
2761 struct soc_mixer_control
*mc
=
2762 (struct soc_mixer_control
*)kcontrol
->private_value
;
2764 unsigned int shift
= mc
->shift
;
2766 unsigned int mask
= (1 << fls(max
)) - 1;
2767 unsigned int invert
= mc
->invert
;
2771 if (snd_soc_volsw_is_stereo(mc
))
2773 "ASoC: Control '%s' is stereo, which is not supported\n",
2776 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2777 if (dapm_kcontrol_is_powered(kcontrol
) && reg
!= SND_SOC_NOPM
) {
2778 ret
= soc_dapm_read(dapm
, reg
, &val
);
2779 val
= (val
>> shift
) & mask
;
2781 val
= dapm_kcontrol_get_value(kcontrol
);
2783 mutex_unlock(&card
->dapm_mutex
);
2786 ucontrol
->value
.integer
.value
[0] = max
- val
;
2788 ucontrol
->value
.integer
.value
[0] = val
;
2792 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
2795 * snd_soc_dapm_put_volsw - dapm mixer set callback
2796 * @kcontrol: mixer control
2797 * @ucontrol: control element information
2799 * Callback to set the value of a dapm mixer control.
2801 * Returns 0 for success.
2803 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
2804 struct snd_ctl_elem_value
*ucontrol
)
2806 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
2807 struct snd_soc_card
*card
= dapm
->card
;
2808 struct soc_mixer_control
*mc
=
2809 (struct soc_mixer_control
*)kcontrol
->private_value
;
2811 unsigned int shift
= mc
->shift
;
2813 unsigned int mask
= (1 << fls(max
)) - 1;
2814 unsigned int invert
= mc
->invert
;
2816 int connect
, change
, reg_change
= 0;
2817 struct snd_soc_dapm_update update
;
2820 if (snd_soc_volsw_is_stereo(mc
))
2822 "ASoC: Control '%s' is stereo, which is not supported\n",
2825 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2831 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2833 change
= dapm_kcontrol_set_value(kcontrol
, val
);
2835 if (reg
!= SND_SOC_NOPM
) {
2836 mask
= mask
<< shift
;
2839 reg_change
= soc_dapm_test_bits(dapm
, reg
, mask
, val
);
2842 if (change
|| reg_change
) {
2844 update
.kcontrol
= kcontrol
;
2848 card
->update
= &update
;
2850 change
|= reg_change
;
2852 ret
= soc_dapm_mixer_update_power(card
, kcontrol
, connect
);
2854 card
->update
= NULL
;
2857 mutex_unlock(&card
->dapm_mutex
);
2860 soc_dpcm_runtime_update(card
);
2864 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
2867 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2868 * @kcontrol: mixer control
2869 * @ucontrol: control element information
2871 * Callback to get the value of a dapm enumerated double mixer control.
2873 * Returns 0 for success.
2875 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
2876 struct snd_ctl_elem_value
*ucontrol
)
2878 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
2879 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2880 unsigned int reg_val
, val
;
2882 if (e
->reg
!= SND_SOC_NOPM
) {
2883 int ret
= soc_dapm_read(dapm
, e
->reg
, ®_val
);
2887 reg_val
= dapm_kcontrol_get_value(kcontrol
);
2890 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
2891 ucontrol
->value
.enumerated
.item
[0] = snd_soc_enum_val_to_item(e
, val
);
2892 if (e
->shift_l
!= e
->shift_r
) {
2893 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
2894 val
= snd_soc_enum_val_to_item(e
, val
);
2895 ucontrol
->value
.enumerated
.item
[1] = val
;
2900 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
2903 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2904 * @kcontrol: mixer control
2905 * @ucontrol: control element information
2907 * Callback to set the value of a dapm enumerated double mixer control.
2909 * Returns 0 for success.
2911 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
2912 struct snd_ctl_elem_value
*ucontrol
)
2914 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kcontrol
);
2915 struct snd_soc_card
*card
= dapm
->card
;
2916 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2917 unsigned int *item
= ucontrol
->value
.enumerated
.item
;
2918 unsigned int val
, change
;
2920 struct snd_soc_dapm_update update
;
2923 if (item
[0] >= e
->items
)
2926 val
= snd_soc_enum_item_to_val(e
, item
[0]) << e
->shift_l
;
2927 mask
= e
->mask
<< e
->shift_l
;
2928 if (e
->shift_l
!= e
->shift_r
) {
2929 if (item
[1] > e
->items
)
2931 val
|= snd_soc_enum_item_to_val(e
, item
[1]) << e
->shift_l
;
2932 mask
|= e
->mask
<< e
->shift_r
;
2935 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2937 if (e
->reg
!= SND_SOC_NOPM
)
2938 change
= soc_dapm_test_bits(dapm
, e
->reg
, mask
, val
);
2940 change
= dapm_kcontrol_set_value(kcontrol
, val
);
2943 if (e
->reg
!= SND_SOC_NOPM
) {
2944 update
.kcontrol
= kcontrol
;
2945 update
.reg
= e
->reg
;
2948 card
->update
= &update
;
2951 ret
= soc_dapm_mux_update_power(card
, kcontrol
, item
[0], e
);
2953 card
->update
= NULL
;
2956 mutex_unlock(&card
->dapm_mutex
);
2959 soc_dpcm_runtime_update(card
);
2963 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
2966 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2968 * @kcontrol: mixer control
2969 * @uinfo: control element information
2971 * Callback to provide information about a pin switch control.
2973 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
2974 struct snd_ctl_elem_info
*uinfo
)
2976 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2978 uinfo
->value
.integer
.min
= 0;
2979 uinfo
->value
.integer
.max
= 1;
2983 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
2986 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2988 * @kcontrol: mixer control
2991 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
2992 struct snd_ctl_elem_value
*ucontrol
)
2994 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
2995 const char *pin
= (const char *)kcontrol
->private_value
;
2997 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2999 ucontrol
->value
.integer
.value
[0] =
3000 snd_soc_dapm_get_pin_status(&card
->dapm
, pin
);
3002 mutex_unlock(&card
->dapm_mutex
);
3006 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
3009 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3011 * @kcontrol: mixer control
3014 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
3015 struct snd_ctl_elem_value
*ucontrol
)
3017 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
3018 const char *pin
= (const char *)kcontrol
->private_value
;
3020 if (ucontrol
->value
.integer
.value
[0])
3021 snd_soc_dapm_enable_pin(&card
->dapm
, pin
);
3023 snd_soc_dapm_disable_pin(&card
->dapm
, pin
);
3025 snd_soc_dapm_sync(&card
->dapm
);
3028 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
3030 static struct snd_soc_dapm_widget
*
3031 snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
3032 const struct snd_soc_dapm_widget
*widget
)
3034 struct snd_soc_dapm_widget
*w
;
3038 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
3042 case snd_soc_dapm_regulator_supply
:
3043 w
->regulator
= devm_regulator_get(dapm
->dev
, w
->name
);
3044 if (IS_ERR(w
->regulator
)) {
3045 ret
= PTR_ERR(w
->regulator
);
3046 dev_err(dapm
->dev
, "ASoC: Failed to request %s: %d\n",
3051 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
3052 ret
= regulator_allow_bypass(w
->regulator
, true);
3054 dev_warn(w
->dapm
->dev
,
3055 "ASoC: Failed to bypass %s: %d\n",
3059 case snd_soc_dapm_clock_supply
:
3060 #ifdef CONFIG_CLKDEV_LOOKUP
3061 w
->clk
= devm_clk_get(dapm
->dev
, w
->name
);
3062 if (IS_ERR(w
->clk
)) {
3063 ret
= PTR_ERR(w
->clk
);
3064 dev_err(dapm
->dev
, "ASoC: Failed to request %s: %d\n",
3076 prefix
= soc_dapm_prefix(dapm
);
3078 w
->name
= kasprintf(GFP_KERNEL
, "%s %s", prefix
, widget
->name
);
3080 w
->name
= kasprintf(GFP_KERNEL
, "%s", widget
->name
);
3082 if (w
->name
== NULL
) {
3088 case snd_soc_dapm_mic
:
3090 w
->power_check
= dapm_generic_check_power
;
3092 case snd_soc_dapm_input
:
3093 if (!dapm
->card
->fully_routed
)
3095 w
->power_check
= dapm_generic_check_power
;
3097 case snd_soc_dapm_spk
:
3098 case snd_soc_dapm_hp
:
3100 w
->power_check
= dapm_generic_check_power
;
3102 case snd_soc_dapm_output
:
3103 if (!dapm
->card
->fully_routed
)
3105 w
->power_check
= dapm_generic_check_power
;
3107 case snd_soc_dapm_vmid
:
3108 case snd_soc_dapm_siggen
:
3110 w
->power_check
= dapm_always_on_check_power
;
3112 case snd_soc_dapm_mux
:
3113 case snd_soc_dapm_switch
:
3114 case snd_soc_dapm_mixer
:
3115 case snd_soc_dapm_mixer_named_ctl
:
3116 case snd_soc_dapm_adc
:
3117 case snd_soc_dapm_aif_out
:
3118 case snd_soc_dapm_dac
:
3119 case snd_soc_dapm_aif_in
:
3120 case snd_soc_dapm_pga
:
3121 case snd_soc_dapm_out_drv
:
3122 case snd_soc_dapm_micbias
:
3123 case snd_soc_dapm_line
:
3124 case snd_soc_dapm_dai_link
:
3125 case snd_soc_dapm_dai_out
:
3126 case snd_soc_dapm_dai_in
:
3127 w
->power_check
= dapm_generic_check_power
;
3129 case snd_soc_dapm_supply
:
3130 case snd_soc_dapm_regulator_supply
:
3131 case snd_soc_dapm_clock_supply
:
3132 case snd_soc_dapm_kcontrol
:
3134 w
->power_check
= dapm_supply_check_power
;
3137 w
->power_check
= dapm_always_on_check_power
;
3142 INIT_LIST_HEAD(&w
->sources
);
3143 INIT_LIST_HEAD(&w
->sinks
);
3144 INIT_LIST_HEAD(&w
->list
);
3145 INIT_LIST_HEAD(&w
->dirty
);
3146 list_add(&w
->list
, &dapm
->card
->widgets
);
3151 /* machine layer set ups unconnected pins and insertions */
3157 * snd_soc_dapm_new_controls - create new dapm controls
3158 * @dapm: DAPM context
3159 * @widget: widget array
3160 * @num: number of widgets
3162 * Creates new DAPM controls based upon the templates.
3164 * Returns 0 for success else error.
3166 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context
*dapm
,
3167 const struct snd_soc_dapm_widget
*widget
,
3170 struct snd_soc_dapm_widget
*w
;
3174 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
3175 for (i
= 0; i
< num
; i
++) {
3176 w
= snd_soc_dapm_new_control(dapm
, widget
);
3179 "ASoC: Failed to create DAPM control %s\n",
3186 mutex_unlock(&dapm
->card
->dapm_mutex
);
3189 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
3191 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget
*w
,
3192 struct snd_kcontrol
*kcontrol
, int event
)
3194 struct snd_soc_dapm_path
*source_p
, *sink_p
;
3195 struct snd_soc_dai
*source
, *sink
;
3196 const struct snd_soc_pcm_stream
*config
= w
->params
;
3197 struct snd_pcm_substream substream
;
3198 struct snd_pcm_hw_params
*params
= NULL
;
3202 if (WARN_ON(!config
) ||
3203 WARN_ON(list_empty(&w
->sources
) || list_empty(&w
->sinks
)))
3206 /* We only support a single source and sink, pick the first */
3207 source_p
= list_first_entry(&w
->sources
, struct snd_soc_dapm_path
,
3209 sink_p
= list_first_entry(&w
->sinks
, struct snd_soc_dapm_path
,
3212 if (WARN_ON(!source_p
|| !sink_p
) ||
3213 WARN_ON(!sink_p
->source
|| !source_p
->sink
) ||
3214 WARN_ON(!source_p
->source
|| !sink_p
->sink
))
3217 source
= source_p
->source
->priv
;
3218 sink
= sink_p
->sink
->priv
;
3220 /* Be a little careful as we don't want to overflow the mask array */
3221 if (config
->formats
) {
3222 fmt
= ffs(config
->formats
) - 1;
3224 dev_warn(w
->dapm
->dev
, "ASoC: Invalid format %llx specified\n",
3229 /* Currently very limited parameter selection */
3230 params
= kzalloc(sizeof(*params
), GFP_KERNEL
);
3235 snd_mask_set(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), fmt
);
3237 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
)->min
=
3239 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
)->max
=
3242 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
)->min
3243 = config
->channels_min
;
3244 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
)->max
3245 = config
->channels_max
;
3247 memset(&substream
, 0, sizeof(substream
));
3250 case SND_SOC_DAPM_PRE_PMU
:
3251 substream
.stream
= SNDRV_PCM_STREAM_CAPTURE
;
3252 ret
= soc_dai_hw_params(&substream
, params
, source
);
3256 substream
.stream
= SNDRV_PCM_STREAM_PLAYBACK
;
3257 ret
= soc_dai_hw_params(&substream
, params
, sink
);
3262 case SND_SOC_DAPM_POST_PMU
:
3263 ret
= snd_soc_dai_digital_mute(sink
, 0,
3264 SNDRV_PCM_STREAM_PLAYBACK
);
3265 if (ret
!= 0 && ret
!= -ENOTSUPP
)
3266 dev_warn(sink
->dev
, "ASoC: Failed to unmute: %d\n", ret
);
3270 case SND_SOC_DAPM_PRE_PMD
:
3271 ret
= snd_soc_dai_digital_mute(sink
, 1,
3272 SNDRV_PCM_STREAM_PLAYBACK
);
3273 if (ret
!= 0 && ret
!= -ENOTSUPP
)
3274 dev_warn(sink
->dev
, "ASoC: Failed to mute: %d\n", ret
);
3279 WARN(1, "Unknown event %d\n", event
);
3288 int snd_soc_dapm_new_pcm(struct snd_soc_card
*card
,
3289 const struct snd_soc_pcm_stream
*params
,
3290 struct snd_soc_dapm_widget
*source
,
3291 struct snd_soc_dapm_widget
*sink
)
3293 struct snd_soc_dapm_widget
template;
3294 struct snd_soc_dapm_widget
*w
;
3299 len
= strlen(source
->name
) + strlen(sink
->name
) + 2;
3300 link_name
= devm_kzalloc(card
->dev
, len
, GFP_KERNEL
);
3303 snprintf(link_name
, len
, "%s-%s", source
->name
, sink
->name
);
3305 memset(&template, 0, sizeof(template));
3306 template.reg
= SND_SOC_NOPM
;
3307 template.id
= snd_soc_dapm_dai_link
;
3308 template.name
= link_name
;
3309 template.event
= snd_soc_dai_link_event
;
3310 template.event_flags
= SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMU
|
3311 SND_SOC_DAPM_PRE_PMD
;
3313 dev_dbg(card
->dev
, "ASoC: adding %s widget\n", link_name
);
3315 w
= snd_soc_dapm_new_control(&card
->dapm
, &template);
3317 dev_err(card
->dev
, "ASoC: Failed to create %s widget\n",
3324 ret
= snd_soc_dapm_add_path(&card
->dapm
, source
, w
, NULL
, NULL
);
3327 return snd_soc_dapm_add_path(&card
->dapm
, w
, sink
, NULL
, NULL
);
3330 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context
*dapm
,
3331 struct snd_soc_dai
*dai
)
3333 struct snd_soc_dapm_widget
template;
3334 struct snd_soc_dapm_widget
*w
;
3336 WARN_ON(dapm
->dev
!= dai
->dev
);
3338 memset(&template, 0, sizeof(template));
3339 template.reg
= SND_SOC_NOPM
;
3341 if (dai
->driver
->playback
.stream_name
) {
3342 template.id
= snd_soc_dapm_dai_in
;
3343 template.name
= dai
->driver
->playback
.stream_name
;
3344 template.sname
= dai
->driver
->playback
.stream_name
;
3346 dev_dbg(dai
->dev
, "ASoC: adding %s widget\n",
3349 w
= snd_soc_dapm_new_control(dapm
, &template);
3351 dev_err(dapm
->dev
, "ASoC: Failed to create %s widget\n",
3352 dai
->driver
->playback
.stream_name
);
3357 dai
->playback_widget
= w
;
3360 if (dai
->driver
->capture
.stream_name
) {
3361 template.id
= snd_soc_dapm_dai_out
;
3362 template.name
= dai
->driver
->capture
.stream_name
;
3363 template.sname
= dai
->driver
->capture
.stream_name
;
3365 dev_dbg(dai
->dev
, "ASoC: adding %s widget\n",
3368 w
= snd_soc_dapm_new_control(dapm
, &template);
3370 dev_err(dapm
->dev
, "ASoC: Failed to create %s widget\n",
3371 dai
->driver
->capture
.stream_name
);
3376 dai
->capture_widget
= w
;
3382 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card
*card
)
3384 struct snd_soc_dapm_widget
*dai_w
, *w
;
3385 struct snd_soc_dapm_widget
*src
, *sink
;
3386 struct snd_soc_dai
*dai
;
3388 /* For each DAI widget... */
3389 list_for_each_entry(dai_w
, &card
->widgets
, list
) {
3390 switch (dai_w
->id
) {
3391 case snd_soc_dapm_dai_in
:
3392 case snd_soc_dapm_dai_out
:
3400 /* ...find all widgets with the same stream and link them */
3401 list_for_each_entry(w
, &card
->widgets
, list
) {
3402 if (w
->dapm
!= dai_w
->dapm
)
3406 case snd_soc_dapm_dai_in
:
3407 case snd_soc_dapm_dai_out
:
3413 if (!w
->sname
|| !strstr(w
->sname
, dai_w
->name
))
3416 if (dai_w
->id
== snd_soc_dapm_dai_in
) {
3423 dev_dbg(dai
->dev
, "%s -> %s\n", src
->name
, sink
->name
);
3424 snd_soc_dapm_add_path(w
->dapm
, src
, sink
, NULL
, NULL
);
3431 static void dapm_connect_dai_link_widgets(struct snd_soc_card
*card
,
3432 struct snd_soc_pcm_runtime
*rtd
)
3434 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
3435 struct snd_soc_dapm_widget
*sink
, *source
;
3438 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
3439 struct snd_soc_dai
*codec_dai
= rtd
->codec_dais
[i
];
3441 /* there is no point in connecting BE DAI links with dummies */
3442 if (snd_soc_dai_is_dummy(codec_dai
) ||
3443 snd_soc_dai_is_dummy(cpu_dai
))
3446 /* connect BE DAI playback if widgets are valid */
3447 if (codec_dai
->playback_widget
&& cpu_dai
->playback_widget
) {
3448 source
= cpu_dai
->playback_widget
;
3449 sink
= codec_dai
->playback_widget
;
3450 dev_dbg(rtd
->dev
, "connected DAI link %s:%s -> %s:%s\n",
3451 cpu_dai
->component
->name
, source
->name
,
3452 codec_dai
->component
->name
, sink
->name
);
3454 snd_soc_dapm_add_path(&card
->dapm
, source
, sink
,
3458 /* connect BE DAI capture if widgets are valid */
3459 if (codec_dai
->capture_widget
&& cpu_dai
->capture_widget
) {
3460 source
= codec_dai
->capture_widget
;
3461 sink
= cpu_dai
->capture_widget
;
3462 dev_dbg(rtd
->dev
, "connected DAI link %s:%s -> %s:%s\n",
3463 codec_dai
->component
->name
, source
->name
,
3464 cpu_dai
->component
->name
, sink
->name
);
3466 snd_soc_dapm_add_path(&card
->dapm
, source
, sink
,
3472 static void soc_dapm_dai_stream_event(struct snd_soc_dai
*dai
, int stream
,
3475 struct snd_soc_dapm_widget
*w
;
3477 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
3478 w
= dai
->playback_widget
;
3480 w
= dai
->capture_widget
;
3483 dapm_mark_dirty(w
, "stream event");
3486 case SND_SOC_DAPM_STREAM_START
:
3489 case SND_SOC_DAPM_STREAM_STOP
:
3492 case SND_SOC_DAPM_STREAM_SUSPEND
:
3493 case SND_SOC_DAPM_STREAM_RESUME
:
3494 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
3495 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
3499 if (w
->id
== snd_soc_dapm_dai_in
) {
3500 w
->is_source
= w
->active
;
3501 dapm_widget_invalidate_input_paths(w
);
3503 w
->is_sink
= w
->active
;
3504 dapm_widget_invalidate_output_paths(w
);
3509 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card
*card
)
3511 struct snd_soc_pcm_runtime
*rtd
= card
->rtd
;
3514 /* for each BE DAI link... */
3515 for (i
= 0; i
< card
->num_rtd
; i
++) {
3516 rtd
= &card
->rtd
[i
];
3519 * dynamic FE links have no fixed DAI mapping.
3520 * CODEC<->CODEC links have no direct connection.
3522 if (rtd
->dai_link
->dynamic
|| rtd
->dai_link
->params
)
3525 dapm_connect_dai_link_widgets(card
, rtd
);
3529 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
, int stream
,
3534 soc_dapm_dai_stream_event(rtd
->cpu_dai
, stream
, event
);
3535 for (i
= 0; i
< rtd
->num_codecs
; i
++)
3536 soc_dapm_dai_stream_event(rtd
->codec_dais
[i
], stream
, event
);
3538 dapm_power_widgets(rtd
->card
, event
);
3542 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3543 * @rtd: PCM runtime data
3544 * @stream: stream name
3545 * @event: stream event
3547 * Sends a stream event to the dapm core. The core then makes any
3548 * necessary widget power changes.
3550 * Returns 0 for success else error.
3552 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
, int stream
,
3555 struct snd_soc_card
*card
= rtd
->card
;
3557 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3558 soc_dapm_stream_event(rtd
, stream
, event
);
3559 mutex_unlock(&card
->dapm_mutex
);
3563 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3564 * @dapm: DAPM context
3567 * Enables input/output pin and its parents or children widgets iff there is
3568 * a valid audio route and active audio stream.
3570 * Requires external locking.
3572 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3573 * do any widget power switching.
3575 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
3578 return snd_soc_dapm_set_pin(dapm
, pin
, 1);
3580 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked
);
3583 * snd_soc_dapm_enable_pin - enable pin.
3584 * @dapm: DAPM context
3587 * Enables input/output pin and its parents or children widgets iff there is
3588 * a valid audio route and active audio stream.
3590 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3591 * do any widget power switching.
3593 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
3597 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3599 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 1);
3601 mutex_unlock(&dapm
->card
->dapm_mutex
);
3605 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
3608 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3609 * @dapm: DAPM context
3612 * Enables input/output pin regardless of any other state. This is
3613 * intended for use with microphone bias supplies used in microphone
3616 * Requires external locking.
3618 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3619 * do any widget power switching.
3621 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
3624 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
3627 dev_err(dapm
->dev
, "ASoC: unknown pin %s\n", pin
);
3631 dev_dbg(w
->dapm
->dev
, "ASoC: force enable pin %s\n", pin
);
3632 if (!w
->connected
) {
3634 * w->force does not affect the number of input or output paths,
3635 * so we only have to recheck if w->connected is changed
3637 dapm_widget_invalidate_input_paths(w
);
3638 dapm_widget_invalidate_output_paths(w
);
3642 dapm_mark_dirty(w
, "force enable");
3646 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked
);
3649 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3650 * @dapm: DAPM context
3653 * Enables input/output pin regardless of any other state. This is
3654 * intended for use with microphone bias supplies used in microphone
3657 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3658 * do any widget power switching.
3660 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context
*dapm
,
3665 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3667 ret
= snd_soc_dapm_force_enable_pin_unlocked(dapm
, pin
);
3669 mutex_unlock(&dapm
->card
->dapm_mutex
);
3673 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
3676 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3677 * @dapm: DAPM context
3680 * Disables input/output pin and its parents or children widgets.
3682 * Requires external locking.
3684 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3685 * do any widget power switching.
3687 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
3690 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
3692 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked
);
3695 * snd_soc_dapm_disable_pin - disable pin.
3696 * @dapm: DAPM context
3699 * Disables input/output pin and its parents or children widgets.
3701 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3702 * do any widget power switching.
3704 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context
*dapm
,
3709 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3711 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 0);
3713 mutex_unlock(&dapm
->card
->dapm_mutex
);
3717 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
3720 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3721 * @dapm: DAPM context
3724 * Marks the specified pin as being not connected, disabling it along
3725 * any parent or child widgets. At present this is identical to
3726 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3727 * additional things such as disabling controls which only affect
3728 * paths through the pin.
3730 * Requires external locking.
3732 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3733 * do any widget power switching.
3735 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
3738 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
3740 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked
);
3743 * snd_soc_dapm_nc_pin - permanently disable pin.
3744 * @dapm: DAPM context
3747 * Marks the specified pin as being not connected, disabling it along
3748 * any parent or child widgets. At present this is identical to
3749 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3750 * additional things such as disabling controls which only affect
3751 * paths through the pin.
3753 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3754 * do any widget power switching.
3756 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
3760 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3762 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 0);
3764 mutex_unlock(&dapm
->card
->dapm_mutex
);
3768 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
3771 * snd_soc_dapm_get_pin_status - get audio pin status
3772 * @dapm: DAPM context
3773 * @pin: audio signal pin endpoint (or start point)
3775 * Get audio pin status - connected or disconnected.
3777 * Returns 1 for connected otherwise 0.
3779 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context
*dapm
,
3782 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
3785 return w
->connected
;
3789 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
3792 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3793 * @dapm: DAPM context
3794 * @pin: audio signal pin endpoint (or start point)
3796 * Mark the given endpoint or pin as ignoring suspend. When the
3797 * system is disabled a path between two endpoints flagged as ignoring
3798 * suspend will not be disabled. The path must already be enabled via
3799 * normal means at suspend time, it will not be turned on if it was not
3802 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context
*dapm
,
3805 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, false);
3808 dev_err(dapm
->dev
, "ASoC: unknown pin %s\n", pin
);
3812 w
->ignore_suspend
= 1;
3816 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
3819 * snd_soc_dapm_free - free dapm resources
3820 * @dapm: DAPM context
3822 * Free all dapm widgets and resources.
3824 void snd_soc_dapm_free(struct snd_soc_dapm_context
*dapm
)
3826 dapm_debugfs_cleanup(dapm
);
3827 dapm_free_widgets(dapm
);
3828 list_del(&dapm
->list
);
3830 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
3832 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context
*dapm
)
3834 struct snd_soc_card
*card
= dapm
->card
;
3835 struct snd_soc_dapm_widget
*w
;
3836 LIST_HEAD(down_list
);
3839 mutex_lock(&card
->dapm_mutex
);
3841 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
3842 if (w
->dapm
!= dapm
)
3845 dapm_seq_insert(w
, &down_list
, false);
3851 /* If there were no widgets to power down we're already in
3855 if (dapm
->bias_level
== SND_SOC_BIAS_ON
)
3856 snd_soc_dapm_set_bias_level(dapm
,
3857 SND_SOC_BIAS_PREPARE
);
3858 dapm_seq_run(card
, &down_list
, 0, false);
3859 if (dapm
->bias_level
== SND_SOC_BIAS_PREPARE
)
3860 snd_soc_dapm_set_bias_level(dapm
,
3861 SND_SOC_BIAS_STANDBY
);
3864 mutex_unlock(&card
->dapm_mutex
);
3868 * snd_soc_dapm_shutdown - callback for system shutdown
3870 void snd_soc_dapm_shutdown(struct snd_soc_card
*card
)
3872 struct snd_soc_dapm_context
*dapm
;
3874 list_for_each_entry(dapm
, &card
->dapm_list
, list
) {
3875 if (dapm
!= &card
->dapm
) {
3876 soc_dapm_shutdown_dapm(dapm
);
3877 if (dapm
->bias_level
== SND_SOC_BIAS_STANDBY
)
3878 snd_soc_dapm_set_bias_level(dapm
,
3883 soc_dapm_shutdown_dapm(&card
->dapm
);
3884 if (card
->dapm
.bias_level
== SND_SOC_BIAS_STANDBY
)
3885 snd_soc_dapm_set_bias_level(&card
->dapm
,
3889 /* Module information */
3890 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3891 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3892 MODULE_LICENSE("GPL");