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/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
25 * o DAPM power change sequencing - allow for configurable per
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/init.h>
35 #include <linux/async.h>
36 #include <linux/delay.h>
38 #include <linux/bitops.h>
39 #include <linux/platform_device.h>
40 #include <linux/jiffies.h>
41 #include <linux/debugfs.h>
42 #include <linux/pm_runtime.h>
43 #include <linux/slab.h>
44 #include <sound/core.h>
45 #include <sound/pcm.h>
46 #include <sound/pcm_params.h>
47 #include <sound/soc.h>
48 #include <sound/initval.h>
50 #include <trace/events/asoc.h>
52 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
54 /* dapm power sequences - make this per codec in the future */
55 static int dapm_up_seq
[] = {
56 [snd_soc_dapm_pre
] = 0,
57 [snd_soc_dapm_supply
] = 1,
58 [snd_soc_dapm_micbias
] = 2,
59 [snd_soc_dapm_aif_in
] = 3,
60 [snd_soc_dapm_aif_out
] = 3,
61 [snd_soc_dapm_mic
] = 4,
62 [snd_soc_dapm_mux
] = 5,
63 [snd_soc_dapm_virt_mux
] = 5,
64 [snd_soc_dapm_value_mux
] = 5,
65 [snd_soc_dapm_dac
] = 6,
66 [snd_soc_dapm_mixer
] = 7,
67 [snd_soc_dapm_mixer_named_ctl
] = 7,
68 [snd_soc_dapm_pga
] = 8,
69 [snd_soc_dapm_adc
] = 9,
70 [snd_soc_dapm_out_drv
] = 10,
71 [snd_soc_dapm_hp
] = 10,
72 [snd_soc_dapm_spk
] = 10,
73 [snd_soc_dapm_post
] = 11,
76 static int dapm_down_seq
[] = {
77 [snd_soc_dapm_pre
] = 0,
78 [snd_soc_dapm_adc
] = 1,
79 [snd_soc_dapm_hp
] = 2,
80 [snd_soc_dapm_spk
] = 2,
81 [snd_soc_dapm_out_drv
] = 2,
82 [snd_soc_dapm_pga
] = 4,
83 [snd_soc_dapm_mixer_named_ctl
] = 5,
84 [snd_soc_dapm_mixer
] = 5,
85 [snd_soc_dapm_dac
] = 6,
86 [snd_soc_dapm_mic
] = 7,
87 [snd_soc_dapm_micbias
] = 8,
88 [snd_soc_dapm_mux
] = 9,
89 [snd_soc_dapm_virt_mux
] = 9,
90 [snd_soc_dapm_value_mux
] = 9,
91 [snd_soc_dapm_aif_in
] = 10,
92 [snd_soc_dapm_aif_out
] = 10,
93 [snd_soc_dapm_supply
] = 11,
94 [snd_soc_dapm_post
] = 12,
97 static void pop_wait(u32 pop_time
)
100 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time
));
103 static void pop_dbg(struct device
*dev
, u32 pop_time
, const char *fmt
, ...)
111 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
116 vsnprintf(buf
, PAGE_SIZE
, fmt
, args
);
117 dev_info(dev
, "%s", buf
);
123 static bool dapm_dirty_widget(struct snd_soc_dapm_widget
*w
)
125 return !list_empty(&w
->dirty
);
128 void dapm_mark_dirty(struct snd_soc_dapm_widget
*w
, const char *reason
)
130 if (!dapm_dirty_widget(w
)) {
131 dev_vdbg(w
->dapm
->dev
, "Marking %s dirty due to %s\n",
133 list_add_tail(&w
->dirty
, &w
->dapm
->card
->dapm_dirty
);
136 EXPORT_SYMBOL_GPL(dapm_mark_dirty
);
138 /* create a new dapm widget */
139 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
140 const struct snd_soc_dapm_widget
*_widget
)
142 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
145 /* get snd_card from DAPM context */
146 static inline struct snd_card
*dapm_get_snd_card(
147 struct snd_soc_dapm_context
*dapm
)
150 return dapm
->codec
->card
->snd_card
;
151 else if (dapm
->platform
)
152 return dapm
->platform
->card
->snd_card
;
160 /* get soc_card from DAPM context */
161 static inline struct snd_soc_card
*dapm_get_soc_card(
162 struct snd_soc_dapm_context
*dapm
)
165 return dapm
->codec
->card
;
166 else if (dapm
->platform
)
167 return dapm
->platform
->card
;
175 static int soc_widget_read(struct snd_soc_dapm_widget
*w
, int reg
)
178 return snd_soc_read(w
->codec
, reg
);
179 else if (w
->platform
)
180 return snd_soc_platform_read(w
->platform
, reg
);
182 dev_err(w
->dapm
->dev
, "no valid widget read method\n");
186 static int soc_widget_write(struct snd_soc_dapm_widget
*w
, int reg
, int val
)
189 return snd_soc_write(w
->codec
, reg
, val
);
190 else if (w
->platform
)
191 return snd_soc_platform_write(w
->platform
, reg
, val
);
193 dev_err(w
->dapm
->dev
, "no valid widget write method\n");
197 static int soc_widget_update_bits(struct snd_soc_dapm_widget
*w
,
198 unsigned short reg
, unsigned int mask
, unsigned int value
)
201 unsigned int old
, new;
204 if (w
->codec
&& w
->codec
->using_regmap
) {
205 ret
= regmap_update_bits_check(w
->codec
->control_data
,
206 reg
, mask
, value
, &change
);
210 ret
= soc_widget_read(w
, reg
);
215 new = (old
& ~mask
) | (value
& mask
);
218 ret
= soc_widget_write(w
, reg
, new);
228 * snd_soc_dapm_set_bias_level - set the bias level for the system
229 * @dapm: DAPM context
230 * @level: level to configure
232 * Configure the bias (power) levels for the SoC audio device.
234 * Returns 0 for success else error.
236 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context
*dapm
,
237 enum snd_soc_bias_level level
)
239 struct snd_soc_card
*card
= dapm
->card
;
242 trace_snd_soc_bias_level_start(card
, level
);
244 if (card
&& card
->set_bias_level
)
245 ret
= card
->set_bias_level(card
, dapm
, level
);
250 if (dapm
->codec
->driver
->set_bias_level
)
251 ret
= dapm
->codec
->driver
->set_bias_level(dapm
->codec
,
254 dapm
->bias_level
= level
;
259 if (card
&& card
->set_bias_level_post
)
260 ret
= card
->set_bias_level_post(card
, dapm
, level
);
262 trace_snd_soc_bias_level_done(card
, level
);
267 /* set up initial codec paths */
268 static void dapm_set_path_status(struct snd_soc_dapm_widget
*w
,
269 struct snd_soc_dapm_path
*p
, int i
)
272 case snd_soc_dapm_switch
:
273 case snd_soc_dapm_mixer
:
274 case snd_soc_dapm_mixer_named_ctl
: {
276 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
277 w
->kcontrol_news
[i
].private_value
;
278 unsigned int reg
= mc
->reg
;
279 unsigned int shift
= mc
->shift
;
281 unsigned int mask
= (1 << fls(max
)) - 1;
282 unsigned int invert
= mc
->invert
;
284 val
= soc_widget_read(w
, reg
);
285 val
= (val
>> shift
) & mask
;
287 if ((invert
&& !val
) || (!invert
&& val
))
293 case snd_soc_dapm_mux
: {
294 struct soc_enum
*e
= (struct soc_enum
*)
295 w
->kcontrol_news
[i
].private_value
;
296 int val
, item
, bitmask
;
298 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
300 val
= soc_widget_read(w
, e
->reg
);
301 item
= (val
>> e
->shift_l
) & (bitmask
- 1);
304 for (i
= 0; i
< e
->max
; i
++) {
305 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
310 case snd_soc_dapm_virt_mux
: {
311 struct soc_enum
*e
= (struct soc_enum
*)
312 w
->kcontrol_news
[i
].private_value
;
315 /* since a virtual mux has no backing registers to
316 * decide which path to connect, it will try to match
317 * with the first enumeration. This is to ensure
318 * that the default mux choice (the first) will be
319 * correctly powered up during initialization.
321 if (!strcmp(p
->name
, e
->texts
[0]))
325 case snd_soc_dapm_value_mux
: {
326 struct soc_enum
*e
= (struct soc_enum
*)
327 w
->kcontrol_news
[i
].private_value
;
330 val
= soc_widget_read(w
, e
->reg
);
331 val
= (val
>> e
->shift_l
) & e
->mask
;
332 for (item
= 0; item
< e
->max
; item
++) {
333 if (val
== e
->values
[item
])
338 for (i
= 0; i
< e
->max
; i
++) {
339 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
344 /* does not affect routing - always connected */
345 case snd_soc_dapm_pga
:
346 case snd_soc_dapm_out_drv
:
347 case snd_soc_dapm_output
:
348 case snd_soc_dapm_adc
:
349 case snd_soc_dapm_input
:
350 case snd_soc_dapm_siggen
:
351 case snd_soc_dapm_dac
:
352 case snd_soc_dapm_micbias
:
353 case snd_soc_dapm_vmid
:
354 case snd_soc_dapm_supply
:
355 case snd_soc_dapm_aif_in
:
356 case snd_soc_dapm_aif_out
:
357 case snd_soc_dapm_hp
:
358 case snd_soc_dapm_mic
:
359 case snd_soc_dapm_spk
:
360 case snd_soc_dapm_line
:
363 /* does affect routing - dynamically connected */
364 case snd_soc_dapm_pre
:
365 case snd_soc_dapm_post
:
371 /* connect mux widget to its interconnecting audio paths */
372 static int dapm_connect_mux(struct snd_soc_dapm_context
*dapm
,
373 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
374 struct snd_soc_dapm_path
*path
, const char *control_name
,
375 const struct snd_kcontrol_new
*kcontrol
)
377 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
380 for (i
= 0; i
< e
->max
; i
++) {
381 if (!(strcmp(control_name
, e
->texts
[i
]))) {
382 list_add(&path
->list
, &dapm
->card
->paths
);
383 list_add(&path
->list_sink
, &dest
->sources
);
384 list_add(&path
->list_source
, &src
->sinks
);
385 path
->name
= (char*)e
->texts
[i
];
386 dapm_set_path_status(dest
, path
, 0);
394 /* connect mixer widget to its interconnecting audio paths */
395 static int dapm_connect_mixer(struct snd_soc_dapm_context
*dapm
,
396 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
397 struct snd_soc_dapm_path
*path
, const char *control_name
)
401 /* search for mixer kcontrol */
402 for (i
= 0; i
< dest
->num_kcontrols
; i
++) {
403 if (!strcmp(control_name
, dest
->kcontrol_news
[i
].name
)) {
404 list_add(&path
->list
, &dapm
->card
->paths
);
405 list_add(&path
->list_sink
, &dest
->sources
);
406 list_add(&path
->list_source
, &src
->sinks
);
407 path
->name
= dest
->kcontrol_news
[i
].name
;
408 dapm_set_path_status(dest
, path
, i
);
415 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context
*dapm
,
416 struct snd_soc_dapm_widget
*kcontrolw
,
417 const struct snd_kcontrol_new
*kcontrol_new
,
418 struct snd_kcontrol
**kcontrol
)
420 struct snd_soc_dapm_widget
*w
;
425 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
426 if (w
== kcontrolw
|| w
->dapm
!= kcontrolw
->dapm
)
428 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
429 if (&w
->kcontrol_news
[i
] == kcontrol_new
) {
431 *kcontrol
= w
->kcontrols
[i
];
440 /* create new dapm mixer control */
441 static int dapm_new_mixer(struct snd_soc_dapm_widget
*w
)
443 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
445 size_t name_len
, prefix_len
;
446 struct snd_soc_dapm_path
*path
;
447 struct snd_card
*card
= dapm
->card
->snd_card
;
449 struct snd_soc_dapm_widget_list
*wlist
;
453 prefix
= dapm
->codec
->name_prefix
;
458 prefix_len
= strlen(prefix
) + 1;
463 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
466 list_for_each_entry(path
, &w
->sources
, list_sink
) {
468 /* mixer/mux paths name must match control name */
469 if (path
->name
!= (char *)w
->kcontrol_news
[i
].name
)
472 if (w
->kcontrols
[i
]) {
473 path
->kcontrol
= w
->kcontrols
[i
];
477 wlistsize
= sizeof(struct snd_soc_dapm_widget_list
) +
478 sizeof(struct snd_soc_dapm_widget
*),
479 wlist
= kzalloc(wlistsize
, GFP_KERNEL
);
482 "asoc: can't allocate widget list for %s\n",
486 wlist
->num_widgets
= 1;
487 wlist
->widgets
[0] = w
;
489 /* add dapm control with long name.
490 * for dapm_mixer this is the concatenation of the
491 * mixer and kcontrol name.
492 * for dapm_mixer_named_ctl this is simply the
495 name_len
= strlen(w
->kcontrol_news
[i
].name
) + 1;
496 if (w
->id
!= snd_soc_dapm_mixer_named_ctl
)
497 name_len
+= 1 + strlen(w
->name
);
499 path
->long_name
= kmalloc(name_len
, GFP_KERNEL
);
501 if (path
->long_name
== NULL
) {
508 /* The control will get a prefix from
509 * the control creation process but
510 * we're also using the same prefix
511 * for widgets so cut the prefix off
512 * the front of the widget name.
514 snprintf(path
->long_name
, name_len
, "%s %s",
515 w
->name
+ prefix_len
,
516 w
->kcontrol_news
[i
].name
);
518 case snd_soc_dapm_mixer_named_ctl
:
519 snprintf(path
->long_name
, name_len
, "%s",
520 w
->kcontrol_news
[i
].name
);
524 path
->long_name
[name_len
- 1] = '\0';
526 path
->kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[i
],
527 wlist
, path
->long_name
,
529 ret
= snd_ctl_add(card
, path
->kcontrol
);
532 "asoc: failed to add dapm kcontrol %s: %d\n",
533 path
->long_name
, ret
);
535 kfree(path
->long_name
);
536 path
->long_name
= NULL
;
539 w
->kcontrols
[i
] = path
->kcontrol
;
545 /* create new dapm mux control */
546 static int dapm_new_mux(struct snd_soc_dapm_widget
*w
)
548 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
549 struct snd_soc_dapm_path
*path
= NULL
;
550 struct snd_kcontrol
*kcontrol
;
551 struct snd_card
*card
= dapm
->card
->snd_card
;
555 struct snd_soc_dapm_widget_list
*wlist
;
556 int shared
, wlistentries
;
560 if (w
->num_kcontrols
!= 1) {
562 "asoc: mux %s has incorrect number of controls\n",
567 shared
= dapm_is_shared_kcontrol(dapm
, w
, &w
->kcontrol_news
[0],
570 wlist
= kcontrol
->private_data
;
571 wlistentries
= wlist
->num_widgets
+ 1;
576 wlistsize
= sizeof(struct snd_soc_dapm_widget_list
) +
577 wlistentries
* sizeof(struct snd_soc_dapm_widget
*),
578 wlist
= krealloc(wlist
, wlistsize
, GFP_KERNEL
);
581 "asoc: can't allocate widget list for %s\n", w
->name
);
584 wlist
->num_widgets
= wlistentries
;
585 wlist
->widgets
[wlistentries
- 1] = w
;
589 prefix
= dapm
->codec
->name_prefix
;
594 name
= w
->kcontrol_news
[0].name
;
599 prefix_len
= strlen(prefix
) + 1;
605 * The control will get a prefix from the control creation
606 * process but we're also using the same prefix for widgets so
607 * cut the prefix off the front of the widget name.
609 kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[0], wlist
,
610 name
+ prefix_len
, prefix
);
611 ret
= snd_ctl_add(card
, kcontrol
);
613 dev_err(dapm
->dev
, "failed to add kcontrol %s: %d\n",
620 kcontrol
->private_data
= wlist
;
622 w
->kcontrols
[0] = kcontrol
;
624 list_for_each_entry(path
, &w
->sources
, list_sink
)
625 path
->kcontrol
= kcontrol
;
630 /* create new dapm volume control */
631 static int dapm_new_pga(struct snd_soc_dapm_widget
*w
)
633 if (w
->num_kcontrols
)
634 dev_err(w
->dapm
->dev
,
635 "asoc: PGA controls not supported: '%s'\n", w
->name
);
640 /* reset 'walked' bit for each dapm path */
641 static inline void dapm_clear_walk(struct snd_soc_dapm_context
*dapm
)
643 struct snd_soc_dapm_path
*p
;
645 list_for_each_entry(p
, &dapm
->card
->paths
, list
)
649 /* We implement power down on suspend by checking the power state of
650 * the ALSA card - when we are suspending the ALSA state for the card
653 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
655 int level
= snd_power_get_state(widget
->dapm
->card
->snd_card
);
658 case SNDRV_CTL_POWER_D3hot
:
659 case SNDRV_CTL_POWER_D3cold
:
660 if (widget
->ignore_suspend
)
661 dev_dbg(widget
->dapm
->dev
, "%s ignoring suspend\n",
663 return widget
->ignore_suspend
;
670 * Recursively check for a completed path to an active or physically connected
671 * output widget. Returns number of complete paths.
673 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
)
675 struct snd_soc_dapm_path
*path
;
678 if (widget
->outputs
>= 0)
679 return widget
->outputs
;
681 DAPM_UPDATE_STAT(widget
, path_checks
);
683 if (widget
->id
== snd_soc_dapm_supply
)
686 switch (widget
->id
) {
687 case snd_soc_dapm_adc
:
688 case snd_soc_dapm_aif_out
:
689 if (widget
->active
) {
690 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
691 return widget
->outputs
;
697 if (widget
->connected
) {
698 /* connected pin ? */
699 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
) {
700 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
701 return widget
->outputs
;
704 /* connected jack or spk ? */
705 if (widget
->id
== snd_soc_dapm_hp
||
706 widget
->id
== snd_soc_dapm_spk
||
707 (widget
->id
== snd_soc_dapm_line
&&
708 !list_empty(&widget
->sources
))) {
709 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
710 return widget
->outputs
;
714 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
715 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
723 if (path
->sink
&& path
->connect
) {
725 con
+= is_connected_output_ep(path
->sink
);
729 widget
->outputs
= con
;
735 * Recursively check for a completed path to an active or physically connected
736 * input widget. Returns number of complete paths.
738 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
)
740 struct snd_soc_dapm_path
*path
;
743 if (widget
->inputs
>= 0)
744 return widget
->inputs
;
746 DAPM_UPDATE_STAT(widget
, path_checks
);
748 if (widget
->id
== snd_soc_dapm_supply
)
751 /* active stream ? */
752 switch (widget
->id
) {
753 case snd_soc_dapm_dac
:
754 case snd_soc_dapm_aif_in
:
755 if (widget
->active
) {
756 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
757 return widget
->inputs
;
763 if (widget
->connected
) {
764 /* connected pin ? */
765 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
) {
766 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
767 return widget
->inputs
;
770 /* connected VMID/Bias for lower pops */
771 if (widget
->id
== snd_soc_dapm_vmid
) {
772 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
773 return widget
->inputs
;
776 /* connected jack ? */
777 if (widget
->id
== snd_soc_dapm_mic
||
778 (widget
->id
== snd_soc_dapm_line
&&
779 !list_empty(&widget
->sinks
))) {
780 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
781 return widget
->inputs
;
784 /* signal generator */
785 if (widget
->id
== snd_soc_dapm_siggen
) {
786 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
787 return widget
->inputs
;
791 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
792 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
800 if (path
->source
&& path
->connect
) {
802 con
+= is_connected_input_ep(path
->source
);
806 widget
->inputs
= con
;
812 * Handler for generic register modifier widget.
814 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
815 struct snd_kcontrol
*kcontrol
, int event
)
819 if (SND_SOC_DAPM_EVENT_ON(event
))
824 soc_widget_update_bits(w
, -(w
->reg
+ 1),
825 w
->mask
<< w
->shift
, val
<< w
->shift
);
829 EXPORT_SYMBOL_GPL(dapm_reg_event
);
831 static int dapm_widget_power_check(struct snd_soc_dapm_widget
*w
)
833 if (w
->power_checked
)
839 w
->new_power
= w
->power_check(w
);
841 w
->power_checked
= true;
846 /* Generic check to see if a widget should be powered.
848 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
852 DAPM_UPDATE_STAT(w
, power_checks
);
854 in
= is_connected_input_ep(w
);
855 dapm_clear_walk(w
->dapm
);
856 out
= is_connected_output_ep(w
);
857 dapm_clear_walk(w
->dapm
);
858 return out
!= 0 && in
!= 0;
861 /* Check to see if an ADC has power */
862 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
866 DAPM_UPDATE_STAT(w
, power_checks
);
869 in
= is_connected_input_ep(w
);
870 dapm_clear_walk(w
->dapm
);
873 return dapm_generic_check_power(w
);
877 /* Check to see if a DAC has power */
878 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
882 DAPM_UPDATE_STAT(w
, power_checks
);
885 out
= is_connected_output_ep(w
);
886 dapm_clear_walk(w
->dapm
);
889 return dapm_generic_check_power(w
);
893 /* Check to see if a power supply is needed */
894 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
896 struct snd_soc_dapm_path
*path
;
898 DAPM_UPDATE_STAT(w
, power_checks
);
900 /* Check if one of our outputs is connected */
901 list_for_each_entry(path
, &w
->sinks
, list_source
) {
902 DAPM_UPDATE_STAT(w
, neighbour_checks
);
907 if (path
->connected
&&
908 !path
->connected(path
->source
, path
->sink
))
914 if (dapm_widget_power_check(path
->sink
))
918 dapm_clear_walk(w
->dapm
);
923 static int dapm_always_on_check_power(struct snd_soc_dapm_widget
*w
)
928 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
929 struct snd_soc_dapm_widget
*b
,
937 sort
= dapm_down_seq
;
939 if (sort
[a
->id
] != sort
[b
->id
])
940 return sort
[a
->id
] - sort
[b
->id
];
941 if (a
->subseq
!= b
->subseq
) {
943 return a
->subseq
- b
->subseq
;
945 return b
->subseq
- a
->subseq
;
947 if (a
->reg
!= b
->reg
)
948 return a
->reg
- b
->reg
;
949 if (a
->dapm
!= b
->dapm
)
950 return (unsigned long)a
->dapm
- (unsigned long)b
->dapm
;
955 /* Insert a widget in order into a DAPM power sequence. */
956 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
957 struct list_head
*list
,
960 struct snd_soc_dapm_widget
*w
;
962 list_for_each_entry(w
, list
, power_list
)
963 if (dapm_seq_compare(new_widget
, w
, power_up
) < 0) {
964 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
968 list_add_tail(&new_widget
->power_list
, list
);
971 static void dapm_seq_check_event(struct snd_soc_dapm_context
*dapm
,
972 struct snd_soc_dapm_widget
*w
, int event
)
974 struct snd_soc_card
*card
= dapm
->card
;
979 case SND_SOC_DAPM_PRE_PMU
:
983 case SND_SOC_DAPM_POST_PMU
:
984 ev_name
= "POST_PMU";
987 case SND_SOC_DAPM_PRE_PMD
:
991 case SND_SOC_DAPM_POST_PMD
:
992 ev_name
= "POST_PMD";
1000 if (w
->power
!= power
)
1003 if (w
->event
&& (w
->event_flags
& event
)) {
1004 pop_dbg(dapm
->dev
, card
->pop_time
, "pop test : %s %s\n",
1006 trace_snd_soc_dapm_widget_event_start(w
, event
);
1007 ret
= w
->event(w
, NULL
, event
);
1008 trace_snd_soc_dapm_widget_event_done(w
, event
);
1010 pr_err("%s: %s event failed: %d\n",
1011 ev_name
, w
->name
, ret
);
1015 /* Apply the coalesced changes from a DAPM sequence */
1016 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context
*dapm
,
1017 struct list_head
*pending
)
1019 struct snd_soc_card
*card
= dapm
->card
;
1020 struct snd_soc_dapm_widget
*w
;
1022 unsigned int value
= 0;
1023 unsigned int mask
= 0;
1024 unsigned int cur_mask
;
1026 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
1029 list_for_each_entry(w
, pending
, power_list
) {
1030 cur_mask
= 1 << w
->shift
;
1031 BUG_ON(reg
!= w
->reg
);
1042 pop_dbg(dapm
->dev
, card
->pop_time
,
1043 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1044 w
->name
, reg
, value
, mask
);
1046 /* Check for events */
1047 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_PRE_PMU
);
1048 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_PRE_PMD
);
1052 /* Any widget will do, they should all be updating the
1055 w
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
1058 pop_dbg(dapm
->dev
, card
->pop_time
,
1059 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1060 value
, mask
, reg
, card
->pop_time
);
1061 pop_wait(card
->pop_time
);
1062 soc_widget_update_bits(w
, reg
, mask
, value
);
1065 list_for_each_entry(w
, pending
, power_list
) {
1066 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_POST_PMU
);
1067 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_POST_PMD
);
1071 /* Apply a DAPM power sequence.
1073 * We walk over a pre-sorted list of widgets to apply power to. In
1074 * order to minimise the number of writes to the device required
1075 * multiple widgets will be updated in a single write where possible.
1076 * Currently anything that requires more than a single write is not
1079 static void dapm_seq_run(struct snd_soc_dapm_context
*dapm
,
1080 struct list_head
*list
, int event
, bool power_up
)
1082 struct snd_soc_dapm_widget
*w
, *n
;
1085 int cur_subseq
= -1;
1086 int cur_reg
= SND_SOC_NOPM
;
1087 struct snd_soc_dapm_context
*cur_dapm
= NULL
;
1094 sort
= dapm_down_seq
;
1096 list_for_each_entry_safe(w
, n
, list
, power_list
) {
1099 /* Do we need to apply any queued changes? */
1100 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
||
1101 w
->dapm
!= cur_dapm
|| w
->subseq
!= cur_subseq
) {
1102 if (!list_empty(&pending
))
1103 dapm_seq_run_coalesced(cur_dapm
, &pending
);
1105 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1106 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1107 if (sort
[i
] == cur_sort
)
1108 cur_dapm
->seq_notifier(cur_dapm
,
1113 INIT_LIST_HEAD(&pending
);
1115 cur_subseq
= INT_MIN
;
1116 cur_reg
= SND_SOC_NOPM
;
1121 case snd_soc_dapm_pre
:
1123 list_for_each_entry_safe_continue(w
, n
, list
,
1126 if (event
== SND_SOC_DAPM_STREAM_START
)
1128 NULL
, SND_SOC_DAPM_PRE_PMU
);
1129 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1131 NULL
, SND_SOC_DAPM_PRE_PMD
);
1134 case snd_soc_dapm_post
:
1136 list_for_each_entry_safe_continue(w
, n
, list
,
1139 if (event
== SND_SOC_DAPM_STREAM_START
)
1141 NULL
, SND_SOC_DAPM_POST_PMU
);
1142 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1144 NULL
, SND_SOC_DAPM_POST_PMD
);
1148 /* Queue it up for application */
1149 cur_sort
= sort
[w
->id
];
1150 cur_subseq
= w
->subseq
;
1153 list_move(&w
->power_list
, &pending
);
1158 dev_err(w
->dapm
->dev
,
1159 "Failed to apply widget power: %d\n", ret
);
1162 if (!list_empty(&pending
))
1163 dapm_seq_run_coalesced(cur_dapm
, &pending
);
1165 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1166 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1167 if (sort
[i
] == cur_sort
)
1168 cur_dapm
->seq_notifier(cur_dapm
,
1173 static void dapm_widget_update(struct snd_soc_dapm_context
*dapm
)
1175 struct snd_soc_dapm_update
*update
= dapm
->update
;
1176 struct snd_soc_dapm_widget
*w
;
1185 (w
->event_flags
& SND_SOC_DAPM_PRE_REG
)) {
1186 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_PRE_REG
);
1188 pr_err("%s DAPM pre-event failed: %d\n",
1192 ret
= snd_soc_update_bits(w
->codec
, update
->reg
, update
->mask
,
1195 pr_err("%s DAPM update failed: %d\n", w
->name
, ret
);
1198 (w
->event_flags
& SND_SOC_DAPM_POST_REG
)) {
1199 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_POST_REG
);
1201 pr_err("%s DAPM post-event failed: %d\n",
1206 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1207 * they're changing state.
1209 static void dapm_pre_sequence_async(void *data
, async_cookie_t cookie
)
1211 struct snd_soc_dapm_context
*d
= data
;
1214 /* If we're off and we're not supposed to be go into STANDBY */
1215 if (d
->bias_level
== SND_SOC_BIAS_OFF
&&
1216 d
->target_bias_level
!= SND_SOC_BIAS_OFF
) {
1218 pm_runtime_get_sync(d
->dev
);
1220 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1223 "Failed to turn on bias: %d\n", ret
);
1226 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1227 if (d
->bias_level
!= d
->target_bias_level
) {
1228 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_PREPARE
);
1231 "Failed to prepare bias: %d\n", ret
);
1235 /* Async callback run prior to DAPM sequences - brings to their final
1238 static void dapm_post_sequence_async(void *data
, async_cookie_t cookie
)
1240 struct snd_soc_dapm_context
*d
= data
;
1243 /* If we just powered the last thing off drop to standby bias */
1244 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1245 (d
->target_bias_level
== SND_SOC_BIAS_STANDBY
||
1246 d
->target_bias_level
== SND_SOC_BIAS_OFF
)) {
1247 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1249 dev_err(d
->dev
, "Failed to apply standby bias: %d\n",
1253 /* If we're in standby and can support bias off then do that */
1254 if (d
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1255 d
->target_bias_level
== SND_SOC_BIAS_OFF
) {
1256 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_OFF
);
1258 dev_err(d
->dev
, "Failed to turn off bias: %d\n", ret
);
1261 pm_runtime_put_sync(d
->dev
);
1264 /* If we just powered up then move to active bias */
1265 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1266 d
->target_bias_level
== SND_SOC_BIAS_ON
) {
1267 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_ON
);
1269 dev_err(d
->dev
, "Failed to apply active bias: %d\n",
1274 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget
*peer
,
1275 bool power
, bool connect
)
1277 /* If a connection is being made or broken then that update
1278 * will have marked the peer dirty, otherwise the widgets are
1279 * not connected and this update has no impact. */
1283 /* If the peer is already in the state we're moving to then we
1284 * won't have an impact on it. */
1285 if (power
!= peer
->power
)
1286 dapm_mark_dirty(peer
, "peer state change");
1289 static void dapm_widget_set_power(struct snd_soc_dapm_widget
*w
, bool power
,
1290 struct list_head
*up_list
,
1291 struct list_head
*down_list
)
1293 struct snd_soc_dapm_path
*path
;
1295 if (w
->power
== power
)
1298 trace_snd_soc_dapm_widget_power(w
, power
);
1300 /* If we changed our power state perhaps our neigbours changed
1303 list_for_each_entry(path
, &w
->sources
, list_sink
) {
1305 dapm_widget_set_peer_power(path
->source
, power
,
1310 case snd_soc_dapm_supply
:
1311 /* Supplies can't affect their outputs, only their inputs */
1314 list_for_each_entry(path
, &w
->sinks
, list_source
) {
1316 dapm_widget_set_peer_power(path
->sink
, power
,
1324 dapm_seq_insert(w
, up_list
, true);
1326 dapm_seq_insert(w
, down_list
, false);
1331 static void dapm_power_one_widget(struct snd_soc_dapm_widget
*w
,
1332 struct list_head
*up_list
,
1333 struct list_head
*down_list
)
1338 case snd_soc_dapm_pre
:
1339 dapm_seq_insert(w
, down_list
, false);
1341 case snd_soc_dapm_post
:
1342 dapm_seq_insert(w
, up_list
, true);
1346 power
= dapm_widget_power_check(w
);
1348 dapm_widget_set_power(w
, power
, up_list
, down_list
);
1354 * Scan each dapm widget for complete audio path.
1355 * A complete path is a route that has valid endpoints i.e.:-
1357 * o DAC to output pin.
1358 * o Input Pin to ADC.
1359 * o Input pin to Output pin (bypass, sidetone)
1360 * o DAC to ADC (loopback).
1362 static int dapm_power_widgets(struct snd_soc_dapm_context
*dapm
, int event
)
1364 struct snd_soc_card
*card
= dapm
->card
;
1365 struct snd_soc_dapm_widget
*w
;
1366 struct snd_soc_dapm_context
*d
;
1368 LIST_HEAD(down_list
);
1369 LIST_HEAD(async_domain
);
1370 enum snd_soc_bias_level bias
;
1372 trace_snd_soc_dapm_start(card
);
1374 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1375 if (d
->n_widgets
|| d
->codec
== NULL
) {
1376 if (d
->idle_bias_off
)
1377 d
->target_bias_level
= SND_SOC_BIAS_OFF
;
1379 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1383 memset(&card
->dapm_stats
, 0, sizeof(card
->dapm_stats
));
1385 list_for_each_entry(w
, &card
->widgets
, list
) {
1386 w
->power_checked
= false;
1391 /* Check which widgets we need to power and store them in
1392 * lists indicating if they should be powered up or down. We
1393 * only check widgets that have been flagged as dirty but note
1394 * that new widgets may be added to the dirty list while we
1397 list_for_each_entry(w
, &card
->dapm_dirty
, dirty
) {
1398 dapm_power_one_widget(w
, &up_list
, &down_list
);
1401 list_for_each_entry(w
, &card
->widgets
, list
) {
1402 list_del_init(&w
->dirty
);
1407 /* Supplies and micbiases only bring the
1408 * context up to STANDBY as unless something
1409 * else is active and passing audio they
1410 * generally don't require full power.
1413 case snd_soc_dapm_supply
:
1414 case snd_soc_dapm_micbias
:
1415 if (d
->target_bias_level
< SND_SOC_BIAS_STANDBY
)
1416 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1419 d
->target_bias_level
= SND_SOC_BIAS_ON
;
1426 /* If there are no DAPM widgets then try to figure out power from the
1429 if (!dapm
->n_widgets
) {
1431 case SND_SOC_DAPM_STREAM_START
:
1432 case SND_SOC_DAPM_STREAM_RESUME
:
1433 dapm
->target_bias_level
= SND_SOC_BIAS_ON
;
1435 case SND_SOC_DAPM_STREAM_STOP
:
1436 if (dapm
->codec
&& dapm
->codec
->active
)
1437 dapm
->target_bias_level
= SND_SOC_BIAS_ON
;
1439 dapm
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1441 case SND_SOC_DAPM_STREAM_SUSPEND
:
1442 dapm
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1444 case SND_SOC_DAPM_STREAM_NOP
:
1445 dapm
->target_bias_level
= dapm
->bias_level
;
1452 /* Force all contexts in the card to the same bias state if
1453 * they're not ground referenced.
1455 bias
= SND_SOC_BIAS_OFF
;
1456 list_for_each_entry(d
, &card
->dapm_list
, list
)
1457 if (d
->target_bias_level
> bias
)
1458 bias
= d
->target_bias_level
;
1459 list_for_each_entry(d
, &card
->dapm_list
, list
)
1460 if (!d
->idle_bias_off
)
1461 d
->target_bias_level
= bias
;
1463 trace_snd_soc_dapm_walk_done(card
);
1465 /* Run all the bias changes in parallel */
1466 list_for_each_entry(d
, &dapm
->card
->dapm_list
, list
)
1467 async_schedule_domain(dapm_pre_sequence_async
, d
,
1469 async_synchronize_full_domain(&async_domain
);
1471 /* Power down widgets first; try to avoid amplifying pops. */
1472 dapm_seq_run(dapm
, &down_list
, event
, false);
1474 dapm_widget_update(dapm
);
1477 dapm_seq_run(dapm
, &up_list
, event
, true);
1479 /* Run all the bias changes in parallel */
1480 list_for_each_entry(d
, &dapm
->card
->dapm_list
, list
)
1481 async_schedule_domain(dapm_post_sequence_async
, d
,
1483 async_synchronize_full_domain(&async_domain
);
1485 pop_dbg(dapm
->dev
, card
->pop_time
,
1486 "DAPM sequencing finished, waiting %dms\n", card
->pop_time
);
1487 pop_wait(card
->pop_time
);
1489 trace_snd_soc_dapm_done(card
);
1494 #ifdef CONFIG_DEBUG_FS
1495 static int dapm_widget_power_open_file(struct inode
*inode
, struct file
*file
)
1497 file
->private_data
= inode
->i_private
;
1501 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1502 char __user
*user_buf
,
1503 size_t count
, loff_t
*ppos
)
1505 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1509 struct snd_soc_dapm_path
*p
= NULL
;
1511 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1515 in
= is_connected_input_ep(w
);
1516 dapm_clear_walk(w
->dapm
);
1517 out
= is_connected_output_ep(w
);
1518 dapm_clear_walk(w
->dapm
);
1520 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s in %d out %d",
1521 w
->name
, w
->power
? "On" : "Off", in
, out
);
1524 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1525 " - R%d(0x%x) bit %d",
1526 w
->reg
, w
->reg
, w
->shift
);
1528 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
1531 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1533 w
->active
? "active" : "inactive");
1535 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1536 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1540 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1541 " in \"%s\" \"%s\"\n",
1542 p
->name
? p
->name
: "static",
1545 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1546 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1550 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1551 " out \"%s\" \"%s\"\n",
1552 p
->name
? p
->name
: "static",
1556 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1562 static const struct file_operations dapm_widget_power_fops
= {
1563 .open
= dapm_widget_power_open_file
,
1564 .read
= dapm_widget_power_read_file
,
1565 .llseek
= default_llseek
,
1568 static int dapm_bias_open_file(struct inode
*inode
, struct file
*file
)
1570 file
->private_data
= inode
->i_private
;
1574 static ssize_t
dapm_bias_read_file(struct file
*file
, char __user
*user_buf
,
1575 size_t count
, loff_t
*ppos
)
1577 struct snd_soc_dapm_context
*dapm
= file
->private_data
;
1580 switch (dapm
->bias_level
) {
1581 case SND_SOC_BIAS_ON
:
1584 case SND_SOC_BIAS_PREPARE
:
1585 level
= "Prepare\n";
1587 case SND_SOC_BIAS_STANDBY
:
1588 level
= "Standby\n";
1590 case SND_SOC_BIAS_OFF
:
1595 level
= "Unknown\n";
1599 return simple_read_from_buffer(user_buf
, count
, ppos
, level
,
1603 static const struct file_operations dapm_bias_fops
= {
1604 .open
= dapm_bias_open_file
,
1605 .read
= dapm_bias_read_file
,
1606 .llseek
= default_llseek
,
1609 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
1610 struct dentry
*parent
)
1614 dapm
->debugfs_dapm
= debugfs_create_dir("dapm", parent
);
1616 if (!dapm
->debugfs_dapm
) {
1618 "Failed to create DAPM debugfs directory\n");
1622 d
= debugfs_create_file("bias_level", 0444,
1623 dapm
->debugfs_dapm
, dapm
,
1627 "ASoC: Failed to create bias level debugfs file\n");
1630 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
1632 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
1635 if (!dapm
->debugfs_dapm
|| !w
->name
)
1638 d
= debugfs_create_file(w
->name
, 0444,
1639 dapm
->debugfs_dapm
, w
,
1640 &dapm_widget_power_fops
);
1642 dev_warn(w
->dapm
->dev
,
1643 "ASoC: Failed to create %s debugfs file\n",
1647 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
1649 debugfs_remove_recursive(dapm
->debugfs_dapm
);
1653 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
1654 struct dentry
*parent
)
1658 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
1662 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
1668 /* test and update the power status of a mux widget */
1669 static int dapm_mux_update_power(struct snd_soc_dapm_widget
*widget
,
1670 struct snd_kcontrol
*kcontrol
, int change
,
1671 int mux
, struct soc_enum
*e
)
1673 struct snd_soc_dapm_path
*path
;
1676 if (widget
->id
!= snd_soc_dapm_mux
&&
1677 widget
->id
!= snd_soc_dapm_virt_mux
&&
1678 widget
->id
!= snd_soc_dapm_value_mux
)
1684 /* find dapm widget path assoc with kcontrol */
1685 list_for_each_entry(path
, &widget
->dapm
->card
->paths
, list
) {
1686 if (path
->kcontrol
!= kcontrol
)
1689 if (!path
->name
|| !e
->texts
[mux
])
1693 /* we now need to match the string in the enum to the path */
1694 if (!(strcmp(path
->name
, e
->texts
[mux
]))) {
1695 path
->connect
= 1; /* new connection */
1696 dapm_mark_dirty(path
->source
, "mux connection");
1699 dapm_mark_dirty(path
->source
,
1700 "mux disconnection");
1701 path
->connect
= 0; /* old connection must be powered down */
1706 dapm_mark_dirty(widget
, "mux change");
1707 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1713 /* test and update the power status of a mixer or switch widget */
1714 static int dapm_mixer_update_power(struct snd_soc_dapm_widget
*widget
,
1715 struct snd_kcontrol
*kcontrol
, int connect
)
1717 struct snd_soc_dapm_path
*path
;
1720 if (widget
->id
!= snd_soc_dapm_mixer
&&
1721 widget
->id
!= snd_soc_dapm_mixer_named_ctl
&&
1722 widget
->id
!= snd_soc_dapm_switch
)
1725 /* find dapm widget path assoc with kcontrol */
1726 list_for_each_entry(path
, &widget
->dapm
->card
->paths
, list
) {
1727 if (path
->kcontrol
!= kcontrol
)
1730 /* found, now check type */
1732 path
->connect
= connect
;
1733 dapm_mark_dirty(path
->source
, "mixer connection");
1737 dapm_mark_dirty(widget
, "mixer update");
1738 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1744 /* show dapm widget status in sys fs */
1745 static ssize_t
dapm_widget_show(struct device
*dev
,
1746 struct device_attribute
*attr
, char *buf
)
1748 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
1749 struct snd_soc_codec
*codec
=rtd
->codec
;
1750 struct snd_soc_dapm_widget
*w
;
1752 char *state
= "not set";
1754 list_for_each_entry(w
, &codec
->card
->widgets
, list
) {
1755 if (w
->dapm
!= &codec
->dapm
)
1758 /* only display widgets that burnm power */
1760 case snd_soc_dapm_hp
:
1761 case snd_soc_dapm_mic
:
1762 case snd_soc_dapm_spk
:
1763 case snd_soc_dapm_line
:
1764 case snd_soc_dapm_micbias
:
1765 case snd_soc_dapm_dac
:
1766 case snd_soc_dapm_adc
:
1767 case snd_soc_dapm_pga
:
1768 case snd_soc_dapm_out_drv
:
1769 case snd_soc_dapm_mixer
:
1770 case snd_soc_dapm_mixer_named_ctl
:
1771 case snd_soc_dapm_supply
:
1773 count
+= sprintf(buf
+ count
, "%s: %s\n",
1774 w
->name
, w
->power
? "On":"Off");
1781 switch (codec
->dapm
.bias_level
) {
1782 case SND_SOC_BIAS_ON
:
1785 case SND_SOC_BIAS_PREPARE
:
1788 case SND_SOC_BIAS_STANDBY
:
1791 case SND_SOC_BIAS_OFF
:
1795 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
1800 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
1802 int snd_soc_dapm_sys_add(struct device
*dev
)
1804 return device_create_file(dev
, &dev_attr_dapm_widget
);
1807 static void snd_soc_dapm_sys_remove(struct device
*dev
)
1809 device_remove_file(dev
, &dev_attr_dapm_widget
);
1812 /* free all dapm widgets and resources */
1813 static void dapm_free_widgets(struct snd_soc_dapm_context
*dapm
)
1815 struct snd_soc_dapm_widget
*w
, *next_w
;
1816 struct snd_soc_dapm_path
*p
, *next_p
;
1818 list_for_each_entry_safe(w
, next_w
, &dapm
->card
->widgets
, list
) {
1819 if (w
->dapm
!= dapm
)
1823 * remove source and sink paths associated to this widget.
1824 * While removing the path, remove reference to it from both
1825 * source and sink widgets so that path is removed only once.
1827 list_for_each_entry_safe(p
, next_p
, &w
->sources
, list_sink
) {
1828 list_del(&p
->list_sink
);
1829 list_del(&p
->list_source
);
1831 kfree(p
->long_name
);
1834 list_for_each_entry_safe(p
, next_p
, &w
->sinks
, list_source
) {
1835 list_del(&p
->list_sink
);
1836 list_del(&p
->list_source
);
1838 kfree(p
->long_name
);
1841 kfree(w
->kcontrols
);
1847 static struct snd_soc_dapm_widget
*dapm_find_widget(
1848 struct snd_soc_dapm_context
*dapm
, const char *pin
,
1849 bool search_other_contexts
)
1851 struct snd_soc_dapm_widget
*w
;
1852 struct snd_soc_dapm_widget
*fallback
= NULL
;
1854 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
1855 if (!strcmp(w
->name
, pin
)) {
1856 if (w
->dapm
== dapm
)
1863 if (search_other_contexts
)
1869 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context
*dapm
,
1870 const char *pin
, int status
)
1872 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
1875 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
1879 w
->connected
= status
;
1882 dapm_mark_dirty(w
, "pin configuration");
1888 * snd_soc_dapm_sync - scan and power dapm paths
1889 * @dapm: DAPM context
1891 * Walks all dapm audio paths and powers widgets according to their
1892 * stream or path usage.
1894 * Returns 0 for success.
1896 int snd_soc_dapm_sync(struct snd_soc_dapm_context
*dapm
)
1899 * Suppress early reports (eg, jacks syncing their state) to avoid
1900 * silly DAPM runs during card startup.
1902 if (!dapm
->card
|| !dapm
->card
->instantiated
)
1905 return dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
1907 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
1909 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context
*dapm
,
1910 const struct snd_soc_dapm_route
*route
)
1912 struct snd_soc_dapm_path
*path
;
1913 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
1914 struct snd_soc_dapm_widget
*wtsource
= NULL
, *wtsink
= NULL
;
1916 const char *control
= route
->control
;
1918 char prefixed_sink
[80];
1919 char prefixed_source
[80];
1922 if (dapm
->codec
&& dapm
->codec
->name_prefix
) {
1923 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
1924 dapm
->codec
->name_prefix
, route
->sink
);
1925 sink
= prefixed_sink
;
1926 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
1927 dapm
->codec
->name_prefix
, route
->source
);
1928 source
= prefixed_source
;
1931 source
= route
->source
;
1935 * find src and dest widgets over all widgets but favor a widget from
1936 * current DAPM context
1938 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
1939 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
1941 if (w
->dapm
== dapm
)
1945 if (!wsource
&& !(strcmp(w
->name
, source
))) {
1947 if (w
->dapm
== dapm
)
1951 /* use widget from another DAPM context if not found from this */
1957 if (wsource
== NULL
|| wsink
== NULL
)
1960 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
1964 path
->source
= wsource
;
1966 path
->connected
= route
->connected
;
1967 INIT_LIST_HEAD(&path
->list
);
1968 INIT_LIST_HEAD(&path
->list_source
);
1969 INIT_LIST_HEAD(&path
->list_sink
);
1971 /* check for external widgets */
1972 if (wsink
->id
== snd_soc_dapm_input
) {
1973 if (wsource
->id
== snd_soc_dapm_micbias
||
1974 wsource
->id
== snd_soc_dapm_mic
||
1975 wsource
->id
== snd_soc_dapm_line
||
1976 wsource
->id
== snd_soc_dapm_output
)
1979 if (wsource
->id
== snd_soc_dapm_output
) {
1980 if (wsink
->id
== snd_soc_dapm_spk
||
1981 wsink
->id
== snd_soc_dapm_hp
||
1982 wsink
->id
== snd_soc_dapm_line
||
1983 wsink
->id
== snd_soc_dapm_input
)
1987 /* connect static paths */
1988 if (control
== NULL
) {
1989 list_add(&path
->list
, &dapm
->card
->paths
);
1990 list_add(&path
->list_sink
, &wsink
->sources
);
1991 list_add(&path
->list_source
, &wsource
->sinks
);
1996 /* connect dynamic paths */
1997 switch (wsink
->id
) {
1998 case snd_soc_dapm_adc
:
1999 case snd_soc_dapm_dac
:
2000 case snd_soc_dapm_pga
:
2001 case snd_soc_dapm_out_drv
:
2002 case snd_soc_dapm_input
:
2003 case snd_soc_dapm_output
:
2004 case snd_soc_dapm_siggen
:
2005 case snd_soc_dapm_micbias
:
2006 case snd_soc_dapm_vmid
:
2007 case snd_soc_dapm_pre
:
2008 case snd_soc_dapm_post
:
2009 case snd_soc_dapm_supply
:
2010 case snd_soc_dapm_aif_in
:
2011 case snd_soc_dapm_aif_out
:
2012 list_add(&path
->list
, &dapm
->card
->paths
);
2013 list_add(&path
->list_sink
, &wsink
->sources
);
2014 list_add(&path
->list_source
, &wsource
->sinks
);
2017 case snd_soc_dapm_mux
:
2018 case snd_soc_dapm_virt_mux
:
2019 case snd_soc_dapm_value_mux
:
2020 ret
= dapm_connect_mux(dapm
, wsource
, wsink
, path
, control
,
2021 &wsink
->kcontrol_news
[0]);
2025 case snd_soc_dapm_switch
:
2026 case snd_soc_dapm_mixer
:
2027 case snd_soc_dapm_mixer_named_ctl
:
2028 ret
= dapm_connect_mixer(dapm
, wsource
, wsink
, path
, control
);
2032 case snd_soc_dapm_hp
:
2033 case snd_soc_dapm_mic
:
2034 case snd_soc_dapm_line
:
2035 case snd_soc_dapm_spk
:
2036 list_add(&path
->list
, &dapm
->card
->paths
);
2037 list_add(&path
->list_sink
, &wsink
->sources
);
2038 list_add(&path
->list_source
, &wsource
->sinks
);
2045 dev_warn(dapm
->dev
, "asoc: no dapm match for %s --> %s --> %s\n",
2046 source
, control
, sink
);
2052 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2053 * @dapm: DAPM context
2054 * @route: audio routes
2055 * @num: number of routes
2057 * Connects 2 dapm widgets together via a named audio path. The sink is
2058 * the widget receiving the audio signal, whilst the source is the sender
2059 * of the audio signal.
2061 * Returns 0 for success else error. On error all resources can be freed
2062 * with a call to snd_soc_card_free().
2064 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context
*dapm
,
2065 const struct snd_soc_dapm_route
*route
, int num
)
2069 for (i
= 0; i
< num
; i
++) {
2070 ret
= snd_soc_dapm_add_route(dapm
, route
);
2072 dev_err(dapm
->dev
, "Failed to add route %s->%s\n",
2073 route
->source
, route
->sink
);
2081 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
2083 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context
*dapm
,
2084 const struct snd_soc_dapm_route
*route
)
2086 struct snd_soc_dapm_widget
*source
= dapm_find_widget(dapm
,
2089 struct snd_soc_dapm_widget
*sink
= dapm_find_widget(dapm
,
2092 struct snd_soc_dapm_path
*path
;
2096 dev_err(dapm
->dev
, "Unable to find source %s for weak route\n",
2102 dev_err(dapm
->dev
, "Unable to find sink %s for weak route\n",
2107 if (route
->control
|| route
->connected
)
2108 dev_warn(dapm
->dev
, "Ignoring control for weak route %s->%s\n",
2109 route
->source
, route
->sink
);
2111 list_for_each_entry(path
, &source
->sinks
, list_source
) {
2112 if (path
->sink
== sink
) {
2119 dev_err(dapm
->dev
, "No path found for weak route %s->%s\n",
2120 route
->source
, route
->sink
);
2122 dev_warn(dapm
->dev
, "%d paths found for weak route %s->%s\n",
2123 count
, route
->source
, route
->sink
);
2129 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2130 * @dapm: DAPM context
2131 * @route: audio routes
2132 * @num: number of routes
2134 * Mark existing routes matching those specified in the passed array
2135 * as being weak, meaning that they are ignored for the purpose of
2136 * power decisions. The main intended use case is for sidetone paths
2137 * which couple audio between other independent paths if they are both
2138 * active in order to make the combination work better at the user
2139 * level but which aren't intended to be "used".
2141 * Note that CODEC drivers should not use this as sidetone type paths
2142 * can frequently also be used as bypass paths.
2144 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context
*dapm
,
2145 const struct snd_soc_dapm_route
*route
, int num
)
2150 for (i
= 0; i
< num
; i
++) {
2151 err
= snd_soc_dapm_weak_route(dapm
, route
);
2159 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes
);
2162 * snd_soc_dapm_new_widgets - add new dapm widgets
2163 * @dapm: DAPM context
2165 * Checks the codec for any new dapm widgets and creates them if found.
2167 * Returns 0 for success.
2169 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context
*dapm
)
2171 struct snd_soc_dapm_widget
*w
;
2174 list_for_each_entry(w
, &dapm
->card
->widgets
, list
)
2179 if (w
->num_kcontrols
) {
2180 w
->kcontrols
= kzalloc(w
->num_kcontrols
*
2181 sizeof(struct snd_kcontrol
*),
2188 case snd_soc_dapm_switch
:
2189 case snd_soc_dapm_mixer
:
2190 case snd_soc_dapm_mixer_named_ctl
:
2193 case snd_soc_dapm_mux
:
2194 case snd_soc_dapm_virt_mux
:
2195 case snd_soc_dapm_value_mux
:
2198 case snd_soc_dapm_pga
:
2199 case snd_soc_dapm_out_drv
:
2206 /* Read the initial power state from the device */
2208 val
= soc_widget_read(w
, w
->reg
);
2209 val
&= 1 << w
->shift
;
2219 dapm_mark_dirty(w
, "new widget");
2220 dapm_debugfs_add_widget(w
);
2223 dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
2226 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
2229 * snd_soc_dapm_get_volsw - dapm mixer get callback
2230 * @kcontrol: mixer control
2231 * @ucontrol: control element information
2233 * Callback to get the value of a dapm mixer control.
2235 * Returns 0 for success.
2237 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
2238 struct snd_ctl_elem_value
*ucontrol
)
2240 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2241 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2242 struct soc_mixer_control
*mc
=
2243 (struct soc_mixer_control
*)kcontrol
->private_value
;
2244 unsigned int reg
= mc
->reg
;
2245 unsigned int shift
= mc
->shift
;
2246 unsigned int rshift
= mc
->rshift
;
2248 unsigned int invert
= mc
->invert
;
2249 unsigned int mask
= (1 << fls(max
)) - 1;
2251 ucontrol
->value
.integer
.value
[0] =
2252 (snd_soc_read(widget
->codec
, reg
) >> shift
) & mask
;
2253 if (shift
!= rshift
)
2254 ucontrol
->value
.integer
.value
[1] =
2255 (snd_soc_read(widget
->codec
, reg
) >> rshift
) & mask
;
2257 ucontrol
->value
.integer
.value
[0] =
2258 max
- ucontrol
->value
.integer
.value
[0];
2259 if (shift
!= rshift
)
2260 ucontrol
->value
.integer
.value
[1] =
2261 max
- ucontrol
->value
.integer
.value
[1];
2266 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
2269 * snd_soc_dapm_put_volsw - dapm mixer set callback
2270 * @kcontrol: mixer control
2271 * @ucontrol: control element information
2273 * Callback to set the value of a dapm mixer control.
2275 * Returns 0 for success.
2277 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
2278 struct snd_ctl_elem_value
*ucontrol
)
2280 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2281 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2282 struct snd_soc_codec
*codec
= widget
->codec
;
2283 struct soc_mixer_control
*mc
=
2284 (struct soc_mixer_control
*)kcontrol
->private_value
;
2285 unsigned int reg
= mc
->reg
;
2286 unsigned int shift
= mc
->shift
;
2288 unsigned int mask
= (1 << fls(max
)) - 1;
2289 unsigned int invert
= mc
->invert
;
2291 int connect
, change
;
2292 struct snd_soc_dapm_update update
;
2295 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2299 mask
= mask
<< shift
;
2303 /* new connection */
2304 connect
= invert
? 0 : 1;
2306 /* old connection must be powered down */
2307 connect
= invert
? 1 : 0;
2309 mutex_lock(&codec
->mutex
);
2311 change
= snd_soc_test_bits(widget
->codec
, reg
, mask
, val
);
2313 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2314 widget
= wlist
->widgets
[wi
];
2316 widget
->value
= val
;
2318 update
.kcontrol
= kcontrol
;
2319 update
.widget
= widget
;
2323 widget
->dapm
->update
= &update
;
2325 dapm_mixer_update_power(widget
, kcontrol
, connect
);
2327 widget
->dapm
->update
= NULL
;
2331 mutex_unlock(&codec
->mutex
);
2334 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
2337 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2338 * @kcontrol: mixer control
2339 * @ucontrol: control element information
2341 * Callback to get the value of a dapm enumerated double mixer control.
2343 * Returns 0 for success.
2345 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
2346 struct snd_ctl_elem_value
*ucontrol
)
2348 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2349 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2350 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2351 unsigned int val
, bitmask
;
2353 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2355 val
= snd_soc_read(widget
->codec
, e
->reg
);
2356 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & (bitmask
- 1);
2357 if (e
->shift_l
!= e
->shift_r
)
2358 ucontrol
->value
.enumerated
.item
[1] =
2359 (val
>> e
->shift_r
) & (bitmask
- 1);
2363 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
2366 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2367 * @kcontrol: mixer control
2368 * @ucontrol: control element information
2370 * Callback to set the value of a dapm enumerated double mixer control.
2372 * Returns 0 for success.
2374 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
2375 struct snd_ctl_elem_value
*ucontrol
)
2377 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2378 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2379 struct snd_soc_codec
*codec
= widget
->codec
;
2380 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2381 unsigned int val
, mux
, change
;
2382 unsigned int mask
, bitmask
;
2383 struct snd_soc_dapm_update update
;
2386 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2388 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2390 mux
= ucontrol
->value
.enumerated
.item
[0];
2391 val
= mux
<< e
->shift_l
;
2392 mask
= (bitmask
- 1) << e
->shift_l
;
2393 if (e
->shift_l
!= e
->shift_r
) {
2394 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2396 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
2397 mask
|= (bitmask
- 1) << e
->shift_r
;
2400 mutex_lock(&codec
->mutex
);
2402 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
2404 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2405 widget
= wlist
->widgets
[wi
];
2407 widget
->value
= val
;
2409 update
.kcontrol
= kcontrol
;
2410 update
.widget
= widget
;
2411 update
.reg
= e
->reg
;
2414 widget
->dapm
->update
= &update
;
2416 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
2418 widget
->dapm
->update
= NULL
;
2422 mutex_unlock(&codec
->mutex
);
2425 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
2428 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2429 * @kcontrol: mixer control
2430 * @ucontrol: control element information
2432 * Returns 0 for success.
2434 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol
*kcontrol
,
2435 struct snd_ctl_elem_value
*ucontrol
)
2437 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2438 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2440 ucontrol
->value
.enumerated
.item
[0] = widget
->value
;
2444 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt
);
2447 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2448 * @kcontrol: mixer control
2449 * @ucontrol: control element information
2451 * Returns 0 for success.
2453 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol
*kcontrol
,
2454 struct snd_ctl_elem_value
*ucontrol
)
2456 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2457 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2458 struct snd_soc_codec
*codec
= widget
->codec
;
2459 struct soc_enum
*e
=
2460 (struct soc_enum
*)kcontrol
->private_value
;
2465 if (ucontrol
->value
.enumerated
.item
[0] >= e
->max
)
2468 mutex_lock(&codec
->mutex
);
2470 change
= widget
->value
!= ucontrol
->value
.enumerated
.item
[0];
2472 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2473 widget
= wlist
->widgets
[wi
];
2475 widget
->value
= ucontrol
->value
.enumerated
.item
[0];
2477 dapm_mux_update_power(widget
, kcontrol
, change
,
2482 mutex_unlock(&codec
->mutex
);
2485 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt
);
2488 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2490 * @kcontrol: mixer control
2491 * @ucontrol: control element information
2493 * Callback to get the value of a dapm semi enumerated double mixer control.
2495 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2496 * used for handling bitfield coded enumeration for example.
2498 * Returns 0 for success.
2500 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
2501 struct snd_ctl_elem_value
*ucontrol
)
2503 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2504 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2505 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2506 unsigned int reg_val
, val
, mux
;
2508 reg_val
= snd_soc_read(widget
->codec
, e
->reg
);
2509 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
2510 for (mux
= 0; mux
< e
->max
; mux
++) {
2511 if (val
== e
->values
[mux
])
2514 ucontrol
->value
.enumerated
.item
[0] = mux
;
2515 if (e
->shift_l
!= e
->shift_r
) {
2516 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
2517 for (mux
= 0; mux
< e
->max
; mux
++) {
2518 if (val
== e
->values
[mux
])
2521 ucontrol
->value
.enumerated
.item
[1] = mux
;
2526 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double
);
2529 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2531 * @kcontrol: mixer control
2532 * @ucontrol: control element information
2534 * Callback to set the value of a dapm semi enumerated double mixer control.
2536 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2537 * used for handling bitfield coded enumeration for example.
2539 * Returns 0 for success.
2541 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
2542 struct snd_ctl_elem_value
*ucontrol
)
2544 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2545 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2546 struct snd_soc_codec
*codec
= widget
->codec
;
2547 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2548 unsigned int val
, mux
, change
;
2550 struct snd_soc_dapm_update update
;
2553 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2555 mux
= ucontrol
->value
.enumerated
.item
[0];
2556 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
2557 mask
= e
->mask
<< e
->shift_l
;
2558 if (e
->shift_l
!= e
->shift_r
) {
2559 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2561 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
2562 mask
|= e
->mask
<< e
->shift_r
;
2565 mutex_lock(&codec
->mutex
);
2567 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
2569 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2570 widget
= wlist
->widgets
[wi
];
2572 widget
->value
= val
;
2574 update
.kcontrol
= kcontrol
;
2575 update
.widget
= widget
;
2576 update
.reg
= e
->reg
;
2579 widget
->dapm
->update
= &update
;
2581 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
2583 widget
->dapm
->update
= NULL
;
2587 mutex_unlock(&codec
->mutex
);
2590 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double
);
2593 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2595 * @kcontrol: mixer control
2596 * @uinfo: control element information
2598 * Callback to provide information about a pin switch control.
2600 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
2601 struct snd_ctl_elem_info
*uinfo
)
2603 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2605 uinfo
->value
.integer
.min
= 0;
2606 uinfo
->value
.integer
.max
= 1;
2610 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
2613 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2615 * @kcontrol: mixer control
2618 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
2619 struct snd_ctl_elem_value
*ucontrol
)
2621 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2622 const char *pin
= (const char *)kcontrol
->private_value
;
2624 mutex_lock(&codec
->mutex
);
2626 ucontrol
->value
.integer
.value
[0] =
2627 snd_soc_dapm_get_pin_status(&codec
->dapm
, pin
);
2629 mutex_unlock(&codec
->mutex
);
2633 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
2636 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2638 * @kcontrol: mixer control
2641 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
2642 struct snd_ctl_elem_value
*ucontrol
)
2644 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2645 const char *pin
= (const char *)kcontrol
->private_value
;
2647 mutex_lock(&codec
->mutex
);
2649 if (ucontrol
->value
.integer
.value
[0])
2650 snd_soc_dapm_enable_pin(&codec
->dapm
, pin
);
2652 snd_soc_dapm_disable_pin(&codec
->dapm
, pin
);
2654 snd_soc_dapm_sync(&codec
->dapm
);
2656 mutex_unlock(&codec
->mutex
);
2660 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
2663 * snd_soc_dapm_new_control - create new dapm control
2664 * @dapm: DAPM context
2665 * @widget: widget template
2667 * Creates a new dapm control based upon the template.
2669 * Returns 0 for success else error.
2671 int snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
2672 const struct snd_soc_dapm_widget
*widget
)
2674 struct snd_soc_dapm_widget
*w
;
2677 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
2680 name_len
= strlen(widget
->name
) + 1;
2681 if (dapm
->codec
&& dapm
->codec
->name_prefix
)
2682 name_len
+= 1 + strlen(dapm
->codec
->name_prefix
);
2683 w
->name
= kmalloc(name_len
, GFP_KERNEL
);
2684 if (w
->name
== NULL
) {
2688 if (dapm
->codec
&& dapm
->codec
->name_prefix
)
2689 snprintf(w
->name
, name_len
, "%s %s",
2690 dapm
->codec
->name_prefix
, widget
->name
);
2692 snprintf(w
->name
, name_len
, "%s", widget
->name
);
2695 case snd_soc_dapm_switch
:
2696 case snd_soc_dapm_mixer
:
2697 case snd_soc_dapm_mixer_named_ctl
:
2698 w
->power_check
= dapm_generic_check_power
;
2700 case snd_soc_dapm_mux
:
2701 case snd_soc_dapm_virt_mux
:
2702 case snd_soc_dapm_value_mux
:
2703 w
->power_check
= dapm_generic_check_power
;
2705 case snd_soc_dapm_adc
:
2706 case snd_soc_dapm_aif_out
:
2707 w
->power_check
= dapm_adc_check_power
;
2709 case snd_soc_dapm_dac
:
2710 case snd_soc_dapm_aif_in
:
2711 w
->power_check
= dapm_dac_check_power
;
2713 case snd_soc_dapm_pga
:
2714 case snd_soc_dapm_out_drv
:
2715 case snd_soc_dapm_input
:
2716 case snd_soc_dapm_output
:
2717 case snd_soc_dapm_micbias
:
2718 case snd_soc_dapm_spk
:
2719 case snd_soc_dapm_hp
:
2720 case snd_soc_dapm_mic
:
2721 case snd_soc_dapm_line
:
2722 w
->power_check
= dapm_generic_check_power
;
2724 case snd_soc_dapm_supply
:
2725 w
->power_check
= dapm_supply_check_power
;
2728 w
->power_check
= dapm_always_on_check_power
;
2734 w
->codec
= dapm
->codec
;
2735 w
->platform
= dapm
->platform
;
2736 INIT_LIST_HEAD(&w
->sources
);
2737 INIT_LIST_HEAD(&w
->sinks
);
2738 INIT_LIST_HEAD(&w
->list
);
2739 INIT_LIST_HEAD(&w
->dirty
);
2740 list_add(&w
->list
, &dapm
->card
->widgets
);
2742 /* machine layer set ups unconnected pins and insertions */
2746 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
2749 * snd_soc_dapm_new_controls - create new dapm controls
2750 * @dapm: DAPM context
2751 * @widget: widget array
2752 * @num: number of widgets
2754 * Creates new DAPM controls based upon the templates.
2756 * Returns 0 for success else error.
2758 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context
*dapm
,
2759 const struct snd_soc_dapm_widget
*widget
,
2764 for (i
= 0; i
< num
; i
++) {
2765 ret
= snd_soc_dapm_new_control(dapm
, widget
);
2768 "ASoC: Failed to create DAPM control %s: %d\n",
2776 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
2778 static void soc_dapm_stream_event(struct snd_soc_dapm_context
*dapm
,
2779 const char *stream
, int event
)
2781 struct snd_soc_dapm_widget
*w
;
2783 list_for_each_entry(w
, &dapm
->card
->widgets
, list
)
2785 if (!w
->sname
|| w
->dapm
!= dapm
)
2787 dev_vdbg(w
->dapm
->dev
, "widget %s\n %s stream %s event %d\n",
2788 w
->name
, w
->sname
, stream
, event
);
2789 if (strstr(w
->sname
, stream
)) {
2790 dapm_mark_dirty(w
, "stream event");
2792 case SND_SOC_DAPM_STREAM_START
:
2795 case SND_SOC_DAPM_STREAM_STOP
:
2798 case SND_SOC_DAPM_STREAM_SUSPEND
:
2799 case SND_SOC_DAPM_STREAM_RESUME
:
2800 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
2801 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
2807 dapm_power_widgets(dapm
, event
);
2809 /* do we need to notify any clients that DAPM stream is complete */
2810 if (dapm
->stream_event
)
2811 dapm
->stream_event(dapm
, event
);
2815 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2816 * @rtd: PCM runtime data
2817 * @stream: stream name
2818 * @event: stream event
2820 * Sends a stream event to the dapm core. The core then makes any
2821 * necessary widget power changes.
2823 * Returns 0 for success else error.
2825 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
,
2826 const char *stream
, int event
)
2828 struct snd_soc_codec
*codec
= rtd
->codec
;
2833 mutex_lock(&codec
->mutex
);
2834 soc_dapm_stream_event(&codec
->dapm
, stream
, event
);
2835 mutex_unlock(&codec
->mutex
);
2840 * snd_soc_dapm_enable_pin - enable pin.
2841 * @dapm: DAPM context
2844 * Enables input/output pin and its parents or children widgets iff there is
2845 * a valid audio route and active audio stream.
2846 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2847 * do any widget power switching.
2849 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
2851 return snd_soc_dapm_set_pin(dapm
, pin
, 1);
2853 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
2856 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2857 * @dapm: DAPM context
2860 * Enables input/output pin regardless of any other state. This is
2861 * intended for use with microphone bias supplies used in microphone
2864 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2865 * do any widget power switching.
2867 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context
*dapm
,
2870 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
2873 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
2877 dev_dbg(w
->dapm
->dev
, "dapm: force enable pin %s\n", pin
);
2880 dapm_mark_dirty(w
, "force enable");
2884 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
2887 * snd_soc_dapm_disable_pin - disable pin.
2888 * @dapm: DAPM context
2891 * Disables input/output pin and its parents or children widgets.
2892 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2893 * do any widget power switching.
2895 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context
*dapm
,
2898 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
2900 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
2903 * snd_soc_dapm_nc_pin - permanently disable pin.
2904 * @dapm: DAPM context
2907 * Marks the specified pin as being not connected, disabling it along
2908 * any parent or child widgets. At present this is identical to
2909 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2910 * additional things such as disabling controls which only affect
2911 * paths through the pin.
2913 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2914 * do any widget power switching.
2916 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
2918 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
2920 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
2923 * snd_soc_dapm_get_pin_status - get audio pin status
2924 * @dapm: DAPM context
2925 * @pin: audio signal pin endpoint (or start point)
2927 * Get audio pin status - connected or disconnected.
2929 * Returns 1 for connected otherwise 0.
2931 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context
*dapm
,
2934 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
2937 return w
->connected
;
2941 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
2944 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2945 * @dapm: DAPM context
2946 * @pin: audio signal pin endpoint (or start point)
2948 * Mark the given endpoint or pin as ignoring suspend. When the
2949 * system is disabled a path between two endpoints flagged as ignoring
2950 * suspend will not be disabled. The path must already be enabled via
2951 * normal means at suspend time, it will not be turned on if it was not
2954 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context
*dapm
,
2957 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, false);
2960 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
2964 w
->ignore_suspend
= 1;
2968 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
2970 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card
*card
,
2971 struct snd_soc_dapm_widget
*w
)
2973 struct snd_soc_dapm_path
*p
;
2975 list_for_each_entry(p
, &card
->paths
, list
) {
2976 if ((p
->source
== w
) || (p
->sink
== w
)) {
2978 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
2979 p
->source
->name
, p
->source
->id
, p
->source
->dapm
,
2980 p
->sink
->name
, p
->sink
->id
, p
->sink
->dapm
);
2982 /* Connected to something other than the codec */
2983 if (p
->source
->dapm
!= p
->sink
->dapm
)
2986 * Loopback connection from codec external pin to
2987 * codec external pin
2989 if (p
->sink
->id
== snd_soc_dapm_input
) {
2990 switch (p
->source
->id
) {
2991 case snd_soc_dapm_output
:
2992 case snd_soc_dapm_micbias
:
3005 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3006 * @codec: The codec whose pins should be processed
3008 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3009 * which are unused. Pins are used if they are connected externally to the
3010 * codec, whether that be to some other device, or a loop-back connection to
3013 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec
*codec
)
3015 struct snd_soc_card
*card
= codec
->card
;
3016 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
3017 struct snd_soc_dapm_widget
*w
;
3019 dev_dbg(codec
->dev
, "Auto NC: DAPMs: card:%p codec:%p\n",
3020 &card
->dapm
, &codec
->dapm
);
3022 list_for_each_entry(w
, &card
->widgets
, list
) {
3023 if (w
->dapm
!= dapm
)
3026 case snd_soc_dapm_input
:
3027 case snd_soc_dapm_output
:
3028 case snd_soc_dapm_micbias
:
3029 dev_dbg(codec
->dev
, "Auto NC: Checking widget %s\n",
3031 if (!snd_soc_dapm_widget_in_card_paths(card
, w
)) {
3033 "... Not in map; disabling\n");
3034 snd_soc_dapm_nc_pin(dapm
, w
->name
);
3044 * snd_soc_dapm_free - free dapm resources
3045 * @dapm: DAPM context
3047 * Free all dapm widgets and resources.
3049 void snd_soc_dapm_free(struct snd_soc_dapm_context
*dapm
)
3051 snd_soc_dapm_sys_remove(dapm
->dev
);
3052 dapm_debugfs_cleanup(dapm
);
3053 dapm_free_widgets(dapm
);
3054 list_del(&dapm
->list
);
3056 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
3058 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context
*dapm
)
3060 struct snd_soc_dapm_widget
*w
;
3061 LIST_HEAD(down_list
);
3064 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
3065 if (w
->dapm
!= dapm
)
3068 dapm_seq_insert(w
, &down_list
, false);
3074 /* If there were no widgets to power down we're already in
3078 snd_soc_dapm_set_bias_level(dapm
, SND_SOC_BIAS_PREPARE
);
3079 dapm_seq_run(dapm
, &down_list
, 0, false);
3080 snd_soc_dapm_set_bias_level(dapm
, SND_SOC_BIAS_STANDBY
);
3085 * snd_soc_dapm_shutdown - callback for system shutdown
3087 void snd_soc_dapm_shutdown(struct snd_soc_card
*card
)
3089 struct snd_soc_codec
*codec
;
3091 list_for_each_entry(codec
, &card
->codec_dev_list
, list
) {
3092 soc_dapm_shutdown_codec(&codec
->dapm
);
3093 snd_soc_dapm_set_bias_level(&codec
->dapm
, SND_SOC_BIAS_OFF
);
3097 /* Module information */
3098 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3099 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3100 MODULE_LICENSE("GPL");