x86/xen: resume timer irqs early
[linux/fpc-iii.git] / sound / soc / soc-dapm.c
blob4136cc25154ef8e3f38839da34344d5ce7e597e5
1 /*
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.
12 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
15 * DACs/ADCs.
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
20 * sinks, dacs, etc
21 * o Delayed power down of audio subsystem to reduce pops between a quick
22 * device reopen.
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>
31 #include <linux/pm.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,
52 const char *control,
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)
120 if (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, ...)
126 va_list args;
127 char *buf;
129 if (!pop_time)
130 return;
132 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
133 if (buf == NULL)
134 return;
136 va_start(args, fmt);
137 vsnprintf(buf, PAGE_SIZE, fmt, args);
138 dev_info(dev, "%s", buf);
139 va_end(args);
141 kfree(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",
153 w->name, reason);
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) {
167 switch (w->id) {
168 case snd_soc_dapm_input:
169 case snd_soc_dapm_output:
170 dapm_mark_dirty(w, "Rechecking inputs and outputs");
171 break;
172 default:
173 break;
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 {
189 unsigned int value;
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);
202 if (!data) {
203 dev_err(widget->dapm->dev,
204 "ASoC: can't allocate kcontrol data for %s\n",
205 widget->name);
206 return -ENOMEM;
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;
224 if (mc->invert)
225 template.off_val = mc->max;
226 else
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,
235 &template);
236 if (!data->widget) {
237 kfree(data);
238 return -ENOMEM;
241 break;
242 default:
243 break;
246 kcontrol->private_data = data;
248 return 0;
251 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
253 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
254 kfree(data->wlist);
255 kfree(data);
258 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
259 const struct snd_kcontrol *kcontrol)
261 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
263 return data->wlist;
266 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
267 struct snd_soc_dapm_widget *widget)
269 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
270 struct snd_soc_dapm_widget_list *new_wlist;
271 unsigned int n;
273 if (data->wlist)
274 n = data->wlist->num_widgets + 1;
275 else
276 n = 1;
278 new_wlist = krealloc(data->wlist,
279 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
280 if (!new_wlist)
281 return -ENOMEM;
283 new_wlist->widgets[n - 1] = widget;
284 new_wlist->num_widgets = n;
286 data->wlist = new_wlist;
288 return 0;
291 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
292 struct snd_soc_dapm_path *path)
294 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
296 list_add_tail(&path->list_kcontrol, &data->paths);
298 if (data->widget) {
299 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
300 path->source, NULL, NULL);
304 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
306 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
308 if (!data->widget)
309 return true;
311 return data->widget->power;
314 static struct list_head *dapm_kcontrol_get_path_list(
315 const struct snd_kcontrol *kcontrol)
317 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
319 return &data->paths;
322 #define dapm_kcontrol_for_each_path(path, kcontrol) \
323 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
324 list_kcontrol)
326 static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
328 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
330 return data->value;
333 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
334 unsigned int value)
336 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
338 if (data->value == value)
339 return false;
341 if (data->widget)
342 data->widget->on_val = value;
344 data->value = value;
346 return true;
350 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
351 * @kcontrol: The kcontrol
353 struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
355 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
357 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
359 static void dapm_reset(struct snd_soc_card *card)
361 struct snd_soc_dapm_widget *w;
363 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
365 list_for_each_entry(w, &card->widgets, list) {
366 w->new_power = w->power;
367 w->power_checked = false;
368 w->inputs = -1;
369 w->outputs = -1;
373 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
375 if (w->codec)
376 return snd_soc_read(w->codec, reg);
377 else if (w->platform)
378 return snd_soc_platform_read(w->platform, reg);
380 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
381 return -1;
384 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
386 if (w->codec)
387 return snd_soc_write(w->codec, reg, val);
388 else if (w->platform)
389 return snd_soc_platform_write(w->platform, reg, val);
391 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
392 return -1;
395 static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
397 if (w->codec && !w->codec->using_regmap)
398 mutex_lock(&w->codec->mutex);
399 else if (w->platform)
400 mutex_lock(&w->platform->mutex);
403 static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
405 if (w->codec && !w->codec->using_regmap)
406 mutex_unlock(&w->codec->mutex);
407 else if (w->platform)
408 mutex_unlock(&w->platform->mutex);
411 static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
412 unsigned short reg, unsigned int mask, unsigned int value)
414 bool change;
415 unsigned int old, new;
416 int ret;
418 if (w->codec && w->codec->using_regmap) {
419 ret = regmap_update_bits_check(w->codec->control_data,
420 reg, mask, value, &change);
421 if (ret != 0)
422 return ret;
423 } else {
424 soc_widget_lock(w);
425 ret = soc_widget_read(w, reg);
426 if (ret < 0) {
427 soc_widget_unlock(w);
428 return ret;
431 old = ret;
432 new = (old & ~mask) | (value & mask);
433 change = old != new;
434 if (change) {
435 ret = soc_widget_write(w, reg, new);
436 if (ret < 0) {
437 soc_widget_unlock(w);
438 return ret;
441 soc_widget_unlock(w);
444 return change;
448 * snd_soc_dapm_set_bias_level - set the bias level for the system
449 * @dapm: DAPM context
450 * @level: level to configure
452 * Configure the bias (power) levels for the SoC audio device.
454 * Returns 0 for success else error.
456 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
457 enum snd_soc_bias_level level)
459 struct snd_soc_card *card = dapm->card;
460 int ret = 0;
462 trace_snd_soc_bias_level_start(card, level);
464 if (card && card->set_bias_level)
465 ret = card->set_bias_level(card, dapm, level);
466 if (ret != 0)
467 goto out;
469 if (dapm->codec) {
470 if (dapm->codec->driver->set_bias_level)
471 ret = dapm->codec->driver->set_bias_level(dapm->codec,
472 level);
473 else
474 dapm->bias_level = level;
475 } else if (!card || dapm != &card->dapm) {
476 dapm->bias_level = level;
479 if (ret != 0)
480 goto out;
482 if (card && card->set_bias_level_post)
483 ret = card->set_bias_level_post(card, dapm, level);
484 out:
485 trace_snd_soc_bias_level_done(card, level);
487 return ret;
490 /* set up initial codec paths */
491 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
492 struct snd_soc_dapm_path *p, int i)
494 switch (w->id) {
495 case snd_soc_dapm_switch:
496 case snd_soc_dapm_mixer:
497 case snd_soc_dapm_mixer_named_ctl: {
498 int val;
499 struct soc_mixer_control *mc = (struct soc_mixer_control *)
500 w->kcontrol_news[i].private_value;
501 unsigned int reg = mc->reg;
502 unsigned int shift = mc->shift;
503 int max = mc->max;
504 unsigned int mask = (1 << fls(max)) - 1;
505 unsigned int invert = mc->invert;
507 val = soc_widget_read(w, reg);
508 val = (val >> shift) & mask;
509 if (invert)
510 val = max - val;
512 p->connect = !!val;
514 break;
515 case snd_soc_dapm_mux: {
516 struct soc_enum *e = (struct soc_enum *)
517 w->kcontrol_news[i].private_value;
518 int val, item;
520 val = soc_widget_read(w, e->reg);
521 item = (val >> e->shift_l) & e->mask;
523 if (item < e->max && !strcmp(p->name, e->texts[item]))
524 p->connect = 1;
525 else
526 p->connect = 0;
528 break;
529 case snd_soc_dapm_virt_mux: {
530 struct soc_enum *e = (struct soc_enum *)
531 w->kcontrol_news[i].private_value;
533 p->connect = 0;
534 /* since a virtual mux has no backing registers to
535 * decide which path to connect, it will try to match
536 * with the first enumeration. This is to ensure
537 * that the default mux choice (the first) will be
538 * correctly powered up during initialization.
540 if (!strcmp(p->name, e->texts[0]))
541 p->connect = 1;
543 break;
544 case snd_soc_dapm_value_mux: {
545 struct soc_enum *e = (struct soc_enum *)
546 w->kcontrol_news[i].private_value;
547 int val, item;
549 val = soc_widget_read(w, e->reg);
550 val = (val >> e->shift_l) & e->mask;
551 for (item = 0; item < e->max; item++) {
552 if (val == e->values[item])
553 break;
556 if (item < e->max && !strcmp(p->name, e->texts[item]))
557 p->connect = 1;
558 else
559 p->connect = 0;
561 break;
562 /* does not affect routing - always connected */
563 case snd_soc_dapm_pga:
564 case snd_soc_dapm_out_drv:
565 case snd_soc_dapm_output:
566 case snd_soc_dapm_adc:
567 case snd_soc_dapm_input:
568 case snd_soc_dapm_siggen:
569 case snd_soc_dapm_dac:
570 case snd_soc_dapm_micbias:
571 case snd_soc_dapm_vmid:
572 case snd_soc_dapm_supply:
573 case snd_soc_dapm_regulator_supply:
574 case snd_soc_dapm_clock_supply:
575 case snd_soc_dapm_aif_in:
576 case snd_soc_dapm_aif_out:
577 case snd_soc_dapm_dai_in:
578 case snd_soc_dapm_dai_out:
579 case snd_soc_dapm_hp:
580 case snd_soc_dapm_mic:
581 case snd_soc_dapm_spk:
582 case snd_soc_dapm_line:
583 case snd_soc_dapm_dai_link:
584 case snd_soc_dapm_kcontrol:
585 p->connect = 1;
586 break;
587 /* does affect routing - dynamically connected */
588 case snd_soc_dapm_pre:
589 case snd_soc_dapm_post:
590 p->connect = 0;
591 break;
595 /* connect mux widget to its interconnecting audio paths */
596 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
597 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
598 struct snd_soc_dapm_path *path, const char *control_name,
599 const struct snd_kcontrol_new *kcontrol)
601 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
602 int i;
604 for (i = 0; i < e->max; i++) {
605 if (!(strcmp(control_name, e->texts[i]))) {
606 list_add(&path->list, &dapm->card->paths);
607 list_add(&path->list_sink, &dest->sources);
608 list_add(&path->list_source, &src->sinks);
609 path->name = (char*)e->texts[i];
610 dapm_set_path_status(dest, path, 0);
611 return 0;
615 return -ENODEV;
618 /* connect mixer widget to its interconnecting audio paths */
619 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
620 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
621 struct snd_soc_dapm_path *path, const char *control_name)
623 int i;
625 /* search for mixer kcontrol */
626 for (i = 0; i < dest->num_kcontrols; i++) {
627 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
628 list_add(&path->list, &dapm->card->paths);
629 list_add(&path->list_sink, &dest->sources);
630 list_add(&path->list_source, &src->sinks);
631 path->name = dest->kcontrol_news[i].name;
632 dapm_set_path_status(dest, path, i);
633 return 0;
636 return -ENODEV;
639 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
640 struct snd_soc_dapm_widget *kcontrolw,
641 const struct snd_kcontrol_new *kcontrol_new,
642 struct snd_kcontrol **kcontrol)
644 struct snd_soc_dapm_widget *w;
645 int i;
647 *kcontrol = NULL;
649 list_for_each_entry(w, &dapm->card->widgets, list) {
650 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
651 continue;
652 for (i = 0; i < w->num_kcontrols; i++) {
653 if (&w->kcontrol_news[i] == kcontrol_new) {
654 if (w->kcontrols)
655 *kcontrol = w->kcontrols[i];
656 return 1;
661 return 0;
665 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
666 * create it. Either way, add the widget into the control's widget list
668 static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
669 int kci)
671 struct snd_soc_dapm_context *dapm = w->dapm;
672 struct snd_card *card = dapm->card->snd_card;
673 const char *prefix;
674 size_t prefix_len;
675 int shared;
676 struct snd_kcontrol *kcontrol;
677 bool wname_in_long_name, kcname_in_long_name;
678 char *long_name;
679 const char *name;
680 int ret;
682 if (dapm->codec)
683 prefix = dapm->codec->name_prefix;
684 else
685 prefix = NULL;
687 if (prefix)
688 prefix_len = strlen(prefix) + 1;
689 else
690 prefix_len = 0;
692 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
693 &kcontrol);
695 if (!kcontrol) {
696 if (shared) {
697 wname_in_long_name = false;
698 kcname_in_long_name = true;
699 } else {
700 switch (w->id) {
701 case snd_soc_dapm_switch:
702 case snd_soc_dapm_mixer:
703 wname_in_long_name = true;
704 kcname_in_long_name = true;
705 break;
706 case snd_soc_dapm_mixer_named_ctl:
707 wname_in_long_name = false;
708 kcname_in_long_name = true;
709 break;
710 case snd_soc_dapm_mux:
711 case snd_soc_dapm_virt_mux:
712 case snd_soc_dapm_value_mux:
713 wname_in_long_name = true;
714 kcname_in_long_name = false;
715 break;
716 default:
717 return -EINVAL;
721 if (wname_in_long_name && kcname_in_long_name) {
723 * The control will get a prefix from the control
724 * creation process but we're also using the same
725 * prefix for widgets so cut the prefix off the
726 * front of the widget name.
728 long_name = kasprintf(GFP_KERNEL, "%s %s",
729 w->name + prefix_len,
730 w->kcontrol_news[kci].name);
731 if (long_name == NULL)
732 return -ENOMEM;
734 name = long_name;
735 } else if (wname_in_long_name) {
736 long_name = NULL;
737 name = w->name + prefix_len;
738 } else {
739 long_name = NULL;
740 name = w->kcontrol_news[kci].name;
743 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
744 prefix);
745 kfree(long_name);
746 if (!kcontrol)
747 return -ENOMEM;
748 kcontrol->private_free = dapm_kcontrol_free;
750 ret = dapm_kcontrol_data_alloc(w, kcontrol);
751 if (ret) {
752 snd_ctl_free_one(kcontrol);
753 return ret;
756 ret = snd_ctl_add(card, kcontrol);
757 if (ret < 0) {
758 dev_err(dapm->dev,
759 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
760 w->name, name, ret);
761 return ret;
765 ret = dapm_kcontrol_add_widget(kcontrol, w);
766 if (ret)
767 return ret;
769 w->kcontrols[kci] = kcontrol;
771 return 0;
774 /* create new dapm mixer control */
775 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
777 int i, ret;
778 struct snd_soc_dapm_path *path;
780 /* add kcontrol */
781 for (i = 0; i < w->num_kcontrols; i++) {
782 /* match name */
783 list_for_each_entry(path, &w->sources, list_sink) {
784 /* mixer/mux paths name must match control name */
785 if (path->name != (char *)w->kcontrol_news[i].name)
786 continue;
788 if (w->kcontrols[i]) {
789 dapm_kcontrol_add_path(w->kcontrols[i], path);
790 continue;
793 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
794 if (ret < 0)
795 return ret;
797 dapm_kcontrol_add_path(w->kcontrols[i], path);
801 return 0;
804 /* create new dapm mux control */
805 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
807 struct snd_soc_dapm_context *dapm = w->dapm;
808 struct snd_soc_dapm_path *path;
809 int ret;
811 if (w->num_kcontrols != 1) {
812 dev_err(dapm->dev,
813 "ASoC: mux %s has incorrect number of controls\n",
814 w->name);
815 return -EINVAL;
818 if (list_empty(&w->sources)) {
819 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
820 return -EINVAL;
823 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
824 if (ret < 0)
825 return ret;
827 list_for_each_entry(path, &w->sources, list_sink)
828 dapm_kcontrol_add_path(w->kcontrols[0], path);
830 return 0;
833 /* create new dapm volume control */
834 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
836 if (w->num_kcontrols)
837 dev_err(w->dapm->dev,
838 "ASoC: PGA controls not supported: '%s'\n", w->name);
840 return 0;
843 /* reset 'walked' bit for each dapm path */
844 static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
845 struct list_head *sink)
847 struct snd_soc_dapm_path *p;
849 list_for_each_entry(p, sink, list_source) {
850 if (p->walked) {
851 p->walked = 0;
852 dapm_clear_walk_output(dapm, &p->sink->sinks);
857 static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
858 struct list_head *source)
860 struct snd_soc_dapm_path *p;
862 list_for_each_entry(p, source, list_sink) {
863 if (p->walked) {
864 p->walked = 0;
865 dapm_clear_walk_input(dapm, &p->source->sources);
871 /* We implement power down on suspend by checking the power state of
872 * the ALSA card - when we are suspending the ALSA state for the card
873 * is set to D3.
875 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
877 int level = snd_power_get_state(widget->dapm->card->snd_card);
879 switch (level) {
880 case SNDRV_CTL_POWER_D3hot:
881 case SNDRV_CTL_POWER_D3cold:
882 if (widget->ignore_suspend)
883 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
884 widget->name);
885 return widget->ignore_suspend;
886 default:
887 return 1;
891 /* add widget to list if it's not already in the list */
892 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
893 struct snd_soc_dapm_widget *w)
895 struct snd_soc_dapm_widget_list *wlist;
896 int wlistsize, wlistentries, i;
898 if (*list == NULL)
899 return -EINVAL;
901 wlist = *list;
903 /* is this widget already in the list */
904 for (i = 0; i < wlist->num_widgets; i++) {
905 if (wlist->widgets[i] == w)
906 return 0;
909 /* allocate some new space */
910 wlistentries = wlist->num_widgets + 1;
911 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
912 wlistentries * sizeof(struct snd_soc_dapm_widget *);
913 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
914 if (*list == NULL) {
915 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
916 w->name);
917 return -ENOMEM;
919 wlist = *list;
921 /* insert the widget */
922 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
923 w->name, wlist->num_widgets);
925 wlist->widgets[wlist->num_widgets] = w;
926 wlist->num_widgets++;
927 return 1;
931 * Recursively check for a completed path to an active or physically connected
932 * output widget. Returns number of complete paths.
934 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
935 struct snd_soc_dapm_widget_list **list)
937 struct snd_soc_dapm_path *path;
938 int con = 0;
940 if (widget->outputs >= 0)
941 return widget->outputs;
943 DAPM_UPDATE_STAT(widget, path_checks);
945 switch (widget->id) {
946 case snd_soc_dapm_supply:
947 case snd_soc_dapm_regulator_supply:
948 case snd_soc_dapm_clock_supply:
949 case snd_soc_dapm_kcontrol:
950 return 0;
951 default:
952 break;
955 switch (widget->id) {
956 case snd_soc_dapm_adc:
957 case snd_soc_dapm_aif_out:
958 case snd_soc_dapm_dai_out:
959 if (widget->active) {
960 widget->outputs = snd_soc_dapm_suspend_check(widget);
961 return widget->outputs;
963 default:
964 break;
967 if (widget->connected) {
968 /* connected pin ? */
969 if (widget->id == snd_soc_dapm_output && !widget->ext) {
970 widget->outputs = snd_soc_dapm_suspend_check(widget);
971 return widget->outputs;
974 /* connected jack or spk ? */
975 if (widget->id == snd_soc_dapm_hp ||
976 widget->id == snd_soc_dapm_spk ||
977 (widget->id == snd_soc_dapm_line &&
978 !list_empty(&widget->sources))) {
979 widget->outputs = snd_soc_dapm_suspend_check(widget);
980 return widget->outputs;
984 list_for_each_entry(path, &widget->sinks, list_source) {
985 DAPM_UPDATE_STAT(widget, neighbour_checks);
987 if (path->weak)
988 continue;
990 if (path->walking)
991 return 1;
993 if (path->walked)
994 continue;
996 trace_snd_soc_dapm_output_path(widget, path);
998 if (path->sink && path->connect) {
999 path->walked = 1;
1000 path->walking = 1;
1002 /* do we need to add this widget to the list ? */
1003 if (list) {
1004 int err;
1005 err = dapm_list_add_widget(list, path->sink);
1006 if (err < 0) {
1007 dev_err(widget->dapm->dev,
1008 "ASoC: could not add widget %s\n",
1009 widget->name);
1010 path->walking = 0;
1011 return con;
1015 con += is_connected_output_ep(path->sink, list);
1017 path->walking = 0;
1021 widget->outputs = con;
1023 return con;
1027 * Recursively check for a completed path to an active or physically connected
1028 * input widget. Returns number of complete paths.
1030 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1031 struct snd_soc_dapm_widget_list **list)
1033 struct snd_soc_dapm_path *path;
1034 int con = 0;
1036 if (widget->inputs >= 0)
1037 return widget->inputs;
1039 DAPM_UPDATE_STAT(widget, path_checks);
1041 switch (widget->id) {
1042 case snd_soc_dapm_supply:
1043 case snd_soc_dapm_regulator_supply:
1044 case snd_soc_dapm_clock_supply:
1045 case snd_soc_dapm_kcontrol:
1046 return 0;
1047 default:
1048 break;
1051 /* active stream ? */
1052 switch (widget->id) {
1053 case snd_soc_dapm_dac:
1054 case snd_soc_dapm_aif_in:
1055 case snd_soc_dapm_dai_in:
1056 if (widget->active) {
1057 widget->inputs = snd_soc_dapm_suspend_check(widget);
1058 return widget->inputs;
1060 default:
1061 break;
1064 if (widget->connected) {
1065 /* connected pin ? */
1066 if (widget->id == snd_soc_dapm_input && !widget->ext) {
1067 widget->inputs = snd_soc_dapm_suspend_check(widget);
1068 return widget->inputs;
1071 /* connected VMID/Bias for lower pops */
1072 if (widget->id == snd_soc_dapm_vmid) {
1073 widget->inputs = snd_soc_dapm_suspend_check(widget);
1074 return widget->inputs;
1077 /* connected jack ? */
1078 if (widget->id == snd_soc_dapm_mic ||
1079 (widget->id == snd_soc_dapm_line &&
1080 !list_empty(&widget->sinks))) {
1081 widget->inputs = snd_soc_dapm_suspend_check(widget);
1082 return widget->inputs;
1085 /* signal generator */
1086 if (widget->id == snd_soc_dapm_siggen) {
1087 widget->inputs = snd_soc_dapm_suspend_check(widget);
1088 return widget->inputs;
1092 list_for_each_entry(path, &widget->sources, list_sink) {
1093 DAPM_UPDATE_STAT(widget, neighbour_checks);
1095 if (path->weak)
1096 continue;
1098 if (path->walking)
1099 return 1;
1101 if (path->walked)
1102 continue;
1104 trace_snd_soc_dapm_input_path(widget, path);
1106 if (path->source && path->connect) {
1107 path->walked = 1;
1108 path->walking = 1;
1110 /* do we need to add this widget to the list ? */
1111 if (list) {
1112 int err;
1113 err = dapm_list_add_widget(list, path->source);
1114 if (err < 0) {
1115 dev_err(widget->dapm->dev,
1116 "ASoC: could not add widget %s\n",
1117 widget->name);
1118 path->walking = 0;
1119 return con;
1123 con += is_connected_input_ep(path->source, list);
1125 path->walking = 0;
1129 widget->inputs = con;
1131 return con;
1135 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1136 * @dai: the soc DAI.
1137 * @stream: stream direction.
1138 * @list: list of active widgets for this stream.
1140 * Queries DAPM graph as to whether an valid audio stream path exists for
1141 * the initial stream specified by name. This takes into account
1142 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1144 * Returns the number of valid paths or negative error.
1146 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1147 struct snd_soc_dapm_widget_list **list)
1149 struct snd_soc_card *card = dai->card;
1150 int paths;
1152 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1153 dapm_reset(card);
1155 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1156 paths = is_connected_output_ep(dai->playback_widget, list);
1157 dapm_clear_walk_output(&card->dapm,
1158 &dai->playback_widget->sinks);
1159 } else {
1160 paths = is_connected_input_ep(dai->capture_widget, list);
1161 dapm_clear_walk_input(&card->dapm,
1162 &dai->capture_widget->sources);
1165 trace_snd_soc_dapm_connected(paths, stream);
1166 mutex_unlock(&card->dapm_mutex);
1168 return paths;
1172 * Handler for generic register modifier widget.
1174 int dapm_reg_event(struct snd_soc_dapm_widget *w,
1175 struct snd_kcontrol *kcontrol, int event)
1177 unsigned int val;
1179 if (SND_SOC_DAPM_EVENT_ON(event))
1180 val = w->on_val;
1181 else
1182 val = w->off_val;
1184 soc_widget_update_bits_locked(w, -(w->reg + 1),
1185 w->mask << w->shift, val << w->shift);
1187 return 0;
1189 EXPORT_SYMBOL_GPL(dapm_reg_event);
1192 * Handler for regulator supply widget.
1194 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1195 struct snd_kcontrol *kcontrol, int event)
1197 int ret;
1199 if (SND_SOC_DAPM_EVENT_ON(event)) {
1200 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1201 ret = regulator_allow_bypass(w->regulator, false);
1202 if (ret != 0)
1203 dev_warn(w->dapm->dev,
1204 "ASoC: Failed to bypass %s: %d\n",
1205 w->name, ret);
1208 return regulator_enable(w->regulator);
1209 } else {
1210 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1211 ret = regulator_allow_bypass(w->regulator, true);
1212 if (ret != 0)
1213 dev_warn(w->dapm->dev,
1214 "ASoC: Failed to unbypass %s: %d\n",
1215 w->name, ret);
1218 return regulator_disable_deferred(w->regulator, w->shift);
1221 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1224 * Handler for clock supply widget.
1226 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1227 struct snd_kcontrol *kcontrol, int event)
1229 if (!w->clk)
1230 return -EIO;
1232 #ifdef CONFIG_HAVE_CLK
1233 if (SND_SOC_DAPM_EVENT_ON(event)) {
1234 return clk_prepare_enable(w->clk);
1235 } else {
1236 clk_disable_unprepare(w->clk);
1237 return 0;
1239 #endif
1240 return 0;
1242 EXPORT_SYMBOL_GPL(dapm_clock_event);
1244 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1246 if (w->power_checked)
1247 return w->new_power;
1249 if (w->force)
1250 w->new_power = 1;
1251 else
1252 w->new_power = w->power_check(w);
1254 w->power_checked = true;
1256 return w->new_power;
1259 /* Generic check to see if a widget should be powered.
1261 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1263 int in, out;
1265 DAPM_UPDATE_STAT(w, power_checks);
1267 in = is_connected_input_ep(w, NULL);
1268 dapm_clear_walk_input(w->dapm, &w->sources);
1269 out = is_connected_output_ep(w, NULL);
1270 dapm_clear_walk_output(w->dapm, &w->sinks);
1271 return out != 0 && in != 0;
1274 /* Check to see if an ADC has power */
1275 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1277 int in;
1279 DAPM_UPDATE_STAT(w, power_checks);
1281 if (w->active) {
1282 in = is_connected_input_ep(w, NULL);
1283 dapm_clear_walk_input(w->dapm, &w->sources);
1284 return in != 0;
1285 } else {
1286 return dapm_generic_check_power(w);
1290 /* Check to see if a DAC has power */
1291 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1293 int out;
1295 DAPM_UPDATE_STAT(w, power_checks);
1297 if (w->active) {
1298 out = is_connected_output_ep(w, NULL);
1299 dapm_clear_walk_output(w->dapm, &w->sinks);
1300 return out != 0;
1301 } else {
1302 return dapm_generic_check_power(w);
1306 /* Check to see if a power supply is needed */
1307 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1309 struct snd_soc_dapm_path *path;
1311 DAPM_UPDATE_STAT(w, power_checks);
1313 /* Check if one of our outputs is connected */
1314 list_for_each_entry(path, &w->sinks, list_source) {
1315 DAPM_UPDATE_STAT(w, neighbour_checks);
1317 if (path->weak)
1318 continue;
1320 if (path->connected &&
1321 !path->connected(path->source, path->sink))
1322 continue;
1324 if (!path->sink)
1325 continue;
1327 if (dapm_widget_power_check(path->sink))
1328 return 1;
1331 return 0;
1334 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1336 return 1;
1339 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1340 struct snd_soc_dapm_widget *b,
1341 bool power_up)
1343 int *sort;
1345 if (power_up)
1346 sort = dapm_up_seq;
1347 else
1348 sort = dapm_down_seq;
1350 if (sort[a->id] != sort[b->id])
1351 return sort[a->id] - sort[b->id];
1352 if (a->subseq != b->subseq) {
1353 if (power_up)
1354 return a->subseq - b->subseq;
1355 else
1356 return b->subseq - a->subseq;
1358 if (a->reg != b->reg)
1359 return a->reg - b->reg;
1360 if (a->dapm != b->dapm)
1361 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1363 return 0;
1366 /* Insert a widget in order into a DAPM power sequence. */
1367 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1368 struct list_head *list,
1369 bool power_up)
1371 struct snd_soc_dapm_widget *w;
1373 list_for_each_entry(w, list, power_list)
1374 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1375 list_add_tail(&new_widget->power_list, &w->power_list);
1376 return;
1379 list_add_tail(&new_widget->power_list, list);
1382 static void dapm_seq_check_event(struct snd_soc_card *card,
1383 struct snd_soc_dapm_widget *w, int event)
1385 const char *ev_name;
1386 int power, ret;
1388 switch (event) {
1389 case SND_SOC_DAPM_PRE_PMU:
1390 ev_name = "PRE_PMU";
1391 power = 1;
1392 break;
1393 case SND_SOC_DAPM_POST_PMU:
1394 ev_name = "POST_PMU";
1395 power = 1;
1396 break;
1397 case SND_SOC_DAPM_PRE_PMD:
1398 ev_name = "PRE_PMD";
1399 power = 0;
1400 break;
1401 case SND_SOC_DAPM_POST_PMD:
1402 ev_name = "POST_PMD";
1403 power = 0;
1404 break;
1405 case SND_SOC_DAPM_WILL_PMU:
1406 ev_name = "WILL_PMU";
1407 power = 1;
1408 break;
1409 case SND_SOC_DAPM_WILL_PMD:
1410 ev_name = "WILL_PMD";
1411 power = 0;
1412 break;
1413 default:
1414 BUG();
1415 return;
1418 if (w->new_power != power)
1419 return;
1421 if (w->event && (w->event_flags & event)) {
1422 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1423 w->name, ev_name);
1424 trace_snd_soc_dapm_widget_event_start(w, event);
1425 ret = w->event(w, NULL, event);
1426 trace_snd_soc_dapm_widget_event_done(w, event);
1427 if (ret < 0)
1428 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1429 ev_name, w->name, ret);
1433 /* Apply the coalesced changes from a DAPM sequence */
1434 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1435 struct list_head *pending)
1437 struct snd_soc_dapm_widget *w;
1438 int reg;
1439 unsigned int value = 0;
1440 unsigned int mask = 0;
1442 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1443 power_list)->reg;
1445 list_for_each_entry(w, pending, power_list) {
1446 BUG_ON(reg != w->reg);
1447 w->power = w->new_power;
1449 mask |= w->mask << w->shift;
1450 if (w->power)
1451 value |= w->on_val << w->shift;
1452 else
1453 value |= w->off_val << w->shift;
1455 pop_dbg(w->dapm->dev, card->pop_time,
1456 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1457 w->name, reg, value, mask);
1459 /* Check for events */
1460 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1461 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1464 if (reg >= 0) {
1465 /* Any widget will do, they should all be updating the
1466 * same register.
1468 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1469 power_list);
1471 pop_dbg(w->dapm->dev, card->pop_time,
1472 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1473 value, mask, reg, card->pop_time);
1474 pop_wait(card->pop_time);
1475 soc_widget_update_bits_locked(w, reg, mask, value);
1478 list_for_each_entry(w, pending, power_list) {
1479 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1480 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1484 /* Apply a DAPM power sequence.
1486 * We walk over a pre-sorted list of widgets to apply power to. In
1487 * order to minimise the number of writes to the device required
1488 * multiple widgets will be updated in a single write where possible.
1489 * Currently anything that requires more than a single write is not
1490 * handled.
1492 static void dapm_seq_run(struct snd_soc_card *card,
1493 struct list_head *list, int event, bool power_up)
1495 struct snd_soc_dapm_widget *w, *n;
1496 LIST_HEAD(pending);
1497 int cur_sort = -1;
1498 int cur_subseq = -1;
1499 int cur_reg = SND_SOC_NOPM;
1500 struct snd_soc_dapm_context *cur_dapm = NULL;
1501 int ret, i;
1502 int *sort;
1504 if (power_up)
1505 sort = dapm_up_seq;
1506 else
1507 sort = dapm_down_seq;
1509 list_for_each_entry_safe(w, n, list, power_list) {
1510 ret = 0;
1512 /* Do we need to apply any queued changes? */
1513 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1514 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1515 if (!list_empty(&pending))
1516 dapm_seq_run_coalesced(card, &pending);
1518 if (cur_dapm && cur_dapm->seq_notifier) {
1519 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1520 if (sort[i] == cur_sort)
1521 cur_dapm->seq_notifier(cur_dapm,
1523 cur_subseq);
1526 INIT_LIST_HEAD(&pending);
1527 cur_sort = -1;
1528 cur_subseq = INT_MIN;
1529 cur_reg = SND_SOC_NOPM;
1530 cur_dapm = NULL;
1533 switch (w->id) {
1534 case snd_soc_dapm_pre:
1535 if (!w->event)
1536 list_for_each_entry_safe_continue(w, n, list,
1537 power_list);
1539 if (event == SND_SOC_DAPM_STREAM_START)
1540 ret = w->event(w,
1541 NULL, SND_SOC_DAPM_PRE_PMU);
1542 else if (event == SND_SOC_DAPM_STREAM_STOP)
1543 ret = w->event(w,
1544 NULL, SND_SOC_DAPM_PRE_PMD);
1545 break;
1547 case snd_soc_dapm_post:
1548 if (!w->event)
1549 list_for_each_entry_safe_continue(w, n, list,
1550 power_list);
1552 if (event == SND_SOC_DAPM_STREAM_START)
1553 ret = w->event(w,
1554 NULL, SND_SOC_DAPM_POST_PMU);
1555 else if (event == SND_SOC_DAPM_STREAM_STOP)
1556 ret = w->event(w,
1557 NULL, SND_SOC_DAPM_POST_PMD);
1558 break;
1560 default:
1561 /* Queue it up for application */
1562 cur_sort = sort[w->id];
1563 cur_subseq = w->subseq;
1564 cur_reg = w->reg;
1565 cur_dapm = w->dapm;
1566 list_move(&w->power_list, &pending);
1567 break;
1570 if (ret < 0)
1571 dev_err(w->dapm->dev,
1572 "ASoC: Failed to apply widget power: %d\n", ret);
1575 if (!list_empty(&pending))
1576 dapm_seq_run_coalesced(card, &pending);
1578 if (cur_dapm && cur_dapm->seq_notifier) {
1579 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1580 if (sort[i] == cur_sort)
1581 cur_dapm->seq_notifier(cur_dapm,
1582 i, cur_subseq);
1586 static void dapm_widget_update(struct snd_soc_card *card)
1588 struct snd_soc_dapm_update *update = card->update;
1589 struct snd_soc_dapm_widget_list *wlist;
1590 struct snd_soc_dapm_widget *w = NULL;
1591 unsigned int wi;
1592 int ret;
1594 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1595 return;
1597 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1599 for (wi = 0; wi < wlist->num_widgets; wi++) {
1600 w = wlist->widgets[wi];
1602 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1603 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1604 if (ret != 0)
1605 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1606 w->name, ret);
1610 if (!w)
1611 return;
1613 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
1614 update->val);
1615 if (ret < 0)
1616 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1617 w->name, ret);
1619 for (wi = 0; wi < wlist->num_widgets; wi++) {
1620 w = wlist->widgets[wi];
1622 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1623 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1624 if (ret != 0)
1625 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1626 w->name, ret);
1631 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1632 * they're changing state.
1634 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1636 struct snd_soc_dapm_context *d = data;
1637 int ret;
1639 /* If we're off and we're not supposed to be go into STANDBY */
1640 if (d->bias_level == SND_SOC_BIAS_OFF &&
1641 d->target_bias_level != SND_SOC_BIAS_OFF) {
1642 if (d->dev)
1643 pm_runtime_get_sync(d->dev);
1645 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1646 if (ret != 0)
1647 dev_err(d->dev,
1648 "ASoC: Failed to turn on bias: %d\n", ret);
1651 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1652 if (d->bias_level != d->target_bias_level) {
1653 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1654 if (ret != 0)
1655 dev_err(d->dev,
1656 "ASoC: Failed to prepare bias: %d\n", ret);
1660 /* Async callback run prior to DAPM sequences - brings to their final
1661 * state.
1663 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1665 struct snd_soc_dapm_context *d = data;
1666 int ret;
1668 /* If we just powered the last thing off drop to standby bias */
1669 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1670 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1671 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1672 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1673 if (ret != 0)
1674 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1675 ret);
1678 /* If we're in standby and can support bias off then do that */
1679 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1680 d->target_bias_level == SND_SOC_BIAS_OFF) {
1681 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1682 if (ret != 0)
1683 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1684 ret);
1686 if (d->dev)
1687 pm_runtime_put(d->dev);
1690 /* If we just powered up then move to active bias */
1691 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1692 d->target_bias_level == SND_SOC_BIAS_ON) {
1693 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1694 if (ret != 0)
1695 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1696 ret);
1700 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1701 bool power, bool connect)
1703 /* If a connection is being made or broken then that update
1704 * will have marked the peer dirty, otherwise the widgets are
1705 * not connected and this update has no impact. */
1706 if (!connect)
1707 return;
1709 /* If the peer is already in the state we're moving to then we
1710 * won't have an impact on it. */
1711 if (power != peer->power)
1712 dapm_mark_dirty(peer, "peer state change");
1715 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1716 struct list_head *up_list,
1717 struct list_head *down_list)
1719 struct snd_soc_dapm_path *path;
1721 if (w->power == power)
1722 return;
1724 trace_snd_soc_dapm_widget_power(w, power);
1726 /* If we changed our power state perhaps our neigbours changed
1727 * also.
1729 list_for_each_entry(path, &w->sources, list_sink) {
1730 if (path->source) {
1731 dapm_widget_set_peer_power(path->source, power,
1732 path->connect);
1735 switch (w->id) {
1736 case snd_soc_dapm_supply:
1737 case snd_soc_dapm_regulator_supply:
1738 case snd_soc_dapm_clock_supply:
1739 case snd_soc_dapm_kcontrol:
1740 /* Supplies can't affect their outputs, only their inputs */
1741 break;
1742 default:
1743 list_for_each_entry(path, &w->sinks, list_source) {
1744 if (path->sink) {
1745 dapm_widget_set_peer_power(path->sink, power,
1746 path->connect);
1749 break;
1752 if (power)
1753 dapm_seq_insert(w, up_list, true);
1754 else
1755 dapm_seq_insert(w, down_list, false);
1758 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1759 struct list_head *up_list,
1760 struct list_head *down_list)
1762 int power;
1764 switch (w->id) {
1765 case snd_soc_dapm_pre:
1766 dapm_seq_insert(w, down_list, false);
1767 break;
1768 case snd_soc_dapm_post:
1769 dapm_seq_insert(w, up_list, true);
1770 break;
1772 default:
1773 power = dapm_widget_power_check(w);
1775 dapm_widget_set_power(w, power, up_list, down_list);
1776 break;
1781 * Scan each dapm widget for complete audio path.
1782 * A complete path is a route that has valid endpoints i.e.:-
1784 * o DAC to output pin.
1785 * o Input Pin to ADC.
1786 * o Input pin to Output pin (bypass, sidetone)
1787 * o DAC to ADC (loopback).
1789 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1791 struct snd_soc_dapm_widget *w;
1792 struct snd_soc_dapm_context *d;
1793 LIST_HEAD(up_list);
1794 LIST_HEAD(down_list);
1795 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1796 enum snd_soc_bias_level bias;
1798 trace_snd_soc_dapm_start(card);
1800 list_for_each_entry(d, &card->dapm_list, list) {
1801 if (d->idle_bias_off)
1802 d->target_bias_level = SND_SOC_BIAS_OFF;
1803 else
1804 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1807 dapm_reset(card);
1809 /* Check which widgets we need to power and store them in
1810 * lists indicating if they should be powered up or down. We
1811 * only check widgets that have been flagged as dirty but note
1812 * that new widgets may be added to the dirty list while we
1813 * iterate.
1815 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1816 dapm_power_one_widget(w, &up_list, &down_list);
1819 list_for_each_entry(w, &card->widgets, list) {
1820 switch (w->id) {
1821 case snd_soc_dapm_pre:
1822 case snd_soc_dapm_post:
1823 /* These widgets always need to be powered */
1824 break;
1825 default:
1826 list_del_init(&w->dirty);
1827 break;
1830 if (w->new_power) {
1831 d = w->dapm;
1833 /* Supplies and micbiases only bring the
1834 * context up to STANDBY as unless something
1835 * else is active and passing audio they
1836 * generally don't require full power. Signal
1837 * generators are virtual pins and have no
1838 * power impact themselves.
1840 switch (w->id) {
1841 case snd_soc_dapm_siggen:
1842 break;
1843 case snd_soc_dapm_supply:
1844 case snd_soc_dapm_regulator_supply:
1845 case snd_soc_dapm_clock_supply:
1846 case snd_soc_dapm_micbias:
1847 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1848 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1849 break;
1850 default:
1851 d->target_bias_level = SND_SOC_BIAS_ON;
1852 break;
1858 /* Force all contexts in the card to the same bias state if
1859 * they're not ground referenced.
1861 bias = SND_SOC_BIAS_OFF;
1862 list_for_each_entry(d, &card->dapm_list, list)
1863 if (d->target_bias_level > bias)
1864 bias = d->target_bias_level;
1865 list_for_each_entry(d, &card->dapm_list, list)
1866 if (!d->idle_bias_off)
1867 d->target_bias_level = bias;
1869 trace_snd_soc_dapm_walk_done(card);
1871 /* Run all the bias changes in parallel */
1872 list_for_each_entry(d, &card->dapm_list, list)
1873 async_schedule_domain(dapm_pre_sequence_async, d,
1874 &async_domain);
1875 async_synchronize_full_domain(&async_domain);
1877 list_for_each_entry(w, &down_list, power_list) {
1878 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1881 list_for_each_entry(w, &up_list, power_list) {
1882 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1885 /* Power down widgets first; try to avoid amplifying pops. */
1886 dapm_seq_run(card, &down_list, event, false);
1888 dapm_widget_update(card);
1890 /* Now power up. */
1891 dapm_seq_run(card, &up_list, event, true);
1893 /* Run all the bias changes in parallel */
1894 list_for_each_entry(d, &card->dapm_list, list)
1895 async_schedule_domain(dapm_post_sequence_async, d,
1896 &async_domain);
1897 async_synchronize_full_domain(&async_domain);
1899 /* do we need to notify any clients that DAPM event is complete */
1900 list_for_each_entry(d, &card->dapm_list, list) {
1901 if (d->stream_event)
1902 d->stream_event(d, event);
1905 pop_dbg(card->dev, card->pop_time,
1906 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1907 pop_wait(card->pop_time);
1909 trace_snd_soc_dapm_done(card);
1911 return 0;
1914 #ifdef CONFIG_DEBUG_FS
1915 static ssize_t dapm_widget_power_read_file(struct file *file,
1916 char __user *user_buf,
1917 size_t count, loff_t *ppos)
1919 struct snd_soc_dapm_widget *w = file->private_data;
1920 char *buf;
1921 int in, out;
1922 ssize_t ret;
1923 struct snd_soc_dapm_path *p = NULL;
1925 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1926 if (!buf)
1927 return -ENOMEM;
1929 in = is_connected_input_ep(w, NULL);
1930 dapm_clear_walk_input(w->dapm, &w->sources);
1931 out = is_connected_output_ep(w, NULL);
1932 dapm_clear_walk_output(w->dapm, &w->sinks);
1934 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1935 w->name, w->power ? "On" : "Off",
1936 w->force ? " (forced)" : "", in, out);
1938 if (w->reg >= 0)
1939 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1940 " - R%d(0x%x) mask 0x%x",
1941 w->reg, w->reg, w->mask << w->shift);
1943 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1945 if (w->sname)
1946 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1947 w->sname,
1948 w->active ? "active" : "inactive");
1950 list_for_each_entry(p, &w->sources, list_sink) {
1951 if (p->connected && !p->connected(w, p->source))
1952 continue;
1954 if (p->connect)
1955 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1956 " in \"%s\" \"%s\"\n",
1957 p->name ? p->name : "static",
1958 p->source->name);
1960 list_for_each_entry(p, &w->sinks, list_source) {
1961 if (p->connected && !p->connected(w, p->sink))
1962 continue;
1964 if (p->connect)
1965 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1966 " out \"%s\" \"%s\"\n",
1967 p->name ? p->name : "static",
1968 p->sink->name);
1971 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1973 kfree(buf);
1974 return ret;
1977 static const struct file_operations dapm_widget_power_fops = {
1978 .open = simple_open,
1979 .read = dapm_widget_power_read_file,
1980 .llseek = default_llseek,
1983 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1984 size_t count, loff_t *ppos)
1986 struct snd_soc_dapm_context *dapm = file->private_data;
1987 char *level;
1989 switch (dapm->bias_level) {
1990 case SND_SOC_BIAS_ON:
1991 level = "On\n";
1992 break;
1993 case SND_SOC_BIAS_PREPARE:
1994 level = "Prepare\n";
1995 break;
1996 case SND_SOC_BIAS_STANDBY:
1997 level = "Standby\n";
1998 break;
1999 case SND_SOC_BIAS_OFF:
2000 level = "Off\n";
2001 break;
2002 default:
2003 BUG();
2004 level = "Unknown\n";
2005 break;
2008 return simple_read_from_buffer(user_buf, count, ppos, level,
2009 strlen(level));
2012 static const struct file_operations dapm_bias_fops = {
2013 .open = simple_open,
2014 .read = dapm_bias_read_file,
2015 .llseek = default_llseek,
2018 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2019 struct dentry *parent)
2021 struct dentry *d;
2023 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2025 if (!dapm->debugfs_dapm) {
2026 dev_warn(dapm->dev,
2027 "ASoC: Failed to create DAPM debugfs directory\n");
2028 return;
2031 d = debugfs_create_file("bias_level", 0444,
2032 dapm->debugfs_dapm, dapm,
2033 &dapm_bias_fops);
2034 if (!d)
2035 dev_warn(dapm->dev,
2036 "ASoC: Failed to create bias level debugfs file\n");
2039 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2041 struct snd_soc_dapm_context *dapm = w->dapm;
2042 struct dentry *d;
2044 if (!dapm->debugfs_dapm || !w->name)
2045 return;
2047 d = debugfs_create_file(w->name, 0444,
2048 dapm->debugfs_dapm, w,
2049 &dapm_widget_power_fops);
2050 if (!d)
2051 dev_warn(w->dapm->dev,
2052 "ASoC: Failed to create %s debugfs file\n",
2053 w->name);
2056 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2058 debugfs_remove_recursive(dapm->debugfs_dapm);
2061 #else
2062 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2063 struct dentry *parent)
2067 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2071 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2075 #endif
2077 /* test and update the power status of a mux widget */
2078 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2079 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2081 struct snd_soc_dapm_path *path;
2082 int found = 0;
2084 /* find dapm widget path assoc with kcontrol */
2085 dapm_kcontrol_for_each_path(path, kcontrol) {
2086 if (!path->name || !e->texts[mux])
2087 continue;
2089 found = 1;
2090 /* we now need to match the string in the enum to the path */
2091 if (!(strcmp(path->name, e->texts[mux]))) {
2092 path->connect = 1; /* new connection */
2093 dapm_mark_dirty(path->source, "mux connection");
2094 } else {
2095 if (path->connect)
2096 dapm_mark_dirty(path->source,
2097 "mux disconnection");
2098 path->connect = 0; /* old connection must be powered down */
2100 dapm_mark_dirty(path->sink, "mux change");
2103 if (found)
2104 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2106 return found;
2109 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2110 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2111 struct snd_soc_dapm_update *update)
2113 struct snd_soc_card *card = dapm->card;
2114 int ret;
2116 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2117 card->update = update;
2118 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2119 card->update = NULL;
2120 mutex_unlock(&card->dapm_mutex);
2121 if (ret > 0)
2122 soc_dpcm_runtime_update(card);
2123 return ret;
2125 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2127 /* test and update the power status of a mixer or switch widget */
2128 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2129 struct snd_kcontrol *kcontrol, int connect)
2131 struct snd_soc_dapm_path *path;
2132 int found = 0;
2134 /* find dapm widget path assoc with kcontrol */
2135 dapm_kcontrol_for_each_path(path, kcontrol) {
2136 found = 1;
2137 path->connect = connect;
2138 dapm_mark_dirty(path->source, "mixer connection");
2139 dapm_mark_dirty(path->sink, "mixer update");
2142 if (found)
2143 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2145 return found;
2148 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2149 struct snd_kcontrol *kcontrol, int connect,
2150 struct snd_soc_dapm_update *update)
2152 struct snd_soc_card *card = dapm->card;
2153 int ret;
2155 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2156 card->update = update;
2157 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2158 card->update = NULL;
2159 mutex_unlock(&card->dapm_mutex);
2160 if (ret > 0)
2161 soc_dpcm_runtime_update(card);
2162 return ret;
2164 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2166 /* show dapm widget status in sys fs */
2167 static ssize_t dapm_widget_show(struct device *dev,
2168 struct device_attribute *attr, char *buf)
2170 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2171 struct snd_soc_codec *codec =rtd->codec;
2172 struct snd_soc_dapm_widget *w;
2173 int count = 0;
2174 char *state = "not set";
2176 list_for_each_entry(w, &codec->card->widgets, list) {
2177 if (w->dapm != &codec->dapm)
2178 continue;
2180 /* only display widgets that burnm power */
2181 switch (w->id) {
2182 case snd_soc_dapm_hp:
2183 case snd_soc_dapm_mic:
2184 case snd_soc_dapm_spk:
2185 case snd_soc_dapm_line:
2186 case snd_soc_dapm_micbias:
2187 case snd_soc_dapm_dac:
2188 case snd_soc_dapm_adc:
2189 case snd_soc_dapm_pga:
2190 case snd_soc_dapm_out_drv:
2191 case snd_soc_dapm_mixer:
2192 case snd_soc_dapm_mixer_named_ctl:
2193 case snd_soc_dapm_supply:
2194 case snd_soc_dapm_regulator_supply:
2195 case snd_soc_dapm_clock_supply:
2196 if (w->name)
2197 count += sprintf(buf + count, "%s: %s\n",
2198 w->name, w->power ? "On":"Off");
2199 break;
2200 default:
2201 break;
2205 switch (codec->dapm.bias_level) {
2206 case SND_SOC_BIAS_ON:
2207 state = "On";
2208 break;
2209 case SND_SOC_BIAS_PREPARE:
2210 state = "Prepare";
2211 break;
2212 case SND_SOC_BIAS_STANDBY:
2213 state = "Standby";
2214 break;
2215 case SND_SOC_BIAS_OFF:
2216 state = "Off";
2217 break;
2219 count += sprintf(buf + count, "PM State: %s\n", state);
2221 return count;
2224 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2226 int snd_soc_dapm_sys_add(struct device *dev)
2228 return device_create_file(dev, &dev_attr_dapm_widget);
2231 static void snd_soc_dapm_sys_remove(struct device *dev)
2233 device_remove_file(dev, &dev_attr_dapm_widget);
2236 static void dapm_free_path(struct snd_soc_dapm_path *path)
2238 list_del(&path->list_sink);
2239 list_del(&path->list_source);
2240 list_del(&path->list_kcontrol);
2241 list_del(&path->list);
2242 kfree(path);
2245 /* free all dapm widgets and resources */
2246 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2248 struct snd_soc_dapm_widget *w, *next_w;
2249 struct snd_soc_dapm_path *p, *next_p;
2251 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2252 if (w->dapm != dapm)
2253 continue;
2254 list_del(&w->list);
2256 * remove source and sink paths associated to this widget.
2257 * While removing the path, remove reference to it from both
2258 * source and sink widgets so that path is removed only once.
2260 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2261 dapm_free_path(p);
2263 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2264 dapm_free_path(p);
2266 kfree(w->kcontrols);
2267 kfree(w->name);
2268 kfree(w);
2272 static struct snd_soc_dapm_widget *dapm_find_widget(
2273 struct snd_soc_dapm_context *dapm, const char *pin,
2274 bool search_other_contexts)
2276 struct snd_soc_dapm_widget *w;
2277 struct snd_soc_dapm_widget *fallback = NULL;
2279 list_for_each_entry(w, &dapm->card->widgets, list) {
2280 if (!strcmp(w->name, pin)) {
2281 if (w->dapm == dapm)
2282 return w;
2283 else
2284 fallback = w;
2288 if (search_other_contexts)
2289 return fallback;
2291 return NULL;
2294 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2295 const char *pin, int status)
2297 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2299 if (!w) {
2300 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2301 return -EINVAL;
2304 if (w->connected != status)
2305 dapm_mark_dirty(w, "pin configuration");
2307 w->connected = status;
2308 if (status == 0)
2309 w->force = 0;
2311 return 0;
2315 * snd_soc_dapm_sync - scan and power dapm paths
2316 * @dapm: DAPM context
2318 * Walks all dapm audio paths and powers widgets according to their
2319 * stream or path usage.
2321 * Returns 0 for success.
2323 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2325 int ret;
2328 * Suppress early reports (eg, jacks syncing their state) to avoid
2329 * silly DAPM runs during card startup.
2331 if (!dapm->card || !dapm->card->instantiated)
2332 return 0;
2334 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2335 ret = dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2336 mutex_unlock(&dapm->card->dapm_mutex);
2337 return ret;
2339 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2341 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2342 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2343 const char *control,
2344 int (*connected)(struct snd_soc_dapm_widget *source,
2345 struct snd_soc_dapm_widget *sink))
2347 struct snd_soc_dapm_path *path;
2348 int ret;
2350 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2351 if (!path)
2352 return -ENOMEM;
2354 path->source = wsource;
2355 path->sink = wsink;
2356 path->connected = connected;
2357 INIT_LIST_HEAD(&path->list);
2358 INIT_LIST_HEAD(&path->list_kcontrol);
2359 INIT_LIST_HEAD(&path->list_source);
2360 INIT_LIST_HEAD(&path->list_sink);
2362 /* check for external widgets */
2363 if (wsink->id == snd_soc_dapm_input) {
2364 if (wsource->id == snd_soc_dapm_micbias ||
2365 wsource->id == snd_soc_dapm_mic ||
2366 wsource->id == snd_soc_dapm_line ||
2367 wsource->id == snd_soc_dapm_output)
2368 wsink->ext = 1;
2370 if (wsource->id == snd_soc_dapm_output) {
2371 if (wsink->id == snd_soc_dapm_spk ||
2372 wsink->id == snd_soc_dapm_hp ||
2373 wsink->id == snd_soc_dapm_line ||
2374 wsink->id == snd_soc_dapm_input)
2375 wsource->ext = 1;
2378 dapm_mark_dirty(wsource, "Route added");
2379 dapm_mark_dirty(wsink, "Route added");
2381 /* connect static paths */
2382 if (control == NULL) {
2383 list_add(&path->list, &dapm->card->paths);
2384 list_add(&path->list_sink, &wsink->sources);
2385 list_add(&path->list_source, &wsource->sinks);
2386 path->connect = 1;
2387 return 0;
2390 /* connect dynamic paths */
2391 switch (wsink->id) {
2392 case snd_soc_dapm_adc:
2393 case snd_soc_dapm_dac:
2394 case snd_soc_dapm_pga:
2395 case snd_soc_dapm_out_drv:
2396 case snd_soc_dapm_input:
2397 case snd_soc_dapm_output:
2398 case snd_soc_dapm_siggen:
2399 case snd_soc_dapm_micbias:
2400 case snd_soc_dapm_vmid:
2401 case snd_soc_dapm_pre:
2402 case snd_soc_dapm_post:
2403 case snd_soc_dapm_supply:
2404 case snd_soc_dapm_regulator_supply:
2405 case snd_soc_dapm_clock_supply:
2406 case snd_soc_dapm_aif_in:
2407 case snd_soc_dapm_aif_out:
2408 case snd_soc_dapm_dai_in:
2409 case snd_soc_dapm_dai_out:
2410 case snd_soc_dapm_dai_link:
2411 case snd_soc_dapm_kcontrol:
2412 list_add(&path->list, &dapm->card->paths);
2413 list_add(&path->list_sink, &wsink->sources);
2414 list_add(&path->list_source, &wsource->sinks);
2415 path->connect = 1;
2416 return 0;
2417 case snd_soc_dapm_mux:
2418 case snd_soc_dapm_virt_mux:
2419 case snd_soc_dapm_value_mux:
2420 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2421 &wsink->kcontrol_news[0]);
2422 if (ret != 0)
2423 goto err;
2424 break;
2425 case snd_soc_dapm_switch:
2426 case snd_soc_dapm_mixer:
2427 case snd_soc_dapm_mixer_named_ctl:
2428 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2429 if (ret != 0)
2430 goto err;
2431 break;
2432 case snd_soc_dapm_hp:
2433 case snd_soc_dapm_mic:
2434 case snd_soc_dapm_line:
2435 case snd_soc_dapm_spk:
2436 list_add(&path->list, &dapm->card->paths);
2437 list_add(&path->list_sink, &wsink->sources);
2438 list_add(&path->list_source, &wsource->sinks);
2439 path->connect = 0;
2440 return 0;
2443 return 0;
2444 err:
2445 kfree(path);
2446 return ret;
2449 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2450 const struct snd_soc_dapm_route *route)
2452 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2453 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2454 const char *sink;
2455 const char *source;
2456 char prefixed_sink[80];
2457 char prefixed_source[80];
2458 int ret;
2460 if (dapm->codec && dapm->codec->name_prefix) {
2461 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2462 dapm->codec->name_prefix, route->sink);
2463 sink = prefixed_sink;
2464 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2465 dapm->codec->name_prefix, route->source);
2466 source = prefixed_source;
2467 } else {
2468 sink = route->sink;
2469 source = route->source;
2473 * find src and dest widgets over all widgets but favor a widget from
2474 * current DAPM context
2476 list_for_each_entry(w, &dapm->card->widgets, list) {
2477 if (!wsink && !(strcmp(w->name, sink))) {
2478 wtsink = w;
2479 if (w->dapm == dapm)
2480 wsink = w;
2481 continue;
2483 if (!wsource && !(strcmp(w->name, source))) {
2484 wtsource = w;
2485 if (w->dapm == dapm)
2486 wsource = w;
2489 /* use widget from another DAPM context if not found from this */
2490 if (!wsink)
2491 wsink = wtsink;
2492 if (!wsource)
2493 wsource = wtsource;
2495 if (wsource == NULL) {
2496 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2497 route->source);
2498 return -ENODEV;
2500 if (wsink == NULL) {
2501 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2502 route->sink);
2503 return -ENODEV;
2506 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2507 route->connected);
2508 if (ret)
2509 goto err;
2511 return 0;
2512 err:
2513 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2514 source, route->control, sink);
2515 return ret;
2518 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2519 const struct snd_soc_dapm_route *route)
2521 struct snd_soc_dapm_path *path, *p;
2522 const char *sink;
2523 const char *source;
2524 char prefixed_sink[80];
2525 char prefixed_source[80];
2527 if (route->control) {
2528 dev_err(dapm->dev,
2529 "ASoC: Removal of routes with controls not supported\n");
2530 return -EINVAL;
2533 if (dapm->codec && dapm->codec->name_prefix) {
2534 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2535 dapm->codec->name_prefix, route->sink);
2536 sink = prefixed_sink;
2537 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2538 dapm->codec->name_prefix, route->source);
2539 source = prefixed_source;
2540 } else {
2541 sink = route->sink;
2542 source = route->source;
2545 path = NULL;
2546 list_for_each_entry(p, &dapm->card->paths, list) {
2547 if (strcmp(p->source->name, source) != 0)
2548 continue;
2549 if (strcmp(p->sink->name, sink) != 0)
2550 continue;
2551 path = p;
2552 break;
2555 if (path) {
2556 dapm_mark_dirty(path->source, "Route removed");
2557 dapm_mark_dirty(path->sink, "Route removed");
2559 dapm_free_path(path);
2560 } else {
2561 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2562 source, sink);
2565 return 0;
2569 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2570 * @dapm: DAPM context
2571 * @route: audio routes
2572 * @num: number of routes
2574 * Connects 2 dapm widgets together via a named audio path. The sink is
2575 * the widget receiving the audio signal, whilst the source is the sender
2576 * of the audio signal.
2578 * Returns 0 for success else error. On error all resources can be freed
2579 * with a call to snd_soc_card_free().
2581 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2582 const struct snd_soc_dapm_route *route, int num)
2584 int i, r, ret = 0;
2586 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2587 for (i = 0; i < num; i++) {
2588 r = snd_soc_dapm_add_route(dapm, route);
2589 if (r < 0) {
2590 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2591 route->source,
2592 route->control ? route->control : "direct",
2593 route->sink);
2594 ret = r;
2596 route++;
2598 mutex_unlock(&dapm->card->dapm_mutex);
2600 return ret;
2602 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2605 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2606 * @dapm: DAPM context
2607 * @route: audio routes
2608 * @num: number of routes
2610 * Removes routes from the DAPM context.
2612 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2613 const struct snd_soc_dapm_route *route, int num)
2615 int i, ret = 0;
2617 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2618 for (i = 0; i < num; i++) {
2619 snd_soc_dapm_del_route(dapm, route);
2620 route++;
2622 mutex_unlock(&dapm->card->dapm_mutex);
2624 return ret;
2626 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2628 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2629 const struct snd_soc_dapm_route *route)
2631 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2632 route->source,
2633 true);
2634 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2635 route->sink,
2636 true);
2637 struct snd_soc_dapm_path *path;
2638 int count = 0;
2640 if (!source) {
2641 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2642 route->source);
2643 return -ENODEV;
2646 if (!sink) {
2647 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2648 route->sink);
2649 return -ENODEV;
2652 if (route->control || route->connected)
2653 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2654 route->source, route->sink);
2656 list_for_each_entry(path, &source->sinks, list_source) {
2657 if (path->sink == sink) {
2658 path->weak = 1;
2659 count++;
2663 if (count == 0)
2664 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2665 route->source, route->sink);
2666 if (count > 1)
2667 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2668 count, route->source, route->sink);
2670 return 0;
2674 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2675 * @dapm: DAPM context
2676 * @route: audio routes
2677 * @num: number of routes
2679 * Mark existing routes matching those specified in the passed array
2680 * as being weak, meaning that they are ignored for the purpose of
2681 * power decisions. The main intended use case is for sidetone paths
2682 * which couple audio between other independent paths if they are both
2683 * active in order to make the combination work better at the user
2684 * level but which aren't intended to be "used".
2686 * Note that CODEC drivers should not use this as sidetone type paths
2687 * can frequently also be used as bypass paths.
2689 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2690 const struct snd_soc_dapm_route *route, int num)
2692 int i, err;
2693 int ret = 0;
2695 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2696 for (i = 0; i < num; i++) {
2697 err = snd_soc_dapm_weak_route(dapm, route);
2698 if (err)
2699 ret = err;
2700 route++;
2702 mutex_unlock(&dapm->card->dapm_mutex);
2704 return ret;
2706 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2709 * snd_soc_dapm_new_widgets - add new dapm widgets
2710 * @dapm: DAPM context
2712 * Checks the codec for any new dapm widgets and creates them if found.
2714 * Returns 0 for success.
2716 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
2718 struct snd_soc_dapm_widget *w;
2719 unsigned int val;
2721 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2723 list_for_each_entry(w, &card->widgets, list)
2725 if (w->new)
2726 continue;
2728 if (w->num_kcontrols) {
2729 w->kcontrols = kzalloc(w->num_kcontrols *
2730 sizeof(struct snd_kcontrol *),
2731 GFP_KERNEL);
2732 if (!w->kcontrols) {
2733 mutex_unlock(&card->dapm_mutex);
2734 return -ENOMEM;
2738 switch(w->id) {
2739 case snd_soc_dapm_switch:
2740 case snd_soc_dapm_mixer:
2741 case snd_soc_dapm_mixer_named_ctl:
2742 dapm_new_mixer(w);
2743 break;
2744 case snd_soc_dapm_mux:
2745 case snd_soc_dapm_virt_mux:
2746 case snd_soc_dapm_value_mux:
2747 dapm_new_mux(w);
2748 break;
2749 case snd_soc_dapm_pga:
2750 case snd_soc_dapm_out_drv:
2751 dapm_new_pga(w);
2752 break;
2753 default:
2754 break;
2757 /* Read the initial power state from the device */
2758 if (w->reg >= 0) {
2759 val = soc_widget_read(w, w->reg) >> w->shift;
2760 val &= w->mask;
2761 if (val == w->on_val)
2762 w->power = 1;
2765 w->new = 1;
2767 dapm_mark_dirty(w, "new widget");
2768 dapm_debugfs_add_widget(w);
2771 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2772 mutex_unlock(&card->dapm_mutex);
2773 return 0;
2775 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2778 * snd_soc_dapm_get_volsw - dapm mixer get callback
2779 * @kcontrol: mixer control
2780 * @ucontrol: control element information
2782 * Callback to get the value of a dapm mixer control.
2784 * Returns 0 for success.
2786 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2787 struct snd_ctl_elem_value *ucontrol)
2789 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2790 struct snd_soc_card *card = codec->card;
2791 struct soc_mixer_control *mc =
2792 (struct soc_mixer_control *)kcontrol->private_value;
2793 unsigned int reg = mc->reg;
2794 unsigned int shift = mc->shift;
2795 int max = mc->max;
2796 unsigned int mask = (1 << fls(max)) - 1;
2797 unsigned int invert = mc->invert;
2798 unsigned int val;
2800 if (snd_soc_volsw_is_stereo(mc))
2801 dev_warn(codec->dapm.dev,
2802 "ASoC: Control '%s' is stereo, which is not supported\n",
2803 kcontrol->id.name);
2805 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2806 if (dapm_kcontrol_is_powered(kcontrol))
2807 val = (snd_soc_read(codec, reg) >> shift) & mask;
2808 else
2809 val = dapm_kcontrol_get_value(kcontrol);
2810 mutex_unlock(&card->dapm_mutex);
2812 if (invert)
2813 ucontrol->value.integer.value[0] = max - val;
2814 else
2815 ucontrol->value.integer.value[0] = val;
2817 return 0;
2819 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2822 * snd_soc_dapm_put_volsw - dapm mixer set callback
2823 * @kcontrol: mixer control
2824 * @ucontrol: control element information
2826 * Callback to set the value of a dapm mixer control.
2828 * Returns 0 for success.
2830 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2831 struct snd_ctl_elem_value *ucontrol)
2833 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2834 struct snd_soc_card *card = codec->card;
2835 struct soc_mixer_control *mc =
2836 (struct soc_mixer_control *)kcontrol->private_value;
2837 unsigned int reg = mc->reg;
2838 unsigned int shift = mc->shift;
2839 int max = mc->max;
2840 unsigned int mask = (1 << fls(max)) - 1;
2841 unsigned int invert = mc->invert;
2842 unsigned int val;
2843 int connect, change;
2844 struct snd_soc_dapm_update update;
2846 if (snd_soc_volsw_is_stereo(mc))
2847 dev_warn(codec->dapm.dev,
2848 "ASoC: Control '%s' is stereo, which is not supported\n",
2849 kcontrol->id.name);
2851 val = (ucontrol->value.integer.value[0] & mask);
2852 connect = !!val;
2854 if (invert)
2855 val = max - val;
2857 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2859 dapm_kcontrol_set_value(kcontrol, val);
2861 mask = mask << shift;
2862 val = val << shift;
2864 change = snd_soc_test_bits(codec, reg, mask, val);
2865 if (change) {
2866 update.kcontrol = kcontrol;
2867 update.reg = reg;
2868 update.mask = mask;
2869 update.val = val;
2871 card->update = &update;
2873 soc_dapm_mixer_update_power(card, kcontrol, connect);
2875 card->update = NULL;
2878 mutex_unlock(&card->dapm_mutex);
2879 return change;
2881 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2884 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2885 * @kcontrol: mixer control
2886 * @ucontrol: control element information
2888 * Callback to get the value of a dapm enumerated double mixer control.
2890 * Returns 0 for success.
2892 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2893 struct snd_ctl_elem_value *ucontrol)
2895 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2896 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2897 unsigned int val;
2899 val = snd_soc_read(codec, e->reg);
2900 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
2901 if (e->shift_l != e->shift_r)
2902 ucontrol->value.enumerated.item[1] =
2903 (val >> e->shift_r) & e->mask;
2905 return 0;
2907 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2910 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2911 * @kcontrol: mixer control
2912 * @ucontrol: control element information
2914 * Callback to set the value of a dapm enumerated double mixer control.
2916 * Returns 0 for success.
2918 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2919 struct snd_ctl_elem_value *ucontrol)
2921 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2922 struct snd_soc_card *card = codec->card;
2923 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2924 unsigned int val, mux, change;
2925 unsigned int mask;
2926 struct snd_soc_dapm_update update;
2928 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2929 return -EINVAL;
2930 mux = ucontrol->value.enumerated.item[0];
2931 val = mux << e->shift_l;
2932 mask = e->mask << e->shift_l;
2933 if (e->shift_l != e->shift_r) {
2934 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2935 return -EINVAL;
2936 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2937 mask |= e->mask << e->shift_r;
2940 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2942 change = snd_soc_test_bits(codec, e->reg, mask, val);
2943 if (change) {
2944 update.kcontrol = kcontrol;
2945 update.reg = e->reg;
2946 update.mask = mask;
2947 update.val = val;
2948 card->update = &update;
2950 soc_dapm_mux_update_power(card, kcontrol, mux, e);
2952 card->update = NULL;
2955 mutex_unlock(&card->dapm_mutex);
2956 return change;
2958 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2961 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2962 * @kcontrol: mixer control
2963 * @ucontrol: control element information
2965 * Returns 0 for success.
2967 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2968 struct snd_ctl_elem_value *ucontrol)
2970 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
2971 return 0;
2973 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2976 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2977 * @kcontrol: mixer control
2978 * @ucontrol: control element information
2980 * Returns 0 for success.
2982 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2983 struct snd_ctl_elem_value *ucontrol)
2985 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2986 struct snd_soc_card *card = codec->card;
2987 unsigned int value;
2988 struct soc_enum *e =
2989 (struct soc_enum *)kcontrol->private_value;
2990 int change;
2992 if (ucontrol->value.enumerated.item[0] >= e->max)
2993 return -EINVAL;
2995 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2997 value = ucontrol->value.enumerated.item[0];
2998 change = dapm_kcontrol_set_value(kcontrol, value);
2999 if (change)
3000 soc_dapm_mux_update_power(card, kcontrol, value, e);
3002 mutex_unlock(&card->dapm_mutex);
3003 return change;
3005 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
3008 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
3009 * callback
3010 * @kcontrol: mixer control
3011 * @ucontrol: control element information
3013 * Callback to get the value of a dapm semi enumerated double mixer control.
3015 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3016 * used for handling bitfield coded enumeration for example.
3018 * Returns 0 for success.
3020 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
3021 struct snd_ctl_elem_value *ucontrol)
3023 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
3024 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3025 unsigned int reg_val, val, mux;
3027 reg_val = snd_soc_read(codec, e->reg);
3028 val = (reg_val >> e->shift_l) & e->mask;
3029 for (mux = 0; mux < e->max; mux++) {
3030 if (val == e->values[mux])
3031 break;
3033 ucontrol->value.enumerated.item[0] = mux;
3034 if (e->shift_l != e->shift_r) {
3035 val = (reg_val >> e->shift_r) & e->mask;
3036 for (mux = 0; mux < e->max; mux++) {
3037 if (val == e->values[mux])
3038 break;
3040 ucontrol->value.enumerated.item[1] = mux;
3043 return 0;
3045 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
3048 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
3049 * callback
3050 * @kcontrol: mixer control
3051 * @ucontrol: control element information
3053 * Callback to set the value of a dapm semi enumerated double mixer control.
3055 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3056 * used for handling bitfield coded enumeration for example.
3058 * Returns 0 for success.
3060 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
3061 struct snd_ctl_elem_value *ucontrol)
3063 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
3064 struct snd_soc_card *card = codec->card;
3065 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3066 unsigned int val, mux, change;
3067 unsigned int mask;
3068 struct snd_soc_dapm_update update;
3070 if (ucontrol->value.enumerated.item[0] > e->max - 1)
3071 return -EINVAL;
3072 mux = ucontrol->value.enumerated.item[0];
3073 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
3074 mask = e->mask << e->shift_l;
3075 if (e->shift_l != e->shift_r) {
3076 if (ucontrol->value.enumerated.item[1] > e->max - 1)
3077 return -EINVAL;
3078 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
3079 mask |= e->mask << e->shift_r;
3082 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3084 change = snd_soc_test_bits(codec, e->reg, mask, val);
3085 if (change) {
3086 update.kcontrol = kcontrol;
3087 update.reg = e->reg;
3088 update.mask = mask;
3089 update.val = val;
3090 card->update = &update;
3092 soc_dapm_mux_update_power(card, kcontrol, mux, e);
3094 card->update = NULL;
3097 mutex_unlock(&card->dapm_mutex);
3098 return change;
3100 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
3103 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3105 * @kcontrol: mixer control
3106 * @uinfo: control element information
3108 * Callback to provide information about a pin switch control.
3110 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3111 struct snd_ctl_elem_info *uinfo)
3113 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3114 uinfo->count = 1;
3115 uinfo->value.integer.min = 0;
3116 uinfo->value.integer.max = 1;
3118 return 0;
3120 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3123 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3125 * @kcontrol: mixer control
3126 * @ucontrol: Value
3128 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3129 struct snd_ctl_elem_value *ucontrol)
3131 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3132 const char *pin = (const char *)kcontrol->private_value;
3134 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3136 ucontrol->value.integer.value[0] =
3137 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3139 mutex_unlock(&card->dapm_mutex);
3141 return 0;
3143 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3146 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3148 * @kcontrol: mixer control
3149 * @ucontrol: Value
3151 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3152 struct snd_ctl_elem_value *ucontrol)
3154 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3155 const char *pin = (const char *)kcontrol->private_value;
3157 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3159 if (ucontrol->value.integer.value[0])
3160 snd_soc_dapm_enable_pin(&card->dapm, pin);
3161 else
3162 snd_soc_dapm_disable_pin(&card->dapm, pin);
3164 mutex_unlock(&card->dapm_mutex);
3166 snd_soc_dapm_sync(&card->dapm);
3167 return 0;
3169 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3171 static struct snd_soc_dapm_widget *
3172 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3173 const struct snd_soc_dapm_widget *widget)
3175 struct snd_soc_dapm_widget *w;
3176 int ret;
3178 if ((w = dapm_cnew_widget(widget)) == NULL)
3179 return NULL;
3181 switch (w->id) {
3182 case snd_soc_dapm_regulator_supply:
3183 w->regulator = devm_regulator_get(dapm->dev, w->name);
3184 if (IS_ERR(w->regulator)) {
3185 ret = PTR_ERR(w->regulator);
3186 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3187 w->name, ret);
3188 return NULL;
3191 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3192 ret = regulator_allow_bypass(w->regulator, true);
3193 if (ret != 0)
3194 dev_warn(w->dapm->dev,
3195 "ASoC: Failed to unbypass %s: %d\n",
3196 w->name, ret);
3198 break;
3199 case snd_soc_dapm_clock_supply:
3200 #ifdef CONFIG_CLKDEV_LOOKUP
3201 w->clk = devm_clk_get(dapm->dev, w->name);
3202 if (IS_ERR(w->clk)) {
3203 ret = PTR_ERR(w->clk);
3204 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3205 w->name, ret);
3206 return NULL;
3208 #else
3209 return NULL;
3210 #endif
3211 break;
3212 default:
3213 break;
3216 if (dapm->codec && dapm->codec->name_prefix)
3217 w->name = kasprintf(GFP_KERNEL, "%s %s",
3218 dapm->codec->name_prefix, widget->name);
3219 else
3220 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3222 if (w->name == NULL) {
3223 kfree(w);
3224 return NULL;
3227 switch (w->id) {
3228 case snd_soc_dapm_switch:
3229 case snd_soc_dapm_mixer:
3230 case snd_soc_dapm_mixer_named_ctl:
3231 w->power_check = dapm_generic_check_power;
3232 break;
3233 case snd_soc_dapm_mux:
3234 case snd_soc_dapm_virt_mux:
3235 case snd_soc_dapm_value_mux:
3236 w->power_check = dapm_generic_check_power;
3237 break;
3238 case snd_soc_dapm_dai_out:
3239 w->power_check = dapm_adc_check_power;
3240 break;
3241 case snd_soc_dapm_dai_in:
3242 w->power_check = dapm_dac_check_power;
3243 break;
3244 case snd_soc_dapm_adc:
3245 case snd_soc_dapm_aif_out:
3246 case snd_soc_dapm_dac:
3247 case snd_soc_dapm_aif_in:
3248 case snd_soc_dapm_pga:
3249 case snd_soc_dapm_out_drv:
3250 case snd_soc_dapm_input:
3251 case snd_soc_dapm_output:
3252 case snd_soc_dapm_micbias:
3253 case snd_soc_dapm_spk:
3254 case snd_soc_dapm_hp:
3255 case snd_soc_dapm_mic:
3256 case snd_soc_dapm_line:
3257 case snd_soc_dapm_dai_link:
3258 w->power_check = dapm_generic_check_power;
3259 break;
3260 case snd_soc_dapm_supply:
3261 case snd_soc_dapm_regulator_supply:
3262 case snd_soc_dapm_clock_supply:
3263 case snd_soc_dapm_kcontrol:
3264 w->power_check = dapm_supply_check_power;
3265 break;
3266 default:
3267 w->power_check = dapm_always_on_check_power;
3268 break;
3271 w->dapm = dapm;
3272 w->codec = dapm->codec;
3273 w->platform = dapm->platform;
3274 INIT_LIST_HEAD(&w->sources);
3275 INIT_LIST_HEAD(&w->sinks);
3276 INIT_LIST_HEAD(&w->list);
3277 INIT_LIST_HEAD(&w->dirty);
3278 list_add(&w->list, &dapm->card->widgets);
3280 /* machine layer set ups unconnected pins and insertions */
3281 w->connected = 1;
3282 return w;
3286 * snd_soc_dapm_new_controls - create new dapm controls
3287 * @dapm: DAPM context
3288 * @widget: widget array
3289 * @num: number of widgets
3291 * Creates new DAPM controls based upon the templates.
3293 * Returns 0 for success else error.
3295 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3296 const struct snd_soc_dapm_widget *widget,
3297 int num)
3299 struct snd_soc_dapm_widget *w;
3300 int i;
3301 int ret = 0;
3303 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3304 for (i = 0; i < num; i++) {
3305 w = snd_soc_dapm_new_control(dapm, widget);
3306 if (!w) {
3307 dev_err(dapm->dev,
3308 "ASoC: Failed to create DAPM control %s\n",
3309 widget->name);
3310 ret = -ENOMEM;
3311 break;
3313 widget++;
3315 mutex_unlock(&dapm->card->dapm_mutex);
3316 return ret;
3318 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3320 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3321 struct snd_kcontrol *kcontrol, int event)
3323 struct snd_soc_dapm_path *source_p, *sink_p;
3324 struct snd_soc_dai *source, *sink;
3325 const struct snd_soc_pcm_stream *config = w->params;
3326 struct snd_pcm_substream substream;
3327 struct snd_pcm_hw_params *params = NULL;
3328 u64 fmt;
3329 int ret;
3331 BUG_ON(!config);
3332 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3334 /* We only support a single source and sink, pick the first */
3335 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3336 list_sink);
3337 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3338 list_source);
3340 BUG_ON(!source_p || !sink_p);
3341 BUG_ON(!sink_p->source || !source_p->sink);
3342 BUG_ON(!source_p->source || !sink_p->sink);
3344 source = source_p->source->priv;
3345 sink = sink_p->sink->priv;
3347 /* Be a little careful as we don't want to overflow the mask array */
3348 if (config->formats) {
3349 fmt = ffs(config->formats) - 1;
3350 } else {
3351 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3352 config->formats);
3353 fmt = 0;
3356 /* Currently very limited parameter selection */
3357 params = kzalloc(sizeof(*params), GFP_KERNEL);
3358 if (!params) {
3359 ret = -ENOMEM;
3360 goto out;
3362 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3364 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3365 config->rate_min;
3366 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3367 config->rate_max;
3369 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3370 = config->channels_min;
3371 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3372 = config->channels_max;
3374 memset(&substream, 0, sizeof(substream));
3376 switch (event) {
3377 case SND_SOC_DAPM_PRE_PMU:
3378 if (source->driver->ops && source->driver->ops->hw_params) {
3379 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3380 ret = source->driver->ops->hw_params(&substream,
3381 params, source);
3382 if (ret != 0) {
3383 dev_err(source->dev,
3384 "ASoC: hw_params() failed: %d\n", ret);
3385 goto out;
3389 if (sink->driver->ops && sink->driver->ops->hw_params) {
3390 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3391 ret = sink->driver->ops->hw_params(&substream, params,
3392 sink);
3393 if (ret != 0) {
3394 dev_err(sink->dev,
3395 "ASoC: hw_params() failed: %d\n", ret);
3396 goto out;
3399 break;
3401 case SND_SOC_DAPM_POST_PMU:
3402 ret = snd_soc_dai_digital_mute(sink, 0,
3403 SNDRV_PCM_STREAM_PLAYBACK);
3404 if (ret != 0 && ret != -ENOTSUPP)
3405 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3406 ret = 0;
3407 break;
3409 case SND_SOC_DAPM_PRE_PMD:
3410 ret = snd_soc_dai_digital_mute(sink, 1,
3411 SNDRV_PCM_STREAM_PLAYBACK);
3412 if (ret != 0 && ret != -ENOTSUPP)
3413 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3414 ret = 0;
3415 break;
3417 default:
3418 BUG();
3419 return -EINVAL;
3422 out:
3423 kfree(params);
3424 return ret;
3427 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3428 const struct snd_soc_pcm_stream *params,
3429 struct snd_soc_dapm_widget *source,
3430 struct snd_soc_dapm_widget *sink)
3432 struct snd_soc_dapm_route routes[2];
3433 struct snd_soc_dapm_widget template;
3434 struct snd_soc_dapm_widget *w;
3435 size_t len;
3436 char *link_name;
3438 len = strlen(source->name) + strlen(sink->name) + 2;
3439 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3440 if (!link_name)
3441 return -ENOMEM;
3442 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3444 memset(&template, 0, sizeof(template));
3445 template.reg = SND_SOC_NOPM;
3446 template.id = snd_soc_dapm_dai_link;
3447 template.name = link_name;
3448 template.event = snd_soc_dai_link_event;
3449 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3450 SND_SOC_DAPM_PRE_PMD;
3452 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3454 w = snd_soc_dapm_new_control(&card->dapm, &template);
3455 if (!w) {
3456 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3457 link_name);
3458 return -ENOMEM;
3461 w->params = params;
3463 memset(&routes, 0, sizeof(routes));
3465 routes[0].source = source->name;
3466 routes[0].sink = link_name;
3467 routes[1].source = link_name;
3468 routes[1].sink = sink->name;
3470 return snd_soc_dapm_add_routes(&card->dapm, routes,
3471 ARRAY_SIZE(routes));
3474 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3475 struct snd_soc_dai *dai)
3477 struct snd_soc_dapm_widget template;
3478 struct snd_soc_dapm_widget *w;
3480 WARN_ON(dapm->dev != dai->dev);
3482 memset(&template, 0, sizeof(template));
3483 template.reg = SND_SOC_NOPM;
3485 if (dai->driver->playback.stream_name) {
3486 template.id = snd_soc_dapm_dai_in;
3487 template.name = dai->driver->playback.stream_name;
3488 template.sname = dai->driver->playback.stream_name;
3490 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3491 template.name);
3493 w = snd_soc_dapm_new_control(dapm, &template);
3494 if (!w) {
3495 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3496 dai->driver->playback.stream_name);
3497 return -ENOMEM;
3500 w->priv = dai;
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",
3510 template.name);
3512 w = snd_soc_dapm_new_control(dapm, &template);
3513 if (!w) {
3514 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3515 dai->driver->capture.stream_name);
3516 return -ENOMEM;
3519 w->priv = dai;
3520 dai->capture_widget = w;
3523 return 0;
3526 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3528 struct snd_soc_dapm_widget *dai_w, *w;
3529 struct snd_soc_dai *dai;
3531 /* For each DAI widget... */
3532 list_for_each_entry(dai_w, &card->widgets, list) {
3533 switch (dai_w->id) {
3534 case snd_soc_dapm_dai_in:
3535 case snd_soc_dapm_dai_out:
3536 break;
3537 default:
3538 continue;
3541 dai = dai_w->priv;
3543 /* ...find all widgets with the same stream and link them */
3544 list_for_each_entry(w, &card->widgets, list) {
3545 if (w->dapm != dai_w->dapm)
3546 continue;
3548 switch (w->id) {
3549 case snd_soc_dapm_dai_in:
3550 case snd_soc_dapm_dai_out:
3551 continue;
3552 default:
3553 break;
3556 if (!w->sname || !strstr(w->sname, dai_w->name))
3557 continue;
3559 if (dai->driver->playback.stream_name &&
3560 strstr(w->sname,
3561 dai->driver->playback.stream_name)) {
3562 dev_dbg(dai->dev, "%s -> %s\n",
3563 dai->playback_widget->name, w->name);
3565 snd_soc_dapm_add_path(w->dapm,
3566 dai->playback_widget, w, NULL, NULL);
3569 if (dai->driver->capture.stream_name &&
3570 strstr(w->sname,
3571 dai->driver->capture.stream_name)) {
3572 dev_dbg(dai->dev, "%s -> %s\n",
3573 w->name, dai->capture_widget->name);
3575 snd_soc_dapm_add_path(w->dapm, w,
3576 dai->capture_widget, NULL, NULL);
3581 return 0;
3584 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3585 int event)
3588 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3589 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3590 struct snd_soc_dai *codec_dai = rtd->codec_dai;
3592 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3593 w_cpu = cpu_dai->playback_widget;
3594 w_codec = codec_dai->playback_widget;
3595 } else {
3596 w_cpu = cpu_dai->capture_widget;
3597 w_codec = codec_dai->capture_widget;
3600 if (w_cpu) {
3602 dapm_mark_dirty(w_cpu, "stream event");
3604 switch (event) {
3605 case SND_SOC_DAPM_STREAM_START:
3606 w_cpu->active = 1;
3607 break;
3608 case SND_SOC_DAPM_STREAM_STOP:
3609 w_cpu->active = 0;
3610 break;
3611 case SND_SOC_DAPM_STREAM_SUSPEND:
3612 case SND_SOC_DAPM_STREAM_RESUME:
3613 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3614 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3615 break;
3619 if (w_codec) {
3621 dapm_mark_dirty(w_codec, "stream event");
3623 switch (event) {
3624 case SND_SOC_DAPM_STREAM_START:
3625 w_codec->active = 1;
3626 break;
3627 case SND_SOC_DAPM_STREAM_STOP:
3628 w_codec->active = 0;
3629 break;
3630 case SND_SOC_DAPM_STREAM_SUSPEND:
3631 case SND_SOC_DAPM_STREAM_RESUME:
3632 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3633 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3634 break;
3638 dapm_power_widgets(rtd->card, event);
3642 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3643 * @rtd: PCM runtime data
3644 * @stream: stream name
3645 * @event: stream event
3647 * Sends a stream event to the dapm core. The core then makes any
3648 * necessary widget power changes.
3650 * Returns 0 for success else error.
3652 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3653 int event)
3655 struct snd_soc_card *card = rtd->card;
3657 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3658 soc_dapm_stream_event(rtd, stream, event);
3659 mutex_unlock(&card->dapm_mutex);
3663 * snd_soc_dapm_enable_pin - enable pin.
3664 * @dapm: DAPM context
3665 * @pin: pin name
3667 * Enables input/output pin and its parents or children widgets iff there is
3668 * a valid audio route and active audio stream.
3669 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3670 * do any widget power switching.
3672 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3674 return snd_soc_dapm_set_pin(dapm, pin, 1);
3676 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3679 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3680 * @dapm: DAPM context
3681 * @pin: pin name
3683 * Enables input/output pin regardless of any other state. This is
3684 * intended for use with microphone bias supplies used in microphone
3685 * jack detection.
3687 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3688 * do any widget power switching.
3690 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3691 const char *pin)
3693 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3695 if (!w) {
3696 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3697 return -EINVAL;
3700 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3701 w->connected = 1;
3702 w->force = 1;
3703 dapm_mark_dirty(w, "force enable");
3705 return 0;
3707 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3710 * snd_soc_dapm_disable_pin - disable pin.
3711 * @dapm: DAPM context
3712 * @pin: pin name
3714 * Disables input/output pin and its parents or children widgets.
3715 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3716 * do any widget power switching.
3718 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3719 const char *pin)
3721 return snd_soc_dapm_set_pin(dapm, pin, 0);
3723 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3726 * snd_soc_dapm_nc_pin - permanently disable pin.
3727 * @dapm: DAPM context
3728 * @pin: pin name
3730 * Marks the specified pin as being not connected, disabling it along
3731 * any parent or child widgets. At present this is identical to
3732 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3733 * additional things such as disabling controls which only affect
3734 * paths through the pin.
3736 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3737 * do any widget power switching.
3739 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3741 return snd_soc_dapm_set_pin(dapm, pin, 0);
3743 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3746 * snd_soc_dapm_get_pin_status - get audio pin status
3747 * @dapm: DAPM context
3748 * @pin: audio signal pin endpoint (or start point)
3750 * Get audio pin status - connected or disconnected.
3752 * Returns 1 for connected otherwise 0.
3754 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3755 const char *pin)
3757 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3759 if (w)
3760 return w->connected;
3762 return 0;
3764 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3767 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3768 * @dapm: DAPM context
3769 * @pin: audio signal pin endpoint (or start point)
3771 * Mark the given endpoint or pin as ignoring suspend. When the
3772 * system is disabled a path between two endpoints flagged as ignoring
3773 * suspend will not be disabled. The path must already be enabled via
3774 * normal means at suspend time, it will not be turned on if it was not
3775 * already enabled.
3777 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3778 const char *pin)
3780 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3782 if (!w) {
3783 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3784 return -EINVAL;
3787 w->ignore_suspend = 1;
3789 return 0;
3791 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3793 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3794 struct snd_soc_dapm_widget *w)
3796 struct snd_soc_dapm_path *p;
3798 list_for_each_entry(p, &card->paths, list) {
3799 if ((p->source == w) || (p->sink == w)) {
3800 dev_dbg(card->dev,
3801 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3802 p->source->name, p->source->id, p->source->dapm,
3803 p->sink->name, p->sink->id, p->sink->dapm);
3805 /* Connected to something other than the codec */
3806 if (p->source->dapm != p->sink->dapm)
3807 return true;
3809 * Loopback connection from codec external pin to
3810 * codec external pin
3812 if (p->sink->id == snd_soc_dapm_input) {
3813 switch (p->source->id) {
3814 case snd_soc_dapm_output:
3815 case snd_soc_dapm_micbias:
3816 return true;
3817 default:
3818 break;
3824 return false;
3828 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3829 * @codec: The codec whose pins should be processed
3831 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3832 * which are unused. Pins are used if they are connected externally to the
3833 * codec, whether that be to some other device, or a loop-back connection to
3834 * the codec itself.
3836 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3838 struct snd_soc_card *card = codec->card;
3839 struct snd_soc_dapm_context *dapm = &codec->dapm;
3840 struct snd_soc_dapm_widget *w;
3842 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
3843 &card->dapm, &codec->dapm);
3845 list_for_each_entry(w, &card->widgets, list) {
3846 if (w->dapm != dapm)
3847 continue;
3848 switch (w->id) {
3849 case snd_soc_dapm_input:
3850 case snd_soc_dapm_output:
3851 case snd_soc_dapm_micbias:
3852 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
3853 w->name);
3854 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3855 dev_dbg(codec->dev,
3856 "... Not in map; disabling\n");
3857 snd_soc_dapm_nc_pin(dapm, w->name);
3859 break;
3860 default:
3861 break;
3867 * snd_soc_dapm_free - free dapm resources
3868 * @dapm: DAPM context
3870 * Free all dapm widgets and resources.
3872 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3874 snd_soc_dapm_sys_remove(dapm->dev);
3875 dapm_debugfs_cleanup(dapm);
3876 dapm_free_widgets(dapm);
3877 list_del(&dapm->list);
3879 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3881 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
3883 struct snd_soc_card *card = dapm->card;
3884 struct snd_soc_dapm_widget *w;
3885 LIST_HEAD(down_list);
3886 int powerdown = 0;
3888 mutex_lock(&card->dapm_mutex);
3890 list_for_each_entry(w, &dapm->card->widgets, list) {
3891 if (w->dapm != dapm)
3892 continue;
3893 if (w->power) {
3894 dapm_seq_insert(w, &down_list, false);
3895 w->power = 0;
3896 powerdown = 1;
3900 /* If there were no widgets to power down we're already in
3901 * standby.
3903 if (powerdown) {
3904 if (dapm->bias_level == SND_SOC_BIAS_ON)
3905 snd_soc_dapm_set_bias_level(dapm,
3906 SND_SOC_BIAS_PREPARE);
3907 dapm_seq_run(card, &down_list, 0, false);
3908 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3909 snd_soc_dapm_set_bias_level(dapm,
3910 SND_SOC_BIAS_STANDBY);
3913 mutex_unlock(&card->dapm_mutex);
3917 * snd_soc_dapm_shutdown - callback for system shutdown
3919 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3921 struct snd_soc_codec *codec;
3923 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
3924 soc_dapm_shutdown_codec(&codec->dapm);
3925 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3926 snd_soc_dapm_set_bias_level(&codec->dapm,
3927 SND_SOC_BIAS_OFF);
3931 /* Module information */
3932 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3933 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3934 MODULE_LICENSE("GPL");