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