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 void dapm_reset(struct snd_soc_card
*card
)
174 struct snd_soc_dapm_widget
*w
;
176 memset(&card
->dapm_stats
, 0, sizeof(card
->dapm_stats
));
178 list_for_each_entry(w
, &card
->widgets
, list
) {
179 w
->power_checked
= false;
185 static int soc_widget_read(struct snd_soc_dapm_widget
*w
, int reg
)
188 return snd_soc_read(w
->codec
, reg
);
189 else if (w
->platform
)
190 return snd_soc_platform_read(w
->platform
, reg
);
192 dev_err(w
->dapm
->dev
, "no valid widget read method\n");
196 static int soc_widget_write(struct snd_soc_dapm_widget
*w
, int reg
, int val
)
199 return snd_soc_write(w
->codec
, reg
, val
);
200 else if (w
->platform
)
201 return snd_soc_platform_write(w
->platform
, reg
, val
);
203 dev_err(w
->dapm
->dev
, "no valid widget write method\n");
207 static int soc_widget_update_bits(struct snd_soc_dapm_widget
*w
,
208 unsigned short reg
, unsigned int mask
, unsigned int value
)
211 unsigned int old
, new;
214 if (w
->codec
&& w
->codec
->using_regmap
) {
215 ret
= regmap_update_bits_check(w
->codec
->control_data
,
216 reg
, mask
, value
, &change
);
220 ret
= soc_widget_read(w
, reg
);
225 new = (old
& ~mask
) | (value
& mask
);
228 ret
= soc_widget_write(w
, reg
, new);
238 * snd_soc_dapm_set_bias_level - set the bias level for the system
239 * @dapm: DAPM context
240 * @level: level to configure
242 * Configure the bias (power) levels for the SoC audio device.
244 * Returns 0 for success else error.
246 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context
*dapm
,
247 enum snd_soc_bias_level level
)
249 struct snd_soc_card
*card
= dapm
->card
;
252 trace_snd_soc_bias_level_start(card
, level
);
254 if (card
&& card
->set_bias_level
)
255 ret
= card
->set_bias_level(card
, dapm
, level
);
260 if (dapm
->codec
->driver
->set_bias_level
)
261 ret
= dapm
->codec
->driver
->set_bias_level(dapm
->codec
,
264 dapm
->bias_level
= level
;
269 if (card
&& card
->set_bias_level_post
)
270 ret
= card
->set_bias_level_post(card
, dapm
, level
);
272 trace_snd_soc_bias_level_done(card
, level
);
277 /* set up initial codec paths */
278 static void dapm_set_path_status(struct snd_soc_dapm_widget
*w
,
279 struct snd_soc_dapm_path
*p
, int i
)
282 case snd_soc_dapm_switch
:
283 case snd_soc_dapm_mixer
:
284 case snd_soc_dapm_mixer_named_ctl
: {
286 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
287 w
->kcontrol_news
[i
].private_value
;
288 unsigned int reg
= mc
->reg
;
289 unsigned int shift
= mc
->shift
;
291 unsigned int mask
= (1 << fls(max
)) - 1;
292 unsigned int invert
= mc
->invert
;
294 val
= soc_widget_read(w
, reg
);
295 val
= (val
>> shift
) & mask
;
297 if ((invert
&& !val
) || (!invert
&& val
))
303 case snd_soc_dapm_mux
: {
304 struct soc_enum
*e
= (struct soc_enum
*)
305 w
->kcontrol_news
[i
].private_value
;
306 int val
, item
, bitmask
;
308 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
310 val
= soc_widget_read(w
, e
->reg
);
311 item
= (val
>> e
->shift_l
) & (bitmask
- 1);
314 for (i
= 0; i
< e
->max
; i
++) {
315 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
320 case snd_soc_dapm_virt_mux
: {
321 struct soc_enum
*e
= (struct soc_enum
*)
322 w
->kcontrol_news
[i
].private_value
;
325 /* since a virtual mux has no backing registers to
326 * decide which path to connect, it will try to match
327 * with the first enumeration. This is to ensure
328 * that the default mux choice (the first) will be
329 * correctly powered up during initialization.
331 if (!strcmp(p
->name
, e
->texts
[0]))
335 case snd_soc_dapm_value_mux
: {
336 struct soc_enum
*e
= (struct soc_enum
*)
337 w
->kcontrol_news
[i
].private_value
;
340 val
= soc_widget_read(w
, e
->reg
);
341 val
= (val
>> e
->shift_l
) & e
->mask
;
342 for (item
= 0; item
< e
->max
; item
++) {
343 if (val
== e
->values
[item
])
348 for (i
= 0; i
< e
->max
; i
++) {
349 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
354 /* does not affect routing - always connected */
355 case snd_soc_dapm_pga
:
356 case snd_soc_dapm_out_drv
:
357 case snd_soc_dapm_output
:
358 case snd_soc_dapm_adc
:
359 case snd_soc_dapm_input
:
360 case snd_soc_dapm_siggen
:
361 case snd_soc_dapm_dac
:
362 case snd_soc_dapm_micbias
:
363 case snd_soc_dapm_vmid
:
364 case snd_soc_dapm_supply
:
365 case snd_soc_dapm_regulator_supply
:
366 case snd_soc_dapm_aif_in
:
367 case snd_soc_dapm_aif_out
:
368 case snd_soc_dapm_hp
:
369 case snd_soc_dapm_mic
:
370 case snd_soc_dapm_spk
:
371 case snd_soc_dapm_line
:
374 /* does affect routing - dynamically connected */
375 case snd_soc_dapm_pre
:
376 case snd_soc_dapm_post
:
382 /* connect mux widget to its interconnecting audio paths */
383 static int dapm_connect_mux(struct snd_soc_dapm_context
*dapm
,
384 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
385 struct snd_soc_dapm_path
*path
, const char *control_name
,
386 const struct snd_kcontrol_new
*kcontrol
)
388 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
391 for (i
= 0; i
< e
->max
; i
++) {
392 if (!(strcmp(control_name
, e
->texts
[i
]))) {
393 list_add(&path
->list
, &dapm
->card
->paths
);
394 list_add(&path
->list_sink
, &dest
->sources
);
395 list_add(&path
->list_source
, &src
->sinks
);
396 path
->name
= (char*)e
->texts
[i
];
397 dapm_set_path_status(dest
, path
, 0);
405 /* connect mixer widget to its interconnecting audio paths */
406 static int dapm_connect_mixer(struct snd_soc_dapm_context
*dapm
,
407 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
408 struct snd_soc_dapm_path
*path
, const char *control_name
)
412 /* search for mixer kcontrol */
413 for (i
= 0; i
< dest
->num_kcontrols
; i
++) {
414 if (!strcmp(control_name
, dest
->kcontrol_news
[i
].name
)) {
415 list_add(&path
->list
, &dapm
->card
->paths
);
416 list_add(&path
->list_sink
, &dest
->sources
);
417 list_add(&path
->list_source
, &src
->sinks
);
418 path
->name
= dest
->kcontrol_news
[i
].name
;
419 dapm_set_path_status(dest
, path
, i
);
426 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context
*dapm
,
427 struct snd_soc_dapm_widget
*kcontrolw
,
428 const struct snd_kcontrol_new
*kcontrol_new
,
429 struct snd_kcontrol
**kcontrol
)
431 struct snd_soc_dapm_widget
*w
;
436 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
437 if (w
== kcontrolw
|| w
->dapm
!= kcontrolw
->dapm
)
439 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
440 if (&w
->kcontrol_news
[i
] == kcontrol_new
) {
442 *kcontrol
= w
->kcontrols
[i
];
451 /* create new dapm mixer control */
452 static int dapm_new_mixer(struct snd_soc_dapm_widget
*w
)
454 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
456 size_t name_len
, prefix_len
;
457 struct snd_soc_dapm_path
*path
;
458 struct snd_card
*card
= dapm
->card
->snd_card
;
460 struct snd_soc_dapm_widget_list
*wlist
;
464 prefix
= dapm
->codec
->name_prefix
;
469 prefix_len
= strlen(prefix
) + 1;
474 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
477 list_for_each_entry(path
, &w
->sources
, list_sink
) {
479 /* mixer/mux paths name must match control name */
480 if (path
->name
!= (char *)w
->kcontrol_news
[i
].name
)
483 if (w
->kcontrols
[i
]) {
484 path
->kcontrol
= w
->kcontrols
[i
];
488 wlistsize
= sizeof(struct snd_soc_dapm_widget_list
) +
489 sizeof(struct snd_soc_dapm_widget
*),
490 wlist
= kzalloc(wlistsize
, GFP_KERNEL
);
493 "asoc: can't allocate widget list for %s\n",
497 wlist
->num_widgets
= 1;
498 wlist
->widgets
[0] = w
;
500 /* add dapm control with long name.
501 * for dapm_mixer this is the concatenation of the
502 * mixer and kcontrol name.
503 * for dapm_mixer_named_ctl this is simply the
506 name_len
= strlen(w
->kcontrol_news
[i
].name
) + 1;
507 if (w
->id
!= snd_soc_dapm_mixer_named_ctl
)
508 name_len
+= 1 + strlen(w
->name
);
510 path
->long_name
= kmalloc(name_len
, GFP_KERNEL
);
512 if (path
->long_name
== NULL
) {
519 /* The control will get a prefix from
520 * the control creation process but
521 * we're also using the same prefix
522 * for widgets so cut the prefix off
523 * the front of the widget name.
525 snprintf(path
->long_name
, name_len
, "%s %s",
526 w
->name
+ prefix_len
,
527 w
->kcontrol_news
[i
].name
);
529 case snd_soc_dapm_mixer_named_ctl
:
530 snprintf(path
->long_name
, name_len
, "%s",
531 w
->kcontrol_news
[i
].name
);
535 path
->long_name
[name_len
- 1] = '\0';
537 path
->kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[i
],
538 wlist
, path
->long_name
,
540 ret
= snd_ctl_add(card
, path
->kcontrol
);
543 "asoc: failed to add dapm kcontrol %s: %d\n",
544 path
->long_name
, ret
);
546 kfree(path
->long_name
);
547 path
->long_name
= NULL
;
550 w
->kcontrols
[i
] = path
->kcontrol
;
556 /* create new dapm mux control */
557 static int dapm_new_mux(struct snd_soc_dapm_widget
*w
)
559 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
560 struct snd_soc_dapm_path
*path
= NULL
;
561 struct snd_kcontrol
*kcontrol
;
562 struct snd_card
*card
= dapm
->card
->snd_card
;
566 struct snd_soc_dapm_widget_list
*wlist
;
567 int shared
, wlistentries
;
571 if (w
->num_kcontrols
!= 1) {
573 "asoc: mux %s has incorrect number of controls\n",
578 shared
= dapm_is_shared_kcontrol(dapm
, w
, &w
->kcontrol_news
[0],
581 wlist
= kcontrol
->private_data
;
582 wlistentries
= wlist
->num_widgets
+ 1;
587 wlistsize
= sizeof(struct snd_soc_dapm_widget_list
) +
588 wlistentries
* sizeof(struct snd_soc_dapm_widget
*),
589 wlist
= krealloc(wlist
, wlistsize
, GFP_KERNEL
);
592 "asoc: can't allocate widget list for %s\n", w
->name
);
595 wlist
->num_widgets
= wlistentries
;
596 wlist
->widgets
[wlistentries
- 1] = w
;
600 prefix
= dapm
->codec
->name_prefix
;
605 name
= w
->kcontrol_news
[0].name
;
610 prefix_len
= strlen(prefix
) + 1;
616 * The control will get a prefix from the control creation
617 * process but we're also using the same prefix for widgets so
618 * cut the prefix off the front of the widget name.
620 kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[0], wlist
,
621 name
+ prefix_len
, prefix
);
622 ret
= snd_ctl_add(card
, kcontrol
);
624 dev_err(dapm
->dev
, "failed to add kcontrol %s: %d\n",
631 kcontrol
->private_data
= wlist
;
633 w
->kcontrols
[0] = kcontrol
;
635 list_for_each_entry(path
, &w
->sources
, list_sink
)
636 path
->kcontrol
= kcontrol
;
641 /* create new dapm volume control */
642 static int dapm_new_pga(struct snd_soc_dapm_widget
*w
)
644 if (w
->num_kcontrols
)
645 dev_err(w
->dapm
->dev
,
646 "asoc: PGA controls not supported: '%s'\n", w
->name
);
651 /* reset 'walked' bit for each dapm path */
652 static inline void dapm_clear_walk(struct snd_soc_dapm_context
*dapm
)
654 struct snd_soc_dapm_path
*p
;
656 list_for_each_entry(p
, &dapm
->card
->paths
, list
)
660 /* We implement power down on suspend by checking the power state of
661 * the ALSA card - when we are suspending the ALSA state for the card
664 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
666 int level
= snd_power_get_state(widget
->dapm
->card
->snd_card
);
669 case SNDRV_CTL_POWER_D3hot
:
670 case SNDRV_CTL_POWER_D3cold
:
671 if (widget
->ignore_suspend
)
672 dev_dbg(widget
->dapm
->dev
, "%s ignoring suspend\n",
674 return widget
->ignore_suspend
;
681 * Recursively check for a completed path to an active or physically connected
682 * output widget. Returns number of complete paths.
684 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
)
686 struct snd_soc_dapm_path
*path
;
689 if (widget
->outputs
>= 0)
690 return widget
->outputs
;
692 DAPM_UPDATE_STAT(widget
, path_checks
);
694 switch (widget
->id
) {
695 case snd_soc_dapm_supply
:
696 case snd_soc_dapm_regulator_supply
:
702 switch (widget
->id
) {
703 case snd_soc_dapm_adc
:
704 case snd_soc_dapm_aif_out
:
705 if (widget
->active
) {
706 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
707 return widget
->outputs
;
713 if (widget
->connected
) {
714 /* connected pin ? */
715 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
) {
716 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
717 return widget
->outputs
;
720 /* connected jack or spk ? */
721 if (widget
->id
== snd_soc_dapm_hp
||
722 widget
->id
== snd_soc_dapm_spk
||
723 (widget
->id
== snd_soc_dapm_line
&&
724 !list_empty(&widget
->sources
))) {
725 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
726 return widget
->outputs
;
730 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
731 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
739 if (path
->sink
&& path
->connect
) {
741 con
+= is_connected_output_ep(path
->sink
);
745 widget
->outputs
= con
;
751 * Recursively check for a completed path to an active or physically connected
752 * input widget. Returns number of complete paths.
754 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
)
756 struct snd_soc_dapm_path
*path
;
759 if (widget
->inputs
>= 0)
760 return widget
->inputs
;
762 DAPM_UPDATE_STAT(widget
, path_checks
);
764 switch (widget
->id
) {
765 case snd_soc_dapm_supply
:
766 case snd_soc_dapm_regulator_supply
:
772 /* active stream ? */
773 switch (widget
->id
) {
774 case snd_soc_dapm_dac
:
775 case snd_soc_dapm_aif_in
:
776 if (widget
->active
) {
777 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
778 return widget
->inputs
;
784 if (widget
->connected
) {
785 /* connected pin ? */
786 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
) {
787 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
788 return widget
->inputs
;
791 /* connected VMID/Bias for lower pops */
792 if (widget
->id
== snd_soc_dapm_vmid
) {
793 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
794 return widget
->inputs
;
797 /* connected jack ? */
798 if (widget
->id
== snd_soc_dapm_mic
||
799 (widget
->id
== snd_soc_dapm_line
&&
800 !list_empty(&widget
->sinks
))) {
801 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
802 return widget
->inputs
;
805 /* signal generator */
806 if (widget
->id
== snd_soc_dapm_siggen
) {
807 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
808 return widget
->inputs
;
812 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
813 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
821 if (path
->source
&& path
->connect
) {
823 con
+= is_connected_input_ep(path
->source
);
827 widget
->inputs
= con
;
833 * Handler for generic register modifier widget.
835 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
836 struct snd_kcontrol
*kcontrol
, int event
)
840 if (SND_SOC_DAPM_EVENT_ON(event
))
845 soc_widget_update_bits(w
, -(w
->reg
+ 1),
846 w
->mask
<< w
->shift
, val
<< w
->shift
);
850 EXPORT_SYMBOL_GPL(dapm_reg_event
);
853 * Handler for regulator supply widget.
855 int dapm_regulator_event(struct snd_soc_dapm_widget
*w
,
856 struct snd_kcontrol
*kcontrol
, int event
)
858 if (SND_SOC_DAPM_EVENT_ON(event
))
859 return regulator_enable(w
->priv
);
861 return regulator_disable_deferred(w
->priv
, w
->shift
);
863 EXPORT_SYMBOL_GPL(dapm_regulator_event
);
865 static int dapm_widget_power_check(struct snd_soc_dapm_widget
*w
)
867 if (w
->power_checked
)
873 w
->new_power
= w
->power_check(w
);
875 w
->power_checked
= true;
880 /* Generic check to see if a widget should be powered.
882 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
886 DAPM_UPDATE_STAT(w
, power_checks
);
888 in
= is_connected_input_ep(w
);
889 dapm_clear_walk(w
->dapm
);
890 out
= is_connected_output_ep(w
);
891 dapm_clear_walk(w
->dapm
);
892 return out
!= 0 && in
!= 0;
895 /* Check to see if an ADC has power */
896 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
900 DAPM_UPDATE_STAT(w
, power_checks
);
903 in
= is_connected_input_ep(w
);
904 dapm_clear_walk(w
->dapm
);
907 return dapm_generic_check_power(w
);
911 /* Check to see if a DAC has power */
912 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
916 DAPM_UPDATE_STAT(w
, power_checks
);
919 out
= is_connected_output_ep(w
);
920 dapm_clear_walk(w
->dapm
);
923 return dapm_generic_check_power(w
);
927 /* Check to see if a power supply is needed */
928 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
930 struct snd_soc_dapm_path
*path
;
932 DAPM_UPDATE_STAT(w
, power_checks
);
934 /* Check if one of our outputs is connected */
935 list_for_each_entry(path
, &w
->sinks
, list_source
) {
936 DAPM_UPDATE_STAT(w
, neighbour_checks
);
941 if (path
->connected
&&
942 !path
->connected(path
->source
, path
->sink
))
948 if (dapm_widget_power_check(path
->sink
))
952 dapm_clear_walk(w
->dapm
);
957 static int dapm_always_on_check_power(struct snd_soc_dapm_widget
*w
)
962 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
963 struct snd_soc_dapm_widget
*b
,
971 sort
= dapm_down_seq
;
973 if (sort
[a
->id
] != sort
[b
->id
])
974 return sort
[a
->id
] - sort
[b
->id
];
975 if (a
->subseq
!= b
->subseq
) {
977 return a
->subseq
- b
->subseq
;
979 return b
->subseq
- a
->subseq
;
981 if (a
->reg
!= b
->reg
)
982 return a
->reg
- b
->reg
;
983 if (a
->dapm
!= b
->dapm
)
984 return (unsigned long)a
->dapm
- (unsigned long)b
->dapm
;
989 /* Insert a widget in order into a DAPM power sequence. */
990 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
991 struct list_head
*list
,
994 struct snd_soc_dapm_widget
*w
;
996 list_for_each_entry(w
, list
, power_list
)
997 if (dapm_seq_compare(new_widget
, w
, power_up
) < 0) {
998 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
1002 list_add_tail(&new_widget
->power_list
, list
);
1005 static void dapm_seq_check_event(struct snd_soc_dapm_context
*dapm
,
1006 struct snd_soc_dapm_widget
*w
, int event
)
1008 struct snd_soc_card
*card
= dapm
->card
;
1009 const char *ev_name
;
1013 case SND_SOC_DAPM_PRE_PMU
:
1014 ev_name
= "PRE_PMU";
1017 case SND_SOC_DAPM_POST_PMU
:
1018 ev_name
= "POST_PMU";
1021 case SND_SOC_DAPM_PRE_PMD
:
1022 ev_name
= "PRE_PMD";
1025 case SND_SOC_DAPM_POST_PMD
:
1026 ev_name
= "POST_PMD";
1034 if (w
->power
!= power
)
1037 if (w
->event
&& (w
->event_flags
& event
)) {
1038 pop_dbg(dapm
->dev
, card
->pop_time
, "pop test : %s %s\n",
1040 trace_snd_soc_dapm_widget_event_start(w
, event
);
1041 ret
= w
->event(w
, NULL
, event
);
1042 trace_snd_soc_dapm_widget_event_done(w
, event
);
1044 pr_err("%s: %s event failed: %d\n",
1045 ev_name
, w
->name
, ret
);
1049 /* Apply the coalesced changes from a DAPM sequence */
1050 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context
*dapm
,
1051 struct list_head
*pending
)
1053 struct snd_soc_card
*card
= dapm
->card
;
1054 struct snd_soc_dapm_widget
*w
;
1056 unsigned int value
= 0;
1057 unsigned int mask
= 0;
1058 unsigned int cur_mask
;
1060 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
1063 list_for_each_entry(w
, pending
, power_list
) {
1064 cur_mask
= 1 << w
->shift
;
1065 BUG_ON(reg
!= w
->reg
);
1076 pop_dbg(dapm
->dev
, card
->pop_time
,
1077 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1078 w
->name
, reg
, value
, mask
);
1080 /* Check for events */
1081 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_PRE_PMU
);
1082 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_PRE_PMD
);
1086 /* Any widget will do, they should all be updating the
1089 w
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
1092 pop_dbg(dapm
->dev
, card
->pop_time
,
1093 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1094 value
, mask
, reg
, card
->pop_time
);
1095 pop_wait(card
->pop_time
);
1096 soc_widget_update_bits(w
, reg
, mask
, value
);
1099 list_for_each_entry(w
, pending
, power_list
) {
1100 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_POST_PMU
);
1101 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_POST_PMD
);
1105 /* Apply a DAPM power sequence.
1107 * We walk over a pre-sorted list of widgets to apply power to. In
1108 * order to minimise the number of writes to the device required
1109 * multiple widgets will be updated in a single write where possible.
1110 * Currently anything that requires more than a single write is not
1113 static void dapm_seq_run(struct snd_soc_dapm_context
*dapm
,
1114 struct list_head
*list
, int event
, bool power_up
)
1116 struct snd_soc_dapm_widget
*w
, *n
;
1119 int cur_subseq
= -1;
1120 int cur_reg
= SND_SOC_NOPM
;
1121 struct snd_soc_dapm_context
*cur_dapm
= NULL
;
1128 sort
= dapm_down_seq
;
1130 list_for_each_entry_safe(w
, n
, list
, power_list
) {
1133 /* Do we need to apply any queued changes? */
1134 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
||
1135 w
->dapm
!= cur_dapm
|| w
->subseq
!= cur_subseq
) {
1136 if (!list_empty(&pending
))
1137 dapm_seq_run_coalesced(cur_dapm
, &pending
);
1139 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1140 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1141 if (sort
[i
] == cur_sort
)
1142 cur_dapm
->seq_notifier(cur_dapm
,
1147 INIT_LIST_HEAD(&pending
);
1149 cur_subseq
= INT_MIN
;
1150 cur_reg
= SND_SOC_NOPM
;
1155 case snd_soc_dapm_pre
:
1157 list_for_each_entry_safe_continue(w
, n
, list
,
1160 if (event
== SND_SOC_DAPM_STREAM_START
)
1162 NULL
, SND_SOC_DAPM_PRE_PMU
);
1163 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1165 NULL
, SND_SOC_DAPM_PRE_PMD
);
1168 case snd_soc_dapm_post
:
1170 list_for_each_entry_safe_continue(w
, n
, list
,
1173 if (event
== SND_SOC_DAPM_STREAM_START
)
1175 NULL
, SND_SOC_DAPM_POST_PMU
);
1176 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1178 NULL
, SND_SOC_DAPM_POST_PMD
);
1182 /* Queue it up for application */
1183 cur_sort
= sort
[w
->id
];
1184 cur_subseq
= w
->subseq
;
1187 list_move(&w
->power_list
, &pending
);
1192 dev_err(w
->dapm
->dev
,
1193 "Failed to apply widget power: %d\n", ret
);
1196 if (!list_empty(&pending
))
1197 dapm_seq_run_coalesced(cur_dapm
, &pending
);
1199 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1200 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1201 if (sort
[i
] == cur_sort
)
1202 cur_dapm
->seq_notifier(cur_dapm
,
1207 static void dapm_widget_update(struct snd_soc_dapm_context
*dapm
)
1209 struct snd_soc_dapm_update
*update
= dapm
->update
;
1210 struct snd_soc_dapm_widget
*w
;
1219 (w
->event_flags
& SND_SOC_DAPM_PRE_REG
)) {
1220 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_PRE_REG
);
1222 pr_err("%s DAPM pre-event failed: %d\n",
1226 ret
= snd_soc_update_bits(w
->codec
, update
->reg
, update
->mask
,
1229 pr_err("%s DAPM update failed: %d\n", w
->name
, ret
);
1232 (w
->event_flags
& SND_SOC_DAPM_POST_REG
)) {
1233 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_POST_REG
);
1235 pr_err("%s DAPM post-event failed: %d\n",
1240 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1241 * they're changing state.
1243 static void dapm_pre_sequence_async(void *data
, async_cookie_t cookie
)
1245 struct snd_soc_dapm_context
*d
= data
;
1248 /* If we're off and we're not supposed to be go into STANDBY */
1249 if (d
->bias_level
== SND_SOC_BIAS_OFF
&&
1250 d
->target_bias_level
!= SND_SOC_BIAS_OFF
) {
1252 pm_runtime_get_sync(d
->dev
);
1254 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1257 "Failed to turn on bias: %d\n", ret
);
1260 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1261 if (d
->bias_level
!= d
->target_bias_level
) {
1262 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_PREPARE
);
1265 "Failed to prepare bias: %d\n", ret
);
1269 /* Async callback run prior to DAPM sequences - brings to their final
1272 static void dapm_post_sequence_async(void *data
, async_cookie_t cookie
)
1274 struct snd_soc_dapm_context
*d
= data
;
1277 /* If we just powered the last thing off drop to standby bias */
1278 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1279 (d
->target_bias_level
== SND_SOC_BIAS_STANDBY
||
1280 d
->target_bias_level
== SND_SOC_BIAS_OFF
)) {
1281 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1283 dev_err(d
->dev
, "Failed to apply standby bias: %d\n",
1287 /* If we're in standby and can support bias off then do that */
1288 if (d
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1289 d
->target_bias_level
== SND_SOC_BIAS_OFF
) {
1290 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_OFF
);
1292 dev_err(d
->dev
, "Failed to turn off bias: %d\n", ret
);
1295 pm_runtime_put(d
->dev
);
1298 /* If we just powered up then move to active bias */
1299 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1300 d
->target_bias_level
== SND_SOC_BIAS_ON
) {
1301 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_ON
);
1303 dev_err(d
->dev
, "Failed to apply active bias: %d\n",
1308 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget
*peer
,
1309 bool power
, bool connect
)
1311 /* If a connection is being made or broken then that update
1312 * will have marked the peer dirty, otherwise the widgets are
1313 * not connected and this update has no impact. */
1317 /* If the peer is already in the state we're moving to then we
1318 * won't have an impact on it. */
1319 if (power
!= peer
->power
)
1320 dapm_mark_dirty(peer
, "peer state change");
1323 static void dapm_widget_set_power(struct snd_soc_dapm_widget
*w
, bool power
,
1324 struct list_head
*up_list
,
1325 struct list_head
*down_list
)
1327 struct snd_soc_dapm_path
*path
;
1329 if (w
->power
== power
)
1332 trace_snd_soc_dapm_widget_power(w
, power
);
1334 /* If we changed our power state perhaps our neigbours changed
1337 list_for_each_entry(path
, &w
->sources
, list_sink
) {
1339 dapm_widget_set_peer_power(path
->source
, power
,
1344 case snd_soc_dapm_supply
:
1345 case snd_soc_dapm_regulator_supply
:
1346 /* Supplies can't affect their outputs, only their inputs */
1349 list_for_each_entry(path
, &w
->sinks
, list_source
) {
1351 dapm_widget_set_peer_power(path
->sink
, power
,
1359 dapm_seq_insert(w
, up_list
, true);
1361 dapm_seq_insert(w
, down_list
, false);
1366 static void dapm_power_one_widget(struct snd_soc_dapm_widget
*w
,
1367 struct list_head
*up_list
,
1368 struct list_head
*down_list
)
1373 case snd_soc_dapm_pre
:
1374 dapm_seq_insert(w
, down_list
, false);
1376 case snd_soc_dapm_post
:
1377 dapm_seq_insert(w
, up_list
, true);
1381 power
= dapm_widget_power_check(w
);
1383 dapm_widget_set_power(w
, power
, up_list
, down_list
);
1389 * Scan each dapm widget for complete audio path.
1390 * A complete path is a route that has valid endpoints i.e.:-
1392 * o DAC to output pin.
1393 * o Input Pin to ADC.
1394 * o Input pin to Output pin (bypass, sidetone)
1395 * o DAC to ADC (loopback).
1397 static int dapm_power_widgets(struct snd_soc_dapm_context
*dapm
, int event
)
1399 struct snd_soc_card
*card
= dapm
->card
;
1400 struct snd_soc_dapm_widget
*w
;
1401 struct snd_soc_dapm_context
*d
;
1403 LIST_HEAD(down_list
);
1404 LIST_HEAD(async_domain
);
1405 enum snd_soc_bias_level bias
;
1407 trace_snd_soc_dapm_start(card
);
1409 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1410 if (d
->n_widgets
|| d
->codec
== NULL
) {
1411 if (d
->idle_bias_off
)
1412 d
->target_bias_level
= SND_SOC_BIAS_OFF
;
1414 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1420 /* Check which widgets we need to power and store them in
1421 * lists indicating if they should be powered up or down. We
1422 * only check widgets that have been flagged as dirty but note
1423 * that new widgets may be added to the dirty list while we
1426 list_for_each_entry(w
, &card
->dapm_dirty
, dirty
) {
1427 dapm_power_one_widget(w
, &up_list
, &down_list
);
1430 list_for_each_entry(w
, &card
->widgets
, list
) {
1431 list_del_init(&w
->dirty
);
1436 /* Supplies and micbiases only bring the
1437 * context up to STANDBY as unless something
1438 * else is active and passing audio they
1439 * generally don't require full power. Signal
1440 * generators are virtual pins and have no
1441 * power impact themselves.
1444 case snd_soc_dapm_siggen
:
1446 case snd_soc_dapm_supply
:
1447 case snd_soc_dapm_regulator_supply
:
1448 case snd_soc_dapm_micbias
:
1449 if (d
->target_bias_level
< SND_SOC_BIAS_STANDBY
)
1450 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1453 d
->target_bias_level
= SND_SOC_BIAS_ON
;
1460 /* If there are no DAPM widgets then try to figure out power from the
1463 if (!dapm
->n_widgets
) {
1465 case SND_SOC_DAPM_STREAM_START
:
1466 case SND_SOC_DAPM_STREAM_RESUME
:
1467 dapm
->target_bias_level
= SND_SOC_BIAS_ON
;
1469 case SND_SOC_DAPM_STREAM_STOP
:
1470 if (dapm
->codec
&& dapm
->codec
->active
)
1471 dapm
->target_bias_level
= SND_SOC_BIAS_ON
;
1473 dapm
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1475 case SND_SOC_DAPM_STREAM_SUSPEND
:
1476 dapm
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1478 case SND_SOC_DAPM_STREAM_NOP
:
1479 dapm
->target_bias_level
= dapm
->bias_level
;
1486 /* Force all contexts in the card to the same bias state if
1487 * they're not ground referenced.
1489 bias
= SND_SOC_BIAS_OFF
;
1490 list_for_each_entry(d
, &card
->dapm_list
, list
)
1491 if (d
->target_bias_level
> bias
)
1492 bias
= d
->target_bias_level
;
1493 list_for_each_entry(d
, &card
->dapm_list
, list
)
1494 if (!d
->idle_bias_off
)
1495 d
->target_bias_level
= bias
;
1497 trace_snd_soc_dapm_walk_done(card
);
1499 /* Run all the bias changes in parallel */
1500 list_for_each_entry(d
, &dapm
->card
->dapm_list
, list
)
1501 async_schedule_domain(dapm_pre_sequence_async
, d
,
1503 async_synchronize_full_domain(&async_domain
);
1505 /* Power down widgets first; try to avoid amplifying pops. */
1506 dapm_seq_run(dapm
, &down_list
, event
, false);
1508 dapm_widget_update(dapm
);
1511 dapm_seq_run(dapm
, &up_list
, event
, true);
1513 /* Run all the bias changes in parallel */
1514 list_for_each_entry(d
, &dapm
->card
->dapm_list
, list
)
1515 async_schedule_domain(dapm_post_sequence_async
, d
,
1517 async_synchronize_full_domain(&async_domain
);
1519 pop_dbg(dapm
->dev
, card
->pop_time
,
1520 "DAPM sequencing finished, waiting %dms\n", card
->pop_time
);
1521 pop_wait(card
->pop_time
);
1523 trace_snd_soc_dapm_done(card
);
1528 #ifdef CONFIG_DEBUG_FS
1529 static int dapm_widget_power_open_file(struct inode
*inode
, struct file
*file
)
1531 file
->private_data
= inode
->i_private
;
1535 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1536 char __user
*user_buf
,
1537 size_t count
, loff_t
*ppos
)
1539 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1543 struct snd_soc_dapm_path
*p
= NULL
;
1545 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1549 in
= is_connected_input_ep(w
);
1550 dapm_clear_walk(w
->dapm
);
1551 out
= is_connected_output_ep(w
);
1552 dapm_clear_walk(w
->dapm
);
1554 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s in %d out %d",
1555 w
->name
, w
->power
? "On" : "Off", in
, out
);
1558 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1559 " - R%d(0x%x) bit %d",
1560 w
->reg
, w
->reg
, w
->shift
);
1562 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
1565 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1567 w
->active
? "active" : "inactive");
1569 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1570 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1574 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1575 " in \"%s\" \"%s\"\n",
1576 p
->name
? p
->name
: "static",
1579 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1580 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1584 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1585 " out \"%s\" \"%s\"\n",
1586 p
->name
? p
->name
: "static",
1590 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1596 static const struct file_operations dapm_widget_power_fops
= {
1597 .open
= dapm_widget_power_open_file
,
1598 .read
= dapm_widget_power_read_file
,
1599 .llseek
= default_llseek
,
1602 static int dapm_bias_open_file(struct inode
*inode
, struct file
*file
)
1604 file
->private_data
= inode
->i_private
;
1608 static ssize_t
dapm_bias_read_file(struct file
*file
, char __user
*user_buf
,
1609 size_t count
, loff_t
*ppos
)
1611 struct snd_soc_dapm_context
*dapm
= file
->private_data
;
1614 switch (dapm
->bias_level
) {
1615 case SND_SOC_BIAS_ON
:
1618 case SND_SOC_BIAS_PREPARE
:
1619 level
= "Prepare\n";
1621 case SND_SOC_BIAS_STANDBY
:
1622 level
= "Standby\n";
1624 case SND_SOC_BIAS_OFF
:
1629 level
= "Unknown\n";
1633 return simple_read_from_buffer(user_buf
, count
, ppos
, level
,
1637 static const struct file_operations dapm_bias_fops
= {
1638 .open
= dapm_bias_open_file
,
1639 .read
= dapm_bias_read_file
,
1640 .llseek
= default_llseek
,
1643 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
1644 struct dentry
*parent
)
1648 dapm
->debugfs_dapm
= debugfs_create_dir("dapm", parent
);
1650 if (!dapm
->debugfs_dapm
) {
1652 "Failed to create DAPM debugfs directory\n");
1656 d
= debugfs_create_file("bias_level", 0444,
1657 dapm
->debugfs_dapm
, dapm
,
1661 "ASoC: Failed to create bias level debugfs file\n");
1664 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
1666 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
1669 if (!dapm
->debugfs_dapm
|| !w
->name
)
1672 d
= debugfs_create_file(w
->name
, 0444,
1673 dapm
->debugfs_dapm
, w
,
1674 &dapm_widget_power_fops
);
1676 dev_warn(w
->dapm
->dev
,
1677 "ASoC: Failed to create %s debugfs file\n",
1681 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
1683 debugfs_remove_recursive(dapm
->debugfs_dapm
);
1687 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
1688 struct dentry
*parent
)
1692 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
1696 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
1702 /* test and update the power status of a mux widget */
1703 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget
*widget
,
1704 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
)
1706 struct snd_soc_dapm_path
*path
;
1709 if (widget
->id
!= snd_soc_dapm_mux
&&
1710 widget
->id
!= snd_soc_dapm_virt_mux
&&
1711 widget
->id
!= snd_soc_dapm_value_mux
)
1714 /* find dapm widget path assoc with kcontrol */
1715 list_for_each_entry(path
, &widget
->dapm
->card
->paths
, list
) {
1716 if (path
->kcontrol
!= kcontrol
)
1719 if (!path
->name
|| !e
->texts
[mux
])
1723 /* we now need to match the string in the enum to the path */
1724 if (!(strcmp(path
->name
, e
->texts
[mux
]))) {
1725 path
->connect
= 1; /* new connection */
1726 dapm_mark_dirty(path
->source
, "mux connection");
1729 dapm_mark_dirty(path
->source
,
1730 "mux disconnection");
1731 path
->connect
= 0; /* old connection must be powered down */
1736 dapm_mark_dirty(widget
, "mux change");
1737 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1742 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power
);
1744 /* test and update the power status of a mixer or switch widget */
1745 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget
*widget
,
1746 struct snd_kcontrol
*kcontrol
, int connect
)
1748 struct snd_soc_dapm_path
*path
;
1751 if (widget
->id
!= snd_soc_dapm_mixer
&&
1752 widget
->id
!= snd_soc_dapm_mixer_named_ctl
&&
1753 widget
->id
!= snd_soc_dapm_switch
)
1756 /* find dapm widget path assoc with kcontrol */
1757 list_for_each_entry(path
, &widget
->dapm
->card
->paths
, list
) {
1758 if (path
->kcontrol
!= kcontrol
)
1761 /* found, now check type */
1763 path
->connect
= connect
;
1764 dapm_mark_dirty(path
->source
, "mixer connection");
1768 dapm_mark_dirty(widget
, "mixer update");
1769 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1774 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power
);
1776 /* show dapm widget status in sys fs */
1777 static ssize_t
dapm_widget_show(struct device
*dev
,
1778 struct device_attribute
*attr
, char *buf
)
1780 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
1781 struct snd_soc_codec
*codec
=rtd
->codec
;
1782 struct snd_soc_dapm_widget
*w
;
1784 char *state
= "not set";
1786 list_for_each_entry(w
, &codec
->card
->widgets
, list
) {
1787 if (w
->dapm
!= &codec
->dapm
)
1790 /* only display widgets that burnm power */
1792 case snd_soc_dapm_hp
:
1793 case snd_soc_dapm_mic
:
1794 case snd_soc_dapm_spk
:
1795 case snd_soc_dapm_line
:
1796 case snd_soc_dapm_micbias
:
1797 case snd_soc_dapm_dac
:
1798 case snd_soc_dapm_adc
:
1799 case snd_soc_dapm_pga
:
1800 case snd_soc_dapm_out_drv
:
1801 case snd_soc_dapm_mixer
:
1802 case snd_soc_dapm_mixer_named_ctl
:
1803 case snd_soc_dapm_supply
:
1804 case snd_soc_dapm_regulator_supply
:
1806 count
+= sprintf(buf
+ count
, "%s: %s\n",
1807 w
->name
, w
->power
? "On":"Off");
1814 switch (codec
->dapm
.bias_level
) {
1815 case SND_SOC_BIAS_ON
:
1818 case SND_SOC_BIAS_PREPARE
:
1821 case SND_SOC_BIAS_STANDBY
:
1824 case SND_SOC_BIAS_OFF
:
1828 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
1833 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
1835 int snd_soc_dapm_sys_add(struct device
*dev
)
1837 return device_create_file(dev
, &dev_attr_dapm_widget
);
1840 static void snd_soc_dapm_sys_remove(struct device
*dev
)
1842 device_remove_file(dev
, &dev_attr_dapm_widget
);
1845 /* free all dapm widgets and resources */
1846 static void dapm_free_widgets(struct snd_soc_dapm_context
*dapm
)
1848 struct snd_soc_dapm_widget
*w
, *next_w
;
1849 struct snd_soc_dapm_path
*p
, *next_p
;
1851 list_for_each_entry_safe(w
, next_w
, &dapm
->card
->widgets
, list
) {
1852 if (w
->dapm
!= dapm
)
1856 * remove source and sink paths associated to this widget.
1857 * While removing the path, remove reference to it from both
1858 * source and sink widgets so that path is removed only once.
1860 list_for_each_entry_safe(p
, next_p
, &w
->sources
, list_sink
) {
1861 list_del(&p
->list_sink
);
1862 list_del(&p
->list_source
);
1864 kfree(p
->long_name
);
1867 list_for_each_entry_safe(p
, next_p
, &w
->sinks
, list_source
) {
1868 list_del(&p
->list_sink
);
1869 list_del(&p
->list_source
);
1871 kfree(p
->long_name
);
1874 kfree(w
->kcontrols
);
1880 static struct snd_soc_dapm_widget
*dapm_find_widget(
1881 struct snd_soc_dapm_context
*dapm
, const char *pin
,
1882 bool search_other_contexts
)
1884 struct snd_soc_dapm_widget
*w
;
1885 struct snd_soc_dapm_widget
*fallback
= NULL
;
1887 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
1888 if (!strcmp(w
->name
, pin
)) {
1889 if (w
->dapm
== dapm
)
1896 if (search_other_contexts
)
1902 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context
*dapm
,
1903 const char *pin
, int status
)
1905 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
1908 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
1912 w
->connected
= status
;
1915 dapm_mark_dirty(w
, "pin configuration");
1921 * snd_soc_dapm_sync - scan and power dapm paths
1922 * @dapm: DAPM context
1924 * Walks all dapm audio paths and powers widgets according to their
1925 * stream or path usage.
1927 * Returns 0 for success.
1929 int snd_soc_dapm_sync(struct snd_soc_dapm_context
*dapm
)
1932 * Suppress early reports (eg, jacks syncing their state) to avoid
1933 * silly DAPM runs during card startup.
1935 if (!dapm
->card
|| !dapm
->card
->instantiated
)
1938 return dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
1940 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
1942 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context
*dapm
,
1943 const struct snd_soc_dapm_route
*route
)
1945 struct snd_soc_dapm_path
*path
;
1946 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
1947 struct snd_soc_dapm_widget
*wtsource
= NULL
, *wtsink
= NULL
;
1949 const char *control
= route
->control
;
1951 char prefixed_sink
[80];
1952 char prefixed_source
[80];
1955 if (dapm
->codec
&& dapm
->codec
->name_prefix
) {
1956 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
1957 dapm
->codec
->name_prefix
, route
->sink
);
1958 sink
= prefixed_sink
;
1959 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
1960 dapm
->codec
->name_prefix
, route
->source
);
1961 source
= prefixed_source
;
1964 source
= route
->source
;
1968 * find src and dest widgets over all widgets but favor a widget from
1969 * current DAPM context
1971 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
1972 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
1974 if (w
->dapm
== dapm
)
1978 if (!wsource
&& !(strcmp(w
->name
, source
))) {
1980 if (w
->dapm
== dapm
)
1984 /* use widget from another DAPM context if not found from this */
1990 if (wsource
== NULL
|| wsink
== NULL
)
1993 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
1997 path
->source
= wsource
;
1999 path
->connected
= route
->connected
;
2000 INIT_LIST_HEAD(&path
->list
);
2001 INIT_LIST_HEAD(&path
->list_source
);
2002 INIT_LIST_HEAD(&path
->list_sink
);
2004 /* check for external widgets */
2005 if (wsink
->id
== snd_soc_dapm_input
) {
2006 if (wsource
->id
== snd_soc_dapm_micbias
||
2007 wsource
->id
== snd_soc_dapm_mic
||
2008 wsource
->id
== snd_soc_dapm_line
||
2009 wsource
->id
== snd_soc_dapm_output
)
2012 if (wsource
->id
== snd_soc_dapm_output
) {
2013 if (wsink
->id
== snd_soc_dapm_spk
||
2014 wsink
->id
== snd_soc_dapm_hp
||
2015 wsink
->id
== snd_soc_dapm_line
||
2016 wsink
->id
== snd_soc_dapm_input
)
2020 /* connect static paths */
2021 if (control
== NULL
) {
2022 list_add(&path
->list
, &dapm
->card
->paths
);
2023 list_add(&path
->list_sink
, &wsink
->sources
);
2024 list_add(&path
->list_source
, &wsource
->sinks
);
2029 /* connect dynamic paths */
2030 switch (wsink
->id
) {
2031 case snd_soc_dapm_adc
:
2032 case snd_soc_dapm_dac
:
2033 case snd_soc_dapm_pga
:
2034 case snd_soc_dapm_out_drv
:
2035 case snd_soc_dapm_input
:
2036 case snd_soc_dapm_output
:
2037 case snd_soc_dapm_siggen
:
2038 case snd_soc_dapm_micbias
:
2039 case snd_soc_dapm_vmid
:
2040 case snd_soc_dapm_pre
:
2041 case snd_soc_dapm_post
:
2042 case snd_soc_dapm_supply
:
2043 case snd_soc_dapm_regulator_supply
:
2044 case snd_soc_dapm_aif_in
:
2045 case snd_soc_dapm_aif_out
:
2046 list_add(&path
->list
, &dapm
->card
->paths
);
2047 list_add(&path
->list_sink
, &wsink
->sources
);
2048 list_add(&path
->list_source
, &wsource
->sinks
);
2051 case snd_soc_dapm_mux
:
2052 case snd_soc_dapm_virt_mux
:
2053 case snd_soc_dapm_value_mux
:
2054 ret
= dapm_connect_mux(dapm
, wsource
, wsink
, path
, control
,
2055 &wsink
->kcontrol_news
[0]);
2059 case snd_soc_dapm_switch
:
2060 case snd_soc_dapm_mixer
:
2061 case snd_soc_dapm_mixer_named_ctl
:
2062 ret
= dapm_connect_mixer(dapm
, wsource
, wsink
, path
, control
);
2066 case snd_soc_dapm_hp
:
2067 case snd_soc_dapm_mic
:
2068 case snd_soc_dapm_line
:
2069 case snd_soc_dapm_spk
:
2070 list_add(&path
->list
, &dapm
->card
->paths
);
2071 list_add(&path
->list_sink
, &wsink
->sources
);
2072 list_add(&path
->list_source
, &wsource
->sinks
);
2079 dev_warn(dapm
->dev
, "asoc: no dapm match for %s --> %s --> %s\n",
2080 source
, control
, sink
);
2086 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2087 * @dapm: DAPM context
2088 * @route: audio routes
2089 * @num: number of routes
2091 * Connects 2 dapm widgets together via a named audio path. The sink is
2092 * the widget receiving the audio signal, whilst the source is the sender
2093 * of the audio signal.
2095 * Returns 0 for success else error. On error all resources can be freed
2096 * with a call to snd_soc_card_free().
2098 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context
*dapm
,
2099 const struct snd_soc_dapm_route
*route
, int num
)
2103 for (i
= 0; i
< num
; i
++) {
2104 ret
= snd_soc_dapm_add_route(dapm
, route
);
2106 dev_err(dapm
->dev
, "Failed to add route %s->%s\n",
2107 route
->source
, route
->sink
);
2115 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
2117 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context
*dapm
,
2118 const struct snd_soc_dapm_route
*route
)
2120 struct snd_soc_dapm_widget
*source
= dapm_find_widget(dapm
,
2123 struct snd_soc_dapm_widget
*sink
= dapm_find_widget(dapm
,
2126 struct snd_soc_dapm_path
*path
;
2130 dev_err(dapm
->dev
, "Unable to find source %s for weak route\n",
2136 dev_err(dapm
->dev
, "Unable to find sink %s for weak route\n",
2141 if (route
->control
|| route
->connected
)
2142 dev_warn(dapm
->dev
, "Ignoring control for weak route %s->%s\n",
2143 route
->source
, route
->sink
);
2145 list_for_each_entry(path
, &source
->sinks
, list_source
) {
2146 if (path
->sink
== sink
) {
2153 dev_err(dapm
->dev
, "No path found for weak route %s->%s\n",
2154 route
->source
, route
->sink
);
2156 dev_warn(dapm
->dev
, "%d paths found for weak route %s->%s\n",
2157 count
, route
->source
, route
->sink
);
2163 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2164 * @dapm: DAPM context
2165 * @route: audio routes
2166 * @num: number of routes
2168 * Mark existing routes matching those specified in the passed array
2169 * as being weak, meaning that they are ignored for the purpose of
2170 * power decisions. The main intended use case is for sidetone paths
2171 * which couple audio between other independent paths if they are both
2172 * active in order to make the combination work better at the user
2173 * level but which aren't intended to be "used".
2175 * Note that CODEC drivers should not use this as sidetone type paths
2176 * can frequently also be used as bypass paths.
2178 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context
*dapm
,
2179 const struct snd_soc_dapm_route
*route
, int num
)
2184 for (i
= 0; i
< num
; i
++) {
2185 err
= snd_soc_dapm_weak_route(dapm
, route
);
2193 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes
);
2196 * snd_soc_dapm_new_widgets - add new dapm widgets
2197 * @dapm: DAPM context
2199 * Checks the codec for any new dapm widgets and creates them if found.
2201 * Returns 0 for success.
2203 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context
*dapm
)
2205 struct snd_soc_dapm_widget
*w
;
2208 list_for_each_entry(w
, &dapm
->card
->widgets
, list
)
2213 if (w
->num_kcontrols
) {
2214 w
->kcontrols
= kzalloc(w
->num_kcontrols
*
2215 sizeof(struct snd_kcontrol
*),
2222 case snd_soc_dapm_switch
:
2223 case snd_soc_dapm_mixer
:
2224 case snd_soc_dapm_mixer_named_ctl
:
2227 case snd_soc_dapm_mux
:
2228 case snd_soc_dapm_virt_mux
:
2229 case snd_soc_dapm_value_mux
:
2232 case snd_soc_dapm_pga
:
2233 case snd_soc_dapm_out_drv
:
2240 /* Read the initial power state from the device */
2242 val
= soc_widget_read(w
, w
->reg
);
2243 val
&= 1 << w
->shift
;
2253 dapm_mark_dirty(w
, "new widget");
2254 dapm_debugfs_add_widget(w
);
2257 dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
2260 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
2263 * snd_soc_dapm_get_volsw - dapm mixer get callback
2264 * @kcontrol: mixer control
2265 * @ucontrol: control element information
2267 * Callback to get the value of a dapm mixer control.
2269 * Returns 0 for success.
2271 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
2272 struct snd_ctl_elem_value
*ucontrol
)
2274 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2275 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2276 struct soc_mixer_control
*mc
=
2277 (struct soc_mixer_control
*)kcontrol
->private_value
;
2278 unsigned int reg
= mc
->reg
;
2279 unsigned int shift
= mc
->shift
;
2280 unsigned int rshift
= mc
->rshift
;
2282 unsigned int invert
= mc
->invert
;
2283 unsigned int mask
= (1 << fls(max
)) - 1;
2285 ucontrol
->value
.integer
.value
[0] =
2286 (snd_soc_read(widget
->codec
, reg
) >> shift
) & mask
;
2287 if (shift
!= rshift
)
2288 ucontrol
->value
.integer
.value
[1] =
2289 (snd_soc_read(widget
->codec
, reg
) >> rshift
) & mask
;
2291 ucontrol
->value
.integer
.value
[0] =
2292 max
- ucontrol
->value
.integer
.value
[0];
2293 if (shift
!= rshift
)
2294 ucontrol
->value
.integer
.value
[1] =
2295 max
- ucontrol
->value
.integer
.value
[1];
2300 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
2303 * snd_soc_dapm_put_volsw - dapm mixer set callback
2304 * @kcontrol: mixer control
2305 * @ucontrol: control element information
2307 * Callback to set the value of a dapm mixer control.
2309 * Returns 0 for success.
2311 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
2312 struct snd_ctl_elem_value
*ucontrol
)
2314 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2315 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2316 struct snd_soc_codec
*codec
= widget
->codec
;
2317 struct soc_mixer_control
*mc
=
2318 (struct soc_mixer_control
*)kcontrol
->private_value
;
2319 unsigned int reg
= mc
->reg
;
2320 unsigned int shift
= mc
->shift
;
2322 unsigned int mask
= (1 << fls(max
)) - 1;
2323 unsigned int invert
= mc
->invert
;
2325 int connect
, change
;
2326 struct snd_soc_dapm_update update
;
2329 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2333 mask
= mask
<< shift
;
2337 /* new connection */
2338 connect
= invert
? 0 : 1;
2340 /* old connection must be powered down */
2341 connect
= invert
? 1 : 0;
2343 mutex_lock(&codec
->mutex
);
2345 change
= snd_soc_test_bits(widget
->codec
, reg
, mask
, val
);
2347 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2348 widget
= wlist
->widgets
[wi
];
2350 widget
->value
= val
;
2352 update
.kcontrol
= kcontrol
;
2353 update
.widget
= widget
;
2357 widget
->dapm
->update
= &update
;
2359 snd_soc_dapm_mixer_update_power(widget
, kcontrol
, connect
);
2361 widget
->dapm
->update
= NULL
;
2365 mutex_unlock(&codec
->mutex
);
2368 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
2371 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2372 * @kcontrol: mixer control
2373 * @ucontrol: control element information
2375 * Callback to get the value of a dapm enumerated double mixer control.
2377 * Returns 0 for success.
2379 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
2380 struct snd_ctl_elem_value
*ucontrol
)
2382 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2383 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2384 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2385 unsigned int val
, bitmask
;
2387 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2389 val
= snd_soc_read(widget
->codec
, e
->reg
);
2390 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & (bitmask
- 1);
2391 if (e
->shift_l
!= e
->shift_r
)
2392 ucontrol
->value
.enumerated
.item
[1] =
2393 (val
>> e
->shift_r
) & (bitmask
- 1);
2397 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
2400 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2401 * @kcontrol: mixer control
2402 * @ucontrol: control element information
2404 * Callback to set the value of a dapm enumerated double mixer control.
2406 * Returns 0 for success.
2408 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
2409 struct snd_ctl_elem_value
*ucontrol
)
2411 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2412 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2413 struct snd_soc_codec
*codec
= widget
->codec
;
2414 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2415 unsigned int val
, mux
, change
;
2416 unsigned int mask
, bitmask
;
2417 struct snd_soc_dapm_update update
;
2420 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2422 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2424 mux
= ucontrol
->value
.enumerated
.item
[0];
2425 val
= mux
<< e
->shift_l
;
2426 mask
= (bitmask
- 1) << e
->shift_l
;
2427 if (e
->shift_l
!= e
->shift_r
) {
2428 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2430 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
2431 mask
|= (bitmask
- 1) << e
->shift_r
;
2434 mutex_lock(&codec
->mutex
);
2436 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
2438 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2439 widget
= wlist
->widgets
[wi
];
2441 widget
->value
= val
;
2443 update
.kcontrol
= kcontrol
;
2444 update
.widget
= widget
;
2445 update
.reg
= e
->reg
;
2448 widget
->dapm
->update
= &update
;
2450 snd_soc_dapm_mux_update_power(widget
, kcontrol
, mux
, e
);
2452 widget
->dapm
->update
= NULL
;
2456 mutex_unlock(&codec
->mutex
);
2459 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
2462 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2463 * @kcontrol: mixer control
2464 * @ucontrol: control element information
2466 * Returns 0 for success.
2468 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol
*kcontrol
,
2469 struct snd_ctl_elem_value
*ucontrol
)
2471 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2472 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2474 ucontrol
->value
.enumerated
.item
[0] = widget
->value
;
2478 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt
);
2481 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2482 * @kcontrol: mixer control
2483 * @ucontrol: control element information
2485 * Returns 0 for success.
2487 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol
*kcontrol
,
2488 struct snd_ctl_elem_value
*ucontrol
)
2490 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2491 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2492 struct snd_soc_codec
*codec
= widget
->codec
;
2493 struct soc_enum
*e
=
2494 (struct soc_enum
*)kcontrol
->private_value
;
2499 if (ucontrol
->value
.enumerated
.item
[0] >= e
->max
)
2502 mutex_lock(&codec
->mutex
);
2504 change
= widget
->value
!= ucontrol
->value
.enumerated
.item
[0];
2506 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2507 widget
= wlist
->widgets
[wi
];
2509 widget
->value
= ucontrol
->value
.enumerated
.item
[0];
2511 snd_soc_dapm_mux_update_power(widget
, kcontrol
, widget
->value
, e
);
2515 mutex_unlock(&codec
->mutex
);
2518 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt
);
2521 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2523 * @kcontrol: mixer control
2524 * @ucontrol: control element information
2526 * Callback to get the value of a dapm semi enumerated double mixer control.
2528 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2529 * used for handling bitfield coded enumeration for example.
2531 * Returns 0 for success.
2533 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
2534 struct snd_ctl_elem_value
*ucontrol
)
2536 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2537 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2538 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2539 unsigned int reg_val
, val
, mux
;
2541 reg_val
= snd_soc_read(widget
->codec
, e
->reg
);
2542 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
2543 for (mux
= 0; mux
< e
->max
; mux
++) {
2544 if (val
== e
->values
[mux
])
2547 ucontrol
->value
.enumerated
.item
[0] = mux
;
2548 if (e
->shift_l
!= e
->shift_r
) {
2549 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
2550 for (mux
= 0; mux
< e
->max
; mux
++) {
2551 if (val
== e
->values
[mux
])
2554 ucontrol
->value
.enumerated
.item
[1] = mux
;
2559 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double
);
2562 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2564 * @kcontrol: mixer control
2565 * @ucontrol: control element information
2567 * Callback to set the value of a dapm semi enumerated double mixer control.
2569 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2570 * used for handling bitfield coded enumeration for example.
2572 * Returns 0 for success.
2574 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
2575 struct snd_ctl_elem_value
*ucontrol
)
2577 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2578 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2579 struct snd_soc_codec
*codec
= widget
->codec
;
2580 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2581 unsigned int val
, mux
, change
;
2583 struct snd_soc_dapm_update update
;
2586 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2588 mux
= ucontrol
->value
.enumerated
.item
[0];
2589 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
2590 mask
= e
->mask
<< e
->shift_l
;
2591 if (e
->shift_l
!= e
->shift_r
) {
2592 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2594 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
2595 mask
|= e
->mask
<< e
->shift_r
;
2598 mutex_lock(&codec
->mutex
);
2600 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
2602 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2603 widget
= wlist
->widgets
[wi
];
2605 widget
->value
= val
;
2607 update
.kcontrol
= kcontrol
;
2608 update
.widget
= widget
;
2609 update
.reg
= e
->reg
;
2612 widget
->dapm
->update
= &update
;
2614 snd_soc_dapm_mux_update_power(widget
, kcontrol
, mux
, e
);
2616 widget
->dapm
->update
= NULL
;
2620 mutex_unlock(&codec
->mutex
);
2623 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double
);
2626 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2628 * @kcontrol: mixer control
2629 * @uinfo: control element information
2631 * Callback to provide information about a pin switch control.
2633 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
2634 struct snd_ctl_elem_info
*uinfo
)
2636 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2638 uinfo
->value
.integer
.min
= 0;
2639 uinfo
->value
.integer
.max
= 1;
2643 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
2646 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2648 * @kcontrol: mixer control
2651 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
2652 struct snd_ctl_elem_value
*ucontrol
)
2654 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2655 const char *pin
= (const char *)kcontrol
->private_value
;
2657 mutex_lock(&codec
->mutex
);
2659 ucontrol
->value
.integer
.value
[0] =
2660 snd_soc_dapm_get_pin_status(&codec
->dapm
, pin
);
2662 mutex_unlock(&codec
->mutex
);
2666 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
2669 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2671 * @kcontrol: mixer control
2674 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
2675 struct snd_ctl_elem_value
*ucontrol
)
2677 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2678 const char *pin
= (const char *)kcontrol
->private_value
;
2680 mutex_lock(&codec
->mutex
);
2682 if (ucontrol
->value
.integer
.value
[0])
2683 snd_soc_dapm_enable_pin(&codec
->dapm
, pin
);
2685 snd_soc_dapm_disable_pin(&codec
->dapm
, pin
);
2687 snd_soc_dapm_sync(&codec
->dapm
);
2689 mutex_unlock(&codec
->mutex
);
2693 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
2696 * snd_soc_dapm_new_control - create new dapm control
2697 * @dapm: DAPM context
2698 * @widget: widget template
2700 * Creates a new dapm control based upon the template.
2702 * Returns 0 for success else error.
2704 int snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
2705 const struct snd_soc_dapm_widget
*widget
)
2707 struct snd_soc_dapm_widget
*w
;
2711 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
2715 case snd_soc_dapm_regulator_supply
:
2716 w
->priv
= devm_regulator_get(dapm
->dev
, w
->name
);
2717 if (IS_ERR(w
->priv
)) {
2718 ret
= PTR_ERR(w
->priv
);
2719 dev_err(dapm
->dev
, "Failed to request %s: %d\n",
2728 name_len
= strlen(widget
->name
) + 1;
2729 if (dapm
->codec
&& dapm
->codec
->name_prefix
)
2730 name_len
+= 1 + strlen(dapm
->codec
->name_prefix
);
2731 w
->name
= kmalloc(name_len
, GFP_KERNEL
);
2732 if (w
->name
== NULL
) {
2736 if (dapm
->codec
&& dapm
->codec
->name_prefix
)
2737 snprintf(w
->name
, name_len
, "%s %s",
2738 dapm
->codec
->name_prefix
, widget
->name
);
2740 snprintf(w
->name
, name_len
, "%s", widget
->name
);
2743 case snd_soc_dapm_switch
:
2744 case snd_soc_dapm_mixer
:
2745 case snd_soc_dapm_mixer_named_ctl
:
2746 w
->power_check
= dapm_generic_check_power
;
2748 case snd_soc_dapm_mux
:
2749 case snd_soc_dapm_virt_mux
:
2750 case snd_soc_dapm_value_mux
:
2751 w
->power_check
= dapm_generic_check_power
;
2753 case snd_soc_dapm_adc
:
2754 case snd_soc_dapm_aif_out
:
2755 w
->power_check
= dapm_adc_check_power
;
2757 case snd_soc_dapm_dac
:
2758 case snd_soc_dapm_aif_in
:
2759 w
->power_check
= dapm_dac_check_power
;
2761 case snd_soc_dapm_pga
:
2762 case snd_soc_dapm_out_drv
:
2763 case snd_soc_dapm_input
:
2764 case snd_soc_dapm_output
:
2765 case snd_soc_dapm_micbias
:
2766 case snd_soc_dapm_spk
:
2767 case snd_soc_dapm_hp
:
2768 case snd_soc_dapm_mic
:
2769 case snd_soc_dapm_line
:
2770 w
->power_check
= dapm_generic_check_power
;
2772 case snd_soc_dapm_supply
:
2773 case snd_soc_dapm_regulator_supply
:
2774 w
->power_check
= dapm_supply_check_power
;
2777 w
->power_check
= dapm_always_on_check_power
;
2783 w
->codec
= dapm
->codec
;
2784 w
->platform
= dapm
->platform
;
2785 INIT_LIST_HEAD(&w
->sources
);
2786 INIT_LIST_HEAD(&w
->sinks
);
2787 INIT_LIST_HEAD(&w
->list
);
2788 INIT_LIST_HEAD(&w
->dirty
);
2789 list_add(&w
->list
, &dapm
->card
->widgets
);
2791 /* machine layer set ups unconnected pins and insertions */
2795 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
2798 * snd_soc_dapm_new_controls - create new dapm controls
2799 * @dapm: DAPM context
2800 * @widget: widget array
2801 * @num: number of widgets
2803 * Creates new DAPM controls based upon the templates.
2805 * Returns 0 for success else error.
2807 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context
*dapm
,
2808 const struct snd_soc_dapm_widget
*widget
,
2813 for (i
= 0; i
< num
; i
++) {
2814 ret
= snd_soc_dapm_new_control(dapm
, widget
);
2817 "ASoC: Failed to create DAPM control %s: %d\n",
2825 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
2827 static void soc_dapm_stream_event(struct snd_soc_dapm_context
*dapm
,
2828 const char *stream
, int event
)
2830 struct snd_soc_dapm_widget
*w
;
2832 list_for_each_entry(w
, &dapm
->card
->widgets
, list
)
2834 if (!w
->sname
|| w
->dapm
!= dapm
)
2836 dev_vdbg(w
->dapm
->dev
, "widget %s\n %s stream %s event %d\n",
2837 w
->name
, w
->sname
, stream
, event
);
2838 if (strstr(w
->sname
, stream
)) {
2839 dapm_mark_dirty(w
, "stream event");
2841 case SND_SOC_DAPM_STREAM_START
:
2844 case SND_SOC_DAPM_STREAM_STOP
:
2847 case SND_SOC_DAPM_STREAM_SUSPEND
:
2848 case SND_SOC_DAPM_STREAM_RESUME
:
2849 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
2850 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
2856 dapm_power_widgets(dapm
, event
);
2858 /* do we need to notify any clients that DAPM stream is complete */
2859 if (dapm
->stream_event
)
2860 dapm
->stream_event(dapm
, event
);
2864 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2865 * @rtd: PCM runtime data
2866 * @stream: stream name
2867 * @event: stream event
2869 * Sends a stream event to the dapm core. The core then makes any
2870 * necessary widget power changes.
2872 * Returns 0 for success else error.
2874 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
,
2875 const char *stream
, int event
)
2877 struct snd_soc_codec
*codec
= rtd
->codec
;
2882 mutex_lock(&codec
->mutex
);
2883 soc_dapm_stream_event(&codec
->dapm
, stream
, event
);
2884 mutex_unlock(&codec
->mutex
);
2889 * snd_soc_dapm_enable_pin - enable pin.
2890 * @dapm: DAPM context
2893 * Enables input/output pin and its parents or children widgets iff there is
2894 * a valid audio route and active audio stream.
2895 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2896 * do any widget power switching.
2898 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
2900 return snd_soc_dapm_set_pin(dapm
, pin
, 1);
2902 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
2905 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2906 * @dapm: DAPM context
2909 * Enables input/output pin regardless of any other state. This is
2910 * intended for use with microphone bias supplies used in microphone
2913 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2914 * do any widget power switching.
2916 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context
*dapm
,
2919 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
2922 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
2926 dev_dbg(w
->dapm
->dev
, "dapm: force enable pin %s\n", pin
);
2929 dapm_mark_dirty(w
, "force enable");
2933 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
2936 * snd_soc_dapm_disable_pin - disable pin.
2937 * @dapm: DAPM context
2940 * Disables input/output pin and its parents or children widgets.
2941 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2942 * do any widget power switching.
2944 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context
*dapm
,
2947 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
2949 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
2952 * snd_soc_dapm_nc_pin - permanently disable pin.
2953 * @dapm: DAPM context
2956 * Marks the specified pin as being not connected, disabling it along
2957 * any parent or child widgets. At present this is identical to
2958 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2959 * additional things such as disabling controls which only affect
2960 * paths through the pin.
2962 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2963 * do any widget power switching.
2965 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
2967 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
2969 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
2972 * snd_soc_dapm_get_pin_status - get audio pin status
2973 * @dapm: DAPM context
2974 * @pin: audio signal pin endpoint (or start point)
2976 * Get audio pin status - connected or disconnected.
2978 * Returns 1 for connected otherwise 0.
2980 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context
*dapm
,
2983 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
2986 return w
->connected
;
2990 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
2993 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2994 * @dapm: DAPM context
2995 * @pin: audio signal pin endpoint (or start point)
2997 * Mark the given endpoint or pin as ignoring suspend. When the
2998 * system is disabled a path between two endpoints flagged as ignoring
2999 * suspend will not be disabled. The path must already be enabled via
3000 * normal means at suspend time, it will not be turned on if it was not
3003 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context
*dapm
,
3006 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, false);
3009 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
3013 w
->ignore_suspend
= 1;
3017 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
3019 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card
*card
,
3020 struct snd_soc_dapm_widget
*w
)
3022 struct snd_soc_dapm_path
*p
;
3024 list_for_each_entry(p
, &card
->paths
, list
) {
3025 if ((p
->source
== w
) || (p
->sink
== w
)) {
3027 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3028 p
->source
->name
, p
->source
->id
, p
->source
->dapm
,
3029 p
->sink
->name
, p
->sink
->id
, p
->sink
->dapm
);
3031 /* Connected to something other than the codec */
3032 if (p
->source
->dapm
!= p
->sink
->dapm
)
3035 * Loopback connection from codec external pin to
3036 * codec external pin
3038 if (p
->sink
->id
== snd_soc_dapm_input
) {
3039 switch (p
->source
->id
) {
3040 case snd_soc_dapm_output
:
3041 case snd_soc_dapm_micbias
:
3054 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3055 * @codec: The codec whose pins should be processed
3057 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3058 * which are unused. Pins are used if they are connected externally to the
3059 * codec, whether that be to some other device, or a loop-back connection to
3062 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec
*codec
)
3064 struct snd_soc_card
*card
= codec
->card
;
3065 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
3066 struct snd_soc_dapm_widget
*w
;
3068 dev_dbg(codec
->dev
, "Auto NC: DAPMs: card:%p codec:%p\n",
3069 &card
->dapm
, &codec
->dapm
);
3071 list_for_each_entry(w
, &card
->widgets
, list
) {
3072 if (w
->dapm
!= dapm
)
3075 case snd_soc_dapm_input
:
3076 case snd_soc_dapm_output
:
3077 case snd_soc_dapm_micbias
:
3078 dev_dbg(codec
->dev
, "Auto NC: Checking widget %s\n",
3080 if (!snd_soc_dapm_widget_in_card_paths(card
, w
)) {
3082 "... Not in map; disabling\n");
3083 snd_soc_dapm_nc_pin(dapm
, w
->name
);
3093 * snd_soc_dapm_free - free dapm resources
3094 * @dapm: DAPM context
3096 * Free all dapm widgets and resources.
3098 void snd_soc_dapm_free(struct snd_soc_dapm_context
*dapm
)
3100 snd_soc_dapm_sys_remove(dapm
->dev
);
3101 dapm_debugfs_cleanup(dapm
);
3102 dapm_free_widgets(dapm
);
3103 list_del(&dapm
->list
);
3105 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
3107 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context
*dapm
)
3109 struct snd_soc_dapm_widget
*w
;
3110 LIST_HEAD(down_list
);
3113 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
3114 if (w
->dapm
!= dapm
)
3117 dapm_seq_insert(w
, &down_list
, false);
3123 /* If there were no widgets to power down we're already in
3127 snd_soc_dapm_set_bias_level(dapm
, SND_SOC_BIAS_PREPARE
);
3128 dapm_seq_run(dapm
, &down_list
, 0, false);
3129 snd_soc_dapm_set_bias_level(dapm
, SND_SOC_BIAS_STANDBY
);
3134 * snd_soc_dapm_shutdown - callback for system shutdown
3136 void snd_soc_dapm_shutdown(struct snd_soc_card
*card
)
3138 struct snd_soc_codec
*codec
;
3140 list_for_each_entry(codec
, &card
->codec_dev_list
, list
) {
3141 soc_dapm_shutdown_codec(&codec
->dapm
);
3142 snd_soc_dapm_set_bias_level(&codec
->dapm
, SND_SOC_BIAS_OFF
);
3146 /* Module information */
3147 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3148 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3149 MODULE_LICENSE("GPL");