2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/headphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed power down of audio subsystem to reduce pops between a quick
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
29 #include <linux/async.h>
30 #include <linux/delay.h>
32 #include <linux/bitops.h>
33 #include <linux/platform_device.h>
34 #include <linux/jiffies.h>
35 #include <linux/debugfs.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/slab.h>
39 #include <sound/core.h>
40 #include <sound/pcm.h>
41 #include <sound/pcm_params.h>
42 #include <sound/soc.h>
43 #include <sound/initval.h>
45 #include <trace/events/asoc.h>
47 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
49 /* dapm power sequences - make this per codec in the future */
50 static int dapm_up_seq
[] = {
51 [snd_soc_dapm_pre
] = 0,
52 [snd_soc_dapm_supply
] = 1,
53 [snd_soc_dapm_regulator_supply
] = 1,
54 [snd_soc_dapm_micbias
] = 2,
55 [snd_soc_dapm_aif_in
] = 3,
56 [snd_soc_dapm_aif_out
] = 3,
57 [snd_soc_dapm_mic
] = 4,
58 [snd_soc_dapm_mux
] = 5,
59 [snd_soc_dapm_virt_mux
] = 5,
60 [snd_soc_dapm_value_mux
] = 5,
61 [snd_soc_dapm_dac
] = 6,
62 [snd_soc_dapm_mixer
] = 7,
63 [snd_soc_dapm_mixer_named_ctl
] = 7,
64 [snd_soc_dapm_pga
] = 8,
65 [snd_soc_dapm_adc
] = 9,
66 [snd_soc_dapm_out_drv
] = 10,
67 [snd_soc_dapm_hp
] = 10,
68 [snd_soc_dapm_spk
] = 10,
69 [snd_soc_dapm_post
] = 11,
72 static int dapm_down_seq
[] = {
73 [snd_soc_dapm_pre
] = 0,
74 [snd_soc_dapm_adc
] = 1,
75 [snd_soc_dapm_hp
] = 2,
76 [snd_soc_dapm_spk
] = 2,
77 [snd_soc_dapm_out_drv
] = 2,
78 [snd_soc_dapm_pga
] = 4,
79 [snd_soc_dapm_mixer_named_ctl
] = 5,
80 [snd_soc_dapm_mixer
] = 5,
81 [snd_soc_dapm_dac
] = 6,
82 [snd_soc_dapm_mic
] = 7,
83 [snd_soc_dapm_micbias
] = 8,
84 [snd_soc_dapm_mux
] = 9,
85 [snd_soc_dapm_virt_mux
] = 9,
86 [snd_soc_dapm_value_mux
] = 9,
87 [snd_soc_dapm_aif_in
] = 10,
88 [snd_soc_dapm_aif_out
] = 10,
89 [snd_soc_dapm_regulator_supply
] = 11,
90 [snd_soc_dapm_supply
] = 11,
91 [snd_soc_dapm_post
] = 12,
94 static void pop_wait(u32 pop_time
)
97 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time
));
100 static void pop_dbg(struct device
*dev
, u32 pop_time
, const char *fmt
, ...)
108 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
113 vsnprintf(buf
, PAGE_SIZE
, fmt
, args
);
114 dev_info(dev
, "%s", buf
);
120 static bool dapm_dirty_widget(struct snd_soc_dapm_widget
*w
)
122 return !list_empty(&w
->dirty
);
125 void dapm_mark_dirty(struct snd_soc_dapm_widget
*w
, const char *reason
)
127 if (!dapm_dirty_widget(w
)) {
128 dev_vdbg(w
->dapm
->dev
, "Marking %s dirty due to %s\n",
130 list_add_tail(&w
->dirty
, &w
->dapm
->card
->dapm_dirty
);
133 EXPORT_SYMBOL_GPL(dapm_mark_dirty
);
135 /* create a new dapm widget */
136 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
137 const struct snd_soc_dapm_widget
*_widget
)
139 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
142 /* get snd_card from DAPM context */
143 static inline struct snd_card
*dapm_get_snd_card(
144 struct snd_soc_dapm_context
*dapm
)
147 return dapm
->codec
->card
->snd_card
;
148 else if (dapm
->platform
)
149 return dapm
->platform
->card
->snd_card
;
157 /* get soc_card from DAPM context */
158 static inline struct snd_soc_card
*dapm_get_soc_card(
159 struct snd_soc_dapm_context
*dapm
)
162 return dapm
->codec
->card
;
163 else if (dapm
->platform
)
164 return dapm
->platform
->card
;
172 static int soc_widget_read(struct snd_soc_dapm_widget
*w
, int reg
)
175 return snd_soc_read(w
->codec
, reg
);
176 else if (w
->platform
)
177 return snd_soc_platform_read(w
->platform
, reg
);
179 dev_err(w
->dapm
->dev
, "no valid widget read method\n");
183 static int soc_widget_write(struct snd_soc_dapm_widget
*w
, int reg
, int val
)
186 return snd_soc_write(w
->codec
, reg
, val
);
187 else if (w
->platform
)
188 return snd_soc_platform_write(w
->platform
, reg
, val
);
190 dev_err(w
->dapm
->dev
, "no valid widget write method\n");
194 static int soc_widget_update_bits(struct snd_soc_dapm_widget
*w
,
195 unsigned short reg
, unsigned int mask
, unsigned int value
)
198 unsigned int old
, new;
201 if (w
->codec
&& w
->codec
->using_regmap
) {
202 ret
= regmap_update_bits_check(w
->codec
->control_data
,
203 reg
, mask
, value
, &change
);
207 ret
= soc_widget_read(w
, reg
);
212 new = (old
& ~mask
) | (value
& mask
);
215 ret
= soc_widget_write(w
, reg
, new);
225 * snd_soc_dapm_set_bias_level - set the bias level for the system
226 * @dapm: DAPM context
227 * @level: level to configure
229 * Configure the bias (power) levels for the SoC audio device.
231 * Returns 0 for success else error.
233 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context
*dapm
,
234 enum snd_soc_bias_level level
)
236 struct snd_soc_card
*card
= dapm
->card
;
239 trace_snd_soc_bias_level_start(card
, level
);
241 if (card
&& card
->set_bias_level
)
242 ret
= card
->set_bias_level(card
, dapm
, level
);
247 if (dapm
->codec
->driver
->set_bias_level
)
248 ret
= dapm
->codec
->driver
->set_bias_level(dapm
->codec
,
251 dapm
->bias_level
= level
;
256 if (card
&& card
->set_bias_level_post
)
257 ret
= card
->set_bias_level_post(card
, dapm
, level
);
259 trace_snd_soc_bias_level_done(card
, level
);
264 /* set up initial codec paths */
265 static void dapm_set_path_status(struct snd_soc_dapm_widget
*w
,
266 struct snd_soc_dapm_path
*p
, int i
)
269 case snd_soc_dapm_switch
:
270 case snd_soc_dapm_mixer
:
271 case snd_soc_dapm_mixer_named_ctl
: {
273 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
274 w
->kcontrol_news
[i
].private_value
;
275 unsigned int reg
= mc
->reg
;
276 unsigned int shift
= mc
->shift
;
278 unsigned int mask
= (1 << fls(max
)) - 1;
279 unsigned int invert
= mc
->invert
;
281 val
= soc_widget_read(w
, reg
);
282 val
= (val
>> shift
) & mask
;
284 if ((invert
&& !val
) || (!invert
&& val
))
290 case snd_soc_dapm_mux
: {
291 struct soc_enum
*e
= (struct soc_enum
*)
292 w
->kcontrol_news
[i
].private_value
;
293 int val
, item
, bitmask
;
295 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
297 val
= soc_widget_read(w
, e
->reg
);
298 item
= (val
>> e
->shift_l
) & (bitmask
- 1);
301 for (i
= 0; i
< e
->max
; i
++) {
302 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
307 case snd_soc_dapm_virt_mux
: {
308 struct soc_enum
*e
= (struct soc_enum
*)
309 w
->kcontrol_news
[i
].private_value
;
312 /* since a virtual mux has no backing registers to
313 * decide which path to connect, it will try to match
314 * with the first enumeration. This is to ensure
315 * that the default mux choice (the first) will be
316 * correctly powered up during initialization.
318 if (!strcmp(p
->name
, e
->texts
[0]))
322 case snd_soc_dapm_value_mux
: {
323 struct soc_enum
*e
= (struct soc_enum
*)
324 w
->kcontrol_news
[i
].private_value
;
327 val
= soc_widget_read(w
, e
->reg
);
328 val
= (val
>> e
->shift_l
) & e
->mask
;
329 for (item
= 0; item
< e
->max
; item
++) {
330 if (val
== e
->values
[item
])
335 for (i
= 0; i
< e
->max
; i
++) {
336 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
341 /* does not affect routing - always connected */
342 case snd_soc_dapm_pga
:
343 case snd_soc_dapm_out_drv
:
344 case snd_soc_dapm_output
:
345 case snd_soc_dapm_adc
:
346 case snd_soc_dapm_input
:
347 case snd_soc_dapm_siggen
:
348 case snd_soc_dapm_dac
:
349 case snd_soc_dapm_micbias
:
350 case snd_soc_dapm_vmid
:
351 case snd_soc_dapm_supply
:
352 case snd_soc_dapm_regulator_supply
:
353 case snd_soc_dapm_aif_in
:
354 case snd_soc_dapm_aif_out
:
355 case snd_soc_dapm_hp
:
356 case snd_soc_dapm_mic
:
357 case snd_soc_dapm_spk
:
358 case snd_soc_dapm_line
:
361 /* does affect routing - dynamically connected */
362 case snd_soc_dapm_pre
:
363 case snd_soc_dapm_post
:
369 /* connect mux widget to its interconnecting audio paths */
370 static int dapm_connect_mux(struct snd_soc_dapm_context
*dapm
,
371 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
372 struct snd_soc_dapm_path
*path
, const char *control_name
,
373 const struct snd_kcontrol_new
*kcontrol
)
375 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
378 for (i
= 0; i
< e
->max
; i
++) {
379 if (!(strcmp(control_name
, e
->texts
[i
]))) {
380 list_add(&path
->list
, &dapm
->card
->paths
);
381 list_add(&path
->list_sink
, &dest
->sources
);
382 list_add(&path
->list_source
, &src
->sinks
);
383 path
->name
= (char*)e
->texts
[i
];
384 dapm_set_path_status(dest
, path
, 0);
392 /* connect mixer widget to its interconnecting audio paths */
393 static int dapm_connect_mixer(struct snd_soc_dapm_context
*dapm
,
394 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
395 struct snd_soc_dapm_path
*path
, const char *control_name
)
399 /* search for mixer kcontrol */
400 for (i
= 0; i
< dest
->num_kcontrols
; i
++) {
401 if (!strcmp(control_name
, dest
->kcontrol_news
[i
].name
)) {
402 list_add(&path
->list
, &dapm
->card
->paths
);
403 list_add(&path
->list_sink
, &dest
->sources
);
404 list_add(&path
->list_source
, &src
->sinks
);
405 path
->name
= dest
->kcontrol_news
[i
].name
;
406 dapm_set_path_status(dest
, path
, i
);
413 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context
*dapm
,
414 struct snd_soc_dapm_widget
*kcontrolw
,
415 const struct snd_kcontrol_new
*kcontrol_new
,
416 struct snd_kcontrol
**kcontrol
)
418 struct snd_soc_dapm_widget
*w
;
423 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
424 if (w
== kcontrolw
|| w
->dapm
!= kcontrolw
->dapm
)
426 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
427 if (&w
->kcontrol_news
[i
] == kcontrol_new
) {
429 *kcontrol
= w
->kcontrols
[i
];
438 /* create new dapm mixer control */
439 static int dapm_new_mixer(struct snd_soc_dapm_widget
*w
)
441 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
443 size_t name_len
, prefix_len
;
444 struct snd_soc_dapm_path
*path
;
445 struct snd_card
*card
= dapm
->card
->snd_card
;
447 struct snd_soc_dapm_widget_list
*wlist
;
451 prefix
= dapm
->codec
->name_prefix
;
456 prefix_len
= strlen(prefix
) + 1;
461 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
464 list_for_each_entry(path
, &w
->sources
, list_sink
) {
466 /* mixer/mux paths name must match control name */
467 if (path
->name
!= (char *)w
->kcontrol_news
[i
].name
)
470 if (w
->kcontrols
[i
]) {
471 path
->kcontrol
= w
->kcontrols
[i
];
475 wlistsize
= sizeof(struct snd_soc_dapm_widget_list
) +
476 sizeof(struct snd_soc_dapm_widget
*),
477 wlist
= kzalloc(wlistsize
, GFP_KERNEL
);
480 "asoc: can't allocate widget list for %s\n",
484 wlist
->num_widgets
= 1;
485 wlist
->widgets
[0] = w
;
487 /* add dapm control with long name.
488 * for dapm_mixer this is the concatenation of the
489 * mixer and kcontrol name.
490 * for dapm_mixer_named_ctl this is simply the
493 name_len
= strlen(w
->kcontrol_news
[i
].name
) + 1;
494 if (w
->id
!= snd_soc_dapm_mixer_named_ctl
)
495 name_len
+= 1 + strlen(w
->name
);
497 path
->long_name
= kmalloc(name_len
, GFP_KERNEL
);
499 if (path
->long_name
== NULL
) {
506 /* The control will get a prefix from
507 * the control creation process but
508 * we're also using the same prefix
509 * for widgets so cut the prefix off
510 * the front of the widget name.
512 snprintf(path
->long_name
, name_len
, "%s %s",
513 w
->name
+ prefix_len
,
514 w
->kcontrol_news
[i
].name
);
516 case snd_soc_dapm_mixer_named_ctl
:
517 snprintf(path
->long_name
, name_len
, "%s",
518 w
->kcontrol_news
[i
].name
);
522 path
->long_name
[name_len
- 1] = '\0';
524 path
->kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[i
],
525 wlist
, path
->long_name
,
527 ret
= snd_ctl_add(card
, path
->kcontrol
);
530 "asoc: failed to add dapm kcontrol %s: %d\n",
531 path
->long_name
, ret
);
533 kfree(path
->long_name
);
534 path
->long_name
= NULL
;
537 w
->kcontrols
[i
] = path
->kcontrol
;
543 /* create new dapm mux control */
544 static int dapm_new_mux(struct snd_soc_dapm_widget
*w
)
546 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
547 struct snd_soc_dapm_path
*path
= NULL
;
548 struct snd_kcontrol
*kcontrol
;
549 struct snd_card
*card
= dapm
->card
->snd_card
;
553 struct snd_soc_dapm_widget_list
*wlist
;
554 int shared
, wlistentries
;
558 if (w
->num_kcontrols
!= 1) {
560 "asoc: mux %s has incorrect number of controls\n",
565 shared
= dapm_is_shared_kcontrol(dapm
, w
, &w
->kcontrol_news
[0],
568 wlist
= kcontrol
->private_data
;
569 wlistentries
= wlist
->num_widgets
+ 1;
574 wlistsize
= sizeof(struct snd_soc_dapm_widget_list
) +
575 wlistentries
* sizeof(struct snd_soc_dapm_widget
*),
576 wlist
= krealloc(wlist
, wlistsize
, GFP_KERNEL
);
579 "asoc: can't allocate widget list for %s\n", w
->name
);
582 wlist
->num_widgets
= wlistentries
;
583 wlist
->widgets
[wlistentries
- 1] = w
;
587 prefix
= dapm
->codec
->name_prefix
;
592 name
= w
->kcontrol_news
[0].name
;
597 prefix_len
= strlen(prefix
) + 1;
603 * The control will get a prefix from the control creation
604 * process but we're also using the same prefix for widgets so
605 * cut the prefix off the front of the widget name.
607 kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[0], wlist
,
608 name
+ prefix_len
, prefix
);
609 ret
= snd_ctl_add(card
, kcontrol
);
611 dev_err(dapm
->dev
, "failed to add kcontrol %s: %d\n",
618 kcontrol
->private_data
= wlist
;
620 w
->kcontrols
[0] = kcontrol
;
622 list_for_each_entry(path
, &w
->sources
, list_sink
)
623 path
->kcontrol
= kcontrol
;
628 /* create new dapm volume control */
629 static int dapm_new_pga(struct snd_soc_dapm_widget
*w
)
631 if (w
->num_kcontrols
)
632 dev_err(w
->dapm
->dev
,
633 "asoc: PGA controls not supported: '%s'\n", w
->name
);
638 /* reset 'walked' bit for each dapm path */
639 static inline void dapm_clear_walk(struct snd_soc_dapm_context
*dapm
)
641 struct snd_soc_dapm_path
*p
;
643 list_for_each_entry(p
, &dapm
->card
->paths
, list
)
647 /* We implement power down on suspend by checking the power state of
648 * the ALSA card - when we are suspending the ALSA state for the card
651 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
653 int level
= snd_power_get_state(widget
->dapm
->card
->snd_card
);
656 case SNDRV_CTL_POWER_D3hot
:
657 case SNDRV_CTL_POWER_D3cold
:
658 if (widget
->ignore_suspend
)
659 dev_dbg(widget
->dapm
->dev
, "%s ignoring suspend\n",
661 return widget
->ignore_suspend
;
668 * Recursively check for a completed path to an active or physically connected
669 * output widget. Returns number of complete paths.
671 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
)
673 struct snd_soc_dapm_path
*path
;
676 if (widget
->outputs
>= 0)
677 return widget
->outputs
;
679 DAPM_UPDATE_STAT(widget
, path_checks
);
681 switch (widget
->id
) {
682 case snd_soc_dapm_supply
:
683 case snd_soc_dapm_regulator_supply
:
689 switch (widget
->id
) {
690 case snd_soc_dapm_adc
:
691 case snd_soc_dapm_aif_out
:
692 if (widget
->active
) {
693 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
694 return widget
->outputs
;
700 if (widget
->connected
) {
701 /* connected pin ? */
702 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
) {
703 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
704 return widget
->outputs
;
707 /* connected jack or spk ? */
708 if (widget
->id
== snd_soc_dapm_hp
||
709 widget
->id
== snd_soc_dapm_spk
||
710 (widget
->id
== snd_soc_dapm_line
&&
711 !list_empty(&widget
->sources
))) {
712 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
713 return widget
->outputs
;
717 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
718 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
726 if (path
->sink
&& path
->connect
) {
728 con
+= is_connected_output_ep(path
->sink
);
732 widget
->outputs
= con
;
738 * Recursively check for a completed path to an active or physically connected
739 * input widget. Returns number of complete paths.
741 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
)
743 struct snd_soc_dapm_path
*path
;
746 if (widget
->inputs
>= 0)
747 return widget
->inputs
;
749 DAPM_UPDATE_STAT(widget
, path_checks
);
751 switch (widget
->id
) {
752 case snd_soc_dapm_supply
:
753 case snd_soc_dapm_regulator_supply
:
759 /* active stream ? */
760 switch (widget
->id
) {
761 case snd_soc_dapm_dac
:
762 case snd_soc_dapm_aif_in
:
763 if (widget
->active
) {
764 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
765 return widget
->inputs
;
771 if (widget
->connected
) {
772 /* connected pin ? */
773 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
) {
774 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
775 return widget
->inputs
;
778 /* connected VMID/Bias for lower pops */
779 if (widget
->id
== snd_soc_dapm_vmid
) {
780 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
781 return widget
->inputs
;
784 /* connected jack ? */
785 if (widget
->id
== snd_soc_dapm_mic
||
786 (widget
->id
== snd_soc_dapm_line
&&
787 !list_empty(&widget
->sinks
))) {
788 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
789 return widget
->inputs
;
792 /* signal generator */
793 if (widget
->id
== snd_soc_dapm_siggen
) {
794 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
795 return widget
->inputs
;
799 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
800 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
808 if (path
->source
&& path
->connect
) {
810 con
+= is_connected_input_ep(path
->source
);
814 widget
->inputs
= con
;
820 * Handler for generic register modifier widget.
822 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
823 struct snd_kcontrol
*kcontrol
, int event
)
827 if (SND_SOC_DAPM_EVENT_ON(event
))
832 soc_widget_update_bits(w
, -(w
->reg
+ 1),
833 w
->mask
<< w
->shift
, val
<< w
->shift
);
837 EXPORT_SYMBOL_GPL(dapm_reg_event
);
840 * Handler for regulator supply widget.
842 int dapm_regulator_event(struct snd_soc_dapm_widget
*w
,
843 struct snd_kcontrol
*kcontrol
, int event
)
845 if (SND_SOC_DAPM_EVENT_ON(event
))
846 return regulator_enable(w
->priv
);
848 return regulator_disable_deferred(w
->priv
, w
->shift
);
850 EXPORT_SYMBOL_GPL(dapm_regulator_event
);
852 static int dapm_widget_power_check(struct snd_soc_dapm_widget
*w
)
854 if (w
->power_checked
)
860 w
->new_power
= w
->power_check(w
);
862 w
->power_checked
= true;
867 /* Generic check to see if a widget should be powered.
869 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
873 DAPM_UPDATE_STAT(w
, power_checks
);
875 in
= is_connected_input_ep(w
);
876 dapm_clear_walk(w
->dapm
);
877 out
= is_connected_output_ep(w
);
878 dapm_clear_walk(w
->dapm
);
879 return out
!= 0 && in
!= 0;
882 /* Check to see if an ADC has power */
883 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
887 DAPM_UPDATE_STAT(w
, power_checks
);
890 in
= is_connected_input_ep(w
);
891 dapm_clear_walk(w
->dapm
);
894 return dapm_generic_check_power(w
);
898 /* Check to see if a DAC has power */
899 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
903 DAPM_UPDATE_STAT(w
, power_checks
);
906 out
= is_connected_output_ep(w
);
907 dapm_clear_walk(w
->dapm
);
910 return dapm_generic_check_power(w
);
914 /* Check to see if a power supply is needed */
915 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
917 struct snd_soc_dapm_path
*path
;
919 DAPM_UPDATE_STAT(w
, power_checks
);
921 /* Check if one of our outputs is connected */
922 list_for_each_entry(path
, &w
->sinks
, list_source
) {
923 DAPM_UPDATE_STAT(w
, neighbour_checks
);
928 if (path
->connected
&&
929 !path
->connected(path
->source
, path
->sink
))
935 if (dapm_widget_power_check(path
->sink
))
939 dapm_clear_walk(w
->dapm
);
944 static int dapm_always_on_check_power(struct snd_soc_dapm_widget
*w
)
949 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
950 struct snd_soc_dapm_widget
*b
,
958 sort
= dapm_down_seq
;
960 if (sort
[a
->id
] != sort
[b
->id
])
961 return sort
[a
->id
] - sort
[b
->id
];
962 if (a
->subseq
!= b
->subseq
) {
964 return a
->subseq
- b
->subseq
;
966 return b
->subseq
- a
->subseq
;
968 if (a
->reg
!= b
->reg
)
969 return a
->reg
- b
->reg
;
970 if (a
->dapm
!= b
->dapm
)
971 return (unsigned long)a
->dapm
- (unsigned long)b
->dapm
;
976 /* Insert a widget in order into a DAPM power sequence. */
977 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
978 struct list_head
*list
,
981 struct snd_soc_dapm_widget
*w
;
983 list_for_each_entry(w
, list
, power_list
)
984 if (dapm_seq_compare(new_widget
, w
, power_up
) < 0) {
985 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
989 list_add_tail(&new_widget
->power_list
, list
);
992 static void dapm_seq_check_event(struct snd_soc_dapm_context
*dapm
,
993 struct snd_soc_dapm_widget
*w
, int event
)
995 struct snd_soc_card
*card
= dapm
->card
;
1000 case SND_SOC_DAPM_PRE_PMU
:
1001 ev_name
= "PRE_PMU";
1004 case SND_SOC_DAPM_POST_PMU
:
1005 ev_name
= "POST_PMU";
1008 case SND_SOC_DAPM_PRE_PMD
:
1009 ev_name
= "PRE_PMD";
1012 case SND_SOC_DAPM_POST_PMD
:
1013 ev_name
= "POST_PMD";
1021 if (w
->power
!= power
)
1024 if (w
->event
&& (w
->event_flags
& event
)) {
1025 pop_dbg(dapm
->dev
, card
->pop_time
, "pop test : %s %s\n",
1027 trace_snd_soc_dapm_widget_event_start(w
, event
);
1028 ret
= w
->event(w
, NULL
, event
);
1029 trace_snd_soc_dapm_widget_event_done(w
, event
);
1031 pr_err("%s: %s event failed: %d\n",
1032 ev_name
, w
->name
, ret
);
1036 /* Apply the coalesced changes from a DAPM sequence */
1037 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context
*dapm
,
1038 struct list_head
*pending
)
1040 struct snd_soc_card
*card
= dapm
->card
;
1041 struct snd_soc_dapm_widget
*w
;
1043 unsigned int value
= 0;
1044 unsigned int mask
= 0;
1045 unsigned int cur_mask
;
1047 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
1050 list_for_each_entry(w
, pending
, power_list
) {
1051 cur_mask
= 1 << w
->shift
;
1052 BUG_ON(reg
!= w
->reg
);
1063 pop_dbg(dapm
->dev
, card
->pop_time
,
1064 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1065 w
->name
, reg
, value
, mask
);
1067 /* Check for events */
1068 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_PRE_PMU
);
1069 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_PRE_PMD
);
1073 /* Any widget will do, they should all be updating the
1076 w
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
1079 pop_dbg(dapm
->dev
, card
->pop_time
,
1080 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1081 value
, mask
, reg
, card
->pop_time
);
1082 pop_wait(card
->pop_time
);
1083 soc_widget_update_bits(w
, reg
, mask
, value
);
1086 list_for_each_entry(w
, pending
, power_list
) {
1087 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_POST_PMU
);
1088 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_POST_PMD
);
1092 /* Apply a DAPM power sequence.
1094 * We walk over a pre-sorted list of widgets to apply power to. In
1095 * order to minimise the number of writes to the device required
1096 * multiple widgets will be updated in a single write where possible.
1097 * Currently anything that requires more than a single write is not
1100 static void dapm_seq_run(struct snd_soc_dapm_context
*dapm
,
1101 struct list_head
*list
, int event
, bool power_up
)
1103 struct snd_soc_dapm_widget
*w
, *n
;
1106 int cur_subseq
= -1;
1107 int cur_reg
= SND_SOC_NOPM
;
1108 struct snd_soc_dapm_context
*cur_dapm
= NULL
;
1115 sort
= dapm_down_seq
;
1117 list_for_each_entry_safe(w
, n
, list
, power_list
) {
1120 /* Do we need to apply any queued changes? */
1121 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
||
1122 w
->dapm
!= cur_dapm
|| w
->subseq
!= cur_subseq
) {
1123 if (!list_empty(&pending
))
1124 dapm_seq_run_coalesced(cur_dapm
, &pending
);
1126 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1127 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1128 if (sort
[i
] == cur_sort
)
1129 cur_dapm
->seq_notifier(cur_dapm
,
1134 INIT_LIST_HEAD(&pending
);
1136 cur_subseq
= INT_MIN
;
1137 cur_reg
= SND_SOC_NOPM
;
1142 case snd_soc_dapm_pre
:
1144 list_for_each_entry_safe_continue(w
, n
, list
,
1147 if (event
== SND_SOC_DAPM_STREAM_START
)
1149 NULL
, SND_SOC_DAPM_PRE_PMU
);
1150 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1152 NULL
, SND_SOC_DAPM_PRE_PMD
);
1155 case snd_soc_dapm_post
:
1157 list_for_each_entry_safe_continue(w
, n
, list
,
1160 if (event
== SND_SOC_DAPM_STREAM_START
)
1162 NULL
, SND_SOC_DAPM_POST_PMU
);
1163 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1165 NULL
, SND_SOC_DAPM_POST_PMD
);
1169 /* Queue it up for application */
1170 cur_sort
= sort
[w
->id
];
1171 cur_subseq
= w
->subseq
;
1174 list_move(&w
->power_list
, &pending
);
1179 dev_err(w
->dapm
->dev
,
1180 "Failed to apply widget power: %d\n", ret
);
1183 if (!list_empty(&pending
))
1184 dapm_seq_run_coalesced(cur_dapm
, &pending
);
1186 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1187 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1188 if (sort
[i
] == cur_sort
)
1189 cur_dapm
->seq_notifier(cur_dapm
,
1194 static void dapm_widget_update(struct snd_soc_dapm_context
*dapm
)
1196 struct snd_soc_dapm_update
*update
= dapm
->update
;
1197 struct snd_soc_dapm_widget
*w
;
1206 (w
->event_flags
& SND_SOC_DAPM_PRE_REG
)) {
1207 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_PRE_REG
);
1209 pr_err("%s DAPM pre-event failed: %d\n",
1213 ret
= snd_soc_update_bits(w
->codec
, update
->reg
, update
->mask
,
1216 pr_err("%s DAPM update failed: %d\n", w
->name
, ret
);
1219 (w
->event_flags
& SND_SOC_DAPM_POST_REG
)) {
1220 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_POST_REG
);
1222 pr_err("%s DAPM post-event failed: %d\n",
1227 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1228 * they're changing state.
1230 static void dapm_pre_sequence_async(void *data
, async_cookie_t cookie
)
1232 struct snd_soc_dapm_context
*d
= data
;
1235 /* If we're off and we're not supposed to be go into STANDBY */
1236 if (d
->bias_level
== SND_SOC_BIAS_OFF
&&
1237 d
->target_bias_level
!= SND_SOC_BIAS_OFF
) {
1239 pm_runtime_get_sync(d
->dev
);
1241 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1244 "Failed to turn on bias: %d\n", ret
);
1247 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1248 if (d
->bias_level
!= d
->target_bias_level
) {
1249 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_PREPARE
);
1252 "Failed to prepare bias: %d\n", ret
);
1256 /* Async callback run prior to DAPM sequences - brings to their final
1259 static void dapm_post_sequence_async(void *data
, async_cookie_t cookie
)
1261 struct snd_soc_dapm_context
*d
= data
;
1264 /* If we just powered the last thing off drop to standby bias */
1265 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1266 (d
->target_bias_level
== SND_SOC_BIAS_STANDBY
||
1267 d
->target_bias_level
== SND_SOC_BIAS_OFF
)) {
1268 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1270 dev_err(d
->dev
, "Failed to apply standby bias: %d\n",
1274 /* If we're in standby and can support bias off then do that */
1275 if (d
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1276 d
->target_bias_level
== SND_SOC_BIAS_OFF
) {
1277 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_OFF
);
1279 dev_err(d
->dev
, "Failed to turn off bias: %d\n", ret
);
1282 pm_runtime_put(d
->dev
);
1285 /* If we just powered up then move to active bias */
1286 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1287 d
->target_bias_level
== SND_SOC_BIAS_ON
) {
1288 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_ON
);
1290 dev_err(d
->dev
, "Failed to apply active bias: %d\n",
1295 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget
*peer
,
1296 bool power
, bool connect
)
1298 /* If a connection is being made or broken then that update
1299 * will have marked the peer dirty, otherwise the widgets are
1300 * not connected and this update has no impact. */
1304 /* If the peer is already in the state we're moving to then we
1305 * won't have an impact on it. */
1306 if (power
!= peer
->power
)
1307 dapm_mark_dirty(peer
, "peer state change");
1310 static void dapm_widget_set_power(struct snd_soc_dapm_widget
*w
, bool power
,
1311 struct list_head
*up_list
,
1312 struct list_head
*down_list
)
1314 struct snd_soc_dapm_path
*path
;
1316 if (w
->power
== power
)
1319 trace_snd_soc_dapm_widget_power(w
, power
);
1321 /* If we changed our power state perhaps our neigbours changed
1324 list_for_each_entry(path
, &w
->sources
, list_sink
) {
1326 dapm_widget_set_peer_power(path
->source
, power
,
1331 case snd_soc_dapm_supply
:
1332 case snd_soc_dapm_regulator_supply
:
1333 /* Supplies can't affect their outputs, only their inputs */
1336 list_for_each_entry(path
, &w
->sinks
, list_source
) {
1338 dapm_widget_set_peer_power(path
->sink
, power
,
1346 dapm_seq_insert(w
, up_list
, true);
1348 dapm_seq_insert(w
, down_list
, false);
1353 static void dapm_power_one_widget(struct snd_soc_dapm_widget
*w
,
1354 struct list_head
*up_list
,
1355 struct list_head
*down_list
)
1360 case snd_soc_dapm_pre
:
1361 dapm_seq_insert(w
, down_list
, false);
1363 case snd_soc_dapm_post
:
1364 dapm_seq_insert(w
, up_list
, true);
1368 power
= dapm_widget_power_check(w
);
1370 dapm_widget_set_power(w
, power
, up_list
, down_list
);
1376 * Scan each dapm widget for complete audio path.
1377 * A complete path is a route that has valid endpoints i.e.:-
1379 * o DAC to output pin.
1380 * o Input Pin to ADC.
1381 * o Input pin to Output pin (bypass, sidetone)
1382 * o DAC to ADC (loopback).
1384 static int dapm_power_widgets(struct snd_soc_dapm_context
*dapm
, int event
)
1386 struct snd_soc_card
*card
= dapm
->card
;
1387 struct snd_soc_dapm_widget
*w
;
1388 struct snd_soc_dapm_context
*d
;
1390 LIST_HEAD(down_list
);
1391 LIST_HEAD(async_domain
);
1392 enum snd_soc_bias_level bias
;
1394 trace_snd_soc_dapm_start(card
);
1396 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1397 if (d
->n_widgets
|| d
->codec
== NULL
) {
1398 if (d
->idle_bias_off
)
1399 d
->target_bias_level
= SND_SOC_BIAS_OFF
;
1401 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1405 memset(&card
->dapm_stats
, 0, sizeof(card
->dapm_stats
));
1407 list_for_each_entry(w
, &card
->widgets
, list
) {
1408 w
->power_checked
= false;
1413 /* Check which widgets we need to power and store them in
1414 * lists indicating if they should be powered up or down. We
1415 * only check widgets that have been flagged as dirty but note
1416 * that new widgets may be added to the dirty list while we
1419 list_for_each_entry(w
, &card
->dapm_dirty
, dirty
) {
1420 dapm_power_one_widget(w
, &up_list
, &down_list
);
1423 list_for_each_entry(w
, &card
->widgets
, list
) {
1424 list_del_init(&w
->dirty
);
1429 /* Supplies and micbiases only bring the
1430 * context up to STANDBY as unless something
1431 * else is active and passing audio they
1432 * generally don't require full power. Signal
1433 * generators are virtual pins and have no
1434 * power impact themselves.
1437 case snd_soc_dapm_siggen
:
1439 case snd_soc_dapm_supply
:
1440 case snd_soc_dapm_regulator_supply
:
1441 case snd_soc_dapm_micbias
:
1442 if (d
->target_bias_level
< SND_SOC_BIAS_STANDBY
)
1443 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1446 d
->target_bias_level
= SND_SOC_BIAS_ON
;
1453 /* If there are no DAPM widgets then try to figure out power from the
1456 if (!dapm
->n_widgets
) {
1458 case SND_SOC_DAPM_STREAM_START
:
1459 case SND_SOC_DAPM_STREAM_RESUME
:
1460 dapm
->target_bias_level
= SND_SOC_BIAS_ON
;
1462 case SND_SOC_DAPM_STREAM_STOP
:
1463 if (dapm
->codec
&& dapm
->codec
->active
)
1464 dapm
->target_bias_level
= SND_SOC_BIAS_ON
;
1466 dapm
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1468 case SND_SOC_DAPM_STREAM_SUSPEND
:
1469 dapm
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1471 case SND_SOC_DAPM_STREAM_NOP
:
1472 dapm
->target_bias_level
= dapm
->bias_level
;
1479 /* Force all contexts in the card to the same bias state if
1480 * they're not ground referenced.
1482 bias
= SND_SOC_BIAS_OFF
;
1483 list_for_each_entry(d
, &card
->dapm_list
, list
)
1484 if (d
->target_bias_level
> bias
)
1485 bias
= d
->target_bias_level
;
1486 list_for_each_entry(d
, &card
->dapm_list
, list
)
1487 if (!d
->idle_bias_off
)
1488 d
->target_bias_level
= bias
;
1490 trace_snd_soc_dapm_walk_done(card
);
1492 /* Run all the bias changes in parallel */
1493 list_for_each_entry(d
, &dapm
->card
->dapm_list
, list
)
1494 async_schedule_domain(dapm_pre_sequence_async
, d
,
1496 async_synchronize_full_domain(&async_domain
);
1498 /* Power down widgets first; try to avoid amplifying pops. */
1499 dapm_seq_run(dapm
, &down_list
, event
, false);
1501 dapm_widget_update(dapm
);
1504 dapm_seq_run(dapm
, &up_list
, event
, true);
1506 /* Run all the bias changes in parallel */
1507 list_for_each_entry(d
, &dapm
->card
->dapm_list
, list
)
1508 async_schedule_domain(dapm_post_sequence_async
, d
,
1510 async_synchronize_full_domain(&async_domain
);
1512 pop_dbg(dapm
->dev
, card
->pop_time
,
1513 "DAPM sequencing finished, waiting %dms\n", card
->pop_time
);
1514 pop_wait(card
->pop_time
);
1516 trace_snd_soc_dapm_done(card
);
1521 #ifdef CONFIG_DEBUG_FS
1522 static int dapm_widget_power_open_file(struct inode
*inode
, struct file
*file
)
1524 file
->private_data
= inode
->i_private
;
1528 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1529 char __user
*user_buf
,
1530 size_t count
, loff_t
*ppos
)
1532 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1536 struct snd_soc_dapm_path
*p
= NULL
;
1538 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1542 in
= is_connected_input_ep(w
);
1543 dapm_clear_walk(w
->dapm
);
1544 out
= is_connected_output_ep(w
);
1545 dapm_clear_walk(w
->dapm
);
1547 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s in %d out %d",
1548 w
->name
, w
->power
? "On" : "Off", in
, out
);
1551 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1552 " - R%d(0x%x) bit %d",
1553 w
->reg
, w
->reg
, w
->shift
);
1555 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
1558 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1560 w
->active
? "active" : "inactive");
1562 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1563 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1567 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1568 " in \"%s\" \"%s\"\n",
1569 p
->name
? p
->name
: "static",
1572 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1573 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1577 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1578 " out \"%s\" \"%s\"\n",
1579 p
->name
? p
->name
: "static",
1583 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1589 static const struct file_operations dapm_widget_power_fops
= {
1590 .open
= dapm_widget_power_open_file
,
1591 .read
= dapm_widget_power_read_file
,
1592 .llseek
= default_llseek
,
1595 static int dapm_bias_open_file(struct inode
*inode
, struct file
*file
)
1597 file
->private_data
= inode
->i_private
;
1601 static ssize_t
dapm_bias_read_file(struct file
*file
, char __user
*user_buf
,
1602 size_t count
, loff_t
*ppos
)
1604 struct snd_soc_dapm_context
*dapm
= file
->private_data
;
1607 switch (dapm
->bias_level
) {
1608 case SND_SOC_BIAS_ON
:
1611 case SND_SOC_BIAS_PREPARE
:
1612 level
= "Prepare\n";
1614 case SND_SOC_BIAS_STANDBY
:
1615 level
= "Standby\n";
1617 case SND_SOC_BIAS_OFF
:
1622 level
= "Unknown\n";
1626 return simple_read_from_buffer(user_buf
, count
, ppos
, level
,
1630 static const struct file_operations dapm_bias_fops
= {
1631 .open
= dapm_bias_open_file
,
1632 .read
= dapm_bias_read_file
,
1633 .llseek
= default_llseek
,
1636 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
1637 struct dentry
*parent
)
1641 dapm
->debugfs_dapm
= debugfs_create_dir("dapm", parent
);
1643 if (!dapm
->debugfs_dapm
) {
1645 "Failed to create DAPM debugfs directory\n");
1649 d
= debugfs_create_file("bias_level", 0444,
1650 dapm
->debugfs_dapm
, dapm
,
1654 "ASoC: Failed to create bias level debugfs file\n");
1657 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
1659 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
1662 if (!dapm
->debugfs_dapm
|| !w
->name
)
1665 d
= debugfs_create_file(w
->name
, 0444,
1666 dapm
->debugfs_dapm
, w
,
1667 &dapm_widget_power_fops
);
1669 dev_warn(w
->dapm
->dev
,
1670 "ASoC: Failed to create %s debugfs file\n",
1674 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
1676 debugfs_remove_recursive(dapm
->debugfs_dapm
);
1680 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
1681 struct dentry
*parent
)
1685 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
1689 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
1695 /* test and update the power status of a mux widget */
1696 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget
*widget
,
1697 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
)
1699 struct snd_soc_dapm_path
*path
;
1702 if (widget
->id
!= snd_soc_dapm_mux
&&
1703 widget
->id
!= snd_soc_dapm_virt_mux
&&
1704 widget
->id
!= snd_soc_dapm_value_mux
)
1707 /* find dapm widget path assoc with kcontrol */
1708 list_for_each_entry(path
, &widget
->dapm
->card
->paths
, list
) {
1709 if (path
->kcontrol
!= kcontrol
)
1712 if (!path
->name
|| !e
->texts
[mux
])
1716 /* we now need to match the string in the enum to the path */
1717 if (!(strcmp(path
->name
, e
->texts
[mux
]))) {
1718 path
->connect
= 1; /* new connection */
1719 dapm_mark_dirty(path
->source
, "mux connection");
1722 dapm_mark_dirty(path
->source
,
1723 "mux disconnection");
1724 path
->connect
= 0; /* old connection must be powered down */
1729 dapm_mark_dirty(widget
, "mux change");
1730 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1735 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power
);
1737 /* test and update the power status of a mixer or switch widget */
1738 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget
*widget
,
1739 struct snd_kcontrol
*kcontrol
, int connect
)
1741 struct snd_soc_dapm_path
*path
;
1744 if (widget
->id
!= snd_soc_dapm_mixer
&&
1745 widget
->id
!= snd_soc_dapm_mixer_named_ctl
&&
1746 widget
->id
!= snd_soc_dapm_switch
)
1749 /* find dapm widget path assoc with kcontrol */
1750 list_for_each_entry(path
, &widget
->dapm
->card
->paths
, list
) {
1751 if (path
->kcontrol
!= kcontrol
)
1754 /* found, now check type */
1756 path
->connect
= connect
;
1757 dapm_mark_dirty(path
->source
, "mixer connection");
1761 dapm_mark_dirty(widget
, "mixer update");
1762 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1767 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power
);
1769 /* show dapm widget status in sys fs */
1770 static ssize_t
dapm_widget_show(struct device
*dev
,
1771 struct device_attribute
*attr
, char *buf
)
1773 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
1774 struct snd_soc_codec
*codec
=rtd
->codec
;
1775 struct snd_soc_dapm_widget
*w
;
1777 char *state
= "not set";
1779 list_for_each_entry(w
, &codec
->card
->widgets
, list
) {
1780 if (w
->dapm
!= &codec
->dapm
)
1783 /* only display widgets that burnm power */
1785 case snd_soc_dapm_hp
:
1786 case snd_soc_dapm_mic
:
1787 case snd_soc_dapm_spk
:
1788 case snd_soc_dapm_line
:
1789 case snd_soc_dapm_micbias
:
1790 case snd_soc_dapm_dac
:
1791 case snd_soc_dapm_adc
:
1792 case snd_soc_dapm_pga
:
1793 case snd_soc_dapm_out_drv
:
1794 case snd_soc_dapm_mixer
:
1795 case snd_soc_dapm_mixer_named_ctl
:
1796 case snd_soc_dapm_supply
:
1797 case snd_soc_dapm_regulator_supply
:
1799 count
+= sprintf(buf
+ count
, "%s: %s\n",
1800 w
->name
, w
->power
? "On":"Off");
1807 switch (codec
->dapm
.bias_level
) {
1808 case SND_SOC_BIAS_ON
:
1811 case SND_SOC_BIAS_PREPARE
:
1814 case SND_SOC_BIAS_STANDBY
:
1817 case SND_SOC_BIAS_OFF
:
1821 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
1826 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
1828 int snd_soc_dapm_sys_add(struct device
*dev
)
1830 return device_create_file(dev
, &dev_attr_dapm_widget
);
1833 static void snd_soc_dapm_sys_remove(struct device
*dev
)
1835 device_remove_file(dev
, &dev_attr_dapm_widget
);
1838 /* free all dapm widgets and resources */
1839 static void dapm_free_widgets(struct snd_soc_dapm_context
*dapm
)
1841 struct snd_soc_dapm_widget
*w
, *next_w
;
1842 struct snd_soc_dapm_path
*p
, *next_p
;
1844 list_for_each_entry_safe(w
, next_w
, &dapm
->card
->widgets
, list
) {
1845 if (w
->dapm
!= dapm
)
1849 * remove source and sink paths associated to this widget.
1850 * While removing the path, remove reference to it from both
1851 * source and sink widgets so that path is removed only once.
1853 list_for_each_entry_safe(p
, next_p
, &w
->sources
, list_sink
) {
1854 list_del(&p
->list_sink
);
1855 list_del(&p
->list_source
);
1857 kfree(p
->long_name
);
1860 list_for_each_entry_safe(p
, next_p
, &w
->sinks
, list_source
) {
1861 list_del(&p
->list_sink
);
1862 list_del(&p
->list_source
);
1864 kfree(p
->long_name
);
1867 kfree(w
->kcontrols
);
1873 static struct snd_soc_dapm_widget
*dapm_find_widget(
1874 struct snd_soc_dapm_context
*dapm
, const char *pin
,
1875 bool search_other_contexts
)
1877 struct snd_soc_dapm_widget
*w
;
1878 struct snd_soc_dapm_widget
*fallback
= NULL
;
1880 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
1881 if (!strcmp(w
->name
, pin
)) {
1882 if (w
->dapm
== dapm
)
1889 if (search_other_contexts
)
1895 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context
*dapm
,
1896 const char *pin
, int status
)
1898 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
1901 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
1905 w
->connected
= status
;
1908 dapm_mark_dirty(w
, "pin configuration");
1914 * snd_soc_dapm_sync - scan and power dapm paths
1915 * @dapm: DAPM context
1917 * Walks all dapm audio paths and powers widgets according to their
1918 * stream or path usage.
1920 * Returns 0 for success.
1922 int snd_soc_dapm_sync(struct snd_soc_dapm_context
*dapm
)
1925 * Suppress early reports (eg, jacks syncing their state) to avoid
1926 * silly DAPM runs during card startup.
1928 if (!dapm
->card
|| !dapm
->card
->instantiated
)
1931 return dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
1933 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
1935 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context
*dapm
,
1936 const struct snd_soc_dapm_route
*route
)
1938 struct snd_soc_dapm_path
*path
;
1939 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
1940 struct snd_soc_dapm_widget
*wtsource
= NULL
, *wtsink
= NULL
;
1942 const char *control
= route
->control
;
1944 char prefixed_sink
[80];
1945 char prefixed_source
[80];
1948 if (dapm
->codec
&& dapm
->codec
->name_prefix
) {
1949 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
1950 dapm
->codec
->name_prefix
, route
->sink
);
1951 sink
= prefixed_sink
;
1952 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
1953 dapm
->codec
->name_prefix
, route
->source
);
1954 source
= prefixed_source
;
1957 source
= route
->source
;
1961 * find src and dest widgets over all widgets but favor a widget from
1962 * current DAPM context
1964 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
1965 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
1967 if (w
->dapm
== dapm
)
1971 if (!wsource
&& !(strcmp(w
->name
, source
))) {
1973 if (w
->dapm
== dapm
)
1977 /* use widget from another DAPM context if not found from this */
1983 if (wsource
== NULL
|| wsink
== NULL
)
1986 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
1990 path
->source
= wsource
;
1992 path
->connected
= route
->connected
;
1993 INIT_LIST_HEAD(&path
->list
);
1994 INIT_LIST_HEAD(&path
->list_source
);
1995 INIT_LIST_HEAD(&path
->list_sink
);
1997 /* check for external widgets */
1998 if (wsink
->id
== snd_soc_dapm_input
) {
1999 if (wsource
->id
== snd_soc_dapm_micbias
||
2000 wsource
->id
== snd_soc_dapm_mic
||
2001 wsource
->id
== snd_soc_dapm_line
||
2002 wsource
->id
== snd_soc_dapm_output
)
2005 if (wsource
->id
== snd_soc_dapm_output
) {
2006 if (wsink
->id
== snd_soc_dapm_spk
||
2007 wsink
->id
== snd_soc_dapm_hp
||
2008 wsink
->id
== snd_soc_dapm_line
||
2009 wsink
->id
== snd_soc_dapm_input
)
2013 /* connect static paths */
2014 if (control
== NULL
) {
2015 list_add(&path
->list
, &dapm
->card
->paths
);
2016 list_add(&path
->list_sink
, &wsink
->sources
);
2017 list_add(&path
->list_source
, &wsource
->sinks
);
2022 /* connect dynamic paths */
2023 switch (wsink
->id
) {
2024 case snd_soc_dapm_adc
:
2025 case snd_soc_dapm_dac
:
2026 case snd_soc_dapm_pga
:
2027 case snd_soc_dapm_out_drv
:
2028 case snd_soc_dapm_input
:
2029 case snd_soc_dapm_output
:
2030 case snd_soc_dapm_siggen
:
2031 case snd_soc_dapm_micbias
:
2032 case snd_soc_dapm_vmid
:
2033 case snd_soc_dapm_pre
:
2034 case snd_soc_dapm_post
:
2035 case snd_soc_dapm_supply
:
2036 case snd_soc_dapm_regulator_supply
:
2037 case snd_soc_dapm_aif_in
:
2038 case snd_soc_dapm_aif_out
:
2039 list_add(&path
->list
, &dapm
->card
->paths
);
2040 list_add(&path
->list_sink
, &wsink
->sources
);
2041 list_add(&path
->list_source
, &wsource
->sinks
);
2044 case snd_soc_dapm_mux
:
2045 case snd_soc_dapm_virt_mux
:
2046 case snd_soc_dapm_value_mux
:
2047 ret
= dapm_connect_mux(dapm
, wsource
, wsink
, path
, control
,
2048 &wsink
->kcontrol_news
[0]);
2052 case snd_soc_dapm_switch
:
2053 case snd_soc_dapm_mixer
:
2054 case snd_soc_dapm_mixer_named_ctl
:
2055 ret
= dapm_connect_mixer(dapm
, wsource
, wsink
, path
, control
);
2059 case snd_soc_dapm_hp
:
2060 case snd_soc_dapm_mic
:
2061 case snd_soc_dapm_line
:
2062 case snd_soc_dapm_spk
:
2063 list_add(&path
->list
, &dapm
->card
->paths
);
2064 list_add(&path
->list_sink
, &wsink
->sources
);
2065 list_add(&path
->list_source
, &wsource
->sinks
);
2072 dev_warn(dapm
->dev
, "asoc: no dapm match for %s --> %s --> %s\n",
2073 source
, control
, sink
);
2079 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2080 * @dapm: DAPM context
2081 * @route: audio routes
2082 * @num: number of routes
2084 * Connects 2 dapm widgets together via a named audio path. The sink is
2085 * the widget receiving the audio signal, whilst the source is the sender
2086 * of the audio signal.
2088 * Returns 0 for success else error. On error all resources can be freed
2089 * with a call to snd_soc_card_free().
2091 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context
*dapm
,
2092 const struct snd_soc_dapm_route
*route
, int num
)
2096 for (i
= 0; i
< num
; i
++) {
2097 ret
= snd_soc_dapm_add_route(dapm
, route
);
2099 dev_err(dapm
->dev
, "Failed to add route %s->%s\n",
2100 route
->source
, route
->sink
);
2108 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
2110 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context
*dapm
,
2111 const struct snd_soc_dapm_route
*route
)
2113 struct snd_soc_dapm_widget
*source
= dapm_find_widget(dapm
,
2116 struct snd_soc_dapm_widget
*sink
= dapm_find_widget(dapm
,
2119 struct snd_soc_dapm_path
*path
;
2123 dev_err(dapm
->dev
, "Unable to find source %s for weak route\n",
2129 dev_err(dapm
->dev
, "Unable to find sink %s for weak route\n",
2134 if (route
->control
|| route
->connected
)
2135 dev_warn(dapm
->dev
, "Ignoring control for weak route %s->%s\n",
2136 route
->source
, route
->sink
);
2138 list_for_each_entry(path
, &source
->sinks
, list_source
) {
2139 if (path
->sink
== sink
) {
2146 dev_err(dapm
->dev
, "No path found for weak route %s->%s\n",
2147 route
->source
, route
->sink
);
2149 dev_warn(dapm
->dev
, "%d paths found for weak route %s->%s\n",
2150 count
, route
->source
, route
->sink
);
2156 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2157 * @dapm: DAPM context
2158 * @route: audio routes
2159 * @num: number of routes
2161 * Mark existing routes matching those specified in the passed array
2162 * as being weak, meaning that they are ignored for the purpose of
2163 * power decisions. The main intended use case is for sidetone paths
2164 * which couple audio between other independent paths if they are both
2165 * active in order to make the combination work better at the user
2166 * level but which aren't intended to be "used".
2168 * Note that CODEC drivers should not use this as sidetone type paths
2169 * can frequently also be used as bypass paths.
2171 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context
*dapm
,
2172 const struct snd_soc_dapm_route
*route
, int num
)
2177 for (i
= 0; i
< num
; i
++) {
2178 err
= snd_soc_dapm_weak_route(dapm
, route
);
2186 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes
);
2189 * snd_soc_dapm_new_widgets - add new dapm widgets
2190 * @dapm: DAPM context
2192 * Checks the codec for any new dapm widgets and creates them if found.
2194 * Returns 0 for success.
2196 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context
*dapm
)
2198 struct snd_soc_dapm_widget
*w
;
2201 list_for_each_entry(w
, &dapm
->card
->widgets
, list
)
2206 if (w
->num_kcontrols
) {
2207 w
->kcontrols
= kzalloc(w
->num_kcontrols
*
2208 sizeof(struct snd_kcontrol
*),
2215 case snd_soc_dapm_switch
:
2216 case snd_soc_dapm_mixer
:
2217 case snd_soc_dapm_mixer_named_ctl
:
2220 case snd_soc_dapm_mux
:
2221 case snd_soc_dapm_virt_mux
:
2222 case snd_soc_dapm_value_mux
:
2225 case snd_soc_dapm_pga
:
2226 case snd_soc_dapm_out_drv
:
2233 /* Read the initial power state from the device */
2235 val
= soc_widget_read(w
, w
->reg
);
2236 val
&= 1 << w
->shift
;
2246 dapm_mark_dirty(w
, "new widget");
2247 dapm_debugfs_add_widget(w
);
2250 dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
2253 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
2256 * snd_soc_dapm_get_volsw - dapm mixer get callback
2257 * @kcontrol: mixer control
2258 * @ucontrol: control element information
2260 * Callback to get the value of a dapm mixer control.
2262 * Returns 0 for success.
2264 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
2265 struct snd_ctl_elem_value
*ucontrol
)
2267 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2268 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2269 struct soc_mixer_control
*mc
=
2270 (struct soc_mixer_control
*)kcontrol
->private_value
;
2271 unsigned int reg
= mc
->reg
;
2272 unsigned int shift
= mc
->shift
;
2273 unsigned int rshift
= mc
->rshift
;
2275 unsigned int invert
= mc
->invert
;
2276 unsigned int mask
= (1 << fls(max
)) - 1;
2278 ucontrol
->value
.integer
.value
[0] =
2279 (snd_soc_read(widget
->codec
, reg
) >> shift
) & mask
;
2280 if (shift
!= rshift
)
2281 ucontrol
->value
.integer
.value
[1] =
2282 (snd_soc_read(widget
->codec
, reg
) >> rshift
) & mask
;
2284 ucontrol
->value
.integer
.value
[0] =
2285 max
- ucontrol
->value
.integer
.value
[0];
2286 if (shift
!= rshift
)
2287 ucontrol
->value
.integer
.value
[1] =
2288 max
- ucontrol
->value
.integer
.value
[1];
2293 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
2296 * snd_soc_dapm_put_volsw - dapm mixer set callback
2297 * @kcontrol: mixer control
2298 * @ucontrol: control element information
2300 * Callback to set the value of a dapm mixer control.
2302 * Returns 0 for success.
2304 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
2305 struct snd_ctl_elem_value
*ucontrol
)
2307 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2308 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2309 struct snd_soc_codec
*codec
= widget
->codec
;
2310 struct soc_mixer_control
*mc
=
2311 (struct soc_mixer_control
*)kcontrol
->private_value
;
2312 unsigned int reg
= mc
->reg
;
2313 unsigned int shift
= mc
->shift
;
2315 unsigned int mask
= (1 << fls(max
)) - 1;
2316 unsigned int invert
= mc
->invert
;
2318 int connect
, change
;
2319 struct snd_soc_dapm_update update
;
2322 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2326 mask
= mask
<< shift
;
2330 /* new connection */
2331 connect
= invert
? 0 : 1;
2333 /* old connection must be powered down */
2334 connect
= invert
? 1 : 0;
2336 mutex_lock(&codec
->mutex
);
2338 change
= snd_soc_test_bits(widget
->codec
, reg
, mask
, val
);
2340 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2341 widget
= wlist
->widgets
[wi
];
2343 widget
->value
= val
;
2345 update
.kcontrol
= kcontrol
;
2346 update
.widget
= widget
;
2350 widget
->dapm
->update
= &update
;
2352 snd_soc_dapm_mixer_update_power(widget
, kcontrol
, connect
);
2354 widget
->dapm
->update
= NULL
;
2358 mutex_unlock(&codec
->mutex
);
2361 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
2364 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2365 * @kcontrol: mixer control
2366 * @ucontrol: control element information
2368 * Callback to get the value of a dapm enumerated double mixer control.
2370 * Returns 0 for success.
2372 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
2373 struct snd_ctl_elem_value
*ucontrol
)
2375 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2376 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2377 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2378 unsigned int val
, bitmask
;
2380 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2382 val
= snd_soc_read(widget
->codec
, e
->reg
);
2383 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & (bitmask
- 1);
2384 if (e
->shift_l
!= e
->shift_r
)
2385 ucontrol
->value
.enumerated
.item
[1] =
2386 (val
>> e
->shift_r
) & (bitmask
- 1);
2390 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
2393 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2394 * @kcontrol: mixer control
2395 * @ucontrol: control element information
2397 * Callback to set the value of a dapm enumerated double mixer control.
2399 * Returns 0 for success.
2401 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
2402 struct snd_ctl_elem_value
*ucontrol
)
2404 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2405 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2406 struct snd_soc_codec
*codec
= widget
->codec
;
2407 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2408 unsigned int val
, mux
, change
;
2409 unsigned int mask
, bitmask
;
2410 struct snd_soc_dapm_update update
;
2413 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2415 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2417 mux
= ucontrol
->value
.enumerated
.item
[0];
2418 val
= mux
<< e
->shift_l
;
2419 mask
= (bitmask
- 1) << e
->shift_l
;
2420 if (e
->shift_l
!= e
->shift_r
) {
2421 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2423 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
2424 mask
|= (bitmask
- 1) << e
->shift_r
;
2427 mutex_lock(&codec
->mutex
);
2429 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
2431 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2432 widget
= wlist
->widgets
[wi
];
2434 widget
->value
= val
;
2436 update
.kcontrol
= kcontrol
;
2437 update
.widget
= widget
;
2438 update
.reg
= e
->reg
;
2441 widget
->dapm
->update
= &update
;
2443 snd_soc_dapm_mux_update_power(widget
, kcontrol
, mux
, e
);
2445 widget
->dapm
->update
= NULL
;
2449 mutex_unlock(&codec
->mutex
);
2452 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
2455 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2456 * @kcontrol: mixer control
2457 * @ucontrol: control element information
2459 * Returns 0 for success.
2461 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol
*kcontrol
,
2462 struct snd_ctl_elem_value
*ucontrol
)
2464 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2465 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2467 ucontrol
->value
.enumerated
.item
[0] = widget
->value
;
2471 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt
);
2474 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2475 * @kcontrol: mixer control
2476 * @ucontrol: control element information
2478 * Returns 0 for success.
2480 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol
*kcontrol
,
2481 struct snd_ctl_elem_value
*ucontrol
)
2483 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2484 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2485 struct snd_soc_codec
*codec
= widget
->codec
;
2486 struct soc_enum
*e
=
2487 (struct soc_enum
*)kcontrol
->private_value
;
2492 if (ucontrol
->value
.enumerated
.item
[0] >= e
->max
)
2495 mutex_lock(&codec
->mutex
);
2497 change
= widget
->value
!= ucontrol
->value
.enumerated
.item
[0];
2499 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2500 widget
= wlist
->widgets
[wi
];
2502 widget
->value
= ucontrol
->value
.enumerated
.item
[0];
2504 snd_soc_dapm_mux_update_power(widget
, kcontrol
, widget
->value
, e
);
2508 mutex_unlock(&codec
->mutex
);
2511 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt
);
2514 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2516 * @kcontrol: mixer control
2517 * @ucontrol: control element information
2519 * Callback to get the value of a dapm semi enumerated double mixer control.
2521 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2522 * used for handling bitfield coded enumeration for example.
2524 * Returns 0 for success.
2526 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
2527 struct snd_ctl_elem_value
*ucontrol
)
2529 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2530 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2531 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2532 unsigned int reg_val
, val
, mux
;
2534 reg_val
= snd_soc_read(widget
->codec
, e
->reg
);
2535 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
2536 for (mux
= 0; mux
< e
->max
; mux
++) {
2537 if (val
== e
->values
[mux
])
2540 ucontrol
->value
.enumerated
.item
[0] = mux
;
2541 if (e
->shift_l
!= e
->shift_r
) {
2542 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
2543 for (mux
= 0; mux
< e
->max
; mux
++) {
2544 if (val
== e
->values
[mux
])
2547 ucontrol
->value
.enumerated
.item
[1] = mux
;
2552 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double
);
2555 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2557 * @kcontrol: mixer control
2558 * @ucontrol: control element information
2560 * Callback to set the value of a dapm semi enumerated double mixer control.
2562 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2563 * used for handling bitfield coded enumeration for example.
2565 * Returns 0 for success.
2567 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
2568 struct snd_ctl_elem_value
*ucontrol
)
2570 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2571 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2572 struct snd_soc_codec
*codec
= widget
->codec
;
2573 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2574 unsigned int val
, mux
, change
;
2576 struct snd_soc_dapm_update update
;
2579 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2581 mux
= ucontrol
->value
.enumerated
.item
[0];
2582 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
2583 mask
= e
->mask
<< e
->shift_l
;
2584 if (e
->shift_l
!= e
->shift_r
) {
2585 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2587 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
2588 mask
|= e
->mask
<< e
->shift_r
;
2591 mutex_lock(&codec
->mutex
);
2593 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
2595 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2596 widget
= wlist
->widgets
[wi
];
2598 widget
->value
= val
;
2600 update
.kcontrol
= kcontrol
;
2601 update
.widget
= widget
;
2602 update
.reg
= e
->reg
;
2605 widget
->dapm
->update
= &update
;
2607 snd_soc_dapm_mux_update_power(widget
, kcontrol
, mux
, e
);
2609 widget
->dapm
->update
= NULL
;
2613 mutex_unlock(&codec
->mutex
);
2616 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double
);
2619 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2621 * @kcontrol: mixer control
2622 * @uinfo: control element information
2624 * Callback to provide information about a pin switch control.
2626 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
2627 struct snd_ctl_elem_info
*uinfo
)
2629 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2631 uinfo
->value
.integer
.min
= 0;
2632 uinfo
->value
.integer
.max
= 1;
2636 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
2639 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2641 * @kcontrol: mixer control
2644 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
2645 struct snd_ctl_elem_value
*ucontrol
)
2647 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2648 const char *pin
= (const char *)kcontrol
->private_value
;
2650 mutex_lock(&codec
->mutex
);
2652 ucontrol
->value
.integer
.value
[0] =
2653 snd_soc_dapm_get_pin_status(&codec
->dapm
, pin
);
2655 mutex_unlock(&codec
->mutex
);
2659 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
2662 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2664 * @kcontrol: mixer control
2667 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
2668 struct snd_ctl_elem_value
*ucontrol
)
2670 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2671 const char *pin
= (const char *)kcontrol
->private_value
;
2673 mutex_lock(&codec
->mutex
);
2675 if (ucontrol
->value
.integer
.value
[0])
2676 snd_soc_dapm_enable_pin(&codec
->dapm
, pin
);
2678 snd_soc_dapm_disable_pin(&codec
->dapm
, pin
);
2680 snd_soc_dapm_sync(&codec
->dapm
);
2682 mutex_unlock(&codec
->mutex
);
2686 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
2689 * snd_soc_dapm_new_control - create new dapm control
2690 * @dapm: DAPM context
2691 * @widget: widget template
2693 * Creates a new dapm control based upon the template.
2695 * Returns 0 for success else error.
2697 int snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
2698 const struct snd_soc_dapm_widget
*widget
)
2700 struct snd_soc_dapm_widget
*w
;
2704 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
2708 case snd_soc_dapm_regulator_supply
:
2709 w
->priv
= devm_regulator_get(dapm
->dev
, w
->name
);
2710 if (IS_ERR(w
->priv
)) {
2711 ret
= PTR_ERR(w
->priv
);
2712 dev_err(dapm
->dev
, "Failed to request %s: %d\n",
2721 name_len
= strlen(widget
->name
) + 1;
2722 if (dapm
->codec
&& dapm
->codec
->name_prefix
)
2723 name_len
+= 1 + strlen(dapm
->codec
->name_prefix
);
2724 w
->name
= kmalloc(name_len
, GFP_KERNEL
);
2725 if (w
->name
== NULL
) {
2729 if (dapm
->codec
&& dapm
->codec
->name_prefix
)
2730 snprintf(w
->name
, name_len
, "%s %s",
2731 dapm
->codec
->name_prefix
, widget
->name
);
2733 snprintf(w
->name
, name_len
, "%s", widget
->name
);
2736 case snd_soc_dapm_switch
:
2737 case snd_soc_dapm_mixer
:
2738 case snd_soc_dapm_mixer_named_ctl
:
2739 w
->power_check
= dapm_generic_check_power
;
2741 case snd_soc_dapm_mux
:
2742 case snd_soc_dapm_virt_mux
:
2743 case snd_soc_dapm_value_mux
:
2744 w
->power_check
= dapm_generic_check_power
;
2746 case snd_soc_dapm_adc
:
2747 case snd_soc_dapm_aif_out
:
2748 w
->power_check
= dapm_adc_check_power
;
2750 case snd_soc_dapm_dac
:
2751 case snd_soc_dapm_aif_in
:
2752 w
->power_check
= dapm_dac_check_power
;
2754 case snd_soc_dapm_pga
:
2755 case snd_soc_dapm_out_drv
:
2756 case snd_soc_dapm_input
:
2757 case snd_soc_dapm_output
:
2758 case snd_soc_dapm_micbias
:
2759 case snd_soc_dapm_spk
:
2760 case snd_soc_dapm_hp
:
2761 case snd_soc_dapm_mic
:
2762 case snd_soc_dapm_line
:
2763 w
->power_check
= dapm_generic_check_power
;
2765 case snd_soc_dapm_supply
:
2766 case snd_soc_dapm_regulator_supply
:
2767 w
->power_check
= dapm_supply_check_power
;
2770 w
->power_check
= dapm_always_on_check_power
;
2776 w
->codec
= dapm
->codec
;
2777 w
->platform
= dapm
->platform
;
2778 INIT_LIST_HEAD(&w
->sources
);
2779 INIT_LIST_HEAD(&w
->sinks
);
2780 INIT_LIST_HEAD(&w
->list
);
2781 INIT_LIST_HEAD(&w
->dirty
);
2782 list_add(&w
->list
, &dapm
->card
->widgets
);
2784 /* machine layer set ups unconnected pins and insertions */
2788 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
2791 * snd_soc_dapm_new_controls - create new dapm controls
2792 * @dapm: DAPM context
2793 * @widget: widget array
2794 * @num: number of widgets
2796 * Creates new DAPM controls based upon the templates.
2798 * Returns 0 for success else error.
2800 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context
*dapm
,
2801 const struct snd_soc_dapm_widget
*widget
,
2806 for (i
= 0; i
< num
; i
++) {
2807 ret
= snd_soc_dapm_new_control(dapm
, widget
);
2810 "ASoC: Failed to create DAPM control %s: %d\n",
2818 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
2820 static void soc_dapm_stream_event(struct snd_soc_dapm_context
*dapm
,
2821 const char *stream
, int event
)
2823 struct snd_soc_dapm_widget
*w
;
2825 list_for_each_entry(w
, &dapm
->card
->widgets
, list
)
2827 if (!w
->sname
|| w
->dapm
!= dapm
)
2829 dev_vdbg(w
->dapm
->dev
, "widget %s\n %s stream %s event %d\n",
2830 w
->name
, w
->sname
, stream
, event
);
2831 if (strstr(w
->sname
, stream
)) {
2832 dapm_mark_dirty(w
, "stream event");
2834 case SND_SOC_DAPM_STREAM_START
:
2837 case SND_SOC_DAPM_STREAM_STOP
:
2840 case SND_SOC_DAPM_STREAM_SUSPEND
:
2841 case SND_SOC_DAPM_STREAM_RESUME
:
2842 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
2843 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
2849 dapm_power_widgets(dapm
, event
);
2851 /* do we need to notify any clients that DAPM stream is complete */
2852 if (dapm
->stream_event
)
2853 dapm
->stream_event(dapm
, event
);
2857 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2858 * @rtd: PCM runtime data
2859 * @stream: stream name
2860 * @event: stream event
2862 * Sends a stream event to the dapm core. The core then makes any
2863 * necessary widget power changes.
2865 * Returns 0 for success else error.
2867 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
,
2868 const char *stream
, int event
)
2870 struct snd_soc_codec
*codec
= rtd
->codec
;
2875 mutex_lock(&codec
->mutex
);
2876 soc_dapm_stream_event(&codec
->dapm
, stream
, event
);
2877 mutex_unlock(&codec
->mutex
);
2882 * snd_soc_dapm_enable_pin - enable pin.
2883 * @dapm: DAPM context
2886 * Enables input/output pin and its parents or children widgets iff there is
2887 * a valid audio route and active audio stream.
2888 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2889 * do any widget power switching.
2891 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
2893 return snd_soc_dapm_set_pin(dapm
, pin
, 1);
2895 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
2898 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2899 * @dapm: DAPM context
2902 * Enables input/output pin regardless of any other state. This is
2903 * intended for use with microphone bias supplies used in microphone
2906 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2907 * do any widget power switching.
2909 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context
*dapm
,
2912 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
2915 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
2919 dev_dbg(w
->dapm
->dev
, "dapm: force enable pin %s\n", pin
);
2922 dapm_mark_dirty(w
, "force enable");
2926 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
2929 * snd_soc_dapm_disable_pin - disable pin.
2930 * @dapm: DAPM context
2933 * Disables input/output pin and its parents or children widgets.
2934 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2935 * do any widget power switching.
2937 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context
*dapm
,
2940 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
2942 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
2945 * snd_soc_dapm_nc_pin - permanently disable pin.
2946 * @dapm: DAPM context
2949 * Marks the specified pin as being not connected, disabling it along
2950 * any parent or child widgets. At present this is identical to
2951 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2952 * additional things such as disabling controls which only affect
2953 * paths through the pin.
2955 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2956 * do any widget power switching.
2958 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
2960 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
2962 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
2965 * snd_soc_dapm_get_pin_status - get audio pin status
2966 * @dapm: DAPM context
2967 * @pin: audio signal pin endpoint (or start point)
2969 * Get audio pin status - connected or disconnected.
2971 * Returns 1 for connected otherwise 0.
2973 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context
*dapm
,
2976 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
2979 return w
->connected
;
2983 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
2986 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2987 * @dapm: DAPM context
2988 * @pin: audio signal pin endpoint (or start point)
2990 * Mark the given endpoint or pin as ignoring suspend. When the
2991 * system is disabled a path between two endpoints flagged as ignoring
2992 * suspend will not be disabled. The path must already be enabled via
2993 * normal means at suspend time, it will not be turned on if it was not
2996 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context
*dapm
,
2999 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, false);
3002 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
3006 w
->ignore_suspend
= 1;
3010 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
3012 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card
*card
,
3013 struct snd_soc_dapm_widget
*w
)
3015 struct snd_soc_dapm_path
*p
;
3017 list_for_each_entry(p
, &card
->paths
, list
) {
3018 if ((p
->source
== w
) || (p
->sink
== w
)) {
3020 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3021 p
->source
->name
, p
->source
->id
, p
->source
->dapm
,
3022 p
->sink
->name
, p
->sink
->id
, p
->sink
->dapm
);
3024 /* Connected to something other than the codec */
3025 if (p
->source
->dapm
!= p
->sink
->dapm
)
3028 * Loopback connection from codec external pin to
3029 * codec external pin
3031 if (p
->sink
->id
== snd_soc_dapm_input
) {
3032 switch (p
->source
->id
) {
3033 case snd_soc_dapm_output
:
3034 case snd_soc_dapm_micbias
:
3047 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3048 * @codec: The codec whose pins should be processed
3050 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3051 * which are unused. Pins are used if they are connected externally to the
3052 * codec, whether that be to some other device, or a loop-back connection to
3055 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec
*codec
)
3057 struct snd_soc_card
*card
= codec
->card
;
3058 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
3059 struct snd_soc_dapm_widget
*w
;
3061 dev_dbg(codec
->dev
, "Auto NC: DAPMs: card:%p codec:%p\n",
3062 &card
->dapm
, &codec
->dapm
);
3064 list_for_each_entry(w
, &card
->widgets
, list
) {
3065 if (w
->dapm
!= dapm
)
3068 case snd_soc_dapm_input
:
3069 case snd_soc_dapm_output
:
3070 case snd_soc_dapm_micbias
:
3071 dev_dbg(codec
->dev
, "Auto NC: Checking widget %s\n",
3073 if (!snd_soc_dapm_widget_in_card_paths(card
, w
)) {
3075 "... Not in map; disabling\n");
3076 snd_soc_dapm_nc_pin(dapm
, w
->name
);
3086 * snd_soc_dapm_free - free dapm resources
3087 * @dapm: DAPM context
3089 * Free all dapm widgets and resources.
3091 void snd_soc_dapm_free(struct snd_soc_dapm_context
*dapm
)
3093 snd_soc_dapm_sys_remove(dapm
->dev
);
3094 dapm_debugfs_cleanup(dapm
);
3095 dapm_free_widgets(dapm
);
3096 list_del(&dapm
->list
);
3098 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
3100 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context
*dapm
)
3102 struct snd_soc_dapm_widget
*w
;
3103 LIST_HEAD(down_list
);
3106 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
3107 if (w
->dapm
!= dapm
)
3110 dapm_seq_insert(w
, &down_list
, false);
3116 /* If there were no widgets to power down we're already in
3120 snd_soc_dapm_set_bias_level(dapm
, SND_SOC_BIAS_PREPARE
);
3121 dapm_seq_run(dapm
, &down_list
, 0, false);
3122 snd_soc_dapm_set_bias_level(dapm
, SND_SOC_BIAS_STANDBY
);
3127 * snd_soc_dapm_shutdown - callback for system shutdown
3129 void snd_soc_dapm_shutdown(struct snd_soc_card
*card
)
3131 struct snd_soc_codec
*codec
;
3133 list_for_each_entry(codec
, &card
->codec_dev_list
, list
) {
3134 soc_dapm_shutdown_codec(&codec
->dapm
);
3135 snd_soc_dapm_set_bias_level(&codec
->dapm
, SND_SOC_BIAS_OFF
);
3139 /* Module information */
3140 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3141 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3142 MODULE_LICENSE("GPL");