controlmsg timerfunc delay
[cor_2_6_31.git] / sound / soc / soc-dapm.c
blob21c69074aa17a1819f7bb4b95255cafa7c57a433
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/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 void pop_wait(u32 pop_time)
72 if (pop_time)
73 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
76 static void pop_dbg(u32 pop_time, const char *fmt, ...)
78 va_list args;
80 va_start(args, fmt);
82 if (pop_time) {
83 vprintk(fmt, args);
84 pop_wait(pop_time);
87 va_end(args);
90 /* create a new dapm widget */
91 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
92 const struct snd_soc_dapm_widget *_widget)
94 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
97 /**
98 * snd_soc_dapm_set_bias_level - set the bias level for the system
99 * @socdev: audio device
100 * @level: level to configure
102 * Configure the bias (power) levels for the SoC audio device.
104 * Returns 0 for success else error.
106 static int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
107 enum snd_soc_bias_level level)
109 struct snd_soc_card *card = socdev->card;
110 struct snd_soc_codec *codec = socdev->card->codec;
111 int ret = 0;
113 switch (level) {
114 case SND_SOC_BIAS_ON:
115 dev_dbg(socdev->dev, "Setting full bias\n");
116 break;
117 case SND_SOC_BIAS_PREPARE:
118 dev_dbg(socdev->dev, "Setting bias prepare\n");
119 break;
120 case SND_SOC_BIAS_STANDBY:
121 dev_dbg(socdev->dev, "Setting standby bias\n");
122 break;
123 case SND_SOC_BIAS_OFF:
124 dev_dbg(socdev->dev, "Setting bias off\n");
125 break;
126 default:
127 dev_err(socdev->dev, "Setting invalid bias %d\n", level);
128 return -EINVAL;
131 if (card->set_bias_level)
132 ret = card->set_bias_level(card, level);
133 if (ret == 0 && codec->set_bias_level)
134 ret = codec->set_bias_level(codec, level);
136 return ret;
139 /* set up initial codec paths */
140 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
141 struct snd_soc_dapm_path *p, int i)
143 switch (w->id) {
144 case snd_soc_dapm_switch:
145 case snd_soc_dapm_mixer:
146 case snd_soc_dapm_mixer_named_ctl: {
147 int val;
148 struct soc_mixer_control *mc = (struct soc_mixer_control *)
149 w->kcontrols[i].private_value;
150 unsigned int reg = mc->reg;
151 unsigned int shift = mc->shift;
152 int max = mc->max;
153 unsigned int mask = (1 << fls(max)) - 1;
154 unsigned int invert = mc->invert;
156 val = snd_soc_read(w->codec, reg);
157 val = (val >> shift) & mask;
159 if ((invert && !val) || (!invert && val))
160 p->connect = 1;
161 else
162 p->connect = 0;
164 break;
165 case snd_soc_dapm_mux: {
166 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
167 int val, item, bitmask;
169 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
171 val = snd_soc_read(w->codec, e->reg);
172 item = (val >> e->shift_l) & (bitmask - 1);
174 p->connect = 0;
175 for (i = 0; i < e->max; i++) {
176 if (!(strcmp(p->name, e->texts[i])) && item == i)
177 p->connect = 1;
180 break;
181 case snd_soc_dapm_value_mux: {
182 struct soc_enum *e = (struct soc_enum *)
183 w->kcontrols[i].private_value;
184 int val, item;
186 val = snd_soc_read(w->codec, e->reg);
187 val = (val >> e->shift_l) & e->mask;
188 for (item = 0; item < e->max; item++) {
189 if (val == e->values[item])
190 break;
193 p->connect = 0;
194 for (i = 0; i < e->max; i++) {
195 if (!(strcmp(p->name, e->texts[i])) && item == i)
196 p->connect = 1;
199 break;
200 /* does not effect routing - always connected */
201 case snd_soc_dapm_pga:
202 case snd_soc_dapm_output:
203 case snd_soc_dapm_adc:
204 case snd_soc_dapm_input:
205 case snd_soc_dapm_dac:
206 case snd_soc_dapm_micbias:
207 case snd_soc_dapm_vmid:
208 case snd_soc_dapm_supply:
209 p->connect = 1;
210 break;
211 /* does effect routing - dynamically connected */
212 case snd_soc_dapm_hp:
213 case snd_soc_dapm_mic:
214 case snd_soc_dapm_spk:
215 case snd_soc_dapm_line:
216 case snd_soc_dapm_pre:
217 case snd_soc_dapm_post:
218 p->connect = 0;
219 break;
223 /* connect mux widget to its interconnecting audio paths */
224 static int dapm_connect_mux(struct snd_soc_codec *codec,
225 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
226 struct snd_soc_dapm_path *path, const char *control_name,
227 const struct snd_kcontrol_new *kcontrol)
229 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
230 int i;
232 for (i = 0; i < e->max; i++) {
233 if (!(strcmp(control_name, e->texts[i]))) {
234 list_add(&path->list, &codec->dapm_paths);
235 list_add(&path->list_sink, &dest->sources);
236 list_add(&path->list_source, &src->sinks);
237 path->name = (char*)e->texts[i];
238 dapm_set_path_status(dest, path, 0);
239 return 0;
243 return -ENODEV;
246 /* connect mixer widget to its interconnecting audio paths */
247 static int dapm_connect_mixer(struct snd_soc_codec *codec,
248 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
249 struct snd_soc_dapm_path *path, const char *control_name)
251 int i;
253 /* search for mixer kcontrol */
254 for (i = 0; i < dest->num_kcontrols; i++) {
255 if (!strcmp(control_name, dest->kcontrols[i].name)) {
256 list_add(&path->list, &codec->dapm_paths);
257 list_add(&path->list_sink, &dest->sources);
258 list_add(&path->list_source, &src->sinks);
259 path->name = dest->kcontrols[i].name;
260 dapm_set_path_status(dest, path, i);
261 return 0;
264 return -ENODEV;
267 /* update dapm codec register bits */
268 static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
270 int change, power;
271 unsigned short old, new;
272 struct snd_soc_codec *codec = widget->codec;
274 /* check for valid widgets */
275 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
276 widget->id == snd_soc_dapm_output ||
277 widget->id == snd_soc_dapm_hp ||
278 widget->id == snd_soc_dapm_mic ||
279 widget->id == snd_soc_dapm_line ||
280 widget->id == snd_soc_dapm_spk)
281 return 0;
283 power = widget->power;
284 if (widget->invert)
285 power = (power ? 0:1);
287 old = snd_soc_read(codec, widget->reg);
288 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
290 change = old != new;
291 if (change) {
292 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
293 widget->name, widget->power ? "on" : "off",
294 codec->pop_time);
295 snd_soc_write(codec, widget->reg, new);
296 pop_wait(codec->pop_time);
298 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
299 old, new, change);
300 return change;
303 /* ramps the volume up or down to minimise pops before or after a
304 * DAPM power event */
305 static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
307 const struct snd_kcontrol_new *k = widget->kcontrols;
309 if (widget->muted && !power)
310 return 0;
311 if (!widget->muted && power)
312 return 0;
314 if (widget->num_kcontrols && k) {
315 struct soc_mixer_control *mc =
316 (struct soc_mixer_control *)k->private_value;
317 unsigned int reg = mc->reg;
318 unsigned int shift = mc->shift;
319 int max = mc->max;
320 unsigned int mask = (1 << fls(max)) - 1;
321 unsigned int invert = mc->invert;
323 if (power) {
324 int i;
325 /* power up has happended, increase volume to last level */
326 if (invert) {
327 for (i = max; i > widget->saved_value; i--)
328 snd_soc_update_bits(widget->codec, reg, mask, i);
329 } else {
330 for (i = 0; i < widget->saved_value; i++)
331 snd_soc_update_bits(widget->codec, reg, mask, i);
333 widget->muted = 0;
334 } else {
335 /* power down is about to occur, decrease volume to mute */
336 int val = snd_soc_read(widget->codec, reg);
337 int i = widget->saved_value = (val >> shift) & mask;
338 if (invert) {
339 for (; i < mask; i++)
340 snd_soc_update_bits(widget->codec, reg, mask, i);
341 } else {
342 for (; i > 0; i--)
343 snd_soc_update_bits(widget->codec, reg, mask, i);
345 widget->muted = 1;
348 return 0;
351 /* create new dapm mixer control */
352 static int dapm_new_mixer(struct snd_soc_codec *codec,
353 struct snd_soc_dapm_widget *w)
355 int i, ret = 0;
356 size_t name_len;
357 struct snd_soc_dapm_path *path;
359 /* add kcontrol */
360 for (i = 0; i < w->num_kcontrols; i++) {
362 /* match name */
363 list_for_each_entry(path, &w->sources, list_sink) {
365 /* mixer/mux paths name must match control name */
366 if (path->name != (char*)w->kcontrols[i].name)
367 continue;
369 /* add dapm control with long name.
370 * for dapm_mixer this is the concatenation of the
371 * mixer and kcontrol name.
372 * for dapm_mixer_named_ctl this is simply the
373 * kcontrol name.
375 name_len = strlen(w->kcontrols[i].name) + 1;
376 if (w->id != snd_soc_dapm_mixer_named_ctl)
377 name_len += 1 + strlen(w->name);
379 path->long_name = kmalloc(name_len, GFP_KERNEL);
381 if (path->long_name == NULL)
382 return -ENOMEM;
384 switch (w->id) {
385 default:
386 snprintf(path->long_name, name_len, "%s %s",
387 w->name, w->kcontrols[i].name);
388 break;
389 case snd_soc_dapm_mixer_named_ctl:
390 snprintf(path->long_name, name_len, "%s",
391 w->kcontrols[i].name);
392 break;
395 path->long_name[name_len - 1] = '\0';
397 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
398 path->long_name);
399 ret = snd_ctl_add(codec->card, path->kcontrol);
400 if (ret < 0) {
401 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
402 path->long_name,
403 ret);
404 kfree(path->long_name);
405 path->long_name = NULL;
406 return ret;
410 return ret;
413 /* create new dapm mux control */
414 static int dapm_new_mux(struct snd_soc_codec *codec,
415 struct snd_soc_dapm_widget *w)
417 struct snd_soc_dapm_path *path = NULL;
418 struct snd_kcontrol *kcontrol;
419 int ret = 0;
421 if (!w->num_kcontrols) {
422 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
423 return -EINVAL;
426 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
427 ret = snd_ctl_add(codec->card, kcontrol);
428 if (ret < 0)
429 goto err;
431 list_for_each_entry(path, &w->sources, list_sink)
432 path->kcontrol = kcontrol;
434 return ret;
436 err:
437 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
438 return ret;
441 /* create new dapm volume control */
442 static int dapm_new_pga(struct snd_soc_codec *codec,
443 struct snd_soc_dapm_widget *w)
445 struct snd_kcontrol *kcontrol;
446 int ret = 0;
448 if (!w->num_kcontrols)
449 return -EINVAL;
451 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
452 ret = snd_ctl_add(codec->card, kcontrol);
453 if (ret < 0) {
454 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
455 return ret;
458 return ret;
461 /* reset 'walked' bit for each dapm path */
462 static inline void dapm_clear_walk(struct snd_soc_codec *codec)
464 struct snd_soc_dapm_path *p;
466 list_for_each_entry(p, &codec->dapm_paths, list)
467 p->walked = 0;
471 * Recursively check for a completed path to an active or physically connected
472 * output widget. Returns number of complete paths.
474 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
476 struct snd_soc_dapm_path *path;
477 int con = 0;
479 if (widget->id == snd_soc_dapm_supply)
480 return 0;
482 if (widget->id == snd_soc_dapm_adc && widget->active)
483 return 1;
485 if (widget->connected) {
486 /* connected pin ? */
487 if (widget->id == snd_soc_dapm_output && !widget->ext)
488 return 1;
490 /* connected jack or spk ? */
491 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
492 widget->id == snd_soc_dapm_line)
493 return 1;
496 list_for_each_entry(path, &widget->sinks, list_source) {
497 if (path->walked)
498 continue;
500 if (path->sink && path->connect) {
501 path->walked = 1;
502 con += is_connected_output_ep(path->sink);
506 return con;
510 * Recursively check for a completed path to an active or physically connected
511 * input widget. Returns number of complete paths.
513 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
515 struct snd_soc_dapm_path *path;
516 int con = 0;
518 if (widget->id == snd_soc_dapm_supply)
519 return 0;
521 /* active stream ? */
522 if (widget->id == snd_soc_dapm_dac && widget->active)
523 return 1;
525 if (widget->connected) {
526 /* connected pin ? */
527 if (widget->id == snd_soc_dapm_input && !widget->ext)
528 return 1;
530 /* connected VMID/Bias for lower pops */
531 if (widget->id == snd_soc_dapm_vmid)
532 return 1;
534 /* connected jack ? */
535 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
536 return 1;
539 list_for_each_entry(path, &widget->sources, list_sink) {
540 if (path->walked)
541 continue;
543 if (path->source && path->connect) {
544 path->walked = 1;
545 con += is_connected_input_ep(path->source);
549 return con;
553 * Handler for generic register modifier widget.
555 int dapm_reg_event(struct snd_soc_dapm_widget *w,
556 struct snd_kcontrol *kcontrol, int event)
558 unsigned int val;
560 if (SND_SOC_DAPM_EVENT_ON(event))
561 val = w->on_val;
562 else
563 val = w->off_val;
565 snd_soc_update_bits(w->codec, -(w->reg + 1),
566 w->mask << w->shift, val << w->shift);
568 return 0;
570 EXPORT_SYMBOL_GPL(dapm_reg_event);
572 /* Standard power change method, used to apply power changes to most
573 * widgets.
575 static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
577 int ret;
579 /* call any power change event handlers */
580 if (w->event)
581 pr_debug("power %s event for %s flags %x\n",
582 w->power ? "on" : "off",
583 w->name, w->event_flags);
585 /* power up pre event */
586 if (w->power && w->event &&
587 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
588 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
589 if (ret < 0)
590 return ret;
593 /* power down pre event */
594 if (!w->power && w->event &&
595 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
596 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
597 if (ret < 0)
598 return ret;
601 /* Lower PGA volume to reduce pops */
602 if (w->id == snd_soc_dapm_pga && !w->power)
603 dapm_set_pga(w, w->power);
605 dapm_update_bits(w);
607 /* Raise PGA volume to reduce pops */
608 if (w->id == snd_soc_dapm_pga && w->power)
609 dapm_set_pga(w, w->power);
611 /* power up post event */
612 if (w->power && w->event &&
613 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
614 ret = w->event(w,
615 NULL, SND_SOC_DAPM_POST_PMU);
616 if (ret < 0)
617 return ret;
620 /* power down post event */
621 if (!w->power && w->event &&
622 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
623 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
624 if (ret < 0)
625 return ret;
628 return 0;
631 /* Generic check to see if a widget should be powered.
633 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
635 int in, out;
637 in = is_connected_input_ep(w);
638 dapm_clear_walk(w->codec);
639 out = is_connected_output_ep(w);
640 dapm_clear_walk(w->codec);
641 return out != 0 && in != 0;
644 /* Check to see if an ADC has power */
645 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
647 int in;
649 if (w->active) {
650 in = is_connected_input_ep(w);
651 dapm_clear_walk(w->codec);
652 return in != 0;
653 } else {
654 return dapm_generic_check_power(w);
658 /* Check to see if a DAC has power */
659 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
661 int out;
663 if (w->active) {
664 out = is_connected_output_ep(w);
665 dapm_clear_walk(w->codec);
666 return out != 0;
667 } else {
668 return dapm_generic_check_power(w);
672 /* Check to see if a power supply is needed */
673 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
675 struct snd_soc_dapm_path *path;
676 int power = 0;
678 /* Check if one of our outputs is connected */
679 list_for_each_entry(path, &w->sinks, list_source) {
680 if (path->sink && path->sink->power_check &&
681 path->sink->power_check(path->sink)) {
682 power = 1;
683 break;
687 dapm_clear_walk(w->codec);
689 return power;
693 * Scan a single DAPM widget for a complete audio path and update the
694 * power status appropriately.
696 static int dapm_power_widget(struct snd_soc_codec *codec, int event,
697 struct snd_soc_dapm_widget *w)
699 int ret;
701 switch (w->id) {
702 case snd_soc_dapm_pre:
703 if (!w->event)
704 return 0;
706 if (event == SND_SOC_DAPM_STREAM_START) {
707 ret = w->event(w,
708 NULL, SND_SOC_DAPM_PRE_PMU);
709 if (ret < 0)
710 return ret;
711 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
712 ret = w->event(w,
713 NULL, SND_SOC_DAPM_PRE_PMD);
714 if (ret < 0)
715 return ret;
717 return 0;
719 case snd_soc_dapm_post:
720 if (!w->event)
721 return 0;
723 if (event == SND_SOC_DAPM_STREAM_START) {
724 ret = w->event(w,
725 NULL, SND_SOC_DAPM_POST_PMU);
726 if (ret < 0)
727 return ret;
728 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
729 ret = w->event(w,
730 NULL, SND_SOC_DAPM_POST_PMD);
731 if (ret < 0)
732 return ret;
734 return 0;
736 default:
737 return dapm_generic_apply_power(w);
742 * Scan each dapm widget for complete audio path.
743 * A complete path is a route that has valid endpoints i.e.:-
745 * o DAC to output pin.
746 * o Input Pin to ADC.
747 * o Input pin to Output pin (bypass, sidetone)
748 * o DAC to ADC (loopback).
750 static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
752 struct snd_soc_device *socdev = codec->socdev;
753 struct snd_soc_dapm_widget *w;
754 int ret = 0;
755 int i, power;
756 int sys_power = 0;
758 INIT_LIST_HEAD(&codec->up_list);
759 INIT_LIST_HEAD(&codec->down_list);
761 /* Check which widgets we need to power and store them in
762 * lists indicating if they should be powered up or down.
764 list_for_each_entry(w, &codec->dapm_widgets, list) {
765 switch (w->id) {
766 case snd_soc_dapm_pre:
767 list_add_tail(&codec->down_list, &w->power_list);
768 break;
769 case snd_soc_dapm_post:
770 list_add_tail(&codec->up_list, &w->power_list);
771 break;
773 default:
774 if (!w->power_check)
775 continue;
777 power = w->power_check(w);
778 if (power)
779 sys_power = 1;
781 if (w->power == power)
782 continue;
784 if (power)
785 list_add_tail(&w->power_list, &codec->up_list);
786 else
787 list_add_tail(&w->power_list,
788 &codec->down_list);
790 w->power = power;
791 break;
795 /* If we're changing to all on or all off then prepare */
796 if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
797 (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
798 ret = snd_soc_dapm_set_bias_level(socdev,
799 SND_SOC_BIAS_PREPARE);
800 if (ret != 0)
801 pr_err("Failed to prepare bias: %d\n", ret);
804 /* Power down widgets first; try to avoid amplifying pops. */
805 for (i = 0; i < ARRAY_SIZE(dapm_down_seq); i++) {
806 list_for_each_entry(w, &codec->down_list, power_list) {
807 /* is widget in stream order */
808 if (w->id != dapm_down_seq[i])
809 continue;
811 ret = dapm_power_widget(codec, event, w);
812 if (ret != 0)
813 pr_err("Failed to power down %s: %d\n",
814 w->name, ret);
818 /* Now power up. */
819 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++) {
820 list_for_each_entry(w, &codec->up_list, power_list) {
821 /* is widget in stream order */
822 if (w->id != dapm_up_seq[i])
823 continue;
825 ret = dapm_power_widget(codec, event, w);
826 if (ret != 0)
827 pr_err("Failed to power up %s: %d\n",
828 w->name, ret);
832 /* If we just powered the last thing off drop to standby bias */
833 if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
834 ret = snd_soc_dapm_set_bias_level(socdev,
835 SND_SOC_BIAS_STANDBY);
836 if (ret != 0)
837 pr_err("Failed to apply standby bias: %d\n", ret);
840 /* If we just powered up then move to active bias */
841 if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
842 ret = snd_soc_dapm_set_bias_level(socdev,
843 SND_SOC_BIAS_ON);
844 if (ret != 0)
845 pr_err("Failed to apply active bias: %d\n", ret);
848 return 0;
851 #ifdef DEBUG
852 static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
854 struct snd_soc_dapm_widget *w;
855 struct snd_soc_dapm_path *p = NULL;
856 int in, out;
858 printk("DAPM %s %s\n", codec->name, action);
860 list_for_each_entry(w, &codec->dapm_widgets, list) {
862 /* only display widgets that effect routing */
863 switch (w->id) {
864 case snd_soc_dapm_pre:
865 case snd_soc_dapm_post:
866 case snd_soc_dapm_vmid:
867 continue;
868 case snd_soc_dapm_mux:
869 case snd_soc_dapm_value_mux:
870 case snd_soc_dapm_output:
871 case snd_soc_dapm_input:
872 case snd_soc_dapm_switch:
873 case snd_soc_dapm_hp:
874 case snd_soc_dapm_mic:
875 case snd_soc_dapm_spk:
876 case snd_soc_dapm_line:
877 case snd_soc_dapm_micbias:
878 case snd_soc_dapm_dac:
879 case snd_soc_dapm_adc:
880 case snd_soc_dapm_pga:
881 case snd_soc_dapm_mixer:
882 case snd_soc_dapm_mixer_named_ctl:
883 case snd_soc_dapm_supply:
884 if (w->name) {
885 in = is_connected_input_ep(w);
886 dapm_clear_walk(w->codec);
887 out = is_connected_output_ep(w);
888 dapm_clear_walk(w->codec);
889 printk("%s: %s in %d out %d\n", w->name,
890 w->power ? "On":"Off",in, out);
892 list_for_each_entry(p, &w->sources, list_sink) {
893 if (p->connect)
894 printk(" in %s %s\n", p->name ? p->name : "static",
895 p->source->name);
897 list_for_each_entry(p, &w->sinks, list_source) {
898 if (p->connect)
899 printk(" out %s %s\n", p->name ? p->name : "static",
900 p->sink->name);
903 break;
907 #endif
909 /* test and update the power status of a mux widget */
910 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
911 struct snd_kcontrol *kcontrol, int mask,
912 int mux, int val, struct soc_enum *e)
914 struct snd_soc_dapm_path *path;
915 int found = 0;
917 if (widget->id != snd_soc_dapm_mux &&
918 widget->id != snd_soc_dapm_value_mux)
919 return -ENODEV;
921 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
922 return 0;
924 /* find dapm widget path assoc with kcontrol */
925 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
926 if (path->kcontrol != kcontrol)
927 continue;
929 if (!path->name || !e->texts[mux])
930 continue;
932 found = 1;
933 /* we now need to match the string in the enum to the path */
934 if (!(strcmp(path->name, e->texts[mux])))
935 path->connect = 1; /* new connection */
936 else
937 path->connect = 0; /* old connection must be powered down */
940 if (found) {
941 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
942 dump_dapm(widget->codec, "mux power update");
945 return 0;
948 /* test and update the power status of a mixer or switch widget */
949 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
950 struct snd_kcontrol *kcontrol, int reg,
951 int val_mask, int val, int invert)
953 struct snd_soc_dapm_path *path;
954 int found = 0;
956 if (widget->id != snd_soc_dapm_mixer &&
957 widget->id != snd_soc_dapm_mixer_named_ctl &&
958 widget->id != snd_soc_dapm_switch)
959 return -ENODEV;
961 if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
962 return 0;
964 /* find dapm widget path assoc with kcontrol */
965 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
966 if (path->kcontrol != kcontrol)
967 continue;
969 /* found, now check type */
970 found = 1;
971 if (val)
972 /* new connection */
973 path->connect = invert ? 0:1;
974 else
975 /* old connection must be powered down */
976 path->connect = invert ? 1:0;
977 break;
980 if (found) {
981 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
982 dump_dapm(widget->codec, "mixer power update");
985 return 0;
988 /* show dapm widget status in sys fs */
989 static ssize_t dapm_widget_show(struct device *dev,
990 struct device_attribute *attr, char *buf)
992 struct snd_soc_device *devdata = dev_get_drvdata(dev);
993 struct snd_soc_codec *codec = devdata->card->codec;
994 struct snd_soc_dapm_widget *w;
995 int count = 0;
996 char *state = "not set";
998 list_for_each_entry(w, &codec->dapm_widgets, list) {
1000 /* only display widgets that burnm power */
1001 switch (w->id) {
1002 case snd_soc_dapm_hp:
1003 case snd_soc_dapm_mic:
1004 case snd_soc_dapm_spk:
1005 case snd_soc_dapm_line:
1006 case snd_soc_dapm_micbias:
1007 case snd_soc_dapm_dac:
1008 case snd_soc_dapm_adc:
1009 case snd_soc_dapm_pga:
1010 case snd_soc_dapm_mixer:
1011 case snd_soc_dapm_mixer_named_ctl:
1012 case snd_soc_dapm_supply:
1013 if (w->name)
1014 count += sprintf(buf + count, "%s: %s\n",
1015 w->name, w->power ? "On":"Off");
1016 break;
1017 default:
1018 break;
1022 switch (codec->bias_level) {
1023 case SND_SOC_BIAS_ON:
1024 state = "On";
1025 break;
1026 case SND_SOC_BIAS_PREPARE:
1027 state = "Prepare";
1028 break;
1029 case SND_SOC_BIAS_STANDBY:
1030 state = "Standby";
1031 break;
1032 case SND_SOC_BIAS_OFF:
1033 state = "Off";
1034 break;
1036 count += sprintf(buf + count, "PM State: %s\n", state);
1038 return count;
1041 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1043 int snd_soc_dapm_sys_add(struct device *dev)
1045 return device_create_file(dev, &dev_attr_dapm_widget);
1048 static void snd_soc_dapm_sys_remove(struct device *dev)
1050 device_remove_file(dev, &dev_attr_dapm_widget);
1053 /* free all dapm widgets and resources */
1054 static void dapm_free_widgets(struct snd_soc_codec *codec)
1056 struct snd_soc_dapm_widget *w, *next_w;
1057 struct snd_soc_dapm_path *p, *next_p;
1059 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
1060 list_del(&w->list);
1061 kfree(w);
1064 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
1065 list_del(&p->list);
1066 kfree(p->long_name);
1067 kfree(p);
1071 static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
1072 const char *pin, int status)
1074 struct snd_soc_dapm_widget *w;
1076 list_for_each_entry(w, &codec->dapm_widgets, list) {
1077 if (!strcmp(w->name, pin)) {
1078 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
1079 w->connected = status;
1080 return 0;
1084 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
1085 return -EINVAL;
1089 * snd_soc_dapm_sync - scan and power dapm paths
1090 * @codec: audio codec
1092 * Walks all dapm audio paths and powers widgets according to their
1093 * stream or path usage.
1095 * Returns 0 for success.
1097 int snd_soc_dapm_sync(struct snd_soc_codec *codec)
1099 int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1100 dump_dapm(codec, "sync");
1101 return ret;
1103 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1105 static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
1106 const char *sink, const char *control, const char *source)
1108 struct snd_soc_dapm_path *path;
1109 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1110 int ret = 0;
1112 /* find src and dest widgets */
1113 list_for_each_entry(w, &codec->dapm_widgets, list) {
1115 if (!wsink && !(strcmp(w->name, sink))) {
1116 wsink = w;
1117 continue;
1119 if (!wsource && !(strcmp(w->name, source))) {
1120 wsource = w;
1124 if (wsource == NULL || wsink == NULL)
1125 return -ENODEV;
1127 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1128 if (!path)
1129 return -ENOMEM;
1131 path->source = wsource;
1132 path->sink = wsink;
1133 INIT_LIST_HEAD(&path->list);
1134 INIT_LIST_HEAD(&path->list_source);
1135 INIT_LIST_HEAD(&path->list_sink);
1137 /* check for external widgets */
1138 if (wsink->id == snd_soc_dapm_input) {
1139 if (wsource->id == snd_soc_dapm_micbias ||
1140 wsource->id == snd_soc_dapm_mic ||
1141 wsink->id == snd_soc_dapm_line ||
1142 wsink->id == snd_soc_dapm_output)
1143 wsink->ext = 1;
1145 if (wsource->id == snd_soc_dapm_output) {
1146 if (wsink->id == snd_soc_dapm_spk ||
1147 wsink->id == snd_soc_dapm_hp ||
1148 wsink->id == snd_soc_dapm_line ||
1149 wsink->id == snd_soc_dapm_input)
1150 wsource->ext = 1;
1153 /* connect static paths */
1154 if (control == NULL) {
1155 list_add(&path->list, &codec->dapm_paths);
1156 list_add(&path->list_sink, &wsink->sources);
1157 list_add(&path->list_source, &wsource->sinks);
1158 path->connect = 1;
1159 return 0;
1162 /* connect dynamic paths */
1163 switch(wsink->id) {
1164 case snd_soc_dapm_adc:
1165 case snd_soc_dapm_dac:
1166 case snd_soc_dapm_pga:
1167 case snd_soc_dapm_input:
1168 case snd_soc_dapm_output:
1169 case snd_soc_dapm_micbias:
1170 case snd_soc_dapm_vmid:
1171 case snd_soc_dapm_pre:
1172 case snd_soc_dapm_post:
1173 case snd_soc_dapm_supply:
1174 list_add(&path->list, &codec->dapm_paths);
1175 list_add(&path->list_sink, &wsink->sources);
1176 list_add(&path->list_source, &wsource->sinks);
1177 path->connect = 1;
1178 return 0;
1179 case snd_soc_dapm_mux:
1180 case snd_soc_dapm_value_mux:
1181 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1182 &wsink->kcontrols[0]);
1183 if (ret != 0)
1184 goto err;
1185 break;
1186 case snd_soc_dapm_switch:
1187 case snd_soc_dapm_mixer:
1188 case snd_soc_dapm_mixer_named_ctl:
1189 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1190 if (ret != 0)
1191 goto err;
1192 break;
1193 case snd_soc_dapm_hp:
1194 case snd_soc_dapm_mic:
1195 case snd_soc_dapm_line:
1196 case snd_soc_dapm_spk:
1197 list_add(&path->list, &codec->dapm_paths);
1198 list_add(&path->list_sink, &wsink->sources);
1199 list_add(&path->list_source, &wsource->sinks);
1200 path->connect = 0;
1201 return 0;
1203 return 0;
1205 err:
1206 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1207 control, sink);
1208 kfree(path);
1209 return ret;
1213 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1214 * @codec: codec
1215 * @route: audio routes
1216 * @num: number of routes
1218 * Connects 2 dapm widgets together via a named audio path. The sink is
1219 * the widget receiving the audio signal, whilst the source is the sender
1220 * of the audio signal.
1222 * Returns 0 for success else error. On error all resources can be freed
1223 * with a call to snd_soc_card_free().
1225 int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1226 const struct snd_soc_dapm_route *route, int num)
1228 int i, ret;
1230 for (i = 0; i < num; i++) {
1231 ret = snd_soc_dapm_add_route(codec, route->sink,
1232 route->control, route->source);
1233 if (ret < 0) {
1234 printk(KERN_ERR "Failed to add route %s->%s\n",
1235 route->source,
1236 route->sink);
1237 return ret;
1239 route++;
1242 return 0;
1244 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1247 * snd_soc_dapm_new_widgets - add new dapm widgets
1248 * @codec: audio codec
1250 * Checks the codec for any new dapm widgets and creates them if found.
1252 * Returns 0 for success.
1254 int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1256 struct snd_soc_dapm_widget *w;
1258 list_for_each_entry(w, &codec->dapm_widgets, list)
1260 if (w->new)
1261 continue;
1263 switch(w->id) {
1264 case snd_soc_dapm_switch:
1265 case snd_soc_dapm_mixer:
1266 case snd_soc_dapm_mixer_named_ctl:
1267 w->power_check = dapm_generic_check_power;
1268 dapm_new_mixer(codec, w);
1269 break;
1270 case snd_soc_dapm_mux:
1271 case snd_soc_dapm_value_mux:
1272 w->power_check = dapm_generic_check_power;
1273 dapm_new_mux(codec, w);
1274 break;
1275 case snd_soc_dapm_adc:
1276 w->power_check = dapm_adc_check_power;
1277 break;
1278 case snd_soc_dapm_dac:
1279 w->power_check = dapm_dac_check_power;
1280 break;
1281 case snd_soc_dapm_pga:
1282 w->power_check = dapm_generic_check_power;
1283 dapm_new_pga(codec, w);
1284 break;
1285 case snd_soc_dapm_input:
1286 case snd_soc_dapm_output:
1287 case snd_soc_dapm_micbias:
1288 case snd_soc_dapm_spk:
1289 case snd_soc_dapm_hp:
1290 case snd_soc_dapm_mic:
1291 case snd_soc_dapm_line:
1292 w->power_check = dapm_generic_check_power;
1293 break;
1294 case snd_soc_dapm_supply:
1295 w->power_check = dapm_supply_check_power;
1296 case snd_soc_dapm_vmid:
1297 case snd_soc_dapm_pre:
1298 case snd_soc_dapm_post:
1299 break;
1301 w->new = 1;
1304 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1305 return 0;
1307 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1310 * snd_soc_dapm_get_volsw - dapm mixer get callback
1311 * @kcontrol: mixer control
1312 * @ucontrol: control element information
1314 * Callback to get the value of a dapm mixer control.
1316 * Returns 0 for success.
1318 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1319 struct snd_ctl_elem_value *ucontrol)
1321 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1322 struct soc_mixer_control *mc =
1323 (struct soc_mixer_control *)kcontrol->private_value;
1324 unsigned int reg = mc->reg;
1325 unsigned int shift = mc->shift;
1326 unsigned int rshift = mc->rshift;
1327 int max = mc->max;
1328 unsigned int invert = mc->invert;
1329 unsigned int mask = (1 << fls(max)) - 1;
1331 /* return the saved value if we are powered down */
1332 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1333 ucontrol->value.integer.value[0] = widget->saved_value;
1334 return 0;
1337 ucontrol->value.integer.value[0] =
1338 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1339 if (shift != rshift)
1340 ucontrol->value.integer.value[1] =
1341 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1342 if (invert) {
1343 ucontrol->value.integer.value[0] =
1344 max - ucontrol->value.integer.value[0];
1345 if (shift != rshift)
1346 ucontrol->value.integer.value[1] =
1347 max - ucontrol->value.integer.value[1];
1350 return 0;
1352 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1355 * snd_soc_dapm_put_volsw - dapm mixer set callback
1356 * @kcontrol: mixer control
1357 * @ucontrol: control element information
1359 * Callback to set the value of a dapm mixer control.
1361 * Returns 0 for success.
1363 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1364 struct snd_ctl_elem_value *ucontrol)
1366 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1367 struct soc_mixer_control *mc =
1368 (struct soc_mixer_control *)kcontrol->private_value;
1369 unsigned int reg = mc->reg;
1370 unsigned int shift = mc->shift;
1371 unsigned int rshift = mc->rshift;
1372 int max = mc->max;
1373 unsigned int mask = (1 << fls(max)) - 1;
1374 unsigned int invert = mc->invert;
1375 unsigned short val, val2, val_mask;
1376 int ret;
1378 val = (ucontrol->value.integer.value[0] & mask);
1380 if (invert)
1381 val = max - val;
1382 val_mask = mask << shift;
1383 val = val << shift;
1384 if (shift != rshift) {
1385 val2 = (ucontrol->value.integer.value[1] & mask);
1386 if (invert)
1387 val2 = max - val2;
1388 val_mask |= mask << rshift;
1389 val |= val2 << rshift;
1392 mutex_lock(&widget->codec->mutex);
1393 widget->value = val;
1395 /* save volume value if the widget is powered down */
1396 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1397 widget->saved_value = val;
1398 mutex_unlock(&widget->codec->mutex);
1399 return 1;
1402 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1403 if (widget->event) {
1404 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1405 ret = widget->event(widget, kcontrol,
1406 SND_SOC_DAPM_PRE_REG);
1407 if (ret < 0) {
1408 ret = 1;
1409 goto out;
1412 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1413 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1414 ret = widget->event(widget, kcontrol,
1415 SND_SOC_DAPM_POST_REG);
1416 } else
1417 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1419 out:
1420 mutex_unlock(&widget->codec->mutex);
1421 return ret;
1423 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1426 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1427 * @kcontrol: mixer control
1428 * @ucontrol: control element information
1430 * Callback to get the value of a dapm enumerated double mixer control.
1432 * Returns 0 for success.
1434 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1435 struct snd_ctl_elem_value *ucontrol)
1437 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1438 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1439 unsigned short val, bitmask;
1441 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1443 val = snd_soc_read(widget->codec, e->reg);
1444 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1445 if (e->shift_l != e->shift_r)
1446 ucontrol->value.enumerated.item[1] =
1447 (val >> e->shift_r) & (bitmask - 1);
1449 return 0;
1451 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1454 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1455 * @kcontrol: mixer control
1456 * @ucontrol: control element information
1458 * Callback to set the value of a dapm enumerated double mixer control.
1460 * Returns 0 for success.
1462 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1463 struct snd_ctl_elem_value *ucontrol)
1465 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1466 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1467 unsigned short val, mux;
1468 unsigned short mask, bitmask;
1469 int ret = 0;
1471 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1473 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1474 return -EINVAL;
1475 mux = ucontrol->value.enumerated.item[0];
1476 val = mux << e->shift_l;
1477 mask = (bitmask - 1) << e->shift_l;
1478 if (e->shift_l != e->shift_r) {
1479 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1480 return -EINVAL;
1481 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1482 mask |= (bitmask - 1) << e->shift_r;
1485 mutex_lock(&widget->codec->mutex);
1486 widget->value = val;
1487 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
1488 if (widget->event) {
1489 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1490 ret = widget->event(widget,
1491 kcontrol, SND_SOC_DAPM_PRE_REG);
1492 if (ret < 0)
1493 goto out;
1495 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1496 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1497 ret = widget->event(widget,
1498 kcontrol, SND_SOC_DAPM_POST_REG);
1499 } else
1500 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1502 out:
1503 mutex_unlock(&widget->codec->mutex);
1504 return ret;
1506 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1509 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1510 * callback
1511 * @kcontrol: mixer control
1512 * @ucontrol: control element information
1514 * Callback to get the value of a dapm semi enumerated double mixer control.
1516 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1517 * used for handling bitfield coded enumeration for example.
1519 * Returns 0 for success.
1521 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1522 struct snd_ctl_elem_value *ucontrol)
1524 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1525 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1526 unsigned short reg_val, val, mux;
1528 reg_val = snd_soc_read(widget->codec, e->reg);
1529 val = (reg_val >> e->shift_l) & e->mask;
1530 for (mux = 0; mux < e->max; mux++) {
1531 if (val == e->values[mux])
1532 break;
1534 ucontrol->value.enumerated.item[0] = mux;
1535 if (e->shift_l != e->shift_r) {
1536 val = (reg_val >> e->shift_r) & e->mask;
1537 for (mux = 0; mux < e->max; mux++) {
1538 if (val == e->values[mux])
1539 break;
1541 ucontrol->value.enumerated.item[1] = mux;
1544 return 0;
1546 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1549 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1550 * callback
1551 * @kcontrol: mixer control
1552 * @ucontrol: control element information
1554 * Callback to set the value of a dapm semi enumerated double mixer control.
1556 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1557 * used for handling bitfield coded enumeration for example.
1559 * Returns 0 for success.
1561 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1562 struct snd_ctl_elem_value *ucontrol)
1564 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1565 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1566 unsigned short val, mux;
1567 unsigned short mask;
1568 int ret = 0;
1570 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1571 return -EINVAL;
1572 mux = ucontrol->value.enumerated.item[0];
1573 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1574 mask = e->mask << e->shift_l;
1575 if (e->shift_l != e->shift_r) {
1576 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1577 return -EINVAL;
1578 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1579 mask |= e->mask << e->shift_r;
1582 mutex_lock(&widget->codec->mutex);
1583 widget->value = val;
1584 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
1585 if (widget->event) {
1586 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1587 ret = widget->event(widget,
1588 kcontrol, SND_SOC_DAPM_PRE_REG);
1589 if (ret < 0)
1590 goto out;
1592 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1593 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1594 ret = widget->event(widget,
1595 kcontrol, SND_SOC_DAPM_POST_REG);
1596 } else
1597 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1599 out:
1600 mutex_unlock(&widget->codec->mutex);
1601 return ret;
1603 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1606 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1608 * @kcontrol: mixer control
1609 * @uinfo: control element information
1611 * Callback to provide information about a pin switch control.
1613 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1614 struct snd_ctl_elem_info *uinfo)
1616 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1617 uinfo->count = 1;
1618 uinfo->value.integer.min = 0;
1619 uinfo->value.integer.max = 1;
1621 return 0;
1623 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1626 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1628 * @kcontrol: mixer control
1629 * @ucontrol: Value
1631 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1632 struct snd_ctl_elem_value *ucontrol)
1634 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1635 const char *pin = (const char *)kcontrol->private_value;
1637 mutex_lock(&codec->mutex);
1639 ucontrol->value.integer.value[0] =
1640 snd_soc_dapm_get_pin_status(codec, pin);
1642 mutex_unlock(&codec->mutex);
1644 return 0;
1646 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1649 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1651 * @kcontrol: mixer control
1652 * @ucontrol: Value
1654 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1655 struct snd_ctl_elem_value *ucontrol)
1657 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1658 const char *pin = (const char *)kcontrol->private_value;
1660 mutex_lock(&codec->mutex);
1662 if (ucontrol->value.integer.value[0])
1663 snd_soc_dapm_enable_pin(codec, pin);
1664 else
1665 snd_soc_dapm_disable_pin(codec, pin);
1667 snd_soc_dapm_sync(codec);
1669 mutex_unlock(&codec->mutex);
1671 return 0;
1673 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1676 * snd_soc_dapm_new_control - create new dapm control
1677 * @codec: audio codec
1678 * @widget: widget template
1680 * Creates a new dapm control based upon the template.
1682 * Returns 0 for success else error.
1684 int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1685 const struct snd_soc_dapm_widget *widget)
1687 struct snd_soc_dapm_widget *w;
1689 if ((w = dapm_cnew_widget(widget)) == NULL)
1690 return -ENOMEM;
1692 w->codec = codec;
1693 INIT_LIST_HEAD(&w->sources);
1694 INIT_LIST_HEAD(&w->sinks);
1695 INIT_LIST_HEAD(&w->list);
1696 list_add(&w->list, &codec->dapm_widgets);
1698 /* machine layer set ups unconnected pins and insertions */
1699 w->connected = 1;
1700 return 0;
1702 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1705 * snd_soc_dapm_new_controls - create new dapm controls
1706 * @codec: audio codec
1707 * @widget: widget array
1708 * @num: number of widgets
1710 * Creates new DAPM controls based upon the templates.
1712 * Returns 0 for success else error.
1714 int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1715 const struct snd_soc_dapm_widget *widget,
1716 int num)
1718 int i, ret;
1720 for (i = 0; i < num; i++) {
1721 ret = snd_soc_dapm_new_control(codec, widget);
1722 if (ret < 0) {
1723 printk(KERN_ERR
1724 "ASoC: Failed to create DAPM control %s: %d\n",
1725 widget->name, ret);
1726 return ret;
1728 widget++;
1730 return 0;
1732 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1736 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1737 * @codec: audio codec
1738 * @stream: stream name
1739 * @event: stream event
1741 * Sends a stream event to the dapm core. The core then makes any
1742 * necessary widget power changes.
1744 * Returns 0 for success else error.
1746 int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1747 char *stream, int event)
1749 struct snd_soc_dapm_widget *w;
1751 if (stream == NULL)
1752 return 0;
1754 mutex_lock(&codec->mutex);
1755 list_for_each_entry(w, &codec->dapm_widgets, list)
1757 if (!w->sname)
1758 continue;
1759 pr_debug("widget %s\n %s stream %s event %d\n",
1760 w->name, w->sname, stream, event);
1761 if (strstr(w->sname, stream)) {
1762 switch(event) {
1763 case SND_SOC_DAPM_STREAM_START:
1764 w->active = 1;
1765 break;
1766 case SND_SOC_DAPM_STREAM_STOP:
1767 w->active = 0;
1768 break;
1769 case SND_SOC_DAPM_STREAM_SUSPEND:
1770 if (w->active)
1771 w->suspend = 1;
1772 w->active = 0;
1773 break;
1774 case SND_SOC_DAPM_STREAM_RESUME:
1775 if (w->suspend) {
1776 w->active = 1;
1777 w->suspend = 0;
1779 break;
1780 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1781 break;
1782 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1783 break;
1787 mutex_unlock(&codec->mutex);
1789 dapm_power_widgets(codec, event);
1790 dump_dapm(codec, __func__);
1791 return 0;
1793 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1796 * snd_soc_dapm_enable_pin - enable pin.
1797 * @codec: SoC codec
1798 * @pin: pin name
1800 * Enables input/output pin and its parents or children widgets iff there is
1801 * a valid audio route and active audio stream.
1802 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1803 * do any widget power switching.
1805 int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
1807 return snd_soc_dapm_set_pin(codec, pin, 1);
1809 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
1812 * snd_soc_dapm_disable_pin - disable pin.
1813 * @codec: SoC codec
1814 * @pin: pin name
1816 * Disables input/output pin and its parents or children widgets.
1817 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1818 * do any widget power switching.
1820 int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
1822 return snd_soc_dapm_set_pin(codec, pin, 0);
1824 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
1827 * snd_soc_dapm_nc_pin - permanently disable pin.
1828 * @codec: SoC codec
1829 * @pin: pin name
1831 * Marks the specified pin as being not connected, disabling it along
1832 * any parent or child widgets. At present this is identical to
1833 * snd_soc_dapm_disable_pin() but in future it will be extended to do
1834 * additional things such as disabling controls which only affect
1835 * paths through the pin.
1837 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1838 * do any widget power switching.
1840 int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
1842 return snd_soc_dapm_set_pin(codec, pin, 0);
1844 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
1847 * snd_soc_dapm_get_pin_status - get audio pin status
1848 * @codec: audio codec
1849 * @pin: audio signal pin endpoint (or start point)
1851 * Get audio pin status - connected or disconnected.
1853 * Returns 1 for connected otherwise 0.
1855 int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
1857 struct snd_soc_dapm_widget *w;
1859 list_for_each_entry(w, &codec->dapm_widgets, list) {
1860 if (!strcmp(w->name, pin))
1861 return w->connected;
1864 return 0;
1866 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
1869 * snd_soc_dapm_free - free dapm resources
1870 * @socdev: SoC device
1872 * Free all dapm widgets and resources.
1874 void snd_soc_dapm_free(struct snd_soc_device *socdev)
1876 struct snd_soc_codec *codec = socdev->card->codec;
1878 snd_soc_dapm_sys_remove(socdev->dev);
1879 dapm_free_widgets(codec);
1881 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1883 /* Module information */
1884 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
1885 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1886 MODULE_LICENSE("GPL");