drm/radeon/kms: fix backend map typo on juniper
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / soc / soc-dapm.c
blob586d10b57b758852adc9ac87bfbc682f9793af7b
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 /* dapm power sequences - make this per codec in the future */
52 static int dapm_up_seq[] = {
53 [snd_soc_dapm_pre] = 0,
54 [snd_soc_dapm_supply] = 1,
55 [snd_soc_dapm_micbias] = 2,
56 [snd_soc_dapm_aif_in] = 3,
57 [snd_soc_dapm_aif_out] = 3,
58 [snd_soc_dapm_mic] = 4,
59 [snd_soc_dapm_mux] = 5,
60 [snd_soc_dapm_virt_mux] = 5,
61 [snd_soc_dapm_value_mux] = 5,
62 [snd_soc_dapm_dac] = 6,
63 [snd_soc_dapm_mixer] = 7,
64 [snd_soc_dapm_mixer_named_ctl] = 7,
65 [snd_soc_dapm_pga] = 8,
66 [snd_soc_dapm_adc] = 9,
67 [snd_soc_dapm_out_drv] = 10,
68 [snd_soc_dapm_hp] = 10,
69 [snd_soc_dapm_spk] = 10,
70 [snd_soc_dapm_post] = 11,
73 static int dapm_down_seq[] = {
74 [snd_soc_dapm_pre] = 0,
75 [snd_soc_dapm_adc] = 1,
76 [snd_soc_dapm_hp] = 2,
77 [snd_soc_dapm_spk] = 2,
78 [snd_soc_dapm_out_drv] = 2,
79 [snd_soc_dapm_pga] = 4,
80 [snd_soc_dapm_mixer_named_ctl] = 5,
81 [snd_soc_dapm_mixer] = 5,
82 [snd_soc_dapm_dac] = 6,
83 [snd_soc_dapm_mic] = 7,
84 [snd_soc_dapm_micbias] = 8,
85 [snd_soc_dapm_mux] = 9,
86 [snd_soc_dapm_virt_mux] = 9,
87 [snd_soc_dapm_value_mux] = 9,
88 [snd_soc_dapm_aif_in] = 10,
89 [snd_soc_dapm_aif_out] = 10,
90 [snd_soc_dapm_supply] = 11,
91 [snd_soc_dapm_post] = 12,
94 static void pop_wait(u32 pop_time)
96 if (pop_time)
97 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
100 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
102 va_list args;
103 char *buf;
105 if (!pop_time)
106 return;
108 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
109 if (buf == NULL)
110 return;
112 va_start(args, fmt);
113 vsnprintf(buf, PAGE_SIZE, fmt, args);
114 dev_info(dev, "%s", buf);
115 va_end(args);
117 kfree(buf);
120 /* create a new dapm widget */
121 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
122 const struct snd_soc_dapm_widget *_widget)
124 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
128 * snd_soc_dapm_set_bias_level - set the bias level for the system
129 * @dapm: DAPM context
130 * @level: level to configure
132 * Configure the bias (power) levels for the SoC audio device.
134 * Returns 0 for success else error.
136 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
137 enum snd_soc_bias_level level)
139 struct snd_soc_card *card = dapm->card;
140 int ret = 0;
142 switch (level) {
143 case SND_SOC_BIAS_ON:
144 dev_dbg(dapm->dev, "Setting full bias\n");
145 break;
146 case SND_SOC_BIAS_PREPARE:
147 dev_dbg(dapm->dev, "Setting bias prepare\n");
148 break;
149 case SND_SOC_BIAS_STANDBY:
150 dev_dbg(dapm->dev, "Setting standby bias\n");
151 break;
152 case SND_SOC_BIAS_OFF:
153 dev_dbg(dapm->dev, "Setting bias off\n");
154 break;
155 default:
156 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
157 return -EINVAL;
160 trace_snd_soc_bias_level_start(card, level);
162 if (card && card->set_bias_level)
163 ret = card->set_bias_level(card, level);
164 if (ret == 0) {
165 if (dapm->codec && dapm->codec->driver->set_bias_level)
166 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
167 else
168 dapm->bias_level = level;
170 if (ret == 0) {
171 if (card && card->set_bias_level_post)
172 ret = card->set_bias_level_post(card, level);
175 trace_snd_soc_bias_level_done(card, level);
177 return ret;
180 /* set up initial codec paths */
181 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
182 struct snd_soc_dapm_path *p, int i)
184 switch (w->id) {
185 case snd_soc_dapm_switch:
186 case snd_soc_dapm_mixer:
187 case snd_soc_dapm_mixer_named_ctl: {
188 int val;
189 struct soc_mixer_control *mc = (struct soc_mixer_control *)
190 w->kcontrols[i].private_value;
191 unsigned int reg = mc->reg;
192 unsigned int shift = mc->shift;
193 int max = mc->max;
194 unsigned int mask = (1 << fls(max)) - 1;
195 unsigned int invert = mc->invert;
197 val = snd_soc_read(w->codec, reg);
198 val = (val >> shift) & mask;
200 if ((invert && !val) || (!invert && val))
201 p->connect = 1;
202 else
203 p->connect = 0;
205 break;
206 case snd_soc_dapm_mux: {
207 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
208 int val, item, bitmask;
210 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
212 val = snd_soc_read(w->codec, e->reg);
213 item = (val >> e->shift_l) & (bitmask - 1);
215 p->connect = 0;
216 for (i = 0; i < e->max; i++) {
217 if (!(strcmp(p->name, e->texts[i])) && item == i)
218 p->connect = 1;
221 break;
222 case snd_soc_dapm_virt_mux: {
223 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
225 p->connect = 0;
226 /* since a virtual mux has no backing registers to
227 * decide which path to connect, it will try to match
228 * with the first enumeration. This is to ensure
229 * that the default mux choice (the first) will be
230 * correctly powered up during initialization.
232 if (!strcmp(p->name, e->texts[0]))
233 p->connect = 1;
235 break;
236 case snd_soc_dapm_value_mux: {
237 struct soc_enum *e = (struct soc_enum *)
238 w->kcontrols[i].private_value;
239 int val, item;
241 val = snd_soc_read(w->codec, e->reg);
242 val = (val >> e->shift_l) & e->mask;
243 for (item = 0; item < e->max; item++) {
244 if (val == e->values[item])
245 break;
248 p->connect = 0;
249 for (i = 0; i < e->max; i++) {
250 if (!(strcmp(p->name, e->texts[i])) && item == i)
251 p->connect = 1;
254 break;
255 /* does not effect routing - always connected */
256 case snd_soc_dapm_pga:
257 case snd_soc_dapm_out_drv:
258 case snd_soc_dapm_output:
259 case snd_soc_dapm_adc:
260 case snd_soc_dapm_input:
261 case snd_soc_dapm_dac:
262 case snd_soc_dapm_micbias:
263 case snd_soc_dapm_vmid:
264 case snd_soc_dapm_supply:
265 case snd_soc_dapm_aif_in:
266 case snd_soc_dapm_aif_out:
267 p->connect = 1;
268 break;
269 /* does effect routing - dynamically connected */
270 case snd_soc_dapm_hp:
271 case snd_soc_dapm_mic:
272 case snd_soc_dapm_spk:
273 case snd_soc_dapm_line:
274 case snd_soc_dapm_pre:
275 case snd_soc_dapm_post:
276 p->connect = 0;
277 break;
281 /* connect mux widget to its interconnecting audio paths */
282 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
283 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
284 struct snd_soc_dapm_path *path, const char *control_name,
285 const struct snd_kcontrol_new *kcontrol)
287 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
288 int i;
290 for (i = 0; i < e->max; i++) {
291 if (!(strcmp(control_name, e->texts[i]))) {
292 list_add(&path->list, &dapm->card->paths);
293 list_add(&path->list_sink, &dest->sources);
294 list_add(&path->list_source, &src->sinks);
295 path->name = (char*)e->texts[i];
296 dapm_set_path_status(dest, path, 0);
297 return 0;
301 return -ENODEV;
304 /* connect mixer widget to its interconnecting audio paths */
305 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
306 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
307 struct snd_soc_dapm_path *path, const char *control_name)
309 int i;
311 /* search for mixer kcontrol */
312 for (i = 0; i < dest->num_kcontrols; i++) {
313 if (!strcmp(control_name, dest->kcontrols[i].name)) {
314 list_add(&path->list, &dapm->card->paths);
315 list_add(&path->list_sink, &dest->sources);
316 list_add(&path->list_source, &src->sinks);
317 path->name = dest->kcontrols[i].name;
318 dapm_set_path_status(dest, path, i);
319 return 0;
322 return -ENODEV;
325 /* update dapm codec register bits */
326 static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
328 int change, power;
329 unsigned int old, new;
330 struct snd_soc_codec *codec = widget->codec;
331 struct snd_soc_dapm_context *dapm = widget->dapm;
332 struct snd_soc_card *card = dapm->card;
334 /* check for valid widgets */
335 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
336 widget->id == snd_soc_dapm_output ||
337 widget->id == snd_soc_dapm_hp ||
338 widget->id == snd_soc_dapm_mic ||
339 widget->id == snd_soc_dapm_line ||
340 widget->id == snd_soc_dapm_spk)
341 return 0;
343 power = widget->power;
344 if (widget->invert)
345 power = (power ? 0:1);
347 old = snd_soc_read(codec, widget->reg);
348 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
350 change = old != new;
351 if (change) {
352 pop_dbg(dapm->dev, card->pop_time,
353 "pop test %s : %s in %d ms\n",
354 widget->name, widget->power ? "on" : "off",
355 card->pop_time);
356 pop_wait(card->pop_time);
357 snd_soc_write(codec, widget->reg, new);
359 dev_dbg(dapm->dev, "reg %x old %x new %x change %d\n", widget->reg,
360 old, new, change);
361 return change;
364 /* create new dapm mixer control */
365 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
367 struct snd_soc_dapm_context *dapm = w->dapm;
368 int i, ret = 0;
369 size_t name_len, prefix_len;
370 struct snd_soc_dapm_path *path;
371 struct snd_card *card = dapm->card->snd_card;
372 const char *prefix;
374 if (dapm->codec)
375 prefix = dapm->codec->name_prefix;
376 else
377 prefix = NULL;
379 if (prefix)
380 prefix_len = strlen(prefix) + 1;
381 else
382 prefix_len = 0;
384 /* add kcontrol */
385 for (i = 0; i < w->num_kcontrols; i++) {
387 /* match name */
388 list_for_each_entry(path, &w->sources, list_sink) {
390 /* mixer/mux paths name must match control name */
391 if (path->name != (char*)w->kcontrols[i].name)
392 continue;
394 /* add dapm control with long name.
395 * for dapm_mixer this is the concatenation of the
396 * mixer and kcontrol name.
397 * for dapm_mixer_named_ctl this is simply the
398 * kcontrol name.
400 name_len = strlen(w->kcontrols[i].name) + 1;
401 if (w->id != snd_soc_dapm_mixer_named_ctl)
402 name_len += 1 + strlen(w->name);
404 path->long_name = kmalloc(name_len, GFP_KERNEL);
406 if (path->long_name == NULL)
407 return -ENOMEM;
409 switch (w->id) {
410 default:
411 /* The control will get a prefix from
412 * the control creation process but
413 * we're also using the same prefix
414 * for widgets so cut the prefix off
415 * the front of the widget name.
417 snprintf(path->long_name, name_len, "%s %s",
418 w->name + prefix_len,
419 w->kcontrols[i].name);
420 break;
421 case snd_soc_dapm_mixer_named_ctl:
422 snprintf(path->long_name, name_len, "%s",
423 w->kcontrols[i].name);
424 break;
427 path->long_name[name_len - 1] = '\0';
429 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
430 path->long_name, prefix);
431 ret = snd_ctl_add(card, path->kcontrol);
432 if (ret < 0) {
433 dev_err(dapm->dev,
434 "asoc: failed to add dapm kcontrol %s: %d\n",
435 path->long_name, ret);
436 kfree(path->long_name);
437 path->long_name = NULL;
438 return ret;
442 return ret;
445 /* create new dapm mux control */
446 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
448 struct snd_soc_dapm_context *dapm = w->dapm;
449 struct snd_soc_dapm_path *path = NULL;
450 struct snd_kcontrol *kcontrol;
451 struct snd_card *card = dapm->card->snd_card;
452 const char *prefix;
453 size_t prefix_len;
454 int ret = 0;
456 if (!w->num_kcontrols) {
457 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
458 return -EINVAL;
461 if (dapm->codec)
462 prefix = dapm->codec->name_prefix;
463 else
464 prefix = NULL;
466 if (prefix)
467 prefix_len = strlen(prefix) + 1;
468 else
469 prefix_len = 0;
471 /* The control will get a prefix from the control creation
472 * process but we're also using the same prefix for widgets so
473 * cut the prefix off the front of the widget name.
475 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name + prefix_len,
476 prefix);
477 ret = snd_ctl_add(card, kcontrol);
479 if (ret < 0)
480 goto err;
482 list_for_each_entry(path, &w->sources, list_sink)
483 path->kcontrol = kcontrol;
485 return ret;
487 err:
488 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
489 return ret;
492 /* create new dapm volume control */
493 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
495 if (w->num_kcontrols)
496 dev_err(w->dapm->dev,
497 "asoc: PGA controls not supported: '%s'\n", w->name);
499 return 0;
502 /* reset 'walked' bit for each dapm path */
503 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
505 struct snd_soc_dapm_path *p;
507 list_for_each_entry(p, &dapm->card->paths, list)
508 p->walked = 0;
511 /* We implement power down on suspend by checking the power state of
512 * the ALSA card - when we are suspending the ALSA state for the card
513 * is set to D3.
515 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
517 int level = snd_power_get_state(widget->dapm->card->snd_card);
519 switch (level) {
520 case SNDRV_CTL_POWER_D3hot:
521 case SNDRV_CTL_POWER_D3cold:
522 if (widget->ignore_suspend)
523 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
524 widget->name);
525 return widget->ignore_suspend;
526 default:
527 return 1;
532 * Recursively check for a completed path to an active or physically connected
533 * output widget. Returns number of complete paths.
535 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
537 struct snd_soc_dapm_path *path;
538 int con = 0;
540 if (widget->id == snd_soc_dapm_supply)
541 return 0;
543 switch (widget->id) {
544 case snd_soc_dapm_adc:
545 case snd_soc_dapm_aif_out:
546 if (widget->active)
547 return snd_soc_dapm_suspend_check(widget);
548 default:
549 break;
552 if (widget->connected) {
553 /* connected pin ? */
554 if (widget->id == snd_soc_dapm_output && !widget->ext)
555 return snd_soc_dapm_suspend_check(widget);
557 /* connected jack or spk ? */
558 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
559 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
560 return snd_soc_dapm_suspend_check(widget);
563 list_for_each_entry(path, &widget->sinks, list_source) {
564 if (path->walked)
565 continue;
567 if (path->sink && path->connect) {
568 path->walked = 1;
569 con += is_connected_output_ep(path->sink);
573 return con;
577 * Recursively check for a completed path to an active or physically connected
578 * input widget. Returns number of complete paths.
580 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
582 struct snd_soc_dapm_path *path;
583 int con = 0;
585 if (widget->id == snd_soc_dapm_supply)
586 return 0;
588 /* active stream ? */
589 switch (widget->id) {
590 case snd_soc_dapm_dac:
591 case snd_soc_dapm_aif_in:
592 if (widget->active)
593 return snd_soc_dapm_suspend_check(widget);
594 default:
595 break;
598 if (widget->connected) {
599 /* connected pin ? */
600 if (widget->id == snd_soc_dapm_input && !widget->ext)
601 return snd_soc_dapm_suspend_check(widget);
603 /* connected VMID/Bias for lower pops */
604 if (widget->id == snd_soc_dapm_vmid)
605 return snd_soc_dapm_suspend_check(widget);
607 /* connected jack ? */
608 if (widget->id == snd_soc_dapm_mic ||
609 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
610 return snd_soc_dapm_suspend_check(widget);
613 list_for_each_entry(path, &widget->sources, list_sink) {
614 if (path->walked)
615 continue;
617 if (path->source && path->connect) {
618 path->walked = 1;
619 con += is_connected_input_ep(path->source);
623 return con;
627 * Handler for generic register modifier widget.
629 int dapm_reg_event(struct snd_soc_dapm_widget *w,
630 struct snd_kcontrol *kcontrol, int event)
632 unsigned int val;
634 if (SND_SOC_DAPM_EVENT_ON(event))
635 val = w->on_val;
636 else
637 val = w->off_val;
639 snd_soc_update_bits(w->codec, -(w->reg + 1),
640 w->mask << w->shift, val << w->shift);
642 return 0;
644 EXPORT_SYMBOL_GPL(dapm_reg_event);
646 /* Standard power change method, used to apply power changes to most
647 * widgets.
649 static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
651 int ret;
653 /* call any power change event handlers */
654 if (w->event)
655 dev_dbg(w->dapm->dev, "power %s event for %s flags %x\n",
656 w->power ? "on" : "off",
657 w->name, w->event_flags);
659 /* power up pre event */
660 if (w->power && w->event &&
661 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
662 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
663 if (ret < 0)
664 return ret;
667 /* power down pre event */
668 if (!w->power && w->event &&
669 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
670 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
671 if (ret < 0)
672 return ret;
675 dapm_update_bits(w);
677 /* power up post event */
678 if (w->power && w->event &&
679 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
680 ret = w->event(w,
681 NULL, SND_SOC_DAPM_POST_PMU);
682 if (ret < 0)
683 return ret;
686 /* power down post event */
687 if (!w->power && w->event &&
688 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
689 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
690 if (ret < 0)
691 return ret;
694 return 0;
697 /* Generic check to see if a widget should be powered.
699 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
701 int in, out;
703 in = is_connected_input_ep(w);
704 dapm_clear_walk(w->dapm);
705 out = is_connected_output_ep(w);
706 dapm_clear_walk(w->dapm);
707 return out != 0 && in != 0;
710 /* Check to see if an ADC has power */
711 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
713 int in;
715 if (w->active) {
716 in = is_connected_input_ep(w);
717 dapm_clear_walk(w->dapm);
718 return in != 0;
719 } else {
720 return dapm_generic_check_power(w);
724 /* Check to see if a DAC has power */
725 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
727 int out;
729 if (w->active) {
730 out = is_connected_output_ep(w);
731 dapm_clear_walk(w->dapm);
732 return out != 0;
733 } else {
734 return dapm_generic_check_power(w);
738 /* Check to see if a power supply is needed */
739 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
741 struct snd_soc_dapm_path *path;
742 int power = 0;
744 /* Check if one of our outputs is connected */
745 list_for_each_entry(path, &w->sinks, list_source) {
746 if (path->connected &&
747 !path->connected(path->source, path->sink))
748 continue;
750 if (!path->sink)
751 continue;
753 if (path->sink->force) {
754 power = 1;
755 break;
758 if (path->sink->power_check &&
759 path->sink->power_check(path->sink)) {
760 power = 1;
761 break;
765 dapm_clear_walk(w->dapm);
767 return power;
770 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
771 struct snd_soc_dapm_widget *b,
772 bool power_up)
774 int *sort;
776 if (power_up)
777 sort = dapm_up_seq;
778 else
779 sort = dapm_down_seq;
781 if (sort[a->id] != sort[b->id])
782 return sort[a->id] - sort[b->id];
783 if (a->subseq != b->subseq) {
784 if (power_up)
785 return a->subseq - b->subseq;
786 else
787 return b->subseq - a->subseq;
789 if (a->reg != b->reg)
790 return a->reg - b->reg;
791 if (a->dapm != b->dapm)
792 return (unsigned long)a->dapm - (unsigned long)b->dapm;
794 return 0;
797 /* Insert a widget in order into a DAPM power sequence. */
798 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
799 struct list_head *list,
800 bool power_up)
802 struct snd_soc_dapm_widget *w;
804 list_for_each_entry(w, list, power_list)
805 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
806 list_add_tail(&new_widget->power_list, &w->power_list);
807 return;
810 list_add_tail(&new_widget->power_list, list);
813 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
814 struct snd_soc_dapm_widget *w, int event)
816 struct snd_soc_card *card = dapm->card;
817 const char *ev_name;
818 int power, ret;
820 switch (event) {
821 case SND_SOC_DAPM_PRE_PMU:
822 ev_name = "PRE_PMU";
823 power = 1;
824 break;
825 case SND_SOC_DAPM_POST_PMU:
826 ev_name = "POST_PMU";
827 power = 1;
828 break;
829 case SND_SOC_DAPM_PRE_PMD:
830 ev_name = "PRE_PMD";
831 power = 0;
832 break;
833 case SND_SOC_DAPM_POST_PMD:
834 ev_name = "POST_PMD";
835 power = 0;
836 break;
837 default:
838 BUG();
839 return;
842 if (w->power != power)
843 return;
845 if (w->event && (w->event_flags & event)) {
846 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
847 w->name, ev_name);
848 trace_snd_soc_dapm_widget_event_start(w, event);
849 ret = w->event(w, NULL, event);
850 trace_snd_soc_dapm_widget_event_done(w, event);
851 if (ret < 0)
852 pr_err("%s: %s event failed: %d\n",
853 ev_name, w->name, ret);
857 /* Apply the coalesced changes from a DAPM sequence */
858 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
859 struct list_head *pending)
861 struct snd_soc_card *card = dapm->card;
862 struct snd_soc_dapm_widget *w;
863 int reg, power;
864 unsigned int value = 0;
865 unsigned int mask = 0;
866 unsigned int cur_mask;
868 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
869 power_list)->reg;
871 list_for_each_entry(w, pending, power_list) {
872 cur_mask = 1 << w->shift;
873 BUG_ON(reg != w->reg);
875 if (w->invert)
876 power = !w->power;
877 else
878 power = w->power;
880 mask |= cur_mask;
881 if (power)
882 value |= cur_mask;
884 pop_dbg(dapm->dev, card->pop_time,
885 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
886 w->name, reg, value, mask);
888 /* Check for events */
889 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
890 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
893 if (reg >= 0) {
894 pop_dbg(dapm->dev, card->pop_time,
895 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
896 value, mask, reg, card->pop_time);
897 pop_wait(card->pop_time);
898 snd_soc_update_bits(dapm->codec, reg, mask, value);
901 list_for_each_entry(w, pending, power_list) {
902 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
903 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
907 /* Apply a DAPM power sequence.
909 * We walk over a pre-sorted list of widgets to apply power to. In
910 * order to minimise the number of writes to the device required
911 * multiple widgets will be updated in a single write where possible.
912 * Currently anything that requires more than a single write is not
913 * handled.
915 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
916 struct list_head *list, int event, bool power_up)
918 struct snd_soc_dapm_widget *w, *n;
919 LIST_HEAD(pending);
920 int cur_sort = -1;
921 int cur_subseq = -1;
922 int cur_reg = SND_SOC_NOPM;
923 struct snd_soc_dapm_context *cur_dapm = NULL;
924 int ret, i;
925 int *sort;
927 if (power_up)
928 sort = dapm_up_seq;
929 else
930 sort = dapm_down_seq;
932 list_for_each_entry_safe(w, n, list, power_list) {
933 ret = 0;
935 /* Do we need to apply any queued changes? */
936 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
937 w->dapm != cur_dapm || w->subseq != cur_subseq) {
938 if (!list_empty(&pending))
939 dapm_seq_run_coalesced(cur_dapm, &pending);
941 if (cur_dapm && cur_dapm->seq_notifier) {
942 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
943 if (sort[i] == cur_sort)
944 cur_dapm->seq_notifier(cur_dapm,
946 cur_subseq);
949 INIT_LIST_HEAD(&pending);
950 cur_sort = -1;
951 cur_subseq = -1;
952 cur_reg = SND_SOC_NOPM;
953 cur_dapm = NULL;
956 switch (w->id) {
957 case snd_soc_dapm_pre:
958 if (!w->event)
959 list_for_each_entry_safe_continue(w, n, list,
960 power_list);
962 if (event == SND_SOC_DAPM_STREAM_START)
963 ret = w->event(w,
964 NULL, SND_SOC_DAPM_PRE_PMU);
965 else if (event == SND_SOC_DAPM_STREAM_STOP)
966 ret = w->event(w,
967 NULL, SND_SOC_DAPM_PRE_PMD);
968 break;
970 case snd_soc_dapm_post:
971 if (!w->event)
972 list_for_each_entry_safe_continue(w, n, list,
973 power_list);
975 if (event == SND_SOC_DAPM_STREAM_START)
976 ret = w->event(w,
977 NULL, SND_SOC_DAPM_POST_PMU);
978 else if (event == SND_SOC_DAPM_STREAM_STOP)
979 ret = w->event(w,
980 NULL, SND_SOC_DAPM_POST_PMD);
981 break;
983 case snd_soc_dapm_input:
984 case snd_soc_dapm_output:
985 case snd_soc_dapm_hp:
986 case snd_soc_dapm_mic:
987 case snd_soc_dapm_line:
988 case snd_soc_dapm_spk:
989 /* No register support currently */
990 ret = dapm_generic_apply_power(w);
991 break;
993 default:
994 /* Queue it up for application */
995 cur_sort = sort[w->id];
996 cur_subseq = w->subseq;
997 cur_reg = w->reg;
998 cur_dapm = w->dapm;
999 list_move(&w->power_list, &pending);
1000 break;
1003 if (ret < 0)
1004 dev_err(w->dapm->dev,
1005 "Failed to apply widget power: %d\n", ret);
1008 if (!list_empty(&pending))
1009 dapm_seq_run_coalesced(cur_dapm, &pending);
1011 if (cur_dapm && cur_dapm->seq_notifier) {
1012 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1013 if (sort[i] == cur_sort)
1014 cur_dapm->seq_notifier(cur_dapm,
1015 i, cur_subseq);
1019 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1021 struct snd_soc_dapm_update *update = dapm->update;
1022 struct snd_soc_dapm_widget *w;
1023 int ret;
1025 if (!update)
1026 return;
1028 w = update->widget;
1030 if (w->event &&
1031 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1032 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1033 if (ret != 0)
1034 pr_err("%s DAPM pre-event failed: %d\n",
1035 w->name, ret);
1038 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1039 update->val);
1040 if (ret < 0)
1041 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1043 if (w->event &&
1044 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1045 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1046 if (ret != 0)
1047 pr_err("%s DAPM post-event failed: %d\n",
1048 w->name, ret);
1052 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1053 * they're changing state.
1055 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1057 struct snd_soc_dapm_context *d = data;
1058 int ret;
1060 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
1061 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1062 if (ret != 0)
1063 dev_err(d->dev,
1064 "Failed to turn on bias: %d\n", ret);
1067 /* If we're changing to all on or all off then prepare */
1068 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
1069 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
1070 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1071 if (ret != 0)
1072 dev_err(d->dev,
1073 "Failed to prepare bias: %d\n", ret);
1077 /* Async callback run prior to DAPM sequences - brings to their final
1078 * state.
1080 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1082 struct snd_soc_dapm_context *d = data;
1083 int ret;
1085 /* If we just powered the last thing off drop to standby bias */
1086 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
1087 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1088 if (ret != 0)
1089 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1090 ret);
1093 /* If we're in standby and can support bias off then do that */
1094 if (d->bias_level == SND_SOC_BIAS_STANDBY && d->idle_bias_off) {
1095 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1096 if (ret != 0)
1097 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1100 /* If we just powered up then move to active bias */
1101 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1102 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1103 if (ret != 0)
1104 dev_err(d->dev, "Failed to apply active bias: %d\n",
1105 ret);
1110 * Scan each dapm widget for complete audio path.
1111 * A complete path is a route that has valid endpoints i.e.:-
1113 * o DAC to output pin.
1114 * o Input Pin to ADC.
1115 * o Input pin to Output pin (bypass, sidetone)
1116 * o DAC to ADC (loopback).
1118 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1120 struct snd_soc_card *card = dapm->card;
1121 struct snd_soc_dapm_widget *w;
1122 struct snd_soc_dapm_context *d;
1123 LIST_HEAD(up_list);
1124 LIST_HEAD(down_list);
1125 LIST_HEAD(async_domain);
1126 int power;
1128 trace_snd_soc_dapm_start(card);
1130 list_for_each_entry(d, &card->dapm_list, list)
1131 if (d->n_widgets)
1132 d->dev_power = 0;
1134 /* Check which widgets we need to power and store them in
1135 * lists indicating if they should be powered up or down.
1137 list_for_each_entry(w, &card->widgets, list) {
1138 switch (w->id) {
1139 case snd_soc_dapm_pre:
1140 dapm_seq_insert(w, &down_list, false);
1141 break;
1142 case snd_soc_dapm_post:
1143 dapm_seq_insert(w, &up_list, true);
1144 break;
1146 default:
1147 if (!w->power_check)
1148 continue;
1150 if (!w->force)
1151 power = w->power_check(w);
1152 else
1153 power = 1;
1154 if (power)
1155 w->dapm->dev_power = 1;
1157 if (w->power == power)
1158 continue;
1160 trace_snd_soc_dapm_widget_power(w, power);
1162 if (power)
1163 dapm_seq_insert(w, &up_list, true);
1164 else
1165 dapm_seq_insert(w, &down_list, false);
1167 w->power = power;
1168 break;
1172 /* If there are no DAPM widgets then try to figure out power from the
1173 * event type.
1175 if (!dapm->n_widgets) {
1176 switch (event) {
1177 case SND_SOC_DAPM_STREAM_START:
1178 case SND_SOC_DAPM_STREAM_RESUME:
1179 dapm->dev_power = 1;
1180 break;
1181 case SND_SOC_DAPM_STREAM_STOP:
1182 dapm->dev_power = !!dapm->codec->active;
1183 break;
1184 case SND_SOC_DAPM_STREAM_SUSPEND:
1185 dapm->dev_power = 0;
1186 break;
1187 case SND_SOC_DAPM_STREAM_NOP:
1188 switch (dapm->bias_level) {
1189 case SND_SOC_BIAS_STANDBY:
1190 case SND_SOC_BIAS_OFF:
1191 dapm->dev_power = 0;
1192 break;
1193 default:
1194 dapm->dev_power = 1;
1195 break;
1197 break;
1198 default:
1199 break;
1203 /* Run all the bias changes in parallel */
1204 list_for_each_entry(d, &dapm->card->dapm_list, list)
1205 async_schedule_domain(dapm_pre_sequence_async, d,
1206 &async_domain);
1207 async_synchronize_full_domain(&async_domain);
1209 /* Power down widgets first; try to avoid amplifying pops. */
1210 dapm_seq_run(dapm, &down_list, event, false);
1212 dapm_widget_update(dapm);
1214 /* Now power up. */
1215 dapm_seq_run(dapm, &up_list, event, true);
1217 /* Run all the bias changes in parallel */
1218 list_for_each_entry(d, &dapm->card->dapm_list, list)
1219 async_schedule_domain(dapm_post_sequence_async, d,
1220 &async_domain);
1221 async_synchronize_full_domain(&async_domain);
1223 pop_dbg(dapm->dev, card->pop_time,
1224 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1225 pop_wait(card->pop_time);
1227 trace_snd_soc_dapm_done(card);
1229 return 0;
1232 #ifdef CONFIG_DEBUG_FS
1233 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1235 file->private_data = inode->i_private;
1236 return 0;
1239 static ssize_t dapm_widget_power_read_file(struct file *file,
1240 char __user *user_buf,
1241 size_t count, loff_t *ppos)
1243 struct snd_soc_dapm_widget *w = file->private_data;
1244 char *buf;
1245 int in, out;
1246 ssize_t ret;
1247 struct snd_soc_dapm_path *p = NULL;
1249 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1250 if (!buf)
1251 return -ENOMEM;
1253 in = is_connected_input_ep(w);
1254 dapm_clear_walk(w->dapm);
1255 out = is_connected_output_ep(w);
1256 dapm_clear_walk(w->dapm);
1258 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1259 w->name, w->power ? "On" : "Off", in, out);
1261 if (w->reg >= 0)
1262 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1263 " - R%d(0x%x) bit %d",
1264 w->reg, w->reg, w->shift);
1266 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1268 if (w->sname)
1269 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1270 w->sname,
1271 w->active ? "active" : "inactive");
1273 list_for_each_entry(p, &w->sources, list_sink) {
1274 if (p->connected && !p->connected(w, p->sink))
1275 continue;
1277 if (p->connect)
1278 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1279 " in \"%s\" \"%s\"\n",
1280 p->name ? p->name : "static",
1281 p->source->name);
1283 list_for_each_entry(p, &w->sinks, list_source) {
1284 if (p->connected && !p->connected(w, p->sink))
1285 continue;
1287 if (p->connect)
1288 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1289 " out \"%s\" \"%s\"\n",
1290 p->name ? p->name : "static",
1291 p->sink->name);
1294 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1296 kfree(buf);
1297 return ret;
1300 static const struct file_operations dapm_widget_power_fops = {
1301 .open = dapm_widget_power_open_file,
1302 .read = dapm_widget_power_read_file,
1303 .llseek = default_llseek,
1306 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
1308 struct snd_soc_dapm_widget *w;
1309 struct dentry *d;
1311 if (!dapm->debugfs_dapm)
1312 return;
1314 list_for_each_entry(w, &dapm->card->widgets, list) {
1315 if (!w->name || w->dapm != dapm)
1316 continue;
1318 d = debugfs_create_file(w->name, 0444,
1319 dapm->debugfs_dapm, w,
1320 &dapm_widget_power_fops);
1321 if (!d)
1322 dev_warn(w->dapm->dev,
1323 "ASoC: Failed to create %s debugfs file\n",
1324 w->name);
1327 #else
1328 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
1331 #endif
1333 /* test and update the power status of a mux widget */
1334 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1335 struct snd_kcontrol *kcontrol, int change,
1336 int mux, struct soc_enum *e)
1338 struct snd_soc_dapm_path *path;
1339 int found = 0;
1341 if (widget->id != snd_soc_dapm_mux &&
1342 widget->id != snd_soc_dapm_virt_mux &&
1343 widget->id != snd_soc_dapm_value_mux)
1344 return -ENODEV;
1346 if (!change)
1347 return 0;
1349 /* find dapm widget path assoc with kcontrol */
1350 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1351 if (path->kcontrol != kcontrol)
1352 continue;
1354 if (!path->name || !e->texts[mux])
1355 continue;
1357 found = 1;
1358 /* we now need to match the string in the enum to the path */
1359 if (!(strcmp(path->name, e->texts[mux])))
1360 path->connect = 1; /* new connection */
1361 else
1362 path->connect = 0; /* old connection must be powered down */
1365 if (found)
1366 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1368 return 0;
1371 /* test and update the power status of a mixer or switch widget */
1372 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1373 struct snd_kcontrol *kcontrol, int connect)
1375 struct snd_soc_dapm_path *path;
1376 int found = 0;
1378 if (widget->id != snd_soc_dapm_mixer &&
1379 widget->id != snd_soc_dapm_mixer_named_ctl &&
1380 widget->id != snd_soc_dapm_switch)
1381 return -ENODEV;
1383 /* find dapm widget path assoc with kcontrol */
1384 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1385 if (path->kcontrol != kcontrol)
1386 continue;
1388 /* found, now check type */
1389 found = 1;
1390 path->connect = connect;
1391 break;
1394 if (found)
1395 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1397 return 0;
1400 /* show dapm widget status in sys fs */
1401 static ssize_t dapm_widget_show(struct device *dev,
1402 struct device_attribute *attr, char *buf)
1404 struct snd_soc_pcm_runtime *rtd =
1405 container_of(dev, struct snd_soc_pcm_runtime, dev);
1406 struct snd_soc_codec *codec =rtd->codec;
1407 struct snd_soc_dapm_widget *w;
1408 int count = 0;
1409 char *state = "not set";
1411 list_for_each_entry(w, &codec->card->widgets, list) {
1412 if (w->dapm != &codec->dapm)
1413 continue;
1415 /* only display widgets that burnm power */
1416 switch (w->id) {
1417 case snd_soc_dapm_hp:
1418 case snd_soc_dapm_mic:
1419 case snd_soc_dapm_spk:
1420 case snd_soc_dapm_line:
1421 case snd_soc_dapm_micbias:
1422 case snd_soc_dapm_dac:
1423 case snd_soc_dapm_adc:
1424 case snd_soc_dapm_pga:
1425 case snd_soc_dapm_out_drv:
1426 case snd_soc_dapm_mixer:
1427 case snd_soc_dapm_mixer_named_ctl:
1428 case snd_soc_dapm_supply:
1429 if (w->name)
1430 count += sprintf(buf + count, "%s: %s\n",
1431 w->name, w->power ? "On":"Off");
1432 break;
1433 default:
1434 break;
1438 switch (codec->dapm.bias_level) {
1439 case SND_SOC_BIAS_ON:
1440 state = "On";
1441 break;
1442 case SND_SOC_BIAS_PREPARE:
1443 state = "Prepare";
1444 break;
1445 case SND_SOC_BIAS_STANDBY:
1446 state = "Standby";
1447 break;
1448 case SND_SOC_BIAS_OFF:
1449 state = "Off";
1450 break;
1452 count += sprintf(buf + count, "PM State: %s\n", state);
1454 return count;
1457 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1459 int snd_soc_dapm_sys_add(struct device *dev)
1461 return device_create_file(dev, &dev_attr_dapm_widget);
1464 static void snd_soc_dapm_sys_remove(struct device *dev)
1466 device_remove_file(dev, &dev_attr_dapm_widget);
1469 /* free all dapm widgets and resources */
1470 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1472 struct snd_soc_dapm_widget *w, *next_w;
1473 struct snd_soc_dapm_path *p, *next_p;
1475 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1476 if (w->dapm != dapm)
1477 continue;
1478 list_del(&w->list);
1480 * remove source and sink paths associated to this widget.
1481 * While removing the path, remove reference to it from both
1482 * source and sink widgets so that path is removed only once.
1484 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1485 list_del(&p->list_sink);
1486 list_del(&p->list_source);
1487 list_del(&p->list);
1488 kfree(p->long_name);
1489 kfree(p);
1491 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1492 list_del(&p->list_sink);
1493 list_del(&p->list_source);
1494 list_del(&p->list);
1495 kfree(p->long_name);
1496 kfree(p);
1498 kfree(w->name);
1499 kfree(w);
1503 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1504 const char *pin, int status)
1506 struct snd_soc_dapm_widget *w;
1508 list_for_each_entry(w, &dapm->card->widgets, list) {
1509 if (w->dapm != dapm)
1510 continue;
1511 if (!strcmp(w->name, pin)) {
1512 dev_dbg(w->dapm->dev, "dapm: pin %s = %d\n",
1513 pin, status);
1514 w->connected = status;
1515 /* Allow disabling of forced pins */
1516 if (status == 0)
1517 w->force = 0;
1518 return 0;
1522 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1523 return -EINVAL;
1527 * snd_soc_dapm_sync - scan and power dapm paths
1528 * @dapm: DAPM context
1530 * Walks all dapm audio paths and powers widgets according to their
1531 * stream or path usage.
1533 * Returns 0 for success.
1535 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1537 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1539 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1541 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1542 const struct snd_soc_dapm_route *route)
1544 struct snd_soc_dapm_path *path;
1545 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1546 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1547 const char *sink;
1548 const char *control = route->control;
1549 const char *source;
1550 char prefixed_sink[80];
1551 char prefixed_source[80];
1552 int ret = 0;
1554 if (dapm->codec && dapm->codec->name_prefix) {
1555 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1556 dapm->codec->name_prefix, route->sink);
1557 sink = prefixed_sink;
1558 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1559 dapm->codec->name_prefix, route->source);
1560 source = prefixed_source;
1561 } else {
1562 sink = route->sink;
1563 source = route->source;
1567 * find src and dest widgets over all widgets but favor a widget from
1568 * current DAPM context
1570 list_for_each_entry(w, &dapm->card->widgets, list) {
1571 if (!wsink && !(strcmp(w->name, sink))) {
1572 wtsink = w;
1573 if (w->dapm == dapm)
1574 wsink = w;
1575 continue;
1577 if (!wsource && !(strcmp(w->name, source))) {
1578 wtsource = w;
1579 if (w->dapm == dapm)
1580 wsource = w;
1583 /* use widget from another DAPM context if not found from this */
1584 if (!wsink)
1585 wsink = wtsink;
1586 if (!wsource)
1587 wsource = wtsource;
1589 if (wsource == NULL || wsink == NULL)
1590 return -ENODEV;
1592 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1593 if (!path)
1594 return -ENOMEM;
1596 path->source = wsource;
1597 path->sink = wsink;
1598 path->connected = route->connected;
1599 INIT_LIST_HEAD(&path->list);
1600 INIT_LIST_HEAD(&path->list_source);
1601 INIT_LIST_HEAD(&path->list_sink);
1603 /* check for external widgets */
1604 if (wsink->id == snd_soc_dapm_input) {
1605 if (wsource->id == snd_soc_dapm_micbias ||
1606 wsource->id == snd_soc_dapm_mic ||
1607 wsource->id == snd_soc_dapm_line ||
1608 wsource->id == snd_soc_dapm_output)
1609 wsink->ext = 1;
1611 if (wsource->id == snd_soc_dapm_output) {
1612 if (wsink->id == snd_soc_dapm_spk ||
1613 wsink->id == snd_soc_dapm_hp ||
1614 wsink->id == snd_soc_dapm_line ||
1615 wsink->id == snd_soc_dapm_input)
1616 wsource->ext = 1;
1619 /* connect static paths */
1620 if (control == NULL) {
1621 list_add(&path->list, &dapm->card->paths);
1622 list_add(&path->list_sink, &wsink->sources);
1623 list_add(&path->list_source, &wsource->sinks);
1624 path->connect = 1;
1625 return 0;
1628 /* connect dynamic paths */
1629 switch(wsink->id) {
1630 case snd_soc_dapm_adc:
1631 case snd_soc_dapm_dac:
1632 case snd_soc_dapm_pga:
1633 case snd_soc_dapm_out_drv:
1634 case snd_soc_dapm_input:
1635 case snd_soc_dapm_output:
1636 case snd_soc_dapm_micbias:
1637 case snd_soc_dapm_vmid:
1638 case snd_soc_dapm_pre:
1639 case snd_soc_dapm_post:
1640 case snd_soc_dapm_supply:
1641 case snd_soc_dapm_aif_in:
1642 case snd_soc_dapm_aif_out:
1643 list_add(&path->list, &dapm->card->paths);
1644 list_add(&path->list_sink, &wsink->sources);
1645 list_add(&path->list_source, &wsource->sinks);
1646 path->connect = 1;
1647 return 0;
1648 case snd_soc_dapm_mux:
1649 case snd_soc_dapm_virt_mux:
1650 case snd_soc_dapm_value_mux:
1651 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
1652 &wsink->kcontrols[0]);
1653 if (ret != 0)
1654 goto err;
1655 break;
1656 case snd_soc_dapm_switch:
1657 case snd_soc_dapm_mixer:
1658 case snd_soc_dapm_mixer_named_ctl:
1659 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
1660 if (ret != 0)
1661 goto err;
1662 break;
1663 case snd_soc_dapm_hp:
1664 case snd_soc_dapm_mic:
1665 case snd_soc_dapm_line:
1666 case snd_soc_dapm_spk:
1667 list_add(&path->list, &dapm->card->paths);
1668 list_add(&path->list_sink, &wsink->sources);
1669 list_add(&path->list_source, &wsource->sinks);
1670 path->connect = 0;
1671 return 0;
1673 return 0;
1675 err:
1676 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1677 source, control, sink);
1678 kfree(path);
1679 return ret;
1683 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1684 * @dapm: DAPM context
1685 * @route: audio routes
1686 * @num: number of routes
1688 * Connects 2 dapm widgets together via a named audio path. The sink is
1689 * the widget receiving the audio signal, whilst the source is the sender
1690 * of the audio signal.
1692 * Returns 0 for success else error. On error all resources can be freed
1693 * with a call to snd_soc_card_free().
1695 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
1696 const struct snd_soc_dapm_route *route, int num)
1698 int i, ret;
1700 for (i = 0; i < num; i++) {
1701 ret = snd_soc_dapm_add_route(dapm, route);
1702 if (ret < 0) {
1703 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1704 route->source, route->sink);
1705 return ret;
1707 route++;
1710 return 0;
1712 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1715 * snd_soc_dapm_new_widgets - add new dapm widgets
1716 * @dapm: DAPM context
1718 * Checks the codec for any new dapm widgets and creates them if found.
1720 * Returns 0 for success.
1722 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
1724 struct snd_soc_dapm_widget *w;
1725 unsigned int val;
1727 list_for_each_entry(w, &dapm->card->widgets, list)
1729 if (w->new)
1730 continue;
1732 switch(w->id) {
1733 case snd_soc_dapm_switch:
1734 case snd_soc_dapm_mixer:
1735 case snd_soc_dapm_mixer_named_ctl:
1736 w->power_check = dapm_generic_check_power;
1737 dapm_new_mixer(w);
1738 break;
1739 case snd_soc_dapm_mux:
1740 case snd_soc_dapm_virt_mux:
1741 case snd_soc_dapm_value_mux:
1742 w->power_check = dapm_generic_check_power;
1743 dapm_new_mux(w);
1744 break;
1745 case snd_soc_dapm_adc:
1746 case snd_soc_dapm_aif_out:
1747 w->power_check = dapm_adc_check_power;
1748 break;
1749 case snd_soc_dapm_dac:
1750 case snd_soc_dapm_aif_in:
1751 w->power_check = dapm_dac_check_power;
1752 break;
1753 case snd_soc_dapm_pga:
1754 case snd_soc_dapm_out_drv:
1755 w->power_check = dapm_generic_check_power;
1756 dapm_new_pga(w);
1757 break;
1758 case snd_soc_dapm_input:
1759 case snd_soc_dapm_output:
1760 case snd_soc_dapm_micbias:
1761 case snd_soc_dapm_spk:
1762 case snd_soc_dapm_hp:
1763 case snd_soc_dapm_mic:
1764 case snd_soc_dapm_line:
1765 w->power_check = dapm_generic_check_power;
1766 break;
1767 case snd_soc_dapm_supply:
1768 w->power_check = dapm_supply_check_power;
1769 case snd_soc_dapm_vmid:
1770 case snd_soc_dapm_pre:
1771 case snd_soc_dapm_post:
1772 break;
1775 /* Read the initial power state from the device */
1776 if (w->reg >= 0) {
1777 val = snd_soc_read(w->codec, w->reg);
1778 val &= 1 << w->shift;
1779 if (w->invert)
1780 val = !val;
1782 if (val)
1783 w->power = 1;
1786 w->new = 1;
1789 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1790 return 0;
1792 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1795 * snd_soc_dapm_get_volsw - dapm mixer get callback
1796 * @kcontrol: mixer control
1797 * @ucontrol: control element information
1799 * Callback to get the value of a dapm mixer control.
1801 * Returns 0 for success.
1803 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1804 struct snd_ctl_elem_value *ucontrol)
1806 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1807 struct soc_mixer_control *mc =
1808 (struct soc_mixer_control *)kcontrol->private_value;
1809 unsigned int reg = mc->reg;
1810 unsigned int shift = mc->shift;
1811 unsigned int rshift = mc->rshift;
1812 int max = mc->max;
1813 unsigned int invert = mc->invert;
1814 unsigned int mask = (1 << fls(max)) - 1;
1816 ucontrol->value.integer.value[0] =
1817 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1818 if (shift != rshift)
1819 ucontrol->value.integer.value[1] =
1820 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1821 if (invert) {
1822 ucontrol->value.integer.value[0] =
1823 max - ucontrol->value.integer.value[0];
1824 if (shift != rshift)
1825 ucontrol->value.integer.value[1] =
1826 max - ucontrol->value.integer.value[1];
1829 return 0;
1831 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1834 * snd_soc_dapm_put_volsw - dapm mixer set callback
1835 * @kcontrol: mixer control
1836 * @ucontrol: control element information
1838 * Callback to set the value of a dapm mixer control.
1840 * Returns 0 for success.
1842 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1843 struct snd_ctl_elem_value *ucontrol)
1845 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1846 struct soc_mixer_control *mc =
1847 (struct soc_mixer_control *)kcontrol->private_value;
1848 unsigned int reg = mc->reg;
1849 unsigned int shift = mc->shift;
1850 int max = mc->max;
1851 unsigned int mask = (1 << fls(max)) - 1;
1852 unsigned int invert = mc->invert;
1853 unsigned int val;
1854 int connect, change;
1855 struct snd_soc_dapm_update update;
1857 val = (ucontrol->value.integer.value[0] & mask);
1859 if (invert)
1860 val = max - val;
1861 mask = mask << shift;
1862 val = val << shift;
1864 mutex_lock(&widget->codec->mutex);
1865 widget->value = val;
1867 change = snd_soc_test_bits(widget->codec, reg, mask, val);
1868 if (change) {
1869 if (val)
1870 /* new connection */
1871 connect = invert ? 0:1;
1872 else
1873 /* old connection must be powered down */
1874 connect = invert ? 1:0;
1876 update.kcontrol = kcontrol;
1877 update.widget = widget;
1878 update.reg = reg;
1879 update.mask = mask;
1880 update.val = val;
1881 widget->dapm->update = &update;
1883 dapm_mixer_update_power(widget, kcontrol, connect);
1885 widget->dapm->update = NULL;
1888 mutex_unlock(&widget->codec->mutex);
1889 return 0;
1891 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1894 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1895 * @kcontrol: mixer control
1896 * @ucontrol: control element information
1898 * Callback to get the value of a dapm enumerated double mixer control.
1900 * Returns 0 for success.
1902 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1903 struct snd_ctl_elem_value *ucontrol)
1905 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1906 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1907 unsigned int val, bitmask;
1909 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1911 val = snd_soc_read(widget->codec, e->reg);
1912 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1913 if (e->shift_l != e->shift_r)
1914 ucontrol->value.enumerated.item[1] =
1915 (val >> e->shift_r) & (bitmask - 1);
1917 return 0;
1919 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1922 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1923 * @kcontrol: mixer control
1924 * @ucontrol: control element information
1926 * Callback to set the value of a dapm enumerated double mixer control.
1928 * Returns 0 for success.
1930 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1931 struct snd_ctl_elem_value *ucontrol)
1933 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1934 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1935 unsigned int val, mux, change;
1936 unsigned int mask, bitmask;
1937 struct snd_soc_dapm_update update;
1939 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1941 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1942 return -EINVAL;
1943 mux = ucontrol->value.enumerated.item[0];
1944 val = mux << e->shift_l;
1945 mask = (bitmask - 1) << e->shift_l;
1946 if (e->shift_l != e->shift_r) {
1947 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1948 return -EINVAL;
1949 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1950 mask |= (bitmask - 1) << e->shift_r;
1953 mutex_lock(&widget->codec->mutex);
1954 widget->value = val;
1955 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1957 update.kcontrol = kcontrol;
1958 update.widget = widget;
1959 update.reg = e->reg;
1960 update.mask = mask;
1961 update.val = val;
1962 widget->dapm->update = &update;
1964 dapm_mux_update_power(widget, kcontrol, change, mux, e);
1966 widget->dapm->update = NULL;
1968 mutex_unlock(&widget->codec->mutex);
1969 return change;
1971 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1974 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1975 * @kcontrol: mixer control
1976 * @ucontrol: control element information
1978 * Returns 0 for success.
1980 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1981 struct snd_ctl_elem_value *ucontrol)
1983 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1985 ucontrol->value.enumerated.item[0] = widget->value;
1987 return 0;
1989 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1992 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1993 * @kcontrol: mixer control
1994 * @ucontrol: control element information
1996 * Returns 0 for success.
1998 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1999 struct snd_ctl_elem_value *ucontrol)
2001 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
2002 struct soc_enum *e =
2003 (struct soc_enum *)kcontrol->private_value;
2004 int change;
2005 int ret = 0;
2007 if (ucontrol->value.enumerated.item[0] >= e->max)
2008 return -EINVAL;
2010 mutex_lock(&widget->codec->mutex);
2012 change = widget->value != ucontrol->value.enumerated.item[0];
2013 widget->value = ucontrol->value.enumerated.item[0];
2014 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
2016 mutex_unlock(&widget->codec->mutex);
2017 return ret;
2019 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2022 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2023 * callback
2024 * @kcontrol: mixer control
2025 * @ucontrol: control element information
2027 * Callback to get the value of a dapm semi enumerated double mixer control.
2029 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2030 * used for handling bitfield coded enumeration for example.
2032 * Returns 0 for success.
2034 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2035 struct snd_ctl_elem_value *ucontrol)
2037 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
2038 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2039 unsigned int reg_val, val, mux;
2041 reg_val = snd_soc_read(widget->codec, e->reg);
2042 val = (reg_val >> e->shift_l) & e->mask;
2043 for (mux = 0; mux < e->max; mux++) {
2044 if (val == e->values[mux])
2045 break;
2047 ucontrol->value.enumerated.item[0] = mux;
2048 if (e->shift_l != e->shift_r) {
2049 val = (reg_val >> e->shift_r) & e->mask;
2050 for (mux = 0; mux < e->max; mux++) {
2051 if (val == e->values[mux])
2052 break;
2054 ucontrol->value.enumerated.item[1] = mux;
2057 return 0;
2059 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2062 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2063 * callback
2064 * @kcontrol: mixer control
2065 * @ucontrol: control element information
2067 * Callback to set the value of a dapm semi enumerated double mixer control.
2069 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2070 * used for handling bitfield coded enumeration for example.
2072 * Returns 0 for success.
2074 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2075 struct snd_ctl_elem_value *ucontrol)
2077 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
2078 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2079 unsigned int val, mux, change;
2080 unsigned int mask;
2081 struct snd_soc_dapm_update update;
2083 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2084 return -EINVAL;
2085 mux = ucontrol->value.enumerated.item[0];
2086 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2087 mask = e->mask << e->shift_l;
2088 if (e->shift_l != e->shift_r) {
2089 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2090 return -EINVAL;
2091 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2092 mask |= e->mask << e->shift_r;
2095 mutex_lock(&widget->codec->mutex);
2096 widget->value = val;
2097 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2099 update.kcontrol = kcontrol;
2100 update.widget = widget;
2101 update.reg = e->reg;
2102 update.mask = mask;
2103 update.val = val;
2104 widget->dapm->update = &update;
2106 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2108 widget->dapm->update = NULL;
2110 mutex_unlock(&widget->codec->mutex);
2111 return change;
2113 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2116 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2118 * @kcontrol: mixer control
2119 * @uinfo: control element information
2121 * Callback to provide information about a pin switch control.
2123 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2124 struct snd_ctl_elem_info *uinfo)
2126 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2127 uinfo->count = 1;
2128 uinfo->value.integer.min = 0;
2129 uinfo->value.integer.max = 1;
2131 return 0;
2133 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2136 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2138 * @kcontrol: mixer control
2139 * @ucontrol: Value
2141 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2142 struct snd_ctl_elem_value *ucontrol)
2144 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2145 const char *pin = (const char *)kcontrol->private_value;
2147 mutex_lock(&codec->mutex);
2149 ucontrol->value.integer.value[0] =
2150 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2152 mutex_unlock(&codec->mutex);
2154 return 0;
2156 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2159 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2161 * @kcontrol: mixer control
2162 * @ucontrol: Value
2164 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2165 struct snd_ctl_elem_value *ucontrol)
2167 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2168 const char *pin = (const char *)kcontrol->private_value;
2170 mutex_lock(&codec->mutex);
2172 if (ucontrol->value.integer.value[0])
2173 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2174 else
2175 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2177 snd_soc_dapm_sync(&codec->dapm);
2179 mutex_unlock(&codec->mutex);
2181 return 0;
2183 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2186 * snd_soc_dapm_new_control - create new dapm control
2187 * @dapm: DAPM context
2188 * @widget: widget template
2190 * Creates a new dapm control based upon the template.
2192 * Returns 0 for success else error.
2194 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2195 const struct snd_soc_dapm_widget *widget)
2197 struct snd_soc_dapm_widget *w;
2198 size_t name_len;
2200 if ((w = dapm_cnew_widget(widget)) == NULL)
2201 return -ENOMEM;
2203 name_len = strlen(widget->name) + 1;
2204 if (dapm->codec && dapm->codec->name_prefix)
2205 name_len += 1 + strlen(dapm->codec->name_prefix);
2206 w->name = kmalloc(name_len, GFP_KERNEL);
2207 if (w->name == NULL) {
2208 kfree(w);
2209 return -ENOMEM;
2211 if (dapm->codec && dapm->codec->name_prefix)
2212 snprintf(w->name, name_len, "%s %s",
2213 dapm->codec->name_prefix, widget->name);
2214 else
2215 snprintf(w->name, name_len, "%s", widget->name);
2217 dapm->n_widgets++;
2218 w->dapm = dapm;
2219 w->codec = dapm->codec;
2220 INIT_LIST_HEAD(&w->sources);
2221 INIT_LIST_HEAD(&w->sinks);
2222 INIT_LIST_HEAD(&w->list);
2223 list_add(&w->list, &dapm->card->widgets);
2225 /* machine layer set ups unconnected pins and insertions */
2226 w->connected = 1;
2227 return 0;
2229 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2232 * snd_soc_dapm_new_controls - create new dapm controls
2233 * @dapm: DAPM context
2234 * @widget: widget array
2235 * @num: number of widgets
2237 * Creates new DAPM controls based upon the templates.
2239 * Returns 0 for success else error.
2241 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2242 const struct snd_soc_dapm_widget *widget,
2243 int num)
2245 int i, ret;
2247 for (i = 0; i < num; i++) {
2248 ret = snd_soc_dapm_new_control(dapm, widget);
2249 if (ret < 0) {
2250 dev_err(dapm->dev,
2251 "ASoC: Failed to create DAPM control %s: %d\n",
2252 widget->name, ret);
2253 return ret;
2255 widget++;
2257 return 0;
2259 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2261 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2262 const char *stream, int event)
2264 struct snd_soc_dapm_widget *w;
2266 list_for_each_entry(w, &dapm->card->widgets, list)
2268 if (!w->sname || w->dapm != dapm)
2269 continue;
2270 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2271 w->name, w->sname, stream, event);
2272 if (strstr(w->sname, stream)) {
2273 switch(event) {
2274 case SND_SOC_DAPM_STREAM_START:
2275 w->active = 1;
2276 break;
2277 case SND_SOC_DAPM_STREAM_STOP:
2278 w->active = 0;
2279 break;
2280 case SND_SOC_DAPM_STREAM_SUSPEND:
2281 case SND_SOC_DAPM_STREAM_RESUME:
2282 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2283 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2284 break;
2289 dapm_power_widgets(dapm, event);
2293 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2294 * @rtd: PCM runtime data
2295 * @stream: stream name
2296 * @event: stream event
2298 * Sends a stream event to the dapm core. The core then makes any
2299 * necessary widget power changes.
2301 * Returns 0 for success else error.
2303 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2304 const char *stream, int event)
2306 struct snd_soc_codec *codec = rtd->codec;
2308 if (stream == NULL)
2309 return 0;
2311 mutex_lock(&codec->mutex);
2312 soc_dapm_stream_event(&codec->dapm, stream, event);
2313 mutex_unlock(&codec->mutex);
2314 return 0;
2318 * snd_soc_dapm_enable_pin - enable pin.
2319 * @dapm: DAPM context
2320 * @pin: pin name
2322 * Enables input/output pin and its parents or children widgets iff there is
2323 * a valid audio route and active audio stream.
2324 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2325 * do any widget power switching.
2327 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2329 return snd_soc_dapm_set_pin(dapm, pin, 1);
2331 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2334 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2335 * @dapm: DAPM context
2336 * @pin: pin name
2338 * Enables input/output pin regardless of any other state. This is
2339 * intended for use with microphone bias supplies used in microphone
2340 * jack detection.
2342 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2343 * do any widget power switching.
2345 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2346 const char *pin)
2348 struct snd_soc_dapm_widget *w;
2350 list_for_each_entry(w, &dapm->card->widgets, list) {
2351 if (w->dapm != dapm)
2352 continue;
2353 if (!strcmp(w->name, pin)) {
2354 dev_dbg(w->dapm->dev,
2355 "dapm: force enable pin %s\n", pin);
2356 w->connected = 1;
2357 w->force = 1;
2358 return 0;
2362 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2363 return -EINVAL;
2365 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2368 * snd_soc_dapm_disable_pin - disable pin.
2369 * @dapm: DAPM context
2370 * @pin: pin name
2372 * Disables input/output pin and its parents or children widgets.
2373 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2374 * do any widget power switching.
2376 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2377 const char *pin)
2379 return snd_soc_dapm_set_pin(dapm, pin, 0);
2381 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2384 * snd_soc_dapm_nc_pin - permanently disable pin.
2385 * @dapm: DAPM context
2386 * @pin: pin name
2388 * Marks the specified pin as being not connected, disabling it along
2389 * any parent or child widgets. At present this is identical to
2390 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2391 * additional things such as disabling controls which only affect
2392 * paths through the pin.
2394 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2395 * do any widget power switching.
2397 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2399 return snd_soc_dapm_set_pin(dapm, pin, 0);
2401 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2404 * snd_soc_dapm_get_pin_status - get audio pin status
2405 * @dapm: DAPM context
2406 * @pin: audio signal pin endpoint (or start point)
2408 * Get audio pin status - connected or disconnected.
2410 * Returns 1 for connected otherwise 0.
2412 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2413 const char *pin)
2415 struct snd_soc_dapm_widget *w;
2417 list_for_each_entry(w, &dapm->card->widgets, list) {
2418 if (w->dapm != dapm)
2419 continue;
2420 if (!strcmp(w->name, pin))
2421 return w->connected;
2424 return 0;
2426 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2429 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2430 * @dapm: DAPM context
2431 * @pin: audio signal pin endpoint (or start point)
2433 * Mark the given endpoint or pin as ignoring suspend. When the
2434 * system is disabled a path between two endpoints flagged as ignoring
2435 * suspend will not be disabled. The path must already be enabled via
2436 * normal means at suspend time, it will not be turned on if it was not
2437 * already enabled.
2439 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2440 const char *pin)
2442 struct snd_soc_dapm_widget *w;
2444 list_for_each_entry(w, &dapm->card->widgets, list) {
2445 if (w->dapm != dapm)
2446 continue;
2447 if (!strcmp(w->name, pin)) {
2448 w->ignore_suspend = 1;
2449 return 0;
2453 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2454 return -EINVAL;
2456 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2459 * snd_soc_dapm_free - free dapm resources
2460 * @card: SoC device
2462 * Free all dapm widgets and resources.
2464 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2466 snd_soc_dapm_sys_remove(dapm->dev);
2467 dapm_free_widgets(dapm);
2468 list_del(&dapm->list);
2470 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2472 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
2474 struct snd_soc_dapm_widget *w;
2475 LIST_HEAD(down_list);
2476 int powerdown = 0;
2478 list_for_each_entry(w, &dapm->card->widgets, list) {
2479 if (w->dapm != dapm)
2480 continue;
2481 if (w->power) {
2482 dapm_seq_insert(w, &down_list, false);
2483 w->power = 0;
2484 powerdown = 1;
2488 /* If there were no widgets to power down we're already in
2489 * standby.
2491 if (powerdown) {
2492 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
2493 dapm_seq_run(dapm, &down_list, 0, false);
2494 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
2499 * snd_soc_dapm_shutdown - callback for system shutdown
2501 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2503 struct snd_soc_codec *codec;
2505 list_for_each_entry(codec, &card->codec_dev_list, list) {
2506 soc_dapm_shutdown_codec(&codec->dapm);
2507 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
2511 /* Module information */
2512 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2513 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2514 MODULE_LICENSE("GPL");