2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/headphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed power down of audio subsystem to reduce pops between a quick
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
29 #include <linux/async.h>
30 #include <linux/delay.h>
32 #include <linux/bitops.h>
33 #include <linux/platform_device.h>
34 #include <linux/jiffies.h>
35 #include <linux/debugfs.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/slab.h>
39 #include <sound/core.h>
40 #include <sound/pcm.h>
41 #include <sound/pcm_params.h>
42 #include <sound/soc.h>
43 #include <sound/initval.h>
45 #include <trace/events/asoc.h>
47 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
49 /* dapm power sequences - make this per codec in the future */
50 static int dapm_up_seq
[] = {
51 [snd_soc_dapm_pre
] = 0,
52 [snd_soc_dapm_supply
] = 1,
53 [snd_soc_dapm_regulator_supply
] = 1,
54 [snd_soc_dapm_micbias
] = 2,
55 [snd_soc_dapm_dai_link
] = 2,
56 [snd_soc_dapm_dai
] = 3,
57 [snd_soc_dapm_aif_in
] = 3,
58 [snd_soc_dapm_aif_out
] = 3,
59 [snd_soc_dapm_mic
] = 4,
60 [snd_soc_dapm_mux
] = 5,
61 [snd_soc_dapm_virt_mux
] = 5,
62 [snd_soc_dapm_value_mux
] = 5,
63 [snd_soc_dapm_dac
] = 6,
64 [snd_soc_dapm_mixer
] = 7,
65 [snd_soc_dapm_mixer_named_ctl
] = 7,
66 [snd_soc_dapm_pga
] = 8,
67 [snd_soc_dapm_adc
] = 9,
68 [snd_soc_dapm_out_drv
] = 10,
69 [snd_soc_dapm_hp
] = 10,
70 [snd_soc_dapm_spk
] = 10,
71 [snd_soc_dapm_line
] = 10,
72 [snd_soc_dapm_post
] = 11,
75 static int dapm_down_seq
[] = {
76 [snd_soc_dapm_pre
] = 0,
77 [snd_soc_dapm_adc
] = 1,
78 [snd_soc_dapm_hp
] = 2,
79 [snd_soc_dapm_spk
] = 2,
80 [snd_soc_dapm_line
] = 2,
81 [snd_soc_dapm_out_drv
] = 2,
82 [snd_soc_dapm_pga
] = 4,
83 [snd_soc_dapm_mixer_named_ctl
] = 5,
84 [snd_soc_dapm_mixer
] = 5,
85 [snd_soc_dapm_dac
] = 6,
86 [snd_soc_dapm_mic
] = 7,
87 [snd_soc_dapm_micbias
] = 8,
88 [snd_soc_dapm_mux
] = 9,
89 [snd_soc_dapm_virt_mux
] = 9,
90 [snd_soc_dapm_value_mux
] = 9,
91 [snd_soc_dapm_aif_in
] = 10,
92 [snd_soc_dapm_aif_out
] = 10,
93 [snd_soc_dapm_dai
] = 10,
94 [snd_soc_dapm_dai_link
] = 11,
95 [snd_soc_dapm_regulator_supply
] = 12,
96 [snd_soc_dapm_supply
] = 12,
97 [snd_soc_dapm_post
] = 13,
100 static void pop_wait(u32 pop_time
)
103 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time
));
106 static void pop_dbg(struct device
*dev
, u32 pop_time
, const char *fmt
, ...)
114 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
119 vsnprintf(buf
, PAGE_SIZE
, fmt
, args
);
120 dev_info(dev
, "%s", buf
);
126 static bool dapm_dirty_widget(struct snd_soc_dapm_widget
*w
)
128 return !list_empty(&w
->dirty
);
131 void dapm_mark_dirty(struct snd_soc_dapm_widget
*w
, const char *reason
)
133 if (!dapm_dirty_widget(w
)) {
134 dev_vdbg(w
->dapm
->dev
, "Marking %s dirty due to %s\n",
136 list_add_tail(&w
->dirty
, &w
->dapm
->card
->dapm_dirty
);
139 EXPORT_SYMBOL_GPL(dapm_mark_dirty
);
141 /* create a new dapm widget */
142 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
143 const struct snd_soc_dapm_widget
*_widget
)
145 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
148 /* get snd_card from DAPM context */
149 static inline struct snd_card
*dapm_get_snd_card(
150 struct snd_soc_dapm_context
*dapm
)
153 return dapm
->codec
->card
->snd_card
;
154 else if (dapm
->platform
)
155 return dapm
->platform
->card
->snd_card
;
163 /* get soc_card from DAPM context */
164 static inline struct snd_soc_card
*dapm_get_soc_card(
165 struct snd_soc_dapm_context
*dapm
)
168 return dapm
->codec
->card
;
169 else if (dapm
->platform
)
170 return dapm
->platform
->card
;
178 static void dapm_reset(struct snd_soc_card
*card
)
180 struct snd_soc_dapm_widget
*w
;
182 memset(&card
->dapm_stats
, 0, sizeof(card
->dapm_stats
));
184 list_for_each_entry(w
, &card
->widgets
, list
) {
185 w
->power_checked
= false;
191 static int soc_widget_read(struct snd_soc_dapm_widget
*w
, int reg
)
194 return snd_soc_read(w
->codec
, reg
);
195 else if (w
->platform
)
196 return snd_soc_platform_read(w
->platform
, reg
);
198 dev_err(w
->dapm
->dev
, "no valid widget read method\n");
202 static int soc_widget_write(struct snd_soc_dapm_widget
*w
, int reg
, int val
)
205 return snd_soc_write(w
->codec
, reg
, val
);
206 else if (w
->platform
)
207 return snd_soc_platform_write(w
->platform
, reg
, val
);
209 dev_err(w
->dapm
->dev
, "no valid widget write method\n");
213 static inline void soc_widget_lock(struct snd_soc_dapm_widget
*w
)
215 if (w
->codec
&& !w
->codec
->using_regmap
)
216 mutex_lock(&w
->codec
->mutex
);
217 else if (w
->platform
)
218 mutex_lock(&w
->platform
->mutex
);
221 static inline void soc_widget_unlock(struct snd_soc_dapm_widget
*w
)
223 if (w
->codec
&& !w
->codec
->using_regmap
)
224 mutex_unlock(&w
->codec
->mutex
);
225 else if (w
->platform
)
226 mutex_unlock(&w
->platform
->mutex
);
229 static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget
*w
,
230 unsigned short reg
, unsigned int mask
, unsigned int value
)
233 unsigned int old
, new;
236 if (w
->codec
&& w
->codec
->using_regmap
) {
237 ret
= regmap_update_bits_check(w
->codec
->control_data
,
238 reg
, mask
, value
, &change
);
243 ret
= soc_widget_read(w
, reg
);
245 soc_widget_unlock(w
);
250 new = (old
& ~mask
) | (value
& mask
);
253 ret
= soc_widget_write(w
, reg
, new);
255 soc_widget_unlock(w
);
259 soc_widget_unlock(w
);
266 * snd_soc_dapm_set_bias_level - set the bias level for the system
267 * @dapm: DAPM context
268 * @level: level to configure
270 * Configure the bias (power) levels for the SoC audio device.
272 * Returns 0 for success else error.
274 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context
*dapm
,
275 enum snd_soc_bias_level level
)
277 struct snd_soc_card
*card
= dapm
->card
;
280 trace_snd_soc_bias_level_start(card
, level
);
282 if (card
&& card
->set_bias_level
)
283 ret
= card
->set_bias_level(card
, dapm
, level
);
288 if (dapm
->codec
->driver
->set_bias_level
)
289 ret
= dapm
->codec
->driver
->set_bias_level(dapm
->codec
,
292 dapm
->bias_level
= level
;
297 if (card
&& card
->set_bias_level_post
)
298 ret
= card
->set_bias_level_post(card
, dapm
, level
);
300 trace_snd_soc_bias_level_done(card
, level
);
305 /* set up initial codec paths */
306 static void dapm_set_path_status(struct snd_soc_dapm_widget
*w
,
307 struct snd_soc_dapm_path
*p
, int i
)
310 case snd_soc_dapm_switch
:
311 case snd_soc_dapm_mixer
:
312 case snd_soc_dapm_mixer_named_ctl
: {
314 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
315 w
->kcontrol_news
[i
].private_value
;
316 unsigned int reg
= mc
->reg
;
317 unsigned int shift
= mc
->shift
;
319 unsigned int mask
= (1 << fls(max
)) - 1;
320 unsigned int invert
= mc
->invert
;
322 val
= soc_widget_read(w
, reg
);
323 val
= (val
>> shift
) & mask
;
330 case snd_soc_dapm_mux
: {
331 struct soc_enum
*e
= (struct soc_enum
*)
332 w
->kcontrol_news
[i
].private_value
;
333 int val
, item
, bitmask
;
335 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
337 val
= soc_widget_read(w
, e
->reg
);
338 item
= (val
>> e
->shift_l
) & (bitmask
- 1);
341 for (i
= 0; i
< e
->max
; i
++) {
342 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
347 case snd_soc_dapm_virt_mux
: {
348 struct soc_enum
*e
= (struct soc_enum
*)
349 w
->kcontrol_news
[i
].private_value
;
352 /* since a virtual mux has no backing registers to
353 * decide which path to connect, it will try to match
354 * with the first enumeration. This is to ensure
355 * that the default mux choice (the first) will be
356 * correctly powered up during initialization.
358 if (!strcmp(p
->name
, e
->texts
[0]))
362 case snd_soc_dapm_value_mux
: {
363 struct soc_enum
*e
= (struct soc_enum
*)
364 w
->kcontrol_news
[i
].private_value
;
367 val
= soc_widget_read(w
, e
->reg
);
368 val
= (val
>> e
->shift_l
) & e
->mask
;
369 for (item
= 0; item
< e
->max
; item
++) {
370 if (val
== e
->values
[item
])
375 for (i
= 0; i
< e
->max
; i
++) {
376 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
381 /* does not affect routing - always connected */
382 case snd_soc_dapm_pga
:
383 case snd_soc_dapm_out_drv
:
384 case snd_soc_dapm_output
:
385 case snd_soc_dapm_adc
:
386 case snd_soc_dapm_input
:
387 case snd_soc_dapm_siggen
:
388 case snd_soc_dapm_dac
:
389 case snd_soc_dapm_micbias
:
390 case snd_soc_dapm_vmid
:
391 case snd_soc_dapm_supply
:
392 case snd_soc_dapm_regulator_supply
:
393 case snd_soc_dapm_aif_in
:
394 case snd_soc_dapm_aif_out
:
395 case snd_soc_dapm_dai
:
396 case snd_soc_dapm_hp
:
397 case snd_soc_dapm_mic
:
398 case snd_soc_dapm_spk
:
399 case snd_soc_dapm_line
:
400 case snd_soc_dapm_dai_link
:
403 /* does affect routing - dynamically connected */
404 case snd_soc_dapm_pre
:
405 case snd_soc_dapm_post
:
411 /* connect mux widget to its interconnecting audio paths */
412 static int dapm_connect_mux(struct snd_soc_dapm_context
*dapm
,
413 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
414 struct snd_soc_dapm_path
*path
, const char *control_name
,
415 const struct snd_kcontrol_new
*kcontrol
)
417 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
420 for (i
= 0; i
< e
->max
; i
++) {
421 if (!(strcmp(control_name
, e
->texts
[i
]))) {
422 list_add(&path
->list
, &dapm
->card
->paths
);
423 list_add(&path
->list_sink
, &dest
->sources
);
424 list_add(&path
->list_source
, &src
->sinks
);
425 path
->name
= (char*)e
->texts
[i
];
426 dapm_set_path_status(dest
, path
, 0);
434 /* connect mixer widget to its interconnecting audio paths */
435 static int dapm_connect_mixer(struct snd_soc_dapm_context
*dapm
,
436 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
437 struct snd_soc_dapm_path
*path
, const char *control_name
)
441 /* search for mixer kcontrol */
442 for (i
= 0; i
< dest
->num_kcontrols
; i
++) {
443 if (!strcmp(control_name
, dest
->kcontrol_news
[i
].name
)) {
444 list_add(&path
->list
, &dapm
->card
->paths
);
445 list_add(&path
->list_sink
, &dest
->sources
);
446 list_add(&path
->list_source
, &src
->sinks
);
447 path
->name
= dest
->kcontrol_news
[i
].name
;
448 dapm_set_path_status(dest
, path
, i
);
455 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context
*dapm
,
456 struct snd_soc_dapm_widget
*kcontrolw
,
457 const struct snd_kcontrol_new
*kcontrol_new
,
458 struct snd_kcontrol
**kcontrol
)
460 struct snd_soc_dapm_widget
*w
;
465 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
466 if (w
== kcontrolw
|| w
->dapm
!= kcontrolw
->dapm
)
468 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
469 if (&w
->kcontrol_news
[i
] == kcontrol_new
) {
471 *kcontrol
= w
->kcontrols
[i
];
480 /* create new dapm mixer control */
481 static int dapm_new_mixer(struct snd_soc_dapm_widget
*w
)
483 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
485 size_t name_len
, prefix_len
;
486 struct snd_soc_dapm_path
*path
;
487 struct snd_card
*card
= dapm
->card
->snd_card
;
489 struct snd_soc_dapm_widget_list
*wlist
;
493 prefix
= dapm
->codec
->name_prefix
;
498 prefix_len
= strlen(prefix
) + 1;
503 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
506 list_for_each_entry(path
, &w
->sources
, list_sink
) {
508 /* mixer/mux paths name must match control name */
509 if (path
->name
!= (char *)w
->kcontrol_news
[i
].name
)
512 if (w
->kcontrols
[i
]) {
513 path
->kcontrol
= w
->kcontrols
[i
];
517 wlistsize
= sizeof(struct snd_soc_dapm_widget_list
) +
518 sizeof(struct snd_soc_dapm_widget
*),
519 wlist
= kzalloc(wlistsize
, GFP_KERNEL
);
522 "asoc: can't allocate widget list for %s\n",
526 wlist
->num_widgets
= 1;
527 wlist
->widgets
[0] = w
;
529 /* add dapm control with long name.
530 * for dapm_mixer this is the concatenation of the
531 * mixer and kcontrol name.
532 * for dapm_mixer_named_ctl this is simply the
535 name_len
= strlen(w
->kcontrol_news
[i
].name
) + 1;
536 if (w
->id
!= snd_soc_dapm_mixer_named_ctl
)
537 name_len
+= 1 + strlen(w
->name
);
539 path
->long_name
= kmalloc(name_len
, GFP_KERNEL
);
541 if (path
->long_name
== NULL
) {
548 /* The control will get a prefix from
549 * the control creation process but
550 * we're also using the same prefix
551 * for widgets so cut the prefix off
552 * the front of the widget name.
554 snprintf((char *)path
->long_name
, name_len
,
555 "%s %s", w
->name
+ prefix_len
,
556 w
->kcontrol_news
[i
].name
);
558 case snd_soc_dapm_mixer_named_ctl
:
559 snprintf((char *)path
->long_name
, name_len
,
560 "%s", w
->kcontrol_news
[i
].name
);
564 ((char *)path
->long_name
)[name_len
- 1] = '\0';
566 path
->kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[i
],
567 wlist
, path
->long_name
,
569 ret
= snd_ctl_add(card
, path
->kcontrol
);
572 "asoc: failed to add dapm kcontrol %s: %d\n",
573 path
->long_name
, ret
);
575 kfree(path
->long_name
);
576 path
->long_name
= NULL
;
579 w
->kcontrols
[i
] = path
->kcontrol
;
585 /* create new dapm mux control */
586 static int dapm_new_mux(struct snd_soc_dapm_widget
*w
)
588 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
589 struct snd_soc_dapm_path
*path
= NULL
;
590 struct snd_kcontrol
*kcontrol
;
591 struct snd_card
*card
= dapm
->card
->snd_card
;
595 struct snd_soc_dapm_widget_list
*wlist
;
596 int shared
, wlistentries
;
600 if (w
->num_kcontrols
!= 1) {
602 "asoc: mux %s has incorrect number of controls\n",
607 shared
= dapm_is_shared_kcontrol(dapm
, w
, &w
->kcontrol_news
[0],
610 wlist
= kcontrol
->private_data
;
611 wlistentries
= wlist
->num_widgets
+ 1;
616 wlistsize
= sizeof(struct snd_soc_dapm_widget_list
) +
617 wlistentries
* sizeof(struct snd_soc_dapm_widget
*),
618 wlist
= krealloc(wlist
, wlistsize
, GFP_KERNEL
);
621 "asoc: can't allocate widget list for %s\n", w
->name
);
624 wlist
->num_widgets
= wlistentries
;
625 wlist
->widgets
[wlistentries
- 1] = w
;
629 prefix
= dapm
->codec
->name_prefix
;
634 name
= w
->kcontrol_news
[0].name
;
639 prefix_len
= strlen(prefix
) + 1;
645 * The control will get a prefix from the control creation
646 * process but we're also using the same prefix for widgets so
647 * cut the prefix off the front of the widget name.
649 kcontrol
= snd_soc_cnew(&w
->kcontrol_news
[0], wlist
,
650 name
+ prefix_len
, prefix
);
651 ret
= snd_ctl_add(card
, kcontrol
);
653 dev_err(dapm
->dev
, "failed to add kcontrol %s: %d\n",
660 kcontrol
->private_data
= wlist
;
662 w
->kcontrols
[0] = kcontrol
;
664 list_for_each_entry(path
, &w
->sources
, list_sink
)
665 path
->kcontrol
= kcontrol
;
670 /* create new dapm volume control */
671 static int dapm_new_pga(struct snd_soc_dapm_widget
*w
)
673 if (w
->num_kcontrols
)
674 dev_err(w
->dapm
->dev
,
675 "asoc: PGA controls not supported: '%s'\n", w
->name
);
680 /* reset 'walked' bit for each dapm path */
681 static inline void dapm_clear_walk(struct snd_soc_dapm_context
*dapm
)
683 struct snd_soc_dapm_path
*p
;
685 list_for_each_entry(p
, &dapm
->card
->paths
, list
)
689 /* We implement power down on suspend by checking the power state of
690 * the ALSA card - when we are suspending the ALSA state for the card
693 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
695 int level
= snd_power_get_state(widget
->dapm
->card
->snd_card
);
698 case SNDRV_CTL_POWER_D3hot
:
699 case SNDRV_CTL_POWER_D3cold
:
700 if (widget
->ignore_suspend
)
701 dev_dbg(widget
->dapm
->dev
, "%s ignoring suspend\n",
703 return widget
->ignore_suspend
;
709 /* add widget to list if it's not already in the list */
710 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list
**list
,
711 struct snd_soc_dapm_widget
*w
)
713 struct snd_soc_dapm_widget_list
*wlist
;
714 int wlistsize
, wlistentries
, i
;
721 /* is this widget already in the list */
722 for (i
= 0; i
< wlist
->num_widgets
; i
++) {
723 if (wlist
->widgets
[i
] == w
)
727 /* allocate some new space */
728 wlistentries
= wlist
->num_widgets
+ 1;
729 wlistsize
= sizeof(struct snd_soc_dapm_widget_list
) +
730 wlistentries
* sizeof(struct snd_soc_dapm_widget
*);
731 *list
= krealloc(wlist
, wlistsize
, GFP_KERNEL
);
733 dev_err(w
->dapm
->dev
, "can't allocate widget list for %s\n",
739 /* insert the widget */
740 dev_dbg(w
->dapm
->dev
, "added %s in widget list pos %d\n",
741 w
->name
, wlist
->num_widgets
);
743 wlist
->widgets
[wlist
->num_widgets
] = w
;
744 wlist
->num_widgets
++;
749 * Recursively check for a completed path to an active or physically connected
750 * output widget. Returns number of complete paths.
752 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
,
753 struct snd_soc_dapm_widget_list
**list
)
755 struct snd_soc_dapm_path
*path
;
758 if (widget
->outputs
>= 0)
759 return widget
->outputs
;
761 DAPM_UPDATE_STAT(widget
, path_checks
);
763 switch (widget
->id
) {
764 case snd_soc_dapm_supply
:
765 case snd_soc_dapm_regulator_supply
:
771 switch (widget
->id
) {
772 case snd_soc_dapm_adc
:
773 case snd_soc_dapm_aif_out
:
774 case snd_soc_dapm_dai
:
775 if (widget
->active
) {
776 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
777 return widget
->outputs
;
783 if (widget
->connected
) {
784 /* connected pin ? */
785 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
) {
786 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
787 return widget
->outputs
;
790 /* connected jack or spk ? */
791 if (widget
->id
== snd_soc_dapm_hp
||
792 widget
->id
== snd_soc_dapm_spk
||
793 (widget
->id
== snd_soc_dapm_line
&&
794 !list_empty(&widget
->sources
))) {
795 widget
->outputs
= snd_soc_dapm_suspend_check(widget
);
796 return widget
->outputs
;
800 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
801 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
809 trace_snd_soc_dapm_output_path(widget
, path
);
811 if (path
->sink
&& path
->connect
) {
814 /* do we need to add this widget to the list ? */
817 err
= dapm_list_add_widget(list
, path
->sink
);
819 dev_err(widget
->dapm
->dev
, "could not add widget %s\n",
825 con
+= is_connected_output_ep(path
->sink
, list
);
829 widget
->outputs
= con
;
835 * Recursively check for a completed path to an active or physically connected
836 * input widget. Returns number of complete paths.
838 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
,
839 struct snd_soc_dapm_widget_list
**list
)
841 struct snd_soc_dapm_path
*path
;
844 if (widget
->inputs
>= 0)
845 return widget
->inputs
;
847 DAPM_UPDATE_STAT(widget
, path_checks
);
849 switch (widget
->id
) {
850 case snd_soc_dapm_supply
:
851 case snd_soc_dapm_regulator_supply
:
857 /* active stream ? */
858 switch (widget
->id
) {
859 case snd_soc_dapm_dac
:
860 case snd_soc_dapm_aif_in
:
861 case snd_soc_dapm_dai
:
862 if (widget
->active
) {
863 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
864 return widget
->inputs
;
870 if (widget
->connected
) {
871 /* connected pin ? */
872 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
) {
873 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
874 return widget
->inputs
;
877 /* connected VMID/Bias for lower pops */
878 if (widget
->id
== snd_soc_dapm_vmid
) {
879 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
880 return widget
->inputs
;
883 /* connected jack ? */
884 if (widget
->id
== snd_soc_dapm_mic
||
885 (widget
->id
== snd_soc_dapm_line
&&
886 !list_empty(&widget
->sinks
))) {
887 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
888 return widget
->inputs
;
891 /* signal generator */
892 if (widget
->id
== snd_soc_dapm_siggen
) {
893 widget
->inputs
= snd_soc_dapm_suspend_check(widget
);
894 return widget
->inputs
;
898 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
899 DAPM_UPDATE_STAT(widget
, neighbour_checks
);
907 trace_snd_soc_dapm_input_path(widget
, path
);
909 if (path
->source
&& path
->connect
) {
912 /* do we need to add this widget to the list ? */
915 err
= dapm_list_add_widget(list
, path
->source
);
917 dev_err(widget
->dapm
->dev
, "could not add widget %s\n",
923 con
+= is_connected_input_ep(path
->source
, list
);
927 widget
->inputs
= con
;
933 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
935 * @stream: stream direction.
936 * @list: list of active widgets for this stream.
938 * Queries DAPM graph as to whether an valid audio stream path exists for
939 * the initial stream specified by name. This takes into account
940 * current mixer and mux kcontrol settings. Creates list of valid widgets.
942 * Returns the number of valid paths or negative error.
944 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai
*dai
, int stream
,
945 struct snd_soc_dapm_widget_list
**list
)
947 struct snd_soc_card
*card
= dai
->card
;
950 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
953 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
954 paths
= is_connected_output_ep(dai
->playback_widget
, list
);
956 paths
= is_connected_input_ep(dai
->capture_widget
, list
);
958 trace_snd_soc_dapm_connected(paths
, stream
);
959 dapm_clear_walk(&card
->dapm
);
960 mutex_unlock(&card
->dapm_mutex
);
966 * Handler for generic register modifier widget.
968 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
969 struct snd_kcontrol
*kcontrol
, int event
)
973 if (SND_SOC_DAPM_EVENT_ON(event
))
978 soc_widget_update_bits_locked(w
, -(w
->reg
+ 1),
979 w
->mask
<< w
->shift
, val
<< w
->shift
);
983 EXPORT_SYMBOL_GPL(dapm_reg_event
);
986 * Handler for regulator supply widget.
988 int dapm_regulator_event(struct snd_soc_dapm_widget
*w
,
989 struct snd_kcontrol
*kcontrol
, int event
)
991 if (SND_SOC_DAPM_EVENT_ON(event
))
992 return regulator_enable(w
->regulator
);
994 return regulator_disable_deferred(w
->regulator
, w
->shift
);
996 EXPORT_SYMBOL_GPL(dapm_regulator_event
);
998 static int dapm_widget_power_check(struct snd_soc_dapm_widget
*w
)
1000 if (w
->power_checked
)
1001 return w
->new_power
;
1006 w
->new_power
= w
->power_check(w
);
1008 w
->power_checked
= true;
1010 return w
->new_power
;
1013 /* Generic check to see if a widget should be powered.
1015 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
1019 DAPM_UPDATE_STAT(w
, power_checks
);
1021 in
= is_connected_input_ep(w
, NULL
);
1022 dapm_clear_walk(w
->dapm
);
1023 out
= is_connected_output_ep(w
, NULL
);
1024 dapm_clear_walk(w
->dapm
);
1025 return out
!= 0 && in
!= 0;
1028 static int dapm_dai_check_power(struct snd_soc_dapm_widget
*w
)
1030 DAPM_UPDATE_STAT(w
, power_checks
);
1035 return dapm_generic_check_power(w
);
1038 /* Check to see if an ADC has power */
1039 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
1043 DAPM_UPDATE_STAT(w
, power_checks
);
1046 in
= is_connected_input_ep(w
, NULL
);
1047 dapm_clear_walk(w
->dapm
);
1050 return dapm_generic_check_power(w
);
1054 /* Check to see if a DAC has power */
1055 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
1059 DAPM_UPDATE_STAT(w
, power_checks
);
1062 out
= is_connected_output_ep(w
, NULL
);
1063 dapm_clear_walk(w
->dapm
);
1066 return dapm_generic_check_power(w
);
1070 /* Check to see if a power supply is needed */
1071 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
1073 struct snd_soc_dapm_path
*path
;
1075 DAPM_UPDATE_STAT(w
, power_checks
);
1077 /* Check if one of our outputs is connected */
1078 list_for_each_entry(path
, &w
->sinks
, list_source
) {
1079 DAPM_UPDATE_STAT(w
, neighbour_checks
);
1084 if (path
->connected
&&
1085 !path
->connected(path
->source
, path
->sink
))
1091 if (dapm_widget_power_check(path
->sink
))
1095 dapm_clear_walk(w
->dapm
);
1100 static int dapm_always_on_check_power(struct snd_soc_dapm_widget
*w
)
1105 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
1106 struct snd_soc_dapm_widget
*b
,
1114 sort
= dapm_down_seq
;
1116 if (sort
[a
->id
] != sort
[b
->id
])
1117 return sort
[a
->id
] - sort
[b
->id
];
1118 if (a
->subseq
!= b
->subseq
) {
1120 return a
->subseq
- b
->subseq
;
1122 return b
->subseq
- a
->subseq
;
1124 if (a
->reg
!= b
->reg
)
1125 return a
->reg
- b
->reg
;
1126 if (a
->dapm
!= b
->dapm
)
1127 return (unsigned long)a
->dapm
- (unsigned long)b
->dapm
;
1132 /* Insert a widget in order into a DAPM power sequence. */
1133 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
1134 struct list_head
*list
,
1137 struct snd_soc_dapm_widget
*w
;
1139 list_for_each_entry(w
, list
, power_list
)
1140 if (dapm_seq_compare(new_widget
, w
, power_up
) < 0) {
1141 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
1145 list_add_tail(&new_widget
->power_list
, list
);
1148 static void dapm_seq_check_event(struct snd_soc_dapm_context
*dapm
,
1149 struct snd_soc_dapm_widget
*w
, int event
)
1151 struct snd_soc_card
*card
= dapm
->card
;
1152 const char *ev_name
;
1156 case SND_SOC_DAPM_PRE_PMU
:
1157 ev_name
= "PRE_PMU";
1160 case SND_SOC_DAPM_POST_PMU
:
1161 ev_name
= "POST_PMU";
1164 case SND_SOC_DAPM_PRE_PMD
:
1165 ev_name
= "PRE_PMD";
1168 case SND_SOC_DAPM_POST_PMD
:
1169 ev_name
= "POST_PMD";
1177 if (w
->power
!= power
)
1180 if (w
->event
&& (w
->event_flags
& event
)) {
1181 pop_dbg(dapm
->dev
, card
->pop_time
, "pop test : %s %s\n",
1183 trace_snd_soc_dapm_widget_event_start(w
, event
);
1184 ret
= w
->event(w
, NULL
, event
);
1185 trace_snd_soc_dapm_widget_event_done(w
, event
);
1187 pr_err("%s: %s event failed: %d\n",
1188 ev_name
, w
->name
, ret
);
1192 /* Apply the coalesced changes from a DAPM sequence */
1193 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context
*dapm
,
1194 struct list_head
*pending
)
1196 struct snd_soc_card
*card
= dapm
->card
;
1197 struct snd_soc_dapm_widget
*w
;
1199 unsigned int value
= 0;
1200 unsigned int mask
= 0;
1201 unsigned int cur_mask
;
1203 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
1206 list_for_each_entry(w
, pending
, power_list
) {
1207 cur_mask
= 1 << w
->shift
;
1208 BUG_ON(reg
!= w
->reg
);
1219 pop_dbg(dapm
->dev
, card
->pop_time
,
1220 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1221 w
->name
, reg
, value
, mask
);
1223 /* Check for events */
1224 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_PRE_PMU
);
1225 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_PRE_PMD
);
1229 /* Any widget will do, they should all be updating the
1232 w
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
1235 pop_dbg(dapm
->dev
, card
->pop_time
,
1236 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1237 value
, mask
, reg
, card
->pop_time
);
1238 pop_wait(card
->pop_time
);
1239 soc_widget_update_bits_locked(w
, reg
, mask
, value
);
1242 list_for_each_entry(w
, pending
, power_list
) {
1243 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_POST_PMU
);
1244 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_POST_PMD
);
1248 /* Apply a DAPM power sequence.
1250 * We walk over a pre-sorted list of widgets to apply power to. In
1251 * order to minimise the number of writes to the device required
1252 * multiple widgets will be updated in a single write where possible.
1253 * Currently anything that requires more than a single write is not
1256 static void dapm_seq_run(struct snd_soc_dapm_context
*dapm
,
1257 struct list_head
*list
, int event
, bool power_up
)
1259 struct snd_soc_dapm_widget
*w
, *n
;
1262 int cur_subseq
= -1;
1263 int cur_reg
= SND_SOC_NOPM
;
1264 struct snd_soc_dapm_context
*cur_dapm
= NULL
;
1271 sort
= dapm_down_seq
;
1273 list_for_each_entry_safe(w
, n
, list
, power_list
) {
1276 /* Do we need to apply any queued changes? */
1277 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
||
1278 w
->dapm
!= cur_dapm
|| w
->subseq
!= cur_subseq
) {
1279 if (!list_empty(&pending
))
1280 dapm_seq_run_coalesced(cur_dapm
, &pending
);
1282 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1283 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1284 if (sort
[i
] == cur_sort
)
1285 cur_dapm
->seq_notifier(cur_dapm
,
1290 INIT_LIST_HEAD(&pending
);
1292 cur_subseq
= INT_MIN
;
1293 cur_reg
= SND_SOC_NOPM
;
1298 case snd_soc_dapm_pre
:
1300 list_for_each_entry_safe_continue(w
, n
, list
,
1303 if (event
== SND_SOC_DAPM_STREAM_START
)
1305 NULL
, SND_SOC_DAPM_PRE_PMU
);
1306 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1308 NULL
, SND_SOC_DAPM_PRE_PMD
);
1311 case snd_soc_dapm_post
:
1313 list_for_each_entry_safe_continue(w
, n
, list
,
1316 if (event
== SND_SOC_DAPM_STREAM_START
)
1318 NULL
, SND_SOC_DAPM_POST_PMU
);
1319 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
1321 NULL
, SND_SOC_DAPM_POST_PMD
);
1325 /* Queue it up for application */
1326 cur_sort
= sort
[w
->id
];
1327 cur_subseq
= w
->subseq
;
1330 list_move(&w
->power_list
, &pending
);
1335 dev_err(w
->dapm
->dev
,
1336 "Failed to apply widget power: %d\n", ret
);
1339 if (!list_empty(&pending
))
1340 dapm_seq_run_coalesced(cur_dapm
, &pending
);
1342 if (cur_dapm
&& cur_dapm
->seq_notifier
) {
1343 for (i
= 0; i
< ARRAY_SIZE(dapm_up_seq
); i
++)
1344 if (sort
[i
] == cur_sort
)
1345 cur_dapm
->seq_notifier(cur_dapm
,
1350 static void dapm_widget_update(struct snd_soc_dapm_context
*dapm
)
1352 struct snd_soc_dapm_update
*update
= dapm
->update
;
1353 struct snd_soc_dapm_widget
*w
;
1362 (w
->event_flags
& SND_SOC_DAPM_PRE_REG
)) {
1363 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_PRE_REG
);
1365 pr_err("%s DAPM pre-event failed: %d\n",
1369 ret
= soc_widget_update_bits_locked(w
, update
->reg
, update
->mask
,
1372 pr_err("%s DAPM update failed: %d\n", w
->name
, ret
);
1375 (w
->event_flags
& SND_SOC_DAPM_POST_REG
)) {
1376 ret
= w
->event(w
, update
->kcontrol
, SND_SOC_DAPM_POST_REG
);
1378 pr_err("%s DAPM post-event failed: %d\n",
1383 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1384 * they're changing state.
1386 static void dapm_pre_sequence_async(void *data
, async_cookie_t cookie
)
1388 struct snd_soc_dapm_context
*d
= data
;
1391 /* If we're off and we're not supposed to be go into STANDBY */
1392 if (d
->bias_level
== SND_SOC_BIAS_OFF
&&
1393 d
->target_bias_level
!= SND_SOC_BIAS_OFF
) {
1395 pm_runtime_get_sync(d
->dev
);
1397 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1400 "Failed to turn on bias: %d\n", ret
);
1403 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1404 if (d
->bias_level
!= d
->target_bias_level
) {
1405 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_PREPARE
);
1408 "Failed to prepare bias: %d\n", ret
);
1412 /* Async callback run prior to DAPM sequences - brings to their final
1415 static void dapm_post_sequence_async(void *data
, async_cookie_t cookie
)
1417 struct snd_soc_dapm_context
*d
= data
;
1420 /* If we just powered the last thing off drop to standby bias */
1421 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1422 (d
->target_bias_level
== SND_SOC_BIAS_STANDBY
||
1423 d
->target_bias_level
== SND_SOC_BIAS_OFF
)) {
1424 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_STANDBY
);
1426 dev_err(d
->dev
, "Failed to apply standby bias: %d\n",
1430 /* If we're in standby and can support bias off then do that */
1431 if (d
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1432 d
->target_bias_level
== SND_SOC_BIAS_OFF
) {
1433 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_OFF
);
1435 dev_err(d
->dev
, "Failed to turn off bias: %d\n", ret
);
1438 pm_runtime_put(d
->dev
);
1441 /* If we just powered up then move to active bias */
1442 if (d
->bias_level
== SND_SOC_BIAS_PREPARE
&&
1443 d
->target_bias_level
== SND_SOC_BIAS_ON
) {
1444 ret
= snd_soc_dapm_set_bias_level(d
, SND_SOC_BIAS_ON
);
1446 dev_err(d
->dev
, "Failed to apply active bias: %d\n",
1451 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget
*peer
,
1452 bool power
, bool connect
)
1454 /* If a connection is being made or broken then that update
1455 * will have marked the peer dirty, otherwise the widgets are
1456 * not connected and this update has no impact. */
1460 /* If the peer is already in the state we're moving to then we
1461 * won't have an impact on it. */
1462 if (power
!= peer
->power
)
1463 dapm_mark_dirty(peer
, "peer state change");
1466 static void dapm_widget_set_power(struct snd_soc_dapm_widget
*w
, bool power
,
1467 struct list_head
*up_list
,
1468 struct list_head
*down_list
)
1470 struct snd_soc_dapm_path
*path
;
1472 if (w
->power
== power
)
1475 trace_snd_soc_dapm_widget_power(w
, power
);
1477 /* If we changed our power state perhaps our neigbours changed
1480 list_for_each_entry(path
, &w
->sources
, list_sink
) {
1482 dapm_widget_set_peer_power(path
->source
, power
,
1487 case snd_soc_dapm_supply
:
1488 case snd_soc_dapm_regulator_supply
:
1489 /* Supplies can't affect their outputs, only their inputs */
1492 list_for_each_entry(path
, &w
->sinks
, list_source
) {
1494 dapm_widget_set_peer_power(path
->sink
, power
,
1502 dapm_seq_insert(w
, up_list
, true);
1504 dapm_seq_insert(w
, down_list
, false);
1509 static void dapm_power_one_widget(struct snd_soc_dapm_widget
*w
,
1510 struct list_head
*up_list
,
1511 struct list_head
*down_list
)
1516 case snd_soc_dapm_pre
:
1517 dapm_seq_insert(w
, down_list
, false);
1519 case snd_soc_dapm_post
:
1520 dapm_seq_insert(w
, up_list
, true);
1524 power
= dapm_widget_power_check(w
);
1526 dapm_widget_set_power(w
, power
, up_list
, down_list
);
1532 * Scan each dapm widget for complete audio path.
1533 * A complete path is a route that has valid endpoints i.e.:-
1535 * o DAC to output pin.
1536 * o Input Pin to ADC.
1537 * o Input pin to Output pin (bypass, sidetone)
1538 * o DAC to ADC (loopback).
1540 static int dapm_power_widgets(struct snd_soc_dapm_context
*dapm
, int event
)
1542 struct snd_soc_card
*card
= dapm
->card
;
1543 struct snd_soc_dapm_widget
*w
;
1544 struct snd_soc_dapm_context
*d
;
1546 LIST_HEAD(down_list
);
1547 LIST_HEAD(async_domain
);
1548 enum snd_soc_bias_level bias
;
1550 trace_snd_soc_dapm_start(card
);
1552 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1553 if (d
->idle_bias_off
)
1554 d
->target_bias_level
= SND_SOC_BIAS_OFF
;
1556 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1561 /* Check which widgets we need to power and store them in
1562 * lists indicating if they should be powered up or down. We
1563 * only check widgets that have been flagged as dirty but note
1564 * that new widgets may be added to the dirty list while we
1567 list_for_each_entry(w
, &card
->dapm_dirty
, dirty
) {
1568 dapm_power_one_widget(w
, &up_list
, &down_list
);
1571 list_for_each_entry(w
, &card
->widgets
, list
) {
1572 list_del_init(&w
->dirty
);
1577 /* Supplies and micbiases only bring the
1578 * context up to STANDBY as unless something
1579 * else is active and passing audio they
1580 * generally don't require full power. Signal
1581 * generators are virtual pins and have no
1582 * power impact themselves.
1585 case snd_soc_dapm_siggen
:
1587 case snd_soc_dapm_supply
:
1588 case snd_soc_dapm_regulator_supply
:
1589 case snd_soc_dapm_micbias
:
1590 if (d
->target_bias_level
< SND_SOC_BIAS_STANDBY
)
1591 d
->target_bias_level
= SND_SOC_BIAS_STANDBY
;
1594 d
->target_bias_level
= SND_SOC_BIAS_ON
;
1601 /* Force all contexts in the card to the same bias state if
1602 * they're not ground referenced.
1604 bias
= SND_SOC_BIAS_OFF
;
1605 list_for_each_entry(d
, &card
->dapm_list
, list
)
1606 if (d
->target_bias_level
> bias
)
1607 bias
= d
->target_bias_level
;
1608 list_for_each_entry(d
, &card
->dapm_list
, list
)
1609 if (!d
->idle_bias_off
)
1610 d
->target_bias_level
= bias
;
1612 trace_snd_soc_dapm_walk_done(card
);
1614 /* Run all the bias changes in parallel */
1615 list_for_each_entry(d
, &dapm
->card
->dapm_list
, list
)
1616 async_schedule_domain(dapm_pre_sequence_async
, d
,
1618 async_synchronize_full_domain(&async_domain
);
1620 /* Power down widgets first; try to avoid amplifying pops. */
1621 dapm_seq_run(dapm
, &down_list
, event
, false);
1623 dapm_widget_update(dapm
);
1626 dapm_seq_run(dapm
, &up_list
, event
, true);
1628 /* Run all the bias changes in parallel */
1629 list_for_each_entry(d
, &dapm
->card
->dapm_list
, list
)
1630 async_schedule_domain(dapm_post_sequence_async
, d
,
1632 async_synchronize_full_domain(&async_domain
);
1634 /* do we need to notify any clients that DAPM event is complete */
1635 list_for_each_entry(d
, &card
->dapm_list
, list
) {
1636 if (d
->stream_event
)
1637 d
->stream_event(d
, event
);
1640 pop_dbg(dapm
->dev
, card
->pop_time
,
1641 "DAPM sequencing finished, waiting %dms\n", card
->pop_time
);
1642 pop_wait(card
->pop_time
);
1644 trace_snd_soc_dapm_done(card
);
1649 #ifdef CONFIG_DEBUG_FS
1650 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1651 char __user
*user_buf
,
1652 size_t count
, loff_t
*ppos
)
1654 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1658 struct snd_soc_dapm_path
*p
= NULL
;
1660 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1664 in
= is_connected_input_ep(w
, NULL
);
1665 dapm_clear_walk(w
->dapm
);
1666 out
= is_connected_output_ep(w
, NULL
);
1667 dapm_clear_walk(w
->dapm
);
1669 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s%s in %d out %d",
1670 w
->name
, w
->power
? "On" : "Off",
1671 w
->force
? " (forced)" : "", in
, out
);
1674 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1675 " - R%d(0x%x) bit %d",
1676 w
->reg
, w
->reg
, w
->shift
);
1678 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
1681 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1683 w
->active
? "active" : "inactive");
1685 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1686 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1690 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1691 " in \"%s\" \"%s\"\n",
1692 p
->name
? p
->name
: "static",
1695 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1696 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1700 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1701 " out \"%s\" \"%s\"\n",
1702 p
->name
? p
->name
: "static",
1706 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1712 static const struct file_operations dapm_widget_power_fops
= {
1713 .open
= simple_open
,
1714 .read
= dapm_widget_power_read_file
,
1715 .llseek
= default_llseek
,
1718 static ssize_t
dapm_bias_read_file(struct file
*file
, char __user
*user_buf
,
1719 size_t count
, loff_t
*ppos
)
1721 struct snd_soc_dapm_context
*dapm
= file
->private_data
;
1724 switch (dapm
->bias_level
) {
1725 case SND_SOC_BIAS_ON
:
1728 case SND_SOC_BIAS_PREPARE
:
1729 level
= "Prepare\n";
1731 case SND_SOC_BIAS_STANDBY
:
1732 level
= "Standby\n";
1734 case SND_SOC_BIAS_OFF
:
1739 level
= "Unknown\n";
1743 return simple_read_from_buffer(user_buf
, count
, ppos
, level
,
1747 static const struct file_operations dapm_bias_fops
= {
1748 .open
= simple_open
,
1749 .read
= dapm_bias_read_file
,
1750 .llseek
= default_llseek
,
1753 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
1754 struct dentry
*parent
)
1758 dapm
->debugfs_dapm
= debugfs_create_dir("dapm", parent
);
1760 if (!dapm
->debugfs_dapm
) {
1762 "Failed to create DAPM debugfs directory\n");
1766 d
= debugfs_create_file("bias_level", 0444,
1767 dapm
->debugfs_dapm
, dapm
,
1771 "ASoC: Failed to create bias level debugfs file\n");
1774 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
1776 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
1779 if (!dapm
->debugfs_dapm
|| !w
->name
)
1782 d
= debugfs_create_file(w
->name
, 0444,
1783 dapm
->debugfs_dapm
, w
,
1784 &dapm_widget_power_fops
);
1786 dev_warn(w
->dapm
->dev
,
1787 "ASoC: Failed to create %s debugfs file\n",
1791 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
1793 debugfs_remove_recursive(dapm
->debugfs_dapm
);
1797 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
,
1798 struct dentry
*parent
)
1802 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget
*w
)
1806 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context
*dapm
)
1812 /* test and update the power status of a mux widget */
1813 static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget
*widget
,
1814 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
)
1816 struct snd_soc_dapm_path
*path
;
1819 if (widget
->id
!= snd_soc_dapm_mux
&&
1820 widget
->id
!= snd_soc_dapm_virt_mux
&&
1821 widget
->id
!= snd_soc_dapm_value_mux
)
1824 /* find dapm widget path assoc with kcontrol */
1825 list_for_each_entry(path
, &widget
->dapm
->card
->paths
, list
) {
1826 if (path
->kcontrol
!= kcontrol
)
1829 if (!path
->name
|| !e
->texts
[mux
])
1833 /* we now need to match the string in the enum to the path */
1834 if (!(strcmp(path
->name
, e
->texts
[mux
]))) {
1835 path
->connect
= 1; /* new connection */
1836 dapm_mark_dirty(path
->source
, "mux connection");
1839 dapm_mark_dirty(path
->source
,
1840 "mux disconnection");
1841 path
->connect
= 0; /* old connection must be powered down */
1846 dapm_mark_dirty(widget
, "mux change");
1847 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1853 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget
*widget
,
1854 struct snd_kcontrol
*kcontrol
, int mux
, struct soc_enum
*e
)
1856 struct snd_soc_card
*card
= widget
->dapm
->card
;
1859 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
1860 ret
= soc_dapm_mux_update_power(widget
, kcontrol
, mux
, e
);
1861 mutex_unlock(&card
->dapm_mutex
);
1863 soc_dpcm_runtime_update(widget
);
1866 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power
);
1868 /* test and update the power status of a mixer or switch widget */
1869 static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget
*widget
,
1870 struct snd_kcontrol
*kcontrol
, int connect
)
1872 struct snd_soc_dapm_path
*path
;
1875 if (widget
->id
!= snd_soc_dapm_mixer
&&
1876 widget
->id
!= snd_soc_dapm_mixer_named_ctl
&&
1877 widget
->id
!= snd_soc_dapm_switch
)
1880 /* find dapm widget path assoc with kcontrol */
1881 list_for_each_entry(path
, &widget
->dapm
->card
->paths
, list
) {
1882 if (path
->kcontrol
!= kcontrol
)
1885 /* found, now check type */
1887 path
->connect
= connect
;
1888 dapm_mark_dirty(path
->source
, "mixer connection");
1892 dapm_mark_dirty(widget
, "mixer update");
1893 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1899 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget
*widget
,
1900 struct snd_kcontrol
*kcontrol
, int connect
)
1902 struct snd_soc_card
*card
= widget
->dapm
->card
;
1905 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
1906 ret
= soc_dapm_mixer_update_power(widget
, kcontrol
, connect
);
1907 mutex_unlock(&card
->dapm_mutex
);
1909 soc_dpcm_runtime_update(widget
);
1912 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power
);
1914 /* show dapm widget status in sys fs */
1915 static ssize_t
dapm_widget_show(struct device
*dev
,
1916 struct device_attribute
*attr
, char *buf
)
1918 struct snd_soc_pcm_runtime
*rtd
= dev_get_drvdata(dev
);
1919 struct snd_soc_codec
*codec
=rtd
->codec
;
1920 struct snd_soc_dapm_widget
*w
;
1922 char *state
= "not set";
1924 list_for_each_entry(w
, &codec
->card
->widgets
, list
) {
1925 if (w
->dapm
!= &codec
->dapm
)
1928 /* only display widgets that burnm power */
1930 case snd_soc_dapm_hp
:
1931 case snd_soc_dapm_mic
:
1932 case snd_soc_dapm_spk
:
1933 case snd_soc_dapm_line
:
1934 case snd_soc_dapm_micbias
:
1935 case snd_soc_dapm_dac
:
1936 case snd_soc_dapm_adc
:
1937 case snd_soc_dapm_pga
:
1938 case snd_soc_dapm_out_drv
:
1939 case snd_soc_dapm_mixer
:
1940 case snd_soc_dapm_mixer_named_ctl
:
1941 case snd_soc_dapm_supply
:
1942 case snd_soc_dapm_regulator_supply
:
1944 count
+= sprintf(buf
+ count
, "%s: %s\n",
1945 w
->name
, w
->power
? "On":"Off");
1952 switch (codec
->dapm
.bias_level
) {
1953 case SND_SOC_BIAS_ON
:
1956 case SND_SOC_BIAS_PREPARE
:
1959 case SND_SOC_BIAS_STANDBY
:
1962 case SND_SOC_BIAS_OFF
:
1966 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
1971 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
1973 int snd_soc_dapm_sys_add(struct device
*dev
)
1975 return device_create_file(dev
, &dev_attr_dapm_widget
);
1978 static void snd_soc_dapm_sys_remove(struct device
*dev
)
1980 device_remove_file(dev
, &dev_attr_dapm_widget
);
1983 /* free all dapm widgets and resources */
1984 static void dapm_free_widgets(struct snd_soc_dapm_context
*dapm
)
1986 struct snd_soc_dapm_widget
*w
, *next_w
;
1987 struct snd_soc_dapm_path
*p
, *next_p
;
1989 list_for_each_entry_safe(w
, next_w
, &dapm
->card
->widgets
, list
) {
1990 if (w
->dapm
!= dapm
)
1994 * remove source and sink paths associated to this widget.
1995 * While removing the path, remove reference to it from both
1996 * source and sink widgets so that path is removed only once.
1998 list_for_each_entry_safe(p
, next_p
, &w
->sources
, list_sink
) {
1999 list_del(&p
->list_sink
);
2000 list_del(&p
->list_source
);
2002 kfree(p
->long_name
);
2005 list_for_each_entry_safe(p
, next_p
, &w
->sinks
, list_source
) {
2006 list_del(&p
->list_sink
);
2007 list_del(&p
->list_source
);
2009 kfree(p
->long_name
);
2012 kfree(w
->kcontrols
);
2018 static struct snd_soc_dapm_widget
*dapm_find_widget(
2019 struct snd_soc_dapm_context
*dapm
, const char *pin
,
2020 bool search_other_contexts
)
2022 struct snd_soc_dapm_widget
*w
;
2023 struct snd_soc_dapm_widget
*fallback
= NULL
;
2025 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
2026 if (!strcmp(w
->name
, pin
)) {
2027 if (w
->dapm
== dapm
)
2034 if (search_other_contexts
)
2040 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context
*dapm
,
2041 const char *pin
, int status
)
2043 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
2046 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
2050 if (w
->connected
!= status
)
2051 dapm_mark_dirty(w
, "pin configuration");
2053 w
->connected
= status
;
2061 * snd_soc_dapm_sync - scan and power dapm paths
2062 * @dapm: DAPM context
2064 * Walks all dapm audio paths and powers widgets according to their
2065 * stream or path usage.
2067 * Returns 0 for success.
2069 int snd_soc_dapm_sync(struct snd_soc_dapm_context
*dapm
)
2074 * Suppress early reports (eg, jacks syncing their state) to avoid
2075 * silly DAPM runs during card startup.
2077 if (!dapm
->card
|| !dapm
->card
->instantiated
)
2080 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2081 ret
= dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
2082 mutex_unlock(&dapm
->card
->dapm_mutex
);
2085 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
2087 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context
*dapm
,
2088 const struct snd_soc_dapm_route
*route
)
2090 struct snd_soc_dapm_path
*path
;
2091 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
2092 struct snd_soc_dapm_widget
*wtsource
= NULL
, *wtsink
= NULL
;
2094 const char *control
= route
->control
;
2096 char prefixed_sink
[80];
2097 char prefixed_source
[80];
2100 if (dapm
->codec
&& dapm
->codec
->name_prefix
) {
2101 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
2102 dapm
->codec
->name_prefix
, route
->sink
);
2103 sink
= prefixed_sink
;
2104 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
2105 dapm
->codec
->name_prefix
, route
->source
);
2106 source
= prefixed_source
;
2109 source
= route
->source
;
2113 * find src and dest widgets over all widgets but favor a widget from
2114 * current DAPM context
2116 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
2117 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
2119 if (w
->dapm
== dapm
)
2123 if (!wsource
&& !(strcmp(w
->name
, source
))) {
2125 if (w
->dapm
== dapm
)
2129 /* use widget from another DAPM context if not found from this */
2135 if (wsource
== NULL
|| wsink
== NULL
)
2138 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
2142 path
->source
= wsource
;
2144 path
->connected
= route
->connected
;
2145 INIT_LIST_HEAD(&path
->list
);
2146 INIT_LIST_HEAD(&path
->list_source
);
2147 INIT_LIST_HEAD(&path
->list_sink
);
2149 /* check for external widgets */
2150 if (wsink
->id
== snd_soc_dapm_input
) {
2151 if (wsource
->id
== snd_soc_dapm_micbias
||
2152 wsource
->id
== snd_soc_dapm_mic
||
2153 wsource
->id
== snd_soc_dapm_line
||
2154 wsource
->id
== snd_soc_dapm_output
)
2157 if (wsource
->id
== snd_soc_dapm_output
) {
2158 if (wsink
->id
== snd_soc_dapm_spk
||
2159 wsink
->id
== snd_soc_dapm_hp
||
2160 wsink
->id
== snd_soc_dapm_line
||
2161 wsink
->id
== snd_soc_dapm_input
)
2165 /* connect static paths */
2166 if (control
== NULL
) {
2167 list_add(&path
->list
, &dapm
->card
->paths
);
2168 list_add(&path
->list_sink
, &wsink
->sources
);
2169 list_add(&path
->list_source
, &wsource
->sinks
);
2174 /* connect dynamic paths */
2175 switch (wsink
->id
) {
2176 case snd_soc_dapm_adc
:
2177 case snd_soc_dapm_dac
:
2178 case snd_soc_dapm_pga
:
2179 case snd_soc_dapm_out_drv
:
2180 case snd_soc_dapm_input
:
2181 case snd_soc_dapm_output
:
2182 case snd_soc_dapm_siggen
:
2183 case snd_soc_dapm_micbias
:
2184 case snd_soc_dapm_vmid
:
2185 case snd_soc_dapm_pre
:
2186 case snd_soc_dapm_post
:
2187 case snd_soc_dapm_supply
:
2188 case snd_soc_dapm_regulator_supply
:
2189 case snd_soc_dapm_aif_in
:
2190 case snd_soc_dapm_aif_out
:
2191 case snd_soc_dapm_dai
:
2192 case snd_soc_dapm_dai_link
:
2193 list_add(&path
->list
, &dapm
->card
->paths
);
2194 list_add(&path
->list_sink
, &wsink
->sources
);
2195 list_add(&path
->list_source
, &wsource
->sinks
);
2198 case snd_soc_dapm_mux
:
2199 case snd_soc_dapm_virt_mux
:
2200 case snd_soc_dapm_value_mux
:
2201 ret
= dapm_connect_mux(dapm
, wsource
, wsink
, path
, control
,
2202 &wsink
->kcontrol_news
[0]);
2206 case snd_soc_dapm_switch
:
2207 case snd_soc_dapm_mixer
:
2208 case snd_soc_dapm_mixer_named_ctl
:
2209 ret
= dapm_connect_mixer(dapm
, wsource
, wsink
, path
, control
);
2213 case snd_soc_dapm_hp
:
2214 case snd_soc_dapm_mic
:
2215 case snd_soc_dapm_line
:
2216 case snd_soc_dapm_spk
:
2217 list_add(&path
->list
, &dapm
->card
->paths
);
2218 list_add(&path
->list_sink
, &wsink
->sources
);
2219 list_add(&path
->list_source
, &wsource
->sinks
);
2226 dev_warn(dapm
->dev
, "asoc: no dapm match for %s --> %s --> %s\n",
2227 source
, control
, sink
);
2233 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2234 * @dapm: DAPM context
2235 * @route: audio routes
2236 * @num: number of routes
2238 * Connects 2 dapm widgets together via a named audio path. The sink is
2239 * the widget receiving the audio signal, whilst the source is the sender
2240 * of the audio signal.
2242 * Returns 0 for success else error. On error all resources can be freed
2243 * with a call to snd_soc_card_free().
2245 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context
*dapm
,
2246 const struct snd_soc_dapm_route
*route
, int num
)
2250 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2251 for (i
= 0; i
< num
; i
++) {
2252 ret
= snd_soc_dapm_add_route(dapm
, route
);
2254 dev_err(dapm
->dev
, "Failed to add route %s->%s\n",
2255 route
->source
, route
->sink
);
2260 mutex_unlock(&dapm
->card
->dapm_mutex
);
2264 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
2266 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context
*dapm
,
2267 const struct snd_soc_dapm_route
*route
)
2269 struct snd_soc_dapm_widget
*source
= dapm_find_widget(dapm
,
2272 struct snd_soc_dapm_widget
*sink
= dapm_find_widget(dapm
,
2275 struct snd_soc_dapm_path
*path
;
2279 dev_err(dapm
->dev
, "Unable to find source %s for weak route\n",
2285 dev_err(dapm
->dev
, "Unable to find sink %s for weak route\n",
2290 if (route
->control
|| route
->connected
)
2291 dev_warn(dapm
->dev
, "Ignoring control for weak route %s->%s\n",
2292 route
->source
, route
->sink
);
2294 list_for_each_entry(path
, &source
->sinks
, list_source
) {
2295 if (path
->sink
== sink
) {
2302 dev_err(dapm
->dev
, "No path found for weak route %s->%s\n",
2303 route
->source
, route
->sink
);
2305 dev_warn(dapm
->dev
, "%d paths found for weak route %s->%s\n",
2306 count
, route
->source
, route
->sink
);
2312 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2313 * @dapm: DAPM context
2314 * @route: audio routes
2315 * @num: number of routes
2317 * Mark existing routes matching those specified in the passed array
2318 * as being weak, meaning that they are ignored for the purpose of
2319 * power decisions. The main intended use case is for sidetone paths
2320 * which couple audio between other independent paths if they are both
2321 * active in order to make the combination work better at the user
2322 * level but which aren't intended to be "used".
2324 * Note that CODEC drivers should not use this as sidetone type paths
2325 * can frequently also be used as bypass paths.
2327 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context
*dapm
,
2328 const struct snd_soc_dapm_route
*route
, int num
)
2333 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2334 for (i
= 0; i
< num
; i
++) {
2335 err
= snd_soc_dapm_weak_route(dapm
, route
);
2340 mutex_unlock(&dapm
->card
->dapm_mutex
);
2344 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes
);
2347 * snd_soc_dapm_new_widgets - add new dapm widgets
2348 * @dapm: DAPM context
2350 * Checks the codec for any new dapm widgets and creates them if found.
2352 * Returns 0 for success.
2354 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context
*dapm
)
2356 struct snd_soc_dapm_widget
*w
;
2359 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2361 list_for_each_entry(w
, &dapm
->card
->widgets
, list
)
2366 if (w
->num_kcontrols
) {
2367 w
->kcontrols
= kzalloc(w
->num_kcontrols
*
2368 sizeof(struct snd_kcontrol
*),
2370 if (!w
->kcontrols
) {
2371 mutex_unlock(&dapm
->card
->dapm_mutex
);
2377 case snd_soc_dapm_switch
:
2378 case snd_soc_dapm_mixer
:
2379 case snd_soc_dapm_mixer_named_ctl
:
2382 case snd_soc_dapm_mux
:
2383 case snd_soc_dapm_virt_mux
:
2384 case snd_soc_dapm_value_mux
:
2387 case snd_soc_dapm_pga
:
2388 case snd_soc_dapm_out_drv
:
2395 /* Read the initial power state from the device */
2397 val
= soc_widget_read(w
, w
->reg
);
2398 val
&= 1 << w
->shift
;
2408 dapm_mark_dirty(w
, "new widget");
2409 dapm_debugfs_add_widget(w
);
2412 dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
2413 mutex_unlock(&dapm
->card
->dapm_mutex
);
2416 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
2419 * snd_soc_dapm_get_volsw - dapm mixer get callback
2420 * @kcontrol: mixer control
2421 * @ucontrol: control element information
2423 * Callback to get the value of a dapm mixer control.
2425 * Returns 0 for success.
2427 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
2428 struct snd_ctl_elem_value
*ucontrol
)
2430 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2431 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2432 struct soc_mixer_control
*mc
=
2433 (struct soc_mixer_control
*)kcontrol
->private_value
;
2434 unsigned int reg
= mc
->reg
;
2435 unsigned int shift
= mc
->shift
;
2436 unsigned int rshift
= mc
->rshift
;
2438 unsigned int invert
= mc
->invert
;
2439 unsigned int mask
= (1 << fls(max
)) - 1;
2441 ucontrol
->value
.integer
.value
[0] =
2442 (snd_soc_read(widget
->codec
, reg
) >> shift
) & mask
;
2443 if (shift
!= rshift
)
2444 ucontrol
->value
.integer
.value
[1] =
2445 (snd_soc_read(widget
->codec
, reg
) >> rshift
) & mask
;
2447 ucontrol
->value
.integer
.value
[0] =
2448 max
- ucontrol
->value
.integer
.value
[0];
2449 if (shift
!= rshift
)
2450 ucontrol
->value
.integer
.value
[1] =
2451 max
- ucontrol
->value
.integer
.value
[1];
2456 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
2459 * snd_soc_dapm_put_volsw - dapm mixer set callback
2460 * @kcontrol: mixer control
2461 * @ucontrol: control element information
2463 * Callback to set the value of a dapm mixer control.
2465 * Returns 0 for success.
2467 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
2468 struct snd_ctl_elem_value
*ucontrol
)
2470 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2471 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2472 struct snd_soc_codec
*codec
= widget
->codec
;
2473 struct snd_soc_card
*card
= codec
->card
;
2474 struct soc_mixer_control
*mc
=
2475 (struct soc_mixer_control
*)kcontrol
->private_value
;
2476 unsigned int reg
= mc
->reg
;
2477 unsigned int shift
= mc
->shift
;
2479 unsigned int mask
= (1 << fls(max
)) - 1;
2480 unsigned int invert
= mc
->invert
;
2482 int connect
, change
;
2483 struct snd_soc_dapm_update update
;
2486 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2490 mask
= mask
<< shift
;
2494 /* new connection */
2495 connect
= invert
? 0 : 1;
2497 /* old connection must be powered down */
2498 connect
= invert
? 1 : 0;
2500 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2502 change
= snd_soc_test_bits(widget
->codec
, reg
, mask
, val
);
2504 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2505 widget
= wlist
->widgets
[wi
];
2507 widget
->value
= val
;
2509 update
.kcontrol
= kcontrol
;
2510 update
.widget
= widget
;
2514 widget
->dapm
->update
= &update
;
2516 soc_dapm_mixer_update_power(widget
, kcontrol
, connect
);
2518 widget
->dapm
->update
= NULL
;
2522 mutex_unlock(&card
->dapm_mutex
);
2525 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
2528 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2529 * @kcontrol: mixer control
2530 * @ucontrol: control element information
2532 * Callback to get the value of a dapm enumerated double mixer control.
2534 * Returns 0 for success.
2536 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
2537 struct snd_ctl_elem_value
*ucontrol
)
2539 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2540 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2541 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2542 unsigned int val
, bitmask
;
2544 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2546 val
= snd_soc_read(widget
->codec
, e
->reg
);
2547 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & (bitmask
- 1);
2548 if (e
->shift_l
!= e
->shift_r
)
2549 ucontrol
->value
.enumerated
.item
[1] =
2550 (val
>> e
->shift_r
) & (bitmask
- 1);
2554 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
2557 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2558 * @kcontrol: mixer control
2559 * @ucontrol: control element information
2561 * Callback to set the value of a dapm enumerated double mixer control.
2563 * Returns 0 for success.
2565 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
2566 struct snd_ctl_elem_value
*ucontrol
)
2568 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2569 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2570 struct snd_soc_codec
*codec
= widget
->codec
;
2571 struct snd_soc_card
*card
= codec
->card
;
2572 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2573 unsigned int val
, mux
, change
;
2574 unsigned int mask
, bitmask
;
2575 struct snd_soc_dapm_update update
;
2578 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2580 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2582 mux
= ucontrol
->value
.enumerated
.item
[0];
2583 val
= mux
<< e
->shift_l
;
2584 mask
= (bitmask
- 1) << e
->shift_l
;
2585 if (e
->shift_l
!= e
->shift_r
) {
2586 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2588 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
2589 mask
|= (bitmask
- 1) << e
->shift_r
;
2592 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2594 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
2596 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2597 widget
= wlist
->widgets
[wi
];
2599 widget
->value
= val
;
2601 update
.kcontrol
= kcontrol
;
2602 update
.widget
= widget
;
2603 update
.reg
= e
->reg
;
2606 widget
->dapm
->update
= &update
;
2608 soc_dapm_mux_update_power(widget
, kcontrol
, mux
, e
);
2610 widget
->dapm
->update
= NULL
;
2614 mutex_unlock(&card
->dapm_mutex
);
2617 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
2620 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2621 * @kcontrol: mixer control
2622 * @ucontrol: control element information
2624 * Returns 0 for success.
2626 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol
*kcontrol
,
2627 struct snd_ctl_elem_value
*ucontrol
)
2629 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2630 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2632 ucontrol
->value
.enumerated
.item
[0] = widget
->value
;
2636 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt
);
2639 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2640 * @kcontrol: mixer control
2641 * @ucontrol: control element information
2643 * Returns 0 for success.
2645 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol
*kcontrol
,
2646 struct snd_ctl_elem_value
*ucontrol
)
2648 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2649 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2650 struct snd_soc_codec
*codec
= widget
->codec
;
2651 struct snd_soc_card
*card
= codec
->card
;
2652 struct soc_enum
*e
=
2653 (struct soc_enum
*)kcontrol
->private_value
;
2658 if (ucontrol
->value
.enumerated
.item
[0] >= e
->max
)
2661 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2663 change
= widget
->value
!= ucontrol
->value
.enumerated
.item
[0];
2665 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2666 widget
= wlist
->widgets
[wi
];
2668 widget
->value
= ucontrol
->value
.enumerated
.item
[0];
2670 soc_dapm_mux_update_power(widget
, kcontrol
, widget
->value
, e
);
2674 mutex_unlock(&card
->dapm_mutex
);
2677 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt
);
2680 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2682 * @kcontrol: mixer control
2683 * @ucontrol: control element information
2685 * Callback to get the value of a dapm semi enumerated double mixer control.
2687 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2688 * used for handling bitfield coded enumeration for example.
2690 * Returns 0 for success.
2692 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
2693 struct snd_ctl_elem_value
*ucontrol
)
2695 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2696 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2697 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2698 unsigned int reg_val
, val
, mux
;
2700 reg_val
= snd_soc_read(widget
->codec
, e
->reg
);
2701 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
2702 for (mux
= 0; mux
< e
->max
; mux
++) {
2703 if (val
== e
->values
[mux
])
2706 ucontrol
->value
.enumerated
.item
[0] = mux
;
2707 if (e
->shift_l
!= e
->shift_r
) {
2708 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
2709 for (mux
= 0; mux
< e
->max
; mux
++) {
2710 if (val
== e
->values
[mux
])
2713 ucontrol
->value
.enumerated
.item
[1] = mux
;
2718 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double
);
2721 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2723 * @kcontrol: mixer control
2724 * @ucontrol: control element information
2726 * Callback to set the value of a dapm semi enumerated double mixer control.
2728 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2729 * used for handling bitfield coded enumeration for example.
2731 * Returns 0 for success.
2733 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
2734 struct snd_ctl_elem_value
*ucontrol
)
2736 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
2737 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
2738 struct snd_soc_codec
*codec
= widget
->codec
;
2739 struct snd_soc_card
*card
= codec
->card
;
2740 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2741 unsigned int val
, mux
, change
;
2743 struct snd_soc_dapm_update update
;
2746 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2748 mux
= ucontrol
->value
.enumerated
.item
[0];
2749 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
2750 mask
= e
->mask
<< e
->shift_l
;
2751 if (e
->shift_l
!= e
->shift_r
) {
2752 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2754 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
2755 mask
|= e
->mask
<< e
->shift_r
;
2758 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2760 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
2762 for (wi
= 0; wi
< wlist
->num_widgets
; wi
++) {
2763 widget
= wlist
->widgets
[wi
];
2765 widget
->value
= val
;
2767 update
.kcontrol
= kcontrol
;
2768 update
.widget
= widget
;
2769 update
.reg
= e
->reg
;
2772 widget
->dapm
->update
= &update
;
2774 soc_dapm_mux_update_power(widget
, kcontrol
, mux
, e
);
2776 widget
->dapm
->update
= NULL
;
2780 mutex_unlock(&card
->dapm_mutex
);
2783 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double
);
2786 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2788 * @kcontrol: mixer control
2789 * @uinfo: control element information
2791 * Callback to provide information about a pin switch control.
2793 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
2794 struct snd_ctl_elem_info
*uinfo
)
2796 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2798 uinfo
->value
.integer
.min
= 0;
2799 uinfo
->value
.integer
.max
= 1;
2803 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
2806 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2808 * @kcontrol: mixer control
2811 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
2812 struct snd_ctl_elem_value
*ucontrol
)
2814 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
2815 const char *pin
= (const char *)kcontrol
->private_value
;
2817 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2819 ucontrol
->value
.integer
.value
[0] =
2820 snd_soc_dapm_get_pin_status(&card
->dapm
, pin
);
2822 mutex_unlock(&card
->dapm_mutex
);
2826 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
2829 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2831 * @kcontrol: mixer control
2834 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
2835 struct snd_ctl_elem_value
*ucontrol
)
2837 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
2838 const char *pin
= (const char *)kcontrol
->private_value
;
2840 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
2842 if (ucontrol
->value
.integer
.value
[0])
2843 snd_soc_dapm_enable_pin(&card
->dapm
, pin
);
2845 snd_soc_dapm_disable_pin(&card
->dapm
, pin
);
2847 mutex_unlock(&card
->dapm_mutex
);
2849 snd_soc_dapm_sync(&card
->dapm
);
2852 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
2854 static struct snd_soc_dapm_widget
*
2855 snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
2856 const struct snd_soc_dapm_widget
*widget
)
2858 struct snd_soc_dapm_widget
*w
;
2862 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
2866 case snd_soc_dapm_regulator_supply
:
2867 w
->regulator
= devm_regulator_get(dapm
->dev
, w
->name
);
2868 if (IS_ERR(w
->regulator
)) {
2869 ret
= PTR_ERR(w
->regulator
);
2870 dev_err(dapm
->dev
, "Failed to request %s: %d\n",
2879 name_len
= strlen(widget
->name
) + 1;
2880 if (dapm
->codec
&& dapm
->codec
->name_prefix
)
2881 name_len
+= 1 + strlen(dapm
->codec
->name_prefix
);
2882 w
->name
= kmalloc(name_len
, GFP_KERNEL
);
2883 if (w
->name
== NULL
) {
2887 if (dapm
->codec
&& dapm
->codec
->name_prefix
)
2888 snprintf((char *)w
->name
, name_len
, "%s %s",
2889 dapm
->codec
->name_prefix
, widget
->name
);
2891 snprintf((char *)w
->name
, name_len
, "%s", widget
->name
);
2894 case snd_soc_dapm_switch
:
2895 case snd_soc_dapm_mixer
:
2896 case snd_soc_dapm_mixer_named_ctl
:
2897 w
->power_check
= dapm_generic_check_power
;
2899 case snd_soc_dapm_mux
:
2900 case snd_soc_dapm_virt_mux
:
2901 case snd_soc_dapm_value_mux
:
2902 w
->power_check
= dapm_generic_check_power
;
2904 case snd_soc_dapm_adc
:
2905 case snd_soc_dapm_aif_out
:
2906 w
->power_check
= dapm_adc_check_power
;
2908 case snd_soc_dapm_dac
:
2909 case snd_soc_dapm_aif_in
:
2910 w
->power_check
= dapm_dac_check_power
;
2912 case snd_soc_dapm_pga
:
2913 case snd_soc_dapm_out_drv
:
2914 case snd_soc_dapm_input
:
2915 case snd_soc_dapm_output
:
2916 case snd_soc_dapm_micbias
:
2917 case snd_soc_dapm_spk
:
2918 case snd_soc_dapm_hp
:
2919 case snd_soc_dapm_mic
:
2920 case snd_soc_dapm_line
:
2921 case snd_soc_dapm_dai_link
:
2922 w
->power_check
= dapm_generic_check_power
;
2924 case snd_soc_dapm_supply
:
2925 case snd_soc_dapm_regulator_supply
:
2926 w
->power_check
= dapm_supply_check_power
;
2928 case snd_soc_dapm_dai
:
2929 w
->power_check
= dapm_dai_check_power
;
2932 w
->power_check
= dapm_always_on_check_power
;
2938 w
->codec
= dapm
->codec
;
2939 w
->platform
= dapm
->platform
;
2940 INIT_LIST_HEAD(&w
->sources
);
2941 INIT_LIST_HEAD(&w
->sinks
);
2942 INIT_LIST_HEAD(&w
->list
);
2943 INIT_LIST_HEAD(&w
->dirty
);
2944 list_add(&w
->list
, &dapm
->card
->widgets
);
2946 /* machine layer set ups unconnected pins and insertions */
2952 * snd_soc_dapm_new_controls - create new dapm controls
2953 * @dapm: DAPM context
2954 * @widget: widget array
2955 * @num: number of widgets
2957 * Creates new DAPM controls based upon the templates.
2959 * Returns 0 for success else error.
2961 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context
*dapm
,
2962 const struct snd_soc_dapm_widget
*widget
,
2965 struct snd_soc_dapm_widget
*w
;
2969 mutex_lock_nested(&dapm
->card
->dapm_mutex
, SND_SOC_DAPM_CLASS_INIT
);
2970 for (i
= 0; i
< num
; i
++) {
2971 w
= snd_soc_dapm_new_control(dapm
, widget
);
2974 "ASoC: Failed to create DAPM control %s\n",
2981 mutex_unlock(&dapm
->card
->dapm_mutex
);
2984 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
2986 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget
*w
,
2987 struct snd_kcontrol
*kcontrol
, int event
)
2989 struct snd_soc_dapm_path
*source_p
, *sink_p
;
2990 struct snd_soc_dai
*source
, *sink
;
2991 const struct snd_soc_pcm_stream
*config
= w
->params
;
2992 struct snd_pcm_substream substream
;
2993 struct snd_pcm_hw_params
*params
= NULL
;
2998 BUG_ON(list_empty(&w
->sources
) || list_empty(&w
->sinks
));
3000 /* We only support a single source and sink, pick the first */
3001 source_p
= list_first_entry(&w
->sources
, struct snd_soc_dapm_path
,
3003 sink_p
= list_first_entry(&w
->sinks
, struct snd_soc_dapm_path
,
3006 BUG_ON(!source_p
|| !sink_p
);
3007 BUG_ON(!sink_p
->source
|| !source_p
->sink
);
3008 BUG_ON(!source_p
->source
|| !sink_p
->sink
);
3010 source
= source_p
->source
->priv
;
3011 sink
= sink_p
->sink
->priv
;
3013 /* Be a little careful as we don't want to overflow the mask array */
3014 if (config
->formats
) {
3015 fmt
= ffs(config
->formats
) - 1;
3017 dev_warn(w
->dapm
->dev
, "Invalid format %llx specified\n",
3022 /* Currently very limited parameter selection */
3023 params
= kzalloc(sizeof(*params
), GFP_KERNEL
);
3028 snd_mask_set(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), fmt
);
3030 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
)->min
=
3032 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
)->max
=
3035 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
)->min
3036 = config
->channels_min
;
3037 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
)->max
3038 = config
->channels_max
;
3040 memset(&substream
, 0, sizeof(substream
));
3043 case SND_SOC_DAPM_PRE_PMU
:
3044 if (source
->driver
->ops
&& source
->driver
->ops
->hw_params
) {
3045 substream
.stream
= SNDRV_PCM_STREAM_CAPTURE
;
3046 ret
= source
->driver
->ops
->hw_params(&substream
,
3049 dev_err(source
->dev
,
3050 "hw_params() failed: %d\n", ret
);
3055 if (sink
->driver
->ops
&& sink
->driver
->ops
->hw_params
) {
3056 substream
.stream
= SNDRV_PCM_STREAM_PLAYBACK
;
3057 ret
= sink
->driver
->ops
->hw_params(&substream
, params
,
3061 "hw_params() failed: %d\n", ret
);
3067 case SND_SOC_DAPM_POST_PMU
:
3068 ret
= snd_soc_dai_digital_mute(sink
, 0);
3069 if (ret
!= 0 && ret
!= -ENOTSUPP
)
3070 dev_warn(sink
->dev
, "Failed to unmute: %d\n", ret
);
3074 case SND_SOC_DAPM_PRE_PMD
:
3075 ret
= snd_soc_dai_digital_mute(sink
, 1);
3076 if (ret
!= 0 && ret
!= -ENOTSUPP
)
3077 dev_warn(sink
->dev
, "Failed to mute: %d\n", ret
);
3091 int snd_soc_dapm_new_pcm(struct snd_soc_card
*card
,
3092 const struct snd_soc_pcm_stream
*params
,
3093 struct snd_soc_dapm_widget
*source
,
3094 struct snd_soc_dapm_widget
*sink
)
3096 struct snd_soc_dapm_route routes
[2];
3097 struct snd_soc_dapm_widget
template;
3098 struct snd_soc_dapm_widget
*w
;
3102 len
= strlen(source
->name
) + strlen(sink
->name
) + 2;
3103 link_name
= devm_kzalloc(card
->dev
, len
, GFP_KERNEL
);
3106 snprintf(link_name
, len
, "%s-%s", source
->name
, sink
->name
);
3108 memset(&template, 0, sizeof(template));
3109 template.reg
= SND_SOC_NOPM
;
3110 template.id
= snd_soc_dapm_dai_link
;
3111 template.name
= link_name
;
3112 template.event
= snd_soc_dai_link_event
;
3113 template.event_flags
= SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMU
|
3114 SND_SOC_DAPM_PRE_PMD
;
3116 dev_dbg(card
->dev
, "adding %s widget\n", link_name
);
3118 w
= snd_soc_dapm_new_control(&card
->dapm
, &template);
3120 dev_err(card
->dev
, "Failed to create %s widget\n",
3127 memset(&routes
, 0, sizeof(routes
));
3129 routes
[0].source
= source
->name
;
3130 routes
[0].sink
= link_name
;
3131 routes
[1].source
= link_name
;
3132 routes
[1].sink
= sink
->name
;
3134 return snd_soc_dapm_add_routes(&card
->dapm
, routes
,
3135 ARRAY_SIZE(routes
));
3138 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context
*dapm
,
3139 struct snd_soc_dai
*dai
)
3141 struct snd_soc_dapm_widget
template;
3142 struct snd_soc_dapm_widget
*w
;
3144 WARN_ON(dapm
->dev
!= dai
->dev
);
3146 memset(&template, 0, sizeof(template));
3147 template.reg
= SND_SOC_NOPM
;
3149 if (dai
->driver
->playback
.stream_name
) {
3150 template.id
= snd_soc_dapm_dai
;
3151 template.name
= dai
->driver
->playback
.stream_name
;
3152 template.sname
= dai
->driver
->playback
.stream_name
;
3154 dev_dbg(dai
->dev
, "adding %s widget\n",
3157 w
= snd_soc_dapm_new_control(dapm
, &template);
3159 dev_err(dapm
->dev
, "Failed to create %s widget\n",
3160 dai
->driver
->playback
.stream_name
);
3164 dai
->playback_widget
= w
;
3167 if (dai
->driver
->capture
.stream_name
) {
3168 template.id
= snd_soc_dapm_dai
;
3169 template.name
= dai
->driver
->capture
.stream_name
;
3170 template.sname
= dai
->driver
->capture
.stream_name
;
3172 dev_dbg(dai
->dev
, "adding %s widget\n",
3175 w
= snd_soc_dapm_new_control(dapm
, &template);
3177 dev_err(dapm
->dev
, "Failed to create %s widget\n",
3178 dai
->driver
->capture
.stream_name
);
3182 dai
->capture_widget
= w
;
3188 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card
*card
)
3190 struct snd_soc_dapm_widget
*dai_w
, *w
;
3191 struct snd_soc_dai
*dai
;
3192 struct snd_soc_dapm_route r
;
3194 memset(&r
, 0, sizeof(r
));
3196 /* For each DAI widget... */
3197 list_for_each_entry(dai_w
, &card
->widgets
, list
) {
3198 if (dai_w
->id
!= snd_soc_dapm_dai
)
3203 /* ...find all widgets with the same stream and link them */
3204 list_for_each_entry(w
, &card
->widgets
, list
) {
3205 if (w
->dapm
!= dai_w
->dapm
)
3208 if (w
->id
== snd_soc_dapm_dai
)
3214 if (dai
->driver
->playback
.stream_name
&&
3216 dai
->driver
->playback
.stream_name
)) {
3217 r
.source
= dai
->playback_widget
->name
;
3219 dev_dbg(dai
->dev
, "%s -> %s\n",
3222 snd_soc_dapm_add_route(w
->dapm
, &r
);
3225 if (dai
->driver
->capture
.stream_name
&&
3227 dai
->driver
->capture
.stream_name
)) {
3229 r
.sink
= dai
->capture_widget
->name
;
3230 dev_dbg(dai
->dev
, "%s -> %s\n",
3233 snd_soc_dapm_add_route(w
->dapm
, &r
);
3241 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
, int stream
,
3245 struct snd_soc_dapm_widget
*w_cpu
, *w_codec
;
3246 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
3247 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
3249 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3250 w_cpu
= cpu_dai
->playback_widget
;
3251 w_codec
= codec_dai
->playback_widget
;
3253 w_cpu
= cpu_dai
->capture_widget
;
3254 w_codec
= codec_dai
->capture_widget
;
3259 dapm_mark_dirty(w_cpu
, "stream event");
3262 case SND_SOC_DAPM_STREAM_START
:
3265 case SND_SOC_DAPM_STREAM_STOP
:
3268 case SND_SOC_DAPM_STREAM_SUSPEND
:
3269 case SND_SOC_DAPM_STREAM_RESUME
:
3270 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
3271 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
3278 dapm_mark_dirty(w_codec
, "stream event");
3281 case SND_SOC_DAPM_STREAM_START
:
3282 w_codec
->active
= 1;
3284 case SND_SOC_DAPM_STREAM_STOP
:
3285 w_codec
->active
= 0;
3287 case SND_SOC_DAPM_STREAM_SUSPEND
:
3288 case SND_SOC_DAPM_STREAM_RESUME
:
3289 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
3290 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
3295 dapm_power_widgets(&rtd
->card
->dapm
, event
);
3299 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3300 * @rtd: PCM runtime data
3301 * @stream: stream name
3302 * @event: stream event
3304 * Sends a stream event to the dapm core. The core then makes any
3305 * necessary widget power changes.
3307 * Returns 0 for success else error.
3309 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
, int stream
,
3312 struct snd_soc_card
*card
= rtd
->card
;
3314 mutex_lock_nested(&card
->dapm_mutex
, SND_SOC_DAPM_CLASS_RUNTIME
);
3315 soc_dapm_stream_event(rtd
, stream
, event
);
3316 mutex_unlock(&card
->dapm_mutex
);
3320 * snd_soc_dapm_enable_pin - enable pin.
3321 * @dapm: DAPM context
3324 * Enables input/output pin and its parents or children widgets iff there is
3325 * a valid audio route and active audio stream.
3326 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3327 * do any widget power switching.
3329 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
3331 return snd_soc_dapm_set_pin(dapm
, pin
, 1);
3333 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
3336 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3337 * @dapm: DAPM context
3340 * Enables input/output pin regardless of any other state. This is
3341 * intended for use with microphone bias supplies used in microphone
3344 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3345 * do any widget power switching.
3347 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context
*dapm
,
3350 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
3353 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
3357 dev_dbg(w
->dapm
->dev
, "dapm: force enable pin %s\n", pin
);
3360 dapm_mark_dirty(w
, "force enable");
3364 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
3367 * snd_soc_dapm_disable_pin - disable pin.
3368 * @dapm: DAPM context
3371 * Disables input/output pin and its parents or children widgets.
3372 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3373 * do any widget power switching.
3375 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context
*dapm
,
3378 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
3380 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
3383 * snd_soc_dapm_nc_pin - permanently disable pin.
3384 * @dapm: DAPM context
3387 * Marks the specified pin as being not connected, disabling it along
3388 * any parent or child widgets. At present this is identical to
3389 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3390 * additional things such as disabling controls which only affect
3391 * paths through the pin.
3393 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3394 * do any widget power switching.
3396 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
3398 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
3400 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
3403 * snd_soc_dapm_get_pin_status - get audio pin status
3404 * @dapm: DAPM context
3405 * @pin: audio signal pin endpoint (or start point)
3407 * Get audio pin status - connected or disconnected.
3409 * Returns 1 for connected otherwise 0.
3411 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context
*dapm
,
3414 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, true);
3417 return w
->connected
;
3421 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
3424 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3425 * @dapm: DAPM context
3426 * @pin: audio signal pin endpoint (or start point)
3428 * Mark the given endpoint or pin as ignoring suspend. When the
3429 * system is disabled a path between two endpoints flagged as ignoring
3430 * suspend will not be disabled. The path must already be enabled via
3431 * normal means at suspend time, it will not be turned on if it was not
3434 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context
*dapm
,
3437 struct snd_soc_dapm_widget
*w
= dapm_find_widget(dapm
, pin
, false);
3440 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
3444 w
->ignore_suspend
= 1;
3448 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
3450 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card
*card
,
3451 struct snd_soc_dapm_widget
*w
)
3453 struct snd_soc_dapm_path
*p
;
3455 list_for_each_entry(p
, &card
->paths
, list
) {
3456 if ((p
->source
== w
) || (p
->sink
== w
)) {
3458 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3459 p
->source
->name
, p
->source
->id
, p
->source
->dapm
,
3460 p
->sink
->name
, p
->sink
->id
, p
->sink
->dapm
);
3462 /* Connected to something other than the codec */
3463 if (p
->source
->dapm
!= p
->sink
->dapm
)
3466 * Loopback connection from codec external pin to
3467 * codec external pin
3469 if (p
->sink
->id
== snd_soc_dapm_input
) {
3470 switch (p
->source
->id
) {
3471 case snd_soc_dapm_output
:
3472 case snd_soc_dapm_micbias
:
3485 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3486 * @codec: The codec whose pins should be processed
3488 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3489 * which are unused. Pins are used if they are connected externally to the
3490 * codec, whether that be to some other device, or a loop-back connection to
3493 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec
*codec
)
3495 struct snd_soc_card
*card
= codec
->card
;
3496 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
3497 struct snd_soc_dapm_widget
*w
;
3499 dev_dbg(codec
->dev
, "Auto NC: DAPMs: card:%p codec:%p\n",
3500 &card
->dapm
, &codec
->dapm
);
3502 list_for_each_entry(w
, &card
->widgets
, list
) {
3503 if (w
->dapm
!= dapm
)
3506 case snd_soc_dapm_input
:
3507 case snd_soc_dapm_output
:
3508 case snd_soc_dapm_micbias
:
3509 dev_dbg(codec
->dev
, "Auto NC: Checking widget %s\n",
3511 if (!snd_soc_dapm_widget_in_card_paths(card
, w
)) {
3513 "... Not in map; disabling\n");
3514 snd_soc_dapm_nc_pin(dapm
, w
->name
);
3524 * snd_soc_dapm_free - free dapm resources
3525 * @dapm: DAPM context
3527 * Free all dapm widgets and resources.
3529 void snd_soc_dapm_free(struct snd_soc_dapm_context
*dapm
)
3531 snd_soc_dapm_sys_remove(dapm
->dev
);
3532 dapm_debugfs_cleanup(dapm
);
3533 dapm_free_widgets(dapm
);
3534 list_del(&dapm
->list
);
3536 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
3538 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context
*dapm
)
3540 struct snd_soc_card
*card
= dapm
->card
;
3541 struct snd_soc_dapm_widget
*w
;
3542 LIST_HEAD(down_list
);
3545 mutex_lock(&card
->dapm_mutex
);
3547 list_for_each_entry(w
, &dapm
->card
->widgets
, list
) {
3548 if (w
->dapm
!= dapm
)
3551 dapm_seq_insert(w
, &down_list
, false);
3557 /* If there were no widgets to power down we're already in
3561 if (dapm
->bias_level
== SND_SOC_BIAS_ON
)
3562 snd_soc_dapm_set_bias_level(dapm
,
3563 SND_SOC_BIAS_PREPARE
);
3564 dapm_seq_run(dapm
, &down_list
, 0, false);
3565 if (dapm
->bias_level
== SND_SOC_BIAS_PREPARE
)
3566 snd_soc_dapm_set_bias_level(dapm
,
3567 SND_SOC_BIAS_STANDBY
);
3570 mutex_unlock(&card
->dapm_mutex
);
3574 * snd_soc_dapm_shutdown - callback for system shutdown
3576 void snd_soc_dapm_shutdown(struct snd_soc_card
*card
)
3578 struct snd_soc_codec
*codec
;
3580 list_for_each_entry(codec
, &card
->codec_dev_list
, list
) {
3581 soc_dapm_shutdown_codec(&codec
->dapm
);
3582 if (codec
->dapm
.bias_level
== SND_SOC_BIAS_STANDBY
)
3583 snd_soc_dapm_set_bias_level(&codec
->dapm
,
3588 /* Module information */
3589 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3590 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3591 MODULE_LICENSE("GPL");