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/delay.h>
37 #include <linux/bitops.h>
38 #include <linux/platform_device.h>
39 #include <linux/jiffies.h>
40 #include <linux/debugfs.h>
41 #include <linux/slab.h>
42 #include <sound/core.h>
43 #include <sound/pcm.h>
44 #include <sound/pcm_params.h>
45 #include <sound/soc.h>
46 #include <sound/soc-dapm.h>
47 #include <sound/initval.h>
49 /* dapm power sequences - make this per codec in the future */
50 static int dapm_up_seq
[] = {
51 [snd_soc_dapm_pre
] = 0,
52 [snd_soc_dapm_supply
] = 1,
53 [snd_soc_dapm_micbias
] = 2,
54 [snd_soc_dapm_aif_in
] = 3,
55 [snd_soc_dapm_aif_out
] = 3,
56 [snd_soc_dapm_mic
] = 4,
57 [snd_soc_dapm_mux
] = 5,
58 [snd_soc_dapm_value_mux
] = 5,
59 [snd_soc_dapm_dac
] = 6,
60 [snd_soc_dapm_mixer
] = 7,
61 [snd_soc_dapm_mixer_named_ctl
] = 7,
62 [snd_soc_dapm_pga
] = 8,
63 [snd_soc_dapm_adc
] = 9,
64 [snd_soc_dapm_hp
] = 10,
65 [snd_soc_dapm_spk
] = 10,
66 [snd_soc_dapm_post
] = 11,
69 static int dapm_down_seq
[] = {
70 [snd_soc_dapm_pre
] = 0,
71 [snd_soc_dapm_adc
] = 1,
72 [snd_soc_dapm_hp
] = 2,
73 [snd_soc_dapm_spk
] = 2,
74 [snd_soc_dapm_pga
] = 4,
75 [snd_soc_dapm_mixer_named_ctl
] = 5,
76 [snd_soc_dapm_mixer
] = 5,
77 [snd_soc_dapm_dac
] = 6,
78 [snd_soc_dapm_mic
] = 7,
79 [snd_soc_dapm_micbias
] = 8,
80 [snd_soc_dapm_mux
] = 9,
81 [snd_soc_dapm_value_mux
] = 9,
82 [snd_soc_dapm_aif_in
] = 10,
83 [snd_soc_dapm_aif_out
] = 10,
84 [snd_soc_dapm_supply
] = 11,
85 [snd_soc_dapm_post
] = 12,
88 static void pop_wait(u32 pop_time
)
91 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time
));
94 static void pop_dbg(struct device
*dev
, u32 pop_time
, const char *fmt
, ...)
102 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
107 vsnprintf(buf
, PAGE_SIZE
, fmt
, args
);
114 /* create a new dapm widget */
115 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
116 const struct snd_soc_dapm_widget
*_widget
)
118 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
122 * snd_soc_dapm_set_bias_level - set the bias level for the system
123 * @card: audio device
124 * @level: level to configure
126 * Configure the bias (power) levels for the SoC audio device.
128 * Returns 0 for success else error.
130 static int snd_soc_dapm_set_bias_level(struct snd_soc_card
*card
,
131 struct snd_soc_dapm_context
*dapm
,
132 enum snd_soc_bias_level level
)
137 case SND_SOC_BIAS_ON
:
138 dev_dbg(dapm
->dev
, "Setting full bias\n");
140 case SND_SOC_BIAS_PREPARE
:
141 dev_dbg(dapm
->dev
, "Setting bias prepare\n");
143 case SND_SOC_BIAS_STANDBY
:
144 dev_dbg(dapm
->dev
, "Setting standby bias\n");
146 case SND_SOC_BIAS_OFF
:
147 dev_dbg(dapm
->dev
, "Setting bias off\n");
150 dev_err(dapm
->dev
, "Setting invalid bias %d\n", level
);
154 if (card
&& card
->set_bias_level
)
155 ret
= card
->set_bias_level(card
, level
);
157 if (dapm
->codec
&& dapm
->codec
->driver
->set_bias_level
)
158 ret
= dapm
->codec
->driver
->set_bias_level(dapm
->codec
, level
);
160 dapm
->bias_level
= level
;
166 /* set up initial codec paths */
167 static void dapm_set_path_status(struct snd_soc_dapm_widget
*w
,
168 struct snd_soc_dapm_path
*p
, int i
)
171 case snd_soc_dapm_switch
:
172 case snd_soc_dapm_mixer
:
173 case snd_soc_dapm_mixer_named_ctl
: {
175 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
176 w
->kcontrols
[i
].private_value
;
177 unsigned int reg
= mc
->reg
;
178 unsigned int shift
= mc
->shift
;
180 unsigned int mask
= (1 << fls(max
)) - 1;
181 unsigned int invert
= mc
->invert
;
183 val
= snd_soc_read(w
->codec
, reg
);
184 val
= (val
>> shift
) & mask
;
186 if ((invert
&& !val
) || (!invert
&& val
))
192 case snd_soc_dapm_mux
: {
193 struct soc_enum
*e
= (struct soc_enum
*)w
->kcontrols
[i
].private_value
;
194 int val
, item
, bitmask
;
196 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
198 val
= snd_soc_read(w
->codec
, e
->reg
);
199 item
= (val
>> e
->shift_l
) & (bitmask
- 1);
202 for (i
= 0; i
< e
->max
; i
++) {
203 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
208 case snd_soc_dapm_value_mux
: {
209 struct soc_enum
*e
= (struct soc_enum
*)
210 w
->kcontrols
[i
].private_value
;
213 val
= snd_soc_read(w
->codec
, e
->reg
);
214 val
= (val
>> e
->shift_l
) & e
->mask
;
215 for (item
= 0; item
< e
->max
; item
++) {
216 if (val
== e
->values
[item
])
221 for (i
= 0; i
< e
->max
; i
++) {
222 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
227 /* does not effect routing - always connected */
228 case snd_soc_dapm_pga
:
229 case snd_soc_dapm_output
:
230 case snd_soc_dapm_adc
:
231 case snd_soc_dapm_input
:
232 case snd_soc_dapm_dac
:
233 case snd_soc_dapm_micbias
:
234 case snd_soc_dapm_vmid
:
235 case snd_soc_dapm_supply
:
236 case snd_soc_dapm_aif_in
:
237 case snd_soc_dapm_aif_out
:
240 /* does effect routing - dynamically connected */
241 case snd_soc_dapm_hp
:
242 case snd_soc_dapm_mic
:
243 case snd_soc_dapm_spk
:
244 case snd_soc_dapm_line
:
245 case snd_soc_dapm_pre
:
246 case snd_soc_dapm_post
:
252 /* connect mux widget to its interconnecting audio paths */
253 static int dapm_connect_mux(struct snd_soc_dapm_context
*dapm
,
254 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
255 struct snd_soc_dapm_path
*path
, const char *control_name
,
256 const struct snd_kcontrol_new
*kcontrol
)
258 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
261 for (i
= 0; i
< e
->max
; i
++) {
262 if (!(strcmp(control_name
, e
->texts
[i
]))) {
263 list_add(&path
->list
, &dapm
->paths
);
264 list_add(&path
->list_sink
, &dest
->sources
);
265 list_add(&path
->list_source
, &src
->sinks
);
266 path
->name
= (char*)e
->texts
[i
];
267 dapm_set_path_status(dest
, path
, 0);
275 /* connect mixer widget to its interconnecting audio paths */
276 static int dapm_connect_mixer(struct snd_soc_dapm_context
*dapm
,
277 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
278 struct snd_soc_dapm_path
*path
, const char *control_name
)
282 /* search for mixer kcontrol */
283 for (i
= 0; i
< dest
->num_kcontrols
; i
++) {
284 if (!strcmp(control_name
, dest
->kcontrols
[i
].name
)) {
285 list_add(&path
->list
, &dapm
->paths
);
286 list_add(&path
->list_sink
, &dest
->sources
);
287 list_add(&path
->list_source
, &src
->sinks
);
288 path
->name
= dest
->kcontrols
[i
].name
;
289 dapm_set_path_status(dest
, path
, i
);
296 /* update dapm codec register bits */
297 static int dapm_update_bits(struct snd_soc_dapm_widget
*widget
)
300 unsigned int old
, new;
301 struct snd_soc_codec
*codec
= widget
->codec
;
302 struct snd_soc_dapm_context
*dapm
= widget
->dapm
;
303 struct snd_soc_card
*card
= dapm
->card
;
305 /* check for valid widgets */
306 if (widget
->reg
< 0 || widget
->id
== snd_soc_dapm_input
||
307 widget
->id
== snd_soc_dapm_output
||
308 widget
->id
== snd_soc_dapm_hp
||
309 widget
->id
== snd_soc_dapm_mic
||
310 widget
->id
== snd_soc_dapm_line
||
311 widget
->id
== snd_soc_dapm_spk
)
314 power
= widget
->power
;
316 power
= (power
? 0:1);
318 old
= snd_soc_read(codec
, widget
->reg
);
319 new = (old
& ~(0x1 << widget
->shift
)) | (power
<< widget
->shift
);
323 pop_dbg(dapm
->dev
, card
->pop_time
,
324 "pop test %s : %s in %d ms\n",
325 widget
->name
, widget
->power
? "on" : "off",
327 pop_wait(card
->pop_time
);
328 snd_soc_write(codec
, widget
->reg
, new);
330 dev_dbg(dapm
->dev
, "reg %x old %x new %x change %d\n", widget
->reg
,
335 /* create new dapm mixer control */
336 static int dapm_new_mixer(struct snd_soc_dapm_context
*dapm
,
337 struct snd_soc_dapm_widget
*w
)
341 struct snd_soc_dapm_path
*path
;
342 struct snd_card
*card
= dapm
->codec
->card
->snd_card
;
345 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
348 list_for_each_entry(path
, &w
->sources
, list_sink
) {
350 /* mixer/mux paths name must match control name */
351 if (path
->name
!= (char*)w
->kcontrols
[i
].name
)
354 /* add dapm control with long name.
355 * for dapm_mixer this is the concatenation of the
356 * mixer and kcontrol name.
357 * for dapm_mixer_named_ctl this is simply the
360 name_len
= strlen(w
->kcontrols
[i
].name
) + 1;
361 if (w
->id
!= snd_soc_dapm_mixer_named_ctl
)
362 name_len
+= 1 + strlen(w
->name
);
364 path
->long_name
= kmalloc(name_len
, GFP_KERNEL
);
366 if (path
->long_name
== NULL
)
371 snprintf(path
->long_name
, name_len
, "%s %s",
372 w
->name
, w
->kcontrols
[i
].name
);
374 case snd_soc_dapm_mixer_named_ctl
:
375 snprintf(path
->long_name
, name_len
, "%s",
376 w
->kcontrols
[i
].name
);
380 path
->long_name
[name_len
- 1] = '\0';
382 path
->kcontrol
= snd_soc_cnew(&w
->kcontrols
[i
], w
,
384 ret
= snd_ctl_add(card
, path
->kcontrol
);
387 "asoc: failed to add dapm kcontrol %s: %d\n",
388 path
->long_name
, ret
);
389 kfree(path
->long_name
);
390 path
->long_name
= NULL
;
398 /* create new dapm mux control */
399 static int dapm_new_mux(struct snd_soc_dapm_context
*dapm
,
400 struct snd_soc_dapm_widget
*w
)
402 struct snd_soc_dapm_path
*path
= NULL
;
403 struct snd_kcontrol
*kcontrol
;
404 struct snd_card
*card
= dapm
->codec
->card
->snd_card
;
407 if (!w
->num_kcontrols
) {
408 dev_err(dapm
->dev
, "asoc: mux %s has no controls\n", w
->name
);
412 kcontrol
= snd_soc_cnew(&w
->kcontrols
[0], w
, w
->name
);
413 ret
= snd_ctl_add(card
, kcontrol
);
418 list_for_each_entry(path
, &w
->sources
, list_sink
)
419 path
->kcontrol
= kcontrol
;
424 dev_err(dapm
->dev
, "asoc: failed to add kcontrol %s\n", w
->name
);
428 /* create new dapm volume control */
429 static int dapm_new_pga(struct snd_soc_dapm_context
*dapm
,
430 struct snd_soc_dapm_widget
*w
)
432 if (w
->num_kcontrols
)
433 dev_err(w
->dapm
->dev
,
434 "asoc: PGA controls not supported: '%s'\n", w
->name
);
439 /* reset 'walked' bit for each dapm path */
440 static inline void dapm_clear_walk(struct snd_soc_dapm_context
*dapm
)
442 struct snd_soc_dapm_path
*p
;
444 list_for_each_entry(p
, &dapm
->paths
, list
)
448 /* We implement power down on suspend by checking the power state of
449 * the ALSA card - when we are suspending the ALSA state for the card
452 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
454 int level
= snd_power_get_state(widget
->dapm
->codec
->card
->snd_card
);
457 case SNDRV_CTL_POWER_D3hot
:
458 case SNDRV_CTL_POWER_D3cold
:
459 if (widget
->ignore_suspend
)
460 dev_dbg(widget
->dapm
->dev
, "%s ignoring suspend\n",
462 return widget
->ignore_suspend
;
469 * Recursively check for a completed path to an active or physically connected
470 * output widget. Returns number of complete paths.
472 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
)
474 struct snd_soc_dapm_path
*path
;
477 if (widget
->id
== snd_soc_dapm_supply
)
480 switch (widget
->id
) {
481 case snd_soc_dapm_adc
:
482 case snd_soc_dapm_aif_out
:
484 return snd_soc_dapm_suspend_check(widget
);
489 if (widget
->connected
) {
490 /* connected pin ? */
491 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
)
492 return snd_soc_dapm_suspend_check(widget
);
494 /* connected jack or spk ? */
495 if (widget
->id
== snd_soc_dapm_hp
|| widget
->id
== snd_soc_dapm_spk
||
496 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sources
)))
497 return snd_soc_dapm_suspend_check(widget
);
500 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
504 if (path
->sink
&& path
->connect
) {
506 con
+= is_connected_output_ep(path
->sink
);
514 * Recursively check for a completed path to an active or physically connected
515 * input widget. Returns number of complete paths.
517 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
)
519 struct snd_soc_dapm_path
*path
;
522 if (widget
->id
== snd_soc_dapm_supply
)
525 /* active stream ? */
526 switch (widget
->id
) {
527 case snd_soc_dapm_dac
:
528 case snd_soc_dapm_aif_in
:
530 return snd_soc_dapm_suspend_check(widget
);
535 if (widget
->connected
) {
536 /* connected pin ? */
537 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
)
538 return snd_soc_dapm_suspend_check(widget
);
540 /* connected VMID/Bias for lower pops */
541 if (widget
->id
== snd_soc_dapm_vmid
)
542 return snd_soc_dapm_suspend_check(widget
);
544 /* connected jack ? */
545 if (widget
->id
== snd_soc_dapm_mic
||
546 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sinks
)))
547 return snd_soc_dapm_suspend_check(widget
);
550 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
554 if (path
->source
&& path
->connect
) {
556 con
+= is_connected_input_ep(path
->source
);
564 * Handler for generic register modifier widget.
566 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
567 struct snd_kcontrol
*kcontrol
, int event
)
571 if (SND_SOC_DAPM_EVENT_ON(event
))
576 snd_soc_update_bits(w
->codec
, -(w
->reg
+ 1),
577 w
->mask
<< w
->shift
, val
<< w
->shift
);
581 EXPORT_SYMBOL_GPL(dapm_reg_event
);
583 /* Standard power change method, used to apply power changes to most
586 static int dapm_generic_apply_power(struct snd_soc_dapm_widget
*w
)
590 /* call any power change event handlers */
592 dev_dbg(w
->dapm
->dev
, "power %s event for %s flags %x\n",
593 w
->power
? "on" : "off",
594 w
->name
, w
->event_flags
);
596 /* power up pre event */
597 if (w
->power
&& w
->event
&&
598 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
599 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
604 /* power down pre event */
605 if (!w
->power
&& w
->event
&&
606 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
607 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
614 /* power up post event */
615 if (w
->power
&& w
->event
&&
616 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
618 NULL
, SND_SOC_DAPM_POST_PMU
);
623 /* power down post event */
624 if (!w
->power
&& w
->event
&&
625 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
626 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
634 /* Generic check to see if a widget should be powered.
636 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
640 in
= is_connected_input_ep(w
);
641 dapm_clear_walk(w
->dapm
);
642 out
= is_connected_output_ep(w
);
643 dapm_clear_walk(w
->dapm
);
644 return out
!= 0 && in
!= 0;
647 /* Check to see if an ADC has power */
648 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
653 in
= is_connected_input_ep(w
);
654 dapm_clear_walk(w
->dapm
);
657 return dapm_generic_check_power(w
);
661 /* Check to see if a DAC has power */
662 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
667 out
= is_connected_output_ep(w
);
668 dapm_clear_walk(w
->dapm
);
671 return dapm_generic_check_power(w
);
675 /* Check to see if a power supply is needed */
676 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
678 struct snd_soc_dapm_path
*path
;
681 /* Check if one of our outputs is connected */
682 list_for_each_entry(path
, &w
->sinks
, list_source
) {
683 if (path
->connected
&&
684 !path
->connected(path
->source
, path
->sink
))
687 if (path
->sink
&& path
->sink
->power_check
&&
688 path
->sink
->power_check(path
->sink
)) {
694 dapm_clear_walk(w
->dapm
);
699 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
700 struct snd_soc_dapm_widget
*b
,
703 if (a
->codec
!= b
->codec
)
704 return (unsigned long)a
- (unsigned long)b
;
705 if (sort
[a
->id
] != sort
[b
->id
])
706 return sort
[a
->id
] - sort
[b
->id
];
707 if (a
->reg
!= b
->reg
)
708 return a
->reg
- b
->reg
;
713 /* Insert a widget in order into a DAPM power sequence. */
714 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
715 struct list_head
*list
,
718 struct snd_soc_dapm_widget
*w
;
720 list_for_each_entry(w
, list
, power_list
)
721 if (dapm_seq_compare(new_widget
, w
, sort
) < 0) {
722 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
726 list_add_tail(&new_widget
->power_list
, list
);
729 /* Apply the coalesced changes from a DAPM sequence */
730 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context
*dapm
,
731 struct list_head
*pending
)
733 struct snd_soc_dapm_widget
*w
;
734 struct snd_soc_card
*card
= dapm
->card
;
736 unsigned int value
= 0;
737 unsigned int mask
= 0;
738 unsigned int cur_mask
;
740 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
743 list_for_each_entry(w
, pending
, power_list
) {
744 cur_mask
= 1 << w
->shift
;
745 BUG_ON(reg
!= w
->reg
);
756 pop_dbg(dapm
->dev
, card
->pop_time
,
757 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
758 w
->name
, reg
, value
, mask
);
760 /* power up pre event */
761 if (w
->power
&& w
->event
&&
762 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
763 pop_dbg(dapm
->dev
, card
->pop_time
,
764 "pop test : %s PRE_PMU\n", w
->name
);
765 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
768 "%s: pre event failed: %d\n",
772 /* power down pre event */
773 if (!w
->power
&& w
->event
&&
774 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
775 pop_dbg(dapm
->dev
, card
->pop_time
,
776 "pop test : %s PRE_PMD\n", w
->name
);
777 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
780 "%s: pre event failed: %d\n",
786 pop_dbg(dapm
->dev
, card
->pop_time
,
787 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
788 value
, mask
, reg
, card
->pop_time
);
789 pop_wait(card
->pop_time
);
790 snd_soc_update_bits(dapm
->codec
, reg
, mask
, value
);
793 list_for_each_entry(w
, pending
, power_list
) {
794 /* power up post event */
795 if (w
->power
&& w
->event
&&
796 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
797 pop_dbg(dapm
->dev
, card
->pop_time
,
798 "pop test : %s POST_PMU\n", w
->name
);
800 NULL
, SND_SOC_DAPM_POST_PMU
);
803 "%s: post event failed: %d\n",
807 /* power down post event */
808 if (!w
->power
&& w
->event
&&
809 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
810 pop_dbg(dapm
->dev
, card
->pop_time
,
811 "pop test : %s POST_PMD\n", w
->name
);
812 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
815 "%s: post event failed: %d\n",
821 /* Apply a DAPM power sequence.
823 * We walk over a pre-sorted list of widgets to apply power to. In
824 * order to minimise the number of writes to the device required
825 * multiple widgets will be updated in a single write where possible.
826 * Currently anything that requires more than a single write is not
829 static void dapm_seq_run(struct snd_soc_dapm_context
*dapm
,
830 struct list_head
*list
, int event
, int sort
[])
832 struct snd_soc_dapm_widget
*w
, *n
;
835 int cur_reg
= SND_SOC_NOPM
;
838 list_for_each_entry_safe(w
, n
, list
, power_list
) {
841 /* Do we need to apply any queued changes? */
842 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
) {
843 if (!list_empty(&pending
))
844 dapm_seq_run_coalesced(dapm
, &pending
);
846 INIT_LIST_HEAD(&pending
);
848 cur_reg
= SND_SOC_NOPM
;
852 case snd_soc_dapm_pre
:
854 list_for_each_entry_safe_continue(w
, n
, list
,
857 if (event
== SND_SOC_DAPM_STREAM_START
)
859 NULL
, SND_SOC_DAPM_PRE_PMU
);
860 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
862 NULL
, SND_SOC_DAPM_PRE_PMD
);
865 case snd_soc_dapm_post
:
867 list_for_each_entry_safe_continue(w
, n
, list
,
870 if (event
== SND_SOC_DAPM_STREAM_START
)
872 NULL
, SND_SOC_DAPM_POST_PMU
);
873 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
875 NULL
, SND_SOC_DAPM_POST_PMD
);
878 case snd_soc_dapm_input
:
879 case snd_soc_dapm_output
:
880 case snd_soc_dapm_hp
:
881 case snd_soc_dapm_mic
:
882 case snd_soc_dapm_line
:
883 case snd_soc_dapm_spk
:
884 /* No register support currently */
885 ret
= dapm_generic_apply_power(w
);
889 /* Queue it up for application */
890 cur_sort
= sort
[w
->id
];
892 list_move(&w
->power_list
, &pending
);
897 dev_err(w
->dapm
->dev
,
898 "Failed to apply widget power: %d\n", ret
);
901 if (!list_empty(&pending
))
902 dapm_seq_run_coalesced(dapm
, &pending
);
906 * Scan each dapm widget for complete audio path.
907 * A complete path is a route that has valid endpoints i.e.:-
909 * o DAC to output pin.
910 * o Input Pin to ADC.
911 * o Input pin to Output pin (bypass, sidetone)
912 * o DAC to ADC (loopback).
914 static int dapm_power_widgets(struct snd_soc_dapm_context
*dapm
, int event
)
916 struct snd_soc_card
*card
= dapm
->codec
->card
;
917 struct snd_soc_dapm_widget
*w
;
919 LIST_HEAD(down_list
);
924 /* Check which widgets we need to power and store them in
925 * lists indicating if they should be powered up or down.
927 list_for_each_entry(w
, &dapm
->widgets
, list
) {
929 case snd_soc_dapm_pre
:
930 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
932 case snd_soc_dapm_post
:
933 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
941 power
= w
->power_check(w
);
947 if (w
->power
== power
)
951 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
953 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
960 /* If there are no DAPM widgets then try to figure out power from the
963 if (list_empty(&dapm
->widgets
)) {
965 case SND_SOC_DAPM_STREAM_START
:
966 case SND_SOC_DAPM_STREAM_RESUME
:
969 case SND_SOC_DAPM_STREAM_SUSPEND
:
972 case SND_SOC_DAPM_STREAM_NOP
:
973 switch (dapm
->bias_level
) {
974 case SND_SOC_BIAS_STANDBY
:
975 case SND_SOC_BIAS_OFF
:
988 if (sys_power
&& dapm
->bias_level
== SND_SOC_BIAS_OFF
) {
989 ret
= snd_soc_dapm_set_bias_level(card
, dapm
,
990 SND_SOC_BIAS_STANDBY
);
993 "Failed to turn on bias: %d\n", ret
);
996 /* If we're changing to all on or all off then prepare */
997 if ((sys_power
&& dapm
->bias_level
== SND_SOC_BIAS_STANDBY
) ||
998 (!sys_power
&& dapm
->bias_level
== SND_SOC_BIAS_ON
)) {
999 ret
= snd_soc_dapm_set_bias_level(card
, dapm
, SND_SOC_BIAS_PREPARE
);
1002 "Failed to prepare bias: %d\n", ret
);
1005 /* Power down widgets first; try to avoid amplifying pops. */
1006 dapm_seq_run(dapm
, &down_list
, event
, dapm_down_seq
);
1009 dapm_seq_run(dapm
, &up_list
, event
, dapm_up_seq
);
1011 /* If we just powered the last thing off drop to standby bias */
1012 if (dapm
->bias_level
== SND_SOC_BIAS_PREPARE
&& !sys_power
) {
1013 ret
= snd_soc_dapm_set_bias_level(card
, dapm
, SND_SOC_BIAS_STANDBY
);
1016 "Failed to apply standby bias: %d\n", ret
);
1019 /* If we're in standby and can support bias off then do that */
1020 if (dapm
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1021 dapm
->idle_bias_off
) {
1022 ret
= snd_soc_dapm_set_bias_level(card
, dapm
, SND_SOC_BIAS_OFF
);
1025 "Failed to turn off bias: %d\n", ret
);
1028 /* If we just powered up then move to active bias */
1029 if (dapm
->bias_level
== SND_SOC_BIAS_PREPARE
&& sys_power
) {
1030 ret
= snd_soc_dapm_set_bias_level(card
, dapm
, SND_SOC_BIAS_ON
);
1033 "Failed to apply active bias: %d\n", ret
);
1036 pop_dbg(dapm
->dev
, card
->pop_time
,
1037 "DAPM sequencing finished, waiting %dms\n", card
->pop_time
);
1038 pop_wait(card
->pop_time
);
1043 #ifdef CONFIG_DEBUG_FS
1044 static int dapm_widget_power_open_file(struct inode
*inode
, struct file
*file
)
1046 file
->private_data
= inode
->i_private
;
1050 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1051 char __user
*user_buf
,
1052 size_t count
, loff_t
*ppos
)
1054 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1058 struct snd_soc_dapm_path
*p
= NULL
;
1060 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1064 in
= is_connected_input_ep(w
);
1065 dapm_clear_walk(w
->dapm
);
1066 out
= is_connected_output_ep(w
);
1067 dapm_clear_walk(w
->dapm
);
1069 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s in %d out %d",
1070 w
->name
, w
->power
? "On" : "Off", in
, out
);
1073 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1074 " - R%d(0x%x) bit %d",
1075 w
->reg
, w
->reg
, w
->shift
);
1077 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
1080 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1082 w
->active
? "active" : "inactive");
1084 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1085 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1089 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1091 p
->name
? p
->name
: "static",
1094 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1095 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1099 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1101 p
->name
? p
->name
: "static",
1105 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1111 static const struct file_operations dapm_widget_power_fops
= {
1112 .open
= dapm_widget_power_open_file
,
1113 .read
= dapm_widget_power_read_file
,
1114 .llseek
= default_llseek
,
1117 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
)
1119 struct snd_soc_dapm_widget
*w
;
1122 if (!dapm
->debugfs_dapm
)
1125 list_for_each_entry(w
, &dapm
->widgets
, list
) {
1129 d
= debugfs_create_file(w
->name
, 0444,
1130 dapm
->debugfs_dapm
, w
,
1131 &dapm_widget_power_fops
);
1133 dev_warn(w
->dapm
->dev
,
1134 "ASoC: Failed to create %s debugfs file\n",
1139 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
)
1144 /* test and update the power status of a mux widget */
1145 static int dapm_mux_update_power(struct snd_soc_dapm_widget
*widget
,
1146 struct snd_kcontrol
*kcontrol
, int change
,
1147 int mux
, struct soc_enum
*e
)
1149 struct snd_soc_dapm_path
*path
;
1152 if (widget
->id
!= snd_soc_dapm_mux
&&
1153 widget
->id
!= snd_soc_dapm_value_mux
)
1159 /* find dapm widget path assoc with kcontrol */
1160 list_for_each_entry(path
, &widget
->dapm
->paths
, list
) {
1161 if (path
->kcontrol
!= kcontrol
)
1164 if (!path
->name
|| !e
->texts
[mux
])
1168 /* we now need to match the string in the enum to the path */
1169 if (!(strcmp(path
->name
, e
->texts
[mux
])))
1170 path
->connect
= 1; /* new connection */
1172 path
->connect
= 0; /* old connection must be powered down */
1176 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1181 /* test and update the power status of a mixer or switch widget */
1182 static int dapm_mixer_update_power(struct snd_soc_dapm_widget
*widget
,
1183 struct snd_kcontrol
*kcontrol
, int connect
)
1185 struct snd_soc_dapm_path
*path
;
1188 if (widget
->id
!= snd_soc_dapm_mixer
&&
1189 widget
->id
!= snd_soc_dapm_mixer_named_ctl
&&
1190 widget
->id
!= snd_soc_dapm_switch
)
1193 /* find dapm widget path assoc with kcontrol */
1194 list_for_each_entry(path
, &widget
->dapm
->paths
, list
) {
1195 if (path
->kcontrol
!= kcontrol
)
1198 /* found, now check type */
1200 path
->connect
= connect
;
1205 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1210 /* show dapm widget status in sys fs */
1211 static ssize_t
dapm_widget_show(struct device
*dev
,
1212 struct device_attribute
*attr
, char *buf
)
1214 struct snd_soc_pcm_runtime
*rtd
=
1215 container_of(dev
, struct snd_soc_pcm_runtime
, dev
);
1216 struct snd_soc_codec
*codec
=rtd
->codec
;
1217 struct snd_soc_dapm_widget
*w
;
1219 char *state
= "not set";
1221 list_for_each_entry(w
, &codec
->dapm
.widgets
, list
) {
1223 /* only display widgets that burnm power */
1225 case snd_soc_dapm_hp
:
1226 case snd_soc_dapm_mic
:
1227 case snd_soc_dapm_spk
:
1228 case snd_soc_dapm_line
:
1229 case snd_soc_dapm_micbias
:
1230 case snd_soc_dapm_dac
:
1231 case snd_soc_dapm_adc
:
1232 case snd_soc_dapm_pga
:
1233 case snd_soc_dapm_mixer
:
1234 case snd_soc_dapm_mixer_named_ctl
:
1235 case snd_soc_dapm_supply
:
1237 count
+= sprintf(buf
+ count
, "%s: %s\n",
1238 w
->name
, w
->power
? "On":"Off");
1245 switch (codec
->dapm
.bias_level
) {
1246 case SND_SOC_BIAS_ON
:
1249 case SND_SOC_BIAS_PREPARE
:
1252 case SND_SOC_BIAS_STANDBY
:
1255 case SND_SOC_BIAS_OFF
:
1259 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
1264 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
1266 int snd_soc_dapm_sys_add(struct device
*dev
)
1268 return device_create_file(dev
, &dev_attr_dapm_widget
);
1271 static void snd_soc_dapm_sys_remove(struct device
*dev
)
1273 device_remove_file(dev
, &dev_attr_dapm_widget
);
1276 /* free all dapm widgets and resources */
1277 static void dapm_free_widgets(struct snd_soc_dapm_context
*dapm
)
1279 struct snd_soc_dapm_widget
*w
, *next_w
;
1280 struct snd_soc_dapm_path
*p
, *next_p
;
1282 list_for_each_entry_safe(w
, next_w
, &dapm
->widgets
, list
) {
1287 list_for_each_entry_safe(p
, next_p
, &dapm
->paths
, list
) {
1289 kfree(p
->long_name
);
1294 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context
*dapm
,
1295 const char *pin
, int status
)
1297 struct snd_soc_dapm_widget
*w
;
1299 list_for_each_entry(w
, &dapm
->widgets
, list
) {
1300 if (!strcmp(w
->name
, pin
)) {
1301 dev_dbg(w
->dapm
->dev
, "dapm: pin %s = %d\n",
1303 w
->connected
= status
;
1304 /* Allow disabling of forced pins */
1311 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
1316 * snd_soc_dapm_sync - scan and power dapm paths
1317 * @dapm: DAPM context
1319 * Walks all dapm audio paths and powers widgets according to their
1320 * stream or path usage.
1322 * Returns 0 for success.
1324 int snd_soc_dapm_sync(struct snd_soc_dapm_context
*dapm
)
1326 return dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
1328 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
1330 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context
*dapm
,
1331 const struct snd_soc_dapm_route
*route
)
1333 struct snd_soc_dapm_path
*path
;
1334 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
1335 const char *sink
= route
->sink
;
1336 const char *control
= route
->control
;
1337 const char *source
= route
->source
;
1340 /* find src and dest widgets */
1341 list_for_each_entry(w
, &dapm
->widgets
, list
) {
1343 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
1347 if (!wsource
&& !(strcmp(w
->name
, source
))) {
1352 if (wsource
== NULL
|| wsink
== NULL
)
1355 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
1359 path
->source
= wsource
;
1361 path
->connected
= route
->connected
;
1362 INIT_LIST_HEAD(&path
->list
);
1363 INIT_LIST_HEAD(&path
->list_source
);
1364 INIT_LIST_HEAD(&path
->list_sink
);
1366 /* check for external widgets */
1367 if (wsink
->id
== snd_soc_dapm_input
) {
1368 if (wsource
->id
== snd_soc_dapm_micbias
||
1369 wsource
->id
== snd_soc_dapm_mic
||
1370 wsource
->id
== snd_soc_dapm_line
||
1371 wsource
->id
== snd_soc_dapm_output
)
1374 if (wsource
->id
== snd_soc_dapm_output
) {
1375 if (wsink
->id
== snd_soc_dapm_spk
||
1376 wsink
->id
== snd_soc_dapm_hp
||
1377 wsink
->id
== snd_soc_dapm_line
||
1378 wsink
->id
== snd_soc_dapm_input
)
1382 /* connect static paths */
1383 if (control
== NULL
) {
1384 list_add(&path
->list
, &dapm
->paths
);
1385 list_add(&path
->list_sink
, &wsink
->sources
);
1386 list_add(&path
->list_source
, &wsource
->sinks
);
1391 /* connect dynamic paths */
1393 case snd_soc_dapm_adc
:
1394 case snd_soc_dapm_dac
:
1395 case snd_soc_dapm_pga
:
1396 case snd_soc_dapm_input
:
1397 case snd_soc_dapm_output
:
1398 case snd_soc_dapm_micbias
:
1399 case snd_soc_dapm_vmid
:
1400 case snd_soc_dapm_pre
:
1401 case snd_soc_dapm_post
:
1402 case snd_soc_dapm_supply
:
1403 case snd_soc_dapm_aif_in
:
1404 case snd_soc_dapm_aif_out
:
1405 list_add(&path
->list
, &dapm
->paths
);
1406 list_add(&path
->list_sink
, &wsink
->sources
);
1407 list_add(&path
->list_source
, &wsource
->sinks
);
1410 case snd_soc_dapm_mux
:
1411 case snd_soc_dapm_value_mux
:
1412 ret
= dapm_connect_mux(dapm
, wsource
, wsink
, path
, control
,
1413 &wsink
->kcontrols
[0]);
1417 case snd_soc_dapm_switch
:
1418 case snd_soc_dapm_mixer
:
1419 case snd_soc_dapm_mixer_named_ctl
:
1420 ret
= dapm_connect_mixer(dapm
, wsource
, wsink
, path
, control
);
1424 case snd_soc_dapm_hp
:
1425 case snd_soc_dapm_mic
:
1426 case snd_soc_dapm_line
:
1427 case snd_soc_dapm_spk
:
1428 list_add(&path
->list
, &dapm
->paths
);
1429 list_add(&path
->list_sink
, &wsink
->sources
);
1430 list_add(&path
->list_source
, &wsource
->sinks
);
1437 dev_warn(dapm
->dev
, "asoc: no dapm match for %s --> %s --> %s\n",
1438 source
, control
, sink
);
1444 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1445 * @dapm: DAPM context
1446 * @route: audio routes
1447 * @num: number of routes
1449 * Connects 2 dapm widgets together via a named audio path. The sink is
1450 * the widget receiving the audio signal, whilst the source is the sender
1451 * of the audio signal.
1453 * Returns 0 for success else error. On error all resources can be freed
1454 * with a call to snd_soc_card_free().
1456 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context
*dapm
,
1457 const struct snd_soc_dapm_route
*route
, int num
)
1461 for (i
= 0; i
< num
; i
++) {
1462 ret
= snd_soc_dapm_add_route(dapm
, route
);
1464 dev_err(dapm
->dev
, "Failed to add route %s->%s\n",
1465 route
->source
, route
->sink
);
1473 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
1476 * snd_soc_dapm_new_widgets - add new dapm widgets
1477 * @dapm: DAPM context
1479 * Checks the codec for any new dapm widgets and creates them if found.
1481 * Returns 0 for success.
1483 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context
*dapm
)
1485 struct snd_soc_dapm_widget
*w
;
1487 list_for_each_entry(w
, &dapm
->widgets
, list
)
1493 case snd_soc_dapm_switch
:
1494 case snd_soc_dapm_mixer
:
1495 case snd_soc_dapm_mixer_named_ctl
:
1496 w
->power_check
= dapm_generic_check_power
;
1497 dapm_new_mixer(dapm
, w
);
1499 case snd_soc_dapm_mux
:
1500 case snd_soc_dapm_value_mux
:
1501 w
->power_check
= dapm_generic_check_power
;
1502 dapm_new_mux(dapm
, w
);
1504 case snd_soc_dapm_adc
:
1505 case snd_soc_dapm_aif_out
:
1506 w
->power_check
= dapm_adc_check_power
;
1508 case snd_soc_dapm_dac
:
1509 case snd_soc_dapm_aif_in
:
1510 w
->power_check
= dapm_dac_check_power
;
1512 case snd_soc_dapm_pga
:
1513 w
->power_check
= dapm_generic_check_power
;
1514 dapm_new_pga(dapm
, w
);
1516 case snd_soc_dapm_input
:
1517 case snd_soc_dapm_output
:
1518 case snd_soc_dapm_micbias
:
1519 case snd_soc_dapm_spk
:
1520 case snd_soc_dapm_hp
:
1521 case snd_soc_dapm_mic
:
1522 case snd_soc_dapm_line
:
1523 w
->power_check
= dapm_generic_check_power
;
1525 case snd_soc_dapm_supply
:
1526 w
->power_check
= dapm_supply_check_power
;
1527 case snd_soc_dapm_vmid
:
1528 case snd_soc_dapm_pre
:
1529 case snd_soc_dapm_post
:
1535 dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
1538 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
1541 * snd_soc_dapm_get_volsw - dapm mixer get callback
1542 * @kcontrol: mixer control
1543 * @ucontrol: control element information
1545 * Callback to get the value of a dapm mixer control.
1547 * Returns 0 for success.
1549 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
1550 struct snd_ctl_elem_value
*ucontrol
)
1552 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1553 struct soc_mixer_control
*mc
=
1554 (struct soc_mixer_control
*)kcontrol
->private_value
;
1555 unsigned int reg
= mc
->reg
;
1556 unsigned int shift
= mc
->shift
;
1557 unsigned int rshift
= mc
->rshift
;
1559 unsigned int invert
= mc
->invert
;
1560 unsigned int mask
= (1 << fls(max
)) - 1;
1562 ucontrol
->value
.integer
.value
[0] =
1563 (snd_soc_read(widget
->codec
, reg
) >> shift
) & mask
;
1564 if (shift
!= rshift
)
1565 ucontrol
->value
.integer
.value
[1] =
1566 (snd_soc_read(widget
->codec
, reg
) >> rshift
) & mask
;
1568 ucontrol
->value
.integer
.value
[0] =
1569 max
- ucontrol
->value
.integer
.value
[0];
1570 if (shift
!= rshift
)
1571 ucontrol
->value
.integer
.value
[1] =
1572 max
- ucontrol
->value
.integer
.value
[1];
1577 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
1580 * snd_soc_dapm_put_volsw - dapm mixer set callback
1581 * @kcontrol: mixer control
1582 * @ucontrol: control element information
1584 * Callback to set the value of a dapm mixer control.
1586 * Returns 0 for success.
1588 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
1589 struct snd_ctl_elem_value
*ucontrol
)
1591 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1592 struct soc_mixer_control
*mc
=
1593 (struct soc_mixer_control
*)kcontrol
->private_value
;
1594 unsigned int reg
= mc
->reg
;
1595 unsigned int shift
= mc
->shift
;
1596 unsigned int rshift
= mc
->rshift
;
1598 unsigned int mask
= (1 << fls(max
)) - 1;
1599 unsigned int invert
= mc
->invert
;
1600 unsigned int val
, val2
, val_mask
;
1604 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
1608 val_mask
= mask
<< shift
;
1610 if (shift
!= rshift
) {
1611 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
1614 val_mask
|= mask
<< rshift
;
1615 val
|= val2
<< rshift
;
1618 mutex_lock(&widget
->codec
->mutex
);
1619 widget
->value
= val
;
1621 if (snd_soc_test_bits(widget
->codec
, reg
, val_mask
, val
)) {
1623 /* new connection */
1624 connect
= invert
? 0:1;
1626 /* old connection must be powered down */
1627 connect
= invert
? 1:0;
1629 dapm_mixer_update_power(widget
, kcontrol
, connect
);
1632 if (widget
->event
) {
1633 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1634 ret
= widget
->event(widget
, kcontrol
,
1635 SND_SOC_DAPM_PRE_REG
);
1641 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1642 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1643 ret
= widget
->event(widget
, kcontrol
,
1644 SND_SOC_DAPM_POST_REG
);
1646 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1649 mutex_unlock(&widget
->codec
->mutex
);
1652 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
1655 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1656 * @kcontrol: mixer control
1657 * @ucontrol: control element information
1659 * Callback to get the value of a dapm enumerated double mixer control.
1661 * Returns 0 for success.
1663 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
1664 struct snd_ctl_elem_value
*ucontrol
)
1666 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1667 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1668 unsigned int val
, bitmask
;
1670 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1672 val
= snd_soc_read(widget
->codec
, e
->reg
);
1673 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & (bitmask
- 1);
1674 if (e
->shift_l
!= e
->shift_r
)
1675 ucontrol
->value
.enumerated
.item
[1] =
1676 (val
>> e
->shift_r
) & (bitmask
- 1);
1680 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
1683 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1684 * @kcontrol: mixer control
1685 * @ucontrol: control element information
1687 * Callback to set the value of a dapm enumerated double mixer control.
1689 * Returns 0 for success.
1691 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
1692 struct snd_ctl_elem_value
*ucontrol
)
1694 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1695 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1696 unsigned int val
, mux
, change
;
1697 unsigned int mask
, bitmask
;
1700 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1702 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1704 mux
= ucontrol
->value
.enumerated
.item
[0];
1705 val
= mux
<< e
->shift_l
;
1706 mask
= (bitmask
- 1) << e
->shift_l
;
1707 if (e
->shift_l
!= e
->shift_r
) {
1708 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1710 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
1711 mask
|= (bitmask
- 1) << e
->shift_r
;
1714 mutex_lock(&widget
->codec
->mutex
);
1715 widget
->value
= val
;
1716 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
1717 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
1719 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1720 ret
= widget
->event(widget
,
1721 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1726 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1728 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1729 ret
= widget
->event(widget
,
1730 kcontrol
, SND_SOC_DAPM_POST_REG
);
1733 mutex_unlock(&widget
->codec
->mutex
);
1736 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
1739 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1740 * @kcontrol: mixer control
1741 * @ucontrol: control element information
1743 * Returns 0 for success.
1745 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol
*kcontrol
,
1746 struct snd_ctl_elem_value
*ucontrol
)
1748 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1750 ucontrol
->value
.enumerated
.item
[0] = widget
->value
;
1754 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt
);
1757 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1758 * @kcontrol: mixer control
1759 * @ucontrol: control element information
1761 * Returns 0 for success.
1763 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol
*kcontrol
,
1764 struct snd_ctl_elem_value
*ucontrol
)
1766 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1767 struct soc_enum
*e
=
1768 (struct soc_enum
*)kcontrol
->private_value
;
1772 if (ucontrol
->value
.enumerated
.item
[0] >= e
->max
)
1775 mutex_lock(&widget
->codec
->mutex
);
1777 change
= widget
->value
!= ucontrol
->value
.enumerated
.item
[0];
1778 widget
->value
= ucontrol
->value
.enumerated
.item
[0];
1779 dapm_mux_update_power(widget
, kcontrol
, change
, widget
->value
, e
);
1781 mutex_unlock(&widget
->codec
->mutex
);
1784 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt
);
1787 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1789 * @kcontrol: mixer control
1790 * @ucontrol: control element information
1792 * Callback to get the value of a dapm semi enumerated double mixer control.
1794 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1795 * used for handling bitfield coded enumeration for example.
1797 * Returns 0 for success.
1799 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
1800 struct snd_ctl_elem_value
*ucontrol
)
1802 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1803 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1804 unsigned int reg_val
, val
, mux
;
1806 reg_val
= snd_soc_read(widget
->codec
, e
->reg
);
1807 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
1808 for (mux
= 0; mux
< e
->max
; mux
++) {
1809 if (val
== e
->values
[mux
])
1812 ucontrol
->value
.enumerated
.item
[0] = mux
;
1813 if (e
->shift_l
!= e
->shift_r
) {
1814 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
1815 for (mux
= 0; mux
< e
->max
; mux
++) {
1816 if (val
== e
->values
[mux
])
1819 ucontrol
->value
.enumerated
.item
[1] = mux
;
1824 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double
);
1827 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1829 * @kcontrol: mixer control
1830 * @ucontrol: control element information
1832 * Callback to set the value of a dapm semi enumerated double mixer control.
1834 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1835 * used for handling bitfield coded enumeration for example.
1837 * Returns 0 for success.
1839 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
1840 struct snd_ctl_elem_value
*ucontrol
)
1842 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1843 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1844 unsigned int val
, mux
, change
;
1848 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1850 mux
= ucontrol
->value
.enumerated
.item
[0];
1851 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
1852 mask
= e
->mask
<< e
->shift_l
;
1853 if (e
->shift_l
!= e
->shift_r
) {
1854 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1856 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
1857 mask
|= e
->mask
<< e
->shift_r
;
1860 mutex_lock(&widget
->codec
->mutex
);
1861 widget
->value
= val
;
1862 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
1863 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
1865 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1866 ret
= widget
->event(widget
,
1867 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1872 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1874 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1875 ret
= widget
->event(widget
,
1876 kcontrol
, SND_SOC_DAPM_POST_REG
);
1879 mutex_unlock(&widget
->codec
->mutex
);
1882 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double
);
1885 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1887 * @kcontrol: mixer control
1888 * @uinfo: control element information
1890 * Callback to provide information about a pin switch control.
1892 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
1893 struct snd_ctl_elem_info
*uinfo
)
1895 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1897 uinfo
->value
.integer
.min
= 0;
1898 uinfo
->value
.integer
.max
= 1;
1902 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
1905 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1907 * @kcontrol: mixer control
1910 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
1911 struct snd_ctl_elem_value
*ucontrol
)
1913 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1914 const char *pin
= (const char *)kcontrol
->private_value
;
1916 mutex_lock(&codec
->mutex
);
1918 ucontrol
->value
.integer
.value
[0] =
1919 snd_soc_dapm_get_pin_status(&codec
->dapm
, pin
);
1921 mutex_unlock(&codec
->mutex
);
1925 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
1928 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1930 * @kcontrol: mixer control
1933 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
1934 struct snd_ctl_elem_value
*ucontrol
)
1936 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1937 const char *pin
= (const char *)kcontrol
->private_value
;
1939 mutex_lock(&codec
->mutex
);
1941 if (ucontrol
->value
.integer
.value
[0])
1942 snd_soc_dapm_enable_pin(&codec
->dapm
, pin
);
1944 snd_soc_dapm_disable_pin(&codec
->dapm
, pin
);
1946 snd_soc_dapm_sync(&codec
->dapm
);
1948 mutex_unlock(&codec
->mutex
);
1952 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
1955 * snd_soc_dapm_new_control - create new dapm control
1956 * @dapm: DAPM context
1957 * @widget: widget template
1959 * Creates a new dapm control based upon the template.
1961 * Returns 0 for success else error.
1963 int snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
1964 const struct snd_soc_dapm_widget
*widget
)
1966 struct snd_soc_dapm_widget
*w
;
1968 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
1972 w
->codec
= dapm
->codec
;
1973 INIT_LIST_HEAD(&w
->sources
);
1974 INIT_LIST_HEAD(&w
->sinks
);
1975 INIT_LIST_HEAD(&w
->list
);
1976 list_add(&w
->list
, &dapm
->widgets
);
1978 /* machine layer set ups unconnected pins and insertions */
1982 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
1985 * snd_soc_dapm_new_controls - create new dapm controls
1986 * @dapm: DAPM context
1987 * @widget: widget array
1988 * @num: number of widgets
1990 * Creates new DAPM controls based upon the templates.
1992 * Returns 0 for success else error.
1994 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context
*dapm
,
1995 const struct snd_soc_dapm_widget
*widget
,
2000 for (i
= 0; i
< num
; i
++) {
2001 ret
= snd_soc_dapm_new_control(dapm
, widget
);
2004 "ASoC: Failed to create DAPM control %s: %d\n",
2012 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
2014 static void soc_dapm_stream_event(struct snd_soc_dapm_context
*dapm
,
2015 const char *stream
, int event
)
2017 struct snd_soc_dapm_widget
*w
;
2019 list_for_each_entry(w
, &dapm
->widgets
, list
)
2023 dev_dbg(w
->dapm
->dev
, "widget %s\n %s stream %s event %d\n",
2024 w
->name
, w
->sname
, stream
, event
);
2025 if (strstr(w
->sname
, stream
)) {
2027 case SND_SOC_DAPM_STREAM_START
:
2030 case SND_SOC_DAPM_STREAM_STOP
:
2033 case SND_SOC_DAPM_STREAM_SUSPEND
:
2034 case SND_SOC_DAPM_STREAM_RESUME
:
2035 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
2036 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
2042 dapm_power_widgets(dapm
, event
);
2046 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2047 * @rtd: PCM runtime data
2048 * @stream: stream name
2049 * @event: stream event
2051 * Sends a stream event to the dapm core. The core then makes any
2052 * necessary widget power changes.
2054 * Returns 0 for success else error.
2056 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
,
2057 const char *stream
, int event
)
2059 struct snd_soc_codec
*codec
= rtd
->codec
;
2064 mutex_lock(&codec
->mutex
);
2065 soc_dapm_stream_event(&codec
->dapm
, stream
, event
);
2066 mutex_unlock(&codec
->mutex
);
2069 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event
);
2072 * snd_soc_dapm_enable_pin - enable pin.
2073 * @dapm: DAPM context
2076 * Enables input/output pin and its parents or children widgets iff there is
2077 * a valid audio route and active audio stream.
2078 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2079 * do any widget power switching.
2081 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
2083 return snd_soc_dapm_set_pin(dapm
, pin
, 1);
2085 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
2088 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2089 * @dapm: DAPM context
2092 * Enables input/output pin regardless of any other state. This is
2093 * intended for use with microphone bias supplies used in microphone
2096 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2097 * do any widget power switching.
2099 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context
*dapm
,
2102 struct snd_soc_dapm_widget
*w
;
2104 list_for_each_entry(w
, &dapm
->widgets
, list
) {
2105 if (!strcmp(w
->name
, pin
)) {
2106 dev_dbg(w
->dapm
->dev
,
2107 "dapm: force enable pin %s\n", pin
);
2114 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
2117 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
2120 * snd_soc_dapm_disable_pin - disable pin.
2121 * @dapm: DAPM context
2124 * Disables input/output pin and its parents or children widgets.
2125 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2126 * do any widget power switching.
2128 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context
*dapm
,
2131 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
2133 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
2136 * snd_soc_dapm_nc_pin - permanently disable pin.
2137 * @dapm: DAPM context
2140 * Marks the specified pin as being not connected, disabling it along
2141 * any parent or child widgets. At present this is identical to
2142 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2143 * additional things such as disabling controls which only affect
2144 * paths through the pin.
2146 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2147 * do any widget power switching.
2149 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
2151 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
2153 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
2156 * snd_soc_dapm_get_pin_status - get audio pin status
2157 * @dapm: DAPM context
2158 * @pin: audio signal pin endpoint (or start point)
2160 * Get audio pin status - connected or disconnected.
2162 * Returns 1 for connected otherwise 0.
2164 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context
*dapm
,
2167 struct snd_soc_dapm_widget
*w
;
2169 list_for_each_entry(w
, &dapm
->widgets
, list
) {
2170 if (!strcmp(w
->name
, pin
))
2171 return w
->connected
;
2176 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
2179 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2180 * @dapm: DAPM context
2181 * @pin: audio signal pin endpoint (or start point)
2183 * Mark the given endpoint or pin as ignoring suspend. When the
2184 * system is disabled a path between two endpoints flagged as ignoring
2185 * suspend will not be disabled. The path must already be enabled via
2186 * normal means at suspend time, it will not be turned on if it was not
2189 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context
*dapm
,
2192 struct snd_soc_dapm_widget
*w
;
2194 list_for_each_entry(w
, &dapm
->widgets
, list
) {
2195 if (!strcmp(w
->name
, pin
)) {
2196 w
->ignore_suspend
= 1;
2201 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
2204 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
2207 * snd_soc_dapm_free - free dapm resources
2210 * Free all dapm widgets and resources.
2212 void snd_soc_dapm_free(struct snd_soc_dapm_context
*dapm
)
2214 snd_soc_dapm_sys_remove(dapm
->dev
);
2215 dapm_free_widgets(dapm
);
2217 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
2219 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context
*dapm
)
2221 struct snd_soc_dapm_widget
*w
;
2222 LIST_HEAD(down_list
);
2225 list_for_each_entry(w
, &dapm
->widgets
, list
) {
2227 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
2233 /* If there were no widgets to power down we're already in
2237 snd_soc_dapm_set_bias_level(NULL
, dapm
, SND_SOC_BIAS_PREPARE
);
2238 dapm_seq_run(dapm
, &down_list
, 0, dapm_down_seq
);
2239 snd_soc_dapm_set_bias_level(NULL
, dapm
, SND_SOC_BIAS_STANDBY
);
2244 * snd_soc_dapm_shutdown - callback for system shutdown
2246 void snd_soc_dapm_shutdown(struct snd_soc_card
*card
)
2248 struct snd_soc_codec
*codec
;
2250 list_for_each_entry(codec
, &card
->codec_dev_list
, list
) {
2251 soc_dapm_shutdown_codec(&codec
->dapm
);
2252 snd_soc_dapm_set_bias_level(card
, &codec
->dapm
, SND_SOC_BIAS_OFF
);
2256 /* Module information */
2257 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2258 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2259 MODULE_LICENSE("GPL");