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