]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - sound/soc/soc-dapm.c
ASoC: TWL4030: Enable/disable voice digital filters
[mirror_ubuntu-hirsute-kernel.git] / sound / soc / soc-dapm.c
CommitLineData
2b97eabc
RP
1/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
d331124d 5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
2b97eabc
RP
6 *
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.
11 *
2b97eabc
RP
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
3a4fa0a2 21 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
2b97eabc
RP
22 * device reopen.
23 *
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.
30 */
31
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>
2b97eabc
RP
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>
45
46/* debug */
c1286b86 47#ifdef DEBUG
2b97eabc 48#define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
2b97eabc
RP
49#else
50#define dump_dapm(codec, action)
2b97eabc
RP
51#endif
52
2b97eabc
RP
53/* dapm power sequences - make this per codec in the future */
54static int dapm_up_seq[] = {
246d0a17
MB
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
2b97eabc 60};
ca9c1aae 61
2b97eabc
RP
62static int dapm_down_seq[] = {
63 snd_soc_dapm_pre, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
ca9c1aae
IM
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,
246d0a17
MB
66 snd_soc_dapm_mux, snd_soc_dapm_value_mux, snd_soc_dapm_supply,
67 snd_soc_dapm_post
2b97eabc
RP
68};
69
70static int dapm_status = 1;
71module_param(dapm_status, int, 0);
72MODULE_PARM_DESC(dapm_status, "enable DPM sysfs entries");
73
12ef193d 74static void pop_wait(u32 pop_time)
15e4c72f
MB
75{
76 if (pop_time)
77 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
78}
79
12ef193d 80static void pop_dbg(u32 pop_time, const char *fmt, ...)
15e4c72f
MB
81{
82 va_list args;
83
84 va_start(args, fmt);
85
86 if (pop_time) {
87 vprintk(fmt, args);
12ef193d 88 pop_wait(pop_time);
15e4c72f
MB
89 }
90
91 va_end(args);
92}
93
2b97eabc 94/* create a new dapm widget */
88cb4290 95static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
2b97eabc
RP
96 const struct snd_soc_dapm_widget *_widget)
97{
88cb4290 98 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
2b97eabc
RP
99}
100
101/* set up initial codec paths */
102static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
103 struct snd_soc_dapm_path *p, int i)
104{
105 switch (w->id) {
106 case snd_soc_dapm_switch:
ca9c1aae
IM
107 case snd_soc_dapm_mixer:
108 case snd_soc_dapm_mixer_named_ctl: {
2b97eabc 109 int val;
4eaa9819
JS
110 struct soc_mixer_control *mc = (struct soc_mixer_control *)
111 w->kcontrols[i].private_value;
815ecf8d
JS
112 unsigned int reg = mc->reg;
113 unsigned int shift = mc->shift;
4eaa9819 114 int max = mc->max;
815ecf8d
JS
115 unsigned int mask = (1 << fls(max)) - 1;
116 unsigned int invert = mc->invert;
2b97eabc
RP
117
118 val = snd_soc_read(w->codec, reg);
119 val = (val >> shift) & mask;
120
121 if ((invert && !val) || (!invert && val))
122 p->connect = 1;
123 else
124 p->connect = 0;
125 }
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;
130
f8ba0b7b 131 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2b97eabc
RP
132 ;
133 val = snd_soc_read(w->codec, e->reg);
134 item = (val >> e->shift_l) & (bitmask - 1);
135
136 p->connect = 0;
f8ba0b7b 137 for (i = 0; i < e->max; i++) {
2b97eabc
RP
138 if (!(strcmp(p->name, e->texts[i])) && item == i)
139 p->connect = 1;
140 }
141 }
142 break;
2e72f8e3 143 case snd_soc_dapm_value_mux: {
74155556 144 struct soc_enum *e = (struct soc_enum *)
2e72f8e3
PU
145 w->kcontrols[i].private_value;
146 int val, item;
147
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;
153 }
154
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;
159 }
160 }
161 break;
2b97eabc
RP
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:
246d0a17 170 case snd_soc_dapm_supply:
2b97eabc
RP
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;
182 }
183}
184
185/* connect mux widget to it's interconnecting audio paths */
186static 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)
190{
191 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
192 int i;
193
f8ba0b7b 194 for (i = 0; i < e->max; i++) {
2b97eabc
RP
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;
202 }
203 }
204
205 return -ENODEV;
206}
207
208/* connect mixer widget to it's interconnecting audio paths */
209static 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)
212{
213 int i;
214
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;
224 }
225 }
226 return -ENODEV;
227}
228
229/* update dapm codec register bits */
230static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
231{
232 int change, power;
233 unsigned short old, new;
234 struct snd_soc_codec *codec = widget->codec;
235
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;
244
245 power = widget->power;
246 if (widget->invert)
247 power = (power ? 0:1);
248
249 old = snd_soc_read(codec, widget->reg);
250 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
251
252 change = old != new;
253 if (change) {
12ef193d
TK
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);
2b97eabc 257 snd_soc_write(codec, widget->reg, new);
12ef193d 258 pop_wait(codec->pop_time);
2b97eabc 259 }
c1286b86
MB
260 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
261 old, new, change);
2b97eabc
RP
262 return change;
263}
264
265/* ramps the volume up or down to minimise pops before or after a
266 * DAPM power event */
267static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
268{
269 const struct snd_kcontrol_new *k = widget->kcontrols;
270
271 if (widget->muted && !power)
272 return 0;
273 if (!widget->muted && power)
274 return 0;
275
276 if (widget->num_kcontrols && k) {
4eaa9819
JS
277 struct soc_mixer_control *mc =
278 (struct soc_mixer_control *)k->private_value;
815ecf8d
JS
279 unsigned int reg = mc->reg;
280 unsigned int shift = mc->shift;
4eaa9819 281 int max = mc->max;
815ecf8d
JS
282 unsigned int mask = (1 << fls(max)) - 1;
283 unsigned int invert = mc->invert;
2b97eabc
RP
284
285 if (power) {
286 int i;
287 /* power up has happended, increase volume to last level */
288 if (invert) {
4eaa9819 289 for (i = max; i > widget->saved_value; i--)
2b97eabc
RP
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);
294 }
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);
306 }
307 widget->muted = 1;
308 }
309 }
310 return 0;
311}
312
313/* create new dapm mixer control */
314static int dapm_new_mixer(struct snd_soc_codec *codec,
315 struct snd_soc_dapm_widget *w)
316{
317 int i, ret = 0;
219b93f5 318 size_t name_len;
2b97eabc
RP
319 struct snd_soc_dapm_path *path;
320
321 /* add kcontrol */
322 for (i = 0; i < w->num_kcontrols; i++) {
323
324 /* match name */
325 list_for_each_entry(path, &w->sources, list_sink) {
326
327 /* mixer/mux paths name must match control name */
328 if (path->name != (char*)w->kcontrols[i].name)
329 continue;
330
ca9c1aae
IM
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.
336 */
337 name_len = strlen(w->kcontrols[i].name) + 1;
07495f3e 338 if (w->id != snd_soc_dapm_mixer_named_ctl)
ca9c1aae
IM
339 name_len += 1 + strlen(w->name);
340
219b93f5 341 path->long_name = kmalloc(name_len, GFP_KERNEL);
ca9c1aae 342
2b97eabc
RP
343 if (path->long_name == NULL)
344 return -ENOMEM;
345
ca9c1aae 346 switch (w->id) {
ca9c1aae
IM
347 default:
348 snprintf(path->long_name, name_len, "%s %s",
349 w->name, w->kcontrols[i].name);
07495f3e 350 break;
ca9c1aae
IM
351 case snd_soc_dapm_mixer_named_ctl:
352 snprintf(path->long_name, name_len, "%s",
353 w->kcontrols[i].name);
07495f3e 354 break;
ca9c1aae
IM
355 }
356
219b93f5
MB
357 path->long_name[name_len - 1] = '\0';
358
2b97eabc
RP
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) {
6553e192
MB
363 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
364 path->long_name,
365 ret);
2b97eabc
RP
366 kfree(path->long_name);
367 path->long_name = NULL;
368 return ret;
369 }
370 }
371 }
372 return ret;
373}
374
375/* create new dapm mux control */
376static int dapm_new_mux(struct snd_soc_codec *codec,
377 struct snd_soc_dapm_widget *w)
378{
379 struct snd_soc_dapm_path *path = NULL;
380 struct snd_kcontrol *kcontrol;
381 int ret = 0;
382
383 if (!w->num_kcontrols) {
384 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
385 return -EINVAL;
386 }
387
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;
392
393 list_for_each_entry(path, &w->sources, list_sink)
394 path->kcontrol = kcontrol;
395
396 return ret;
397
398err:
399 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
400 return ret;
401}
402
403/* create new dapm volume control */
404static int dapm_new_pga(struct snd_soc_codec *codec,
405 struct snd_soc_dapm_widget *w)
406{
407 struct snd_kcontrol *kcontrol;
408 int ret = 0;
409
410 if (!w->num_kcontrols)
411 return -EINVAL;
412
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;
418 }
419
420 return ret;
421}
422
423/* reset 'walked' bit for each dapm path */
424static inline void dapm_clear_walk(struct snd_soc_codec *codec)
425{
426 struct snd_soc_dapm_path *p;
427
428 list_for_each_entry(p, &codec->dapm_paths, list)
429 p->walked = 0;
430}
431
432/*
433 * Recursively check for a completed path to an active or physically connected
434 * output widget. Returns number of complete paths.
435 */
436static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
437{
438 struct snd_soc_dapm_path *path;
439 int con = 0;
440
246d0a17
MB
441 if (widget->id == snd_soc_dapm_supply)
442 return 0;
443
2b97eabc
RP
444 if (widget->id == snd_soc_dapm_adc && widget->active)
445 return 1;
446
447 if (widget->connected) {
448 /* connected pin ? */
449 if (widget->id == snd_soc_dapm_output && !widget->ext)
450 return 1;
451
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;
456 }
457
458 list_for_each_entry(path, &widget->sinks, list_source) {
459 if (path->walked)
460 continue;
461
462 if (path->sink && path->connect) {
463 path->walked = 1;
464 con += is_connected_output_ep(path->sink);
465 }
466 }
467
468 return con;
469}
470
471/*
472 * Recursively check for a completed path to an active or physically connected
473 * input widget. Returns number of complete paths.
474 */
475static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
476{
477 struct snd_soc_dapm_path *path;
478 int con = 0;
479
246d0a17
MB
480 if (widget->id == snd_soc_dapm_supply)
481 return 0;
482
2b97eabc
RP
483 /* active stream ? */
484 if (widget->id == snd_soc_dapm_dac && widget->active)
485 return 1;
486
487 if (widget->connected) {
488 /* connected pin ? */
489 if (widget->id == snd_soc_dapm_input && !widget->ext)
490 return 1;
491
492 /* connected VMID/Bias for lower pops */
493 if (widget->id == snd_soc_dapm_vmid)
494 return 1;
495
496 /* connected jack ? */
497 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
498 return 1;
499 }
500
501 list_for_each_entry(path, &widget->sources, list_sink) {
502 if (path->walked)
503 continue;
504
505 if (path->source && path->connect) {
506 path->walked = 1;
507 con += is_connected_input_ep(path->source);
508 }
509 }
510
511 return con;
512}
513
e2be2ccf
JN
514/*
515 * Handler for generic register modifier widget.
516 */
517int dapm_reg_event(struct snd_soc_dapm_widget *w,
518 struct snd_kcontrol *kcontrol, int event)
519{
520 unsigned int val;
521
522 if (SND_SOC_DAPM_EVENT_ON(event))
523 val = w->on_val;
524 else
525 val = w->off_val;
526
527 snd_soc_update_bits(w->codec, -(w->reg + 1),
528 w->mask << w->shift, val << w->shift);
529
530 return 0;
531}
11589418 532EXPORT_SYMBOL_GPL(dapm_reg_event);
e2be2ccf 533
025756ec
MB
534/* Standard power change method, used to apply power changes to most
535 * widgets.
536 */
537static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
538{
539 int ret;
540
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);
546
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;
553 }
554
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;
561 }
562
563 /* Lower PGA volume to reduce pops */
564 if (w->id == snd_soc_dapm_pga && !w->power)
565 dapm_set_pga(w, w->power);
566
567 dapm_update_bits(w);
568
569 /* Raise PGA volume to reduce pops */
570 if (w->id == snd_soc_dapm_pga && w->power)
571 dapm_set_pga(w, w->power);
572
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;
580 }
581
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;
588 }
589
590 return 0;
591}
592
cd0f2d47
MB
593/* Generic check to see if a widget should be powered.
594 */
595static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
596{
597 int in, out;
598
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;
604}
605
6ea31b9f
MB
606/* Check to see if an ADC has power */
607static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
608{
609 int in;
610
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);
617 }
618}
619
620/* Check to see if a DAC has power */
621static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
622{
623 int out;
624
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);
631 }
632}
633
246d0a17
MB
634/* Check to see if a power supply is needed */
635static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
636{
637 struct snd_soc_dapm_path *path;
638 int power = 0;
639
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;
646 }
647 }
648
649 dapm_clear_walk(w->codec);
650
651 return power;
652}
653
42aa3418
MB
654/*
655 * Scan a single DAPM widget for a complete audio path and update the
656 * power status appropriately.
657 */
658static int dapm_power_widget(struct snd_soc_codec *codec, int event,
659 struct snd_soc_dapm_widget *w)
660{
6ea31b9f 661 int power, ret;
42aa3418 662
6ea31b9f 663 switch (w->id) {
6ea31b9f 664 case snd_soc_dapm_pre:
42aa3418
MB
665 if (!w->event)
666 return 0;
667
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;
678 }
679 return 0;
6ea31b9f
MB
680
681 case snd_soc_dapm_post:
42aa3418
MB
682 if (!w->event)
683 return 0;
684
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;
695 }
696 return 0;
42aa3418 697
6ea31b9f 698 default:
6ea31b9f
MB
699 break;
700 }
42aa3418 701
b75576d7
MB
702 if (!w->power_check)
703 return 0;
704
705 power = w->power_check(w);
6ea31b9f 706 if (w->power == power)
42aa3418 707 return 0;
6ea31b9f 708 w->power = power;
42aa3418 709
025756ec 710 return dapm_generic_apply_power(w);
42aa3418
MB
711}
712
2b97eabc
RP
713/*
714 * Scan each dapm widget for complete audio path.
715 * A complete path is a route that has valid endpoints i.e.:-
716 *
717 * o DAC to output pin.
718 * o Input Pin to ADC.
719 * o Input pin to Output pin (bypass, sidetone)
720 * o DAC to ADC (loopback).
721 */
d9c96cf3 722static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
2b97eabc
RP
723{
724 struct snd_soc_dapm_widget *w;
42aa3418 725 int i, c = 1, *seq = NULL, ret = 0;
2b97eabc
RP
726
727 /* do we have a sequenced stream event */
728 if (event == SND_SOC_DAPM_STREAM_START) {
729 c = ARRAY_SIZE(dapm_up_seq);
730 seq = dapm_up_seq;
731 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
732 c = ARRAY_SIZE(dapm_down_seq);
733 seq = dapm_down_seq;
734 }
735
42aa3418 736 for (i = 0; i < c; i++) {
2b97eabc
RP
737 list_for_each_entry(w, &codec->dapm_widgets, list) {
738
739 /* is widget in stream order */
740 if (seq && seq[i] && w->id != seq[i])
741 continue;
742
42aa3418
MB
743 ret = dapm_power_widget(codec, event, w);
744 if (ret != 0)
745 return ret;
2b97eabc
RP
746 }
747 }
748
42aa3418 749 return 0;
2b97eabc
RP
750}
751
c1286b86 752#ifdef DEBUG
2b97eabc
RP
753static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
754{
755 struct snd_soc_dapm_widget *w;
756 struct snd_soc_dapm_path *p = NULL;
757 int in, out;
758
759 printk("DAPM %s %s\n", codec->name, action);
760
761 list_for_each_entry(w, &codec->dapm_widgets, list) {
762
763 /* only display widgets that effect routing */
764 switch (w->id) {
765 case snd_soc_dapm_pre:
766 case snd_soc_dapm_post:
767 case snd_soc_dapm_vmid:
768 continue;
769 case snd_soc_dapm_mux:
2e72f8e3 770 case snd_soc_dapm_value_mux:
2b97eabc
RP
771 case snd_soc_dapm_output:
772 case snd_soc_dapm_input:
773 case snd_soc_dapm_switch:
774 case snd_soc_dapm_hp:
775 case snd_soc_dapm_mic:
776 case snd_soc_dapm_spk:
777 case snd_soc_dapm_line:
778 case snd_soc_dapm_micbias:
779 case snd_soc_dapm_dac:
780 case snd_soc_dapm_adc:
781 case snd_soc_dapm_pga:
782 case snd_soc_dapm_mixer:
ca9c1aae 783 case snd_soc_dapm_mixer_named_ctl:
246d0a17 784 case snd_soc_dapm_supply:
2b97eabc
RP
785 if (w->name) {
786 in = is_connected_input_ep(w);
787 dapm_clear_walk(w->codec);
788 out = is_connected_output_ep(w);
789 dapm_clear_walk(w->codec);
790 printk("%s: %s in %d out %d\n", w->name,
791 w->power ? "On":"Off",in, out);
792
793 list_for_each_entry(p, &w->sources, list_sink) {
794 if (p->connect)
795 printk(" in %s %s\n", p->name ? p->name : "static",
796 p->source->name);
797 }
798 list_for_each_entry(p, &w->sinks, list_source) {
2b97eabc
RP
799 if (p->connect)
800 printk(" out %s %s\n", p->name ? p->name : "static",
801 p->sink->name);
802 }
803 }
804 break;
805 }
806 }
807}
808#endif
809
810/* test and update the power status of a mux widget */
d9c96cf3
AB
811static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
812 struct snd_kcontrol *kcontrol, int mask,
cb01e2b9 813 int mux, int val, struct soc_enum *e)
2b97eabc
RP
814{
815 struct snd_soc_dapm_path *path;
816 int found = 0;
817
eff317d0
PU
818 if (widget->id != snd_soc_dapm_mux &&
819 widget->id != snd_soc_dapm_value_mux)
2b97eabc
RP
820 return -ENODEV;
821
822 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
823 return 0;
824
825 /* find dapm widget path assoc with kcontrol */
826 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
827 if (path->kcontrol != kcontrol)
828 continue;
829
cb01e2b9 830 if (!path->name || !e->texts[mux])
2b97eabc
RP
831 continue;
832
833 found = 1;
834 /* we now need to match the string in the enum to the path */
cb01e2b9 835 if (!(strcmp(path->name, e->texts[mux])))
2b97eabc
RP
836 path->connect = 1; /* new connection */
837 else
838 path->connect = 0; /* old connection must be powered down */
839 }
840
3fccd8b1 841 if (found) {
2b97eabc 842 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
3fccd8b1
MB
843 dump_dapm(widget->codec, "mux power update");
844 }
2b97eabc
RP
845
846 return 0;
847}
2b97eabc 848
1b075e3f 849/* test and update the power status of a mixer or switch widget */
d9c96cf3
AB
850static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
851 struct snd_kcontrol *kcontrol, int reg,
852 int val_mask, int val, int invert)
2b97eabc
RP
853{
854 struct snd_soc_dapm_path *path;
855 int found = 0;
856
1b075e3f 857 if (widget->id != snd_soc_dapm_mixer &&
ca9c1aae 858 widget->id != snd_soc_dapm_mixer_named_ctl &&
1b075e3f 859 widget->id != snd_soc_dapm_switch)
2b97eabc
RP
860 return -ENODEV;
861
862 if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
863 return 0;
864
865 /* find dapm widget path assoc with kcontrol */
866 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
867 if (path->kcontrol != kcontrol)
868 continue;
869
870 /* found, now check type */
871 found = 1;
872 if (val)
873 /* new connection */
874 path->connect = invert ? 0:1;
875 else
876 /* old connection must be powered down */
877 path->connect = invert ? 1:0;
878 break;
879 }
880
3fccd8b1 881 if (found) {
2b97eabc 882 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
3fccd8b1
MB
883 dump_dapm(widget->codec, "mixer power update");
884 }
2b97eabc
RP
885
886 return 0;
887}
2b97eabc
RP
888
889/* show dapm widget status in sys fs */
890static ssize_t dapm_widget_show(struct device *dev,
891 struct device_attribute *attr, char *buf)
892{
893 struct snd_soc_device *devdata = dev_get_drvdata(dev);
6627a653 894 struct snd_soc_codec *codec = devdata->card->codec;
2b97eabc
RP
895 struct snd_soc_dapm_widget *w;
896 int count = 0;
897 char *state = "not set";
898
899 list_for_each_entry(w, &codec->dapm_widgets, list) {
900
901 /* only display widgets that burnm power */
902 switch (w->id) {
903 case snd_soc_dapm_hp:
904 case snd_soc_dapm_mic:
905 case snd_soc_dapm_spk:
906 case snd_soc_dapm_line:
907 case snd_soc_dapm_micbias:
908 case snd_soc_dapm_dac:
909 case snd_soc_dapm_adc:
910 case snd_soc_dapm_pga:
911 case snd_soc_dapm_mixer:
ca9c1aae 912 case snd_soc_dapm_mixer_named_ctl:
246d0a17 913 case snd_soc_dapm_supply:
2b97eabc
RP
914 if (w->name)
915 count += sprintf(buf + count, "%s: %s\n",
916 w->name, w->power ? "On":"Off");
917 break;
918 default:
919 break;
920 }
921 }
922
0be9898a
MB
923 switch (codec->bias_level) {
924 case SND_SOC_BIAS_ON:
925 state = "On";
2b97eabc 926 break;
0be9898a
MB
927 case SND_SOC_BIAS_PREPARE:
928 state = "Prepare";
2b97eabc 929 break;
0be9898a
MB
930 case SND_SOC_BIAS_STANDBY:
931 state = "Standby";
2b97eabc 932 break;
0be9898a
MB
933 case SND_SOC_BIAS_OFF:
934 state = "Off";
2b97eabc
RP
935 break;
936 }
937 count += sprintf(buf + count, "PM State: %s\n", state);
938
939 return count;
940}
941
942static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
943
944int snd_soc_dapm_sys_add(struct device *dev)
945{
f0062a92
MB
946 if (!dapm_status)
947 return 0;
12ef193d 948 return device_create_file(dev, &dev_attr_dapm_widget);
2b97eabc
RP
949}
950
951static void snd_soc_dapm_sys_remove(struct device *dev)
952{
15e4c72f 953 if (dapm_status) {
2b97eabc 954 device_remove_file(dev, &dev_attr_dapm_widget);
15e4c72f 955 }
2b97eabc
RP
956}
957
958/* free all dapm widgets and resources */
d9c96cf3 959static void dapm_free_widgets(struct snd_soc_codec *codec)
2b97eabc
RP
960{
961 struct snd_soc_dapm_widget *w, *next_w;
962 struct snd_soc_dapm_path *p, *next_p;
963
964 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
965 list_del(&w->list);
966 kfree(w);
967 }
968
969 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
970 list_del(&p->list);
971 kfree(p->long_name);
972 kfree(p);
973 }
974}
975
a5302181 976static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
1649923d 977 const char *pin, int status)
a5302181
LG
978{
979 struct snd_soc_dapm_widget *w;
980
981 list_for_each_entry(w, &codec->dapm_widgets, list) {
982 if (!strcmp(w->name, pin)) {
c1286b86 983 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
a5302181
LG
984 w->connected = status;
985 return 0;
986 }
987 }
988
c1286b86 989 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
a5302181
LG
990 return -EINVAL;
991}
992
2b97eabc 993/**
a5302181 994 * snd_soc_dapm_sync - scan and power dapm paths
2b97eabc
RP
995 * @codec: audio codec
996 *
997 * Walks all dapm audio paths and powers widgets according to their
998 * stream or path usage.
999 *
1000 * Returns 0 for success.
1001 */
a5302181 1002int snd_soc_dapm_sync(struct snd_soc_codec *codec)
2b97eabc 1003{
3fccd8b1
MB
1004 int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1005 dump_dapm(codec, "sync");
1006 return ret;
2b97eabc 1007}
a5302181 1008EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2b97eabc 1009
105f1c28
MB
1010static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
1011 const char *sink, const char *control, const char *source)
2b97eabc
RP
1012{
1013 struct snd_soc_dapm_path *path;
1014 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1015 int ret = 0;
1016
1017 /* find src and dest widgets */
1018 list_for_each_entry(w, &codec->dapm_widgets, list) {
1019
1020 if (!wsink && !(strcmp(w->name, sink))) {
1021 wsink = w;
1022 continue;
1023 }
1024 if (!wsource && !(strcmp(w->name, source))) {
1025 wsource = w;
1026 }
1027 }
1028
1029 if (wsource == NULL || wsink == NULL)
1030 return -ENODEV;
1031
1032 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1033 if (!path)
1034 return -ENOMEM;
1035
1036 path->source = wsource;
1037 path->sink = wsink;
1038 INIT_LIST_HEAD(&path->list);
1039 INIT_LIST_HEAD(&path->list_source);
1040 INIT_LIST_HEAD(&path->list_sink);
1041
1042 /* check for external widgets */
1043 if (wsink->id == snd_soc_dapm_input) {
1044 if (wsource->id == snd_soc_dapm_micbias ||
1045 wsource->id == snd_soc_dapm_mic ||
1e39221e
SF
1046 wsink->id == snd_soc_dapm_line ||
1047 wsink->id == snd_soc_dapm_output)
2b97eabc
RP
1048 wsink->ext = 1;
1049 }
1050 if (wsource->id == snd_soc_dapm_output) {
1051 if (wsink->id == snd_soc_dapm_spk ||
1052 wsink->id == snd_soc_dapm_hp ||
1e39221e
SF
1053 wsink->id == snd_soc_dapm_line ||
1054 wsink->id == snd_soc_dapm_input)
2b97eabc
RP
1055 wsource->ext = 1;
1056 }
1057
1058 /* connect static paths */
1059 if (control == NULL) {
1060 list_add(&path->list, &codec->dapm_paths);
1061 list_add(&path->list_sink, &wsink->sources);
1062 list_add(&path->list_source, &wsource->sinks);
1063 path->connect = 1;
1064 return 0;
1065 }
1066
1067 /* connect dynamic paths */
1068 switch(wsink->id) {
1069 case snd_soc_dapm_adc:
1070 case snd_soc_dapm_dac:
1071 case snd_soc_dapm_pga:
1072 case snd_soc_dapm_input:
1073 case snd_soc_dapm_output:
1074 case snd_soc_dapm_micbias:
1075 case snd_soc_dapm_vmid:
1076 case snd_soc_dapm_pre:
1077 case snd_soc_dapm_post:
246d0a17 1078 case snd_soc_dapm_supply:
2b97eabc
RP
1079 list_add(&path->list, &codec->dapm_paths);
1080 list_add(&path->list_sink, &wsink->sources);
1081 list_add(&path->list_source, &wsource->sinks);
1082 path->connect = 1;
1083 return 0;
1084 case snd_soc_dapm_mux:
74155556 1085 case snd_soc_dapm_value_mux:
2b97eabc
RP
1086 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1087 &wsink->kcontrols[0]);
1088 if (ret != 0)
1089 goto err;
1090 break;
1091 case snd_soc_dapm_switch:
1092 case snd_soc_dapm_mixer:
ca9c1aae 1093 case snd_soc_dapm_mixer_named_ctl:
2b97eabc
RP
1094 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1095 if (ret != 0)
1096 goto err;
1097 break;
1098 case snd_soc_dapm_hp:
1099 case snd_soc_dapm_mic:
1100 case snd_soc_dapm_line:
1101 case snd_soc_dapm_spk:
1102 list_add(&path->list, &codec->dapm_paths);
1103 list_add(&path->list_sink, &wsink->sources);
1104 list_add(&path->list_source, &wsource->sinks);
1105 path->connect = 0;
1106 return 0;
1107 }
1108 return 0;
1109
1110err:
1111 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1112 control, sink);
1113 kfree(path);
1114 return ret;
1115}
105f1c28 1116
105f1c28
MB
1117/**
1118 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1119 * @codec: codec
1120 * @route: audio routes
1121 * @num: number of routes
1122 *
1123 * Connects 2 dapm widgets together via a named audio path. The sink is
1124 * the widget receiving the audio signal, whilst the source is the sender
1125 * of the audio signal.
1126 *
1127 * Returns 0 for success else error. On error all resources can be freed
1128 * with a call to snd_soc_card_free().
1129 */
1130int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1131 const struct snd_soc_dapm_route *route, int num)
1132{
1133 int i, ret;
1134
1135 for (i = 0; i < num; i++) {
1136 ret = snd_soc_dapm_add_route(codec, route->sink,
1137 route->control, route->source);
1138 if (ret < 0) {
1139 printk(KERN_ERR "Failed to add route %s->%s\n",
1140 route->source,
1141 route->sink);
1142 return ret;
1143 }
1144 route++;
1145 }
1146
1147 return 0;
1148}
1149EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1150
2b97eabc
RP
1151/**
1152 * snd_soc_dapm_new_widgets - add new dapm widgets
1153 * @codec: audio codec
1154 *
1155 * Checks the codec for any new dapm widgets and creates them if found.
1156 *
1157 * Returns 0 for success.
1158 */
1159int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1160{
1161 struct snd_soc_dapm_widget *w;
1162
2b97eabc
RP
1163 list_for_each_entry(w, &codec->dapm_widgets, list)
1164 {
1165 if (w->new)
1166 continue;
1167
1168 switch(w->id) {
1169 case snd_soc_dapm_switch:
1170 case snd_soc_dapm_mixer:
ca9c1aae 1171 case snd_soc_dapm_mixer_named_ctl:
b75576d7 1172 w->power_check = dapm_generic_check_power;
2b97eabc
RP
1173 dapm_new_mixer(codec, w);
1174 break;
1175 case snd_soc_dapm_mux:
2e72f8e3 1176 case snd_soc_dapm_value_mux:
b75576d7 1177 w->power_check = dapm_generic_check_power;
2b97eabc
RP
1178 dapm_new_mux(codec, w);
1179 break;
1180 case snd_soc_dapm_adc:
b75576d7
MB
1181 w->power_check = dapm_adc_check_power;
1182 break;
2b97eabc 1183 case snd_soc_dapm_dac:
b75576d7
MB
1184 w->power_check = dapm_dac_check_power;
1185 break;
2b97eabc 1186 case snd_soc_dapm_pga:
b75576d7 1187 w->power_check = dapm_generic_check_power;
2b97eabc
RP
1188 dapm_new_pga(codec, w);
1189 break;
1190 case snd_soc_dapm_input:
1191 case snd_soc_dapm_output:
1192 case snd_soc_dapm_micbias:
1193 case snd_soc_dapm_spk:
1194 case snd_soc_dapm_hp:
1195 case snd_soc_dapm_mic:
1196 case snd_soc_dapm_line:
b75576d7
MB
1197 w->power_check = dapm_generic_check_power;
1198 break;
246d0a17
MB
1199 case snd_soc_dapm_supply:
1200 w->power_check = dapm_supply_check_power;
2b97eabc
RP
1201 case snd_soc_dapm_vmid:
1202 case snd_soc_dapm_pre:
1203 case snd_soc_dapm_post:
1204 break;
1205 }
1206 w->new = 1;
1207 }
1208
1209 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
2b97eabc
RP
1210 return 0;
1211}
1212EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1213
1214/**
1215 * snd_soc_dapm_get_volsw - dapm mixer get callback
1216 * @kcontrol: mixer control
ac11a2b3 1217 * @ucontrol: control element information
2b97eabc
RP
1218 *
1219 * Callback to get the value of a dapm mixer control.
1220 *
1221 * Returns 0 for success.
1222 */
1223int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1224 struct snd_ctl_elem_value *ucontrol)
1225{
1226 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
4eaa9819
JS
1227 struct soc_mixer_control *mc =
1228 (struct soc_mixer_control *)kcontrol->private_value;
815ecf8d
JS
1229 unsigned int reg = mc->reg;
1230 unsigned int shift = mc->shift;
1231 unsigned int rshift = mc->rshift;
4eaa9819 1232 int max = mc->max;
815ecf8d
JS
1233 unsigned int invert = mc->invert;
1234 unsigned int mask = (1 << fls(max)) - 1;
2b97eabc
RP
1235
1236 /* return the saved value if we are powered down */
1237 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1238 ucontrol->value.integer.value[0] = widget->saved_value;
1239 return 0;
1240 }
1241
1242 ucontrol->value.integer.value[0] =
1243 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1244 if (shift != rshift)
1245 ucontrol->value.integer.value[1] =
1246 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1247 if (invert) {
1248 ucontrol->value.integer.value[0] =
a7a4ac86 1249 max - ucontrol->value.integer.value[0];
2b97eabc
RP
1250 if (shift != rshift)
1251 ucontrol->value.integer.value[1] =
a7a4ac86 1252 max - ucontrol->value.integer.value[1];
2b97eabc
RP
1253 }
1254
1255 return 0;
1256}
1257EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1258
1259/**
1260 * snd_soc_dapm_put_volsw - dapm mixer set callback
1261 * @kcontrol: mixer control
ac11a2b3 1262 * @ucontrol: control element information
2b97eabc
RP
1263 *
1264 * Callback to set the value of a dapm mixer control.
1265 *
1266 * Returns 0 for success.
1267 */
1268int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1269 struct snd_ctl_elem_value *ucontrol)
1270{
1271 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
4eaa9819
JS
1272 struct soc_mixer_control *mc =
1273 (struct soc_mixer_control *)kcontrol->private_value;
815ecf8d
JS
1274 unsigned int reg = mc->reg;
1275 unsigned int shift = mc->shift;
1276 unsigned int rshift = mc->rshift;
4eaa9819 1277 int max = mc->max;
815ecf8d
JS
1278 unsigned int mask = (1 << fls(max)) - 1;
1279 unsigned int invert = mc->invert;
2b97eabc
RP
1280 unsigned short val, val2, val_mask;
1281 int ret;
1282
1283 val = (ucontrol->value.integer.value[0] & mask);
1284
1285 if (invert)
a7a4ac86 1286 val = max - val;
2b97eabc
RP
1287 val_mask = mask << shift;
1288 val = val << shift;
1289 if (shift != rshift) {
1290 val2 = (ucontrol->value.integer.value[1] & mask);
1291 if (invert)
a7a4ac86 1292 val2 = max - val2;
2b97eabc
RP
1293 val_mask |= mask << rshift;
1294 val |= val2 << rshift;
1295 }
1296
1297 mutex_lock(&widget->codec->mutex);
1298 widget->value = val;
1299
1300 /* save volume value if the widget is powered down */
1301 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1302 widget->saved_value = val;
1303 mutex_unlock(&widget->codec->mutex);
1304 return 1;
1305 }
1306
1307 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1308 if (widget->event) {
1309 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
9af6d956
LG
1310 ret = widget->event(widget, kcontrol,
1311 SND_SOC_DAPM_PRE_REG);
1312 if (ret < 0) {
1313 ret = 1;
2b97eabc 1314 goto out;
9af6d956 1315 }
2b97eabc
RP
1316 }
1317 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1318 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
9af6d956
LG
1319 ret = widget->event(widget, kcontrol,
1320 SND_SOC_DAPM_POST_REG);
2b97eabc
RP
1321 } else
1322 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1323
1324out:
1325 mutex_unlock(&widget->codec->mutex);
1326 return ret;
1327}
1328EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1329
1330/**
1331 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1332 * @kcontrol: mixer control
ac11a2b3 1333 * @ucontrol: control element information
2b97eabc
RP
1334 *
1335 * Callback to get the value of a dapm enumerated double mixer control.
1336 *
1337 * Returns 0 for success.
1338 */
1339int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1340 struct snd_ctl_elem_value *ucontrol)
1341{
1342 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1343 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1344 unsigned short val, bitmask;
1345
f8ba0b7b 1346 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2b97eabc
RP
1347 ;
1348 val = snd_soc_read(widget->codec, e->reg);
1349 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1350 if (e->shift_l != e->shift_r)
1351 ucontrol->value.enumerated.item[1] =
1352 (val >> e->shift_r) & (bitmask - 1);
1353
1354 return 0;
1355}
1356EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1357
1358/**
1359 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1360 * @kcontrol: mixer control
ac11a2b3 1361 * @ucontrol: control element information
2b97eabc
RP
1362 *
1363 * Callback to set the value of a dapm enumerated double mixer control.
1364 *
1365 * Returns 0 for success.
1366 */
1367int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1368 struct snd_ctl_elem_value *ucontrol)
1369{
1370 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1371 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1372 unsigned short val, mux;
1373 unsigned short mask, bitmask;
1374 int ret = 0;
1375
f8ba0b7b 1376 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2b97eabc 1377 ;
f8ba0b7b 1378 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2b97eabc
RP
1379 return -EINVAL;
1380 mux = ucontrol->value.enumerated.item[0];
1381 val = mux << e->shift_l;
1382 mask = (bitmask - 1) << e->shift_l;
1383 if (e->shift_l != e->shift_r) {
f8ba0b7b 1384 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2b97eabc
RP
1385 return -EINVAL;
1386 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1387 mask |= (bitmask - 1) << e->shift_r;
1388 }
1389
1390 mutex_lock(&widget->codec->mutex);
1391 widget->value = val;
cb01e2b9 1392 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
2b97eabc
RP
1393 if (widget->event) {
1394 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
9af6d956
LG
1395 ret = widget->event(widget,
1396 kcontrol, SND_SOC_DAPM_PRE_REG);
2b97eabc
RP
1397 if (ret < 0)
1398 goto out;
1399 }
1400 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1401 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
9af6d956
LG
1402 ret = widget->event(widget,
1403 kcontrol, SND_SOC_DAPM_POST_REG);
2b97eabc
RP
1404 } else
1405 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1406
1407out:
1408 mutex_unlock(&widget->codec->mutex);
1409 return ret;
1410}
1411EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1412
2e72f8e3
PU
1413/**
1414 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1415 * callback
1416 * @kcontrol: mixer control
1417 * @ucontrol: control element information
1418 *
1419 * Callback to get the value of a dapm semi enumerated double mixer control.
1420 *
1421 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1422 * used for handling bitfield coded enumeration for example.
1423 *
1424 * Returns 0 for success.
1425 */
1426int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1427 struct snd_ctl_elem_value *ucontrol)
1428{
1429 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
74155556 1430 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2e72f8e3
PU
1431 unsigned short reg_val, val, mux;
1432
1433 reg_val = snd_soc_read(widget->codec, e->reg);
1434 val = (reg_val >> e->shift_l) & e->mask;
1435 for (mux = 0; mux < e->max; mux++) {
1436 if (val == e->values[mux])
1437 break;
1438 }
1439 ucontrol->value.enumerated.item[0] = mux;
1440 if (e->shift_l != e->shift_r) {
1441 val = (reg_val >> e->shift_r) & e->mask;
1442 for (mux = 0; mux < e->max; mux++) {
1443 if (val == e->values[mux])
1444 break;
1445 }
1446 ucontrol->value.enumerated.item[1] = mux;
1447 }
1448
1449 return 0;
1450}
1451EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1452
1453/**
1454 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1455 * callback
1456 * @kcontrol: mixer control
1457 * @ucontrol: control element information
1458 *
1459 * Callback to set the value of a dapm semi enumerated double mixer control.
1460 *
1461 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1462 * used for handling bitfield coded enumeration for example.
1463 *
1464 * Returns 0 for success.
1465 */
1466int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1467 struct snd_ctl_elem_value *ucontrol)
1468{
1469 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
74155556 1470 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2e72f8e3
PU
1471 unsigned short val, mux;
1472 unsigned short mask;
1473 int ret = 0;
1474
1475 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1476 return -EINVAL;
1477 mux = ucontrol->value.enumerated.item[0];
1478 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1479 mask = e->mask << e->shift_l;
1480 if (e->shift_l != e->shift_r) {
1481 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1482 return -EINVAL;
1483 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1484 mask |= e->mask << e->shift_r;
1485 }
1486
1487 mutex_lock(&widget->codec->mutex);
1488 widget->value = val;
74155556 1489 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
2e72f8e3
PU
1490 if (widget->event) {
1491 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1492 ret = widget->event(widget,
1493 kcontrol, SND_SOC_DAPM_PRE_REG);
1494 if (ret < 0)
1495 goto out;
1496 }
1497 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1498 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1499 ret = widget->event(widget,
1500 kcontrol, SND_SOC_DAPM_POST_REG);
1501 } else
1502 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1503
1504out:
1505 mutex_unlock(&widget->codec->mutex);
1506 return ret;
1507}
1508EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1509
8b37dbd2
MB
1510/**
1511 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1512 *
1513 * @kcontrol: mixer control
1514 * @uinfo: control element information
1515 *
1516 * Callback to provide information about a pin switch control.
1517 */
1518int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1519 struct snd_ctl_elem_info *uinfo)
1520{
1521 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1522 uinfo->count = 1;
1523 uinfo->value.integer.min = 0;
1524 uinfo->value.integer.max = 1;
1525
1526 return 0;
1527}
1528EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1529
1530/**
1531 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1532 *
1533 * @kcontrol: mixer control
1534 * @ucontrol: Value
1535 */
1536int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1537 struct snd_ctl_elem_value *ucontrol)
1538{
1539 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1540 const char *pin = (const char *)kcontrol->private_value;
1541
1542 mutex_lock(&codec->mutex);
1543
1544 ucontrol->value.integer.value[0] =
1545 snd_soc_dapm_get_pin_status(codec, pin);
1546
1547 mutex_unlock(&codec->mutex);
1548
1549 return 0;
1550}
1551EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1552
1553/**
1554 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1555 *
1556 * @kcontrol: mixer control
1557 * @ucontrol: Value
1558 */
1559int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1560 struct snd_ctl_elem_value *ucontrol)
1561{
1562 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1563 const char *pin = (const char *)kcontrol->private_value;
1564
1565 mutex_lock(&codec->mutex);
1566
1567 if (ucontrol->value.integer.value[0])
1568 snd_soc_dapm_enable_pin(codec, pin);
1569 else
1570 snd_soc_dapm_disable_pin(codec, pin);
1571
1572 snd_soc_dapm_sync(codec);
1573
1574 mutex_unlock(&codec->mutex);
1575
1576 return 0;
1577}
1578EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1579
2b97eabc
RP
1580/**
1581 * snd_soc_dapm_new_control - create new dapm control
1582 * @codec: audio codec
1583 * @widget: widget template
1584 *
1585 * Creates a new dapm control based upon the template.
1586 *
1587 * Returns 0 for success else error.
1588 */
1589int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1590 const struct snd_soc_dapm_widget *widget)
1591{
1592 struct snd_soc_dapm_widget *w;
1593
1594 if ((w = dapm_cnew_widget(widget)) == NULL)
1595 return -ENOMEM;
1596
1597 w->codec = codec;
1598 INIT_LIST_HEAD(&w->sources);
1599 INIT_LIST_HEAD(&w->sinks);
1600 INIT_LIST_HEAD(&w->list);
1601 list_add(&w->list, &codec->dapm_widgets);
1602
1603 /* machine layer set ups unconnected pins and insertions */
1604 w->connected = 1;
1605 return 0;
1606}
1607EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1608
4ba1327a
MB
1609/**
1610 * snd_soc_dapm_new_controls - create new dapm controls
1611 * @codec: audio codec
1612 * @widget: widget array
1613 * @num: number of widgets
1614 *
1615 * Creates new DAPM controls based upon the templates.
1616 *
1617 * Returns 0 for success else error.
1618 */
1619int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1620 const struct snd_soc_dapm_widget *widget,
1621 int num)
1622{
1623 int i, ret;
1624
1625 for (i = 0; i < num; i++) {
1626 ret = snd_soc_dapm_new_control(codec, widget);
b8b33cb5
MB
1627 if (ret < 0) {
1628 printk(KERN_ERR
1629 "ASoC: Failed to create DAPM control %s: %d\n",
1630 widget->name, ret);
4ba1327a 1631 return ret;
b8b33cb5 1632 }
4ba1327a
MB
1633 widget++;
1634 }
1635 return 0;
1636}
1637EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1638
1639
2b97eabc
RP
1640/**
1641 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1642 * @codec: audio codec
1643 * @stream: stream name
1644 * @event: stream event
1645 *
1646 * Sends a stream event to the dapm core. The core then makes any
1647 * necessary widget power changes.
1648 *
1649 * Returns 0 for success else error.
1650 */
1651int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1652 char *stream, int event)
1653{
1654 struct snd_soc_dapm_widget *w;
1655
11da21a7
SF
1656 if (stream == NULL)
1657 return 0;
1658
2b97eabc
RP
1659 mutex_lock(&codec->mutex);
1660 list_for_each_entry(w, &codec->dapm_widgets, list)
1661 {
1662 if (!w->sname)
1663 continue;
c1286b86
MB
1664 pr_debug("widget %s\n %s stream %s event %d\n",
1665 w->name, w->sname, stream, event);
2b97eabc
RP
1666 if (strstr(w->sname, stream)) {
1667 switch(event) {
1668 case SND_SOC_DAPM_STREAM_START:
1669 w->active = 1;
1670 break;
1671 case SND_SOC_DAPM_STREAM_STOP:
1672 w->active = 0;
1673 break;
1674 case SND_SOC_DAPM_STREAM_SUSPEND:
1675 if (w->active)
1676 w->suspend = 1;
1677 w->active = 0;
1678 break;
1679 case SND_SOC_DAPM_STREAM_RESUME:
1680 if (w->suspend) {
1681 w->active = 1;
1682 w->suspend = 0;
1683 }
1684 break;
1685 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1686 break;
1687 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1688 break;
1689 }
1690 }
1691 }
1692 mutex_unlock(&codec->mutex);
1693
1694 dapm_power_widgets(codec, event);
9bf8e7dd 1695 dump_dapm(codec, __func__);
2b97eabc
RP
1696 return 0;
1697}
1698EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1699
0b4d221b 1700/**
0be9898a 1701 * snd_soc_dapm_set_bias_level - set the bias level for the system
0b4d221b 1702 * @socdev: audio device
0be9898a 1703 * @level: level to configure
0b4d221b 1704 *
0be9898a 1705 * Configure the bias (power) levels for the SoC audio device.
0b4d221b
LG
1706 *
1707 * Returns 0 for success else error.
1708 */
0be9898a
MB
1709int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
1710 enum snd_soc_bias_level level)
0b4d221b 1711{
87506549 1712 struct snd_soc_card *card = socdev->card;
6627a653 1713 struct snd_soc_codec *codec = socdev->card->codec;
0be9898a 1714 int ret = 0;
0b4d221b 1715
87506549
MB
1716 if (card->set_bias_level)
1717 ret = card->set_bias_level(card, level);
0be9898a
MB
1718 if (ret == 0 && codec->set_bias_level)
1719 ret = codec->set_bias_level(codec, level);
1720
1721 return ret;
0b4d221b 1722}
0b4d221b 1723
2b97eabc 1724/**
a5302181 1725 * snd_soc_dapm_enable_pin - enable pin.
ac11a2b3 1726 * @codec: SoC codec
a5302181 1727 * @pin: pin name
2b97eabc 1728 *
a5302181
LG
1729 * Enables input/output pin and it's parents or children widgets iff there is
1730 * a valid audio route and active audio stream.
1731 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1732 * do any widget power switching.
2b97eabc 1733 */
1649923d 1734int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
2b97eabc 1735{
a5302181
LG
1736 return snd_soc_dapm_set_pin(codec, pin, 1);
1737}
1738EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2b97eabc 1739
a5302181
LG
1740/**
1741 * snd_soc_dapm_disable_pin - disable pin.
1742 * @codec: SoC codec
1743 * @pin: pin name
1744 *
1745 * Disables input/output pin and it's parents or children widgets.
1746 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1747 * do any widget power switching.
1748 */
1649923d 1749int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
a5302181
LG
1750{
1751 return snd_soc_dapm_set_pin(codec, pin, 0);
2b97eabc 1752}
a5302181 1753EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2b97eabc 1754
5817b52a
MB
1755/**
1756 * snd_soc_dapm_nc_pin - permanently disable pin.
1757 * @codec: SoC codec
1758 * @pin: pin name
1759 *
1760 * Marks the specified pin as being not connected, disabling it along
1761 * any parent or child widgets. At present this is identical to
1762 * snd_soc_dapm_disable_pin() but in future it will be extended to do
1763 * additional things such as disabling controls which only affect
1764 * paths through the pin.
1765 *
1766 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1767 * do any widget power switching.
1768 */
1649923d 1769int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
5817b52a
MB
1770{
1771 return snd_soc_dapm_set_pin(codec, pin, 0);
1772}
1773EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
1774
eeec12bf 1775/**
a5302181 1776 * snd_soc_dapm_get_pin_status - get audio pin status
eeec12bf 1777 * @codec: audio codec
a5302181 1778 * @pin: audio signal pin endpoint (or start point)
eeec12bf 1779 *
a5302181 1780 * Get audio pin status - connected or disconnected.
eeec12bf 1781 *
a5302181 1782 * Returns 1 for connected otherwise 0.
eeec12bf 1783 */
1649923d 1784int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
eeec12bf
GG
1785{
1786 struct snd_soc_dapm_widget *w;
1787
1788 list_for_each_entry(w, &codec->dapm_widgets, list) {
a5302181 1789 if (!strcmp(w->name, pin))
eeec12bf
GG
1790 return w->connected;
1791 }
1792
1793 return 0;
1794}
a5302181 1795EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
eeec12bf 1796
2b97eabc
RP
1797/**
1798 * snd_soc_dapm_free - free dapm resources
1799 * @socdev: SoC device
1800 *
1801 * Free all dapm widgets and resources.
1802 */
1803void snd_soc_dapm_free(struct snd_soc_device *socdev)
1804{
6627a653 1805 struct snd_soc_codec *codec = socdev->card->codec;
2b97eabc
RP
1806
1807 snd_soc_dapm_sys_remove(socdev->dev);
1808 dapm_free_widgets(codec);
1809}
1810EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1811
1812/* Module information */
d331124d 1813MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2b97eabc
RP
1814MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1815MODULE_LICENSE("GPL");