ASoC: s/w->kcontrols/w->kcontrol_news/g
[linux-2.6.git] / sound / soc / soc-dapm.c
blob35cc1ed00a446e8292e136d199a08918a26ef0d2
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->kcontrol_news[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 *)
208 w->kcontrol_news[i].private_value;
209 int val, item, bitmask;
211 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
213 val = snd_soc_read(w->codec, e->reg);
214 item = (val >> e->shift_l) & (bitmask - 1);
216 p->connect = 0;
217 for (i = 0; i < e->max; i++) {
218 if (!(strcmp(p->name, e->texts[i])) && item == i)
219 p->connect = 1;
222 break;
223 case snd_soc_dapm_virt_mux: {
224 struct soc_enum *e = (struct soc_enum *)
225 w->kcontrol_news[i].private_value;
227 p->connect = 0;
228 /* since a virtual mux has no backing registers to
229 * decide which path to connect, it will try to match
230 * with the first enumeration. This is to ensure
231 * that the default mux choice (the first) will be
232 * correctly powered up during initialization.
234 if (!strcmp(p->name, e->texts[0]))
235 p->connect = 1;
237 break;
238 case snd_soc_dapm_value_mux: {
239 struct soc_enum *e = (struct soc_enum *)
240 w->kcontrol_news[i].private_value;
241 int val, item;
243 val = snd_soc_read(w->codec, e->reg);
244 val = (val >> e->shift_l) & e->mask;
245 for (item = 0; item < e->max; item++) {
246 if (val == e->values[item])
247 break;
250 p->connect = 0;
251 for (i = 0; i < e->max; i++) {
252 if (!(strcmp(p->name, e->texts[i])) && item == i)
253 p->connect = 1;
256 break;
257 /* does not effect routing - always connected */
258 case snd_soc_dapm_pga:
259 case snd_soc_dapm_out_drv:
260 case snd_soc_dapm_output:
261 case snd_soc_dapm_adc:
262 case snd_soc_dapm_input:
263 case snd_soc_dapm_dac:
264 case snd_soc_dapm_micbias:
265 case snd_soc_dapm_vmid:
266 case snd_soc_dapm_supply:
267 case snd_soc_dapm_aif_in:
268 case snd_soc_dapm_aif_out:
269 p->connect = 1;
270 break;
271 /* does effect routing - dynamically connected */
272 case snd_soc_dapm_hp:
273 case snd_soc_dapm_mic:
274 case snd_soc_dapm_spk:
275 case snd_soc_dapm_line:
276 case snd_soc_dapm_pre:
277 case snd_soc_dapm_post:
278 p->connect = 0;
279 break;
283 /* connect mux widget to its interconnecting audio paths */
284 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
285 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
286 struct snd_soc_dapm_path *path, const char *control_name,
287 const struct snd_kcontrol_new *kcontrol)
289 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
290 int i;
292 for (i = 0; i < e->max; i++) {
293 if (!(strcmp(control_name, e->texts[i]))) {
294 list_add(&path->list, &dapm->card->paths);
295 list_add(&path->list_sink, &dest->sources);
296 list_add(&path->list_source, &src->sinks);
297 path->name = (char*)e->texts[i];
298 dapm_set_path_status(dest, path, 0);
299 return 0;
303 return -ENODEV;
306 /* connect mixer widget to its interconnecting audio paths */
307 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
308 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
309 struct snd_soc_dapm_path *path, const char *control_name)
311 int i;
313 /* search for mixer kcontrol */
314 for (i = 0; i < dest->num_kcontrols; i++) {
315 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
316 list_add(&path->list, &dapm->card->paths);
317 list_add(&path->list_sink, &dest->sources);
318 list_add(&path->list_source, &src->sinks);
319 path->name = dest->kcontrol_news[i].name;
320 dapm_set_path_status(dest, path, i);
321 return 0;
324 return -ENODEV;
327 /* create new dapm mixer control */
328 static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
329 struct snd_soc_dapm_widget *w)
331 int i, ret = 0;
332 size_t name_len, prefix_len;
333 struct snd_soc_dapm_path *path;
334 struct snd_card *card = dapm->card->snd_card;
335 const char *prefix;
337 if (dapm->codec)
338 prefix = dapm->codec->name_prefix;
339 else
340 prefix = NULL;
342 if (prefix)
343 prefix_len = strlen(prefix) + 1;
344 else
345 prefix_len = 0;
347 /* add kcontrol */
348 for (i = 0; i < w->num_kcontrols; i++) {
350 /* match name */
351 list_for_each_entry(path, &w->sources, list_sink) {
353 /* mixer/mux paths name must match control name */
354 if (path->name != (char *)w->kcontrol_news[i].name)
355 continue;
357 /* add dapm control with long name.
358 * for dapm_mixer this is the concatenation of the
359 * mixer and kcontrol name.
360 * for dapm_mixer_named_ctl this is simply the
361 * kcontrol name.
363 name_len = strlen(w->kcontrol_news[i].name) + 1;
364 if (w->id != snd_soc_dapm_mixer_named_ctl)
365 name_len += 1 + strlen(w->name);
367 path->long_name = kmalloc(name_len, GFP_KERNEL);
369 if (path->long_name == NULL)
370 return -ENOMEM;
372 switch (w->id) {
373 default:
374 /* The control will get a prefix from
375 * the control creation process but
376 * we're also using the same prefix
377 * for widgets so cut the prefix off
378 * the front of the widget name.
380 snprintf(path->long_name, name_len, "%s %s",
381 w->name + prefix_len,
382 w->kcontrol_news[i].name);
383 break;
384 case snd_soc_dapm_mixer_named_ctl:
385 snprintf(path->long_name, name_len, "%s",
386 w->kcontrol_news[i].name);
387 break;
390 path->long_name[name_len - 1] = '\0';
392 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
393 path->long_name, prefix);
394 ret = snd_ctl_add(card, path->kcontrol);
395 if (ret < 0) {
396 dev_err(dapm->dev,
397 "asoc: failed to add dapm kcontrol %s: %d\n",
398 path->long_name, ret);
399 kfree(path->long_name);
400 path->long_name = NULL;
401 return ret;
405 return ret;
408 /* create new dapm mux control */
409 static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
410 struct snd_soc_dapm_widget *w)
412 struct snd_soc_dapm_path *path = NULL;
413 struct snd_kcontrol *kcontrol;
414 struct snd_card *card = dapm->card->snd_card;
415 const char *prefix;
416 size_t prefix_len;
417 int ret = 0;
419 if (!w->num_kcontrols) {
420 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
421 return -EINVAL;
424 if (dapm->codec)
425 prefix = dapm->codec->name_prefix;
426 else
427 prefix = NULL;
429 if (prefix)
430 prefix_len = strlen(prefix) + 1;
431 else
432 prefix_len = 0;
434 /* The control will get a prefix from the control creation
435 * process but we're also using the same prefix for widgets so
436 * cut the prefix off the front of the widget name.
438 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], w, w->name + prefix_len,
439 prefix);
440 ret = snd_ctl_add(card, kcontrol);
442 if (ret < 0)
443 goto err;
445 list_for_each_entry(path, &w->sources, list_sink)
446 path->kcontrol = kcontrol;
448 return ret;
450 err:
451 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
452 return ret;
455 /* create new dapm volume control */
456 static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
457 struct snd_soc_dapm_widget *w)
459 if (w->num_kcontrols)
460 dev_err(w->dapm->dev,
461 "asoc: PGA controls not supported: '%s'\n", w->name);
463 return 0;
466 /* reset 'walked' bit for each dapm path */
467 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
469 struct snd_soc_dapm_path *p;
471 list_for_each_entry(p, &dapm->card->paths, list)
472 p->walked = 0;
475 /* We implement power down on suspend by checking the power state of
476 * the ALSA card - when we are suspending the ALSA state for the card
477 * is set to D3.
479 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
481 int level = snd_power_get_state(widget->dapm->card->snd_card);
483 switch (level) {
484 case SNDRV_CTL_POWER_D3hot:
485 case SNDRV_CTL_POWER_D3cold:
486 if (widget->ignore_suspend)
487 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
488 widget->name);
489 return widget->ignore_suspend;
490 default:
491 return 1;
496 * Recursively check for a completed path to an active or physically connected
497 * output widget. Returns number of complete paths.
499 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
501 struct snd_soc_dapm_path *path;
502 int con = 0;
504 if (widget->id == snd_soc_dapm_supply)
505 return 0;
507 switch (widget->id) {
508 case snd_soc_dapm_adc:
509 case snd_soc_dapm_aif_out:
510 if (widget->active)
511 return snd_soc_dapm_suspend_check(widget);
512 default:
513 break;
516 if (widget->connected) {
517 /* connected pin ? */
518 if (widget->id == snd_soc_dapm_output && !widget->ext)
519 return snd_soc_dapm_suspend_check(widget);
521 /* connected jack or spk ? */
522 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
523 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
524 return snd_soc_dapm_suspend_check(widget);
527 list_for_each_entry(path, &widget->sinks, list_source) {
528 if (path->walked)
529 continue;
531 if (path->sink && path->connect) {
532 path->walked = 1;
533 con += is_connected_output_ep(path->sink);
537 return con;
541 * Recursively check for a completed path to an active or physically connected
542 * input widget. Returns number of complete paths.
544 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
546 struct snd_soc_dapm_path *path;
547 int con = 0;
549 if (widget->id == snd_soc_dapm_supply)
550 return 0;
552 /* active stream ? */
553 switch (widget->id) {
554 case snd_soc_dapm_dac:
555 case snd_soc_dapm_aif_in:
556 if (widget->active)
557 return snd_soc_dapm_suspend_check(widget);
558 default:
559 break;
562 if (widget->connected) {
563 /* connected pin ? */
564 if (widget->id == snd_soc_dapm_input && !widget->ext)
565 return snd_soc_dapm_suspend_check(widget);
567 /* connected VMID/Bias for lower pops */
568 if (widget->id == snd_soc_dapm_vmid)
569 return snd_soc_dapm_suspend_check(widget);
571 /* connected jack ? */
572 if (widget->id == snd_soc_dapm_mic ||
573 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
574 return snd_soc_dapm_suspend_check(widget);
577 list_for_each_entry(path, &widget->sources, list_sink) {
578 if (path->walked)
579 continue;
581 if (path->source && path->connect) {
582 path->walked = 1;
583 con += is_connected_input_ep(path->source);
587 return con;
591 * Handler for generic register modifier widget.
593 int dapm_reg_event(struct snd_soc_dapm_widget *w,
594 struct snd_kcontrol *kcontrol, int event)
596 unsigned int val;
598 if (SND_SOC_DAPM_EVENT_ON(event))
599 val = w->on_val;
600 else
601 val = w->off_val;
603 snd_soc_update_bits(w->codec, -(w->reg + 1),
604 w->mask << w->shift, val << w->shift);
606 return 0;
608 EXPORT_SYMBOL_GPL(dapm_reg_event);
610 /* Generic check to see if a widget should be powered.
612 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
614 int in, out;
616 in = is_connected_input_ep(w);
617 dapm_clear_walk(w->dapm);
618 out = is_connected_output_ep(w);
619 dapm_clear_walk(w->dapm);
620 return out != 0 && in != 0;
623 /* Check to see if an ADC has power */
624 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
626 int in;
628 if (w->active) {
629 in = is_connected_input_ep(w);
630 dapm_clear_walk(w->dapm);
631 return in != 0;
632 } else {
633 return dapm_generic_check_power(w);
637 /* Check to see if a DAC has power */
638 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
640 int out;
642 if (w->active) {
643 out = is_connected_output_ep(w);
644 dapm_clear_walk(w->dapm);
645 return out != 0;
646 } else {
647 return dapm_generic_check_power(w);
651 /* Check to see if a power supply is needed */
652 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
654 struct snd_soc_dapm_path *path;
655 int power = 0;
657 /* Check if one of our outputs is connected */
658 list_for_each_entry(path, &w->sinks, list_source) {
659 if (path->connected &&
660 !path->connected(path->source, path->sink))
661 continue;
663 if (!path->sink)
664 continue;
666 if (path->sink->force) {
667 power = 1;
668 break;
671 if (path->sink->power_check &&
672 path->sink->power_check(path->sink)) {
673 power = 1;
674 break;
678 dapm_clear_walk(w->dapm);
680 return power;
683 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
684 struct snd_soc_dapm_widget *b,
685 bool power_up)
687 int *sort;
689 if (power_up)
690 sort = dapm_up_seq;
691 else
692 sort = dapm_down_seq;
694 if (sort[a->id] != sort[b->id])
695 return sort[a->id] - sort[b->id];
696 if (a->subseq != b->subseq) {
697 if (power_up)
698 return a->subseq - b->subseq;
699 else
700 return b->subseq - a->subseq;
702 if (a->reg != b->reg)
703 return a->reg - b->reg;
704 if (a->dapm != b->dapm)
705 return (unsigned long)a->dapm - (unsigned long)b->dapm;
707 return 0;
710 /* Insert a widget in order into a DAPM power sequence. */
711 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
712 struct list_head *list,
713 bool power_up)
715 struct snd_soc_dapm_widget *w;
717 list_for_each_entry(w, list, power_list)
718 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
719 list_add_tail(&new_widget->power_list, &w->power_list);
720 return;
723 list_add_tail(&new_widget->power_list, list);
726 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
727 struct snd_soc_dapm_widget *w, int event)
729 struct snd_soc_card *card = dapm->card;
730 const char *ev_name;
731 int power, ret;
733 switch (event) {
734 case SND_SOC_DAPM_PRE_PMU:
735 ev_name = "PRE_PMU";
736 power = 1;
737 break;
738 case SND_SOC_DAPM_POST_PMU:
739 ev_name = "POST_PMU";
740 power = 1;
741 break;
742 case SND_SOC_DAPM_PRE_PMD:
743 ev_name = "PRE_PMD";
744 power = 0;
745 break;
746 case SND_SOC_DAPM_POST_PMD:
747 ev_name = "POST_PMD";
748 power = 0;
749 break;
750 default:
751 BUG();
752 return;
755 if (w->power != power)
756 return;
758 if (w->event && (w->event_flags & event)) {
759 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
760 w->name, ev_name);
761 trace_snd_soc_dapm_widget_event_start(w, event);
762 ret = w->event(w, NULL, event);
763 trace_snd_soc_dapm_widget_event_done(w, event);
764 if (ret < 0)
765 pr_err("%s: %s event failed: %d\n",
766 ev_name, w->name, ret);
770 /* Apply the coalesced changes from a DAPM sequence */
771 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
772 struct list_head *pending)
774 struct snd_soc_card *card = dapm->card;
775 struct snd_soc_dapm_widget *w;
776 int reg, power;
777 unsigned int value = 0;
778 unsigned int mask = 0;
779 unsigned int cur_mask;
781 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
782 power_list)->reg;
784 list_for_each_entry(w, pending, power_list) {
785 cur_mask = 1 << w->shift;
786 BUG_ON(reg != w->reg);
788 if (w->invert)
789 power = !w->power;
790 else
791 power = w->power;
793 mask |= cur_mask;
794 if (power)
795 value |= cur_mask;
797 pop_dbg(dapm->dev, card->pop_time,
798 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
799 w->name, reg, value, mask);
801 /* Check for events */
802 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
803 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
806 if (reg >= 0) {
807 pop_dbg(dapm->dev, card->pop_time,
808 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
809 value, mask, reg, card->pop_time);
810 pop_wait(card->pop_time);
811 snd_soc_update_bits(dapm->codec, reg, mask, value);
814 list_for_each_entry(w, pending, power_list) {
815 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
816 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
820 /* Apply a DAPM power sequence.
822 * We walk over a pre-sorted list of widgets to apply power to. In
823 * order to minimise the number of writes to the device required
824 * multiple widgets will be updated in a single write where possible.
825 * Currently anything that requires more than a single write is not
826 * handled.
828 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
829 struct list_head *list, int event, bool power_up)
831 struct snd_soc_dapm_widget *w, *n;
832 LIST_HEAD(pending);
833 int cur_sort = -1;
834 int cur_subseq = -1;
835 int cur_reg = SND_SOC_NOPM;
836 struct snd_soc_dapm_context *cur_dapm = NULL;
837 int ret, i;
838 int *sort;
840 if (power_up)
841 sort = dapm_up_seq;
842 else
843 sort = dapm_down_seq;
845 list_for_each_entry_safe(w, n, list, power_list) {
846 ret = 0;
848 /* Do we need to apply any queued changes? */
849 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
850 w->dapm != cur_dapm || w->subseq != cur_subseq) {
851 if (!list_empty(&pending))
852 dapm_seq_run_coalesced(cur_dapm, &pending);
854 if (cur_dapm && cur_dapm->seq_notifier) {
855 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
856 if (sort[i] == cur_sort)
857 cur_dapm->seq_notifier(cur_dapm,
859 cur_subseq);
862 INIT_LIST_HEAD(&pending);
863 cur_sort = -1;
864 cur_subseq = -1;
865 cur_reg = SND_SOC_NOPM;
866 cur_dapm = NULL;
869 switch (w->id) {
870 case snd_soc_dapm_pre:
871 if (!w->event)
872 list_for_each_entry_safe_continue(w, n, list,
873 power_list);
875 if (event == SND_SOC_DAPM_STREAM_START)
876 ret = w->event(w,
877 NULL, SND_SOC_DAPM_PRE_PMU);
878 else if (event == SND_SOC_DAPM_STREAM_STOP)
879 ret = w->event(w,
880 NULL, SND_SOC_DAPM_PRE_PMD);
881 break;
883 case snd_soc_dapm_post:
884 if (!w->event)
885 list_for_each_entry_safe_continue(w, n, list,
886 power_list);
888 if (event == SND_SOC_DAPM_STREAM_START)
889 ret = w->event(w,
890 NULL, SND_SOC_DAPM_POST_PMU);
891 else if (event == SND_SOC_DAPM_STREAM_STOP)
892 ret = w->event(w,
893 NULL, SND_SOC_DAPM_POST_PMD);
894 break;
896 default:
897 /* Queue it up for application */
898 cur_sort = sort[w->id];
899 cur_subseq = w->subseq;
900 cur_reg = w->reg;
901 cur_dapm = w->dapm;
902 list_move(&w->power_list, &pending);
903 break;
906 if (ret < 0)
907 dev_err(w->dapm->dev,
908 "Failed to apply widget power: %d\n", ret);
911 if (!list_empty(&pending))
912 dapm_seq_run_coalesced(cur_dapm, &pending);
914 if (cur_dapm && cur_dapm->seq_notifier) {
915 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
916 if (sort[i] == cur_sort)
917 cur_dapm->seq_notifier(cur_dapm,
918 i, cur_subseq);
922 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
924 struct snd_soc_dapm_update *update = dapm->update;
925 struct snd_soc_dapm_widget *w;
926 int ret;
928 if (!update)
929 return;
931 w = update->widget;
933 if (w->event &&
934 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
935 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
936 if (ret != 0)
937 pr_err("%s DAPM pre-event failed: %d\n",
938 w->name, ret);
941 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
942 update->val);
943 if (ret < 0)
944 pr_err("%s DAPM update failed: %d\n", w->name, ret);
946 if (w->event &&
947 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
948 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
949 if (ret != 0)
950 pr_err("%s DAPM post-event failed: %d\n",
951 w->name, ret);
955 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
956 * they're changing state.
958 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
960 struct snd_soc_dapm_context *d = data;
961 int ret;
963 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
964 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
965 if (ret != 0)
966 dev_err(d->dev,
967 "Failed to turn on bias: %d\n", ret);
970 /* If we're changing to all on or all off then prepare */
971 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
972 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
973 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
974 if (ret != 0)
975 dev_err(d->dev,
976 "Failed to prepare bias: %d\n", ret);
980 /* Async callback run prior to DAPM sequences - brings to their final
981 * state.
983 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
985 struct snd_soc_dapm_context *d = data;
986 int ret;
988 /* If we just powered the last thing off drop to standby bias */
989 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
990 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
991 if (ret != 0)
992 dev_err(d->dev, "Failed to apply standby bias: %d\n",
993 ret);
996 /* If we're in standby and can support bias off then do that */
997 if (d->bias_level == SND_SOC_BIAS_STANDBY && d->idle_bias_off) {
998 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
999 if (ret != 0)
1000 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1003 /* If we just powered up then move to active bias */
1004 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1005 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1006 if (ret != 0)
1007 dev_err(d->dev, "Failed to apply active bias: %d\n",
1008 ret);
1013 * Scan each dapm widget for complete audio path.
1014 * A complete path is a route that has valid endpoints i.e.:-
1016 * o DAC to output pin.
1017 * o Input Pin to ADC.
1018 * o Input pin to Output pin (bypass, sidetone)
1019 * o DAC to ADC (loopback).
1021 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1023 struct snd_soc_card *card = dapm->card;
1024 struct snd_soc_dapm_widget *w;
1025 struct snd_soc_dapm_context *d;
1026 LIST_HEAD(up_list);
1027 LIST_HEAD(down_list);
1028 LIST_HEAD(async_domain);
1029 int power;
1031 trace_snd_soc_dapm_start(card);
1033 list_for_each_entry(d, &card->dapm_list, list)
1034 if (d->n_widgets)
1035 d->dev_power = 0;
1037 /* Check which widgets we need to power and store them in
1038 * lists indicating if they should be powered up or down.
1040 list_for_each_entry(w, &card->widgets, list) {
1041 switch (w->id) {
1042 case snd_soc_dapm_pre:
1043 dapm_seq_insert(w, &down_list, false);
1044 break;
1045 case snd_soc_dapm_post:
1046 dapm_seq_insert(w, &up_list, true);
1047 break;
1049 default:
1050 if (!w->power_check)
1051 continue;
1053 if (!w->force)
1054 power = w->power_check(w);
1055 else
1056 power = 1;
1057 if (power)
1058 w->dapm->dev_power = 1;
1060 if (w->power == power)
1061 continue;
1063 trace_snd_soc_dapm_widget_power(w, power);
1065 if (power)
1066 dapm_seq_insert(w, &up_list, true);
1067 else
1068 dapm_seq_insert(w, &down_list, false);
1070 w->power = power;
1071 break;
1075 /* If there are no DAPM widgets then try to figure out power from the
1076 * event type.
1078 if (!dapm->n_widgets) {
1079 switch (event) {
1080 case SND_SOC_DAPM_STREAM_START:
1081 case SND_SOC_DAPM_STREAM_RESUME:
1082 dapm->dev_power = 1;
1083 break;
1084 case SND_SOC_DAPM_STREAM_STOP:
1085 dapm->dev_power = !!dapm->codec->active;
1086 break;
1087 case SND_SOC_DAPM_STREAM_SUSPEND:
1088 dapm->dev_power = 0;
1089 break;
1090 case SND_SOC_DAPM_STREAM_NOP:
1091 switch (dapm->bias_level) {
1092 case SND_SOC_BIAS_STANDBY:
1093 case SND_SOC_BIAS_OFF:
1094 dapm->dev_power = 0;
1095 break;
1096 default:
1097 dapm->dev_power = 1;
1098 break;
1100 break;
1101 default:
1102 break;
1106 /* Force all contexts in the card to the same bias state */
1107 power = 0;
1108 list_for_each_entry(d, &card->dapm_list, list)
1109 if (d->dev_power)
1110 power = 1;
1111 list_for_each_entry(d, &card->dapm_list, list)
1112 d->dev_power = power;
1115 /* Run all the bias changes in parallel */
1116 list_for_each_entry(d, &dapm->card->dapm_list, list)
1117 async_schedule_domain(dapm_pre_sequence_async, d,
1118 &async_domain);
1119 async_synchronize_full_domain(&async_domain);
1121 /* Power down widgets first; try to avoid amplifying pops. */
1122 dapm_seq_run(dapm, &down_list, event, false);
1124 dapm_widget_update(dapm);
1126 /* Now power up. */
1127 dapm_seq_run(dapm, &up_list, event, true);
1129 /* Run all the bias changes in parallel */
1130 list_for_each_entry(d, &dapm->card->dapm_list, list)
1131 async_schedule_domain(dapm_post_sequence_async, d,
1132 &async_domain);
1133 async_synchronize_full_domain(&async_domain);
1135 pop_dbg(dapm->dev, card->pop_time,
1136 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1137 pop_wait(card->pop_time);
1139 trace_snd_soc_dapm_done(card);
1141 return 0;
1144 #ifdef CONFIG_DEBUG_FS
1145 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1147 file->private_data = inode->i_private;
1148 return 0;
1151 static ssize_t dapm_widget_power_read_file(struct file *file,
1152 char __user *user_buf,
1153 size_t count, loff_t *ppos)
1155 struct snd_soc_dapm_widget *w = file->private_data;
1156 char *buf;
1157 int in, out;
1158 ssize_t ret;
1159 struct snd_soc_dapm_path *p = NULL;
1161 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1162 if (!buf)
1163 return -ENOMEM;
1165 in = is_connected_input_ep(w);
1166 dapm_clear_walk(w->dapm);
1167 out = is_connected_output_ep(w);
1168 dapm_clear_walk(w->dapm);
1170 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1171 w->name, w->power ? "On" : "Off", in, out);
1173 if (w->reg >= 0)
1174 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1175 " - R%d(0x%x) bit %d",
1176 w->reg, w->reg, w->shift);
1178 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1180 if (w->sname)
1181 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1182 w->sname,
1183 w->active ? "active" : "inactive");
1185 list_for_each_entry(p, &w->sources, list_sink) {
1186 if (p->connected && !p->connected(w, p->sink))
1187 continue;
1189 if (p->connect)
1190 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1191 " in \"%s\" \"%s\"\n",
1192 p->name ? p->name : "static",
1193 p->source->name);
1195 list_for_each_entry(p, &w->sinks, list_source) {
1196 if (p->connected && !p->connected(w, p->sink))
1197 continue;
1199 if (p->connect)
1200 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1201 " out \"%s\" \"%s\"\n",
1202 p->name ? p->name : "static",
1203 p->sink->name);
1206 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1208 kfree(buf);
1209 return ret;
1212 static const struct file_operations dapm_widget_power_fops = {
1213 .open = dapm_widget_power_open_file,
1214 .read = dapm_widget_power_read_file,
1215 .llseek = default_llseek,
1218 static int dapm_bias_open_file(struct inode *inode, struct file *file)
1220 file->private_data = inode->i_private;
1221 return 0;
1224 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1225 size_t count, loff_t *ppos)
1227 struct snd_soc_dapm_context *dapm = file->private_data;
1228 char *level;
1230 switch (dapm->bias_level) {
1231 case SND_SOC_BIAS_ON:
1232 level = "On\n";
1233 break;
1234 case SND_SOC_BIAS_PREPARE:
1235 level = "Prepare\n";
1236 break;
1237 case SND_SOC_BIAS_STANDBY:
1238 level = "Standby\n";
1239 break;
1240 case SND_SOC_BIAS_OFF:
1241 level = "Off\n";
1242 break;
1243 default:
1244 BUG();
1245 level = "Unknown\n";
1246 break;
1249 return simple_read_from_buffer(user_buf, count, ppos, level,
1250 strlen(level));
1253 static const struct file_operations dapm_bias_fops = {
1254 .open = dapm_bias_open_file,
1255 .read = dapm_bias_read_file,
1256 .llseek = default_llseek,
1259 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1260 struct dentry *parent)
1262 struct dentry *d;
1264 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1266 if (!dapm->debugfs_dapm) {
1267 printk(KERN_WARNING
1268 "Failed to create DAPM debugfs directory\n");
1269 return;
1272 d = debugfs_create_file("bias_level", 0444,
1273 dapm->debugfs_dapm, dapm,
1274 &dapm_bias_fops);
1275 if (!d)
1276 dev_warn(dapm->dev,
1277 "ASoC: Failed to create bias level debugfs file\n");
1280 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1282 struct snd_soc_dapm_context *dapm = w->dapm;
1283 struct dentry *d;
1285 if (!dapm->debugfs_dapm || !w->name)
1286 return;
1288 d = debugfs_create_file(w->name, 0444,
1289 dapm->debugfs_dapm, w,
1290 &dapm_widget_power_fops);
1291 if (!d)
1292 dev_warn(w->dapm->dev,
1293 "ASoC: Failed to create %s debugfs file\n",
1294 w->name);
1297 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1299 debugfs_remove_recursive(dapm->debugfs_dapm);
1302 #else
1303 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1304 struct dentry *parent)
1308 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1312 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1316 #endif
1318 /* test and update the power status of a mux widget */
1319 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1320 struct snd_kcontrol *kcontrol, int change,
1321 int mux, struct soc_enum *e)
1323 struct snd_soc_dapm_path *path;
1324 int found = 0;
1326 if (widget->id != snd_soc_dapm_mux &&
1327 widget->id != snd_soc_dapm_virt_mux &&
1328 widget->id != snd_soc_dapm_value_mux)
1329 return -ENODEV;
1331 if (!change)
1332 return 0;
1334 /* find dapm widget path assoc with kcontrol */
1335 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1336 if (path->kcontrol != kcontrol)
1337 continue;
1339 if (!path->name || !e->texts[mux])
1340 continue;
1342 found = 1;
1343 /* we now need to match the string in the enum to the path */
1344 if (!(strcmp(path->name, e->texts[mux])))
1345 path->connect = 1; /* new connection */
1346 else
1347 path->connect = 0; /* old connection must be powered down */
1350 if (found)
1351 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1353 return 0;
1356 /* test and update the power status of a mixer or switch widget */
1357 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1358 struct snd_kcontrol *kcontrol, int connect)
1360 struct snd_soc_dapm_path *path;
1361 int found = 0;
1363 if (widget->id != snd_soc_dapm_mixer &&
1364 widget->id != snd_soc_dapm_mixer_named_ctl &&
1365 widget->id != snd_soc_dapm_switch)
1366 return -ENODEV;
1368 /* find dapm widget path assoc with kcontrol */
1369 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1370 if (path->kcontrol != kcontrol)
1371 continue;
1373 /* found, now check type */
1374 found = 1;
1375 path->connect = connect;
1376 break;
1379 if (found)
1380 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1382 return 0;
1385 /* show dapm widget status in sys fs */
1386 static ssize_t dapm_widget_show(struct device *dev,
1387 struct device_attribute *attr, char *buf)
1389 struct snd_soc_pcm_runtime *rtd =
1390 container_of(dev, struct snd_soc_pcm_runtime, dev);
1391 struct snd_soc_codec *codec =rtd->codec;
1392 struct snd_soc_dapm_widget *w;
1393 int count = 0;
1394 char *state = "not set";
1396 list_for_each_entry(w, &codec->card->widgets, list) {
1397 if (w->dapm != &codec->dapm)
1398 continue;
1400 /* only display widgets that burnm power */
1401 switch (w->id) {
1402 case snd_soc_dapm_hp:
1403 case snd_soc_dapm_mic:
1404 case snd_soc_dapm_spk:
1405 case snd_soc_dapm_line:
1406 case snd_soc_dapm_micbias:
1407 case snd_soc_dapm_dac:
1408 case snd_soc_dapm_adc:
1409 case snd_soc_dapm_pga:
1410 case snd_soc_dapm_out_drv:
1411 case snd_soc_dapm_mixer:
1412 case snd_soc_dapm_mixer_named_ctl:
1413 case snd_soc_dapm_supply:
1414 if (w->name)
1415 count += sprintf(buf + count, "%s: %s\n",
1416 w->name, w->power ? "On":"Off");
1417 break;
1418 default:
1419 break;
1423 switch (codec->dapm.bias_level) {
1424 case SND_SOC_BIAS_ON:
1425 state = "On";
1426 break;
1427 case SND_SOC_BIAS_PREPARE:
1428 state = "Prepare";
1429 break;
1430 case SND_SOC_BIAS_STANDBY:
1431 state = "Standby";
1432 break;
1433 case SND_SOC_BIAS_OFF:
1434 state = "Off";
1435 break;
1437 count += sprintf(buf + count, "PM State: %s\n", state);
1439 return count;
1442 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1444 int snd_soc_dapm_sys_add(struct device *dev)
1446 return device_create_file(dev, &dev_attr_dapm_widget);
1449 static void snd_soc_dapm_sys_remove(struct device *dev)
1451 device_remove_file(dev, &dev_attr_dapm_widget);
1454 /* free all dapm widgets and resources */
1455 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1457 struct snd_soc_dapm_widget *w, *next_w;
1458 struct snd_soc_dapm_path *p, *next_p;
1460 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1461 if (w->dapm != dapm)
1462 continue;
1463 list_del(&w->list);
1465 * remove source and sink paths associated to this widget.
1466 * While removing the path, remove reference to it from both
1467 * source and sink widgets so that path is removed only once.
1469 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1470 list_del(&p->list_sink);
1471 list_del(&p->list_source);
1472 list_del(&p->list);
1473 kfree(p->long_name);
1474 kfree(p);
1476 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1477 list_del(&p->list_sink);
1478 list_del(&p->list_source);
1479 list_del(&p->list);
1480 kfree(p->long_name);
1481 kfree(p);
1483 kfree(w->name);
1484 kfree(w);
1488 static struct snd_soc_dapm_widget *dapm_find_widget(
1489 struct snd_soc_dapm_context *dapm, const char *pin,
1490 bool search_other_contexts)
1492 struct snd_soc_dapm_widget *w;
1493 struct snd_soc_dapm_widget *fallback = NULL;
1495 list_for_each_entry(w, &dapm->card->widgets, list) {
1496 if (!strcmp(w->name, pin)) {
1497 if (w->dapm == dapm)
1498 return w;
1499 else
1500 fallback = w;
1504 if (search_other_contexts)
1505 return fallback;
1507 return NULL;
1510 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1511 const char *pin, int status)
1513 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
1515 if (!w) {
1516 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1517 return -EINVAL;
1520 w->connected = status;
1521 if (status == 0)
1522 w->force = 0;
1524 return 0;
1528 * snd_soc_dapm_sync - scan and power dapm paths
1529 * @dapm: DAPM context
1531 * Walks all dapm audio paths and powers widgets according to their
1532 * stream or path usage.
1534 * Returns 0 for success.
1536 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1538 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1540 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1542 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1543 const struct snd_soc_dapm_route *route)
1545 struct snd_soc_dapm_path *path;
1546 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1547 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1548 const char *sink;
1549 const char *control = route->control;
1550 const char *source;
1551 char prefixed_sink[80];
1552 char prefixed_source[80];
1553 int ret = 0;
1555 if (dapm->codec && dapm->codec->name_prefix) {
1556 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1557 dapm->codec->name_prefix, route->sink);
1558 sink = prefixed_sink;
1559 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1560 dapm->codec->name_prefix, route->source);
1561 source = prefixed_source;
1562 } else {
1563 sink = route->sink;
1564 source = route->source;
1568 * find src and dest widgets over all widgets but favor a widget from
1569 * current DAPM context
1571 list_for_each_entry(w, &dapm->card->widgets, list) {
1572 if (!wsink && !(strcmp(w->name, sink))) {
1573 wtsink = w;
1574 if (w->dapm == dapm)
1575 wsink = w;
1576 continue;
1578 if (!wsource && !(strcmp(w->name, source))) {
1579 wtsource = w;
1580 if (w->dapm == dapm)
1581 wsource = w;
1584 /* use widget from another DAPM context if not found from this */
1585 if (!wsink)
1586 wsink = wtsink;
1587 if (!wsource)
1588 wsource = wtsource;
1590 if (wsource == NULL || wsink == NULL)
1591 return -ENODEV;
1593 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1594 if (!path)
1595 return -ENOMEM;
1597 path->source = wsource;
1598 path->sink = wsink;
1599 path->connected = route->connected;
1600 INIT_LIST_HEAD(&path->list);
1601 INIT_LIST_HEAD(&path->list_source);
1602 INIT_LIST_HEAD(&path->list_sink);
1604 /* check for external widgets */
1605 if (wsink->id == snd_soc_dapm_input) {
1606 if (wsource->id == snd_soc_dapm_micbias ||
1607 wsource->id == snd_soc_dapm_mic ||
1608 wsource->id == snd_soc_dapm_line ||
1609 wsource->id == snd_soc_dapm_output)
1610 wsink->ext = 1;
1612 if (wsource->id == snd_soc_dapm_output) {
1613 if (wsink->id == snd_soc_dapm_spk ||
1614 wsink->id == snd_soc_dapm_hp ||
1615 wsink->id == snd_soc_dapm_line ||
1616 wsink->id == snd_soc_dapm_input)
1617 wsource->ext = 1;
1620 /* connect static paths */
1621 if (control == NULL) {
1622 list_add(&path->list, &dapm->card->paths);
1623 list_add(&path->list_sink, &wsink->sources);
1624 list_add(&path->list_source, &wsource->sinks);
1625 path->connect = 1;
1626 return 0;
1629 /* connect dynamic paths */
1630 switch (wsink->id) {
1631 case snd_soc_dapm_adc:
1632 case snd_soc_dapm_dac:
1633 case snd_soc_dapm_pga:
1634 case snd_soc_dapm_out_drv:
1635 case snd_soc_dapm_input:
1636 case snd_soc_dapm_output:
1637 case snd_soc_dapm_micbias:
1638 case snd_soc_dapm_vmid:
1639 case snd_soc_dapm_pre:
1640 case snd_soc_dapm_post:
1641 case snd_soc_dapm_supply:
1642 case snd_soc_dapm_aif_in:
1643 case snd_soc_dapm_aif_out:
1644 list_add(&path->list, &dapm->card->paths);
1645 list_add(&path->list_sink, &wsink->sources);
1646 list_add(&path->list_source, &wsource->sinks);
1647 path->connect = 1;
1648 return 0;
1649 case snd_soc_dapm_mux:
1650 case snd_soc_dapm_virt_mux:
1651 case snd_soc_dapm_value_mux:
1652 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
1653 &wsink->kcontrol_news[0]);
1654 if (ret != 0)
1655 goto err;
1656 break;
1657 case snd_soc_dapm_switch:
1658 case snd_soc_dapm_mixer:
1659 case snd_soc_dapm_mixer_named_ctl:
1660 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
1661 if (ret != 0)
1662 goto err;
1663 break;
1664 case snd_soc_dapm_hp:
1665 case snd_soc_dapm_mic:
1666 case snd_soc_dapm_line:
1667 case snd_soc_dapm_spk:
1668 list_add(&path->list, &dapm->card->paths);
1669 list_add(&path->list_sink, &wsink->sources);
1670 list_add(&path->list_source, &wsource->sinks);
1671 path->connect = 0;
1672 return 0;
1674 return 0;
1676 err:
1677 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1678 source, control, sink);
1679 kfree(path);
1680 return ret;
1684 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1685 * @dapm: DAPM context
1686 * @route: audio routes
1687 * @num: number of routes
1689 * Connects 2 dapm widgets together via a named audio path. The sink is
1690 * the widget receiving the audio signal, whilst the source is the sender
1691 * of the audio signal.
1693 * Returns 0 for success else error. On error all resources can be freed
1694 * with a call to snd_soc_card_free().
1696 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
1697 const struct snd_soc_dapm_route *route, int num)
1699 int i, ret;
1701 for (i = 0; i < num; i++) {
1702 ret = snd_soc_dapm_add_route(dapm, route);
1703 if (ret < 0) {
1704 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1705 route->source, route->sink);
1706 return ret;
1708 route++;
1711 return 0;
1713 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1716 * snd_soc_dapm_new_widgets - add new dapm widgets
1717 * @dapm: DAPM context
1719 * Checks the codec for any new dapm widgets and creates them if found.
1721 * Returns 0 for success.
1723 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
1725 struct snd_soc_dapm_widget *w;
1726 unsigned int val;
1728 list_for_each_entry(w, &dapm->card->widgets, list)
1730 if (w->new)
1731 continue;
1733 switch(w->id) {
1734 case snd_soc_dapm_switch:
1735 case snd_soc_dapm_mixer:
1736 case snd_soc_dapm_mixer_named_ctl:
1737 w->power_check = dapm_generic_check_power;
1738 dapm_new_mixer(dapm, w);
1739 break;
1740 case snd_soc_dapm_mux:
1741 case snd_soc_dapm_virt_mux:
1742 case snd_soc_dapm_value_mux:
1743 w->power_check = dapm_generic_check_power;
1744 dapm_new_mux(dapm, w);
1745 break;
1746 case snd_soc_dapm_adc:
1747 case snd_soc_dapm_aif_out:
1748 w->power_check = dapm_adc_check_power;
1749 break;
1750 case snd_soc_dapm_dac:
1751 case snd_soc_dapm_aif_in:
1752 w->power_check = dapm_dac_check_power;
1753 break;
1754 case snd_soc_dapm_pga:
1755 case snd_soc_dapm_out_drv:
1756 w->power_check = dapm_generic_check_power;
1757 dapm_new_pga(dapm, w);
1758 break;
1759 case snd_soc_dapm_input:
1760 case snd_soc_dapm_output:
1761 case snd_soc_dapm_micbias:
1762 case snd_soc_dapm_spk:
1763 case snd_soc_dapm_hp:
1764 case snd_soc_dapm_mic:
1765 case snd_soc_dapm_line:
1766 w->power_check = dapm_generic_check_power;
1767 break;
1768 case snd_soc_dapm_supply:
1769 w->power_check = dapm_supply_check_power;
1770 case snd_soc_dapm_vmid:
1771 case snd_soc_dapm_pre:
1772 case snd_soc_dapm_post:
1773 break;
1776 /* Read the initial power state from the device */
1777 if (w->reg >= 0) {
1778 val = snd_soc_read(w->codec, w->reg);
1779 val &= 1 << w->shift;
1780 if (w->invert)
1781 val = !val;
1783 if (val)
1784 w->power = 1;
1787 w->new = 1;
1789 dapm_debugfs_add_widget(w);
1792 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1793 return 0;
1795 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1798 * snd_soc_dapm_get_volsw - dapm mixer get callback
1799 * @kcontrol: mixer control
1800 * @ucontrol: control element information
1802 * Callback to get the value of a dapm mixer control.
1804 * Returns 0 for success.
1806 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1807 struct snd_ctl_elem_value *ucontrol)
1809 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1810 struct soc_mixer_control *mc =
1811 (struct soc_mixer_control *)kcontrol->private_value;
1812 unsigned int reg = mc->reg;
1813 unsigned int shift = mc->shift;
1814 unsigned int rshift = mc->rshift;
1815 int max = mc->max;
1816 unsigned int invert = mc->invert;
1817 unsigned int mask = (1 << fls(max)) - 1;
1819 ucontrol->value.integer.value[0] =
1820 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1821 if (shift != rshift)
1822 ucontrol->value.integer.value[1] =
1823 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1824 if (invert) {
1825 ucontrol->value.integer.value[0] =
1826 max - ucontrol->value.integer.value[0];
1827 if (shift != rshift)
1828 ucontrol->value.integer.value[1] =
1829 max - ucontrol->value.integer.value[1];
1832 return 0;
1834 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1837 * snd_soc_dapm_put_volsw - dapm mixer set callback
1838 * @kcontrol: mixer control
1839 * @ucontrol: control element information
1841 * Callback to set the value of a dapm mixer control.
1843 * Returns 0 for success.
1845 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1846 struct snd_ctl_elem_value *ucontrol)
1848 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1849 struct soc_mixer_control *mc =
1850 (struct soc_mixer_control *)kcontrol->private_value;
1851 unsigned int reg = mc->reg;
1852 unsigned int shift = mc->shift;
1853 int max = mc->max;
1854 unsigned int mask = (1 << fls(max)) - 1;
1855 unsigned int invert = mc->invert;
1856 unsigned int val;
1857 int connect, change;
1858 struct snd_soc_dapm_update update;
1860 val = (ucontrol->value.integer.value[0] & mask);
1862 if (invert)
1863 val = max - val;
1864 mask = mask << shift;
1865 val = val << shift;
1867 mutex_lock(&widget->codec->mutex);
1868 widget->value = val;
1870 change = snd_soc_test_bits(widget->codec, reg, mask, val);
1871 if (change) {
1872 if (val)
1873 /* new connection */
1874 connect = invert ? 0:1;
1875 else
1876 /* old connection must be powered down */
1877 connect = invert ? 1:0;
1879 update.kcontrol = kcontrol;
1880 update.widget = widget;
1881 update.reg = reg;
1882 update.mask = mask;
1883 update.val = val;
1884 widget->dapm->update = &update;
1886 dapm_mixer_update_power(widget, kcontrol, connect);
1888 widget->dapm->update = NULL;
1891 mutex_unlock(&widget->codec->mutex);
1892 return 0;
1894 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1897 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1898 * @kcontrol: mixer control
1899 * @ucontrol: control element information
1901 * Callback to get the value of a dapm enumerated double mixer control.
1903 * Returns 0 for success.
1905 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1906 struct snd_ctl_elem_value *ucontrol)
1908 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1909 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1910 unsigned int val, bitmask;
1912 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1914 val = snd_soc_read(widget->codec, e->reg);
1915 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1916 if (e->shift_l != e->shift_r)
1917 ucontrol->value.enumerated.item[1] =
1918 (val >> e->shift_r) & (bitmask - 1);
1920 return 0;
1922 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1925 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1926 * @kcontrol: mixer control
1927 * @ucontrol: control element information
1929 * Callback to set the value of a dapm enumerated double mixer control.
1931 * Returns 0 for success.
1933 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1934 struct snd_ctl_elem_value *ucontrol)
1936 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1937 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1938 unsigned int val, mux, change;
1939 unsigned int mask, bitmask;
1940 struct snd_soc_dapm_update update;
1942 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1944 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1945 return -EINVAL;
1946 mux = ucontrol->value.enumerated.item[0];
1947 val = mux << e->shift_l;
1948 mask = (bitmask - 1) << e->shift_l;
1949 if (e->shift_l != e->shift_r) {
1950 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1951 return -EINVAL;
1952 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1953 mask |= (bitmask - 1) << e->shift_r;
1956 mutex_lock(&widget->codec->mutex);
1957 widget->value = val;
1958 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1960 update.kcontrol = kcontrol;
1961 update.widget = widget;
1962 update.reg = e->reg;
1963 update.mask = mask;
1964 update.val = val;
1965 widget->dapm->update = &update;
1967 dapm_mux_update_power(widget, kcontrol, change, mux, e);
1969 widget->dapm->update = NULL;
1971 mutex_unlock(&widget->codec->mutex);
1972 return change;
1974 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1977 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1978 * @kcontrol: mixer control
1979 * @ucontrol: control element information
1981 * Returns 0 for success.
1983 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1984 struct snd_ctl_elem_value *ucontrol)
1986 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1988 ucontrol->value.enumerated.item[0] = widget->value;
1990 return 0;
1992 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1995 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1996 * @kcontrol: mixer control
1997 * @ucontrol: control element information
1999 * Returns 0 for success.
2001 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2002 struct snd_ctl_elem_value *ucontrol)
2004 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
2005 struct soc_enum *e =
2006 (struct soc_enum *)kcontrol->private_value;
2007 int change;
2008 int ret = 0;
2010 if (ucontrol->value.enumerated.item[0] >= e->max)
2011 return -EINVAL;
2013 mutex_lock(&widget->codec->mutex);
2015 change = widget->value != ucontrol->value.enumerated.item[0];
2016 widget->value = ucontrol->value.enumerated.item[0];
2017 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
2019 mutex_unlock(&widget->codec->mutex);
2020 return ret;
2022 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2025 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2026 * callback
2027 * @kcontrol: mixer control
2028 * @ucontrol: control element information
2030 * Callback to get the value of a dapm semi enumerated double mixer control.
2032 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2033 * used for handling bitfield coded enumeration for example.
2035 * Returns 0 for success.
2037 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2038 struct snd_ctl_elem_value *ucontrol)
2040 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
2041 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2042 unsigned int reg_val, val, mux;
2044 reg_val = snd_soc_read(widget->codec, e->reg);
2045 val = (reg_val >> e->shift_l) & e->mask;
2046 for (mux = 0; mux < e->max; mux++) {
2047 if (val == e->values[mux])
2048 break;
2050 ucontrol->value.enumerated.item[0] = mux;
2051 if (e->shift_l != e->shift_r) {
2052 val = (reg_val >> e->shift_r) & e->mask;
2053 for (mux = 0; mux < e->max; mux++) {
2054 if (val == e->values[mux])
2055 break;
2057 ucontrol->value.enumerated.item[1] = mux;
2060 return 0;
2062 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2065 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2066 * callback
2067 * @kcontrol: mixer control
2068 * @ucontrol: control element information
2070 * Callback to set the value of a dapm semi enumerated double mixer control.
2072 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2073 * used for handling bitfield coded enumeration for example.
2075 * Returns 0 for success.
2077 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2078 struct snd_ctl_elem_value *ucontrol)
2080 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
2081 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2082 unsigned int val, mux, change;
2083 unsigned int mask;
2084 struct snd_soc_dapm_update update;
2086 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2087 return -EINVAL;
2088 mux = ucontrol->value.enumerated.item[0];
2089 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2090 mask = e->mask << e->shift_l;
2091 if (e->shift_l != e->shift_r) {
2092 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2093 return -EINVAL;
2094 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2095 mask |= e->mask << e->shift_r;
2098 mutex_lock(&widget->codec->mutex);
2099 widget->value = val;
2100 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2102 update.kcontrol = kcontrol;
2103 update.widget = widget;
2104 update.reg = e->reg;
2105 update.mask = mask;
2106 update.val = val;
2107 widget->dapm->update = &update;
2109 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2111 widget->dapm->update = NULL;
2113 mutex_unlock(&widget->codec->mutex);
2114 return change;
2116 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2119 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2121 * @kcontrol: mixer control
2122 * @uinfo: control element information
2124 * Callback to provide information about a pin switch control.
2126 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2127 struct snd_ctl_elem_info *uinfo)
2129 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2130 uinfo->count = 1;
2131 uinfo->value.integer.min = 0;
2132 uinfo->value.integer.max = 1;
2134 return 0;
2136 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2139 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2141 * @kcontrol: mixer control
2142 * @ucontrol: Value
2144 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2145 struct snd_ctl_elem_value *ucontrol)
2147 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2148 const char *pin = (const char *)kcontrol->private_value;
2150 mutex_lock(&codec->mutex);
2152 ucontrol->value.integer.value[0] =
2153 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2155 mutex_unlock(&codec->mutex);
2157 return 0;
2159 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2162 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2164 * @kcontrol: mixer control
2165 * @ucontrol: Value
2167 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2168 struct snd_ctl_elem_value *ucontrol)
2170 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2171 const char *pin = (const char *)kcontrol->private_value;
2173 mutex_lock(&codec->mutex);
2175 if (ucontrol->value.integer.value[0])
2176 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2177 else
2178 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2180 snd_soc_dapm_sync(&codec->dapm);
2182 mutex_unlock(&codec->mutex);
2184 return 0;
2186 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2189 * snd_soc_dapm_new_control - create new dapm control
2190 * @dapm: DAPM context
2191 * @widget: widget template
2193 * Creates a new dapm control based upon the template.
2195 * Returns 0 for success else error.
2197 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2198 const struct snd_soc_dapm_widget *widget)
2200 struct snd_soc_dapm_widget *w;
2201 size_t name_len;
2203 if ((w = dapm_cnew_widget(widget)) == NULL)
2204 return -ENOMEM;
2206 name_len = strlen(widget->name) + 1;
2207 if (dapm->codec && dapm->codec->name_prefix)
2208 name_len += 1 + strlen(dapm->codec->name_prefix);
2209 w->name = kmalloc(name_len, GFP_KERNEL);
2210 if (w->name == NULL) {
2211 kfree(w);
2212 return -ENOMEM;
2214 if (dapm->codec && dapm->codec->name_prefix)
2215 snprintf(w->name, name_len, "%s %s",
2216 dapm->codec->name_prefix, widget->name);
2217 else
2218 snprintf(w->name, name_len, "%s", widget->name);
2220 dapm->n_widgets++;
2221 w->dapm = dapm;
2222 w->codec = dapm->codec;
2223 INIT_LIST_HEAD(&w->sources);
2224 INIT_LIST_HEAD(&w->sinks);
2225 INIT_LIST_HEAD(&w->list);
2226 list_add(&w->list, &dapm->card->widgets);
2228 /* machine layer set ups unconnected pins and insertions */
2229 w->connected = 1;
2230 return 0;
2232 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2235 * snd_soc_dapm_new_controls - create new dapm controls
2236 * @dapm: DAPM context
2237 * @widget: widget array
2238 * @num: number of widgets
2240 * Creates new DAPM controls based upon the templates.
2242 * Returns 0 for success else error.
2244 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2245 const struct snd_soc_dapm_widget *widget,
2246 int num)
2248 int i, ret;
2250 for (i = 0; i < num; i++) {
2251 ret = snd_soc_dapm_new_control(dapm, widget);
2252 if (ret < 0) {
2253 dev_err(dapm->dev,
2254 "ASoC: Failed to create DAPM control %s: %d\n",
2255 widget->name, ret);
2256 return ret;
2258 widget++;
2260 return 0;
2262 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2264 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2265 const char *stream, int event)
2267 struct snd_soc_dapm_widget *w;
2269 list_for_each_entry(w, &dapm->card->widgets, list)
2271 if (!w->sname || w->dapm != dapm)
2272 continue;
2273 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2274 w->name, w->sname, stream, event);
2275 if (strstr(w->sname, stream)) {
2276 switch(event) {
2277 case SND_SOC_DAPM_STREAM_START:
2278 w->active = 1;
2279 break;
2280 case SND_SOC_DAPM_STREAM_STOP:
2281 w->active = 0;
2282 break;
2283 case SND_SOC_DAPM_STREAM_SUSPEND:
2284 case SND_SOC_DAPM_STREAM_RESUME:
2285 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2286 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2287 break;
2292 dapm_power_widgets(dapm, event);
2296 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2297 * @rtd: PCM runtime data
2298 * @stream: stream name
2299 * @event: stream event
2301 * Sends a stream event to the dapm core. The core then makes any
2302 * necessary widget power changes.
2304 * Returns 0 for success else error.
2306 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2307 const char *stream, int event)
2309 struct snd_soc_codec *codec = rtd->codec;
2311 if (stream == NULL)
2312 return 0;
2314 mutex_lock(&codec->mutex);
2315 soc_dapm_stream_event(&codec->dapm, stream, event);
2316 mutex_unlock(&codec->mutex);
2317 return 0;
2321 * snd_soc_dapm_enable_pin - enable pin.
2322 * @dapm: DAPM context
2323 * @pin: pin name
2325 * Enables input/output pin and its parents or children widgets iff there is
2326 * a valid audio route and active audio stream.
2327 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2328 * do any widget power switching.
2330 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2332 return snd_soc_dapm_set_pin(dapm, pin, 1);
2334 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2337 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2338 * @dapm: DAPM context
2339 * @pin: pin name
2341 * Enables input/output pin regardless of any other state. This is
2342 * intended for use with microphone bias supplies used in microphone
2343 * jack detection.
2345 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2346 * do any widget power switching.
2348 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2349 const char *pin)
2351 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2353 if (!w) {
2354 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2355 return -EINVAL;
2358 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2359 w->connected = 1;
2360 w->force = 1;
2362 return 0;
2364 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2367 * snd_soc_dapm_disable_pin - disable pin.
2368 * @dapm: DAPM context
2369 * @pin: pin name
2371 * Disables input/output pin and its parents or children widgets.
2372 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2373 * do any widget power switching.
2375 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2376 const char *pin)
2378 return snd_soc_dapm_set_pin(dapm, pin, 0);
2380 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2383 * snd_soc_dapm_nc_pin - permanently disable pin.
2384 * @dapm: DAPM context
2385 * @pin: pin name
2387 * Marks the specified pin as being not connected, disabling it along
2388 * any parent or child widgets. At present this is identical to
2389 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2390 * additional things such as disabling controls which only affect
2391 * paths through the pin.
2393 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2394 * do any widget power switching.
2396 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2398 return snd_soc_dapm_set_pin(dapm, pin, 0);
2400 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2403 * snd_soc_dapm_get_pin_status - get audio pin status
2404 * @dapm: DAPM context
2405 * @pin: audio signal pin endpoint (or start point)
2407 * Get audio pin status - connected or disconnected.
2409 * Returns 1 for connected otherwise 0.
2411 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2412 const char *pin)
2414 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2416 if (w)
2417 return w->connected;
2419 return 0;
2421 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2424 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2425 * @dapm: DAPM context
2426 * @pin: audio signal pin endpoint (or start point)
2428 * Mark the given endpoint or pin as ignoring suspend. When the
2429 * system is disabled a path between two endpoints flagged as ignoring
2430 * suspend will not be disabled. The path must already be enabled via
2431 * normal means at suspend time, it will not be turned on if it was not
2432 * already enabled.
2434 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2435 const char *pin)
2437 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
2439 if (!w) {
2440 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2441 return -EINVAL;
2444 w->ignore_suspend = 1;
2446 return 0;
2448 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2451 * snd_soc_dapm_free - free dapm resources
2452 * @card: SoC device
2454 * Free all dapm widgets and resources.
2456 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2458 snd_soc_dapm_sys_remove(dapm->dev);
2459 dapm_debugfs_cleanup(dapm);
2460 dapm_free_widgets(dapm);
2461 list_del(&dapm->list);
2463 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2465 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
2467 struct snd_soc_dapm_widget *w;
2468 LIST_HEAD(down_list);
2469 int powerdown = 0;
2471 list_for_each_entry(w, &dapm->card->widgets, list) {
2472 if (w->dapm != dapm)
2473 continue;
2474 if (w->power) {
2475 dapm_seq_insert(w, &down_list, false);
2476 w->power = 0;
2477 powerdown = 1;
2481 /* If there were no widgets to power down we're already in
2482 * standby.
2484 if (powerdown) {
2485 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
2486 dapm_seq_run(dapm, &down_list, 0, false);
2487 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
2492 * snd_soc_dapm_shutdown - callback for system shutdown
2494 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2496 struct snd_soc_codec *codec;
2498 list_for_each_entry(codec, &card->codec_dev_list, list) {
2499 soc_dapm_shutdown_codec(&codec->dapm);
2500 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
2504 /* Module information */
2505 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2506 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2507 MODULE_LICENSE("GPL");