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
);
262 static struct snd_soc_dapm_widget_list
*dapm_kcontrol_get_wlist(
263 const struct snd_kcontrol
*kcontrol
)
265 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
270 static int dapm_kcontrol_add_widget(struct snd_kcontrol
*kcontrol
,
271 struct snd_soc_dapm_widget
*widget
)
273 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
274 struct snd_soc_dapm_widget_list
*new_wlist
;
278 n
= data
->wlist
->num_widgets
+ 1;
282 new_wlist
= krealloc(data
->wlist
,
283 sizeof(*new_wlist
) + sizeof(widget
) * n
, GFP_KERNEL
);
287 new_wlist
->widgets
[n
- 1] = widget
;
288 new_wlist
->num_widgets
= n
;
290 data
->wlist
= new_wlist
;
295 static void dapm_kcontrol_add_path(const struct snd_kcontrol
*kcontrol
,
296 struct snd_soc_dapm_path
*path
)
298 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
300 list_add_tail(&path
->list_kcontrol
, &data
->paths
);
303 snd_soc_dapm_add_path(data
->widget
->dapm
, data
->widget
,
304 path
->source
, NULL
, NULL
);
308 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol
*kcontrol
)
310 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
315 return data
->widget
->power
;
318 static struct list_head
*dapm_kcontrol_get_path_list(
319 const struct snd_kcontrol
*kcontrol
)
321 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
326 #define dapm_kcontrol_for_each_path(path, kcontrol) \
327 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
330 static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol
*kcontrol
)
332 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
337 static bool dapm_kcontrol_set_value(const struct snd_kcontrol
*kcontrol
,
340 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
342 if (data
->value
== value
)
346 data
->widget
->on_val
= value
;
354 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
355 * @kcontrol: The kcontrol
357 struct snd_soc_codec
*snd_soc_dapm_kcontrol_codec(struct snd_kcontrol
*kcontrol
)
359 return dapm_kcontrol_get_wlist(kcontrol
)->widgets
[0]->codec
;
361 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec
);
363 static void dapm_reset(struct snd_soc_card
*card
)
365 struct snd_soc_dapm_widget
*w
;
367 lockdep_assert_held(&card
->dapm_mutex
);
369 memset(&card
->dapm_stats
, 0, sizeof(card
->dapm_stats
));
371 list_for_each_entry(w
, &card
->widgets
, list
) {
372 w
->new_power
= w
->power
;
373 w
->power_checked
= false;
379 static int soc_widget_read(struct snd_soc_dapm_widget
*w
, int reg
,
383 *value
= snd_soc_read(w
->codec
, reg
);
385 } else if (w
->platform
) {
386 *value
= snd_soc_platform_read(w
->platform
, reg
);
390 dev_err(w
->dapm
->dev
, "ASoC: no valid widget read method\n");
394 static int soc_widget_write(struct snd_soc_dapm_widget
*w
, int reg
,
398 return snd_soc_write(w
->codec
, reg
, val
);
399 else if (w
->platform
)
400 return snd_soc_platform_write(w
->platform
, reg
, val
);
402 dev_err(w
->dapm
->dev
, "ASoC: no valid widget write method\n");
406 static inline void soc_widget_lock(struct snd_soc_dapm_widget
*w
)
408 if (w
->codec
&& !w
->codec
->using_regmap
)
409 mutex_lock(&w
->codec
->mutex
);
410 else if (w
->platform
)
411 mutex_lock(&w
->platform
->mutex
);
414 static inline void soc_widget_unlock(struct snd_soc_dapm_widget
*w
)
416 if (w
->codec
&& !w
->codec
->using_regmap
)
417 mutex_unlock(&w
->codec
->mutex
);
418 else if (w
->platform
)
419 mutex_unlock(&w
->platform
->mutex
);
422 static void soc_dapm_async_complete(struct snd_soc_dapm_context
*dapm
)
424 if (dapm
->codec
&& dapm
->codec
->using_regmap
)
425 regmap_async_complete(dapm
->codec
->control_data
);
428 static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget
*w
,
429 unsigned short reg
, unsigned int mask
, unsigned int value
)
432 unsigned int old
, new;
435 if (w
->codec
&& w
->codec
->using_regmap
) {
436 ret
= regmap_update_bits_check_async(w
->codec
->control_data
,
443 ret
= soc_widget_read(w
, reg
, &old
);
445 soc_widget_unlock(w
);
449 new = (old
& ~mask
) | (value
& mask
);
452 ret
= soc_widget_write(w
, reg
, new);
454 soc_widget_unlock(w
);
458 soc_widget_unlock(w
);
465 * snd_soc_dapm_set_bias_level - set the bias level for the system
466 * @dapm: DAPM context
467 * @level: level to configure
469 * Configure the bias (power) levels for the SoC audio device.
471 * Returns 0 for success else error.
473 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context
*dapm
,
474 enum snd_soc_bias_level level
)
476 struct snd_soc_card
*card
= dapm
->card
;
479 trace_snd_soc_bias_level_start(card
, level
);
481 if (card
&& card
->set_bias_level
)
482 ret
= card
->set_bias_level(card
, dapm
, level
);
487 if (dapm
->codec
->driver
->set_bias_level
)
488 ret
= dapm
->codec
->driver
->set_bias_level(dapm
->codec
,
491 dapm
->bias_level
= level
;
492 } else if (!card
|| dapm
!= &card
->dapm
) {
493 dapm
->bias_level
= level
;
499 if (card
&& card
->set_bias_level_post
)
500 ret
= card
->set_bias_level_post(card
, dapm
, level
);
502 trace_snd_soc_bias_level_done(card
, level
);
507 /* connect mux widget to its interconnecting audio paths */
508 static int dapm_connect_mux(struct snd_soc_dapm_context
*dapm
,
509 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
510 struct snd_soc_dapm_path
*path
, const char *control_name
,
511 const struct snd_kcontrol_new
*kcontrol
)
513 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
514 unsigned int val
, item
;
517 if (e
->reg
!= SND_SOC_NOPM
) {
518 soc_widget_read(dest
, e
->reg
, &val
);
519 val
= (val
>> e
->shift_l
) & e
->mask
;
520 item
= snd_soc_enum_val_to_item(e
, val
);
522 /* since a virtual mux has no backing registers to
523 * decide which path to connect, it will try to match
524 * with the first enumeration. This is to ensure
525 * that the default mux choice (the first) will be
526 * correctly powered up during initialization.
531 for (i
= 0; i
< e
->items
; i
++) {
532 if (!(strcmp(control_name
, e
->texts
[i
]))) {
533 list_add(&path
->list
, &dapm
->card
->paths
);
534 list_add(&path
->list_sink
, &dest
->sources
);
535 list_add(&path
->list_source
, &src
->sinks
);
536 path
->name
= (char*)e
->texts
[i
];
548 /* set up initial codec paths */
549 static void dapm_set_mixer_path_status(struct snd_soc_dapm_widget
*w
,
550 struct snd_soc_dapm_path
*p
, int i
)
552 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
553 w
->kcontrol_news
[i
].private_value
;
554 unsigned int reg
= mc
->reg
;
555 unsigned int shift
= mc
->shift
;
556 unsigned int max
= mc
->max
;
557 unsigned int mask
= (1 << fls(max
)) - 1;
558 unsigned int invert
= mc
->invert
;
561 if (reg
!= SND_SOC_NOPM
) {
562 soc_widget_read(w
, reg
, &val
);
563 val
= (val
>> shift
) & mask
;
572 /* connect mixer widget to its interconnecting audio paths */
573 static int dapm_connect_mixer(struct snd_soc_dapm_context
*dapm
,
574 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
575 struct snd_soc_dapm_path
*path
, const char *control_name
)
579 /* search for mixer kcontrol */
580 for (i
= 0; i
< dest
->num_kcontrols
; i
++) {
581 if (!strcmp(control_name
, dest
->kcontrol_news
[i
].name
)) {
582 list_add(&path
->list
, &dapm
->card
->paths
);
583 list_add(&path
->list_sink
, &dest
->sources
);
584 list_add(&path
->list_source
, &src
->sinks
);
585 path
->name
= dest
->kcontrol_news
[i
].name
;
586 dapm_set_mixer_path_status(dest
, path
, i
);
593 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context
*dapm
,
594 struct snd_soc_dapm_widget
*kcontrolw
,
595 const struct snd_kcontrol_new
*kcontrol_new
,
596 struct snd_kcontrol
**kcontrol
)
598 struct snd_soc_dapm_widget
*w
;
603 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
604 if (w
== kcontrolw
|| w
->dapm
!= kcontrolw
->dapm
)
606 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
607 if (&w
->kcontrol_news
[i
] == kcontrol_new
) {
609 *kcontrol
= w
->kcontrols
[i
];
619 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
620 * create it. Either way, add the widget into the control's widget list
622 static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget
*w
,
625 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
626 struct snd_card
*card
= dapm
->card
->snd_card
;
630 struct snd_kcontrol
*kcontrol
;
631 bool wname_in_long_name
, kcname_in_long_name
;
637 prefix
= dapm
->codec
->name_prefix
;
642 prefix_len
= strlen(prefix
) + 1;
646 shared
= dapm_is_shared_kcontrol(dapm
, w
, &w
->kcontrol_news
[kci
],
651 wname_in_long_name
= false;
652 kcname_in_long_name
= true;
655 case snd_soc_dapm_switch
:
656 case snd_soc_dapm_mixer
:
657 wname_in_long_name
= true;
658 kcname_in_long_name
= true;
660 case snd_soc_dapm_mixer_named_ctl
:
661 wname_in_long_name
= false;
662 kcname_in_long_name
= true;
664 case snd_soc_dapm_mux
:
665 wname_in_long_name
= true;
666 kcname_in_long_name
= false;
673 if (wname_in_long_name
&& kcname_in_long_name
) {
675 * The control will get a prefix from the control
676 * creation process but we're also using the same
677 * prefix for widgets so cut the prefix off the
678 * front of the widget name.
680 long_name
= kasprintf(GFP_KERNEL
, "%s %s",
681 w
->name
+ prefix_len
,
682 w
->kcontrol_news
[kci
].name
);
683 if (long_name
== NULL
)
687 } else if (wname_in_long_name
) {
689 name
= w
->name
+ prefix_len
;
692 name
= w
->kcontrol_news
[kci
].name
;
695 kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[kci
], NULL
, name
,
700 kcontrol
->private_free
= dapm_kcontrol_free
;
702 ret
= dapm_kcontrol_data_alloc(w
, kcontrol
);
704 snd_ctl_free_one(kcontrol
);
708 ret
= snd_ctl_add(card
, kcontrol
);
711 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
717 ret
= dapm_kcontrol_add_widget(kcontrol
, w
);
721 w
->kcontrols
[kci
] = kcontrol
;
726 /* create new dapm mixer control */
727 static int dapm_new_mixer(struct snd_soc_dapm_widget
*w
)
730 struct snd_soc_dapm_path
*path
;
733 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
735 list_for_each_entry(path
, &w
->sources
, list_sink
) {
736 /* mixer/mux paths name must match control name */
737 if (path
->name
!= (char *)w
->kcontrol_news
[i
].name
)
740 if (w
->kcontrols
[i
]) {
741 dapm_kcontrol_add_path(w
->kcontrols
[i
], path
);
745 ret
= dapm_create_or_share_mixmux_kcontrol(w
, i
);
749 dapm_kcontrol_add_path(w
->kcontrols
[i
], path
);
756 /* create new dapm mux control */
757 static int dapm_new_mux(struct snd_soc_dapm_widget
*w
)
759 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
760 struct snd_soc_dapm_path
*path
;
763 if (w
->num_kcontrols
!= 1) {
765 "ASoC: mux %s has incorrect number of controls\n",
770 if (list_empty(&w
->sources
)) {
771 dev_err(dapm
->dev
, "ASoC: mux %s has no paths\n", w
->name
);
775 ret
= dapm_create_or_share_mixmux_kcontrol(w
, 0);
779 list_for_each_entry(path
, &w
->sources
, list_sink
)
780 dapm_kcontrol_add_path(w
->kcontrols
[0], path
);
785 /* create new dapm volume control */
786 static int dapm_new_pga(struct snd_soc_dapm_widget
*w
)
788 if (w
->num_kcontrols
)
789 dev_err(w
->dapm
->dev
,
790 "ASoC: PGA controls not supported: '%s'\n", w
->name
);
795 /* reset 'walked' bit for each dapm path */
796 static void dapm_clear_walk_output(struct snd_soc_dapm_context
*dapm
,
797 struct list_head
*sink
)
799 struct snd_soc_dapm_path
*p
;
801 list_for_each_entry(p
, sink
, list_source
) {
804 dapm_clear_walk_output(dapm
, &p
->sink
->sinks
);
809 static void dapm_clear_walk_input(struct snd_soc_dapm_context
*dapm
,
810 struct list_head
*source
)
812 struct snd_soc_dapm_path
*p
;
814 list_for_each_entry(p
, source
, list_sink
) {
817 dapm_clear_walk_input(dapm
, &p
->source
->sources
);
823 /* We implement power down on suspend by checking the power state of
824 * the ALSA card - when we are suspending the ALSA state for the card
827 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
829 int level
= snd_power_get_state(widget
->dapm
->card
->snd_card
);
832 case SNDRV_CTL_POWER_D3hot
:
833 case SNDRV_CTL_POWER_D3cold
:
834 if (widget
->ignore_suspend
)
835 dev_dbg(widget
->dapm
->dev
, "ASoC: %s ignoring suspend\n",
837 return widget
->ignore_suspend
;
843 /* add widget to list if it's not already in the list */
844 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list
**list
,
845 struct snd_soc_dapm_widget
*w
)
847 struct snd_soc_dapm_widget_list
*wlist
;
848 int wlistsize
, wlistentries
, i
;
855 /* is this widget already in the list */
856 for (i
= 0; i
< wlist
->num_widgets
; i
++) {
857 if (wlist
->widgets
[i
] == w
)
861 /* allocate some new space */
862 wlistentries
= wlist
->num_widgets
+ 1;
863 wlistsize
= sizeof(struct snd_soc_dapm_widget_list
) +
864 wlistentries
* sizeof(struct snd_soc_dapm_widget
*);
865 *list
= krealloc(wlist
, wlistsize
, GFP_KERNEL
);
867 dev_err(w
->dapm
->dev
, "ASoC: can't allocate widget list for %s\n",
873 /* insert the widget */
874 dev_dbg(w
->dapm
->dev
, "ASoC: added %s in widget list pos %d\n",
875 w
->name
, wlist
->num_widgets
);
877 wlist
->widgets
[wlist
->num_widgets
] = w
;
878 wlist
->num_widgets
++;
883 * Recursively check for a completed path to an active or physically connected
884 * output widget. Returns number of complete paths.
886 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
,
887 struct snd_soc_dapm_widget_list
**list
)
889 struct snd_soc_dapm_path
*path
;
892 if (widget
->outputs
>= 0)
893 return widget
->outputs
;
895 DAPM_UPDATE_STAT(widget
, path_checks
);
897 switch (widget
->id
) {
898 case snd_soc_dapm_supply
:
899 case snd_soc_dapm_regulator_supply
:
900 case snd_soc_dapm_clock_supply
:
901 case snd_soc_dapm_kcontrol
:
907 switch (widget
->id
) {
908 case snd_soc_dapm_adc
:
909 case snd_soc_dapm_aif_out
:
910 case snd_soc_dapm_dai_out
:
911 if (widget
->active
) {
912 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
913 return widget
->outputs
;
919 if (widget
->connected
) {
920 /* connected pin ? */
921 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
) {
922 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
923 return widget
->outputs
;
926 /* connected jack or spk ? */
927 if (widget
->id
== snd_soc_dapm_hp
||
928 widget
->id
== snd_soc_dapm_spk
||
929 (widget
->id
== snd_soc_dapm_line
&&
930 !list_empty(&widget
->sources
))) {
931 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
932 return widget
->outputs
;
936 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
937 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
948 trace_snd_soc_dapm_output_path(widget
, path
);
950 if (path
->sink
&& path
->connect
) {
954 /* do we need to add this widget to the list ? */
957 err
= dapm_list_add_widget(list
, path
->sink
);
959 dev_err(widget
->dapm
->dev
,
960 "ASoC: could not add widget %s\n",
967 con
+= is_connected_output_ep(path
->sink
, list
);
973 widget
->outputs
= con
;
979 * Recursively check for a completed path to an active or physically connected
980 * input widget. Returns number of complete paths.
982 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
,
983 struct snd_soc_dapm_widget_list
**list
)
985 struct snd_soc_dapm_path
*path
;
988 if (widget
->inputs
>= 0)
989 return widget
->inputs
;
991 DAPM_UPDATE_STAT(widget
, path_checks
);
993 switch (widget
->id
) {
994 case snd_soc_dapm_supply
:
995 case snd_soc_dapm_regulator_supply
:
996 case snd_soc_dapm_clock_supply
:
997 case snd_soc_dapm_kcontrol
:
1003 /* active stream ? */
1004 switch (widget
->id
) {
1005 case snd_soc_dapm_dac
:
1006 case snd_soc_dapm_aif_in
:
1007 case snd_soc_dapm_dai_in
:
1008 if (widget
->active
) {
1009 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
1010 return widget
->inputs
;
1016 if (widget
->connected
) {
1017 /* connected pin ? */
1018 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
) {
1019 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
1020 return widget
->inputs
;
1023 /* connected VMID/Bias for lower pops */
1024 if (widget
->id
== snd_soc_dapm_vmid
) {
1025 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
1026 return widget
->inputs
;
1029 /* connected jack ? */
1030 if (widget
->id
== snd_soc_dapm_mic
||
1031 (widget
->id
== snd_soc_dapm_line
&&
1032 !list_empty(&widget
->sinks
))) {
1033 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
1034 return widget
->inputs
;
1037 /* signal generator */
1038 if (widget
->id
== snd_soc_dapm_siggen
) {
1039 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
1040 return widget
->inputs
;
1044 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
1045 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
1056 trace_snd_soc_dapm_input_path(widget
, path
);
1058 if (path
->source
&& path
->connect
) {
1062 /* do we need to add this widget to the list ? */
1065 err
= dapm_list_add_widget(list
, path
->source
);
1067 dev_err(widget
->dapm
->dev
,
1068 "ASoC: could not add widget %s\n",
1075 con
+= is_connected_input_ep(path
->source
, list
);
1081 widget
->inputs
= con
;
1087 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1088 * @dai: the soc DAI.
1089 * @stream: stream direction.
1090 * @list: list of active widgets for this stream.
1092 * Queries DAPM graph as to whether an valid audio stream path exists for
1093 * the initial stream specified by name. This takes into account
1094 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1096 * Returns the number of valid paths or negative error.
1098 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai
*dai
, int stream
,
1099 struct snd_soc_dapm_widget_list
**list
)
1101 struct snd_soc_card
*card
= dai
->card
;
1104 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
1107 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1108 paths
= is_connected_output_ep(dai
->playback_widget
, list
);
1109 dapm_clear_walk_output(&card
->dapm
,
1110 &dai
->playback_widget
->sinks
);
1112 paths
= is_connected_input_ep(dai
->capture_widget
, list
);
1113 dapm_clear_walk_input(&card
->dapm
,
1114 &dai
->capture_widget
->sources
);
1117 trace_snd_soc_dapm_connected(paths
, stream
);
1118 mutex_unlock(&card
->dapm_mutex
);
1124 * Handler for generic register modifier widget.
1126 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
1127 struct snd_kcontrol
*kcontrol
, int event
)
1131 if (SND_SOC_DAPM_EVENT_ON(event
))
1136 soc_widget_update_bits_locked(w
, -(w
->reg
+ 1),
1137 w
->mask
<< w
->shift
, val
<< w
->shift
);
1141 EXPORT_SYMBOL_GPL(dapm_reg_event
);
1144 * Handler for regulator supply widget.
1146 int dapm_regulator_event(struct snd_soc_dapm_widget
*w
,
1147 struct snd_kcontrol
*kcontrol
, int event
)
1151 soc_dapm_async_complete(w
->dapm
);
1153 if (SND_SOC_DAPM_EVENT_ON(event
)) {
1154 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
1155 ret
= regulator_allow_bypass(w
->regulator
, false);
1157 dev_warn(w
->dapm
->dev
,
1158 "ASoC: Failed to unbypass %s: %d\n",
1162 return regulator_enable(w
->regulator
);
1164 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
1165 ret
= regulator_allow_bypass(w
->regulator
, true);
1167 dev_warn(w
->dapm
->dev
,
1168 "ASoC: Failed to bypass %s: %d\n",
1172 return regulator_disable_deferred(w
->regulator
, w
->shift
);
1175 EXPORT_SYMBOL_GPL(dapm_regulator_event
);
1178 * Handler for clock supply widget.
1180 int dapm_clock_event(struct snd_soc_dapm_widget
*w
,
1181 struct snd_kcontrol
*kcontrol
, int event
)
1186 soc_dapm_async_complete(w
->dapm
);
1188 #ifdef CONFIG_HAVE_CLK
1189 if (SND_SOC_DAPM_EVENT_ON(event
)) {
1190 return clk_prepare_enable(w
->clk
);
1192 clk_disable_unprepare(w
->clk
);
1198 EXPORT_SYMBOL_GPL(dapm_clock_event
);
1200 static int dapm_widget_power_check(struct snd_soc_dapm_widget
*w
)
1202 if (w
->power_checked
)
1203 return w
->new_power
;
1208 w
->new_power
= w
->power_check(w
);
1210 w
->power_checked
= true;
1212 return w
->new_power
;
1215 /* Generic check to see if a widget should be powered.
1217 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
1221 DAPM_UPDATE_STAT(w
, power_checks
);
1223 in
= is_connected_input_ep(w
, NULL
);
1224 dapm_clear_walk_input(w
->dapm
, &w
->sources
);
1225 out
= is_connected_output_ep(w
, NULL
);
1226 dapm_clear_walk_output(w
->dapm
, &w
->sinks
);
1227 return out
!= 0 && in
!= 0;
1230 /* Check to see if an ADC has power */
1231 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
1235 DAPM_UPDATE_STAT(w
, power_checks
);
1238 in
= is_connected_input_ep(w
, NULL
);
1239 dapm_clear_walk_input(w
->dapm
, &w
->sources
);
1242 return dapm_generic_check_power(w
);
1246 /* Check to see if a DAC has power */
1247 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
1251 DAPM_UPDATE_STAT(w
, power_checks
);
1254 out
= is_connected_output_ep(w
, NULL
);
1255 dapm_clear_walk_output(w
->dapm
, &w
->sinks
);
1258 return dapm_generic_check_power(w
);
1262 /* Check to see if a power supply is needed */
1263 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
1265 struct snd_soc_dapm_path
*path
;
1267 DAPM_UPDATE_STAT(w
, power_checks
);
1269 /* Check if one of our outputs is connected */
1270 list_for_each_entry(path
, &w
->sinks
, list_source
) {
1271 DAPM_UPDATE_STAT(w
, neighbour_checks
);
1276 if (path
->connected
&&
1277 !path
->connected(path
->source
, path
->sink
))
1283 if (dapm_widget_power_check(path
->sink
))
1290 static int dapm_always_on_check_power(struct snd_soc_dapm_widget
*w
)
1295 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
1296 struct snd_soc_dapm_widget
*b
,
1304 sort
= dapm_down_seq
;
1306 if (sort
[a
->id
] != sort
[b
->id
])
1307 return sort
[a
->id
] - sort
[b
->id
];
1308 if (a
->subseq
!= b
->subseq
) {
1310 return a
->subseq
- b
->subseq
;
1312 return b
->subseq
- a
->subseq
;
1314 if (a
->reg
!= b
->reg
)
1315 return a
->reg
- b
->reg
;
1316 if (a
->dapm
!= b
->dapm
)
1317 return (unsigned long)a
->dapm
- (unsigned long)b
->dapm
;
1322 /* Insert a widget in order into a DAPM power sequence. */
1323 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
1324 struct list_head
*list
,
1327 struct snd_soc_dapm_widget
*w
;
1329 list_for_each_entry(w
, list
, power_list
)
1330 if (dapm_seq_compare(new_widget
, w
, power_up
) < 0) {
1331 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
1335 list_add_tail(&new_widget
->power_list
, list
);
1338 static void dapm_seq_check_event(struct snd_soc_card
*card
,
1339 struct snd_soc_dapm_widget
*w
, int event
)
1341 const char *ev_name
;
1345 case SND_SOC_DAPM_PRE_PMU
:
1346 ev_name
= "PRE_PMU";
1349 case SND_SOC_DAPM_POST_PMU
:
1350 ev_name
= "POST_PMU";
1353 case SND_SOC_DAPM_PRE_PMD
:
1354 ev_name
= "PRE_PMD";
1357 case SND_SOC_DAPM_POST_PMD
:
1358 ev_name
= "POST_PMD";
1361 case SND_SOC_DAPM_WILL_PMU
:
1362 ev_name
= "WILL_PMU";
1365 case SND_SOC_DAPM_WILL_PMD
:
1366 ev_name
= "WILL_PMD";
1370 WARN(1, "Unknown event %d\n", event
);
1374 if (w
->new_power
!= power
)
1377 if (w
->event
&& (w
->event_flags
& event
)) {
1378 pop_dbg(w
->dapm
->dev
, card
->pop_time
, "pop test : %s %s\n",
1380 soc_dapm_async_complete(w
->dapm
);
1381 trace_snd_soc_dapm_widget_event_start(w
, event
);
1382 ret
= w
->event(w
, NULL
, event
);
1383 trace_snd_soc_dapm_widget_event_done(w
, event
);
1385 dev_err(w
->dapm
->dev
, "ASoC: %s: %s event failed: %d\n",
1386 ev_name
, w
->name
, ret
);
1390 /* Apply the coalesced changes from a DAPM sequence */
1391 static void dapm_seq_run_coalesced(struct snd_soc_card
*card
,
1392 struct list_head
*pending
)
1394 struct snd_soc_dapm_widget
*w
;
1396 unsigned int value
= 0;
1397 unsigned int mask
= 0;
1399 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
1402 list_for_each_entry(w
, pending
, power_list
) {
1403 WARN_ON(reg
!= w
->reg
);
1404 w
->power
= w
->new_power
;
1406 mask
|= w
->mask
<< w
->shift
;
1408 value
|= w
->on_val
<< w
->shift
;
1410 value
|= w
->off_val
<< w
->shift
;
1412 pop_dbg(w
->dapm
->dev
, card
->pop_time
,
1413 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1414 w
->name
, reg
, value
, mask
);
1416 /* Check for events */
1417 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_PRE_PMU
);
1418 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_PRE_PMD
);
1422 /* Any widget will do, they should all be updating the
1425 w
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
1428 pop_dbg(w
->dapm
->dev
, card
->pop_time
,
1429 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1430 value
, mask
, reg
, card
->pop_time
);
1431 pop_wait(card
->pop_time
);
1432 soc_widget_update_bits_locked(w
, reg
, mask
, value
);
1435 list_for_each_entry(w
, pending
, power_list
) {
1436 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_POST_PMU
);
1437 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_POST_PMD
);
1441 /* Apply a DAPM power sequence.
1443 * We walk over a pre-sorted list of widgets to apply power to. In
1444 * order to minimise the number of writes to the device required
1445 * multiple widgets will be updated in a single write where possible.
1446 * Currently anything that requires more than a single write is not
1449 static void dapm_seq_run(struct snd_soc_card
*card
,
1450 struct list_head
*list
, int event
, bool power_up
)
1452 struct snd_soc_dapm_widget
*w
, *n
;
1453 struct snd_soc_dapm_context
*d
;
1456 int cur_subseq
= -1;
1457 int cur_reg
= SND_SOC_NOPM
;
1458 struct snd_soc_dapm_context
*cur_dapm
= NULL
;
1465 sort
= dapm_down_seq
;
1467 list_for_each_entry_safe(w
, n
, list
, power_list
) {
1470 /* Do we need to apply any queued changes? */
1471 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
||
1472 w
->dapm
!= cur_dapm
|| w
->subseq
!= cur_subseq
) {
1473 if (!list_empty(&pending
))
1474 dapm_seq_run_coalesced(card
, &pending
);
1476 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1477 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1478 if (sort
[i
] == cur_sort
)
1479 cur_dapm
->seq_notifier(cur_dapm
,
1484 if (cur_dapm
&& w
->dapm
!= cur_dapm
)
1485 soc_dapm_async_complete(cur_dapm
);
1487 INIT_LIST_HEAD(&pending
);
1489 cur_subseq
= INT_MIN
;
1490 cur_reg
= SND_SOC_NOPM
;
1495 case snd_soc_dapm_pre
:
1497 list_for_each_entry_safe_continue(w
, n
, list
,
1500 if (event
== SND_SOC_DAPM_STREAM_START
)
1502 NULL
, SND_SOC_DAPM_PRE_PMU
);
1503 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1505 NULL
, SND_SOC_DAPM_PRE_PMD
);
1508 case snd_soc_dapm_post
:
1510 list_for_each_entry_safe_continue(w
, n
, list
,
1513 if (event
== SND_SOC_DAPM_STREAM_START
)
1515 NULL
, SND_SOC_DAPM_POST_PMU
);
1516 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1518 NULL
, SND_SOC_DAPM_POST_PMD
);
1522 /* Queue it up for application */
1523 cur_sort
= sort
[w
->id
];
1524 cur_subseq
= w
->subseq
;
1527 list_move(&w
->power_list
, &pending
);
1532 dev_err(w
->dapm
->dev
,
1533 "ASoC: Failed to apply widget power: %d\n", ret
);
1536 if (!list_empty(&pending
))
1537 dapm_seq_run_coalesced(card
, &pending
);
1539 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1540 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1541 if (sort
[i
] == cur_sort
)
1542 cur_dapm
->seq_notifier(cur_dapm
,
1546 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1547 soc_dapm_async_complete(d
);
1551 static void dapm_widget_update(struct snd_soc_card
*card
)
1553 struct snd_soc_dapm_update
*update
= card
->update
;
1554 struct snd_soc_dapm_widget_list
*wlist
;
1555 struct snd_soc_dapm_widget
*w
= NULL
;
1559 if (!update
|| !dapm_kcontrol_is_powered(update
->kcontrol
))
1562 wlist
= dapm_kcontrol_get_wlist(update
->kcontrol
);
1564 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
1565 w
= wlist
->widgets
[wi
];
1567 if (w
->event
&& (w
->event_flags
& SND_SOC_DAPM_PRE_REG
)) {
1568 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_PRE_REG
);
1570 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM pre-event failed: %d\n",
1578 ret
= soc_widget_update_bits_locked(w
, update
->reg
, update
->mask
,
1581 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM update failed: %d\n",
1584 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
1585 w
= wlist
->widgets
[wi
];
1587 if (w
->event
&& (w
->event_flags
& SND_SOC_DAPM_POST_REG
)) {
1588 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_POST_REG
);
1590 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM post-event failed: %d\n",
1596 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1597 * they're changing state.
1599 static void dapm_pre_sequence_async(void *data
, async_cookie_t cookie
)
1601 struct snd_soc_dapm_context
*d
= data
;
1604 /* If we're off and we're not supposed to be go into STANDBY */
1605 if (d
->bias_level
== SND_SOC_BIAS_OFF
&&
1606 d
->target_bias_level
!= SND_SOC_BIAS_OFF
) {
1608 pm_runtime_get_sync(d
->dev
);
1610 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1613 "ASoC: Failed to turn on bias: %d\n", ret
);
1616 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1617 if (d
->bias_level
!= d
->target_bias_level
) {
1618 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_PREPARE
);
1621 "ASoC: Failed to prepare bias: %d\n", ret
);
1625 /* Async callback run prior to DAPM sequences - brings to their final
1628 static void dapm_post_sequence_async(void *data
, async_cookie_t cookie
)
1630 struct snd_soc_dapm_context
*d
= data
;
1633 /* If we just powered the last thing off drop to standby bias */
1634 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1635 (d
->target_bias_level
== SND_SOC_BIAS_STANDBY
||
1636 d
->target_bias_level
== SND_SOC_BIAS_OFF
)) {
1637 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1639 dev_err(d
->dev
, "ASoC: Failed to apply standby bias: %d\n",
1643 /* If we're in standby and can support bias off then do that */
1644 if (d
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1645 d
->target_bias_level
== SND_SOC_BIAS_OFF
) {
1646 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_OFF
);
1648 dev_err(d
->dev
, "ASoC: Failed to turn off bias: %d\n",
1652 pm_runtime_put(d
->dev
);
1655 /* If we just powered up then move to active bias */
1656 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1657 d
->target_bias_level
== SND_SOC_BIAS_ON
) {
1658 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_ON
);
1660 dev_err(d
->dev
, "ASoC: Failed to apply active bias: %d\n",
1665 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget
*peer
,
1666 bool power
, bool connect
)
1668 /* If a connection is being made or broken then that update
1669 * will have marked the peer dirty, otherwise the widgets are
1670 * not connected and this update has no impact. */
1674 /* If the peer is already in the state we're moving to then we
1675 * won't have an impact on it. */
1676 if (power
!= peer
->power
)
1677 dapm_mark_dirty(peer
, "peer state change");
1680 static void dapm_widget_set_power(struct snd_soc_dapm_widget
*w
, bool power
,
1681 struct list_head
*up_list
,
1682 struct list_head
*down_list
)
1684 struct snd_soc_dapm_path
*path
;
1686 if (w
->power
== power
)
1689 trace_snd_soc_dapm_widget_power(w
, power
);
1691 /* If we changed our power state perhaps our neigbours changed
1694 list_for_each_entry(path
, &w
->sources
, list_sink
) {
1696 dapm_widget_set_peer_power(path
->source
, power
,
1701 case snd_soc_dapm_supply
:
1702 case snd_soc_dapm_regulator_supply
:
1703 case snd_soc_dapm_clock_supply
:
1704 case snd_soc_dapm_kcontrol
:
1705 /* Supplies can't affect their outputs, only their inputs */
1708 list_for_each_entry(path
, &w
->sinks
, list_source
) {
1710 dapm_widget_set_peer_power(path
->sink
, power
,
1718 dapm_seq_insert(w
, up_list
, true);
1720 dapm_seq_insert(w
, down_list
, false);
1723 static void dapm_power_one_widget(struct snd_soc_dapm_widget
*w
,
1724 struct list_head
*up_list
,
1725 struct list_head
*down_list
)
1730 case snd_soc_dapm_pre
:
1731 dapm_seq_insert(w
, down_list
, false);
1733 case snd_soc_dapm_post
:
1734 dapm_seq_insert(w
, up_list
, true);
1738 power
= dapm_widget_power_check(w
);
1740 dapm_widget_set_power(w
, power
, up_list
, down_list
);
1746 * Scan each dapm widget for complete audio path.
1747 * A complete path is a route that has valid endpoints i.e.:-
1749 * o DAC to output pin.
1750 * o Input Pin to ADC.
1751 * o Input pin to Output pin (bypass, sidetone)
1752 * o DAC to ADC (loopback).
1754 static int dapm_power_widgets(struct snd_soc_card
*card
, int event
)
1756 struct snd_soc_dapm_widget
*w
;
1757 struct snd_soc_dapm_context
*d
;
1759 LIST_HEAD(down_list
);
1760 ASYNC_DOMAIN_EXCLUSIVE(async_domain
);
1761 enum snd_soc_bias_level bias
;
1763 lockdep_assert_held(&card
->dapm_mutex
);
1765 trace_snd_soc_dapm_start(card
);
1767 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1768 if (d
->idle_bias_off
)
1769 d
->target_bias_level
= SND_SOC_BIAS_OFF
;
1771 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1776 /* Check which widgets we need to power and store them in
1777 * lists indicating if they should be powered up or down. We
1778 * only check widgets that have been flagged as dirty but note
1779 * that new widgets may be added to the dirty list while we
1782 list_for_each_entry(w
, &card
->dapm_dirty
, dirty
) {
1783 dapm_power_one_widget(w
, &up_list
, &down_list
);
1786 list_for_each_entry(w
, &card
->widgets
, list
) {
1788 case snd_soc_dapm_pre
:
1789 case snd_soc_dapm_post
:
1790 /* These widgets always need to be powered */
1793 list_del_init(&w
->dirty
);
1800 /* Supplies and micbiases only bring the
1801 * context up to STANDBY as unless something
1802 * else is active and passing audio they
1803 * generally don't require full power. Signal
1804 * generators are virtual pins and have no
1805 * power impact themselves.
1808 case snd_soc_dapm_siggen
:
1809 case snd_soc_dapm_vmid
:
1811 case snd_soc_dapm_supply
:
1812 case snd_soc_dapm_regulator_supply
:
1813 case snd_soc_dapm_clock_supply
:
1814 case snd_soc_dapm_micbias
:
1815 if (d
->target_bias_level
< SND_SOC_BIAS_STANDBY
)
1816 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1819 d
->target_bias_level
= SND_SOC_BIAS_ON
;
1826 /* Force all contexts in the card to the same bias state if
1827 * they're not ground referenced.
1829 bias
= SND_SOC_BIAS_OFF
;
1830 list_for_each_entry(d
, &card
->dapm_list
, list
)
1831 if (d
->target_bias_level
> bias
)
1832 bias
= d
->target_bias_level
;
1833 list_for_each_entry(d
, &card
->dapm_list
, list
)
1834 if (!d
->idle_bias_off
)
1835 d
->target_bias_level
= bias
;
1837 trace_snd_soc_dapm_walk_done(card
);
1839 /* Run card bias changes at first */
1840 dapm_pre_sequence_async(&card
->dapm
, 0);
1841 /* Run other bias changes in parallel */
1842 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1843 if (d
!= &card
->dapm
)
1844 async_schedule_domain(dapm_pre_sequence_async
, d
,
1847 async_synchronize_full_domain(&async_domain
);
1849 list_for_each_entry(w
, &down_list
, power_list
) {
1850 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_WILL_PMD
);
1853 list_for_each_entry(w
, &up_list
, power_list
) {
1854 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_WILL_PMU
);
1857 /* Power down widgets first; try to avoid amplifying pops. */
1858 dapm_seq_run(card
, &down_list
, event
, false);
1860 dapm_widget_update(card
);
1863 dapm_seq_run(card
, &up_list
, event
, true);
1865 /* Run all the bias changes in parallel */
1866 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1867 if (d
!= &card
->dapm
)
1868 async_schedule_domain(dapm_post_sequence_async
, d
,
1871 async_synchronize_full_domain(&async_domain
);
1872 /* Run card bias changes at last */
1873 dapm_post_sequence_async(&card
->dapm
, 0);
1875 /* do we need to notify any clients that DAPM event is complete */
1876 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1877 if (d
->stream_event
)
1878 d
->stream_event(d
, event
);
1881 pop_dbg(card
->dev
, card
->pop_time
,
1882 "DAPM sequencing finished, waiting %dms\n", card
->pop_time
);
1883 pop_wait(card
->pop_time
);
1885 trace_snd_soc_dapm_done(card
);
1890 #ifdef CONFIG_DEBUG_FS
1891 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1892 char __user
*user_buf
,
1893 size_t count
, loff_t
*ppos
)
1895 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1899 struct snd_soc_dapm_path
*p
= NULL
;
1901 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1905 in
= is_connected_input_ep(w
, NULL
);
1906 dapm_clear_walk_input(w
->dapm
, &w
->sources
);
1907 out
= is_connected_output_ep(w
, NULL
);
1908 dapm_clear_walk_output(w
->dapm
, &w
->sinks
);
1910 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s%s in %d out %d",
1911 w
->name
, w
->power
? "On" : "Off",
1912 w
->force
? " (forced)" : "", in
, out
);
1915 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1916 " - R%d(0x%x) mask 0x%x",
1917 w
->reg
, w
->reg
, w
->mask
<< w
->shift
);
1919 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
1922 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1924 w
->active
? "active" : "inactive");
1926 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1927 if (p
->connected
&& !p
->connected(w
, p
->source
))
1931 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1932 " in \"%s\" \"%s\"\n",
1933 p
->name
? p
->name
: "static",
1936 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1937 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1941 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1942 " out \"%s\" \"%s\"\n",
1943 p
->name
? p
->name
: "static",
1947 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1953 static const struct file_operations dapm_widget_power_fops
= {
1954 .open
= simple_open
,
1955 .read
= dapm_widget_power_read_file
,
1956 .llseek
= default_llseek
,
1959 static ssize_t
dapm_bias_read_file(struct file
*file
, char __user
*user_buf
,
1960 size_t count
, loff_t
*ppos
)
1962 struct snd_soc_dapm_context
*dapm
= file
->private_data
;
1965 switch (dapm
->bias_level
) {
1966 case SND_SOC_BIAS_ON
:
1969 case SND_SOC_BIAS_PREPARE
:
1970 level
= "Prepare\n";
1972 case SND_SOC_BIAS_STANDBY
:
1973 level
= "Standby\n";
1975 case SND_SOC_BIAS_OFF
:
1979 WARN(1, "Unknown bias_level %d\n", dapm
->bias_level
);
1980 level
= "Unknown\n";
1984 return simple_read_from_buffer(user_buf
, count
, ppos
, level
,
1988 static const struct file_operations dapm_bias_fops
= {
1989 .open
= simple_open
,
1990 .read
= dapm_bias_read_file
,
1991 .llseek
= default_llseek
,
1994 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
1995 struct dentry
*parent
)
1999 dapm
->debugfs_dapm
= debugfs_create_dir("dapm", parent
);
2001 if (!dapm
->debugfs_dapm
) {
2003 "ASoC: Failed to create DAPM debugfs directory\n");
2007 d
= debugfs_create_file("bias_level", 0444,
2008 dapm
->debugfs_dapm
, dapm
,
2012 "ASoC: Failed to create bias level debugfs file\n");
2015 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
2017 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
2020 if (!dapm
->debugfs_dapm
|| !w
->name
)
2023 d
= debugfs_create_file(w
->name
, 0444,
2024 dapm
->debugfs_dapm
, w
,
2025 &dapm_widget_power_fops
);
2027 dev_warn(w
->dapm
->dev
,
2028 "ASoC: Failed to create %s debugfs file\n",
2032 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
2034 debugfs_remove_recursive(dapm
->debugfs_dapm
);
2038 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
2039 struct dentry
*parent
)
2043 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
2047 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
2053 /* test and update the power status of a mux widget */
2054 static int soc_dapm_mux_update_power(struct snd_soc_card
*card
,
2055 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
)
2057 struct snd_soc_dapm_path
*path
;
2060 lockdep_assert_held(&card
->dapm_mutex
);
2062 /* find dapm widget path assoc with kcontrol */
2063 dapm_kcontrol_for_each_path(path
, kcontrol
) {
2064 if (!path
->name
|| !e
->texts
[mux
])
2068 /* we now need to match the string in the enum to the path */
2069 if (!(strcmp(path
->name
, e
->texts
[mux
]))) {
2070 path
->connect
= 1; /* new connection */
2071 dapm_mark_dirty(path
->source
, "mux connection");
2074 dapm_mark_dirty(path
->source
,
2075 "mux disconnection");
2076 path
->connect
= 0; /* old connection must be powered down */
2078 dapm_mark_dirty(path
->sink
, "mux change");
2082 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2087 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context
*dapm
,
2088 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
,
2089 struct snd_soc_dapm_update
*update
)
2091 struct snd_soc_card
*card
= dapm
->card
;
2094 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2095 card
->update
= update
;
2096 ret
= soc_dapm_mux_update_power(card
, kcontrol
, mux
, e
);
2097 card
->update
= NULL
;
2098 mutex_unlock(&card
->dapm_mutex
);
2100 soc_dpcm_runtime_update(card
);
2103 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power
);
2105 /* test and update the power status of a mixer or switch widget */
2106 static int soc_dapm_mixer_update_power(struct snd_soc_card
*card
,
2107 struct snd_kcontrol
*kcontrol
, int connect
)
2109 struct snd_soc_dapm_path
*path
;
2112 lockdep_assert_held(&card
->dapm_mutex
);
2114 /* find dapm widget path assoc with kcontrol */
2115 dapm_kcontrol_for_each_path(path
, kcontrol
) {
2117 path
->connect
= connect
;
2118 dapm_mark_dirty(path
->source
, "mixer connection");
2119 dapm_mark_dirty(path
->sink
, "mixer update");
2123 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2128 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context
*dapm
,
2129 struct snd_kcontrol
*kcontrol
, int connect
,
2130 struct snd_soc_dapm_update
*update
)
2132 struct snd_soc_card
*card
= dapm
->card
;
2135 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2136 card
->update
= update
;
2137 ret
= soc_dapm_mixer_update_power(card
, kcontrol
, connect
);
2138 card
->update
= NULL
;
2139 mutex_unlock(&card
->dapm_mutex
);
2141 soc_dpcm_runtime_update(card
);
2144 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power
);
2146 /* show dapm widget status in sys fs */
2147 static ssize_t
dapm_widget_show(struct device
*dev
,
2148 struct device_attribute
*attr
, char *buf
)
2150 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
2151 struct snd_soc_codec
*codec
=rtd
->codec
;
2152 struct snd_soc_dapm_widget
*w
;
2154 char *state
= "not set";
2156 list_for_each_entry(w
, &codec
->card
->widgets
, list
) {
2157 if (w
->dapm
!= &codec
->dapm
)
2160 /* only display widgets that burnm power */
2162 case snd_soc_dapm_hp
:
2163 case snd_soc_dapm_mic
:
2164 case snd_soc_dapm_spk
:
2165 case snd_soc_dapm_line
:
2166 case snd_soc_dapm_micbias
:
2167 case snd_soc_dapm_dac
:
2168 case snd_soc_dapm_adc
:
2169 case snd_soc_dapm_pga
:
2170 case snd_soc_dapm_out_drv
:
2171 case snd_soc_dapm_mixer
:
2172 case snd_soc_dapm_mixer_named_ctl
:
2173 case snd_soc_dapm_supply
:
2174 case snd_soc_dapm_regulator_supply
:
2175 case snd_soc_dapm_clock_supply
:
2177 count
+= sprintf(buf
+ count
, "%s: %s\n",
2178 w
->name
, w
->power
? "On":"Off");
2185 switch (codec
->dapm
.bias_level
) {
2186 case SND_SOC_BIAS_ON
:
2189 case SND_SOC_BIAS_PREPARE
:
2192 case SND_SOC_BIAS_STANDBY
:
2195 case SND_SOC_BIAS_OFF
:
2199 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
2204 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
2206 int snd_soc_dapm_sys_add(struct device
*dev
)
2208 return device_create_file(dev
, &dev_attr_dapm_widget
);
2211 static void snd_soc_dapm_sys_remove(struct device
*dev
)
2213 device_remove_file(dev
, &dev_attr_dapm_widget
);
2216 static void dapm_free_path(struct snd_soc_dapm_path
*path
)
2218 list_del(&path
->list_sink
);
2219 list_del(&path
->list_source
);
2220 list_del(&path
->list_kcontrol
);
2221 list_del(&path
->list
);
2225 /* free all dapm widgets and resources */
2226 static void dapm_free_widgets(struct snd_soc_dapm_context
*dapm
)
2228 struct snd_soc_dapm_widget
*w
, *next_w
;
2229 struct snd_soc_dapm_path
*p
, *next_p
;
2231 list_for_each_entry_safe(w
, next_w
, &dapm
->card
->widgets
, list
) {
2232 if (w
->dapm
!= dapm
)
2236 * remove source and sink paths associated to this widget.
2237 * While removing the path, remove reference to it from both
2238 * source and sink widgets so that path is removed only once.
2240 list_for_each_entry_safe(p
, next_p
, &w
->sources
, list_sink
)
2243 list_for_each_entry_safe(p
, next_p
, &w
->sinks
, list_source
)
2246 kfree(w
->kcontrols
);
2252 static struct snd_soc_dapm_widget
*dapm_find_widget(
2253 struct snd_soc_dapm_context
*dapm
, const char *pin
,
2254 bool search_other_contexts
)
2256 struct snd_soc_dapm_widget
*w
;
2257 struct snd_soc_dapm_widget
*fallback
= NULL
;
2259 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
2260 if (!strcmp(w
->name
, pin
)) {
2261 if (w
->dapm
== dapm
)
2268 if (search_other_contexts
)
2274 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context
*dapm
,
2275 const char *pin
, int status
)
2277 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
2279 dapm_assert_locked(dapm
);
2282 dev_err(dapm
->dev
, "ASoC: DAPM unknown pin %s\n", pin
);
2286 if (w
->connected
!= status
)
2287 dapm_mark_dirty(w
, "pin configuration");
2289 w
->connected
= status
;
2297 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2298 * @dapm: DAPM context
2300 * Walks all dapm audio paths and powers widgets according to their
2301 * stream or path usage.
2303 * Requires external locking.
2305 * Returns 0 for success.
2307 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context
*dapm
)
2310 * Suppress early reports (eg, jacks syncing their state) to avoid
2311 * silly DAPM runs during card startup.
2313 if (!dapm
->card
|| !dapm
->card
->instantiated
)
2316 return dapm_power_widgets(dapm
->card
, SND_SOC_DAPM_STREAM_NOP
);
2318 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked
);
2321 * snd_soc_dapm_sync - scan and power dapm paths
2322 * @dapm: DAPM context
2324 * Walks all dapm audio paths and powers widgets according to their
2325 * stream or path usage.
2327 * Returns 0 for success.
2329 int snd_soc_dapm_sync(struct snd_soc_dapm_context
*dapm
)
2333 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2334 ret
= snd_soc_dapm_sync_unlocked(dapm
);
2335 mutex_unlock(&dapm
->card
->dapm_mutex
);
2338 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
2340 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context
*dapm
,
2341 struct snd_soc_dapm_widget
*wsource
, struct snd_soc_dapm_widget
*wsink
,
2342 const char *control
,
2343 int (*connected
)(struct snd_soc_dapm_widget
*source
,
2344 struct snd_soc_dapm_widget
*sink
))
2346 struct snd_soc_dapm_path
*path
;
2349 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
2353 path
->source
= wsource
;
2355 path
->connected
= connected
;
2356 INIT_LIST_HEAD(&path
->list
);
2357 INIT_LIST_HEAD(&path
->list_kcontrol
);
2358 INIT_LIST_HEAD(&path
->list_source
);
2359 INIT_LIST_HEAD(&path
->list_sink
);
2361 /* check for external widgets */
2362 if (wsink
->id
== snd_soc_dapm_input
) {
2363 if (wsource
->id
== snd_soc_dapm_micbias
||
2364 wsource
->id
== snd_soc_dapm_mic
||
2365 wsource
->id
== snd_soc_dapm_line
||
2366 wsource
->id
== snd_soc_dapm_output
)
2369 if (wsource
->id
== snd_soc_dapm_output
) {
2370 if (wsink
->id
== snd_soc_dapm_spk
||
2371 wsink
->id
== snd_soc_dapm_hp
||
2372 wsink
->id
== snd_soc_dapm_line
||
2373 wsink
->id
== snd_soc_dapm_input
)
2377 dapm_mark_dirty(wsource
, "Route added");
2378 dapm_mark_dirty(wsink
, "Route added");
2380 /* connect static paths */
2381 if (control
== NULL
) {
2382 list_add(&path
->list
, &dapm
->card
->paths
);
2383 list_add(&path
->list_sink
, &wsink
->sources
);
2384 list_add(&path
->list_source
, &wsource
->sinks
);
2389 /* connect dynamic paths */
2390 switch (wsink
->id
) {
2391 case snd_soc_dapm_adc
:
2392 case snd_soc_dapm_dac
:
2393 case snd_soc_dapm_pga
:
2394 case snd_soc_dapm_out_drv
:
2395 case snd_soc_dapm_input
:
2396 case snd_soc_dapm_output
:
2397 case snd_soc_dapm_siggen
:
2398 case snd_soc_dapm_micbias
:
2399 case snd_soc_dapm_vmid
:
2400 case snd_soc_dapm_pre
:
2401 case snd_soc_dapm_post
:
2402 case snd_soc_dapm_supply
:
2403 case snd_soc_dapm_regulator_supply
:
2404 case snd_soc_dapm_clock_supply
:
2405 case snd_soc_dapm_aif_in
:
2406 case snd_soc_dapm_aif_out
:
2407 case snd_soc_dapm_dai_in
:
2408 case snd_soc_dapm_dai_out
:
2409 case snd_soc_dapm_dai_link
:
2410 case snd_soc_dapm_kcontrol
:
2411 list_add(&path
->list
, &dapm
->card
->paths
);
2412 list_add(&path
->list_sink
, &wsink
->sources
);
2413 list_add(&path
->list_source
, &wsource
->sinks
);
2416 case snd_soc_dapm_mux
:
2417 ret
= dapm_connect_mux(dapm
, wsource
, wsink
, path
, control
,
2418 &wsink
->kcontrol_news
[0]);
2422 case snd_soc_dapm_switch
:
2423 case snd_soc_dapm_mixer
:
2424 case snd_soc_dapm_mixer_named_ctl
:
2425 ret
= dapm_connect_mixer(dapm
, wsource
, wsink
, path
, control
);
2429 case snd_soc_dapm_hp
:
2430 case snd_soc_dapm_mic
:
2431 case snd_soc_dapm_line
:
2432 case snd_soc_dapm_spk
:
2433 list_add(&path
->list
, &dapm
->card
->paths
);
2434 list_add(&path
->list_sink
, &wsink
->sources
);
2435 list_add(&path
->list_source
, &wsource
->sinks
);
2446 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context
*dapm
,
2447 const struct snd_soc_dapm_route
*route
,
2448 unsigned int is_prefixed
)
2450 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
2451 struct snd_soc_dapm_widget
*wtsource
= NULL
, *wtsink
= NULL
;
2454 char prefixed_sink
[80];
2455 char prefixed_source
[80];
2458 if (dapm
->codec
&& dapm
->codec
->name_prefix
&& !is_prefixed
) {
2459 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
2460 dapm
->codec
->name_prefix
, route
->sink
);
2461 sink
= prefixed_sink
;
2462 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
2463 dapm
->codec
->name_prefix
, route
->source
);
2464 source
= prefixed_source
;
2467 source
= route
->source
;
2471 * find src and dest widgets over all widgets but favor a widget from
2472 * current DAPM context
2474 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
2475 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
2477 if (w
->dapm
== dapm
)
2481 if (!wsource
&& !(strcmp(w
->name
, source
))) {
2483 if (w
->dapm
== dapm
)
2487 /* use widget from another DAPM context if not found from this */
2493 if (wsource
== NULL
) {
2494 dev_err(dapm
->dev
, "ASoC: no source widget found for %s\n",
2498 if (wsink
== NULL
) {
2499 dev_err(dapm
->dev
, "ASoC: no sink widget found for %s\n",
2504 ret
= snd_soc_dapm_add_path(dapm
, wsource
, wsink
, route
->control
,
2511 dev_warn(dapm
->dev
, "ASoC: no dapm match for %s --> %s --> %s\n",
2512 source
, route
->control
, sink
);
2516 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context
*dapm
,
2517 const struct snd_soc_dapm_route
*route
)
2519 struct snd_soc_dapm_path
*path
, *p
;
2522 char prefixed_sink
[80];
2523 char prefixed_source
[80];
2525 if (route
->control
) {
2527 "ASoC: Removal of routes with controls not supported\n");
2531 if (dapm
->codec
&& dapm
->codec
->name_prefix
) {
2532 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
2533 dapm
->codec
->name_prefix
, route
->sink
);
2534 sink
= prefixed_sink
;
2535 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
2536 dapm
->codec
->name_prefix
, route
->source
);
2537 source
= prefixed_source
;
2540 source
= route
->source
;
2544 list_for_each_entry(p
, &dapm
->card
->paths
, list
) {
2545 if (strcmp(p
->source
->name
, source
) != 0)
2547 if (strcmp(p
->sink
->name
, sink
) != 0)
2554 dapm_mark_dirty(path
->source
, "Route removed");
2555 dapm_mark_dirty(path
->sink
, "Route removed");
2557 dapm_free_path(path
);
2559 dev_warn(dapm
->dev
, "ASoC: Route %s->%s does not exist\n",
2567 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2568 * @dapm: DAPM context
2569 * @route: audio routes
2570 * @num: number of routes
2572 * Connects 2 dapm widgets together via a named audio path. The sink is
2573 * the widget receiving the audio signal, whilst the source is the sender
2574 * of the audio signal.
2576 * Returns 0 for success else error. On error all resources can be freed
2577 * with a call to snd_soc_card_free().
2579 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context
*dapm
,
2580 const struct snd_soc_dapm_route
*route
, int num
)
2584 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2585 for (i
= 0; i
< num
; i
++) {
2586 r
= snd_soc_dapm_add_route(dapm
, route
, false);
2588 dev_err(dapm
->dev
, "ASoC: Failed to add route %s -> %s -> %s\n",
2590 route
->control
? route
->control
: "direct",
2596 mutex_unlock(&dapm
->card
->dapm_mutex
);
2600 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
2603 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2604 * @dapm: DAPM context
2605 * @route: audio routes
2606 * @num: number of routes
2608 * Removes routes from the DAPM context.
2610 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context
*dapm
,
2611 const struct snd_soc_dapm_route
*route
, int num
)
2615 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2616 for (i
= 0; i
< num
; i
++) {
2617 snd_soc_dapm_del_route(dapm
, route
);
2620 mutex_unlock(&dapm
->card
->dapm_mutex
);
2624 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes
);
2626 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context
*dapm
,
2627 const struct snd_soc_dapm_route
*route
)
2629 struct snd_soc_dapm_widget
*source
= dapm_find_widget(dapm
,
2632 struct snd_soc_dapm_widget
*sink
= dapm_find_widget(dapm
,
2635 struct snd_soc_dapm_path
*path
;
2639 dev_err(dapm
->dev
, "ASoC: Unable to find source %s for weak route\n",
2645 dev_err(dapm
->dev
, "ASoC: Unable to find sink %s for weak route\n",
2650 if (route
->control
|| route
->connected
)
2651 dev_warn(dapm
->dev
, "ASoC: Ignoring control for weak route %s->%s\n",
2652 route
->source
, route
->sink
);
2654 list_for_each_entry(path
, &source
->sinks
, list_source
) {
2655 if (path
->sink
== sink
) {
2662 dev_err(dapm
->dev
, "ASoC: No path found for weak route %s->%s\n",
2663 route
->source
, route
->sink
);
2665 dev_warn(dapm
->dev
, "ASoC: %d paths found for weak route %s->%s\n",
2666 count
, route
->source
, route
->sink
);
2672 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2673 * @dapm: DAPM context
2674 * @route: audio routes
2675 * @num: number of routes
2677 * Mark existing routes matching those specified in the passed array
2678 * as being weak, meaning that they are ignored for the purpose of
2679 * power decisions. The main intended use case is for sidetone paths
2680 * which couple audio between other independent paths if they are both
2681 * active in order to make the combination work better at the user
2682 * level but which aren't intended to be "used".
2684 * Note that CODEC drivers should not use this as sidetone type paths
2685 * can frequently also be used as bypass paths.
2687 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context
*dapm
,
2688 const struct snd_soc_dapm_route
*route
, int num
)
2693 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2694 for (i
= 0; i
< num
; i
++) {
2695 err
= snd_soc_dapm_weak_route(dapm
, route
);
2700 mutex_unlock(&dapm
->card
->dapm_mutex
);
2704 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes
);
2707 * snd_soc_dapm_new_widgets - add new dapm widgets
2708 * @dapm: DAPM context
2710 * Checks the codec for any new dapm widgets and creates them if found.
2712 * Returns 0 for success.
2714 int snd_soc_dapm_new_widgets(struct snd_soc_card
*card
)
2716 struct snd_soc_dapm_widget
*w
;
2719 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2721 list_for_each_entry(w
, &card
->widgets
, list
)
2726 if (w
->num_kcontrols
) {
2727 w
->kcontrols
= kzalloc(w
->num_kcontrols
*
2728 sizeof(struct snd_kcontrol
*),
2730 if (!w
->kcontrols
) {
2731 mutex_unlock(&card
->dapm_mutex
);
2737 case snd_soc_dapm_switch
:
2738 case snd_soc_dapm_mixer
:
2739 case snd_soc_dapm_mixer_named_ctl
:
2742 case snd_soc_dapm_mux
:
2745 case snd_soc_dapm_pga
:
2746 case snd_soc_dapm_out_drv
:
2753 /* Read the initial power state from the device */
2755 soc_widget_read(w
, w
->reg
, &val
);
2756 val
= val
>> w
->shift
;
2758 if (val
== w
->on_val
)
2764 dapm_mark_dirty(w
, "new widget");
2765 dapm_debugfs_add_widget(w
);
2768 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2769 mutex_unlock(&card
->dapm_mutex
);
2772 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
2775 * snd_soc_dapm_get_volsw - dapm mixer get callback
2776 * @kcontrol: mixer control
2777 * @ucontrol: control element information
2779 * Callback to get the value of a dapm mixer control.
2781 * Returns 0 for success.
2783 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
2784 struct snd_ctl_elem_value
*ucontrol
)
2786 struct snd_soc_codec
*codec
= snd_soc_dapm_kcontrol_codec(kcontrol
);
2787 struct snd_soc_card
*card
= codec
->card
;
2788 struct soc_mixer_control
*mc
=
2789 (struct soc_mixer_control
*)kcontrol
->private_value
;
2791 unsigned int shift
= mc
->shift
;
2793 unsigned int mask
= (1 << fls(max
)) - 1;
2794 unsigned int invert
= mc
->invert
;
2797 if (snd_soc_volsw_is_stereo(mc
))
2798 dev_warn(codec
->dapm
.dev
,
2799 "ASoC: Control '%s' is stereo, which is not supported\n",
2802 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2803 if (dapm_kcontrol_is_powered(kcontrol
) && reg
!= SND_SOC_NOPM
)
2804 val
= (snd_soc_read(codec
, reg
) >> shift
) & mask
;
2806 val
= dapm_kcontrol_get_value(kcontrol
);
2807 mutex_unlock(&card
->dapm_mutex
);
2810 ucontrol
->value
.integer
.value
[0] = max
- val
;
2812 ucontrol
->value
.integer
.value
[0] = val
;
2816 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
2819 * snd_soc_dapm_put_volsw - dapm mixer set callback
2820 * @kcontrol: mixer control
2821 * @ucontrol: control element information
2823 * Callback to set the value of a dapm mixer control.
2825 * Returns 0 for success.
2827 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
2828 struct snd_ctl_elem_value
*ucontrol
)
2830 struct snd_soc_codec
*codec
= snd_soc_dapm_kcontrol_codec(kcontrol
);
2831 struct snd_soc_card
*card
= codec
->card
;
2832 struct soc_mixer_control
*mc
=
2833 (struct soc_mixer_control
*)kcontrol
->private_value
;
2835 unsigned int shift
= mc
->shift
;
2837 unsigned int mask
= (1 << fls(max
)) - 1;
2838 unsigned int invert
= mc
->invert
;
2840 int connect
, change
;
2841 struct snd_soc_dapm_update update
;
2844 if (snd_soc_volsw_is_stereo(mc
))
2845 dev_warn(codec
->dapm
.dev
,
2846 "ASoC: Control '%s' is stereo, which is not supported\n",
2849 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2855 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2857 change
= dapm_kcontrol_set_value(kcontrol
, val
);
2859 if (reg
!= SND_SOC_NOPM
) {
2860 mask
= mask
<< shift
;
2863 change
= snd_soc_test_bits(codec
, reg
, mask
, val
);
2867 if (reg
!= SND_SOC_NOPM
) {
2868 update
.kcontrol
= kcontrol
;
2873 card
->update
= &update
;
2876 ret
= soc_dapm_mixer_update_power(card
, kcontrol
, connect
);
2878 card
->update
= NULL
;
2881 mutex_unlock(&card
->dapm_mutex
);
2884 soc_dpcm_runtime_update(card
);
2888 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
2891 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2892 * @kcontrol: mixer control
2893 * @ucontrol: control element information
2895 * Callback to get the value of a dapm enumerated double mixer control.
2897 * Returns 0 for success.
2899 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
2900 struct snd_ctl_elem_value
*ucontrol
)
2902 struct snd_soc_codec
*codec
= snd_soc_dapm_kcontrol_codec(kcontrol
);
2903 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2904 unsigned int reg_val
, val
;
2906 if (e
->reg
!= SND_SOC_NOPM
)
2907 reg_val
= snd_soc_read(codec
, e
->reg
);
2909 reg_val
= dapm_kcontrol_get_value(kcontrol
);
2911 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
2912 ucontrol
->value
.enumerated
.item
[0] = snd_soc_enum_val_to_item(e
, val
);
2913 if (e
->shift_l
!= e
->shift_r
) {
2914 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
2915 val
= snd_soc_enum_val_to_item(e
, val
);
2916 ucontrol
->value
.enumerated
.item
[1] = val
;
2921 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
2924 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2925 * @kcontrol: mixer control
2926 * @ucontrol: control element information
2928 * Callback to set the value of a dapm enumerated double mixer control.
2930 * Returns 0 for success.
2932 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
2933 struct snd_ctl_elem_value
*ucontrol
)
2935 struct snd_soc_codec
*codec
= snd_soc_dapm_kcontrol_codec(kcontrol
);
2936 struct snd_soc_card
*card
= codec
->card
;
2937 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2938 unsigned int *item
= ucontrol
->value
.enumerated
.item
;
2939 unsigned int val
, change
;
2941 struct snd_soc_dapm_update update
;
2944 if (item
[0] >= e
->items
)
2947 val
= snd_soc_enum_item_to_val(e
, item
[0]) << e
->shift_l
;
2948 mask
= e
->mask
<< e
->shift_l
;
2949 if (e
->shift_l
!= e
->shift_r
) {
2950 if (item
[1] > e
->items
)
2952 val
|= snd_soc_enum_item_to_val(e
, item
[1]) << e
->shift_l
;
2953 mask
|= e
->mask
<< e
->shift_r
;
2956 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2958 if (e
->reg
!= SND_SOC_NOPM
)
2959 change
= snd_soc_test_bits(codec
, e
->reg
, mask
, val
);
2961 change
= dapm_kcontrol_set_value(kcontrol
, val
);
2964 if (e
->reg
!= SND_SOC_NOPM
) {
2965 update
.kcontrol
= kcontrol
;
2966 update
.reg
= e
->reg
;
2969 card
->update
= &update
;
2972 ret
= soc_dapm_mux_update_power(card
, kcontrol
, item
[0], e
);
2974 card
->update
= NULL
;
2977 mutex_unlock(&card
->dapm_mutex
);
2980 soc_dpcm_runtime_update(card
);
2984 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
2987 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2989 * @kcontrol: mixer control
2990 * @uinfo: control element information
2992 * Callback to provide information about a pin switch control.
2994 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
2995 struct snd_ctl_elem_info
*uinfo
)
2997 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2999 uinfo
->value
.integer
.min
= 0;
3000 uinfo
->value
.integer
.max
= 1;
3004 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
3007 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3009 * @kcontrol: mixer control
3012 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
3013 struct snd_ctl_elem_value
*ucontrol
)
3015 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
3016 const char *pin
= (const char *)kcontrol
->private_value
;
3018 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3020 ucontrol
->value
.integer
.value
[0] =
3021 snd_soc_dapm_get_pin_status(&card
->dapm
, pin
);
3023 mutex_unlock(&card
->dapm_mutex
);
3027 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
3030 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3032 * @kcontrol: mixer control
3035 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
3036 struct snd_ctl_elem_value
*ucontrol
)
3038 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
3039 const char *pin
= (const char *)kcontrol
->private_value
;
3041 if (ucontrol
->value
.integer
.value
[0])
3042 snd_soc_dapm_enable_pin(&card
->dapm
, pin
);
3044 snd_soc_dapm_disable_pin(&card
->dapm
, pin
);
3046 snd_soc_dapm_sync(&card
->dapm
);
3049 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
3051 static struct snd_soc_dapm_widget
*
3052 snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
3053 const struct snd_soc_dapm_widget
*widget
)
3055 struct snd_soc_dapm_widget
*w
;
3058 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
3062 case snd_soc_dapm_regulator_supply
:
3063 w
->regulator
= devm_regulator_get(dapm
->dev
, w
->name
);
3064 if (IS_ERR(w
->regulator
)) {
3065 ret
= PTR_ERR(w
->regulator
);
3066 dev_err(dapm
->dev
, "ASoC: Failed to request %s: %d\n",
3071 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
3072 ret
= regulator_allow_bypass(w
->regulator
, true);
3074 dev_warn(w
->dapm
->dev
,
3075 "ASoC: Failed to bypass %s: %d\n",
3079 case snd_soc_dapm_clock_supply
:
3080 #ifdef CONFIG_CLKDEV_LOOKUP
3081 w
->clk
= devm_clk_get(dapm
->dev
, w
->name
);
3082 if (IS_ERR(w
->clk
)) {
3083 ret
= PTR_ERR(w
->clk
);
3084 dev_err(dapm
->dev
, "ASoC: Failed to request %s: %d\n",
3096 if (dapm
->codec
&& dapm
->codec
->name_prefix
)
3097 w
->name
= kasprintf(GFP_KERNEL
, "%s %s",
3098 dapm
->codec
->name_prefix
, widget
->name
);
3100 w
->name
= kasprintf(GFP_KERNEL
, "%s", widget
->name
);
3102 if (w
->name
== NULL
) {
3108 case snd_soc_dapm_switch
:
3109 case snd_soc_dapm_mixer
:
3110 case snd_soc_dapm_mixer_named_ctl
:
3111 w
->power_check
= dapm_generic_check_power
;
3113 case snd_soc_dapm_mux
:
3114 w
->power_check
= dapm_generic_check_power
;
3116 case snd_soc_dapm_dai_out
:
3117 w
->power_check
= dapm_adc_check_power
;
3119 case snd_soc_dapm_dai_in
:
3120 w
->power_check
= dapm_dac_check_power
;
3122 case snd_soc_dapm_adc
:
3123 case snd_soc_dapm_aif_out
:
3124 case snd_soc_dapm_dac
:
3125 case snd_soc_dapm_aif_in
:
3126 case snd_soc_dapm_pga
:
3127 case snd_soc_dapm_out_drv
:
3128 case snd_soc_dapm_input
:
3129 case snd_soc_dapm_output
:
3130 case snd_soc_dapm_micbias
:
3131 case snd_soc_dapm_spk
:
3132 case snd_soc_dapm_hp
:
3133 case snd_soc_dapm_mic
:
3134 case snd_soc_dapm_line
:
3135 case snd_soc_dapm_dai_link
:
3136 w
->power_check
= dapm_generic_check_power
;
3138 case snd_soc_dapm_supply
:
3139 case snd_soc_dapm_regulator_supply
:
3140 case snd_soc_dapm_clock_supply
:
3141 case snd_soc_dapm_kcontrol
:
3142 w
->power_check
= dapm_supply_check_power
;
3145 w
->power_check
= dapm_always_on_check_power
;
3150 w
->codec
= dapm
->codec
;
3151 w
->platform
= dapm
->platform
;
3152 INIT_LIST_HEAD(&w
->sources
);
3153 INIT_LIST_HEAD(&w
->sinks
);
3154 INIT_LIST_HEAD(&w
->list
);
3155 INIT_LIST_HEAD(&w
->dirty
);
3156 list_add(&w
->list
, &dapm
->card
->widgets
);
3158 /* machine layer set ups unconnected pins and insertions */
3164 * snd_soc_dapm_new_controls - create new dapm controls
3165 * @dapm: DAPM context
3166 * @widget: widget array
3167 * @num: number of widgets
3169 * Creates new DAPM controls based upon the templates.
3171 * Returns 0 for success else error.
3173 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context
*dapm
,
3174 const struct snd_soc_dapm_widget
*widget
,
3177 struct snd_soc_dapm_widget
*w
;
3181 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
3182 for (i
= 0; i
< num
; i
++) {
3183 w
= snd_soc_dapm_new_control(dapm
, widget
);
3186 "ASoC: Failed to create DAPM control %s\n",
3193 mutex_unlock(&dapm
->card
->dapm_mutex
);
3196 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
3198 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget
*w
,
3199 struct snd_kcontrol
*kcontrol
, int event
)
3201 struct snd_soc_dapm_path
*source_p
, *sink_p
;
3202 struct snd_soc_dai
*source
, *sink
;
3203 const struct snd_soc_pcm_stream
*config
= w
->params
;
3204 struct snd_pcm_substream substream
;
3205 struct snd_pcm_hw_params
*params
= NULL
;
3209 if (WARN_ON(!config
) ||
3210 WARN_ON(list_empty(&w
->sources
) || list_empty(&w
->sinks
)))
3213 /* We only support a single source and sink, pick the first */
3214 source_p
= list_first_entry(&w
->sources
, struct snd_soc_dapm_path
,
3216 sink_p
= list_first_entry(&w
->sinks
, struct snd_soc_dapm_path
,
3219 if (WARN_ON(!source_p
|| !sink_p
) ||
3220 WARN_ON(!sink_p
->source
|| !source_p
->sink
) ||
3221 WARN_ON(!source_p
->source
|| !sink_p
->sink
))
3224 source
= source_p
->source
->priv
;
3225 sink
= sink_p
->sink
->priv
;
3227 /* Be a little careful as we don't want to overflow the mask array */
3228 if (config
->formats
) {
3229 fmt
= ffs(config
->formats
) - 1;
3231 dev_warn(w
->dapm
->dev
, "ASoC: Invalid format %llx specified\n",
3236 /* Currently very limited parameter selection */
3237 params
= kzalloc(sizeof(*params
), GFP_KERNEL
);
3242 snd_mask_set(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), fmt
);
3244 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
)->min
=
3246 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
)->max
=
3249 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
)->min
3250 = config
->channels_min
;
3251 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
)->max
3252 = config
->channels_max
;
3254 memset(&substream
, 0, sizeof(substream
));
3257 case SND_SOC_DAPM_PRE_PMU
:
3258 if (source
->driver
->ops
&& source
->driver
->ops
->hw_params
) {
3259 substream
.stream
= SNDRV_PCM_STREAM_CAPTURE
;
3260 ret
= source
->driver
->ops
->hw_params(&substream
,
3263 dev_err(source
->dev
,
3264 "ASoC: hw_params() failed: %d\n", ret
);
3269 if (sink
->driver
->ops
&& sink
->driver
->ops
->hw_params
) {
3270 substream
.stream
= SNDRV_PCM_STREAM_PLAYBACK
;
3271 ret
= sink
->driver
->ops
->hw_params(&substream
, params
,
3275 "ASoC: hw_params() failed: %d\n", ret
);
3281 case SND_SOC_DAPM_POST_PMU
:
3282 ret
= snd_soc_dai_digital_mute(sink
, 0,
3283 SNDRV_PCM_STREAM_PLAYBACK
);
3284 if (ret
!= 0 && ret
!= -ENOTSUPP
)
3285 dev_warn(sink
->dev
, "ASoC: Failed to unmute: %d\n", ret
);
3289 case SND_SOC_DAPM_PRE_PMD
:
3290 ret
= snd_soc_dai_digital_mute(sink
, 1,
3291 SNDRV_PCM_STREAM_PLAYBACK
);
3292 if (ret
!= 0 && ret
!= -ENOTSUPP
)
3293 dev_warn(sink
->dev
, "ASoC: Failed to mute: %d\n", ret
);
3298 WARN(1, "Unknown event %d\n", event
);
3307 int snd_soc_dapm_new_pcm(struct snd_soc_card
*card
,
3308 const struct snd_soc_pcm_stream
*params
,
3309 struct snd_soc_dapm_widget
*source
,
3310 struct snd_soc_dapm_widget
*sink
)
3312 struct snd_soc_dapm_route routes
[2];
3313 struct snd_soc_dapm_widget
template;
3314 struct snd_soc_dapm_widget
*w
;
3318 len
= strlen(source
->name
) + strlen(sink
->name
) + 2;
3319 link_name
= devm_kzalloc(card
->dev
, len
, GFP_KERNEL
);
3322 snprintf(link_name
, len
, "%s-%s", source
->name
, sink
->name
);
3324 memset(&template, 0, sizeof(template));
3325 template.reg
= SND_SOC_NOPM
;
3326 template.id
= snd_soc_dapm_dai_link
;
3327 template.name
= link_name
;
3328 template.event
= snd_soc_dai_link_event
;
3329 template.event_flags
= SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMU
|
3330 SND_SOC_DAPM_PRE_PMD
;
3332 dev_dbg(card
->dev
, "ASoC: adding %s widget\n", link_name
);
3334 w
= snd_soc_dapm_new_control(&card
->dapm
, &template);
3336 dev_err(card
->dev
, "ASoC: Failed to create %s widget\n",
3343 memset(&routes
, 0, sizeof(routes
));
3345 routes
[0].source
= source
->name
;
3346 routes
[0].sink
= link_name
;
3347 routes
[1].source
= link_name
;
3348 routes
[1].sink
= sink
->name
;
3350 return snd_soc_dapm_add_routes(&card
->dapm
, routes
,
3351 ARRAY_SIZE(routes
));
3354 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context
*dapm
,
3355 struct snd_soc_dai
*dai
)
3357 struct snd_soc_dapm_widget
template;
3358 struct snd_soc_dapm_widget
*w
;
3360 WARN_ON(dapm
->dev
!= dai
->dev
);
3362 memset(&template, 0, sizeof(template));
3363 template.reg
= SND_SOC_NOPM
;
3365 if (dai
->driver
->playback
.stream_name
) {
3366 template.id
= snd_soc_dapm_dai_in
;
3367 template.name
= dai
->driver
->playback
.stream_name
;
3368 template.sname
= dai
->driver
->playback
.stream_name
;
3370 dev_dbg(dai
->dev
, "ASoC: adding %s widget\n",
3373 w
= snd_soc_dapm_new_control(dapm
, &template);
3375 dev_err(dapm
->dev
, "ASoC: Failed to create %s widget\n",
3376 dai
->driver
->playback
.stream_name
);
3381 dai
->playback_widget
= w
;
3384 if (dai
->driver
->capture
.stream_name
) {
3385 template.id
= snd_soc_dapm_dai_out
;
3386 template.name
= dai
->driver
->capture
.stream_name
;
3387 template.sname
= dai
->driver
->capture
.stream_name
;
3389 dev_dbg(dai
->dev
, "ASoC: adding %s widget\n",
3392 w
= snd_soc_dapm_new_control(dapm
, &template);
3394 dev_err(dapm
->dev
, "ASoC: Failed to create %s widget\n",
3395 dai
->driver
->capture
.stream_name
);
3400 dai
->capture_widget
= w
;
3406 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card
*card
)
3408 struct snd_soc_dapm_widget
*dai_w
, *w
;
3409 struct snd_soc_dai
*dai
;
3411 /* For each DAI widget... */
3412 list_for_each_entry(dai_w
, &card
->widgets
, list
) {
3413 switch (dai_w
->id
) {
3414 case snd_soc_dapm_dai_in
:
3415 case snd_soc_dapm_dai_out
:
3423 /* ...find all widgets with the same stream and link them */
3424 list_for_each_entry(w
, &card
->widgets
, list
) {
3425 if (w
->dapm
!= dai_w
->dapm
)
3429 case snd_soc_dapm_dai_in
:
3430 case snd_soc_dapm_dai_out
:
3436 if (!w
->sname
|| !strstr(w
->sname
, dai_w
->name
))
3439 if (dai
->driver
->playback
.stream_name
&&
3441 dai
->driver
->playback
.stream_name
)) {
3442 dev_dbg(dai
->dev
, "%s -> %s\n",
3443 dai
->playback_widget
->name
, w
->name
);
3445 snd_soc_dapm_add_path(w
->dapm
,
3446 dai
->playback_widget
, w
, NULL
, NULL
);
3449 if (dai
->driver
->capture
.stream_name
&&
3451 dai
->driver
->capture
.stream_name
)) {
3452 dev_dbg(dai
->dev
, "%s -> %s\n",
3453 w
->name
, dai
->capture_widget
->name
);
3455 snd_soc_dapm_add_path(w
->dapm
, w
,
3456 dai
->capture_widget
, NULL
, NULL
);
3464 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card
*card
)
3466 struct snd_soc_pcm_runtime
*rtd
= card
->rtd
;
3467 struct snd_soc_dai
*cpu_dai
, *codec_dai
;
3468 struct snd_soc_dapm_route r
;
3471 memset(&r
, 0, sizeof(r
));
3473 /* for each BE DAI link... */
3474 for (i
= 0; i
< card
->num_rtd
; i
++) {
3475 rtd
= &card
->rtd
[i
];
3476 cpu_dai
= rtd
->cpu_dai
;
3477 codec_dai
= rtd
->codec_dai
;
3479 /* dynamic FE links have no fixed DAI mapping */
3480 if (rtd
->dai_link
->dynamic
)
3483 /* there is no point in connecting BE DAI links with dummies */
3484 if (snd_soc_dai_is_dummy(codec_dai
) ||
3485 snd_soc_dai_is_dummy(cpu_dai
))
3488 /* connect BE DAI playback if widgets are valid */
3489 if (codec_dai
->playback_widget
&& cpu_dai
->playback_widget
) {
3490 r
.source
= cpu_dai
->playback_widget
->name
;
3491 r
.sink
= codec_dai
->playback_widget
->name
;
3492 dev_dbg(rtd
->dev
, "connected DAI link %s:%s -> %s:%s\n",
3493 cpu_dai
->codec
->name
, r
.source
,
3494 codec_dai
->platform
->name
, r
.sink
);
3496 snd_soc_dapm_add_route(&card
->dapm
, &r
, true);
3499 /* connect BE DAI capture if widgets are valid */
3500 if (codec_dai
->capture_widget
&& cpu_dai
->capture_widget
) {
3501 r
.source
= codec_dai
->capture_widget
->name
;
3502 r
.sink
= cpu_dai
->capture_widget
->name
;
3503 dev_dbg(rtd
->dev
, "connected DAI link %s:%s -> %s:%s\n",
3504 codec_dai
->codec
->name
, r
.source
,
3505 cpu_dai
->platform
->name
, r
.sink
);
3507 snd_soc_dapm_add_route(&card
->dapm
, &r
, true);
3513 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
, int stream
,
3517 struct snd_soc_dapm_widget
*w_cpu
, *w_codec
;
3518 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
3519 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
3521 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3522 w_cpu
= cpu_dai
->playback_widget
;
3523 w_codec
= codec_dai
->playback_widget
;
3525 w_cpu
= cpu_dai
->capture_widget
;
3526 w_codec
= codec_dai
->capture_widget
;
3531 dapm_mark_dirty(w_cpu
, "stream event");
3534 case SND_SOC_DAPM_STREAM_START
:
3537 case SND_SOC_DAPM_STREAM_STOP
:
3540 case SND_SOC_DAPM_STREAM_SUSPEND
:
3541 case SND_SOC_DAPM_STREAM_RESUME
:
3542 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
3543 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
3550 dapm_mark_dirty(w_codec
, "stream event");
3553 case SND_SOC_DAPM_STREAM_START
:
3554 w_codec
->active
= 1;
3556 case SND_SOC_DAPM_STREAM_STOP
:
3557 w_codec
->active
= 0;
3559 case SND_SOC_DAPM_STREAM_SUSPEND
:
3560 case SND_SOC_DAPM_STREAM_RESUME
:
3561 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
3562 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
3567 dapm_power_widgets(rtd
->card
, event
);
3571 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3572 * @rtd: PCM runtime data
3573 * @stream: stream name
3574 * @event: stream event
3576 * Sends a stream event to the dapm core. The core then makes any
3577 * necessary widget power changes.
3579 * Returns 0 for success else error.
3581 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
, int stream
,
3584 struct snd_soc_card
*card
= rtd
->card
;
3586 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3587 soc_dapm_stream_event(rtd
, stream
, event
);
3588 mutex_unlock(&card
->dapm_mutex
);
3592 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3593 * @dapm: DAPM context
3596 * Enables input/output pin and its parents or children widgets iff there is
3597 * a valid audio route and active audio stream.
3599 * Requires external locking.
3601 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3602 * do any widget power switching.
3604 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
3607 return snd_soc_dapm_set_pin(dapm
, pin
, 1);
3609 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked
);
3612 * snd_soc_dapm_enable_pin - enable pin.
3613 * @dapm: DAPM context
3616 * Enables input/output pin and its parents or children widgets iff there is
3617 * a valid audio route and active audio stream.
3619 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3620 * do any widget power switching.
3622 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
3626 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3628 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 1);
3630 mutex_unlock(&dapm
->card
->dapm_mutex
);
3634 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
3637 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3638 * @dapm: DAPM context
3641 * Enables input/output pin regardless of any other state. This is
3642 * intended for use with microphone bias supplies used in microphone
3645 * Requires external locking.
3647 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3648 * do any widget power switching.
3650 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
3653 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
3656 dev_err(dapm
->dev
, "ASoC: unknown pin %s\n", pin
);
3660 dev_dbg(w
->dapm
->dev
, "ASoC: force enable pin %s\n", pin
);
3663 dapm_mark_dirty(w
, "force enable");
3667 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked
);
3670 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3671 * @dapm: DAPM context
3674 * Enables input/output pin regardless of any other state. This is
3675 * intended for use with microphone bias supplies used in microphone
3678 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3679 * do any widget power switching.
3681 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context
*dapm
,
3686 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3688 ret
= snd_soc_dapm_force_enable_pin_unlocked(dapm
, pin
);
3690 mutex_unlock(&dapm
->card
->dapm_mutex
);
3694 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
3697 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3698 * @dapm: DAPM context
3701 * Disables input/output pin and its parents or children widgets.
3703 * Requires external locking.
3705 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3706 * do any widget power switching.
3708 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context
*dapm
,
3711 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
3713 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked
);
3716 * snd_soc_dapm_disable_pin - disable pin.
3717 * @dapm: DAPM context
3720 * Disables input/output pin and its parents or children widgets.
3722 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3723 * do any widget power switching.
3725 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context
*dapm
,
3730 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3732 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 0);
3734 mutex_unlock(&dapm
->card
->dapm_mutex
);
3738 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
3741 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3742 * @dapm: DAPM context
3745 * Marks the specified pin as being not connected, disabling it along
3746 * any parent or child widgets. At present this is identical to
3747 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3748 * additional things such as disabling controls which only affect
3749 * paths through the pin.
3751 * Requires external locking.
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_unlocked(struct snd_soc_dapm_context
*dapm
,
3759 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
3761 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked
);
3764 * snd_soc_dapm_nc_pin - permanently disable pin.
3765 * @dapm: DAPM context
3768 * Marks the specified pin as being not connected, disabling it along
3769 * any parent or child widgets. At present this is identical to
3770 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3771 * additional things such as disabling controls which only affect
3772 * paths through the pin.
3774 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3775 * do any widget power switching.
3777 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
3781 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3783 ret
= snd_soc_dapm_set_pin(dapm
, pin
, 0);
3785 mutex_unlock(&dapm
->card
->dapm_mutex
);
3789 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
3792 * snd_soc_dapm_get_pin_status - get audio pin status
3793 * @dapm: DAPM context
3794 * @pin: audio signal pin endpoint (or start point)
3796 * Get audio pin status - connected or disconnected.
3798 * Returns 1 for connected otherwise 0.
3800 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context
*dapm
,
3803 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
3806 return w
->connected
;
3810 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
3813 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3814 * @dapm: DAPM context
3815 * @pin: audio signal pin endpoint (or start point)
3817 * Mark the given endpoint or pin as ignoring suspend. When the
3818 * system is disabled a path between two endpoints flagged as ignoring
3819 * suspend will not be disabled. The path must already be enabled via
3820 * normal means at suspend time, it will not be turned on if it was not
3823 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context
*dapm
,
3826 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, false);
3829 dev_err(dapm
->dev
, "ASoC: unknown pin %s\n", pin
);
3833 w
->ignore_suspend
= 1;
3837 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
3839 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card
*card
,
3840 struct snd_soc_dapm_widget
*w
)
3842 struct snd_soc_dapm_path
*p
;
3844 list_for_each_entry(p
, &card
->paths
, list
) {
3845 if ((p
->source
== w
) || (p
->sink
== w
)) {
3847 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3848 p
->source
->name
, p
->source
->id
, p
->source
->dapm
,
3849 p
->sink
->name
, p
->sink
->id
, p
->sink
->dapm
);
3851 /* Connected to something other than the codec */
3852 if (p
->source
->dapm
!= p
->sink
->dapm
)
3855 * Loopback connection from codec external pin to
3856 * codec external pin
3858 if (p
->sink
->id
== snd_soc_dapm_input
) {
3859 switch (p
->source
->id
) {
3860 case snd_soc_dapm_output
:
3861 case snd_soc_dapm_micbias
:
3874 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3875 * @codec: The codec whose pins should be processed
3877 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3878 * which are unused. Pins are used if they are connected externally to the
3879 * codec, whether that be to some other device, or a loop-back connection to
3882 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec
*codec
)
3884 struct snd_soc_card
*card
= codec
->card
;
3885 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
3886 struct snd_soc_dapm_widget
*w
;
3888 dev_dbg(codec
->dev
, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
3889 &card
->dapm
, &codec
->dapm
);
3891 list_for_each_entry(w
, &card
->widgets
, list
) {
3892 if (w
->dapm
!= dapm
)
3895 case snd_soc_dapm_input
:
3896 case snd_soc_dapm_output
:
3897 case snd_soc_dapm_micbias
:
3898 dev_dbg(codec
->dev
, "ASoC: Auto NC: Checking widget %s\n",
3900 if (!snd_soc_dapm_widget_in_card_paths(card
, w
)) {
3902 "... Not in map; disabling\n");
3903 snd_soc_dapm_nc_pin(dapm
, w
->name
);
3913 * snd_soc_dapm_free - free dapm resources
3914 * @dapm: DAPM context
3916 * Free all dapm widgets and resources.
3918 void snd_soc_dapm_free(struct snd_soc_dapm_context
*dapm
)
3920 snd_soc_dapm_sys_remove(dapm
->dev
);
3921 dapm_debugfs_cleanup(dapm
);
3922 dapm_free_widgets(dapm
);
3923 list_del(&dapm
->list
);
3925 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
3927 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context
*dapm
)
3929 struct snd_soc_card
*card
= dapm
->card
;
3930 struct snd_soc_dapm_widget
*w
;
3931 LIST_HEAD(down_list
);
3934 mutex_lock(&card
->dapm_mutex
);
3936 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
3937 if (w
->dapm
!= dapm
)
3940 dapm_seq_insert(w
, &down_list
, false);
3946 /* If there were no widgets to power down we're already in
3950 if (dapm
->bias_level
== SND_SOC_BIAS_ON
)
3951 snd_soc_dapm_set_bias_level(dapm
,
3952 SND_SOC_BIAS_PREPARE
);
3953 dapm_seq_run(card
, &down_list
, 0, false);
3954 if (dapm
->bias_level
== SND_SOC_BIAS_PREPARE
)
3955 snd_soc_dapm_set_bias_level(dapm
,
3956 SND_SOC_BIAS_STANDBY
);
3959 mutex_unlock(&card
->dapm_mutex
);
3963 * snd_soc_dapm_shutdown - callback for system shutdown
3965 void snd_soc_dapm_shutdown(struct snd_soc_card
*card
)
3967 struct snd_soc_dapm_context
*dapm
;
3969 list_for_each_entry(dapm
, &card
->dapm_list
, list
) {
3970 if (dapm
!= &card
->dapm
) {
3971 soc_dapm_shutdown_dapm(dapm
);
3972 if (dapm
->bias_level
== SND_SOC_BIAS_STANDBY
)
3973 snd_soc_dapm_set_bias_level(dapm
,
3978 soc_dapm_shutdown_dapm(&card
->dapm
);
3979 if (card
->dapm
.bias_level
== SND_SOC_BIAS_STANDBY
)
3980 snd_soc_dapm_set_bias_level(&card
->dapm
,
3984 /* Module information */
3985 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3986 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3987 MODULE_LICENSE("GPL");