MIPS: Yosemite, Emma: Fix off-by-two in arcs_cmdline buffer size check
[linux-2.6/linux-mips.git] / sound / soc / soc-dapm.c
blobf42e8b9fb17db7baeabaf7152f81123773cdecd0
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 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);
135 EXPORT_SYMBOL_GPL(dapm_mark_dirty);
137 /* create a new dapm widget */
138 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
139 const struct snd_soc_dapm_widget *_widget)
141 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
144 /* get snd_card from DAPM context */
145 static inline struct snd_card *dapm_get_snd_card(
146 struct snd_soc_dapm_context *dapm)
148 if (dapm->codec)
149 return dapm->codec->card->snd_card;
150 else if (dapm->platform)
151 return dapm->platform->card->snd_card;
152 else
153 BUG();
155 /* unreachable */
156 return NULL;
159 /* get soc_card from DAPM context */
160 static inline struct snd_soc_card *dapm_get_soc_card(
161 struct snd_soc_dapm_context *dapm)
163 if (dapm->codec)
164 return dapm->codec->card;
165 else if (dapm->platform)
166 return dapm->platform->card;
167 else
168 BUG();
170 /* unreachable */
171 return NULL;
174 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
176 if (w->codec)
177 return snd_soc_read(w->codec, reg);
178 else if (w->platform)
179 return snd_soc_platform_read(w->platform, reg);
181 dev_err(w->dapm->dev, "no valid widget read method\n");
182 return -1;
185 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
187 if (w->codec)
188 return snd_soc_write(w->codec, reg, val);
189 else if (w->platform)
190 return snd_soc_platform_write(w->platform, reg, val);
192 dev_err(w->dapm->dev, "no valid widget write method\n");
193 return -1;
196 static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
197 unsigned short reg, unsigned int mask, unsigned int value)
199 int change;
200 unsigned int old, new;
201 int ret;
203 ret = soc_widget_read(w, reg);
204 if (ret < 0)
205 return ret;
207 old = ret;
208 new = (old & ~mask) | (value & mask);
209 change = old != new;
210 if (change) {
211 ret = soc_widget_write(w, reg, new);
212 if (ret < 0)
213 return ret;
216 return change;
220 * snd_soc_dapm_set_bias_level - set the bias level for the system
221 * @dapm: DAPM context
222 * @level: level to configure
224 * Configure the bias (power) levels for the SoC audio device.
226 * Returns 0 for success else error.
228 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
229 enum snd_soc_bias_level level)
231 struct snd_soc_card *card = dapm->card;
232 int ret = 0;
234 trace_snd_soc_bias_level_start(card, level);
236 if (card && card->set_bias_level)
237 ret = card->set_bias_level(card, dapm, level);
238 if (ret != 0)
239 goto out;
241 if (dapm->codec) {
242 if (dapm->codec->driver->set_bias_level)
243 ret = dapm->codec->driver->set_bias_level(dapm->codec,
244 level);
245 else
246 dapm->bias_level = level;
248 if (ret != 0)
249 goto out;
251 if (card && card->set_bias_level_post)
252 ret = card->set_bias_level_post(card, dapm, level);
253 out:
254 trace_snd_soc_bias_level_done(card, level);
256 return ret;
259 /* set up initial codec paths */
260 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
261 struct snd_soc_dapm_path *p, int i)
263 switch (w->id) {
264 case snd_soc_dapm_switch:
265 case snd_soc_dapm_mixer:
266 case snd_soc_dapm_mixer_named_ctl: {
267 int val;
268 struct soc_mixer_control *mc = (struct soc_mixer_control *)
269 w->kcontrol_news[i].private_value;
270 unsigned int reg = mc->reg;
271 unsigned int shift = mc->shift;
272 int max = mc->max;
273 unsigned int mask = (1 << fls(max)) - 1;
274 unsigned int invert = mc->invert;
276 val = soc_widget_read(w, reg);
277 val = (val >> shift) & mask;
279 if ((invert && !val) || (!invert && val))
280 p->connect = 1;
281 else
282 p->connect = 0;
284 break;
285 case snd_soc_dapm_mux: {
286 struct soc_enum *e = (struct soc_enum *)
287 w->kcontrol_news[i].private_value;
288 int val, item, bitmask;
290 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
292 val = soc_widget_read(w, e->reg);
293 item = (val >> e->shift_l) & (bitmask - 1);
295 p->connect = 0;
296 for (i = 0; i < e->max; i++) {
297 if (!(strcmp(p->name, e->texts[i])) && item == i)
298 p->connect = 1;
301 break;
302 case snd_soc_dapm_virt_mux: {
303 struct soc_enum *e = (struct soc_enum *)
304 w->kcontrol_news[i].private_value;
306 p->connect = 0;
307 /* since a virtual mux has no backing registers to
308 * decide which path to connect, it will try to match
309 * with the first enumeration. This is to ensure
310 * that the default mux choice (the first) will be
311 * correctly powered up during initialization.
313 if (!strcmp(p->name, e->texts[0]))
314 p->connect = 1;
316 break;
317 case snd_soc_dapm_value_mux: {
318 struct soc_enum *e = (struct soc_enum *)
319 w->kcontrol_news[i].private_value;
320 int val, item;
322 val = soc_widget_read(w, e->reg);
323 val = (val >> e->shift_l) & e->mask;
324 for (item = 0; item < e->max; item++) {
325 if (val == e->values[item])
326 break;
329 p->connect = 0;
330 for (i = 0; i < e->max; i++) {
331 if (!(strcmp(p->name, e->texts[i])) && item == i)
332 p->connect = 1;
335 break;
336 /* does not affect routing - always connected */
337 case snd_soc_dapm_pga:
338 case snd_soc_dapm_out_drv:
339 case snd_soc_dapm_output:
340 case snd_soc_dapm_adc:
341 case snd_soc_dapm_input:
342 case snd_soc_dapm_dac:
343 case snd_soc_dapm_micbias:
344 case snd_soc_dapm_vmid:
345 case snd_soc_dapm_supply:
346 case snd_soc_dapm_aif_in:
347 case snd_soc_dapm_aif_out:
348 case snd_soc_dapm_hp:
349 case snd_soc_dapm_mic:
350 case snd_soc_dapm_spk:
351 case snd_soc_dapm_line:
352 p->connect = 1;
353 break;
354 /* does affect routing - dynamically connected */
355 case snd_soc_dapm_pre:
356 case snd_soc_dapm_post:
357 p->connect = 0;
358 break;
362 /* connect mux widget to its interconnecting audio paths */
363 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
364 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
365 struct snd_soc_dapm_path *path, const char *control_name,
366 const struct snd_kcontrol_new *kcontrol)
368 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
369 int i;
371 for (i = 0; i < e->max; i++) {
372 if (!(strcmp(control_name, e->texts[i]))) {
373 list_add(&path->list, &dapm->card->paths);
374 list_add(&path->list_sink, &dest->sources);
375 list_add(&path->list_source, &src->sinks);
376 path->name = (char*)e->texts[i];
377 dapm_set_path_status(dest, path, 0);
378 return 0;
382 return -ENODEV;
385 /* connect mixer widget to its interconnecting audio paths */
386 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
387 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
388 struct snd_soc_dapm_path *path, const char *control_name)
390 int i;
392 /* search for mixer kcontrol */
393 for (i = 0; i < dest->num_kcontrols; i++) {
394 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
395 list_add(&path->list, &dapm->card->paths);
396 list_add(&path->list_sink, &dest->sources);
397 list_add(&path->list_source, &src->sinks);
398 path->name = dest->kcontrol_news[i].name;
399 dapm_set_path_status(dest, path, i);
400 return 0;
403 return -ENODEV;
406 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
407 struct snd_soc_dapm_widget *kcontrolw,
408 const struct snd_kcontrol_new *kcontrol_new,
409 struct snd_kcontrol **kcontrol)
411 struct snd_soc_dapm_widget *w;
412 int i;
414 *kcontrol = NULL;
416 list_for_each_entry(w, &dapm->card->widgets, list) {
417 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
418 continue;
419 for (i = 0; i < w->num_kcontrols; i++) {
420 if (&w->kcontrol_news[i] == kcontrol_new) {
421 if (w->kcontrols)
422 *kcontrol = w->kcontrols[i];
423 return 1;
428 return 0;
431 /* create new dapm mixer control */
432 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
434 struct snd_soc_dapm_context *dapm = w->dapm;
435 int i, ret = 0;
436 size_t name_len, prefix_len;
437 struct snd_soc_dapm_path *path;
438 struct snd_card *card = dapm->card->snd_card;
439 const char *prefix;
440 struct snd_soc_dapm_widget_list *wlist;
441 size_t wlistsize;
443 if (dapm->codec)
444 prefix = dapm->codec->name_prefix;
445 else
446 prefix = NULL;
448 if (prefix)
449 prefix_len = strlen(prefix) + 1;
450 else
451 prefix_len = 0;
453 /* add kcontrol */
454 for (i = 0; i < w->num_kcontrols; i++) {
456 /* match name */
457 list_for_each_entry(path, &w->sources, list_sink) {
459 /* mixer/mux paths name must match control name */
460 if (path->name != (char *)w->kcontrol_news[i].name)
461 continue;
463 if (w->kcontrols[i]) {
464 path->kcontrol = w->kcontrols[i];
465 continue;
468 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
469 sizeof(struct snd_soc_dapm_widget *),
470 wlist = kzalloc(wlistsize, GFP_KERNEL);
471 if (wlist == NULL) {
472 dev_err(dapm->dev,
473 "asoc: can't allocate widget list for %s\n",
474 w->name);
475 return -ENOMEM;
477 wlist->num_widgets = 1;
478 wlist->widgets[0] = w;
480 /* add dapm control with long name.
481 * for dapm_mixer this is the concatenation of the
482 * mixer and kcontrol name.
483 * for dapm_mixer_named_ctl this is simply the
484 * kcontrol name.
486 name_len = strlen(w->kcontrol_news[i].name) + 1;
487 if (w->id != snd_soc_dapm_mixer_named_ctl)
488 name_len += 1 + strlen(w->name);
490 path->long_name = kmalloc(name_len, GFP_KERNEL);
492 if (path->long_name == NULL) {
493 kfree(wlist);
494 return -ENOMEM;
497 switch (w->id) {
498 default:
499 /* The control will get a prefix from
500 * the control creation process but
501 * we're also using the same prefix
502 * for widgets so cut the prefix off
503 * the front of the widget name.
505 snprintf(path->long_name, name_len, "%s %s",
506 w->name + prefix_len,
507 w->kcontrol_news[i].name);
508 break;
509 case snd_soc_dapm_mixer_named_ctl:
510 snprintf(path->long_name, name_len, "%s",
511 w->kcontrol_news[i].name);
512 break;
515 path->long_name[name_len - 1] = '\0';
517 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
518 wlist, path->long_name,
519 prefix);
520 ret = snd_ctl_add(card, path->kcontrol);
521 if (ret < 0) {
522 dev_err(dapm->dev,
523 "asoc: failed to add dapm kcontrol %s: %d\n",
524 path->long_name, ret);
525 kfree(wlist);
526 kfree(path->long_name);
527 path->long_name = NULL;
528 return ret;
530 w->kcontrols[i] = path->kcontrol;
533 return ret;
536 /* create new dapm mux control */
537 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
539 struct snd_soc_dapm_context *dapm = w->dapm;
540 struct snd_soc_dapm_path *path = NULL;
541 struct snd_kcontrol *kcontrol;
542 struct snd_card *card = dapm->card->snd_card;
543 const char *prefix;
544 size_t prefix_len;
545 int ret;
546 struct snd_soc_dapm_widget_list *wlist;
547 int shared, wlistentries;
548 size_t wlistsize;
549 char *name;
551 if (w->num_kcontrols != 1) {
552 dev_err(dapm->dev,
553 "asoc: mux %s has incorrect number of controls\n",
554 w->name);
555 return -EINVAL;
558 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
559 &kcontrol);
560 if (kcontrol) {
561 wlist = kcontrol->private_data;
562 wlistentries = wlist->num_widgets + 1;
563 } else {
564 wlist = NULL;
565 wlistentries = 1;
567 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
568 wlistentries * sizeof(struct snd_soc_dapm_widget *),
569 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
570 if (wlist == NULL) {
571 dev_err(dapm->dev,
572 "asoc: can't allocate widget list for %s\n", w->name);
573 return -ENOMEM;
575 wlist->num_widgets = wlistentries;
576 wlist->widgets[wlistentries - 1] = w;
578 if (!kcontrol) {
579 if (dapm->codec)
580 prefix = dapm->codec->name_prefix;
581 else
582 prefix = NULL;
584 if (shared) {
585 name = w->kcontrol_news[0].name;
586 prefix_len = 0;
587 } else {
588 name = w->name;
589 if (prefix)
590 prefix_len = strlen(prefix) + 1;
591 else
592 prefix_len = 0;
596 * The control will get a prefix from the control creation
597 * process but we're also using the same prefix for widgets so
598 * cut the prefix off the front of the widget name.
600 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
601 name + prefix_len, prefix);
602 ret = snd_ctl_add(card, kcontrol);
603 if (ret < 0) {
604 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
605 w->name, ret);
606 kfree(wlist);
607 return ret;
611 kcontrol->private_data = wlist;
613 w->kcontrols[0] = kcontrol;
615 list_for_each_entry(path, &w->sources, list_sink)
616 path->kcontrol = kcontrol;
618 return 0;
621 /* create new dapm volume control */
622 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
624 if (w->num_kcontrols)
625 dev_err(w->dapm->dev,
626 "asoc: PGA controls not supported: '%s'\n", w->name);
628 return 0;
631 /* reset 'walked' bit for each dapm path */
632 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
634 struct snd_soc_dapm_path *p;
636 list_for_each_entry(p, &dapm->card->paths, list)
637 p->walked = 0;
640 /* We implement power down on suspend by checking the power state of
641 * the ALSA card - when we are suspending the ALSA state for the card
642 * is set to D3.
644 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
646 int level = snd_power_get_state(widget->dapm->card->snd_card);
648 switch (level) {
649 case SNDRV_CTL_POWER_D3hot:
650 case SNDRV_CTL_POWER_D3cold:
651 if (widget->ignore_suspend)
652 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
653 widget->name);
654 return widget->ignore_suspend;
655 default:
656 return 1;
661 * Recursively check for a completed path to an active or physically connected
662 * output widget. Returns number of complete paths.
664 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
666 struct snd_soc_dapm_path *path;
667 int con = 0;
669 if (widget->outputs >= 0)
670 return widget->outputs;
672 DAPM_UPDATE_STAT(widget, path_checks);
674 if (widget->id == snd_soc_dapm_supply)
675 return 0;
677 switch (widget->id) {
678 case snd_soc_dapm_adc:
679 case snd_soc_dapm_aif_out:
680 if (widget->active) {
681 widget->outputs = snd_soc_dapm_suspend_check(widget);
682 return widget->outputs;
684 default:
685 break;
688 if (widget->connected) {
689 /* connected pin ? */
690 if (widget->id == snd_soc_dapm_output && !widget->ext) {
691 widget->outputs = snd_soc_dapm_suspend_check(widget);
692 return widget->outputs;
695 /* connected jack or spk ? */
696 if (widget->id == snd_soc_dapm_hp ||
697 widget->id == snd_soc_dapm_spk ||
698 (widget->id == snd_soc_dapm_line &&
699 !list_empty(&widget->sources))) {
700 widget->outputs = snd_soc_dapm_suspend_check(widget);
701 return widget->outputs;
705 list_for_each_entry(path, &widget->sinks, list_source) {
706 DAPM_UPDATE_STAT(widget, neighbour_checks);
708 if (path->weak)
709 continue;
711 if (path->walked)
712 continue;
714 if (path->sink && path->connect) {
715 path->walked = 1;
716 con += is_connected_output_ep(path->sink);
720 widget->outputs = con;
722 return con;
726 * Recursively check for a completed path to an active or physically connected
727 * input widget. Returns number of complete paths.
729 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
731 struct snd_soc_dapm_path *path;
732 int con = 0;
734 if (widget->inputs >= 0)
735 return widget->inputs;
737 DAPM_UPDATE_STAT(widget, path_checks);
739 if (widget->id == snd_soc_dapm_supply)
740 return 0;
742 /* active stream ? */
743 switch (widget->id) {
744 case snd_soc_dapm_dac:
745 case snd_soc_dapm_aif_in:
746 if (widget->active) {
747 widget->inputs = snd_soc_dapm_suspend_check(widget);
748 return widget->inputs;
750 default:
751 break;
754 if (widget->connected) {
755 /* connected pin ? */
756 if (widget->id == snd_soc_dapm_input && !widget->ext) {
757 widget->inputs = snd_soc_dapm_suspend_check(widget);
758 return widget->inputs;
761 /* connected VMID/Bias for lower pops */
762 if (widget->id == snd_soc_dapm_vmid) {
763 widget->inputs = snd_soc_dapm_suspend_check(widget);
764 return widget->inputs;
767 /* connected jack ? */
768 if (widget->id == snd_soc_dapm_mic ||
769 (widget->id == snd_soc_dapm_line &&
770 !list_empty(&widget->sinks))) {
771 widget->inputs = snd_soc_dapm_suspend_check(widget);
772 return widget->inputs;
777 list_for_each_entry(path, &widget->sources, list_sink) {
778 DAPM_UPDATE_STAT(widget, neighbour_checks);
780 if (path->weak)
781 continue;
783 if (path->walked)
784 continue;
786 if (path->source && path->connect) {
787 path->walked = 1;
788 con += is_connected_input_ep(path->source);
792 widget->inputs = con;
794 return con;
798 * Handler for generic register modifier widget.
800 int dapm_reg_event(struct snd_soc_dapm_widget *w,
801 struct snd_kcontrol *kcontrol, int event)
803 unsigned int val;
805 if (SND_SOC_DAPM_EVENT_ON(event))
806 val = w->on_val;
807 else
808 val = w->off_val;
810 soc_widget_update_bits(w, -(w->reg + 1),
811 w->mask << w->shift, val << w->shift);
813 return 0;
815 EXPORT_SYMBOL_GPL(dapm_reg_event);
817 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
819 if (w->power_checked)
820 return w->new_power;
822 if (w->force)
823 w->new_power = 1;
824 else
825 w->new_power = w->power_check(w);
827 w->power_checked = true;
829 return w->new_power;
832 /* Generic check to see if a widget should be powered.
834 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
836 int in, out;
838 DAPM_UPDATE_STAT(w, power_checks);
840 in = is_connected_input_ep(w);
841 dapm_clear_walk(w->dapm);
842 out = is_connected_output_ep(w);
843 dapm_clear_walk(w->dapm);
844 return out != 0 && in != 0;
847 /* Check to see if an ADC has power */
848 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
850 int in;
852 DAPM_UPDATE_STAT(w, power_checks);
854 if (w->active) {
855 in = is_connected_input_ep(w);
856 dapm_clear_walk(w->dapm);
857 return in != 0;
858 } else {
859 return dapm_generic_check_power(w);
863 /* Check to see if a DAC has power */
864 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
866 int out;
868 DAPM_UPDATE_STAT(w, power_checks);
870 if (w->active) {
871 out = is_connected_output_ep(w);
872 dapm_clear_walk(w->dapm);
873 return out != 0;
874 } else {
875 return dapm_generic_check_power(w);
879 /* Check to see if a power supply is needed */
880 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
882 struct snd_soc_dapm_path *path;
884 DAPM_UPDATE_STAT(w, power_checks);
886 /* Check if one of our outputs is connected */
887 list_for_each_entry(path, &w->sinks, list_source) {
888 DAPM_UPDATE_STAT(w, neighbour_checks);
890 if (path->weak)
891 continue;
893 if (path->connected &&
894 !path->connected(path->source, path->sink))
895 continue;
897 if (!path->sink)
898 continue;
900 if (dapm_widget_power_check(path->sink))
901 return 1;
904 dapm_clear_walk(w->dapm);
906 return 0;
909 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
911 return 1;
914 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
915 struct snd_soc_dapm_widget *b,
916 bool power_up)
918 int *sort;
920 if (power_up)
921 sort = dapm_up_seq;
922 else
923 sort = dapm_down_seq;
925 if (sort[a->id] != sort[b->id])
926 return sort[a->id] - sort[b->id];
927 if (a->subseq != b->subseq) {
928 if (power_up)
929 return a->subseq - b->subseq;
930 else
931 return b->subseq - a->subseq;
933 if (a->reg != b->reg)
934 return a->reg - b->reg;
935 if (a->dapm != b->dapm)
936 return (unsigned long)a->dapm - (unsigned long)b->dapm;
938 return 0;
941 /* Insert a widget in order into a DAPM power sequence. */
942 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
943 struct list_head *list,
944 bool power_up)
946 struct snd_soc_dapm_widget *w;
948 list_for_each_entry(w, list, power_list)
949 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
950 list_add_tail(&new_widget->power_list, &w->power_list);
951 return;
954 list_add_tail(&new_widget->power_list, list);
957 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
958 struct snd_soc_dapm_widget *w, int event)
960 struct snd_soc_card *card = dapm->card;
961 const char *ev_name;
962 int power, ret;
964 switch (event) {
965 case SND_SOC_DAPM_PRE_PMU:
966 ev_name = "PRE_PMU";
967 power = 1;
968 break;
969 case SND_SOC_DAPM_POST_PMU:
970 ev_name = "POST_PMU";
971 power = 1;
972 break;
973 case SND_SOC_DAPM_PRE_PMD:
974 ev_name = "PRE_PMD";
975 power = 0;
976 break;
977 case SND_SOC_DAPM_POST_PMD:
978 ev_name = "POST_PMD";
979 power = 0;
980 break;
981 default:
982 BUG();
983 return;
986 if (w->power != power)
987 return;
989 if (w->event && (w->event_flags & event)) {
990 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
991 w->name, ev_name);
992 trace_snd_soc_dapm_widget_event_start(w, event);
993 ret = w->event(w, NULL, event);
994 trace_snd_soc_dapm_widget_event_done(w, event);
995 if (ret < 0)
996 pr_err("%s: %s event failed: %d\n",
997 ev_name, w->name, ret);
1001 /* Apply the coalesced changes from a DAPM sequence */
1002 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
1003 struct list_head *pending)
1005 struct snd_soc_card *card = dapm->card;
1006 struct snd_soc_dapm_widget *w;
1007 int reg, power;
1008 unsigned int value = 0;
1009 unsigned int mask = 0;
1010 unsigned int cur_mask;
1012 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1013 power_list)->reg;
1015 list_for_each_entry(w, pending, power_list) {
1016 cur_mask = 1 << w->shift;
1017 BUG_ON(reg != w->reg);
1019 if (w->invert)
1020 power = !w->power;
1021 else
1022 power = w->power;
1024 mask |= cur_mask;
1025 if (power)
1026 value |= cur_mask;
1028 pop_dbg(dapm->dev, card->pop_time,
1029 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1030 w->name, reg, value, mask);
1032 /* Check for events */
1033 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1034 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1037 if (reg >= 0) {
1038 /* Any widget will do, they should all be updating the
1039 * same register.
1041 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1042 power_list);
1044 pop_dbg(dapm->dev, card->pop_time,
1045 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1046 value, mask, reg, card->pop_time);
1047 pop_wait(card->pop_time);
1048 soc_widget_update_bits(w, reg, mask, value);
1051 list_for_each_entry(w, pending, power_list) {
1052 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1053 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1057 /* Apply a DAPM power sequence.
1059 * We walk over a pre-sorted list of widgets to apply power to. In
1060 * order to minimise the number of writes to the device required
1061 * multiple widgets will be updated in a single write where possible.
1062 * Currently anything that requires more than a single write is not
1063 * handled.
1065 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1066 struct list_head *list, int event, bool power_up)
1068 struct snd_soc_dapm_widget *w, *n;
1069 LIST_HEAD(pending);
1070 int cur_sort = -1;
1071 int cur_subseq = -1;
1072 int cur_reg = SND_SOC_NOPM;
1073 struct snd_soc_dapm_context *cur_dapm = NULL;
1074 int ret, i;
1075 int *sort;
1077 if (power_up)
1078 sort = dapm_up_seq;
1079 else
1080 sort = dapm_down_seq;
1082 list_for_each_entry_safe(w, n, list, power_list) {
1083 ret = 0;
1085 /* Do we need to apply any queued changes? */
1086 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1087 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1088 if (!list_empty(&pending))
1089 dapm_seq_run_coalesced(cur_dapm, &pending);
1091 if (cur_dapm && cur_dapm->seq_notifier) {
1092 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1093 if (sort[i] == cur_sort)
1094 cur_dapm->seq_notifier(cur_dapm,
1096 cur_subseq);
1099 INIT_LIST_HEAD(&pending);
1100 cur_sort = -1;
1101 cur_subseq = INT_MIN;
1102 cur_reg = SND_SOC_NOPM;
1103 cur_dapm = NULL;
1106 switch (w->id) {
1107 case snd_soc_dapm_pre:
1108 if (!w->event)
1109 list_for_each_entry_safe_continue(w, n, list,
1110 power_list);
1112 if (event == SND_SOC_DAPM_STREAM_START)
1113 ret = w->event(w,
1114 NULL, SND_SOC_DAPM_PRE_PMU);
1115 else if (event == SND_SOC_DAPM_STREAM_STOP)
1116 ret = w->event(w,
1117 NULL, SND_SOC_DAPM_PRE_PMD);
1118 break;
1120 case snd_soc_dapm_post:
1121 if (!w->event)
1122 list_for_each_entry_safe_continue(w, n, list,
1123 power_list);
1125 if (event == SND_SOC_DAPM_STREAM_START)
1126 ret = w->event(w,
1127 NULL, SND_SOC_DAPM_POST_PMU);
1128 else if (event == SND_SOC_DAPM_STREAM_STOP)
1129 ret = w->event(w,
1130 NULL, SND_SOC_DAPM_POST_PMD);
1131 break;
1133 default:
1134 /* Queue it up for application */
1135 cur_sort = sort[w->id];
1136 cur_subseq = w->subseq;
1137 cur_reg = w->reg;
1138 cur_dapm = w->dapm;
1139 list_move(&w->power_list, &pending);
1140 break;
1143 if (ret < 0)
1144 dev_err(w->dapm->dev,
1145 "Failed to apply widget power: %d\n", ret);
1148 if (!list_empty(&pending))
1149 dapm_seq_run_coalesced(cur_dapm, &pending);
1151 if (cur_dapm && cur_dapm->seq_notifier) {
1152 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1153 if (sort[i] == cur_sort)
1154 cur_dapm->seq_notifier(cur_dapm,
1155 i, cur_subseq);
1159 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1161 struct snd_soc_dapm_update *update = dapm->update;
1162 struct snd_soc_dapm_widget *w;
1163 int ret;
1165 if (!update)
1166 return;
1168 w = update->widget;
1170 if (w->event &&
1171 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1172 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1173 if (ret != 0)
1174 pr_err("%s DAPM pre-event failed: %d\n",
1175 w->name, ret);
1178 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1179 update->val);
1180 if (ret < 0)
1181 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1183 if (w->event &&
1184 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1185 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1186 if (ret != 0)
1187 pr_err("%s DAPM post-event failed: %d\n",
1188 w->name, ret);
1192 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1193 * they're changing state.
1195 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1197 struct snd_soc_dapm_context *d = data;
1198 int ret;
1200 /* If we're off and we're not supposed to be go into STANDBY */
1201 if (d->bias_level == SND_SOC_BIAS_OFF &&
1202 d->target_bias_level != SND_SOC_BIAS_OFF) {
1203 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1204 if (ret != 0)
1205 dev_err(d->dev,
1206 "Failed to turn on bias: %d\n", ret);
1209 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1210 if (d->bias_level != d->target_bias_level) {
1211 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1212 if (ret != 0)
1213 dev_err(d->dev,
1214 "Failed to prepare bias: %d\n", ret);
1218 /* Async callback run prior to DAPM sequences - brings to their final
1219 * state.
1221 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1223 struct snd_soc_dapm_context *d = data;
1224 int ret;
1226 /* If we just powered the last thing off drop to standby bias */
1227 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1228 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1229 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1230 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1231 if (ret != 0)
1232 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1233 ret);
1236 /* If we're in standby and can support bias off then do that */
1237 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1238 d->target_bias_level == SND_SOC_BIAS_OFF) {
1239 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1240 if (ret != 0)
1241 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1244 /* If we just powered up then move to active bias */
1245 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1246 d->target_bias_level == SND_SOC_BIAS_ON) {
1247 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1248 if (ret != 0)
1249 dev_err(d->dev, "Failed to apply active bias: %d\n",
1250 ret);
1254 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1255 bool power, bool connect)
1257 /* If a connection is being made or broken then that update
1258 * will have marked the peer dirty, otherwise the widgets are
1259 * not connected and this update has no impact. */
1260 if (!connect)
1261 return;
1263 /* If the peer is already in the state we're moving to then we
1264 * won't have an impact on it. */
1265 if (power != peer->power)
1266 dapm_mark_dirty(peer, "peer state change");
1269 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1270 struct list_head *up_list,
1271 struct list_head *down_list)
1273 struct snd_soc_dapm_path *path;
1275 if (w->power == power)
1276 return;
1278 trace_snd_soc_dapm_widget_power(w, power);
1280 /* If we changed our power state perhaps our neigbours changed
1281 * also.
1283 list_for_each_entry(path, &w->sources, list_sink) {
1284 if (path->source) {
1285 dapm_widget_set_peer_power(path->source, power,
1286 path->connect);
1289 switch (w->id) {
1290 case snd_soc_dapm_supply:
1291 /* Supplies can't affect their outputs, only their inputs */
1292 break;
1293 default:
1294 list_for_each_entry(path, &w->sinks, list_source) {
1295 if (path->sink) {
1296 dapm_widget_set_peer_power(path->sink, power,
1297 path->connect);
1300 break;
1303 if (power)
1304 dapm_seq_insert(w, up_list, true);
1305 else
1306 dapm_seq_insert(w, down_list, false);
1308 w->power = power;
1311 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1312 struct list_head *up_list,
1313 struct list_head *down_list)
1315 int power;
1317 switch (w->id) {
1318 case snd_soc_dapm_pre:
1319 dapm_seq_insert(w, down_list, false);
1320 break;
1321 case snd_soc_dapm_post:
1322 dapm_seq_insert(w, up_list, true);
1323 break;
1325 default:
1326 power = dapm_widget_power_check(w);
1328 dapm_widget_set_power(w, power, up_list, down_list);
1329 break;
1334 * Scan each dapm widget for complete audio path.
1335 * A complete path is a route that has valid endpoints i.e.:-
1337 * o DAC to output pin.
1338 * o Input Pin to ADC.
1339 * o Input pin to Output pin (bypass, sidetone)
1340 * o DAC to ADC (loopback).
1342 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1344 struct snd_soc_card *card = dapm->card;
1345 struct snd_soc_dapm_widget *w;
1346 struct snd_soc_dapm_context *d;
1347 LIST_HEAD(up_list);
1348 LIST_HEAD(down_list);
1349 LIST_HEAD(async_domain);
1350 enum snd_soc_bias_level bias;
1352 trace_snd_soc_dapm_start(card);
1354 list_for_each_entry(d, &card->dapm_list, list) {
1355 if (d->n_widgets || d->codec == NULL) {
1356 if (d->idle_bias_off)
1357 d->target_bias_level = SND_SOC_BIAS_OFF;
1358 else
1359 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1363 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
1365 list_for_each_entry(w, &card->widgets, list) {
1366 w->power_checked = false;
1367 w->inputs = -1;
1368 w->outputs = -1;
1371 /* Check which widgets we need to power and store them in
1372 * lists indicating if they should be powered up or down. We
1373 * only check widgets that have been flagged as dirty but note
1374 * that new widgets may be added to the dirty list while we
1375 * iterate.
1377 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1378 dapm_power_one_widget(w, &up_list, &down_list);
1381 list_for_each_entry(w, &card->widgets, list) {
1382 list_del_init(&w->dirty);
1384 if (w->power) {
1385 d = w->dapm;
1387 /* Supplies and micbiases only bring the
1388 * context up to STANDBY as unless something
1389 * else is active and passing audio they
1390 * generally don't require full power.
1392 switch (w->id) {
1393 case snd_soc_dapm_supply:
1394 case snd_soc_dapm_micbias:
1395 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1396 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1397 break;
1398 default:
1399 d->target_bias_level = SND_SOC_BIAS_ON;
1400 break;
1406 /* If there are no DAPM widgets then try to figure out power from the
1407 * event type.
1409 if (!dapm->n_widgets) {
1410 switch (event) {
1411 case SND_SOC_DAPM_STREAM_START:
1412 case SND_SOC_DAPM_STREAM_RESUME:
1413 dapm->target_bias_level = SND_SOC_BIAS_ON;
1414 break;
1415 case SND_SOC_DAPM_STREAM_STOP:
1416 if (dapm->codec->active)
1417 dapm->target_bias_level = SND_SOC_BIAS_ON;
1418 else
1419 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1420 break;
1421 case SND_SOC_DAPM_STREAM_SUSPEND:
1422 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1423 break;
1424 case SND_SOC_DAPM_STREAM_NOP:
1425 dapm->target_bias_level = dapm->bias_level;
1426 break;
1427 default:
1428 break;
1432 /* Force all contexts in the card to the same bias state if
1433 * they're not ground referenced.
1435 bias = SND_SOC_BIAS_OFF;
1436 list_for_each_entry(d, &card->dapm_list, list)
1437 if (d->target_bias_level > bias)
1438 bias = d->target_bias_level;
1439 list_for_each_entry(d, &card->dapm_list, list)
1440 if (!d->idle_bias_off)
1441 d->target_bias_level = bias;
1443 trace_snd_soc_dapm_walk_done(card);
1445 /* Run all the bias changes in parallel */
1446 list_for_each_entry(d, &dapm->card->dapm_list, list)
1447 async_schedule_domain(dapm_pre_sequence_async, d,
1448 &async_domain);
1449 async_synchronize_full_domain(&async_domain);
1451 /* Power down widgets first; try to avoid amplifying pops. */
1452 dapm_seq_run(dapm, &down_list, event, false);
1454 dapm_widget_update(dapm);
1456 /* Now power up. */
1457 dapm_seq_run(dapm, &up_list, event, true);
1459 /* Run all the bias changes in parallel */
1460 list_for_each_entry(d, &dapm->card->dapm_list, list)
1461 async_schedule_domain(dapm_post_sequence_async, d,
1462 &async_domain);
1463 async_synchronize_full_domain(&async_domain);
1465 pop_dbg(dapm->dev, card->pop_time,
1466 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1467 pop_wait(card->pop_time);
1469 trace_snd_soc_dapm_done(card);
1471 return 0;
1474 #ifdef CONFIG_DEBUG_FS
1475 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1477 file->private_data = inode->i_private;
1478 return 0;
1481 static ssize_t dapm_widget_power_read_file(struct file *file,
1482 char __user *user_buf,
1483 size_t count, loff_t *ppos)
1485 struct snd_soc_dapm_widget *w = file->private_data;
1486 char *buf;
1487 int in, out;
1488 ssize_t ret;
1489 struct snd_soc_dapm_path *p = NULL;
1491 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1492 if (!buf)
1493 return -ENOMEM;
1495 in = is_connected_input_ep(w);
1496 dapm_clear_walk(w->dapm);
1497 out = is_connected_output_ep(w);
1498 dapm_clear_walk(w->dapm);
1500 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1501 w->name, w->power ? "On" : "Off", in, out);
1503 if (w->reg >= 0)
1504 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1505 " - R%d(0x%x) bit %d",
1506 w->reg, w->reg, w->shift);
1508 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1510 if (w->sname)
1511 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1512 w->sname,
1513 w->active ? "active" : "inactive");
1515 list_for_each_entry(p, &w->sources, list_sink) {
1516 if (p->connected && !p->connected(w, p->sink))
1517 continue;
1519 if (p->connect)
1520 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1521 " in \"%s\" \"%s\"\n",
1522 p->name ? p->name : "static",
1523 p->source->name);
1525 list_for_each_entry(p, &w->sinks, list_source) {
1526 if (p->connected && !p->connected(w, p->sink))
1527 continue;
1529 if (p->connect)
1530 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1531 " out \"%s\" \"%s\"\n",
1532 p->name ? p->name : "static",
1533 p->sink->name);
1536 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1538 kfree(buf);
1539 return ret;
1542 static const struct file_operations dapm_widget_power_fops = {
1543 .open = dapm_widget_power_open_file,
1544 .read = dapm_widget_power_read_file,
1545 .llseek = default_llseek,
1548 static int dapm_bias_open_file(struct inode *inode, struct file *file)
1550 file->private_data = inode->i_private;
1551 return 0;
1554 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1555 size_t count, loff_t *ppos)
1557 struct snd_soc_dapm_context *dapm = file->private_data;
1558 char *level;
1560 switch (dapm->bias_level) {
1561 case SND_SOC_BIAS_ON:
1562 level = "On\n";
1563 break;
1564 case SND_SOC_BIAS_PREPARE:
1565 level = "Prepare\n";
1566 break;
1567 case SND_SOC_BIAS_STANDBY:
1568 level = "Standby\n";
1569 break;
1570 case SND_SOC_BIAS_OFF:
1571 level = "Off\n";
1572 break;
1573 default:
1574 BUG();
1575 level = "Unknown\n";
1576 break;
1579 return simple_read_from_buffer(user_buf, count, ppos, level,
1580 strlen(level));
1583 static const struct file_operations dapm_bias_fops = {
1584 .open = dapm_bias_open_file,
1585 .read = dapm_bias_read_file,
1586 .llseek = default_llseek,
1589 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1590 struct dentry *parent)
1592 struct dentry *d;
1594 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1596 if (!dapm->debugfs_dapm) {
1597 printk(KERN_WARNING
1598 "Failed to create DAPM debugfs directory\n");
1599 return;
1602 d = debugfs_create_file("bias_level", 0444,
1603 dapm->debugfs_dapm, dapm,
1604 &dapm_bias_fops);
1605 if (!d)
1606 dev_warn(dapm->dev,
1607 "ASoC: Failed to create bias level debugfs file\n");
1610 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1612 struct snd_soc_dapm_context *dapm = w->dapm;
1613 struct dentry *d;
1615 if (!dapm->debugfs_dapm || !w->name)
1616 return;
1618 d = debugfs_create_file(w->name, 0444,
1619 dapm->debugfs_dapm, w,
1620 &dapm_widget_power_fops);
1621 if (!d)
1622 dev_warn(w->dapm->dev,
1623 "ASoC: Failed to create %s debugfs file\n",
1624 w->name);
1627 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1629 debugfs_remove_recursive(dapm->debugfs_dapm);
1632 #else
1633 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1634 struct dentry *parent)
1638 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1642 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1646 #endif
1648 /* test and update the power status of a mux widget */
1649 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1650 struct snd_kcontrol *kcontrol, int change,
1651 int mux, struct soc_enum *e)
1653 struct snd_soc_dapm_path *path;
1654 int found = 0;
1656 if (widget->id != snd_soc_dapm_mux &&
1657 widget->id != snd_soc_dapm_virt_mux &&
1658 widget->id != snd_soc_dapm_value_mux)
1659 return -ENODEV;
1661 if (!change)
1662 return 0;
1664 /* find dapm widget path assoc with kcontrol */
1665 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1666 if (path->kcontrol != kcontrol)
1667 continue;
1669 if (!path->name || !e->texts[mux])
1670 continue;
1672 found = 1;
1673 /* we now need to match the string in the enum to the path */
1674 if (!(strcmp(path->name, e->texts[mux]))) {
1675 path->connect = 1; /* new connection */
1676 dapm_mark_dirty(path->source, "mux connection");
1677 } else {
1678 if (path->connect)
1679 dapm_mark_dirty(path->source,
1680 "mux disconnection");
1681 path->connect = 0; /* old connection must be powered down */
1685 if (found) {
1686 dapm_mark_dirty(widget, "mux change");
1687 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1690 return 0;
1693 /* test and update the power status of a mixer or switch widget */
1694 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1695 struct snd_kcontrol *kcontrol, int connect)
1697 struct snd_soc_dapm_path *path;
1698 int found = 0;
1700 if (widget->id != snd_soc_dapm_mixer &&
1701 widget->id != snd_soc_dapm_mixer_named_ctl &&
1702 widget->id != snd_soc_dapm_switch)
1703 return -ENODEV;
1705 /* find dapm widget path assoc with kcontrol */
1706 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1707 if (path->kcontrol != kcontrol)
1708 continue;
1710 /* found, now check type */
1711 found = 1;
1712 path->connect = connect;
1713 dapm_mark_dirty(path->source, "mixer connection");
1716 if (found) {
1717 dapm_mark_dirty(widget, "mixer update");
1718 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1721 return 0;
1724 /* show dapm widget status in sys fs */
1725 static ssize_t dapm_widget_show(struct device *dev,
1726 struct device_attribute *attr, char *buf)
1728 struct snd_soc_pcm_runtime *rtd =
1729 container_of(dev, struct snd_soc_pcm_runtime, dev);
1730 struct snd_soc_codec *codec =rtd->codec;
1731 struct snd_soc_dapm_widget *w;
1732 int count = 0;
1733 char *state = "not set";
1735 list_for_each_entry(w, &codec->card->widgets, list) {
1736 if (w->dapm != &codec->dapm)
1737 continue;
1739 /* only display widgets that burnm power */
1740 switch (w->id) {
1741 case snd_soc_dapm_hp:
1742 case snd_soc_dapm_mic:
1743 case snd_soc_dapm_spk:
1744 case snd_soc_dapm_line:
1745 case snd_soc_dapm_micbias:
1746 case snd_soc_dapm_dac:
1747 case snd_soc_dapm_adc:
1748 case snd_soc_dapm_pga:
1749 case snd_soc_dapm_out_drv:
1750 case snd_soc_dapm_mixer:
1751 case snd_soc_dapm_mixer_named_ctl:
1752 case snd_soc_dapm_supply:
1753 if (w->name)
1754 count += sprintf(buf + count, "%s: %s\n",
1755 w->name, w->power ? "On":"Off");
1756 break;
1757 default:
1758 break;
1762 switch (codec->dapm.bias_level) {
1763 case SND_SOC_BIAS_ON:
1764 state = "On";
1765 break;
1766 case SND_SOC_BIAS_PREPARE:
1767 state = "Prepare";
1768 break;
1769 case SND_SOC_BIAS_STANDBY:
1770 state = "Standby";
1771 break;
1772 case SND_SOC_BIAS_OFF:
1773 state = "Off";
1774 break;
1776 count += sprintf(buf + count, "PM State: %s\n", state);
1778 return count;
1781 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1783 int snd_soc_dapm_sys_add(struct device *dev)
1785 return device_create_file(dev, &dev_attr_dapm_widget);
1788 static void snd_soc_dapm_sys_remove(struct device *dev)
1790 device_remove_file(dev, &dev_attr_dapm_widget);
1793 /* free all dapm widgets and resources */
1794 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1796 struct snd_soc_dapm_widget *w, *next_w;
1797 struct snd_soc_dapm_path *p, *next_p;
1799 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1800 if (w->dapm != dapm)
1801 continue;
1802 list_del(&w->list);
1804 * remove source and sink paths associated to this widget.
1805 * While removing the path, remove reference to it from both
1806 * source and sink widgets so that path is removed only once.
1808 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1809 list_del(&p->list_sink);
1810 list_del(&p->list_source);
1811 list_del(&p->list);
1812 kfree(p->long_name);
1813 kfree(p);
1815 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1816 list_del(&p->list_sink);
1817 list_del(&p->list_source);
1818 list_del(&p->list);
1819 kfree(p->long_name);
1820 kfree(p);
1822 kfree(w->kcontrols);
1823 kfree(w->name);
1824 kfree(w);
1828 static struct snd_soc_dapm_widget *dapm_find_widget(
1829 struct snd_soc_dapm_context *dapm, const char *pin,
1830 bool search_other_contexts)
1832 struct snd_soc_dapm_widget *w;
1833 struct snd_soc_dapm_widget *fallback = NULL;
1835 list_for_each_entry(w, &dapm->card->widgets, list) {
1836 if (!strcmp(w->name, pin)) {
1837 if (w->dapm == dapm)
1838 return w;
1839 else
1840 fallback = w;
1844 if (search_other_contexts)
1845 return fallback;
1847 return NULL;
1850 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1851 const char *pin, int status)
1853 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
1855 if (!w) {
1856 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1857 return -EINVAL;
1860 w->connected = status;
1861 if (status == 0)
1862 w->force = 0;
1863 dapm_mark_dirty(w, "pin configuration");
1865 return 0;
1869 * snd_soc_dapm_sync - scan and power dapm paths
1870 * @dapm: DAPM context
1872 * Walks all dapm audio paths and powers widgets according to their
1873 * stream or path usage.
1875 * Returns 0 for success.
1877 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1880 * Suppress early reports (eg, jacks syncing their state) to avoid
1881 * silly DAPM runs during card startup.
1883 if (!dapm->card || !dapm->card->instantiated)
1884 return 0;
1886 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1888 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1890 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1891 const struct snd_soc_dapm_route *route)
1893 struct snd_soc_dapm_path *path;
1894 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1895 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1896 const char *sink;
1897 const char *control = route->control;
1898 const char *source;
1899 char prefixed_sink[80];
1900 char prefixed_source[80];
1901 int ret = 0;
1903 if (dapm->codec && dapm->codec->name_prefix) {
1904 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1905 dapm->codec->name_prefix, route->sink);
1906 sink = prefixed_sink;
1907 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1908 dapm->codec->name_prefix, route->source);
1909 source = prefixed_source;
1910 } else {
1911 sink = route->sink;
1912 source = route->source;
1916 * find src and dest widgets over all widgets but favor a widget from
1917 * current DAPM context
1919 list_for_each_entry(w, &dapm->card->widgets, list) {
1920 if (!wsink && !(strcmp(w->name, sink))) {
1921 wtsink = w;
1922 if (w->dapm == dapm)
1923 wsink = w;
1924 continue;
1926 if (!wsource && !(strcmp(w->name, source))) {
1927 wtsource = w;
1928 if (w->dapm == dapm)
1929 wsource = w;
1932 /* use widget from another DAPM context if not found from this */
1933 if (!wsink)
1934 wsink = wtsink;
1935 if (!wsource)
1936 wsource = wtsource;
1938 if (wsource == NULL || wsink == NULL)
1939 return -ENODEV;
1941 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1942 if (!path)
1943 return -ENOMEM;
1945 path->source = wsource;
1946 path->sink = wsink;
1947 path->connected = route->connected;
1948 INIT_LIST_HEAD(&path->list);
1949 INIT_LIST_HEAD(&path->list_source);
1950 INIT_LIST_HEAD(&path->list_sink);
1952 /* check for external widgets */
1953 if (wsink->id == snd_soc_dapm_input) {
1954 if (wsource->id == snd_soc_dapm_micbias ||
1955 wsource->id == snd_soc_dapm_mic ||
1956 wsource->id == snd_soc_dapm_line ||
1957 wsource->id == snd_soc_dapm_output)
1958 wsink->ext = 1;
1960 if (wsource->id == snd_soc_dapm_output) {
1961 if (wsink->id == snd_soc_dapm_spk ||
1962 wsink->id == snd_soc_dapm_hp ||
1963 wsink->id == snd_soc_dapm_line ||
1964 wsink->id == snd_soc_dapm_input)
1965 wsource->ext = 1;
1968 /* connect static paths */
1969 if (control == NULL) {
1970 list_add(&path->list, &dapm->card->paths);
1971 list_add(&path->list_sink, &wsink->sources);
1972 list_add(&path->list_source, &wsource->sinks);
1973 path->connect = 1;
1974 return 0;
1977 /* connect dynamic paths */
1978 switch (wsink->id) {
1979 case snd_soc_dapm_adc:
1980 case snd_soc_dapm_dac:
1981 case snd_soc_dapm_pga:
1982 case snd_soc_dapm_out_drv:
1983 case snd_soc_dapm_input:
1984 case snd_soc_dapm_output:
1985 case snd_soc_dapm_micbias:
1986 case snd_soc_dapm_vmid:
1987 case snd_soc_dapm_pre:
1988 case snd_soc_dapm_post:
1989 case snd_soc_dapm_supply:
1990 case snd_soc_dapm_aif_in:
1991 case snd_soc_dapm_aif_out:
1992 list_add(&path->list, &dapm->card->paths);
1993 list_add(&path->list_sink, &wsink->sources);
1994 list_add(&path->list_source, &wsource->sinks);
1995 path->connect = 1;
1996 return 0;
1997 case snd_soc_dapm_mux:
1998 case snd_soc_dapm_virt_mux:
1999 case snd_soc_dapm_value_mux:
2000 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2001 &wsink->kcontrol_news[0]);
2002 if (ret != 0)
2003 goto err;
2004 break;
2005 case snd_soc_dapm_switch:
2006 case snd_soc_dapm_mixer:
2007 case snd_soc_dapm_mixer_named_ctl:
2008 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2009 if (ret != 0)
2010 goto err;
2011 break;
2012 case snd_soc_dapm_hp:
2013 case snd_soc_dapm_mic:
2014 case snd_soc_dapm_line:
2015 case snd_soc_dapm_spk:
2016 list_add(&path->list, &dapm->card->paths);
2017 list_add(&path->list_sink, &wsink->sources);
2018 list_add(&path->list_source, &wsource->sinks);
2019 path->connect = 0;
2020 return 0;
2022 return 0;
2024 err:
2025 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2026 source, control, sink);
2027 kfree(path);
2028 return ret;
2032 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2033 * @dapm: DAPM context
2034 * @route: audio routes
2035 * @num: number of routes
2037 * Connects 2 dapm widgets together via a named audio path. The sink is
2038 * the widget receiving the audio signal, whilst the source is the sender
2039 * of the audio signal.
2041 * Returns 0 for success else error. On error all resources can be freed
2042 * with a call to snd_soc_card_free().
2044 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2045 const struct snd_soc_dapm_route *route, int num)
2047 int i, ret;
2049 for (i = 0; i < num; i++) {
2050 ret = snd_soc_dapm_add_route(dapm, route);
2051 if (ret < 0) {
2052 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2053 route->source, route->sink);
2054 return ret;
2056 route++;
2059 return 0;
2061 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2063 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2064 const struct snd_soc_dapm_route *route)
2066 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2067 route->source,
2068 true);
2069 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2070 route->sink,
2071 true);
2072 struct snd_soc_dapm_path *path;
2073 int count = 0;
2075 if (!source) {
2076 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2077 route->source);
2078 return -ENODEV;
2081 if (!sink) {
2082 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2083 route->sink);
2084 return -ENODEV;
2087 if (route->control || route->connected)
2088 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2089 route->source, route->sink);
2091 list_for_each_entry(path, &source->sinks, list_source) {
2092 if (path->sink == sink) {
2093 path->weak = 1;
2094 count++;
2098 if (count == 0)
2099 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2100 route->source, route->sink);
2101 if (count > 1)
2102 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2103 count, route->source, route->sink);
2105 return 0;
2109 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2110 * @dapm: DAPM context
2111 * @route: audio routes
2112 * @num: number of routes
2114 * Mark existing routes matching those specified in the passed array
2115 * as being weak, meaning that they are ignored for the purpose of
2116 * power decisions. The main intended use case is for sidetone paths
2117 * which couple audio between other independent paths if they are both
2118 * active in order to make the combination work better at the user
2119 * level but which aren't intended to be "used".
2121 * Note that CODEC drivers should not use this as sidetone type paths
2122 * can frequently also be used as bypass paths.
2124 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2125 const struct snd_soc_dapm_route *route, int num)
2127 int i, err;
2128 int ret = 0;
2130 for (i = 0; i < num; i++) {
2131 err = snd_soc_dapm_weak_route(dapm, route);
2132 if (err)
2133 ret = err;
2134 route++;
2137 return ret;
2139 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2142 * snd_soc_dapm_new_widgets - add new dapm widgets
2143 * @dapm: DAPM context
2145 * Checks the codec for any new dapm widgets and creates them if found.
2147 * Returns 0 for success.
2149 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2151 struct snd_soc_dapm_widget *w;
2152 unsigned int val;
2154 list_for_each_entry(w, &dapm->card->widgets, list)
2156 if (w->new)
2157 continue;
2159 if (w->num_kcontrols) {
2160 w->kcontrols = kzalloc(w->num_kcontrols *
2161 sizeof(struct snd_kcontrol *),
2162 GFP_KERNEL);
2163 if (!w->kcontrols)
2164 return -ENOMEM;
2167 switch(w->id) {
2168 case snd_soc_dapm_switch:
2169 case snd_soc_dapm_mixer:
2170 case snd_soc_dapm_mixer_named_ctl:
2171 dapm_new_mixer(w);
2172 break;
2173 case snd_soc_dapm_mux:
2174 case snd_soc_dapm_virt_mux:
2175 case snd_soc_dapm_value_mux:
2176 dapm_new_mux(w);
2177 break;
2178 case snd_soc_dapm_pga:
2179 case snd_soc_dapm_out_drv:
2180 dapm_new_pga(w);
2181 break;
2182 default:
2183 break;
2186 /* Read the initial power state from the device */
2187 if (w->reg >= 0) {
2188 val = soc_widget_read(w, w->reg);
2189 val &= 1 << w->shift;
2190 if (w->invert)
2191 val = !val;
2193 if (val)
2194 w->power = 1;
2197 w->new = 1;
2199 dapm_mark_dirty(w, "new widget");
2200 dapm_debugfs_add_widget(w);
2203 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2204 return 0;
2206 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2209 * snd_soc_dapm_get_volsw - dapm mixer get callback
2210 * @kcontrol: mixer control
2211 * @ucontrol: control element information
2213 * Callback to get the value of a dapm mixer control.
2215 * Returns 0 for success.
2217 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2218 struct snd_ctl_elem_value *ucontrol)
2220 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2221 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2222 struct soc_mixer_control *mc =
2223 (struct soc_mixer_control *)kcontrol->private_value;
2224 unsigned int reg = mc->reg;
2225 unsigned int shift = mc->shift;
2226 unsigned int rshift = mc->rshift;
2227 int max = mc->max;
2228 unsigned int invert = mc->invert;
2229 unsigned int mask = (1 << fls(max)) - 1;
2231 ucontrol->value.integer.value[0] =
2232 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2233 if (shift != rshift)
2234 ucontrol->value.integer.value[1] =
2235 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2236 if (invert) {
2237 ucontrol->value.integer.value[0] =
2238 max - ucontrol->value.integer.value[0];
2239 if (shift != rshift)
2240 ucontrol->value.integer.value[1] =
2241 max - ucontrol->value.integer.value[1];
2244 return 0;
2246 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2249 * snd_soc_dapm_put_volsw - dapm mixer set callback
2250 * @kcontrol: mixer control
2251 * @ucontrol: control element information
2253 * Callback to set the value of a dapm mixer control.
2255 * Returns 0 for success.
2257 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2258 struct snd_ctl_elem_value *ucontrol)
2260 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2261 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2262 struct snd_soc_codec *codec = widget->codec;
2263 struct soc_mixer_control *mc =
2264 (struct soc_mixer_control *)kcontrol->private_value;
2265 unsigned int reg = mc->reg;
2266 unsigned int shift = mc->shift;
2267 int max = mc->max;
2268 unsigned int mask = (1 << fls(max)) - 1;
2269 unsigned int invert = mc->invert;
2270 unsigned int val;
2271 int connect, change;
2272 struct snd_soc_dapm_update update;
2273 int wi;
2275 val = (ucontrol->value.integer.value[0] & mask);
2277 if (invert)
2278 val = max - val;
2279 mask = mask << shift;
2280 val = val << shift;
2282 if (val)
2283 /* new connection */
2284 connect = invert ? 0 : 1;
2285 else
2286 /* old connection must be powered down */
2287 connect = invert ? 1 : 0;
2289 mutex_lock(&codec->mutex);
2291 change = snd_soc_test_bits(widget->codec, reg, mask, val);
2292 if (change) {
2293 for (wi = 0; wi < wlist->num_widgets; wi++) {
2294 widget = wlist->widgets[wi];
2296 widget->value = val;
2298 update.kcontrol = kcontrol;
2299 update.widget = widget;
2300 update.reg = reg;
2301 update.mask = mask;
2302 update.val = val;
2303 widget->dapm->update = &update;
2305 dapm_mixer_update_power(widget, kcontrol, connect);
2307 widget->dapm->update = NULL;
2311 mutex_unlock(&codec->mutex);
2312 return 0;
2314 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2317 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2318 * @kcontrol: mixer control
2319 * @ucontrol: control element information
2321 * Callback to get the value of a dapm enumerated double mixer control.
2323 * Returns 0 for success.
2325 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2326 struct snd_ctl_elem_value *ucontrol)
2328 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2329 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2330 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2331 unsigned int val, bitmask;
2333 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2335 val = snd_soc_read(widget->codec, e->reg);
2336 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2337 if (e->shift_l != e->shift_r)
2338 ucontrol->value.enumerated.item[1] =
2339 (val >> e->shift_r) & (bitmask - 1);
2341 return 0;
2343 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2346 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2347 * @kcontrol: mixer control
2348 * @ucontrol: control element information
2350 * Callback to set the value of a dapm enumerated double mixer control.
2352 * Returns 0 for success.
2354 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2355 struct snd_ctl_elem_value *ucontrol)
2357 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2358 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2359 struct snd_soc_codec *codec = widget->codec;
2360 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2361 unsigned int val, mux, change;
2362 unsigned int mask, bitmask;
2363 struct snd_soc_dapm_update update;
2364 int wi;
2366 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2368 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2369 return -EINVAL;
2370 mux = ucontrol->value.enumerated.item[0];
2371 val = mux << e->shift_l;
2372 mask = (bitmask - 1) << e->shift_l;
2373 if (e->shift_l != e->shift_r) {
2374 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2375 return -EINVAL;
2376 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2377 mask |= (bitmask - 1) << e->shift_r;
2380 mutex_lock(&codec->mutex);
2382 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2383 if (change) {
2384 for (wi = 0; wi < wlist->num_widgets; wi++) {
2385 widget = wlist->widgets[wi];
2387 widget->value = val;
2389 update.kcontrol = kcontrol;
2390 update.widget = widget;
2391 update.reg = e->reg;
2392 update.mask = mask;
2393 update.val = val;
2394 widget->dapm->update = &update;
2396 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2398 widget->dapm->update = NULL;
2402 mutex_unlock(&codec->mutex);
2403 return change;
2405 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2408 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2409 * @kcontrol: mixer control
2410 * @ucontrol: control element information
2412 * Returns 0 for success.
2414 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2415 struct snd_ctl_elem_value *ucontrol)
2417 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2418 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2420 ucontrol->value.enumerated.item[0] = widget->value;
2422 return 0;
2424 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2427 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2428 * @kcontrol: mixer control
2429 * @ucontrol: control element information
2431 * Returns 0 for success.
2433 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2434 struct snd_ctl_elem_value *ucontrol)
2436 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2437 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2438 struct snd_soc_codec *codec = widget->codec;
2439 struct soc_enum *e =
2440 (struct soc_enum *)kcontrol->private_value;
2441 int change;
2442 int ret = 0;
2443 int wi;
2445 if (ucontrol->value.enumerated.item[0] >= e->max)
2446 return -EINVAL;
2448 mutex_lock(&codec->mutex);
2450 change = widget->value != ucontrol->value.enumerated.item[0];
2451 if (change) {
2452 for (wi = 0; wi < wlist->num_widgets; wi++) {
2453 widget = wlist->widgets[wi];
2455 widget->value = ucontrol->value.enumerated.item[0];
2457 dapm_mux_update_power(widget, kcontrol, change,
2458 widget->value, e);
2462 mutex_unlock(&codec->mutex);
2463 return ret;
2465 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2468 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2469 * callback
2470 * @kcontrol: mixer control
2471 * @ucontrol: control element information
2473 * Callback to get the value of a dapm semi enumerated double mixer control.
2475 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2476 * used for handling bitfield coded enumeration for example.
2478 * Returns 0 for success.
2480 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2481 struct snd_ctl_elem_value *ucontrol)
2483 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2484 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2485 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2486 unsigned int reg_val, val, mux;
2488 reg_val = snd_soc_read(widget->codec, e->reg);
2489 val = (reg_val >> e->shift_l) & e->mask;
2490 for (mux = 0; mux < e->max; mux++) {
2491 if (val == e->values[mux])
2492 break;
2494 ucontrol->value.enumerated.item[0] = mux;
2495 if (e->shift_l != e->shift_r) {
2496 val = (reg_val >> e->shift_r) & e->mask;
2497 for (mux = 0; mux < e->max; mux++) {
2498 if (val == e->values[mux])
2499 break;
2501 ucontrol->value.enumerated.item[1] = mux;
2504 return 0;
2506 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2509 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2510 * callback
2511 * @kcontrol: mixer control
2512 * @ucontrol: control element information
2514 * Callback to set the value of a dapm semi enumerated double mixer control.
2516 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2517 * used for handling bitfield coded enumeration for example.
2519 * Returns 0 for success.
2521 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2522 struct snd_ctl_elem_value *ucontrol)
2524 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2525 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2526 struct snd_soc_codec *codec = widget->codec;
2527 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2528 unsigned int val, mux, change;
2529 unsigned int mask;
2530 struct snd_soc_dapm_update update;
2531 int wi;
2533 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2534 return -EINVAL;
2535 mux = ucontrol->value.enumerated.item[0];
2536 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2537 mask = e->mask << e->shift_l;
2538 if (e->shift_l != e->shift_r) {
2539 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2540 return -EINVAL;
2541 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2542 mask |= e->mask << e->shift_r;
2545 mutex_lock(&codec->mutex);
2547 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2548 if (change) {
2549 for (wi = 0; wi < wlist->num_widgets; wi++) {
2550 widget = wlist->widgets[wi];
2552 widget->value = val;
2554 update.kcontrol = kcontrol;
2555 update.widget = widget;
2556 update.reg = e->reg;
2557 update.mask = mask;
2558 update.val = val;
2559 widget->dapm->update = &update;
2561 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2563 widget->dapm->update = NULL;
2567 mutex_unlock(&codec->mutex);
2568 return change;
2570 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2573 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2575 * @kcontrol: mixer control
2576 * @uinfo: control element information
2578 * Callback to provide information about a pin switch control.
2580 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2581 struct snd_ctl_elem_info *uinfo)
2583 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2584 uinfo->count = 1;
2585 uinfo->value.integer.min = 0;
2586 uinfo->value.integer.max = 1;
2588 return 0;
2590 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2593 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2595 * @kcontrol: mixer control
2596 * @ucontrol: Value
2598 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2599 struct snd_ctl_elem_value *ucontrol)
2601 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2602 const char *pin = (const char *)kcontrol->private_value;
2604 mutex_lock(&codec->mutex);
2606 ucontrol->value.integer.value[0] =
2607 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2609 mutex_unlock(&codec->mutex);
2611 return 0;
2613 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2616 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2618 * @kcontrol: mixer control
2619 * @ucontrol: Value
2621 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2622 struct snd_ctl_elem_value *ucontrol)
2624 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2625 const char *pin = (const char *)kcontrol->private_value;
2627 mutex_lock(&codec->mutex);
2629 if (ucontrol->value.integer.value[0])
2630 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2631 else
2632 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2634 snd_soc_dapm_sync(&codec->dapm);
2636 mutex_unlock(&codec->mutex);
2638 return 0;
2640 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2643 * snd_soc_dapm_new_control - create new dapm control
2644 * @dapm: DAPM context
2645 * @widget: widget template
2647 * Creates a new dapm control based upon the template.
2649 * Returns 0 for success else error.
2651 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2652 const struct snd_soc_dapm_widget *widget)
2654 struct snd_soc_dapm_widget *w;
2655 size_t name_len;
2657 if ((w = dapm_cnew_widget(widget)) == NULL)
2658 return -ENOMEM;
2660 name_len = strlen(widget->name) + 1;
2661 if (dapm->codec && dapm->codec->name_prefix)
2662 name_len += 1 + strlen(dapm->codec->name_prefix);
2663 w->name = kmalloc(name_len, GFP_KERNEL);
2664 if (w->name == NULL) {
2665 kfree(w);
2666 return -ENOMEM;
2668 if (dapm->codec && dapm->codec->name_prefix)
2669 snprintf(w->name, name_len, "%s %s",
2670 dapm->codec->name_prefix, widget->name);
2671 else
2672 snprintf(w->name, name_len, "%s", widget->name);
2674 switch (w->id) {
2675 case snd_soc_dapm_switch:
2676 case snd_soc_dapm_mixer:
2677 case snd_soc_dapm_mixer_named_ctl:
2678 w->power_check = dapm_generic_check_power;
2679 break;
2680 case snd_soc_dapm_mux:
2681 case snd_soc_dapm_virt_mux:
2682 case snd_soc_dapm_value_mux:
2683 w->power_check = dapm_generic_check_power;
2684 break;
2685 case snd_soc_dapm_adc:
2686 case snd_soc_dapm_aif_out:
2687 w->power_check = dapm_adc_check_power;
2688 break;
2689 case snd_soc_dapm_dac:
2690 case snd_soc_dapm_aif_in:
2691 w->power_check = dapm_dac_check_power;
2692 break;
2693 case snd_soc_dapm_pga:
2694 case snd_soc_dapm_out_drv:
2695 case snd_soc_dapm_input:
2696 case snd_soc_dapm_output:
2697 case snd_soc_dapm_micbias:
2698 case snd_soc_dapm_spk:
2699 case snd_soc_dapm_hp:
2700 case snd_soc_dapm_mic:
2701 case snd_soc_dapm_line:
2702 w->power_check = dapm_generic_check_power;
2703 break;
2704 case snd_soc_dapm_supply:
2705 w->power_check = dapm_supply_check_power;
2706 break;
2707 default:
2708 w->power_check = dapm_always_on_check_power;
2709 break;
2712 dapm->n_widgets++;
2713 w->dapm = dapm;
2714 w->codec = dapm->codec;
2715 w->platform = dapm->platform;
2716 INIT_LIST_HEAD(&w->sources);
2717 INIT_LIST_HEAD(&w->sinks);
2718 INIT_LIST_HEAD(&w->list);
2719 INIT_LIST_HEAD(&w->dirty);
2720 list_add(&w->list, &dapm->card->widgets);
2722 /* machine layer set ups unconnected pins and insertions */
2723 w->connected = 1;
2724 return 0;
2726 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2729 * snd_soc_dapm_new_controls - create new dapm controls
2730 * @dapm: DAPM context
2731 * @widget: widget array
2732 * @num: number of widgets
2734 * Creates new DAPM controls based upon the templates.
2736 * Returns 0 for success else error.
2738 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2739 const struct snd_soc_dapm_widget *widget,
2740 int num)
2742 int i, ret;
2744 for (i = 0; i < num; i++) {
2745 ret = snd_soc_dapm_new_control(dapm, widget);
2746 if (ret < 0) {
2747 dev_err(dapm->dev,
2748 "ASoC: Failed to create DAPM control %s: %d\n",
2749 widget->name, ret);
2750 return ret;
2752 widget++;
2754 return 0;
2756 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2758 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2759 const char *stream, int event)
2761 struct snd_soc_dapm_widget *w;
2763 list_for_each_entry(w, &dapm->card->widgets, list)
2765 if (!w->sname || w->dapm != dapm)
2766 continue;
2767 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2768 w->name, w->sname, stream, event);
2769 if (strstr(w->sname, stream)) {
2770 dapm_mark_dirty(w, "stream event");
2771 switch(event) {
2772 case SND_SOC_DAPM_STREAM_START:
2773 w->active = 1;
2774 break;
2775 case SND_SOC_DAPM_STREAM_STOP:
2776 w->active = 0;
2777 break;
2778 case SND_SOC_DAPM_STREAM_SUSPEND:
2779 case SND_SOC_DAPM_STREAM_RESUME:
2780 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2781 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2782 break;
2787 dapm_power_widgets(dapm, event);
2789 /* do we need to notify any clients that DAPM stream is complete */
2790 if (dapm->stream_event)
2791 dapm->stream_event(dapm, event);
2795 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2796 * @rtd: PCM runtime data
2797 * @stream: stream name
2798 * @event: stream event
2800 * Sends a stream event to the dapm core. The core then makes any
2801 * necessary widget power changes.
2803 * Returns 0 for success else error.
2805 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2806 const char *stream, int event)
2808 struct snd_soc_codec *codec = rtd->codec;
2810 if (stream == NULL)
2811 return 0;
2813 mutex_lock(&codec->mutex);
2814 soc_dapm_stream_event(&codec->dapm, stream, event);
2815 mutex_unlock(&codec->mutex);
2816 return 0;
2820 * snd_soc_dapm_enable_pin - enable pin.
2821 * @dapm: DAPM context
2822 * @pin: pin name
2824 * Enables input/output pin and its parents or children widgets iff there is
2825 * a valid audio route and active audio stream.
2826 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2827 * do any widget power switching.
2829 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2831 return snd_soc_dapm_set_pin(dapm, pin, 1);
2833 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2836 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2837 * @dapm: DAPM context
2838 * @pin: pin name
2840 * Enables input/output pin regardless of any other state. This is
2841 * intended for use with microphone bias supplies used in microphone
2842 * jack detection.
2844 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2845 * do any widget power switching.
2847 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2848 const char *pin)
2850 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2852 if (!w) {
2853 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2854 return -EINVAL;
2857 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2858 w->connected = 1;
2859 w->force = 1;
2860 dapm_mark_dirty(w, "force enable");
2862 return 0;
2864 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2867 * snd_soc_dapm_disable_pin - disable pin.
2868 * @dapm: DAPM context
2869 * @pin: pin name
2871 * Disables input/output pin and its parents or children widgets.
2872 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2873 * do any widget power switching.
2875 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2876 const char *pin)
2878 return snd_soc_dapm_set_pin(dapm, pin, 0);
2880 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2883 * snd_soc_dapm_nc_pin - permanently disable pin.
2884 * @dapm: DAPM context
2885 * @pin: pin name
2887 * Marks the specified pin as being not connected, disabling it along
2888 * any parent or child widgets. At present this is identical to
2889 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2890 * additional things such as disabling controls which only affect
2891 * paths through the pin.
2893 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2894 * do any widget power switching.
2896 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2898 return snd_soc_dapm_set_pin(dapm, pin, 0);
2900 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2903 * snd_soc_dapm_get_pin_status - get audio pin status
2904 * @dapm: DAPM context
2905 * @pin: audio signal pin endpoint (or start point)
2907 * Get audio pin status - connected or disconnected.
2909 * Returns 1 for connected otherwise 0.
2911 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2912 const char *pin)
2914 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2916 if (w)
2917 return w->connected;
2919 return 0;
2921 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2924 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2925 * @dapm: DAPM context
2926 * @pin: audio signal pin endpoint (or start point)
2928 * Mark the given endpoint or pin as ignoring suspend. When the
2929 * system is disabled a path between two endpoints flagged as ignoring
2930 * suspend will not be disabled. The path must already be enabled via
2931 * normal means at suspend time, it will not be turned on if it was not
2932 * already enabled.
2934 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2935 const char *pin)
2937 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
2939 if (!w) {
2940 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2941 return -EINVAL;
2944 w->ignore_suspend = 1;
2946 return 0;
2948 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2951 * snd_soc_dapm_free - free dapm resources
2952 * @dapm: DAPM context
2954 * Free all dapm widgets and resources.
2956 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2958 snd_soc_dapm_sys_remove(dapm->dev);
2959 dapm_debugfs_cleanup(dapm);
2960 dapm_free_widgets(dapm);
2961 list_del(&dapm->list);
2963 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2965 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
2967 struct snd_soc_dapm_widget *w;
2968 LIST_HEAD(down_list);
2969 int powerdown = 0;
2971 list_for_each_entry(w, &dapm->card->widgets, list) {
2972 if (w->dapm != dapm)
2973 continue;
2974 if (w->power) {
2975 dapm_seq_insert(w, &down_list, false);
2976 w->power = 0;
2977 powerdown = 1;
2981 /* If there were no widgets to power down we're already in
2982 * standby.
2984 if (powerdown) {
2985 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
2986 dapm_seq_run(dapm, &down_list, 0, false);
2987 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
2992 * snd_soc_dapm_shutdown - callback for system shutdown
2994 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2996 struct snd_soc_codec *codec;
2998 list_for_each_entry(codec, &card->codec_dev_list, list) {
2999 soc_dapm_shutdown_codec(&codec->dapm);
3000 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
3004 /* Module information */
3005 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3006 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3007 MODULE_LICENSE("GPL");