mtip32xx: fix error handling in mtip_init()
[zen-stable.git] / sound / soc / soc-dapm.c
blobac6b8693da8c41ec51211271b8f086f7a9234040
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/meadphone 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 powerdown of audio susbsystem to reduce pops between a quick
22 * device reopen.
24 * Todo:
25 * o DAPM power change sequencing - allow for configurable per
26 * codec sequences.
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/init.h>
35 #include <linux/async.h>
36 #include <linux/delay.h>
37 #include <linux/pm.h>
38 #include <linux/bitops.h>
39 #include <linux/platform_device.h>
40 #include <linux/jiffies.h>
41 #include <linux/debugfs.h>
42 #include <linux/pm_runtime.h>
43 #include <linux/slab.h>
44 #include <sound/core.h>
45 #include <sound/pcm.h>
46 #include <sound/pcm_params.h>
47 #include <sound/soc.h>
48 #include <sound/initval.h>
50 #include <trace/events/asoc.h>
52 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
54 /* dapm power sequences - make this per codec in the future */
55 static int dapm_up_seq[] = {
56 [snd_soc_dapm_pre] = 0,
57 [snd_soc_dapm_supply] = 1,
58 [snd_soc_dapm_micbias] = 2,
59 [snd_soc_dapm_aif_in] = 3,
60 [snd_soc_dapm_aif_out] = 3,
61 [snd_soc_dapm_mic] = 4,
62 [snd_soc_dapm_mux] = 5,
63 [snd_soc_dapm_virt_mux] = 5,
64 [snd_soc_dapm_value_mux] = 5,
65 [snd_soc_dapm_dac] = 6,
66 [snd_soc_dapm_mixer] = 7,
67 [snd_soc_dapm_mixer_named_ctl] = 7,
68 [snd_soc_dapm_pga] = 8,
69 [snd_soc_dapm_adc] = 9,
70 [snd_soc_dapm_out_drv] = 10,
71 [snd_soc_dapm_hp] = 10,
72 [snd_soc_dapm_spk] = 10,
73 [snd_soc_dapm_line] = 10,
74 [snd_soc_dapm_post] = 11,
77 static int dapm_down_seq[] = {
78 [snd_soc_dapm_pre] = 0,
79 [snd_soc_dapm_adc] = 1,
80 [snd_soc_dapm_hp] = 2,
81 [snd_soc_dapm_spk] = 2,
82 [snd_soc_dapm_line] = 2,
83 [snd_soc_dapm_out_drv] = 2,
84 [snd_soc_dapm_pga] = 4,
85 [snd_soc_dapm_mixer_named_ctl] = 5,
86 [snd_soc_dapm_mixer] = 5,
87 [snd_soc_dapm_dac] = 6,
88 [snd_soc_dapm_mic] = 7,
89 [snd_soc_dapm_micbias] = 8,
90 [snd_soc_dapm_mux] = 9,
91 [snd_soc_dapm_virt_mux] = 9,
92 [snd_soc_dapm_value_mux] = 9,
93 [snd_soc_dapm_aif_in] = 10,
94 [snd_soc_dapm_aif_out] = 10,
95 [snd_soc_dapm_supply] = 11,
96 [snd_soc_dapm_post] = 12,
99 static void pop_wait(u32 pop_time)
101 if (pop_time)
102 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
105 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
107 va_list args;
108 char *buf;
110 if (!pop_time)
111 return;
113 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
114 if (buf == NULL)
115 return;
117 va_start(args, fmt);
118 vsnprintf(buf, PAGE_SIZE, fmt, args);
119 dev_info(dev, "%s", buf);
120 va_end(args);
122 kfree(buf);
125 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
127 return !list_empty(&w->dirty);
130 void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
132 if (!dapm_dirty_widget(w)) {
133 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
134 w->name, reason);
135 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
138 EXPORT_SYMBOL_GPL(dapm_mark_dirty);
140 /* create a new dapm widget */
141 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
142 const struct snd_soc_dapm_widget *_widget)
144 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
147 /* get snd_card from DAPM context */
148 static inline struct snd_card *dapm_get_snd_card(
149 struct snd_soc_dapm_context *dapm)
151 if (dapm->codec)
152 return dapm->codec->card->snd_card;
153 else if (dapm->platform)
154 return dapm->platform->card->snd_card;
155 else
156 BUG();
158 /* unreachable */
159 return NULL;
162 /* get soc_card from DAPM context */
163 static inline struct snd_soc_card *dapm_get_soc_card(
164 struct snd_soc_dapm_context *dapm)
166 if (dapm->codec)
167 return dapm->codec->card;
168 else if (dapm->platform)
169 return dapm->platform->card;
170 else
171 BUG();
173 /* unreachable */
174 return NULL;
177 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
179 if (w->codec)
180 return snd_soc_read(w->codec, reg);
181 else if (w->platform)
182 return snd_soc_platform_read(w->platform, reg);
184 dev_err(w->dapm->dev, "no valid widget read method\n");
185 return -1;
188 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
190 if (w->codec)
191 return snd_soc_write(w->codec, reg, val);
192 else if (w->platform)
193 return snd_soc_platform_write(w->platform, reg, val);
195 dev_err(w->dapm->dev, "no valid widget write method\n");
196 return -1;
199 static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
200 unsigned short reg, unsigned int mask, unsigned int value)
202 int change;
203 unsigned int old, new;
204 int ret;
206 ret = soc_widget_read(w, reg);
207 if (ret < 0)
208 return ret;
210 old = ret;
211 new = (old & ~mask) | (value & mask);
212 change = old != new;
213 if (change) {
214 ret = soc_widget_write(w, reg, new);
215 if (ret < 0)
216 return ret;
219 return change;
223 * snd_soc_dapm_set_bias_level - set the bias level for the system
224 * @dapm: DAPM context
225 * @level: level to configure
227 * Configure the bias (power) levels for the SoC audio device.
229 * Returns 0 for success else error.
231 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
232 enum snd_soc_bias_level level)
234 struct snd_soc_card *card = dapm->card;
235 int ret = 0;
237 trace_snd_soc_bias_level_start(card, level);
239 if (card && card->set_bias_level)
240 ret = card->set_bias_level(card, dapm, level);
241 if (ret != 0)
242 goto out;
244 if (dapm->codec) {
245 if (dapm->codec->driver->set_bias_level)
246 ret = dapm->codec->driver->set_bias_level(dapm->codec,
247 level);
248 else
249 dapm->bias_level = level;
251 if (ret != 0)
252 goto out;
254 if (card && card->set_bias_level_post)
255 ret = card->set_bias_level_post(card, dapm, level);
256 out:
257 trace_snd_soc_bias_level_done(card, level);
259 return ret;
262 /* set up initial codec paths */
263 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
264 struct snd_soc_dapm_path *p, int i)
266 switch (w->id) {
267 case snd_soc_dapm_switch:
268 case snd_soc_dapm_mixer:
269 case snd_soc_dapm_mixer_named_ctl: {
270 int val;
271 struct soc_mixer_control *mc = (struct soc_mixer_control *)
272 w->kcontrol_news[i].private_value;
273 unsigned int reg = mc->reg;
274 unsigned int shift = mc->shift;
275 int max = mc->max;
276 unsigned int mask = (1 << fls(max)) - 1;
277 unsigned int invert = mc->invert;
279 val = soc_widget_read(w, reg);
280 val = (val >> shift) & mask;
282 if ((invert && !val) || (!invert && val))
283 p->connect = 1;
284 else
285 p->connect = 0;
287 break;
288 case snd_soc_dapm_mux: {
289 struct soc_enum *e = (struct soc_enum *)
290 w->kcontrol_news[i].private_value;
291 int val, item, bitmask;
293 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
295 val = soc_widget_read(w, e->reg);
296 item = (val >> e->shift_l) & (bitmask - 1);
298 p->connect = 0;
299 for (i = 0; i < e->max; i++) {
300 if (!(strcmp(p->name, e->texts[i])) && item == i)
301 p->connect = 1;
304 break;
305 case snd_soc_dapm_virt_mux: {
306 struct soc_enum *e = (struct soc_enum *)
307 w->kcontrol_news[i].private_value;
309 p->connect = 0;
310 /* since a virtual mux has no backing registers to
311 * decide which path to connect, it will try to match
312 * with the first enumeration. This is to ensure
313 * that the default mux choice (the first) will be
314 * correctly powered up during initialization.
316 if (!strcmp(p->name, e->texts[0]))
317 p->connect = 1;
319 break;
320 case snd_soc_dapm_value_mux: {
321 struct soc_enum *e = (struct soc_enum *)
322 w->kcontrol_news[i].private_value;
323 int val, item;
325 val = soc_widget_read(w, e->reg);
326 val = (val >> e->shift_l) & e->mask;
327 for (item = 0; item < e->max; item++) {
328 if (val == e->values[item])
329 break;
332 p->connect = 0;
333 for (i = 0; i < e->max; i++) {
334 if (!(strcmp(p->name, e->texts[i])) && item == i)
335 p->connect = 1;
338 break;
339 /* does not affect routing - always connected */
340 case snd_soc_dapm_pga:
341 case snd_soc_dapm_out_drv:
342 case snd_soc_dapm_output:
343 case snd_soc_dapm_adc:
344 case snd_soc_dapm_input:
345 case snd_soc_dapm_siggen:
346 case snd_soc_dapm_dac:
347 case snd_soc_dapm_micbias:
348 case snd_soc_dapm_vmid:
349 case snd_soc_dapm_supply:
350 case snd_soc_dapm_aif_in:
351 case snd_soc_dapm_aif_out:
352 case snd_soc_dapm_hp:
353 case snd_soc_dapm_mic:
354 case snd_soc_dapm_spk:
355 case snd_soc_dapm_line:
356 p->connect = 1;
357 break;
358 /* does affect routing - dynamically connected */
359 case snd_soc_dapm_pre:
360 case snd_soc_dapm_post:
361 p->connect = 0;
362 break;
366 /* connect mux widget to its interconnecting audio paths */
367 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
368 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
369 struct snd_soc_dapm_path *path, const char *control_name,
370 const struct snd_kcontrol_new *kcontrol)
372 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
373 int i;
375 for (i = 0; i < e->max; i++) {
376 if (!(strcmp(control_name, e->texts[i]))) {
377 list_add(&path->list, &dapm->card->paths);
378 list_add(&path->list_sink, &dest->sources);
379 list_add(&path->list_source, &src->sinks);
380 path->name = (char*)e->texts[i];
381 dapm_set_path_status(dest, path, 0);
382 return 0;
386 return -ENODEV;
389 /* connect mixer widget to its interconnecting audio paths */
390 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
391 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
392 struct snd_soc_dapm_path *path, const char *control_name)
394 int i;
396 /* search for mixer kcontrol */
397 for (i = 0; i < dest->num_kcontrols; i++) {
398 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
399 list_add(&path->list, &dapm->card->paths);
400 list_add(&path->list_sink, &dest->sources);
401 list_add(&path->list_source, &src->sinks);
402 path->name = dest->kcontrol_news[i].name;
403 dapm_set_path_status(dest, path, i);
404 return 0;
407 return -ENODEV;
410 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
411 struct snd_soc_dapm_widget *kcontrolw,
412 const struct snd_kcontrol_new *kcontrol_new,
413 struct snd_kcontrol **kcontrol)
415 struct snd_soc_dapm_widget *w;
416 int i;
418 *kcontrol = NULL;
420 list_for_each_entry(w, &dapm->card->widgets, list) {
421 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
422 continue;
423 for (i = 0; i < w->num_kcontrols; i++) {
424 if (&w->kcontrol_news[i] == kcontrol_new) {
425 if (w->kcontrols)
426 *kcontrol = w->kcontrols[i];
427 return 1;
432 return 0;
435 /* create new dapm mixer control */
436 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
438 struct snd_soc_dapm_context *dapm = w->dapm;
439 int i, ret = 0;
440 size_t name_len, prefix_len;
441 struct snd_soc_dapm_path *path;
442 struct snd_card *card = dapm->card->snd_card;
443 const char *prefix;
444 struct snd_soc_dapm_widget_list *wlist;
445 size_t wlistsize;
447 if (dapm->codec)
448 prefix = dapm->codec->name_prefix;
449 else
450 prefix = NULL;
452 if (prefix)
453 prefix_len = strlen(prefix) + 1;
454 else
455 prefix_len = 0;
457 /* add kcontrol */
458 for (i = 0; i < w->num_kcontrols; i++) {
460 /* match name */
461 list_for_each_entry(path, &w->sources, list_sink) {
463 /* mixer/mux paths name must match control name */
464 if (path->name != (char *)w->kcontrol_news[i].name)
465 continue;
467 if (w->kcontrols[i]) {
468 path->kcontrol = w->kcontrols[i];
469 continue;
472 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
473 sizeof(struct snd_soc_dapm_widget *),
474 wlist = kzalloc(wlistsize, GFP_KERNEL);
475 if (wlist == NULL) {
476 dev_err(dapm->dev,
477 "asoc: can't allocate widget list for %s\n",
478 w->name);
479 return -ENOMEM;
481 wlist->num_widgets = 1;
482 wlist->widgets[0] = w;
484 /* add dapm control with long name.
485 * for dapm_mixer this is the concatenation of the
486 * mixer and kcontrol name.
487 * for dapm_mixer_named_ctl this is simply the
488 * kcontrol name.
490 name_len = strlen(w->kcontrol_news[i].name) + 1;
491 if (w->id != snd_soc_dapm_mixer_named_ctl)
492 name_len += 1 + strlen(w->name);
494 path->long_name = kmalloc(name_len, GFP_KERNEL);
496 if (path->long_name == NULL) {
497 kfree(wlist);
498 return -ENOMEM;
501 switch (w->id) {
502 default:
503 /* The control will get a prefix from
504 * the control creation process but
505 * we're also using the same prefix
506 * for widgets so cut the prefix off
507 * the front of the widget name.
509 snprintf(path->long_name, name_len, "%s %s",
510 w->name + prefix_len,
511 w->kcontrol_news[i].name);
512 break;
513 case snd_soc_dapm_mixer_named_ctl:
514 snprintf(path->long_name, name_len, "%s",
515 w->kcontrol_news[i].name);
516 break;
519 path->long_name[name_len - 1] = '\0';
521 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
522 wlist, path->long_name,
523 prefix);
524 ret = snd_ctl_add(card, path->kcontrol);
525 if (ret < 0) {
526 dev_err(dapm->dev,
527 "asoc: failed to add dapm kcontrol %s: %d\n",
528 path->long_name, ret);
529 kfree(wlist);
530 kfree(path->long_name);
531 path->long_name = NULL;
532 return ret;
534 w->kcontrols[i] = path->kcontrol;
537 return ret;
540 /* create new dapm mux control */
541 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
543 struct snd_soc_dapm_context *dapm = w->dapm;
544 struct snd_soc_dapm_path *path = NULL;
545 struct snd_kcontrol *kcontrol;
546 struct snd_card *card = dapm->card->snd_card;
547 const char *prefix;
548 size_t prefix_len;
549 int ret;
550 struct snd_soc_dapm_widget_list *wlist;
551 int shared, wlistentries;
552 size_t wlistsize;
553 char *name;
555 if (w->num_kcontrols != 1) {
556 dev_err(dapm->dev,
557 "asoc: mux %s has incorrect number of controls\n",
558 w->name);
559 return -EINVAL;
562 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
563 &kcontrol);
564 if (kcontrol) {
565 wlist = kcontrol->private_data;
566 wlistentries = wlist->num_widgets + 1;
567 } else {
568 wlist = NULL;
569 wlistentries = 1;
571 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
572 wlistentries * sizeof(struct snd_soc_dapm_widget *),
573 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
574 if (wlist == NULL) {
575 dev_err(dapm->dev,
576 "asoc: can't allocate widget list for %s\n", w->name);
577 return -ENOMEM;
579 wlist->num_widgets = wlistentries;
580 wlist->widgets[wlistentries - 1] = w;
582 if (!kcontrol) {
583 if (dapm->codec)
584 prefix = dapm->codec->name_prefix;
585 else
586 prefix = NULL;
588 if (shared) {
589 name = w->kcontrol_news[0].name;
590 prefix_len = 0;
591 } else {
592 name = w->name;
593 if (prefix)
594 prefix_len = strlen(prefix) + 1;
595 else
596 prefix_len = 0;
600 * The control will get a prefix from the control creation
601 * process but we're also using the same prefix for widgets so
602 * cut the prefix off the front of the widget name.
604 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
605 name + prefix_len, prefix);
606 ret = snd_ctl_add(card, kcontrol);
607 if (ret < 0) {
608 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
609 w->name, ret);
610 kfree(wlist);
611 return ret;
615 kcontrol->private_data = wlist;
617 w->kcontrols[0] = kcontrol;
619 list_for_each_entry(path, &w->sources, list_sink)
620 path->kcontrol = kcontrol;
622 return 0;
625 /* create new dapm volume control */
626 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
628 if (w->num_kcontrols)
629 dev_err(w->dapm->dev,
630 "asoc: PGA controls not supported: '%s'\n", w->name);
632 return 0;
635 /* reset 'walked' bit for each dapm path */
636 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
638 struct snd_soc_dapm_path *p;
640 list_for_each_entry(p, &dapm->card->paths, list)
641 p->walked = 0;
644 /* We implement power down on suspend by checking the power state of
645 * the ALSA card - when we are suspending the ALSA state for the card
646 * is set to D3.
648 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
650 int level = snd_power_get_state(widget->dapm->card->snd_card);
652 switch (level) {
653 case SNDRV_CTL_POWER_D3hot:
654 case SNDRV_CTL_POWER_D3cold:
655 if (widget->ignore_suspend)
656 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
657 widget->name);
658 return widget->ignore_suspend;
659 default:
660 return 1;
665 * Recursively check for a completed path to an active or physically connected
666 * output widget. Returns number of complete paths.
668 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
670 struct snd_soc_dapm_path *path;
671 int con = 0;
673 if (widget->outputs >= 0)
674 return widget->outputs;
676 DAPM_UPDATE_STAT(widget, path_checks);
678 if (widget->id == snd_soc_dapm_supply)
679 return 0;
681 switch (widget->id) {
682 case snd_soc_dapm_adc:
683 case snd_soc_dapm_aif_out:
684 if (widget->active) {
685 widget->outputs = snd_soc_dapm_suspend_check(widget);
686 return widget->outputs;
688 default:
689 break;
692 if (widget->connected) {
693 /* connected pin ? */
694 if (widget->id == snd_soc_dapm_output && !widget->ext) {
695 widget->outputs = snd_soc_dapm_suspend_check(widget);
696 return widget->outputs;
699 /* connected jack or spk ? */
700 if (widget->id == snd_soc_dapm_hp ||
701 widget->id == snd_soc_dapm_spk ||
702 (widget->id == snd_soc_dapm_line &&
703 !list_empty(&widget->sources))) {
704 widget->outputs = snd_soc_dapm_suspend_check(widget);
705 return widget->outputs;
709 list_for_each_entry(path, &widget->sinks, list_source) {
710 DAPM_UPDATE_STAT(widget, neighbour_checks);
712 if (path->weak)
713 continue;
715 if (path->walked)
716 continue;
718 if (path->sink && path->connect) {
719 path->walked = 1;
720 con += is_connected_output_ep(path->sink);
724 widget->outputs = con;
726 return con;
730 * Recursively check for a completed path to an active or physically connected
731 * input widget. Returns number of complete paths.
733 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
735 struct snd_soc_dapm_path *path;
736 int con = 0;
738 if (widget->inputs >= 0)
739 return widget->inputs;
741 DAPM_UPDATE_STAT(widget, path_checks);
743 if (widget->id == snd_soc_dapm_supply)
744 return 0;
746 /* active stream ? */
747 switch (widget->id) {
748 case snd_soc_dapm_dac:
749 case snd_soc_dapm_aif_in:
750 if (widget->active) {
751 widget->inputs = snd_soc_dapm_suspend_check(widget);
752 return widget->inputs;
754 default:
755 break;
758 if (widget->connected) {
759 /* connected pin ? */
760 if (widget->id == snd_soc_dapm_input && !widget->ext) {
761 widget->inputs = snd_soc_dapm_suspend_check(widget);
762 return widget->inputs;
765 /* connected VMID/Bias for lower pops */
766 if (widget->id == snd_soc_dapm_vmid) {
767 widget->inputs = snd_soc_dapm_suspend_check(widget);
768 return widget->inputs;
771 /* connected jack ? */
772 if (widget->id == snd_soc_dapm_mic ||
773 (widget->id == snd_soc_dapm_line &&
774 !list_empty(&widget->sinks))) {
775 widget->inputs = snd_soc_dapm_suspend_check(widget);
776 return widget->inputs;
779 /* signal generator */
780 if (widget->id == snd_soc_dapm_siggen) {
781 widget->inputs = snd_soc_dapm_suspend_check(widget);
782 return widget->inputs;
786 list_for_each_entry(path, &widget->sources, list_sink) {
787 DAPM_UPDATE_STAT(widget, neighbour_checks);
789 if (path->weak)
790 continue;
792 if (path->walked)
793 continue;
795 if (path->source && path->connect) {
796 path->walked = 1;
797 con += is_connected_input_ep(path->source);
801 widget->inputs = con;
803 return con;
807 * Handler for generic register modifier widget.
809 int dapm_reg_event(struct snd_soc_dapm_widget *w,
810 struct snd_kcontrol *kcontrol, int event)
812 unsigned int val;
814 if (SND_SOC_DAPM_EVENT_ON(event))
815 val = w->on_val;
816 else
817 val = w->off_val;
819 soc_widget_update_bits(w, -(w->reg + 1),
820 w->mask << w->shift, val << w->shift);
822 return 0;
824 EXPORT_SYMBOL_GPL(dapm_reg_event);
826 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
828 if (w->power_checked)
829 return w->new_power;
831 if (w->force)
832 w->new_power = 1;
833 else
834 w->new_power = w->power_check(w);
836 w->power_checked = true;
838 return w->new_power;
841 /* Generic check to see if a widget should be powered.
843 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
845 int in, out;
847 DAPM_UPDATE_STAT(w, power_checks);
849 in = is_connected_input_ep(w);
850 dapm_clear_walk(w->dapm);
851 out = is_connected_output_ep(w);
852 dapm_clear_walk(w->dapm);
853 return out != 0 && in != 0;
856 /* Check to see if an ADC has power */
857 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
859 int in;
861 DAPM_UPDATE_STAT(w, power_checks);
863 if (w->active) {
864 in = is_connected_input_ep(w);
865 dapm_clear_walk(w->dapm);
866 return in != 0;
867 } else {
868 return dapm_generic_check_power(w);
872 /* Check to see if a DAC has power */
873 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
875 int out;
877 DAPM_UPDATE_STAT(w, power_checks);
879 if (w->active) {
880 out = is_connected_output_ep(w);
881 dapm_clear_walk(w->dapm);
882 return out != 0;
883 } else {
884 return dapm_generic_check_power(w);
888 /* Check to see if a power supply is needed */
889 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
891 struct snd_soc_dapm_path *path;
893 DAPM_UPDATE_STAT(w, power_checks);
895 /* Check if one of our outputs is connected */
896 list_for_each_entry(path, &w->sinks, list_source) {
897 DAPM_UPDATE_STAT(w, neighbour_checks);
899 if (path->weak)
900 continue;
902 if (path->connected &&
903 !path->connected(path->source, path->sink))
904 continue;
906 if (!path->sink)
907 continue;
909 if (dapm_widget_power_check(path->sink))
910 return 1;
913 dapm_clear_walk(w->dapm);
915 return 0;
918 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
920 return 1;
923 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
924 struct snd_soc_dapm_widget *b,
925 bool power_up)
927 int *sort;
929 if (power_up)
930 sort = dapm_up_seq;
931 else
932 sort = dapm_down_seq;
934 if (sort[a->id] != sort[b->id])
935 return sort[a->id] - sort[b->id];
936 if (a->subseq != b->subseq) {
937 if (power_up)
938 return a->subseq - b->subseq;
939 else
940 return b->subseq - a->subseq;
942 if (a->reg != b->reg)
943 return a->reg - b->reg;
944 if (a->dapm != b->dapm)
945 return (unsigned long)a->dapm - (unsigned long)b->dapm;
947 return 0;
950 /* Insert a widget in order into a DAPM power sequence. */
951 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
952 struct list_head *list,
953 bool power_up)
955 struct snd_soc_dapm_widget *w;
957 list_for_each_entry(w, list, power_list)
958 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
959 list_add_tail(&new_widget->power_list, &w->power_list);
960 return;
963 list_add_tail(&new_widget->power_list, list);
966 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
967 struct snd_soc_dapm_widget *w, int event)
969 struct snd_soc_card *card = dapm->card;
970 const char *ev_name;
971 int power, ret;
973 switch (event) {
974 case SND_SOC_DAPM_PRE_PMU:
975 ev_name = "PRE_PMU";
976 power = 1;
977 break;
978 case SND_SOC_DAPM_POST_PMU:
979 ev_name = "POST_PMU";
980 power = 1;
981 break;
982 case SND_SOC_DAPM_PRE_PMD:
983 ev_name = "PRE_PMD";
984 power = 0;
985 break;
986 case SND_SOC_DAPM_POST_PMD:
987 ev_name = "POST_PMD";
988 power = 0;
989 break;
990 default:
991 BUG();
992 return;
995 if (w->power != power)
996 return;
998 if (w->event && (w->event_flags & event)) {
999 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1000 w->name, ev_name);
1001 trace_snd_soc_dapm_widget_event_start(w, event);
1002 ret = w->event(w, NULL, event);
1003 trace_snd_soc_dapm_widget_event_done(w, event);
1004 if (ret < 0)
1005 pr_err("%s: %s event failed: %d\n",
1006 ev_name, w->name, ret);
1010 /* Apply the coalesced changes from a DAPM sequence */
1011 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
1012 struct list_head *pending)
1014 struct snd_soc_card *card = dapm->card;
1015 struct snd_soc_dapm_widget *w;
1016 int reg, power;
1017 unsigned int value = 0;
1018 unsigned int mask = 0;
1019 unsigned int cur_mask;
1021 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1022 power_list)->reg;
1024 list_for_each_entry(w, pending, power_list) {
1025 cur_mask = 1 << w->shift;
1026 BUG_ON(reg != w->reg);
1028 if (w->invert)
1029 power = !w->power;
1030 else
1031 power = w->power;
1033 mask |= cur_mask;
1034 if (power)
1035 value |= cur_mask;
1037 pop_dbg(dapm->dev, card->pop_time,
1038 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1039 w->name, reg, value, mask);
1041 /* Check for events */
1042 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1043 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1046 if (reg >= 0) {
1047 /* Any widget will do, they should all be updating the
1048 * same register.
1050 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1051 power_list);
1053 pop_dbg(dapm->dev, card->pop_time,
1054 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1055 value, mask, reg, card->pop_time);
1056 pop_wait(card->pop_time);
1057 soc_widget_update_bits(w, reg, mask, value);
1060 list_for_each_entry(w, pending, power_list) {
1061 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1062 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1066 /* Apply a DAPM power sequence.
1068 * We walk over a pre-sorted list of widgets to apply power to. In
1069 * order to minimise the number of writes to the device required
1070 * multiple widgets will be updated in a single write where possible.
1071 * Currently anything that requires more than a single write is not
1072 * handled.
1074 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1075 struct list_head *list, int event, bool power_up)
1077 struct snd_soc_dapm_widget *w, *n;
1078 LIST_HEAD(pending);
1079 int cur_sort = -1;
1080 int cur_subseq = -1;
1081 int cur_reg = SND_SOC_NOPM;
1082 struct snd_soc_dapm_context *cur_dapm = NULL;
1083 int ret, i;
1084 int *sort;
1086 if (power_up)
1087 sort = dapm_up_seq;
1088 else
1089 sort = dapm_down_seq;
1091 list_for_each_entry_safe(w, n, list, power_list) {
1092 ret = 0;
1094 /* Do we need to apply any queued changes? */
1095 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1096 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1097 if (!list_empty(&pending))
1098 dapm_seq_run_coalesced(cur_dapm, &pending);
1100 if (cur_dapm && cur_dapm->seq_notifier) {
1101 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1102 if (sort[i] == cur_sort)
1103 cur_dapm->seq_notifier(cur_dapm,
1105 cur_subseq);
1108 INIT_LIST_HEAD(&pending);
1109 cur_sort = -1;
1110 cur_subseq = INT_MIN;
1111 cur_reg = SND_SOC_NOPM;
1112 cur_dapm = NULL;
1115 switch (w->id) {
1116 case snd_soc_dapm_pre:
1117 if (!w->event)
1118 list_for_each_entry_safe_continue(w, n, list,
1119 power_list);
1121 if (event == SND_SOC_DAPM_STREAM_START)
1122 ret = w->event(w,
1123 NULL, SND_SOC_DAPM_PRE_PMU);
1124 else if (event == SND_SOC_DAPM_STREAM_STOP)
1125 ret = w->event(w,
1126 NULL, SND_SOC_DAPM_PRE_PMD);
1127 break;
1129 case snd_soc_dapm_post:
1130 if (!w->event)
1131 list_for_each_entry_safe_continue(w, n, list,
1132 power_list);
1134 if (event == SND_SOC_DAPM_STREAM_START)
1135 ret = w->event(w,
1136 NULL, SND_SOC_DAPM_POST_PMU);
1137 else if (event == SND_SOC_DAPM_STREAM_STOP)
1138 ret = w->event(w,
1139 NULL, SND_SOC_DAPM_POST_PMD);
1140 break;
1142 default:
1143 /* Queue it up for application */
1144 cur_sort = sort[w->id];
1145 cur_subseq = w->subseq;
1146 cur_reg = w->reg;
1147 cur_dapm = w->dapm;
1148 list_move(&w->power_list, &pending);
1149 break;
1152 if (ret < 0)
1153 dev_err(w->dapm->dev,
1154 "Failed to apply widget power: %d\n", ret);
1157 if (!list_empty(&pending))
1158 dapm_seq_run_coalesced(cur_dapm, &pending);
1160 if (cur_dapm && cur_dapm->seq_notifier) {
1161 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1162 if (sort[i] == cur_sort)
1163 cur_dapm->seq_notifier(cur_dapm,
1164 i, cur_subseq);
1168 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1170 struct snd_soc_dapm_update *update = dapm->update;
1171 struct snd_soc_dapm_widget *w;
1172 int ret;
1174 if (!update)
1175 return;
1177 w = update->widget;
1179 if (w->event &&
1180 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1181 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1182 if (ret != 0)
1183 pr_err("%s DAPM pre-event failed: %d\n",
1184 w->name, ret);
1187 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1188 update->val);
1189 if (ret < 0)
1190 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1192 if (w->event &&
1193 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1194 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1195 if (ret != 0)
1196 pr_err("%s DAPM post-event failed: %d\n",
1197 w->name, ret);
1201 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1202 * they're changing state.
1204 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1206 struct snd_soc_dapm_context *d = data;
1207 int ret;
1209 /* If we're off and we're not supposed to be go into STANDBY */
1210 if (d->bias_level == SND_SOC_BIAS_OFF &&
1211 d->target_bias_level != SND_SOC_BIAS_OFF) {
1212 if (d->dev)
1213 pm_runtime_get_sync(d->dev);
1215 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1216 if (ret != 0)
1217 dev_err(d->dev,
1218 "Failed to turn on bias: %d\n", ret);
1221 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1222 if (d->bias_level != d->target_bias_level) {
1223 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1224 if (ret != 0)
1225 dev_err(d->dev,
1226 "Failed to prepare bias: %d\n", ret);
1230 /* Async callback run prior to DAPM sequences - brings to their final
1231 * state.
1233 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1235 struct snd_soc_dapm_context *d = data;
1236 int ret;
1238 /* If we just powered the last thing off drop to standby bias */
1239 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1240 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1241 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1242 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1243 if (ret != 0)
1244 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1245 ret);
1248 /* If we're in standby and can support bias off then do that */
1249 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1250 d->target_bias_level == SND_SOC_BIAS_OFF) {
1251 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1252 if (ret != 0)
1253 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1255 if (d->dev)
1256 pm_runtime_put_sync(d->dev);
1259 /* If we just powered up then move to active bias */
1260 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1261 d->target_bias_level == SND_SOC_BIAS_ON) {
1262 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1263 if (ret != 0)
1264 dev_err(d->dev, "Failed to apply active bias: %d\n",
1265 ret);
1269 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1270 bool power, bool connect)
1272 /* If a connection is being made or broken then that update
1273 * will have marked the peer dirty, otherwise the widgets are
1274 * not connected and this update has no impact. */
1275 if (!connect)
1276 return;
1278 /* If the peer is already in the state we're moving to then we
1279 * won't have an impact on it. */
1280 if (power != peer->power)
1281 dapm_mark_dirty(peer, "peer state change");
1284 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1285 struct list_head *up_list,
1286 struct list_head *down_list)
1288 struct snd_soc_dapm_path *path;
1290 if (w->power == power)
1291 return;
1293 trace_snd_soc_dapm_widget_power(w, power);
1295 /* If we changed our power state perhaps our neigbours changed
1296 * also.
1298 list_for_each_entry(path, &w->sources, list_sink) {
1299 if (path->source) {
1300 dapm_widget_set_peer_power(path->source, power,
1301 path->connect);
1304 switch (w->id) {
1305 case snd_soc_dapm_supply:
1306 /* Supplies can't affect their outputs, only their inputs */
1307 break;
1308 default:
1309 list_for_each_entry(path, &w->sinks, list_source) {
1310 if (path->sink) {
1311 dapm_widget_set_peer_power(path->sink, power,
1312 path->connect);
1315 break;
1318 if (power)
1319 dapm_seq_insert(w, up_list, true);
1320 else
1321 dapm_seq_insert(w, down_list, false);
1323 w->power = power;
1326 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1327 struct list_head *up_list,
1328 struct list_head *down_list)
1330 int power;
1332 switch (w->id) {
1333 case snd_soc_dapm_pre:
1334 dapm_seq_insert(w, down_list, false);
1335 break;
1336 case snd_soc_dapm_post:
1337 dapm_seq_insert(w, up_list, true);
1338 break;
1340 default:
1341 power = dapm_widget_power_check(w);
1343 dapm_widget_set_power(w, power, up_list, down_list);
1344 break;
1349 * Scan each dapm widget for complete audio path.
1350 * A complete path is a route that has valid endpoints i.e.:-
1352 * o DAC to output pin.
1353 * o Input Pin to ADC.
1354 * o Input pin to Output pin (bypass, sidetone)
1355 * o DAC to ADC (loopback).
1357 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1359 struct snd_soc_card *card = dapm->card;
1360 struct snd_soc_dapm_widget *w;
1361 struct snd_soc_dapm_context *d;
1362 LIST_HEAD(up_list);
1363 LIST_HEAD(down_list);
1364 LIST_HEAD(async_domain);
1365 enum snd_soc_bias_level bias;
1367 trace_snd_soc_dapm_start(card);
1369 list_for_each_entry(d, &card->dapm_list, list) {
1370 if (d->n_widgets || d->codec == NULL) {
1371 if (d->idle_bias_off)
1372 d->target_bias_level = SND_SOC_BIAS_OFF;
1373 else
1374 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1378 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
1380 list_for_each_entry(w, &card->widgets, list) {
1381 w->power_checked = false;
1382 w->inputs = -1;
1383 w->outputs = -1;
1386 /* Check which widgets we need to power and store them in
1387 * lists indicating if they should be powered up or down. We
1388 * only check widgets that have been flagged as dirty but note
1389 * that new widgets may be added to the dirty list while we
1390 * iterate.
1392 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1393 dapm_power_one_widget(w, &up_list, &down_list);
1396 list_for_each_entry(w, &card->widgets, list) {
1397 list_del_init(&w->dirty);
1399 if (w->power) {
1400 d = w->dapm;
1402 /* Supplies and micbiases only bring the
1403 * context up to STANDBY as unless something
1404 * else is active and passing audio they
1405 * generally don't require full power.
1407 switch (w->id) {
1408 case snd_soc_dapm_supply:
1409 case snd_soc_dapm_micbias:
1410 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1411 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1412 break;
1413 default:
1414 d->target_bias_level = SND_SOC_BIAS_ON;
1415 break;
1421 /* If there are no DAPM widgets then try to figure out power from the
1422 * event type.
1424 if (!dapm->n_widgets) {
1425 switch (event) {
1426 case SND_SOC_DAPM_STREAM_START:
1427 case SND_SOC_DAPM_STREAM_RESUME:
1428 dapm->target_bias_level = SND_SOC_BIAS_ON;
1429 break;
1430 case SND_SOC_DAPM_STREAM_STOP:
1431 if (dapm->codec && dapm->codec->active)
1432 dapm->target_bias_level = SND_SOC_BIAS_ON;
1433 else
1434 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1435 break;
1436 case SND_SOC_DAPM_STREAM_SUSPEND:
1437 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1438 break;
1439 case SND_SOC_DAPM_STREAM_NOP:
1440 dapm->target_bias_level = dapm->bias_level;
1441 break;
1442 default:
1443 break;
1447 /* Force all contexts in the card to the same bias state if
1448 * they're not ground referenced.
1450 bias = SND_SOC_BIAS_OFF;
1451 list_for_each_entry(d, &card->dapm_list, list)
1452 if (d->target_bias_level > bias)
1453 bias = d->target_bias_level;
1454 list_for_each_entry(d, &card->dapm_list, list)
1455 if (!d->idle_bias_off)
1456 d->target_bias_level = bias;
1458 trace_snd_soc_dapm_walk_done(card);
1460 /* Run all the bias changes in parallel */
1461 list_for_each_entry(d, &dapm->card->dapm_list, list)
1462 async_schedule_domain(dapm_pre_sequence_async, d,
1463 &async_domain);
1464 async_synchronize_full_domain(&async_domain);
1466 /* Power down widgets first; try to avoid amplifying pops. */
1467 dapm_seq_run(dapm, &down_list, event, false);
1469 dapm_widget_update(dapm);
1471 /* Now power up. */
1472 dapm_seq_run(dapm, &up_list, event, true);
1474 /* Run all the bias changes in parallel */
1475 list_for_each_entry(d, &dapm->card->dapm_list, list)
1476 async_schedule_domain(dapm_post_sequence_async, d,
1477 &async_domain);
1478 async_synchronize_full_domain(&async_domain);
1480 pop_dbg(dapm->dev, card->pop_time,
1481 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1482 pop_wait(card->pop_time);
1484 trace_snd_soc_dapm_done(card);
1486 return 0;
1489 #ifdef CONFIG_DEBUG_FS
1490 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1492 file->private_data = inode->i_private;
1493 return 0;
1496 static ssize_t dapm_widget_power_read_file(struct file *file,
1497 char __user *user_buf,
1498 size_t count, loff_t *ppos)
1500 struct snd_soc_dapm_widget *w = file->private_data;
1501 char *buf;
1502 int in, out;
1503 ssize_t ret;
1504 struct snd_soc_dapm_path *p = NULL;
1506 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1507 if (!buf)
1508 return -ENOMEM;
1510 in = is_connected_input_ep(w);
1511 dapm_clear_walk(w->dapm);
1512 out = is_connected_output_ep(w);
1513 dapm_clear_walk(w->dapm);
1515 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1516 w->name, w->power ? "On" : "Off", in, out);
1518 if (w->reg >= 0)
1519 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1520 " - R%d(0x%x) bit %d",
1521 w->reg, w->reg, w->shift);
1523 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1525 if (w->sname)
1526 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1527 w->sname,
1528 w->active ? "active" : "inactive");
1530 list_for_each_entry(p, &w->sources, list_sink) {
1531 if (p->connected && !p->connected(w, p->sink))
1532 continue;
1534 if (p->connect)
1535 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1536 " in \"%s\" \"%s\"\n",
1537 p->name ? p->name : "static",
1538 p->source->name);
1540 list_for_each_entry(p, &w->sinks, list_source) {
1541 if (p->connected && !p->connected(w, p->sink))
1542 continue;
1544 if (p->connect)
1545 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1546 " out \"%s\" \"%s\"\n",
1547 p->name ? p->name : "static",
1548 p->sink->name);
1551 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1553 kfree(buf);
1554 return ret;
1557 static const struct file_operations dapm_widget_power_fops = {
1558 .open = dapm_widget_power_open_file,
1559 .read = dapm_widget_power_read_file,
1560 .llseek = default_llseek,
1563 static int dapm_bias_open_file(struct inode *inode, struct file *file)
1565 file->private_data = inode->i_private;
1566 return 0;
1569 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1570 size_t count, loff_t *ppos)
1572 struct snd_soc_dapm_context *dapm = file->private_data;
1573 char *level;
1575 switch (dapm->bias_level) {
1576 case SND_SOC_BIAS_ON:
1577 level = "On\n";
1578 break;
1579 case SND_SOC_BIAS_PREPARE:
1580 level = "Prepare\n";
1581 break;
1582 case SND_SOC_BIAS_STANDBY:
1583 level = "Standby\n";
1584 break;
1585 case SND_SOC_BIAS_OFF:
1586 level = "Off\n";
1587 break;
1588 default:
1589 BUG();
1590 level = "Unknown\n";
1591 break;
1594 return simple_read_from_buffer(user_buf, count, ppos, level,
1595 strlen(level));
1598 static const struct file_operations dapm_bias_fops = {
1599 .open = dapm_bias_open_file,
1600 .read = dapm_bias_read_file,
1601 .llseek = default_llseek,
1604 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1605 struct dentry *parent)
1607 struct dentry *d;
1609 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1611 if (!dapm->debugfs_dapm) {
1612 printk(KERN_WARNING
1613 "Failed to create DAPM debugfs directory\n");
1614 return;
1617 d = debugfs_create_file("bias_level", 0444,
1618 dapm->debugfs_dapm, dapm,
1619 &dapm_bias_fops);
1620 if (!d)
1621 dev_warn(dapm->dev,
1622 "ASoC: Failed to create bias level debugfs file\n");
1625 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1627 struct snd_soc_dapm_context *dapm = w->dapm;
1628 struct dentry *d;
1630 if (!dapm->debugfs_dapm || !w->name)
1631 return;
1633 d = debugfs_create_file(w->name, 0444,
1634 dapm->debugfs_dapm, w,
1635 &dapm_widget_power_fops);
1636 if (!d)
1637 dev_warn(w->dapm->dev,
1638 "ASoC: Failed to create %s debugfs file\n",
1639 w->name);
1642 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1644 debugfs_remove_recursive(dapm->debugfs_dapm);
1647 #else
1648 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1649 struct dentry *parent)
1653 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1657 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1661 #endif
1663 /* test and update the power status of a mux widget */
1664 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1665 struct snd_kcontrol *kcontrol, int change,
1666 int mux, struct soc_enum *e)
1668 struct snd_soc_dapm_path *path;
1669 int found = 0;
1671 if (widget->id != snd_soc_dapm_mux &&
1672 widget->id != snd_soc_dapm_virt_mux &&
1673 widget->id != snd_soc_dapm_value_mux)
1674 return -ENODEV;
1676 if (!change)
1677 return 0;
1679 /* find dapm widget path assoc with kcontrol */
1680 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1681 if (path->kcontrol != kcontrol)
1682 continue;
1684 if (!path->name || !e->texts[mux])
1685 continue;
1687 found = 1;
1688 /* we now need to match the string in the enum to the path */
1689 if (!(strcmp(path->name, e->texts[mux]))) {
1690 path->connect = 1; /* new connection */
1691 dapm_mark_dirty(path->source, "mux connection");
1692 } else {
1693 if (path->connect)
1694 dapm_mark_dirty(path->source,
1695 "mux disconnection");
1696 path->connect = 0; /* old connection must be powered down */
1700 if (found) {
1701 dapm_mark_dirty(widget, "mux change");
1702 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1705 return 0;
1708 /* test and update the power status of a mixer or switch widget */
1709 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1710 struct snd_kcontrol *kcontrol, int connect)
1712 struct snd_soc_dapm_path *path;
1713 int found = 0;
1715 if (widget->id != snd_soc_dapm_mixer &&
1716 widget->id != snd_soc_dapm_mixer_named_ctl &&
1717 widget->id != snd_soc_dapm_switch)
1718 return -ENODEV;
1720 /* find dapm widget path assoc with kcontrol */
1721 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1722 if (path->kcontrol != kcontrol)
1723 continue;
1725 /* found, now check type */
1726 found = 1;
1727 path->connect = connect;
1728 dapm_mark_dirty(path->source, "mixer connection");
1731 if (found) {
1732 dapm_mark_dirty(widget, "mixer update");
1733 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1736 return 0;
1739 /* show dapm widget status in sys fs */
1740 static ssize_t dapm_widget_show(struct device *dev,
1741 struct device_attribute *attr, char *buf)
1743 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
1744 struct snd_soc_codec *codec =rtd->codec;
1745 struct snd_soc_dapm_widget *w;
1746 int count = 0;
1747 char *state = "not set";
1749 list_for_each_entry(w, &codec->card->widgets, list) {
1750 if (w->dapm != &codec->dapm)
1751 continue;
1753 /* only display widgets that burnm power */
1754 switch (w->id) {
1755 case snd_soc_dapm_hp:
1756 case snd_soc_dapm_mic:
1757 case snd_soc_dapm_spk:
1758 case snd_soc_dapm_line:
1759 case snd_soc_dapm_micbias:
1760 case snd_soc_dapm_dac:
1761 case snd_soc_dapm_adc:
1762 case snd_soc_dapm_pga:
1763 case snd_soc_dapm_out_drv:
1764 case snd_soc_dapm_mixer:
1765 case snd_soc_dapm_mixer_named_ctl:
1766 case snd_soc_dapm_supply:
1767 if (w->name)
1768 count += sprintf(buf + count, "%s: %s\n",
1769 w->name, w->power ? "On":"Off");
1770 break;
1771 default:
1772 break;
1776 switch (codec->dapm.bias_level) {
1777 case SND_SOC_BIAS_ON:
1778 state = "On";
1779 break;
1780 case SND_SOC_BIAS_PREPARE:
1781 state = "Prepare";
1782 break;
1783 case SND_SOC_BIAS_STANDBY:
1784 state = "Standby";
1785 break;
1786 case SND_SOC_BIAS_OFF:
1787 state = "Off";
1788 break;
1790 count += sprintf(buf + count, "PM State: %s\n", state);
1792 return count;
1795 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1797 int snd_soc_dapm_sys_add(struct device *dev)
1799 return device_create_file(dev, &dev_attr_dapm_widget);
1802 static void snd_soc_dapm_sys_remove(struct device *dev)
1804 device_remove_file(dev, &dev_attr_dapm_widget);
1807 /* free all dapm widgets and resources */
1808 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1810 struct snd_soc_dapm_widget *w, *next_w;
1811 struct snd_soc_dapm_path *p, *next_p;
1813 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1814 if (w->dapm != dapm)
1815 continue;
1816 list_del(&w->list);
1818 * remove source and sink paths associated to this widget.
1819 * While removing the path, remove reference to it from both
1820 * source and sink widgets so that path is removed only once.
1822 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1823 list_del(&p->list_sink);
1824 list_del(&p->list_source);
1825 list_del(&p->list);
1826 kfree(p->long_name);
1827 kfree(p);
1829 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1830 list_del(&p->list_sink);
1831 list_del(&p->list_source);
1832 list_del(&p->list);
1833 kfree(p->long_name);
1834 kfree(p);
1836 kfree(w->kcontrols);
1837 kfree(w->name);
1838 kfree(w);
1842 static struct snd_soc_dapm_widget *dapm_find_widget(
1843 struct snd_soc_dapm_context *dapm, const char *pin,
1844 bool search_other_contexts)
1846 struct snd_soc_dapm_widget *w;
1847 struct snd_soc_dapm_widget *fallback = NULL;
1849 list_for_each_entry(w, &dapm->card->widgets, list) {
1850 if (!strcmp(w->name, pin)) {
1851 if (w->dapm == dapm)
1852 return w;
1853 else
1854 fallback = w;
1858 if (search_other_contexts)
1859 return fallback;
1861 return NULL;
1864 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1865 const char *pin, int status)
1867 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
1869 if (!w) {
1870 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1871 return -EINVAL;
1874 w->connected = status;
1875 if (status == 0)
1876 w->force = 0;
1877 dapm_mark_dirty(w, "pin configuration");
1879 return 0;
1883 * snd_soc_dapm_sync - scan and power dapm paths
1884 * @dapm: DAPM context
1886 * Walks all dapm audio paths and powers widgets according to their
1887 * stream or path usage.
1889 * Returns 0 for success.
1891 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1894 * Suppress early reports (eg, jacks syncing their state) to avoid
1895 * silly DAPM runs during card startup.
1897 if (!dapm->card || !dapm->card->instantiated)
1898 return 0;
1900 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1902 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1904 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1905 const struct snd_soc_dapm_route *route)
1907 struct snd_soc_dapm_path *path;
1908 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1909 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1910 const char *sink;
1911 const char *control = route->control;
1912 const char *source;
1913 char prefixed_sink[80];
1914 char prefixed_source[80];
1915 int ret = 0;
1917 if (dapm->codec && dapm->codec->name_prefix) {
1918 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1919 dapm->codec->name_prefix, route->sink);
1920 sink = prefixed_sink;
1921 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1922 dapm->codec->name_prefix, route->source);
1923 source = prefixed_source;
1924 } else {
1925 sink = route->sink;
1926 source = route->source;
1930 * find src and dest widgets over all widgets but favor a widget from
1931 * current DAPM context
1933 list_for_each_entry(w, &dapm->card->widgets, list) {
1934 if (!wsink && !(strcmp(w->name, sink))) {
1935 wtsink = w;
1936 if (w->dapm == dapm)
1937 wsink = w;
1938 continue;
1940 if (!wsource && !(strcmp(w->name, source))) {
1941 wtsource = w;
1942 if (w->dapm == dapm)
1943 wsource = w;
1946 /* use widget from another DAPM context if not found from this */
1947 if (!wsink)
1948 wsink = wtsink;
1949 if (!wsource)
1950 wsource = wtsource;
1952 if (wsource == NULL || wsink == NULL)
1953 return -ENODEV;
1955 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1956 if (!path)
1957 return -ENOMEM;
1959 path->source = wsource;
1960 path->sink = wsink;
1961 path->connected = route->connected;
1962 INIT_LIST_HEAD(&path->list);
1963 INIT_LIST_HEAD(&path->list_source);
1964 INIT_LIST_HEAD(&path->list_sink);
1966 /* check for external widgets */
1967 if (wsink->id == snd_soc_dapm_input) {
1968 if (wsource->id == snd_soc_dapm_micbias ||
1969 wsource->id == snd_soc_dapm_mic ||
1970 wsource->id == snd_soc_dapm_line ||
1971 wsource->id == snd_soc_dapm_output)
1972 wsink->ext = 1;
1974 if (wsource->id == snd_soc_dapm_output) {
1975 if (wsink->id == snd_soc_dapm_spk ||
1976 wsink->id == snd_soc_dapm_hp ||
1977 wsink->id == snd_soc_dapm_line ||
1978 wsink->id == snd_soc_dapm_input)
1979 wsource->ext = 1;
1982 /* connect static paths */
1983 if (control == NULL) {
1984 list_add(&path->list, &dapm->card->paths);
1985 list_add(&path->list_sink, &wsink->sources);
1986 list_add(&path->list_source, &wsource->sinks);
1987 path->connect = 1;
1988 return 0;
1991 /* connect dynamic paths */
1992 switch (wsink->id) {
1993 case snd_soc_dapm_adc:
1994 case snd_soc_dapm_dac:
1995 case snd_soc_dapm_pga:
1996 case snd_soc_dapm_out_drv:
1997 case snd_soc_dapm_input:
1998 case snd_soc_dapm_output:
1999 case snd_soc_dapm_siggen:
2000 case snd_soc_dapm_micbias:
2001 case snd_soc_dapm_vmid:
2002 case snd_soc_dapm_pre:
2003 case snd_soc_dapm_post:
2004 case snd_soc_dapm_supply:
2005 case snd_soc_dapm_aif_in:
2006 case snd_soc_dapm_aif_out:
2007 list_add(&path->list, &dapm->card->paths);
2008 list_add(&path->list_sink, &wsink->sources);
2009 list_add(&path->list_source, &wsource->sinks);
2010 path->connect = 1;
2011 return 0;
2012 case snd_soc_dapm_mux:
2013 case snd_soc_dapm_virt_mux:
2014 case snd_soc_dapm_value_mux:
2015 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2016 &wsink->kcontrol_news[0]);
2017 if (ret != 0)
2018 goto err;
2019 break;
2020 case snd_soc_dapm_switch:
2021 case snd_soc_dapm_mixer:
2022 case snd_soc_dapm_mixer_named_ctl:
2023 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2024 if (ret != 0)
2025 goto err;
2026 break;
2027 case snd_soc_dapm_hp:
2028 case snd_soc_dapm_mic:
2029 case snd_soc_dapm_line:
2030 case snd_soc_dapm_spk:
2031 list_add(&path->list, &dapm->card->paths);
2032 list_add(&path->list_sink, &wsink->sources);
2033 list_add(&path->list_source, &wsource->sinks);
2034 path->connect = 0;
2035 return 0;
2037 return 0;
2039 err:
2040 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2041 source, control, sink);
2042 kfree(path);
2043 return ret;
2047 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2048 * @dapm: DAPM context
2049 * @route: audio routes
2050 * @num: number of routes
2052 * Connects 2 dapm widgets together via a named audio path. The sink is
2053 * the widget receiving the audio signal, whilst the source is the sender
2054 * of the audio signal.
2056 * Returns 0 for success else error. On error all resources can be freed
2057 * with a call to snd_soc_card_free().
2059 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2060 const struct snd_soc_dapm_route *route, int num)
2062 int i, ret;
2064 for (i = 0; i < num; i++) {
2065 ret = snd_soc_dapm_add_route(dapm, route);
2066 if (ret < 0) {
2067 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2068 route->source, route->sink);
2069 return ret;
2071 route++;
2074 return 0;
2076 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2078 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2079 const struct snd_soc_dapm_route *route)
2081 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2082 route->source,
2083 true);
2084 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2085 route->sink,
2086 true);
2087 struct snd_soc_dapm_path *path;
2088 int count = 0;
2090 if (!source) {
2091 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2092 route->source);
2093 return -ENODEV;
2096 if (!sink) {
2097 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2098 route->sink);
2099 return -ENODEV;
2102 if (route->control || route->connected)
2103 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2104 route->source, route->sink);
2106 list_for_each_entry(path, &source->sinks, list_source) {
2107 if (path->sink == sink) {
2108 path->weak = 1;
2109 count++;
2113 if (count == 0)
2114 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2115 route->source, route->sink);
2116 if (count > 1)
2117 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2118 count, route->source, route->sink);
2120 return 0;
2124 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2125 * @dapm: DAPM context
2126 * @route: audio routes
2127 * @num: number of routes
2129 * Mark existing routes matching those specified in the passed array
2130 * as being weak, meaning that they are ignored for the purpose of
2131 * power decisions. The main intended use case is for sidetone paths
2132 * which couple audio between other independent paths if they are both
2133 * active in order to make the combination work better at the user
2134 * level but which aren't intended to be "used".
2136 * Note that CODEC drivers should not use this as sidetone type paths
2137 * can frequently also be used as bypass paths.
2139 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2140 const struct snd_soc_dapm_route *route, int num)
2142 int i, err;
2143 int ret = 0;
2145 for (i = 0; i < num; i++) {
2146 err = snd_soc_dapm_weak_route(dapm, route);
2147 if (err)
2148 ret = err;
2149 route++;
2152 return ret;
2154 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2157 * snd_soc_dapm_new_widgets - add new dapm widgets
2158 * @dapm: DAPM context
2160 * Checks the codec for any new dapm widgets and creates them if found.
2162 * Returns 0 for success.
2164 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2166 struct snd_soc_dapm_widget *w;
2167 unsigned int val;
2169 list_for_each_entry(w, &dapm->card->widgets, list)
2171 if (w->new)
2172 continue;
2174 if (w->num_kcontrols) {
2175 w->kcontrols = kzalloc(w->num_kcontrols *
2176 sizeof(struct snd_kcontrol *),
2177 GFP_KERNEL);
2178 if (!w->kcontrols)
2179 return -ENOMEM;
2182 switch(w->id) {
2183 case snd_soc_dapm_switch:
2184 case snd_soc_dapm_mixer:
2185 case snd_soc_dapm_mixer_named_ctl:
2186 dapm_new_mixer(w);
2187 break;
2188 case snd_soc_dapm_mux:
2189 case snd_soc_dapm_virt_mux:
2190 case snd_soc_dapm_value_mux:
2191 dapm_new_mux(w);
2192 break;
2193 case snd_soc_dapm_pga:
2194 case snd_soc_dapm_out_drv:
2195 dapm_new_pga(w);
2196 break;
2197 default:
2198 break;
2201 /* Read the initial power state from the device */
2202 if (w->reg >= 0) {
2203 val = soc_widget_read(w, w->reg);
2204 val &= 1 << w->shift;
2205 if (w->invert)
2206 val = !val;
2208 if (val)
2209 w->power = 1;
2212 w->new = 1;
2214 dapm_mark_dirty(w, "new widget");
2215 dapm_debugfs_add_widget(w);
2218 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2219 return 0;
2221 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2224 * snd_soc_dapm_get_volsw - dapm mixer get callback
2225 * @kcontrol: mixer control
2226 * @ucontrol: control element information
2228 * Callback to get the value of a dapm mixer control.
2230 * Returns 0 for success.
2232 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2233 struct snd_ctl_elem_value *ucontrol)
2235 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2236 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2237 struct soc_mixer_control *mc =
2238 (struct soc_mixer_control *)kcontrol->private_value;
2239 unsigned int reg = mc->reg;
2240 unsigned int shift = mc->shift;
2241 unsigned int rshift = mc->rshift;
2242 int max = mc->max;
2243 unsigned int invert = mc->invert;
2244 unsigned int mask = (1 << fls(max)) - 1;
2246 ucontrol->value.integer.value[0] =
2247 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2248 if (shift != rshift)
2249 ucontrol->value.integer.value[1] =
2250 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2251 if (invert) {
2252 ucontrol->value.integer.value[0] =
2253 max - ucontrol->value.integer.value[0];
2254 if (shift != rshift)
2255 ucontrol->value.integer.value[1] =
2256 max - ucontrol->value.integer.value[1];
2259 return 0;
2261 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2264 * snd_soc_dapm_put_volsw - dapm mixer set callback
2265 * @kcontrol: mixer control
2266 * @ucontrol: control element information
2268 * Callback to set the value of a dapm mixer control.
2270 * Returns 0 for success.
2272 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2273 struct snd_ctl_elem_value *ucontrol)
2275 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2276 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2277 struct snd_soc_codec *codec = widget->codec;
2278 struct soc_mixer_control *mc =
2279 (struct soc_mixer_control *)kcontrol->private_value;
2280 unsigned int reg = mc->reg;
2281 unsigned int shift = mc->shift;
2282 int max = mc->max;
2283 unsigned int mask = (1 << fls(max)) - 1;
2284 unsigned int invert = mc->invert;
2285 unsigned int val;
2286 int connect, change;
2287 struct snd_soc_dapm_update update;
2288 int wi;
2290 val = (ucontrol->value.integer.value[0] & mask);
2292 if (invert)
2293 val = max - val;
2294 mask = mask << shift;
2295 val = val << shift;
2297 if (val)
2298 /* new connection */
2299 connect = invert ? 0 : 1;
2300 else
2301 /* old connection must be powered down */
2302 connect = invert ? 1 : 0;
2304 mutex_lock(&codec->mutex);
2306 change = snd_soc_test_bits(widget->codec, reg, mask, val);
2307 if (change) {
2308 for (wi = 0; wi < wlist->num_widgets; wi++) {
2309 widget = wlist->widgets[wi];
2311 widget->value = val;
2313 update.kcontrol = kcontrol;
2314 update.widget = widget;
2315 update.reg = reg;
2316 update.mask = mask;
2317 update.val = val;
2318 widget->dapm->update = &update;
2320 dapm_mixer_update_power(widget, kcontrol, connect);
2322 widget->dapm->update = NULL;
2326 mutex_unlock(&codec->mutex);
2327 return 0;
2329 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2332 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2333 * @kcontrol: mixer control
2334 * @ucontrol: control element information
2336 * Callback to get the value of a dapm enumerated double mixer control.
2338 * Returns 0 for success.
2340 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2341 struct snd_ctl_elem_value *ucontrol)
2343 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2344 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2345 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2346 unsigned int val, bitmask;
2348 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2350 val = snd_soc_read(widget->codec, e->reg);
2351 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2352 if (e->shift_l != e->shift_r)
2353 ucontrol->value.enumerated.item[1] =
2354 (val >> e->shift_r) & (bitmask - 1);
2356 return 0;
2358 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2361 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2362 * @kcontrol: mixer control
2363 * @ucontrol: control element information
2365 * Callback to set the value of a dapm enumerated double mixer control.
2367 * Returns 0 for success.
2369 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2370 struct snd_ctl_elem_value *ucontrol)
2372 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2373 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2374 struct snd_soc_codec *codec = widget->codec;
2375 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2376 unsigned int val, mux, change;
2377 unsigned int mask, bitmask;
2378 struct snd_soc_dapm_update update;
2379 int wi;
2381 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2383 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2384 return -EINVAL;
2385 mux = ucontrol->value.enumerated.item[0];
2386 val = mux << e->shift_l;
2387 mask = (bitmask - 1) << e->shift_l;
2388 if (e->shift_l != e->shift_r) {
2389 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2390 return -EINVAL;
2391 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2392 mask |= (bitmask - 1) << e->shift_r;
2395 mutex_lock(&codec->mutex);
2397 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2398 if (change) {
2399 for (wi = 0; wi < wlist->num_widgets; wi++) {
2400 widget = wlist->widgets[wi];
2402 widget->value = val;
2404 update.kcontrol = kcontrol;
2405 update.widget = widget;
2406 update.reg = e->reg;
2407 update.mask = mask;
2408 update.val = val;
2409 widget->dapm->update = &update;
2411 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2413 widget->dapm->update = NULL;
2417 mutex_unlock(&codec->mutex);
2418 return change;
2420 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2423 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2424 * @kcontrol: mixer control
2425 * @ucontrol: control element information
2427 * Returns 0 for success.
2429 int snd_soc_dapm_get_enum_virt(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];
2435 ucontrol->value.enumerated.item[0] = widget->value;
2437 return 0;
2439 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2442 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2443 * @kcontrol: mixer control
2444 * @ucontrol: control element information
2446 * Returns 0 for success.
2448 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2449 struct snd_ctl_elem_value *ucontrol)
2451 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2452 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2453 struct snd_soc_codec *codec = widget->codec;
2454 struct soc_enum *e =
2455 (struct soc_enum *)kcontrol->private_value;
2456 int change;
2457 int ret = 0;
2458 int wi;
2460 if (ucontrol->value.enumerated.item[0] >= e->max)
2461 return -EINVAL;
2463 mutex_lock(&codec->mutex);
2465 change = widget->value != ucontrol->value.enumerated.item[0];
2466 if (change) {
2467 for (wi = 0; wi < wlist->num_widgets; wi++) {
2468 widget = wlist->widgets[wi];
2470 widget->value = ucontrol->value.enumerated.item[0];
2472 dapm_mux_update_power(widget, kcontrol, change,
2473 widget->value, e);
2477 mutex_unlock(&codec->mutex);
2478 return ret;
2480 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2483 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2484 * callback
2485 * @kcontrol: mixer control
2486 * @ucontrol: control element information
2488 * Callback to get the value of a dapm semi enumerated double mixer control.
2490 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2491 * used for handling bitfield coded enumeration for example.
2493 * Returns 0 for success.
2495 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2496 struct snd_ctl_elem_value *ucontrol)
2498 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2499 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2500 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2501 unsigned int reg_val, val, mux;
2503 reg_val = snd_soc_read(widget->codec, e->reg);
2504 val = (reg_val >> e->shift_l) & e->mask;
2505 for (mux = 0; mux < e->max; mux++) {
2506 if (val == e->values[mux])
2507 break;
2509 ucontrol->value.enumerated.item[0] = mux;
2510 if (e->shift_l != e->shift_r) {
2511 val = (reg_val >> e->shift_r) & e->mask;
2512 for (mux = 0; mux < e->max; mux++) {
2513 if (val == e->values[mux])
2514 break;
2516 ucontrol->value.enumerated.item[1] = mux;
2519 return 0;
2521 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2524 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2525 * callback
2526 * @kcontrol: mixer control
2527 * @ucontrol: control element information
2529 * Callback to set the value of a dapm semi enumerated double mixer control.
2531 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2532 * used for handling bitfield coded enumeration for example.
2534 * Returns 0 for success.
2536 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2537 struct snd_ctl_elem_value *ucontrol)
2539 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2540 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2541 struct snd_soc_codec *codec = widget->codec;
2542 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2543 unsigned int val, mux, change;
2544 unsigned int mask;
2545 struct snd_soc_dapm_update update;
2546 int wi;
2548 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2549 return -EINVAL;
2550 mux = ucontrol->value.enumerated.item[0];
2551 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2552 mask = e->mask << e->shift_l;
2553 if (e->shift_l != e->shift_r) {
2554 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2555 return -EINVAL;
2556 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2557 mask |= e->mask << e->shift_r;
2560 mutex_lock(&codec->mutex);
2562 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2563 if (change) {
2564 for (wi = 0; wi < wlist->num_widgets; wi++) {
2565 widget = wlist->widgets[wi];
2567 widget->value = val;
2569 update.kcontrol = kcontrol;
2570 update.widget = widget;
2571 update.reg = e->reg;
2572 update.mask = mask;
2573 update.val = val;
2574 widget->dapm->update = &update;
2576 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2578 widget->dapm->update = NULL;
2582 mutex_unlock(&codec->mutex);
2583 return change;
2585 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2588 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2590 * @kcontrol: mixer control
2591 * @uinfo: control element information
2593 * Callback to provide information about a pin switch control.
2595 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2596 struct snd_ctl_elem_info *uinfo)
2598 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2599 uinfo->count = 1;
2600 uinfo->value.integer.min = 0;
2601 uinfo->value.integer.max = 1;
2603 return 0;
2605 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2608 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2610 * @kcontrol: mixer control
2611 * @ucontrol: Value
2613 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2614 struct snd_ctl_elem_value *ucontrol)
2616 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2617 const char *pin = (const char *)kcontrol->private_value;
2619 mutex_lock(&codec->mutex);
2621 ucontrol->value.integer.value[0] =
2622 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2624 mutex_unlock(&codec->mutex);
2626 return 0;
2628 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2631 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2633 * @kcontrol: mixer control
2634 * @ucontrol: Value
2636 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2637 struct snd_ctl_elem_value *ucontrol)
2639 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2640 const char *pin = (const char *)kcontrol->private_value;
2642 mutex_lock(&codec->mutex);
2644 if (ucontrol->value.integer.value[0])
2645 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2646 else
2647 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2649 snd_soc_dapm_sync(&codec->dapm);
2651 mutex_unlock(&codec->mutex);
2653 return 0;
2655 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2658 * snd_soc_dapm_new_control - create new dapm control
2659 * @dapm: DAPM context
2660 * @widget: widget template
2662 * Creates a new dapm control based upon the template.
2664 * Returns 0 for success else error.
2666 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2667 const struct snd_soc_dapm_widget *widget)
2669 struct snd_soc_dapm_widget *w;
2670 size_t name_len;
2672 if ((w = dapm_cnew_widget(widget)) == NULL)
2673 return -ENOMEM;
2675 name_len = strlen(widget->name) + 1;
2676 if (dapm->codec && dapm->codec->name_prefix)
2677 name_len += 1 + strlen(dapm->codec->name_prefix);
2678 w->name = kmalloc(name_len, GFP_KERNEL);
2679 if (w->name == NULL) {
2680 kfree(w);
2681 return -ENOMEM;
2683 if (dapm->codec && dapm->codec->name_prefix)
2684 snprintf(w->name, name_len, "%s %s",
2685 dapm->codec->name_prefix, widget->name);
2686 else
2687 snprintf(w->name, name_len, "%s", widget->name);
2689 switch (w->id) {
2690 case snd_soc_dapm_switch:
2691 case snd_soc_dapm_mixer:
2692 case snd_soc_dapm_mixer_named_ctl:
2693 w->power_check = dapm_generic_check_power;
2694 break;
2695 case snd_soc_dapm_mux:
2696 case snd_soc_dapm_virt_mux:
2697 case snd_soc_dapm_value_mux:
2698 w->power_check = dapm_generic_check_power;
2699 break;
2700 case snd_soc_dapm_adc:
2701 case snd_soc_dapm_aif_out:
2702 w->power_check = dapm_adc_check_power;
2703 break;
2704 case snd_soc_dapm_dac:
2705 case snd_soc_dapm_aif_in:
2706 w->power_check = dapm_dac_check_power;
2707 break;
2708 case snd_soc_dapm_pga:
2709 case snd_soc_dapm_out_drv:
2710 case snd_soc_dapm_input:
2711 case snd_soc_dapm_output:
2712 case snd_soc_dapm_micbias:
2713 case snd_soc_dapm_spk:
2714 case snd_soc_dapm_hp:
2715 case snd_soc_dapm_mic:
2716 case snd_soc_dapm_line:
2717 w->power_check = dapm_generic_check_power;
2718 break;
2719 case snd_soc_dapm_supply:
2720 w->power_check = dapm_supply_check_power;
2721 break;
2722 default:
2723 w->power_check = dapm_always_on_check_power;
2724 break;
2727 dapm->n_widgets++;
2728 w->dapm = dapm;
2729 w->codec = dapm->codec;
2730 w->platform = dapm->platform;
2731 INIT_LIST_HEAD(&w->sources);
2732 INIT_LIST_HEAD(&w->sinks);
2733 INIT_LIST_HEAD(&w->list);
2734 INIT_LIST_HEAD(&w->dirty);
2735 list_add(&w->list, &dapm->card->widgets);
2737 /* machine layer set ups unconnected pins and insertions */
2738 w->connected = 1;
2739 return 0;
2741 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2744 * snd_soc_dapm_new_controls - create new dapm controls
2745 * @dapm: DAPM context
2746 * @widget: widget array
2747 * @num: number of widgets
2749 * Creates new DAPM controls based upon the templates.
2751 * Returns 0 for success else error.
2753 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2754 const struct snd_soc_dapm_widget *widget,
2755 int num)
2757 int i, ret;
2759 for (i = 0; i < num; i++) {
2760 ret = snd_soc_dapm_new_control(dapm, widget);
2761 if (ret < 0) {
2762 dev_err(dapm->dev,
2763 "ASoC: Failed to create DAPM control %s: %d\n",
2764 widget->name, ret);
2765 return ret;
2767 widget++;
2769 return 0;
2771 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2773 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2774 const char *stream, int event)
2776 struct snd_soc_dapm_widget *w;
2778 list_for_each_entry(w, &dapm->card->widgets, list)
2780 if (!w->sname || w->dapm != dapm)
2781 continue;
2782 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2783 w->name, w->sname, stream, event);
2784 if (strstr(w->sname, stream)) {
2785 dapm_mark_dirty(w, "stream event");
2786 switch(event) {
2787 case SND_SOC_DAPM_STREAM_START:
2788 w->active = 1;
2789 break;
2790 case SND_SOC_DAPM_STREAM_STOP:
2791 w->active = 0;
2792 break;
2793 case SND_SOC_DAPM_STREAM_SUSPEND:
2794 case SND_SOC_DAPM_STREAM_RESUME:
2795 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2796 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2797 break;
2802 dapm_power_widgets(dapm, event);
2804 /* do we need to notify any clients that DAPM stream is complete */
2805 if (dapm->stream_event)
2806 dapm->stream_event(dapm, event);
2810 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2811 * @rtd: PCM runtime data
2812 * @stream: stream name
2813 * @event: stream event
2815 * Sends a stream event to the dapm core. The core then makes any
2816 * necessary widget power changes.
2818 * Returns 0 for success else error.
2820 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2821 const char *stream, int event)
2823 struct snd_soc_codec *codec = rtd->codec;
2825 if (stream == NULL)
2826 return 0;
2828 mutex_lock(&codec->mutex);
2829 soc_dapm_stream_event(&codec->dapm, stream, event);
2830 mutex_unlock(&codec->mutex);
2831 return 0;
2835 * snd_soc_dapm_enable_pin - enable pin.
2836 * @dapm: DAPM context
2837 * @pin: pin name
2839 * Enables input/output pin and its parents or children widgets iff there is
2840 * a valid audio route and active audio stream.
2841 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2842 * do any widget power switching.
2844 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2846 return snd_soc_dapm_set_pin(dapm, pin, 1);
2848 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2851 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2852 * @dapm: DAPM context
2853 * @pin: pin name
2855 * Enables input/output pin regardless of any other state. This is
2856 * intended for use with microphone bias supplies used in microphone
2857 * jack detection.
2859 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2860 * do any widget power switching.
2862 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2863 const char *pin)
2865 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2867 if (!w) {
2868 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2869 return -EINVAL;
2872 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2873 w->connected = 1;
2874 w->force = 1;
2875 dapm_mark_dirty(w, "force enable");
2877 return 0;
2879 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2882 * snd_soc_dapm_disable_pin - disable pin.
2883 * @dapm: DAPM context
2884 * @pin: pin name
2886 * Disables input/output pin and its parents or children widgets.
2887 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2888 * do any widget power switching.
2890 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2891 const char *pin)
2893 return snd_soc_dapm_set_pin(dapm, pin, 0);
2895 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2898 * snd_soc_dapm_nc_pin - permanently disable pin.
2899 * @dapm: DAPM context
2900 * @pin: pin name
2902 * Marks the specified pin as being not connected, disabling it along
2903 * any parent or child widgets. At present this is identical to
2904 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2905 * additional things such as disabling controls which only affect
2906 * paths through the pin.
2908 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2909 * do any widget power switching.
2911 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2913 return snd_soc_dapm_set_pin(dapm, pin, 0);
2915 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2918 * snd_soc_dapm_get_pin_status - get audio pin status
2919 * @dapm: DAPM context
2920 * @pin: audio signal pin endpoint (or start point)
2922 * Get audio pin status - connected or disconnected.
2924 * Returns 1 for connected otherwise 0.
2926 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2927 const char *pin)
2929 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2931 if (w)
2932 return w->connected;
2934 return 0;
2936 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2939 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2940 * @dapm: DAPM context
2941 * @pin: audio signal pin endpoint (or start point)
2943 * Mark the given endpoint or pin as ignoring suspend. When the
2944 * system is disabled a path between two endpoints flagged as ignoring
2945 * suspend will not be disabled. The path must already be enabled via
2946 * normal means at suspend time, it will not be turned on if it was not
2947 * already enabled.
2949 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2950 const char *pin)
2952 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
2954 if (!w) {
2955 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2956 return -EINVAL;
2959 w->ignore_suspend = 1;
2961 return 0;
2963 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2965 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
2966 struct snd_soc_dapm_widget *w)
2968 struct snd_soc_dapm_path *p;
2970 list_for_each_entry(p, &card->paths, list) {
2971 if ((p->source == w) || (p->sink == w)) {
2972 dev_dbg(card->dev,
2973 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
2974 p->source->name, p->source->id, p->source->dapm,
2975 p->sink->name, p->sink->id, p->sink->dapm);
2977 /* Connected to something other than the codec */
2978 if (p->source->dapm != p->sink->dapm)
2979 return true;
2981 * Loopback connection from codec external pin to
2982 * codec external pin
2984 if (p->sink->id == snd_soc_dapm_input) {
2985 switch (p->source->id) {
2986 case snd_soc_dapm_output:
2987 case snd_soc_dapm_micbias:
2988 return true;
2989 default:
2990 break;
2996 return false;
3000 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3001 * @codec: The codec whose pins should be processed
3003 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3004 * which are unused. Pins are used if they are connected externally to the
3005 * codec, whether that be to some other device, or a loop-back connection to
3006 * the codec itself.
3008 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3010 struct snd_soc_card *card = codec->card;
3011 struct snd_soc_dapm_context *dapm = &codec->dapm;
3012 struct snd_soc_dapm_widget *w;
3014 dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
3015 &card->dapm, &codec->dapm);
3017 list_for_each_entry(w, &card->widgets, list) {
3018 if (w->dapm != dapm)
3019 continue;
3020 switch (w->id) {
3021 case snd_soc_dapm_input:
3022 case snd_soc_dapm_output:
3023 case snd_soc_dapm_micbias:
3024 dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
3025 w->name);
3026 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3027 dev_dbg(codec->dev,
3028 "... Not in map; disabling\n");
3029 snd_soc_dapm_nc_pin(dapm, w->name);
3031 break;
3032 default:
3033 break;
3039 * snd_soc_dapm_free - free dapm resources
3040 * @dapm: DAPM context
3042 * Free all dapm widgets and resources.
3044 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3046 snd_soc_dapm_sys_remove(dapm->dev);
3047 dapm_debugfs_cleanup(dapm);
3048 dapm_free_widgets(dapm);
3049 list_del(&dapm->list);
3051 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3053 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
3055 struct snd_soc_dapm_widget *w;
3056 LIST_HEAD(down_list);
3057 int powerdown = 0;
3059 list_for_each_entry(w, &dapm->card->widgets, list) {
3060 if (w->dapm != dapm)
3061 continue;
3062 if (w->power) {
3063 dapm_seq_insert(w, &down_list, false);
3064 w->power = 0;
3065 powerdown = 1;
3069 /* If there were no widgets to power down we're already in
3070 * standby.
3072 if (powerdown) {
3073 if (dapm->bias_level == SND_SOC_BIAS_ON)
3074 snd_soc_dapm_set_bias_level(dapm,
3075 SND_SOC_BIAS_PREPARE);
3076 dapm_seq_run(dapm, &down_list, 0, false);
3077 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3078 snd_soc_dapm_set_bias_level(dapm,
3079 SND_SOC_BIAS_STANDBY);
3084 * snd_soc_dapm_shutdown - callback for system shutdown
3086 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3088 struct snd_soc_codec *codec;
3090 list_for_each_entry(codec, &card->codec_dev_list, list) {
3091 soc_dapm_shutdown_codec(&codec->dapm);
3092 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3093 snd_soc_dapm_set_bias_level(&codec->dapm,
3094 SND_SOC_BIAS_OFF);
3098 /* Module information */
3099 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3100 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3101 MODULE_LICENSE("GPL");