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