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