ASoC: Split DAPM power checks from sequencing of power changes
[linux-2.6/btrfs-unstable.git] / sound / soc / soc-dapm.c
blob04ef84106d7c9d7172324c5ec012cfba94f98fa4
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 * DAC's/ADC's.
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/delay.h>
36 #include <linux/pm.h>
37 #include <linux/bitops.h>
38 #include <linux/platform_device.h>
39 #include <linux/jiffies.h>
40 #include <sound/core.h>
41 #include <sound/pcm.h>
42 #include <sound/pcm_params.h>
43 #include <sound/soc-dapm.h>
44 #include <sound/initval.h>
46 /* debug */
47 #ifdef DEBUG
48 #define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
49 #else
50 #define dump_dapm(codec, action)
51 #endif
53 /* dapm power sequences - make this per codec in the future */
54 static int dapm_up_seq[] = {
55 snd_soc_dapm_pre, snd_soc_dapm_supply, snd_soc_dapm_micbias,
56 snd_soc_dapm_mic, snd_soc_dapm_mux, snd_soc_dapm_value_mux,
57 snd_soc_dapm_dac, snd_soc_dapm_mixer, snd_soc_dapm_mixer_named_ctl,
58 snd_soc_dapm_pga, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
59 snd_soc_dapm_post
62 static int dapm_down_seq[] = {
63 snd_soc_dapm_pre, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
64 snd_soc_dapm_pga, snd_soc_dapm_mixer_named_ctl, snd_soc_dapm_mixer,
65 snd_soc_dapm_dac, snd_soc_dapm_mic, snd_soc_dapm_micbias,
66 snd_soc_dapm_mux, snd_soc_dapm_value_mux, snd_soc_dapm_supply,
67 snd_soc_dapm_post
70 static int dapm_status = 1;
71 module_param(dapm_status, int, 0);
72 MODULE_PARM_DESC(dapm_status, "enable DPM sysfs entries");
74 static void pop_wait(u32 pop_time)
76 if (pop_time)
77 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
80 static void pop_dbg(u32 pop_time, const char *fmt, ...)
82 va_list args;
84 va_start(args, fmt);
86 if (pop_time) {
87 vprintk(fmt, args);
88 pop_wait(pop_time);
91 va_end(args);
94 /* create a new dapm widget */
95 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
96 const struct snd_soc_dapm_widget *_widget)
98 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
101 /* set up initial codec paths */
102 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
103 struct snd_soc_dapm_path *p, int i)
105 switch (w->id) {
106 case snd_soc_dapm_switch:
107 case snd_soc_dapm_mixer:
108 case snd_soc_dapm_mixer_named_ctl: {
109 int val;
110 struct soc_mixer_control *mc = (struct soc_mixer_control *)
111 w->kcontrols[i].private_value;
112 unsigned int reg = mc->reg;
113 unsigned int shift = mc->shift;
114 int max = mc->max;
115 unsigned int mask = (1 << fls(max)) - 1;
116 unsigned int invert = mc->invert;
118 val = snd_soc_read(w->codec, reg);
119 val = (val >> shift) & mask;
121 if ((invert && !val) || (!invert && val))
122 p->connect = 1;
123 else
124 p->connect = 0;
126 break;
127 case snd_soc_dapm_mux: {
128 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
129 int val, item, bitmask;
131 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
133 val = snd_soc_read(w->codec, e->reg);
134 item = (val >> e->shift_l) & (bitmask - 1);
136 p->connect = 0;
137 for (i = 0; i < e->max; i++) {
138 if (!(strcmp(p->name, e->texts[i])) && item == i)
139 p->connect = 1;
142 break;
143 case snd_soc_dapm_value_mux: {
144 struct soc_enum *e = (struct soc_enum *)
145 w->kcontrols[i].private_value;
146 int val, item;
148 val = snd_soc_read(w->codec, e->reg);
149 val = (val >> e->shift_l) & e->mask;
150 for (item = 0; item < e->max; item++) {
151 if (val == e->values[item])
152 break;
155 p->connect = 0;
156 for (i = 0; i < e->max; i++) {
157 if (!(strcmp(p->name, e->texts[i])) && item == i)
158 p->connect = 1;
161 break;
162 /* does not effect routing - always connected */
163 case snd_soc_dapm_pga:
164 case snd_soc_dapm_output:
165 case snd_soc_dapm_adc:
166 case snd_soc_dapm_input:
167 case snd_soc_dapm_dac:
168 case snd_soc_dapm_micbias:
169 case snd_soc_dapm_vmid:
170 case snd_soc_dapm_supply:
171 p->connect = 1;
172 break;
173 /* does effect routing - dynamically connected */
174 case snd_soc_dapm_hp:
175 case snd_soc_dapm_mic:
176 case snd_soc_dapm_spk:
177 case snd_soc_dapm_line:
178 case snd_soc_dapm_pre:
179 case snd_soc_dapm_post:
180 p->connect = 0;
181 break;
185 /* connect mux widget to it's interconnecting audio paths */
186 static int dapm_connect_mux(struct snd_soc_codec *codec,
187 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
188 struct snd_soc_dapm_path *path, const char *control_name,
189 const struct snd_kcontrol_new *kcontrol)
191 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
192 int i;
194 for (i = 0; i < e->max; i++) {
195 if (!(strcmp(control_name, e->texts[i]))) {
196 list_add(&path->list, &codec->dapm_paths);
197 list_add(&path->list_sink, &dest->sources);
198 list_add(&path->list_source, &src->sinks);
199 path->name = (char*)e->texts[i];
200 dapm_set_path_status(dest, path, 0);
201 return 0;
205 return -ENODEV;
208 /* connect mixer widget to it's interconnecting audio paths */
209 static int dapm_connect_mixer(struct snd_soc_codec *codec,
210 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
211 struct snd_soc_dapm_path *path, const char *control_name)
213 int i;
215 /* search for mixer kcontrol */
216 for (i = 0; i < dest->num_kcontrols; i++) {
217 if (!strcmp(control_name, dest->kcontrols[i].name)) {
218 list_add(&path->list, &codec->dapm_paths);
219 list_add(&path->list_sink, &dest->sources);
220 list_add(&path->list_source, &src->sinks);
221 path->name = dest->kcontrols[i].name;
222 dapm_set_path_status(dest, path, i);
223 return 0;
226 return -ENODEV;
229 /* update dapm codec register bits */
230 static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
232 int change, power;
233 unsigned short old, new;
234 struct snd_soc_codec *codec = widget->codec;
236 /* check for valid widgets */
237 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
238 widget->id == snd_soc_dapm_output ||
239 widget->id == snd_soc_dapm_hp ||
240 widget->id == snd_soc_dapm_mic ||
241 widget->id == snd_soc_dapm_line ||
242 widget->id == snd_soc_dapm_spk)
243 return 0;
245 power = widget->power;
246 if (widget->invert)
247 power = (power ? 0:1);
249 old = snd_soc_read(codec, widget->reg);
250 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
252 change = old != new;
253 if (change) {
254 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
255 widget->name, widget->power ? "on" : "off",
256 codec->pop_time);
257 snd_soc_write(codec, widget->reg, new);
258 pop_wait(codec->pop_time);
260 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
261 old, new, change);
262 return change;
265 /* ramps the volume up or down to minimise pops before or after a
266 * DAPM power event */
267 static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
269 const struct snd_kcontrol_new *k = widget->kcontrols;
271 if (widget->muted && !power)
272 return 0;
273 if (!widget->muted && power)
274 return 0;
276 if (widget->num_kcontrols && k) {
277 struct soc_mixer_control *mc =
278 (struct soc_mixer_control *)k->private_value;
279 unsigned int reg = mc->reg;
280 unsigned int shift = mc->shift;
281 int max = mc->max;
282 unsigned int mask = (1 << fls(max)) - 1;
283 unsigned int invert = mc->invert;
285 if (power) {
286 int i;
287 /* power up has happended, increase volume to last level */
288 if (invert) {
289 for (i = max; i > widget->saved_value; i--)
290 snd_soc_update_bits(widget->codec, reg, mask, i);
291 } else {
292 for (i = 0; i < widget->saved_value; i++)
293 snd_soc_update_bits(widget->codec, reg, mask, i);
295 widget->muted = 0;
296 } else {
297 /* power down is about to occur, decrease volume to mute */
298 int val = snd_soc_read(widget->codec, reg);
299 int i = widget->saved_value = (val >> shift) & mask;
300 if (invert) {
301 for (; i < mask; i++)
302 snd_soc_update_bits(widget->codec, reg, mask, i);
303 } else {
304 for (; i > 0; i--)
305 snd_soc_update_bits(widget->codec, reg, mask, i);
307 widget->muted = 1;
310 return 0;
313 /* create new dapm mixer control */
314 static int dapm_new_mixer(struct snd_soc_codec *codec,
315 struct snd_soc_dapm_widget *w)
317 int i, ret = 0;
318 size_t name_len;
319 struct snd_soc_dapm_path *path;
321 /* add kcontrol */
322 for (i = 0; i < w->num_kcontrols; i++) {
324 /* match name */
325 list_for_each_entry(path, &w->sources, list_sink) {
327 /* mixer/mux paths name must match control name */
328 if (path->name != (char*)w->kcontrols[i].name)
329 continue;
331 /* add dapm control with long name.
332 * for dapm_mixer this is the concatenation of the
333 * mixer and kcontrol name.
334 * for dapm_mixer_named_ctl this is simply the
335 * kcontrol name.
337 name_len = strlen(w->kcontrols[i].name) + 1;
338 if (w->id != snd_soc_dapm_mixer_named_ctl)
339 name_len += 1 + strlen(w->name);
341 path->long_name = kmalloc(name_len, GFP_KERNEL);
343 if (path->long_name == NULL)
344 return -ENOMEM;
346 switch (w->id) {
347 default:
348 snprintf(path->long_name, name_len, "%s %s",
349 w->name, w->kcontrols[i].name);
350 break;
351 case snd_soc_dapm_mixer_named_ctl:
352 snprintf(path->long_name, name_len, "%s",
353 w->kcontrols[i].name);
354 break;
357 path->long_name[name_len - 1] = '\0';
359 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
360 path->long_name);
361 ret = snd_ctl_add(codec->card, path->kcontrol);
362 if (ret < 0) {
363 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
364 path->long_name,
365 ret);
366 kfree(path->long_name);
367 path->long_name = NULL;
368 return ret;
372 return ret;
375 /* create new dapm mux control */
376 static int dapm_new_mux(struct snd_soc_codec *codec,
377 struct snd_soc_dapm_widget *w)
379 struct snd_soc_dapm_path *path = NULL;
380 struct snd_kcontrol *kcontrol;
381 int ret = 0;
383 if (!w->num_kcontrols) {
384 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
385 return -EINVAL;
388 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
389 ret = snd_ctl_add(codec->card, kcontrol);
390 if (ret < 0)
391 goto err;
393 list_for_each_entry(path, &w->sources, list_sink)
394 path->kcontrol = kcontrol;
396 return ret;
398 err:
399 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
400 return ret;
403 /* create new dapm volume control */
404 static int dapm_new_pga(struct snd_soc_codec *codec,
405 struct snd_soc_dapm_widget *w)
407 struct snd_kcontrol *kcontrol;
408 int ret = 0;
410 if (!w->num_kcontrols)
411 return -EINVAL;
413 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
414 ret = snd_ctl_add(codec->card, kcontrol);
415 if (ret < 0) {
416 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
417 return ret;
420 return ret;
423 /* reset 'walked' bit for each dapm path */
424 static inline void dapm_clear_walk(struct snd_soc_codec *codec)
426 struct snd_soc_dapm_path *p;
428 list_for_each_entry(p, &codec->dapm_paths, list)
429 p->walked = 0;
433 * Recursively check for a completed path to an active or physically connected
434 * output widget. Returns number of complete paths.
436 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
438 struct snd_soc_dapm_path *path;
439 int con = 0;
441 if (widget->id == snd_soc_dapm_supply)
442 return 0;
444 if (widget->id == snd_soc_dapm_adc && widget->active)
445 return 1;
447 if (widget->connected) {
448 /* connected pin ? */
449 if (widget->id == snd_soc_dapm_output && !widget->ext)
450 return 1;
452 /* connected jack or spk ? */
453 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
454 widget->id == snd_soc_dapm_line)
455 return 1;
458 list_for_each_entry(path, &widget->sinks, list_source) {
459 if (path->walked)
460 continue;
462 if (path->sink && path->connect) {
463 path->walked = 1;
464 con += is_connected_output_ep(path->sink);
468 return con;
472 * Recursively check for a completed path to an active or physically connected
473 * input widget. Returns number of complete paths.
475 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
477 struct snd_soc_dapm_path *path;
478 int con = 0;
480 if (widget->id == snd_soc_dapm_supply)
481 return 0;
483 /* active stream ? */
484 if (widget->id == snd_soc_dapm_dac && widget->active)
485 return 1;
487 if (widget->connected) {
488 /* connected pin ? */
489 if (widget->id == snd_soc_dapm_input && !widget->ext)
490 return 1;
492 /* connected VMID/Bias for lower pops */
493 if (widget->id == snd_soc_dapm_vmid)
494 return 1;
496 /* connected jack ? */
497 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
498 return 1;
501 list_for_each_entry(path, &widget->sources, list_sink) {
502 if (path->walked)
503 continue;
505 if (path->source && path->connect) {
506 path->walked = 1;
507 con += is_connected_input_ep(path->source);
511 return con;
515 * Handler for generic register modifier widget.
517 int dapm_reg_event(struct snd_soc_dapm_widget *w,
518 struct snd_kcontrol *kcontrol, int event)
520 unsigned int val;
522 if (SND_SOC_DAPM_EVENT_ON(event))
523 val = w->on_val;
524 else
525 val = w->off_val;
527 snd_soc_update_bits(w->codec, -(w->reg + 1),
528 w->mask << w->shift, val << w->shift);
530 return 0;
532 EXPORT_SYMBOL_GPL(dapm_reg_event);
534 /* Standard power change method, used to apply power changes to most
535 * widgets.
537 static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
539 int ret;
541 /* call any power change event handlers */
542 if (w->event)
543 pr_debug("power %s event for %s flags %x\n",
544 w->power ? "on" : "off",
545 w->name, w->event_flags);
547 /* power up pre event */
548 if (w->power && w->event &&
549 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
550 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
551 if (ret < 0)
552 return ret;
555 /* power down pre event */
556 if (!w->power && w->event &&
557 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
558 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
559 if (ret < 0)
560 return ret;
563 /* Lower PGA volume to reduce pops */
564 if (w->id == snd_soc_dapm_pga && !w->power)
565 dapm_set_pga(w, w->power);
567 dapm_update_bits(w);
569 /* Raise PGA volume to reduce pops */
570 if (w->id == snd_soc_dapm_pga && w->power)
571 dapm_set_pga(w, w->power);
573 /* power up post event */
574 if (w->power && w->event &&
575 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
576 ret = w->event(w,
577 NULL, SND_SOC_DAPM_POST_PMU);
578 if (ret < 0)
579 return ret;
582 /* power down post event */
583 if (!w->power && w->event &&
584 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
585 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
586 if (ret < 0)
587 return ret;
590 return 0;
593 /* Generic check to see if a widget should be powered.
595 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
597 int in, out;
599 in = is_connected_input_ep(w);
600 dapm_clear_walk(w->codec);
601 out = is_connected_output_ep(w);
602 dapm_clear_walk(w->codec);
603 return out != 0 && in != 0;
606 /* Check to see if an ADC has power */
607 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
609 int in;
611 if (w->active) {
612 in = is_connected_input_ep(w);
613 dapm_clear_walk(w->codec);
614 return in != 0;
615 } else {
616 return dapm_generic_check_power(w);
620 /* Check to see if a DAC has power */
621 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
623 int out;
625 if (w->active) {
626 out = is_connected_output_ep(w);
627 dapm_clear_walk(w->codec);
628 return out != 0;
629 } else {
630 return dapm_generic_check_power(w);
634 /* Check to see if a power supply is needed */
635 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
637 struct snd_soc_dapm_path *path;
638 int power = 0;
640 /* Check if one of our outputs is connected */
641 list_for_each_entry(path, &w->sinks, list_source) {
642 if (path->sink && path->sink->power_check &&
643 path->sink->power_check(path->sink)) {
644 power = 1;
645 break;
649 dapm_clear_walk(w->codec);
651 return power;
655 * Scan a single DAPM widget for a complete audio path and update the
656 * power status appropriately.
658 static int dapm_power_widget(struct snd_soc_codec *codec, int event,
659 struct snd_soc_dapm_widget *w)
661 int ret;
663 switch (w->id) {
664 case snd_soc_dapm_pre:
665 if (!w->event)
666 return 0;
668 if (event == SND_SOC_DAPM_STREAM_START) {
669 ret = w->event(w,
670 NULL, SND_SOC_DAPM_PRE_PMU);
671 if (ret < 0)
672 return ret;
673 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
674 ret = w->event(w,
675 NULL, SND_SOC_DAPM_PRE_PMD);
676 if (ret < 0)
677 return ret;
679 return 0;
681 case snd_soc_dapm_post:
682 if (!w->event)
683 return 0;
685 if (event == SND_SOC_DAPM_STREAM_START) {
686 ret = w->event(w,
687 NULL, SND_SOC_DAPM_POST_PMU);
688 if (ret < 0)
689 return ret;
690 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
691 ret = w->event(w,
692 NULL, SND_SOC_DAPM_POST_PMD);
693 if (ret < 0)
694 return ret;
696 return 0;
698 default:
699 return dapm_generic_apply_power(w);
704 * Scan each dapm widget for complete audio path.
705 * A complete path is a route that has valid endpoints i.e.:-
707 * o DAC to output pin.
708 * o Input Pin to ADC.
709 * o Input pin to Output pin (bypass, sidetone)
710 * o DAC to ADC (loopback).
712 static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
714 struct snd_soc_dapm_widget *w;
715 int ret = 0;
716 int i, power;
718 INIT_LIST_HEAD(&codec->up_list);
719 INIT_LIST_HEAD(&codec->down_list);
721 /* Check which widgets we need to power and store them in
722 * lists indicating if they should be powered up or down.
724 list_for_each_entry(w, &codec->dapm_widgets, list) {
725 switch (w->id) {
726 case snd_soc_dapm_pre:
727 list_add_tail(&codec->down_list, &w->power_list);
728 break;
729 case snd_soc_dapm_post:
730 list_add_tail(&codec->up_list, &w->power_list);
731 break;
733 default:
734 if (!w->power_check)
735 continue;
737 power = w->power_check(w);
738 if (w->power == power)
739 continue;
741 if (power)
742 list_add_tail(&w->power_list, &codec->up_list);
743 else
744 list_add_tail(&w->power_list,
745 &codec->down_list);
747 w->power = power;
748 break;
752 /* Power down widgets first; try to avoid amplifying pops. */
753 for (i = 0; i < ARRAY_SIZE(dapm_down_seq); i++) {
754 list_for_each_entry(w, &codec->down_list, power_list) {
755 /* is widget in stream order */
756 if (w->id != dapm_down_seq[i])
757 continue;
759 ret = dapm_power_widget(codec, event, w);
760 if (ret != 0)
761 pr_err("Failed to power down %s: %d\n",
762 w->name, ret);
766 /* Now power up. */
767 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++) {
768 list_for_each_entry(w, &codec->up_list, power_list) {
769 /* is widget in stream order */
770 if (w->id != dapm_up_seq[i])
771 continue;
773 ret = dapm_power_widget(codec, event, w);
774 if (ret != 0)
775 pr_err("Failed to power up %s: %d\n",
776 w->name, ret);
780 return 0;
783 #ifdef DEBUG
784 static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
786 struct snd_soc_dapm_widget *w;
787 struct snd_soc_dapm_path *p = NULL;
788 int in, out;
790 printk("DAPM %s %s\n", codec->name, action);
792 list_for_each_entry(w, &codec->dapm_widgets, list) {
794 /* only display widgets that effect routing */
795 switch (w->id) {
796 case snd_soc_dapm_pre:
797 case snd_soc_dapm_post:
798 case snd_soc_dapm_vmid:
799 continue;
800 case snd_soc_dapm_mux:
801 case snd_soc_dapm_value_mux:
802 case snd_soc_dapm_output:
803 case snd_soc_dapm_input:
804 case snd_soc_dapm_switch:
805 case snd_soc_dapm_hp:
806 case snd_soc_dapm_mic:
807 case snd_soc_dapm_spk:
808 case snd_soc_dapm_line:
809 case snd_soc_dapm_micbias:
810 case snd_soc_dapm_dac:
811 case snd_soc_dapm_adc:
812 case snd_soc_dapm_pga:
813 case snd_soc_dapm_mixer:
814 case snd_soc_dapm_mixer_named_ctl:
815 case snd_soc_dapm_supply:
816 if (w->name) {
817 in = is_connected_input_ep(w);
818 dapm_clear_walk(w->codec);
819 out = is_connected_output_ep(w);
820 dapm_clear_walk(w->codec);
821 printk("%s: %s in %d out %d\n", w->name,
822 w->power ? "On":"Off",in, out);
824 list_for_each_entry(p, &w->sources, list_sink) {
825 if (p->connect)
826 printk(" in %s %s\n", p->name ? p->name : "static",
827 p->source->name);
829 list_for_each_entry(p, &w->sinks, list_source) {
830 if (p->connect)
831 printk(" out %s %s\n", p->name ? p->name : "static",
832 p->sink->name);
835 break;
839 #endif
841 /* test and update the power status of a mux widget */
842 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
843 struct snd_kcontrol *kcontrol, int mask,
844 int mux, int val, struct soc_enum *e)
846 struct snd_soc_dapm_path *path;
847 int found = 0;
849 if (widget->id != snd_soc_dapm_mux &&
850 widget->id != snd_soc_dapm_value_mux)
851 return -ENODEV;
853 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
854 return 0;
856 /* find dapm widget path assoc with kcontrol */
857 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
858 if (path->kcontrol != kcontrol)
859 continue;
861 if (!path->name || !e->texts[mux])
862 continue;
864 found = 1;
865 /* we now need to match the string in the enum to the path */
866 if (!(strcmp(path->name, e->texts[mux])))
867 path->connect = 1; /* new connection */
868 else
869 path->connect = 0; /* old connection must be powered down */
872 if (found) {
873 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
874 dump_dapm(widget->codec, "mux power update");
877 return 0;
880 /* test and update the power status of a mixer or switch widget */
881 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
882 struct snd_kcontrol *kcontrol, int reg,
883 int val_mask, int val, int invert)
885 struct snd_soc_dapm_path *path;
886 int found = 0;
888 if (widget->id != snd_soc_dapm_mixer &&
889 widget->id != snd_soc_dapm_mixer_named_ctl &&
890 widget->id != snd_soc_dapm_switch)
891 return -ENODEV;
893 if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
894 return 0;
896 /* find dapm widget path assoc with kcontrol */
897 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
898 if (path->kcontrol != kcontrol)
899 continue;
901 /* found, now check type */
902 found = 1;
903 if (val)
904 /* new connection */
905 path->connect = invert ? 0:1;
906 else
907 /* old connection must be powered down */
908 path->connect = invert ? 1:0;
909 break;
912 if (found) {
913 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
914 dump_dapm(widget->codec, "mixer power update");
917 return 0;
920 /* show dapm widget status in sys fs */
921 static ssize_t dapm_widget_show(struct device *dev,
922 struct device_attribute *attr, char *buf)
924 struct snd_soc_device *devdata = dev_get_drvdata(dev);
925 struct snd_soc_codec *codec = devdata->card->codec;
926 struct snd_soc_dapm_widget *w;
927 int count = 0;
928 char *state = "not set";
930 list_for_each_entry(w, &codec->dapm_widgets, list) {
932 /* only display widgets that burnm power */
933 switch (w->id) {
934 case snd_soc_dapm_hp:
935 case snd_soc_dapm_mic:
936 case snd_soc_dapm_spk:
937 case snd_soc_dapm_line:
938 case snd_soc_dapm_micbias:
939 case snd_soc_dapm_dac:
940 case snd_soc_dapm_adc:
941 case snd_soc_dapm_pga:
942 case snd_soc_dapm_mixer:
943 case snd_soc_dapm_mixer_named_ctl:
944 case snd_soc_dapm_supply:
945 if (w->name)
946 count += sprintf(buf + count, "%s: %s\n",
947 w->name, w->power ? "On":"Off");
948 break;
949 default:
950 break;
954 switch (codec->bias_level) {
955 case SND_SOC_BIAS_ON:
956 state = "On";
957 break;
958 case SND_SOC_BIAS_PREPARE:
959 state = "Prepare";
960 break;
961 case SND_SOC_BIAS_STANDBY:
962 state = "Standby";
963 break;
964 case SND_SOC_BIAS_OFF:
965 state = "Off";
966 break;
968 count += sprintf(buf + count, "PM State: %s\n", state);
970 return count;
973 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
975 int snd_soc_dapm_sys_add(struct device *dev)
977 if (!dapm_status)
978 return 0;
979 return device_create_file(dev, &dev_attr_dapm_widget);
982 static void snd_soc_dapm_sys_remove(struct device *dev)
984 if (dapm_status) {
985 device_remove_file(dev, &dev_attr_dapm_widget);
989 /* free all dapm widgets and resources */
990 static void dapm_free_widgets(struct snd_soc_codec *codec)
992 struct snd_soc_dapm_widget *w, *next_w;
993 struct snd_soc_dapm_path *p, *next_p;
995 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
996 list_del(&w->list);
997 kfree(w);
1000 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
1001 list_del(&p->list);
1002 kfree(p->long_name);
1003 kfree(p);
1007 static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
1008 const char *pin, int status)
1010 struct snd_soc_dapm_widget *w;
1012 list_for_each_entry(w, &codec->dapm_widgets, list) {
1013 if (!strcmp(w->name, pin)) {
1014 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
1015 w->connected = status;
1016 return 0;
1020 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
1021 return -EINVAL;
1025 * snd_soc_dapm_sync - scan and power dapm paths
1026 * @codec: audio codec
1028 * Walks all dapm audio paths and powers widgets according to their
1029 * stream or path usage.
1031 * Returns 0 for success.
1033 int snd_soc_dapm_sync(struct snd_soc_codec *codec)
1035 int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1036 dump_dapm(codec, "sync");
1037 return ret;
1039 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1041 static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
1042 const char *sink, const char *control, const char *source)
1044 struct snd_soc_dapm_path *path;
1045 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1046 int ret = 0;
1048 /* find src and dest widgets */
1049 list_for_each_entry(w, &codec->dapm_widgets, list) {
1051 if (!wsink && !(strcmp(w->name, sink))) {
1052 wsink = w;
1053 continue;
1055 if (!wsource && !(strcmp(w->name, source))) {
1056 wsource = w;
1060 if (wsource == NULL || wsink == NULL)
1061 return -ENODEV;
1063 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1064 if (!path)
1065 return -ENOMEM;
1067 path->source = wsource;
1068 path->sink = wsink;
1069 INIT_LIST_HEAD(&path->list);
1070 INIT_LIST_HEAD(&path->list_source);
1071 INIT_LIST_HEAD(&path->list_sink);
1073 /* check for external widgets */
1074 if (wsink->id == snd_soc_dapm_input) {
1075 if (wsource->id == snd_soc_dapm_micbias ||
1076 wsource->id == snd_soc_dapm_mic ||
1077 wsink->id == snd_soc_dapm_line ||
1078 wsink->id == snd_soc_dapm_output)
1079 wsink->ext = 1;
1081 if (wsource->id == snd_soc_dapm_output) {
1082 if (wsink->id == snd_soc_dapm_spk ||
1083 wsink->id == snd_soc_dapm_hp ||
1084 wsink->id == snd_soc_dapm_line ||
1085 wsink->id == snd_soc_dapm_input)
1086 wsource->ext = 1;
1089 /* connect static paths */
1090 if (control == NULL) {
1091 list_add(&path->list, &codec->dapm_paths);
1092 list_add(&path->list_sink, &wsink->sources);
1093 list_add(&path->list_source, &wsource->sinks);
1094 path->connect = 1;
1095 return 0;
1098 /* connect dynamic paths */
1099 switch(wsink->id) {
1100 case snd_soc_dapm_adc:
1101 case snd_soc_dapm_dac:
1102 case snd_soc_dapm_pga:
1103 case snd_soc_dapm_input:
1104 case snd_soc_dapm_output:
1105 case snd_soc_dapm_micbias:
1106 case snd_soc_dapm_vmid:
1107 case snd_soc_dapm_pre:
1108 case snd_soc_dapm_post:
1109 case snd_soc_dapm_supply:
1110 list_add(&path->list, &codec->dapm_paths);
1111 list_add(&path->list_sink, &wsink->sources);
1112 list_add(&path->list_source, &wsource->sinks);
1113 path->connect = 1;
1114 return 0;
1115 case snd_soc_dapm_mux:
1116 case snd_soc_dapm_value_mux:
1117 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1118 &wsink->kcontrols[0]);
1119 if (ret != 0)
1120 goto err;
1121 break;
1122 case snd_soc_dapm_switch:
1123 case snd_soc_dapm_mixer:
1124 case snd_soc_dapm_mixer_named_ctl:
1125 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1126 if (ret != 0)
1127 goto err;
1128 break;
1129 case snd_soc_dapm_hp:
1130 case snd_soc_dapm_mic:
1131 case snd_soc_dapm_line:
1132 case snd_soc_dapm_spk:
1133 list_add(&path->list, &codec->dapm_paths);
1134 list_add(&path->list_sink, &wsink->sources);
1135 list_add(&path->list_source, &wsource->sinks);
1136 path->connect = 0;
1137 return 0;
1139 return 0;
1141 err:
1142 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1143 control, sink);
1144 kfree(path);
1145 return ret;
1149 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1150 * @codec: codec
1151 * @route: audio routes
1152 * @num: number of routes
1154 * Connects 2 dapm widgets together via a named audio path. The sink is
1155 * the widget receiving the audio signal, whilst the source is the sender
1156 * of the audio signal.
1158 * Returns 0 for success else error. On error all resources can be freed
1159 * with a call to snd_soc_card_free().
1161 int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1162 const struct snd_soc_dapm_route *route, int num)
1164 int i, ret;
1166 for (i = 0; i < num; i++) {
1167 ret = snd_soc_dapm_add_route(codec, route->sink,
1168 route->control, route->source);
1169 if (ret < 0) {
1170 printk(KERN_ERR "Failed to add route %s->%s\n",
1171 route->source,
1172 route->sink);
1173 return ret;
1175 route++;
1178 return 0;
1180 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1183 * snd_soc_dapm_new_widgets - add new dapm widgets
1184 * @codec: audio codec
1186 * Checks the codec for any new dapm widgets and creates them if found.
1188 * Returns 0 for success.
1190 int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1192 struct snd_soc_dapm_widget *w;
1194 list_for_each_entry(w, &codec->dapm_widgets, list)
1196 if (w->new)
1197 continue;
1199 switch(w->id) {
1200 case snd_soc_dapm_switch:
1201 case snd_soc_dapm_mixer:
1202 case snd_soc_dapm_mixer_named_ctl:
1203 w->power_check = dapm_generic_check_power;
1204 dapm_new_mixer(codec, w);
1205 break;
1206 case snd_soc_dapm_mux:
1207 case snd_soc_dapm_value_mux:
1208 w->power_check = dapm_generic_check_power;
1209 dapm_new_mux(codec, w);
1210 break;
1211 case snd_soc_dapm_adc:
1212 w->power_check = dapm_adc_check_power;
1213 break;
1214 case snd_soc_dapm_dac:
1215 w->power_check = dapm_dac_check_power;
1216 break;
1217 case snd_soc_dapm_pga:
1218 w->power_check = dapm_generic_check_power;
1219 dapm_new_pga(codec, w);
1220 break;
1221 case snd_soc_dapm_input:
1222 case snd_soc_dapm_output:
1223 case snd_soc_dapm_micbias:
1224 case snd_soc_dapm_spk:
1225 case snd_soc_dapm_hp:
1226 case snd_soc_dapm_mic:
1227 case snd_soc_dapm_line:
1228 w->power_check = dapm_generic_check_power;
1229 break;
1230 case snd_soc_dapm_supply:
1231 w->power_check = dapm_supply_check_power;
1232 case snd_soc_dapm_vmid:
1233 case snd_soc_dapm_pre:
1234 case snd_soc_dapm_post:
1235 break;
1237 w->new = 1;
1240 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1241 return 0;
1243 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1246 * snd_soc_dapm_get_volsw - dapm mixer get callback
1247 * @kcontrol: mixer control
1248 * @ucontrol: control element information
1250 * Callback to get the value of a dapm mixer control.
1252 * Returns 0 for success.
1254 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1255 struct snd_ctl_elem_value *ucontrol)
1257 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1258 struct soc_mixer_control *mc =
1259 (struct soc_mixer_control *)kcontrol->private_value;
1260 unsigned int reg = mc->reg;
1261 unsigned int shift = mc->shift;
1262 unsigned int rshift = mc->rshift;
1263 int max = mc->max;
1264 unsigned int invert = mc->invert;
1265 unsigned int mask = (1 << fls(max)) - 1;
1267 /* return the saved value if we are powered down */
1268 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1269 ucontrol->value.integer.value[0] = widget->saved_value;
1270 return 0;
1273 ucontrol->value.integer.value[0] =
1274 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1275 if (shift != rshift)
1276 ucontrol->value.integer.value[1] =
1277 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1278 if (invert) {
1279 ucontrol->value.integer.value[0] =
1280 max - ucontrol->value.integer.value[0];
1281 if (shift != rshift)
1282 ucontrol->value.integer.value[1] =
1283 max - ucontrol->value.integer.value[1];
1286 return 0;
1288 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1291 * snd_soc_dapm_put_volsw - dapm mixer set callback
1292 * @kcontrol: mixer control
1293 * @ucontrol: control element information
1295 * Callback to set the value of a dapm mixer control.
1297 * Returns 0 for success.
1299 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1300 struct snd_ctl_elem_value *ucontrol)
1302 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1303 struct soc_mixer_control *mc =
1304 (struct soc_mixer_control *)kcontrol->private_value;
1305 unsigned int reg = mc->reg;
1306 unsigned int shift = mc->shift;
1307 unsigned int rshift = mc->rshift;
1308 int max = mc->max;
1309 unsigned int mask = (1 << fls(max)) - 1;
1310 unsigned int invert = mc->invert;
1311 unsigned short val, val2, val_mask;
1312 int ret;
1314 val = (ucontrol->value.integer.value[0] & mask);
1316 if (invert)
1317 val = max - val;
1318 val_mask = mask << shift;
1319 val = val << shift;
1320 if (shift != rshift) {
1321 val2 = (ucontrol->value.integer.value[1] & mask);
1322 if (invert)
1323 val2 = max - val2;
1324 val_mask |= mask << rshift;
1325 val |= val2 << rshift;
1328 mutex_lock(&widget->codec->mutex);
1329 widget->value = val;
1331 /* save volume value if the widget is powered down */
1332 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1333 widget->saved_value = val;
1334 mutex_unlock(&widget->codec->mutex);
1335 return 1;
1338 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1339 if (widget->event) {
1340 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1341 ret = widget->event(widget, kcontrol,
1342 SND_SOC_DAPM_PRE_REG);
1343 if (ret < 0) {
1344 ret = 1;
1345 goto out;
1348 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1349 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1350 ret = widget->event(widget, kcontrol,
1351 SND_SOC_DAPM_POST_REG);
1352 } else
1353 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1355 out:
1356 mutex_unlock(&widget->codec->mutex);
1357 return ret;
1359 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1362 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1363 * @kcontrol: mixer control
1364 * @ucontrol: control element information
1366 * Callback to get the value of a dapm enumerated double mixer control.
1368 * Returns 0 for success.
1370 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1371 struct snd_ctl_elem_value *ucontrol)
1373 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1374 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1375 unsigned short val, bitmask;
1377 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1379 val = snd_soc_read(widget->codec, e->reg);
1380 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1381 if (e->shift_l != e->shift_r)
1382 ucontrol->value.enumerated.item[1] =
1383 (val >> e->shift_r) & (bitmask - 1);
1385 return 0;
1387 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1390 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1391 * @kcontrol: mixer control
1392 * @ucontrol: control element information
1394 * Callback to set the value of a dapm enumerated double mixer control.
1396 * Returns 0 for success.
1398 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1399 struct snd_ctl_elem_value *ucontrol)
1401 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1402 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1403 unsigned short val, mux;
1404 unsigned short mask, bitmask;
1405 int ret = 0;
1407 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1409 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1410 return -EINVAL;
1411 mux = ucontrol->value.enumerated.item[0];
1412 val = mux << e->shift_l;
1413 mask = (bitmask - 1) << e->shift_l;
1414 if (e->shift_l != e->shift_r) {
1415 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1416 return -EINVAL;
1417 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1418 mask |= (bitmask - 1) << e->shift_r;
1421 mutex_lock(&widget->codec->mutex);
1422 widget->value = val;
1423 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
1424 if (widget->event) {
1425 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1426 ret = widget->event(widget,
1427 kcontrol, SND_SOC_DAPM_PRE_REG);
1428 if (ret < 0)
1429 goto out;
1431 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1432 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1433 ret = widget->event(widget,
1434 kcontrol, SND_SOC_DAPM_POST_REG);
1435 } else
1436 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1438 out:
1439 mutex_unlock(&widget->codec->mutex);
1440 return ret;
1442 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1445 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1446 * callback
1447 * @kcontrol: mixer control
1448 * @ucontrol: control element information
1450 * Callback to get the value of a dapm semi enumerated double mixer control.
1452 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1453 * used for handling bitfield coded enumeration for example.
1455 * Returns 0 for success.
1457 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1458 struct snd_ctl_elem_value *ucontrol)
1460 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1461 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1462 unsigned short reg_val, val, mux;
1464 reg_val = snd_soc_read(widget->codec, e->reg);
1465 val = (reg_val >> e->shift_l) & e->mask;
1466 for (mux = 0; mux < e->max; mux++) {
1467 if (val == e->values[mux])
1468 break;
1470 ucontrol->value.enumerated.item[0] = mux;
1471 if (e->shift_l != e->shift_r) {
1472 val = (reg_val >> e->shift_r) & e->mask;
1473 for (mux = 0; mux < e->max; mux++) {
1474 if (val == e->values[mux])
1475 break;
1477 ucontrol->value.enumerated.item[1] = mux;
1480 return 0;
1482 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1485 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1486 * callback
1487 * @kcontrol: mixer control
1488 * @ucontrol: control element information
1490 * Callback to set the value of a dapm semi enumerated double mixer control.
1492 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1493 * used for handling bitfield coded enumeration for example.
1495 * Returns 0 for success.
1497 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1498 struct snd_ctl_elem_value *ucontrol)
1500 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1501 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1502 unsigned short val, mux;
1503 unsigned short mask;
1504 int ret = 0;
1506 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1507 return -EINVAL;
1508 mux = ucontrol->value.enumerated.item[0];
1509 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1510 mask = e->mask << e->shift_l;
1511 if (e->shift_l != e->shift_r) {
1512 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1513 return -EINVAL;
1514 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1515 mask |= e->mask << e->shift_r;
1518 mutex_lock(&widget->codec->mutex);
1519 widget->value = val;
1520 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
1521 if (widget->event) {
1522 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1523 ret = widget->event(widget,
1524 kcontrol, SND_SOC_DAPM_PRE_REG);
1525 if (ret < 0)
1526 goto out;
1528 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1529 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1530 ret = widget->event(widget,
1531 kcontrol, SND_SOC_DAPM_POST_REG);
1532 } else
1533 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1535 out:
1536 mutex_unlock(&widget->codec->mutex);
1537 return ret;
1539 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1542 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1544 * @kcontrol: mixer control
1545 * @uinfo: control element information
1547 * Callback to provide information about a pin switch control.
1549 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1550 struct snd_ctl_elem_info *uinfo)
1552 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1553 uinfo->count = 1;
1554 uinfo->value.integer.min = 0;
1555 uinfo->value.integer.max = 1;
1557 return 0;
1559 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1562 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1564 * @kcontrol: mixer control
1565 * @ucontrol: Value
1567 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1568 struct snd_ctl_elem_value *ucontrol)
1570 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1571 const char *pin = (const char *)kcontrol->private_value;
1573 mutex_lock(&codec->mutex);
1575 ucontrol->value.integer.value[0] =
1576 snd_soc_dapm_get_pin_status(codec, pin);
1578 mutex_unlock(&codec->mutex);
1580 return 0;
1582 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1585 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1587 * @kcontrol: mixer control
1588 * @ucontrol: Value
1590 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1591 struct snd_ctl_elem_value *ucontrol)
1593 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1594 const char *pin = (const char *)kcontrol->private_value;
1596 mutex_lock(&codec->mutex);
1598 if (ucontrol->value.integer.value[0])
1599 snd_soc_dapm_enable_pin(codec, pin);
1600 else
1601 snd_soc_dapm_disable_pin(codec, pin);
1603 snd_soc_dapm_sync(codec);
1605 mutex_unlock(&codec->mutex);
1607 return 0;
1609 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1612 * snd_soc_dapm_new_control - create new dapm control
1613 * @codec: audio codec
1614 * @widget: widget template
1616 * Creates a new dapm control based upon the template.
1618 * Returns 0 for success else error.
1620 int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1621 const struct snd_soc_dapm_widget *widget)
1623 struct snd_soc_dapm_widget *w;
1625 if ((w = dapm_cnew_widget(widget)) == NULL)
1626 return -ENOMEM;
1628 w->codec = codec;
1629 INIT_LIST_HEAD(&w->sources);
1630 INIT_LIST_HEAD(&w->sinks);
1631 INIT_LIST_HEAD(&w->list);
1632 list_add(&w->list, &codec->dapm_widgets);
1634 /* machine layer set ups unconnected pins and insertions */
1635 w->connected = 1;
1636 return 0;
1638 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1641 * snd_soc_dapm_new_controls - create new dapm controls
1642 * @codec: audio codec
1643 * @widget: widget array
1644 * @num: number of widgets
1646 * Creates new DAPM controls based upon the templates.
1648 * Returns 0 for success else error.
1650 int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1651 const struct snd_soc_dapm_widget *widget,
1652 int num)
1654 int i, ret;
1656 for (i = 0; i < num; i++) {
1657 ret = snd_soc_dapm_new_control(codec, widget);
1658 if (ret < 0) {
1659 printk(KERN_ERR
1660 "ASoC: Failed to create DAPM control %s: %d\n",
1661 widget->name, ret);
1662 return ret;
1664 widget++;
1666 return 0;
1668 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1672 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1673 * @codec: audio codec
1674 * @stream: stream name
1675 * @event: stream event
1677 * Sends a stream event to the dapm core. The core then makes any
1678 * necessary widget power changes.
1680 * Returns 0 for success else error.
1682 int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1683 char *stream, int event)
1685 struct snd_soc_dapm_widget *w;
1687 if (stream == NULL)
1688 return 0;
1690 mutex_lock(&codec->mutex);
1691 list_for_each_entry(w, &codec->dapm_widgets, list)
1693 if (!w->sname)
1694 continue;
1695 pr_debug("widget %s\n %s stream %s event %d\n",
1696 w->name, w->sname, stream, event);
1697 if (strstr(w->sname, stream)) {
1698 switch(event) {
1699 case SND_SOC_DAPM_STREAM_START:
1700 w->active = 1;
1701 break;
1702 case SND_SOC_DAPM_STREAM_STOP:
1703 w->active = 0;
1704 break;
1705 case SND_SOC_DAPM_STREAM_SUSPEND:
1706 if (w->active)
1707 w->suspend = 1;
1708 w->active = 0;
1709 break;
1710 case SND_SOC_DAPM_STREAM_RESUME:
1711 if (w->suspend) {
1712 w->active = 1;
1713 w->suspend = 0;
1715 break;
1716 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1717 break;
1718 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1719 break;
1723 mutex_unlock(&codec->mutex);
1725 dapm_power_widgets(codec, event);
1726 dump_dapm(codec, __func__);
1727 return 0;
1729 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1732 * snd_soc_dapm_set_bias_level - set the bias level for the system
1733 * @socdev: audio device
1734 * @level: level to configure
1736 * Configure the bias (power) levels for the SoC audio device.
1738 * Returns 0 for success else error.
1740 int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
1741 enum snd_soc_bias_level level)
1743 struct snd_soc_card *card = socdev->card;
1744 struct snd_soc_codec *codec = socdev->card->codec;
1745 int ret = 0;
1747 if (card->set_bias_level)
1748 ret = card->set_bias_level(card, level);
1749 if (ret == 0 && codec->set_bias_level)
1750 ret = codec->set_bias_level(codec, level);
1752 return ret;
1756 * snd_soc_dapm_enable_pin - enable pin.
1757 * @codec: SoC codec
1758 * @pin: pin name
1760 * Enables input/output pin and it's parents or children widgets iff there is
1761 * a valid audio route and active audio stream.
1762 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1763 * do any widget power switching.
1765 int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
1767 return snd_soc_dapm_set_pin(codec, pin, 1);
1769 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
1772 * snd_soc_dapm_disable_pin - disable pin.
1773 * @codec: SoC codec
1774 * @pin: pin name
1776 * Disables input/output pin and it's parents or children widgets.
1777 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1778 * do any widget power switching.
1780 int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
1782 return snd_soc_dapm_set_pin(codec, pin, 0);
1784 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
1787 * snd_soc_dapm_nc_pin - permanently disable pin.
1788 * @codec: SoC codec
1789 * @pin: pin name
1791 * Marks the specified pin as being not connected, disabling it along
1792 * any parent or child widgets. At present this is identical to
1793 * snd_soc_dapm_disable_pin() but in future it will be extended to do
1794 * additional things such as disabling controls which only affect
1795 * paths through the pin.
1797 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1798 * do any widget power switching.
1800 int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
1802 return snd_soc_dapm_set_pin(codec, pin, 0);
1804 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
1807 * snd_soc_dapm_get_pin_status - get audio pin status
1808 * @codec: audio codec
1809 * @pin: audio signal pin endpoint (or start point)
1811 * Get audio pin status - connected or disconnected.
1813 * Returns 1 for connected otherwise 0.
1815 int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
1817 struct snd_soc_dapm_widget *w;
1819 list_for_each_entry(w, &codec->dapm_widgets, list) {
1820 if (!strcmp(w->name, pin))
1821 return w->connected;
1824 return 0;
1826 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
1829 * snd_soc_dapm_free - free dapm resources
1830 * @socdev: SoC device
1832 * Free all dapm widgets and resources.
1834 void snd_soc_dapm_free(struct snd_soc_device *socdev)
1836 struct snd_soc_codec *codec = socdev->card->codec;
1838 snd_soc_dapm_sys_remove(socdev->dev);
1839 dapm_free_widgets(codec);
1841 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1843 /* Module information */
1844 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
1845 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1846 MODULE_LICENSE("GPL");