ASoC: Cache connected input and output recursions
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / soc / soc-dapm.c
blob258326b031cfa115304cab000b3a5ff7eb852d91
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/slab.h>
43 #include <sound/core.h>
44 #include <sound/pcm.h>
45 #include <sound/pcm_params.h>
46 #include <sound/soc.h>
47 #include <sound/initval.h>
49 #include <trace/events/asoc.h>
51 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
53 /* dapm power sequences - make this per codec in the future */
54 static int dapm_up_seq[] = {
55 [snd_soc_dapm_pre] = 0,
56 [snd_soc_dapm_supply] = 1,
57 [snd_soc_dapm_micbias] = 2,
58 [snd_soc_dapm_aif_in] = 3,
59 [snd_soc_dapm_aif_out] = 3,
60 [snd_soc_dapm_mic] = 4,
61 [snd_soc_dapm_mux] = 5,
62 [snd_soc_dapm_virt_mux] = 5,
63 [snd_soc_dapm_value_mux] = 5,
64 [snd_soc_dapm_dac] = 6,
65 [snd_soc_dapm_mixer] = 7,
66 [snd_soc_dapm_mixer_named_ctl] = 7,
67 [snd_soc_dapm_pga] = 8,
68 [snd_soc_dapm_adc] = 9,
69 [snd_soc_dapm_out_drv] = 10,
70 [snd_soc_dapm_hp] = 10,
71 [snd_soc_dapm_spk] = 10,
72 [snd_soc_dapm_post] = 11,
75 static int dapm_down_seq[] = {
76 [snd_soc_dapm_pre] = 0,
77 [snd_soc_dapm_adc] = 1,
78 [snd_soc_dapm_hp] = 2,
79 [snd_soc_dapm_spk] = 2,
80 [snd_soc_dapm_out_drv] = 2,
81 [snd_soc_dapm_pga] = 4,
82 [snd_soc_dapm_mixer_named_ctl] = 5,
83 [snd_soc_dapm_mixer] = 5,
84 [snd_soc_dapm_dac] = 6,
85 [snd_soc_dapm_mic] = 7,
86 [snd_soc_dapm_micbias] = 8,
87 [snd_soc_dapm_mux] = 9,
88 [snd_soc_dapm_virt_mux] = 9,
89 [snd_soc_dapm_value_mux] = 9,
90 [snd_soc_dapm_aif_in] = 10,
91 [snd_soc_dapm_aif_out] = 10,
92 [snd_soc_dapm_supply] = 11,
93 [snd_soc_dapm_post] = 12,
96 static void pop_wait(u32 pop_time)
98 if (pop_time)
99 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
102 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
104 va_list args;
105 char *buf;
107 if (!pop_time)
108 return;
110 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
111 if (buf == NULL)
112 return;
114 va_start(args, fmt);
115 vsnprintf(buf, PAGE_SIZE, fmt, args);
116 dev_info(dev, "%s", buf);
117 va_end(args);
119 kfree(buf);
122 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
124 return !list_empty(&w->dirty);
127 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
129 if (!dapm_dirty_widget(w)) {
130 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
131 w->name, reason);
132 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
136 /* create a new dapm widget */
137 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
138 const struct snd_soc_dapm_widget *_widget)
140 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
143 /* get snd_card from DAPM context */
144 static inline struct snd_card *dapm_get_snd_card(
145 struct snd_soc_dapm_context *dapm)
147 if (dapm->codec)
148 return dapm->codec->card->snd_card;
149 else if (dapm->platform)
150 return dapm->platform->card->snd_card;
151 else
152 BUG();
154 /* unreachable */
155 return NULL;
158 /* get soc_card from DAPM context */
159 static inline struct snd_soc_card *dapm_get_soc_card(
160 struct snd_soc_dapm_context *dapm)
162 if (dapm->codec)
163 return dapm->codec->card;
164 else if (dapm->platform)
165 return dapm->platform->card;
166 else
167 BUG();
169 /* unreachable */
170 return NULL;
173 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
175 if (w->codec)
176 return snd_soc_read(w->codec, reg);
177 else if (w->platform)
178 return snd_soc_platform_read(w->platform, reg);
180 dev_err(w->dapm->dev, "no valid widget read method\n");
181 return -1;
184 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
186 if (w->codec)
187 return snd_soc_write(w->codec, reg, val);
188 else if (w->platform)
189 return snd_soc_platform_write(w->platform, reg, val);
191 dev_err(w->dapm->dev, "no valid widget write method\n");
192 return -1;
195 static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
196 unsigned short reg, unsigned int mask, unsigned int value)
198 int change;
199 unsigned int old, new;
200 int ret;
202 ret = soc_widget_read(w, reg);
203 if (ret < 0)
204 return ret;
206 old = ret;
207 new = (old & ~mask) | (value & mask);
208 change = old != new;
209 if (change) {
210 ret = soc_widget_write(w, reg, new);
211 if (ret < 0)
212 return ret;
215 return change;
219 * snd_soc_dapm_set_bias_level - set the bias level for the system
220 * @dapm: DAPM context
221 * @level: level to configure
223 * Configure the bias (power) levels for the SoC audio device.
225 * Returns 0 for success else error.
227 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
228 enum snd_soc_bias_level level)
230 struct snd_soc_card *card = dapm->card;
231 int ret = 0;
233 trace_snd_soc_bias_level_start(card, level);
235 if (card && card->set_bias_level)
236 ret = card->set_bias_level(card, dapm, level);
237 if (ret != 0)
238 goto out;
240 if (dapm->codec) {
241 if (dapm->codec->driver->set_bias_level)
242 ret = dapm->codec->driver->set_bias_level(dapm->codec,
243 level);
244 else
245 dapm->bias_level = level;
247 if (ret != 0)
248 goto out;
250 if (card && card->set_bias_level_post)
251 ret = card->set_bias_level_post(card, dapm, level);
252 out:
253 trace_snd_soc_bias_level_done(card, level);
255 return ret;
258 /* set up initial codec paths */
259 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
260 struct snd_soc_dapm_path *p, int i)
262 switch (w->id) {
263 case snd_soc_dapm_switch:
264 case snd_soc_dapm_mixer:
265 case snd_soc_dapm_mixer_named_ctl: {
266 int val;
267 struct soc_mixer_control *mc = (struct soc_mixer_control *)
268 w->kcontrol_news[i].private_value;
269 unsigned int reg = mc->reg;
270 unsigned int shift = mc->shift;
271 int max = mc->max;
272 unsigned int mask = (1 << fls(max)) - 1;
273 unsigned int invert = mc->invert;
275 val = soc_widget_read(w, reg);
276 val = (val >> shift) & mask;
278 if ((invert && !val) || (!invert && val))
279 p->connect = 1;
280 else
281 p->connect = 0;
283 break;
284 case snd_soc_dapm_mux: {
285 struct soc_enum *e = (struct soc_enum *)
286 w->kcontrol_news[i].private_value;
287 int val, item, bitmask;
289 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
291 val = soc_widget_read(w, e->reg);
292 item = (val >> e->shift_l) & (bitmask - 1);
294 p->connect = 0;
295 for (i = 0; i < e->max; i++) {
296 if (!(strcmp(p->name, e->texts[i])) && item == i)
297 p->connect = 1;
300 break;
301 case snd_soc_dapm_virt_mux: {
302 struct soc_enum *e = (struct soc_enum *)
303 w->kcontrol_news[i].private_value;
305 p->connect = 0;
306 /* since a virtual mux has no backing registers to
307 * decide which path to connect, it will try to match
308 * with the first enumeration. This is to ensure
309 * that the default mux choice (the first) will be
310 * correctly powered up during initialization.
312 if (!strcmp(p->name, e->texts[0]))
313 p->connect = 1;
315 break;
316 case snd_soc_dapm_value_mux: {
317 struct soc_enum *e = (struct soc_enum *)
318 w->kcontrol_news[i].private_value;
319 int val, item;
321 val = soc_widget_read(w, e->reg);
322 val = (val >> e->shift_l) & e->mask;
323 for (item = 0; item < e->max; item++) {
324 if (val == e->values[item])
325 break;
328 p->connect = 0;
329 for (i = 0; i < e->max; i++) {
330 if (!(strcmp(p->name, e->texts[i])) && item == i)
331 p->connect = 1;
334 break;
335 /* does not affect routing - always connected */
336 case snd_soc_dapm_pga:
337 case snd_soc_dapm_out_drv:
338 case snd_soc_dapm_output:
339 case snd_soc_dapm_adc:
340 case snd_soc_dapm_input:
341 case snd_soc_dapm_dac:
342 case snd_soc_dapm_micbias:
343 case snd_soc_dapm_vmid:
344 case snd_soc_dapm_supply:
345 case snd_soc_dapm_aif_in:
346 case snd_soc_dapm_aif_out:
347 case snd_soc_dapm_hp:
348 case snd_soc_dapm_mic:
349 case snd_soc_dapm_spk:
350 case snd_soc_dapm_line:
351 p->connect = 1;
352 break;
353 /* does affect routing - dynamically connected */
354 case snd_soc_dapm_pre:
355 case snd_soc_dapm_post:
356 p->connect = 0;
357 break;
361 /* connect mux widget to its interconnecting audio paths */
362 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
363 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
364 struct snd_soc_dapm_path *path, const char *control_name,
365 const struct snd_kcontrol_new *kcontrol)
367 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
368 int i;
370 for (i = 0; i < e->max; i++) {
371 if (!(strcmp(control_name, e->texts[i]))) {
372 list_add(&path->list, &dapm->card->paths);
373 list_add(&path->list_sink, &dest->sources);
374 list_add(&path->list_source, &src->sinks);
375 path->name = (char*)e->texts[i];
376 dapm_set_path_status(dest, path, 0);
377 return 0;
381 return -ENODEV;
384 /* connect mixer widget to its interconnecting audio paths */
385 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
386 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
387 struct snd_soc_dapm_path *path, const char *control_name)
389 int i;
391 /* search for mixer kcontrol */
392 for (i = 0; i < dest->num_kcontrols; i++) {
393 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
394 list_add(&path->list, &dapm->card->paths);
395 list_add(&path->list_sink, &dest->sources);
396 list_add(&path->list_source, &src->sinks);
397 path->name = dest->kcontrol_news[i].name;
398 dapm_set_path_status(dest, path, i);
399 return 0;
402 return -ENODEV;
405 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
406 struct snd_soc_dapm_widget *kcontrolw,
407 const struct snd_kcontrol_new *kcontrol_new,
408 struct snd_kcontrol **kcontrol)
410 struct snd_soc_dapm_widget *w;
411 int i;
413 *kcontrol = NULL;
415 list_for_each_entry(w, &dapm->card->widgets, list) {
416 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
417 continue;
418 for (i = 0; i < w->num_kcontrols; i++) {
419 if (&w->kcontrol_news[i] == kcontrol_new) {
420 if (w->kcontrols)
421 *kcontrol = w->kcontrols[i];
422 return 1;
427 return 0;
430 /* create new dapm mixer control */
431 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
433 struct snd_soc_dapm_context *dapm = w->dapm;
434 int i, ret = 0;
435 size_t name_len, prefix_len;
436 struct snd_soc_dapm_path *path;
437 struct snd_card *card = dapm->card->snd_card;
438 const char *prefix;
439 struct snd_soc_dapm_widget_list *wlist;
440 size_t wlistsize;
442 if (dapm->codec)
443 prefix = dapm->codec->name_prefix;
444 else
445 prefix = NULL;
447 if (prefix)
448 prefix_len = strlen(prefix) + 1;
449 else
450 prefix_len = 0;
452 /* add kcontrol */
453 for (i = 0; i < w->num_kcontrols; i++) {
455 /* match name */
456 list_for_each_entry(path, &w->sources, list_sink) {
458 /* mixer/mux paths name must match control name */
459 if (path->name != (char *)w->kcontrol_news[i].name)
460 continue;
462 if (w->kcontrols[i]) {
463 path->kcontrol = w->kcontrols[i];
464 continue;
467 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
468 sizeof(struct snd_soc_dapm_widget *),
469 wlist = kzalloc(wlistsize, GFP_KERNEL);
470 if (wlist == NULL) {
471 dev_err(dapm->dev,
472 "asoc: can't allocate widget list for %s\n",
473 w->name);
474 return -ENOMEM;
476 wlist->num_widgets = 1;
477 wlist->widgets[0] = w;
479 /* add dapm control with long name.
480 * for dapm_mixer this is the concatenation of the
481 * mixer and kcontrol name.
482 * for dapm_mixer_named_ctl this is simply the
483 * kcontrol name.
485 name_len = strlen(w->kcontrol_news[i].name) + 1;
486 if (w->id != snd_soc_dapm_mixer_named_ctl)
487 name_len += 1 + strlen(w->name);
489 path->long_name = kmalloc(name_len, GFP_KERNEL);
491 if (path->long_name == NULL) {
492 kfree(wlist);
493 return -ENOMEM;
496 switch (w->id) {
497 default:
498 /* The control will get a prefix from
499 * the control creation process but
500 * we're also using the same prefix
501 * for widgets so cut the prefix off
502 * the front of the widget name.
504 snprintf(path->long_name, name_len, "%s %s",
505 w->name + prefix_len,
506 w->kcontrol_news[i].name);
507 break;
508 case snd_soc_dapm_mixer_named_ctl:
509 snprintf(path->long_name, name_len, "%s",
510 w->kcontrol_news[i].name);
511 break;
514 path->long_name[name_len - 1] = '\0';
516 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
517 wlist, path->long_name,
518 prefix);
519 ret = snd_ctl_add(card, path->kcontrol);
520 if (ret < 0) {
521 dev_err(dapm->dev,
522 "asoc: failed to add dapm kcontrol %s: %d\n",
523 path->long_name, ret);
524 kfree(wlist);
525 kfree(path->long_name);
526 path->long_name = NULL;
527 return ret;
529 w->kcontrols[i] = path->kcontrol;
532 return ret;
535 /* create new dapm mux control */
536 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
538 struct snd_soc_dapm_context *dapm = w->dapm;
539 struct snd_soc_dapm_path *path = NULL;
540 struct snd_kcontrol *kcontrol;
541 struct snd_card *card = dapm->card->snd_card;
542 const char *prefix;
543 size_t prefix_len;
544 int ret;
545 struct snd_soc_dapm_widget_list *wlist;
546 int shared, wlistentries;
547 size_t wlistsize;
548 char *name;
550 if (w->num_kcontrols != 1) {
551 dev_err(dapm->dev,
552 "asoc: mux %s has incorrect number of controls\n",
553 w->name);
554 return -EINVAL;
557 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
558 &kcontrol);
559 if (kcontrol) {
560 wlist = kcontrol->private_data;
561 wlistentries = wlist->num_widgets + 1;
562 } else {
563 wlist = NULL;
564 wlistentries = 1;
566 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
567 wlistentries * sizeof(struct snd_soc_dapm_widget *),
568 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
569 if (wlist == NULL) {
570 dev_err(dapm->dev,
571 "asoc: can't allocate widget list for %s\n", w->name);
572 return -ENOMEM;
574 wlist->num_widgets = wlistentries;
575 wlist->widgets[wlistentries - 1] = w;
577 if (!kcontrol) {
578 if (dapm->codec)
579 prefix = dapm->codec->name_prefix;
580 else
581 prefix = NULL;
583 if (shared) {
584 name = w->kcontrol_news[0].name;
585 prefix_len = 0;
586 } else {
587 name = w->name;
588 if (prefix)
589 prefix_len = strlen(prefix) + 1;
590 else
591 prefix_len = 0;
595 * The control will get a prefix from the control creation
596 * process but we're also using the same prefix for widgets so
597 * cut the prefix off the front of the widget name.
599 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
600 name + prefix_len, prefix);
601 ret = snd_ctl_add(card, kcontrol);
602 if (ret < 0) {
603 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
604 w->name, ret);
605 kfree(wlist);
606 return ret;
610 kcontrol->private_data = wlist;
612 w->kcontrols[0] = kcontrol;
614 list_for_each_entry(path, &w->sources, list_sink)
615 path->kcontrol = kcontrol;
617 return 0;
620 /* create new dapm volume control */
621 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
623 if (w->num_kcontrols)
624 dev_err(w->dapm->dev,
625 "asoc: PGA controls not supported: '%s'\n", w->name);
627 return 0;
630 /* reset 'walked' bit for each dapm path */
631 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
633 struct snd_soc_dapm_path *p;
635 list_for_each_entry(p, &dapm->card->paths, list)
636 p->walked = 0;
639 /* We implement power down on suspend by checking the power state of
640 * the ALSA card - when we are suspending the ALSA state for the card
641 * is set to D3.
643 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
645 int level = snd_power_get_state(widget->dapm->card->snd_card);
647 switch (level) {
648 case SNDRV_CTL_POWER_D3hot:
649 case SNDRV_CTL_POWER_D3cold:
650 if (widget->ignore_suspend)
651 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
652 widget->name);
653 return widget->ignore_suspend;
654 default:
655 return 1;
660 * Recursively check for a completed path to an active or physically connected
661 * output widget. Returns number of complete paths.
663 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
665 struct snd_soc_dapm_path *path;
666 int con = 0;
668 if (widget->outputs >= 0)
669 return widget->outputs;
671 DAPM_UPDATE_STAT(widget, path_checks);
673 if (widget->id == snd_soc_dapm_supply)
674 return 0;
676 switch (widget->id) {
677 case snd_soc_dapm_adc:
678 case snd_soc_dapm_aif_out:
679 if (widget->active) {
680 widget->outputs = snd_soc_dapm_suspend_check(widget);
681 return widget->outputs;
683 default:
684 break;
687 if (widget->connected) {
688 /* connected pin ? */
689 if (widget->id == snd_soc_dapm_output && !widget->ext) {
690 widget->outputs = snd_soc_dapm_suspend_check(widget);
691 return widget->outputs;
694 /* connected jack or spk ? */
695 if (widget->id == snd_soc_dapm_hp ||
696 widget->id == snd_soc_dapm_spk ||
697 (widget->id == snd_soc_dapm_line &&
698 !list_empty(&widget->sources))) {
699 widget->outputs = snd_soc_dapm_suspend_check(widget);
700 return widget->outputs;
704 list_for_each_entry(path, &widget->sinks, list_source) {
705 DAPM_UPDATE_STAT(widget, neighbour_checks);
707 if (path->weak)
708 continue;
710 if (path->walked)
711 continue;
713 if (path->sink && path->connect) {
714 path->walked = 1;
715 con += is_connected_output_ep(path->sink);
719 widget->outputs = con;
721 return con;
725 * Recursively check for a completed path to an active or physically connected
726 * input widget. Returns number of complete paths.
728 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
730 struct snd_soc_dapm_path *path;
731 int con = 0;
733 if (widget->inputs >= 0)
734 return widget->inputs;
736 DAPM_UPDATE_STAT(widget, path_checks);
738 if (widget->id == snd_soc_dapm_supply)
739 return 0;
741 /* active stream ? */
742 switch (widget->id) {
743 case snd_soc_dapm_dac:
744 case snd_soc_dapm_aif_in:
745 if (widget->active) {
746 widget->inputs = snd_soc_dapm_suspend_check(widget);
747 return widget->inputs;
749 default:
750 break;
753 if (widget->connected) {
754 /* connected pin ? */
755 if (widget->id == snd_soc_dapm_input && !widget->ext) {
756 widget->inputs = snd_soc_dapm_suspend_check(widget);
757 return widget->inputs;
760 /* connected VMID/Bias for lower pops */
761 if (widget->id == snd_soc_dapm_vmid) {
762 widget->inputs = snd_soc_dapm_suspend_check(widget);
763 return widget->inputs;
766 /* connected jack ? */
767 if (widget->id == snd_soc_dapm_mic ||
768 (widget->id == snd_soc_dapm_line &&
769 !list_empty(&widget->sinks))) {
770 widget->inputs = snd_soc_dapm_suspend_check(widget);
771 return widget->inputs;
776 list_for_each_entry(path, &widget->sources, list_sink) {
777 DAPM_UPDATE_STAT(widget, neighbour_checks);
779 if (path->weak)
780 continue;
782 if (path->walked)
783 continue;
785 if (path->source && path->connect) {
786 path->walked = 1;
787 con += is_connected_input_ep(path->source);
791 widget->inputs = con;
793 return con;
797 * Handler for generic register modifier widget.
799 int dapm_reg_event(struct snd_soc_dapm_widget *w,
800 struct snd_kcontrol *kcontrol, int event)
802 unsigned int val;
804 if (SND_SOC_DAPM_EVENT_ON(event))
805 val = w->on_val;
806 else
807 val = w->off_val;
809 soc_widget_update_bits(w, -(w->reg + 1),
810 w->mask << w->shift, val << w->shift);
812 return 0;
814 EXPORT_SYMBOL_GPL(dapm_reg_event);
816 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
818 if (w->power_checked)
819 return w->new_power;
821 if (w->force)
822 w->new_power = 1;
823 else
824 w->new_power = w->power_check(w);
826 w->power_checked = true;
828 return w->new_power;
831 /* Generic check to see if a widget should be powered.
833 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
835 int in, out;
837 DAPM_UPDATE_STAT(w, power_checks);
839 in = is_connected_input_ep(w);
840 dapm_clear_walk(w->dapm);
841 out = is_connected_output_ep(w);
842 dapm_clear_walk(w->dapm);
843 return out != 0 && in != 0;
846 /* Check to see if an ADC has power */
847 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
849 int in;
851 DAPM_UPDATE_STAT(w, power_checks);
853 if (w->active) {
854 in = is_connected_input_ep(w);
855 dapm_clear_walk(w->dapm);
856 return in != 0;
857 } else {
858 return dapm_generic_check_power(w);
862 /* Check to see if a DAC has power */
863 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
865 int out;
867 DAPM_UPDATE_STAT(w, power_checks);
869 if (w->active) {
870 out = is_connected_output_ep(w);
871 dapm_clear_walk(w->dapm);
872 return out != 0;
873 } else {
874 return dapm_generic_check_power(w);
878 /* Check to see if a power supply is needed */
879 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
881 struct snd_soc_dapm_path *path;
883 DAPM_UPDATE_STAT(w, power_checks);
885 /* Check if one of our outputs is connected */
886 list_for_each_entry(path, &w->sinks, list_source) {
887 DAPM_UPDATE_STAT(w, neighbour_checks);
889 if (path->weak)
890 continue;
892 if (path->connected &&
893 !path->connected(path->source, path->sink))
894 continue;
896 if (!path->sink)
897 continue;
899 if (dapm_widget_power_check(path->sink))
900 return 1;
903 dapm_clear_walk(w->dapm);
905 return 0;
908 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
910 return 1;
913 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
914 struct snd_soc_dapm_widget *b,
915 bool power_up)
917 int *sort;
919 if (power_up)
920 sort = dapm_up_seq;
921 else
922 sort = dapm_down_seq;
924 if (sort[a->id] != sort[b->id])
925 return sort[a->id] - sort[b->id];
926 if (a->subseq != b->subseq) {
927 if (power_up)
928 return a->subseq - b->subseq;
929 else
930 return b->subseq - a->subseq;
932 if (a->reg != b->reg)
933 return a->reg - b->reg;
934 if (a->dapm != b->dapm)
935 return (unsigned long)a->dapm - (unsigned long)b->dapm;
937 return 0;
940 /* Insert a widget in order into a DAPM power sequence. */
941 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
942 struct list_head *list,
943 bool power_up)
945 struct snd_soc_dapm_widget *w;
947 list_for_each_entry(w, list, power_list)
948 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
949 list_add_tail(&new_widget->power_list, &w->power_list);
950 return;
953 list_add_tail(&new_widget->power_list, list);
956 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
957 struct snd_soc_dapm_widget *w, int event)
959 struct snd_soc_card *card = dapm->card;
960 const char *ev_name;
961 int power, ret;
963 switch (event) {
964 case SND_SOC_DAPM_PRE_PMU:
965 ev_name = "PRE_PMU";
966 power = 1;
967 break;
968 case SND_SOC_DAPM_POST_PMU:
969 ev_name = "POST_PMU";
970 power = 1;
971 break;
972 case SND_SOC_DAPM_PRE_PMD:
973 ev_name = "PRE_PMD";
974 power = 0;
975 break;
976 case SND_SOC_DAPM_POST_PMD:
977 ev_name = "POST_PMD";
978 power = 0;
979 break;
980 default:
981 BUG();
982 return;
985 if (w->power != power)
986 return;
988 if (w->event && (w->event_flags & event)) {
989 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
990 w->name, ev_name);
991 trace_snd_soc_dapm_widget_event_start(w, event);
992 ret = w->event(w, NULL, event);
993 trace_snd_soc_dapm_widget_event_done(w, event);
994 if (ret < 0)
995 pr_err("%s: %s event failed: %d\n",
996 ev_name, w->name, ret);
1000 /* Apply the coalesced changes from a DAPM sequence */
1001 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
1002 struct list_head *pending)
1004 struct snd_soc_card *card = dapm->card;
1005 struct snd_soc_dapm_widget *w;
1006 int reg, power;
1007 unsigned int value = 0;
1008 unsigned int mask = 0;
1009 unsigned int cur_mask;
1011 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1012 power_list)->reg;
1014 list_for_each_entry(w, pending, power_list) {
1015 cur_mask = 1 << w->shift;
1016 BUG_ON(reg != w->reg);
1018 if (w->invert)
1019 power = !w->power;
1020 else
1021 power = w->power;
1023 mask |= cur_mask;
1024 if (power)
1025 value |= cur_mask;
1027 pop_dbg(dapm->dev, card->pop_time,
1028 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1029 w->name, reg, value, mask);
1031 /* Check for events */
1032 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1033 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1036 if (reg >= 0) {
1037 /* Any widget will do, they should all be updating the
1038 * same register.
1040 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1041 power_list);
1043 pop_dbg(dapm->dev, card->pop_time,
1044 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1045 value, mask, reg, card->pop_time);
1046 pop_wait(card->pop_time);
1047 soc_widget_update_bits(w, reg, mask, value);
1050 list_for_each_entry(w, pending, power_list) {
1051 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1052 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1056 /* Apply a DAPM power sequence.
1058 * We walk over a pre-sorted list of widgets to apply power to. In
1059 * order to minimise the number of writes to the device required
1060 * multiple widgets will be updated in a single write where possible.
1061 * Currently anything that requires more than a single write is not
1062 * handled.
1064 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1065 struct list_head *list, int event, bool power_up)
1067 struct snd_soc_dapm_widget *w, *n;
1068 LIST_HEAD(pending);
1069 int cur_sort = -1;
1070 int cur_subseq = -1;
1071 int cur_reg = SND_SOC_NOPM;
1072 struct snd_soc_dapm_context *cur_dapm = NULL;
1073 int ret, i;
1074 int *sort;
1076 if (power_up)
1077 sort = dapm_up_seq;
1078 else
1079 sort = dapm_down_seq;
1081 list_for_each_entry_safe(w, n, list, power_list) {
1082 ret = 0;
1084 /* Do we need to apply any queued changes? */
1085 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1086 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1087 if (!list_empty(&pending))
1088 dapm_seq_run_coalesced(cur_dapm, &pending);
1090 if (cur_dapm && cur_dapm->seq_notifier) {
1091 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1092 if (sort[i] == cur_sort)
1093 cur_dapm->seq_notifier(cur_dapm,
1095 cur_subseq);
1098 INIT_LIST_HEAD(&pending);
1099 cur_sort = -1;
1100 cur_subseq = INT_MIN;
1101 cur_reg = SND_SOC_NOPM;
1102 cur_dapm = NULL;
1105 switch (w->id) {
1106 case snd_soc_dapm_pre:
1107 if (!w->event)
1108 list_for_each_entry_safe_continue(w, n, list,
1109 power_list);
1111 if (event == SND_SOC_DAPM_STREAM_START)
1112 ret = w->event(w,
1113 NULL, SND_SOC_DAPM_PRE_PMU);
1114 else if (event == SND_SOC_DAPM_STREAM_STOP)
1115 ret = w->event(w,
1116 NULL, SND_SOC_DAPM_PRE_PMD);
1117 break;
1119 case snd_soc_dapm_post:
1120 if (!w->event)
1121 list_for_each_entry_safe_continue(w, n, list,
1122 power_list);
1124 if (event == SND_SOC_DAPM_STREAM_START)
1125 ret = w->event(w,
1126 NULL, SND_SOC_DAPM_POST_PMU);
1127 else if (event == SND_SOC_DAPM_STREAM_STOP)
1128 ret = w->event(w,
1129 NULL, SND_SOC_DAPM_POST_PMD);
1130 break;
1132 default:
1133 /* Queue it up for application */
1134 cur_sort = sort[w->id];
1135 cur_subseq = w->subseq;
1136 cur_reg = w->reg;
1137 cur_dapm = w->dapm;
1138 list_move(&w->power_list, &pending);
1139 break;
1142 if (ret < 0)
1143 dev_err(w->dapm->dev,
1144 "Failed to apply widget power: %d\n", ret);
1147 if (!list_empty(&pending))
1148 dapm_seq_run_coalesced(cur_dapm, &pending);
1150 if (cur_dapm && cur_dapm->seq_notifier) {
1151 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1152 if (sort[i] == cur_sort)
1153 cur_dapm->seq_notifier(cur_dapm,
1154 i, cur_subseq);
1158 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1160 struct snd_soc_dapm_update *update = dapm->update;
1161 struct snd_soc_dapm_widget *w;
1162 int ret;
1164 if (!update)
1165 return;
1167 w = update->widget;
1169 if (w->event &&
1170 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1171 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1172 if (ret != 0)
1173 pr_err("%s DAPM pre-event failed: %d\n",
1174 w->name, ret);
1177 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1178 update->val);
1179 if (ret < 0)
1180 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1182 if (w->event &&
1183 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1184 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1185 if (ret != 0)
1186 pr_err("%s DAPM post-event failed: %d\n",
1187 w->name, ret);
1191 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1192 * they're changing state.
1194 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1196 struct snd_soc_dapm_context *d = data;
1197 int ret;
1199 /* If we're off and we're not supposed to be go into STANDBY */
1200 if (d->bias_level == SND_SOC_BIAS_OFF &&
1201 d->target_bias_level != SND_SOC_BIAS_OFF) {
1202 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1203 if (ret != 0)
1204 dev_err(d->dev,
1205 "Failed to turn on bias: %d\n", ret);
1208 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1209 if (d->bias_level != d->target_bias_level) {
1210 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1211 if (ret != 0)
1212 dev_err(d->dev,
1213 "Failed to prepare bias: %d\n", ret);
1217 /* Async callback run prior to DAPM sequences - brings to their final
1218 * state.
1220 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1222 struct snd_soc_dapm_context *d = data;
1223 int ret;
1225 /* If we just powered the last thing off drop to standby bias */
1226 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1227 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1228 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1229 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1230 if (ret != 0)
1231 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1232 ret);
1235 /* If we're in standby and can support bias off then do that */
1236 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1237 d->target_bias_level == SND_SOC_BIAS_OFF) {
1238 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1239 if (ret != 0)
1240 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1243 /* If we just powered up then move to active bias */
1244 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1245 d->target_bias_level == SND_SOC_BIAS_ON) {
1246 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1247 if (ret != 0)
1248 dev_err(d->dev, "Failed to apply active bias: %d\n",
1249 ret);
1253 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1254 bool power, bool connect)
1256 /* If a connection is being made or broken then that update
1257 * will have marked the peer dirty, otherwise the widgets are
1258 * not connected and this update has no impact. */
1259 if (!connect)
1260 return;
1262 /* If the peer is already in the state we're moving to then we
1263 * won't have an impact on it. */
1264 if (power != peer->power)
1265 dapm_mark_dirty(peer, "peer state change");
1268 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1269 struct list_head *up_list,
1270 struct list_head *down_list)
1272 struct snd_soc_dapm_path *path;
1274 if (w->power == power)
1275 return;
1277 trace_snd_soc_dapm_widget_power(w, power);
1279 /* If we changed our power state perhaps our neigbours changed
1280 * also.
1282 list_for_each_entry(path, &w->sources, list_sink) {
1283 if (path->source) {
1284 dapm_widget_set_peer_power(path->source, power,
1285 path->connect);
1288 switch (w->id) {
1289 case snd_soc_dapm_supply:
1290 /* Supplies can't affect their outputs, only their inputs */
1291 break;
1292 default:
1293 list_for_each_entry(path, &w->sinks, list_source) {
1294 if (path->sink) {
1295 dapm_widget_set_peer_power(path->sink, power,
1296 path->connect);
1299 break;
1302 if (power)
1303 dapm_seq_insert(w, up_list, true);
1304 else
1305 dapm_seq_insert(w, down_list, false);
1307 w->power = power;
1310 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1311 struct list_head *up_list,
1312 struct list_head *down_list)
1314 int power;
1316 switch (w->id) {
1317 case snd_soc_dapm_pre:
1318 dapm_seq_insert(w, down_list, false);
1319 break;
1320 case snd_soc_dapm_post:
1321 dapm_seq_insert(w, up_list, true);
1322 break;
1324 default:
1325 power = dapm_widget_power_check(w);
1327 dapm_widget_set_power(w, power, up_list, down_list);
1328 break;
1333 * Scan each dapm widget for complete audio path.
1334 * A complete path is a route that has valid endpoints i.e.:-
1336 * o DAC to output pin.
1337 * o Input Pin to ADC.
1338 * o Input pin to Output pin (bypass, sidetone)
1339 * o DAC to ADC (loopback).
1341 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1343 struct snd_soc_card *card = dapm->card;
1344 struct snd_soc_dapm_widget *w;
1345 struct snd_soc_dapm_context *d;
1346 LIST_HEAD(up_list);
1347 LIST_HEAD(down_list);
1348 LIST_HEAD(async_domain);
1349 enum snd_soc_bias_level bias;
1351 trace_snd_soc_dapm_start(card);
1353 list_for_each_entry(d, &card->dapm_list, list) {
1354 if (d->n_widgets || d->codec == NULL) {
1355 if (d->idle_bias_off)
1356 d->target_bias_level = SND_SOC_BIAS_OFF;
1357 else
1358 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1362 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
1364 list_for_each_entry(w, &card->widgets, list) {
1365 w->power_checked = false;
1366 w->inputs = -1;
1367 w->outputs = -1;
1370 /* Check which widgets we need to power and store them in
1371 * lists indicating if they should be powered up or down. We
1372 * only check widgets that have been flagged as dirty but note
1373 * that new widgets may be added to the dirty list while we
1374 * iterate.
1376 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1377 dapm_power_one_widget(w, &up_list, &down_list);
1380 list_for_each_entry(w, &card->widgets, list) {
1381 list_del_init(&w->dirty);
1383 if (w->power) {
1384 d = w->dapm;
1386 /* Supplies and micbiases only bring the
1387 * context up to STANDBY as unless something
1388 * else is active and passing audio they
1389 * generally don't require full power.
1391 switch (w->id) {
1392 case snd_soc_dapm_supply:
1393 case snd_soc_dapm_micbias:
1394 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1395 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1396 break;
1397 default:
1398 d->target_bias_level = SND_SOC_BIAS_ON;
1399 break;
1405 /* If there are no DAPM widgets then try to figure out power from the
1406 * event type.
1408 if (!dapm->n_widgets) {
1409 switch (event) {
1410 case SND_SOC_DAPM_STREAM_START:
1411 case SND_SOC_DAPM_STREAM_RESUME:
1412 dapm->target_bias_level = SND_SOC_BIAS_ON;
1413 break;
1414 case SND_SOC_DAPM_STREAM_STOP:
1415 if (dapm->codec->active)
1416 dapm->target_bias_level = SND_SOC_BIAS_ON;
1417 else
1418 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1419 break;
1420 case SND_SOC_DAPM_STREAM_SUSPEND:
1421 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1422 break;
1423 case SND_SOC_DAPM_STREAM_NOP:
1424 dapm->target_bias_level = dapm->bias_level;
1425 break;
1426 default:
1427 break;
1431 /* Force all contexts in the card to the same bias state if
1432 * they're not ground referenced.
1434 bias = SND_SOC_BIAS_OFF;
1435 list_for_each_entry(d, &card->dapm_list, list)
1436 if (d->target_bias_level > bias)
1437 bias = d->target_bias_level;
1438 list_for_each_entry(d, &card->dapm_list, list)
1439 if (!d->idle_bias_off)
1440 d->target_bias_level = bias;
1442 trace_snd_soc_dapm_walk_done(card);
1444 /* Run all the bias changes in parallel */
1445 list_for_each_entry(d, &dapm->card->dapm_list, list)
1446 async_schedule_domain(dapm_pre_sequence_async, d,
1447 &async_domain);
1448 async_synchronize_full_domain(&async_domain);
1450 /* Power down widgets first; try to avoid amplifying pops. */
1451 dapm_seq_run(dapm, &down_list, event, false);
1453 dapm_widget_update(dapm);
1455 /* Now power up. */
1456 dapm_seq_run(dapm, &up_list, event, true);
1458 /* Run all the bias changes in parallel */
1459 list_for_each_entry(d, &dapm->card->dapm_list, list)
1460 async_schedule_domain(dapm_post_sequence_async, d,
1461 &async_domain);
1462 async_synchronize_full_domain(&async_domain);
1464 pop_dbg(dapm->dev, card->pop_time,
1465 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1466 pop_wait(card->pop_time);
1468 trace_snd_soc_dapm_done(card);
1470 return 0;
1473 #ifdef CONFIG_DEBUG_FS
1474 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1476 file->private_data = inode->i_private;
1477 return 0;
1480 static ssize_t dapm_widget_power_read_file(struct file *file,
1481 char __user *user_buf,
1482 size_t count, loff_t *ppos)
1484 struct snd_soc_dapm_widget *w = file->private_data;
1485 char *buf;
1486 int in, out;
1487 ssize_t ret;
1488 struct snd_soc_dapm_path *p = NULL;
1490 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1491 if (!buf)
1492 return -ENOMEM;
1494 in = is_connected_input_ep(w);
1495 dapm_clear_walk(w->dapm);
1496 out = is_connected_output_ep(w);
1497 dapm_clear_walk(w->dapm);
1499 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1500 w->name, w->power ? "On" : "Off", in, out);
1502 if (w->reg >= 0)
1503 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1504 " - R%d(0x%x) bit %d",
1505 w->reg, w->reg, w->shift);
1507 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1509 if (w->sname)
1510 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1511 w->sname,
1512 w->active ? "active" : "inactive");
1514 list_for_each_entry(p, &w->sources, list_sink) {
1515 if (p->connected && !p->connected(w, p->sink))
1516 continue;
1518 if (p->connect)
1519 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1520 " in \"%s\" \"%s\"\n",
1521 p->name ? p->name : "static",
1522 p->source->name);
1524 list_for_each_entry(p, &w->sinks, list_source) {
1525 if (p->connected && !p->connected(w, p->sink))
1526 continue;
1528 if (p->connect)
1529 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1530 " out \"%s\" \"%s\"\n",
1531 p->name ? p->name : "static",
1532 p->sink->name);
1535 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1537 kfree(buf);
1538 return ret;
1541 static const struct file_operations dapm_widget_power_fops = {
1542 .open = dapm_widget_power_open_file,
1543 .read = dapm_widget_power_read_file,
1544 .llseek = default_llseek,
1547 static int dapm_bias_open_file(struct inode *inode, struct file *file)
1549 file->private_data = inode->i_private;
1550 return 0;
1553 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1554 size_t count, loff_t *ppos)
1556 struct snd_soc_dapm_context *dapm = file->private_data;
1557 char *level;
1559 switch (dapm->bias_level) {
1560 case SND_SOC_BIAS_ON:
1561 level = "On\n";
1562 break;
1563 case SND_SOC_BIAS_PREPARE:
1564 level = "Prepare\n";
1565 break;
1566 case SND_SOC_BIAS_STANDBY:
1567 level = "Standby\n";
1568 break;
1569 case SND_SOC_BIAS_OFF:
1570 level = "Off\n";
1571 break;
1572 default:
1573 BUG();
1574 level = "Unknown\n";
1575 break;
1578 return simple_read_from_buffer(user_buf, count, ppos, level,
1579 strlen(level));
1582 static const struct file_operations dapm_bias_fops = {
1583 .open = dapm_bias_open_file,
1584 .read = dapm_bias_read_file,
1585 .llseek = default_llseek,
1588 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1589 struct dentry *parent)
1591 struct dentry *d;
1593 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1595 if (!dapm->debugfs_dapm) {
1596 printk(KERN_WARNING
1597 "Failed to create DAPM debugfs directory\n");
1598 return;
1601 d = debugfs_create_file("bias_level", 0444,
1602 dapm->debugfs_dapm, dapm,
1603 &dapm_bias_fops);
1604 if (!d)
1605 dev_warn(dapm->dev,
1606 "ASoC: Failed to create bias level debugfs file\n");
1609 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1611 struct snd_soc_dapm_context *dapm = w->dapm;
1612 struct dentry *d;
1614 if (!dapm->debugfs_dapm || !w->name)
1615 return;
1617 d = debugfs_create_file(w->name, 0444,
1618 dapm->debugfs_dapm, w,
1619 &dapm_widget_power_fops);
1620 if (!d)
1621 dev_warn(w->dapm->dev,
1622 "ASoC: Failed to create %s debugfs file\n",
1623 w->name);
1626 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1628 debugfs_remove_recursive(dapm->debugfs_dapm);
1631 #else
1632 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1633 struct dentry *parent)
1637 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1641 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1645 #endif
1647 /* test and update the power status of a mux widget */
1648 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1649 struct snd_kcontrol *kcontrol, int change,
1650 int mux, struct soc_enum *e)
1652 struct snd_soc_dapm_path *path;
1653 int found = 0;
1655 if (widget->id != snd_soc_dapm_mux &&
1656 widget->id != snd_soc_dapm_virt_mux &&
1657 widget->id != snd_soc_dapm_value_mux)
1658 return -ENODEV;
1660 if (!change)
1661 return 0;
1663 /* find dapm widget path assoc with kcontrol */
1664 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1665 if (path->kcontrol != kcontrol)
1666 continue;
1668 if (!path->name || !e->texts[mux])
1669 continue;
1671 found = 1;
1672 /* we now need to match the string in the enum to the path */
1673 if (!(strcmp(path->name, e->texts[mux]))) {
1674 path->connect = 1; /* new connection */
1675 dapm_mark_dirty(path->source, "mux connection");
1676 } else {
1677 if (path->connect)
1678 dapm_mark_dirty(path->source,
1679 "mux disconnection");
1680 path->connect = 0; /* old connection must be powered down */
1684 if (found) {
1685 dapm_mark_dirty(widget, "mux change");
1686 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1689 return 0;
1692 /* test and update the power status of a mixer or switch widget */
1693 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1694 struct snd_kcontrol *kcontrol, int connect)
1696 struct snd_soc_dapm_path *path;
1697 int found = 0;
1699 if (widget->id != snd_soc_dapm_mixer &&
1700 widget->id != snd_soc_dapm_mixer_named_ctl &&
1701 widget->id != snd_soc_dapm_switch)
1702 return -ENODEV;
1704 /* find dapm widget path assoc with kcontrol */
1705 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1706 if (path->kcontrol != kcontrol)
1707 continue;
1709 /* found, now check type */
1710 found = 1;
1711 path->connect = connect;
1712 dapm_mark_dirty(path->source, "mixer connection");
1715 if (found) {
1716 dapm_mark_dirty(widget, "mixer update");
1717 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1720 return 0;
1723 /* show dapm widget status in sys fs */
1724 static ssize_t dapm_widget_show(struct device *dev,
1725 struct device_attribute *attr, char *buf)
1727 struct snd_soc_pcm_runtime *rtd =
1728 container_of(dev, struct snd_soc_pcm_runtime, dev);
1729 struct snd_soc_codec *codec =rtd->codec;
1730 struct snd_soc_dapm_widget *w;
1731 int count = 0;
1732 char *state = "not set";
1734 list_for_each_entry(w, &codec->card->widgets, list) {
1735 if (w->dapm != &codec->dapm)
1736 continue;
1738 /* only display widgets that burnm power */
1739 switch (w->id) {
1740 case snd_soc_dapm_hp:
1741 case snd_soc_dapm_mic:
1742 case snd_soc_dapm_spk:
1743 case snd_soc_dapm_line:
1744 case snd_soc_dapm_micbias:
1745 case snd_soc_dapm_dac:
1746 case snd_soc_dapm_adc:
1747 case snd_soc_dapm_pga:
1748 case snd_soc_dapm_out_drv:
1749 case snd_soc_dapm_mixer:
1750 case snd_soc_dapm_mixer_named_ctl:
1751 case snd_soc_dapm_supply:
1752 if (w->name)
1753 count += sprintf(buf + count, "%s: %s\n",
1754 w->name, w->power ? "On":"Off");
1755 break;
1756 default:
1757 break;
1761 switch (codec->dapm.bias_level) {
1762 case SND_SOC_BIAS_ON:
1763 state = "On";
1764 break;
1765 case SND_SOC_BIAS_PREPARE:
1766 state = "Prepare";
1767 break;
1768 case SND_SOC_BIAS_STANDBY:
1769 state = "Standby";
1770 break;
1771 case SND_SOC_BIAS_OFF:
1772 state = "Off";
1773 break;
1775 count += sprintf(buf + count, "PM State: %s\n", state);
1777 return count;
1780 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1782 int snd_soc_dapm_sys_add(struct device *dev)
1784 return device_create_file(dev, &dev_attr_dapm_widget);
1787 static void snd_soc_dapm_sys_remove(struct device *dev)
1789 device_remove_file(dev, &dev_attr_dapm_widget);
1792 /* free all dapm widgets and resources */
1793 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1795 struct snd_soc_dapm_widget *w, *next_w;
1796 struct snd_soc_dapm_path *p, *next_p;
1798 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1799 if (w->dapm != dapm)
1800 continue;
1801 list_del(&w->list);
1803 * remove source and sink paths associated to this widget.
1804 * While removing the path, remove reference to it from both
1805 * source and sink widgets so that path is removed only once.
1807 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1808 list_del(&p->list_sink);
1809 list_del(&p->list_source);
1810 list_del(&p->list);
1811 kfree(p->long_name);
1812 kfree(p);
1814 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1815 list_del(&p->list_sink);
1816 list_del(&p->list_source);
1817 list_del(&p->list);
1818 kfree(p->long_name);
1819 kfree(p);
1821 kfree(w->kcontrols);
1822 kfree(w->name);
1823 kfree(w);
1827 static struct snd_soc_dapm_widget *dapm_find_widget(
1828 struct snd_soc_dapm_context *dapm, const char *pin,
1829 bool search_other_contexts)
1831 struct snd_soc_dapm_widget *w;
1832 struct snd_soc_dapm_widget *fallback = NULL;
1834 list_for_each_entry(w, &dapm->card->widgets, list) {
1835 if (!strcmp(w->name, pin)) {
1836 if (w->dapm == dapm)
1837 return w;
1838 else
1839 fallback = w;
1843 if (search_other_contexts)
1844 return fallback;
1846 return NULL;
1849 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1850 const char *pin, int status)
1852 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
1854 if (!w) {
1855 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1856 return -EINVAL;
1859 w->connected = status;
1860 if (status == 0)
1861 w->force = 0;
1862 dapm_mark_dirty(w, "pin configuration");
1864 return 0;
1868 * snd_soc_dapm_sync - scan and power dapm paths
1869 * @dapm: DAPM context
1871 * Walks all dapm audio paths and powers widgets according to their
1872 * stream or path usage.
1874 * Returns 0 for success.
1876 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1879 * Suppress early reports (eg, jacks syncing their state) to avoid
1880 * silly DAPM runs during card startup.
1882 if (!dapm->card || !dapm->card->instantiated)
1883 return 0;
1885 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1887 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1889 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1890 const struct snd_soc_dapm_route *route)
1892 struct snd_soc_dapm_path *path;
1893 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1894 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1895 const char *sink;
1896 const char *control = route->control;
1897 const char *source;
1898 char prefixed_sink[80];
1899 char prefixed_source[80];
1900 int ret = 0;
1902 if (dapm->codec && dapm->codec->name_prefix) {
1903 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1904 dapm->codec->name_prefix, route->sink);
1905 sink = prefixed_sink;
1906 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1907 dapm->codec->name_prefix, route->source);
1908 source = prefixed_source;
1909 } else {
1910 sink = route->sink;
1911 source = route->source;
1915 * find src and dest widgets over all widgets but favor a widget from
1916 * current DAPM context
1918 list_for_each_entry(w, &dapm->card->widgets, list) {
1919 if (!wsink && !(strcmp(w->name, sink))) {
1920 wtsink = w;
1921 if (w->dapm == dapm)
1922 wsink = w;
1923 continue;
1925 if (!wsource && !(strcmp(w->name, source))) {
1926 wtsource = w;
1927 if (w->dapm == dapm)
1928 wsource = w;
1931 /* use widget from another DAPM context if not found from this */
1932 if (!wsink)
1933 wsink = wtsink;
1934 if (!wsource)
1935 wsource = wtsource;
1937 if (wsource == NULL || wsink == NULL)
1938 return -ENODEV;
1940 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1941 if (!path)
1942 return -ENOMEM;
1944 path->source = wsource;
1945 path->sink = wsink;
1946 path->connected = route->connected;
1947 INIT_LIST_HEAD(&path->list);
1948 INIT_LIST_HEAD(&path->list_source);
1949 INIT_LIST_HEAD(&path->list_sink);
1951 /* check for external widgets */
1952 if (wsink->id == snd_soc_dapm_input) {
1953 if (wsource->id == snd_soc_dapm_micbias ||
1954 wsource->id == snd_soc_dapm_mic ||
1955 wsource->id == snd_soc_dapm_line ||
1956 wsource->id == snd_soc_dapm_output)
1957 wsink->ext = 1;
1959 if (wsource->id == snd_soc_dapm_output) {
1960 if (wsink->id == snd_soc_dapm_spk ||
1961 wsink->id == snd_soc_dapm_hp ||
1962 wsink->id == snd_soc_dapm_line ||
1963 wsink->id == snd_soc_dapm_input)
1964 wsource->ext = 1;
1967 /* connect static paths */
1968 if (control == NULL) {
1969 list_add(&path->list, &dapm->card->paths);
1970 list_add(&path->list_sink, &wsink->sources);
1971 list_add(&path->list_source, &wsource->sinks);
1972 path->connect = 1;
1973 return 0;
1976 /* connect dynamic paths */
1977 switch (wsink->id) {
1978 case snd_soc_dapm_adc:
1979 case snd_soc_dapm_dac:
1980 case snd_soc_dapm_pga:
1981 case snd_soc_dapm_out_drv:
1982 case snd_soc_dapm_input:
1983 case snd_soc_dapm_output:
1984 case snd_soc_dapm_micbias:
1985 case snd_soc_dapm_vmid:
1986 case snd_soc_dapm_pre:
1987 case snd_soc_dapm_post:
1988 case snd_soc_dapm_supply:
1989 case snd_soc_dapm_aif_in:
1990 case snd_soc_dapm_aif_out:
1991 list_add(&path->list, &dapm->card->paths);
1992 list_add(&path->list_sink, &wsink->sources);
1993 list_add(&path->list_source, &wsource->sinks);
1994 path->connect = 1;
1995 return 0;
1996 case snd_soc_dapm_mux:
1997 case snd_soc_dapm_virt_mux:
1998 case snd_soc_dapm_value_mux:
1999 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2000 &wsink->kcontrol_news[0]);
2001 if (ret != 0)
2002 goto err;
2003 break;
2004 case snd_soc_dapm_switch:
2005 case snd_soc_dapm_mixer:
2006 case snd_soc_dapm_mixer_named_ctl:
2007 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2008 if (ret != 0)
2009 goto err;
2010 break;
2011 case snd_soc_dapm_hp:
2012 case snd_soc_dapm_mic:
2013 case snd_soc_dapm_line:
2014 case snd_soc_dapm_spk:
2015 list_add(&path->list, &dapm->card->paths);
2016 list_add(&path->list_sink, &wsink->sources);
2017 list_add(&path->list_source, &wsource->sinks);
2018 path->connect = 0;
2019 return 0;
2021 return 0;
2023 err:
2024 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2025 source, control, sink);
2026 kfree(path);
2027 return ret;
2031 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2032 * @dapm: DAPM context
2033 * @route: audio routes
2034 * @num: number of routes
2036 * Connects 2 dapm widgets together via a named audio path. The sink is
2037 * the widget receiving the audio signal, whilst the source is the sender
2038 * of the audio signal.
2040 * Returns 0 for success else error. On error all resources can be freed
2041 * with a call to snd_soc_card_free().
2043 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2044 const struct snd_soc_dapm_route *route, int num)
2046 int i, ret;
2048 for (i = 0; i < num; i++) {
2049 ret = snd_soc_dapm_add_route(dapm, route);
2050 if (ret < 0) {
2051 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2052 route->source, route->sink);
2053 return ret;
2055 route++;
2058 return 0;
2060 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2062 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2063 const struct snd_soc_dapm_route *route)
2065 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2066 route->source,
2067 true);
2068 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2069 route->sink,
2070 true);
2071 struct snd_soc_dapm_path *path;
2072 int count = 0;
2074 if (!source) {
2075 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2076 route->source);
2077 return -ENODEV;
2080 if (!sink) {
2081 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2082 route->sink);
2083 return -ENODEV;
2086 if (route->control || route->connected)
2087 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2088 route->source, route->sink);
2090 list_for_each_entry(path, &source->sinks, list_source) {
2091 if (path->sink == sink) {
2092 path->weak = 1;
2093 count++;
2097 if (count == 0)
2098 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2099 route->source, route->sink);
2100 if (count > 1)
2101 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2102 count, route->source, route->sink);
2104 return 0;
2108 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2109 * @dapm: DAPM context
2110 * @route: audio routes
2111 * @num: number of routes
2113 * Mark existing routes matching those specified in the passed array
2114 * as being weak, meaning that they are ignored for the purpose of
2115 * power decisions. The main intended use case is for sidetone paths
2116 * which couple audio between other independent paths if they are both
2117 * active in order to make the combination work better at the user
2118 * level but which aren't intended to be "used".
2120 * Note that CODEC drivers should not use this as sidetone type paths
2121 * can frequently also be used as bypass paths.
2123 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2124 const struct snd_soc_dapm_route *route, int num)
2126 int i, err;
2127 int ret = 0;
2129 for (i = 0; i < num; i++) {
2130 err = snd_soc_dapm_weak_route(dapm, route);
2131 if (err)
2132 ret = err;
2133 route++;
2136 return ret;
2138 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2141 * snd_soc_dapm_new_widgets - add new dapm widgets
2142 * @dapm: DAPM context
2144 * Checks the codec for any new dapm widgets and creates them if found.
2146 * Returns 0 for success.
2148 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2150 struct snd_soc_dapm_widget *w;
2151 unsigned int val;
2153 list_for_each_entry(w, &dapm->card->widgets, list)
2155 if (w->new)
2156 continue;
2158 if (w->num_kcontrols) {
2159 w->kcontrols = kzalloc(w->num_kcontrols *
2160 sizeof(struct snd_kcontrol *),
2161 GFP_KERNEL);
2162 if (!w->kcontrols)
2163 return -ENOMEM;
2166 switch(w->id) {
2167 case snd_soc_dapm_switch:
2168 case snd_soc_dapm_mixer:
2169 case snd_soc_dapm_mixer_named_ctl:
2170 dapm_new_mixer(w);
2171 break;
2172 case snd_soc_dapm_mux:
2173 case snd_soc_dapm_virt_mux:
2174 case snd_soc_dapm_value_mux:
2175 dapm_new_mux(w);
2176 break;
2177 case snd_soc_dapm_pga:
2178 case snd_soc_dapm_out_drv:
2179 dapm_new_pga(w);
2180 break;
2181 default:
2182 break;
2185 /* Read the initial power state from the device */
2186 if (w->reg >= 0) {
2187 val = soc_widget_read(w, w->reg);
2188 val &= 1 << w->shift;
2189 if (w->invert)
2190 val = !val;
2192 if (val)
2193 w->power = 1;
2196 w->new = 1;
2198 dapm_mark_dirty(w, "new widget");
2199 dapm_debugfs_add_widget(w);
2202 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2203 return 0;
2205 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2208 * snd_soc_dapm_get_volsw - dapm mixer get callback
2209 * @kcontrol: mixer control
2210 * @ucontrol: control element information
2212 * Callback to get the value of a dapm mixer control.
2214 * Returns 0 for success.
2216 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2217 struct snd_ctl_elem_value *ucontrol)
2219 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2220 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2221 struct soc_mixer_control *mc =
2222 (struct soc_mixer_control *)kcontrol->private_value;
2223 unsigned int reg = mc->reg;
2224 unsigned int shift = mc->shift;
2225 unsigned int rshift = mc->rshift;
2226 int max = mc->max;
2227 unsigned int invert = mc->invert;
2228 unsigned int mask = (1 << fls(max)) - 1;
2230 ucontrol->value.integer.value[0] =
2231 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2232 if (shift != rshift)
2233 ucontrol->value.integer.value[1] =
2234 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2235 if (invert) {
2236 ucontrol->value.integer.value[0] =
2237 max - ucontrol->value.integer.value[0];
2238 if (shift != rshift)
2239 ucontrol->value.integer.value[1] =
2240 max - ucontrol->value.integer.value[1];
2243 return 0;
2245 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2248 * snd_soc_dapm_put_volsw - dapm mixer set callback
2249 * @kcontrol: mixer control
2250 * @ucontrol: control element information
2252 * Callback to set the value of a dapm mixer control.
2254 * Returns 0 for success.
2256 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2257 struct snd_ctl_elem_value *ucontrol)
2259 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2260 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2261 struct snd_soc_codec *codec = widget->codec;
2262 struct soc_mixer_control *mc =
2263 (struct soc_mixer_control *)kcontrol->private_value;
2264 unsigned int reg = mc->reg;
2265 unsigned int shift = mc->shift;
2266 int max = mc->max;
2267 unsigned int mask = (1 << fls(max)) - 1;
2268 unsigned int invert = mc->invert;
2269 unsigned int val;
2270 int connect, change;
2271 struct snd_soc_dapm_update update;
2272 int wi;
2274 val = (ucontrol->value.integer.value[0] & mask);
2276 if (invert)
2277 val = max - val;
2278 mask = mask << shift;
2279 val = val << shift;
2281 if (val)
2282 /* new connection */
2283 connect = invert ? 0 : 1;
2284 else
2285 /* old connection must be powered down */
2286 connect = invert ? 1 : 0;
2288 mutex_lock(&codec->mutex);
2290 change = snd_soc_test_bits(widget->codec, reg, mask, val);
2291 if (change) {
2292 for (wi = 0; wi < wlist->num_widgets; wi++) {
2293 widget = wlist->widgets[wi];
2295 widget->value = val;
2297 update.kcontrol = kcontrol;
2298 update.widget = widget;
2299 update.reg = reg;
2300 update.mask = mask;
2301 update.val = val;
2302 widget->dapm->update = &update;
2304 dapm_mixer_update_power(widget, kcontrol, connect);
2306 widget->dapm->update = NULL;
2310 mutex_unlock(&codec->mutex);
2311 return 0;
2313 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2316 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2317 * @kcontrol: mixer control
2318 * @ucontrol: control element information
2320 * Callback to get the value of a dapm enumerated double mixer control.
2322 * Returns 0 for success.
2324 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2325 struct snd_ctl_elem_value *ucontrol)
2327 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2328 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2329 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2330 unsigned int val, bitmask;
2332 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2334 val = snd_soc_read(widget->codec, e->reg);
2335 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2336 if (e->shift_l != e->shift_r)
2337 ucontrol->value.enumerated.item[1] =
2338 (val >> e->shift_r) & (bitmask - 1);
2340 return 0;
2342 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2345 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2346 * @kcontrol: mixer control
2347 * @ucontrol: control element information
2349 * Callback to set the value of a dapm enumerated double mixer control.
2351 * Returns 0 for success.
2353 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2354 struct snd_ctl_elem_value *ucontrol)
2356 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2357 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2358 struct snd_soc_codec *codec = widget->codec;
2359 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2360 unsigned int val, mux, change;
2361 unsigned int mask, bitmask;
2362 struct snd_soc_dapm_update update;
2363 int wi;
2365 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2367 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2368 return -EINVAL;
2369 mux = ucontrol->value.enumerated.item[0];
2370 val = mux << e->shift_l;
2371 mask = (bitmask - 1) << e->shift_l;
2372 if (e->shift_l != e->shift_r) {
2373 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2374 return -EINVAL;
2375 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2376 mask |= (bitmask - 1) << e->shift_r;
2379 mutex_lock(&codec->mutex);
2381 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2382 if (change) {
2383 for (wi = 0; wi < wlist->num_widgets; wi++) {
2384 widget = wlist->widgets[wi];
2386 widget->value = val;
2388 update.kcontrol = kcontrol;
2389 update.widget = widget;
2390 update.reg = e->reg;
2391 update.mask = mask;
2392 update.val = val;
2393 widget->dapm->update = &update;
2395 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2397 widget->dapm->update = NULL;
2401 mutex_unlock(&codec->mutex);
2402 return change;
2404 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2407 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2408 * @kcontrol: mixer control
2409 * @ucontrol: control element information
2411 * Returns 0 for success.
2413 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2414 struct snd_ctl_elem_value *ucontrol)
2416 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2417 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2419 ucontrol->value.enumerated.item[0] = widget->value;
2421 return 0;
2423 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2426 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2427 * @kcontrol: mixer control
2428 * @ucontrol: control element information
2430 * Returns 0 for success.
2432 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2433 struct snd_ctl_elem_value *ucontrol)
2435 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2436 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2437 struct snd_soc_codec *codec = widget->codec;
2438 struct soc_enum *e =
2439 (struct soc_enum *)kcontrol->private_value;
2440 int change;
2441 int ret = 0;
2442 int wi;
2444 if (ucontrol->value.enumerated.item[0] >= e->max)
2445 return -EINVAL;
2447 mutex_lock(&codec->mutex);
2449 change = widget->value != ucontrol->value.enumerated.item[0];
2450 if (change) {
2451 for (wi = 0; wi < wlist->num_widgets; wi++) {
2452 widget = wlist->widgets[wi];
2454 widget->value = ucontrol->value.enumerated.item[0];
2456 dapm_mux_update_power(widget, kcontrol, change,
2457 widget->value, e);
2461 mutex_unlock(&codec->mutex);
2462 return ret;
2464 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2467 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2468 * callback
2469 * @kcontrol: mixer control
2470 * @ucontrol: control element information
2472 * Callback to get the value of a dapm semi enumerated double mixer control.
2474 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2475 * used for handling bitfield coded enumeration for example.
2477 * Returns 0 for success.
2479 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2480 struct snd_ctl_elem_value *ucontrol)
2482 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2483 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2484 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2485 unsigned int reg_val, val, mux;
2487 reg_val = snd_soc_read(widget->codec, e->reg);
2488 val = (reg_val >> e->shift_l) & e->mask;
2489 for (mux = 0; mux < e->max; mux++) {
2490 if (val == e->values[mux])
2491 break;
2493 ucontrol->value.enumerated.item[0] = mux;
2494 if (e->shift_l != e->shift_r) {
2495 val = (reg_val >> e->shift_r) & e->mask;
2496 for (mux = 0; mux < e->max; mux++) {
2497 if (val == e->values[mux])
2498 break;
2500 ucontrol->value.enumerated.item[1] = mux;
2503 return 0;
2505 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2508 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2509 * callback
2510 * @kcontrol: mixer control
2511 * @ucontrol: control element information
2513 * Callback to set the value of a dapm semi enumerated double mixer control.
2515 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2516 * used for handling bitfield coded enumeration for example.
2518 * Returns 0 for success.
2520 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2521 struct snd_ctl_elem_value *ucontrol)
2523 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2524 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2525 struct snd_soc_codec *codec = widget->codec;
2526 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2527 unsigned int val, mux, change;
2528 unsigned int mask;
2529 struct snd_soc_dapm_update update;
2530 int wi;
2532 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2533 return -EINVAL;
2534 mux = ucontrol->value.enumerated.item[0];
2535 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2536 mask = e->mask << e->shift_l;
2537 if (e->shift_l != e->shift_r) {
2538 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2539 return -EINVAL;
2540 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2541 mask |= e->mask << e->shift_r;
2544 mutex_lock(&codec->mutex);
2546 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2547 if (change) {
2548 for (wi = 0; wi < wlist->num_widgets; wi++) {
2549 widget = wlist->widgets[wi];
2551 widget->value = val;
2553 update.kcontrol = kcontrol;
2554 update.widget = widget;
2555 update.reg = e->reg;
2556 update.mask = mask;
2557 update.val = val;
2558 widget->dapm->update = &update;
2560 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2562 widget->dapm->update = NULL;
2566 mutex_unlock(&codec->mutex);
2567 return change;
2569 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2572 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2574 * @kcontrol: mixer control
2575 * @uinfo: control element information
2577 * Callback to provide information about a pin switch control.
2579 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2580 struct snd_ctl_elem_info *uinfo)
2582 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2583 uinfo->count = 1;
2584 uinfo->value.integer.min = 0;
2585 uinfo->value.integer.max = 1;
2587 return 0;
2589 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2592 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2594 * @kcontrol: mixer control
2595 * @ucontrol: Value
2597 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2598 struct snd_ctl_elem_value *ucontrol)
2600 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2601 const char *pin = (const char *)kcontrol->private_value;
2603 mutex_lock(&codec->mutex);
2605 ucontrol->value.integer.value[0] =
2606 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2608 mutex_unlock(&codec->mutex);
2610 return 0;
2612 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2615 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2617 * @kcontrol: mixer control
2618 * @ucontrol: Value
2620 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2621 struct snd_ctl_elem_value *ucontrol)
2623 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2624 const char *pin = (const char *)kcontrol->private_value;
2626 mutex_lock(&codec->mutex);
2628 if (ucontrol->value.integer.value[0])
2629 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2630 else
2631 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2633 snd_soc_dapm_sync(&codec->dapm);
2635 mutex_unlock(&codec->mutex);
2637 return 0;
2639 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2642 * snd_soc_dapm_new_control - create new dapm control
2643 * @dapm: DAPM context
2644 * @widget: widget template
2646 * Creates a new dapm control based upon the template.
2648 * Returns 0 for success else error.
2650 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2651 const struct snd_soc_dapm_widget *widget)
2653 struct snd_soc_dapm_widget *w;
2654 size_t name_len;
2656 if ((w = dapm_cnew_widget(widget)) == NULL)
2657 return -ENOMEM;
2659 name_len = strlen(widget->name) + 1;
2660 if (dapm->codec && dapm->codec->name_prefix)
2661 name_len += 1 + strlen(dapm->codec->name_prefix);
2662 w->name = kmalloc(name_len, GFP_KERNEL);
2663 if (w->name == NULL) {
2664 kfree(w);
2665 return -ENOMEM;
2667 if (dapm->codec && dapm->codec->name_prefix)
2668 snprintf(w->name, name_len, "%s %s",
2669 dapm->codec->name_prefix, widget->name);
2670 else
2671 snprintf(w->name, name_len, "%s", widget->name);
2673 switch (w->id) {
2674 case snd_soc_dapm_switch:
2675 case snd_soc_dapm_mixer:
2676 case snd_soc_dapm_mixer_named_ctl:
2677 w->power_check = dapm_generic_check_power;
2678 break;
2679 case snd_soc_dapm_mux:
2680 case snd_soc_dapm_virt_mux:
2681 case snd_soc_dapm_value_mux:
2682 w->power_check = dapm_generic_check_power;
2683 break;
2684 case snd_soc_dapm_adc:
2685 case snd_soc_dapm_aif_out:
2686 w->power_check = dapm_adc_check_power;
2687 break;
2688 case snd_soc_dapm_dac:
2689 case snd_soc_dapm_aif_in:
2690 w->power_check = dapm_dac_check_power;
2691 break;
2692 case snd_soc_dapm_pga:
2693 case snd_soc_dapm_out_drv:
2694 case snd_soc_dapm_input:
2695 case snd_soc_dapm_output:
2696 case snd_soc_dapm_micbias:
2697 case snd_soc_dapm_spk:
2698 case snd_soc_dapm_hp:
2699 case snd_soc_dapm_mic:
2700 case snd_soc_dapm_line:
2701 w->power_check = dapm_generic_check_power;
2702 break;
2703 case snd_soc_dapm_supply:
2704 w->power_check = dapm_supply_check_power;
2705 break;
2706 default:
2707 w->power_check = dapm_always_on_check_power;
2708 break;
2711 dapm->n_widgets++;
2712 w->dapm = dapm;
2713 w->codec = dapm->codec;
2714 w->platform = dapm->platform;
2715 INIT_LIST_HEAD(&w->sources);
2716 INIT_LIST_HEAD(&w->sinks);
2717 INIT_LIST_HEAD(&w->list);
2718 INIT_LIST_HEAD(&w->dirty);
2719 list_add(&w->list, &dapm->card->widgets);
2721 /* machine layer set ups unconnected pins and insertions */
2722 w->connected = 1;
2723 return 0;
2725 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2728 * snd_soc_dapm_new_controls - create new dapm controls
2729 * @dapm: DAPM context
2730 * @widget: widget array
2731 * @num: number of widgets
2733 * Creates new DAPM controls based upon the templates.
2735 * Returns 0 for success else error.
2737 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2738 const struct snd_soc_dapm_widget *widget,
2739 int num)
2741 int i, ret;
2743 for (i = 0; i < num; i++) {
2744 ret = snd_soc_dapm_new_control(dapm, widget);
2745 if (ret < 0) {
2746 dev_err(dapm->dev,
2747 "ASoC: Failed to create DAPM control %s: %d\n",
2748 widget->name, ret);
2749 return ret;
2751 widget++;
2753 return 0;
2755 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2757 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2758 const char *stream, int event)
2760 struct snd_soc_dapm_widget *w;
2762 list_for_each_entry(w, &dapm->card->widgets, list)
2764 if (!w->sname || w->dapm != dapm)
2765 continue;
2766 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2767 w->name, w->sname, stream, event);
2768 if (strstr(w->sname, stream)) {
2769 dapm_mark_dirty(w, "stream event");
2770 switch(event) {
2771 case SND_SOC_DAPM_STREAM_START:
2772 w->active = 1;
2773 break;
2774 case SND_SOC_DAPM_STREAM_STOP:
2775 w->active = 0;
2776 break;
2777 case SND_SOC_DAPM_STREAM_SUSPEND:
2778 case SND_SOC_DAPM_STREAM_RESUME:
2779 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2780 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2781 break;
2786 dapm_power_widgets(dapm, event);
2788 /* do we need to notify any clients that DAPM stream is complete */
2789 if (dapm->stream_event)
2790 dapm->stream_event(dapm, event);
2794 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2795 * @rtd: PCM runtime data
2796 * @stream: stream name
2797 * @event: stream event
2799 * Sends a stream event to the dapm core. The core then makes any
2800 * necessary widget power changes.
2802 * Returns 0 for success else error.
2804 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2805 const char *stream, int event)
2807 struct snd_soc_codec *codec = rtd->codec;
2809 if (stream == NULL)
2810 return 0;
2812 mutex_lock(&codec->mutex);
2813 soc_dapm_stream_event(&codec->dapm, stream, event);
2814 mutex_unlock(&codec->mutex);
2815 return 0;
2819 * snd_soc_dapm_enable_pin - enable pin.
2820 * @dapm: DAPM context
2821 * @pin: pin name
2823 * Enables input/output pin and its parents or children widgets iff there is
2824 * a valid audio route and active audio stream.
2825 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2826 * do any widget power switching.
2828 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2830 return snd_soc_dapm_set_pin(dapm, pin, 1);
2832 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2835 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2836 * @dapm: DAPM context
2837 * @pin: pin name
2839 * Enables input/output pin regardless of any other state. This is
2840 * intended for use with microphone bias supplies used in microphone
2841 * jack detection.
2843 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2844 * do any widget power switching.
2846 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2847 const char *pin)
2849 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2851 if (!w) {
2852 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2853 return -EINVAL;
2856 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2857 w->connected = 1;
2858 w->force = 1;
2859 dapm_mark_dirty(w, "force enable");
2861 return 0;
2863 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2866 * snd_soc_dapm_disable_pin - disable pin.
2867 * @dapm: DAPM context
2868 * @pin: pin name
2870 * Disables input/output pin and its parents or children widgets.
2871 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2872 * do any widget power switching.
2874 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2875 const char *pin)
2877 return snd_soc_dapm_set_pin(dapm, pin, 0);
2879 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2882 * snd_soc_dapm_nc_pin - permanently disable pin.
2883 * @dapm: DAPM context
2884 * @pin: pin name
2886 * Marks the specified pin as being not connected, disabling it along
2887 * any parent or child widgets. At present this is identical to
2888 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2889 * additional things such as disabling controls which only affect
2890 * paths through the pin.
2892 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2893 * do any widget power switching.
2895 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2897 return snd_soc_dapm_set_pin(dapm, pin, 0);
2899 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2902 * snd_soc_dapm_get_pin_status - get audio pin status
2903 * @dapm: DAPM context
2904 * @pin: audio signal pin endpoint (or start point)
2906 * Get audio pin status - connected or disconnected.
2908 * Returns 1 for connected otherwise 0.
2910 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2911 const char *pin)
2913 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2915 if (w)
2916 return w->connected;
2918 return 0;
2920 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2923 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2924 * @dapm: DAPM context
2925 * @pin: audio signal pin endpoint (or start point)
2927 * Mark the given endpoint or pin as ignoring suspend. When the
2928 * system is disabled a path between two endpoints flagged as ignoring
2929 * suspend will not be disabled. The path must already be enabled via
2930 * normal means at suspend time, it will not be turned on if it was not
2931 * already enabled.
2933 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2934 const char *pin)
2936 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
2938 if (!w) {
2939 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2940 return -EINVAL;
2943 w->ignore_suspend = 1;
2945 return 0;
2947 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2950 * snd_soc_dapm_free - free dapm resources
2951 * @dapm: DAPM context
2953 * Free all dapm widgets and resources.
2955 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2957 snd_soc_dapm_sys_remove(dapm->dev);
2958 dapm_debugfs_cleanup(dapm);
2959 dapm_free_widgets(dapm);
2960 list_del(&dapm->list);
2962 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2964 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
2966 struct snd_soc_dapm_widget *w;
2967 LIST_HEAD(down_list);
2968 int powerdown = 0;
2970 list_for_each_entry(w, &dapm->card->widgets, list) {
2971 if (w->dapm != dapm)
2972 continue;
2973 if (w->power) {
2974 dapm_seq_insert(w, &down_list, false);
2975 w->power = 0;
2976 powerdown = 1;
2980 /* If there were no widgets to power down we're already in
2981 * standby.
2983 if (powerdown) {
2984 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
2985 dapm_seq_run(dapm, &down_list, 0, false);
2986 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
2991 * snd_soc_dapm_shutdown - callback for system shutdown
2993 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2995 struct snd_soc_codec *codec;
2997 list_for_each_entry(codec, &card->codec_dev_list, list) {
2998 soc_dapm_shutdown_codec(&codec->dapm);
2999 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
3003 /* Module information */
3004 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3005 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3006 MODULE_LICENSE("GPL");