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