2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
25 * o DAPM power change sequencing - allow for configurable per
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/init.h>
35 #include <linux/async.h>
36 #include <linux/delay.h>
38 #include <linux/bitops.h>
39 #include <linux/platform_device.h>
40 #include <linux/jiffies.h>
41 #include <linux/debugfs.h>
42 #include <linux/pm_runtime.h>
43 #include <linux/slab.h>
44 #include <sound/core.h>
45 #include <sound/pcm.h>
46 #include <sound/pcm_params.h>
47 #include <sound/soc.h>
48 #include <sound/initval.h>
50 #include <trace/events/asoc.h>
52 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
54 /* dapm power sequences - make this per codec in the future */
55 static int dapm_up_seq
[] = {
56 [snd_soc_dapm_pre
] = 0,
57 [snd_soc_dapm_supply
] = 1,
58 [snd_soc_dapm_micbias
] = 2,
59 [snd_soc_dapm_aif_in
] = 3,
60 [snd_soc_dapm_aif_out
] = 3,
61 [snd_soc_dapm_mic
] = 4,
62 [snd_soc_dapm_mux
] = 5,
63 [snd_soc_dapm_virt_mux
] = 5,
64 [snd_soc_dapm_value_mux
] = 5,
65 [snd_soc_dapm_dac
] = 6,
66 [snd_soc_dapm_mixer
] = 7,
67 [snd_soc_dapm_mixer_named_ctl
] = 7,
68 [snd_soc_dapm_pga
] = 8,
69 [snd_soc_dapm_adc
] = 9,
70 [snd_soc_dapm_out_drv
] = 10,
71 [snd_soc_dapm_hp
] = 10,
72 [snd_soc_dapm_spk
] = 10,
73 [snd_soc_dapm_line
] = 10,
74 [snd_soc_dapm_post
] = 11,
77 static int dapm_down_seq
[] = {
78 [snd_soc_dapm_pre
] = 0,
79 [snd_soc_dapm_adc
] = 1,
80 [snd_soc_dapm_hp
] = 2,
81 [snd_soc_dapm_spk
] = 2,
82 [snd_soc_dapm_line
] = 2,
83 [snd_soc_dapm_out_drv
] = 2,
84 [snd_soc_dapm_pga
] = 4,
85 [snd_soc_dapm_mixer_named_ctl
] = 5,
86 [snd_soc_dapm_mixer
] = 5,
87 [snd_soc_dapm_dac
] = 6,
88 [snd_soc_dapm_mic
] = 7,
89 [snd_soc_dapm_micbias
] = 8,
90 [snd_soc_dapm_mux
] = 9,
91 [snd_soc_dapm_virt_mux
] = 9,
92 [snd_soc_dapm_value_mux
] = 9,
93 [snd_soc_dapm_aif_in
] = 10,
94 [snd_soc_dapm_aif_out
] = 10,
95 [snd_soc_dapm_supply
] = 11,
96 [snd_soc_dapm_post
] = 12,
99 static void pop_wait(u32 pop_time
)
102 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time
));
105 static void pop_dbg(struct device
*dev
, u32 pop_time
, const char *fmt
, ...)
113 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
118 vsnprintf(buf
, PAGE_SIZE
, fmt
, args
);
119 dev_info(dev
, "%s", buf
);
125 static bool dapm_dirty_widget(struct snd_soc_dapm_widget
*w
)
127 return !list_empty(&w
->dirty
);
130 void dapm_mark_dirty(struct snd_soc_dapm_widget
*w
, const char *reason
)
132 if (!dapm_dirty_widget(w
)) {
133 dev_vdbg(w
->dapm
->dev
, "Marking %s dirty due to %s\n",
135 list_add_tail(&w
->dirty
, &w
->dapm
->card
->dapm_dirty
);
138 EXPORT_SYMBOL_GPL(dapm_mark_dirty
);
140 /* create a new dapm widget */
141 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
142 const struct snd_soc_dapm_widget
*_widget
)
144 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
147 /* get snd_card from DAPM context */
148 static inline struct snd_card
*dapm_get_snd_card(
149 struct snd_soc_dapm_context
*dapm
)
152 return dapm
->codec
->card
->snd_card
;
153 else if (dapm
->platform
)
154 return dapm
->platform
->card
->snd_card
;
162 /* get soc_card from DAPM context */
163 static inline struct snd_soc_card
*dapm_get_soc_card(
164 struct snd_soc_dapm_context
*dapm
)
167 return dapm
->codec
->card
;
168 else if (dapm
->platform
)
169 return dapm
->platform
->card
;
177 static int soc_widget_read(struct snd_soc_dapm_widget
*w
, int reg
)
180 return snd_soc_read(w
->codec
, reg
);
181 else if (w
->platform
)
182 return snd_soc_platform_read(w
->platform
, reg
);
184 dev_err(w
->dapm
->dev
, "no valid widget read method\n");
188 static int soc_widget_write(struct snd_soc_dapm_widget
*w
, int reg
, int val
)
191 return snd_soc_write(w
->codec
, reg
, val
);
192 else if (w
->platform
)
193 return snd_soc_platform_write(w
->platform
, reg
, val
);
195 dev_err(w
->dapm
->dev
, "no valid widget write method\n");
199 static int soc_widget_update_bits(struct snd_soc_dapm_widget
*w
,
200 unsigned short reg
, unsigned int mask
, unsigned int value
)
203 unsigned int old
, new;
206 ret
= soc_widget_read(w
, reg
);
211 new = (old
& ~mask
) | (value
& mask
);
214 ret
= soc_widget_write(w
, reg
, new);
223 * snd_soc_dapm_set_bias_level - set the bias level for the system
224 * @dapm: DAPM context
225 * @level: level to configure
227 * Configure the bias (power) levels for the SoC audio device.
229 * Returns 0 for success else error.
231 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context
*dapm
,
232 enum snd_soc_bias_level level
)
234 struct snd_soc_card
*card
= dapm
->card
;
237 trace_snd_soc_bias_level_start(card
, level
);
239 if (card
&& card
->set_bias_level
)
240 ret
= card
->set_bias_level(card
, dapm
, level
);
245 if (dapm
->codec
->driver
->set_bias_level
)
246 ret
= dapm
->codec
->driver
->set_bias_level(dapm
->codec
,
249 dapm
->bias_level
= level
;
254 if (card
&& card
->set_bias_level_post
)
255 ret
= card
->set_bias_level_post(card
, dapm
, level
);
257 trace_snd_soc_bias_level_done(card
, level
);
262 /* set up initial codec paths */
263 static void dapm_set_path_status(struct snd_soc_dapm_widget
*w
,
264 struct snd_soc_dapm_path
*p
, int i
)
267 case snd_soc_dapm_switch
:
268 case snd_soc_dapm_mixer
:
269 case snd_soc_dapm_mixer_named_ctl
: {
271 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
272 w
->kcontrol_news
[i
].private_value
;
273 unsigned int reg
= mc
->reg
;
274 unsigned int shift
= mc
->shift
;
276 unsigned int mask
= (1 << fls(max
)) - 1;
277 unsigned int invert
= mc
->invert
;
279 val
= soc_widget_read(w
, reg
);
280 val
= (val
>> shift
) & mask
;
282 if ((invert
&& !val
) || (!invert
&& val
))
288 case snd_soc_dapm_mux
: {
289 struct soc_enum
*e
= (struct soc_enum
*)
290 w
->kcontrol_news
[i
].private_value
;
291 int val
, item
, bitmask
;
293 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
295 val
= soc_widget_read(w
, e
->reg
);
296 item
= (val
>> e
->shift_l
) & (bitmask
- 1);
299 for (i
= 0; i
< e
->max
; i
++) {
300 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
305 case snd_soc_dapm_virt_mux
: {
306 struct soc_enum
*e
= (struct soc_enum
*)
307 w
->kcontrol_news
[i
].private_value
;
310 /* since a virtual mux has no backing registers to
311 * decide which path to connect, it will try to match
312 * with the first enumeration. This is to ensure
313 * that the default mux choice (the first) will be
314 * correctly powered up during initialization.
316 if (!strcmp(p
->name
, e
->texts
[0]))
320 case snd_soc_dapm_value_mux
: {
321 struct soc_enum
*e
= (struct soc_enum
*)
322 w
->kcontrol_news
[i
].private_value
;
325 val
= soc_widget_read(w
, e
->reg
);
326 val
= (val
>> e
->shift_l
) & e
->mask
;
327 for (item
= 0; item
< e
->max
; item
++) {
328 if (val
== e
->values
[item
])
333 for (i
= 0; i
< e
->max
; i
++) {
334 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
339 /* does not affect routing - always connected */
340 case snd_soc_dapm_pga
:
341 case snd_soc_dapm_out_drv
:
342 case snd_soc_dapm_output
:
343 case snd_soc_dapm_adc
:
344 case snd_soc_dapm_input
:
345 case snd_soc_dapm_siggen
:
346 case snd_soc_dapm_dac
:
347 case snd_soc_dapm_micbias
:
348 case snd_soc_dapm_vmid
:
349 case snd_soc_dapm_supply
:
350 case snd_soc_dapm_aif_in
:
351 case snd_soc_dapm_aif_out
:
352 case snd_soc_dapm_hp
:
353 case snd_soc_dapm_mic
:
354 case snd_soc_dapm_spk
:
355 case snd_soc_dapm_line
:
358 /* does affect routing - dynamically connected */
359 case snd_soc_dapm_pre
:
360 case snd_soc_dapm_post
:
366 /* connect mux widget to its interconnecting audio paths */
367 static int dapm_connect_mux(struct snd_soc_dapm_context
*dapm
,
368 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
369 struct snd_soc_dapm_path
*path
, const char *control_name
,
370 const struct snd_kcontrol_new
*kcontrol
)
372 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
375 for (i
= 0; i
< e
->max
; i
++) {
376 if (!(strcmp(control_name
, e
->texts
[i
]))) {
377 list_add(&path
->list
, &dapm
->card
->paths
);
378 list_add(&path
->list_sink
, &dest
->sources
);
379 list_add(&path
->list_source
, &src
->sinks
);
380 path
->name
= (char*)e
->texts
[i
];
381 dapm_set_path_status(dest
, path
, 0);
389 /* connect mixer widget to its interconnecting audio paths */
390 static int dapm_connect_mixer(struct snd_soc_dapm_context
*dapm
,
391 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
392 struct snd_soc_dapm_path
*path
, const char *control_name
)
396 /* search for mixer kcontrol */
397 for (i
= 0; i
< dest
->num_kcontrols
; i
++) {
398 if (!strcmp(control_name
, dest
->kcontrol_news
[i
].name
)) {
399 list_add(&path
->list
, &dapm
->card
->paths
);
400 list_add(&path
->list_sink
, &dest
->sources
);
401 list_add(&path
->list_source
, &src
->sinks
);
402 path
->name
= dest
->kcontrol_news
[i
].name
;
403 dapm_set_path_status(dest
, path
, i
);
410 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context
*dapm
,
411 struct snd_soc_dapm_widget
*kcontrolw
,
412 const struct snd_kcontrol_new
*kcontrol_new
,
413 struct snd_kcontrol
**kcontrol
)
415 struct snd_soc_dapm_widget
*w
;
420 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
421 if (w
== kcontrolw
|| w
->dapm
!= kcontrolw
->dapm
)
423 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
424 if (&w
->kcontrol_news
[i
] == kcontrol_new
) {
426 *kcontrol
= w
->kcontrols
[i
];
435 /* create new dapm mixer control */
436 static int dapm_new_mixer(struct snd_soc_dapm_widget
*w
)
438 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
440 size_t name_len
, prefix_len
;
441 struct snd_soc_dapm_path
*path
;
442 struct snd_card
*card
= dapm
->card
->snd_card
;
444 struct snd_soc_dapm_widget_list
*wlist
;
448 prefix
= dapm
->codec
->name_prefix
;
453 prefix_len
= strlen(prefix
) + 1;
458 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
461 list_for_each_entry(path
, &w
->sources
, list_sink
) {
463 /* mixer/mux paths name must match control name */
464 if (path
->name
!= (char *)w
->kcontrol_news
[i
].name
)
467 if (w
->kcontrols
[i
]) {
468 path
->kcontrol
= w
->kcontrols
[i
];
472 wlistsize
= sizeof(struct snd_soc_dapm_widget_list
) +
473 sizeof(struct snd_soc_dapm_widget
*),
474 wlist
= kzalloc(wlistsize
, GFP_KERNEL
);
477 "asoc: can't allocate widget list for %s\n",
481 wlist
->num_widgets
= 1;
482 wlist
->widgets
[0] = w
;
484 /* add dapm control with long name.
485 * for dapm_mixer this is the concatenation of the
486 * mixer and kcontrol name.
487 * for dapm_mixer_named_ctl this is simply the
490 name_len
= strlen(w
->kcontrol_news
[i
].name
) + 1;
491 if (w
->id
!= snd_soc_dapm_mixer_named_ctl
)
492 name_len
+= 1 + strlen(w
->name
);
494 path
->long_name
= kmalloc(name_len
, GFP_KERNEL
);
496 if (path
->long_name
== NULL
) {
503 /* The control will get a prefix from
504 * the control creation process but
505 * we're also using the same prefix
506 * for widgets so cut the prefix off
507 * the front of the widget name.
509 snprintf(path
->long_name
, name_len
, "%s %s",
510 w
->name
+ prefix_len
,
511 w
->kcontrol_news
[i
].name
);
513 case snd_soc_dapm_mixer_named_ctl
:
514 snprintf(path
->long_name
, name_len
, "%s",
515 w
->kcontrol_news
[i
].name
);
519 path
->long_name
[name_len
- 1] = '\0';
521 path
->kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[i
],
522 wlist
, path
->long_name
,
524 ret
= snd_ctl_add(card
, path
->kcontrol
);
527 "asoc: failed to add dapm kcontrol %s: %d\n",
528 path
->long_name
, ret
);
530 kfree(path
->long_name
);
531 path
->long_name
= NULL
;
534 w
->kcontrols
[i
] = path
->kcontrol
;
540 /* create new dapm mux control */
541 static int dapm_new_mux(struct snd_soc_dapm_widget
*w
)
543 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
544 struct snd_soc_dapm_path
*path
= NULL
;
545 struct snd_kcontrol
*kcontrol
;
546 struct snd_card
*card
= dapm
->card
->snd_card
;
550 struct snd_soc_dapm_widget_list
*wlist
;
551 int shared
, wlistentries
;
555 if (w
->num_kcontrols
!= 1) {
557 "asoc: mux %s has incorrect number of controls\n",
562 shared
= dapm_is_shared_kcontrol(dapm
, w
, &w
->kcontrol_news
[0],
565 wlist
= kcontrol
->private_data
;
566 wlistentries
= wlist
->num_widgets
+ 1;
571 wlistsize
= sizeof(struct snd_soc_dapm_widget_list
) +
572 wlistentries
* sizeof(struct snd_soc_dapm_widget
*),
573 wlist
= krealloc(wlist
, wlistsize
, GFP_KERNEL
);
576 "asoc: can't allocate widget list for %s\n", w
->name
);
579 wlist
->num_widgets
= wlistentries
;
580 wlist
->widgets
[wlistentries
- 1] = w
;
584 prefix
= dapm
->codec
->name_prefix
;
589 name
= w
->kcontrol_news
[0].name
;
594 prefix_len
= strlen(prefix
) + 1;
600 * The control will get a prefix from the control creation
601 * process but we're also using the same prefix for widgets so
602 * cut the prefix off the front of the widget name.
604 kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[0], wlist
,
605 name
+ prefix_len
, prefix
);
606 ret
= snd_ctl_add(card
, kcontrol
);
608 dev_err(dapm
->dev
, "failed to add kcontrol %s: %d\n",
615 kcontrol
->private_data
= wlist
;
617 w
->kcontrols
[0] = kcontrol
;
619 list_for_each_entry(path
, &w
->sources
, list_sink
)
620 path
->kcontrol
= kcontrol
;
625 /* create new dapm volume control */
626 static int dapm_new_pga(struct snd_soc_dapm_widget
*w
)
628 if (w
->num_kcontrols
)
629 dev_err(w
->dapm
->dev
,
630 "asoc: PGA controls not supported: '%s'\n", w
->name
);
635 /* reset 'walked' bit for each dapm path */
636 static inline void dapm_clear_walk(struct snd_soc_dapm_context
*dapm
)
638 struct snd_soc_dapm_path
*p
;
640 list_for_each_entry(p
, &dapm
->card
->paths
, list
)
644 /* We implement power down on suspend by checking the power state of
645 * the ALSA card - when we are suspending the ALSA state for the card
648 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
650 int level
= snd_power_get_state(widget
->dapm
->card
->snd_card
);
653 case SNDRV_CTL_POWER_D3hot
:
654 case SNDRV_CTL_POWER_D3cold
:
655 if (widget
->ignore_suspend
)
656 dev_dbg(widget
->dapm
->dev
, "%s ignoring suspend\n",
658 return widget
->ignore_suspend
;
665 * Recursively check for a completed path to an active or physically connected
666 * output widget. Returns number of complete paths.
668 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
)
670 struct snd_soc_dapm_path
*path
;
673 if (widget
->outputs
>= 0)
674 return widget
->outputs
;
676 DAPM_UPDATE_STAT(widget
, path_checks
);
678 if (widget
->id
== snd_soc_dapm_supply
)
681 switch (widget
->id
) {
682 case snd_soc_dapm_adc
:
683 case snd_soc_dapm_aif_out
:
684 if (widget
->active
) {
685 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
686 return widget
->outputs
;
692 if (widget
->connected
) {
693 /* connected pin ? */
694 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
) {
695 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
696 return widget
->outputs
;
699 /* connected jack or spk ? */
700 if (widget
->id
== snd_soc_dapm_hp
||
701 widget
->id
== snd_soc_dapm_spk
||
702 (widget
->id
== snd_soc_dapm_line
&&
703 !list_empty(&widget
->sources
))) {
704 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
705 return widget
->outputs
;
709 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
710 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
718 if (path
->sink
&& path
->connect
) {
720 con
+= is_connected_output_ep(path
->sink
);
724 widget
->outputs
= con
;
730 * Recursively check for a completed path to an active or physically connected
731 * input widget. Returns number of complete paths.
733 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
)
735 struct snd_soc_dapm_path
*path
;
738 if (widget
->inputs
>= 0)
739 return widget
->inputs
;
741 DAPM_UPDATE_STAT(widget
, path_checks
);
743 if (widget
->id
== snd_soc_dapm_supply
)
746 /* active stream ? */
747 switch (widget
->id
) {
748 case snd_soc_dapm_dac
:
749 case snd_soc_dapm_aif_in
:
750 if (widget
->active
) {
751 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
752 return widget
->inputs
;
758 if (widget
->connected
) {
759 /* connected pin ? */
760 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
) {
761 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
762 return widget
->inputs
;
765 /* connected VMID/Bias for lower pops */
766 if (widget
->id
== snd_soc_dapm_vmid
) {
767 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
768 return widget
->inputs
;
771 /* connected jack ? */
772 if (widget
->id
== snd_soc_dapm_mic
||
773 (widget
->id
== snd_soc_dapm_line
&&
774 !list_empty(&widget
->sinks
))) {
775 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
776 return widget
->inputs
;
779 /* signal generator */
780 if (widget
->id
== snd_soc_dapm_siggen
) {
781 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
782 return widget
->inputs
;
786 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
787 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
795 if (path
->source
&& path
->connect
) {
797 con
+= is_connected_input_ep(path
->source
);
801 widget
->inputs
= con
;
807 * Handler for generic register modifier widget.
809 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
810 struct snd_kcontrol
*kcontrol
, int event
)
814 if (SND_SOC_DAPM_EVENT_ON(event
))
819 soc_widget_update_bits(w
, -(w
->reg
+ 1),
820 w
->mask
<< w
->shift
, val
<< w
->shift
);
824 EXPORT_SYMBOL_GPL(dapm_reg_event
);
826 static int dapm_widget_power_check(struct snd_soc_dapm_widget
*w
)
828 if (w
->power_checked
)
834 w
->new_power
= w
->power_check(w
);
836 w
->power_checked
= true;
841 /* Generic check to see if a widget should be powered.
843 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
847 DAPM_UPDATE_STAT(w
, power_checks
);
849 in
= is_connected_input_ep(w
);
850 dapm_clear_walk(w
->dapm
);
851 out
= is_connected_output_ep(w
);
852 dapm_clear_walk(w
->dapm
);
853 return out
!= 0 && in
!= 0;
856 /* Check to see if an ADC has power */
857 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
861 DAPM_UPDATE_STAT(w
, power_checks
);
864 in
= is_connected_input_ep(w
);
865 dapm_clear_walk(w
->dapm
);
868 return dapm_generic_check_power(w
);
872 /* Check to see if a DAC has power */
873 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
877 DAPM_UPDATE_STAT(w
, power_checks
);
880 out
= is_connected_output_ep(w
);
881 dapm_clear_walk(w
->dapm
);
884 return dapm_generic_check_power(w
);
888 /* Check to see if a power supply is needed */
889 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
891 struct snd_soc_dapm_path
*path
;
893 DAPM_UPDATE_STAT(w
, power_checks
);
895 /* Check if one of our outputs is connected */
896 list_for_each_entry(path
, &w
->sinks
, list_source
) {
897 DAPM_UPDATE_STAT(w
, neighbour_checks
);
902 if (path
->connected
&&
903 !path
->connected(path
->source
, path
->sink
))
909 if (dapm_widget_power_check(path
->sink
))
913 dapm_clear_walk(w
->dapm
);
918 static int dapm_always_on_check_power(struct snd_soc_dapm_widget
*w
)
923 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
924 struct snd_soc_dapm_widget
*b
,
932 sort
= dapm_down_seq
;
934 if (sort
[a
->id
] != sort
[b
->id
])
935 return sort
[a
->id
] - sort
[b
->id
];
936 if (a
->subseq
!= b
->subseq
) {
938 return a
->subseq
- b
->subseq
;
940 return b
->subseq
- a
->subseq
;
942 if (a
->reg
!= b
->reg
)
943 return a
->reg
- b
->reg
;
944 if (a
->dapm
!= b
->dapm
)
945 return (unsigned long)a
->dapm
- (unsigned long)b
->dapm
;
950 /* Insert a widget in order into a DAPM power sequence. */
951 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
952 struct list_head
*list
,
955 struct snd_soc_dapm_widget
*w
;
957 list_for_each_entry(w
, list
, power_list
)
958 if (dapm_seq_compare(new_widget
, w
, power_up
) < 0) {
959 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
963 list_add_tail(&new_widget
->power_list
, list
);
966 static void dapm_seq_check_event(struct snd_soc_dapm_context
*dapm
,
967 struct snd_soc_dapm_widget
*w
, int event
)
969 struct snd_soc_card
*card
= dapm
->card
;
974 case SND_SOC_DAPM_PRE_PMU
:
978 case SND_SOC_DAPM_POST_PMU
:
979 ev_name
= "POST_PMU";
982 case SND_SOC_DAPM_PRE_PMD
:
986 case SND_SOC_DAPM_POST_PMD
:
987 ev_name
= "POST_PMD";
995 if (w
->power
!= power
)
998 if (w
->event
&& (w
->event_flags
& event
)) {
999 pop_dbg(dapm
->dev
, card
->pop_time
, "pop test : %s %s\n",
1001 trace_snd_soc_dapm_widget_event_start(w
, event
);
1002 ret
= w
->event(w
, NULL
, event
);
1003 trace_snd_soc_dapm_widget_event_done(w
, event
);
1005 pr_err("%s: %s event failed: %d\n",
1006 ev_name
, w
->name
, ret
);
1010 /* Apply the coalesced changes from a DAPM sequence */
1011 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context
*dapm
,
1012 struct list_head
*pending
)
1014 struct snd_soc_card
*card
= dapm
->card
;
1015 struct snd_soc_dapm_widget
*w
;
1017 unsigned int value
= 0;
1018 unsigned int mask
= 0;
1019 unsigned int cur_mask
;
1021 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
1024 list_for_each_entry(w
, pending
, power_list
) {
1025 cur_mask
= 1 << w
->shift
;
1026 BUG_ON(reg
!= w
->reg
);
1037 pop_dbg(dapm
->dev
, card
->pop_time
,
1038 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1039 w
->name
, reg
, value
, mask
);
1041 /* Check for events */
1042 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_PRE_PMU
);
1043 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_PRE_PMD
);
1047 /* Any widget will do, they should all be updating the
1050 w
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
1053 pop_dbg(dapm
->dev
, card
->pop_time
,
1054 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1055 value
, mask
, reg
, card
->pop_time
);
1056 pop_wait(card
->pop_time
);
1057 soc_widget_update_bits(w
, reg
, mask
, value
);
1060 list_for_each_entry(w
, pending
, power_list
) {
1061 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_POST_PMU
);
1062 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_POST_PMD
);
1066 /* Apply a DAPM power sequence.
1068 * We walk over a pre-sorted list of widgets to apply power to. In
1069 * order to minimise the number of writes to the device required
1070 * multiple widgets will be updated in a single write where possible.
1071 * Currently anything that requires more than a single write is not
1074 static void dapm_seq_run(struct snd_soc_dapm_context
*dapm
,
1075 struct list_head
*list
, int event
, bool power_up
)
1077 struct snd_soc_dapm_widget
*w
, *n
;
1080 int cur_subseq
= -1;
1081 int cur_reg
= SND_SOC_NOPM
;
1082 struct snd_soc_dapm_context
*cur_dapm
= NULL
;
1089 sort
= dapm_down_seq
;
1091 list_for_each_entry_safe(w
, n
, list
, power_list
) {
1094 /* Do we need to apply any queued changes? */
1095 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
||
1096 w
->dapm
!= cur_dapm
|| w
->subseq
!= cur_subseq
) {
1097 if (!list_empty(&pending
))
1098 dapm_seq_run_coalesced(cur_dapm
, &pending
);
1100 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1101 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1102 if (sort
[i
] == cur_sort
)
1103 cur_dapm
->seq_notifier(cur_dapm
,
1108 INIT_LIST_HEAD(&pending
);
1110 cur_subseq
= INT_MIN
;
1111 cur_reg
= SND_SOC_NOPM
;
1116 case snd_soc_dapm_pre
:
1118 list_for_each_entry_safe_continue(w
, n
, list
,
1121 if (event
== SND_SOC_DAPM_STREAM_START
)
1123 NULL
, SND_SOC_DAPM_PRE_PMU
);
1124 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1126 NULL
, SND_SOC_DAPM_PRE_PMD
);
1129 case snd_soc_dapm_post
:
1131 list_for_each_entry_safe_continue(w
, n
, list
,
1134 if (event
== SND_SOC_DAPM_STREAM_START
)
1136 NULL
, SND_SOC_DAPM_POST_PMU
);
1137 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1139 NULL
, SND_SOC_DAPM_POST_PMD
);
1143 /* Queue it up for application */
1144 cur_sort
= sort
[w
->id
];
1145 cur_subseq
= w
->subseq
;
1148 list_move(&w
->power_list
, &pending
);
1153 dev_err(w
->dapm
->dev
,
1154 "Failed to apply widget power: %d\n", ret
);
1157 if (!list_empty(&pending
))
1158 dapm_seq_run_coalesced(cur_dapm
, &pending
);
1160 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1161 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1162 if (sort
[i
] == cur_sort
)
1163 cur_dapm
->seq_notifier(cur_dapm
,
1168 static void dapm_widget_update(struct snd_soc_dapm_context
*dapm
)
1170 struct snd_soc_dapm_update
*update
= dapm
->update
;
1171 struct snd_soc_dapm_widget
*w
;
1180 (w
->event_flags
& SND_SOC_DAPM_PRE_REG
)) {
1181 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_PRE_REG
);
1183 pr_err("%s DAPM pre-event failed: %d\n",
1187 ret
= snd_soc_update_bits(w
->codec
, update
->reg
, update
->mask
,
1190 pr_err("%s DAPM update failed: %d\n", w
->name
, ret
);
1193 (w
->event_flags
& SND_SOC_DAPM_POST_REG
)) {
1194 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_POST_REG
);
1196 pr_err("%s DAPM post-event failed: %d\n",
1201 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1202 * they're changing state.
1204 static void dapm_pre_sequence_async(void *data
, async_cookie_t cookie
)
1206 struct snd_soc_dapm_context
*d
= data
;
1209 /* If we're off and we're not supposed to be go into STANDBY */
1210 if (d
->bias_level
== SND_SOC_BIAS_OFF
&&
1211 d
->target_bias_level
!= SND_SOC_BIAS_OFF
) {
1213 pm_runtime_get_sync(d
->dev
);
1215 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1218 "Failed to turn on bias: %d\n", ret
);
1221 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1222 if (d
->bias_level
!= d
->target_bias_level
) {
1223 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_PREPARE
);
1226 "Failed to prepare bias: %d\n", ret
);
1230 /* Async callback run prior to DAPM sequences - brings to their final
1233 static void dapm_post_sequence_async(void *data
, async_cookie_t cookie
)
1235 struct snd_soc_dapm_context
*d
= data
;
1238 /* If we just powered the last thing off drop to standby bias */
1239 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1240 (d
->target_bias_level
== SND_SOC_BIAS_STANDBY
||
1241 d
->target_bias_level
== SND_SOC_BIAS_OFF
)) {
1242 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1244 dev_err(d
->dev
, "Failed to apply standby bias: %d\n",
1248 /* If we're in standby and can support bias off then do that */
1249 if (d
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1250 d
->target_bias_level
== SND_SOC_BIAS_OFF
) {
1251 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_OFF
);
1253 dev_err(d
->dev
, "Failed to turn off bias: %d\n", ret
);
1256 pm_runtime_put_sync(d
->dev
);
1259 /* If we just powered up then move to active bias */
1260 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1261 d
->target_bias_level
== SND_SOC_BIAS_ON
) {
1262 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_ON
);
1264 dev_err(d
->dev
, "Failed to apply active bias: %d\n",
1269 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget
*peer
,
1270 bool power
, bool connect
)
1272 /* If a connection is being made or broken then that update
1273 * will have marked the peer dirty, otherwise the widgets are
1274 * not connected and this update has no impact. */
1278 /* If the peer is already in the state we're moving to then we
1279 * won't have an impact on it. */
1280 if (power
!= peer
->power
)
1281 dapm_mark_dirty(peer
, "peer state change");
1284 static void dapm_widget_set_power(struct snd_soc_dapm_widget
*w
, bool power
,
1285 struct list_head
*up_list
,
1286 struct list_head
*down_list
)
1288 struct snd_soc_dapm_path
*path
;
1290 if (w
->power
== power
)
1293 trace_snd_soc_dapm_widget_power(w
, power
);
1295 /* If we changed our power state perhaps our neigbours changed
1298 list_for_each_entry(path
, &w
->sources
, list_sink
) {
1300 dapm_widget_set_peer_power(path
->source
, power
,
1305 case snd_soc_dapm_supply
:
1306 /* Supplies can't affect their outputs, only their inputs */
1309 list_for_each_entry(path
, &w
->sinks
, list_source
) {
1311 dapm_widget_set_peer_power(path
->sink
, power
,
1319 dapm_seq_insert(w
, up_list
, true);
1321 dapm_seq_insert(w
, down_list
, false);
1326 static void dapm_power_one_widget(struct snd_soc_dapm_widget
*w
,
1327 struct list_head
*up_list
,
1328 struct list_head
*down_list
)
1333 case snd_soc_dapm_pre
:
1334 dapm_seq_insert(w
, down_list
, false);
1336 case snd_soc_dapm_post
:
1337 dapm_seq_insert(w
, up_list
, true);
1341 power
= dapm_widget_power_check(w
);
1343 dapm_widget_set_power(w
, power
, up_list
, down_list
);
1349 * Scan each dapm widget for complete audio path.
1350 * A complete path is a route that has valid endpoints i.e.:-
1352 * o DAC to output pin.
1353 * o Input Pin to ADC.
1354 * o Input pin to Output pin (bypass, sidetone)
1355 * o DAC to ADC (loopback).
1357 static int dapm_power_widgets(struct snd_soc_dapm_context
*dapm
, int event
)
1359 struct snd_soc_card
*card
= dapm
->card
;
1360 struct snd_soc_dapm_widget
*w
;
1361 struct snd_soc_dapm_context
*d
;
1363 LIST_HEAD(down_list
);
1364 LIST_HEAD(async_domain
);
1365 enum snd_soc_bias_level bias
;
1367 trace_snd_soc_dapm_start(card
);
1369 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1370 if (d
->n_widgets
|| d
->codec
== NULL
) {
1371 if (d
->idle_bias_off
)
1372 d
->target_bias_level
= SND_SOC_BIAS_OFF
;
1374 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1378 memset(&card
->dapm_stats
, 0, sizeof(card
->dapm_stats
));
1380 list_for_each_entry(w
, &card
->widgets
, list
) {
1381 w
->power_checked
= false;
1386 /* Check which widgets we need to power and store them in
1387 * lists indicating if they should be powered up or down. We
1388 * only check widgets that have been flagged as dirty but note
1389 * that new widgets may be added to the dirty list while we
1392 list_for_each_entry(w
, &card
->dapm_dirty
, dirty
) {
1393 dapm_power_one_widget(w
, &up_list
, &down_list
);
1396 list_for_each_entry(w
, &card
->widgets
, list
) {
1397 list_del_init(&w
->dirty
);
1402 /* Supplies and micbiases only bring the
1403 * context up to STANDBY as unless something
1404 * else is active and passing audio they
1405 * generally don't require full power.
1408 case snd_soc_dapm_supply
:
1409 case snd_soc_dapm_micbias
:
1410 if (d
->target_bias_level
< SND_SOC_BIAS_STANDBY
)
1411 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1414 d
->target_bias_level
= SND_SOC_BIAS_ON
;
1421 /* If there are no DAPM widgets then try to figure out power from the
1424 if (!dapm
->n_widgets
) {
1426 case SND_SOC_DAPM_STREAM_START
:
1427 case SND_SOC_DAPM_STREAM_RESUME
:
1428 dapm
->target_bias_level
= SND_SOC_BIAS_ON
;
1430 case SND_SOC_DAPM_STREAM_STOP
:
1431 if (dapm
->codec
&& dapm
->codec
->active
)
1432 dapm
->target_bias_level
= SND_SOC_BIAS_ON
;
1434 dapm
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1436 case SND_SOC_DAPM_STREAM_SUSPEND
:
1437 dapm
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1439 case SND_SOC_DAPM_STREAM_NOP
:
1440 dapm
->target_bias_level
= dapm
->bias_level
;
1447 /* Force all contexts in the card to the same bias state if
1448 * they're not ground referenced.
1450 bias
= SND_SOC_BIAS_OFF
;
1451 list_for_each_entry(d
, &card
->dapm_list
, list
)
1452 if (d
->target_bias_level
> bias
)
1453 bias
= d
->target_bias_level
;
1454 list_for_each_entry(d
, &card
->dapm_list
, list
)
1455 if (!d
->idle_bias_off
)
1456 d
->target_bias_level
= bias
;
1458 trace_snd_soc_dapm_walk_done(card
);
1460 /* Run all the bias changes in parallel */
1461 list_for_each_entry(d
, &dapm
->card
->dapm_list
, list
)
1462 async_schedule_domain(dapm_pre_sequence_async
, d
,
1464 async_synchronize_full_domain(&async_domain
);
1466 /* Power down widgets first; try to avoid amplifying pops. */
1467 dapm_seq_run(dapm
, &down_list
, event
, false);
1469 dapm_widget_update(dapm
);
1472 dapm_seq_run(dapm
, &up_list
, event
, true);
1474 /* Run all the bias changes in parallel */
1475 list_for_each_entry(d
, &dapm
->card
->dapm_list
, list
)
1476 async_schedule_domain(dapm_post_sequence_async
, d
,
1478 async_synchronize_full_domain(&async_domain
);
1480 pop_dbg(dapm
->dev
, card
->pop_time
,
1481 "DAPM sequencing finished, waiting %dms\n", card
->pop_time
);
1482 pop_wait(card
->pop_time
);
1484 trace_snd_soc_dapm_done(card
);
1489 #ifdef CONFIG_DEBUG_FS
1490 static int dapm_widget_power_open_file(struct inode
*inode
, struct file
*file
)
1492 file
->private_data
= inode
->i_private
;
1496 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1497 char __user
*user_buf
,
1498 size_t count
, loff_t
*ppos
)
1500 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1504 struct snd_soc_dapm_path
*p
= NULL
;
1506 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1510 in
= is_connected_input_ep(w
);
1511 dapm_clear_walk(w
->dapm
);
1512 out
= is_connected_output_ep(w
);
1513 dapm_clear_walk(w
->dapm
);
1515 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s in %d out %d",
1516 w
->name
, w
->power
? "On" : "Off", in
, out
);
1519 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1520 " - R%d(0x%x) bit %d",
1521 w
->reg
, w
->reg
, w
->shift
);
1523 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
1526 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1528 w
->active
? "active" : "inactive");
1530 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1531 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1535 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1536 " in \"%s\" \"%s\"\n",
1537 p
->name
? p
->name
: "static",
1540 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1541 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1545 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1546 " out \"%s\" \"%s\"\n",
1547 p
->name
? p
->name
: "static",
1551 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1557 static const struct file_operations dapm_widget_power_fops
= {
1558 .open
= dapm_widget_power_open_file
,
1559 .read
= dapm_widget_power_read_file
,
1560 .llseek
= default_llseek
,
1563 static int dapm_bias_open_file(struct inode
*inode
, struct file
*file
)
1565 file
->private_data
= inode
->i_private
;
1569 static ssize_t
dapm_bias_read_file(struct file
*file
, char __user
*user_buf
,
1570 size_t count
, loff_t
*ppos
)
1572 struct snd_soc_dapm_context
*dapm
= file
->private_data
;
1575 switch (dapm
->bias_level
) {
1576 case SND_SOC_BIAS_ON
:
1579 case SND_SOC_BIAS_PREPARE
:
1580 level
= "Prepare\n";
1582 case SND_SOC_BIAS_STANDBY
:
1583 level
= "Standby\n";
1585 case SND_SOC_BIAS_OFF
:
1590 level
= "Unknown\n";
1594 return simple_read_from_buffer(user_buf
, count
, ppos
, level
,
1598 static const struct file_operations dapm_bias_fops
= {
1599 .open
= dapm_bias_open_file
,
1600 .read
= dapm_bias_read_file
,
1601 .llseek
= default_llseek
,
1604 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
1605 struct dentry
*parent
)
1609 dapm
->debugfs_dapm
= debugfs_create_dir("dapm", parent
);
1611 if (!dapm
->debugfs_dapm
) {
1613 "Failed to create DAPM debugfs directory\n");
1617 d
= debugfs_create_file("bias_level", 0444,
1618 dapm
->debugfs_dapm
, dapm
,
1622 "ASoC: Failed to create bias level debugfs file\n");
1625 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
1627 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
1630 if (!dapm
->debugfs_dapm
|| !w
->name
)
1633 d
= debugfs_create_file(w
->name
, 0444,
1634 dapm
->debugfs_dapm
, w
,
1635 &dapm_widget_power_fops
);
1637 dev_warn(w
->dapm
->dev
,
1638 "ASoC: Failed to create %s debugfs file\n",
1642 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
1644 debugfs_remove_recursive(dapm
->debugfs_dapm
);
1648 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
1649 struct dentry
*parent
)
1653 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
1657 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
1663 /* test and update the power status of a mux widget */
1664 static int dapm_mux_update_power(struct snd_soc_dapm_widget
*widget
,
1665 struct snd_kcontrol
*kcontrol
, int change
,
1666 int mux
, struct soc_enum
*e
)
1668 struct snd_soc_dapm_path
*path
;
1671 if (widget
->id
!= snd_soc_dapm_mux
&&
1672 widget
->id
!= snd_soc_dapm_virt_mux
&&
1673 widget
->id
!= snd_soc_dapm_value_mux
)
1679 /* find dapm widget path assoc with kcontrol */
1680 list_for_each_entry(path
, &widget
->dapm
->card
->paths
, list
) {
1681 if (path
->kcontrol
!= kcontrol
)
1684 if (!path
->name
|| !e
->texts
[mux
])
1688 /* we now need to match the string in the enum to the path */
1689 if (!(strcmp(path
->name
, e
->texts
[mux
]))) {
1690 path
->connect
= 1; /* new connection */
1691 dapm_mark_dirty(path
->source
, "mux connection");
1694 dapm_mark_dirty(path
->source
,
1695 "mux disconnection");
1696 path
->connect
= 0; /* old connection must be powered down */
1701 dapm_mark_dirty(widget
, "mux change");
1702 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1708 /* test and update the power status of a mixer or switch widget */
1709 static int dapm_mixer_update_power(struct snd_soc_dapm_widget
*widget
,
1710 struct snd_kcontrol
*kcontrol
, int connect
)
1712 struct snd_soc_dapm_path
*path
;
1715 if (widget
->id
!= snd_soc_dapm_mixer
&&
1716 widget
->id
!= snd_soc_dapm_mixer_named_ctl
&&
1717 widget
->id
!= snd_soc_dapm_switch
)
1720 /* find dapm widget path assoc with kcontrol */
1721 list_for_each_entry(path
, &widget
->dapm
->card
->paths
, list
) {
1722 if (path
->kcontrol
!= kcontrol
)
1725 /* found, now check type */
1727 path
->connect
= connect
;
1728 dapm_mark_dirty(path
->source
, "mixer connection");
1732 dapm_mark_dirty(widget
, "mixer update");
1733 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1739 /* show dapm widget status in sys fs */
1740 static ssize_t
dapm_widget_show(struct device
*dev
,
1741 struct device_attribute
*attr
, char *buf
)
1743 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
1744 struct snd_soc_codec
*codec
=rtd
->codec
;
1745 struct snd_soc_dapm_widget
*w
;
1747 char *state
= "not set";
1749 list_for_each_entry(w
, &codec
->card
->widgets
, list
) {
1750 if (w
->dapm
!= &codec
->dapm
)
1753 /* only display widgets that burnm power */
1755 case snd_soc_dapm_hp
:
1756 case snd_soc_dapm_mic
:
1757 case snd_soc_dapm_spk
:
1758 case snd_soc_dapm_line
:
1759 case snd_soc_dapm_micbias
:
1760 case snd_soc_dapm_dac
:
1761 case snd_soc_dapm_adc
:
1762 case snd_soc_dapm_pga
:
1763 case snd_soc_dapm_out_drv
:
1764 case snd_soc_dapm_mixer
:
1765 case snd_soc_dapm_mixer_named_ctl
:
1766 case snd_soc_dapm_supply
:
1768 count
+= sprintf(buf
+ count
, "%s: %s\n",
1769 w
->name
, w
->power
? "On":"Off");
1776 switch (codec
->dapm
.bias_level
) {
1777 case SND_SOC_BIAS_ON
:
1780 case SND_SOC_BIAS_PREPARE
:
1783 case SND_SOC_BIAS_STANDBY
:
1786 case SND_SOC_BIAS_OFF
:
1790 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
1795 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
1797 int snd_soc_dapm_sys_add(struct device
*dev
)
1799 return device_create_file(dev
, &dev_attr_dapm_widget
);
1802 static void snd_soc_dapm_sys_remove(struct device
*dev
)
1804 device_remove_file(dev
, &dev_attr_dapm_widget
);
1807 /* free all dapm widgets and resources */
1808 static void dapm_free_widgets(struct snd_soc_dapm_context
*dapm
)
1810 struct snd_soc_dapm_widget
*w
, *next_w
;
1811 struct snd_soc_dapm_path
*p
, *next_p
;
1813 list_for_each_entry_safe(w
, next_w
, &dapm
->card
->widgets
, list
) {
1814 if (w
->dapm
!= dapm
)
1818 * remove source and sink paths associated to this widget.
1819 * While removing the path, remove reference to it from both
1820 * source and sink widgets so that path is removed only once.
1822 list_for_each_entry_safe(p
, next_p
, &w
->sources
, list_sink
) {
1823 list_del(&p
->list_sink
);
1824 list_del(&p
->list_source
);
1826 kfree(p
->long_name
);
1829 list_for_each_entry_safe(p
, next_p
, &w
->sinks
, list_source
) {
1830 list_del(&p
->list_sink
);
1831 list_del(&p
->list_source
);
1833 kfree(p
->long_name
);
1836 kfree(w
->kcontrols
);
1842 static struct snd_soc_dapm_widget
*dapm_find_widget(
1843 struct snd_soc_dapm_context
*dapm
, const char *pin
,
1844 bool search_other_contexts
)
1846 struct snd_soc_dapm_widget
*w
;
1847 struct snd_soc_dapm_widget
*fallback
= NULL
;
1849 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
1850 if (!strcmp(w
->name
, pin
)) {
1851 if (w
->dapm
== dapm
)
1858 if (search_other_contexts
)
1864 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context
*dapm
,
1865 const char *pin
, int status
)
1867 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
1870 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
1874 w
->connected
= status
;
1877 dapm_mark_dirty(w
, "pin configuration");
1883 * snd_soc_dapm_sync - scan and power dapm paths
1884 * @dapm: DAPM context
1886 * Walks all dapm audio paths and powers widgets according to their
1887 * stream or path usage.
1889 * Returns 0 for success.
1891 int snd_soc_dapm_sync(struct snd_soc_dapm_context
*dapm
)
1894 * Suppress early reports (eg, jacks syncing their state) to avoid
1895 * silly DAPM runs during card startup.
1897 if (!dapm
->card
|| !dapm
->card
->instantiated
)
1900 return dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
1902 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
1904 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context
*dapm
,
1905 const struct snd_soc_dapm_route
*route
)
1907 struct snd_soc_dapm_path
*path
;
1908 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
1909 struct snd_soc_dapm_widget
*wtsource
= NULL
, *wtsink
= NULL
;
1911 const char *control
= route
->control
;
1913 char prefixed_sink
[80];
1914 char prefixed_source
[80];
1917 if (dapm
->codec
&& dapm
->codec
->name_prefix
) {
1918 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
1919 dapm
->codec
->name_prefix
, route
->sink
);
1920 sink
= prefixed_sink
;
1921 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
1922 dapm
->codec
->name_prefix
, route
->source
);
1923 source
= prefixed_source
;
1926 source
= route
->source
;
1930 * find src and dest widgets over all widgets but favor a widget from
1931 * current DAPM context
1933 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
1934 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
1936 if (w
->dapm
== dapm
)
1940 if (!wsource
&& !(strcmp(w
->name
, source
))) {
1942 if (w
->dapm
== dapm
)
1946 /* use widget from another DAPM context if not found from this */
1952 if (wsource
== NULL
|| wsink
== NULL
)
1955 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
1959 path
->source
= wsource
;
1961 path
->connected
= route
->connected
;
1962 INIT_LIST_HEAD(&path
->list
);
1963 INIT_LIST_HEAD(&path
->list_source
);
1964 INIT_LIST_HEAD(&path
->list_sink
);
1966 /* check for external widgets */
1967 if (wsink
->id
== snd_soc_dapm_input
) {
1968 if (wsource
->id
== snd_soc_dapm_micbias
||
1969 wsource
->id
== snd_soc_dapm_mic
||
1970 wsource
->id
== snd_soc_dapm_line
||
1971 wsource
->id
== snd_soc_dapm_output
)
1974 if (wsource
->id
== snd_soc_dapm_output
) {
1975 if (wsink
->id
== snd_soc_dapm_spk
||
1976 wsink
->id
== snd_soc_dapm_hp
||
1977 wsink
->id
== snd_soc_dapm_line
||
1978 wsink
->id
== snd_soc_dapm_input
)
1982 /* connect static paths */
1983 if (control
== NULL
) {
1984 list_add(&path
->list
, &dapm
->card
->paths
);
1985 list_add(&path
->list_sink
, &wsink
->sources
);
1986 list_add(&path
->list_source
, &wsource
->sinks
);
1991 /* connect dynamic paths */
1992 switch (wsink
->id
) {
1993 case snd_soc_dapm_adc
:
1994 case snd_soc_dapm_dac
:
1995 case snd_soc_dapm_pga
:
1996 case snd_soc_dapm_out_drv
:
1997 case snd_soc_dapm_input
:
1998 case snd_soc_dapm_output
:
1999 case snd_soc_dapm_siggen
:
2000 case snd_soc_dapm_micbias
:
2001 case snd_soc_dapm_vmid
:
2002 case snd_soc_dapm_pre
:
2003 case snd_soc_dapm_post
:
2004 case snd_soc_dapm_supply
:
2005 case snd_soc_dapm_aif_in
:
2006 case snd_soc_dapm_aif_out
:
2007 list_add(&path
->list
, &dapm
->card
->paths
);
2008 list_add(&path
->list_sink
, &wsink
->sources
);
2009 list_add(&path
->list_source
, &wsource
->sinks
);
2012 case snd_soc_dapm_mux
:
2013 case snd_soc_dapm_virt_mux
:
2014 case snd_soc_dapm_value_mux
:
2015 ret
= dapm_connect_mux(dapm
, wsource
, wsink
, path
, control
,
2016 &wsink
->kcontrol_news
[0]);
2020 case snd_soc_dapm_switch
:
2021 case snd_soc_dapm_mixer
:
2022 case snd_soc_dapm_mixer_named_ctl
:
2023 ret
= dapm_connect_mixer(dapm
, wsource
, wsink
, path
, control
);
2027 case snd_soc_dapm_hp
:
2028 case snd_soc_dapm_mic
:
2029 case snd_soc_dapm_line
:
2030 case snd_soc_dapm_spk
:
2031 list_add(&path
->list
, &dapm
->card
->paths
);
2032 list_add(&path
->list_sink
, &wsink
->sources
);
2033 list_add(&path
->list_source
, &wsource
->sinks
);
2040 dev_warn(dapm
->dev
, "asoc: no dapm match for %s --> %s --> %s\n",
2041 source
, control
, sink
);
2047 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2048 * @dapm: DAPM context
2049 * @route: audio routes
2050 * @num: number of routes
2052 * Connects 2 dapm widgets together via a named audio path. The sink is
2053 * the widget receiving the audio signal, whilst the source is the sender
2054 * of the audio signal.
2056 * Returns 0 for success else error. On error all resources can be freed
2057 * with a call to snd_soc_card_free().
2059 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context
*dapm
,
2060 const struct snd_soc_dapm_route
*route
, int num
)
2064 for (i
= 0; i
< num
; i
++) {
2065 ret
= snd_soc_dapm_add_route(dapm
, route
);
2067 dev_err(dapm
->dev
, "Failed to add route %s->%s\n",
2068 route
->source
, route
->sink
);
2076 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
2078 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context
*dapm
,
2079 const struct snd_soc_dapm_route
*route
)
2081 struct snd_soc_dapm_widget
*source
= dapm_find_widget(dapm
,
2084 struct snd_soc_dapm_widget
*sink
= dapm_find_widget(dapm
,
2087 struct snd_soc_dapm_path
*path
;
2091 dev_err(dapm
->dev
, "Unable to find source %s for weak route\n",
2097 dev_err(dapm
->dev
, "Unable to find sink %s for weak route\n",
2102 if (route
->control
|| route
->connected
)
2103 dev_warn(dapm
->dev
, "Ignoring control for weak route %s->%s\n",
2104 route
->source
, route
->sink
);
2106 list_for_each_entry(path
, &source
->sinks
, list_source
) {
2107 if (path
->sink
== sink
) {
2114 dev_err(dapm
->dev
, "No path found for weak route %s->%s\n",
2115 route
->source
, route
->sink
);
2117 dev_warn(dapm
->dev
, "%d paths found for weak route %s->%s\n",
2118 count
, route
->source
, route
->sink
);
2124 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2125 * @dapm: DAPM context
2126 * @route: audio routes
2127 * @num: number of routes
2129 * Mark existing routes matching those specified in the passed array
2130 * as being weak, meaning that they are ignored for the purpose of
2131 * power decisions. The main intended use case is for sidetone paths
2132 * which couple audio between other independent paths if they are both
2133 * active in order to make the combination work better at the user
2134 * level but which aren't intended to be "used".
2136 * Note that CODEC drivers should not use this as sidetone type paths
2137 * can frequently also be used as bypass paths.
2139 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context
*dapm
,
2140 const struct snd_soc_dapm_route
*route
, int num
)
2145 for (i
= 0; i
< num
; i
++) {
2146 err
= snd_soc_dapm_weak_route(dapm
, route
);
2154 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes
);
2157 * snd_soc_dapm_new_widgets - add new dapm widgets
2158 * @dapm: DAPM context
2160 * Checks the codec for any new dapm widgets and creates them if found.
2162 * Returns 0 for success.
2164 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context
*dapm
)
2166 struct snd_soc_dapm_widget
*w
;
2169 list_for_each_entry(w
, &dapm
->card
->widgets
, list
)
2174 if (w
->num_kcontrols
) {
2175 w
->kcontrols
= kzalloc(w
->num_kcontrols
*
2176 sizeof(struct snd_kcontrol
*),
2183 case snd_soc_dapm_switch
:
2184 case snd_soc_dapm_mixer
:
2185 case snd_soc_dapm_mixer_named_ctl
:
2188 case snd_soc_dapm_mux
:
2189 case snd_soc_dapm_virt_mux
:
2190 case snd_soc_dapm_value_mux
:
2193 case snd_soc_dapm_pga
:
2194 case snd_soc_dapm_out_drv
:
2201 /* Read the initial power state from the device */
2203 val
= soc_widget_read(w
, w
->reg
);
2204 val
&= 1 << w
->shift
;
2214 dapm_mark_dirty(w
, "new widget");
2215 dapm_debugfs_add_widget(w
);
2218 dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
2221 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
2224 * snd_soc_dapm_get_volsw - dapm mixer get callback
2225 * @kcontrol: mixer control
2226 * @ucontrol: control element information
2228 * Callback to get the value of a dapm mixer control.
2230 * Returns 0 for success.
2232 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
2233 struct snd_ctl_elem_value
*ucontrol
)
2235 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2236 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2237 struct soc_mixer_control
*mc
=
2238 (struct soc_mixer_control
*)kcontrol
->private_value
;
2239 unsigned int reg
= mc
->reg
;
2240 unsigned int shift
= mc
->shift
;
2241 unsigned int rshift
= mc
->rshift
;
2243 unsigned int invert
= mc
->invert
;
2244 unsigned int mask
= (1 << fls(max
)) - 1;
2246 ucontrol
->value
.integer
.value
[0] =
2247 (snd_soc_read(widget
->codec
, reg
) >> shift
) & mask
;
2248 if (shift
!= rshift
)
2249 ucontrol
->value
.integer
.value
[1] =
2250 (snd_soc_read(widget
->codec
, reg
) >> rshift
) & mask
;
2252 ucontrol
->value
.integer
.value
[0] =
2253 max
- ucontrol
->value
.integer
.value
[0];
2254 if (shift
!= rshift
)
2255 ucontrol
->value
.integer
.value
[1] =
2256 max
- ucontrol
->value
.integer
.value
[1];
2261 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
2264 * snd_soc_dapm_put_volsw - dapm mixer set callback
2265 * @kcontrol: mixer control
2266 * @ucontrol: control element information
2268 * Callback to set the value of a dapm mixer control.
2270 * Returns 0 for success.
2272 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
2273 struct snd_ctl_elem_value
*ucontrol
)
2275 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2276 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2277 struct snd_soc_codec
*codec
= widget
->codec
;
2278 struct soc_mixer_control
*mc
=
2279 (struct soc_mixer_control
*)kcontrol
->private_value
;
2280 unsigned int reg
= mc
->reg
;
2281 unsigned int shift
= mc
->shift
;
2283 unsigned int mask
= (1 << fls(max
)) - 1;
2284 unsigned int invert
= mc
->invert
;
2286 int connect
, change
;
2287 struct snd_soc_dapm_update update
;
2290 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2294 mask
= mask
<< shift
;
2298 /* new connection */
2299 connect
= invert
? 0 : 1;
2301 /* old connection must be powered down */
2302 connect
= invert
? 1 : 0;
2304 mutex_lock(&codec
->mutex
);
2306 change
= snd_soc_test_bits(widget
->codec
, reg
, mask
, val
);
2308 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2309 widget
= wlist
->widgets
[wi
];
2311 widget
->value
= val
;
2313 update
.kcontrol
= kcontrol
;
2314 update
.widget
= widget
;
2318 widget
->dapm
->update
= &update
;
2320 dapm_mixer_update_power(widget
, kcontrol
, connect
);
2322 widget
->dapm
->update
= NULL
;
2326 mutex_unlock(&codec
->mutex
);
2329 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
2332 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2333 * @kcontrol: mixer control
2334 * @ucontrol: control element information
2336 * Callback to get the value of a dapm enumerated double mixer control.
2338 * Returns 0 for success.
2340 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
2341 struct snd_ctl_elem_value
*ucontrol
)
2343 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2344 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2345 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2346 unsigned int val
, bitmask
;
2348 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2350 val
= snd_soc_read(widget
->codec
, e
->reg
);
2351 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & (bitmask
- 1);
2352 if (e
->shift_l
!= e
->shift_r
)
2353 ucontrol
->value
.enumerated
.item
[1] =
2354 (val
>> e
->shift_r
) & (bitmask
- 1);
2358 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
2361 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2362 * @kcontrol: mixer control
2363 * @ucontrol: control element information
2365 * Callback to set the value of a dapm enumerated double mixer control.
2367 * Returns 0 for success.
2369 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
2370 struct snd_ctl_elem_value
*ucontrol
)
2372 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2373 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2374 struct snd_soc_codec
*codec
= widget
->codec
;
2375 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2376 unsigned int val
, mux
, change
;
2377 unsigned int mask
, bitmask
;
2378 struct snd_soc_dapm_update update
;
2381 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2383 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2385 mux
= ucontrol
->value
.enumerated
.item
[0];
2386 val
= mux
<< e
->shift_l
;
2387 mask
= (bitmask
- 1) << e
->shift_l
;
2388 if (e
->shift_l
!= e
->shift_r
) {
2389 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2391 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
2392 mask
|= (bitmask
- 1) << e
->shift_r
;
2395 mutex_lock(&codec
->mutex
);
2397 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
2399 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2400 widget
= wlist
->widgets
[wi
];
2402 widget
->value
= val
;
2404 update
.kcontrol
= kcontrol
;
2405 update
.widget
= widget
;
2406 update
.reg
= e
->reg
;
2409 widget
->dapm
->update
= &update
;
2411 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
2413 widget
->dapm
->update
= NULL
;
2417 mutex_unlock(&codec
->mutex
);
2420 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
2423 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2424 * @kcontrol: mixer control
2425 * @ucontrol: control element information
2427 * Returns 0 for success.
2429 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol
*kcontrol
,
2430 struct snd_ctl_elem_value
*ucontrol
)
2432 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2433 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2435 ucontrol
->value
.enumerated
.item
[0] = widget
->value
;
2439 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt
);
2442 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2443 * @kcontrol: mixer control
2444 * @ucontrol: control element information
2446 * Returns 0 for success.
2448 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol
*kcontrol
,
2449 struct snd_ctl_elem_value
*ucontrol
)
2451 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2452 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2453 struct snd_soc_codec
*codec
= widget
->codec
;
2454 struct soc_enum
*e
=
2455 (struct soc_enum
*)kcontrol
->private_value
;
2460 if (ucontrol
->value
.enumerated
.item
[0] >= e
->max
)
2463 mutex_lock(&codec
->mutex
);
2465 change
= widget
->value
!= ucontrol
->value
.enumerated
.item
[0];
2467 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2468 widget
= wlist
->widgets
[wi
];
2470 widget
->value
= ucontrol
->value
.enumerated
.item
[0];
2472 dapm_mux_update_power(widget
, kcontrol
, change
,
2477 mutex_unlock(&codec
->mutex
);
2480 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt
);
2483 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2485 * @kcontrol: mixer control
2486 * @ucontrol: control element information
2488 * Callback to get the value of a dapm semi enumerated double mixer control.
2490 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2491 * used for handling bitfield coded enumeration for example.
2493 * Returns 0 for success.
2495 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
2496 struct snd_ctl_elem_value
*ucontrol
)
2498 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2499 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2500 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2501 unsigned int reg_val
, val
, mux
;
2503 reg_val
= snd_soc_read(widget
->codec
, e
->reg
);
2504 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
2505 for (mux
= 0; mux
< e
->max
; mux
++) {
2506 if (val
== e
->values
[mux
])
2509 ucontrol
->value
.enumerated
.item
[0] = mux
;
2510 if (e
->shift_l
!= e
->shift_r
) {
2511 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
2512 for (mux
= 0; mux
< e
->max
; mux
++) {
2513 if (val
== e
->values
[mux
])
2516 ucontrol
->value
.enumerated
.item
[1] = mux
;
2521 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double
);
2524 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2526 * @kcontrol: mixer control
2527 * @ucontrol: control element information
2529 * Callback to set the value of a dapm semi enumerated double mixer control.
2531 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2532 * used for handling bitfield coded enumeration for example.
2534 * Returns 0 for success.
2536 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
2537 struct snd_ctl_elem_value
*ucontrol
)
2539 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2540 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2541 struct snd_soc_codec
*codec
= widget
->codec
;
2542 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2543 unsigned int val
, mux
, change
;
2545 struct snd_soc_dapm_update update
;
2548 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2550 mux
= ucontrol
->value
.enumerated
.item
[0];
2551 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
2552 mask
= e
->mask
<< e
->shift_l
;
2553 if (e
->shift_l
!= e
->shift_r
) {
2554 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2556 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
2557 mask
|= e
->mask
<< e
->shift_r
;
2560 mutex_lock(&codec
->mutex
);
2562 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
2564 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2565 widget
= wlist
->widgets
[wi
];
2567 widget
->value
= val
;
2569 update
.kcontrol
= kcontrol
;
2570 update
.widget
= widget
;
2571 update
.reg
= e
->reg
;
2574 widget
->dapm
->update
= &update
;
2576 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
2578 widget
->dapm
->update
= NULL
;
2582 mutex_unlock(&codec
->mutex
);
2585 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double
);
2588 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2590 * @kcontrol: mixer control
2591 * @uinfo: control element information
2593 * Callback to provide information about a pin switch control.
2595 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
2596 struct snd_ctl_elem_info
*uinfo
)
2598 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2600 uinfo
->value
.integer
.min
= 0;
2601 uinfo
->value
.integer
.max
= 1;
2605 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
2608 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2610 * @kcontrol: mixer control
2613 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
2614 struct snd_ctl_elem_value
*ucontrol
)
2616 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2617 const char *pin
= (const char *)kcontrol
->private_value
;
2619 mutex_lock(&codec
->mutex
);
2621 ucontrol
->value
.integer
.value
[0] =
2622 snd_soc_dapm_get_pin_status(&codec
->dapm
, pin
);
2624 mutex_unlock(&codec
->mutex
);
2628 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
2631 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2633 * @kcontrol: mixer control
2636 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
2637 struct snd_ctl_elem_value
*ucontrol
)
2639 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2640 const char *pin
= (const char *)kcontrol
->private_value
;
2642 mutex_lock(&codec
->mutex
);
2644 if (ucontrol
->value
.integer
.value
[0])
2645 snd_soc_dapm_enable_pin(&codec
->dapm
, pin
);
2647 snd_soc_dapm_disable_pin(&codec
->dapm
, pin
);
2649 snd_soc_dapm_sync(&codec
->dapm
);
2651 mutex_unlock(&codec
->mutex
);
2655 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
2658 * snd_soc_dapm_new_control - create new dapm control
2659 * @dapm: DAPM context
2660 * @widget: widget template
2662 * Creates a new dapm control based upon the template.
2664 * Returns 0 for success else error.
2666 int snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
2667 const struct snd_soc_dapm_widget
*widget
)
2669 struct snd_soc_dapm_widget
*w
;
2672 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
2675 name_len
= strlen(widget
->name
) + 1;
2676 if (dapm
->codec
&& dapm
->codec
->name_prefix
)
2677 name_len
+= 1 + strlen(dapm
->codec
->name_prefix
);
2678 w
->name
= kmalloc(name_len
, GFP_KERNEL
);
2679 if (w
->name
== NULL
) {
2683 if (dapm
->codec
&& dapm
->codec
->name_prefix
)
2684 snprintf(w
->name
, name_len
, "%s %s",
2685 dapm
->codec
->name_prefix
, widget
->name
);
2687 snprintf(w
->name
, name_len
, "%s", widget
->name
);
2690 case snd_soc_dapm_switch
:
2691 case snd_soc_dapm_mixer
:
2692 case snd_soc_dapm_mixer_named_ctl
:
2693 w
->power_check
= dapm_generic_check_power
;
2695 case snd_soc_dapm_mux
:
2696 case snd_soc_dapm_virt_mux
:
2697 case snd_soc_dapm_value_mux
:
2698 w
->power_check
= dapm_generic_check_power
;
2700 case snd_soc_dapm_adc
:
2701 case snd_soc_dapm_aif_out
:
2702 w
->power_check
= dapm_adc_check_power
;
2704 case snd_soc_dapm_dac
:
2705 case snd_soc_dapm_aif_in
:
2706 w
->power_check
= dapm_dac_check_power
;
2708 case snd_soc_dapm_pga
:
2709 case snd_soc_dapm_out_drv
:
2710 case snd_soc_dapm_input
:
2711 case snd_soc_dapm_output
:
2712 case snd_soc_dapm_micbias
:
2713 case snd_soc_dapm_spk
:
2714 case snd_soc_dapm_hp
:
2715 case snd_soc_dapm_mic
:
2716 case snd_soc_dapm_line
:
2717 w
->power_check
= dapm_generic_check_power
;
2719 case snd_soc_dapm_supply
:
2720 w
->power_check
= dapm_supply_check_power
;
2723 w
->power_check
= dapm_always_on_check_power
;
2729 w
->codec
= dapm
->codec
;
2730 w
->platform
= dapm
->platform
;
2731 INIT_LIST_HEAD(&w
->sources
);
2732 INIT_LIST_HEAD(&w
->sinks
);
2733 INIT_LIST_HEAD(&w
->list
);
2734 INIT_LIST_HEAD(&w
->dirty
);
2735 list_add(&w
->list
, &dapm
->card
->widgets
);
2737 /* machine layer set ups unconnected pins and insertions */
2741 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
2744 * snd_soc_dapm_new_controls - create new dapm controls
2745 * @dapm: DAPM context
2746 * @widget: widget array
2747 * @num: number of widgets
2749 * Creates new DAPM controls based upon the templates.
2751 * Returns 0 for success else error.
2753 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context
*dapm
,
2754 const struct snd_soc_dapm_widget
*widget
,
2759 for (i
= 0; i
< num
; i
++) {
2760 ret
= snd_soc_dapm_new_control(dapm
, widget
);
2763 "ASoC: Failed to create DAPM control %s: %d\n",
2771 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
2773 static void soc_dapm_stream_event(struct snd_soc_dapm_context
*dapm
,
2774 const char *stream
, int event
)
2776 struct snd_soc_dapm_widget
*w
;
2778 list_for_each_entry(w
, &dapm
->card
->widgets
, list
)
2780 if (!w
->sname
|| w
->dapm
!= dapm
)
2782 dev_vdbg(w
->dapm
->dev
, "widget %s\n %s stream %s event %d\n",
2783 w
->name
, w
->sname
, stream
, event
);
2784 if (strstr(w
->sname
, stream
)) {
2785 dapm_mark_dirty(w
, "stream event");
2787 case SND_SOC_DAPM_STREAM_START
:
2790 case SND_SOC_DAPM_STREAM_STOP
:
2793 case SND_SOC_DAPM_STREAM_SUSPEND
:
2794 case SND_SOC_DAPM_STREAM_RESUME
:
2795 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
2796 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
2802 dapm_power_widgets(dapm
, event
);
2804 /* do we need to notify any clients that DAPM stream is complete */
2805 if (dapm
->stream_event
)
2806 dapm
->stream_event(dapm
, event
);
2810 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2811 * @rtd: PCM runtime data
2812 * @stream: stream name
2813 * @event: stream event
2815 * Sends a stream event to the dapm core. The core then makes any
2816 * necessary widget power changes.
2818 * Returns 0 for success else error.
2820 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
,
2821 const char *stream
, int event
)
2823 struct snd_soc_codec
*codec
= rtd
->codec
;
2828 mutex_lock(&codec
->mutex
);
2829 soc_dapm_stream_event(&codec
->dapm
, stream
, event
);
2830 mutex_unlock(&codec
->mutex
);
2835 * snd_soc_dapm_enable_pin - enable pin.
2836 * @dapm: DAPM context
2839 * Enables input/output pin and its parents or children widgets iff there is
2840 * a valid audio route and active audio stream.
2841 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2842 * do any widget power switching.
2844 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
2846 return snd_soc_dapm_set_pin(dapm
, pin
, 1);
2848 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
2851 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2852 * @dapm: DAPM context
2855 * Enables input/output pin regardless of any other state. This is
2856 * intended for use with microphone bias supplies used in microphone
2859 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2860 * do any widget power switching.
2862 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context
*dapm
,
2865 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
2868 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
2872 dev_dbg(w
->dapm
->dev
, "dapm: force enable pin %s\n", pin
);
2875 dapm_mark_dirty(w
, "force enable");
2879 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
2882 * snd_soc_dapm_disable_pin - disable pin.
2883 * @dapm: DAPM context
2886 * Disables input/output pin and its parents or children widgets.
2887 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2888 * do any widget power switching.
2890 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context
*dapm
,
2893 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
2895 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
2898 * snd_soc_dapm_nc_pin - permanently disable pin.
2899 * @dapm: DAPM context
2902 * Marks the specified pin as being not connected, disabling it along
2903 * any parent or child widgets. At present this is identical to
2904 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2905 * additional things such as disabling controls which only affect
2906 * paths through the pin.
2908 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2909 * do any widget power switching.
2911 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
2913 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
2915 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
2918 * snd_soc_dapm_get_pin_status - get audio pin status
2919 * @dapm: DAPM context
2920 * @pin: audio signal pin endpoint (or start point)
2922 * Get audio pin status - connected or disconnected.
2924 * Returns 1 for connected otherwise 0.
2926 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context
*dapm
,
2929 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
2932 return w
->connected
;
2936 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
2939 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2940 * @dapm: DAPM context
2941 * @pin: audio signal pin endpoint (or start point)
2943 * Mark the given endpoint or pin as ignoring suspend. When the
2944 * system is disabled a path between two endpoints flagged as ignoring
2945 * suspend will not be disabled. The path must already be enabled via
2946 * normal means at suspend time, it will not be turned on if it was not
2949 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context
*dapm
,
2952 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, false);
2955 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
2959 w
->ignore_suspend
= 1;
2963 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
2965 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card
*card
,
2966 struct snd_soc_dapm_widget
*w
)
2968 struct snd_soc_dapm_path
*p
;
2970 list_for_each_entry(p
, &card
->paths
, list
) {
2971 if ((p
->source
== w
) || (p
->sink
== w
)) {
2973 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
2974 p
->source
->name
, p
->source
->id
, p
->source
->dapm
,
2975 p
->sink
->name
, p
->sink
->id
, p
->sink
->dapm
);
2977 /* Connected to something other than the codec */
2978 if (p
->source
->dapm
!= p
->sink
->dapm
)
2981 * Loopback connection from codec external pin to
2982 * codec external pin
2984 if (p
->sink
->id
== snd_soc_dapm_input
) {
2985 switch (p
->source
->id
) {
2986 case snd_soc_dapm_output
:
2987 case snd_soc_dapm_micbias
:
3000 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3001 * @codec: The codec whose pins should be processed
3003 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3004 * which are unused. Pins are used if they are connected externally to the
3005 * codec, whether that be to some other device, or a loop-back connection to
3008 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec
*codec
)
3010 struct snd_soc_card
*card
= codec
->card
;
3011 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
3012 struct snd_soc_dapm_widget
*w
;
3014 dev_dbg(codec
->dev
, "Auto NC: DAPMs: card:%p codec:%p\n",
3015 &card
->dapm
, &codec
->dapm
);
3017 list_for_each_entry(w
, &card
->widgets
, list
) {
3018 if (w
->dapm
!= dapm
)
3021 case snd_soc_dapm_input
:
3022 case snd_soc_dapm_output
:
3023 case snd_soc_dapm_micbias
:
3024 dev_dbg(codec
->dev
, "Auto NC: Checking widget %s\n",
3026 if (!snd_soc_dapm_widget_in_card_paths(card
, w
)) {
3028 "... Not in map; disabling\n");
3029 snd_soc_dapm_nc_pin(dapm
, w
->name
);
3039 * snd_soc_dapm_free - free dapm resources
3040 * @dapm: DAPM context
3042 * Free all dapm widgets and resources.
3044 void snd_soc_dapm_free(struct snd_soc_dapm_context
*dapm
)
3046 snd_soc_dapm_sys_remove(dapm
->dev
);
3047 dapm_debugfs_cleanup(dapm
);
3048 dapm_free_widgets(dapm
);
3049 list_del(&dapm
->list
);
3051 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
3053 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context
*dapm
)
3055 struct snd_soc_dapm_widget
*w
;
3056 LIST_HEAD(down_list
);
3059 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
3060 if (w
->dapm
!= dapm
)
3063 dapm_seq_insert(w
, &down_list
, false);
3069 /* If there were no widgets to power down we're already in
3073 if (dapm
->bias_level
== SND_SOC_BIAS_ON
)
3074 snd_soc_dapm_set_bias_level(dapm
,
3075 SND_SOC_BIAS_PREPARE
);
3076 dapm_seq_run(dapm
, &down_list
, 0, false);
3077 if (dapm
->bias_level
== SND_SOC_BIAS_PREPARE
)
3078 snd_soc_dapm_set_bias_level(dapm
,
3079 SND_SOC_BIAS_STANDBY
);
3084 * snd_soc_dapm_shutdown - callback for system shutdown
3086 void snd_soc_dapm_shutdown(struct snd_soc_card
*card
)
3088 struct snd_soc_codec
*codec
;
3090 list_for_each_entry(codec
, &card
->codec_dev_list
, list
) {
3091 soc_dapm_shutdown_codec(&codec
->dapm
);
3092 if (codec
->dapm
.bias_level
== SND_SOC_BIAS_STANDBY
)
3093 snd_soc_dapm_set_bias_level(&codec
->dapm
,
3098 /* Module information */
3099 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3100 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3101 MODULE_LICENSE("GPL");