Blackfin: convert to asm-generic ptrace.h
[linux-2.6/libata-dev.git] / sound / soc / soc-dapm.c
blob456617e63789893c7d90f547cbc00f3c7ba168d5
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 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
328 const struct snd_kcontrol_new *kcontrol_new,
329 struct snd_kcontrol **kcontrol)
331 struct snd_soc_dapm_widget *w;
332 int i;
334 *kcontrol = NULL;
336 list_for_each_entry(w, &dapm->card->widgets, list) {
337 for (i = 0; i < w->num_kcontrols; i++) {
338 if (&w->kcontrol_news[i] == kcontrol_new) {
339 if (w->kcontrols)
340 *kcontrol = w->kcontrols[i];
341 return 1;
346 return 0;
349 /* create new dapm mixer control */
350 static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
351 struct snd_soc_dapm_widget *w)
353 int i, ret = 0;
354 size_t name_len, prefix_len;
355 struct snd_soc_dapm_path *path;
356 struct snd_card *card = dapm->card->snd_card;
357 const char *prefix;
358 struct snd_soc_dapm_widget_list *wlist;
359 size_t wlistsize;
361 if (dapm->codec)
362 prefix = dapm->codec->name_prefix;
363 else
364 prefix = NULL;
366 if (prefix)
367 prefix_len = strlen(prefix) + 1;
368 else
369 prefix_len = 0;
371 /* add kcontrol */
372 for (i = 0; i < w->num_kcontrols; i++) {
374 /* match name */
375 list_for_each_entry(path, &w->sources, list_sink) {
377 /* mixer/mux paths name must match control name */
378 if (path->name != (char *)w->kcontrol_news[i].name)
379 continue;
381 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
382 sizeof(struct snd_soc_dapm_widget *),
383 wlist = kzalloc(wlistsize, GFP_KERNEL);
384 if (wlist == NULL) {
385 dev_err(dapm->dev,
386 "asoc: can't allocate widget list for %s\n",
387 w->name);
388 return -ENOMEM;
390 wlist->num_widgets = 1;
391 wlist->widgets[0] = w;
393 /* add dapm control with long name.
394 * for dapm_mixer this is the concatenation of the
395 * mixer and kcontrol name.
396 * for dapm_mixer_named_ctl this is simply the
397 * kcontrol name.
399 name_len = strlen(w->kcontrol_news[i].name) + 1;
400 if (w->id != snd_soc_dapm_mixer_named_ctl)
401 name_len += 1 + strlen(w->name);
403 path->long_name = kmalloc(name_len, GFP_KERNEL);
405 if (path->long_name == NULL) {
406 kfree(wlist);
407 return -ENOMEM;
410 switch (w->id) {
411 default:
412 /* The control will get a prefix from
413 * the control creation process but
414 * we're also using the same prefix
415 * for widgets so cut the prefix off
416 * the front of the widget name.
418 snprintf(path->long_name, name_len, "%s %s",
419 w->name + prefix_len,
420 w->kcontrol_news[i].name);
421 break;
422 case snd_soc_dapm_mixer_named_ctl:
423 snprintf(path->long_name, name_len, "%s",
424 w->kcontrol_news[i].name);
425 break;
428 path->long_name[name_len - 1] = '\0';
430 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
431 wlist, path->long_name,
432 prefix);
433 ret = snd_ctl_add(card, path->kcontrol);
434 if (ret < 0) {
435 dev_err(dapm->dev,
436 "asoc: failed to add dapm kcontrol %s: %d\n",
437 path->long_name, ret);
438 kfree(wlist);
439 kfree(path->long_name);
440 path->long_name = NULL;
441 return ret;
443 w->kcontrols[i] = path->kcontrol;
446 return ret;
449 /* create new dapm mux control */
450 static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
451 struct snd_soc_dapm_widget *w)
453 struct snd_soc_dapm_path *path = NULL;
454 struct snd_kcontrol *kcontrol;
455 struct snd_card *card = dapm->card->snd_card;
456 const char *prefix;
457 size_t prefix_len;
458 int ret;
459 struct snd_soc_dapm_widget_list *wlist;
460 int shared, wlistentries;
461 size_t wlistsize;
462 char *name;
464 if (w->num_kcontrols != 1) {
465 dev_err(dapm->dev,
466 "asoc: mux %s has incorrect number of controls\n",
467 w->name);
468 return -EINVAL;
471 shared = dapm_is_shared_kcontrol(dapm, &w->kcontrol_news[0],
472 &kcontrol);
473 if (kcontrol) {
474 wlist = kcontrol->private_data;
475 wlistentries = wlist->num_widgets + 1;
476 } else {
477 wlist = NULL;
478 wlistentries = 1;
480 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
481 wlistentries * sizeof(struct snd_soc_dapm_widget *),
482 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
483 if (wlist == NULL) {
484 dev_err(dapm->dev,
485 "asoc: can't allocate widget list for %s\n", w->name);
486 return -ENOMEM;
488 wlist->num_widgets = wlistentries;
489 wlist->widgets[wlistentries - 1] = w;
491 if (!kcontrol) {
492 if (dapm->codec)
493 prefix = dapm->codec->name_prefix;
494 else
495 prefix = NULL;
497 if (shared) {
498 name = w->kcontrol_news[0].name;
499 prefix_len = 0;
500 } else {
501 name = w->name;
502 if (prefix)
503 prefix_len = strlen(prefix) + 1;
504 else
505 prefix_len = 0;
509 * The control will get a prefix from the control creation
510 * process but we're also using the same prefix for widgets so
511 * cut the prefix off the front of the widget name.
513 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
514 name + prefix_len, prefix);
515 ret = snd_ctl_add(card, kcontrol);
516 if (ret < 0) {
517 dev_err(dapm->dev,
518 "asoc: failed to add kcontrol %s\n", w->name);
519 kfree(wlist);
520 return ret;
524 kcontrol->private_data = wlist;
526 w->kcontrols[0] = kcontrol;
528 list_for_each_entry(path, &w->sources, list_sink)
529 path->kcontrol = kcontrol;
531 return 0;
534 /* create new dapm volume control */
535 static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
536 struct snd_soc_dapm_widget *w)
538 if (w->num_kcontrols)
539 dev_err(w->dapm->dev,
540 "asoc: PGA controls not supported: '%s'\n", w->name);
542 return 0;
545 /* reset 'walked' bit for each dapm path */
546 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
548 struct snd_soc_dapm_path *p;
550 list_for_each_entry(p, &dapm->card->paths, list)
551 p->walked = 0;
554 /* We implement power down on suspend by checking the power state of
555 * the ALSA card - when we are suspending the ALSA state for the card
556 * is set to D3.
558 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
560 int level = snd_power_get_state(widget->dapm->card->snd_card);
562 switch (level) {
563 case SNDRV_CTL_POWER_D3hot:
564 case SNDRV_CTL_POWER_D3cold:
565 if (widget->ignore_suspend)
566 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
567 widget->name);
568 return widget->ignore_suspend;
569 default:
570 return 1;
575 * Recursively check for a completed path to an active or physically connected
576 * output widget. Returns number of complete paths.
578 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
580 struct snd_soc_dapm_path *path;
581 int con = 0;
583 if (widget->id == snd_soc_dapm_supply)
584 return 0;
586 switch (widget->id) {
587 case snd_soc_dapm_adc:
588 case snd_soc_dapm_aif_out:
589 if (widget->active)
590 return snd_soc_dapm_suspend_check(widget);
591 default:
592 break;
595 if (widget->connected) {
596 /* connected pin ? */
597 if (widget->id == snd_soc_dapm_output && !widget->ext)
598 return snd_soc_dapm_suspend_check(widget);
600 /* connected jack or spk ? */
601 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
602 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
603 return snd_soc_dapm_suspend_check(widget);
606 list_for_each_entry(path, &widget->sinks, list_source) {
607 if (path->walked)
608 continue;
610 if (path->sink && path->connect) {
611 path->walked = 1;
612 con += is_connected_output_ep(path->sink);
616 return con;
620 * Recursively check for a completed path to an active or physically connected
621 * input widget. Returns number of complete paths.
623 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
625 struct snd_soc_dapm_path *path;
626 int con = 0;
628 if (widget->id == snd_soc_dapm_supply)
629 return 0;
631 /* active stream ? */
632 switch (widget->id) {
633 case snd_soc_dapm_dac:
634 case snd_soc_dapm_aif_in:
635 if (widget->active)
636 return snd_soc_dapm_suspend_check(widget);
637 default:
638 break;
641 if (widget->connected) {
642 /* connected pin ? */
643 if (widget->id == snd_soc_dapm_input && !widget->ext)
644 return snd_soc_dapm_suspend_check(widget);
646 /* connected VMID/Bias for lower pops */
647 if (widget->id == snd_soc_dapm_vmid)
648 return snd_soc_dapm_suspend_check(widget);
650 /* connected jack ? */
651 if (widget->id == snd_soc_dapm_mic ||
652 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
653 return snd_soc_dapm_suspend_check(widget);
656 list_for_each_entry(path, &widget->sources, list_sink) {
657 if (path->walked)
658 continue;
660 if (path->source && path->connect) {
661 path->walked = 1;
662 con += is_connected_input_ep(path->source);
666 return con;
670 * Handler for generic register modifier widget.
672 int dapm_reg_event(struct snd_soc_dapm_widget *w,
673 struct snd_kcontrol *kcontrol, int event)
675 unsigned int val;
677 if (SND_SOC_DAPM_EVENT_ON(event))
678 val = w->on_val;
679 else
680 val = w->off_val;
682 snd_soc_update_bits(w->codec, -(w->reg + 1),
683 w->mask << w->shift, val << w->shift);
685 return 0;
687 EXPORT_SYMBOL_GPL(dapm_reg_event);
689 /* Generic check to see if a widget should be powered.
691 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
693 int in, out;
695 in = is_connected_input_ep(w);
696 dapm_clear_walk(w->dapm);
697 out = is_connected_output_ep(w);
698 dapm_clear_walk(w->dapm);
699 return out != 0 && in != 0;
702 /* Check to see if an ADC has power */
703 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
705 int in;
707 if (w->active) {
708 in = is_connected_input_ep(w);
709 dapm_clear_walk(w->dapm);
710 return in != 0;
711 } else {
712 return dapm_generic_check_power(w);
716 /* Check to see if a DAC has power */
717 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
719 int out;
721 if (w->active) {
722 out = is_connected_output_ep(w);
723 dapm_clear_walk(w->dapm);
724 return out != 0;
725 } else {
726 return dapm_generic_check_power(w);
730 /* Check to see if a power supply is needed */
731 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
733 struct snd_soc_dapm_path *path;
734 int power = 0;
736 /* Check if one of our outputs is connected */
737 list_for_each_entry(path, &w->sinks, list_source) {
738 if (path->connected &&
739 !path->connected(path->source, path->sink))
740 continue;
742 if (!path->sink)
743 continue;
745 if (path->sink->force) {
746 power = 1;
747 break;
750 if (path->sink->power_check &&
751 path->sink->power_check(path->sink)) {
752 power = 1;
753 break;
757 dapm_clear_walk(w->dapm);
759 return power;
762 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
763 struct snd_soc_dapm_widget *b,
764 bool power_up)
766 int *sort;
768 if (power_up)
769 sort = dapm_up_seq;
770 else
771 sort = dapm_down_seq;
773 if (sort[a->id] != sort[b->id])
774 return sort[a->id] - sort[b->id];
775 if (a->subseq != b->subseq) {
776 if (power_up)
777 return a->subseq - b->subseq;
778 else
779 return b->subseq - a->subseq;
781 if (a->reg != b->reg)
782 return a->reg - b->reg;
783 if (a->dapm != b->dapm)
784 return (unsigned long)a->dapm - (unsigned long)b->dapm;
786 return 0;
789 /* Insert a widget in order into a DAPM power sequence. */
790 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
791 struct list_head *list,
792 bool power_up)
794 struct snd_soc_dapm_widget *w;
796 list_for_each_entry(w, list, power_list)
797 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
798 list_add_tail(&new_widget->power_list, &w->power_list);
799 return;
802 list_add_tail(&new_widget->power_list, list);
805 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
806 struct snd_soc_dapm_widget *w, int event)
808 struct snd_soc_card *card = dapm->card;
809 const char *ev_name;
810 int power, ret;
812 switch (event) {
813 case SND_SOC_DAPM_PRE_PMU:
814 ev_name = "PRE_PMU";
815 power = 1;
816 break;
817 case SND_SOC_DAPM_POST_PMU:
818 ev_name = "POST_PMU";
819 power = 1;
820 break;
821 case SND_SOC_DAPM_PRE_PMD:
822 ev_name = "PRE_PMD";
823 power = 0;
824 break;
825 case SND_SOC_DAPM_POST_PMD:
826 ev_name = "POST_PMD";
827 power = 0;
828 break;
829 default:
830 BUG();
831 return;
834 if (w->power != power)
835 return;
837 if (w->event && (w->event_flags & event)) {
838 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
839 w->name, ev_name);
840 trace_snd_soc_dapm_widget_event_start(w, event);
841 ret = w->event(w, NULL, event);
842 trace_snd_soc_dapm_widget_event_done(w, event);
843 if (ret < 0)
844 pr_err("%s: %s event failed: %d\n",
845 ev_name, w->name, ret);
849 /* Apply the coalesced changes from a DAPM sequence */
850 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
851 struct list_head *pending)
853 struct snd_soc_card *card = dapm->card;
854 struct snd_soc_dapm_widget *w;
855 int reg, power;
856 unsigned int value = 0;
857 unsigned int mask = 0;
858 unsigned int cur_mask;
860 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
861 power_list)->reg;
863 list_for_each_entry(w, pending, power_list) {
864 cur_mask = 1 << w->shift;
865 BUG_ON(reg != w->reg);
867 if (w->invert)
868 power = !w->power;
869 else
870 power = w->power;
872 mask |= cur_mask;
873 if (power)
874 value |= cur_mask;
876 pop_dbg(dapm->dev, card->pop_time,
877 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
878 w->name, reg, value, mask);
880 /* Check for events */
881 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
882 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
885 if (reg >= 0) {
886 pop_dbg(dapm->dev, card->pop_time,
887 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
888 value, mask, reg, card->pop_time);
889 pop_wait(card->pop_time);
890 snd_soc_update_bits(dapm->codec, reg, mask, value);
893 list_for_each_entry(w, pending, power_list) {
894 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
895 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
899 /* Apply a DAPM power sequence.
901 * We walk over a pre-sorted list of widgets to apply power to. In
902 * order to minimise the number of writes to the device required
903 * multiple widgets will be updated in a single write where possible.
904 * Currently anything that requires more than a single write is not
905 * handled.
907 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
908 struct list_head *list, int event, bool power_up)
910 struct snd_soc_dapm_widget *w, *n;
911 LIST_HEAD(pending);
912 int cur_sort = -1;
913 int cur_subseq = -1;
914 int cur_reg = SND_SOC_NOPM;
915 struct snd_soc_dapm_context *cur_dapm = NULL;
916 int ret, i;
917 int *sort;
919 if (power_up)
920 sort = dapm_up_seq;
921 else
922 sort = dapm_down_seq;
924 list_for_each_entry_safe(w, n, list, power_list) {
925 ret = 0;
927 /* Do we need to apply any queued changes? */
928 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
929 w->dapm != cur_dapm || w->subseq != cur_subseq) {
930 if (!list_empty(&pending))
931 dapm_seq_run_coalesced(cur_dapm, &pending);
933 if (cur_dapm && cur_dapm->seq_notifier) {
934 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
935 if (sort[i] == cur_sort)
936 cur_dapm->seq_notifier(cur_dapm,
938 cur_subseq);
941 INIT_LIST_HEAD(&pending);
942 cur_sort = -1;
943 cur_subseq = -1;
944 cur_reg = SND_SOC_NOPM;
945 cur_dapm = NULL;
948 switch (w->id) {
949 case snd_soc_dapm_pre:
950 if (!w->event)
951 list_for_each_entry_safe_continue(w, n, list,
952 power_list);
954 if (event == SND_SOC_DAPM_STREAM_START)
955 ret = w->event(w,
956 NULL, SND_SOC_DAPM_PRE_PMU);
957 else if (event == SND_SOC_DAPM_STREAM_STOP)
958 ret = w->event(w,
959 NULL, SND_SOC_DAPM_PRE_PMD);
960 break;
962 case snd_soc_dapm_post:
963 if (!w->event)
964 list_for_each_entry_safe_continue(w, n, list,
965 power_list);
967 if (event == SND_SOC_DAPM_STREAM_START)
968 ret = w->event(w,
969 NULL, SND_SOC_DAPM_POST_PMU);
970 else if (event == SND_SOC_DAPM_STREAM_STOP)
971 ret = w->event(w,
972 NULL, SND_SOC_DAPM_POST_PMD);
973 break;
975 default:
976 /* Queue it up for application */
977 cur_sort = sort[w->id];
978 cur_subseq = w->subseq;
979 cur_reg = w->reg;
980 cur_dapm = w->dapm;
981 list_move(&w->power_list, &pending);
982 break;
985 if (ret < 0)
986 dev_err(w->dapm->dev,
987 "Failed to apply widget power: %d\n", ret);
990 if (!list_empty(&pending))
991 dapm_seq_run_coalesced(cur_dapm, &pending);
993 if (cur_dapm && cur_dapm->seq_notifier) {
994 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
995 if (sort[i] == cur_sort)
996 cur_dapm->seq_notifier(cur_dapm,
997 i, cur_subseq);
1001 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1003 struct snd_soc_dapm_update *update = dapm->update;
1004 struct snd_soc_dapm_widget *w;
1005 int ret;
1007 if (!update)
1008 return;
1010 w = update->widget;
1012 if (w->event &&
1013 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1014 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1015 if (ret != 0)
1016 pr_err("%s DAPM pre-event failed: %d\n",
1017 w->name, ret);
1020 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1021 update->val);
1022 if (ret < 0)
1023 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1025 if (w->event &&
1026 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1027 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1028 if (ret != 0)
1029 pr_err("%s DAPM post-event failed: %d\n",
1030 w->name, ret);
1034 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1035 * they're changing state.
1037 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1039 struct snd_soc_dapm_context *d = data;
1040 int ret;
1042 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
1043 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1044 if (ret != 0)
1045 dev_err(d->dev,
1046 "Failed to turn on bias: %d\n", ret);
1049 /* If we're changing to all on or all off then prepare */
1050 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
1051 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
1052 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1053 if (ret != 0)
1054 dev_err(d->dev,
1055 "Failed to prepare bias: %d\n", ret);
1059 /* Async callback run prior to DAPM sequences - brings to their final
1060 * state.
1062 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1064 struct snd_soc_dapm_context *d = data;
1065 int ret;
1067 /* If we just powered the last thing off drop to standby bias */
1068 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
1069 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1070 if (ret != 0)
1071 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1072 ret);
1075 /* If we're in standby and can support bias off then do that */
1076 if (d->bias_level == SND_SOC_BIAS_STANDBY && d->idle_bias_off) {
1077 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1078 if (ret != 0)
1079 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1082 /* If we just powered up then move to active bias */
1083 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1084 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1085 if (ret != 0)
1086 dev_err(d->dev, "Failed to apply active bias: %d\n",
1087 ret);
1092 * Scan each dapm widget for complete audio path.
1093 * A complete path is a route that has valid endpoints i.e.:-
1095 * o DAC to output pin.
1096 * o Input Pin to ADC.
1097 * o Input pin to Output pin (bypass, sidetone)
1098 * o DAC to ADC (loopback).
1100 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1102 struct snd_soc_card *card = dapm->card;
1103 struct snd_soc_dapm_widget *w;
1104 struct snd_soc_dapm_context *d;
1105 LIST_HEAD(up_list);
1106 LIST_HEAD(down_list);
1107 LIST_HEAD(async_domain);
1108 int power;
1110 trace_snd_soc_dapm_start(card);
1112 list_for_each_entry(d, &card->dapm_list, list)
1113 if (d->n_widgets)
1114 d->dev_power = 0;
1116 /* Check which widgets we need to power and store them in
1117 * lists indicating if they should be powered up or down.
1119 list_for_each_entry(w, &card->widgets, list) {
1120 switch (w->id) {
1121 case snd_soc_dapm_pre:
1122 dapm_seq_insert(w, &down_list, false);
1123 break;
1124 case snd_soc_dapm_post:
1125 dapm_seq_insert(w, &up_list, true);
1126 break;
1128 default:
1129 if (!w->power_check)
1130 continue;
1132 if (!w->force)
1133 power = w->power_check(w);
1134 else
1135 power = 1;
1136 if (power)
1137 w->dapm->dev_power = 1;
1139 if (w->power == power)
1140 continue;
1142 trace_snd_soc_dapm_widget_power(w, power);
1144 if (power)
1145 dapm_seq_insert(w, &up_list, true);
1146 else
1147 dapm_seq_insert(w, &down_list, false);
1149 w->power = power;
1150 break;
1154 /* If there are no DAPM widgets then try to figure out power from the
1155 * event type.
1157 if (!dapm->n_widgets) {
1158 switch (event) {
1159 case SND_SOC_DAPM_STREAM_START:
1160 case SND_SOC_DAPM_STREAM_RESUME:
1161 dapm->dev_power = 1;
1162 break;
1163 case SND_SOC_DAPM_STREAM_STOP:
1164 dapm->dev_power = !!dapm->codec->active;
1165 break;
1166 case SND_SOC_DAPM_STREAM_SUSPEND:
1167 dapm->dev_power = 0;
1168 break;
1169 case SND_SOC_DAPM_STREAM_NOP:
1170 switch (dapm->bias_level) {
1171 case SND_SOC_BIAS_STANDBY:
1172 case SND_SOC_BIAS_OFF:
1173 dapm->dev_power = 0;
1174 break;
1175 default:
1176 dapm->dev_power = 1;
1177 break;
1179 break;
1180 default:
1181 break;
1185 /* Force all contexts in the card to the same bias state */
1186 power = 0;
1187 list_for_each_entry(d, &card->dapm_list, list)
1188 if (d->dev_power)
1189 power = 1;
1190 list_for_each_entry(d, &card->dapm_list, list)
1191 d->dev_power = power;
1194 /* Run all the bias changes in parallel */
1195 list_for_each_entry(d, &dapm->card->dapm_list, list)
1196 async_schedule_domain(dapm_pre_sequence_async, d,
1197 &async_domain);
1198 async_synchronize_full_domain(&async_domain);
1200 /* Power down widgets first; try to avoid amplifying pops. */
1201 dapm_seq_run(dapm, &down_list, event, false);
1203 dapm_widget_update(dapm);
1205 /* Now power up. */
1206 dapm_seq_run(dapm, &up_list, event, true);
1208 /* Run all the bias changes in parallel */
1209 list_for_each_entry(d, &dapm->card->dapm_list, list)
1210 async_schedule_domain(dapm_post_sequence_async, d,
1211 &async_domain);
1212 async_synchronize_full_domain(&async_domain);
1214 pop_dbg(dapm->dev, card->pop_time,
1215 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1216 pop_wait(card->pop_time);
1218 trace_snd_soc_dapm_done(card);
1220 return 0;
1223 #ifdef CONFIG_DEBUG_FS
1224 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1226 file->private_data = inode->i_private;
1227 return 0;
1230 static ssize_t dapm_widget_power_read_file(struct file *file,
1231 char __user *user_buf,
1232 size_t count, loff_t *ppos)
1234 struct snd_soc_dapm_widget *w = file->private_data;
1235 char *buf;
1236 int in, out;
1237 ssize_t ret;
1238 struct snd_soc_dapm_path *p = NULL;
1240 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1241 if (!buf)
1242 return -ENOMEM;
1244 in = is_connected_input_ep(w);
1245 dapm_clear_walk(w->dapm);
1246 out = is_connected_output_ep(w);
1247 dapm_clear_walk(w->dapm);
1249 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1250 w->name, w->power ? "On" : "Off", in, out);
1252 if (w->reg >= 0)
1253 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1254 " - R%d(0x%x) bit %d",
1255 w->reg, w->reg, w->shift);
1257 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1259 if (w->sname)
1260 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1261 w->sname,
1262 w->active ? "active" : "inactive");
1264 list_for_each_entry(p, &w->sources, list_sink) {
1265 if (p->connected && !p->connected(w, p->sink))
1266 continue;
1268 if (p->connect)
1269 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1270 " in \"%s\" \"%s\"\n",
1271 p->name ? p->name : "static",
1272 p->source->name);
1274 list_for_each_entry(p, &w->sinks, list_source) {
1275 if (p->connected && !p->connected(w, p->sink))
1276 continue;
1278 if (p->connect)
1279 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1280 " out \"%s\" \"%s\"\n",
1281 p->name ? p->name : "static",
1282 p->sink->name);
1285 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1287 kfree(buf);
1288 return ret;
1291 static const struct file_operations dapm_widget_power_fops = {
1292 .open = dapm_widget_power_open_file,
1293 .read = dapm_widget_power_read_file,
1294 .llseek = default_llseek,
1297 static int dapm_bias_open_file(struct inode *inode, struct file *file)
1299 file->private_data = inode->i_private;
1300 return 0;
1303 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1304 size_t count, loff_t *ppos)
1306 struct snd_soc_dapm_context *dapm = file->private_data;
1307 char *level;
1309 switch (dapm->bias_level) {
1310 case SND_SOC_BIAS_ON:
1311 level = "On\n";
1312 break;
1313 case SND_SOC_BIAS_PREPARE:
1314 level = "Prepare\n";
1315 break;
1316 case SND_SOC_BIAS_STANDBY:
1317 level = "Standby\n";
1318 break;
1319 case SND_SOC_BIAS_OFF:
1320 level = "Off\n";
1321 break;
1322 default:
1323 BUG();
1324 level = "Unknown\n";
1325 break;
1328 return simple_read_from_buffer(user_buf, count, ppos, level,
1329 strlen(level));
1332 static const struct file_operations dapm_bias_fops = {
1333 .open = dapm_bias_open_file,
1334 .read = dapm_bias_read_file,
1335 .llseek = default_llseek,
1338 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1339 struct dentry *parent)
1341 struct dentry *d;
1343 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1345 if (!dapm->debugfs_dapm) {
1346 printk(KERN_WARNING
1347 "Failed to create DAPM debugfs directory\n");
1348 return;
1351 d = debugfs_create_file("bias_level", 0444,
1352 dapm->debugfs_dapm, dapm,
1353 &dapm_bias_fops);
1354 if (!d)
1355 dev_warn(dapm->dev,
1356 "ASoC: Failed to create bias level debugfs file\n");
1359 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1361 struct snd_soc_dapm_context *dapm = w->dapm;
1362 struct dentry *d;
1364 if (!dapm->debugfs_dapm || !w->name)
1365 return;
1367 d = debugfs_create_file(w->name, 0444,
1368 dapm->debugfs_dapm, w,
1369 &dapm_widget_power_fops);
1370 if (!d)
1371 dev_warn(w->dapm->dev,
1372 "ASoC: Failed to create %s debugfs file\n",
1373 w->name);
1376 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1378 debugfs_remove_recursive(dapm->debugfs_dapm);
1381 #else
1382 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1383 struct dentry *parent)
1387 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1391 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1395 #endif
1397 /* test and update the power status of a mux widget */
1398 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1399 struct snd_kcontrol *kcontrol, int change,
1400 int mux, struct soc_enum *e)
1402 struct snd_soc_dapm_path *path;
1403 int found = 0;
1405 if (widget->id != snd_soc_dapm_mux &&
1406 widget->id != snd_soc_dapm_virt_mux &&
1407 widget->id != snd_soc_dapm_value_mux)
1408 return -ENODEV;
1410 if (!change)
1411 return 0;
1413 /* find dapm widget path assoc with kcontrol */
1414 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1415 if (path->kcontrol != kcontrol)
1416 continue;
1418 if (!path->name || !e->texts[mux])
1419 continue;
1421 found = 1;
1422 /* we now need to match the string in the enum to the path */
1423 if (!(strcmp(path->name, e->texts[mux])))
1424 path->connect = 1; /* new connection */
1425 else
1426 path->connect = 0; /* old connection must be powered down */
1429 if (found)
1430 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1432 return 0;
1435 /* test and update the power status of a mixer or switch widget */
1436 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1437 struct snd_kcontrol *kcontrol, int connect)
1439 struct snd_soc_dapm_path *path;
1440 int found = 0;
1442 if (widget->id != snd_soc_dapm_mixer &&
1443 widget->id != snd_soc_dapm_mixer_named_ctl &&
1444 widget->id != snd_soc_dapm_switch)
1445 return -ENODEV;
1447 /* find dapm widget path assoc with kcontrol */
1448 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1449 if (path->kcontrol != kcontrol)
1450 continue;
1452 /* found, now check type */
1453 found = 1;
1454 path->connect = connect;
1455 break;
1458 if (found)
1459 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1461 return 0;
1464 /* show dapm widget status in sys fs */
1465 static ssize_t dapm_widget_show(struct device *dev,
1466 struct device_attribute *attr, char *buf)
1468 struct snd_soc_pcm_runtime *rtd =
1469 container_of(dev, struct snd_soc_pcm_runtime, dev);
1470 struct snd_soc_codec *codec =rtd->codec;
1471 struct snd_soc_dapm_widget *w;
1472 int count = 0;
1473 char *state = "not set";
1475 list_for_each_entry(w, &codec->card->widgets, list) {
1476 if (w->dapm != &codec->dapm)
1477 continue;
1479 /* only display widgets that burnm power */
1480 switch (w->id) {
1481 case snd_soc_dapm_hp:
1482 case snd_soc_dapm_mic:
1483 case snd_soc_dapm_spk:
1484 case snd_soc_dapm_line:
1485 case snd_soc_dapm_micbias:
1486 case snd_soc_dapm_dac:
1487 case snd_soc_dapm_adc:
1488 case snd_soc_dapm_pga:
1489 case snd_soc_dapm_out_drv:
1490 case snd_soc_dapm_mixer:
1491 case snd_soc_dapm_mixer_named_ctl:
1492 case snd_soc_dapm_supply:
1493 if (w->name)
1494 count += sprintf(buf + count, "%s: %s\n",
1495 w->name, w->power ? "On":"Off");
1496 break;
1497 default:
1498 break;
1502 switch (codec->dapm.bias_level) {
1503 case SND_SOC_BIAS_ON:
1504 state = "On";
1505 break;
1506 case SND_SOC_BIAS_PREPARE:
1507 state = "Prepare";
1508 break;
1509 case SND_SOC_BIAS_STANDBY:
1510 state = "Standby";
1511 break;
1512 case SND_SOC_BIAS_OFF:
1513 state = "Off";
1514 break;
1516 count += sprintf(buf + count, "PM State: %s\n", state);
1518 return count;
1521 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1523 int snd_soc_dapm_sys_add(struct device *dev)
1525 return device_create_file(dev, &dev_attr_dapm_widget);
1528 static void snd_soc_dapm_sys_remove(struct device *dev)
1530 device_remove_file(dev, &dev_attr_dapm_widget);
1533 /* free all dapm widgets and resources */
1534 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1536 struct snd_soc_dapm_widget *w, *next_w;
1537 struct snd_soc_dapm_path *p, *next_p;
1539 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1540 if (w->dapm != dapm)
1541 continue;
1542 list_del(&w->list);
1544 * remove source and sink paths associated to this widget.
1545 * While removing the path, remove reference to it from both
1546 * source and sink widgets so that path is removed only once.
1548 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1549 list_del(&p->list_sink);
1550 list_del(&p->list_source);
1551 list_del(&p->list);
1552 kfree(p->long_name);
1553 kfree(p);
1555 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1556 list_del(&p->list_sink);
1557 list_del(&p->list_source);
1558 list_del(&p->list);
1559 kfree(p->long_name);
1560 kfree(p);
1562 kfree(w->kcontrols);
1563 kfree(w->name);
1564 kfree(w);
1568 static struct snd_soc_dapm_widget *dapm_find_widget(
1569 struct snd_soc_dapm_context *dapm, const char *pin,
1570 bool search_other_contexts)
1572 struct snd_soc_dapm_widget *w;
1573 struct snd_soc_dapm_widget *fallback = NULL;
1575 list_for_each_entry(w, &dapm->card->widgets, list) {
1576 if (!strcmp(w->name, pin)) {
1577 if (w->dapm == dapm)
1578 return w;
1579 else
1580 fallback = w;
1584 if (search_other_contexts)
1585 return fallback;
1587 return NULL;
1590 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1591 const char *pin, int status)
1593 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
1595 if (!w) {
1596 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1597 return -EINVAL;
1600 w->connected = status;
1601 if (status == 0)
1602 w->force = 0;
1604 return 0;
1608 * snd_soc_dapm_sync - scan and power dapm paths
1609 * @dapm: DAPM context
1611 * Walks all dapm audio paths and powers widgets according to their
1612 * stream or path usage.
1614 * Returns 0 for success.
1616 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1618 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1620 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1622 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1623 const struct snd_soc_dapm_route *route)
1625 struct snd_soc_dapm_path *path;
1626 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1627 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1628 const char *sink;
1629 const char *control = route->control;
1630 const char *source;
1631 char prefixed_sink[80];
1632 char prefixed_source[80];
1633 int ret = 0;
1635 if (dapm->codec && dapm->codec->name_prefix) {
1636 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1637 dapm->codec->name_prefix, route->sink);
1638 sink = prefixed_sink;
1639 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1640 dapm->codec->name_prefix, route->source);
1641 source = prefixed_source;
1642 } else {
1643 sink = route->sink;
1644 source = route->source;
1648 * find src and dest widgets over all widgets but favor a widget from
1649 * current DAPM context
1651 list_for_each_entry(w, &dapm->card->widgets, list) {
1652 if (!wsink && !(strcmp(w->name, sink))) {
1653 wtsink = w;
1654 if (w->dapm == dapm)
1655 wsink = w;
1656 continue;
1658 if (!wsource && !(strcmp(w->name, source))) {
1659 wtsource = w;
1660 if (w->dapm == dapm)
1661 wsource = w;
1664 /* use widget from another DAPM context if not found from this */
1665 if (!wsink)
1666 wsink = wtsink;
1667 if (!wsource)
1668 wsource = wtsource;
1670 if (wsource == NULL || wsink == NULL)
1671 return -ENODEV;
1673 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1674 if (!path)
1675 return -ENOMEM;
1677 path->source = wsource;
1678 path->sink = wsink;
1679 path->connected = route->connected;
1680 INIT_LIST_HEAD(&path->list);
1681 INIT_LIST_HEAD(&path->list_source);
1682 INIT_LIST_HEAD(&path->list_sink);
1684 /* check for external widgets */
1685 if (wsink->id == snd_soc_dapm_input) {
1686 if (wsource->id == snd_soc_dapm_micbias ||
1687 wsource->id == snd_soc_dapm_mic ||
1688 wsource->id == snd_soc_dapm_line ||
1689 wsource->id == snd_soc_dapm_output)
1690 wsink->ext = 1;
1692 if (wsource->id == snd_soc_dapm_output) {
1693 if (wsink->id == snd_soc_dapm_spk ||
1694 wsink->id == snd_soc_dapm_hp ||
1695 wsink->id == snd_soc_dapm_line ||
1696 wsink->id == snd_soc_dapm_input)
1697 wsource->ext = 1;
1700 /* connect static paths */
1701 if (control == NULL) {
1702 list_add(&path->list, &dapm->card->paths);
1703 list_add(&path->list_sink, &wsink->sources);
1704 list_add(&path->list_source, &wsource->sinks);
1705 path->connect = 1;
1706 return 0;
1709 /* connect dynamic paths */
1710 switch (wsink->id) {
1711 case snd_soc_dapm_adc:
1712 case snd_soc_dapm_dac:
1713 case snd_soc_dapm_pga:
1714 case snd_soc_dapm_out_drv:
1715 case snd_soc_dapm_input:
1716 case snd_soc_dapm_output:
1717 case snd_soc_dapm_micbias:
1718 case snd_soc_dapm_vmid:
1719 case snd_soc_dapm_pre:
1720 case snd_soc_dapm_post:
1721 case snd_soc_dapm_supply:
1722 case snd_soc_dapm_aif_in:
1723 case snd_soc_dapm_aif_out:
1724 list_add(&path->list, &dapm->card->paths);
1725 list_add(&path->list_sink, &wsink->sources);
1726 list_add(&path->list_source, &wsource->sinks);
1727 path->connect = 1;
1728 return 0;
1729 case snd_soc_dapm_mux:
1730 case snd_soc_dapm_virt_mux:
1731 case snd_soc_dapm_value_mux:
1732 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
1733 &wsink->kcontrol_news[0]);
1734 if (ret != 0)
1735 goto err;
1736 break;
1737 case snd_soc_dapm_switch:
1738 case snd_soc_dapm_mixer:
1739 case snd_soc_dapm_mixer_named_ctl:
1740 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
1741 if (ret != 0)
1742 goto err;
1743 break;
1744 case snd_soc_dapm_hp:
1745 case snd_soc_dapm_mic:
1746 case snd_soc_dapm_line:
1747 case snd_soc_dapm_spk:
1748 list_add(&path->list, &dapm->card->paths);
1749 list_add(&path->list_sink, &wsink->sources);
1750 list_add(&path->list_source, &wsource->sinks);
1751 path->connect = 0;
1752 return 0;
1754 return 0;
1756 err:
1757 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1758 source, control, sink);
1759 kfree(path);
1760 return ret;
1764 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1765 * @dapm: DAPM context
1766 * @route: audio routes
1767 * @num: number of routes
1769 * Connects 2 dapm widgets together via a named audio path. The sink is
1770 * the widget receiving the audio signal, whilst the source is the sender
1771 * of the audio signal.
1773 * Returns 0 for success else error. On error all resources can be freed
1774 * with a call to snd_soc_card_free().
1776 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
1777 const struct snd_soc_dapm_route *route, int num)
1779 int i, ret;
1781 for (i = 0; i < num; i++) {
1782 ret = snd_soc_dapm_add_route(dapm, route);
1783 if (ret < 0) {
1784 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1785 route->source, route->sink);
1786 return ret;
1788 route++;
1791 return 0;
1793 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1796 * snd_soc_dapm_new_widgets - add new dapm widgets
1797 * @dapm: DAPM context
1799 * Checks the codec for any new dapm widgets and creates them if found.
1801 * Returns 0 for success.
1803 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
1805 struct snd_soc_dapm_widget *w;
1806 unsigned int val;
1808 list_for_each_entry(w, &dapm->card->widgets, list)
1810 if (w->new)
1811 continue;
1813 if (w->num_kcontrols) {
1814 w->kcontrols = kzalloc(w->num_kcontrols *
1815 sizeof(struct snd_kcontrol *),
1816 GFP_KERNEL);
1817 if (!w->kcontrols)
1818 return -ENOMEM;
1821 switch(w->id) {
1822 case snd_soc_dapm_switch:
1823 case snd_soc_dapm_mixer:
1824 case snd_soc_dapm_mixer_named_ctl:
1825 w->power_check = dapm_generic_check_power;
1826 dapm_new_mixer(dapm, w);
1827 break;
1828 case snd_soc_dapm_mux:
1829 case snd_soc_dapm_virt_mux:
1830 case snd_soc_dapm_value_mux:
1831 w->power_check = dapm_generic_check_power;
1832 dapm_new_mux(dapm, w);
1833 break;
1834 case snd_soc_dapm_adc:
1835 case snd_soc_dapm_aif_out:
1836 w->power_check = dapm_adc_check_power;
1837 break;
1838 case snd_soc_dapm_dac:
1839 case snd_soc_dapm_aif_in:
1840 w->power_check = dapm_dac_check_power;
1841 break;
1842 case snd_soc_dapm_pga:
1843 case snd_soc_dapm_out_drv:
1844 w->power_check = dapm_generic_check_power;
1845 dapm_new_pga(dapm, w);
1846 break;
1847 case snd_soc_dapm_input:
1848 case snd_soc_dapm_output:
1849 case snd_soc_dapm_micbias:
1850 case snd_soc_dapm_spk:
1851 case snd_soc_dapm_hp:
1852 case snd_soc_dapm_mic:
1853 case snd_soc_dapm_line:
1854 w->power_check = dapm_generic_check_power;
1855 break;
1856 case snd_soc_dapm_supply:
1857 w->power_check = dapm_supply_check_power;
1858 case snd_soc_dapm_vmid:
1859 case snd_soc_dapm_pre:
1860 case snd_soc_dapm_post:
1861 break;
1864 /* Read the initial power state from the device */
1865 if (w->reg >= 0) {
1866 val = snd_soc_read(w->codec, w->reg);
1867 val &= 1 << w->shift;
1868 if (w->invert)
1869 val = !val;
1871 if (val)
1872 w->power = 1;
1875 w->new = 1;
1877 dapm_debugfs_add_widget(w);
1880 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1881 return 0;
1883 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1886 * snd_soc_dapm_get_volsw - dapm mixer get callback
1887 * @kcontrol: mixer control
1888 * @ucontrol: control element information
1890 * Callback to get the value of a dapm mixer control.
1892 * Returns 0 for success.
1894 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1895 struct snd_ctl_elem_value *ucontrol)
1897 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1898 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
1899 struct soc_mixer_control *mc =
1900 (struct soc_mixer_control *)kcontrol->private_value;
1901 unsigned int reg = mc->reg;
1902 unsigned int shift = mc->shift;
1903 unsigned int rshift = mc->rshift;
1904 int max = mc->max;
1905 unsigned int invert = mc->invert;
1906 unsigned int mask = (1 << fls(max)) - 1;
1908 ucontrol->value.integer.value[0] =
1909 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1910 if (shift != rshift)
1911 ucontrol->value.integer.value[1] =
1912 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1913 if (invert) {
1914 ucontrol->value.integer.value[0] =
1915 max - ucontrol->value.integer.value[0];
1916 if (shift != rshift)
1917 ucontrol->value.integer.value[1] =
1918 max - ucontrol->value.integer.value[1];
1921 return 0;
1923 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1926 * snd_soc_dapm_put_volsw - dapm mixer set callback
1927 * @kcontrol: mixer control
1928 * @ucontrol: control element information
1930 * Callback to set the value of a dapm mixer control.
1932 * Returns 0 for success.
1934 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1935 struct snd_ctl_elem_value *ucontrol)
1937 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1938 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
1939 struct snd_soc_codec *codec = widget->codec;
1940 struct soc_mixer_control *mc =
1941 (struct soc_mixer_control *)kcontrol->private_value;
1942 unsigned int reg = mc->reg;
1943 unsigned int shift = mc->shift;
1944 int max = mc->max;
1945 unsigned int mask = (1 << fls(max)) - 1;
1946 unsigned int invert = mc->invert;
1947 unsigned int val;
1948 int connect, change;
1949 struct snd_soc_dapm_update update;
1950 int wi;
1952 val = (ucontrol->value.integer.value[0] & mask);
1954 if (invert)
1955 val = max - val;
1956 mask = mask << shift;
1957 val = val << shift;
1959 if (val)
1960 /* new connection */
1961 connect = invert ? 0 : 1;
1962 else
1963 /* old connection must be powered down */
1964 connect = invert ? 1 : 0;
1966 mutex_lock(&codec->mutex);
1968 change = snd_soc_test_bits(widget->codec, reg, mask, val);
1969 if (change) {
1970 for (wi = 0; wi < wlist->num_widgets; wi++) {
1971 widget = wlist->widgets[wi];
1973 widget->value = val;
1975 update.kcontrol = kcontrol;
1976 update.widget = widget;
1977 update.reg = reg;
1978 update.mask = mask;
1979 update.val = val;
1980 widget->dapm->update = &update;
1982 dapm_mixer_update_power(widget, kcontrol, connect);
1984 widget->dapm->update = NULL;
1988 mutex_unlock(&codec->mutex);
1989 return 0;
1991 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1994 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1995 * @kcontrol: mixer control
1996 * @ucontrol: control element information
1998 * Callback to get the value of a dapm enumerated double mixer control.
2000 * Returns 0 for success.
2002 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2003 struct snd_ctl_elem_value *ucontrol)
2005 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2006 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2007 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2008 unsigned int val, bitmask;
2010 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2012 val = snd_soc_read(widget->codec, e->reg);
2013 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2014 if (e->shift_l != e->shift_r)
2015 ucontrol->value.enumerated.item[1] =
2016 (val >> e->shift_r) & (bitmask - 1);
2018 return 0;
2020 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2023 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2024 * @kcontrol: mixer control
2025 * @ucontrol: control element information
2027 * Callback to set the value of a dapm enumerated double mixer control.
2029 * Returns 0 for success.
2031 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2032 struct snd_ctl_elem_value *ucontrol)
2034 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2035 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2036 struct snd_soc_codec *codec = widget->codec;
2037 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2038 unsigned int val, mux, change;
2039 unsigned int mask, bitmask;
2040 struct snd_soc_dapm_update update;
2041 int wi;
2043 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2045 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2046 return -EINVAL;
2047 mux = ucontrol->value.enumerated.item[0];
2048 val = mux << e->shift_l;
2049 mask = (bitmask - 1) << e->shift_l;
2050 if (e->shift_l != e->shift_r) {
2051 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2052 return -EINVAL;
2053 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2054 mask |= (bitmask - 1) << e->shift_r;
2057 mutex_lock(&codec->mutex);
2059 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2060 if (change) {
2061 for (wi = 0; wi < wlist->num_widgets; wi++) {
2062 widget = wlist->widgets[wi];
2064 widget->value = val;
2066 update.kcontrol = kcontrol;
2067 update.widget = widget;
2068 update.reg = e->reg;
2069 update.mask = mask;
2070 update.val = val;
2071 widget->dapm->update = &update;
2073 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2075 widget->dapm->update = NULL;
2079 mutex_unlock(&codec->mutex);
2080 return change;
2082 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2085 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2086 * @kcontrol: mixer control
2087 * @ucontrol: control element information
2089 * Returns 0 for success.
2091 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2092 struct snd_ctl_elem_value *ucontrol)
2094 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2095 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2097 ucontrol->value.enumerated.item[0] = widget->value;
2099 return 0;
2101 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2104 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2105 * @kcontrol: mixer control
2106 * @ucontrol: control element information
2108 * Returns 0 for success.
2110 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2111 struct snd_ctl_elem_value *ucontrol)
2113 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2114 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2115 struct snd_soc_codec *codec = widget->codec;
2116 struct soc_enum *e =
2117 (struct soc_enum *)kcontrol->private_value;
2118 int change;
2119 int ret = 0;
2120 int wi;
2122 if (ucontrol->value.enumerated.item[0] >= e->max)
2123 return -EINVAL;
2125 mutex_lock(&codec->mutex);
2127 change = widget->value != ucontrol->value.enumerated.item[0];
2128 if (change) {
2129 for (wi = 0; wi < wlist->num_widgets; wi++) {
2130 widget = wlist->widgets[wi];
2132 widget->value = ucontrol->value.enumerated.item[0];
2134 dapm_mux_update_power(widget, kcontrol, change,
2135 widget->value, e);
2139 mutex_unlock(&codec->mutex);
2140 return ret;
2142 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2145 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2146 * callback
2147 * @kcontrol: mixer control
2148 * @ucontrol: control element information
2150 * Callback to get the value of a dapm semi enumerated double mixer control.
2152 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2153 * used for handling bitfield coded enumeration for example.
2155 * Returns 0 for success.
2157 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2158 struct snd_ctl_elem_value *ucontrol)
2160 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2161 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2162 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2163 unsigned int reg_val, val, mux;
2165 reg_val = snd_soc_read(widget->codec, e->reg);
2166 val = (reg_val >> e->shift_l) & e->mask;
2167 for (mux = 0; mux < e->max; mux++) {
2168 if (val == e->values[mux])
2169 break;
2171 ucontrol->value.enumerated.item[0] = mux;
2172 if (e->shift_l != e->shift_r) {
2173 val = (reg_val >> e->shift_r) & e->mask;
2174 for (mux = 0; mux < e->max; mux++) {
2175 if (val == e->values[mux])
2176 break;
2178 ucontrol->value.enumerated.item[1] = mux;
2181 return 0;
2183 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2186 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2187 * callback
2188 * @kcontrol: mixer control
2189 * @ucontrol: control element information
2191 * Callback to set the value of a dapm semi enumerated double mixer control.
2193 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2194 * used for handling bitfield coded enumeration for example.
2196 * Returns 0 for success.
2198 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2199 struct snd_ctl_elem_value *ucontrol)
2201 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2202 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2203 struct snd_soc_codec *codec = widget->codec;
2204 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2205 unsigned int val, mux, change;
2206 unsigned int mask;
2207 struct snd_soc_dapm_update update;
2208 int wi;
2210 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2211 return -EINVAL;
2212 mux = ucontrol->value.enumerated.item[0];
2213 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2214 mask = e->mask << e->shift_l;
2215 if (e->shift_l != e->shift_r) {
2216 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2217 return -EINVAL;
2218 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2219 mask |= e->mask << e->shift_r;
2222 mutex_lock(&codec->mutex);
2224 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2225 if (change) {
2226 for (wi = 0; wi < wlist->num_widgets; wi++) {
2227 widget = wlist->widgets[wi];
2229 widget->value = val;
2231 update.kcontrol = kcontrol;
2232 update.widget = widget;
2233 update.reg = e->reg;
2234 update.mask = mask;
2235 update.val = val;
2236 widget->dapm->update = &update;
2238 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2240 widget->dapm->update = NULL;
2244 mutex_unlock(&codec->mutex);
2245 return change;
2247 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2250 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2252 * @kcontrol: mixer control
2253 * @uinfo: control element information
2255 * Callback to provide information about a pin switch control.
2257 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2258 struct snd_ctl_elem_info *uinfo)
2260 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2261 uinfo->count = 1;
2262 uinfo->value.integer.min = 0;
2263 uinfo->value.integer.max = 1;
2265 return 0;
2267 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2270 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2272 * @kcontrol: mixer control
2273 * @ucontrol: Value
2275 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2276 struct snd_ctl_elem_value *ucontrol)
2278 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2279 const char *pin = (const char *)kcontrol->private_value;
2281 mutex_lock(&codec->mutex);
2283 ucontrol->value.integer.value[0] =
2284 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2286 mutex_unlock(&codec->mutex);
2288 return 0;
2290 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2293 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2295 * @kcontrol: mixer control
2296 * @ucontrol: Value
2298 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2299 struct snd_ctl_elem_value *ucontrol)
2301 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2302 const char *pin = (const char *)kcontrol->private_value;
2304 mutex_lock(&codec->mutex);
2306 if (ucontrol->value.integer.value[0])
2307 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2308 else
2309 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2311 snd_soc_dapm_sync(&codec->dapm);
2313 mutex_unlock(&codec->mutex);
2315 return 0;
2317 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2320 * snd_soc_dapm_new_control - create new dapm control
2321 * @dapm: DAPM context
2322 * @widget: widget template
2324 * Creates a new dapm control based upon the template.
2326 * Returns 0 for success else error.
2328 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2329 const struct snd_soc_dapm_widget *widget)
2331 struct snd_soc_dapm_widget *w;
2332 size_t name_len;
2334 if ((w = dapm_cnew_widget(widget)) == NULL)
2335 return -ENOMEM;
2337 name_len = strlen(widget->name) + 1;
2338 if (dapm->codec && dapm->codec->name_prefix)
2339 name_len += 1 + strlen(dapm->codec->name_prefix);
2340 w->name = kmalloc(name_len, GFP_KERNEL);
2341 if (w->name == NULL) {
2342 kfree(w);
2343 return -ENOMEM;
2345 if (dapm->codec && dapm->codec->name_prefix)
2346 snprintf(w->name, name_len, "%s %s",
2347 dapm->codec->name_prefix, widget->name);
2348 else
2349 snprintf(w->name, name_len, "%s", widget->name);
2351 dapm->n_widgets++;
2352 w->dapm = dapm;
2353 w->codec = dapm->codec;
2354 INIT_LIST_HEAD(&w->sources);
2355 INIT_LIST_HEAD(&w->sinks);
2356 INIT_LIST_HEAD(&w->list);
2357 list_add(&w->list, &dapm->card->widgets);
2359 /* machine layer set ups unconnected pins and insertions */
2360 w->connected = 1;
2361 return 0;
2363 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2366 * snd_soc_dapm_new_controls - create new dapm controls
2367 * @dapm: DAPM context
2368 * @widget: widget array
2369 * @num: number of widgets
2371 * Creates new DAPM controls based upon the templates.
2373 * Returns 0 for success else error.
2375 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2376 const struct snd_soc_dapm_widget *widget,
2377 int num)
2379 int i, ret;
2381 for (i = 0; i < num; i++) {
2382 ret = snd_soc_dapm_new_control(dapm, widget);
2383 if (ret < 0) {
2384 dev_err(dapm->dev,
2385 "ASoC: Failed to create DAPM control %s: %d\n",
2386 widget->name, ret);
2387 return ret;
2389 widget++;
2391 return 0;
2393 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2395 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2396 const char *stream, int event)
2398 struct snd_soc_dapm_widget *w;
2400 list_for_each_entry(w, &dapm->card->widgets, list)
2402 if (!w->sname || w->dapm != dapm)
2403 continue;
2404 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2405 w->name, w->sname, stream, event);
2406 if (strstr(w->sname, stream)) {
2407 switch(event) {
2408 case SND_SOC_DAPM_STREAM_START:
2409 w->active = 1;
2410 break;
2411 case SND_SOC_DAPM_STREAM_STOP:
2412 w->active = 0;
2413 break;
2414 case SND_SOC_DAPM_STREAM_SUSPEND:
2415 case SND_SOC_DAPM_STREAM_RESUME:
2416 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2417 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2418 break;
2423 dapm_power_widgets(dapm, event);
2427 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2428 * @rtd: PCM runtime data
2429 * @stream: stream name
2430 * @event: stream event
2432 * Sends a stream event to the dapm core. The core then makes any
2433 * necessary widget power changes.
2435 * Returns 0 for success else error.
2437 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2438 const char *stream, int event)
2440 struct snd_soc_codec *codec = rtd->codec;
2442 if (stream == NULL)
2443 return 0;
2445 mutex_lock(&codec->mutex);
2446 soc_dapm_stream_event(&codec->dapm, stream, event);
2447 mutex_unlock(&codec->mutex);
2448 return 0;
2452 * snd_soc_dapm_enable_pin - enable pin.
2453 * @dapm: DAPM context
2454 * @pin: pin name
2456 * Enables input/output pin and its parents or children widgets iff there is
2457 * a valid audio route and active audio stream.
2458 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2459 * do any widget power switching.
2461 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2463 return snd_soc_dapm_set_pin(dapm, pin, 1);
2465 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2468 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2469 * @dapm: DAPM context
2470 * @pin: pin name
2472 * Enables input/output pin regardless of any other state. This is
2473 * intended for use with microphone bias supplies used in microphone
2474 * jack detection.
2476 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2477 * do any widget power switching.
2479 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2480 const char *pin)
2482 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2484 if (!w) {
2485 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2486 return -EINVAL;
2489 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2490 w->connected = 1;
2491 w->force = 1;
2493 return 0;
2495 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2498 * snd_soc_dapm_disable_pin - disable pin.
2499 * @dapm: DAPM context
2500 * @pin: pin name
2502 * Disables input/output pin and its parents or children widgets.
2503 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2504 * do any widget power switching.
2506 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2507 const char *pin)
2509 return snd_soc_dapm_set_pin(dapm, pin, 0);
2511 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2514 * snd_soc_dapm_nc_pin - permanently disable pin.
2515 * @dapm: DAPM context
2516 * @pin: pin name
2518 * Marks the specified pin as being not connected, disabling it along
2519 * any parent or child widgets. At present this is identical to
2520 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2521 * additional things such as disabling controls which only affect
2522 * paths through the pin.
2524 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2525 * do any widget power switching.
2527 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2529 return snd_soc_dapm_set_pin(dapm, pin, 0);
2531 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2534 * snd_soc_dapm_get_pin_status - get audio pin status
2535 * @dapm: DAPM context
2536 * @pin: audio signal pin endpoint (or start point)
2538 * Get audio pin status - connected or disconnected.
2540 * Returns 1 for connected otherwise 0.
2542 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2543 const char *pin)
2545 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2547 if (w)
2548 return w->connected;
2550 return 0;
2552 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2555 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2556 * @dapm: DAPM context
2557 * @pin: audio signal pin endpoint (or start point)
2559 * Mark the given endpoint or pin as ignoring suspend. When the
2560 * system is disabled a path between two endpoints flagged as ignoring
2561 * suspend will not be disabled. The path must already be enabled via
2562 * normal means at suspend time, it will not be turned on if it was not
2563 * already enabled.
2565 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2566 const char *pin)
2568 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
2570 if (!w) {
2571 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2572 return -EINVAL;
2575 w->ignore_suspend = 1;
2577 return 0;
2579 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2582 * snd_soc_dapm_free - free dapm resources
2583 * @card: SoC device
2585 * Free all dapm widgets and resources.
2587 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2589 snd_soc_dapm_sys_remove(dapm->dev);
2590 dapm_debugfs_cleanup(dapm);
2591 dapm_free_widgets(dapm);
2592 list_del(&dapm->list);
2594 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2596 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
2598 struct snd_soc_dapm_widget *w;
2599 LIST_HEAD(down_list);
2600 int powerdown = 0;
2602 list_for_each_entry(w, &dapm->card->widgets, list) {
2603 if (w->dapm != dapm)
2604 continue;
2605 if (w->power) {
2606 dapm_seq_insert(w, &down_list, false);
2607 w->power = 0;
2608 powerdown = 1;
2612 /* If there were no widgets to power down we're already in
2613 * standby.
2615 if (powerdown) {
2616 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
2617 dapm_seq_run(dapm, &down_list, 0, false);
2618 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
2623 * snd_soc_dapm_shutdown - callback for system shutdown
2625 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2627 struct snd_soc_codec *codec;
2629 list_for_each_entry(codec, &card->codec_dev_list, list) {
2630 soc_dapm_shutdown_codec(&codec->dapm);
2631 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
2635 /* Module information */
2636 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2637 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2638 MODULE_LICENSE("GPL");