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