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