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