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/slab.h>
43 #include <sound/core.h>
44 #include <sound/pcm.h>
45 #include <sound/pcm_params.h>
46 #include <sound/soc.h>
47 #include <sound/initval.h>
49 #include <trace/events/asoc.h>
51 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
53 /* dapm power sequences - make this per codec in the future */
54 static int dapm_up_seq
[] = {
55 [snd_soc_dapm_pre
] = 0,
56 [snd_soc_dapm_supply
] = 1,
57 [snd_soc_dapm_micbias
] = 2,
58 [snd_soc_dapm_aif_in
] = 3,
59 [snd_soc_dapm_aif_out
] = 3,
60 [snd_soc_dapm_mic
] = 4,
61 [snd_soc_dapm_mux
] = 5,
62 [snd_soc_dapm_virt_mux
] = 5,
63 [snd_soc_dapm_value_mux
] = 5,
64 [snd_soc_dapm_dac
] = 6,
65 [snd_soc_dapm_mixer
] = 7,
66 [snd_soc_dapm_mixer_named_ctl
] = 7,
67 [snd_soc_dapm_pga
] = 8,
68 [snd_soc_dapm_adc
] = 9,
69 [snd_soc_dapm_out_drv
] = 10,
70 [snd_soc_dapm_hp
] = 10,
71 [snd_soc_dapm_spk
] = 10,
72 [snd_soc_dapm_post
] = 11,
75 static int dapm_down_seq
[] = {
76 [snd_soc_dapm_pre
] = 0,
77 [snd_soc_dapm_adc
] = 1,
78 [snd_soc_dapm_hp
] = 2,
79 [snd_soc_dapm_spk
] = 2,
80 [snd_soc_dapm_out_drv
] = 2,
81 [snd_soc_dapm_pga
] = 4,
82 [snd_soc_dapm_mixer_named_ctl
] = 5,
83 [snd_soc_dapm_mixer
] = 5,
84 [snd_soc_dapm_dac
] = 6,
85 [snd_soc_dapm_mic
] = 7,
86 [snd_soc_dapm_micbias
] = 8,
87 [snd_soc_dapm_mux
] = 9,
88 [snd_soc_dapm_virt_mux
] = 9,
89 [snd_soc_dapm_value_mux
] = 9,
90 [snd_soc_dapm_aif_in
] = 10,
91 [snd_soc_dapm_aif_out
] = 10,
92 [snd_soc_dapm_supply
] = 11,
93 [snd_soc_dapm_post
] = 12,
96 static void pop_wait(u32 pop_time
)
99 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time
));
102 static void pop_dbg(struct device
*dev
, u32 pop_time
, const char *fmt
, ...)
110 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
115 vsnprintf(buf
, PAGE_SIZE
, fmt
, args
);
116 dev_info(dev
, "%s", buf
);
122 static bool dapm_dirty_widget(struct snd_soc_dapm_widget
*w
)
124 return !list_empty(&w
->dirty
);
127 static void dapm_mark_dirty(struct snd_soc_dapm_widget
*w
)
129 if (!dapm_dirty_widget(w
))
130 list_add_tail(&w
->dirty
, &w
->dapm
->card
->dapm_dirty
);
133 /* create a new dapm widget */
134 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
135 const struct snd_soc_dapm_widget
*_widget
)
137 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
140 /* get snd_card from DAPM context */
141 static inline struct snd_card
*dapm_get_snd_card(
142 struct snd_soc_dapm_context
*dapm
)
145 return dapm
->codec
->card
->snd_card
;
146 else if (dapm
->platform
)
147 return dapm
->platform
->card
->snd_card
;
155 /* get soc_card from DAPM context */
156 static inline struct snd_soc_card
*dapm_get_soc_card(
157 struct snd_soc_dapm_context
*dapm
)
160 return dapm
->codec
->card
;
161 else if (dapm
->platform
)
162 return dapm
->platform
->card
;
170 static int soc_widget_read(struct snd_soc_dapm_widget
*w
, int reg
)
173 return snd_soc_read(w
->codec
, reg
);
174 else if (w
->platform
)
175 return snd_soc_platform_read(w
->platform
, reg
);
177 dev_err(w
->dapm
->dev
, "no valid widget read method\n");
181 static int soc_widget_write(struct snd_soc_dapm_widget
*w
, int reg
, int val
)
184 return snd_soc_write(w
->codec
, reg
, val
);
185 else if (w
->platform
)
186 return snd_soc_platform_write(w
->platform
, reg
, val
);
188 dev_err(w
->dapm
->dev
, "no valid widget write method\n");
192 static int soc_widget_update_bits(struct snd_soc_dapm_widget
*w
,
193 unsigned short reg
, unsigned int mask
, unsigned int value
)
196 unsigned int old
, new;
199 ret
= soc_widget_read(w
, reg
);
204 new = (old
& ~mask
) | (value
& mask
);
207 ret
= soc_widget_write(w
, reg
, new);
216 * snd_soc_dapm_set_bias_level - set the bias level for the system
217 * @dapm: DAPM context
218 * @level: level to configure
220 * Configure the bias (power) levels for the SoC audio device.
222 * Returns 0 for success else error.
224 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context
*dapm
,
225 enum snd_soc_bias_level level
)
227 struct snd_soc_card
*card
= dapm
->card
;
230 trace_snd_soc_bias_level_start(card
, level
);
232 if (card
&& card
->set_bias_level
)
233 ret
= card
->set_bias_level(card
, dapm
, level
);
238 if (dapm
->codec
->driver
->set_bias_level
)
239 ret
= dapm
->codec
->driver
->set_bias_level(dapm
->codec
,
242 dapm
->bias_level
= level
;
247 if (card
&& card
->set_bias_level_post
)
248 ret
= card
->set_bias_level_post(card
, dapm
, level
);
250 trace_snd_soc_bias_level_done(card
, level
);
255 /* set up initial codec paths */
256 static void dapm_set_path_status(struct snd_soc_dapm_widget
*w
,
257 struct snd_soc_dapm_path
*p
, int i
)
260 case snd_soc_dapm_switch
:
261 case snd_soc_dapm_mixer
:
262 case snd_soc_dapm_mixer_named_ctl
: {
264 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
265 w
->kcontrol_news
[i
].private_value
;
266 unsigned int reg
= mc
->reg
;
267 unsigned int shift
= mc
->shift
;
269 unsigned int mask
= (1 << fls(max
)) - 1;
270 unsigned int invert
= mc
->invert
;
272 val
= soc_widget_read(w
, reg
);
273 val
= (val
>> shift
) & mask
;
275 if ((invert
&& !val
) || (!invert
&& val
))
281 case snd_soc_dapm_mux
: {
282 struct soc_enum
*e
= (struct soc_enum
*)
283 w
->kcontrol_news
[i
].private_value
;
284 int val
, item
, bitmask
;
286 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
288 val
= soc_widget_read(w
, e
->reg
);
289 item
= (val
>> e
->shift_l
) & (bitmask
- 1);
292 for (i
= 0; i
< e
->max
; i
++) {
293 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
298 case snd_soc_dapm_virt_mux
: {
299 struct soc_enum
*e
= (struct soc_enum
*)
300 w
->kcontrol_news
[i
].private_value
;
303 /* since a virtual mux has no backing registers to
304 * decide which path to connect, it will try to match
305 * with the first enumeration. This is to ensure
306 * that the default mux choice (the first) will be
307 * correctly powered up during initialization.
309 if (!strcmp(p
->name
, e
->texts
[0]))
313 case snd_soc_dapm_value_mux
: {
314 struct soc_enum
*e
= (struct soc_enum
*)
315 w
->kcontrol_news
[i
].private_value
;
318 val
= soc_widget_read(w
, e
->reg
);
319 val
= (val
>> e
->shift_l
) & e
->mask
;
320 for (item
= 0; item
< e
->max
; item
++) {
321 if (val
== e
->values
[item
])
326 for (i
= 0; i
< e
->max
; i
++) {
327 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
332 /* does not affect routing - always connected */
333 case snd_soc_dapm_pga
:
334 case snd_soc_dapm_out_drv
:
335 case snd_soc_dapm_output
:
336 case snd_soc_dapm_adc
:
337 case snd_soc_dapm_input
:
338 case snd_soc_dapm_dac
:
339 case snd_soc_dapm_micbias
:
340 case snd_soc_dapm_vmid
:
341 case snd_soc_dapm_supply
:
342 case snd_soc_dapm_aif_in
:
343 case snd_soc_dapm_aif_out
:
344 case snd_soc_dapm_hp
:
345 case snd_soc_dapm_mic
:
346 case snd_soc_dapm_spk
:
347 case snd_soc_dapm_line
:
350 /* does affect routing - dynamically connected */
351 case snd_soc_dapm_pre
:
352 case snd_soc_dapm_post
:
358 /* connect mux widget to its interconnecting audio paths */
359 static int dapm_connect_mux(struct snd_soc_dapm_context
*dapm
,
360 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
361 struct snd_soc_dapm_path
*path
, const char *control_name
,
362 const struct snd_kcontrol_new
*kcontrol
)
364 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
367 for (i
= 0; i
< e
->max
; i
++) {
368 if (!(strcmp(control_name
, e
->texts
[i
]))) {
369 list_add(&path
->list
, &dapm
->card
->paths
);
370 list_add(&path
->list_sink
, &dest
->sources
);
371 list_add(&path
->list_source
, &src
->sinks
);
372 path
->name
= (char*)e
->texts
[i
];
373 dapm_set_path_status(dest
, path
, 0);
381 /* connect mixer widget to its interconnecting audio paths */
382 static int dapm_connect_mixer(struct snd_soc_dapm_context
*dapm
,
383 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
384 struct snd_soc_dapm_path
*path
, const char *control_name
)
388 /* search for mixer kcontrol */
389 for (i
= 0; i
< dest
->num_kcontrols
; i
++) {
390 if (!strcmp(control_name
, dest
->kcontrol_news
[i
].name
)) {
391 list_add(&path
->list
, &dapm
->card
->paths
);
392 list_add(&path
->list_sink
, &dest
->sources
);
393 list_add(&path
->list_source
, &src
->sinks
);
394 path
->name
= dest
->kcontrol_news
[i
].name
;
395 dapm_set_path_status(dest
, path
, i
);
402 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context
*dapm
,
403 struct snd_soc_dapm_widget
*kcontrolw
,
404 const struct snd_kcontrol_new
*kcontrol_new
,
405 struct snd_kcontrol
**kcontrol
)
407 struct snd_soc_dapm_widget
*w
;
412 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
413 if (w
== kcontrolw
|| w
->dapm
!= kcontrolw
->dapm
)
415 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
416 if (&w
->kcontrol_news
[i
] == kcontrol_new
) {
418 *kcontrol
= w
->kcontrols
[i
];
427 /* create new dapm mixer control */
428 static int dapm_new_mixer(struct snd_soc_dapm_widget
*w
)
430 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
432 size_t name_len
, prefix_len
;
433 struct snd_soc_dapm_path
*path
;
434 struct snd_card
*card
= dapm
->card
->snd_card
;
436 struct snd_soc_dapm_widget_list
*wlist
;
440 prefix
= dapm
->codec
->name_prefix
;
445 prefix_len
= strlen(prefix
) + 1;
450 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
453 list_for_each_entry(path
, &w
->sources
, list_sink
) {
455 /* mixer/mux paths name must match control name */
456 if (path
->name
!= (char *)w
->kcontrol_news
[i
].name
)
459 if (w
->kcontrols
[i
]) {
460 path
->kcontrol
= w
->kcontrols
[i
];
464 wlistsize
= sizeof(struct snd_soc_dapm_widget_list
) +
465 sizeof(struct snd_soc_dapm_widget
*),
466 wlist
= kzalloc(wlistsize
, GFP_KERNEL
);
469 "asoc: can't allocate widget list for %s\n",
473 wlist
->num_widgets
= 1;
474 wlist
->widgets
[0] = w
;
476 /* add dapm control with long name.
477 * for dapm_mixer this is the concatenation of the
478 * mixer and kcontrol name.
479 * for dapm_mixer_named_ctl this is simply the
482 name_len
= strlen(w
->kcontrol_news
[i
].name
) + 1;
483 if (w
->id
!= snd_soc_dapm_mixer_named_ctl
)
484 name_len
+= 1 + strlen(w
->name
);
486 path
->long_name
= kmalloc(name_len
, GFP_KERNEL
);
488 if (path
->long_name
== NULL
) {
495 /* The control will get a prefix from
496 * the control creation process but
497 * we're also using the same prefix
498 * for widgets so cut the prefix off
499 * the front of the widget name.
501 snprintf(path
->long_name
, name_len
, "%s %s",
502 w
->name
+ prefix_len
,
503 w
->kcontrol_news
[i
].name
);
505 case snd_soc_dapm_mixer_named_ctl
:
506 snprintf(path
->long_name
, name_len
, "%s",
507 w
->kcontrol_news
[i
].name
);
511 path
->long_name
[name_len
- 1] = '\0';
513 path
->kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[i
],
514 wlist
, path
->long_name
,
516 ret
= snd_ctl_add(card
, path
->kcontrol
);
519 "asoc: failed to add dapm kcontrol %s: %d\n",
520 path
->long_name
, ret
);
522 kfree(path
->long_name
);
523 path
->long_name
= NULL
;
526 w
->kcontrols
[i
] = path
->kcontrol
;
532 /* create new dapm mux control */
533 static int dapm_new_mux(struct snd_soc_dapm_widget
*w
)
535 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
536 struct snd_soc_dapm_path
*path
= NULL
;
537 struct snd_kcontrol
*kcontrol
;
538 struct snd_card
*card
= dapm
->card
->snd_card
;
542 struct snd_soc_dapm_widget_list
*wlist
;
543 int shared
, wlistentries
;
547 if (w
->num_kcontrols
!= 1) {
549 "asoc: mux %s has incorrect number of controls\n",
554 shared
= dapm_is_shared_kcontrol(dapm
, w
, &w
->kcontrol_news
[0],
557 wlist
= kcontrol
->private_data
;
558 wlistentries
= wlist
->num_widgets
+ 1;
563 wlistsize
= sizeof(struct snd_soc_dapm_widget_list
) +
564 wlistentries
* sizeof(struct snd_soc_dapm_widget
*),
565 wlist
= krealloc(wlist
, wlistsize
, GFP_KERNEL
);
568 "asoc: can't allocate widget list for %s\n", w
->name
);
571 wlist
->num_widgets
= wlistentries
;
572 wlist
->widgets
[wlistentries
- 1] = w
;
576 prefix
= dapm
->codec
->name_prefix
;
581 name
= w
->kcontrol_news
[0].name
;
586 prefix_len
= strlen(prefix
) + 1;
592 * The control will get a prefix from the control creation
593 * process but we're also using the same prefix for widgets so
594 * cut the prefix off the front of the widget name.
596 kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[0], wlist
,
597 name
+ prefix_len
, prefix
);
598 ret
= snd_ctl_add(card
, kcontrol
);
600 dev_err(dapm
->dev
, "failed to add kcontrol %s: %d\n",
607 kcontrol
->private_data
= wlist
;
609 w
->kcontrols
[0] = kcontrol
;
611 list_for_each_entry(path
, &w
->sources
, list_sink
)
612 path
->kcontrol
= kcontrol
;
617 /* create new dapm volume control */
618 static int dapm_new_pga(struct snd_soc_dapm_widget
*w
)
620 if (w
->num_kcontrols
)
621 dev_err(w
->dapm
->dev
,
622 "asoc: PGA controls not supported: '%s'\n", w
->name
);
627 /* reset 'walked' bit for each dapm path */
628 static inline void dapm_clear_walk(struct snd_soc_dapm_context
*dapm
)
630 struct snd_soc_dapm_path
*p
;
632 list_for_each_entry(p
, &dapm
->card
->paths
, list
)
636 /* We implement power down on suspend by checking the power state of
637 * the ALSA card - when we are suspending the ALSA state for the card
640 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
642 int level
= snd_power_get_state(widget
->dapm
->card
->snd_card
);
645 case SNDRV_CTL_POWER_D3hot
:
646 case SNDRV_CTL_POWER_D3cold
:
647 if (widget
->ignore_suspend
)
648 dev_dbg(widget
->dapm
->dev
, "%s ignoring suspend\n",
650 return widget
->ignore_suspend
;
657 * Recursively check for a completed path to an active or physically connected
658 * output widget. Returns number of complete paths.
660 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
)
662 struct snd_soc_dapm_path
*path
;
665 DAPM_UPDATE_STAT(widget
, path_checks
);
667 if (widget
->id
== snd_soc_dapm_supply
)
670 switch (widget
->id
) {
671 case snd_soc_dapm_adc
:
672 case snd_soc_dapm_aif_out
:
674 return snd_soc_dapm_suspend_check(widget
);
679 if (widget
->connected
) {
680 /* connected pin ? */
681 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
)
682 return snd_soc_dapm_suspend_check(widget
);
684 /* connected jack or spk ? */
685 if (widget
->id
== snd_soc_dapm_hp
|| widget
->id
== snd_soc_dapm_spk
||
686 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sources
)))
687 return snd_soc_dapm_suspend_check(widget
);
690 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
691 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
699 if (path
->sink
&& path
->connect
) {
701 con
+= is_connected_output_ep(path
->sink
);
709 * Recursively check for a completed path to an active or physically connected
710 * input widget. Returns number of complete paths.
712 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
)
714 struct snd_soc_dapm_path
*path
;
717 DAPM_UPDATE_STAT(widget
, path_checks
);
719 if (widget
->id
== snd_soc_dapm_supply
)
722 /* active stream ? */
723 switch (widget
->id
) {
724 case snd_soc_dapm_dac
:
725 case snd_soc_dapm_aif_in
:
727 return snd_soc_dapm_suspend_check(widget
);
732 if (widget
->connected
) {
733 /* connected pin ? */
734 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
)
735 return snd_soc_dapm_suspend_check(widget
);
737 /* connected VMID/Bias for lower pops */
738 if (widget
->id
== snd_soc_dapm_vmid
)
739 return snd_soc_dapm_suspend_check(widget
);
741 /* connected jack ? */
742 if (widget
->id
== snd_soc_dapm_mic
||
743 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sinks
)))
744 return snd_soc_dapm_suspend_check(widget
);
747 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
748 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
756 if (path
->source
&& path
->connect
) {
758 con
+= is_connected_input_ep(path
->source
);
766 * Handler for generic register modifier widget.
768 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
769 struct snd_kcontrol
*kcontrol
, int event
)
773 if (SND_SOC_DAPM_EVENT_ON(event
))
778 soc_widget_update_bits(w
, -(w
->reg
+ 1),
779 w
->mask
<< w
->shift
, val
<< w
->shift
);
783 EXPORT_SYMBOL_GPL(dapm_reg_event
);
785 static int dapm_widget_power_check(struct snd_soc_dapm_widget
*w
)
790 return w
->power_check(w
);
793 /* Generic check to see if a widget should be powered.
795 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
799 DAPM_UPDATE_STAT(w
, power_checks
);
801 in
= is_connected_input_ep(w
);
802 dapm_clear_walk(w
->dapm
);
803 out
= is_connected_output_ep(w
);
804 dapm_clear_walk(w
->dapm
);
805 return out
!= 0 && in
!= 0;
808 /* Check to see if an ADC has power */
809 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
813 DAPM_UPDATE_STAT(w
, power_checks
);
816 in
= is_connected_input_ep(w
);
817 dapm_clear_walk(w
->dapm
);
820 return dapm_generic_check_power(w
);
824 /* Check to see if a DAC has power */
825 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
829 DAPM_UPDATE_STAT(w
, power_checks
);
832 out
= is_connected_output_ep(w
);
833 dapm_clear_walk(w
->dapm
);
836 return dapm_generic_check_power(w
);
840 /* Check to see if a power supply is needed */
841 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
843 struct snd_soc_dapm_path
*path
;
846 DAPM_UPDATE_STAT(w
, power_checks
);
848 /* Check if one of our outputs is connected */
849 list_for_each_entry(path
, &w
->sinks
, list_source
) {
850 DAPM_UPDATE_STAT(w
, neighbour_checks
);
855 if (path
->connected
&&
856 !path
->connected(path
->source
, path
->sink
))
862 if (dapm_widget_power_check(path
->sink
)) {
868 dapm_clear_walk(w
->dapm
);
873 static int dapm_always_on_check_power(struct snd_soc_dapm_widget
*w
)
878 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
879 struct snd_soc_dapm_widget
*b
,
887 sort
= dapm_down_seq
;
889 if (sort
[a
->id
] != sort
[b
->id
])
890 return sort
[a
->id
] - sort
[b
->id
];
891 if (a
->subseq
!= b
->subseq
) {
893 return a
->subseq
- b
->subseq
;
895 return b
->subseq
- a
->subseq
;
897 if (a
->reg
!= b
->reg
)
898 return a
->reg
- b
->reg
;
899 if (a
->dapm
!= b
->dapm
)
900 return (unsigned long)a
->dapm
- (unsigned long)b
->dapm
;
905 /* Insert a widget in order into a DAPM power sequence. */
906 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
907 struct list_head
*list
,
910 struct snd_soc_dapm_widget
*w
;
912 list_for_each_entry(w
, list
, power_list
)
913 if (dapm_seq_compare(new_widget
, w
, power_up
) < 0) {
914 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
918 list_add_tail(&new_widget
->power_list
, list
);
921 static void dapm_seq_check_event(struct snd_soc_dapm_context
*dapm
,
922 struct snd_soc_dapm_widget
*w
, int event
)
924 struct snd_soc_card
*card
= dapm
->card
;
929 case SND_SOC_DAPM_PRE_PMU
:
933 case SND_SOC_DAPM_POST_PMU
:
934 ev_name
= "POST_PMU";
937 case SND_SOC_DAPM_PRE_PMD
:
941 case SND_SOC_DAPM_POST_PMD
:
942 ev_name
= "POST_PMD";
950 if (w
->power
!= power
)
953 if (w
->event
&& (w
->event_flags
& event
)) {
954 pop_dbg(dapm
->dev
, card
->pop_time
, "pop test : %s %s\n",
956 trace_snd_soc_dapm_widget_event_start(w
, event
);
957 ret
= w
->event(w
, NULL
, event
);
958 trace_snd_soc_dapm_widget_event_done(w
, event
);
960 pr_err("%s: %s event failed: %d\n",
961 ev_name
, w
->name
, ret
);
965 /* Apply the coalesced changes from a DAPM sequence */
966 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context
*dapm
,
967 struct list_head
*pending
)
969 struct snd_soc_card
*card
= dapm
->card
;
970 struct snd_soc_dapm_widget
*w
;
972 unsigned int value
= 0;
973 unsigned int mask
= 0;
974 unsigned int cur_mask
;
976 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
979 list_for_each_entry(w
, pending
, power_list
) {
980 cur_mask
= 1 << w
->shift
;
981 BUG_ON(reg
!= w
->reg
);
992 pop_dbg(dapm
->dev
, card
->pop_time
,
993 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
994 w
->name
, reg
, value
, mask
);
996 /* Check for events */
997 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_PRE_PMU
);
998 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_PRE_PMD
);
1002 /* Any widget will do, they should all be updating the
1005 w
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
1008 pop_dbg(dapm
->dev
, card
->pop_time
,
1009 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1010 value
, mask
, reg
, card
->pop_time
);
1011 pop_wait(card
->pop_time
);
1012 soc_widget_update_bits(w
, reg
, mask
, value
);
1015 list_for_each_entry(w
, pending
, power_list
) {
1016 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_POST_PMU
);
1017 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_POST_PMD
);
1021 /* Apply a DAPM power sequence.
1023 * We walk over a pre-sorted list of widgets to apply power to. In
1024 * order to minimise the number of writes to the device required
1025 * multiple widgets will be updated in a single write where possible.
1026 * Currently anything that requires more than a single write is not
1029 static void dapm_seq_run(struct snd_soc_dapm_context
*dapm
,
1030 struct list_head
*list
, int event
, bool power_up
)
1032 struct snd_soc_dapm_widget
*w
, *n
;
1035 int cur_subseq
= -1;
1036 int cur_reg
= SND_SOC_NOPM
;
1037 struct snd_soc_dapm_context
*cur_dapm
= NULL
;
1044 sort
= dapm_down_seq
;
1046 list_for_each_entry_safe(w
, n
, list
, power_list
) {
1049 /* Do we need to apply any queued changes? */
1050 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
||
1051 w
->dapm
!= cur_dapm
|| w
->subseq
!= cur_subseq
) {
1052 if (!list_empty(&pending
))
1053 dapm_seq_run_coalesced(cur_dapm
, &pending
);
1055 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1056 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1057 if (sort
[i
] == cur_sort
)
1058 cur_dapm
->seq_notifier(cur_dapm
,
1063 INIT_LIST_HEAD(&pending
);
1065 cur_subseq
= INT_MIN
;
1066 cur_reg
= SND_SOC_NOPM
;
1071 case snd_soc_dapm_pre
:
1073 list_for_each_entry_safe_continue(w
, n
, list
,
1076 if (event
== SND_SOC_DAPM_STREAM_START
)
1078 NULL
, SND_SOC_DAPM_PRE_PMU
);
1079 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1081 NULL
, SND_SOC_DAPM_PRE_PMD
);
1084 case snd_soc_dapm_post
:
1086 list_for_each_entry_safe_continue(w
, n
, list
,
1089 if (event
== SND_SOC_DAPM_STREAM_START
)
1091 NULL
, SND_SOC_DAPM_POST_PMU
);
1092 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1094 NULL
, SND_SOC_DAPM_POST_PMD
);
1098 /* Queue it up for application */
1099 cur_sort
= sort
[w
->id
];
1100 cur_subseq
= w
->subseq
;
1103 list_move(&w
->power_list
, &pending
);
1108 dev_err(w
->dapm
->dev
,
1109 "Failed to apply widget power: %d\n", ret
);
1112 if (!list_empty(&pending
))
1113 dapm_seq_run_coalesced(cur_dapm
, &pending
);
1115 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1116 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1117 if (sort
[i
] == cur_sort
)
1118 cur_dapm
->seq_notifier(cur_dapm
,
1123 static void dapm_widget_update(struct snd_soc_dapm_context
*dapm
)
1125 struct snd_soc_dapm_update
*update
= dapm
->update
;
1126 struct snd_soc_dapm_widget
*w
;
1135 (w
->event_flags
& SND_SOC_DAPM_PRE_REG
)) {
1136 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_PRE_REG
);
1138 pr_err("%s DAPM pre-event failed: %d\n",
1142 ret
= snd_soc_update_bits(w
->codec
, update
->reg
, update
->mask
,
1145 pr_err("%s DAPM update failed: %d\n", w
->name
, ret
);
1148 (w
->event_flags
& SND_SOC_DAPM_POST_REG
)) {
1149 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_POST_REG
);
1151 pr_err("%s DAPM post-event failed: %d\n",
1156 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1157 * they're changing state.
1159 static void dapm_pre_sequence_async(void *data
, async_cookie_t cookie
)
1161 struct snd_soc_dapm_context
*d
= data
;
1164 /* If we're off and we're not supposed to be go into STANDBY */
1165 if (d
->bias_level
== SND_SOC_BIAS_OFF
&&
1166 d
->target_bias_level
!= SND_SOC_BIAS_OFF
) {
1167 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1170 "Failed to turn on bias: %d\n", ret
);
1173 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1174 if (d
->bias_level
!= d
->target_bias_level
) {
1175 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_PREPARE
);
1178 "Failed to prepare bias: %d\n", ret
);
1182 /* Async callback run prior to DAPM sequences - brings to their final
1185 static void dapm_post_sequence_async(void *data
, async_cookie_t cookie
)
1187 struct snd_soc_dapm_context
*d
= data
;
1190 /* If we just powered the last thing off drop to standby bias */
1191 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1192 (d
->target_bias_level
== SND_SOC_BIAS_STANDBY
||
1193 d
->target_bias_level
== SND_SOC_BIAS_OFF
)) {
1194 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1196 dev_err(d
->dev
, "Failed to apply standby bias: %d\n",
1200 /* If we're in standby and can support bias off then do that */
1201 if (d
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1202 d
->target_bias_level
== SND_SOC_BIAS_OFF
) {
1203 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_OFF
);
1205 dev_err(d
->dev
, "Failed to turn off bias: %d\n", ret
);
1208 /* If we just powered up then move to active bias */
1209 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1210 d
->target_bias_level
== SND_SOC_BIAS_ON
) {
1211 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_ON
);
1213 dev_err(d
->dev
, "Failed to apply active bias: %d\n",
1218 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget
*peer
,
1219 bool power
, bool connect
)
1221 /* If a connection is being made or broken then that update
1222 * will have marked the peer dirty, otherwise the widgets are
1223 * not connected and this update has no impact. */
1227 /* If the peer is already in the state we're moving to then we
1228 * won't have an impact on it. */
1229 if (power
!= peer
->power
)
1230 dapm_mark_dirty(peer
);
1233 static void dapm_widget_set_power(struct snd_soc_dapm_widget
*w
, bool power
,
1234 struct list_head
*up_list
,
1235 struct list_head
*down_list
)
1237 struct snd_soc_dapm_path
*path
;
1239 if (w
->power
== power
)
1242 trace_snd_soc_dapm_widget_power(w
, power
);
1244 /* If we changed our power state perhaps our neigbours changed
1247 list_for_each_entry(path
, &w
->sources
, list_sink
) {
1249 dapm_widget_set_peer_power(path
->source
, power
,
1253 list_for_each_entry(path
, &w
->sinks
, list_source
) {
1255 dapm_widget_set_peer_power(path
->sink
, power
,
1261 dapm_seq_insert(w
, up_list
, true);
1263 dapm_seq_insert(w
, down_list
, false);
1268 static void dapm_power_one_widget(struct snd_soc_dapm_widget
*w
,
1269 struct list_head
*up_list
,
1270 struct list_head
*down_list
)
1275 case snd_soc_dapm_pre
:
1276 dapm_seq_insert(w
, down_list
, false);
1278 case snd_soc_dapm_post
:
1279 dapm_seq_insert(w
, up_list
, true);
1283 power
= dapm_widget_power_check(w
);
1285 dapm_widget_set_power(w
, power
, up_list
, down_list
);
1291 * Scan each dapm widget for complete audio path.
1292 * A complete path is a route that has valid endpoints i.e.:-
1294 * o DAC to output pin.
1295 * o Input Pin to ADC.
1296 * o Input pin to Output pin (bypass, sidetone)
1297 * o DAC to ADC (loopback).
1299 static int dapm_power_widgets(struct snd_soc_dapm_context
*dapm
, int event
)
1301 struct snd_soc_card
*card
= dapm
->card
;
1302 struct snd_soc_dapm_widget
*w
;
1303 struct snd_soc_dapm_context
*d
;
1305 LIST_HEAD(down_list
);
1306 LIST_HEAD(async_domain
);
1307 enum snd_soc_bias_level bias
;
1309 trace_snd_soc_dapm_start(card
);
1311 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1312 if (d
->n_widgets
|| d
->codec
== NULL
) {
1313 if (d
->idle_bias_off
)
1314 d
->target_bias_level
= SND_SOC_BIAS_OFF
;
1316 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1320 memset(&card
->dapm_stats
, 0, sizeof(card
->dapm_stats
));
1322 /* Check which widgets we need to power and store them in
1323 * lists indicating if they should be powered up or down. We
1324 * only check widgets that have been flagged as dirty but note
1325 * that new widgets may be added to the dirty list while we
1328 list_for_each_entry(w
, &card
->dapm_dirty
, dirty
) {
1329 dapm_power_one_widget(w
, &up_list
, &down_list
);
1332 list_for_each_entry(w
, &card
->widgets
, list
) {
1333 list_del_init(&w
->dirty
);
1338 /* Supplies and micbiases only bring the
1339 * context up to STANDBY as unless something
1340 * else is active and passing audio they
1341 * generally don't require full power.
1344 case snd_soc_dapm_supply
:
1345 case snd_soc_dapm_micbias
:
1346 if (d
->target_bias_level
< SND_SOC_BIAS_STANDBY
)
1347 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1350 d
->target_bias_level
= SND_SOC_BIAS_ON
;
1357 /* If there are no DAPM widgets then try to figure out power from the
1360 if (!dapm
->n_widgets
) {
1362 case SND_SOC_DAPM_STREAM_START
:
1363 case SND_SOC_DAPM_STREAM_RESUME
:
1364 dapm
->target_bias_level
= SND_SOC_BIAS_ON
;
1366 case SND_SOC_DAPM_STREAM_STOP
:
1367 if (dapm
->codec
->active
)
1368 dapm
->target_bias_level
= SND_SOC_BIAS_ON
;
1370 dapm
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1372 case SND_SOC_DAPM_STREAM_SUSPEND
:
1373 dapm
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1375 case SND_SOC_DAPM_STREAM_NOP
:
1376 dapm
->target_bias_level
= dapm
->bias_level
;
1383 /* Force all contexts in the card to the same bias state if
1384 * they're not ground referenced.
1386 bias
= SND_SOC_BIAS_OFF
;
1387 list_for_each_entry(d
, &card
->dapm_list
, list
)
1388 if (d
->target_bias_level
> bias
)
1389 bias
= d
->target_bias_level
;
1390 list_for_each_entry(d
, &card
->dapm_list
, list
)
1391 if (!d
->idle_bias_off
)
1392 d
->target_bias_level
= bias
;
1394 trace_snd_soc_dapm_walk_done(card
);
1396 /* Run all the bias changes in parallel */
1397 list_for_each_entry(d
, &dapm
->card
->dapm_list
, list
)
1398 async_schedule_domain(dapm_pre_sequence_async
, d
,
1400 async_synchronize_full_domain(&async_domain
);
1402 /* Power down widgets first; try to avoid amplifying pops. */
1403 dapm_seq_run(dapm
, &down_list
, event
, false);
1405 dapm_widget_update(dapm
);
1408 dapm_seq_run(dapm
, &up_list
, event
, true);
1410 /* Run all the bias changes in parallel */
1411 list_for_each_entry(d
, &dapm
->card
->dapm_list
, list
)
1412 async_schedule_domain(dapm_post_sequence_async
, d
,
1414 async_synchronize_full_domain(&async_domain
);
1416 pop_dbg(dapm
->dev
, card
->pop_time
,
1417 "DAPM sequencing finished, waiting %dms\n", card
->pop_time
);
1418 pop_wait(card
->pop_time
);
1420 trace_snd_soc_dapm_done(card
);
1425 #ifdef CONFIG_DEBUG_FS
1426 static int dapm_widget_power_open_file(struct inode
*inode
, struct file
*file
)
1428 file
->private_data
= inode
->i_private
;
1432 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1433 char __user
*user_buf
,
1434 size_t count
, loff_t
*ppos
)
1436 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1440 struct snd_soc_dapm_path
*p
= NULL
;
1442 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1446 in
= is_connected_input_ep(w
);
1447 dapm_clear_walk(w
->dapm
);
1448 out
= is_connected_output_ep(w
);
1449 dapm_clear_walk(w
->dapm
);
1451 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s in %d out %d",
1452 w
->name
, w
->power
? "On" : "Off", in
, out
);
1455 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1456 " - R%d(0x%x) bit %d",
1457 w
->reg
, w
->reg
, w
->shift
);
1459 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
1462 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1464 w
->active
? "active" : "inactive");
1466 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1467 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1471 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1472 " in \"%s\" \"%s\"\n",
1473 p
->name
? p
->name
: "static",
1476 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1477 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1481 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1482 " out \"%s\" \"%s\"\n",
1483 p
->name
? p
->name
: "static",
1487 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1493 static const struct file_operations dapm_widget_power_fops
= {
1494 .open
= dapm_widget_power_open_file
,
1495 .read
= dapm_widget_power_read_file
,
1496 .llseek
= default_llseek
,
1499 static int dapm_bias_open_file(struct inode
*inode
, struct file
*file
)
1501 file
->private_data
= inode
->i_private
;
1505 static ssize_t
dapm_bias_read_file(struct file
*file
, char __user
*user_buf
,
1506 size_t count
, loff_t
*ppos
)
1508 struct snd_soc_dapm_context
*dapm
= file
->private_data
;
1511 switch (dapm
->bias_level
) {
1512 case SND_SOC_BIAS_ON
:
1515 case SND_SOC_BIAS_PREPARE
:
1516 level
= "Prepare\n";
1518 case SND_SOC_BIAS_STANDBY
:
1519 level
= "Standby\n";
1521 case SND_SOC_BIAS_OFF
:
1526 level
= "Unknown\n";
1530 return simple_read_from_buffer(user_buf
, count
, ppos
, level
,
1534 static const struct file_operations dapm_bias_fops
= {
1535 .open
= dapm_bias_open_file
,
1536 .read
= dapm_bias_read_file
,
1537 .llseek
= default_llseek
,
1540 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
1541 struct dentry
*parent
)
1545 dapm
->debugfs_dapm
= debugfs_create_dir("dapm", parent
);
1547 if (!dapm
->debugfs_dapm
) {
1549 "Failed to create DAPM debugfs directory\n");
1553 d
= debugfs_create_file("bias_level", 0444,
1554 dapm
->debugfs_dapm
, dapm
,
1558 "ASoC: Failed to create bias level debugfs file\n");
1561 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
1563 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
1566 if (!dapm
->debugfs_dapm
|| !w
->name
)
1569 d
= debugfs_create_file(w
->name
, 0444,
1570 dapm
->debugfs_dapm
, w
,
1571 &dapm_widget_power_fops
);
1573 dev_warn(w
->dapm
->dev
,
1574 "ASoC: Failed to create %s debugfs file\n",
1578 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
1580 debugfs_remove_recursive(dapm
->debugfs_dapm
);
1584 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
1585 struct dentry
*parent
)
1589 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
1593 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
1599 /* test and update the power status of a mux widget */
1600 static int dapm_mux_update_power(struct snd_soc_dapm_widget
*widget
,
1601 struct snd_kcontrol
*kcontrol
, int change
,
1602 int mux
, struct soc_enum
*e
)
1604 struct snd_soc_dapm_path
*path
;
1607 if (widget
->id
!= snd_soc_dapm_mux
&&
1608 widget
->id
!= snd_soc_dapm_virt_mux
&&
1609 widget
->id
!= snd_soc_dapm_value_mux
)
1615 /* find dapm widget path assoc with kcontrol */
1616 list_for_each_entry(path
, &widget
->dapm
->card
->paths
, list
) {
1617 if (path
->kcontrol
!= kcontrol
)
1620 if (!path
->name
|| !e
->texts
[mux
])
1624 /* we now need to match the string in the enum to the path */
1625 if (!(strcmp(path
->name
, e
->texts
[mux
]))) {
1626 path
->connect
= 1; /* new connection */
1627 dapm_mark_dirty(path
->source
);
1630 dapm_mark_dirty(path
->source
);
1631 path
->connect
= 0; /* old connection must be powered down */
1636 dapm_mark_dirty(widget
);
1637 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1643 /* test and update the power status of a mixer or switch widget */
1644 static int dapm_mixer_update_power(struct snd_soc_dapm_widget
*widget
,
1645 struct snd_kcontrol
*kcontrol
, int connect
)
1647 struct snd_soc_dapm_path
*path
;
1650 if (widget
->id
!= snd_soc_dapm_mixer
&&
1651 widget
->id
!= snd_soc_dapm_mixer_named_ctl
&&
1652 widget
->id
!= snd_soc_dapm_switch
)
1655 /* find dapm widget path assoc with kcontrol */
1656 list_for_each_entry(path
, &widget
->dapm
->card
->paths
, list
) {
1657 if (path
->kcontrol
!= kcontrol
)
1660 /* found, now check type */
1662 path
->connect
= connect
;
1663 dapm_mark_dirty(path
->source
);
1667 dapm_mark_dirty(widget
);
1668 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1674 /* show dapm widget status in sys fs */
1675 static ssize_t
dapm_widget_show(struct device
*dev
,
1676 struct device_attribute
*attr
, char *buf
)
1678 struct snd_soc_pcm_runtime
*rtd
=
1679 container_of(dev
, struct snd_soc_pcm_runtime
, dev
);
1680 struct snd_soc_codec
*codec
=rtd
->codec
;
1681 struct snd_soc_dapm_widget
*w
;
1683 char *state
= "not set";
1685 list_for_each_entry(w
, &codec
->card
->widgets
, list
) {
1686 if (w
->dapm
!= &codec
->dapm
)
1689 /* only display widgets that burnm power */
1691 case snd_soc_dapm_hp
:
1692 case snd_soc_dapm_mic
:
1693 case snd_soc_dapm_spk
:
1694 case snd_soc_dapm_line
:
1695 case snd_soc_dapm_micbias
:
1696 case snd_soc_dapm_dac
:
1697 case snd_soc_dapm_adc
:
1698 case snd_soc_dapm_pga
:
1699 case snd_soc_dapm_out_drv
:
1700 case snd_soc_dapm_mixer
:
1701 case snd_soc_dapm_mixer_named_ctl
:
1702 case snd_soc_dapm_supply
:
1704 count
+= sprintf(buf
+ count
, "%s: %s\n",
1705 w
->name
, w
->power
? "On":"Off");
1712 switch (codec
->dapm
.bias_level
) {
1713 case SND_SOC_BIAS_ON
:
1716 case SND_SOC_BIAS_PREPARE
:
1719 case SND_SOC_BIAS_STANDBY
:
1722 case SND_SOC_BIAS_OFF
:
1726 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
1731 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
1733 int snd_soc_dapm_sys_add(struct device
*dev
)
1735 return device_create_file(dev
, &dev_attr_dapm_widget
);
1738 static void snd_soc_dapm_sys_remove(struct device
*dev
)
1740 device_remove_file(dev
, &dev_attr_dapm_widget
);
1743 /* free all dapm widgets and resources */
1744 static void dapm_free_widgets(struct snd_soc_dapm_context
*dapm
)
1746 struct snd_soc_dapm_widget
*w
, *next_w
;
1747 struct snd_soc_dapm_path
*p
, *next_p
;
1749 list_for_each_entry_safe(w
, next_w
, &dapm
->card
->widgets
, list
) {
1750 if (w
->dapm
!= dapm
)
1754 * remove source and sink paths associated to this widget.
1755 * While removing the path, remove reference to it from both
1756 * source and sink widgets so that path is removed only once.
1758 list_for_each_entry_safe(p
, next_p
, &w
->sources
, list_sink
) {
1759 list_del(&p
->list_sink
);
1760 list_del(&p
->list_source
);
1762 kfree(p
->long_name
);
1765 list_for_each_entry_safe(p
, next_p
, &w
->sinks
, list_source
) {
1766 list_del(&p
->list_sink
);
1767 list_del(&p
->list_source
);
1769 kfree(p
->long_name
);
1772 kfree(w
->kcontrols
);
1778 static struct snd_soc_dapm_widget
*dapm_find_widget(
1779 struct snd_soc_dapm_context
*dapm
, const char *pin
,
1780 bool search_other_contexts
)
1782 struct snd_soc_dapm_widget
*w
;
1783 struct snd_soc_dapm_widget
*fallback
= NULL
;
1785 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
1786 if (!strcmp(w
->name
, pin
)) {
1787 if (w
->dapm
== dapm
)
1794 if (search_other_contexts
)
1800 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context
*dapm
,
1801 const char *pin
, int status
)
1803 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
1806 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
1810 w
->connected
= status
;
1819 * snd_soc_dapm_sync - scan and power dapm paths
1820 * @dapm: DAPM context
1822 * Walks all dapm audio paths and powers widgets according to their
1823 * stream or path usage.
1825 * Returns 0 for success.
1827 int snd_soc_dapm_sync(struct snd_soc_dapm_context
*dapm
)
1829 return dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
1831 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
1833 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context
*dapm
,
1834 const struct snd_soc_dapm_route
*route
)
1836 struct snd_soc_dapm_path
*path
;
1837 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
1838 struct snd_soc_dapm_widget
*wtsource
= NULL
, *wtsink
= NULL
;
1840 const char *control
= route
->control
;
1842 char prefixed_sink
[80];
1843 char prefixed_source
[80];
1846 if (dapm
->codec
&& dapm
->codec
->name_prefix
) {
1847 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
1848 dapm
->codec
->name_prefix
, route
->sink
);
1849 sink
= prefixed_sink
;
1850 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
1851 dapm
->codec
->name_prefix
, route
->source
);
1852 source
= prefixed_source
;
1855 source
= route
->source
;
1859 * find src and dest widgets over all widgets but favor a widget from
1860 * current DAPM context
1862 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
1863 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
1865 if (w
->dapm
== dapm
)
1869 if (!wsource
&& !(strcmp(w
->name
, source
))) {
1871 if (w
->dapm
== dapm
)
1875 /* use widget from another DAPM context if not found from this */
1881 if (wsource
== NULL
|| wsink
== NULL
)
1884 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
1888 path
->source
= wsource
;
1890 path
->connected
= route
->connected
;
1891 INIT_LIST_HEAD(&path
->list
);
1892 INIT_LIST_HEAD(&path
->list_source
);
1893 INIT_LIST_HEAD(&path
->list_sink
);
1895 /* check for external widgets */
1896 if (wsink
->id
== snd_soc_dapm_input
) {
1897 if (wsource
->id
== snd_soc_dapm_micbias
||
1898 wsource
->id
== snd_soc_dapm_mic
||
1899 wsource
->id
== snd_soc_dapm_line
||
1900 wsource
->id
== snd_soc_dapm_output
)
1903 if (wsource
->id
== snd_soc_dapm_output
) {
1904 if (wsink
->id
== snd_soc_dapm_spk
||
1905 wsink
->id
== snd_soc_dapm_hp
||
1906 wsink
->id
== snd_soc_dapm_line
||
1907 wsink
->id
== snd_soc_dapm_input
)
1911 /* connect static paths */
1912 if (control
== NULL
) {
1913 list_add(&path
->list
, &dapm
->card
->paths
);
1914 list_add(&path
->list_sink
, &wsink
->sources
);
1915 list_add(&path
->list_source
, &wsource
->sinks
);
1920 /* connect dynamic paths */
1921 switch (wsink
->id
) {
1922 case snd_soc_dapm_adc
:
1923 case snd_soc_dapm_dac
:
1924 case snd_soc_dapm_pga
:
1925 case snd_soc_dapm_out_drv
:
1926 case snd_soc_dapm_input
:
1927 case snd_soc_dapm_output
:
1928 case snd_soc_dapm_micbias
:
1929 case snd_soc_dapm_vmid
:
1930 case snd_soc_dapm_pre
:
1931 case snd_soc_dapm_post
:
1932 case snd_soc_dapm_supply
:
1933 case snd_soc_dapm_aif_in
:
1934 case snd_soc_dapm_aif_out
:
1935 list_add(&path
->list
, &dapm
->card
->paths
);
1936 list_add(&path
->list_sink
, &wsink
->sources
);
1937 list_add(&path
->list_source
, &wsource
->sinks
);
1940 case snd_soc_dapm_mux
:
1941 case snd_soc_dapm_virt_mux
:
1942 case snd_soc_dapm_value_mux
:
1943 ret
= dapm_connect_mux(dapm
, wsource
, wsink
, path
, control
,
1944 &wsink
->kcontrol_news
[0]);
1948 case snd_soc_dapm_switch
:
1949 case snd_soc_dapm_mixer
:
1950 case snd_soc_dapm_mixer_named_ctl
:
1951 ret
= dapm_connect_mixer(dapm
, wsource
, wsink
, path
, control
);
1955 case snd_soc_dapm_hp
:
1956 case snd_soc_dapm_mic
:
1957 case snd_soc_dapm_line
:
1958 case snd_soc_dapm_spk
:
1959 list_add(&path
->list
, &dapm
->card
->paths
);
1960 list_add(&path
->list_sink
, &wsink
->sources
);
1961 list_add(&path
->list_source
, &wsource
->sinks
);
1968 dev_warn(dapm
->dev
, "asoc: no dapm match for %s --> %s --> %s\n",
1969 source
, control
, sink
);
1975 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1976 * @dapm: DAPM context
1977 * @route: audio routes
1978 * @num: number of routes
1980 * Connects 2 dapm widgets together via a named audio path. The sink is
1981 * the widget receiving the audio signal, whilst the source is the sender
1982 * of the audio signal.
1984 * Returns 0 for success else error. On error all resources can be freed
1985 * with a call to snd_soc_card_free().
1987 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context
*dapm
,
1988 const struct snd_soc_dapm_route
*route
, int num
)
1992 for (i
= 0; i
< num
; i
++) {
1993 ret
= snd_soc_dapm_add_route(dapm
, route
);
1995 dev_err(dapm
->dev
, "Failed to add route %s->%s\n",
1996 route
->source
, route
->sink
);
2004 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
2006 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context
*dapm
,
2007 const struct snd_soc_dapm_route
*route
)
2009 struct snd_soc_dapm_widget
*source
= dapm_find_widget(dapm
,
2012 struct snd_soc_dapm_widget
*sink
= dapm_find_widget(dapm
,
2015 struct snd_soc_dapm_path
*path
;
2019 dev_err(dapm
->dev
, "Unable to find source %s for weak route\n",
2025 dev_err(dapm
->dev
, "Unable to find sink %s for weak route\n",
2030 if (route
->control
|| route
->connected
)
2031 dev_warn(dapm
->dev
, "Ignoring control for weak route %s->%s\n",
2032 route
->source
, route
->sink
);
2034 list_for_each_entry(path
, &source
->sinks
, list_source
) {
2035 if (path
->sink
== sink
) {
2042 dev_err(dapm
->dev
, "No path found for weak route %s->%s\n",
2043 route
->source
, route
->sink
);
2045 dev_warn(dapm
->dev
, "%d paths found for weak route %s->%s\n",
2046 count
, route
->source
, route
->sink
);
2052 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2053 * @dapm: DAPM context
2054 * @route: audio routes
2055 * @num: number of routes
2057 * Mark existing routes matching those specified in the passed array
2058 * as being weak, meaning that they are ignored for the purpose of
2059 * power decisions. The main intended use case is for sidetone paths
2060 * which couple audio between other independent paths if they are both
2061 * active in order to make the combination work better at the user
2062 * level but which aren't intended to be "used".
2064 * Note that CODEC drivers should not use this as sidetone type paths
2065 * can frequently also be used as bypass paths.
2067 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context
*dapm
,
2068 const struct snd_soc_dapm_route
*route
, int num
)
2073 for (i
= 0; i
< num
; i
++) {
2074 err
= snd_soc_dapm_weak_route(dapm
, route
);
2082 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes
);
2085 * snd_soc_dapm_new_widgets - add new dapm widgets
2086 * @dapm: DAPM context
2088 * Checks the codec for any new dapm widgets and creates them if found.
2090 * Returns 0 for success.
2092 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context
*dapm
)
2094 struct snd_soc_dapm_widget
*w
;
2097 list_for_each_entry(w
, &dapm
->card
->widgets
, list
)
2102 if (w
->num_kcontrols
) {
2103 w
->kcontrols
= kzalloc(w
->num_kcontrols
*
2104 sizeof(struct snd_kcontrol
*),
2111 case snd_soc_dapm_switch
:
2112 case snd_soc_dapm_mixer
:
2113 case snd_soc_dapm_mixer_named_ctl
:
2114 w
->power_check
= dapm_generic_check_power
;
2117 case snd_soc_dapm_mux
:
2118 case snd_soc_dapm_virt_mux
:
2119 case snd_soc_dapm_value_mux
:
2120 w
->power_check
= dapm_generic_check_power
;
2123 case snd_soc_dapm_adc
:
2124 case snd_soc_dapm_aif_out
:
2125 w
->power_check
= dapm_adc_check_power
;
2127 case snd_soc_dapm_dac
:
2128 case snd_soc_dapm_aif_in
:
2129 w
->power_check
= dapm_dac_check_power
;
2131 case snd_soc_dapm_pga
:
2132 case snd_soc_dapm_out_drv
:
2133 w
->power_check
= dapm_generic_check_power
;
2136 case snd_soc_dapm_input
:
2137 case snd_soc_dapm_output
:
2138 case snd_soc_dapm_micbias
:
2139 case snd_soc_dapm_spk
:
2140 case snd_soc_dapm_hp
:
2141 case snd_soc_dapm_mic
:
2142 case snd_soc_dapm_line
:
2143 w
->power_check
= dapm_generic_check_power
;
2145 case snd_soc_dapm_supply
:
2146 w
->power_check
= dapm_supply_check_power
;
2147 case snd_soc_dapm_vmid
:
2148 case snd_soc_dapm_pre
:
2149 case snd_soc_dapm_post
:
2153 if (!w
->power_check
)
2154 w
->power_check
= dapm_always_on_check_power
;
2156 /* Read the initial power state from the device */
2158 val
= soc_widget_read(w
, w
->reg
);
2159 val
&= 1 << w
->shift
;
2169 list_add(&w
->dirty
, &(w
->dapm
->card
->dapm_dirty
));
2170 dapm_debugfs_add_widget(w
);
2173 dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
2176 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
2179 * snd_soc_dapm_get_volsw - dapm mixer get callback
2180 * @kcontrol: mixer control
2181 * @ucontrol: control element information
2183 * Callback to get the value of a dapm mixer control.
2185 * Returns 0 for success.
2187 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
2188 struct snd_ctl_elem_value
*ucontrol
)
2190 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2191 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2192 struct soc_mixer_control
*mc
=
2193 (struct soc_mixer_control
*)kcontrol
->private_value
;
2194 unsigned int reg
= mc
->reg
;
2195 unsigned int shift
= mc
->shift
;
2196 unsigned int rshift
= mc
->rshift
;
2198 unsigned int invert
= mc
->invert
;
2199 unsigned int mask
= (1 << fls(max
)) - 1;
2201 ucontrol
->value
.integer
.value
[0] =
2202 (snd_soc_read(widget
->codec
, reg
) >> shift
) & mask
;
2203 if (shift
!= rshift
)
2204 ucontrol
->value
.integer
.value
[1] =
2205 (snd_soc_read(widget
->codec
, reg
) >> rshift
) & mask
;
2207 ucontrol
->value
.integer
.value
[0] =
2208 max
- ucontrol
->value
.integer
.value
[0];
2209 if (shift
!= rshift
)
2210 ucontrol
->value
.integer
.value
[1] =
2211 max
- ucontrol
->value
.integer
.value
[1];
2216 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
2219 * snd_soc_dapm_put_volsw - dapm mixer set callback
2220 * @kcontrol: mixer control
2221 * @ucontrol: control element information
2223 * Callback to set the value of a dapm mixer control.
2225 * Returns 0 for success.
2227 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
2228 struct snd_ctl_elem_value
*ucontrol
)
2230 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2231 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2232 struct snd_soc_codec
*codec
= widget
->codec
;
2233 struct soc_mixer_control
*mc
=
2234 (struct soc_mixer_control
*)kcontrol
->private_value
;
2235 unsigned int reg
= mc
->reg
;
2236 unsigned int shift
= mc
->shift
;
2238 unsigned int mask
= (1 << fls(max
)) - 1;
2239 unsigned int invert
= mc
->invert
;
2241 int connect
, change
;
2242 struct snd_soc_dapm_update update
;
2245 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2249 mask
= mask
<< shift
;
2253 /* new connection */
2254 connect
= invert
? 0 : 1;
2256 /* old connection must be powered down */
2257 connect
= invert
? 1 : 0;
2259 mutex_lock(&codec
->mutex
);
2261 change
= snd_soc_test_bits(widget
->codec
, reg
, mask
, val
);
2263 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2264 widget
= wlist
->widgets
[wi
];
2266 widget
->value
= val
;
2268 update
.kcontrol
= kcontrol
;
2269 update
.widget
= widget
;
2273 widget
->dapm
->update
= &update
;
2275 dapm_mixer_update_power(widget
, kcontrol
, connect
);
2277 widget
->dapm
->update
= NULL
;
2281 mutex_unlock(&codec
->mutex
);
2284 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
2287 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2288 * @kcontrol: mixer control
2289 * @ucontrol: control element information
2291 * Callback to get the value of a dapm enumerated double mixer control.
2293 * Returns 0 for success.
2295 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
2296 struct snd_ctl_elem_value
*ucontrol
)
2298 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2299 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2300 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2301 unsigned int val
, bitmask
;
2303 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2305 val
= snd_soc_read(widget
->codec
, e
->reg
);
2306 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & (bitmask
- 1);
2307 if (e
->shift_l
!= e
->shift_r
)
2308 ucontrol
->value
.enumerated
.item
[1] =
2309 (val
>> e
->shift_r
) & (bitmask
- 1);
2313 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
2316 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2317 * @kcontrol: mixer control
2318 * @ucontrol: control element information
2320 * Callback to set the value of a dapm enumerated double mixer control.
2322 * Returns 0 for success.
2324 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
2325 struct snd_ctl_elem_value
*ucontrol
)
2327 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2328 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2329 struct snd_soc_codec
*codec
= widget
->codec
;
2330 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2331 unsigned int val
, mux
, change
;
2332 unsigned int mask
, bitmask
;
2333 struct snd_soc_dapm_update update
;
2336 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2338 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2340 mux
= ucontrol
->value
.enumerated
.item
[0];
2341 val
= mux
<< e
->shift_l
;
2342 mask
= (bitmask
- 1) << e
->shift_l
;
2343 if (e
->shift_l
!= e
->shift_r
) {
2344 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2346 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
2347 mask
|= (bitmask
- 1) << e
->shift_r
;
2350 mutex_lock(&codec
->mutex
);
2352 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
2354 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2355 widget
= wlist
->widgets
[wi
];
2357 widget
->value
= val
;
2359 update
.kcontrol
= kcontrol
;
2360 update
.widget
= widget
;
2361 update
.reg
= e
->reg
;
2364 widget
->dapm
->update
= &update
;
2366 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
2368 widget
->dapm
->update
= NULL
;
2372 mutex_unlock(&codec
->mutex
);
2375 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
2378 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2379 * @kcontrol: mixer control
2380 * @ucontrol: control element information
2382 * Returns 0 for success.
2384 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol
*kcontrol
,
2385 struct snd_ctl_elem_value
*ucontrol
)
2387 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2388 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2390 ucontrol
->value
.enumerated
.item
[0] = widget
->value
;
2394 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt
);
2397 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2398 * @kcontrol: mixer control
2399 * @ucontrol: control element information
2401 * Returns 0 for success.
2403 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol
*kcontrol
,
2404 struct snd_ctl_elem_value
*ucontrol
)
2406 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2407 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2408 struct snd_soc_codec
*codec
= widget
->codec
;
2409 struct soc_enum
*e
=
2410 (struct soc_enum
*)kcontrol
->private_value
;
2415 if (ucontrol
->value
.enumerated
.item
[0] >= e
->max
)
2418 mutex_lock(&codec
->mutex
);
2420 change
= widget
->value
!= ucontrol
->value
.enumerated
.item
[0];
2422 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2423 widget
= wlist
->widgets
[wi
];
2425 widget
->value
= ucontrol
->value
.enumerated
.item
[0];
2427 dapm_mux_update_power(widget
, kcontrol
, change
,
2432 mutex_unlock(&codec
->mutex
);
2435 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt
);
2438 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2440 * @kcontrol: mixer control
2441 * @ucontrol: control element information
2443 * Callback to get the value of a dapm semi enumerated double mixer control.
2445 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2446 * used for handling bitfield coded enumeration for example.
2448 * Returns 0 for success.
2450 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
2451 struct snd_ctl_elem_value
*ucontrol
)
2453 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2454 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2455 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2456 unsigned int reg_val
, val
, mux
;
2458 reg_val
= snd_soc_read(widget
->codec
, e
->reg
);
2459 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
2460 for (mux
= 0; mux
< e
->max
; mux
++) {
2461 if (val
== e
->values
[mux
])
2464 ucontrol
->value
.enumerated
.item
[0] = mux
;
2465 if (e
->shift_l
!= e
->shift_r
) {
2466 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
2467 for (mux
= 0; mux
< e
->max
; mux
++) {
2468 if (val
== e
->values
[mux
])
2471 ucontrol
->value
.enumerated
.item
[1] = mux
;
2476 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double
);
2479 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2481 * @kcontrol: mixer control
2482 * @ucontrol: control element information
2484 * Callback to set the value of a dapm semi enumerated double mixer control.
2486 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2487 * used for handling bitfield coded enumeration for example.
2489 * Returns 0 for success.
2491 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
2492 struct snd_ctl_elem_value
*ucontrol
)
2494 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2495 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2496 struct snd_soc_codec
*codec
= widget
->codec
;
2497 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2498 unsigned int val
, mux
, change
;
2500 struct snd_soc_dapm_update update
;
2503 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2505 mux
= ucontrol
->value
.enumerated
.item
[0];
2506 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
2507 mask
= e
->mask
<< e
->shift_l
;
2508 if (e
->shift_l
!= e
->shift_r
) {
2509 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2511 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
2512 mask
|= e
->mask
<< e
->shift_r
;
2515 mutex_lock(&codec
->mutex
);
2517 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
2519 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2520 widget
= wlist
->widgets
[wi
];
2522 widget
->value
= val
;
2524 update
.kcontrol
= kcontrol
;
2525 update
.widget
= widget
;
2526 update
.reg
= e
->reg
;
2529 widget
->dapm
->update
= &update
;
2531 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
2533 widget
->dapm
->update
= NULL
;
2537 mutex_unlock(&codec
->mutex
);
2540 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double
);
2543 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2545 * @kcontrol: mixer control
2546 * @uinfo: control element information
2548 * Callback to provide information about a pin switch control.
2550 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
2551 struct snd_ctl_elem_info
*uinfo
)
2553 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2555 uinfo
->value
.integer
.min
= 0;
2556 uinfo
->value
.integer
.max
= 1;
2560 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
2563 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2565 * @kcontrol: mixer control
2568 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
2569 struct snd_ctl_elem_value
*ucontrol
)
2571 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2572 const char *pin
= (const char *)kcontrol
->private_value
;
2574 mutex_lock(&codec
->mutex
);
2576 ucontrol
->value
.integer
.value
[0] =
2577 snd_soc_dapm_get_pin_status(&codec
->dapm
, pin
);
2579 mutex_unlock(&codec
->mutex
);
2583 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
2586 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2588 * @kcontrol: mixer control
2591 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
2592 struct snd_ctl_elem_value
*ucontrol
)
2594 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2595 const char *pin
= (const char *)kcontrol
->private_value
;
2597 mutex_lock(&codec
->mutex
);
2599 if (ucontrol
->value
.integer
.value
[0])
2600 snd_soc_dapm_enable_pin(&codec
->dapm
, pin
);
2602 snd_soc_dapm_disable_pin(&codec
->dapm
, pin
);
2604 snd_soc_dapm_sync(&codec
->dapm
);
2606 mutex_unlock(&codec
->mutex
);
2610 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
2613 * snd_soc_dapm_new_control - create new dapm control
2614 * @dapm: DAPM context
2615 * @widget: widget template
2617 * Creates a new dapm control based upon the template.
2619 * Returns 0 for success else error.
2621 int snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
2622 const struct snd_soc_dapm_widget
*widget
)
2624 struct snd_soc_dapm_widget
*w
;
2627 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
2630 name_len
= strlen(widget
->name
) + 1;
2631 if (dapm
->codec
&& dapm
->codec
->name_prefix
)
2632 name_len
+= 1 + strlen(dapm
->codec
->name_prefix
);
2633 w
->name
= kmalloc(name_len
, GFP_KERNEL
);
2634 if (w
->name
== NULL
) {
2638 if (dapm
->codec
&& dapm
->codec
->name_prefix
)
2639 snprintf(w
->name
, name_len
, "%s %s",
2640 dapm
->codec
->name_prefix
, widget
->name
);
2642 snprintf(w
->name
, name_len
, "%s", widget
->name
);
2646 w
->codec
= dapm
->codec
;
2647 w
->platform
= dapm
->platform
;
2648 INIT_LIST_HEAD(&w
->sources
);
2649 INIT_LIST_HEAD(&w
->sinks
);
2650 INIT_LIST_HEAD(&w
->list
);
2651 INIT_LIST_HEAD(&w
->dirty
);
2652 list_add(&w
->list
, &dapm
->card
->widgets
);
2654 /* machine layer set ups unconnected pins and insertions */
2658 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
2661 * snd_soc_dapm_new_controls - create new dapm controls
2662 * @dapm: DAPM context
2663 * @widget: widget array
2664 * @num: number of widgets
2666 * Creates new DAPM controls based upon the templates.
2668 * Returns 0 for success else error.
2670 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context
*dapm
,
2671 const struct snd_soc_dapm_widget
*widget
,
2676 for (i
= 0; i
< num
; i
++) {
2677 ret
= snd_soc_dapm_new_control(dapm
, widget
);
2680 "ASoC: Failed to create DAPM control %s: %d\n",
2688 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
2690 static void soc_dapm_stream_event(struct snd_soc_dapm_context
*dapm
,
2691 const char *stream
, int event
)
2693 struct snd_soc_dapm_widget
*w
;
2695 list_for_each_entry(w
, &dapm
->card
->widgets
, list
)
2697 if (!w
->sname
|| w
->dapm
!= dapm
)
2699 dev_vdbg(w
->dapm
->dev
, "widget %s\n %s stream %s event %d\n",
2700 w
->name
, w
->sname
, stream
, event
);
2701 if (strstr(w
->sname
, stream
)) {
2704 case SND_SOC_DAPM_STREAM_START
:
2707 case SND_SOC_DAPM_STREAM_STOP
:
2710 case SND_SOC_DAPM_STREAM_SUSPEND
:
2711 case SND_SOC_DAPM_STREAM_RESUME
:
2712 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
2713 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
2719 dapm_power_widgets(dapm
, event
);
2721 /* do we need to notify any clients that DAPM stream is complete */
2722 if (dapm
->stream_event
)
2723 dapm
->stream_event(dapm
, event
);
2727 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2728 * @rtd: PCM runtime data
2729 * @stream: stream name
2730 * @event: stream event
2732 * Sends a stream event to the dapm core. The core then makes any
2733 * necessary widget power changes.
2735 * Returns 0 for success else error.
2737 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
,
2738 const char *stream
, int event
)
2740 struct snd_soc_codec
*codec
= rtd
->codec
;
2745 mutex_lock(&codec
->mutex
);
2746 soc_dapm_stream_event(&codec
->dapm
, stream
, event
);
2747 mutex_unlock(&codec
->mutex
);
2752 * snd_soc_dapm_enable_pin - enable pin.
2753 * @dapm: DAPM context
2756 * Enables input/output pin and its parents or children widgets iff there is
2757 * a valid audio route and active audio stream.
2758 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2759 * do any widget power switching.
2761 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
2763 return snd_soc_dapm_set_pin(dapm
, pin
, 1);
2765 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
2768 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2769 * @dapm: DAPM context
2772 * Enables input/output pin regardless of any other state. This is
2773 * intended for use with microphone bias supplies used in microphone
2776 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2777 * do any widget power switching.
2779 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context
*dapm
,
2782 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
2785 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
2789 dev_dbg(w
->dapm
->dev
, "dapm: force enable pin %s\n", pin
);
2796 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
2799 * snd_soc_dapm_disable_pin - disable pin.
2800 * @dapm: DAPM context
2803 * Disables input/output pin and its parents or children widgets.
2804 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2805 * do any widget power switching.
2807 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context
*dapm
,
2810 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
2812 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
2815 * snd_soc_dapm_nc_pin - permanently disable pin.
2816 * @dapm: DAPM context
2819 * Marks the specified pin as being not connected, disabling it along
2820 * any parent or child widgets. At present this is identical to
2821 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2822 * additional things such as disabling controls which only affect
2823 * paths through the pin.
2825 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2826 * do any widget power switching.
2828 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
2830 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
2832 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
2835 * snd_soc_dapm_get_pin_status - get audio pin status
2836 * @dapm: DAPM context
2837 * @pin: audio signal pin endpoint (or start point)
2839 * Get audio pin status - connected or disconnected.
2841 * Returns 1 for connected otherwise 0.
2843 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context
*dapm
,
2846 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
2849 return w
->connected
;
2853 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
2856 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2857 * @dapm: DAPM context
2858 * @pin: audio signal pin endpoint (or start point)
2860 * Mark the given endpoint or pin as ignoring suspend. When the
2861 * system is disabled a path between two endpoints flagged as ignoring
2862 * suspend will not be disabled. The path must already be enabled via
2863 * normal means at suspend time, it will not be turned on if it was not
2866 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context
*dapm
,
2869 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, false);
2872 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
2876 w
->ignore_suspend
= 1;
2880 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
2883 * snd_soc_dapm_free - free dapm resources
2884 * @dapm: DAPM context
2886 * Free all dapm widgets and resources.
2888 void snd_soc_dapm_free(struct snd_soc_dapm_context
*dapm
)
2890 snd_soc_dapm_sys_remove(dapm
->dev
);
2891 dapm_debugfs_cleanup(dapm
);
2892 dapm_free_widgets(dapm
);
2893 list_del(&dapm
->list
);
2895 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
2897 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context
*dapm
)
2899 struct snd_soc_dapm_widget
*w
;
2900 LIST_HEAD(down_list
);
2903 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
2904 if (w
->dapm
!= dapm
)
2907 dapm_seq_insert(w
, &down_list
, false);
2913 /* If there were no widgets to power down we're already in
2917 snd_soc_dapm_set_bias_level(dapm
, SND_SOC_BIAS_PREPARE
);
2918 dapm_seq_run(dapm
, &down_list
, 0, false);
2919 snd_soc_dapm_set_bias_level(dapm
, SND_SOC_BIAS_STANDBY
);
2924 * snd_soc_dapm_shutdown - callback for system shutdown
2926 void snd_soc_dapm_shutdown(struct snd_soc_card
*card
)
2928 struct snd_soc_codec
*codec
;
2930 list_for_each_entry(codec
, &card
->codec_dev_list
, list
) {
2931 soc_dapm_shutdown_codec(&codec
->dapm
);
2932 snd_soc_dapm_set_bias_level(&codec
->dapm
, SND_SOC_BIAS_OFF
);
2936 /* Module information */
2937 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2938 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2939 MODULE_LICENSE("GPL");