ASoC: soc-dapm: Use '%llx' with 'u64' type.
[linux-2.6/btrfs-unstable.git] / sound / soc / soc-dapm.c
blobd7ee73a60ca556a1d41ee9c576d1ca0ae50cb127
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/slab.h>
39 #include <sound/core.h>
40 #include <sound/pcm.h>
41 #include <sound/pcm_params.h>
42 #include <sound/soc.h>
43 #include <sound/initval.h>
45 #include <trace/events/asoc.h>
47 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
49 /* dapm power sequences - make this per codec in the future */
50 static int dapm_up_seq[] = {
51 [snd_soc_dapm_pre] = 0,
52 [snd_soc_dapm_supply] = 1,
53 [snd_soc_dapm_regulator_supply] = 1,
54 [snd_soc_dapm_micbias] = 2,
55 [snd_soc_dapm_dai_link] = 2,
56 [snd_soc_dapm_dai] = 3,
57 [snd_soc_dapm_aif_in] = 3,
58 [snd_soc_dapm_aif_out] = 3,
59 [snd_soc_dapm_mic] = 4,
60 [snd_soc_dapm_mux] = 5,
61 [snd_soc_dapm_virt_mux] = 5,
62 [snd_soc_dapm_value_mux] = 5,
63 [snd_soc_dapm_dac] = 6,
64 [snd_soc_dapm_mixer] = 7,
65 [snd_soc_dapm_mixer_named_ctl] = 7,
66 [snd_soc_dapm_pga] = 8,
67 [snd_soc_dapm_adc] = 9,
68 [snd_soc_dapm_out_drv] = 10,
69 [snd_soc_dapm_hp] = 10,
70 [snd_soc_dapm_spk] = 10,
71 [snd_soc_dapm_post] = 11,
74 static int dapm_down_seq[] = {
75 [snd_soc_dapm_pre] = 0,
76 [snd_soc_dapm_adc] = 1,
77 [snd_soc_dapm_hp] = 2,
78 [snd_soc_dapm_spk] = 2,
79 [snd_soc_dapm_out_drv] = 2,
80 [snd_soc_dapm_pga] = 4,
81 [snd_soc_dapm_mixer_named_ctl] = 5,
82 [snd_soc_dapm_mixer] = 5,
83 [snd_soc_dapm_dac] = 6,
84 [snd_soc_dapm_mic] = 7,
85 [snd_soc_dapm_micbias] = 8,
86 [snd_soc_dapm_mux] = 9,
87 [snd_soc_dapm_virt_mux] = 9,
88 [snd_soc_dapm_value_mux] = 9,
89 [snd_soc_dapm_aif_in] = 10,
90 [snd_soc_dapm_aif_out] = 10,
91 [snd_soc_dapm_dai] = 10,
92 [snd_soc_dapm_dai_link] = 11,
93 [snd_soc_dapm_regulator_supply] = 12,
94 [snd_soc_dapm_supply] = 12,
95 [snd_soc_dapm_post] = 13,
98 static void pop_wait(u32 pop_time)
100 if (pop_time)
101 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
104 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
106 va_list args;
107 char *buf;
109 if (!pop_time)
110 return;
112 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
113 if (buf == NULL)
114 return;
116 va_start(args, fmt);
117 vsnprintf(buf, PAGE_SIZE, fmt, args);
118 dev_info(dev, "%s", buf);
119 va_end(args);
121 kfree(buf);
124 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
126 return !list_empty(&w->dirty);
129 void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
131 if (!dapm_dirty_widget(w)) {
132 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
133 w->name, reason);
134 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
137 EXPORT_SYMBOL_GPL(dapm_mark_dirty);
139 /* create a new dapm widget */
140 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
141 const struct snd_soc_dapm_widget *_widget)
143 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
146 /* get snd_card from DAPM context */
147 static inline struct snd_card *dapm_get_snd_card(
148 struct snd_soc_dapm_context *dapm)
150 if (dapm->codec)
151 return dapm->codec->card->snd_card;
152 else if (dapm->platform)
153 return dapm->platform->card->snd_card;
154 else
155 BUG();
157 /* unreachable */
158 return NULL;
161 /* get soc_card from DAPM context */
162 static inline struct snd_soc_card *dapm_get_soc_card(
163 struct snd_soc_dapm_context *dapm)
165 if (dapm->codec)
166 return dapm->codec->card;
167 else if (dapm->platform)
168 return dapm->platform->card;
169 else
170 BUG();
172 /* unreachable */
173 return NULL;
176 static void dapm_reset(struct snd_soc_card *card)
178 struct snd_soc_dapm_widget *w;
180 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
182 list_for_each_entry(w, &card->widgets, list) {
183 w->power_checked = false;
184 w->inputs = -1;
185 w->outputs = -1;
189 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
191 if (w->codec)
192 return snd_soc_read(w->codec, reg);
193 else if (w->platform)
194 return snd_soc_platform_read(w->platform, reg);
196 dev_err(w->dapm->dev, "no valid widget read method\n");
197 return -1;
200 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
202 if (w->codec)
203 return snd_soc_write(w->codec, reg, val);
204 else if (w->platform)
205 return snd_soc_platform_write(w->platform, reg, val);
207 dev_err(w->dapm->dev, "no valid widget write method\n");
208 return -1;
211 static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
213 if (w->codec && !w->codec->using_regmap)
214 mutex_lock(&w->codec->mutex);
215 else if (w->platform)
216 mutex_lock(&w->platform->mutex);
219 static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
221 if (w->codec && !w->codec->using_regmap)
222 mutex_unlock(&w->codec->mutex);
223 else if (w->platform)
224 mutex_unlock(&w->platform->mutex);
227 static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
228 unsigned short reg, unsigned int mask, unsigned int value)
230 bool change;
231 unsigned int old, new;
232 int ret;
234 if (w->codec && w->codec->using_regmap) {
235 ret = regmap_update_bits_check(w->codec->control_data,
236 reg, mask, value, &change);
237 if (ret != 0)
238 return ret;
239 } else {
240 soc_widget_lock(w);
241 ret = soc_widget_read(w, reg);
242 if (ret < 0) {
243 soc_widget_unlock(w);
244 return ret;
247 old = ret;
248 new = (old & ~mask) | (value & mask);
249 change = old != new;
250 if (change) {
251 ret = soc_widget_write(w, reg, new);
252 if (ret < 0) {
253 soc_widget_unlock(w);
254 return ret;
257 soc_widget_unlock(w);
260 return change;
264 * snd_soc_dapm_set_bias_level - set the bias level for the system
265 * @dapm: DAPM context
266 * @level: level to configure
268 * Configure the bias (power) levels for the SoC audio device.
270 * Returns 0 for success else error.
272 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
273 enum snd_soc_bias_level level)
275 struct snd_soc_card *card = dapm->card;
276 int ret = 0;
278 trace_snd_soc_bias_level_start(card, level);
280 if (card && card->set_bias_level)
281 ret = card->set_bias_level(card, dapm, level);
282 if (ret != 0)
283 goto out;
285 if (dapm->codec) {
286 if (dapm->codec->driver->set_bias_level)
287 ret = dapm->codec->driver->set_bias_level(dapm->codec,
288 level);
289 else
290 dapm->bias_level = level;
292 if (ret != 0)
293 goto out;
295 if (card && card->set_bias_level_post)
296 ret = card->set_bias_level_post(card, dapm, level);
297 out:
298 trace_snd_soc_bias_level_done(card, level);
300 return ret;
303 /* set up initial codec paths */
304 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
305 struct snd_soc_dapm_path *p, int i)
307 switch (w->id) {
308 case snd_soc_dapm_switch:
309 case snd_soc_dapm_mixer:
310 case snd_soc_dapm_mixer_named_ctl: {
311 int val;
312 struct soc_mixer_control *mc = (struct soc_mixer_control *)
313 w->kcontrol_news[i].private_value;
314 unsigned int reg = mc->reg;
315 unsigned int shift = mc->shift;
316 int max = mc->max;
317 unsigned int mask = (1 << fls(max)) - 1;
318 unsigned int invert = mc->invert;
320 val = soc_widget_read(w, reg);
321 val = (val >> shift) & mask;
323 if ((invert && !val) || (!invert && val))
324 p->connect = 1;
325 else
326 p->connect = 0;
328 break;
329 case snd_soc_dapm_mux: {
330 struct soc_enum *e = (struct soc_enum *)
331 w->kcontrol_news[i].private_value;
332 int val, item, bitmask;
334 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
336 val = soc_widget_read(w, e->reg);
337 item = (val >> e->shift_l) & (bitmask - 1);
339 p->connect = 0;
340 for (i = 0; i < e->max; i++) {
341 if (!(strcmp(p->name, e->texts[i])) && item == i)
342 p->connect = 1;
345 break;
346 case snd_soc_dapm_virt_mux: {
347 struct soc_enum *e = (struct soc_enum *)
348 w->kcontrol_news[i].private_value;
350 p->connect = 0;
351 /* since a virtual mux has no backing registers to
352 * decide which path to connect, it will try to match
353 * with the first enumeration. This is to ensure
354 * that the default mux choice (the first) will be
355 * correctly powered up during initialization.
357 if (!strcmp(p->name, e->texts[0]))
358 p->connect = 1;
360 break;
361 case snd_soc_dapm_value_mux: {
362 struct soc_enum *e = (struct soc_enum *)
363 w->kcontrol_news[i].private_value;
364 int val, item;
366 val = soc_widget_read(w, e->reg);
367 val = (val >> e->shift_l) & e->mask;
368 for (item = 0; item < e->max; item++) {
369 if (val == e->values[item])
370 break;
373 p->connect = 0;
374 for (i = 0; i < e->max; i++) {
375 if (!(strcmp(p->name, e->texts[i])) && item == i)
376 p->connect = 1;
379 break;
380 /* does not affect routing - always connected */
381 case snd_soc_dapm_pga:
382 case snd_soc_dapm_out_drv:
383 case snd_soc_dapm_output:
384 case snd_soc_dapm_adc:
385 case snd_soc_dapm_input:
386 case snd_soc_dapm_siggen:
387 case snd_soc_dapm_dac:
388 case snd_soc_dapm_micbias:
389 case snd_soc_dapm_vmid:
390 case snd_soc_dapm_supply:
391 case snd_soc_dapm_regulator_supply:
392 case snd_soc_dapm_aif_in:
393 case snd_soc_dapm_aif_out:
394 case snd_soc_dapm_dai:
395 case snd_soc_dapm_hp:
396 case snd_soc_dapm_mic:
397 case snd_soc_dapm_spk:
398 case snd_soc_dapm_line:
399 case snd_soc_dapm_dai_link:
400 p->connect = 1;
401 break;
402 /* does affect routing - dynamically connected */
403 case snd_soc_dapm_pre:
404 case snd_soc_dapm_post:
405 p->connect = 0;
406 break;
410 /* connect mux widget to its interconnecting audio paths */
411 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
412 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
413 struct snd_soc_dapm_path *path, const char *control_name,
414 const struct snd_kcontrol_new *kcontrol)
416 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
417 int i;
419 for (i = 0; i < e->max; i++) {
420 if (!(strcmp(control_name, e->texts[i]))) {
421 list_add(&path->list, &dapm->card->paths);
422 list_add(&path->list_sink, &dest->sources);
423 list_add(&path->list_source, &src->sinks);
424 path->name = (char*)e->texts[i];
425 dapm_set_path_status(dest, path, 0);
426 return 0;
430 return -ENODEV;
433 /* connect mixer widget to its interconnecting audio paths */
434 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
435 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
436 struct snd_soc_dapm_path *path, const char *control_name)
438 int i;
440 /* search for mixer kcontrol */
441 for (i = 0; i < dest->num_kcontrols; i++) {
442 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
443 list_add(&path->list, &dapm->card->paths);
444 list_add(&path->list_sink, &dest->sources);
445 list_add(&path->list_source, &src->sinks);
446 path->name = dest->kcontrol_news[i].name;
447 dapm_set_path_status(dest, path, i);
448 return 0;
451 return -ENODEV;
454 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
455 struct snd_soc_dapm_widget *kcontrolw,
456 const struct snd_kcontrol_new *kcontrol_new,
457 struct snd_kcontrol **kcontrol)
459 struct snd_soc_dapm_widget *w;
460 int i;
462 *kcontrol = NULL;
464 list_for_each_entry(w, &dapm->card->widgets, list) {
465 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
466 continue;
467 for (i = 0; i < w->num_kcontrols; i++) {
468 if (&w->kcontrol_news[i] == kcontrol_new) {
469 if (w->kcontrols)
470 *kcontrol = w->kcontrols[i];
471 return 1;
476 return 0;
479 /* create new dapm mixer control */
480 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
482 struct snd_soc_dapm_context *dapm = w->dapm;
483 int i, ret = 0;
484 size_t name_len, prefix_len;
485 struct snd_soc_dapm_path *path;
486 struct snd_card *card = dapm->card->snd_card;
487 const char *prefix;
488 struct snd_soc_dapm_widget_list *wlist;
489 size_t wlistsize;
491 if (dapm->codec)
492 prefix = dapm->codec->name_prefix;
493 else
494 prefix = NULL;
496 if (prefix)
497 prefix_len = strlen(prefix) + 1;
498 else
499 prefix_len = 0;
501 /* add kcontrol */
502 for (i = 0; i < w->num_kcontrols; i++) {
504 /* match name */
505 list_for_each_entry(path, &w->sources, list_sink) {
507 /* mixer/mux paths name must match control name */
508 if (path->name != (char *)w->kcontrol_news[i].name)
509 continue;
511 if (w->kcontrols[i]) {
512 path->kcontrol = w->kcontrols[i];
513 continue;
516 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
517 sizeof(struct snd_soc_dapm_widget *),
518 wlist = kzalloc(wlistsize, GFP_KERNEL);
519 if (wlist == NULL) {
520 dev_err(dapm->dev,
521 "asoc: can't allocate widget list for %s\n",
522 w->name);
523 return -ENOMEM;
525 wlist->num_widgets = 1;
526 wlist->widgets[0] = w;
528 /* add dapm control with long name.
529 * for dapm_mixer this is the concatenation of the
530 * mixer and kcontrol name.
531 * for dapm_mixer_named_ctl this is simply the
532 * kcontrol name.
534 name_len = strlen(w->kcontrol_news[i].name) + 1;
535 if (w->id != snd_soc_dapm_mixer_named_ctl)
536 name_len += 1 + strlen(w->name);
538 path->long_name = kmalloc(name_len, GFP_KERNEL);
540 if (path->long_name == NULL) {
541 kfree(wlist);
542 return -ENOMEM;
545 switch (w->id) {
546 default:
547 /* The control will get a prefix from
548 * the control creation process but
549 * we're also using the same prefix
550 * for widgets so cut the prefix off
551 * the front of the widget name.
553 snprintf((char *)path->long_name, name_len,
554 "%s %s", w->name + prefix_len,
555 w->kcontrol_news[i].name);
556 break;
557 case snd_soc_dapm_mixer_named_ctl:
558 snprintf((char *)path->long_name, name_len,
559 "%s", w->kcontrol_news[i].name);
560 break;
563 ((char *)path->long_name)[name_len - 1] = '\0';
565 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
566 wlist, path->long_name,
567 prefix);
568 ret = snd_ctl_add(card, path->kcontrol);
569 if (ret < 0) {
570 dev_err(dapm->dev,
571 "asoc: failed to add dapm kcontrol %s: %d\n",
572 path->long_name, ret);
573 kfree(wlist);
574 kfree(path->long_name);
575 path->long_name = NULL;
576 return ret;
578 w->kcontrols[i] = path->kcontrol;
581 return ret;
584 /* create new dapm mux control */
585 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
587 struct snd_soc_dapm_context *dapm = w->dapm;
588 struct snd_soc_dapm_path *path = NULL;
589 struct snd_kcontrol *kcontrol;
590 struct snd_card *card = dapm->card->snd_card;
591 const char *prefix;
592 size_t prefix_len;
593 int ret;
594 struct snd_soc_dapm_widget_list *wlist;
595 int shared, wlistentries;
596 size_t wlistsize;
597 const char *name;
599 if (w->num_kcontrols != 1) {
600 dev_err(dapm->dev,
601 "asoc: mux %s has incorrect number of controls\n",
602 w->name);
603 return -EINVAL;
606 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
607 &kcontrol);
608 if (kcontrol) {
609 wlist = kcontrol->private_data;
610 wlistentries = wlist->num_widgets + 1;
611 } else {
612 wlist = NULL;
613 wlistentries = 1;
615 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
616 wlistentries * sizeof(struct snd_soc_dapm_widget *),
617 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
618 if (wlist == NULL) {
619 dev_err(dapm->dev,
620 "asoc: can't allocate widget list for %s\n", w->name);
621 return -ENOMEM;
623 wlist->num_widgets = wlistentries;
624 wlist->widgets[wlistentries - 1] = w;
626 if (!kcontrol) {
627 if (dapm->codec)
628 prefix = dapm->codec->name_prefix;
629 else
630 prefix = NULL;
632 if (shared) {
633 name = w->kcontrol_news[0].name;
634 prefix_len = 0;
635 } else {
636 name = w->name;
637 if (prefix)
638 prefix_len = strlen(prefix) + 1;
639 else
640 prefix_len = 0;
644 * The control will get a prefix from the control creation
645 * process but we're also using the same prefix for widgets so
646 * cut the prefix off the front of the widget name.
648 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
649 name + prefix_len, prefix);
650 ret = snd_ctl_add(card, kcontrol);
651 if (ret < 0) {
652 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
653 w->name, ret);
654 kfree(wlist);
655 return ret;
659 kcontrol->private_data = wlist;
661 w->kcontrols[0] = kcontrol;
663 list_for_each_entry(path, &w->sources, list_sink)
664 path->kcontrol = kcontrol;
666 return 0;
669 /* create new dapm volume control */
670 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
672 if (w->num_kcontrols)
673 dev_err(w->dapm->dev,
674 "asoc: PGA controls not supported: '%s'\n", w->name);
676 return 0;
679 /* reset 'walked' bit for each dapm path */
680 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
682 struct snd_soc_dapm_path *p;
684 list_for_each_entry(p, &dapm->card->paths, list)
685 p->walked = 0;
688 /* We implement power down on suspend by checking the power state of
689 * the ALSA card - when we are suspending the ALSA state for the card
690 * is set to D3.
692 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
694 int level = snd_power_get_state(widget->dapm->card->snd_card);
696 switch (level) {
697 case SNDRV_CTL_POWER_D3hot:
698 case SNDRV_CTL_POWER_D3cold:
699 if (widget->ignore_suspend)
700 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
701 widget->name);
702 return widget->ignore_suspend;
703 default:
704 return 1;
709 * Recursively check for a completed path to an active or physically connected
710 * output widget. Returns number of complete paths.
712 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
714 struct snd_soc_dapm_path *path;
715 int con = 0;
717 if (widget->outputs >= 0)
718 return widget->outputs;
720 DAPM_UPDATE_STAT(widget, path_checks);
722 switch (widget->id) {
723 case snd_soc_dapm_supply:
724 case snd_soc_dapm_regulator_supply:
725 return 0;
726 default:
727 break;
730 switch (widget->id) {
731 case snd_soc_dapm_adc:
732 case snd_soc_dapm_aif_out:
733 case snd_soc_dapm_dai:
734 if (widget->active) {
735 widget->outputs = snd_soc_dapm_suspend_check(widget);
736 return widget->outputs;
738 default:
739 break;
742 if (widget->connected) {
743 /* connected pin ? */
744 if (widget->id == snd_soc_dapm_output && !widget->ext) {
745 widget->outputs = snd_soc_dapm_suspend_check(widget);
746 return widget->outputs;
749 /* connected jack or spk ? */
750 if (widget->id == snd_soc_dapm_hp ||
751 widget->id == snd_soc_dapm_spk ||
752 (widget->id == snd_soc_dapm_line &&
753 !list_empty(&widget->sources))) {
754 widget->outputs = snd_soc_dapm_suspend_check(widget);
755 return widget->outputs;
759 list_for_each_entry(path, &widget->sinks, list_source) {
760 DAPM_UPDATE_STAT(widget, neighbour_checks);
762 if (path->weak)
763 continue;
765 if (path->walked)
766 continue;
768 if (path->sink && path->connect) {
769 path->walked = 1;
770 con += is_connected_output_ep(path->sink);
774 widget->outputs = con;
776 return con;
780 * Recursively check for a completed path to an active or physically connected
781 * input widget. Returns number of complete paths.
783 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
785 struct snd_soc_dapm_path *path;
786 int con = 0;
788 if (widget->inputs >= 0)
789 return widget->inputs;
791 DAPM_UPDATE_STAT(widget, path_checks);
793 switch (widget->id) {
794 case snd_soc_dapm_supply:
795 case snd_soc_dapm_regulator_supply:
796 return 0;
797 default:
798 break;
801 /* active stream ? */
802 switch (widget->id) {
803 case snd_soc_dapm_dac:
804 case snd_soc_dapm_aif_in:
805 case snd_soc_dapm_dai:
806 if (widget->active) {
807 widget->inputs = snd_soc_dapm_suspend_check(widget);
808 return widget->inputs;
810 default:
811 break;
814 if (widget->connected) {
815 /* connected pin ? */
816 if (widget->id == snd_soc_dapm_input && !widget->ext) {
817 widget->inputs = snd_soc_dapm_suspend_check(widget);
818 return widget->inputs;
821 /* connected VMID/Bias for lower pops */
822 if (widget->id == snd_soc_dapm_vmid) {
823 widget->inputs = snd_soc_dapm_suspend_check(widget);
824 return widget->inputs;
827 /* connected jack ? */
828 if (widget->id == snd_soc_dapm_mic ||
829 (widget->id == snd_soc_dapm_line &&
830 !list_empty(&widget->sinks))) {
831 widget->inputs = snd_soc_dapm_suspend_check(widget);
832 return widget->inputs;
835 /* signal generator */
836 if (widget->id == snd_soc_dapm_siggen) {
837 widget->inputs = snd_soc_dapm_suspend_check(widget);
838 return widget->inputs;
842 list_for_each_entry(path, &widget->sources, list_sink) {
843 DAPM_UPDATE_STAT(widget, neighbour_checks);
845 if (path->weak)
846 continue;
848 if (path->walked)
849 continue;
851 if (path->source && path->connect) {
852 path->walked = 1;
853 con += is_connected_input_ep(path->source);
857 widget->inputs = con;
859 return con;
863 * Handler for generic register modifier widget.
865 int dapm_reg_event(struct snd_soc_dapm_widget *w,
866 struct snd_kcontrol *kcontrol, int event)
868 unsigned int val;
870 if (SND_SOC_DAPM_EVENT_ON(event))
871 val = w->on_val;
872 else
873 val = w->off_val;
875 soc_widget_update_bits_locked(w, -(w->reg + 1),
876 w->mask << w->shift, val << w->shift);
878 return 0;
880 EXPORT_SYMBOL_GPL(dapm_reg_event);
883 * Handler for regulator supply widget.
885 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
886 struct snd_kcontrol *kcontrol, int event)
888 if (SND_SOC_DAPM_EVENT_ON(event))
889 return regulator_enable(w->regulator);
890 else
891 return regulator_disable_deferred(w->regulator, w->shift);
893 EXPORT_SYMBOL_GPL(dapm_regulator_event);
895 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
897 if (w->power_checked)
898 return w->new_power;
900 if (w->force)
901 w->new_power = 1;
902 else
903 w->new_power = w->power_check(w);
905 w->power_checked = true;
907 return w->new_power;
910 /* Generic check to see if a widget should be powered.
912 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
914 int in, out;
916 DAPM_UPDATE_STAT(w, power_checks);
918 in = is_connected_input_ep(w);
919 dapm_clear_walk(w->dapm);
920 out = is_connected_output_ep(w);
921 dapm_clear_walk(w->dapm);
922 return out != 0 && in != 0;
925 static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
927 DAPM_UPDATE_STAT(w, power_checks);
929 if (w->active)
930 return w->active;
932 return dapm_generic_check_power(w);
935 /* Check to see if an ADC has power */
936 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
938 int in;
940 DAPM_UPDATE_STAT(w, power_checks);
942 if (w->active) {
943 in = is_connected_input_ep(w);
944 dapm_clear_walk(w->dapm);
945 return in != 0;
946 } else {
947 return dapm_generic_check_power(w);
951 /* Check to see if a DAC has power */
952 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
954 int out;
956 DAPM_UPDATE_STAT(w, power_checks);
958 if (w->active) {
959 out = is_connected_output_ep(w);
960 dapm_clear_walk(w->dapm);
961 return out != 0;
962 } else {
963 return dapm_generic_check_power(w);
967 /* Check to see if a power supply is needed */
968 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
970 struct snd_soc_dapm_path *path;
972 DAPM_UPDATE_STAT(w, power_checks);
974 /* Check if one of our outputs is connected */
975 list_for_each_entry(path, &w->sinks, list_source) {
976 DAPM_UPDATE_STAT(w, neighbour_checks);
978 if (path->weak)
979 continue;
981 if (path->connected &&
982 !path->connected(path->source, path->sink))
983 continue;
985 if (!path->sink)
986 continue;
988 if (dapm_widget_power_check(path->sink))
989 return 1;
992 dapm_clear_walk(w->dapm);
994 return 0;
997 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
999 return 1;
1002 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1003 struct snd_soc_dapm_widget *b,
1004 bool power_up)
1006 int *sort;
1008 if (power_up)
1009 sort = dapm_up_seq;
1010 else
1011 sort = dapm_down_seq;
1013 if (sort[a->id] != sort[b->id])
1014 return sort[a->id] - sort[b->id];
1015 if (a->subseq != b->subseq) {
1016 if (power_up)
1017 return a->subseq - b->subseq;
1018 else
1019 return b->subseq - a->subseq;
1021 if (a->reg != b->reg)
1022 return a->reg - b->reg;
1023 if (a->dapm != b->dapm)
1024 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1026 return 0;
1029 /* Insert a widget in order into a DAPM power sequence. */
1030 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1031 struct list_head *list,
1032 bool power_up)
1034 struct snd_soc_dapm_widget *w;
1036 list_for_each_entry(w, list, power_list)
1037 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1038 list_add_tail(&new_widget->power_list, &w->power_list);
1039 return;
1042 list_add_tail(&new_widget->power_list, list);
1045 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1046 struct snd_soc_dapm_widget *w, int event)
1048 struct snd_soc_card *card = dapm->card;
1049 const char *ev_name;
1050 int power, ret;
1052 switch (event) {
1053 case SND_SOC_DAPM_PRE_PMU:
1054 ev_name = "PRE_PMU";
1055 power = 1;
1056 break;
1057 case SND_SOC_DAPM_POST_PMU:
1058 ev_name = "POST_PMU";
1059 power = 1;
1060 break;
1061 case SND_SOC_DAPM_PRE_PMD:
1062 ev_name = "PRE_PMD";
1063 power = 0;
1064 break;
1065 case SND_SOC_DAPM_POST_PMD:
1066 ev_name = "POST_PMD";
1067 power = 0;
1068 break;
1069 default:
1070 BUG();
1071 return;
1074 if (w->power != power)
1075 return;
1077 if (w->event && (w->event_flags & event)) {
1078 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1079 w->name, ev_name);
1080 trace_snd_soc_dapm_widget_event_start(w, event);
1081 ret = w->event(w, NULL, event);
1082 trace_snd_soc_dapm_widget_event_done(w, event);
1083 if (ret < 0)
1084 pr_err("%s: %s event failed: %d\n",
1085 ev_name, w->name, ret);
1089 /* Apply the coalesced changes from a DAPM sequence */
1090 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
1091 struct list_head *pending)
1093 struct snd_soc_card *card = dapm->card;
1094 struct snd_soc_dapm_widget *w;
1095 int reg, power;
1096 unsigned int value = 0;
1097 unsigned int mask = 0;
1098 unsigned int cur_mask;
1100 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1101 power_list)->reg;
1103 list_for_each_entry(w, pending, power_list) {
1104 cur_mask = 1 << w->shift;
1105 BUG_ON(reg != w->reg);
1107 if (w->invert)
1108 power = !w->power;
1109 else
1110 power = w->power;
1112 mask |= cur_mask;
1113 if (power)
1114 value |= cur_mask;
1116 pop_dbg(dapm->dev, card->pop_time,
1117 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1118 w->name, reg, value, mask);
1120 /* Check for events */
1121 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1122 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1125 if (reg >= 0) {
1126 /* Any widget will do, they should all be updating the
1127 * same register.
1129 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1130 power_list);
1132 pop_dbg(dapm->dev, card->pop_time,
1133 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1134 value, mask, reg, card->pop_time);
1135 pop_wait(card->pop_time);
1136 soc_widget_update_bits_locked(w, reg, mask, value);
1139 list_for_each_entry(w, pending, power_list) {
1140 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1141 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1145 /* Apply a DAPM power sequence.
1147 * We walk over a pre-sorted list of widgets to apply power to. In
1148 * order to minimise the number of writes to the device required
1149 * multiple widgets will be updated in a single write where possible.
1150 * Currently anything that requires more than a single write is not
1151 * handled.
1153 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1154 struct list_head *list, int event, bool power_up)
1156 struct snd_soc_dapm_widget *w, *n;
1157 LIST_HEAD(pending);
1158 int cur_sort = -1;
1159 int cur_subseq = -1;
1160 int cur_reg = SND_SOC_NOPM;
1161 struct snd_soc_dapm_context *cur_dapm = NULL;
1162 int ret, i;
1163 int *sort;
1165 if (power_up)
1166 sort = dapm_up_seq;
1167 else
1168 sort = dapm_down_seq;
1170 list_for_each_entry_safe(w, n, list, power_list) {
1171 ret = 0;
1173 /* Do we need to apply any queued changes? */
1174 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1175 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1176 if (!list_empty(&pending))
1177 dapm_seq_run_coalesced(cur_dapm, &pending);
1179 if (cur_dapm && cur_dapm->seq_notifier) {
1180 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1181 if (sort[i] == cur_sort)
1182 cur_dapm->seq_notifier(cur_dapm,
1184 cur_subseq);
1187 INIT_LIST_HEAD(&pending);
1188 cur_sort = -1;
1189 cur_subseq = INT_MIN;
1190 cur_reg = SND_SOC_NOPM;
1191 cur_dapm = NULL;
1194 switch (w->id) {
1195 case snd_soc_dapm_pre:
1196 if (!w->event)
1197 list_for_each_entry_safe_continue(w, n, list,
1198 power_list);
1200 if (event == SND_SOC_DAPM_STREAM_START)
1201 ret = w->event(w,
1202 NULL, SND_SOC_DAPM_PRE_PMU);
1203 else if (event == SND_SOC_DAPM_STREAM_STOP)
1204 ret = w->event(w,
1205 NULL, SND_SOC_DAPM_PRE_PMD);
1206 break;
1208 case snd_soc_dapm_post:
1209 if (!w->event)
1210 list_for_each_entry_safe_continue(w, n, list,
1211 power_list);
1213 if (event == SND_SOC_DAPM_STREAM_START)
1214 ret = w->event(w,
1215 NULL, SND_SOC_DAPM_POST_PMU);
1216 else if (event == SND_SOC_DAPM_STREAM_STOP)
1217 ret = w->event(w,
1218 NULL, SND_SOC_DAPM_POST_PMD);
1219 break;
1221 default:
1222 /* Queue it up for application */
1223 cur_sort = sort[w->id];
1224 cur_subseq = w->subseq;
1225 cur_reg = w->reg;
1226 cur_dapm = w->dapm;
1227 list_move(&w->power_list, &pending);
1228 break;
1231 if (ret < 0)
1232 dev_err(w->dapm->dev,
1233 "Failed to apply widget power: %d\n", ret);
1236 if (!list_empty(&pending))
1237 dapm_seq_run_coalesced(cur_dapm, &pending);
1239 if (cur_dapm && cur_dapm->seq_notifier) {
1240 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1241 if (sort[i] == cur_sort)
1242 cur_dapm->seq_notifier(cur_dapm,
1243 i, cur_subseq);
1247 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1249 struct snd_soc_dapm_update *update = dapm->update;
1250 struct snd_soc_dapm_widget *w;
1251 int ret;
1253 if (!update)
1254 return;
1256 w = update->widget;
1258 if (w->event &&
1259 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1260 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1261 if (ret != 0)
1262 pr_err("%s DAPM pre-event failed: %d\n",
1263 w->name, ret);
1266 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
1267 update->val);
1268 if (ret < 0)
1269 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1271 if (w->event &&
1272 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1273 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1274 if (ret != 0)
1275 pr_err("%s DAPM post-event failed: %d\n",
1276 w->name, ret);
1280 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1281 * they're changing state.
1283 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1285 struct snd_soc_dapm_context *d = data;
1286 int ret;
1288 /* If we're off and we're not supposed to be go into STANDBY */
1289 if (d->bias_level == SND_SOC_BIAS_OFF &&
1290 d->target_bias_level != SND_SOC_BIAS_OFF) {
1291 if (d->dev)
1292 pm_runtime_get_sync(d->dev);
1294 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1295 if (ret != 0)
1296 dev_err(d->dev,
1297 "Failed to turn on bias: %d\n", ret);
1300 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1301 if (d->bias_level != d->target_bias_level) {
1302 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1303 if (ret != 0)
1304 dev_err(d->dev,
1305 "Failed to prepare bias: %d\n", ret);
1309 /* Async callback run prior to DAPM sequences - brings to their final
1310 * state.
1312 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1314 struct snd_soc_dapm_context *d = data;
1315 int ret;
1317 /* If we just powered the last thing off drop to standby bias */
1318 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1319 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1320 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1321 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1322 if (ret != 0)
1323 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1324 ret);
1327 /* If we're in standby and can support bias off then do that */
1328 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1329 d->target_bias_level == SND_SOC_BIAS_OFF) {
1330 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1331 if (ret != 0)
1332 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1334 if (d->dev)
1335 pm_runtime_put(d->dev);
1338 /* If we just powered up then move to active bias */
1339 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1340 d->target_bias_level == SND_SOC_BIAS_ON) {
1341 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1342 if (ret != 0)
1343 dev_err(d->dev, "Failed to apply active bias: %d\n",
1344 ret);
1348 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1349 bool power, bool connect)
1351 /* If a connection is being made or broken then that update
1352 * will have marked the peer dirty, otherwise the widgets are
1353 * not connected and this update has no impact. */
1354 if (!connect)
1355 return;
1357 /* If the peer is already in the state we're moving to then we
1358 * won't have an impact on it. */
1359 if (power != peer->power)
1360 dapm_mark_dirty(peer, "peer state change");
1363 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1364 struct list_head *up_list,
1365 struct list_head *down_list)
1367 struct snd_soc_dapm_path *path;
1369 if (w->power == power)
1370 return;
1372 trace_snd_soc_dapm_widget_power(w, power);
1374 /* If we changed our power state perhaps our neigbours changed
1375 * also.
1377 list_for_each_entry(path, &w->sources, list_sink) {
1378 if (path->source) {
1379 dapm_widget_set_peer_power(path->source, power,
1380 path->connect);
1383 switch (w->id) {
1384 case snd_soc_dapm_supply:
1385 case snd_soc_dapm_regulator_supply:
1386 /* Supplies can't affect their outputs, only their inputs */
1387 break;
1388 default:
1389 list_for_each_entry(path, &w->sinks, list_source) {
1390 if (path->sink) {
1391 dapm_widget_set_peer_power(path->sink, power,
1392 path->connect);
1395 break;
1398 if (power)
1399 dapm_seq_insert(w, up_list, true);
1400 else
1401 dapm_seq_insert(w, down_list, false);
1403 w->power = power;
1406 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1407 struct list_head *up_list,
1408 struct list_head *down_list)
1410 int power;
1412 switch (w->id) {
1413 case snd_soc_dapm_pre:
1414 dapm_seq_insert(w, down_list, false);
1415 break;
1416 case snd_soc_dapm_post:
1417 dapm_seq_insert(w, up_list, true);
1418 break;
1420 default:
1421 power = dapm_widget_power_check(w);
1423 dapm_widget_set_power(w, power, up_list, down_list);
1424 break;
1429 * Scan each dapm widget for complete audio path.
1430 * A complete path is a route that has valid endpoints i.e.:-
1432 * o DAC to output pin.
1433 * o Input Pin to ADC.
1434 * o Input pin to Output pin (bypass, sidetone)
1435 * o DAC to ADC (loopback).
1437 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1439 struct snd_soc_card *card = dapm->card;
1440 struct snd_soc_dapm_widget *w;
1441 struct snd_soc_dapm_context *d;
1442 LIST_HEAD(up_list);
1443 LIST_HEAD(down_list);
1444 LIST_HEAD(async_domain);
1445 enum snd_soc_bias_level bias;
1447 trace_snd_soc_dapm_start(card);
1449 list_for_each_entry(d, &card->dapm_list, list) {
1450 if (d->idle_bias_off)
1451 d->target_bias_level = SND_SOC_BIAS_OFF;
1452 else
1453 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1456 dapm_reset(card);
1458 /* Check which widgets we need to power and store them in
1459 * lists indicating if they should be powered up or down. We
1460 * only check widgets that have been flagged as dirty but note
1461 * that new widgets may be added to the dirty list while we
1462 * iterate.
1464 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1465 dapm_power_one_widget(w, &up_list, &down_list);
1468 list_for_each_entry(w, &card->widgets, list) {
1469 list_del_init(&w->dirty);
1471 if (w->power) {
1472 d = w->dapm;
1474 /* Supplies and micbiases only bring the
1475 * context up to STANDBY as unless something
1476 * else is active and passing audio they
1477 * generally don't require full power. Signal
1478 * generators are virtual pins and have no
1479 * power impact themselves.
1481 switch (w->id) {
1482 case snd_soc_dapm_siggen:
1483 break;
1484 case snd_soc_dapm_supply:
1485 case snd_soc_dapm_regulator_supply:
1486 case snd_soc_dapm_micbias:
1487 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1488 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1489 break;
1490 default:
1491 d->target_bias_level = SND_SOC_BIAS_ON;
1492 break;
1498 /* Force all contexts in the card to the same bias state if
1499 * they're not ground referenced.
1501 bias = SND_SOC_BIAS_OFF;
1502 list_for_each_entry(d, &card->dapm_list, list)
1503 if (d->target_bias_level > bias)
1504 bias = d->target_bias_level;
1505 list_for_each_entry(d, &card->dapm_list, list)
1506 if (!d->idle_bias_off)
1507 d->target_bias_level = bias;
1509 trace_snd_soc_dapm_walk_done(card);
1511 /* Run all the bias changes in parallel */
1512 list_for_each_entry(d, &dapm->card->dapm_list, list)
1513 async_schedule_domain(dapm_pre_sequence_async, d,
1514 &async_domain);
1515 async_synchronize_full_domain(&async_domain);
1517 /* Power down widgets first; try to avoid amplifying pops. */
1518 dapm_seq_run(dapm, &down_list, event, false);
1520 dapm_widget_update(dapm);
1522 /* Now power up. */
1523 dapm_seq_run(dapm, &up_list, event, true);
1525 /* Run all the bias changes in parallel */
1526 list_for_each_entry(d, &dapm->card->dapm_list, list)
1527 async_schedule_domain(dapm_post_sequence_async, d,
1528 &async_domain);
1529 async_synchronize_full_domain(&async_domain);
1531 /* do we need to notify any clients that DAPM event is complete */
1532 list_for_each_entry(d, &card->dapm_list, list) {
1533 if (d->stream_event)
1534 d->stream_event(d, event);
1537 pop_dbg(dapm->dev, card->pop_time,
1538 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1539 pop_wait(card->pop_time);
1541 trace_snd_soc_dapm_done(card);
1543 return 0;
1546 #ifdef CONFIG_DEBUG_FS
1547 static ssize_t dapm_widget_power_read_file(struct file *file,
1548 char __user *user_buf,
1549 size_t count, loff_t *ppos)
1551 struct snd_soc_dapm_widget *w = file->private_data;
1552 char *buf;
1553 int in, out;
1554 ssize_t ret;
1555 struct snd_soc_dapm_path *p = NULL;
1557 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1558 if (!buf)
1559 return -ENOMEM;
1561 in = is_connected_input_ep(w);
1562 dapm_clear_walk(w->dapm);
1563 out = is_connected_output_ep(w);
1564 dapm_clear_walk(w->dapm);
1566 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1567 w->name, w->power ? "On" : "Off",
1568 w->force ? " (forced)" : "", in, out);
1570 if (w->reg >= 0)
1571 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1572 " - R%d(0x%x) bit %d",
1573 w->reg, w->reg, w->shift);
1575 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1577 if (w->sname)
1578 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1579 w->sname,
1580 w->active ? "active" : "inactive");
1582 list_for_each_entry(p, &w->sources, list_sink) {
1583 if (p->connected && !p->connected(w, p->sink))
1584 continue;
1586 if (p->connect)
1587 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1588 " in \"%s\" \"%s\"\n",
1589 p->name ? p->name : "static",
1590 p->source->name);
1592 list_for_each_entry(p, &w->sinks, list_source) {
1593 if (p->connected && !p->connected(w, p->sink))
1594 continue;
1596 if (p->connect)
1597 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1598 " out \"%s\" \"%s\"\n",
1599 p->name ? p->name : "static",
1600 p->sink->name);
1603 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1605 kfree(buf);
1606 return ret;
1609 static const struct file_operations dapm_widget_power_fops = {
1610 .open = simple_open,
1611 .read = dapm_widget_power_read_file,
1612 .llseek = default_llseek,
1615 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1616 size_t count, loff_t *ppos)
1618 struct snd_soc_dapm_context *dapm = file->private_data;
1619 char *level;
1621 switch (dapm->bias_level) {
1622 case SND_SOC_BIAS_ON:
1623 level = "On\n";
1624 break;
1625 case SND_SOC_BIAS_PREPARE:
1626 level = "Prepare\n";
1627 break;
1628 case SND_SOC_BIAS_STANDBY:
1629 level = "Standby\n";
1630 break;
1631 case SND_SOC_BIAS_OFF:
1632 level = "Off\n";
1633 break;
1634 default:
1635 BUG();
1636 level = "Unknown\n";
1637 break;
1640 return simple_read_from_buffer(user_buf, count, ppos, level,
1641 strlen(level));
1644 static const struct file_operations dapm_bias_fops = {
1645 .open = simple_open,
1646 .read = dapm_bias_read_file,
1647 .llseek = default_llseek,
1650 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1651 struct dentry *parent)
1653 struct dentry *d;
1655 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1657 if (!dapm->debugfs_dapm) {
1658 dev_warn(dapm->dev,
1659 "Failed to create DAPM debugfs directory\n");
1660 return;
1663 d = debugfs_create_file("bias_level", 0444,
1664 dapm->debugfs_dapm, dapm,
1665 &dapm_bias_fops);
1666 if (!d)
1667 dev_warn(dapm->dev,
1668 "ASoC: Failed to create bias level debugfs file\n");
1671 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1673 struct snd_soc_dapm_context *dapm = w->dapm;
1674 struct dentry *d;
1676 if (!dapm->debugfs_dapm || !w->name)
1677 return;
1679 d = debugfs_create_file(w->name, 0444,
1680 dapm->debugfs_dapm, w,
1681 &dapm_widget_power_fops);
1682 if (!d)
1683 dev_warn(w->dapm->dev,
1684 "ASoC: Failed to create %s debugfs file\n",
1685 w->name);
1688 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1690 debugfs_remove_recursive(dapm->debugfs_dapm);
1693 #else
1694 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1695 struct dentry *parent)
1699 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1703 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1707 #endif
1709 /* test and update the power status of a mux widget */
1710 static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1711 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1713 struct snd_soc_dapm_path *path;
1714 int found = 0;
1716 if (widget->id != snd_soc_dapm_mux &&
1717 widget->id != snd_soc_dapm_virt_mux &&
1718 widget->id != snd_soc_dapm_value_mux)
1719 return -ENODEV;
1721 /* find dapm widget path assoc with kcontrol */
1722 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1723 if (path->kcontrol != kcontrol)
1724 continue;
1726 if (!path->name || !e->texts[mux])
1727 continue;
1729 found = 1;
1730 /* we now need to match the string in the enum to the path */
1731 if (!(strcmp(path->name, e->texts[mux]))) {
1732 path->connect = 1; /* new connection */
1733 dapm_mark_dirty(path->source, "mux connection");
1734 } else {
1735 if (path->connect)
1736 dapm_mark_dirty(path->source,
1737 "mux disconnection");
1738 path->connect = 0; /* old connection must be powered down */
1742 if (found) {
1743 dapm_mark_dirty(widget, "mux change");
1744 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1747 return 0;
1750 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1751 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1753 struct snd_soc_card *card = widget->dapm->card;
1754 int ret;
1756 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1757 ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1758 mutex_unlock(&card->dapm_mutex);
1759 return ret;
1761 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
1763 /* test and update the power status of a mixer or switch widget */
1764 static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1765 struct snd_kcontrol *kcontrol, int connect)
1767 struct snd_soc_dapm_path *path;
1768 int found = 0;
1770 if (widget->id != snd_soc_dapm_mixer &&
1771 widget->id != snd_soc_dapm_mixer_named_ctl &&
1772 widget->id != snd_soc_dapm_switch)
1773 return -ENODEV;
1775 /* find dapm widget path assoc with kcontrol */
1776 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1777 if (path->kcontrol != kcontrol)
1778 continue;
1780 /* found, now check type */
1781 found = 1;
1782 path->connect = connect;
1783 dapm_mark_dirty(path->source, "mixer connection");
1786 if (found) {
1787 dapm_mark_dirty(widget, "mixer update");
1788 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1791 return 0;
1794 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1795 struct snd_kcontrol *kcontrol, int connect)
1797 struct snd_soc_card *card = widget->dapm->card;
1798 int ret;
1800 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1801 ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
1802 mutex_unlock(&card->dapm_mutex);
1803 return ret;
1805 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
1807 /* show dapm widget status in sys fs */
1808 static ssize_t dapm_widget_show(struct device *dev,
1809 struct device_attribute *attr, char *buf)
1811 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
1812 struct snd_soc_codec *codec =rtd->codec;
1813 struct snd_soc_dapm_widget *w;
1814 int count = 0;
1815 char *state = "not set";
1817 list_for_each_entry(w, &codec->card->widgets, list) {
1818 if (w->dapm != &codec->dapm)
1819 continue;
1821 /* only display widgets that burnm power */
1822 switch (w->id) {
1823 case snd_soc_dapm_hp:
1824 case snd_soc_dapm_mic:
1825 case snd_soc_dapm_spk:
1826 case snd_soc_dapm_line:
1827 case snd_soc_dapm_micbias:
1828 case snd_soc_dapm_dac:
1829 case snd_soc_dapm_adc:
1830 case snd_soc_dapm_pga:
1831 case snd_soc_dapm_out_drv:
1832 case snd_soc_dapm_mixer:
1833 case snd_soc_dapm_mixer_named_ctl:
1834 case snd_soc_dapm_supply:
1835 case snd_soc_dapm_regulator_supply:
1836 if (w->name)
1837 count += sprintf(buf + count, "%s: %s\n",
1838 w->name, w->power ? "On":"Off");
1839 break;
1840 default:
1841 break;
1845 switch (codec->dapm.bias_level) {
1846 case SND_SOC_BIAS_ON:
1847 state = "On";
1848 break;
1849 case SND_SOC_BIAS_PREPARE:
1850 state = "Prepare";
1851 break;
1852 case SND_SOC_BIAS_STANDBY:
1853 state = "Standby";
1854 break;
1855 case SND_SOC_BIAS_OFF:
1856 state = "Off";
1857 break;
1859 count += sprintf(buf + count, "PM State: %s\n", state);
1861 return count;
1864 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1866 int snd_soc_dapm_sys_add(struct device *dev)
1868 return device_create_file(dev, &dev_attr_dapm_widget);
1871 static void snd_soc_dapm_sys_remove(struct device *dev)
1873 device_remove_file(dev, &dev_attr_dapm_widget);
1876 /* free all dapm widgets and resources */
1877 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1879 struct snd_soc_dapm_widget *w, *next_w;
1880 struct snd_soc_dapm_path *p, *next_p;
1882 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1883 if (w->dapm != dapm)
1884 continue;
1885 list_del(&w->list);
1887 * remove source and sink paths associated to this widget.
1888 * While removing the path, remove reference to it from both
1889 * source and sink widgets so that path is removed only once.
1891 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1892 list_del(&p->list_sink);
1893 list_del(&p->list_source);
1894 list_del(&p->list);
1895 kfree(p->long_name);
1896 kfree(p);
1898 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1899 list_del(&p->list_sink);
1900 list_del(&p->list_source);
1901 list_del(&p->list);
1902 kfree(p->long_name);
1903 kfree(p);
1905 kfree(w->kcontrols);
1906 kfree(w->name);
1907 kfree(w);
1911 static struct snd_soc_dapm_widget *dapm_find_widget(
1912 struct snd_soc_dapm_context *dapm, const char *pin,
1913 bool search_other_contexts)
1915 struct snd_soc_dapm_widget *w;
1916 struct snd_soc_dapm_widget *fallback = NULL;
1918 list_for_each_entry(w, &dapm->card->widgets, list) {
1919 if (!strcmp(w->name, pin)) {
1920 if (w->dapm == dapm)
1921 return w;
1922 else
1923 fallback = w;
1927 if (search_other_contexts)
1928 return fallback;
1930 return NULL;
1933 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1934 const char *pin, int status)
1936 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
1938 if (!w) {
1939 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1940 return -EINVAL;
1943 if (w->connected != status)
1944 dapm_mark_dirty(w, "pin configuration");
1946 w->connected = status;
1947 if (status == 0)
1948 w->force = 0;
1950 return 0;
1954 * snd_soc_dapm_sync - scan and power dapm paths
1955 * @dapm: DAPM context
1957 * Walks all dapm audio paths and powers widgets according to their
1958 * stream or path usage.
1960 * Returns 0 for success.
1962 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1964 int ret;
1967 * Suppress early reports (eg, jacks syncing their state) to avoid
1968 * silly DAPM runs during card startup.
1970 if (!dapm->card || !dapm->card->instantiated)
1971 return 0;
1973 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1974 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1975 mutex_unlock(&dapm->card->dapm_mutex);
1976 return ret;
1978 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1980 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1981 const struct snd_soc_dapm_route *route)
1983 struct snd_soc_dapm_path *path;
1984 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1985 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1986 const char *sink;
1987 const char *control = route->control;
1988 const char *source;
1989 char prefixed_sink[80];
1990 char prefixed_source[80];
1991 int ret = 0;
1993 if (dapm->codec && dapm->codec->name_prefix) {
1994 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1995 dapm->codec->name_prefix, route->sink);
1996 sink = prefixed_sink;
1997 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1998 dapm->codec->name_prefix, route->source);
1999 source = prefixed_source;
2000 } else {
2001 sink = route->sink;
2002 source = route->source;
2006 * find src and dest widgets over all widgets but favor a widget from
2007 * current DAPM context
2009 list_for_each_entry(w, &dapm->card->widgets, list) {
2010 if (!wsink && !(strcmp(w->name, sink))) {
2011 wtsink = w;
2012 if (w->dapm == dapm)
2013 wsink = w;
2014 continue;
2016 if (!wsource && !(strcmp(w->name, source))) {
2017 wtsource = w;
2018 if (w->dapm == dapm)
2019 wsource = w;
2022 /* use widget from another DAPM context if not found from this */
2023 if (!wsink)
2024 wsink = wtsink;
2025 if (!wsource)
2026 wsource = wtsource;
2028 if (wsource == NULL || wsink == NULL)
2029 return -ENODEV;
2031 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2032 if (!path)
2033 return -ENOMEM;
2035 path->source = wsource;
2036 path->sink = wsink;
2037 path->connected = route->connected;
2038 INIT_LIST_HEAD(&path->list);
2039 INIT_LIST_HEAD(&path->list_source);
2040 INIT_LIST_HEAD(&path->list_sink);
2042 /* check for external widgets */
2043 if (wsink->id == snd_soc_dapm_input) {
2044 if (wsource->id == snd_soc_dapm_micbias ||
2045 wsource->id == snd_soc_dapm_mic ||
2046 wsource->id == snd_soc_dapm_line ||
2047 wsource->id == snd_soc_dapm_output)
2048 wsink->ext = 1;
2050 if (wsource->id == snd_soc_dapm_output) {
2051 if (wsink->id == snd_soc_dapm_spk ||
2052 wsink->id == snd_soc_dapm_hp ||
2053 wsink->id == snd_soc_dapm_line ||
2054 wsink->id == snd_soc_dapm_input)
2055 wsource->ext = 1;
2058 /* connect static paths */
2059 if (control == NULL) {
2060 list_add(&path->list, &dapm->card->paths);
2061 list_add(&path->list_sink, &wsink->sources);
2062 list_add(&path->list_source, &wsource->sinks);
2063 path->connect = 1;
2064 return 0;
2067 /* connect dynamic paths */
2068 switch (wsink->id) {
2069 case snd_soc_dapm_adc:
2070 case snd_soc_dapm_dac:
2071 case snd_soc_dapm_pga:
2072 case snd_soc_dapm_out_drv:
2073 case snd_soc_dapm_input:
2074 case snd_soc_dapm_output:
2075 case snd_soc_dapm_siggen:
2076 case snd_soc_dapm_micbias:
2077 case snd_soc_dapm_vmid:
2078 case snd_soc_dapm_pre:
2079 case snd_soc_dapm_post:
2080 case snd_soc_dapm_supply:
2081 case snd_soc_dapm_regulator_supply:
2082 case snd_soc_dapm_aif_in:
2083 case snd_soc_dapm_aif_out:
2084 case snd_soc_dapm_dai:
2085 case snd_soc_dapm_dai_link:
2086 list_add(&path->list, &dapm->card->paths);
2087 list_add(&path->list_sink, &wsink->sources);
2088 list_add(&path->list_source, &wsource->sinks);
2089 path->connect = 1;
2090 return 0;
2091 case snd_soc_dapm_mux:
2092 case snd_soc_dapm_virt_mux:
2093 case snd_soc_dapm_value_mux:
2094 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2095 &wsink->kcontrol_news[0]);
2096 if (ret != 0)
2097 goto err;
2098 break;
2099 case snd_soc_dapm_switch:
2100 case snd_soc_dapm_mixer:
2101 case snd_soc_dapm_mixer_named_ctl:
2102 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2103 if (ret != 0)
2104 goto err;
2105 break;
2106 case snd_soc_dapm_hp:
2107 case snd_soc_dapm_mic:
2108 case snd_soc_dapm_line:
2109 case snd_soc_dapm_spk:
2110 list_add(&path->list, &dapm->card->paths);
2111 list_add(&path->list_sink, &wsink->sources);
2112 list_add(&path->list_source, &wsource->sinks);
2113 path->connect = 0;
2114 return 0;
2116 return 0;
2118 err:
2119 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2120 source, control, sink);
2121 kfree(path);
2122 return ret;
2126 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2127 * @dapm: DAPM context
2128 * @route: audio routes
2129 * @num: number of routes
2131 * Connects 2 dapm widgets together via a named audio path. The sink is
2132 * the widget receiving the audio signal, whilst the source is the sender
2133 * of the audio signal.
2135 * Returns 0 for success else error. On error all resources can be freed
2136 * with a call to snd_soc_card_free().
2138 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2139 const struct snd_soc_dapm_route *route, int num)
2141 int i, ret = 0;
2143 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2144 for (i = 0; i < num; i++) {
2145 ret = snd_soc_dapm_add_route(dapm, route);
2146 if (ret < 0) {
2147 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2148 route->source, route->sink);
2149 break;
2151 route++;
2153 mutex_unlock(&dapm->card->dapm_mutex);
2155 return ret;
2157 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2159 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2160 const struct snd_soc_dapm_route *route)
2162 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2163 route->source,
2164 true);
2165 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2166 route->sink,
2167 true);
2168 struct snd_soc_dapm_path *path;
2169 int count = 0;
2171 if (!source) {
2172 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2173 route->source);
2174 return -ENODEV;
2177 if (!sink) {
2178 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2179 route->sink);
2180 return -ENODEV;
2183 if (route->control || route->connected)
2184 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2185 route->source, route->sink);
2187 list_for_each_entry(path, &source->sinks, list_source) {
2188 if (path->sink == sink) {
2189 path->weak = 1;
2190 count++;
2194 if (count == 0)
2195 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2196 route->source, route->sink);
2197 if (count > 1)
2198 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2199 count, route->source, route->sink);
2201 return 0;
2205 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2206 * @dapm: DAPM context
2207 * @route: audio routes
2208 * @num: number of routes
2210 * Mark existing routes matching those specified in the passed array
2211 * as being weak, meaning that they are ignored for the purpose of
2212 * power decisions. The main intended use case is for sidetone paths
2213 * which couple audio between other independent paths if they are both
2214 * active in order to make the combination work better at the user
2215 * level but which aren't intended to be "used".
2217 * Note that CODEC drivers should not use this as sidetone type paths
2218 * can frequently also be used as bypass paths.
2220 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2221 const struct snd_soc_dapm_route *route, int num)
2223 int i, err;
2224 int ret = 0;
2226 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2227 for (i = 0; i < num; i++) {
2228 err = snd_soc_dapm_weak_route(dapm, route);
2229 if (err)
2230 ret = err;
2231 route++;
2233 mutex_unlock(&dapm->card->dapm_mutex);
2235 return ret;
2237 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2240 * snd_soc_dapm_new_widgets - add new dapm widgets
2241 * @dapm: DAPM context
2243 * Checks the codec for any new dapm widgets and creates them if found.
2245 * Returns 0 for success.
2247 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2249 struct snd_soc_dapm_widget *w;
2250 unsigned int val;
2252 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2254 list_for_each_entry(w, &dapm->card->widgets, list)
2256 if (w->new)
2257 continue;
2259 if (w->num_kcontrols) {
2260 w->kcontrols = kzalloc(w->num_kcontrols *
2261 sizeof(struct snd_kcontrol *),
2262 GFP_KERNEL);
2263 if (!w->kcontrols) {
2264 mutex_unlock(&dapm->card->dapm_mutex);
2265 return -ENOMEM;
2269 switch(w->id) {
2270 case snd_soc_dapm_switch:
2271 case snd_soc_dapm_mixer:
2272 case snd_soc_dapm_mixer_named_ctl:
2273 dapm_new_mixer(w);
2274 break;
2275 case snd_soc_dapm_mux:
2276 case snd_soc_dapm_virt_mux:
2277 case snd_soc_dapm_value_mux:
2278 dapm_new_mux(w);
2279 break;
2280 case snd_soc_dapm_pga:
2281 case snd_soc_dapm_out_drv:
2282 dapm_new_pga(w);
2283 break;
2284 default:
2285 break;
2288 /* Read the initial power state from the device */
2289 if (w->reg >= 0) {
2290 val = soc_widget_read(w, w->reg);
2291 val &= 1 << w->shift;
2292 if (w->invert)
2293 val = !val;
2295 if (val)
2296 w->power = 1;
2299 w->new = 1;
2301 dapm_mark_dirty(w, "new widget");
2302 dapm_debugfs_add_widget(w);
2305 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2306 mutex_unlock(&dapm->card->dapm_mutex);
2307 return 0;
2309 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2312 * snd_soc_dapm_get_volsw - dapm mixer get callback
2313 * @kcontrol: mixer control
2314 * @ucontrol: control element information
2316 * Callback to get the value of a dapm mixer control.
2318 * Returns 0 for success.
2320 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2321 struct snd_ctl_elem_value *ucontrol)
2323 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2324 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2325 struct soc_mixer_control *mc =
2326 (struct soc_mixer_control *)kcontrol->private_value;
2327 unsigned int reg = mc->reg;
2328 unsigned int shift = mc->shift;
2329 unsigned int rshift = mc->rshift;
2330 int max = mc->max;
2331 unsigned int invert = mc->invert;
2332 unsigned int mask = (1 << fls(max)) - 1;
2334 ucontrol->value.integer.value[0] =
2335 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2336 if (shift != rshift)
2337 ucontrol->value.integer.value[1] =
2338 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2339 if (invert) {
2340 ucontrol->value.integer.value[0] =
2341 max - ucontrol->value.integer.value[0];
2342 if (shift != rshift)
2343 ucontrol->value.integer.value[1] =
2344 max - ucontrol->value.integer.value[1];
2347 return 0;
2349 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2352 * snd_soc_dapm_put_volsw - dapm mixer set callback
2353 * @kcontrol: mixer control
2354 * @ucontrol: control element information
2356 * Callback to set the value of a dapm mixer control.
2358 * Returns 0 for success.
2360 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2361 struct snd_ctl_elem_value *ucontrol)
2363 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2364 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2365 struct snd_soc_codec *codec = widget->codec;
2366 struct snd_soc_card *card = codec->card;
2367 struct soc_mixer_control *mc =
2368 (struct soc_mixer_control *)kcontrol->private_value;
2369 unsigned int reg = mc->reg;
2370 unsigned int shift = mc->shift;
2371 int max = mc->max;
2372 unsigned int mask = (1 << fls(max)) - 1;
2373 unsigned int invert = mc->invert;
2374 unsigned int val;
2375 int connect, change;
2376 struct snd_soc_dapm_update update;
2377 int wi;
2379 val = (ucontrol->value.integer.value[0] & mask);
2381 if (invert)
2382 val = max - val;
2383 mask = mask << shift;
2384 val = val << shift;
2386 if (val)
2387 /* new connection */
2388 connect = invert ? 0 : 1;
2389 else
2390 /* old connection must be powered down */
2391 connect = invert ? 1 : 0;
2393 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2395 change = snd_soc_test_bits(widget->codec, reg, mask, val);
2396 if (change) {
2397 for (wi = 0; wi < wlist->num_widgets; wi++) {
2398 widget = wlist->widgets[wi];
2400 widget->value = val;
2402 update.kcontrol = kcontrol;
2403 update.widget = widget;
2404 update.reg = reg;
2405 update.mask = mask;
2406 update.val = val;
2407 widget->dapm->update = &update;
2409 soc_dapm_mixer_update_power(widget, kcontrol, connect);
2411 widget->dapm->update = NULL;
2415 mutex_unlock(&card->dapm_mutex);
2416 return 0;
2418 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2421 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2422 * @kcontrol: mixer control
2423 * @ucontrol: control element information
2425 * Callback to get the value of a dapm enumerated double mixer control.
2427 * Returns 0 for success.
2429 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2430 struct snd_ctl_elem_value *ucontrol)
2432 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2433 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2434 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2435 unsigned int val, bitmask;
2437 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2439 val = snd_soc_read(widget->codec, e->reg);
2440 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2441 if (e->shift_l != e->shift_r)
2442 ucontrol->value.enumerated.item[1] =
2443 (val >> e->shift_r) & (bitmask - 1);
2445 return 0;
2447 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2450 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2451 * @kcontrol: mixer control
2452 * @ucontrol: control element information
2454 * Callback to set the value of a dapm enumerated double mixer control.
2456 * Returns 0 for success.
2458 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2459 struct snd_ctl_elem_value *ucontrol)
2461 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2462 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2463 struct snd_soc_codec *codec = widget->codec;
2464 struct snd_soc_card *card = codec->card;
2465 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2466 unsigned int val, mux, change;
2467 unsigned int mask, bitmask;
2468 struct snd_soc_dapm_update update;
2469 int wi;
2471 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2473 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2474 return -EINVAL;
2475 mux = ucontrol->value.enumerated.item[0];
2476 val = mux << e->shift_l;
2477 mask = (bitmask - 1) << e->shift_l;
2478 if (e->shift_l != e->shift_r) {
2479 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2480 return -EINVAL;
2481 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2482 mask |= (bitmask - 1) << e->shift_r;
2485 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2487 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2488 if (change) {
2489 for (wi = 0; wi < wlist->num_widgets; wi++) {
2490 widget = wlist->widgets[wi];
2492 widget->value = val;
2494 update.kcontrol = kcontrol;
2495 update.widget = widget;
2496 update.reg = e->reg;
2497 update.mask = mask;
2498 update.val = val;
2499 widget->dapm->update = &update;
2501 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2503 widget->dapm->update = NULL;
2507 mutex_unlock(&card->dapm_mutex);
2508 return change;
2510 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2513 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2514 * @kcontrol: mixer control
2515 * @ucontrol: control element information
2517 * Returns 0 for success.
2519 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2520 struct snd_ctl_elem_value *ucontrol)
2522 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2523 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2525 ucontrol->value.enumerated.item[0] = widget->value;
2527 return 0;
2529 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2532 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2533 * @kcontrol: mixer control
2534 * @ucontrol: control element information
2536 * Returns 0 for success.
2538 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2539 struct snd_ctl_elem_value *ucontrol)
2541 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2542 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2543 struct snd_soc_codec *codec = widget->codec;
2544 struct snd_soc_card *card = codec->card;
2545 struct soc_enum *e =
2546 (struct soc_enum *)kcontrol->private_value;
2547 int change;
2548 int ret = 0;
2549 int wi;
2551 if (ucontrol->value.enumerated.item[0] >= e->max)
2552 return -EINVAL;
2554 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2556 change = widget->value != ucontrol->value.enumerated.item[0];
2557 if (change) {
2558 for (wi = 0; wi < wlist->num_widgets; wi++) {
2559 widget = wlist->widgets[wi];
2561 widget->value = ucontrol->value.enumerated.item[0];
2563 soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
2567 mutex_unlock(&card->dapm_mutex);
2568 return ret;
2570 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2573 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2574 * callback
2575 * @kcontrol: mixer control
2576 * @ucontrol: control element information
2578 * Callback to get the value of a dapm semi enumerated double mixer control.
2580 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2581 * used for handling bitfield coded enumeration for example.
2583 * Returns 0 for success.
2585 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2586 struct snd_ctl_elem_value *ucontrol)
2588 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2589 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2590 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2591 unsigned int reg_val, val, mux;
2593 reg_val = snd_soc_read(widget->codec, e->reg);
2594 val = (reg_val >> e->shift_l) & e->mask;
2595 for (mux = 0; mux < e->max; mux++) {
2596 if (val == e->values[mux])
2597 break;
2599 ucontrol->value.enumerated.item[0] = mux;
2600 if (e->shift_l != e->shift_r) {
2601 val = (reg_val >> e->shift_r) & e->mask;
2602 for (mux = 0; mux < e->max; mux++) {
2603 if (val == e->values[mux])
2604 break;
2606 ucontrol->value.enumerated.item[1] = mux;
2609 return 0;
2611 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2614 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2615 * callback
2616 * @kcontrol: mixer control
2617 * @ucontrol: control element information
2619 * Callback to set the value of a dapm semi enumerated double mixer control.
2621 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2622 * used for handling bitfield coded enumeration for example.
2624 * Returns 0 for success.
2626 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2627 struct snd_ctl_elem_value *ucontrol)
2629 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2630 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2631 struct snd_soc_codec *codec = widget->codec;
2632 struct snd_soc_card *card = codec->card;
2633 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2634 unsigned int val, mux, change;
2635 unsigned int mask;
2636 struct snd_soc_dapm_update update;
2637 int wi;
2639 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2640 return -EINVAL;
2641 mux = ucontrol->value.enumerated.item[0];
2642 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2643 mask = e->mask << e->shift_l;
2644 if (e->shift_l != e->shift_r) {
2645 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2646 return -EINVAL;
2647 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2648 mask |= e->mask << e->shift_r;
2651 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2653 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2654 if (change) {
2655 for (wi = 0; wi < wlist->num_widgets; wi++) {
2656 widget = wlist->widgets[wi];
2658 widget->value = val;
2660 update.kcontrol = kcontrol;
2661 update.widget = widget;
2662 update.reg = e->reg;
2663 update.mask = mask;
2664 update.val = val;
2665 widget->dapm->update = &update;
2667 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2669 widget->dapm->update = NULL;
2673 mutex_unlock(&card->dapm_mutex);
2674 return change;
2676 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2679 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2681 * @kcontrol: mixer control
2682 * @uinfo: control element information
2684 * Callback to provide information about a pin switch control.
2686 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2687 struct snd_ctl_elem_info *uinfo)
2689 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2690 uinfo->count = 1;
2691 uinfo->value.integer.min = 0;
2692 uinfo->value.integer.max = 1;
2694 return 0;
2696 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2699 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2701 * @kcontrol: mixer control
2702 * @ucontrol: Value
2704 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2705 struct snd_ctl_elem_value *ucontrol)
2707 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2708 const char *pin = (const char *)kcontrol->private_value;
2710 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2712 ucontrol->value.integer.value[0] =
2713 snd_soc_dapm_get_pin_status(&card->dapm, pin);
2715 mutex_unlock(&card->dapm_mutex);
2717 return 0;
2719 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2722 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2724 * @kcontrol: mixer control
2725 * @ucontrol: Value
2727 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2728 struct snd_ctl_elem_value *ucontrol)
2730 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2731 const char *pin = (const char *)kcontrol->private_value;
2733 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2735 if (ucontrol->value.integer.value[0])
2736 snd_soc_dapm_enable_pin(&card->dapm, pin);
2737 else
2738 snd_soc_dapm_disable_pin(&card->dapm, pin);
2740 mutex_unlock(&card->dapm_mutex);
2742 snd_soc_dapm_sync(&card->dapm);
2743 return 0;
2745 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2747 static struct snd_soc_dapm_widget *
2748 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2749 const struct snd_soc_dapm_widget *widget)
2751 struct snd_soc_dapm_widget *w;
2752 size_t name_len;
2753 int ret;
2755 if ((w = dapm_cnew_widget(widget)) == NULL)
2756 return NULL;
2758 switch (w->id) {
2759 case snd_soc_dapm_regulator_supply:
2760 w->regulator = devm_regulator_get(dapm->dev, w->name);
2761 if (IS_ERR(w->regulator)) {
2762 ret = PTR_ERR(w->regulator);
2763 dev_err(dapm->dev, "Failed to request %s: %d\n",
2764 w->name, ret);
2765 return NULL;
2767 break;
2768 default:
2769 break;
2772 name_len = strlen(widget->name) + 1;
2773 if (dapm->codec && dapm->codec->name_prefix)
2774 name_len += 1 + strlen(dapm->codec->name_prefix);
2775 w->name = kmalloc(name_len, GFP_KERNEL);
2776 if (w->name == NULL) {
2777 kfree(w);
2778 return NULL;
2780 if (dapm->codec && dapm->codec->name_prefix)
2781 snprintf((char *)w->name, name_len, "%s %s",
2782 dapm->codec->name_prefix, widget->name);
2783 else
2784 snprintf((char *)w->name, name_len, "%s", widget->name);
2786 switch (w->id) {
2787 case snd_soc_dapm_switch:
2788 case snd_soc_dapm_mixer:
2789 case snd_soc_dapm_mixer_named_ctl:
2790 w->power_check = dapm_generic_check_power;
2791 break;
2792 case snd_soc_dapm_mux:
2793 case snd_soc_dapm_virt_mux:
2794 case snd_soc_dapm_value_mux:
2795 w->power_check = dapm_generic_check_power;
2796 break;
2797 case snd_soc_dapm_adc:
2798 case snd_soc_dapm_aif_out:
2799 w->power_check = dapm_adc_check_power;
2800 break;
2801 case snd_soc_dapm_dac:
2802 case snd_soc_dapm_aif_in:
2803 w->power_check = dapm_dac_check_power;
2804 break;
2805 case snd_soc_dapm_pga:
2806 case snd_soc_dapm_out_drv:
2807 case snd_soc_dapm_input:
2808 case snd_soc_dapm_output:
2809 case snd_soc_dapm_micbias:
2810 case snd_soc_dapm_spk:
2811 case snd_soc_dapm_hp:
2812 case snd_soc_dapm_mic:
2813 case snd_soc_dapm_line:
2814 case snd_soc_dapm_dai_link:
2815 w->power_check = dapm_generic_check_power;
2816 break;
2817 case snd_soc_dapm_supply:
2818 case snd_soc_dapm_regulator_supply:
2819 w->power_check = dapm_supply_check_power;
2820 break;
2821 case snd_soc_dapm_dai:
2822 w->power_check = dapm_dai_check_power;
2823 break;
2824 default:
2825 w->power_check = dapm_always_on_check_power;
2826 break;
2829 dapm->n_widgets++;
2830 w->dapm = dapm;
2831 w->codec = dapm->codec;
2832 w->platform = dapm->platform;
2833 INIT_LIST_HEAD(&w->sources);
2834 INIT_LIST_HEAD(&w->sinks);
2835 INIT_LIST_HEAD(&w->list);
2836 INIT_LIST_HEAD(&w->dirty);
2837 list_add(&w->list, &dapm->card->widgets);
2839 /* machine layer set ups unconnected pins and insertions */
2840 w->connected = 1;
2841 return w;
2845 * snd_soc_dapm_new_controls - create new dapm controls
2846 * @dapm: DAPM context
2847 * @widget: widget array
2848 * @num: number of widgets
2850 * Creates new DAPM controls based upon the templates.
2852 * Returns 0 for success else error.
2854 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2855 const struct snd_soc_dapm_widget *widget,
2856 int num)
2858 struct snd_soc_dapm_widget *w;
2859 int i;
2860 int ret = 0;
2862 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2863 for (i = 0; i < num; i++) {
2864 w = snd_soc_dapm_new_control(dapm, widget);
2865 if (!w) {
2866 dev_err(dapm->dev,
2867 "ASoC: Failed to create DAPM control %s\n",
2868 widget->name);
2869 ret = -ENOMEM;
2870 break;
2872 widget++;
2874 mutex_unlock(&dapm->card->dapm_mutex);
2875 return ret;
2877 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2879 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
2880 struct snd_kcontrol *kcontrol, int event)
2882 struct snd_soc_dapm_path *source_p, *sink_p;
2883 struct snd_soc_dai *source, *sink;
2884 const struct snd_soc_pcm_stream *config = w->params;
2885 struct snd_pcm_substream substream;
2886 struct snd_pcm_hw_params params;
2887 u64 fmt;
2888 int ret;
2890 BUG_ON(!config);
2891 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
2893 /* We only support a single source and sink, pick the first */
2894 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
2895 list_sink);
2896 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
2897 list_source);
2899 BUG_ON(!source_p || !sink_p);
2900 BUG_ON(!sink_p->source || !source_p->sink);
2901 BUG_ON(!source_p->source || !sink_p->sink);
2903 source = source_p->source->priv;
2904 sink = sink_p->sink->priv;
2906 /* Be a little careful as we don't want to overflow the mask array */
2907 if (config->formats) {
2908 fmt = ffs(config->formats) - 1;
2909 } else {
2910 dev_warn(w->dapm->dev, "Invalid format %llx specified\n",
2911 config->formats);
2912 fmt = 0;
2915 /* Currently very limited parameter selection */
2916 memset(&params, 0, sizeof(params));
2917 snd_mask_set(hw_param_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
2919 hw_param_interval(&params, SNDRV_PCM_HW_PARAM_RATE)->min =
2920 config->rate_min;
2921 hw_param_interval(&params, SNDRV_PCM_HW_PARAM_RATE)->max =
2922 config->rate_max;
2924 hw_param_interval(&params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
2925 = config->channels_min;
2926 hw_param_interval(&params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
2927 = config->channels_max;
2929 memset(&substream, 0, sizeof(substream));
2931 switch (event) {
2932 case SND_SOC_DAPM_PRE_PMU:
2933 if (source->driver->ops && source->driver->ops->hw_params) {
2934 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
2935 ret = source->driver->ops->hw_params(&substream,
2936 &params, source);
2937 if (ret != 0) {
2938 dev_err(source->dev,
2939 "hw_params() failed: %d\n", ret);
2940 return ret;
2944 if (sink->driver->ops && sink->driver->ops->hw_params) {
2945 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
2946 ret = sink->driver->ops->hw_params(&substream, &params,
2947 sink);
2948 if (ret != 0) {
2949 dev_err(sink->dev,
2950 "hw_params() failed: %d\n", ret);
2951 return ret;
2954 break;
2956 case SND_SOC_DAPM_POST_PMU:
2957 ret = snd_soc_dai_digital_mute(sink, 0);
2958 if (ret != 0 && ret != -ENOTSUPP)
2959 dev_warn(sink->dev, "Failed to unmute: %d\n", ret);
2960 break;
2962 case SND_SOC_DAPM_PRE_PMD:
2963 ret = snd_soc_dai_digital_mute(sink, 1);
2964 if (ret != 0 && ret != -ENOTSUPP)
2965 dev_warn(sink->dev, "Failed to mute: %d\n", ret);
2966 break;
2968 default:
2969 BUG();
2970 return -EINVAL;
2973 return 0;
2976 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
2977 const struct snd_soc_pcm_stream *params,
2978 struct snd_soc_dapm_widget *source,
2979 struct snd_soc_dapm_widget *sink)
2981 struct snd_soc_dapm_route routes[2];
2982 struct snd_soc_dapm_widget template;
2983 struct snd_soc_dapm_widget *w;
2984 size_t len;
2985 char *link_name;
2987 len = strlen(source->name) + strlen(sink->name) + 2;
2988 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
2989 if (!link_name)
2990 return -ENOMEM;
2991 snprintf(link_name, len, "%s-%s", source->name, sink->name);
2993 memset(&template, 0, sizeof(template));
2994 template.reg = SND_SOC_NOPM;
2995 template.id = snd_soc_dapm_dai_link;
2996 template.name = link_name;
2997 template.event = snd_soc_dai_link_event;
2998 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
2999 SND_SOC_DAPM_PRE_PMD;
3001 dev_dbg(card->dev, "adding %s widget\n", link_name);
3003 w = snd_soc_dapm_new_control(&card->dapm, &template);
3004 if (!w) {
3005 dev_err(card->dev, "Failed to create %s widget\n",
3006 link_name);
3007 return -ENOMEM;
3010 w->params = params;
3012 memset(&routes, 0, sizeof(routes));
3014 routes[0].source = source->name;
3015 routes[0].sink = link_name;
3016 routes[1].source = link_name;
3017 routes[1].sink = sink->name;
3019 return snd_soc_dapm_add_routes(&card->dapm, routes,
3020 ARRAY_SIZE(routes));
3023 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3024 struct snd_soc_dai *dai)
3026 struct snd_soc_dapm_widget template;
3027 struct snd_soc_dapm_widget *w;
3029 WARN_ON(dapm->dev != dai->dev);
3031 memset(&template, 0, sizeof(template));
3032 template.reg = SND_SOC_NOPM;
3034 if (dai->driver->playback.stream_name) {
3035 template.id = snd_soc_dapm_dai;
3036 template.name = dai->driver->playback.stream_name;
3037 template.sname = dai->driver->playback.stream_name;
3039 dev_dbg(dai->dev, "adding %s widget\n",
3040 template.name);
3042 w = snd_soc_dapm_new_control(dapm, &template);
3043 if (!w) {
3044 dev_err(dapm->dev, "Failed to create %s widget\n",
3045 dai->driver->playback.stream_name);
3048 w->priv = dai;
3049 dai->playback_widget = w;
3052 if (dai->driver->capture.stream_name) {
3053 template.id = snd_soc_dapm_dai;
3054 template.name = dai->driver->capture.stream_name;
3055 template.sname = dai->driver->capture.stream_name;
3057 dev_dbg(dai->dev, "adding %s widget\n",
3058 template.name);
3060 w = snd_soc_dapm_new_control(dapm, &template);
3061 if (!w) {
3062 dev_err(dapm->dev, "Failed to create %s widget\n",
3063 dai->driver->capture.stream_name);
3066 w->priv = dai;
3067 dai->capture_widget = w;
3070 return 0;
3073 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3075 struct snd_soc_dapm_widget *dai_w, *w;
3076 struct snd_soc_dai *dai;
3077 struct snd_soc_dapm_route r;
3079 memset(&r, 0, sizeof(r));
3081 /* For each DAI widget... */
3082 list_for_each_entry(dai_w, &card->widgets, list) {
3083 if (dai_w->id != snd_soc_dapm_dai)
3084 continue;
3086 dai = dai_w->priv;
3088 /* ...find all widgets with the same stream and link them */
3089 list_for_each_entry(w, &card->widgets, list) {
3090 if (w->dapm != dai_w->dapm)
3091 continue;
3093 if (w->id == snd_soc_dapm_dai)
3094 continue;
3096 if (!w->sname)
3097 continue;
3099 if (dai->driver->playback.stream_name &&
3100 strstr(w->sname,
3101 dai->driver->playback.stream_name)) {
3102 r.source = dai->playback_widget->name;
3103 r.sink = w->name;
3104 dev_dbg(dai->dev, "%s -> %s\n",
3105 r.source, r.sink);
3107 snd_soc_dapm_add_route(w->dapm, &r);
3110 if (dai->driver->capture.stream_name &&
3111 strstr(w->sname,
3112 dai->driver->capture.stream_name)) {
3113 r.source = w->name;
3114 r.sink = dai->capture_widget->name;
3115 dev_dbg(dai->dev, "%s -> %s\n",
3116 r.source, r.sink);
3118 snd_soc_dapm_add_route(w->dapm, &r);
3123 return 0;
3126 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3127 int event)
3130 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3131 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3132 struct snd_soc_dai *codec_dai = rtd->codec_dai;
3134 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3135 w_cpu = cpu_dai->playback_widget;
3136 w_codec = codec_dai->playback_widget;
3137 } else {
3138 w_cpu = cpu_dai->capture_widget;
3139 w_codec = codec_dai->capture_widget;
3142 if (w_cpu) {
3144 dapm_mark_dirty(w_cpu, "stream event");
3146 switch (event) {
3147 case SND_SOC_DAPM_STREAM_START:
3148 w_cpu->active = 1;
3149 break;
3150 case SND_SOC_DAPM_STREAM_STOP:
3151 w_cpu->active = 0;
3152 break;
3153 case SND_SOC_DAPM_STREAM_SUSPEND:
3154 case SND_SOC_DAPM_STREAM_RESUME:
3155 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3156 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3157 break;
3161 if (w_codec) {
3163 dapm_mark_dirty(w_codec, "stream event");
3165 switch (event) {
3166 case SND_SOC_DAPM_STREAM_START:
3167 w_codec->active = 1;
3168 break;
3169 case SND_SOC_DAPM_STREAM_STOP:
3170 w_codec->active = 0;
3171 break;
3172 case SND_SOC_DAPM_STREAM_SUSPEND:
3173 case SND_SOC_DAPM_STREAM_RESUME:
3174 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3175 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3176 break;
3180 dapm_power_widgets(&rtd->card->dapm, event);
3184 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3185 * @rtd: PCM runtime data
3186 * @stream: stream name
3187 * @event: stream event
3189 * Sends a stream event to the dapm core. The core then makes any
3190 * necessary widget power changes.
3192 * Returns 0 for success else error.
3194 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3195 int event)
3197 struct snd_soc_card *card = rtd->card;
3199 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3200 soc_dapm_stream_event(rtd, stream, event);
3201 mutex_unlock(&card->dapm_mutex);
3205 * snd_soc_dapm_enable_pin - enable pin.
3206 * @dapm: DAPM context
3207 * @pin: pin name
3209 * Enables input/output pin and its parents or children widgets iff there is
3210 * a valid audio route and active audio stream.
3211 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3212 * do any widget power switching.
3214 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3216 return snd_soc_dapm_set_pin(dapm, pin, 1);
3218 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3221 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3222 * @dapm: DAPM context
3223 * @pin: pin name
3225 * Enables input/output pin regardless of any other state. This is
3226 * intended for use with microphone bias supplies used in microphone
3227 * jack detection.
3229 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3230 * do any widget power switching.
3232 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3233 const char *pin)
3235 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3237 if (!w) {
3238 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3239 return -EINVAL;
3242 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
3243 w->connected = 1;
3244 w->force = 1;
3245 dapm_mark_dirty(w, "force enable");
3247 return 0;
3249 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3252 * snd_soc_dapm_disable_pin - disable pin.
3253 * @dapm: DAPM context
3254 * @pin: pin name
3256 * Disables input/output pin and its parents or children widgets.
3257 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3258 * do any widget power switching.
3260 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3261 const char *pin)
3263 return snd_soc_dapm_set_pin(dapm, pin, 0);
3265 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3268 * snd_soc_dapm_nc_pin - permanently disable pin.
3269 * @dapm: DAPM context
3270 * @pin: pin name
3272 * Marks the specified pin as being not connected, disabling it along
3273 * any parent or child widgets. At present this is identical to
3274 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3275 * additional things such as disabling controls which only affect
3276 * paths through the pin.
3278 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3279 * do any widget power switching.
3281 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3283 return snd_soc_dapm_set_pin(dapm, pin, 0);
3285 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3288 * snd_soc_dapm_get_pin_status - get audio pin status
3289 * @dapm: DAPM context
3290 * @pin: audio signal pin endpoint (or start point)
3292 * Get audio pin status - connected or disconnected.
3294 * Returns 1 for connected otherwise 0.
3296 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3297 const char *pin)
3299 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3301 if (w)
3302 return w->connected;
3304 return 0;
3306 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3309 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3310 * @dapm: DAPM context
3311 * @pin: audio signal pin endpoint (or start point)
3313 * Mark the given endpoint or pin as ignoring suspend. When the
3314 * system is disabled a path between two endpoints flagged as ignoring
3315 * suspend will not be disabled. The path must already be enabled via
3316 * normal means at suspend time, it will not be turned on if it was not
3317 * already enabled.
3319 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3320 const char *pin)
3322 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3324 if (!w) {
3325 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3326 return -EINVAL;
3329 w->ignore_suspend = 1;
3331 return 0;
3333 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3335 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3336 struct snd_soc_dapm_widget *w)
3338 struct snd_soc_dapm_path *p;
3340 list_for_each_entry(p, &card->paths, list) {
3341 if ((p->source == w) || (p->sink == w)) {
3342 dev_dbg(card->dev,
3343 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3344 p->source->name, p->source->id, p->source->dapm,
3345 p->sink->name, p->sink->id, p->sink->dapm);
3347 /* Connected to something other than the codec */
3348 if (p->source->dapm != p->sink->dapm)
3349 return true;
3351 * Loopback connection from codec external pin to
3352 * codec external pin
3354 if (p->sink->id == snd_soc_dapm_input) {
3355 switch (p->source->id) {
3356 case snd_soc_dapm_output:
3357 case snd_soc_dapm_micbias:
3358 return true;
3359 default:
3360 break;
3366 return false;
3370 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3371 * @codec: The codec whose pins should be processed
3373 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3374 * which are unused. Pins are used if they are connected externally to the
3375 * codec, whether that be to some other device, or a loop-back connection to
3376 * the codec itself.
3378 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3380 struct snd_soc_card *card = codec->card;
3381 struct snd_soc_dapm_context *dapm = &codec->dapm;
3382 struct snd_soc_dapm_widget *w;
3384 dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
3385 &card->dapm, &codec->dapm);
3387 list_for_each_entry(w, &card->widgets, list) {
3388 if (w->dapm != dapm)
3389 continue;
3390 switch (w->id) {
3391 case snd_soc_dapm_input:
3392 case snd_soc_dapm_output:
3393 case snd_soc_dapm_micbias:
3394 dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
3395 w->name);
3396 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3397 dev_dbg(codec->dev,
3398 "... Not in map; disabling\n");
3399 snd_soc_dapm_nc_pin(dapm, w->name);
3401 break;
3402 default:
3403 break;
3409 * snd_soc_dapm_free - free dapm resources
3410 * @dapm: DAPM context
3412 * Free all dapm widgets and resources.
3414 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3416 snd_soc_dapm_sys_remove(dapm->dev);
3417 dapm_debugfs_cleanup(dapm);
3418 dapm_free_widgets(dapm);
3419 list_del(&dapm->list);
3421 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3423 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
3425 struct snd_soc_dapm_widget *w;
3426 LIST_HEAD(down_list);
3427 int powerdown = 0;
3429 list_for_each_entry(w, &dapm->card->widgets, list) {
3430 if (w->dapm != dapm)
3431 continue;
3432 if (w->power) {
3433 dapm_seq_insert(w, &down_list, false);
3434 w->power = 0;
3435 powerdown = 1;
3439 /* If there were no widgets to power down we're already in
3440 * standby.
3442 if (powerdown) {
3443 if (dapm->bias_level == SND_SOC_BIAS_ON)
3444 snd_soc_dapm_set_bias_level(dapm,
3445 SND_SOC_BIAS_PREPARE);
3446 dapm_seq_run(dapm, &down_list, 0, false);
3447 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3448 snd_soc_dapm_set_bias_level(dapm,
3449 SND_SOC_BIAS_STANDBY);
3454 * snd_soc_dapm_shutdown - callback for system shutdown
3456 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3458 struct snd_soc_codec *codec;
3460 list_for_each_entry(codec, &card->codec_dev_list, list) {
3461 soc_dapm_shutdown_codec(&codec->dapm);
3462 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3463 snd_soc_dapm_set_bias_level(&codec->dapm,
3464 SND_SOC_BIAS_OFF);
3468 /* Module information */
3469 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3470 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3471 MODULE_LICENSE("GPL");