Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq
[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
74b8f955 15 * DACs/ADCs.
2b97eabc
RP
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>
20496ff3 40#include <linux/debugfs.h>
5a0e3ad6 41#include <linux/slab.h>
2b97eabc
RP
42#include <sound/core.h>
43#include <sound/pcm.h>
44#include <sound/pcm_params.h>
45#include <sound/soc-dapm.h>
46#include <sound/initval.h>
47
2b97eabc
RP
48/* dapm power sequences - make this per codec in the future */
49static int dapm_up_seq[] = {
38357ab2
MB
50 [snd_soc_dapm_pre] = 0,
51 [snd_soc_dapm_supply] = 1,
52 [snd_soc_dapm_micbias] = 2,
010ff262
MB
53 [snd_soc_dapm_aif_in] = 3,
54 [snd_soc_dapm_aif_out] = 3,
55 [snd_soc_dapm_mic] = 4,
56 [snd_soc_dapm_mux] = 5,
57 [snd_soc_dapm_value_mux] = 5,
58 [snd_soc_dapm_dac] = 6,
59 [snd_soc_dapm_mixer] = 7,
60 [snd_soc_dapm_mixer_named_ctl] = 7,
61 [snd_soc_dapm_pga] = 8,
62 [snd_soc_dapm_adc] = 9,
63 [snd_soc_dapm_hp] = 10,
64 [snd_soc_dapm_spk] = 10,
65 [snd_soc_dapm_post] = 11,
2b97eabc 66};
ca9c1aae 67
2b97eabc 68static int dapm_down_seq[] = {
38357ab2
MB
69 [snd_soc_dapm_pre] = 0,
70 [snd_soc_dapm_adc] = 1,
71 [snd_soc_dapm_hp] = 2,
1ca04065 72 [snd_soc_dapm_spk] = 2,
38357ab2
MB
73 [snd_soc_dapm_pga] = 4,
74 [snd_soc_dapm_mixer_named_ctl] = 5,
e3d4dabd
MB
75 [snd_soc_dapm_mixer] = 5,
76 [snd_soc_dapm_dac] = 6,
77 [snd_soc_dapm_mic] = 7,
78 [snd_soc_dapm_micbias] = 8,
79 [snd_soc_dapm_mux] = 9,
80 [snd_soc_dapm_value_mux] = 9,
010ff262
MB
81 [snd_soc_dapm_aif_in] = 10,
82 [snd_soc_dapm_aif_out] = 10,
83 [snd_soc_dapm_supply] = 11,
84 [snd_soc_dapm_post] = 12,
2b97eabc
RP
85};
86
12ef193d 87static void pop_wait(u32 pop_time)
15e4c72f
MB
88{
89 if (pop_time)
90 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
91}
92
12ef193d 93static void pop_dbg(u32 pop_time, const char *fmt, ...)
15e4c72f
MB
94{
95 va_list args;
96
97 va_start(args, fmt);
98
99 if (pop_time) {
100 vprintk(fmt, args);
15e4c72f
MB
101 }
102
103 va_end(args);
104}
105
2b97eabc 106/* create a new dapm widget */
88cb4290 107static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
2b97eabc
RP
108 const struct snd_soc_dapm_widget *_widget)
109{
88cb4290 110 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
2b97eabc
RP
111}
112
452c5eaa
MB
113/**
114 * snd_soc_dapm_set_bias_level - set the bias level for the system
115 * @socdev: audio device
116 * @level: level to configure
117 *
118 * Configure the bias (power) levels for the SoC audio device.
119 *
120 * Returns 0 for success else error.
121 */
122static int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
123 enum snd_soc_bias_level level)
124{
125 struct snd_soc_card *card = socdev->card;
126 struct snd_soc_codec *codec = socdev->card->codec;
127 int ret = 0;
128
f83fba8b
MB
129 switch (level) {
130 case SND_SOC_BIAS_ON:
131 dev_dbg(socdev->dev, "Setting full bias\n");
132 break;
133 case SND_SOC_BIAS_PREPARE:
134 dev_dbg(socdev->dev, "Setting bias prepare\n");
135 break;
136 case SND_SOC_BIAS_STANDBY:
137 dev_dbg(socdev->dev, "Setting standby bias\n");
138 break;
139 case SND_SOC_BIAS_OFF:
140 dev_dbg(socdev->dev, "Setting bias off\n");
141 break;
142 default:
143 dev_err(socdev->dev, "Setting invalid bias %d\n", level);
144 return -EINVAL;
145 }
146
452c5eaa
MB
147 if (card->set_bias_level)
148 ret = card->set_bias_level(card, level);
474e09ca
MB
149 if (ret == 0) {
150 if (codec->set_bias_level)
151 ret = codec->set_bias_level(codec, level);
152 else
153 codec->bias_level = level;
154 }
452c5eaa
MB
155
156 return ret;
157}
158
2b97eabc
RP
159/* set up initial codec paths */
160static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
161 struct snd_soc_dapm_path *p, int i)
162{
163 switch (w->id) {
164 case snd_soc_dapm_switch:
ca9c1aae
IM
165 case snd_soc_dapm_mixer:
166 case snd_soc_dapm_mixer_named_ctl: {
2b97eabc 167 int val;
4eaa9819
JS
168 struct soc_mixer_control *mc = (struct soc_mixer_control *)
169 w->kcontrols[i].private_value;
815ecf8d
JS
170 unsigned int reg = mc->reg;
171 unsigned int shift = mc->shift;
4eaa9819 172 int max = mc->max;
815ecf8d
JS
173 unsigned int mask = (1 << fls(max)) - 1;
174 unsigned int invert = mc->invert;
2b97eabc
RP
175
176 val = snd_soc_read(w->codec, reg);
177 val = (val >> shift) & mask;
178
179 if ((invert && !val) || (!invert && val))
180 p->connect = 1;
181 else
182 p->connect = 0;
183 }
184 break;
185 case snd_soc_dapm_mux: {
186 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
187 int val, item, bitmask;
188
f8ba0b7b 189 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2b97eabc
RP
190 ;
191 val = snd_soc_read(w->codec, e->reg);
192 item = (val >> e->shift_l) & (bitmask - 1);
193
194 p->connect = 0;
f8ba0b7b 195 for (i = 0; i < e->max; i++) {
2b97eabc
RP
196 if (!(strcmp(p->name, e->texts[i])) && item == i)
197 p->connect = 1;
198 }
199 }
200 break;
2e72f8e3 201 case snd_soc_dapm_value_mux: {
74155556 202 struct soc_enum *e = (struct soc_enum *)
2e72f8e3
PU
203 w->kcontrols[i].private_value;
204 int val, item;
205
206 val = snd_soc_read(w->codec, e->reg);
207 val = (val >> e->shift_l) & e->mask;
208 for (item = 0; item < e->max; item++) {
209 if (val == e->values[item])
210 break;
211 }
212
213 p->connect = 0;
214 for (i = 0; i < e->max; i++) {
215 if (!(strcmp(p->name, e->texts[i])) && item == i)
216 p->connect = 1;
217 }
218 }
219 break;
2b97eabc
RP
220 /* does not effect routing - always connected */
221 case snd_soc_dapm_pga:
222 case snd_soc_dapm_output:
223 case snd_soc_dapm_adc:
224 case snd_soc_dapm_input:
225 case snd_soc_dapm_dac:
226 case snd_soc_dapm_micbias:
227 case snd_soc_dapm_vmid:
246d0a17 228 case snd_soc_dapm_supply:
010ff262
MB
229 case snd_soc_dapm_aif_in:
230 case snd_soc_dapm_aif_out:
2b97eabc
RP
231 p->connect = 1;
232 break;
233 /* does effect routing - dynamically connected */
234 case snd_soc_dapm_hp:
235 case snd_soc_dapm_mic:
236 case snd_soc_dapm_spk:
237 case snd_soc_dapm_line:
238 case snd_soc_dapm_pre:
239 case snd_soc_dapm_post:
240 p->connect = 0;
241 break;
242 }
243}
244
74b8f955 245/* connect mux widget to its interconnecting audio paths */
2b97eabc
RP
246static int dapm_connect_mux(struct snd_soc_codec *codec,
247 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
248 struct snd_soc_dapm_path *path, const char *control_name,
249 const struct snd_kcontrol_new *kcontrol)
250{
251 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
252 int i;
253
f8ba0b7b 254 for (i = 0; i < e->max; i++) {
2b97eabc
RP
255 if (!(strcmp(control_name, e->texts[i]))) {
256 list_add(&path->list, &codec->dapm_paths);
257 list_add(&path->list_sink, &dest->sources);
258 list_add(&path->list_source, &src->sinks);
259 path->name = (char*)e->texts[i];
260 dapm_set_path_status(dest, path, 0);
261 return 0;
262 }
263 }
264
265 return -ENODEV;
266}
267
74b8f955 268/* connect mixer widget to its interconnecting audio paths */
2b97eabc
RP
269static int dapm_connect_mixer(struct snd_soc_codec *codec,
270 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
271 struct snd_soc_dapm_path *path, const char *control_name)
272{
273 int i;
274
275 /* search for mixer kcontrol */
276 for (i = 0; i < dest->num_kcontrols; i++) {
277 if (!strcmp(control_name, dest->kcontrols[i].name)) {
278 list_add(&path->list, &codec->dapm_paths);
279 list_add(&path->list_sink, &dest->sources);
280 list_add(&path->list_source, &src->sinks);
281 path->name = dest->kcontrols[i].name;
282 dapm_set_path_status(dest, path, i);
283 return 0;
284 }
285 }
286 return -ENODEV;
287}
288
289/* update dapm codec register bits */
290static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
291{
292 int change, power;
46f5822f 293 unsigned int old, new;
2b97eabc
RP
294 struct snd_soc_codec *codec = widget->codec;
295
296 /* check for valid widgets */
297 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
298 widget->id == snd_soc_dapm_output ||
299 widget->id == snd_soc_dapm_hp ||
300 widget->id == snd_soc_dapm_mic ||
301 widget->id == snd_soc_dapm_line ||
302 widget->id == snd_soc_dapm_spk)
303 return 0;
304
305 power = widget->power;
306 if (widget->invert)
307 power = (power ? 0:1);
308
309 old = snd_soc_read(codec, widget->reg);
310 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
311
312 change = old != new;
313 if (change) {
12ef193d
TK
314 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
315 widget->name, widget->power ? "on" : "off",
316 codec->pop_time);
12ef193d 317 pop_wait(codec->pop_time);
69224719 318 snd_soc_write(codec, widget->reg, new);
2b97eabc 319 }
c1286b86
MB
320 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
321 old, new, change);
2b97eabc
RP
322 return change;
323}
324
2b97eabc
RP
325/* create new dapm mixer control */
326static int dapm_new_mixer(struct snd_soc_codec *codec,
327 struct snd_soc_dapm_widget *w)
328{
329 int i, ret = 0;
219b93f5 330 size_t name_len;
2b97eabc
RP
331 struct snd_soc_dapm_path *path;
332
333 /* add kcontrol */
334 for (i = 0; i < w->num_kcontrols; i++) {
335
336 /* match name */
337 list_for_each_entry(path, &w->sources, list_sink) {
338
339 /* mixer/mux paths name must match control name */
340 if (path->name != (char*)w->kcontrols[i].name)
341 continue;
342
ca9c1aae
IM
343 /* add dapm control with long name.
344 * for dapm_mixer this is the concatenation of the
345 * mixer and kcontrol name.
346 * for dapm_mixer_named_ctl this is simply the
347 * kcontrol name.
348 */
349 name_len = strlen(w->kcontrols[i].name) + 1;
07495f3e 350 if (w->id != snd_soc_dapm_mixer_named_ctl)
ca9c1aae
IM
351 name_len += 1 + strlen(w->name);
352
219b93f5 353 path->long_name = kmalloc(name_len, GFP_KERNEL);
ca9c1aae 354
2b97eabc
RP
355 if (path->long_name == NULL)
356 return -ENOMEM;
357
ca9c1aae 358 switch (w->id) {
ca9c1aae
IM
359 default:
360 snprintf(path->long_name, name_len, "%s %s",
361 w->name, w->kcontrols[i].name);
07495f3e 362 break;
ca9c1aae
IM
363 case snd_soc_dapm_mixer_named_ctl:
364 snprintf(path->long_name, name_len, "%s",
365 w->kcontrols[i].name);
07495f3e 366 break;
ca9c1aae
IM
367 }
368
219b93f5
MB
369 path->long_name[name_len - 1] = '\0';
370
2b97eabc
RP
371 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
372 path->long_name);
373 ret = snd_ctl_add(codec->card, path->kcontrol);
374 if (ret < 0) {
6553e192
MB
375 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
376 path->long_name,
377 ret);
2b97eabc
RP
378 kfree(path->long_name);
379 path->long_name = NULL;
380 return ret;
381 }
382 }
383 }
384 return ret;
385}
386
387/* create new dapm mux control */
388static int dapm_new_mux(struct snd_soc_codec *codec,
389 struct snd_soc_dapm_widget *w)
390{
391 struct snd_soc_dapm_path *path = NULL;
392 struct snd_kcontrol *kcontrol;
393 int ret = 0;
394
395 if (!w->num_kcontrols) {
396 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
397 return -EINVAL;
398 }
399
400 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
401 ret = snd_ctl_add(codec->card, kcontrol);
402 if (ret < 0)
403 goto err;
404
405 list_for_each_entry(path, &w->sources, list_sink)
406 path->kcontrol = kcontrol;
407
408 return ret;
409
410err:
411 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
412 return ret;
413}
414
415/* create new dapm volume control */
416static int dapm_new_pga(struct snd_soc_codec *codec,
417 struct snd_soc_dapm_widget *w)
418{
a6c65736
MB
419 if (w->num_kcontrols)
420 pr_err("asoc: PGA controls not supported: '%s'\n", w->name);
2b97eabc 421
a6c65736 422 return 0;
2b97eabc
RP
423}
424
425/* reset 'walked' bit for each dapm path */
426static inline void dapm_clear_walk(struct snd_soc_codec *codec)
427{
428 struct snd_soc_dapm_path *p;
429
430 list_for_each_entry(p, &codec->dapm_paths, list)
431 p->walked = 0;
432}
433
9949788b
MB
434/* We implement power down on suspend by checking the power state of
435 * the ALSA card - when we are suspending the ALSA state for the card
436 * is set to D3.
437 */
438static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
439{
440 struct snd_soc_codec *codec = widget->codec;
441
442 switch (snd_power_get_state(codec->card)) {
443 case SNDRV_CTL_POWER_D3hot:
444 case SNDRV_CTL_POWER_D3cold:
1547aba9
MB
445 if (widget->ignore_suspend)
446 pr_debug("%s ignoring suspend\n", widget->name);
447 return widget->ignore_suspend;
9949788b
MB
448 default:
449 return 1;
450 }
451}
452
2b97eabc
RP
453/*
454 * Recursively check for a completed path to an active or physically connected
455 * output widget. Returns number of complete paths.
456 */
457static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
458{
459 struct snd_soc_dapm_path *path;
460 int con = 0;
461
246d0a17
MB
462 if (widget->id == snd_soc_dapm_supply)
463 return 0;
464
010ff262
MB
465 switch (widget->id) {
466 case snd_soc_dapm_adc:
467 case snd_soc_dapm_aif_out:
468 if (widget->active)
9949788b 469 return snd_soc_dapm_suspend_check(widget);
010ff262
MB
470 default:
471 break;
472 }
2b97eabc
RP
473
474 if (widget->connected) {
475 /* connected pin ? */
476 if (widget->id == snd_soc_dapm_output && !widget->ext)
9949788b 477 return snd_soc_dapm_suspend_check(widget);
2b97eabc
RP
478
479 /* connected jack or spk ? */
480 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
eaeae5d9 481 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
9949788b 482 return snd_soc_dapm_suspend_check(widget);
2b97eabc
RP
483 }
484
485 list_for_each_entry(path, &widget->sinks, list_source) {
486 if (path->walked)
487 continue;
488
489 if (path->sink && path->connect) {
490 path->walked = 1;
491 con += is_connected_output_ep(path->sink);
492 }
493 }
494
495 return con;
496}
497
498/*
499 * Recursively check for a completed path to an active or physically connected
500 * input widget. Returns number of complete paths.
501 */
502static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
503{
504 struct snd_soc_dapm_path *path;
505 int con = 0;
506
246d0a17
MB
507 if (widget->id == snd_soc_dapm_supply)
508 return 0;
509
2b97eabc 510 /* active stream ? */
010ff262
MB
511 switch (widget->id) {
512 case snd_soc_dapm_dac:
513 case snd_soc_dapm_aif_in:
514 if (widget->active)
9949788b 515 return snd_soc_dapm_suspend_check(widget);
010ff262
MB
516 default:
517 break;
518 }
2b97eabc
RP
519
520 if (widget->connected) {
521 /* connected pin ? */
522 if (widget->id == snd_soc_dapm_input && !widget->ext)
9949788b 523 return snd_soc_dapm_suspend_check(widget);
2b97eabc
RP
524
525 /* connected VMID/Bias for lower pops */
526 if (widget->id == snd_soc_dapm_vmid)
9949788b 527 return snd_soc_dapm_suspend_check(widget);
2b97eabc
RP
528
529 /* connected jack ? */
eaeae5d9
PU
530 if (widget->id == snd_soc_dapm_mic ||
531 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
9949788b 532 return snd_soc_dapm_suspend_check(widget);
2b97eabc
RP
533 }
534
535 list_for_each_entry(path, &widget->sources, list_sink) {
536 if (path->walked)
537 continue;
538
539 if (path->source && path->connect) {
540 path->walked = 1;
541 con += is_connected_input_ep(path->source);
542 }
543 }
544
545 return con;
546}
547
e2be2ccf
JN
548/*
549 * Handler for generic register modifier widget.
550 */
551int dapm_reg_event(struct snd_soc_dapm_widget *w,
552 struct snd_kcontrol *kcontrol, int event)
553{
554 unsigned int val;
555
556 if (SND_SOC_DAPM_EVENT_ON(event))
557 val = w->on_val;
558 else
559 val = w->off_val;
560
561 snd_soc_update_bits(w->codec, -(w->reg + 1),
562 w->mask << w->shift, val << w->shift);
563
564 return 0;
565}
11589418 566EXPORT_SYMBOL_GPL(dapm_reg_event);
e2be2ccf 567
025756ec
MB
568/* Standard power change method, used to apply power changes to most
569 * widgets.
570 */
571static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
572{
573 int ret;
574
575 /* call any power change event handlers */
576 if (w->event)
577 pr_debug("power %s event for %s flags %x\n",
578 w->power ? "on" : "off",
579 w->name, w->event_flags);
580
581 /* power up pre event */
582 if (w->power && w->event &&
583 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
584 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
585 if (ret < 0)
586 return ret;
587 }
588
589 /* power down pre event */
590 if (!w->power && w->event &&
591 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
592 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
593 if (ret < 0)
594 return ret;
595 }
596
025756ec
MB
597 dapm_update_bits(w);
598
025756ec
MB
599 /* power up post event */
600 if (w->power && w->event &&
601 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
602 ret = w->event(w,
603 NULL, SND_SOC_DAPM_POST_PMU);
604 if (ret < 0)
605 return ret;
606 }
607
608 /* power down post event */
609 if (!w->power && w->event &&
610 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
611 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
612 if (ret < 0)
613 return ret;
614 }
615
616 return 0;
617}
618
cd0f2d47
MB
619/* Generic check to see if a widget should be powered.
620 */
621static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
622{
623 int in, out;
624
625 in = is_connected_input_ep(w);
626 dapm_clear_walk(w->codec);
627 out = is_connected_output_ep(w);
628 dapm_clear_walk(w->codec);
629 return out != 0 && in != 0;
630}
631
6ea31b9f
MB
632/* Check to see if an ADC has power */
633static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
634{
635 int in;
636
637 if (w->active) {
638 in = is_connected_input_ep(w);
639 dapm_clear_walk(w->codec);
640 return in != 0;
641 } else {
642 return dapm_generic_check_power(w);
643 }
644}
645
646/* Check to see if a DAC has power */
647static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
648{
649 int out;
650
651 if (w->active) {
652 out = is_connected_output_ep(w);
653 dapm_clear_walk(w->codec);
654 return out != 0;
655 } else {
656 return dapm_generic_check_power(w);
657 }
658}
659
246d0a17
MB
660/* Check to see if a power supply is needed */
661static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
662{
663 struct snd_soc_dapm_path *path;
664 int power = 0;
665
666 /* Check if one of our outputs is connected */
667 list_for_each_entry(path, &w->sinks, list_source) {
215edda3
MB
668 if (path->connected &&
669 !path->connected(path->source, path->sink))
670 continue;
671
246d0a17
MB
672 if (path->sink && path->sink->power_check &&
673 path->sink->power_check(path->sink)) {
674 power = 1;
675 break;
676 }
677 }
678
679 dapm_clear_walk(w->codec);
680
681 return power;
682}
683
38357ab2
MB
684static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
685 struct snd_soc_dapm_widget *b,
686 int sort[])
42aa3418 687{
d207c68d
MB
688 if (a->codec != b->codec)
689 return (unsigned long)a - (unsigned long)b;
38357ab2
MB
690 if (sort[a->id] != sort[b->id])
691 return sort[a->id] - sort[b->id];
b22ead2a
MB
692 if (a->reg != b->reg)
693 return a->reg - b->reg;
42aa3418 694
38357ab2
MB
695 return 0;
696}
42aa3418 697
38357ab2
MB
698/* Insert a widget in order into a DAPM power sequence. */
699static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
700 struct list_head *list,
701 int sort[])
702{
703 struct snd_soc_dapm_widget *w;
704
705 list_for_each_entry(w, list, power_list)
706 if (dapm_seq_compare(new_widget, w, sort) < 0) {
707 list_add_tail(&new_widget->power_list, &w->power_list);
708 return;
709 }
710
711 list_add_tail(&new_widget->power_list, list);
712}
713
b22ead2a
MB
714/* Apply the coalesced changes from a DAPM sequence */
715static void dapm_seq_run_coalesced(struct snd_soc_codec *codec,
716 struct list_head *pending)
163cac06
MB
717{
718 struct snd_soc_dapm_widget *w;
81628103 719 int reg, power, ret;
b22ead2a
MB
720 unsigned int value = 0;
721 unsigned int mask = 0;
722 unsigned int cur_mask;
723
724 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
725 power_list)->reg;
726
727 list_for_each_entry(w, pending, power_list) {
728 cur_mask = 1 << w->shift;
729 BUG_ON(reg != w->reg);
730
731 if (w->invert)
732 power = !w->power;
733 else
734 power = w->power;
735
736 mask |= cur_mask;
737 if (power)
738 value |= cur_mask;
739
740 pop_dbg(codec->pop_time,
741 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
742 w->name, reg, value, mask);
81628103
MB
743
744 /* power up pre event */
745 if (w->power && w->event &&
746 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
747 pop_dbg(codec->pop_time, "pop test : %s PRE_PMU\n",
748 w->name);
749 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
42aa3418 750 if (ret < 0)
81628103
MB
751 pr_err("%s: pre event failed: %d\n",
752 w->name, ret);
753 }
754
755 /* power down pre event */
756 if (!w->power && w->event &&
757 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
758 pop_dbg(codec->pop_time, "pop test : %s PRE_PMD\n",
759 w->name);
760 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
42aa3418 761 if (ret < 0)
81628103
MB
762 pr_err("%s: pre event failed: %d\n",
763 w->name, ret);
42aa3418 764 }
81628103
MB
765 }
766
767 if (reg >= 0) {
768 pop_dbg(codec->pop_time,
769 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
770 value, mask, reg, codec->pop_time);
771 pop_wait(codec->pop_time);
772 snd_soc_update_bits(codec, reg, mask, value);
b22ead2a
MB
773 }
774
81628103
MB
775 list_for_each_entry(w, pending, power_list) {
776 /* power up post event */
777 if (w->power && w->event &&
778 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
779 pop_dbg(codec->pop_time, "pop test : %s POST_PMU\n",
780 w->name);
42aa3418
MB
781 ret = w->event(w,
782 NULL, SND_SOC_DAPM_POST_PMU);
783 if (ret < 0)
81628103
MB
784 pr_err("%s: post event failed: %d\n",
785 w->name, ret);
786 }
787
788 /* power down post event */
789 if (!w->power && w->event &&
790 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
791 pop_dbg(codec->pop_time, "pop test : %s POST_PMD\n",
792 w->name);
793 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
42aa3418 794 if (ret < 0)
81628103
MB
795 pr_err("%s: post event failed: %d\n",
796 w->name, ret);
42aa3418 797 }
81628103 798 }
b22ead2a 799}
42aa3418 800
b22ead2a
MB
801/* Apply a DAPM power sequence.
802 *
803 * We walk over a pre-sorted list of widgets to apply power to. In
804 * order to minimise the number of writes to the device required
805 * multiple widgets will be updated in a single write where possible.
806 * Currently anything that requires more than a single write is not
807 * handled.
808 */
809static void dapm_seq_run(struct snd_soc_codec *codec, struct list_head *list,
810 int event, int sort[])
811{
812 struct snd_soc_dapm_widget *w, *n;
813 LIST_HEAD(pending);
814 int cur_sort = -1;
815 int cur_reg = SND_SOC_NOPM;
163cac06
MB
816 int ret;
817
b22ead2a
MB
818 list_for_each_entry_safe(w, n, list, power_list) {
819 ret = 0;
820
821 /* Do we need to apply any queued changes? */
822 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
823 if (!list_empty(&pending))
824 dapm_seq_run_coalesced(codec, &pending);
825
826 INIT_LIST_HEAD(&pending);
827 cur_sort = -1;
828 cur_reg = SND_SOC_NOPM;
829 }
830
163cac06
MB
831 switch (w->id) {
832 case snd_soc_dapm_pre:
833 if (!w->event)
b22ead2a
MB
834 list_for_each_entry_safe_continue(w, n, list,
835 power_list);
163cac06 836
b22ead2a 837 if (event == SND_SOC_DAPM_STREAM_START)
163cac06
MB
838 ret = w->event(w,
839 NULL, SND_SOC_DAPM_PRE_PMU);
b22ead2a 840 else if (event == SND_SOC_DAPM_STREAM_STOP)
163cac06
MB
841 ret = w->event(w,
842 NULL, SND_SOC_DAPM_PRE_PMD);
163cac06
MB
843 break;
844
845 case snd_soc_dapm_post:
846 if (!w->event)
b22ead2a
MB
847 list_for_each_entry_safe_continue(w, n, list,
848 power_list);
163cac06 849
b22ead2a 850 if (event == SND_SOC_DAPM_STREAM_START)
163cac06
MB
851 ret = w->event(w,
852 NULL, SND_SOC_DAPM_POST_PMU);
b22ead2a 853 else if (event == SND_SOC_DAPM_STREAM_STOP)
163cac06
MB
854 ret = w->event(w,
855 NULL, SND_SOC_DAPM_POST_PMD);
163cac06
MB
856 break;
857
b22ead2a
MB
858 case snd_soc_dapm_input:
859 case snd_soc_dapm_output:
860 case snd_soc_dapm_hp:
861 case snd_soc_dapm_mic:
862 case snd_soc_dapm_line:
863 case snd_soc_dapm_spk:
864 /* No register support currently */
163cac06 865 ret = dapm_generic_apply_power(w);
163cac06 866 break;
b22ead2a
MB
867
868 default:
81628103
MB
869 /* Queue it up for application */
870 cur_sort = sort[w->id];
871 cur_reg = w->reg;
872 list_move(&w->power_list, &pending);
873 break;
163cac06 874 }
b22ead2a
MB
875
876 if (ret < 0)
877 pr_err("Failed to apply widget power: %d\n",
878 ret);
6ea31b9f 879 }
b22ead2a
MB
880
881 if (!list_empty(&pending))
882 dapm_seq_run_coalesced(codec, &pending);
42aa3418
MB
883}
884
2b97eabc
RP
885/*
886 * Scan each dapm widget for complete audio path.
887 * A complete path is a route that has valid endpoints i.e.:-
888 *
889 * o DAC to output pin.
890 * o Input Pin to ADC.
891 * o Input pin to Output pin (bypass, sidetone)
892 * o DAC to ADC (loopback).
893 */
d9c96cf3 894static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
2b97eabc 895{
452c5eaa 896 struct snd_soc_device *socdev = codec->socdev;
2b97eabc 897 struct snd_soc_dapm_widget *w;
291f3bbc
MB
898 LIST_HEAD(up_list);
899 LIST_HEAD(down_list);
6d3ddc81 900 int ret = 0;
38357ab2 901 int power;
452c5eaa 902 int sys_power = 0;
6d3ddc81 903
6d3ddc81
MB
904 /* Check which widgets we need to power and store them in
905 * lists indicating if they should be powered up or down.
906 */
907 list_for_each_entry(w, &codec->dapm_widgets, list) {
908 switch (w->id) {
909 case snd_soc_dapm_pre:
291f3bbc 910 dapm_seq_insert(w, &down_list, dapm_down_seq);
6d3ddc81
MB
911 break;
912 case snd_soc_dapm_post:
291f3bbc 913 dapm_seq_insert(w, &up_list, dapm_up_seq);
6d3ddc81
MB
914 break;
915
916 default:
917 if (!w->power_check)
918 continue;
919
9949788b 920 if (!w->force)
50b6bce5 921 power = w->power_check(w);
9949788b
MB
922 else
923 power = 1;
924 if (power)
925 sys_power = 1;
452c5eaa 926
6d3ddc81
MB
927 if (w->power == power)
928 continue;
929
930 if (power)
291f3bbc 931 dapm_seq_insert(w, &up_list, dapm_up_seq);
6d3ddc81 932 else
291f3bbc 933 dapm_seq_insert(w, &down_list, dapm_down_seq);
6d3ddc81
MB
934
935 w->power = power;
936 break;
937 }
2b97eabc
RP
938 }
939
b14b76a5
MB
940 /* If there are no DAPM widgets then try to figure out power from the
941 * event type.
942 */
943 if (list_empty(&codec->dapm_widgets)) {
944 switch (event) {
945 case SND_SOC_DAPM_STREAM_START:
946 case SND_SOC_DAPM_STREAM_RESUME:
947 sys_power = 1;
948 break;
50b6bce5
MB
949 case SND_SOC_DAPM_STREAM_SUSPEND:
950 sys_power = 0;
951 break;
b14b76a5 952 case SND_SOC_DAPM_STREAM_NOP:
a96ca338
MB
953 switch (codec->bias_level) {
954 case SND_SOC_BIAS_STANDBY:
955 case SND_SOC_BIAS_OFF:
956 sys_power = 0;
957 break;
958 default:
959 sys_power = 1;
960 break;
961 }
50b6bce5 962 break;
b14b76a5
MB
963 default:
964 break;
965 }
966 }
967
a96ca338
MB
968 if (sys_power && codec->bias_level == SND_SOC_BIAS_OFF) {
969 ret = snd_soc_dapm_set_bias_level(socdev,
970 SND_SOC_BIAS_STANDBY);
971 if (ret != 0)
972 pr_err("Failed to turn on bias: %d\n", ret);
973 }
974
452c5eaa
MB
975 /* If we're changing to all on or all off then prepare */
976 if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
977 (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
978 ret = snd_soc_dapm_set_bias_level(socdev,
979 SND_SOC_BIAS_PREPARE);
980 if (ret != 0)
981 pr_err("Failed to prepare bias: %d\n", ret);
982 }
983
6d3ddc81 984 /* Power down widgets first; try to avoid amplifying pops. */
291f3bbc 985 dapm_seq_run(codec, &down_list, event, dapm_down_seq);
2b97eabc 986
6d3ddc81 987 /* Now power up. */
291f3bbc 988 dapm_seq_run(codec, &up_list, event, dapm_up_seq);
2b97eabc 989
452c5eaa
MB
990 /* If we just powered the last thing off drop to standby bias */
991 if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
992 ret = snd_soc_dapm_set_bias_level(socdev,
993 SND_SOC_BIAS_STANDBY);
994 if (ret != 0)
995 pr_err("Failed to apply standby bias: %d\n", ret);
996 }
997
a96ca338
MB
998 /* If we're in standby and can support bias off then do that */
999 if (codec->bias_level == SND_SOC_BIAS_STANDBY &&
1000 codec->idle_bias_off) {
1001 ret = snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
1002 if (ret != 0)
1003 pr_err("Failed to turn off bias: %d\n", ret);
1004 }
1005
452c5eaa
MB
1006 /* If we just powered up then move to active bias */
1007 if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
1008 ret = snd_soc_dapm_set_bias_level(socdev,
1009 SND_SOC_BIAS_ON);
1010 if (ret != 0)
1011 pr_err("Failed to apply active bias: %d\n", ret);
1012 }
1013
cb507e7e
MB
1014 pop_dbg(codec->pop_time, "DAPM sequencing finished, waiting %dms\n",
1015 codec->pop_time);
69224719 1016 pop_wait(codec->pop_time);
cb507e7e 1017
42aa3418 1018 return 0;
2b97eabc
RP
1019}
1020
79fb9387
MB
1021#ifdef CONFIG_DEBUG_FS
1022static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1023{
1024 file->private_data = inode->i_private;
1025 return 0;
1026}
1027
1028static ssize_t dapm_widget_power_read_file(struct file *file,
1029 char __user *user_buf,
1030 size_t count, loff_t *ppos)
1031{
1032 struct snd_soc_dapm_widget *w = file->private_data;
1033 char *buf;
1034 int in, out;
1035 ssize_t ret;
1036 struct snd_soc_dapm_path *p = NULL;
1037
1038 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1039 if (!buf)
1040 return -ENOMEM;
1041
1042 in = is_connected_input_ep(w);
1043 dapm_clear_walk(w->codec);
1044 out = is_connected_output_ep(w);
1045 dapm_clear_walk(w->codec);
1046
d033c36a 1047 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
79fb9387
MB
1048 w->name, w->power ? "On" : "Off", in, out);
1049
d033c36a
MB
1050 if (w->reg >= 0)
1051 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1052 " - R%d(0x%x) bit %d",
1053 w->reg, w->reg, w->shift);
1054
1055 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1056
3eef08ba
MB
1057 if (w->sname)
1058 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1059 w->sname,
1060 w->active ? "active" : "inactive");
79fb9387
MB
1061
1062 list_for_each_entry(p, &w->sources, list_sink) {
215edda3
MB
1063 if (p->connected && !p->connected(w, p->sink))
1064 continue;
1065
79fb9387
MB
1066 if (p->connect)
1067 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1068 " in %s %s\n",
1069 p->name ? p->name : "static",
1070 p->source->name);
1071 }
1072 list_for_each_entry(p, &w->sinks, list_source) {
215edda3
MB
1073 if (p->connected && !p->connected(w, p->sink))
1074 continue;
1075
79fb9387
MB
1076 if (p->connect)
1077 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1078 " out %s %s\n",
1079 p->name ? p->name : "static",
1080 p->sink->name);
1081 }
1082
1083 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1084
1085 kfree(buf);
1086 return ret;
1087}
1088
1089static const struct file_operations dapm_widget_power_fops = {
1090 .open = dapm_widget_power_open_file,
1091 .read = dapm_widget_power_read_file,
6038f373 1092 .llseek = default_llseek,
79fb9387
MB
1093};
1094
1095void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1096{
1097 struct snd_soc_dapm_widget *w;
1098 struct dentry *d;
1099
1100 if (!codec->debugfs_dapm)
1101 return;
1102
1103 list_for_each_entry(w, &codec->dapm_widgets, list) {
1104 if (!w->name)
1105 continue;
1106
1107 d = debugfs_create_file(w->name, 0444,
1108 codec->debugfs_dapm, w,
1109 &dapm_widget_power_fops);
1110 if (!d)
1111 printk(KERN_WARNING
1112 "ASoC: Failed to create %s debugfs file\n",
1113 w->name);
1114 }
1115}
1116#else
1117void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1118{
1119}
1120#endif
1121
2b97eabc 1122/* test and update the power status of a mux widget */
d9c96cf3 1123static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
3a65577d
MB
1124 struct snd_kcontrol *kcontrol, int change,
1125 int mux, struct soc_enum *e)
2b97eabc
RP
1126{
1127 struct snd_soc_dapm_path *path;
1128 int found = 0;
1129
eff317d0
PU
1130 if (widget->id != snd_soc_dapm_mux &&
1131 widget->id != snd_soc_dapm_value_mux)
2b97eabc
RP
1132 return -ENODEV;
1133
3a65577d 1134 if (!change)
2b97eabc
RP
1135 return 0;
1136
1137 /* find dapm widget path assoc with kcontrol */
1138 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1139 if (path->kcontrol != kcontrol)
1140 continue;
1141
cb01e2b9 1142 if (!path->name || !e->texts[mux])
2b97eabc
RP
1143 continue;
1144
1145 found = 1;
1146 /* we now need to match the string in the enum to the path */
cb01e2b9 1147 if (!(strcmp(path->name, e->texts[mux])))
2b97eabc
RP
1148 path->connect = 1; /* new connection */
1149 else
1150 path->connect = 0; /* old connection must be powered down */
1151 }
1152
b91b8fa0 1153 if (found)
2b97eabc
RP
1154 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
1155
1156 return 0;
1157}
2b97eabc 1158
1b075e3f 1159/* test and update the power status of a mixer or switch widget */
d9c96cf3 1160static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
283375ce 1161 struct snd_kcontrol *kcontrol, int connect)
2b97eabc
RP
1162{
1163 struct snd_soc_dapm_path *path;
1164 int found = 0;
1165
1b075e3f 1166 if (widget->id != snd_soc_dapm_mixer &&
ca9c1aae 1167 widget->id != snd_soc_dapm_mixer_named_ctl &&
1b075e3f 1168 widget->id != snd_soc_dapm_switch)
2b97eabc
RP
1169 return -ENODEV;
1170
2b97eabc
RP
1171 /* find dapm widget path assoc with kcontrol */
1172 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1173 if (path->kcontrol != kcontrol)
1174 continue;
1175
1176 /* found, now check type */
1177 found = 1;
283375ce 1178 path->connect = connect;
2b97eabc
RP
1179 break;
1180 }
1181
b91b8fa0 1182 if (found)
2b97eabc
RP
1183 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
1184
1185 return 0;
1186}
2b97eabc
RP
1187
1188/* show dapm widget status in sys fs */
1189static ssize_t dapm_widget_show(struct device *dev,
1190 struct device_attribute *attr, char *buf)
1191{
1192 struct snd_soc_device *devdata = dev_get_drvdata(dev);
6627a653 1193 struct snd_soc_codec *codec = devdata->card->codec;
2b97eabc
RP
1194 struct snd_soc_dapm_widget *w;
1195 int count = 0;
1196 char *state = "not set";
1197
1198 list_for_each_entry(w, &codec->dapm_widgets, list) {
1199
1200 /* only display widgets that burnm power */
1201 switch (w->id) {
1202 case snd_soc_dapm_hp:
1203 case snd_soc_dapm_mic:
1204 case snd_soc_dapm_spk:
1205 case snd_soc_dapm_line:
1206 case snd_soc_dapm_micbias:
1207 case snd_soc_dapm_dac:
1208 case snd_soc_dapm_adc:
1209 case snd_soc_dapm_pga:
1210 case snd_soc_dapm_mixer:
ca9c1aae 1211 case snd_soc_dapm_mixer_named_ctl:
246d0a17 1212 case snd_soc_dapm_supply:
2b97eabc
RP
1213 if (w->name)
1214 count += sprintf(buf + count, "%s: %s\n",
1215 w->name, w->power ? "On":"Off");
1216 break;
1217 default:
1218 break;
1219 }
1220 }
1221
0be9898a
MB
1222 switch (codec->bias_level) {
1223 case SND_SOC_BIAS_ON:
1224 state = "On";
2b97eabc 1225 break;
0be9898a
MB
1226 case SND_SOC_BIAS_PREPARE:
1227 state = "Prepare";
2b97eabc 1228 break;
0be9898a
MB
1229 case SND_SOC_BIAS_STANDBY:
1230 state = "Standby";
2b97eabc 1231 break;
0be9898a
MB
1232 case SND_SOC_BIAS_OFF:
1233 state = "Off";
2b97eabc
RP
1234 break;
1235 }
1236 count += sprintf(buf + count, "PM State: %s\n", state);
1237
1238 return count;
1239}
1240
1241static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1242
1243int snd_soc_dapm_sys_add(struct device *dev)
1244{
12ef193d 1245 return device_create_file(dev, &dev_attr_dapm_widget);
2b97eabc
RP
1246}
1247
1248static void snd_soc_dapm_sys_remove(struct device *dev)
1249{
aef90843 1250 device_remove_file(dev, &dev_attr_dapm_widget);
2b97eabc
RP
1251}
1252
1253/* free all dapm widgets and resources */
d9c96cf3 1254static void dapm_free_widgets(struct snd_soc_codec *codec)
2b97eabc
RP
1255{
1256 struct snd_soc_dapm_widget *w, *next_w;
1257 struct snd_soc_dapm_path *p, *next_p;
1258
1259 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
1260 list_del(&w->list);
1261 kfree(w);
1262 }
1263
1264 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
1265 list_del(&p->list);
1266 kfree(p->long_name);
1267 kfree(p);
1268 }
1269}
1270
a5302181 1271static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
1649923d 1272 const char *pin, int status)
a5302181
LG
1273{
1274 struct snd_soc_dapm_widget *w;
1275
1276 list_for_each_entry(w, &codec->dapm_widgets, list) {
1277 if (!strcmp(w->name, pin)) {
c1286b86 1278 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
a5302181 1279 w->connected = status;
5b9e87cc
MB
1280 /* Allow disabling of forced pins */
1281 if (status == 0)
1282 w->force = 0;
a5302181
LG
1283 return 0;
1284 }
1285 }
1286
c1286b86 1287 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
a5302181
LG
1288 return -EINVAL;
1289}
1290
2b97eabc 1291/**
a5302181 1292 * snd_soc_dapm_sync - scan and power dapm paths
2b97eabc
RP
1293 * @codec: audio codec
1294 *
1295 * Walks all dapm audio paths and powers widgets according to their
1296 * stream or path usage.
1297 *
1298 * Returns 0 for success.
1299 */
a5302181 1300int snd_soc_dapm_sync(struct snd_soc_codec *codec)
2b97eabc 1301{
b91b8fa0 1302 return dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
2b97eabc 1303}
a5302181 1304EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2b97eabc 1305
105f1c28 1306static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
215edda3 1307 const struct snd_soc_dapm_route *route)
2b97eabc
RP
1308{
1309 struct snd_soc_dapm_path *path;
1310 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
215edda3
MB
1311 const char *sink = route->sink;
1312 const char *control = route->control;
1313 const char *source = route->source;
2b97eabc
RP
1314 int ret = 0;
1315
1316 /* find src and dest widgets */
1317 list_for_each_entry(w, &codec->dapm_widgets, list) {
1318
1319 if (!wsink && !(strcmp(w->name, sink))) {
1320 wsink = w;
1321 continue;
1322 }
1323 if (!wsource && !(strcmp(w->name, source))) {
1324 wsource = w;
1325 }
1326 }
1327
1328 if (wsource == NULL || wsink == NULL)
1329 return -ENODEV;
1330
1331 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1332 if (!path)
1333 return -ENOMEM;
1334
1335 path->source = wsource;
1336 path->sink = wsink;
215edda3 1337 path->connected = route->connected;
2b97eabc
RP
1338 INIT_LIST_HEAD(&path->list);
1339 INIT_LIST_HEAD(&path->list_source);
1340 INIT_LIST_HEAD(&path->list_sink);
1341
1342 /* check for external widgets */
1343 if (wsink->id == snd_soc_dapm_input) {
1344 if (wsource->id == snd_soc_dapm_micbias ||
1345 wsource->id == snd_soc_dapm_mic ||
087d53ab
RC
1346 wsource->id == snd_soc_dapm_line ||
1347 wsource->id == snd_soc_dapm_output)
2b97eabc
RP
1348 wsink->ext = 1;
1349 }
1350 if (wsource->id == snd_soc_dapm_output) {
1351 if (wsink->id == snd_soc_dapm_spk ||
1352 wsink->id == snd_soc_dapm_hp ||
1e39221e
SF
1353 wsink->id == snd_soc_dapm_line ||
1354 wsink->id == snd_soc_dapm_input)
2b97eabc
RP
1355 wsource->ext = 1;
1356 }
1357
1358 /* connect static paths */
1359 if (control == NULL) {
1360 list_add(&path->list, &codec->dapm_paths);
1361 list_add(&path->list_sink, &wsink->sources);
1362 list_add(&path->list_source, &wsource->sinks);
1363 path->connect = 1;
1364 return 0;
1365 }
1366
1367 /* connect dynamic paths */
1368 switch(wsink->id) {
1369 case snd_soc_dapm_adc:
1370 case snd_soc_dapm_dac:
1371 case snd_soc_dapm_pga:
1372 case snd_soc_dapm_input:
1373 case snd_soc_dapm_output:
1374 case snd_soc_dapm_micbias:
1375 case snd_soc_dapm_vmid:
1376 case snd_soc_dapm_pre:
1377 case snd_soc_dapm_post:
246d0a17 1378 case snd_soc_dapm_supply:
010ff262
MB
1379 case snd_soc_dapm_aif_in:
1380 case snd_soc_dapm_aif_out:
2b97eabc
RP
1381 list_add(&path->list, &codec->dapm_paths);
1382 list_add(&path->list_sink, &wsink->sources);
1383 list_add(&path->list_source, &wsource->sinks);
1384 path->connect = 1;
1385 return 0;
1386 case snd_soc_dapm_mux:
74155556 1387 case snd_soc_dapm_value_mux:
2b97eabc
RP
1388 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1389 &wsink->kcontrols[0]);
1390 if (ret != 0)
1391 goto err;
1392 break;
1393 case snd_soc_dapm_switch:
1394 case snd_soc_dapm_mixer:
ca9c1aae 1395 case snd_soc_dapm_mixer_named_ctl:
2b97eabc
RP
1396 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1397 if (ret != 0)
1398 goto err;
1399 break;
1400 case snd_soc_dapm_hp:
1401 case snd_soc_dapm_mic:
1402 case snd_soc_dapm_line:
1403 case snd_soc_dapm_spk:
1404 list_add(&path->list, &codec->dapm_paths);
1405 list_add(&path->list_sink, &wsink->sources);
1406 list_add(&path->list_source, &wsource->sinks);
1407 path->connect = 0;
1408 return 0;
1409 }
1410 return 0;
1411
1412err:
1413 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1414 control, sink);
1415 kfree(path);
1416 return ret;
1417}
105f1c28 1418
105f1c28
MB
1419/**
1420 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1421 * @codec: codec
1422 * @route: audio routes
1423 * @num: number of routes
1424 *
1425 * Connects 2 dapm widgets together via a named audio path. The sink is
1426 * the widget receiving the audio signal, whilst the source is the sender
1427 * of the audio signal.
1428 *
1429 * Returns 0 for success else error. On error all resources can be freed
1430 * with a call to snd_soc_card_free().
1431 */
1432int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1433 const struct snd_soc_dapm_route *route, int num)
1434{
1435 int i, ret;
1436
1437 for (i = 0; i < num; i++) {
215edda3 1438 ret = snd_soc_dapm_add_route(codec, route);
105f1c28
MB
1439 if (ret < 0) {
1440 printk(KERN_ERR "Failed to add route %s->%s\n",
1441 route->source,
1442 route->sink);
1443 return ret;
1444 }
1445 route++;
1446 }
1447
1448 return 0;
1449}
1450EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1451
2b97eabc
RP
1452/**
1453 * snd_soc_dapm_new_widgets - add new dapm widgets
1454 * @codec: audio codec
1455 *
1456 * Checks the codec for any new dapm widgets and creates them if found.
1457 *
1458 * Returns 0 for success.
1459 */
1460int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1461{
1462 struct snd_soc_dapm_widget *w;
1463
2b97eabc
RP
1464 list_for_each_entry(w, &codec->dapm_widgets, list)
1465 {
1466 if (w->new)
1467 continue;
1468
1469 switch(w->id) {
1470 case snd_soc_dapm_switch:
1471 case snd_soc_dapm_mixer:
ca9c1aae 1472 case snd_soc_dapm_mixer_named_ctl:
b75576d7 1473 w->power_check = dapm_generic_check_power;
2b97eabc
RP
1474 dapm_new_mixer(codec, w);
1475 break;
1476 case snd_soc_dapm_mux:
2e72f8e3 1477 case snd_soc_dapm_value_mux:
b75576d7 1478 w->power_check = dapm_generic_check_power;
2b97eabc
RP
1479 dapm_new_mux(codec, w);
1480 break;
1481 case snd_soc_dapm_adc:
010ff262 1482 case snd_soc_dapm_aif_out:
b75576d7
MB
1483 w->power_check = dapm_adc_check_power;
1484 break;
2b97eabc 1485 case snd_soc_dapm_dac:
010ff262 1486 case snd_soc_dapm_aif_in:
b75576d7
MB
1487 w->power_check = dapm_dac_check_power;
1488 break;
2b97eabc 1489 case snd_soc_dapm_pga:
b75576d7 1490 w->power_check = dapm_generic_check_power;
2b97eabc
RP
1491 dapm_new_pga(codec, w);
1492 break;
1493 case snd_soc_dapm_input:
1494 case snd_soc_dapm_output:
1495 case snd_soc_dapm_micbias:
1496 case snd_soc_dapm_spk:
1497 case snd_soc_dapm_hp:
1498 case snd_soc_dapm_mic:
1499 case snd_soc_dapm_line:
b75576d7
MB
1500 w->power_check = dapm_generic_check_power;
1501 break;
246d0a17
MB
1502 case snd_soc_dapm_supply:
1503 w->power_check = dapm_supply_check_power;
2b97eabc
RP
1504 case snd_soc_dapm_vmid:
1505 case snd_soc_dapm_pre:
1506 case snd_soc_dapm_post:
1507 break;
1508 }
1509 w->new = 1;
1510 }
1511
1512 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
2b97eabc
RP
1513 return 0;
1514}
1515EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1516
1517/**
1518 * snd_soc_dapm_get_volsw - dapm mixer get callback
1519 * @kcontrol: mixer control
ac11a2b3 1520 * @ucontrol: control element information
2b97eabc
RP
1521 *
1522 * Callback to get the value of a dapm mixer control.
1523 *
1524 * Returns 0 for success.
1525 */
1526int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1527 struct snd_ctl_elem_value *ucontrol)
1528{
1529 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
4eaa9819
JS
1530 struct soc_mixer_control *mc =
1531 (struct soc_mixer_control *)kcontrol->private_value;
815ecf8d
JS
1532 unsigned int reg = mc->reg;
1533 unsigned int shift = mc->shift;
1534 unsigned int rshift = mc->rshift;
4eaa9819 1535 int max = mc->max;
815ecf8d
JS
1536 unsigned int invert = mc->invert;
1537 unsigned int mask = (1 << fls(max)) - 1;
2b97eabc 1538
2b97eabc
RP
1539 ucontrol->value.integer.value[0] =
1540 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1541 if (shift != rshift)
1542 ucontrol->value.integer.value[1] =
1543 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1544 if (invert) {
1545 ucontrol->value.integer.value[0] =
a7a4ac86 1546 max - ucontrol->value.integer.value[0];
2b97eabc
RP
1547 if (shift != rshift)
1548 ucontrol->value.integer.value[1] =
a7a4ac86 1549 max - ucontrol->value.integer.value[1];
2b97eabc
RP
1550 }
1551
1552 return 0;
1553}
1554EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1555
1556/**
1557 * snd_soc_dapm_put_volsw - dapm mixer set callback
1558 * @kcontrol: mixer control
ac11a2b3 1559 * @ucontrol: control element information
2b97eabc
RP
1560 *
1561 * Callback to set the value of a dapm mixer control.
1562 *
1563 * Returns 0 for success.
1564 */
1565int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1566 struct snd_ctl_elem_value *ucontrol)
1567{
1568 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
4eaa9819
JS
1569 struct soc_mixer_control *mc =
1570 (struct soc_mixer_control *)kcontrol->private_value;
815ecf8d
JS
1571 unsigned int reg = mc->reg;
1572 unsigned int shift = mc->shift;
1573 unsigned int rshift = mc->rshift;
4eaa9819 1574 int max = mc->max;
815ecf8d
JS
1575 unsigned int mask = (1 << fls(max)) - 1;
1576 unsigned int invert = mc->invert;
46f5822f 1577 unsigned int val, val2, val_mask;
283375ce 1578 int connect;
2b97eabc
RP
1579 int ret;
1580
1581 val = (ucontrol->value.integer.value[0] & mask);
1582
1583 if (invert)
a7a4ac86 1584 val = max - val;
2b97eabc
RP
1585 val_mask = mask << shift;
1586 val = val << shift;
1587 if (shift != rshift) {
1588 val2 = (ucontrol->value.integer.value[1] & mask);
1589 if (invert)
a7a4ac86 1590 val2 = max - val2;
2b97eabc
RP
1591 val_mask |= mask << rshift;
1592 val |= val2 << rshift;
1593 }
1594
1595 mutex_lock(&widget->codec->mutex);
1596 widget->value = val;
1597
283375ce
MB
1598 if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
1599 if (val)
1600 /* new connection */
1601 connect = invert ? 0:1;
1602 else
1603 /* old connection must be powered down */
1604 connect = invert ? 1:0;
1605
1606 dapm_mixer_update_power(widget, kcontrol, connect);
1607 }
1608
2b97eabc
RP
1609 if (widget->event) {
1610 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
9af6d956
LG
1611 ret = widget->event(widget, kcontrol,
1612 SND_SOC_DAPM_PRE_REG);
1613 if (ret < 0) {
1614 ret = 1;
2b97eabc 1615 goto out;
9af6d956 1616 }
2b97eabc
RP
1617 }
1618 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1619 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
9af6d956
LG
1620 ret = widget->event(widget, kcontrol,
1621 SND_SOC_DAPM_POST_REG);
2b97eabc
RP
1622 } else
1623 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1624
1625out:
1626 mutex_unlock(&widget->codec->mutex);
1627 return ret;
1628}
1629EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1630
1631/**
1632 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1633 * @kcontrol: mixer control
ac11a2b3 1634 * @ucontrol: control element information
2b97eabc
RP
1635 *
1636 * Callback to get the value of a dapm enumerated double mixer control.
1637 *
1638 * Returns 0 for success.
1639 */
1640int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1641 struct snd_ctl_elem_value *ucontrol)
1642{
1643 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1644 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f 1645 unsigned int val, bitmask;
2b97eabc 1646
f8ba0b7b 1647 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2b97eabc
RP
1648 ;
1649 val = snd_soc_read(widget->codec, e->reg);
1650 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1651 if (e->shift_l != e->shift_r)
1652 ucontrol->value.enumerated.item[1] =
1653 (val >> e->shift_r) & (bitmask - 1);
1654
1655 return 0;
1656}
1657EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1658
1659/**
1660 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1661 * @kcontrol: mixer control
ac11a2b3 1662 * @ucontrol: control element information
2b97eabc
RP
1663 *
1664 * Callback to set the value of a dapm enumerated double mixer control.
1665 *
1666 * Returns 0 for success.
1667 */
1668int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1669 struct snd_ctl_elem_value *ucontrol)
1670{
1671 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1672 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3a65577d 1673 unsigned int val, mux, change;
46f5822f 1674 unsigned int mask, bitmask;
2b97eabc
RP
1675 int ret = 0;
1676
f8ba0b7b 1677 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2b97eabc 1678 ;
f8ba0b7b 1679 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2b97eabc
RP
1680 return -EINVAL;
1681 mux = ucontrol->value.enumerated.item[0];
1682 val = mux << e->shift_l;
1683 mask = (bitmask - 1) << e->shift_l;
1684 if (e->shift_l != e->shift_r) {
f8ba0b7b 1685 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2b97eabc
RP
1686 return -EINVAL;
1687 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1688 mask |= (bitmask - 1) << e->shift_r;
1689 }
1690
1691 mutex_lock(&widget->codec->mutex);
1692 widget->value = val;
3a65577d
MB
1693 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1694 dapm_mux_update_power(widget, kcontrol, change, mux, e);
1642e3d4
MB
1695
1696 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1697 ret = widget->event(widget,
1698 kcontrol, SND_SOC_DAPM_PRE_REG);
1699 if (ret < 0)
1700 goto out;
1701 }
1702
1703 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1704
1705 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1706 ret = widget->event(widget,
1707 kcontrol, SND_SOC_DAPM_POST_REG);
2b97eabc
RP
1708
1709out:
1710 mutex_unlock(&widget->codec->mutex);
1711 return ret;
1712}
1713EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1714
d2b247a8
MB
1715/**
1716 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1717 * @kcontrol: mixer control
1718 * @ucontrol: control element information
1719 *
1720 * Returns 0 for success.
1721 */
1722int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1723 struct snd_ctl_elem_value *ucontrol)
1724{
1725 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1726
1727 ucontrol->value.enumerated.item[0] = widget->value;
1728
1729 return 0;
1730}
1731EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1732
1733/**
1734 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1735 * @kcontrol: mixer control
1736 * @ucontrol: control element information
1737 *
1738 * Returns 0 for success.
1739 */
1740int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1741 struct snd_ctl_elem_value *ucontrol)
1742{
1743 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1744 struct soc_enum *e =
1745 (struct soc_enum *)kcontrol->private_value;
1746 int change;
1747 int ret = 0;
1748
1749 if (ucontrol->value.enumerated.item[0] >= e->max)
1750 return -EINVAL;
1751
1752 mutex_lock(&widget->codec->mutex);
1753
1754 change = widget->value != ucontrol->value.enumerated.item[0];
1755 widget->value = ucontrol->value.enumerated.item[0];
1756 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1757
1758 mutex_unlock(&widget->codec->mutex);
1759 return ret;
1760}
1761EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1762
2e72f8e3
PU
1763/**
1764 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1765 * callback
1766 * @kcontrol: mixer control
1767 * @ucontrol: control element information
1768 *
1769 * Callback to get the value of a dapm semi enumerated double mixer control.
1770 *
1771 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1772 * used for handling bitfield coded enumeration for example.
1773 *
1774 * Returns 0 for success.
1775 */
1776int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1777 struct snd_ctl_elem_value *ucontrol)
1778{
1779 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
74155556 1780 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f 1781 unsigned int reg_val, val, mux;
2e72f8e3
PU
1782
1783 reg_val = snd_soc_read(widget->codec, e->reg);
1784 val = (reg_val >> e->shift_l) & e->mask;
1785 for (mux = 0; mux < e->max; mux++) {
1786 if (val == e->values[mux])
1787 break;
1788 }
1789 ucontrol->value.enumerated.item[0] = mux;
1790 if (e->shift_l != e->shift_r) {
1791 val = (reg_val >> e->shift_r) & e->mask;
1792 for (mux = 0; mux < e->max; mux++) {
1793 if (val == e->values[mux])
1794 break;
1795 }
1796 ucontrol->value.enumerated.item[1] = mux;
1797 }
1798
1799 return 0;
1800}
1801EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1802
1803/**
1804 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1805 * callback
1806 * @kcontrol: mixer control
1807 * @ucontrol: control element information
1808 *
1809 * Callback to set the value of a dapm semi enumerated double mixer control.
1810 *
1811 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1812 * used for handling bitfield coded enumeration for example.
1813 *
1814 * Returns 0 for success.
1815 */
1816int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1817 struct snd_ctl_elem_value *ucontrol)
1818{
1819 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
74155556 1820 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3a65577d 1821 unsigned int val, mux, change;
46f5822f 1822 unsigned int mask;
2e72f8e3
PU
1823 int ret = 0;
1824
1825 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1826 return -EINVAL;
1827 mux = ucontrol->value.enumerated.item[0];
1828 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1829 mask = e->mask << e->shift_l;
1830 if (e->shift_l != e->shift_r) {
1831 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1832 return -EINVAL;
1833 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1834 mask |= e->mask << e->shift_r;
1835 }
1836
1837 mutex_lock(&widget->codec->mutex);
1838 widget->value = val;
3a65577d
MB
1839 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1840 dapm_mux_update_power(widget, kcontrol, change, mux, e);
1642e3d4
MB
1841
1842 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1843 ret = widget->event(widget,
1844 kcontrol, SND_SOC_DAPM_PRE_REG);
1845 if (ret < 0)
1846 goto out;
1847 }
1848
1849 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1850
1851 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1852 ret = widget->event(widget,
1853 kcontrol, SND_SOC_DAPM_POST_REG);
2e72f8e3
PU
1854
1855out:
1856 mutex_unlock(&widget->codec->mutex);
1857 return ret;
1858}
1859EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1860
8b37dbd2
MB
1861/**
1862 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1863 *
1864 * @kcontrol: mixer control
1865 * @uinfo: control element information
1866 *
1867 * Callback to provide information about a pin switch control.
1868 */
1869int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1870 struct snd_ctl_elem_info *uinfo)
1871{
1872 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1873 uinfo->count = 1;
1874 uinfo->value.integer.min = 0;
1875 uinfo->value.integer.max = 1;
1876
1877 return 0;
1878}
1879EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1880
1881/**
1882 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1883 *
1884 * @kcontrol: mixer control
1885 * @ucontrol: Value
1886 */
1887int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1888 struct snd_ctl_elem_value *ucontrol)
1889{
1890 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1891 const char *pin = (const char *)kcontrol->private_value;
1892
1893 mutex_lock(&codec->mutex);
1894
1895 ucontrol->value.integer.value[0] =
1896 snd_soc_dapm_get_pin_status(codec, pin);
1897
1898 mutex_unlock(&codec->mutex);
1899
1900 return 0;
1901}
1902EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1903
1904/**
1905 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1906 *
1907 * @kcontrol: mixer control
1908 * @ucontrol: Value
1909 */
1910int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1911 struct snd_ctl_elem_value *ucontrol)
1912{
1913 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1914 const char *pin = (const char *)kcontrol->private_value;
1915
1916 mutex_lock(&codec->mutex);
1917
1918 if (ucontrol->value.integer.value[0])
1919 snd_soc_dapm_enable_pin(codec, pin);
1920 else
1921 snd_soc_dapm_disable_pin(codec, pin);
1922
1923 snd_soc_dapm_sync(codec);
1924
1925 mutex_unlock(&codec->mutex);
1926
1927 return 0;
1928}
1929EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1930
2b97eabc
RP
1931/**
1932 * snd_soc_dapm_new_control - create new dapm control
1933 * @codec: audio codec
1934 * @widget: widget template
1935 *
1936 * Creates a new dapm control based upon the template.
1937 *
1938 * Returns 0 for success else error.
1939 */
1940int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1941 const struct snd_soc_dapm_widget *widget)
1942{
1943 struct snd_soc_dapm_widget *w;
1944
1945 if ((w = dapm_cnew_widget(widget)) == NULL)
1946 return -ENOMEM;
1947
1948 w->codec = codec;
1949 INIT_LIST_HEAD(&w->sources);
1950 INIT_LIST_HEAD(&w->sinks);
1951 INIT_LIST_HEAD(&w->list);
1952 list_add(&w->list, &codec->dapm_widgets);
1953
1954 /* machine layer set ups unconnected pins and insertions */
1955 w->connected = 1;
1956 return 0;
1957}
1958EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1959
4ba1327a
MB
1960/**
1961 * snd_soc_dapm_new_controls - create new dapm controls
1962 * @codec: audio codec
1963 * @widget: widget array
1964 * @num: number of widgets
1965 *
1966 * Creates new DAPM controls based upon the templates.
1967 *
1968 * Returns 0 for success else error.
1969 */
1970int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1971 const struct snd_soc_dapm_widget *widget,
1972 int num)
1973{
1974 int i, ret;
1975
1976 for (i = 0; i < num; i++) {
1977 ret = snd_soc_dapm_new_control(codec, widget);
b8b33cb5
MB
1978 if (ret < 0) {
1979 printk(KERN_ERR
1980 "ASoC: Failed to create DAPM control %s: %d\n",
1981 widget->name, ret);
4ba1327a 1982 return ret;
b8b33cb5 1983 }
4ba1327a
MB
1984 widget++;
1985 }
1986 return 0;
1987}
1988EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1989
1990
2b97eabc
RP
1991/**
1992 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1993 * @codec: audio codec
1994 * @stream: stream name
1995 * @event: stream event
1996 *
1997 * Sends a stream event to the dapm core. The core then makes any
1998 * necessary widget power changes.
1999 *
2000 * Returns 0 for success else error.
2001 */
2002int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
2003 char *stream, int event)
2004{
2005 struct snd_soc_dapm_widget *w;
2006
11da21a7
SF
2007 if (stream == NULL)
2008 return 0;
2009
2b97eabc
RP
2010 mutex_lock(&codec->mutex);
2011 list_for_each_entry(w, &codec->dapm_widgets, list)
2012 {
2013 if (!w->sname)
2014 continue;
c1286b86
MB
2015 pr_debug("widget %s\n %s stream %s event %d\n",
2016 w->name, w->sname, stream, event);
2b97eabc
RP
2017 if (strstr(w->sname, stream)) {
2018 switch(event) {
2019 case SND_SOC_DAPM_STREAM_START:
2020 w->active = 1;
2021 break;
2022 case SND_SOC_DAPM_STREAM_STOP:
2023 w->active = 0;
2024 break;
2025 case SND_SOC_DAPM_STREAM_SUSPEND:
2b97eabc 2026 case SND_SOC_DAPM_STREAM_RESUME:
2b97eabc 2027 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2b97eabc
RP
2028 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2029 break;
2030 }
2031 }
2032 }
2b97eabc
RP
2033
2034 dapm_power_widgets(codec, event);
8e8b2d67 2035 mutex_unlock(&codec->mutex);
2b97eabc
RP
2036 return 0;
2037}
2038EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2039
2040/**
a5302181 2041 * snd_soc_dapm_enable_pin - enable pin.
ac11a2b3 2042 * @codec: SoC codec
a5302181 2043 * @pin: pin name
2b97eabc 2044 *
74b8f955 2045 * Enables input/output pin and its parents or children widgets iff there is
a5302181
LG
2046 * a valid audio route and active audio stream.
2047 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2048 * do any widget power switching.
2b97eabc 2049 */
1649923d 2050int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
2b97eabc 2051{
a5302181
LG
2052 return snd_soc_dapm_set_pin(codec, pin, 1);
2053}
2054EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2b97eabc 2055
da34183e
MB
2056/**
2057 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2058 * @codec: SoC codec
2059 * @pin: pin name
2060 *
2061 * Enables input/output pin regardless of any other state. This is
2062 * intended for use with microphone bias supplies used in microphone
2063 * jack detection.
2064 *
2065 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2066 * do any widget power switching.
2067 */
2068int snd_soc_dapm_force_enable_pin(struct snd_soc_codec *codec, const char *pin)
2069{
2070 struct snd_soc_dapm_widget *w;
2071
2072 list_for_each_entry(w, &codec->dapm_widgets, list) {
2073 if (!strcmp(w->name, pin)) {
2074 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
2075 w->connected = 1;
2076 w->force = 1;
2077 return 0;
2078 }
2079 }
2080
2081 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
2082 return -EINVAL;
2083}
2084EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2085
a5302181
LG
2086/**
2087 * snd_soc_dapm_disable_pin - disable pin.
2088 * @codec: SoC codec
2089 * @pin: pin name
2090 *
74b8f955 2091 * Disables input/output pin and its parents or children widgets.
a5302181
LG
2092 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2093 * do any widget power switching.
2094 */
1649923d 2095int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
a5302181
LG
2096{
2097 return snd_soc_dapm_set_pin(codec, pin, 0);
2b97eabc 2098}
a5302181 2099EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2b97eabc 2100
5817b52a
MB
2101/**
2102 * snd_soc_dapm_nc_pin - permanently disable pin.
2103 * @codec: SoC codec
2104 * @pin: pin name
2105 *
2106 * Marks the specified pin as being not connected, disabling it along
2107 * any parent or child widgets. At present this is identical to
2108 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2109 * additional things such as disabling controls which only affect
2110 * paths through the pin.
2111 *
2112 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2113 * do any widget power switching.
2114 */
1649923d 2115int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
5817b52a
MB
2116{
2117 return snd_soc_dapm_set_pin(codec, pin, 0);
2118}
2119EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2120
eeec12bf 2121/**
a5302181 2122 * snd_soc_dapm_get_pin_status - get audio pin status
eeec12bf 2123 * @codec: audio codec
a5302181 2124 * @pin: audio signal pin endpoint (or start point)
eeec12bf 2125 *
a5302181 2126 * Get audio pin status - connected or disconnected.
eeec12bf 2127 *
a5302181 2128 * Returns 1 for connected otherwise 0.
eeec12bf 2129 */
1649923d 2130int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
eeec12bf
GG
2131{
2132 struct snd_soc_dapm_widget *w;
2133
2134 list_for_each_entry(w, &codec->dapm_widgets, list) {
a5302181 2135 if (!strcmp(w->name, pin))
eeec12bf
GG
2136 return w->connected;
2137 }
2138
2139 return 0;
2140}
a5302181 2141EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
eeec12bf 2142
1547aba9
MB
2143/**
2144 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2145 * @codec: audio codec
2146 * @pin: audio signal pin endpoint (or start point)
2147 *
2148 * Mark the given endpoint or pin as ignoring suspend. When the
2149 * system is disabled a path between two endpoints flagged as ignoring
2150 * suspend will not be disabled. The path must already be enabled via
2151 * normal means at suspend time, it will not be turned on if it was not
2152 * already enabled.
2153 */
2154int snd_soc_dapm_ignore_suspend(struct snd_soc_codec *codec, const char *pin)
2155{
2156 struct snd_soc_dapm_widget *w;
2157
2158 list_for_each_entry(w, &codec->dapm_widgets, list) {
2159 if (!strcmp(w->name, pin)) {
2160 w->ignore_suspend = 1;
2161 return 0;
2162 }
2163 }
2164
2165 pr_err("Unknown DAPM pin: %s\n", pin);
2166 return -EINVAL;
2167}
2168EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2169
2b97eabc
RP
2170/**
2171 * snd_soc_dapm_free - free dapm resources
2172 * @socdev: SoC device
2173 *
2174 * Free all dapm widgets and resources.
2175 */
2176void snd_soc_dapm_free(struct snd_soc_device *socdev)
2177{
6627a653 2178 struct snd_soc_codec *codec = socdev->card->codec;
2b97eabc
RP
2179
2180 snd_soc_dapm_sys_remove(socdev->dev);
2181 dapm_free_widgets(codec);
2182}
2183EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2184
51737470
MB
2185/*
2186 * snd_soc_dapm_shutdown - callback for system shutdown
2187 */
2188void snd_soc_dapm_shutdown(struct snd_soc_device *socdev)
2189{
2190 struct snd_soc_codec *codec = socdev->card->codec;
2191 struct snd_soc_dapm_widget *w;
2192 LIST_HEAD(down_list);
2193 int powerdown = 0;
2194
2195 list_for_each_entry(w, &codec->dapm_widgets, list) {
2196 if (w->power) {
2197 dapm_seq_insert(w, &down_list, dapm_down_seq);
c2caa4da 2198 w->power = 0;
51737470
MB
2199 powerdown = 1;
2200 }
2201 }
2202
2203 /* If there were no widgets to power down we're already in
2204 * standby.
2205 */
2206 if (powerdown) {
2207 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_PREPARE);
2208 dapm_seq_run(codec, &down_list, 0, dapm_down_seq);
2209 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_STANDBY);
2210 }
2211
2212 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
2213}
2214
2b97eabc 2215/* Module information */
d331124d 2216MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2b97eabc
RP
2217MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2218MODULE_LICENSE("GPL");
This page took 0.385691 seconds and 5 git commands to generate.