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