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/clk.h>
39 #include <linux/slab.h>
40 #include <sound/core.h>
41 #include <sound/pcm.h>
42 #include <sound/pcm_params.h>
43 #include <sound/soc.h>
44 #include <sound/initval.h>
46 #include <trace/events/asoc.h>
48 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
50 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context
*dapm
,
51 struct snd_soc_dapm_widget
*wsource
, struct snd_soc_dapm_widget
*wsink
,
53 int (*connected
)(struct snd_soc_dapm_widget
*source
,
54 struct snd_soc_dapm_widget
*sink
));
55 static struct snd_soc_dapm_widget
*
56 snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
57 const struct snd_soc_dapm_widget
*widget
);
59 /* dapm power sequences - make this per codec in the future */
60 static int dapm_up_seq
[] = {
61 [snd_soc_dapm_pre
] = 0,
62 [snd_soc_dapm_supply
] = 1,
63 [snd_soc_dapm_regulator_supply
] = 1,
64 [snd_soc_dapm_clock_supply
] = 1,
65 [snd_soc_dapm_micbias
] = 2,
66 [snd_soc_dapm_dai_link
] = 2,
67 [snd_soc_dapm_dai_in
] = 3,
68 [snd_soc_dapm_dai_out
] = 3,
69 [snd_soc_dapm_aif_in
] = 3,
70 [snd_soc_dapm_aif_out
] = 3,
71 [snd_soc_dapm_mic
] = 4,
72 [snd_soc_dapm_mux
] = 5,
73 [snd_soc_dapm_virt_mux
] = 5,
74 [snd_soc_dapm_value_mux
] = 5,
75 [snd_soc_dapm_dac
] = 6,
76 [snd_soc_dapm_switch
] = 7,
77 [snd_soc_dapm_mixer
] = 7,
78 [snd_soc_dapm_mixer_named_ctl
] = 7,
79 [snd_soc_dapm_pga
] = 8,
80 [snd_soc_dapm_adc
] = 9,
81 [snd_soc_dapm_out_drv
] = 10,
82 [snd_soc_dapm_hp
] = 10,
83 [snd_soc_dapm_spk
] = 10,
84 [snd_soc_dapm_line
] = 10,
85 [snd_soc_dapm_kcontrol
] = 11,
86 [snd_soc_dapm_post
] = 12,
89 static int dapm_down_seq
[] = {
90 [snd_soc_dapm_pre
] = 0,
91 [snd_soc_dapm_kcontrol
] = 1,
92 [snd_soc_dapm_adc
] = 2,
93 [snd_soc_dapm_hp
] = 3,
94 [snd_soc_dapm_spk
] = 3,
95 [snd_soc_dapm_line
] = 3,
96 [snd_soc_dapm_out_drv
] = 3,
97 [snd_soc_dapm_pga
] = 4,
98 [snd_soc_dapm_switch
] = 5,
99 [snd_soc_dapm_mixer_named_ctl
] = 5,
100 [snd_soc_dapm_mixer
] = 5,
101 [snd_soc_dapm_dac
] = 6,
102 [snd_soc_dapm_mic
] = 7,
103 [snd_soc_dapm_micbias
] = 8,
104 [snd_soc_dapm_mux
] = 9,
105 [snd_soc_dapm_virt_mux
] = 9,
106 [snd_soc_dapm_value_mux
] = 9,
107 [snd_soc_dapm_aif_in
] = 10,
108 [snd_soc_dapm_aif_out
] = 10,
109 [snd_soc_dapm_dai_in
] = 10,
110 [snd_soc_dapm_dai_out
] = 10,
111 [snd_soc_dapm_dai_link
] = 11,
112 [snd_soc_dapm_clock_supply
] = 12,
113 [snd_soc_dapm_regulator_supply
] = 12,
114 [snd_soc_dapm_supply
] = 12,
115 [snd_soc_dapm_post
] = 13,
118 static void pop_wait(u32 pop_time
)
121 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time
));
124 static void pop_dbg(struct device
*dev
, u32 pop_time
, const char *fmt
, ...)
132 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
137 vsnprintf(buf
, PAGE_SIZE
, fmt
, args
);
138 dev_info(dev
, "%s", buf
);
144 static bool dapm_dirty_widget(struct snd_soc_dapm_widget
*w
)
146 return !list_empty(&w
->dirty
);
149 void dapm_mark_dirty(struct snd_soc_dapm_widget
*w
, const char *reason
)
151 if (!dapm_dirty_widget(w
)) {
152 dev_vdbg(w
->dapm
->dev
, "Marking %s dirty due to %s\n",
154 list_add_tail(&w
->dirty
, &w
->dapm
->card
->dapm_dirty
);
157 EXPORT_SYMBOL_GPL(dapm_mark_dirty
);
159 void dapm_mark_io_dirty(struct snd_soc_dapm_context
*dapm
)
161 struct snd_soc_card
*card
= dapm
->card
;
162 struct snd_soc_dapm_widget
*w
;
164 mutex_lock(&card
->dapm_mutex
);
166 list_for_each_entry(w
, &card
->widgets
, list
) {
168 case snd_soc_dapm_input
:
169 case snd_soc_dapm_output
:
170 dapm_mark_dirty(w
, "Rechecking inputs and outputs");
177 mutex_unlock(&card
->dapm_mutex
);
179 EXPORT_SYMBOL_GPL(dapm_mark_io_dirty
);
181 /* create a new dapm widget */
182 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
183 const struct snd_soc_dapm_widget
*_widget
)
185 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
188 struct dapm_kcontrol_data
{
190 struct snd_soc_dapm_widget
*widget
;
191 struct list_head paths
;
192 struct snd_soc_dapm_widget_list
*wlist
;
195 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget
*widget
,
196 struct snd_kcontrol
*kcontrol
)
198 struct dapm_kcontrol_data
*data
;
199 struct soc_mixer_control
*mc
;
201 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
203 dev_err(widget
->dapm
->dev
,
204 "ASoC: can't allocate kcontrol data for %s\n",
209 INIT_LIST_HEAD(&data
->paths
);
211 switch (widget
->id
) {
212 case snd_soc_dapm_switch
:
213 case snd_soc_dapm_mixer
:
214 case snd_soc_dapm_mixer_named_ctl
:
215 mc
= (struct soc_mixer_control
*)kcontrol
->private_value
;
217 if (mc
->autodisable
) {
218 struct snd_soc_dapm_widget
template;
220 memset(&template, 0, sizeof(template));
221 template.reg
= mc
->reg
;
222 template.mask
= (1 << fls(mc
->max
)) - 1;
223 template.shift
= mc
->shift
;
225 template.off_val
= mc
->max
;
227 template.off_val
= 0;
228 template.on_val
= template.off_val
;
229 template.id
= snd_soc_dapm_kcontrol
;
230 template.name
= kcontrol
->id
.name
;
232 data
->value
= template.on_val
;
234 data
->widget
= snd_soc_dapm_new_control(widget
->dapm
,
246 kcontrol
->private_data
= data
;
251 static void dapm_kcontrol_free(struct snd_kcontrol
*kctl
)
253 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kctl
);
259 static struct snd_soc_dapm_widget_list
*dapm_kcontrol_get_wlist(
260 const struct snd_kcontrol
*kcontrol
)
262 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
267 static int dapm_kcontrol_add_widget(struct snd_kcontrol
*kcontrol
,
268 struct snd_soc_dapm_widget
*widget
)
270 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
271 struct snd_soc_dapm_widget_list
*new_wlist
;
275 n
= data
->wlist
->num_widgets
+ 1;
279 new_wlist
= krealloc(data
->wlist
,
280 sizeof(*new_wlist
) + sizeof(widget
) * n
, GFP_KERNEL
);
284 new_wlist
->widgets
[n
- 1] = widget
;
285 new_wlist
->num_widgets
= n
;
287 data
->wlist
= new_wlist
;
292 static void dapm_kcontrol_add_path(const struct snd_kcontrol
*kcontrol
,
293 struct snd_soc_dapm_path
*path
)
295 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
297 list_add_tail(&path
->list_kcontrol
, &data
->paths
);
300 snd_soc_dapm_add_path(data
->widget
->dapm
, data
->widget
,
301 path
->source
, NULL
, NULL
);
305 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol
*kcontrol
)
307 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
312 return data
->widget
->power
;
315 static struct list_head
*dapm_kcontrol_get_path_list(
316 const struct snd_kcontrol
*kcontrol
)
318 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
323 #define dapm_kcontrol_for_each_path(path, kcontrol) \
324 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
327 static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol
*kcontrol
)
329 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
334 static bool dapm_kcontrol_set_value(const struct snd_kcontrol
*kcontrol
,
337 struct dapm_kcontrol_data
*data
= snd_kcontrol_chip(kcontrol
);
339 if (data
->value
== value
)
343 data
->widget
->on_val
= value
;
351 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
352 * @kcontrol: The kcontrol
354 struct snd_soc_codec
*snd_soc_dapm_kcontrol_codec(struct snd_kcontrol
*kcontrol
)
356 return dapm_kcontrol_get_wlist(kcontrol
)->widgets
[0]->codec
;
358 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec
);
360 static void dapm_reset(struct snd_soc_card
*card
)
362 struct snd_soc_dapm_widget
*w
;
364 memset(&card
->dapm_stats
, 0, sizeof(card
->dapm_stats
));
366 list_for_each_entry(w
, &card
->widgets
, list
) {
367 w
->new_power
= w
->power
;
368 w
->power_checked
= false;
374 static int soc_widget_read(struct snd_soc_dapm_widget
*w
, int reg
)
377 return snd_soc_read(w
->codec
, reg
);
378 else if (w
->platform
)
379 return snd_soc_platform_read(w
->platform
, reg
);
381 dev_err(w
->dapm
->dev
, "ASoC: no valid widget read method\n");
385 static int soc_widget_write(struct snd_soc_dapm_widget
*w
, int reg
, int val
)
388 return snd_soc_write(w
->codec
, reg
, val
);
389 else if (w
->platform
)
390 return snd_soc_platform_write(w
->platform
, reg
, val
);
392 dev_err(w
->dapm
->dev
, "ASoC: no valid widget write method\n");
396 static inline void soc_widget_lock(struct snd_soc_dapm_widget
*w
)
398 if (w
->codec
&& !w
->codec
->using_regmap
)
399 mutex_lock(&w
->codec
->mutex
);
400 else if (w
->platform
)
401 mutex_lock(&w
->platform
->mutex
);
404 static inline void soc_widget_unlock(struct snd_soc_dapm_widget
*w
)
406 if (w
->codec
&& !w
->codec
->using_regmap
)
407 mutex_unlock(&w
->codec
->mutex
);
408 else if (w
->platform
)
409 mutex_unlock(&w
->platform
->mutex
);
412 static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget
*w
,
413 unsigned short reg
, unsigned int mask
, unsigned int value
)
416 unsigned int old
, new;
419 if (w
->codec
&& w
->codec
->using_regmap
) {
420 ret
= regmap_update_bits_check(w
->codec
->control_data
,
421 reg
, mask
, value
, &change
);
426 ret
= soc_widget_read(w
, reg
);
428 soc_widget_unlock(w
);
433 new = (old
& ~mask
) | (value
& mask
);
436 ret
= soc_widget_write(w
, reg
, new);
438 soc_widget_unlock(w
);
442 soc_widget_unlock(w
);
449 * snd_soc_dapm_set_bias_level - set the bias level for the system
450 * @dapm: DAPM context
451 * @level: level to configure
453 * Configure the bias (power) levels for the SoC audio device.
455 * Returns 0 for success else error.
457 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context
*dapm
,
458 enum snd_soc_bias_level level
)
460 struct snd_soc_card
*card
= dapm
->card
;
463 trace_snd_soc_bias_level_start(card
, level
);
465 if (card
&& card
->set_bias_level
)
466 ret
= card
->set_bias_level(card
, dapm
, level
);
471 if (dapm
->codec
->driver
->set_bias_level
)
472 ret
= dapm
->codec
->driver
->set_bias_level(dapm
->codec
,
475 dapm
->bias_level
= level
;
476 } else if (!card
|| dapm
!= &card
->dapm
) {
477 dapm
->bias_level
= level
;
483 if (card
&& card
->set_bias_level_post
)
484 ret
= card
->set_bias_level_post(card
, dapm
, level
);
486 trace_snd_soc_bias_level_done(card
, level
);
491 /* set up initial codec paths */
492 static void dapm_set_path_status(struct snd_soc_dapm_widget
*w
,
493 struct snd_soc_dapm_path
*p
, int i
)
496 case snd_soc_dapm_switch
:
497 case snd_soc_dapm_mixer
:
498 case snd_soc_dapm_mixer_named_ctl
: {
500 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
501 w
->kcontrol_news
[i
].private_value
;
502 unsigned int reg
= mc
->reg
;
503 unsigned int shift
= mc
->shift
;
505 unsigned int mask
= (1 << fls(max
)) - 1;
506 unsigned int invert
= mc
->invert
;
508 val
= soc_widget_read(w
, reg
);
509 val
= (val
>> shift
) & mask
;
516 case snd_soc_dapm_mux
: {
517 struct soc_enum
*e
= (struct soc_enum
*)
518 w
->kcontrol_news
[i
].private_value
;
521 val
= soc_widget_read(w
, e
->reg
);
522 item
= (val
>> e
->shift_l
) & e
->mask
;
524 if (item
< e
->max
&& !strcmp(p
->name
, e
->texts
[item
]))
530 case snd_soc_dapm_virt_mux
: {
531 struct soc_enum
*e
= (struct soc_enum
*)
532 w
->kcontrol_news
[i
].private_value
;
535 /* since a virtual mux has no backing registers to
536 * decide which path to connect, it will try to match
537 * with the first enumeration. This is to ensure
538 * that the default mux choice (the first) will be
539 * correctly powered up during initialization.
541 if (!strcmp(p
->name
, e
->texts
[0]))
545 case snd_soc_dapm_value_mux
: {
546 struct soc_enum
*e
= (struct soc_enum
*)
547 w
->kcontrol_news
[i
].private_value
;
550 val
= soc_widget_read(w
, e
->reg
);
551 val
= (val
>> e
->shift_l
) & e
->mask
;
552 for (item
= 0; item
< e
->max
; item
++) {
553 if (val
== e
->values
[item
])
557 if (item
< e
->max
&& !strcmp(p
->name
, e
->texts
[item
]))
563 /* does not affect routing - always connected */
564 case snd_soc_dapm_pga
:
565 case snd_soc_dapm_out_drv
:
566 case snd_soc_dapm_output
:
567 case snd_soc_dapm_adc
:
568 case snd_soc_dapm_input
:
569 case snd_soc_dapm_siggen
:
570 case snd_soc_dapm_dac
:
571 case snd_soc_dapm_micbias
:
572 case snd_soc_dapm_vmid
:
573 case snd_soc_dapm_supply
:
574 case snd_soc_dapm_regulator_supply
:
575 case snd_soc_dapm_clock_supply
:
576 case snd_soc_dapm_aif_in
:
577 case snd_soc_dapm_aif_out
:
578 case snd_soc_dapm_dai_in
:
579 case snd_soc_dapm_dai_out
:
580 case snd_soc_dapm_hp
:
581 case snd_soc_dapm_mic
:
582 case snd_soc_dapm_spk
:
583 case snd_soc_dapm_line
:
584 case snd_soc_dapm_dai_link
:
585 case snd_soc_dapm_kcontrol
:
588 /* does affect routing - dynamically connected */
589 case snd_soc_dapm_pre
:
590 case snd_soc_dapm_post
:
596 /* connect mux widget to its interconnecting audio paths */
597 static int dapm_connect_mux(struct snd_soc_dapm_context
*dapm
,
598 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
599 struct snd_soc_dapm_path
*path
, const char *control_name
,
600 const struct snd_kcontrol_new
*kcontrol
)
602 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
605 for (i
= 0; i
< e
->max
; i
++) {
606 if (!(strcmp(control_name
, e
->texts
[i
]))) {
607 list_add(&path
->list
, &dapm
->card
->paths
);
608 list_add(&path
->list_sink
, &dest
->sources
);
609 list_add(&path
->list_source
, &src
->sinks
);
610 path
->name
= (char*)e
->texts
[i
];
611 dapm_set_path_status(dest
, path
, 0);
619 /* connect mixer widget to its interconnecting audio paths */
620 static int dapm_connect_mixer(struct snd_soc_dapm_context
*dapm
,
621 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
622 struct snd_soc_dapm_path
*path
, const char *control_name
)
626 /* search for mixer kcontrol */
627 for (i
= 0; i
< dest
->num_kcontrols
; i
++) {
628 if (!strcmp(control_name
, dest
->kcontrol_news
[i
].name
)) {
629 list_add(&path
->list
, &dapm
->card
->paths
);
630 list_add(&path
->list_sink
, &dest
->sources
);
631 list_add(&path
->list_source
, &src
->sinks
);
632 path
->name
= dest
->kcontrol_news
[i
].name
;
633 dapm_set_path_status(dest
, path
, i
);
640 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context
*dapm
,
641 struct snd_soc_dapm_widget
*kcontrolw
,
642 const struct snd_kcontrol_new
*kcontrol_new
,
643 struct snd_kcontrol
**kcontrol
)
645 struct snd_soc_dapm_widget
*w
;
650 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
651 if (w
== kcontrolw
|| w
->dapm
!= kcontrolw
->dapm
)
653 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
654 if (&w
->kcontrol_news
[i
] == kcontrol_new
) {
656 *kcontrol
= w
->kcontrols
[i
];
666 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
667 * create it. Either way, add the widget into the control's widget list
669 static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget
*w
,
672 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
673 struct snd_card
*card
= dapm
->card
->snd_card
;
677 struct snd_kcontrol
*kcontrol
;
678 bool wname_in_long_name
, kcname_in_long_name
;
684 prefix
= dapm
->codec
->name_prefix
;
689 prefix_len
= strlen(prefix
) + 1;
693 shared
= dapm_is_shared_kcontrol(dapm
, w
, &w
->kcontrol_news
[kci
],
698 wname_in_long_name
= false;
699 kcname_in_long_name
= true;
702 case snd_soc_dapm_switch
:
703 case snd_soc_dapm_mixer
:
704 wname_in_long_name
= true;
705 kcname_in_long_name
= true;
707 case snd_soc_dapm_mixer_named_ctl
:
708 wname_in_long_name
= false;
709 kcname_in_long_name
= true;
711 case snd_soc_dapm_mux
:
712 case snd_soc_dapm_virt_mux
:
713 case snd_soc_dapm_value_mux
:
714 wname_in_long_name
= true;
715 kcname_in_long_name
= false;
722 if (wname_in_long_name
&& kcname_in_long_name
) {
724 * The control will get a prefix from the control
725 * creation process but we're also using the same
726 * prefix for widgets so cut the prefix off the
727 * front of the widget name.
729 long_name
= kasprintf(GFP_KERNEL
, "%s %s",
730 w
->name
+ prefix_len
,
731 w
->kcontrol_news
[kci
].name
);
732 if (long_name
== NULL
)
736 } else if (wname_in_long_name
) {
738 name
= w
->name
+ prefix_len
;
741 name
= w
->kcontrol_news
[kci
].name
;
744 kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[kci
], NULL
, name
,
749 kcontrol
->private_free
= dapm_kcontrol_free
;
751 ret
= dapm_kcontrol_data_alloc(w
, kcontrol
);
753 snd_ctl_free_one(kcontrol
);
757 ret
= snd_ctl_add(card
, kcontrol
);
760 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
766 ret
= dapm_kcontrol_add_widget(kcontrol
, w
);
770 w
->kcontrols
[kci
] = kcontrol
;
775 /* create new dapm mixer control */
776 static int dapm_new_mixer(struct snd_soc_dapm_widget
*w
)
779 struct snd_soc_dapm_path
*path
;
782 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
784 list_for_each_entry(path
, &w
->sources
, list_sink
) {
785 /* mixer/mux paths name must match control name */
786 if (path
->name
!= (char *)w
->kcontrol_news
[i
].name
)
789 if (w
->kcontrols
[i
]) {
790 dapm_kcontrol_add_path(w
->kcontrols
[i
], path
);
794 ret
= dapm_create_or_share_mixmux_kcontrol(w
, i
);
798 dapm_kcontrol_add_path(w
->kcontrols
[i
], path
);
805 /* create new dapm mux control */
806 static int dapm_new_mux(struct snd_soc_dapm_widget
*w
)
808 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
809 struct snd_soc_dapm_path
*path
;
812 if (w
->num_kcontrols
!= 1) {
814 "ASoC: mux %s has incorrect number of controls\n",
819 if (list_empty(&w
->sources
)) {
820 dev_err(dapm
->dev
, "ASoC: mux %s has no paths\n", w
->name
);
824 ret
= dapm_create_or_share_mixmux_kcontrol(w
, 0);
828 list_for_each_entry(path
, &w
->sources
, list_sink
)
829 dapm_kcontrol_add_path(w
->kcontrols
[0], path
);
834 /* create new dapm volume control */
835 static int dapm_new_pga(struct snd_soc_dapm_widget
*w
)
837 if (w
->num_kcontrols
)
838 dev_err(w
->dapm
->dev
,
839 "ASoC: PGA controls not supported: '%s'\n", w
->name
);
844 /* reset 'walked' bit for each dapm path */
845 static void dapm_clear_walk_output(struct snd_soc_dapm_context
*dapm
,
846 struct list_head
*sink
)
848 struct snd_soc_dapm_path
*p
;
850 list_for_each_entry(p
, sink
, list_source
) {
853 dapm_clear_walk_output(dapm
, &p
->sink
->sinks
);
858 static void dapm_clear_walk_input(struct snd_soc_dapm_context
*dapm
,
859 struct list_head
*source
)
861 struct snd_soc_dapm_path
*p
;
863 list_for_each_entry(p
, source
, list_sink
) {
866 dapm_clear_walk_input(dapm
, &p
->source
->sources
);
872 /* We implement power down on suspend by checking the power state of
873 * the ALSA card - when we are suspending the ALSA state for the card
876 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
878 int level
= snd_power_get_state(widget
->dapm
->card
->snd_card
);
881 case SNDRV_CTL_POWER_D3hot
:
882 case SNDRV_CTL_POWER_D3cold
:
883 if (widget
->ignore_suspend
)
884 dev_dbg(widget
->dapm
->dev
, "ASoC: %s ignoring suspend\n",
886 return widget
->ignore_suspend
;
892 /* add widget to list if it's not already in the list */
893 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list
**list
,
894 struct snd_soc_dapm_widget
*w
)
896 struct snd_soc_dapm_widget_list
*wlist
;
897 int wlistsize
, wlistentries
, i
;
904 /* is this widget already in the list */
905 for (i
= 0; i
< wlist
->num_widgets
; i
++) {
906 if (wlist
->widgets
[i
] == w
)
910 /* allocate some new space */
911 wlistentries
= wlist
->num_widgets
+ 1;
912 wlistsize
= sizeof(struct snd_soc_dapm_widget_list
) +
913 wlistentries
* sizeof(struct snd_soc_dapm_widget
*);
914 *list
= krealloc(wlist
, wlistsize
, GFP_KERNEL
);
916 dev_err(w
->dapm
->dev
, "ASoC: can't allocate widget list for %s\n",
922 /* insert the widget */
923 dev_dbg(w
->dapm
->dev
, "ASoC: added %s in widget list pos %d\n",
924 w
->name
, wlist
->num_widgets
);
926 wlist
->widgets
[wlist
->num_widgets
] = w
;
927 wlist
->num_widgets
++;
932 * Recursively check for a completed path to an active or physically connected
933 * output widget. Returns number of complete paths.
935 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
,
936 struct snd_soc_dapm_widget_list
**list
)
938 struct snd_soc_dapm_path
*path
;
941 if (widget
->outputs
>= 0)
942 return widget
->outputs
;
944 DAPM_UPDATE_STAT(widget
, path_checks
);
946 switch (widget
->id
) {
947 case snd_soc_dapm_supply
:
948 case snd_soc_dapm_regulator_supply
:
949 case snd_soc_dapm_clock_supply
:
950 case snd_soc_dapm_kcontrol
:
956 switch (widget
->id
) {
957 case snd_soc_dapm_adc
:
958 case snd_soc_dapm_aif_out
:
959 case snd_soc_dapm_dai_out
:
960 if (widget
->active
) {
961 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
962 return widget
->outputs
;
968 if (widget
->connected
) {
969 /* connected pin ? */
970 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
) {
971 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
972 return widget
->outputs
;
975 /* connected jack or spk ? */
976 if (widget
->id
== snd_soc_dapm_hp
||
977 widget
->id
== snd_soc_dapm_spk
||
978 (widget
->id
== snd_soc_dapm_line
&&
979 !list_empty(&widget
->sources
))) {
980 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
981 return widget
->outputs
;
985 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
986 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
997 trace_snd_soc_dapm_output_path(widget
, path
);
999 if (path
->sink
&& path
->connect
) {
1003 /* do we need to add this widget to the list ? */
1006 err
= dapm_list_add_widget(list
, path
->sink
);
1008 dev_err(widget
->dapm
->dev
,
1009 "ASoC: could not add widget %s\n",
1016 con
+= is_connected_output_ep(path
->sink
, list
);
1022 widget
->outputs
= con
;
1028 * Recursively check for a completed path to an active or physically connected
1029 * input widget. Returns number of complete paths.
1031 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
,
1032 struct snd_soc_dapm_widget_list
**list
)
1034 struct snd_soc_dapm_path
*path
;
1037 if (widget
->inputs
>= 0)
1038 return widget
->inputs
;
1040 DAPM_UPDATE_STAT(widget
, path_checks
);
1042 switch (widget
->id
) {
1043 case snd_soc_dapm_supply
:
1044 case snd_soc_dapm_regulator_supply
:
1045 case snd_soc_dapm_clock_supply
:
1046 case snd_soc_dapm_kcontrol
:
1052 /* active stream ? */
1053 switch (widget
->id
) {
1054 case snd_soc_dapm_dac
:
1055 case snd_soc_dapm_aif_in
:
1056 case snd_soc_dapm_dai_in
:
1057 if (widget
->active
) {
1058 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
1059 return widget
->inputs
;
1065 if (widget
->connected
) {
1066 /* connected pin ? */
1067 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
) {
1068 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
1069 return widget
->inputs
;
1072 /* connected VMID/Bias for lower pops */
1073 if (widget
->id
== snd_soc_dapm_vmid
) {
1074 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
1075 return widget
->inputs
;
1078 /* connected jack ? */
1079 if (widget
->id
== snd_soc_dapm_mic
||
1080 (widget
->id
== snd_soc_dapm_line
&&
1081 !list_empty(&widget
->sinks
))) {
1082 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
1083 return widget
->inputs
;
1086 /* signal generator */
1087 if (widget
->id
== snd_soc_dapm_siggen
) {
1088 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
1089 return widget
->inputs
;
1093 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
1094 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
1105 trace_snd_soc_dapm_input_path(widget
, path
);
1107 if (path
->source
&& path
->connect
) {
1111 /* do we need to add this widget to the list ? */
1114 err
= dapm_list_add_widget(list
, path
->source
);
1116 dev_err(widget
->dapm
->dev
,
1117 "ASoC: could not add widget %s\n",
1124 con
+= is_connected_input_ep(path
->source
, list
);
1130 widget
->inputs
= con
;
1136 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1137 * @dai: the soc DAI.
1138 * @stream: stream direction.
1139 * @list: list of active widgets for this stream.
1141 * Queries DAPM graph as to whether an valid audio stream path exists for
1142 * the initial stream specified by name. This takes into account
1143 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1145 * Returns the number of valid paths or negative error.
1147 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai
*dai
, int stream
,
1148 struct snd_soc_dapm_widget_list
**list
)
1150 struct snd_soc_card
*card
= dai
->card
;
1153 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
1156 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1157 paths
= is_connected_output_ep(dai
->playback_widget
, list
);
1158 dapm_clear_walk_output(&card
->dapm
,
1159 &dai
->playback_widget
->sinks
);
1161 paths
= is_connected_input_ep(dai
->capture_widget
, list
);
1162 dapm_clear_walk_input(&card
->dapm
,
1163 &dai
->capture_widget
->sources
);
1166 trace_snd_soc_dapm_connected(paths
, stream
);
1167 mutex_unlock(&card
->dapm_mutex
);
1173 * Handler for generic register modifier widget.
1175 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
1176 struct snd_kcontrol
*kcontrol
, int event
)
1180 if (SND_SOC_DAPM_EVENT_ON(event
))
1185 soc_widget_update_bits_locked(w
, -(w
->reg
+ 1),
1186 w
->mask
<< w
->shift
, val
<< w
->shift
);
1190 EXPORT_SYMBOL_GPL(dapm_reg_event
);
1193 * Handler for regulator supply widget.
1195 int dapm_regulator_event(struct snd_soc_dapm_widget
*w
,
1196 struct snd_kcontrol
*kcontrol
, int event
)
1200 if (SND_SOC_DAPM_EVENT_ON(event
)) {
1201 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
1202 ret
= regulator_allow_bypass(w
->regulator
, false);
1204 dev_warn(w
->dapm
->dev
,
1205 "ASoC: Failed to bypass %s: %d\n",
1209 return regulator_enable(w
->regulator
);
1211 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
1212 ret
= regulator_allow_bypass(w
->regulator
, true);
1214 dev_warn(w
->dapm
->dev
,
1215 "ASoC: Failed to unbypass %s: %d\n",
1219 return regulator_disable_deferred(w
->regulator
, w
->shift
);
1222 EXPORT_SYMBOL_GPL(dapm_regulator_event
);
1225 * Handler for clock supply widget.
1227 int dapm_clock_event(struct snd_soc_dapm_widget
*w
,
1228 struct snd_kcontrol
*kcontrol
, int event
)
1233 #ifdef CONFIG_HAVE_CLK
1234 if (SND_SOC_DAPM_EVENT_ON(event
)) {
1235 return clk_prepare_enable(w
->clk
);
1237 clk_disable_unprepare(w
->clk
);
1243 EXPORT_SYMBOL_GPL(dapm_clock_event
);
1245 static int dapm_widget_power_check(struct snd_soc_dapm_widget
*w
)
1247 if (w
->power_checked
)
1248 return w
->new_power
;
1253 w
->new_power
= w
->power_check(w
);
1255 w
->power_checked
= true;
1257 return w
->new_power
;
1260 /* Generic check to see if a widget should be powered.
1262 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
1266 DAPM_UPDATE_STAT(w
, power_checks
);
1268 in
= is_connected_input_ep(w
, NULL
);
1269 dapm_clear_walk_input(w
->dapm
, &w
->sources
);
1270 out
= is_connected_output_ep(w
, NULL
);
1271 dapm_clear_walk_output(w
->dapm
, &w
->sinks
);
1272 return out
!= 0 && in
!= 0;
1275 /* Check to see if an ADC has power */
1276 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
1280 DAPM_UPDATE_STAT(w
, power_checks
);
1283 in
= is_connected_input_ep(w
, NULL
);
1284 dapm_clear_walk_input(w
->dapm
, &w
->sources
);
1287 return dapm_generic_check_power(w
);
1291 /* Check to see if a DAC has power */
1292 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
1296 DAPM_UPDATE_STAT(w
, power_checks
);
1299 out
= is_connected_output_ep(w
, NULL
);
1300 dapm_clear_walk_output(w
->dapm
, &w
->sinks
);
1303 return dapm_generic_check_power(w
);
1307 /* Check to see if a power supply is needed */
1308 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
1310 struct snd_soc_dapm_path
*path
;
1312 DAPM_UPDATE_STAT(w
, power_checks
);
1314 /* Check if one of our outputs is connected */
1315 list_for_each_entry(path
, &w
->sinks
, list_source
) {
1316 DAPM_UPDATE_STAT(w
, neighbour_checks
);
1321 if (path
->connected
&&
1322 !path
->connected(path
->source
, path
->sink
))
1328 if (dapm_widget_power_check(path
->sink
))
1335 static int dapm_always_on_check_power(struct snd_soc_dapm_widget
*w
)
1340 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
1341 struct snd_soc_dapm_widget
*b
,
1349 sort
= dapm_down_seq
;
1351 if (sort
[a
->id
] != sort
[b
->id
])
1352 return sort
[a
->id
] - sort
[b
->id
];
1353 if (a
->subseq
!= b
->subseq
) {
1355 return a
->subseq
- b
->subseq
;
1357 return b
->subseq
- a
->subseq
;
1359 if (a
->reg
!= b
->reg
)
1360 return a
->reg
- b
->reg
;
1361 if (a
->dapm
!= b
->dapm
)
1362 return (unsigned long)a
->dapm
- (unsigned long)b
->dapm
;
1367 /* Insert a widget in order into a DAPM power sequence. */
1368 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
1369 struct list_head
*list
,
1372 struct snd_soc_dapm_widget
*w
;
1374 list_for_each_entry(w
, list
, power_list
)
1375 if (dapm_seq_compare(new_widget
, w
, power_up
) < 0) {
1376 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
1380 list_add_tail(&new_widget
->power_list
, list
);
1383 static void dapm_seq_check_event(struct snd_soc_card
*card
,
1384 struct snd_soc_dapm_widget
*w
, int event
)
1386 const char *ev_name
;
1390 case SND_SOC_DAPM_PRE_PMU
:
1391 ev_name
= "PRE_PMU";
1394 case SND_SOC_DAPM_POST_PMU
:
1395 ev_name
= "POST_PMU";
1398 case SND_SOC_DAPM_PRE_PMD
:
1399 ev_name
= "PRE_PMD";
1402 case SND_SOC_DAPM_POST_PMD
:
1403 ev_name
= "POST_PMD";
1406 case SND_SOC_DAPM_WILL_PMU
:
1407 ev_name
= "WILL_PMU";
1410 case SND_SOC_DAPM_WILL_PMD
:
1411 ev_name
= "WILL_PMD";
1419 if (w
->new_power
!= power
)
1422 if (w
->event
&& (w
->event_flags
& event
)) {
1423 pop_dbg(w
->dapm
->dev
, card
->pop_time
, "pop test : %s %s\n",
1425 trace_snd_soc_dapm_widget_event_start(w
, event
);
1426 ret
= w
->event(w
, NULL
, event
);
1427 trace_snd_soc_dapm_widget_event_done(w
, event
);
1429 dev_err(w
->dapm
->dev
, "ASoC: %s: %s event failed: %d\n",
1430 ev_name
, w
->name
, ret
);
1434 /* Apply the coalesced changes from a DAPM sequence */
1435 static void dapm_seq_run_coalesced(struct snd_soc_card
*card
,
1436 struct list_head
*pending
)
1438 struct snd_soc_dapm_widget
*w
;
1440 unsigned int value
= 0;
1441 unsigned int mask
= 0;
1443 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
1446 list_for_each_entry(w
, pending
, power_list
) {
1447 BUG_ON(reg
!= w
->reg
);
1448 w
->power
= w
->new_power
;
1450 mask
|= w
->mask
<< w
->shift
;
1452 value
|= w
->on_val
<< w
->shift
;
1454 value
|= w
->off_val
<< w
->shift
;
1456 pop_dbg(w
->dapm
->dev
, card
->pop_time
,
1457 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1458 w
->name
, reg
, value
, mask
);
1460 /* Check for events */
1461 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_PRE_PMU
);
1462 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_PRE_PMD
);
1466 /* Any widget will do, they should all be updating the
1469 w
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
1472 pop_dbg(w
->dapm
->dev
, card
->pop_time
,
1473 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1474 value
, mask
, reg
, card
->pop_time
);
1475 pop_wait(card
->pop_time
);
1476 soc_widget_update_bits_locked(w
, reg
, mask
, value
);
1479 list_for_each_entry(w
, pending
, power_list
) {
1480 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_POST_PMU
);
1481 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_POST_PMD
);
1485 /* Apply a DAPM power sequence.
1487 * We walk over a pre-sorted list of widgets to apply power to. In
1488 * order to minimise the number of writes to the device required
1489 * multiple widgets will be updated in a single write where possible.
1490 * Currently anything that requires more than a single write is not
1493 static void dapm_seq_run(struct snd_soc_card
*card
,
1494 struct list_head
*list
, int event
, bool power_up
)
1496 struct snd_soc_dapm_widget
*w
, *n
;
1499 int cur_subseq
= -1;
1500 int cur_reg
= SND_SOC_NOPM
;
1501 struct snd_soc_dapm_context
*cur_dapm
= NULL
;
1508 sort
= dapm_down_seq
;
1510 list_for_each_entry_safe(w
, n
, list
, power_list
) {
1513 /* Do we need to apply any queued changes? */
1514 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
||
1515 w
->dapm
!= cur_dapm
|| w
->subseq
!= cur_subseq
) {
1516 if (!list_empty(&pending
))
1517 dapm_seq_run_coalesced(card
, &pending
);
1519 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1520 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1521 if (sort
[i
] == cur_sort
)
1522 cur_dapm
->seq_notifier(cur_dapm
,
1527 INIT_LIST_HEAD(&pending
);
1529 cur_subseq
= INT_MIN
;
1530 cur_reg
= SND_SOC_NOPM
;
1535 case snd_soc_dapm_pre
:
1537 list_for_each_entry_safe_continue(w
, n
, list
,
1540 if (event
== SND_SOC_DAPM_STREAM_START
)
1542 NULL
, SND_SOC_DAPM_PRE_PMU
);
1543 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1545 NULL
, SND_SOC_DAPM_PRE_PMD
);
1548 case snd_soc_dapm_post
:
1550 list_for_each_entry_safe_continue(w
, n
, list
,
1553 if (event
== SND_SOC_DAPM_STREAM_START
)
1555 NULL
, SND_SOC_DAPM_POST_PMU
);
1556 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1558 NULL
, SND_SOC_DAPM_POST_PMD
);
1562 /* Queue it up for application */
1563 cur_sort
= sort
[w
->id
];
1564 cur_subseq
= w
->subseq
;
1567 list_move(&w
->power_list
, &pending
);
1572 dev_err(w
->dapm
->dev
,
1573 "ASoC: Failed to apply widget power: %d\n", ret
);
1576 if (!list_empty(&pending
))
1577 dapm_seq_run_coalesced(card
, &pending
);
1579 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1580 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1581 if (sort
[i
] == cur_sort
)
1582 cur_dapm
->seq_notifier(cur_dapm
,
1587 static void dapm_widget_update(struct snd_soc_card
*card
)
1589 struct snd_soc_dapm_update
*update
= card
->update
;
1590 struct snd_soc_dapm_widget_list
*wlist
;
1591 struct snd_soc_dapm_widget
*w
= NULL
;
1595 if (!update
|| !dapm_kcontrol_is_powered(update
->kcontrol
))
1598 wlist
= dapm_kcontrol_get_wlist(update
->kcontrol
);
1600 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
1601 w
= wlist
->widgets
[wi
];
1603 if (w
->event
&& (w
->event_flags
& SND_SOC_DAPM_PRE_REG
)) {
1604 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_PRE_REG
);
1606 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM pre-event failed: %d\n",
1614 ret
= soc_widget_update_bits_locked(w
, update
->reg
, update
->mask
,
1617 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM update failed: %d\n",
1620 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
1621 w
= wlist
->widgets
[wi
];
1623 if (w
->event
&& (w
->event_flags
& SND_SOC_DAPM_POST_REG
)) {
1624 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_POST_REG
);
1626 dev_err(w
->dapm
->dev
, "ASoC: %s DAPM post-event failed: %d\n",
1632 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1633 * they're changing state.
1635 static void dapm_pre_sequence_async(void *data
, async_cookie_t cookie
)
1637 struct snd_soc_dapm_context
*d
= data
;
1640 /* If we're off and we're not supposed to be go into STANDBY */
1641 if (d
->bias_level
== SND_SOC_BIAS_OFF
&&
1642 d
->target_bias_level
!= SND_SOC_BIAS_OFF
) {
1644 pm_runtime_get_sync(d
->dev
);
1646 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1649 "ASoC: Failed to turn on bias: %d\n", ret
);
1652 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1653 if (d
->bias_level
!= d
->target_bias_level
) {
1654 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_PREPARE
);
1657 "ASoC: Failed to prepare bias: %d\n", ret
);
1661 /* Async callback run prior to DAPM sequences - brings to their final
1664 static void dapm_post_sequence_async(void *data
, async_cookie_t cookie
)
1666 struct snd_soc_dapm_context
*d
= data
;
1669 /* If we just powered the last thing off drop to standby bias */
1670 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1671 (d
->target_bias_level
== SND_SOC_BIAS_STANDBY
||
1672 d
->target_bias_level
== SND_SOC_BIAS_OFF
)) {
1673 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1675 dev_err(d
->dev
, "ASoC: Failed to apply standby bias: %d\n",
1679 /* If we're in standby and can support bias off then do that */
1680 if (d
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1681 d
->target_bias_level
== SND_SOC_BIAS_OFF
) {
1682 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_OFF
);
1684 dev_err(d
->dev
, "ASoC: Failed to turn off bias: %d\n",
1688 pm_runtime_put(d
->dev
);
1691 /* If we just powered up then move to active bias */
1692 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1693 d
->target_bias_level
== SND_SOC_BIAS_ON
) {
1694 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_ON
);
1696 dev_err(d
->dev
, "ASoC: Failed to apply active bias: %d\n",
1701 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget
*peer
,
1702 bool power
, bool connect
)
1704 /* If a connection is being made or broken then that update
1705 * will have marked the peer dirty, otherwise the widgets are
1706 * not connected and this update has no impact. */
1710 /* If the peer is already in the state we're moving to then we
1711 * won't have an impact on it. */
1712 if (power
!= peer
->power
)
1713 dapm_mark_dirty(peer
, "peer state change");
1716 static void dapm_widget_set_power(struct snd_soc_dapm_widget
*w
, bool power
,
1717 struct list_head
*up_list
,
1718 struct list_head
*down_list
)
1720 struct snd_soc_dapm_path
*path
;
1722 if (w
->power
== power
)
1725 trace_snd_soc_dapm_widget_power(w
, power
);
1727 /* If we changed our power state perhaps our neigbours changed
1730 list_for_each_entry(path
, &w
->sources
, list_sink
) {
1732 dapm_widget_set_peer_power(path
->source
, power
,
1737 case snd_soc_dapm_supply
:
1738 case snd_soc_dapm_regulator_supply
:
1739 case snd_soc_dapm_clock_supply
:
1740 case snd_soc_dapm_kcontrol
:
1741 /* Supplies can't affect their outputs, only their inputs */
1744 list_for_each_entry(path
, &w
->sinks
, list_source
) {
1746 dapm_widget_set_peer_power(path
->sink
, power
,
1754 dapm_seq_insert(w
, up_list
, true);
1756 dapm_seq_insert(w
, down_list
, false);
1759 static void dapm_power_one_widget(struct snd_soc_dapm_widget
*w
,
1760 struct list_head
*up_list
,
1761 struct list_head
*down_list
)
1766 case snd_soc_dapm_pre
:
1767 dapm_seq_insert(w
, down_list
, false);
1769 case snd_soc_dapm_post
:
1770 dapm_seq_insert(w
, up_list
, true);
1774 power
= dapm_widget_power_check(w
);
1776 dapm_widget_set_power(w
, power
, up_list
, down_list
);
1782 * Scan each dapm widget for complete audio path.
1783 * A complete path is a route that has valid endpoints i.e.:-
1785 * o DAC to output pin.
1786 * o Input Pin to ADC.
1787 * o Input pin to Output pin (bypass, sidetone)
1788 * o DAC to ADC (loopback).
1790 static int dapm_power_widgets(struct snd_soc_card
*card
, int event
)
1792 struct snd_soc_dapm_widget
*w
;
1793 struct snd_soc_dapm_context
*d
;
1795 LIST_HEAD(down_list
);
1796 ASYNC_DOMAIN_EXCLUSIVE(async_domain
);
1797 enum snd_soc_bias_level bias
;
1799 trace_snd_soc_dapm_start(card
);
1801 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1802 if (d
->idle_bias_off
)
1803 d
->target_bias_level
= SND_SOC_BIAS_OFF
;
1805 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1810 /* Check which widgets we need to power and store them in
1811 * lists indicating if they should be powered up or down. We
1812 * only check widgets that have been flagged as dirty but note
1813 * that new widgets may be added to the dirty list while we
1816 list_for_each_entry(w
, &card
->dapm_dirty
, dirty
) {
1817 dapm_power_one_widget(w
, &up_list
, &down_list
);
1820 list_for_each_entry(w
, &card
->widgets
, list
) {
1822 case snd_soc_dapm_pre
:
1823 case snd_soc_dapm_post
:
1824 /* These widgets always need to be powered */
1827 list_del_init(&w
->dirty
);
1834 /* Supplies and micbiases only bring the
1835 * context up to STANDBY as unless something
1836 * else is active and passing audio they
1837 * generally don't require full power. Signal
1838 * generators are virtual pins and have no
1839 * power impact themselves.
1842 case snd_soc_dapm_siggen
:
1844 case snd_soc_dapm_supply
:
1845 case snd_soc_dapm_regulator_supply
:
1846 case snd_soc_dapm_clock_supply
:
1847 case snd_soc_dapm_micbias
:
1848 if (d
->target_bias_level
< SND_SOC_BIAS_STANDBY
)
1849 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1852 d
->target_bias_level
= SND_SOC_BIAS_ON
;
1859 /* Force all contexts in the card to the same bias state if
1860 * they're not ground referenced.
1862 bias
= SND_SOC_BIAS_OFF
;
1863 list_for_each_entry(d
, &card
->dapm_list
, list
)
1864 if (d
->target_bias_level
> bias
)
1865 bias
= d
->target_bias_level
;
1866 list_for_each_entry(d
, &card
->dapm_list
, list
)
1867 if (!d
->idle_bias_off
)
1868 d
->target_bias_level
= bias
;
1870 trace_snd_soc_dapm_walk_done(card
);
1872 /* Run all the bias changes in parallel */
1873 list_for_each_entry(d
, &card
->dapm_list
, list
)
1874 async_schedule_domain(dapm_pre_sequence_async
, d
,
1876 async_synchronize_full_domain(&async_domain
);
1878 list_for_each_entry(w
, &down_list
, power_list
) {
1879 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_WILL_PMD
);
1882 list_for_each_entry(w
, &up_list
, power_list
) {
1883 dapm_seq_check_event(card
, w
, SND_SOC_DAPM_WILL_PMU
);
1886 /* Power down widgets first; try to avoid amplifying pops. */
1887 dapm_seq_run(card
, &down_list
, event
, false);
1889 dapm_widget_update(card
);
1892 dapm_seq_run(card
, &up_list
, event
, true);
1894 /* Run all the bias changes in parallel */
1895 list_for_each_entry(d
, &card
->dapm_list
, list
)
1896 async_schedule_domain(dapm_post_sequence_async
, d
,
1898 async_synchronize_full_domain(&async_domain
);
1900 /* do we need to notify any clients that DAPM event is complete */
1901 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1902 if (d
->stream_event
)
1903 d
->stream_event(d
, event
);
1906 pop_dbg(card
->dev
, card
->pop_time
,
1907 "DAPM sequencing finished, waiting %dms\n", card
->pop_time
);
1908 pop_wait(card
->pop_time
);
1910 trace_snd_soc_dapm_done(card
);
1915 #ifdef CONFIG_DEBUG_FS
1916 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1917 char __user
*user_buf
,
1918 size_t count
, loff_t
*ppos
)
1920 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1924 struct snd_soc_dapm_path
*p
= NULL
;
1926 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1930 in
= is_connected_input_ep(w
, NULL
);
1931 dapm_clear_walk_input(w
->dapm
, &w
->sources
);
1932 out
= is_connected_output_ep(w
, NULL
);
1933 dapm_clear_walk_output(w
->dapm
, &w
->sinks
);
1935 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s%s in %d out %d",
1936 w
->name
, w
->power
? "On" : "Off",
1937 w
->force
? " (forced)" : "", in
, out
);
1940 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1941 " - R%d(0x%x) mask 0x%x",
1942 w
->reg
, w
->reg
, w
->mask
<< w
->shift
);
1944 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
1947 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1949 w
->active
? "active" : "inactive");
1951 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1952 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1956 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1957 " in \"%s\" \"%s\"\n",
1958 p
->name
? p
->name
: "static",
1961 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1962 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1966 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1967 " out \"%s\" \"%s\"\n",
1968 p
->name
? p
->name
: "static",
1972 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1978 static const struct file_operations dapm_widget_power_fops
= {
1979 .open
= simple_open
,
1980 .read
= dapm_widget_power_read_file
,
1981 .llseek
= default_llseek
,
1984 static ssize_t
dapm_bias_read_file(struct file
*file
, char __user
*user_buf
,
1985 size_t count
, loff_t
*ppos
)
1987 struct snd_soc_dapm_context
*dapm
= file
->private_data
;
1990 switch (dapm
->bias_level
) {
1991 case SND_SOC_BIAS_ON
:
1994 case SND_SOC_BIAS_PREPARE
:
1995 level
= "Prepare\n";
1997 case SND_SOC_BIAS_STANDBY
:
1998 level
= "Standby\n";
2000 case SND_SOC_BIAS_OFF
:
2005 level
= "Unknown\n";
2009 return simple_read_from_buffer(user_buf
, count
, ppos
, level
,
2013 static const struct file_operations dapm_bias_fops
= {
2014 .open
= simple_open
,
2015 .read
= dapm_bias_read_file
,
2016 .llseek
= default_llseek
,
2019 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
2020 struct dentry
*parent
)
2024 dapm
->debugfs_dapm
= debugfs_create_dir("dapm", parent
);
2026 if (!dapm
->debugfs_dapm
) {
2028 "ASoC: Failed to create DAPM debugfs directory\n");
2032 d
= debugfs_create_file("bias_level", 0444,
2033 dapm
->debugfs_dapm
, dapm
,
2037 "ASoC: Failed to create bias level debugfs file\n");
2040 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
2042 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
2045 if (!dapm
->debugfs_dapm
|| !w
->name
)
2048 d
= debugfs_create_file(w
->name
, 0444,
2049 dapm
->debugfs_dapm
, w
,
2050 &dapm_widget_power_fops
);
2052 dev_warn(w
->dapm
->dev
,
2053 "ASoC: Failed to create %s debugfs file\n",
2057 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
2059 debugfs_remove_recursive(dapm
->debugfs_dapm
);
2063 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
2064 struct dentry
*parent
)
2068 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
2072 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
2078 /* test and update the power status of a mux widget */
2079 static int soc_dapm_mux_update_power(struct snd_soc_card
*card
,
2080 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
)
2082 struct snd_soc_dapm_path
*path
;
2085 /* find dapm widget path assoc with kcontrol */
2086 dapm_kcontrol_for_each_path(path
, kcontrol
) {
2087 if (!path
->name
|| !e
->texts
[mux
])
2091 /* we now need to match the string in the enum to the path */
2092 if (!(strcmp(path
->name
, e
->texts
[mux
]))) {
2093 path
->connect
= 1; /* new connection */
2094 dapm_mark_dirty(path
->source
, "mux connection");
2097 dapm_mark_dirty(path
->source
,
2098 "mux disconnection");
2099 path
->connect
= 0; /* old connection must be powered down */
2101 dapm_mark_dirty(path
->sink
, "mux change");
2105 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2110 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context
*dapm
,
2111 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
,
2112 struct snd_soc_dapm_update
*update
)
2114 struct snd_soc_card
*card
= dapm
->card
;
2117 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2118 card
->update
= update
;
2119 ret
= soc_dapm_mux_update_power(card
, kcontrol
, mux
, e
);
2120 card
->update
= NULL
;
2121 mutex_unlock(&card
->dapm_mutex
);
2123 soc_dpcm_runtime_update(card
);
2126 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power
);
2128 /* test and update the power status of a mixer or switch widget */
2129 static int soc_dapm_mixer_update_power(struct snd_soc_card
*card
,
2130 struct snd_kcontrol
*kcontrol
, int connect
)
2132 struct snd_soc_dapm_path
*path
;
2135 /* find dapm widget path assoc with kcontrol */
2136 dapm_kcontrol_for_each_path(path
, kcontrol
) {
2138 path
->connect
= connect
;
2139 dapm_mark_dirty(path
->source
, "mixer connection");
2140 dapm_mark_dirty(path
->sink
, "mixer update");
2144 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2149 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context
*dapm
,
2150 struct snd_kcontrol
*kcontrol
, int connect
,
2151 struct snd_soc_dapm_update
*update
)
2153 struct snd_soc_card
*card
= dapm
->card
;
2156 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2157 card
->update
= update
;
2158 ret
= soc_dapm_mixer_update_power(card
, kcontrol
, connect
);
2159 card
->update
= NULL
;
2160 mutex_unlock(&card
->dapm_mutex
);
2162 soc_dpcm_runtime_update(card
);
2165 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power
);
2167 /* show dapm widget status in sys fs */
2168 static ssize_t
dapm_widget_show(struct device
*dev
,
2169 struct device_attribute
*attr
, char *buf
)
2171 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
2172 struct snd_soc_codec
*codec
=rtd
->codec
;
2173 struct snd_soc_dapm_widget
*w
;
2175 char *state
= "not set";
2177 list_for_each_entry(w
, &codec
->card
->widgets
, list
) {
2178 if (w
->dapm
!= &codec
->dapm
)
2181 /* only display widgets that burnm power */
2183 case snd_soc_dapm_hp
:
2184 case snd_soc_dapm_mic
:
2185 case snd_soc_dapm_spk
:
2186 case snd_soc_dapm_line
:
2187 case snd_soc_dapm_micbias
:
2188 case snd_soc_dapm_dac
:
2189 case snd_soc_dapm_adc
:
2190 case snd_soc_dapm_pga
:
2191 case snd_soc_dapm_out_drv
:
2192 case snd_soc_dapm_mixer
:
2193 case snd_soc_dapm_mixer_named_ctl
:
2194 case snd_soc_dapm_supply
:
2195 case snd_soc_dapm_regulator_supply
:
2196 case snd_soc_dapm_clock_supply
:
2198 count
+= sprintf(buf
+ count
, "%s: %s\n",
2199 w
->name
, w
->power
? "On":"Off");
2206 switch (codec
->dapm
.bias_level
) {
2207 case SND_SOC_BIAS_ON
:
2210 case SND_SOC_BIAS_PREPARE
:
2213 case SND_SOC_BIAS_STANDBY
:
2216 case SND_SOC_BIAS_OFF
:
2220 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
2225 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
2227 int snd_soc_dapm_sys_add(struct device
*dev
)
2229 return device_create_file(dev
, &dev_attr_dapm_widget
);
2232 static void snd_soc_dapm_sys_remove(struct device
*dev
)
2234 device_remove_file(dev
, &dev_attr_dapm_widget
);
2237 static void dapm_free_path(struct snd_soc_dapm_path
*path
)
2239 list_del(&path
->list_sink
);
2240 list_del(&path
->list_source
);
2241 list_del(&path
->list_kcontrol
);
2242 list_del(&path
->list
);
2246 /* free all dapm widgets and resources */
2247 static void dapm_free_widgets(struct snd_soc_dapm_context
*dapm
)
2249 struct snd_soc_dapm_widget
*w
, *next_w
;
2250 struct snd_soc_dapm_path
*p
, *next_p
;
2252 list_for_each_entry_safe(w
, next_w
, &dapm
->card
->widgets
, list
) {
2253 if (w
->dapm
!= dapm
)
2257 * remove source and sink paths associated to this widget.
2258 * While removing the path, remove reference to it from both
2259 * source and sink widgets so that path is removed only once.
2261 list_for_each_entry_safe(p
, next_p
, &w
->sources
, list_sink
)
2264 list_for_each_entry_safe(p
, next_p
, &w
->sinks
, list_source
)
2267 kfree(w
->kcontrols
);
2273 static struct snd_soc_dapm_widget
*dapm_find_widget(
2274 struct snd_soc_dapm_context
*dapm
, const char *pin
,
2275 bool search_other_contexts
)
2277 struct snd_soc_dapm_widget
*w
;
2278 struct snd_soc_dapm_widget
*fallback
= NULL
;
2280 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
2281 if (!strcmp(w
->name
, pin
)) {
2282 if (w
->dapm
== dapm
)
2289 if (search_other_contexts
)
2295 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context
*dapm
,
2296 const char *pin
, int status
)
2298 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
2301 dev_err(dapm
->dev
, "ASoC: DAPM unknown pin %s\n", pin
);
2305 if (w
->connected
!= status
)
2306 dapm_mark_dirty(w
, "pin configuration");
2308 w
->connected
= status
;
2316 * snd_soc_dapm_sync - scan and power dapm paths
2317 * @dapm: DAPM context
2319 * Walks all dapm audio paths and powers widgets according to their
2320 * stream or path usage.
2322 * Returns 0 for success.
2324 int snd_soc_dapm_sync(struct snd_soc_dapm_context
*dapm
)
2329 * Suppress early reports (eg, jacks syncing their state) to avoid
2330 * silly DAPM runs during card startup.
2332 if (!dapm
->card
|| !dapm
->card
->instantiated
)
2335 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2336 ret
= dapm_power_widgets(dapm
->card
, SND_SOC_DAPM_STREAM_NOP
);
2337 mutex_unlock(&dapm
->card
->dapm_mutex
);
2340 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
2342 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context
*dapm
,
2343 struct snd_soc_dapm_widget
*wsource
, struct snd_soc_dapm_widget
*wsink
,
2344 const char *control
,
2345 int (*connected
)(struct snd_soc_dapm_widget
*source
,
2346 struct snd_soc_dapm_widget
*sink
))
2348 struct snd_soc_dapm_path
*path
;
2351 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
2355 path
->source
= wsource
;
2357 path
->connected
= connected
;
2358 INIT_LIST_HEAD(&path
->list
);
2359 INIT_LIST_HEAD(&path
->list_kcontrol
);
2360 INIT_LIST_HEAD(&path
->list_source
);
2361 INIT_LIST_HEAD(&path
->list_sink
);
2363 /* check for external widgets */
2364 if (wsink
->id
== snd_soc_dapm_input
) {
2365 if (wsource
->id
== snd_soc_dapm_micbias
||
2366 wsource
->id
== snd_soc_dapm_mic
||
2367 wsource
->id
== snd_soc_dapm_line
||
2368 wsource
->id
== snd_soc_dapm_output
)
2371 if (wsource
->id
== snd_soc_dapm_output
) {
2372 if (wsink
->id
== snd_soc_dapm_spk
||
2373 wsink
->id
== snd_soc_dapm_hp
||
2374 wsink
->id
== snd_soc_dapm_line
||
2375 wsink
->id
== snd_soc_dapm_input
)
2379 dapm_mark_dirty(wsource
, "Route added");
2380 dapm_mark_dirty(wsink
, "Route added");
2382 /* connect static paths */
2383 if (control
== NULL
) {
2384 list_add(&path
->list
, &dapm
->card
->paths
);
2385 list_add(&path
->list_sink
, &wsink
->sources
);
2386 list_add(&path
->list_source
, &wsource
->sinks
);
2391 /* connect dynamic paths */
2392 switch (wsink
->id
) {
2393 case snd_soc_dapm_adc
:
2394 case snd_soc_dapm_dac
:
2395 case snd_soc_dapm_pga
:
2396 case snd_soc_dapm_out_drv
:
2397 case snd_soc_dapm_input
:
2398 case snd_soc_dapm_output
:
2399 case snd_soc_dapm_siggen
:
2400 case snd_soc_dapm_micbias
:
2401 case snd_soc_dapm_vmid
:
2402 case snd_soc_dapm_pre
:
2403 case snd_soc_dapm_post
:
2404 case snd_soc_dapm_supply
:
2405 case snd_soc_dapm_regulator_supply
:
2406 case snd_soc_dapm_clock_supply
:
2407 case snd_soc_dapm_aif_in
:
2408 case snd_soc_dapm_aif_out
:
2409 case snd_soc_dapm_dai_in
:
2410 case snd_soc_dapm_dai_out
:
2411 case snd_soc_dapm_dai_link
:
2412 case snd_soc_dapm_kcontrol
:
2413 list_add(&path
->list
, &dapm
->card
->paths
);
2414 list_add(&path
->list_sink
, &wsink
->sources
);
2415 list_add(&path
->list_source
, &wsource
->sinks
);
2418 case snd_soc_dapm_mux
:
2419 case snd_soc_dapm_virt_mux
:
2420 case snd_soc_dapm_value_mux
:
2421 ret
= dapm_connect_mux(dapm
, wsource
, wsink
, path
, control
,
2422 &wsink
->kcontrol_news
[0]);
2426 case snd_soc_dapm_switch
:
2427 case snd_soc_dapm_mixer
:
2428 case snd_soc_dapm_mixer_named_ctl
:
2429 ret
= dapm_connect_mixer(dapm
, wsource
, wsink
, path
, control
);
2433 case snd_soc_dapm_hp
:
2434 case snd_soc_dapm_mic
:
2435 case snd_soc_dapm_line
:
2436 case snd_soc_dapm_spk
:
2437 list_add(&path
->list
, &dapm
->card
->paths
);
2438 list_add(&path
->list_sink
, &wsink
->sources
);
2439 list_add(&path
->list_source
, &wsource
->sinks
);
2450 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context
*dapm
,
2451 const struct snd_soc_dapm_route
*route
)
2453 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
2454 struct snd_soc_dapm_widget
*wtsource
= NULL
, *wtsink
= NULL
;
2457 char prefixed_sink
[80];
2458 char prefixed_source
[80];
2461 if (dapm
->codec
&& dapm
->codec
->name_prefix
) {
2462 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
2463 dapm
->codec
->name_prefix
, route
->sink
);
2464 sink
= prefixed_sink
;
2465 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
2466 dapm
->codec
->name_prefix
, route
->source
);
2467 source
= prefixed_source
;
2470 source
= route
->source
;
2474 * find src and dest widgets over all widgets but favor a widget from
2475 * current DAPM context
2477 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
2478 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
2480 if (w
->dapm
== dapm
)
2484 if (!wsource
&& !(strcmp(w
->name
, source
))) {
2486 if (w
->dapm
== dapm
)
2490 /* use widget from another DAPM context if not found from this */
2496 if (wsource
== NULL
) {
2497 dev_err(dapm
->dev
, "ASoC: no source widget found for %s\n",
2501 if (wsink
== NULL
) {
2502 dev_err(dapm
->dev
, "ASoC: no sink widget found for %s\n",
2507 ret
= snd_soc_dapm_add_path(dapm
, wsource
, wsink
, route
->control
,
2514 dev_warn(dapm
->dev
, "ASoC: no dapm match for %s --> %s --> %s\n",
2515 source
, route
->control
, sink
);
2519 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context
*dapm
,
2520 const struct snd_soc_dapm_route
*route
)
2522 struct snd_soc_dapm_path
*path
, *p
;
2525 char prefixed_sink
[80];
2526 char prefixed_source
[80];
2528 if (route
->control
) {
2530 "ASoC: Removal of routes with controls not supported\n");
2534 if (dapm
->codec
&& dapm
->codec
->name_prefix
) {
2535 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
2536 dapm
->codec
->name_prefix
, route
->sink
);
2537 sink
= prefixed_sink
;
2538 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
2539 dapm
->codec
->name_prefix
, route
->source
);
2540 source
= prefixed_source
;
2543 source
= route
->source
;
2547 list_for_each_entry(p
, &dapm
->card
->paths
, list
) {
2548 if (strcmp(p
->source
->name
, source
) != 0)
2550 if (strcmp(p
->sink
->name
, sink
) != 0)
2557 dapm_mark_dirty(path
->source
, "Route removed");
2558 dapm_mark_dirty(path
->sink
, "Route removed");
2560 dapm_free_path(path
);
2562 dev_warn(dapm
->dev
, "ASoC: Route %s->%s does not exist\n",
2570 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2571 * @dapm: DAPM context
2572 * @route: audio routes
2573 * @num: number of routes
2575 * Connects 2 dapm widgets together via a named audio path. The sink is
2576 * the widget receiving the audio signal, whilst the source is the sender
2577 * of the audio signal.
2579 * Returns 0 for success else error. On error all resources can be freed
2580 * with a call to snd_soc_card_free().
2582 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context
*dapm
,
2583 const struct snd_soc_dapm_route
*route
, int num
)
2587 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2588 for (i
= 0; i
< num
; i
++) {
2589 r
= snd_soc_dapm_add_route(dapm
, route
);
2591 dev_err(dapm
->dev
, "ASoC: Failed to add route %s -> %s -> %s\n",
2593 route
->control
? route
->control
: "direct",
2599 mutex_unlock(&dapm
->card
->dapm_mutex
);
2603 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
2606 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2607 * @dapm: DAPM context
2608 * @route: audio routes
2609 * @num: number of routes
2611 * Removes routes from the DAPM context.
2613 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context
*dapm
,
2614 const struct snd_soc_dapm_route
*route
, int num
)
2618 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2619 for (i
= 0; i
< num
; i
++) {
2620 snd_soc_dapm_del_route(dapm
, route
);
2623 mutex_unlock(&dapm
->card
->dapm_mutex
);
2627 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes
);
2629 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context
*dapm
,
2630 const struct snd_soc_dapm_route
*route
)
2632 struct snd_soc_dapm_widget
*source
= dapm_find_widget(dapm
,
2635 struct snd_soc_dapm_widget
*sink
= dapm_find_widget(dapm
,
2638 struct snd_soc_dapm_path
*path
;
2642 dev_err(dapm
->dev
, "ASoC: Unable to find source %s for weak route\n",
2648 dev_err(dapm
->dev
, "ASoC: Unable to find sink %s for weak route\n",
2653 if (route
->control
|| route
->connected
)
2654 dev_warn(dapm
->dev
, "ASoC: Ignoring control for weak route %s->%s\n",
2655 route
->source
, route
->sink
);
2657 list_for_each_entry(path
, &source
->sinks
, list_source
) {
2658 if (path
->sink
== sink
) {
2665 dev_err(dapm
->dev
, "ASoC: No path found for weak route %s->%s\n",
2666 route
->source
, route
->sink
);
2668 dev_warn(dapm
->dev
, "ASoC: %d paths found for weak route %s->%s\n",
2669 count
, route
->source
, route
->sink
);
2675 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2676 * @dapm: DAPM context
2677 * @route: audio routes
2678 * @num: number of routes
2680 * Mark existing routes matching those specified in the passed array
2681 * as being weak, meaning that they are ignored for the purpose of
2682 * power decisions. The main intended use case is for sidetone paths
2683 * which couple audio between other independent paths if they are both
2684 * active in order to make the combination work better at the user
2685 * level but which aren't intended to be "used".
2687 * Note that CODEC drivers should not use this as sidetone type paths
2688 * can frequently also be used as bypass paths.
2690 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context
*dapm
,
2691 const struct snd_soc_dapm_route
*route
, int num
)
2696 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2697 for (i
= 0; i
< num
; i
++) {
2698 err
= snd_soc_dapm_weak_route(dapm
, route
);
2703 mutex_unlock(&dapm
->card
->dapm_mutex
);
2707 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes
);
2710 * snd_soc_dapm_new_widgets - add new dapm widgets
2711 * @dapm: DAPM context
2713 * Checks the codec for any new dapm widgets and creates them if found.
2715 * Returns 0 for success.
2717 int snd_soc_dapm_new_widgets(struct snd_soc_card
*card
)
2719 struct snd_soc_dapm_widget
*w
;
2722 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2724 list_for_each_entry(w
, &card
->widgets
, list
)
2729 if (w
->num_kcontrols
) {
2730 w
->kcontrols
= kzalloc(w
->num_kcontrols
*
2731 sizeof(struct snd_kcontrol
*),
2733 if (!w
->kcontrols
) {
2734 mutex_unlock(&card
->dapm_mutex
);
2740 case snd_soc_dapm_switch
:
2741 case snd_soc_dapm_mixer
:
2742 case snd_soc_dapm_mixer_named_ctl
:
2745 case snd_soc_dapm_mux
:
2746 case snd_soc_dapm_virt_mux
:
2747 case snd_soc_dapm_value_mux
:
2750 case snd_soc_dapm_pga
:
2751 case snd_soc_dapm_out_drv
:
2758 /* Read the initial power state from the device */
2760 val
= soc_widget_read(w
, w
->reg
) >> w
->shift
;
2762 if (val
== w
->on_val
)
2768 dapm_mark_dirty(w
, "new widget");
2769 dapm_debugfs_add_widget(w
);
2772 dapm_power_widgets(card
, SND_SOC_DAPM_STREAM_NOP
);
2773 mutex_unlock(&card
->dapm_mutex
);
2776 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
2779 * snd_soc_dapm_get_volsw - dapm mixer get callback
2780 * @kcontrol: mixer control
2781 * @ucontrol: control element information
2783 * Callback to get the value of a dapm mixer control.
2785 * Returns 0 for success.
2787 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
2788 struct snd_ctl_elem_value
*ucontrol
)
2790 struct snd_soc_codec
*codec
= snd_soc_dapm_kcontrol_codec(kcontrol
);
2791 struct snd_soc_card
*card
= codec
->card
;
2792 struct soc_mixer_control
*mc
=
2793 (struct soc_mixer_control
*)kcontrol
->private_value
;
2794 unsigned int reg
= mc
->reg
;
2795 unsigned int shift
= mc
->shift
;
2797 unsigned int mask
= (1 << fls(max
)) - 1;
2798 unsigned int invert
= mc
->invert
;
2801 if (snd_soc_volsw_is_stereo(mc
))
2802 dev_warn(codec
->dapm
.dev
,
2803 "ASoC: Control '%s' is stereo, which is not supported\n",
2806 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2807 if (dapm_kcontrol_is_powered(kcontrol
))
2808 val
= (snd_soc_read(codec
, reg
) >> shift
) & mask
;
2810 val
= dapm_kcontrol_get_value(kcontrol
);
2811 mutex_unlock(&card
->dapm_mutex
);
2814 ucontrol
->value
.integer
.value
[0] = max
- val
;
2816 ucontrol
->value
.integer
.value
[0] = val
;
2820 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
2823 * snd_soc_dapm_put_volsw - dapm mixer set callback
2824 * @kcontrol: mixer control
2825 * @ucontrol: control element information
2827 * Callback to set the value of a dapm mixer control.
2829 * Returns 0 for success.
2831 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
2832 struct snd_ctl_elem_value
*ucontrol
)
2834 struct snd_soc_codec
*codec
= snd_soc_dapm_kcontrol_codec(kcontrol
);
2835 struct snd_soc_card
*card
= codec
->card
;
2836 struct soc_mixer_control
*mc
=
2837 (struct soc_mixer_control
*)kcontrol
->private_value
;
2838 unsigned int reg
= mc
->reg
;
2839 unsigned int shift
= mc
->shift
;
2841 unsigned int mask
= (1 << fls(max
)) - 1;
2842 unsigned int invert
= mc
->invert
;
2844 int connect
, change
;
2845 struct snd_soc_dapm_update update
;
2847 if (snd_soc_volsw_is_stereo(mc
))
2848 dev_warn(codec
->dapm
.dev
,
2849 "ASoC: Control '%s' is stereo, which is not supported\n",
2852 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2858 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2860 dapm_kcontrol_set_value(kcontrol
, val
);
2862 mask
= mask
<< shift
;
2865 change
= snd_soc_test_bits(codec
, reg
, mask
, val
);
2867 update
.kcontrol
= kcontrol
;
2872 card
->update
= &update
;
2874 soc_dapm_mixer_update_power(card
, kcontrol
, connect
);
2876 card
->update
= NULL
;
2879 mutex_unlock(&card
->dapm_mutex
);
2882 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
2885 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2886 * @kcontrol: mixer control
2887 * @ucontrol: control element information
2889 * Callback to get the value of a dapm enumerated double mixer control.
2891 * Returns 0 for success.
2893 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
2894 struct snd_ctl_elem_value
*ucontrol
)
2896 struct snd_soc_codec
*codec
= snd_soc_dapm_kcontrol_codec(kcontrol
);
2897 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2900 val
= snd_soc_read(codec
, e
->reg
);
2901 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & e
->mask
;
2902 if (e
->shift_l
!= e
->shift_r
)
2903 ucontrol
->value
.enumerated
.item
[1] =
2904 (val
>> e
->shift_r
) & e
->mask
;
2908 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
2911 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2912 * @kcontrol: mixer control
2913 * @ucontrol: control element information
2915 * Callback to set the value of a dapm enumerated double mixer control.
2917 * Returns 0 for success.
2919 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
2920 struct snd_ctl_elem_value
*ucontrol
)
2922 struct snd_soc_codec
*codec
= snd_soc_dapm_kcontrol_codec(kcontrol
);
2923 struct snd_soc_card
*card
= codec
->card
;
2924 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2925 unsigned int val
, mux
, change
;
2927 struct snd_soc_dapm_update update
;
2929 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2931 mux
= ucontrol
->value
.enumerated
.item
[0];
2932 val
= mux
<< e
->shift_l
;
2933 mask
= e
->mask
<< e
->shift_l
;
2934 if (e
->shift_l
!= e
->shift_r
) {
2935 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2937 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
2938 mask
|= e
->mask
<< e
->shift_r
;
2941 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2943 change
= snd_soc_test_bits(codec
, e
->reg
, mask
, val
);
2945 update
.kcontrol
= kcontrol
;
2946 update
.reg
= e
->reg
;
2949 card
->update
= &update
;
2951 soc_dapm_mux_update_power(card
, kcontrol
, mux
, e
);
2953 card
->update
= NULL
;
2956 mutex_unlock(&card
->dapm_mutex
);
2959 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
2962 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2963 * @kcontrol: mixer control
2964 * @ucontrol: control element information
2966 * Returns 0 for success.
2968 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol
*kcontrol
,
2969 struct snd_ctl_elem_value
*ucontrol
)
2971 ucontrol
->value
.enumerated
.item
[0] = dapm_kcontrol_get_value(kcontrol
);
2974 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt
);
2977 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2978 * @kcontrol: mixer control
2979 * @ucontrol: control element information
2981 * Returns 0 for success.
2983 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol
*kcontrol
,
2984 struct snd_ctl_elem_value
*ucontrol
)
2986 struct snd_soc_codec
*codec
= snd_soc_dapm_kcontrol_codec(kcontrol
);
2987 struct snd_soc_card
*card
= codec
->card
;
2989 struct soc_enum
*e
=
2990 (struct soc_enum
*)kcontrol
->private_value
;
2993 if (ucontrol
->value
.enumerated
.item
[0] >= e
->max
)
2996 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2998 value
= ucontrol
->value
.enumerated
.item
[0];
2999 change
= dapm_kcontrol_set_value(kcontrol
, value
);
3001 soc_dapm_mux_update_power(card
, kcontrol
, value
, e
);
3003 mutex_unlock(&card
->dapm_mutex
);
3006 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt
);
3009 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
3011 * @kcontrol: mixer control
3012 * @ucontrol: control element information
3014 * Callback to get the value of a dapm semi enumerated double mixer control.
3016 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3017 * used for handling bitfield coded enumeration for example.
3019 * Returns 0 for success.
3021 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
3022 struct snd_ctl_elem_value
*ucontrol
)
3024 struct snd_soc_codec
*codec
= snd_soc_dapm_kcontrol_codec(kcontrol
);
3025 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
3026 unsigned int reg_val
, val
, mux
;
3028 reg_val
= snd_soc_read(codec
, e
->reg
);
3029 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
3030 for (mux
= 0; mux
< e
->max
; mux
++) {
3031 if (val
== e
->values
[mux
])
3034 ucontrol
->value
.enumerated
.item
[0] = mux
;
3035 if (e
->shift_l
!= e
->shift_r
) {
3036 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
3037 for (mux
= 0; mux
< e
->max
; mux
++) {
3038 if (val
== e
->values
[mux
])
3041 ucontrol
->value
.enumerated
.item
[1] = mux
;
3046 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double
);
3049 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
3051 * @kcontrol: mixer control
3052 * @ucontrol: control element information
3054 * Callback to set the value of a dapm semi enumerated double mixer control.
3056 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3057 * used for handling bitfield coded enumeration for example.
3059 * Returns 0 for success.
3061 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
3062 struct snd_ctl_elem_value
*ucontrol
)
3064 struct snd_soc_codec
*codec
= snd_soc_dapm_kcontrol_codec(kcontrol
);
3065 struct snd_soc_card
*card
= codec
->card
;
3066 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
3067 unsigned int val
, mux
, change
;
3069 struct snd_soc_dapm_update update
;
3071 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
3073 mux
= ucontrol
->value
.enumerated
.item
[0];
3074 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
3075 mask
= e
->mask
<< e
->shift_l
;
3076 if (e
->shift_l
!= e
->shift_r
) {
3077 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
3079 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
3080 mask
|= e
->mask
<< e
->shift_r
;
3083 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3085 change
= snd_soc_test_bits(codec
, e
->reg
, mask
, val
);
3087 update
.kcontrol
= kcontrol
;
3088 update
.reg
= e
->reg
;
3091 card
->update
= &update
;
3093 soc_dapm_mux_update_power(card
, kcontrol
, mux
, e
);
3095 card
->update
= NULL
;
3098 mutex_unlock(&card
->dapm_mutex
);
3101 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double
);
3104 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3106 * @kcontrol: mixer control
3107 * @uinfo: control element information
3109 * Callback to provide information about a pin switch control.
3111 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
3112 struct snd_ctl_elem_info
*uinfo
)
3114 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
3116 uinfo
->value
.integer
.min
= 0;
3117 uinfo
->value
.integer
.max
= 1;
3121 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
3124 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3126 * @kcontrol: mixer control
3129 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
3130 struct snd_ctl_elem_value
*ucontrol
)
3132 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
3133 const char *pin
= (const char *)kcontrol
->private_value
;
3135 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3137 ucontrol
->value
.integer
.value
[0] =
3138 snd_soc_dapm_get_pin_status(&card
->dapm
, pin
);
3140 mutex_unlock(&card
->dapm_mutex
);
3144 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
3147 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3149 * @kcontrol: mixer control
3152 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
3153 struct snd_ctl_elem_value
*ucontrol
)
3155 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
3156 const char *pin
= (const char *)kcontrol
->private_value
;
3158 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3160 if (ucontrol
->value
.integer
.value
[0])
3161 snd_soc_dapm_enable_pin(&card
->dapm
, pin
);
3163 snd_soc_dapm_disable_pin(&card
->dapm
, pin
);
3165 mutex_unlock(&card
->dapm_mutex
);
3167 snd_soc_dapm_sync(&card
->dapm
);
3170 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
3172 static struct snd_soc_dapm_widget
*
3173 snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
3174 const struct snd_soc_dapm_widget
*widget
)
3176 struct snd_soc_dapm_widget
*w
;
3179 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
3183 case snd_soc_dapm_regulator_supply
:
3184 w
->regulator
= devm_regulator_get(dapm
->dev
, w
->name
);
3185 if (IS_ERR(w
->regulator
)) {
3186 ret
= PTR_ERR(w
->regulator
);
3187 dev_err(dapm
->dev
, "ASoC: Failed to request %s: %d\n",
3192 if (w
->on_val
& SND_SOC_DAPM_REGULATOR_BYPASS
) {
3193 ret
= regulator_allow_bypass(w
->regulator
, true);
3195 dev_warn(w
->dapm
->dev
,
3196 "ASoC: Failed to unbypass %s: %d\n",
3200 case snd_soc_dapm_clock_supply
:
3201 #ifdef CONFIG_CLKDEV_LOOKUP
3202 w
->clk
= devm_clk_get(dapm
->dev
, w
->name
);
3203 if (IS_ERR(w
->clk
)) {
3204 ret
= PTR_ERR(w
->clk
);
3205 dev_err(dapm
->dev
, "ASoC: Failed to request %s: %d\n",
3217 if (dapm
->codec
&& dapm
->codec
->name_prefix
)
3218 w
->name
= kasprintf(GFP_KERNEL
, "%s %s",
3219 dapm
->codec
->name_prefix
, widget
->name
);
3221 w
->name
= kasprintf(GFP_KERNEL
, "%s", widget
->name
);
3223 if (w
->name
== NULL
) {
3229 case snd_soc_dapm_switch
:
3230 case snd_soc_dapm_mixer
:
3231 case snd_soc_dapm_mixer_named_ctl
:
3232 w
->power_check
= dapm_generic_check_power
;
3234 case snd_soc_dapm_mux
:
3235 case snd_soc_dapm_virt_mux
:
3236 case snd_soc_dapm_value_mux
:
3237 w
->power_check
= dapm_generic_check_power
;
3239 case snd_soc_dapm_dai_out
:
3240 w
->power_check
= dapm_adc_check_power
;
3242 case snd_soc_dapm_dai_in
:
3243 w
->power_check
= dapm_dac_check_power
;
3245 case snd_soc_dapm_adc
:
3246 case snd_soc_dapm_aif_out
:
3247 case snd_soc_dapm_dac
:
3248 case snd_soc_dapm_aif_in
:
3249 case snd_soc_dapm_pga
:
3250 case snd_soc_dapm_out_drv
:
3251 case snd_soc_dapm_input
:
3252 case snd_soc_dapm_output
:
3253 case snd_soc_dapm_micbias
:
3254 case snd_soc_dapm_spk
:
3255 case snd_soc_dapm_hp
:
3256 case snd_soc_dapm_mic
:
3257 case snd_soc_dapm_line
:
3258 case snd_soc_dapm_dai_link
:
3259 w
->power_check
= dapm_generic_check_power
;
3261 case snd_soc_dapm_supply
:
3262 case snd_soc_dapm_regulator_supply
:
3263 case snd_soc_dapm_clock_supply
:
3264 case snd_soc_dapm_kcontrol
:
3265 w
->power_check
= dapm_supply_check_power
;
3268 w
->power_check
= dapm_always_on_check_power
;
3273 w
->codec
= dapm
->codec
;
3274 w
->platform
= dapm
->platform
;
3275 INIT_LIST_HEAD(&w
->sources
);
3276 INIT_LIST_HEAD(&w
->sinks
);
3277 INIT_LIST_HEAD(&w
->list
);
3278 INIT_LIST_HEAD(&w
->dirty
);
3279 list_add(&w
->list
, &dapm
->card
->widgets
);
3281 /* machine layer set ups unconnected pins and insertions */
3287 * snd_soc_dapm_new_controls - create new dapm controls
3288 * @dapm: DAPM context
3289 * @widget: widget array
3290 * @num: number of widgets
3292 * Creates new DAPM controls based upon the templates.
3294 * Returns 0 for success else error.
3296 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context
*dapm
,
3297 const struct snd_soc_dapm_widget
*widget
,
3300 struct snd_soc_dapm_widget
*w
;
3304 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
3305 for (i
= 0; i
< num
; i
++) {
3306 w
= snd_soc_dapm_new_control(dapm
, widget
);
3309 "ASoC: Failed to create DAPM control %s\n",
3316 mutex_unlock(&dapm
->card
->dapm_mutex
);
3319 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
3321 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget
*w
,
3322 struct snd_kcontrol
*kcontrol
, int event
)
3324 struct snd_soc_dapm_path
*source_p
, *sink_p
;
3325 struct snd_soc_dai
*source
, *sink
;
3326 const struct snd_soc_pcm_stream
*config
= w
->params
;
3327 struct snd_pcm_substream substream
;
3328 struct snd_pcm_hw_params
*params
= NULL
;
3333 BUG_ON(list_empty(&w
->sources
) || list_empty(&w
->sinks
));
3335 /* We only support a single source and sink, pick the first */
3336 source_p
= list_first_entry(&w
->sources
, struct snd_soc_dapm_path
,
3338 sink_p
= list_first_entry(&w
->sinks
, struct snd_soc_dapm_path
,
3341 BUG_ON(!source_p
|| !sink_p
);
3342 BUG_ON(!sink_p
->source
|| !source_p
->sink
);
3343 BUG_ON(!source_p
->source
|| !sink_p
->sink
);
3345 source
= source_p
->source
->priv
;
3346 sink
= sink_p
->sink
->priv
;
3348 /* Be a little careful as we don't want to overflow the mask array */
3349 if (config
->formats
) {
3350 fmt
= ffs(config
->formats
) - 1;
3352 dev_warn(w
->dapm
->dev
, "ASoC: Invalid format %llx specified\n",
3357 /* Currently very limited parameter selection */
3358 params
= kzalloc(sizeof(*params
), GFP_KERNEL
);
3363 snd_mask_set(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), fmt
);
3365 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
)->min
=
3367 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
)->max
=
3370 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
)->min
3371 = config
->channels_min
;
3372 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
)->max
3373 = config
->channels_max
;
3375 memset(&substream
, 0, sizeof(substream
));
3378 case SND_SOC_DAPM_PRE_PMU
:
3379 if (source
->driver
->ops
&& source
->driver
->ops
->hw_params
) {
3380 substream
.stream
= SNDRV_PCM_STREAM_CAPTURE
;
3381 ret
= source
->driver
->ops
->hw_params(&substream
,
3384 dev_err(source
->dev
,
3385 "ASoC: hw_params() failed: %d\n", ret
);
3390 if (sink
->driver
->ops
&& sink
->driver
->ops
->hw_params
) {
3391 substream
.stream
= SNDRV_PCM_STREAM_PLAYBACK
;
3392 ret
= sink
->driver
->ops
->hw_params(&substream
, params
,
3396 "ASoC: hw_params() failed: %d\n", ret
);
3402 case SND_SOC_DAPM_POST_PMU
:
3403 ret
= snd_soc_dai_digital_mute(sink
, 0,
3404 SNDRV_PCM_STREAM_PLAYBACK
);
3405 if (ret
!= 0 && ret
!= -ENOTSUPP
)
3406 dev_warn(sink
->dev
, "ASoC: Failed to unmute: %d\n", ret
);
3410 case SND_SOC_DAPM_PRE_PMD
:
3411 ret
= snd_soc_dai_digital_mute(sink
, 1,
3412 SNDRV_PCM_STREAM_PLAYBACK
);
3413 if (ret
!= 0 && ret
!= -ENOTSUPP
)
3414 dev_warn(sink
->dev
, "ASoC: Failed to mute: %d\n", ret
);
3428 int snd_soc_dapm_new_pcm(struct snd_soc_card
*card
,
3429 const struct snd_soc_pcm_stream
*params
,
3430 struct snd_soc_dapm_widget
*source
,
3431 struct snd_soc_dapm_widget
*sink
)
3433 struct snd_soc_dapm_route routes
[2];
3434 struct snd_soc_dapm_widget
template;
3435 struct snd_soc_dapm_widget
*w
;
3439 len
= strlen(source
->name
) + strlen(sink
->name
) + 2;
3440 link_name
= devm_kzalloc(card
->dev
, len
, GFP_KERNEL
);
3443 snprintf(link_name
, len
, "%s-%s", source
->name
, sink
->name
);
3445 memset(&template, 0, sizeof(template));
3446 template.reg
= SND_SOC_NOPM
;
3447 template.id
= snd_soc_dapm_dai_link
;
3448 template.name
= link_name
;
3449 template.event
= snd_soc_dai_link_event
;
3450 template.event_flags
= SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMU
|
3451 SND_SOC_DAPM_PRE_PMD
;
3453 dev_dbg(card
->dev
, "ASoC: adding %s widget\n", link_name
);
3455 w
= snd_soc_dapm_new_control(&card
->dapm
, &template);
3457 dev_err(card
->dev
, "ASoC: Failed to create %s widget\n",
3464 memset(&routes
, 0, sizeof(routes
));
3466 routes
[0].source
= source
->name
;
3467 routes
[0].sink
= link_name
;
3468 routes
[1].source
= link_name
;
3469 routes
[1].sink
= sink
->name
;
3471 return snd_soc_dapm_add_routes(&card
->dapm
, routes
,
3472 ARRAY_SIZE(routes
));
3475 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context
*dapm
,
3476 struct snd_soc_dai
*dai
)
3478 struct snd_soc_dapm_widget
template;
3479 struct snd_soc_dapm_widget
*w
;
3481 WARN_ON(dapm
->dev
!= dai
->dev
);
3483 memset(&template, 0, sizeof(template));
3484 template.reg
= SND_SOC_NOPM
;
3486 if (dai
->driver
->playback
.stream_name
) {
3487 template.id
= snd_soc_dapm_dai_in
;
3488 template.name
= dai
->driver
->playback
.stream_name
;
3489 template.sname
= dai
->driver
->playback
.stream_name
;
3491 dev_dbg(dai
->dev
, "ASoC: adding %s widget\n",
3494 w
= snd_soc_dapm_new_control(dapm
, &template);
3496 dev_err(dapm
->dev
, "ASoC: Failed to create %s widget\n",
3497 dai
->driver
->playback
.stream_name
);
3501 dai
->playback_widget
= w
;
3504 if (dai
->driver
->capture
.stream_name
) {
3505 template.id
= snd_soc_dapm_dai_out
;
3506 template.name
= dai
->driver
->capture
.stream_name
;
3507 template.sname
= dai
->driver
->capture
.stream_name
;
3509 dev_dbg(dai
->dev
, "ASoC: adding %s widget\n",
3512 w
= snd_soc_dapm_new_control(dapm
, &template);
3514 dev_err(dapm
->dev
, "ASoC: Failed to create %s widget\n",
3515 dai
->driver
->capture
.stream_name
);
3519 dai
->capture_widget
= w
;
3525 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card
*card
)
3527 struct snd_soc_dapm_widget
*dai_w
, *w
;
3528 struct snd_soc_dai
*dai
;
3530 /* For each DAI widget... */
3531 list_for_each_entry(dai_w
, &card
->widgets
, list
) {
3532 switch (dai_w
->id
) {
3533 case snd_soc_dapm_dai_in
:
3534 case snd_soc_dapm_dai_out
:
3542 /* ...find all widgets with the same stream and link them */
3543 list_for_each_entry(w
, &card
->widgets
, list
) {
3544 if (w
->dapm
!= dai_w
->dapm
)
3548 case snd_soc_dapm_dai_in
:
3549 case snd_soc_dapm_dai_out
:
3555 if (!w
->sname
|| !strstr(w
->sname
, dai_w
->name
))
3558 if (dai
->driver
->playback
.stream_name
&&
3560 dai
->driver
->playback
.stream_name
)) {
3561 dev_dbg(dai
->dev
, "%s -> %s\n",
3562 dai
->playback_widget
->name
, w
->name
);
3564 snd_soc_dapm_add_path(w
->dapm
,
3565 dai
->playback_widget
, w
, NULL
, NULL
);
3568 if (dai
->driver
->capture
.stream_name
&&
3570 dai
->driver
->capture
.stream_name
)) {
3571 dev_dbg(dai
->dev
, "%s -> %s\n",
3572 w
->name
, dai
->capture_widget
->name
);
3574 snd_soc_dapm_add_path(w
->dapm
, w
,
3575 dai
->capture_widget
, NULL
, NULL
);
3583 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
, int stream
,
3587 struct snd_soc_dapm_widget
*w_cpu
, *w_codec
;
3588 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
3589 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
3591 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3592 w_cpu
= cpu_dai
->playback_widget
;
3593 w_codec
= codec_dai
->playback_widget
;
3595 w_cpu
= cpu_dai
->capture_widget
;
3596 w_codec
= codec_dai
->capture_widget
;
3601 dapm_mark_dirty(w_cpu
, "stream event");
3604 case SND_SOC_DAPM_STREAM_START
:
3607 case SND_SOC_DAPM_STREAM_STOP
:
3610 case SND_SOC_DAPM_STREAM_SUSPEND
:
3611 case SND_SOC_DAPM_STREAM_RESUME
:
3612 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
3613 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
3620 dapm_mark_dirty(w_codec
, "stream event");
3623 case SND_SOC_DAPM_STREAM_START
:
3624 w_codec
->active
= 1;
3626 case SND_SOC_DAPM_STREAM_STOP
:
3627 w_codec
->active
= 0;
3629 case SND_SOC_DAPM_STREAM_SUSPEND
:
3630 case SND_SOC_DAPM_STREAM_RESUME
:
3631 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
3632 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
3637 dapm_power_widgets(rtd
->card
, event
);
3641 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3642 * @rtd: PCM runtime data
3643 * @stream: stream name
3644 * @event: stream event
3646 * Sends a stream event to the dapm core. The core then makes any
3647 * necessary widget power changes.
3649 * Returns 0 for success else error.
3651 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
, int stream
,
3654 struct snd_soc_card
*card
= rtd
->card
;
3656 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3657 soc_dapm_stream_event(rtd
, stream
, event
);
3658 mutex_unlock(&card
->dapm_mutex
);
3662 * snd_soc_dapm_enable_pin - enable pin.
3663 * @dapm: DAPM context
3666 * Enables input/output pin and its parents or children widgets iff there is
3667 * a valid audio route and active audio stream.
3668 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3669 * do any widget power switching.
3671 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
3673 return snd_soc_dapm_set_pin(dapm
, pin
, 1);
3675 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
3678 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3679 * @dapm: DAPM context
3682 * Enables input/output pin regardless of any other state. This is
3683 * intended for use with microphone bias supplies used in microphone
3686 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3687 * do any widget power switching.
3689 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context
*dapm
,
3692 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
3695 dev_err(dapm
->dev
, "ASoC: unknown pin %s\n", pin
);
3699 dev_dbg(w
->dapm
->dev
, "ASoC: force enable pin %s\n", pin
);
3702 dapm_mark_dirty(w
, "force enable");
3706 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
3709 * snd_soc_dapm_disable_pin - disable pin.
3710 * @dapm: DAPM context
3713 * Disables input/output pin and its parents or children widgets.
3714 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3715 * do any widget power switching.
3717 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context
*dapm
,
3720 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
3722 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
3725 * snd_soc_dapm_nc_pin - permanently disable pin.
3726 * @dapm: DAPM context
3729 * Marks the specified pin as being not connected, disabling it along
3730 * any parent or child widgets. At present this is identical to
3731 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3732 * additional things such as disabling controls which only affect
3733 * paths through the pin.
3735 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3736 * do any widget power switching.
3738 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
3740 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
3742 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
3745 * snd_soc_dapm_get_pin_status - get audio pin status
3746 * @dapm: DAPM context
3747 * @pin: audio signal pin endpoint (or start point)
3749 * Get audio pin status - connected or disconnected.
3751 * Returns 1 for connected otherwise 0.
3753 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context
*dapm
,
3756 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
3759 return w
->connected
;
3763 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
3766 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3767 * @dapm: DAPM context
3768 * @pin: audio signal pin endpoint (or start point)
3770 * Mark the given endpoint or pin as ignoring suspend. When the
3771 * system is disabled a path between two endpoints flagged as ignoring
3772 * suspend will not be disabled. The path must already be enabled via
3773 * normal means at suspend time, it will not be turned on if it was not
3776 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context
*dapm
,
3779 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, false);
3782 dev_err(dapm
->dev
, "ASoC: unknown pin %s\n", pin
);
3786 w
->ignore_suspend
= 1;
3790 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
3792 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card
*card
,
3793 struct snd_soc_dapm_widget
*w
)
3795 struct snd_soc_dapm_path
*p
;
3797 list_for_each_entry(p
, &card
->paths
, list
) {
3798 if ((p
->source
== w
) || (p
->sink
== w
)) {
3800 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3801 p
->source
->name
, p
->source
->id
, p
->source
->dapm
,
3802 p
->sink
->name
, p
->sink
->id
, p
->sink
->dapm
);
3804 /* Connected to something other than the codec */
3805 if (p
->source
->dapm
!= p
->sink
->dapm
)
3808 * Loopback connection from codec external pin to
3809 * codec external pin
3811 if (p
->sink
->id
== snd_soc_dapm_input
) {
3812 switch (p
->source
->id
) {
3813 case snd_soc_dapm_output
:
3814 case snd_soc_dapm_micbias
:
3827 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3828 * @codec: The codec whose pins should be processed
3830 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3831 * which are unused. Pins are used if they are connected externally to the
3832 * codec, whether that be to some other device, or a loop-back connection to
3835 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec
*codec
)
3837 struct snd_soc_card
*card
= codec
->card
;
3838 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
3839 struct snd_soc_dapm_widget
*w
;
3841 dev_dbg(codec
->dev
, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
3842 &card
->dapm
, &codec
->dapm
);
3844 list_for_each_entry(w
, &card
->widgets
, list
) {
3845 if (w
->dapm
!= dapm
)
3848 case snd_soc_dapm_input
:
3849 case snd_soc_dapm_output
:
3850 case snd_soc_dapm_micbias
:
3851 dev_dbg(codec
->dev
, "ASoC: Auto NC: Checking widget %s\n",
3853 if (!snd_soc_dapm_widget_in_card_paths(card
, w
)) {
3855 "... Not in map; disabling\n");
3856 snd_soc_dapm_nc_pin(dapm
, w
->name
);
3866 * snd_soc_dapm_free - free dapm resources
3867 * @dapm: DAPM context
3869 * Free all dapm widgets and resources.
3871 void snd_soc_dapm_free(struct snd_soc_dapm_context
*dapm
)
3873 snd_soc_dapm_sys_remove(dapm
->dev
);
3874 dapm_debugfs_cleanup(dapm
);
3875 dapm_free_widgets(dapm
);
3876 list_del(&dapm
->list
);
3878 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
3880 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context
*dapm
)
3882 struct snd_soc_card
*card
= dapm
->card
;
3883 struct snd_soc_dapm_widget
*w
;
3884 LIST_HEAD(down_list
);
3887 mutex_lock(&card
->dapm_mutex
);
3889 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
3890 if (w
->dapm
!= dapm
)
3893 dapm_seq_insert(w
, &down_list
, false);
3899 /* If there were no widgets to power down we're already in
3903 if (dapm
->bias_level
== SND_SOC_BIAS_ON
)
3904 snd_soc_dapm_set_bias_level(dapm
,
3905 SND_SOC_BIAS_PREPARE
);
3906 dapm_seq_run(card
, &down_list
, 0, false);
3907 if (dapm
->bias_level
== SND_SOC_BIAS_PREPARE
)
3908 snd_soc_dapm_set_bias_level(dapm
,
3909 SND_SOC_BIAS_STANDBY
);
3912 mutex_unlock(&card
->dapm_mutex
);
3916 * snd_soc_dapm_shutdown - callback for system shutdown
3918 void snd_soc_dapm_shutdown(struct snd_soc_card
*card
)
3920 struct snd_soc_codec
*codec
;
3922 list_for_each_entry(codec
, &card
->codec_dev_list
, card_list
) {
3923 soc_dapm_shutdown_codec(&codec
->dapm
);
3924 if (codec
->dapm
.bias_level
== SND_SOC_BIAS_STANDBY
)
3925 snd_soc_dapm_set_bias_level(&codec
->dapm
,
3930 /* Module information */
3931 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3932 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3933 MODULE_LICENSE("GPL");