]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - sound/soc/soc-dapm.c
ASoC: dapm: Change prototype of soc_widget_read
[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 16 * o Platform power domain - can support external components i.e. amps and
612a3fec 17 * mic/headphone insertion events.
2b97eabc
RP
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
612a3fec 21 * o Delayed power down of audio subsystem to reduce pops between a quick
2b97eabc
RP
22 * device reopen.
23 *
2b97eabc
RP
24 */
25
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/init.h>
9d0624a7 29#include <linux/async.h>
2b97eabc
RP
30#include <linux/delay.h>
31#include <linux/pm.h>
32#include <linux/bitops.h>
33#include <linux/platform_device.h>
34#include <linux/jiffies.h>
20496ff3 35#include <linux/debugfs.h>
f1aac484 36#include <linux/pm_runtime.h>
62ea874a 37#include <linux/regulator/consumer.h>
d7e7eb91 38#include <linux/clk.h>
5a0e3ad6 39#include <linux/slab.h>
2b97eabc
RP
40#include <sound/core.h>
41#include <sound/pcm.h>
42#include <sound/pcm_params.h>
ce6120cc 43#include <sound/soc.h>
2b97eabc
RP
44#include <sound/initval.h>
45
84e90930
MB
46#include <trace/events/asoc.h>
47
de02d078
MB
48#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
49
57295073
LPC
50static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
51 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
52 const char *control,
53 int (*connected)(struct snd_soc_dapm_widget *source,
54 struct snd_soc_dapm_widget *sink));
55static struct snd_soc_dapm_widget *
56snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
57 const struct snd_soc_dapm_widget *widget);
58
2b97eabc
RP
59/* dapm power sequences - make this per codec in the future */
60static int dapm_up_seq[] = {
38357ab2 61 [snd_soc_dapm_pre] = 0,
62ea874a 62 [snd_soc_dapm_regulator_supply] = 1,
d7e7eb91 63 [snd_soc_dapm_clock_supply] = 1,
1dd275b6
MB
64 [snd_soc_dapm_supply] = 2,
65 [snd_soc_dapm_micbias] = 3,
c74184ed 66 [snd_soc_dapm_dai_link] = 2,
1dd275b6
MB
67 [snd_soc_dapm_dai_in] = 4,
68 [snd_soc_dapm_dai_out] = 4,
69 [snd_soc_dapm_aif_in] = 4,
70 [snd_soc_dapm_aif_out] = 4,
71 [snd_soc_dapm_mic] = 5,
72 [snd_soc_dapm_mux] = 6,
73 [snd_soc_dapm_virt_mux] = 6,
74 [snd_soc_dapm_value_mux] = 6,
75 [snd_soc_dapm_dac] = 7,
76 [snd_soc_dapm_switch] = 8,
77 [snd_soc_dapm_mixer] = 8,
78 [snd_soc_dapm_mixer_named_ctl] = 8,
79 [snd_soc_dapm_pga] = 9,
80 [snd_soc_dapm_adc] = 10,
81 [snd_soc_dapm_out_drv] = 11,
82 [snd_soc_dapm_hp] = 11,
83 [snd_soc_dapm_spk] = 11,
84 [snd_soc_dapm_line] = 11,
85 [snd_soc_dapm_kcontrol] = 12,
86 [snd_soc_dapm_post] = 13,
2b97eabc 87};
ca9c1aae 88
2b97eabc 89static int dapm_down_seq[] = {
38357ab2 90 [snd_soc_dapm_pre] = 0,
57295073
LPC
91 [snd_soc_dapm_kcontrol] = 1,
92 [snd_soc_dapm_adc] = 2,
93 [snd_soc_dapm_hp] = 3,
94 [snd_soc_dapm_spk] = 3,
95 [snd_soc_dapm_line] = 3,
96 [snd_soc_dapm_out_drv] = 3,
38357ab2 97 [snd_soc_dapm_pga] = 4,
efc77e36 98 [snd_soc_dapm_switch] = 5,
38357ab2 99 [snd_soc_dapm_mixer_named_ctl] = 5,
e3d4dabd
MB
100 [snd_soc_dapm_mixer] = 5,
101 [snd_soc_dapm_dac] = 6,
102 [snd_soc_dapm_mic] = 7,
103 [snd_soc_dapm_micbias] = 8,
104 [snd_soc_dapm_mux] = 9,
24ff33ac 105 [snd_soc_dapm_virt_mux] = 9,
e3d4dabd 106 [snd_soc_dapm_value_mux] = 9,
010ff262
MB
107 [snd_soc_dapm_aif_in] = 10,
108 [snd_soc_dapm_aif_out] = 10,
4616274d
MB
109 [snd_soc_dapm_dai_in] = 10,
110 [snd_soc_dapm_dai_out] = 10,
c74184ed 111 [snd_soc_dapm_dai_link] = 11,
c74184ed 112 [snd_soc_dapm_supply] = 12,
1dd275b6
MB
113 [snd_soc_dapm_clock_supply] = 13,
114 [snd_soc_dapm_regulator_supply] = 13,
115 [snd_soc_dapm_post] = 14,
2b97eabc
RP
116};
117
12ef193d 118static void pop_wait(u32 pop_time)
15e4c72f
MB
119{
120 if (pop_time)
121 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
122}
123
fd8d3bc0 124static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
15e4c72f
MB
125{
126 va_list args;
fd8d3bc0 127 char *buf;
15e4c72f 128
fd8d3bc0
JN
129 if (!pop_time)
130 return;
15e4c72f 131
fd8d3bc0
JN
132 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
133 if (buf == NULL)
134 return;
15e4c72f 135
fd8d3bc0
JN
136 va_start(args, fmt);
137 vsnprintf(buf, PAGE_SIZE, fmt, args);
9d01df06 138 dev_info(dev, "%s", buf);
15e4c72f 139 va_end(args);
fd8d3bc0
JN
140
141 kfree(buf);
15e4c72f
MB
142}
143
db432b41
MB
144static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
145{
146 return !list_empty(&w->dirty);
147}
148
25c77c5f 149void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
db432b41 150{
75c1f891
MB
151 if (!dapm_dirty_widget(w)) {
152 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
153 w->name, reason);
db432b41 154 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
75c1f891 155 }
db432b41 156}
25c77c5f 157EXPORT_SYMBOL_GPL(dapm_mark_dirty);
db432b41 158
e2d32ff6
MB
159void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
160{
161 struct snd_soc_card *card = dapm->card;
162 struct snd_soc_dapm_widget *w;
163
164 mutex_lock(&card->dapm_mutex);
165
166 list_for_each_entry(w, &card->widgets, list) {
167 switch (w->id) {
168 case snd_soc_dapm_input:
169 case snd_soc_dapm_output:
170 dapm_mark_dirty(w, "Rechecking inputs and outputs");
171 break;
172 default:
173 break;
174 }
175 }
176
177 mutex_unlock(&card->dapm_mutex);
178}
179EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
180
2b97eabc 181/* create a new dapm widget */
88cb4290 182static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
2b97eabc
RP
183 const struct snd_soc_dapm_widget *_widget)
184{
88cb4290 185 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
2b97eabc
RP
186}
187
e84357f7 188struct dapm_kcontrol_data {
cf7c1de2 189 unsigned int value;
57295073 190 struct snd_soc_dapm_widget *widget;
5106b92f 191 struct list_head paths;
2c75bdf3 192 struct snd_soc_dapm_widget_list *wlist;
e84357f7
LPC
193};
194
195static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
196 struct snd_kcontrol *kcontrol)
197{
198 struct dapm_kcontrol_data *data;
57295073 199 struct soc_mixer_control *mc;
e84357f7 200
2c75bdf3 201 data = kzalloc(sizeof(*data), GFP_KERNEL);
e84357f7
LPC
202 if (!data) {
203 dev_err(widget->dapm->dev,
204 "ASoC: can't allocate kcontrol data for %s\n",
205 widget->name);
206 return -ENOMEM;
207 }
208
5106b92f 209 INIT_LIST_HEAD(&data->paths);
e84357f7 210
57295073
LPC
211 switch (widget->id) {
212 case snd_soc_dapm_switch:
213 case snd_soc_dapm_mixer:
214 case snd_soc_dapm_mixer_named_ctl:
215 mc = (struct soc_mixer_control *)kcontrol->private_value;
216
217 if (mc->autodisable) {
218 struct snd_soc_dapm_widget template;
219
220 memset(&template, 0, sizeof(template));
221 template.reg = mc->reg;
222 template.mask = (1 << fls(mc->max)) - 1;
223 template.shift = mc->shift;
224 if (mc->invert)
225 template.off_val = mc->max;
226 else
227 template.off_val = 0;
228 template.on_val = template.off_val;
229 template.id = snd_soc_dapm_kcontrol;
230 template.name = kcontrol->id.name;
231
2daabd78
LPC
232 data->value = template.on_val;
233
57295073
LPC
234 data->widget = snd_soc_dapm_new_control(widget->dapm,
235 &template);
236 if (!data->widget) {
237 kfree(data);
238 return -ENOMEM;
239 }
240 }
241 break;
242 default:
243 break;
244 }
245
e84357f7
LPC
246 kcontrol->private_data = data;
247
248 return 0;
249}
250
251static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
252{
253 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
57295073 254 kfree(data->widget);
2c75bdf3 255 kfree(data->wlist);
e84357f7
LPC
256 kfree(data);
257}
258
259static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
260 const struct snd_kcontrol *kcontrol)
261{
262 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
263
2c75bdf3 264 return data->wlist;
e84357f7
LPC
265}
266
267static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
268 struct snd_soc_dapm_widget *widget)
269{
270 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
2c75bdf3
LPC
271 struct snd_soc_dapm_widget_list *new_wlist;
272 unsigned int n;
e84357f7 273
2c75bdf3
LPC
274 if (data->wlist)
275 n = data->wlist->num_widgets + 1;
276 else
277 n = 1;
278
279 new_wlist = krealloc(data->wlist,
280 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
281 if (!new_wlist)
e84357f7
LPC
282 return -ENOMEM;
283
2c75bdf3
LPC
284 new_wlist->widgets[n - 1] = widget;
285 new_wlist->num_widgets = n;
e84357f7 286
2c75bdf3 287 data->wlist = new_wlist;
e84357f7
LPC
288
289 return 0;
290}
291
5106b92f
LPC
292static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
293 struct snd_soc_dapm_path *path)
294{
295 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
296
297 list_add_tail(&path->list_kcontrol, &data->paths);
57295073
LPC
298
299 if (data->widget) {
300 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
301 path->source, NULL, NULL);
302 }
303}
304
305static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
306{
307 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
308
309 if (!data->widget)
310 return true;
311
312 return data->widget->power;
5106b92f
LPC
313}
314
315static struct list_head *dapm_kcontrol_get_path_list(
316 const struct snd_kcontrol *kcontrol)
317{
318 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
319
320 return &data->paths;
321}
322
323#define dapm_kcontrol_for_each_path(path, kcontrol) \
324 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
325 list_kcontrol)
326
cf7c1de2
LPC
327static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
328{
329 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
330
331 return data->value;
332}
333
334static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
335 unsigned int value)
336{
337 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
338
339 if (data->value == value)
340 return false;
341
57295073
LPC
342 if (data->widget)
343 data->widget->on_val = value;
344
cf7c1de2
LPC
345 data->value = value;
346
347 return true;
348}
349
eee5d7f9
LPC
350/**
351 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
352 * @kcontrol: The kcontrol
353 */
354struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
355{
e84357f7 356 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
eee5d7f9
LPC
357}
358EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
359
6c120e19
LG
360static void dapm_reset(struct snd_soc_card *card)
361{
362 struct snd_soc_dapm_widget *w;
363
364 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
365
366 list_for_each_entry(w, &card->widgets, list) {
39eb5fd1 367 w->new_power = w->power;
6c120e19
LG
368 w->power_checked = false;
369 w->inputs = -1;
370 w->outputs = -1;
371 }
372}
373
f7d3c170
ASL
374static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg,
375 unsigned int *value)
0445bdf4 376{
f7d3c170
ASL
377 if (w->codec) {
378 *value = snd_soc_read(w->codec, reg);
379 return 0;
380 } else if (w->platform) {
381 *value = snd_soc_platform_read(w->platform, reg);
382 return 0;
383 }
b7950641 384
30a6a1a4 385 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
b7950641 386 return -1;
0445bdf4
LG
387}
388
389static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
390{
391 if (w->codec)
392 return snd_soc_write(w->codec, reg, val);
b7950641
LG
393 else if (w->platform)
394 return snd_soc_platform_write(w->platform, reg, val);
395
30a6a1a4 396 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
b7950641 397 return -1;
0445bdf4
LG
398}
399
49575fb5
LG
400static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
401{
e06ab3b8 402 if (w->codec && !w->codec->using_regmap)
49575fb5
LG
403 mutex_lock(&w->codec->mutex);
404 else if (w->platform)
405 mutex_lock(&w->platform->mutex);
406}
407
408static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
409{
e06ab3b8 410 if (w->codec && !w->codec->using_regmap)
49575fb5
LG
411 mutex_unlock(&w->codec->mutex);
412 else if (w->platform)
413 mutex_unlock(&w->platform->mutex);
414}
415
eb270e98
MB
416static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
417{
418 if (dapm->codec && dapm->codec->using_regmap)
419 regmap_async_complete(dapm->codec->control_data);
420}
421
49575fb5 422static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
0445bdf4
LG
423 unsigned short reg, unsigned int mask, unsigned int value)
424{
8a713da8 425 bool change;
0445bdf4
LG
426 unsigned int old, new;
427 int ret;
428
8a713da8 429 if (w->codec && w->codec->using_regmap) {
eb270e98
MB
430 ret = regmap_update_bits_check_async(w->codec->control_data,
431 reg, mask, value,
432 &change);
8a713da8
MB
433 if (ret != 0)
434 return ret;
435 } else {
49575fb5 436 soc_widget_lock(w);
f7d3c170 437 ret = soc_widget_read(w, reg, &old);
49575fb5
LG
438 if (ret < 0) {
439 soc_widget_unlock(w);
0445bdf4 440 return ret;
49575fb5 441 }
8a713da8 442
8a713da8
MB
443 new = (old & ~mask) | (value & mask);
444 change = old != new;
445 if (change) {
446 ret = soc_widget_write(w, reg, new);
49575fb5
LG
447 if (ret < 0) {
448 soc_widget_unlock(w);
8a713da8 449 return ret;
49575fb5 450 }
8a713da8 451 }
49575fb5 452 soc_widget_unlock(w);
0445bdf4
LG
453 }
454
455 return change;
456}
457
452c5eaa
MB
458/**
459 * snd_soc_dapm_set_bias_level - set the bias level for the system
ed5a4c47 460 * @dapm: DAPM context
452c5eaa
MB
461 * @level: level to configure
462 *
463 * Configure the bias (power) levels for the SoC audio device.
464 *
465 * Returns 0 for success else error.
466 */
ed5a4c47 467static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
ce6120cc 468 enum snd_soc_bias_level level)
452c5eaa 469{
ed5a4c47 470 struct snd_soc_card *card = dapm->card;
452c5eaa
MB
471 int ret = 0;
472
84e90930
MB
473 trace_snd_soc_bias_level_start(card, level);
474
f0fba2ad 475 if (card && card->set_bias_level)
d4c6005f 476 ret = card->set_bias_level(card, dapm, level);
171ec6b0
MB
477 if (ret != 0)
478 goto out;
479
cc4c670a
MB
480 if (dapm->codec) {
481 if (dapm->codec->driver->set_bias_level)
482 ret = dapm->codec->driver->set_bias_level(dapm->codec,
483 level);
d8c3bb91
MB
484 else
485 dapm->bias_level = level;
4e872a46 486 } else if (!card || dapm != &card->dapm) {
4123128e 487 dapm->bias_level = level;
4e872a46 488 }
4123128e 489
171ec6b0
MB
490 if (ret != 0)
491 goto out;
492
493 if (card && card->set_bias_level_post)
d4c6005f 494 ret = card->set_bias_level_post(card, dapm, level);
171ec6b0 495out:
84e90930
MB
496 trace_snd_soc_bias_level_done(card, level);
497
452c5eaa
MB
498 return ret;
499}
500
2b97eabc
RP
501/* set up initial codec paths */
502static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
503 struct snd_soc_dapm_path *p, int i)
504{
505 switch (w->id) {
506 case snd_soc_dapm_switch:
ca9c1aae
IM
507 case snd_soc_dapm_mixer:
508 case snd_soc_dapm_mixer_named_ctl: {
2b97eabc 509 int val;
4eaa9819 510 struct soc_mixer_control *mc = (struct soc_mixer_control *)
82cfecdc 511 w->kcontrol_news[i].private_value;
249ce138 512 int reg = mc->reg;
815ecf8d 513 unsigned int shift = mc->shift;
4eaa9819 514 int max = mc->max;
815ecf8d
JS
515 unsigned int mask = (1 << fls(max)) - 1;
516 unsigned int invert = mc->invert;
2b97eabc 517
249ce138 518 if (reg != SND_SOC_NOPM) {
f7d3c170 519 soc_widget_read(w, reg, &val);
249ce138
LPC
520 val = (val >> shift) & mask;
521 if (invert)
522 val = max - val;
523 p->connect = !!val;
524 } else {
525 p->connect = 0;
526 }
2b97eabc 527
2b97eabc
RP
528 }
529 break;
530 case snd_soc_dapm_mux: {
82cfecdc
SW
531 struct soc_enum *e = (struct soc_enum *)
532 w->kcontrol_news[i].private_value;
86767b7d 533 int val, item;
2b97eabc 534
f7d3c170 535 soc_widget_read(w, e->reg, &val);
86767b7d 536 item = (val >> e->shift_l) & e->mask;
2b97eabc 537
58fee775
LPC
538 if (item < e->max && !strcmp(p->name, e->texts[item]))
539 p->connect = 1;
540 else
541 p->connect = 0;
2b97eabc
RP
542 }
543 break;
24ff33ac 544 case snd_soc_dapm_virt_mux: {
82cfecdc
SW
545 struct soc_enum *e = (struct soc_enum *)
546 w->kcontrol_news[i].private_value;
24ff33ac
DP
547
548 p->connect = 0;
549 /* since a virtual mux has no backing registers to
550 * decide which path to connect, it will try to match
551 * with the first enumeration. This is to ensure
552 * that the default mux choice (the first) will be
553 * correctly powered up during initialization.
554 */
555 if (!strcmp(p->name, e->texts[0]))
556 p->connect = 1;
557 }
558 break;
2e72f8e3 559 case snd_soc_dapm_value_mux: {
74155556 560 struct soc_enum *e = (struct soc_enum *)
82cfecdc 561 w->kcontrol_news[i].private_value;
2e72f8e3
PU
562 int val, item;
563
f7d3c170 564 soc_widget_read(w, e->reg, &val);
2e72f8e3
PU
565 val = (val >> e->shift_l) & e->mask;
566 for (item = 0; item < e->max; item++) {
567 if (val == e->values[item])
568 break;
569 }
570
58fee775
LPC
571 if (item < e->max && !strcmp(p->name, e->texts[item]))
572 p->connect = 1;
573 else
574 p->connect = 0;
2e72f8e3
PU
575 }
576 break;
56563100 577 /* does not affect routing - always connected */
2b97eabc 578 case snd_soc_dapm_pga:
d88429a6 579 case snd_soc_dapm_out_drv:
2b97eabc
RP
580 case snd_soc_dapm_output:
581 case snd_soc_dapm_adc:
582 case snd_soc_dapm_input:
1ab97c8c 583 case snd_soc_dapm_siggen:
2b97eabc
RP
584 case snd_soc_dapm_dac:
585 case snd_soc_dapm_micbias:
586 case snd_soc_dapm_vmid:
246d0a17 587 case snd_soc_dapm_supply:
62ea874a 588 case snd_soc_dapm_regulator_supply:
d7e7eb91 589 case snd_soc_dapm_clock_supply:
010ff262
MB
590 case snd_soc_dapm_aif_in:
591 case snd_soc_dapm_aif_out:
4616274d
MB
592 case snd_soc_dapm_dai_in:
593 case snd_soc_dapm_dai_out:
2b97eabc
RP
594 case snd_soc_dapm_hp:
595 case snd_soc_dapm_mic:
596 case snd_soc_dapm_spk:
597 case snd_soc_dapm_line:
c74184ed 598 case snd_soc_dapm_dai_link:
57295073 599 case snd_soc_dapm_kcontrol:
56563100
MB
600 p->connect = 1;
601 break;
602 /* does affect routing - dynamically connected */
2b97eabc
RP
603 case snd_soc_dapm_pre:
604 case snd_soc_dapm_post:
605 p->connect = 0;
606 break;
607 }
608}
609
74b8f955 610/* connect mux widget to its interconnecting audio paths */
ce6120cc 611static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
2b97eabc
RP
612 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
613 struct snd_soc_dapm_path *path, const char *control_name,
614 const struct snd_kcontrol_new *kcontrol)
615{
616 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
617 int i;
618
f8ba0b7b 619 for (i = 0; i < e->max; i++) {
2b97eabc 620 if (!(strcmp(control_name, e->texts[i]))) {
8ddab3f5 621 list_add(&path->list, &dapm->card->paths);
2b97eabc
RP
622 list_add(&path->list_sink, &dest->sources);
623 list_add(&path->list_source, &src->sinks);
624 path->name = (char*)e->texts[i];
625 dapm_set_path_status(dest, path, 0);
626 return 0;
627 }
628 }
629
630 return -ENODEV;
631}
632
74b8f955 633/* connect mixer widget to its interconnecting audio paths */
ce6120cc 634static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
2b97eabc
RP
635 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
636 struct snd_soc_dapm_path *path, const char *control_name)
637{
638 int i;
639
640 /* search for mixer kcontrol */
641 for (i = 0; i < dest->num_kcontrols; i++) {
82cfecdc 642 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
8ddab3f5 643 list_add(&path->list, &dapm->card->paths);
2b97eabc
RP
644 list_add(&path->list_sink, &dest->sources);
645 list_add(&path->list_source, &src->sinks);
82cfecdc 646 path->name = dest->kcontrol_news[i].name;
2b97eabc
RP
647 dapm_set_path_status(dest, path, i);
648 return 0;
649 }
650 }
651 return -ENODEV;
652}
653
af46800b 654static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
1007da06 655 struct snd_soc_dapm_widget *kcontrolw,
af46800b
SW
656 const struct snd_kcontrol_new *kcontrol_new,
657 struct snd_kcontrol **kcontrol)
658{
659 struct snd_soc_dapm_widget *w;
660 int i;
661
662 *kcontrol = NULL;
663
664 list_for_each_entry(w, &dapm->card->widgets, list) {
1007da06
SW
665 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
666 continue;
af46800b
SW
667 for (i = 0; i < w->num_kcontrols; i++) {
668 if (&w->kcontrol_news[i] == kcontrol_new) {
669 if (w->kcontrols)
670 *kcontrol = w->kcontrols[i];
671 return 1;
672 }
673 }
674 }
675
676 return 0;
677}
678
85762e71
SW
679/*
680 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
681 * create it. Either way, add the widget into the control's widget list
682 */
683static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
946d92a1 684 int kci)
2b97eabc 685{
4b80b8c2 686 struct snd_soc_dapm_context *dapm = w->dapm;
12ea2c78 687 struct snd_card *card = dapm->card->snd_card;
efb7ac3f 688 const char *prefix;
85762e71
SW
689 size_t prefix_len;
690 int shared;
691 struct snd_kcontrol *kcontrol;
85762e71 692 bool wname_in_long_name, kcname_in_long_name;
85762e71
SW
693 char *long_name;
694 const char *name;
695 int ret;
efb7ac3f
MB
696
697 if (dapm->codec)
698 prefix = dapm->codec->name_prefix;
699 else
700 prefix = NULL;
2b97eabc 701
3e5ff4df
MB
702 if (prefix)
703 prefix_len = strlen(prefix) + 1;
704 else
705 prefix_len = 0;
706
85762e71
SW
707 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
708 &kcontrol);
2b97eabc 709
85762e71
SW
710 if (!kcontrol) {
711 if (shared) {
712 wname_in_long_name = false;
713 kcname_in_long_name = true;
714 } else {
715 switch (w->id) {
716 case snd_soc_dapm_switch:
717 case snd_soc_dapm_mixer:
718 wname_in_long_name = true;
719 kcname_in_long_name = true;
720 break;
721 case snd_soc_dapm_mixer_named_ctl:
722 wname_in_long_name = false;
723 kcname_in_long_name = true;
724 break;
725 case snd_soc_dapm_mux:
726 case snd_soc_dapm_virt_mux:
727 case snd_soc_dapm_value_mux:
728 wname_in_long_name = true;
729 kcname_in_long_name = false;
730 break;
731 default:
85762e71 732 return -EINVAL;
82cd8764 733 }
85762e71
SW
734 }
735
736 if (wname_in_long_name && kcname_in_long_name) {
85762e71
SW
737 /*
738 * The control will get a prefix from the control
739 * creation process but we're also using the same
740 * prefix for widgets so cut the prefix off the
741 * front of the widget name.
ca9c1aae 742 */
2b581074 743 long_name = kasprintf(GFP_KERNEL, "%s %s",
85762e71
SW
744 w->name + prefix_len,
745 w->kcontrol_news[kci].name);
e84357f7 746 if (long_name == NULL)
2b581074 747 return -ENOMEM;
85762e71
SW
748
749 name = long_name;
750 } else if (wname_in_long_name) {
751 long_name = NULL;
752 name = w->name + prefix_len;
753 } else {
754 long_name = NULL;
755 name = w->kcontrol_news[kci].name;
756 }
ca9c1aae 757
e84357f7 758 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
85762e71 759 prefix);
656ca9d3 760 kfree(long_name);
9356e9d5
LPC
761 if (!kcontrol)
762 return -ENOMEM;
763 kcontrol->private_free = dapm_kcontrol_free;
e84357f7
LPC
764
765 ret = dapm_kcontrol_data_alloc(w, kcontrol);
766 if (ret) {
767 snd_ctl_free_one(kcontrol);
768 return ret;
769 }
770
85762e71
SW
771 ret = snd_ctl_add(card, kcontrol);
772 if (ret < 0) {
773 dev_err(dapm->dev,
774 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
775 w->name, name, ret);
85762e71
SW
776 return ret;
777 }
85762e71 778 }
2b97eabc 779
2c75bdf3
LPC
780 ret = dapm_kcontrol_add_widget(kcontrol, w);
781 if (ret)
782 return ret;
783
85762e71 784 w->kcontrols[kci] = kcontrol;
ca9c1aae 785
85762e71
SW
786 return 0;
787}
219b93f5 788
85762e71
SW
789/* create new dapm mixer control */
790static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
791{
792 int i, ret;
793 struct snd_soc_dapm_path *path;
794
795 /* add kcontrol */
796 for (i = 0; i < w->num_kcontrols; i++) {
797 /* match name */
798 list_for_each_entry(path, &w->sources, list_sink) {
799 /* mixer/mux paths name must match control name */
800 if (path->name != (char *)w->kcontrol_news[i].name)
801 continue;
802
803 if (w->kcontrols[i]) {
5106b92f 804 dapm_kcontrol_add_path(w->kcontrols[i], path);
85762e71 805 continue;
2b97eabc 806 }
85762e71 807
946d92a1 808 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
85762e71
SW
809 if (ret < 0)
810 return ret;
946d92a1
MB
811
812 dapm_kcontrol_add_path(w->kcontrols[i], path);
2b97eabc
RP
813 }
814 }
85762e71
SW
815
816 return 0;
2b97eabc
RP
817}
818
819/* create new dapm mux control */
4b80b8c2 820static int dapm_new_mux(struct snd_soc_dapm_widget *w)
2b97eabc 821{
4b80b8c2 822 struct snd_soc_dapm_context *dapm = w->dapm;
85762e71 823 struct snd_soc_dapm_path *path;
af46800b 824 int ret;
2b97eabc 825
af46800b
SW
826 if (w->num_kcontrols != 1) {
827 dev_err(dapm->dev,
30a6a1a4 828 "ASoC: mux %s has incorrect number of controls\n",
af46800b 829 w->name);
2b97eabc
RP
830 return -EINVAL;
831 }
832
fe581391 833 if (list_empty(&w->sources)) {
85762e71
SW
834 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
835 return -EINVAL;
af46800b 836 }
ce6120cc 837
946d92a1 838 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
85762e71
SW
839 if (ret < 0)
840 return ret;
fad59888 841
2b97eabc 842 list_for_each_entry(path, &w->sources, list_sink)
5106b92f 843 dapm_kcontrol_add_path(w->kcontrols[0], path);
2b97eabc 844
af46800b 845 return 0;
2b97eabc
RP
846}
847
848/* create new dapm volume control */
4b80b8c2 849static int dapm_new_pga(struct snd_soc_dapm_widget *w)
2b97eabc 850{
a6c65736 851 if (w->num_kcontrols)
f7d41ae8 852 dev_err(w->dapm->dev,
30a6a1a4 853 "ASoC: PGA controls not supported: '%s'\n", w->name);
2b97eabc 854
a6c65736 855 return 0;
2b97eabc
RP
856}
857
858/* reset 'walked' bit for each dapm path */
1059ecfa
RT
859static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
860 struct list_head *sink)
2b97eabc
RP
861{
862 struct snd_soc_dapm_path *p;
863
1059ecfa
RT
864 list_for_each_entry(p, sink, list_source) {
865 if (p->walked) {
866 p->walked = 0;
867 dapm_clear_walk_output(dapm, &p->sink->sinks);
868 }
869 }
2b97eabc
RP
870}
871
1059ecfa
RT
872static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
873 struct list_head *source)
2b97eabc
RP
874{
875 struct snd_soc_dapm_path *p;
876
1059ecfa
RT
877 list_for_each_entry(p, source, list_sink) {
878 if (p->walked) {
879 p->walked = 0;
880 dapm_clear_walk_input(dapm, &p->source->sources);
881 }
882 }
2b97eabc
RP
883}
884
1059ecfa 885
9949788b
MB
886/* We implement power down on suspend by checking the power state of
887 * the ALSA card - when we are suspending the ALSA state for the card
888 * is set to D3.
889 */
890static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
891{
12ea2c78 892 int level = snd_power_get_state(widget->dapm->card->snd_card);
9949788b 893
f0fba2ad 894 switch (level) {
9949788b
MB
895 case SNDRV_CTL_POWER_D3hot:
896 case SNDRV_CTL_POWER_D3cold:
1547aba9 897 if (widget->ignore_suspend)
30a6a1a4 898 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
f7d41ae8 899 widget->name);
1547aba9 900 return widget->ignore_suspend;
9949788b
MB
901 default:
902 return 1;
903 }
904}
905
ec2e3031
LG
906/* add widget to list if it's not already in the list */
907static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
908 struct snd_soc_dapm_widget *w)
909{
910 struct snd_soc_dapm_widget_list *wlist;
911 int wlistsize, wlistentries, i;
912
913 if (*list == NULL)
914 return -EINVAL;
915
916 wlist = *list;
917
918 /* is this widget already in the list */
919 for (i = 0; i < wlist->num_widgets; i++) {
920 if (wlist->widgets[i] == w)
921 return 0;
922 }
923
924 /* allocate some new space */
925 wlistentries = wlist->num_widgets + 1;
926 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
927 wlistentries * sizeof(struct snd_soc_dapm_widget *);
928 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
929 if (*list == NULL) {
30a6a1a4 930 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
ec2e3031
LG
931 w->name);
932 return -ENOMEM;
933 }
934 wlist = *list;
935
936 /* insert the widget */
30a6a1a4 937 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
ec2e3031
LG
938 w->name, wlist->num_widgets);
939
940 wlist->widgets[wlist->num_widgets] = w;
941 wlist->num_widgets++;
942 return 1;
943}
944
2b97eabc
RP
945/*
946 * Recursively check for a completed path to an active or physically connected
947 * output widget. Returns number of complete paths.
948 */
ec2e3031
LG
949static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
950 struct snd_soc_dapm_widget_list **list)
2b97eabc
RP
951{
952 struct snd_soc_dapm_path *path;
953 int con = 0;
954
024dc078
MB
955 if (widget->outputs >= 0)
956 return widget->outputs;
957
de02d078
MB
958 DAPM_UPDATE_STAT(widget, path_checks);
959
62ea874a
MB
960 switch (widget->id) {
961 case snd_soc_dapm_supply:
962 case snd_soc_dapm_regulator_supply:
d7e7eb91 963 case snd_soc_dapm_clock_supply:
57295073 964 case snd_soc_dapm_kcontrol:
246d0a17 965 return 0;
62ea874a
MB
966 default:
967 break;
968 }
246d0a17 969
010ff262
MB
970 switch (widget->id) {
971 case snd_soc_dapm_adc:
972 case snd_soc_dapm_aif_out:
4616274d 973 case snd_soc_dapm_dai_out:
024dc078
MB
974 if (widget->active) {
975 widget->outputs = snd_soc_dapm_suspend_check(widget);
976 return widget->outputs;
977 }
010ff262
MB
978 default:
979 break;
980 }
2b97eabc
RP
981
982 if (widget->connected) {
983 /* connected pin ? */
024dc078
MB
984 if (widget->id == snd_soc_dapm_output && !widget->ext) {
985 widget->outputs = snd_soc_dapm_suspend_check(widget);
986 return widget->outputs;
987 }
2b97eabc
RP
988
989 /* connected jack or spk ? */
024dc078
MB
990 if (widget->id == snd_soc_dapm_hp ||
991 widget->id == snd_soc_dapm_spk ||
992 (widget->id == snd_soc_dapm_line &&
993 !list_empty(&widget->sources))) {
994 widget->outputs = snd_soc_dapm_suspend_check(widget);
995 return widget->outputs;
996 }
2b97eabc
RP
997 }
998
999 list_for_each_entry(path, &widget->sinks, list_source) {
e56235e0
MB
1000 DAPM_UPDATE_STAT(widget, neighbour_checks);
1001
bf3a9e13
MB
1002 if (path->weak)
1003 continue;
1004
8af294b4
MB
1005 if (path->walking)
1006 return 1;
1007
2b97eabc
RP
1008 if (path->walked)
1009 continue;
1010
ec2e3031
LG
1011 trace_snd_soc_dapm_output_path(widget, path);
1012
2b97eabc
RP
1013 if (path->sink && path->connect) {
1014 path->walked = 1;
8af294b4 1015 path->walking = 1;
ec2e3031
LG
1016
1017 /* do we need to add this widget to the list ? */
1018 if (list) {
1019 int err;
1020 err = dapm_list_add_widget(list, path->sink);
1021 if (err < 0) {
30a6a1a4
LG
1022 dev_err(widget->dapm->dev,
1023 "ASoC: could not add widget %s\n",
ec2e3031 1024 widget->name);
8af294b4 1025 path->walking = 0;
ec2e3031
LG
1026 return con;
1027 }
1028 }
1029
1030 con += is_connected_output_ep(path->sink, list);
8af294b4
MB
1031
1032 path->walking = 0;
2b97eabc
RP
1033 }
1034 }
1035
024dc078
MB
1036 widget->outputs = con;
1037
2b97eabc
RP
1038 return con;
1039}
1040
1041/*
1042 * Recursively check for a completed path to an active or physically connected
1043 * input widget. Returns number of complete paths.
1044 */
ec2e3031
LG
1045static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1046 struct snd_soc_dapm_widget_list **list)
2b97eabc
RP
1047{
1048 struct snd_soc_dapm_path *path;
1049 int con = 0;
1050
024dc078
MB
1051 if (widget->inputs >= 0)
1052 return widget->inputs;
1053
de02d078
MB
1054 DAPM_UPDATE_STAT(widget, path_checks);
1055
62ea874a
MB
1056 switch (widget->id) {
1057 case snd_soc_dapm_supply:
1058 case snd_soc_dapm_regulator_supply:
d7e7eb91 1059 case snd_soc_dapm_clock_supply:
57295073 1060 case snd_soc_dapm_kcontrol:
246d0a17 1061 return 0;
62ea874a
MB
1062 default:
1063 break;
1064 }
246d0a17 1065
2b97eabc 1066 /* active stream ? */
010ff262
MB
1067 switch (widget->id) {
1068 case snd_soc_dapm_dac:
1069 case snd_soc_dapm_aif_in:
4616274d 1070 case snd_soc_dapm_dai_in:
024dc078
MB
1071 if (widget->active) {
1072 widget->inputs = snd_soc_dapm_suspend_check(widget);
1073 return widget->inputs;
1074 }
010ff262
MB
1075 default:
1076 break;
1077 }
2b97eabc
RP
1078
1079 if (widget->connected) {
1080 /* connected pin ? */
024dc078
MB
1081 if (widget->id == snd_soc_dapm_input && !widget->ext) {
1082 widget->inputs = snd_soc_dapm_suspend_check(widget);
1083 return widget->inputs;
1084 }
2b97eabc
RP
1085
1086 /* connected VMID/Bias for lower pops */
024dc078
MB
1087 if (widget->id == snd_soc_dapm_vmid) {
1088 widget->inputs = snd_soc_dapm_suspend_check(widget);
1089 return widget->inputs;
1090 }
2b97eabc
RP
1091
1092 /* connected jack ? */
eaeae5d9 1093 if (widget->id == snd_soc_dapm_mic ||
024dc078
MB
1094 (widget->id == snd_soc_dapm_line &&
1095 !list_empty(&widget->sinks))) {
1096 widget->inputs = snd_soc_dapm_suspend_check(widget);
1097 return widget->inputs;
1098 }
1099
1ab97c8c
MB
1100 /* signal generator */
1101 if (widget->id == snd_soc_dapm_siggen) {
1102 widget->inputs = snd_soc_dapm_suspend_check(widget);
1103 return widget->inputs;
1104 }
2b97eabc
RP
1105 }
1106
1107 list_for_each_entry(path, &widget->sources, list_sink) {
e56235e0
MB
1108 DAPM_UPDATE_STAT(widget, neighbour_checks);
1109
bf3a9e13
MB
1110 if (path->weak)
1111 continue;
1112
8af294b4
MB
1113 if (path->walking)
1114 return 1;
1115
2b97eabc
RP
1116 if (path->walked)
1117 continue;
1118
ec2e3031
LG
1119 trace_snd_soc_dapm_input_path(widget, path);
1120
2b97eabc
RP
1121 if (path->source && path->connect) {
1122 path->walked = 1;
8af294b4 1123 path->walking = 1;
ec2e3031
LG
1124
1125 /* do we need to add this widget to the list ? */
1126 if (list) {
1127 int err;
90c6ce0d 1128 err = dapm_list_add_widget(list, path->source);
ec2e3031 1129 if (err < 0) {
30a6a1a4
LG
1130 dev_err(widget->dapm->dev,
1131 "ASoC: could not add widget %s\n",
ec2e3031 1132 widget->name);
8af294b4 1133 path->walking = 0;
ec2e3031
LG
1134 return con;
1135 }
1136 }
1137
1138 con += is_connected_input_ep(path->source, list);
8af294b4
MB
1139
1140 path->walking = 0;
2b97eabc
RP
1141 }
1142 }
1143
024dc078
MB
1144 widget->inputs = con;
1145
2b97eabc
RP
1146 return con;
1147}
1148
ec2e3031
LG
1149/**
1150 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1151 * @dai: the soc DAI.
1152 * @stream: stream direction.
1153 * @list: list of active widgets for this stream.
1154 *
1155 * Queries DAPM graph as to whether an valid audio stream path exists for
1156 * the initial stream specified by name. This takes into account
1157 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1158 *
1159 * Returns the number of valid paths or negative error.
1160 */
1161int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1162 struct snd_soc_dapm_widget_list **list)
1163{
1164 struct snd_soc_card *card = dai->card;
1165 int paths;
1166
1167 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1168 dapm_reset(card);
1169
1059ecfa 1170 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
ec2e3031 1171 paths = is_connected_output_ep(dai->playback_widget, list);
1059ecfa
RT
1172 dapm_clear_walk_output(&card->dapm,
1173 &dai->playback_widget->sinks);
1174 } else {
d298caae 1175 paths = is_connected_input_ep(dai->capture_widget, list);
1059ecfa
RT
1176 dapm_clear_walk_input(&card->dapm,
1177 &dai->capture_widget->sources);
1178 }
ec2e3031
LG
1179
1180 trace_snd_soc_dapm_connected(paths, stream);
ec2e3031
LG
1181 mutex_unlock(&card->dapm_mutex);
1182
1183 return paths;
1184}
1185
e2be2ccf
JN
1186/*
1187 * Handler for generic register modifier widget.
1188 */
1189int dapm_reg_event(struct snd_soc_dapm_widget *w,
1190 struct snd_kcontrol *kcontrol, int event)
1191{
1192 unsigned int val;
1193
1194 if (SND_SOC_DAPM_EVENT_ON(event))
1195 val = w->on_val;
1196 else
1197 val = w->off_val;
1198
49575fb5 1199 soc_widget_update_bits_locked(w, -(w->reg + 1),
e2be2ccf
JN
1200 w->mask << w->shift, val << w->shift);
1201
1202 return 0;
1203}
11589418 1204EXPORT_SYMBOL_GPL(dapm_reg_event);
e2be2ccf 1205
62ea874a
MB
1206/*
1207 * Handler for regulator supply widget.
1208 */
1209int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1210 struct snd_kcontrol *kcontrol, int event)
1211{
c05b84d1
MB
1212 int ret;
1213
eb270e98
MB
1214 soc_dapm_async_complete(w->dapm);
1215
c05b84d1 1216 if (SND_SOC_DAPM_EVENT_ON(event)) {
de9ba98b 1217 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
8784c77a 1218 ret = regulator_allow_bypass(w->regulator, false);
c05b84d1
MB
1219 if (ret != 0)
1220 dev_warn(w->dapm->dev,
30a6a1a4 1221 "ASoC: Failed to bypass %s: %d\n",
c05b84d1
MB
1222 w->name, ret);
1223 }
1224
a3cc056b 1225 return regulator_enable(w->regulator);
c05b84d1 1226 } else {
de9ba98b 1227 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
8784c77a 1228 ret = regulator_allow_bypass(w->regulator, true);
c05b84d1
MB
1229 if (ret != 0)
1230 dev_warn(w->dapm->dev,
30a6a1a4 1231 "ASoC: Failed to unbypass %s: %d\n",
c05b84d1
MB
1232 w->name, ret);
1233 }
1234
a3cc056b 1235 return regulator_disable_deferred(w->regulator, w->shift);
c05b84d1 1236 }
62ea874a
MB
1237}
1238EXPORT_SYMBOL_GPL(dapm_regulator_event);
1239
d7e7eb91
OL
1240/*
1241 * Handler for clock supply widget.
1242 */
1243int dapm_clock_event(struct snd_soc_dapm_widget *w,
1244 struct snd_kcontrol *kcontrol, int event)
1245{
1246 if (!w->clk)
1247 return -EIO;
1248
eb270e98
MB
1249 soc_dapm_async_complete(w->dapm);
1250
ec02995a 1251#ifdef CONFIG_HAVE_CLK
d7e7eb91 1252 if (SND_SOC_DAPM_EVENT_ON(event)) {
37c1b927 1253 return clk_prepare_enable(w->clk);
d7e7eb91 1254 } else {
37c1b927 1255 clk_disable_unprepare(w->clk);
d7e7eb91
OL
1256 return 0;
1257 }
ec02995a 1258#endif
98b3cf12 1259 return 0;
d7e7eb91
OL
1260}
1261EXPORT_SYMBOL_GPL(dapm_clock_event);
1262
d805002b
MB
1263static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1264{
9b8a83b2
MB
1265 if (w->power_checked)
1266 return w->new_power;
1267
d805002b 1268 if (w->force)
9b8a83b2 1269 w->new_power = 1;
d805002b 1270 else
9b8a83b2
MB
1271 w->new_power = w->power_check(w);
1272
1273 w->power_checked = true;
1274
1275 return w->new_power;
d805002b
MB
1276}
1277
cd0f2d47
MB
1278/* Generic check to see if a widget should be powered.
1279 */
1280static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1281{
1282 int in, out;
1283
de02d078
MB
1284 DAPM_UPDATE_STAT(w, power_checks);
1285
ec2e3031 1286 in = is_connected_input_ep(w, NULL);
1059ecfa 1287 dapm_clear_walk_input(w->dapm, &w->sources);
ec2e3031 1288 out = is_connected_output_ep(w, NULL);
1059ecfa 1289 dapm_clear_walk_output(w->dapm, &w->sinks);
cd0f2d47
MB
1290 return out != 0 && in != 0;
1291}
1292
6ea31b9f
MB
1293/* Check to see if an ADC has power */
1294static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1295{
1296 int in;
1297
de02d078
MB
1298 DAPM_UPDATE_STAT(w, power_checks);
1299
6ea31b9f 1300 if (w->active) {
ec2e3031 1301 in = is_connected_input_ep(w, NULL);
1059ecfa 1302 dapm_clear_walk_input(w->dapm, &w->sources);
6ea31b9f
MB
1303 return in != 0;
1304 } else {
1305 return dapm_generic_check_power(w);
1306 }
1307}
1308
1309/* Check to see if a DAC has power */
1310static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1311{
1312 int out;
1313
de02d078
MB
1314 DAPM_UPDATE_STAT(w, power_checks);
1315
6ea31b9f 1316 if (w->active) {
ec2e3031 1317 out = is_connected_output_ep(w, NULL);
1059ecfa 1318 dapm_clear_walk_output(w->dapm, &w->sinks);
6ea31b9f
MB
1319 return out != 0;
1320 } else {
1321 return dapm_generic_check_power(w);
1322 }
1323}
1324
246d0a17
MB
1325/* Check to see if a power supply is needed */
1326static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1327{
1328 struct snd_soc_dapm_path *path;
246d0a17 1329
de02d078
MB
1330 DAPM_UPDATE_STAT(w, power_checks);
1331
246d0a17
MB
1332 /* Check if one of our outputs is connected */
1333 list_for_each_entry(path, &w->sinks, list_source) {
a8fdac83
MB
1334 DAPM_UPDATE_STAT(w, neighbour_checks);
1335
bf3a9e13
MB
1336 if (path->weak)
1337 continue;
1338
215edda3
MB
1339 if (path->connected &&
1340 !path->connected(path->source, path->sink))
1341 continue;
1342
3017358a
MB
1343 if (!path->sink)
1344 continue;
1345
f68d7e16
MB
1346 if (dapm_widget_power_check(path->sink))
1347 return 1;
246d0a17
MB
1348 }
1349
f68d7e16 1350 return 0;
246d0a17
MB
1351}
1352
35c64bca
MB
1353static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1354{
1355 return 1;
1356}
1357
38357ab2
MB
1358static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1359 struct snd_soc_dapm_widget *b,
828a842f 1360 bool power_up)
42aa3418 1361{
828a842f
MB
1362 int *sort;
1363
1364 if (power_up)
1365 sort = dapm_up_seq;
1366 else
1367 sort = dapm_down_seq;
1368
38357ab2
MB
1369 if (sort[a->id] != sort[b->id])
1370 return sort[a->id] - sort[b->id];
20e4859d
MB
1371 if (a->subseq != b->subseq) {
1372 if (power_up)
1373 return a->subseq - b->subseq;
1374 else
1375 return b->subseq - a->subseq;
1376 }
b22ead2a
MB
1377 if (a->reg != b->reg)
1378 return a->reg - b->reg;
84dab567
MB
1379 if (a->dapm != b->dapm)
1380 return (unsigned long)a->dapm - (unsigned long)b->dapm;
42aa3418 1381
38357ab2
MB
1382 return 0;
1383}
42aa3418 1384
38357ab2
MB
1385/* Insert a widget in order into a DAPM power sequence. */
1386static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1387 struct list_head *list,
828a842f 1388 bool power_up)
38357ab2
MB
1389{
1390 struct snd_soc_dapm_widget *w;
1391
1392 list_for_each_entry(w, list, power_list)
828a842f 1393 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
38357ab2
MB
1394 list_add_tail(&new_widget->power_list, &w->power_list);
1395 return;
1396 }
1397
1398 list_add_tail(&new_widget->power_list, list);
1399}
1400
95dd5cd6 1401static void dapm_seq_check_event(struct snd_soc_card *card,
68f89ad8
MB
1402 struct snd_soc_dapm_widget *w, int event)
1403{
68f89ad8
MB
1404 const char *ev_name;
1405 int power, ret;
1406
1407 switch (event) {
1408 case SND_SOC_DAPM_PRE_PMU:
1409 ev_name = "PRE_PMU";
1410 power = 1;
1411 break;
1412 case SND_SOC_DAPM_POST_PMU:
1413 ev_name = "POST_PMU";
1414 power = 1;
1415 break;
1416 case SND_SOC_DAPM_PRE_PMD:
1417 ev_name = "PRE_PMD";
1418 power = 0;
1419 break;
1420 case SND_SOC_DAPM_POST_PMD:
1421 ev_name = "POST_PMD";
1422 power = 0;
1423 break;
80114129
MB
1424 case SND_SOC_DAPM_WILL_PMU:
1425 ev_name = "WILL_PMU";
1426 power = 1;
1427 break;
1428 case SND_SOC_DAPM_WILL_PMD:
1429 ev_name = "WILL_PMD";
1430 power = 0;
1431 break;
68f89ad8 1432 default:
a6ed0608 1433 WARN(1, "Unknown event %d\n", event);
68f89ad8
MB
1434 return;
1435 }
1436
39eb5fd1 1437 if (w->new_power != power)
68f89ad8
MB
1438 return;
1439
1440 if (w->event && (w->event_flags & event)) {
95dd5cd6 1441 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
68f89ad8 1442 w->name, ev_name);
eb270e98 1443 soc_dapm_async_complete(w->dapm);
84e90930 1444 trace_snd_soc_dapm_widget_event_start(w, event);
68f89ad8 1445 ret = w->event(w, NULL, event);
84e90930 1446 trace_snd_soc_dapm_widget_event_done(w, event);
68f89ad8 1447 if (ret < 0)
95dd5cd6 1448 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
68f89ad8
MB
1449 ev_name, w->name, ret);
1450 }
1451}
1452
b22ead2a 1453/* Apply the coalesced changes from a DAPM sequence */
95dd5cd6 1454static void dapm_seq_run_coalesced(struct snd_soc_card *card,
b22ead2a 1455 struct list_head *pending)
163cac06 1456{
68f89ad8 1457 struct snd_soc_dapm_widget *w;
de9ba98b 1458 int reg;
b22ead2a
MB
1459 unsigned int value = 0;
1460 unsigned int mask = 0;
b22ead2a
MB
1461
1462 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1463 power_list)->reg;
1464
1465 list_for_each_entry(w, pending, power_list) {
bf4edea8 1466 WARN_ON(reg != w->reg);
39eb5fd1 1467 w->power = w->new_power;
b22ead2a 1468
de9ba98b
LPC
1469 mask |= w->mask << w->shift;
1470 if (w->power)
1471 value |= w->on_val << w->shift;
b22ead2a 1472 else
de9ba98b 1473 value |= w->off_val << w->shift;
b22ead2a 1474
95dd5cd6 1475 pop_dbg(w->dapm->dev, card->pop_time,
b22ead2a
MB
1476 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1477 w->name, reg, value, mask);
81628103 1478
68f89ad8 1479 /* Check for events */
95dd5cd6
LPC
1480 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1481 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
81628103
MB
1482 }
1483
1484 if (reg >= 0) {
29376bc7
MB
1485 /* Any widget will do, they should all be updating the
1486 * same register.
1487 */
1488 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1489 power_list);
1490
95dd5cd6 1491 pop_dbg(w->dapm->dev, card->pop_time,
81628103 1492 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
3a45b867
JN
1493 value, mask, reg, card->pop_time);
1494 pop_wait(card->pop_time);
49575fb5 1495 soc_widget_update_bits_locked(w, reg, mask, value);
b22ead2a
MB
1496 }
1497
81628103 1498 list_for_each_entry(w, pending, power_list) {
95dd5cd6
LPC
1499 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1500 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
81628103 1501 }
b22ead2a 1502}
42aa3418 1503
b22ead2a
MB
1504/* Apply a DAPM power sequence.
1505 *
1506 * We walk over a pre-sorted list of widgets to apply power to. In
1507 * order to minimise the number of writes to the device required
1508 * multiple widgets will be updated in a single write where possible.
1509 * Currently anything that requires more than a single write is not
1510 * handled.
1511 */
95dd5cd6
LPC
1512static void dapm_seq_run(struct snd_soc_card *card,
1513 struct list_head *list, int event, bool power_up)
b22ead2a
MB
1514{
1515 struct snd_soc_dapm_widget *w, *n;
eb270e98 1516 struct snd_soc_dapm_context *d;
b22ead2a
MB
1517 LIST_HEAD(pending);
1518 int cur_sort = -1;
20e4859d 1519 int cur_subseq = -1;
b22ead2a 1520 int cur_reg = SND_SOC_NOPM;
7be31be8 1521 struct snd_soc_dapm_context *cur_dapm = NULL;
474b62d6 1522 int ret, i;
828a842f
MB
1523 int *sort;
1524
1525 if (power_up)
1526 sort = dapm_up_seq;
1527 else
1528 sort = dapm_down_seq;
163cac06 1529
b22ead2a
MB
1530 list_for_each_entry_safe(w, n, list, power_list) {
1531 ret = 0;
1532
1533 /* Do we need to apply any queued changes? */
7be31be8 1534 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
20e4859d 1535 w->dapm != cur_dapm || w->subseq != cur_subseq) {
b22ead2a 1536 if (!list_empty(&pending))
95dd5cd6 1537 dapm_seq_run_coalesced(card, &pending);
b22ead2a 1538
474b62d6
MB
1539 if (cur_dapm && cur_dapm->seq_notifier) {
1540 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1541 if (sort[i] == cur_sort)
1542 cur_dapm->seq_notifier(cur_dapm,
f85a9e0d
MB
1543 i,
1544 cur_subseq);
474b62d6
MB
1545 }
1546
eb270e98
MB
1547 if (cur_dapm && w->dapm != cur_dapm)
1548 soc_dapm_async_complete(cur_dapm);
1549
b22ead2a
MB
1550 INIT_LIST_HEAD(&pending);
1551 cur_sort = -1;
b0b3e6f8 1552 cur_subseq = INT_MIN;
b22ead2a 1553 cur_reg = SND_SOC_NOPM;
7be31be8 1554 cur_dapm = NULL;
b22ead2a
MB
1555 }
1556
163cac06
MB
1557 switch (w->id) {
1558 case snd_soc_dapm_pre:
1559 if (!w->event)
b22ead2a
MB
1560 list_for_each_entry_safe_continue(w, n, list,
1561 power_list);
163cac06 1562
b22ead2a 1563 if (event == SND_SOC_DAPM_STREAM_START)
163cac06
MB
1564 ret = w->event(w,
1565 NULL, SND_SOC_DAPM_PRE_PMU);
b22ead2a 1566 else if (event == SND_SOC_DAPM_STREAM_STOP)
163cac06
MB
1567 ret = w->event(w,
1568 NULL, SND_SOC_DAPM_PRE_PMD);
163cac06
MB
1569 break;
1570
1571 case snd_soc_dapm_post:
1572 if (!w->event)
b22ead2a
MB
1573 list_for_each_entry_safe_continue(w, n, list,
1574 power_list);
163cac06 1575
b22ead2a 1576 if (event == SND_SOC_DAPM_STREAM_START)
163cac06
MB
1577 ret = w->event(w,
1578 NULL, SND_SOC_DAPM_POST_PMU);
b22ead2a 1579 else if (event == SND_SOC_DAPM_STREAM_STOP)
163cac06
MB
1580 ret = w->event(w,
1581 NULL, SND_SOC_DAPM_POST_PMD);
163cac06
MB
1582 break;
1583
b22ead2a 1584 default:
81628103
MB
1585 /* Queue it up for application */
1586 cur_sort = sort[w->id];
20e4859d 1587 cur_subseq = w->subseq;
81628103 1588 cur_reg = w->reg;
7be31be8 1589 cur_dapm = w->dapm;
81628103
MB
1590 list_move(&w->power_list, &pending);
1591 break;
163cac06 1592 }
b22ead2a
MB
1593
1594 if (ret < 0)
f7d41ae8 1595 dev_err(w->dapm->dev,
30a6a1a4 1596 "ASoC: Failed to apply widget power: %d\n", ret);
6ea31b9f 1597 }
b22ead2a
MB
1598
1599 if (!list_empty(&pending))
95dd5cd6 1600 dapm_seq_run_coalesced(card, &pending);
474b62d6
MB
1601
1602 if (cur_dapm && cur_dapm->seq_notifier) {
1603 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1604 if (sort[i] == cur_sort)
1605 cur_dapm->seq_notifier(cur_dapm,
f85a9e0d 1606 i, cur_subseq);
474b62d6 1607 }
eb270e98
MB
1608
1609 list_for_each_entry(d, &card->dapm_list, list) {
1610 soc_dapm_async_complete(d);
1611 }
42aa3418
MB
1612}
1613
95dd5cd6 1614static void dapm_widget_update(struct snd_soc_card *card)
97404f2e 1615{
95dd5cd6 1616 struct snd_soc_dapm_update *update = card->update;
ce6cfaf1
LPC
1617 struct snd_soc_dapm_widget_list *wlist;
1618 struct snd_soc_dapm_widget *w = NULL;
1619 unsigned int wi;
97404f2e
MB
1620 int ret;
1621
57295073 1622 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
97404f2e
MB
1623 return;
1624
e84357f7 1625 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
97404f2e 1626
ce6cfaf1
LPC
1627 for (wi = 0; wi < wlist->num_widgets; wi++) {
1628 w = wlist->widgets[wi];
1629
1630 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1631 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1632 if (ret != 0)
95dd5cd6 1633 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
ce6cfaf1
LPC
1634 w->name, ret);
1635 }
97404f2e
MB
1636 }
1637
ce6cfaf1
LPC
1638 if (!w)
1639 return;
1640
49575fb5 1641 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
97404f2e
MB
1642 update->val);
1643 if (ret < 0)
95dd5cd6 1644 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
30a6a1a4 1645 w->name, ret);
97404f2e 1646
ce6cfaf1
LPC
1647 for (wi = 0; wi < wlist->num_widgets; wi++) {
1648 w = wlist->widgets[wi];
1649
1650 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1651 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1652 if (ret != 0)
95dd5cd6 1653 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
ce6cfaf1
LPC
1654 w->name, ret);
1655 }
97404f2e
MB
1656 }
1657}
1658
9d0624a7
MB
1659/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1660 * they're changing state.
1661 */
1662static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1663{
1664 struct snd_soc_dapm_context *d = data;
1665 int ret;
1666
56fba41f
MB
1667 /* If we're off and we're not supposed to be go into STANDBY */
1668 if (d->bias_level == SND_SOC_BIAS_OFF &&
1669 d->target_bias_level != SND_SOC_BIAS_OFF) {
f1aac484
MB
1670 if (d->dev)
1671 pm_runtime_get_sync(d->dev);
1672
9d0624a7
MB
1673 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1674 if (ret != 0)
1675 dev_err(d->dev,
30a6a1a4 1676 "ASoC: Failed to turn on bias: %d\n", ret);
9d0624a7
MB
1677 }
1678
56fba41f
MB
1679 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1680 if (d->bias_level != d->target_bias_level) {
9d0624a7
MB
1681 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1682 if (ret != 0)
1683 dev_err(d->dev,
30a6a1a4 1684 "ASoC: Failed to prepare bias: %d\n", ret);
9d0624a7
MB
1685 }
1686}
1687
1688/* Async callback run prior to DAPM sequences - brings to their final
1689 * state.
1690 */
1691static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1692{
1693 struct snd_soc_dapm_context *d = data;
1694 int ret;
1695
1696 /* If we just powered the last thing off drop to standby bias */
56fba41f
MB
1697 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1698 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1699 d->target_bias_level == SND_SOC_BIAS_OFF)) {
9d0624a7
MB
1700 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1701 if (ret != 0)
30a6a1a4 1702 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
9d0624a7
MB
1703 ret);
1704 }
97404f2e 1705
9d0624a7 1706 /* If we're in standby and can support bias off then do that */
56fba41f
MB
1707 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1708 d->target_bias_level == SND_SOC_BIAS_OFF) {
9d0624a7
MB
1709 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1710 if (ret != 0)
30a6a1a4
LG
1711 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1712 ret);
f1aac484
MB
1713
1714 if (d->dev)
fb644e9c 1715 pm_runtime_put(d->dev);
9d0624a7
MB
1716 }
1717
1718 /* If we just powered up then move to active bias */
56fba41f
MB
1719 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1720 d->target_bias_level == SND_SOC_BIAS_ON) {
9d0624a7
MB
1721 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1722 if (ret != 0)
30a6a1a4 1723 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
9d0624a7
MB
1724 ret);
1725 }
1726}
97404f2e 1727
fe4fda5d
MB
1728static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1729 bool power, bool connect)
1730{
1731 /* If a connection is being made or broken then that update
1732 * will have marked the peer dirty, otherwise the widgets are
1733 * not connected and this update has no impact. */
1734 if (!connect)
1735 return;
1736
1737 /* If the peer is already in the state we're moving to then we
1738 * won't have an impact on it. */
1739 if (power != peer->power)
75c1f891 1740 dapm_mark_dirty(peer, "peer state change");
fe4fda5d
MB
1741}
1742
05623c43
MB
1743static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1744 struct list_head *up_list,
1745 struct list_head *down_list)
1746{
db432b41
MB
1747 struct snd_soc_dapm_path *path;
1748
05623c43
MB
1749 if (w->power == power)
1750 return;
1751
1752 trace_snd_soc_dapm_widget_power(w, power);
1753
db432b41 1754 /* If we changed our power state perhaps our neigbours changed
fe4fda5d 1755 * also.
db432b41
MB
1756 */
1757 list_for_each_entry(path, &w->sources, list_sink) {
1758 if (path->source) {
fe4fda5d
MB
1759 dapm_widget_set_peer_power(path->source, power,
1760 path->connect);
db432b41
MB
1761 }
1762 }
f3bf3e45
MB
1763 switch (w->id) {
1764 case snd_soc_dapm_supply:
62ea874a 1765 case snd_soc_dapm_regulator_supply:
d7e7eb91 1766 case snd_soc_dapm_clock_supply:
57295073 1767 case snd_soc_dapm_kcontrol:
f3bf3e45
MB
1768 /* Supplies can't affect their outputs, only their inputs */
1769 break;
1770 default:
1771 list_for_each_entry(path, &w->sinks, list_source) {
1772 if (path->sink) {
1773 dapm_widget_set_peer_power(path->sink, power,
1774 path->connect);
1775 }
db432b41 1776 }
f3bf3e45 1777 break;
db432b41
MB
1778 }
1779
05623c43
MB
1780 if (power)
1781 dapm_seq_insert(w, up_list, true);
1782 else
1783 dapm_seq_insert(w, down_list, false);
05623c43
MB
1784}
1785
7c81beb0
MB
1786static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1787 struct list_head *up_list,
1788 struct list_head *down_list)
1789{
7c81beb0
MB
1790 int power;
1791
1792 switch (w->id) {
1793 case snd_soc_dapm_pre:
1794 dapm_seq_insert(w, down_list, false);
1795 break;
1796 case snd_soc_dapm_post:
1797 dapm_seq_insert(w, up_list, true);
1798 break;
1799
1800 default:
d805002b 1801 power = dapm_widget_power_check(w);
7c81beb0 1802
05623c43 1803 dapm_widget_set_power(w, power, up_list, down_list);
7c81beb0
MB
1804 break;
1805 }
1806}
1807
2b97eabc
RP
1808/*
1809 * Scan each dapm widget for complete audio path.
1810 * A complete path is a route that has valid endpoints i.e.:-
1811 *
1812 * o DAC to output pin.
1813 * o Input Pin to ADC.
1814 * o Input pin to Output pin (bypass, sidetone)
1815 * o DAC to ADC (loopback).
1816 */
95dd5cd6 1817static int dapm_power_widgets(struct snd_soc_card *card, int event)
2b97eabc
RP
1818{
1819 struct snd_soc_dapm_widget *w;
7be31be8 1820 struct snd_soc_dapm_context *d;
291f3bbc
MB
1821 LIST_HEAD(up_list);
1822 LIST_HEAD(down_list);
2955b47d 1823 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
56fba41f 1824 enum snd_soc_bias_level bias;
6d3ddc81 1825
84e90930
MB
1826 trace_snd_soc_dapm_start(card);
1827
56fba41f 1828 list_for_each_entry(d, &card->dapm_list, list) {
497098be
MB
1829 if (d->idle_bias_off)
1830 d->target_bias_level = SND_SOC_BIAS_OFF;
1831 else
1832 d->target_bias_level = SND_SOC_BIAS_STANDBY;
56fba41f 1833 }
7be31be8 1834
6c120e19 1835 dapm_reset(card);
9b8a83b2 1836
6d3ddc81 1837 /* Check which widgets we need to power and store them in
db432b41
MB
1838 * lists indicating if they should be powered up or down. We
1839 * only check widgets that have been flagged as dirty but note
1840 * that new widgets may be added to the dirty list while we
1841 * iterate.
6d3ddc81 1842 */
db432b41 1843 list_for_each_entry(w, &card->dapm_dirty, dirty) {
7c81beb0 1844 dapm_power_one_widget(w, &up_list, &down_list);
2b97eabc
RP
1845 }
1846
f9de6d74 1847 list_for_each_entry(w, &card->widgets, list) {
0ff97ebf
MB
1848 switch (w->id) {
1849 case snd_soc_dapm_pre:
1850 case snd_soc_dapm_post:
1851 /* These widgets always need to be powered */
1852 break;
1853 default:
1854 list_del_init(&w->dirty);
1855 break;
1856 }
db432b41 1857
39eb5fd1 1858 if (w->new_power) {
f9de6d74
MB
1859 d = w->dapm;
1860
1861 /* Supplies and micbiases only bring the
1862 * context up to STANDBY as unless something
1863 * else is active and passing audio they
afe62367
MB
1864 * generally don't require full power. Signal
1865 * generators are virtual pins and have no
1866 * power impact themselves.
f9de6d74
MB
1867 */
1868 switch (w->id) {
afe62367 1869 case snd_soc_dapm_siggen:
da83fea6 1870 case snd_soc_dapm_vmid:
afe62367 1871 break;
f9de6d74 1872 case snd_soc_dapm_supply:
62ea874a 1873 case snd_soc_dapm_regulator_supply:
d7e7eb91 1874 case snd_soc_dapm_clock_supply:
f9de6d74
MB
1875 case snd_soc_dapm_micbias:
1876 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1877 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1878 break;
1879 default:
1880 d->target_bias_level = SND_SOC_BIAS_ON;
1881 break;
1882 }
1883 }
1884
1885 }
1886
85a843c5
MB
1887 /* Force all contexts in the card to the same bias state if
1888 * they're not ground referenced.
1889 */
56fba41f 1890 bias = SND_SOC_BIAS_OFF;
52ba67bf 1891 list_for_each_entry(d, &card->dapm_list, list)
56fba41f
MB
1892 if (d->target_bias_level > bias)
1893 bias = d->target_bias_level;
52ba67bf 1894 list_for_each_entry(d, &card->dapm_list, list)
85a843c5
MB
1895 if (!d->idle_bias_off)
1896 d->target_bias_level = bias;
52ba67bf 1897
de02d078 1898 trace_snd_soc_dapm_walk_done(card);
52ba67bf 1899
9d0624a7 1900 /* Run all the bias changes in parallel */
95dd5cd6 1901 list_for_each_entry(d, &card->dapm_list, list)
9d0624a7
MB
1902 async_schedule_domain(dapm_pre_sequence_async, d,
1903 &async_domain);
1904 async_synchronize_full_domain(&async_domain);
452c5eaa 1905
cf1f7c6e 1906 list_for_each_entry(w, &down_list, power_list) {
95dd5cd6 1907 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
80114129
MB
1908 }
1909
cf1f7c6e 1910 list_for_each_entry(w, &up_list, power_list) {
95dd5cd6 1911 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
80114129
MB
1912 }
1913
6d3ddc81 1914 /* Power down widgets first; try to avoid amplifying pops. */
95dd5cd6 1915 dapm_seq_run(card, &down_list, event, false);
2b97eabc 1916
95dd5cd6 1917 dapm_widget_update(card);
97404f2e 1918
6d3ddc81 1919 /* Now power up. */
95dd5cd6 1920 dapm_seq_run(card, &up_list, event, true);
2b97eabc 1921
9d0624a7 1922 /* Run all the bias changes in parallel */
95dd5cd6 1923 list_for_each_entry(d, &card->dapm_list, list)
9d0624a7
MB
1924 async_schedule_domain(dapm_post_sequence_async, d,
1925 &async_domain);
1926 async_synchronize_full_domain(&async_domain);
452c5eaa 1927
8078d87f
LG
1928 /* do we need to notify any clients that DAPM event is complete */
1929 list_for_each_entry(d, &card->dapm_list, list) {
1930 if (d->stream_event)
1931 d->stream_event(d, event);
1932 }
1933
95dd5cd6 1934 pop_dbg(card->dev, card->pop_time,
fd8d3bc0 1935 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
3a45b867 1936 pop_wait(card->pop_time);
cb507e7e 1937
84e90930
MB
1938 trace_snd_soc_dapm_done(card);
1939
42aa3418 1940 return 0;
2b97eabc
RP
1941}
1942
79fb9387 1943#ifdef CONFIG_DEBUG_FS
79fb9387
MB
1944static ssize_t dapm_widget_power_read_file(struct file *file,
1945 char __user *user_buf,
1946 size_t count, loff_t *ppos)
1947{
1948 struct snd_soc_dapm_widget *w = file->private_data;
1949 char *buf;
1950 int in, out;
1951 ssize_t ret;
1952 struct snd_soc_dapm_path *p = NULL;
1953
1954 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1955 if (!buf)
1956 return -ENOMEM;
1957
ec2e3031 1958 in = is_connected_input_ep(w, NULL);
1059ecfa 1959 dapm_clear_walk_input(w->dapm, &w->sources);
ec2e3031 1960 out = is_connected_output_ep(w, NULL);
1059ecfa 1961 dapm_clear_walk_output(w->dapm, &w->sinks);
79fb9387 1962
f13ebada
MB
1963 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1964 w->name, w->power ? "On" : "Off",
1965 w->force ? " (forced)" : "", in, out);
79fb9387 1966
d033c36a
MB
1967 if (w->reg >= 0)
1968 ret += snprintf(buf + ret, PAGE_SIZE - ret,
de9ba98b
LPC
1969 " - R%d(0x%x) mask 0x%x",
1970 w->reg, w->reg, w->mask << w->shift);
d033c36a
MB
1971
1972 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1973
3eef08ba
MB
1974 if (w->sname)
1975 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1976 w->sname,
1977 w->active ? "active" : "inactive");
79fb9387
MB
1978
1979 list_for_each_entry(p, &w->sources, list_sink) {
ff18620c 1980 if (p->connected && !p->connected(w, p->source))
215edda3
MB
1981 continue;
1982
79fb9387
MB
1983 if (p->connect)
1984 ret += snprintf(buf + ret, PAGE_SIZE - ret,
67f5ed6e 1985 " in \"%s\" \"%s\"\n",
79fb9387
MB
1986 p->name ? p->name : "static",
1987 p->source->name);
1988 }
1989 list_for_each_entry(p, &w->sinks, list_source) {
215edda3
MB
1990 if (p->connected && !p->connected(w, p->sink))
1991 continue;
1992
79fb9387
MB
1993 if (p->connect)
1994 ret += snprintf(buf + ret, PAGE_SIZE - ret,
67f5ed6e 1995 " out \"%s\" \"%s\"\n",
79fb9387
MB
1996 p->name ? p->name : "static",
1997 p->sink->name);
1998 }
1999
2000 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2001
2002 kfree(buf);
2003 return ret;
2004}
2005
2006static const struct file_operations dapm_widget_power_fops = {
234e3405 2007 .open = simple_open,
79fb9387 2008 .read = dapm_widget_power_read_file,
6038f373 2009 .llseek = default_llseek,
79fb9387
MB
2010};
2011
ef49e4fa
MB
2012static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2013 size_t count, loff_t *ppos)
2014{
2015 struct snd_soc_dapm_context *dapm = file->private_data;
2016 char *level;
2017
2018 switch (dapm->bias_level) {
2019 case SND_SOC_BIAS_ON:
2020 level = "On\n";
2021 break;
2022 case SND_SOC_BIAS_PREPARE:
2023 level = "Prepare\n";
2024 break;
2025 case SND_SOC_BIAS_STANDBY:
2026 level = "Standby\n";
2027 break;
2028 case SND_SOC_BIAS_OFF:
2029 level = "Off\n";
2030 break;
2031 default:
a6ed0608 2032 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
ef49e4fa
MB
2033 level = "Unknown\n";
2034 break;
2035 }
2036
2037 return simple_read_from_buffer(user_buf, count, ppos, level,
2038 strlen(level));
2039}
2040
2041static const struct file_operations dapm_bias_fops = {
234e3405 2042 .open = simple_open,
ef49e4fa
MB
2043 .read = dapm_bias_read_file,
2044 .llseek = default_llseek,
2045};
2046
8eecaf62
LPC
2047void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2048 struct dentry *parent)
79fb9387 2049{
79fb9387
MB
2050 struct dentry *d;
2051
8eecaf62
LPC
2052 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2053
2054 if (!dapm->debugfs_dapm) {
f1e90af2 2055 dev_warn(dapm->dev,
30a6a1a4 2056 "ASoC: Failed to create DAPM debugfs directory\n");
79fb9387 2057 return;
8eecaf62 2058 }
79fb9387 2059
ef49e4fa
MB
2060 d = debugfs_create_file("bias_level", 0444,
2061 dapm->debugfs_dapm, dapm,
2062 &dapm_bias_fops);
2063 if (!d)
2064 dev_warn(dapm->dev,
2065 "ASoC: Failed to create bias level debugfs file\n");
d5d1e0be 2066}
ef49e4fa 2067
d5d1e0be
LPC
2068static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2069{
2070 struct snd_soc_dapm_context *dapm = w->dapm;
2071 struct dentry *d;
79fb9387 2072
d5d1e0be
LPC
2073 if (!dapm->debugfs_dapm || !w->name)
2074 return;
2075
2076 d = debugfs_create_file(w->name, 0444,
2077 dapm->debugfs_dapm, w,
2078 &dapm_widget_power_fops);
2079 if (!d)
2080 dev_warn(w->dapm->dev,
2081 "ASoC: Failed to create %s debugfs file\n",
2082 w->name);
79fb9387 2083}
d5d1e0be 2084
6c45e126
LPC
2085static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2086{
2087 debugfs_remove_recursive(dapm->debugfs_dapm);
2088}
2089
79fb9387 2090#else
8eecaf62
LPC
2091void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2092 struct dentry *parent)
79fb9387
MB
2093{
2094}
d5d1e0be
LPC
2095
2096static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2097{
2098}
2099
6c45e126
LPC
2100static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2101{
2102}
2103
79fb9387
MB
2104#endif
2105
2b97eabc 2106/* test and update the power status of a mux widget */
95dd5cd6 2107static int soc_dapm_mux_update_power(struct snd_soc_card *card,
40f02cd9 2108 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2b97eabc
RP
2109{
2110 struct snd_soc_dapm_path *path;
2111 int found = 0;
2112
2b97eabc 2113 /* find dapm widget path assoc with kcontrol */
5106b92f 2114 dapm_kcontrol_for_each_path(path, kcontrol) {
cb01e2b9 2115 if (!path->name || !e->texts[mux])
2b97eabc
RP
2116 continue;
2117
2118 found = 1;
2119 /* we now need to match the string in the enum to the path */
db432b41 2120 if (!(strcmp(path->name, e->texts[mux]))) {
2b97eabc 2121 path->connect = 1; /* new connection */
75c1f891 2122 dapm_mark_dirty(path->source, "mux connection");
db432b41
MB
2123 } else {
2124 if (path->connect)
75c1f891
MB
2125 dapm_mark_dirty(path->source,
2126 "mux disconnection");
2b97eabc 2127 path->connect = 0; /* old connection must be powered down */
db432b41 2128 }
ce6cfaf1 2129 dapm_mark_dirty(path->sink, "mux change");
2b97eabc
RP
2130 }
2131
ce6cfaf1 2132 if (found)
95dd5cd6 2133 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2b97eabc 2134
618dae11 2135 return found;
2b97eabc 2136}
4edbb345 2137
ce6cfaf1 2138int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
6b3fc03b
LPC
2139 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2140 struct snd_soc_dapm_update *update)
4edbb345 2141{
ce6cfaf1 2142 struct snd_soc_card *card = dapm->card;
4edbb345
LG
2143 int ret;
2144
3cd04343 2145 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
564c6504 2146 card->update = update;
95dd5cd6 2147 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
564c6504 2148 card->update = NULL;
4edbb345 2149 mutex_unlock(&card->dapm_mutex);
618dae11 2150 if (ret > 0)
c3f48ae6 2151 soc_dpcm_runtime_update(card);
4edbb345
LG
2152 return ret;
2153}
40f02cd9 2154EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2b97eabc 2155
1b075e3f 2156/* test and update the power status of a mixer or switch widget */
95dd5cd6 2157static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
283375ce 2158 struct snd_kcontrol *kcontrol, int connect)
2b97eabc
RP
2159{
2160 struct snd_soc_dapm_path *path;
2161 int found = 0;
2162
2b97eabc 2163 /* find dapm widget path assoc with kcontrol */
5106b92f 2164 dapm_kcontrol_for_each_path(path, kcontrol) {
2b97eabc 2165 found = 1;
283375ce 2166 path->connect = connect;
75c1f891 2167 dapm_mark_dirty(path->source, "mixer connection");
ce6cfaf1 2168 dapm_mark_dirty(path->sink, "mixer update");
2b97eabc
RP
2169 }
2170
ce6cfaf1 2171 if (found)
95dd5cd6 2172 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2b97eabc 2173
618dae11 2174 return found;
2b97eabc 2175}
4edbb345 2176
ce6cfaf1 2177int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
6b3fc03b
LPC
2178 struct snd_kcontrol *kcontrol, int connect,
2179 struct snd_soc_dapm_update *update)
4edbb345 2180{
ce6cfaf1 2181 struct snd_soc_card *card = dapm->card;
4edbb345
LG
2182 int ret;
2183
3cd04343 2184 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
564c6504 2185 card->update = update;
95dd5cd6 2186 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
564c6504 2187 card->update = NULL;
4edbb345 2188 mutex_unlock(&card->dapm_mutex);
618dae11 2189 if (ret > 0)
c3f48ae6 2190 soc_dpcm_runtime_update(card);
4edbb345
LG
2191 return ret;
2192}
40f02cd9 2193EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2b97eabc
RP
2194
2195/* show dapm widget status in sys fs */
2196static ssize_t dapm_widget_show(struct device *dev,
2197 struct device_attribute *attr, char *buf)
2198{
36ae1a96 2199 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
f0fba2ad 2200 struct snd_soc_codec *codec =rtd->codec;
2b97eabc
RP
2201 struct snd_soc_dapm_widget *w;
2202 int count = 0;
2203 char *state = "not set";
2204
97c866de
JN
2205 list_for_each_entry(w, &codec->card->widgets, list) {
2206 if (w->dapm != &codec->dapm)
2207 continue;
2b97eabc
RP
2208
2209 /* only display widgets that burnm power */
2210 switch (w->id) {
2211 case snd_soc_dapm_hp:
2212 case snd_soc_dapm_mic:
2213 case snd_soc_dapm_spk:
2214 case snd_soc_dapm_line:
2215 case snd_soc_dapm_micbias:
2216 case snd_soc_dapm_dac:
2217 case snd_soc_dapm_adc:
2218 case snd_soc_dapm_pga:
d88429a6 2219 case snd_soc_dapm_out_drv:
2b97eabc 2220 case snd_soc_dapm_mixer:
ca9c1aae 2221 case snd_soc_dapm_mixer_named_ctl:
246d0a17 2222 case snd_soc_dapm_supply:
62ea874a 2223 case snd_soc_dapm_regulator_supply:
d7e7eb91 2224 case snd_soc_dapm_clock_supply:
2b97eabc
RP
2225 if (w->name)
2226 count += sprintf(buf + count, "%s: %s\n",
2227 w->name, w->power ? "On":"Off");
2228 break;
2229 default:
2230 break;
2231 }
2232 }
2233
ce6120cc 2234 switch (codec->dapm.bias_level) {
0be9898a
MB
2235 case SND_SOC_BIAS_ON:
2236 state = "On";
2b97eabc 2237 break;
0be9898a
MB
2238 case SND_SOC_BIAS_PREPARE:
2239 state = "Prepare";
2b97eabc 2240 break;
0be9898a
MB
2241 case SND_SOC_BIAS_STANDBY:
2242 state = "Standby";
2b97eabc 2243 break;
0be9898a
MB
2244 case SND_SOC_BIAS_OFF:
2245 state = "Off";
2b97eabc
RP
2246 break;
2247 }
2248 count += sprintf(buf + count, "PM State: %s\n", state);
2249
2250 return count;
2251}
2252
2253static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2254
2255int snd_soc_dapm_sys_add(struct device *dev)
2256{
12ef193d 2257 return device_create_file(dev, &dev_attr_dapm_widget);
2b97eabc
RP
2258}
2259
2260static void snd_soc_dapm_sys_remove(struct device *dev)
2261{
aef90843 2262 device_remove_file(dev, &dev_attr_dapm_widget);
2b97eabc
RP
2263}
2264
8872293f
LPC
2265static void dapm_free_path(struct snd_soc_dapm_path *path)
2266{
2267 list_del(&path->list_sink);
2268 list_del(&path->list_source);
5106b92f 2269 list_del(&path->list_kcontrol);
8872293f 2270 list_del(&path->list);
8872293f
LPC
2271 kfree(path);
2272}
2273
2b97eabc 2274/* free all dapm widgets and resources */
ce6120cc 2275static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2b97eabc
RP
2276{
2277 struct snd_soc_dapm_widget *w, *next_w;
2278 struct snd_soc_dapm_path *p, *next_p;
2279
97c866de
JN
2280 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2281 if (w->dapm != dapm)
2282 continue;
2b97eabc 2283 list_del(&w->list);
8ddab3f5
JN
2284 /*
2285 * remove source and sink paths associated to this widget.
2286 * While removing the path, remove reference to it from both
2287 * source and sink widgets so that path is removed only once.
2288 */
8872293f
LPC
2289 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2290 dapm_free_path(p);
2291
2292 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2293 dapm_free_path(p);
2294
fad59888 2295 kfree(w->kcontrols);
ead9b919 2296 kfree(w->name);
2b97eabc
RP
2297 kfree(w);
2298 }
2b97eabc
RP
2299}
2300
91a5fca4
LPC
2301static struct snd_soc_dapm_widget *dapm_find_widget(
2302 struct snd_soc_dapm_context *dapm, const char *pin,
2303 bool search_other_contexts)
a5302181
LG
2304{
2305 struct snd_soc_dapm_widget *w;
91a5fca4 2306 struct snd_soc_dapm_widget *fallback = NULL;
a5302181 2307
97c866de 2308 list_for_each_entry(w, &dapm->card->widgets, list) {
a5302181 2309 if (!strcmp(w->name, pin)) {
91a5fca4
LPC
2310 if (w->dapm == dapm)
2311 return w;
2312 else
2313 fallback = w;
a5302181
LG
2314 }
2315 }
2316
91a5fca4
LPC
2317 if (search_other_contexts)
2318 return fallback;
2319
2320 return NULL;
2321}
2322
2323static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2324 const char *pin, int status)
2325{
2326 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2327
2328 if (!w) {
30a6a1a4 2329 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
91a5fca4 2330 return -EINVAL;
0d86733c
MB
2331 }
2332
1a8b2d9d
MB
2333 if (w->connected != status)
2334 dapm_mark_dirty(w, "pin configuration");
2335
91a5fca4
LPC
2336 w->connected = status;
2337 if (status == 0)
2338 w->force = 0;
2339
2340 return 0;
a5302181
LG
2341}
2342
2b97eabc 2343/**
a5302181 2344 * snd_soc_dapm_sync - scan and power dapm paths
ce6120cc 2345 * @dapm: DAPM context
2b97eabc
RP
2346 *
2347 * Walks all dapm audio paths and powers widgets according to their
2348 * stream or path usage.
2349 *
2350 * Returns 0 for success.
2351 */
ce6120cc 2352int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2b97eabc 2353{
a73fb2df
LG
2354 int ret;
2355
4f4c0072
MB
2356 /*
2357 * Suppress early reports (eg, jacks syncing their state) to avoid
2358 * silly DAPM runs during card startup.
2359 */
2360 if (!dapm->card || !dapm->card->instantiated)
2361 return 0;
2362
3cd04343 2363 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
95dd5cd6 2364 ret = dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
a73fb2df
LG
2365 mutex_unlock(&dapm->card->dapm_mutex);
2366 return ret;
2b97eabc 2367}
a5302181 2368EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2b97eabc 2369
2553628e
LPC
2370static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2371 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2372 const char *control,
2373 int (*connected)(struct snd_soc_dapm_widget *source,
2374 struct snd_soc_dapm_widget *sink))
2b97eabc
RP
2375{
2376 struct snd_soc_dapm_path *path;
2553628e 2377 int ret;
2b97eabc
RP
2378
2379 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2380 if (!path)
2381 return -ENOMEM;
2382
2383 path->source = wsource;
2384 path->sink = wsink;
2553628e 2385 path->connected = connected;
2b97eabc 2386 INIT_LIST_HEAD(&path->list);
69c2d346 2387 INIT_LIST_HEAD(&path->list_kcontrol);
2b97eabc
RP
2388 INIT_LIST_HEAD(&path->list_source);
2389 INIT_LIST_HEAD(&path->list_sink);
2390
2391 /* check for external widgets */
2392 if (wsink->id == snd_soc_dapm_input) {
2393 if (wsource->id == snd_soc_dapm_micbias ||
2394 wsource->id == snd_soc_dapm_mic ||
087d53ab
RC
2395 wsource->id == snd_soc_dapm_line ||
2396 wsource->id == snd_soc_dapm_output)
2b97eabc
RP
2397 wsink->ext = 1;
2398 }
2399 if (wsource->id == snd_soc_dapm_output) {
2400 if (wsink->id == snd_soc_dapm_spk ||
2401 wsink->id == snd_soc_dapm_hp ||
1e39221e
SF
2402 wsink->id == snd_soc_dapm_line ||
2403 wsink->id == snd_soc_dapm_input)
2b97eabc
RP
2404 wsource->ext = 1;
2405 }
2406
34742cb0
LPC
2407 dapm_mark_dirty(wsource, "Route added");
2408 dapm_mark_dirty(wsink, "Route added");
2409
2b97eabc
RP
2410 /* connect static paths */
2411 if (control == NULL) {
8ddab3f5 2412 list_add(&path->list, &dapm->card->paths);
2b97eabc
RP
2413 list_add(&path->list_sink, &wsink->sources);
2414 list_add(&path->list_source, &wsource->sinks);
2415 path->connect = 1;
2416 return 0;
2417 }
2418
2419 /* connect dynamic paths */
dc2bea61 2420 switch (wsink->id) {
2b97eabc
RP
2421 case snd_soc_dapm_adc:
2422 case snd_soc_dapm_dac:
2423 case snd_soc_dapm_pga:
d88429a6 2424 case snd_soc_dapm_out_drv:
2b97eabc
RP
2425 case snd_soc_dapm_input:
2426 case snd_soc_dapm_output:
1ab97c8c 2427 case snd_soc_dapm_siggen:
2b97eabc
RP
2428 case snd_soc_dapm_micbias:
2429 case snd_soc_dapm_vmid:
2430 case snd_soc_dapm_pre:
2431 case snd_soc_dapm_post:
246d0a17 2432 case snd_soc_dapm_supply:
62ea874a 2433 case snd_soc_dapm_regulator_supply:
d7e7eb91 2434 case snd_soc_dapm_clock_supply:
010ff262
MB
2435 case snd_soc_dapm_aif_in:
2436 case snd_soc_dapm_aif_out:
4616274d
MB
2437 case snd_soc_dapm_dai_in:
2438 case snd_soc_dapm_dai_out:
c74184ed 2439 case snd_soc_dapm_dai_link:
57295073 2440 case snd_soc_dapm_kcontrol:
8ddab3f5 2441 list_add(&path->list, &dapm->card->paths);
2b97eabc
RP
2442 list_add(&path->list_sink, &wsink->sources);
2443 list_add(&path->list_source, &wsource->sinks);
2444 path->connect = 1;
2445 return 0;
2446 case snd_soc_dapm_mux:
24ff33ac 2447 case snd_soc_dapm_virt_mux:
74155556 2448 case snd_soc_dapm_value_mux:
ce6120cc 2449 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
82cfecdc 2450 &wsink->kcontrol_news[0]);
2b97eabc
RP
2451 if (ret != 0)
2452 goto err;
2453 break;
2454 case snd_soc_dapm_switch:
2455 case snd_soc_dapm_mixer:
ca9c1aae 2456 case snd_soc_dapm_mixer_named_ctl:
ce6120cc 2457 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2b97eabc
RP
2458 if (ret != 0)
2459 goto err;
2460 break;
2461 case snd_soc_dapm_hp:
2462 case snd_soc_dapm_mic:
2463 case snd_soc_dapm_line:
2464 case snd_soc_dapm_spk:
8ddab3f5 2465 list_add(&path->list, &dapm->card->paths);
2b97eabc
RP
2466 list_add(&path->list_sink, &wsink->sources);
2467 list_add(&path->list_source, &wsource->sinks);
2468 path->connect = 0;
2469 return 0;
2470 }
fabd0384 2471
2b97eabc 2472 return 0;
2553628e
LPC
2473err:
2474 kfree(path);
2475 return ret;
2476}
2b97eabc 2477
2553628e
LPC
2478static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2479 const struct snd_soc_dapm_route *route)
2480{
2481 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2482 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2483 const char *sink;
2484 const char *source;
2485 char prefixed_sink[80];
2486 char prefixed_source[80];
2487 int ret;
2488
2489 if (dapm->codec && dapm->codec->name_prefix) {
2490 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2491 dapm->codec->name_prefix, route->sink);
2492 sink = prefixed_sink;
2493 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2494 dapm->codec->name_prefix, route->source);
2495 source = prefixed_source;
2496 } else {
2497 sink = route->sink;
2498 source = route->source;
2499 }
2500
2501 /*
2502 * find src and dest widgets over all widgets but favor a widget from
2503 * current DAPM context
2504 */
2505 list_for_each_entry(w, &dapm->card->widgets, list) {
2506 if (!wsink && !(strcmp(w->name, sink))) {
2507 wtsink = w;
2508 if (w->dapm == dapm)
2509 wsink = w;
2510 continue;
2511 }
2512 if (!wsource && !(strcmp(w->name, source))) {
2513 wtsource = w;
2514 if (w->dapm == dapm)
2515 wsource = w;
2516 }
2517 }
2518 /* use widget from another DAPM context if not found from this */
2519 if (!wsink)
2520 wsink = wtsink;
2521 if (!wsource)
2522 wsource = wtsource;
2523
2524 if (wsource == NULL) {
2525 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2526 route->source);
2527 return -ENODEV;
2528 }
2529 if (wsink == NULL) {
2530 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2531 route->sink);
2532 return -ENODEV;
2533 }
2534
2535 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2536 route->connected);
2537 if (ret)
2538 goto err;
2539
2540 return 0;
2b97eabc 2541err:
30a6a1a4 2542 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2553628e 2543 source, route->control, sink);
2b97eabc
RP
2544 return ret;
2545}
105f1c28 2546
efcc3c61
MB
2547static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2548 const struct snd_soc_dapm_route *route)
2549{
2550 struct snd_soc_dapm_path *path, *p;
2551 const char *sink;
2552 const char *source;
2553 char prefixed_sink[80];
2554 char prefixed_source[80];
2555
2556 if (route->control) {
2557 dev_err(dapm->dev,
30a6a1a4 2558 "ASoC: Removal of routes with controls not supported\n");
efcc3c61
MB
2559 return -EINVAL;
2560 }
2561
2562 if (dapm->codec && dapm->codec->name_prefix) {
2563 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2564 dapm->codec->name_prefix, route->sink);
2565 sink = prefixed_sink;
2566 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2567 dapm->codec->name_prefix, route->source);
2568 source = prefixed_source;
2569 } else {
2570 sink = route->sink;
2571 source = route->source;
2572 }
2573
2574 path = NULL;
2575 list_for_each_entry(p, &dapm->card->paths, list) {
2576 if (strcmp(p->source->name, source) != 0)
2577 continue;
2578 if (strcmp(p->sink->name, sink) != 0)
2579 continue;
2580 path = p;
2581 break;
2582 }
2583
2584 if (path) {
2585 dapm_mark_dirty(path->source, "Route removed");
2586 dapm_mark_dirty(path->sink, "Route removed");
2587
8872293f 2588 dapm_free_path(path);
efcc3c61 2589 } else {
30a6a1a4 2590 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
efcc3c61
MB
2591 source, sink);
2592 }
2593
2594 return 0;
2595}
2596
105f1c28
MB
2597/**
2598 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
ce6120cc 2599 * @dapm: DAPM context
105f1c28
MB
2600 * @route: audio routes
2601 * @num: number of routes
2602 *
2603 * Connects 2 dapm widgets together via a named audio path. The sink is
2604 * the widget receiving the audio signal, whilst the source is the sender
2605 * of the audio signal.
2606 *
2607 * Returns 0 for success else error. On error all resources can be freed
2608 * with a call to snd_soc_card_free().
2609 */
ce6120cc 2610int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
105f1c28
MB
2611 const struct snd_soc_dapm_route *route, int num)
2612{
62d4a4b9 2613 int i, r, ret = 0;
105f1c28 2614
a73fb2df 2615 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
105f1c28 2616 for (i = 0; i < num; i++) {
62d4a4b9
MB
2617 r = snd_soc_dapm_add_route(dapm, route);
2618 if (r < 0) {
30a6a1a4
LG
2619 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2620 route->source,
2621 route->control ? route->control : "direct",
2622 route->sink);
62d4a4b9 2623 ret = r;
105f1c28
MB
2624 }
2625 route++;
2626 }
a73fb2df 2627 mutex_unlock(&dapm->card->dapm_mutex);
105f1c28 2628
60884c27 2629 return ret;
105f1c28
MB
2630}
2631EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2632
efcc3c61
MB
2633/**
2634 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2635 * @dapm: DAPM context
2636 * @route: audio routes
2637 * @num: number of routes
2638 *
2639 * Removes routes from the DAPM context.
2640 */
2641int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2642 const struct snd_soc_dapm_route *route, int num)
2643{
2644 int i, ret = 0;
2645
2646 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2647 for (i = 0; i < num; i++) {
2648 snd_soc_dapm_del_route(dapm, route);
2649 route++;
2650 }
2651 mutex_unlock(&dapm->card->dapm_mutex);
2652
2653 return ret;
2654}
2655EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2656
bf3a9e13
MB
2657static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2658 const struct snd_soc_dapm_route *route)
2659{
2660 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2661 route->source,
2662 true);
2663 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2664 route->sink,
2665 true);
2666 struct snd_soc_dapm_path *path;
2667 int count = 0;
2668
2669 if (!source) {
30a6a1a4 2670 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
bf3a9e13
MB
2671 route->source);
2672 return -ENODEV;
2673 }
2674
2675 if (!sink) {
30a6a1a4 2676 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
bf3a9e13
MB
2677 route->sink);
2678 return -ENODEV;
2679 }
2680
2681 if (route->control || route->connected)
30a6a1a4 2682 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
bf3a9e13
MB
2683 route->source, route->sink);
2684
2685 list_for_each_entry(path, &source->sinks, list_source) {
2686 if (path->sink == sink) {
2687 path->weak = 1;
2688 count++;
2689 }
2690 }
2691
2692 if (count == 0)
30a6a1a4 2693 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
bf3a9e13
MB
2694 route->source, route->sink);
2695 if (count > 1)
30a6a1a4 2696 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
bf3a9e13
MB
2697 count, route->source, route->sink);
2698
2699 return 0;
2700}
2701
2702/**
2703 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2704 * @dapm: DAPM context
2705 * @route: audio routes
2706 * @num: number of routes
2707 *
2708 * Mark existing routes matching those specified in the passed array
2709 * as being weak, meaning that they are ignored for the purpose of
2710 * power decisions. The main intended use case is for sidetone paths
2711 * which couple audio between other independent paths if they are both
2712 * active in order to make the combination work better at the user
2713 * level but which aren't intended to be "used".
2714 *
2715 * Note that CODEC drivers should not use this as sidetone type paths
2716 * can frequently also be used as bypass paths.
2717 */
2718int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2719 const struct snd_soc_dapm_route *route, int num)
2720{
2721 int i, err;
2722 int ret = 0;
2723
a73fb2df 2724 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
bf3a9e13
MB
2725 for (i = 0; i < num; i++) {
2726 err = snd_soc_dapm_weak_route(dapm, route);
2727 if (err)
2728 ret = err;
2729 route++;
2730 }
a73fb2df 2731 mutex_unlock(&dapm->card->dapm_mutex);
bf3a9e13
MB
2732
2733 return ret;
2734}
2735EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2736
2b97eabc
RP
2737/**
2738 * snd_soc_dapm_new_widgets - add new dapm widgets
ce6120cc 2739 * @dapm: DAPM context
2b97eabc
RP
2740 *
2741 * Checks the codec for any new dapm widgets and creates them if found.
2742 *
2743 * Returns 0 for success.
2744 */
824ef826 2745int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
2b97eabc
RP
2746{
2747 struct snd_soc_dapm_widget *w;
b66a70d5 2748 unsigned int val;
2b97eabc 2749
95dd5cd6 2750 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
a73fb2df 2751
95dd5cd6 2752 list_for_each_entry(w, &card->widgets, list)
2b97eabc
RP
2753 {
2754 if (w->new)
2755 continue;
2756
fad59888
SW
2757 if (w->num_kcontrols) {
2758 w->kcontrols = kzalloc(w->num_kcontrols *
2759 sizeof(struct snd_kcontrol *),
2760 GFP_KERNEL);
a73fb2df 2761 if (!w->kcontrols) {
95dd5cd6 2762 mutex_unlock(&card->dapm_mutex);
fad59888 2763 return -ENOMEM;
a73fb2df 2764 }
fad59888
SW
2765 }
2766
2b97eabc
RP
2767 switch(w->id) {
2768 case snd_soc_dapm_switch:
2769 case snd_soc_dapm_mixer:
ca9c1aae 2770 case snd_soc_dapm_mixer_named_ctl:
4b80b8c2 2771 dapm_new_mixer(w);
2b97eabc
RP
2772 break;
2773 case snd_soc_dapm_mux:
24ff33ac 2774 case snd_soc_dapm_virt_mux:
2e72f8e3 2775 case snd_soc_dapm_value_mux:
4b80b8c2 2776 dapm_new_mux(w);
2b97eabc 2777 break;
2b97eabc 2778 case snd_soc_dapm_pga:
d88429a6 2779 case snd_soc_dapm_out_drv:
4b80b8c2 2780 dapm_new_pga(w);
2b97eabc 2781 break;
7ca3a18b 2782 default:
2b97eabc
RP
2783 break;
2784 }
b66a70d5
MB
2785
2786 /* Read the initial power state from the device */
2787 if (w->reg >= 0) {
f7d3c170
ASL
2788 soc_widget_read(w, w->reg, &val);
2789 val = val >> w->shift;
de9ba98b
LPC
2790 val &= w->mask;
2791 if (val == w->on_val)
b66a70d5
MB
2792 w->power = 1;
2793 }
2794
2b97eabc 2795 w->new = 1;
d5d1e0be 2796
7508b12a 2797 dapm_mark_dirty(w, "new widget");
d5d1e0be 2798 dapm_debugfs_add_widget(w);
2b97eabc
RP
2799 }
2800
95dd5cd6
LPC
2801 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2802 mutex_unlock(&card->dapm_mutex);
2b97eabc
RP
2803 return 0;
2804}
2805EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2806
2807/**
2808 * snd_soc_dapm_get_volsw - dapm mixer get callback
2809 * @kcontrol: mixer control
ac11a2b3 2810 * @ucontrol: control element information
2b97eabc
RP
2811 *
2812 * Callback to get the value of a dapm mixer control.
2813 *
2814 * Returns 0 for success.
2815 */
2816int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2817 struct snd_ctl_elem_value *ucontrol)
2818{
eee5d7f9 2819 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
57295073 2820 struct snd_soc_card *card = codec->card;
4eaa9819
JS
2821 struct soc_mixer_control *mc =
2822 (struct soc_mixer_control *)kcontrol->private_value;
249ce138 2823 int reg = mc->reg;
815ecf8d 2824 unsigned int shift = mc->shift;
4eaa9819 2825 int max = mc->max;
815ecf8d 2826 unsigned int mask = (1 << fls(max)) - 1;
da602ab8 2827 unsigned int invert = mc->invert;
57295073 2828 unsigned int val;
da602ab8
BT
2829
2830 if (snd_soc_volsw_is_stereo(mc))
eee5d7f9 2831 dev_warn(codec->dapm.dev,
30a6a1a4 2832 "ASoC: Control '%s' is stereo, which is not supported\n",
da602ab8 2833 kcontrol->id.name);
2b97eabc 2834
57295073 2835 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
249ce138 2836 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
57295073
LPC
2837 val = (snd_soc_read(codec, reg) >> shift) & mask;
2838 else
2839 val = dapm_kcontrol_get_value(kcontrol);
2840 mutex_unlock(&card->dapm_mutex);
2841
da602ab8 2842 if (invert)
57295073
LPC
2843 ucontrol->value.integer.value[0] = max - val;
2844 else
2845 ucontrol->value.integer.value[0] = val;
2b97eabc
RP
2846
2847 return 0;
2848}
2849EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2850
2851/**
2852 * snd_soc_dapm_put_volsw - dapm mixer set callback
2853 * @kcontrol: mixer control
ac11a2b3 2854 * @ucontrol: control element information
2b97eabc
RP
2855 *
2856 * Callback to set the value of a dapm mixer control.
2857 *
2858 * Returns 0 for success.
2859 */
2860int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2861 struct snd_ctl_elem_value *ucontrol)
2862{
eee5d7f9 2863 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
a73fb2df 2864 struct snd_soc_card *card = codec->card;
4eaa9819
JS
2865 struct soc_mixer_control *mc =
2866 (struct soc_mixer_control *)kcontrol->private_value;
249ce138 2867 int reg = mc->reg;
815ecf8d 2868 unsigned int shift = mc->shift;
4eaa9819 2869 int max = mc->max;
815ecf8d
JS
2870 unsigned int mask = (1 << fls(max)) - 1;
2871 unsigned int invert = mc->invert;
e9cf7049 2872 unsigned int val;
97404f2e
MB
2873 int connect, change;
2874 struct snd_soc_dapm_update update;
52765976 2875 int ret = 0;
2b97eabc 2876
da602ab8 2877 if (snd_soc_volsw_is_stereo(mc))
eee5d7f9 2878 dev_warn(codec->dapm.dev,
30a6a1a4 2879 "ASoC: Control '%s' is stereo, which is not supported\n",
da602ab8
BT
2880 kcontrol->id.name);
2881
2b97eabc 2882 val = (ucontrol->value.integer.value[0] & mask);
8a720718 2883 connect = !!val;
2b97eabc
RP
2884
2885 if (invert)
a7a4ac86 2886 val = max - val;
2b97eabc 2887
3cd04343 2888 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2b97eabc 2889
249ce138 2890 change = dapm_kcontrol_set_value(kcontrol, val);
57295073 2891
249ce138
LPC
2892 if (reg != SND_SOC_NOPM) {
2893 mask = mask << shift;
2894 val = val << shift;
2895
2896 change = snd_soc_test_bits(codec, reg, mask, val);
2897 }
57295073 2898
97404f2e 2899 if (change) {
249ce138
LPC
2900 if (reg != SND_SOC_NOPM) {
2901 update.kcontrol = kcontrol;
2902 update.reg = reg;
2903 update.mask = mask;
2904 update.val = val;
97404f2e 2905
249ce138
LPC
2906 card->update = &update;
2907 }
97404f2e 2908
52765976 2909 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
fafd2176 2910
564c6504 2911 card->update = NULL;
283375ce
MB
2912 }
2913
a73fb2df 2914 mutex_unlock(&card->dapm_mutex);
52765976
NC
2915
2916 if (ret > 0)
2917 soc_dpcm_runtime_update(card);
2918
56a67834 2919 return change;
2b97eabc
RP
2920}
2921EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2922
2923/**
2924 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2925 * @kcontrol: mixer control
ac11a2b3 2926 * @ucontrol: control element information
2b97eabc
RP
2927 *
2928 * Callback to get the value of a dapm enumerated double mixer control.
2929 *
2930 * Returns 0 for success.
2931 */
2932int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2933 struct snd_ctl_elem_value *ucontrol)
2934{
eee5d7f9 2935 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2b97eabc 2936 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
86767b7d 2937 unsigned int val;
2b97eabc 2938
eee5d7f9 2939 val = snd_soc_read(codec, e->reg);
86767b7d 2940 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
2b97eabc
RP
2941 if (e->shift_l != e->shift_r)
2942 ucontrol->value.enumerated.item[1] =
86767b7d 2943 (val >> e->shift_r) & e->mask;
2b97eabc
RP
2944
2945 return 0;
2946}
2947EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2948
2949/**
2950 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2951 * @kcontrol: mixer control
ac11a2b3 2952 * @ucontrol: control element information
2b97eabc
RP
2953 *
2954 * Callback to set the value of a dapm enumerated double mixer control.
2955 *
2956 * Returns 0 for success.
2957 */
2958int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2959 struct snd_ctl_elem_value *ucontrol)
2960{
cf7c1de2 2961 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
a73fb2df 2962 struct snd_soc_card *card = codec->card;
2b97eabc 2963 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3a65577d 2964 unsigned int val, mux, change;
86767b7d 2965 unsigned int mask;
97404f2e 2966 struct snd_soc_dapm_update update;
52765976 2967 int ret = 0;
2b97eabc 2968
f8ba0b7b 2969 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2b97eabc
RP
2970 return -EINVAL;
2971 mux = ucontrol->value.enumerated.item[0];
2972 val = mux << e->shift_l;
86767b7d 2973 mask = e->mask << e->shift_l;
2b97eabc 2974 if (e->shift_l != e->shift_r) {
f8ba0b7b 2975 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2b97eabc
RP
2976 return -EINVAL;
2977 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
86767b7d 2978 mask |= e->mask << e->shift_r;
2b97eabc
RP
2979 }
2980
3cd04343 2981 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
fafd2176 2982
eee5d7f9 2983 change = snd_soc_test_bits(codec, e->reg, mask, val);
fafd2176 2984 if (change) {
ce6cfaf1
LPC
2985 update.kcontrol = kcontrol;
2986 update.reg = e->reg;
2987 update.mask = mask;
2988 update.val = val;
564c6504 2989 card->update = &update;
1642e3d4 2990
52765976 2991 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
1642e3d4 2992
564c6504 2993 card->update = NULL;
fafd2176 2994 }
2b97eabc 2995
a73fb2df 2996 mutex_unlock(&card->dapm_mutex);
52765976
NC
2997
2998 if (ret > 0)
2999 soc_dpcm_runtime_update(card);
3000
97404f2e 3001 return change;
2b97eabc
RP
3002}
3003EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3004
d2b247a8
MB
3005/**
3006 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
3007 * @kcontrol: mixer control
3008 * @ucontrol: control element information
3009 *
3010 * Returns 0 for success.
3011 */
3012int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
3013 struct snd_ctl_elem_value *ucontrol)
3014{
cf7c1de2 3015 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
d2b247a8
MB
3016 return 0;
3017}
3018EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
3019
3020/**
3021 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
3022 * @kcontrol: mixer control
3023 * @ucontrol: control element information
3024 *
3025 * Returns 0 for success.
3026 */
3027int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
3028 struct snd_ctl_elem_value *ucontrol)
3029{
cf7c1de2 3030 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
a73fb2df 3031 struct snd_soc_card *card = codec->card;
cf7c1de2 3032 unsigned int value;
d2b247a8
MB
3033 struct soc_enum *e =
3034 (struct soc_enum *)kcontrol->private_value;
3035 int change;
52765976 3036 int ret = 0;
d2b247a8
MB
3037
3038 if (ucontrol->value.enumerated.item[0] >= e->max)
3039 return -EINVAL;
3040
3cd04343 3041 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
d2b247a8 3042
cf7c1de2
LPC
3043 value = ucontrol->value.enumerated.item[0];
3044 change = dapm_kcontrol_set_value(kcontrol, value);
3045 if (change)
52765976 3046 ret = soc_dapm_mux_update_power(card, kcontrol, value, e);
d2b247a8 3047
a73fb2df 3048 mutex_unlock(&card->dapm_mutex);
52765976
NC
3049
3050 if (ret > 0)
3051 soc_dpcm_runtime_update(card);
3052
56a67834 3053 return change;
d2b247a8
MB
3054}
3055EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
3056
2e72f8e3
PU
3057/**
3058 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
3059 * callback
3060 * @kcontrol: mixer control
3061 * @ucontrol: control element information
3062 *
3063 * Callback to get the value of a dapm semi enumerated double mixer control.
3064 *
3065 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3066 * used for handling bitfield coded enumeration for example.
3067 *
3068 * Returns 0 for success.
3069 */
3070int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
3071 struct snd_ctl_elem_value *ucontrol)
3072{
eee5d7f9 3073 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
74155556 3074 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f 3075 unsigned int reg_val, val, mux;
2e72f8e3 3076
eee5d7f9 3077 reg_val = snd_soc_read(codec, e->reg);
2e72f8e3
PU
3078 val = (reg_val >> e->shift_l) & e->mask;
3079 for (mux = 0; mux < e->max; mux++) {
3080 if (val == e->values[mux])
3081 break;
3082 }
3083 ucontrol->value.enumerated.item[0] = mux;
3084 if (e->shift_l != e->shift_r) {
3085 val = (reg_val >> e->shift_r) & e->mask;
3086 for (mux = 0; mux < e->max; mux++) {
3087 if (val == e->values[mux])
3088 break;
3089 }
3090 ucontrol->value.enumerated.item[1] = mux;
3091 }
3092
3093 return 0;
3094}
3095EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
3096
3097/**
3098 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
3099 * callback
3100 * @kcontrol: mixer control
3101 * @ucontrol: control element information
3102 *
3103 * Callback to set the value of a dapm semi enumerated double mixer control.
3104 *
3105 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3106 * used for handling bitfield coded enumeration for example.
3107 *
3108 * Returns 0 for success.
3109 */
3110int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
3111 struct snd_ctl_elem_value *ucontrol)
3112{
cf7c1de2 3113 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
a73fb2df 3114 struct snd_soc_card *card = codec->card;
74155556 3115 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3a65577d 3116 unsigned int val, mux, change;
46f5822f 3117 unsigned int mask;
97404f2e 3118 struct snd_soc_dapm_update update;
52765976 3119 int ret = 0;
2e72f8e3
PU
3120
3121 if (ucontrol->value.enumerated.item[0] > e->max - 1)
3122 return -EINVAL;
3123 mux = ucontrol->value.enumerated.item[0];
3124 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
3125 mask = e->mask << e->shift_l;
3126 if (e->shift_l != e->shift_r) {
3127 if (ucontrol->value.enumerated.item[1] > e->max - 1)
3128 return -EINVAL;
3129 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
3130 mask |= e->mask << e->shift_r;
3131 }
3132
3cd04343 3133 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
fafd2176 3134
eee5d7f9 3135 change = snd_soc_test_bits(codec, e->reg, mask, val);
fafd2176 3136 if (change) {
ce6cfaf1
LPC
3137 update.kcontrol = kcontrol;
3138 update.reg = e->reg;
3139 update.mask = mask;
3140 update.val = val;
564c6504 3141 card->update = &update;
1642e3d4 3142
52765976 3143 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
fafd2176 3144
564c6504 3145 card->update = NULL;
fafd2176 3146 }
2e72f8e3 3147
a73fb2df 3148 mutex_unlock(&card->dapm_mutex);
52765976
NC
3149
3150 if (ret > 0)
3151 soc_dpcm_runtime_update(card);
3152
97404f2e 3153 return change;
2e72f8e3
PU
3154}
3155EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
3156
8b37dbd2
MB
3157/**
3158 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3159 *
3160 * @kcontrol: mixer control
3161 * @uinfo: control element information
3162 *
3163 * Callback to provide information about a pin switch control.
3164 */
3165int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3166 struct snd_ctl_elem_info *uinfo)
3167{
3168 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3169 uinfo->count = 1;
3170 uinfo->value.integer.min = 0;
3171 uinfo->value.integer.max = 1;
3172
3173 return 0;
3174}
3175EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3176
3177/**
3178 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3179 *
3180 * @kcontrol: mixer control
3181 * @ucontrol: Value
3182 */
3183int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3184 struct snd_ctl_elem_value *ucontrol)
3185{
48a8c394 3186 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
8b37dbd2
MB
3187 const char *pin = (const char *)kcontrol->private_value;
3188
3cd04343 3189 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
8b37dbd2
MB
3190
3191 ucontrol->value.integer.value[0] =
48a8c394 3192 snd_soc_dapm_get_pin_status(&card->dapm, pin);
8b37dbd2 3193
a73fb2df 3194 mutex_unlock(&card->dapm_mutex);
8b37dbd2
MB
3195
3196 return 0;
3197}
3198EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3199
3200/**
3201 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3202 *
3203 * @kcontrol: mixer control
3204 * @ucontrol: Value
3205 */
3206int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3207 struct snd_ctl_elem_value *ucontrol)
3208{
48a8c394 3209 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
8b37dbd2
MB
3210 const char *pin = (const char *)kcontrol->private_value;
3211
3cd04343 3212 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
8b37dbd2
MB
3213
3214 if (ucontrol->value.integer.value[0])
48a8c394 3215 snd_soc_dapm_enable_pin(&card->dapm, pin);
8b37dbd2 3216 else
48a8c394 3217 snd_soc_dapm_disable_pin(&card->dapm, pin);
8b37dbd2 3218
a73fb2df 3219 mutex_unlock(&card->dapm_mutex);
8b37dbd2 3220
a73fb2df 3221 snd_soc_dapm_sync(&card->dapm);
8b37dbd2
MB
3222 return 0;
3223}
3224EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3225
5ba06fc9
MB
3226static struct snd_soc_dapm_widget *
3227snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3228 const struct snd_soc_dapm_widget *widget)
2b97eabc
RP
3229{
3230 struct snd_soc_dapm_widget *w;
62ea874a 3231 int ret;
2b97eabc
RP
3232
3233 if ((w = dapm_cnew_widget(widget)) == NULL)
5ba06fc9 3234 return NULL;
2b97eabc 3235
62ea874a
MB
3236 switch (w->id) {
3237 case snd_soc_dapm_regulator_supply:
a3cc056b
LG
3238 w->regulator = devm_regulator_get(dapm->dev, w->name);
3239 if (IS_ERR(w->regulator)) {
3240 ret = PTR_ERR(w->regulator);
30a6a1a4 3241 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
62ea874a 3242 w->name, ret);
5ba06fc9 3243 return NULL;
62ea874a 3244 }
8784c77a 3245
de9ba98b 3246 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
8784c77a
MB
3247 ret = regulator_allow_bypass(w->regulator, true);
3248 if (ret != 0)
3249 dev_warn(w->dapm->dev,
3250 "ASoC: Failed to unbypass %s: %d\n",
3251 w->name, ret);
3252 }
62ea874a 3253 break;
d7e7eb91 3254 case snd_soc_dapm_clock_supply:
165961ef 3255#ifdef CONFIG_CLKDEV_LOOKUP
695594f1 3256 w->clk = devm_clk_get(dapm->dev, w->name);
d7e7eb91
OL
3257 if (IS_ERR(w->clk)) {
3258 ret = PTR_ERR(w->clk);
30a6a1a4 3259 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
d7e7eb91
OL
3260 w->name, ret);
3261 return NULL;
3262 }
ec02995a
MB
3263#else
3264 return NULL;
3265#endif
d7e7eb91 3266 break;
62ea874a
MB
3267 default:
3268 break;
3269 }
2b97eabc 3270
88e8b9a8 3271 if (dapm->codec && dapm->codec->name_prefix)
2b581074
LPC
3272 w->name = kasprintf(GFP_KERNEL, "%s %s",
3273 dapm->codec->name_prefix, widget->name);
3274 else
3275 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3276
ead9b919
JN
3277 if (w->name == NULL) {
3278 kfree(w);
5ba06fc9 3279 return NULL;
ead9b919 3280 }
ead9b919 3281
7ca3a18b
MB
3282 switch (w->id) {
3283 case snd_soc_dapm_switch:
3284 case snd_soc_dapm_mixer:
3285 case snd_soc_dapm_mixer_named_ctl:
3286 w->power_check = dapm_generic_check_power;
3287 break;
3288 case snd_soc_dapm_mux:
3289 case snd_soc_dapm_virt_mux:
3290 case snd_soc_dapm_value_mux:
3291 w->power_check = dapm_generic_check_power;
3292 break;
4616274d 3293 case snd_soc_dapm_dai_out:
7ca3a18b
MB
3294 w->power_check = dapm_adc_check_power;
3295 break;
4616274d 3296 case snd_soc_dapm_dai_in:
7ca3a18b
MB
3297 w->power_check = dapm_dac_check_power;
3298 break;
63c69a6e
MB
3299 case snd_soc_dapm_adc:
3300 case snd_soc_dapm_aif_out:
3301 case snd_soc_dapm_dac:
3302 case snd_soc_dapm_aif_in:
7ca3a18b
MB
3303 case snd_soc_dapm_pga:
3304 case snd_soc_dapm_out_drv:
3305 case snd_soc_dapm_input:
3306 case snd_soc_dapm_output:
3307 case snd_soc_dapm_micbias:
3308 case snd_soc_dapm_spk:
3309 case snd_soc_dapm_hp:
3310 case snd_soc_dapm_mic:
3311 case snd_soc_dapm_line:
c74184ed 3312 case snd_soc_dapm_dai_link:
7ca3a18b
MB
3313 w->power_check = dapm_generic_check_power;
3314 break;
3315 case snd_soc_dapm_supply:
62ea874a 3316 case snd_soc_dapm_regulator_supply:
d7e7eb91 3317 case snd_soc_dapm_clock_supply:
57295073 3318 case snd_soc_dapm_kcontrol:
7ca3a18b
MB
3319 w->power_check = dapm_supply_check_power;
3320 break;
3321 default:
3322 w->power_check = dapm_always_on_check_power;
3323 break;
3324 }
3325
ce6120cc
LG
3326 w->dapm = dapm;
3327 w->codec = dapm->codec;
b7950641 3328 w->platform = dapm->platform;
2b97eabc
RP
3329 INIT_LIST_HEAD(&w->sources);
3330 INIT_LIST_HEAD(&w->sinks);
3331 INIT_LIST_HEAD(&w->list);
db432b41 3332 INIT_LIST_HEAD(&w->dirty);
97c866de 3333 list_add(&w->list, &dapm->card->widgets);
2b97eabc
RP
3334
3335 /* machine layer set ups unconnected pins and insertions */
3336 w->connected = 1;
5ba06fc9 3337 return w;
2b97eabc 3338}
2b97eabc 3339
4ba1327a
MB
3340/**
3341 * snd_soc_dapm_new_controls - create new dapm controls
ce6120cc 3342 * @dapm: DAPM context
4ba1327a
MB
3343 * @widget: widget array
3344 * @num: number of widgets
3345 *
3346 * Creates new DAPM controls based upon the templates.
3347 *
3348 * Returns 0 for success else error.
3349 */
ce6120cc 3350int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
4ba1327a
MB
3351 const struct snd_soc_dapm_widget *widget,
3352 int num)
3353{
5ba06fc9
MB
3354 struct snd_soc_dapm_widget *w;
3355 int i;
60884c27 3356 int ret = 0;
4ba1327a 3357
a73fb2df 3358 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
4ba1327a 3359 for (i = 0; i < num; i++) {
5ba06fc9
MB
3360 w = snd_soc_dapm_new_control(dapm, widget);
3361 if (!w) {
f7d41ae8 3362 dev_err(dapm->dev,
5ba06fc9
MB
3363 "ASoC: Failed to create DAPM control %s\n",
3364 widget->name);
60884c27
DC
3365 ret = -ENOMEM;
3366 break;
b8b33cb5 3367 }
4ba1327a
MB
3368 widget++;
3369 }
a73fb2df 3370 mutex_unlock(&dapm->card->dapm_mutex);
60884c27 3371 return ret;
4ba1327a
MB
3372}
3373EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3374
c74184ed
MB
3375static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3376 struct snd_kcontrol *kcontrol, int event)
3377{
3378 struct snd_soc_dapm_path *source_p, *sink_p;
3379 struct snd_soc_dai *source, *sink;
3380 const struct snd_soc_pcm_stream *config = w->params;
3381 struct snd_pcm_substream substream;
9747cec2 3382 struct snd_pcm_hw_params *params = NULL;
c74184ed
MB
3383 u64 fmt;
3384 int ret;
3385
bf4edea8
TI
3386 if (WARN_ON(!config) ||
3387 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3388 return -EINVAL;
c74184ed
MB
3389
3390 /* We only support a single source and sink, pick the first */
3391 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3392 list_sink);
3393 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3394 list_source);
3395
bf4edea8
TI
3396 if (WARN_ON(!source_p || !sink_p) ||
3397 WARN_ON(!sink_p->source || !source_p->sink) ||
3398 WARN_ON(!source_p->source || !sink_p->sink))
3399 return -EINVAL;
c74184ed
MB
3400
3401 source = source_p->source->priv;
3402 sink = sink_p->sink->priv;
3403
3404 /* Be a little careful as we don't want to overflow the mask array */
3405 if (config->formats) {
3406 fmt = ffs(config->formats) - 1;
3407 } else {
30a6a1a4 3408 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
c74184ed
MB
3409 config->formats);
3410 fmt = 0;
3411 }
3412
3413 /* Currently very limited parameter selection */
9747cec2
MB
3414 params = kzalloc(sizeof(*params), GFP_KERNEL);
3415 if (!params) {
3416 ret = -ENOMEM;
3417 goto out;
3418 }
3419 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
c74184ed 3420
9747cec2 3421 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
c74184ed 3422 config->rate_min;
9747cec2 3423 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
c74184ed
MB
3424 config->rate_max;
3425
9747cec2 3426 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
c74184ed 3427 = config->channels_min;
9747cec2 3428 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
c74184ed
MB
3429 = config->channels_max;
3430
3431 memset(&substream, 0, sizeof(substream));
3432
3433 switch (event) {
3434 case SND_SOC_DAPM_PRE_PMU:
3435 if (source->driver->ops && source->driver->ops->hw_params) {
3436 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3437 ret = source->driver->ops->hw_params(&substream,
9747cec2 3438 params, source);
c74184ed
MB
3439 if (ret != 0) {
3440 dev_err(source->dev,
30a6a1a4 3441 "ASoC: hw_params() failed: %d\n", ret);
9747cec2 3442 goto out;
c74184ed
MB
3443 }
3444 }
3445
3446 if (sink->driver->ops && sink->driver->ops->hw_params) {
3447 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
9747cec2 3448 ret = sink->driver->ops->hw_params(&substream, params,
c74184ed
MB
3449 sink);
3450 if (ret != 0) {
3451 dev_err(sink->dev,
30a6a1a4 3452 "ASoC: hw_params() failed: %d\n", ret);
9747cec2 3453 goto out;
c74184ed
MB
3454 }
3455 }
3456 break;
3457
3458 case SND_SOC_DAPM_POST_PMU:
da18396f
MB
3459 ret = snd_soc_dai_digital_mute(sink, 0,
3460 SNDRV_PCM_STREAM_PLAYBACK);
c74184ed 3461 if (ret != 0 && ret != -ENOTSUPP)
30a6a1a4 3462 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
9747cec2 3463 ret = 0;
c74184ed
MB
3464 break;
3465
3466 case SND_SOC_DAPM_PRE_PMD:
da18396f
MB
3467 ret = snd_soc_dai_digital_mute(sink, 1,
3468 SNDRV_PCM_STREAM_PLAYBACK);
c74184ed 3469 if (ret != 0 && ret != -ENOTSUPP)
30a6a1a4 3470 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
9747cec2 3471 ret = 0;
c74184ed
MB
3472 break;
3473
3474 default:
a6ed0608 3475 WARN(1, "Unknown event %d\n", event);
c74184ed
MB
3476 return -EINVAL;
3477 }
3478
9747cec2
MB
3479out:
3480 kfree(params);
3481 return ret;
c74184ed
MB
3482}
3483
3484int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3485 const struct snd_soc_pcm_stream *params,
3486 struct snd_soc_dapm_widget *source,
3487 struct snd_soc_dapm_widget *sink)
3488{
3489 struct snd_soc_dapm_route routes[2];
3490 struct snd_soc_dapm_widget template;
3491 struct snd_soc_dapm_widget *w;
3492 size_t len;
3493 char *link_name;
3494
3495 len = strlen(source->name) + strlen(sink->name) + 2;
3496 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3497 if (!link_name)
3498 return -ENOMEM;
3499 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3500
3501 memset(&template, 0, sizeof(template));
3502 template.reg = SND_SOC_NOPM;
3503 template.id = snd_soc_dapm_dai_link;
3504 template.name = link_name;
3505 template.event = snd_soc_dai_link_event;
3506 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3507 SND_SOC_DAPM_PRE_PMD;
3508
30a6a1a4 3509 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
c74184ed
MB
3510
3511 w = snd_soc_dapm_new_control(&card->dapm, &template);
3512 if (!w) {
30a6a1a4 3513 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
c74184ed
MB
3514 link_name);
3515 return -ENOMEM;
3516 }
3517
3518 w->params = params;
3519
3520 memset(&routes, 0, sizeof(routes));
3521
3522 routes[0].source = source->name;
3523 routes[0].sink = link_name;
3524 routes[1].source = link_name;
3525 routes[1].sink = sink->name;
3526
3527 return snd_soc_dapm_add_routes(&card->dapm, routes,
3528 ARRAY_SIZE(routes));
3529}
3530
888df395
MB
3531int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3532 struct snd_soc_dai *dai)
2b97eabc 3533{
888df395 3534 struct snd_soc_dapm_widget template;
2b97eabc
RP
3535 struct snd_soc_dapm_widget *w;
3536
888df395
MB
3537 WARN_ON(dapm->dev != dai->dev);
3538
3539 memset(&template, 0, sizeof(template));
3540 template.reg = SND_SOC_NOPM;
3541
3542 if (dai->driver->playback.stream_name) {
4616274d 3543 template.id = snd_soc_dapm_dai_in;
888df395
MB
3544 template.name = dai->driver->playback.stream_name;
3545 template.sname = dai->driver->playback.stream_name;
3546
30a6a1a4 3547 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
888df395
MB
3548 template.name);
3549
3550 w = snd_soc_dapm_new_control(dapm, &template);
3551 if (!w) {
30a6a1a4 3552 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
888df395 3553 dai->driver->playback.stream_name);
298402a3 3554 return -ENOMEM;
888df395
MB
3555 }
3556
3557 w->priv = dai;
3558 dai->playback_widget = w;
3559 }
3560
3561 if (dai->driver->capture.stream_name) {
4616274d 3562 template.id = snd_soc_dapm_dai_out;
888df395
MB
3563 template.name = dai->driver->capture.stream_name;
3564 template.sname = dai->driver->capture.stream_name;
3565
30a6a1a4 3566 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
888df395
MB
3567 template.name);
3568
3569 w = snd_soc_dapm_new_control(dapm, &template);
3570 if (!w) {
30a6a1a4 3571 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
888df395 3572 dai->driver->capture.stream_name);
298402a3 3573 return -ENOMEM;
888df395
MB
3574 }
3575
3576 w->priv = dai;
3577 dai->capture_widget = w;
3578 }
3579
3580 return 0;
3581}
3582
3583int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3584{
3585 struct snd_soc_dapm_widget *dai_w, *w;
3586 struct snd_soc_dai *dai;
888df395
MB
3587
3588 /* For each DAI widget... */
3589 list_for_each_entry(dai_w, &card->widgets, list) {
4616274d
MB
3590 switch (dai_w->id) {
3591 case snd_soc_dapm_dai_in:
3592 case snd_soc_dapm_dai_out:
3593 break;
3594 default:
2b97eabc 3595 continue;
4616274d 3596 }
888df395
MB
3597
3598 dai = dai_w->priv;
3599
3600 /* ...find all widgets with the same stream and link them */
3601 list_for_each_entry(w, &card->widgets, list) {
3602 if (w->dapm != dai_w->dapm)
3603 continue;
3604
4616274d
MB
3605 switch (w->id) {
3606 case snd_soc_dapm_dai_in:
3607 case snd_soc_dapm_dai_out:
888df395 3608 continue;
4616274d
MB
3609 default:
3610 break;
3611 }
888df395 3612
19c2c5f5 3613 if (!w->sname || !strstr(w->sname, dai_w->name))
888df395
MB
3614 continue;
3615
3616 if (dai->driver->playback.stream_name &&
3617 strstr(w->sname,
3618 dai->driver->playback.stream_name)) {
888df395 3619 dev_dbg(dai->dev, "%s -> %s\n",
2553628e 3620 dai->playback_widget->name, w->name);
888df395 3621
2553628e
LPC
3622 snd_soc_dapm_add_path(w->dapm,
3623 dai->playback_widget, w, NULL, NULL);
888df395
MB
3624 }
3625
3626 if (dai->driver->capture.stream_name &&
3627 strstr(w->sname,
3628 dai->driver->capture.stream_name)) {
888df395 3629 dev_dbg(dai->dev, "%s -> %s\n",
2553628e 3630 w->name, dai->capture_widget->name);
888df395 3631
2553628e
LPC
3632 snd_soc_dapm_add_path(w->dapm, w,
3633 dai->capture_widget, NULL, NULL);
2b97eabc
RP
3634 }
3635 }
3636 }
2b97eabc 3637
888df395
MB
3638 return 0;
3639}
64a648c2 3640
b893ea5f
LG
3641void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3642{
3643 struct snd_soc_pcm_runtime *rtd = card->rtd;
3644 struct snd_soc_dai *cpu_dai, *codec_dai;
3645 struct snd_soc_dapm_route r;
3646 int i;
3647
3648 memset(&r, 0, sizeof(r));
3649
3650 /* for each BE DAI link... */
3651 for (i = 0; i < card->num_rtd; i++) {
3652 rtd = &card->rtd[i];
3653 cpu_dai = rtd->cpu_dai;
3654 codec_dai = rtd->codec_dai;
3655
3656 /* dynamic FE links have no fixed DAI mapping */
3657 if (rtd->dai_link->dynamic)
3658 continue;
3659
3660 /* there is no point in connecting BE DAI links with dummies */
3661 if (snd_soc_dai_is_dummy(codec_dai) ||
3662 snd_soc_dai_is_dummy(cpu_dai))
3663 continue;
3664
3665 /* connect BE DAI playback if widgets are valid */
3666 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3667 r.source = cpu_dai->playback_widget->name;
3668 r.sink = codec_dai->playback_widget->name;
3669 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3670 cpu_dai->codec->name, r.source,
3671 codec_dai->platform->name, r.sink);
3672
3673 snd_soc_dapm_add_route(&card->dapm, &r);
3674 }
3675
3676 /* connect BE DAI capture if widgets are valid */
3677 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3678 r.source = codec_dai->capture_widget->name;
3679 r.sink = cpu_dai->capture_widget->name;
3680 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3681 codec_dai->codec->name, r.source,
3682 cpu_dai->platform->name, r.sink);
3683
3684 snd_soc_dapm_add_route(&card->dapm, &r);
3685 }
3686
3687 }
3688}
3689
d9b0951b
LG
3690static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3691 int event)
2b97eabc 3692{
7bd3a6f3 3693
d9b0951b
LG
3694 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3695 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3696 struct snd_soc_dai *codec_dai = rtd->codec_dai;
7bd3a6f3 3697
d9b0951b
LG
3698 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3699 w_cpu = cpu_dai->playback_widget;
3700 w_codec = codec_dai->playback_widget;
3701 } else {
3702 w_cpu = cpu_dai->capture_widget;
3703 w_codec = codec_dai->capture_widget;
3704 }
2b97eabc 3705
d9b0951b 3706 if (w_cpu) {
fe360685 3707
d9b0951b
LG
3708 dapm_mark_dirty(w_cpu, "stream event");
3709
3710 switch (event) {
3711 case SND_SOC_DAPM_STREAM_START:
3712 w_cpu->active = 1;
3713 break;
3714 case SND_SOC_DAPM_STREAM_STOP:
3715 w_cpu->active = 0;
3716 break;
3717 case SND_SOC_DAPM_STREAM_SUSPEND:
3718 case SND_SOC_DAPM_STREAM_RESUME:
3719 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3720 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3721 break;
3722 }
3723 }
3724
3725 if (w_codec) {
3726
3727 dapm_mark_dirty(w_codec, "stream event");
3728
3729 switch (event) {
3730 case SND_SOC_DAPM_STREAM_START:
3731 w_codec->active = 1;
3732 break;
3733 case SND_SOC_DAPM_STREAM_STOP:
3734 w_codec->active = 0;
3735 break;
3736 case SND_SOC_DAPM_STREAM_SUSPEND:
3737 case SND_SOC_DAPM_STREAM_RESUME:
3738 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3739 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3740 break;
3741 }
2b97eabc 3742 }
2b97eabc 3743
95dd5cd6 3744 dapm_power_widgets(rtd->card, event);
ce6120cc
LG
3745}
3746
3747/**
3748 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3749 * @rtd: PCM runtime data
3750 * @stream: stream name
3751 * @event: stream event
3752 *
3753 * Sends a stream event to the dapm core. The core then makes any
3754 * necessary widget power changes.
3755 *
3756 * Returns 0 for success else error.
3757 */
d9b0951b
LG
3758void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3759 int event)
ce6120cc 3760{
a73fb2df 3761 struct snd_soc_card *card = rtd->card;
ce6120cc 3762
3cd04343 3763 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
d9b0951b 3764 soc_dapm_stream_event(rtd, stream, event);
a73fb2df 3765 mutex_unlock(&card->dapm_mutex);
2b97eabc 3766}
2b97eabc
RP
3767
3768/**
a5302181 3769 * snd_soc_dapm_enable_pin - enable pin.
ce6120cc 3770 * @dapm: DAPM context
a5302181 3771 * @pin: pin name
2b97eabc 3772 *
74b8f955 3773 * Enables input/output pin and its parents or children widgets iff there is
a5302181
LG
3774 * a valid audio route and active audio stream.
3775 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3776 * do any widget power switching.
2b97eabc 3777 */
ce6120cc 3778int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2b97eabc 3779{
ce6120cc 3780 return snd_soc_dapm_set_pin(dapm, pin, 1);
a5302181
LG
3781}
3782EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2b97eabc 3783
da34183e
MB
3784/**
3785 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
ce6120cc 3786 * @dapm: DAPM context
da34183e
MB
3787 * @pin: pin name
3788 *
3789 * Enables input/output pin regardless of any other state. This is
3790 * intended for use with microphone bias supplies used in microphone
3791 * jack detection.
3792 *
3793 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3794 * do any widget power switching.
3795 */
ce6120cc
LG
3796int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3797 const char *pin)
da34183e 3798{
91a5fca4 3799 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
da34183e 3800
91a5fca4 3801 if (!w) {
30a6a1a4 3802 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
91a5fca4 3803 return -EINVAL;
0d86733c
MB
3804 }
3805
30a6a1a4 3806 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
91a5fca4
LPC
3807 w->connected = 1;
3808 w->force = 1;
75c1f891 3809 dapm_mark_dirty(w, "force enable");
da34183e 3810
91a5fca4 3811 return 0;
da34183e
MB
3812}
3813EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3814
a5302181
LG
3815/**
3816 * snd_soc_dapm_disable_pin - disable pin.
ce6120cc 3817 * @dapm: DAPM context
a5302181
LG
3818 * @pin: pin name
3819 *
74b8f955 3820 * Disables input/output pin and its parents or children widgets.
a5302181
LG
3821 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3822 * do any widget power switching.
3823 */
ce6120cc
LG
3824int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3825 const char *pin)
a5302181 3826{
ce6120cc 3827 return snd_soc_dapm_set_pin(dapm, pin, 0);
2b97eabc 3828}
a5302181 3829EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2b97eabc 3830
5817b52a
MB
3831/**
3832 * snd_soc_dapm_nc_pin - permanently disable pin.
ce6120cc 3833 * @dapm: DAPM context
5817b52a
MB
3834 * @pin: pin name
3835 *
3836 * Marks the specified pin as being not connected, disabling it along
3837 * any parent or child widgets. At present this is identical to
3838 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3839 * additional things such as disabling controls which only affect
3840 * paths through the pin.
3841 *
3842 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3843 * do any widget power switching.
3844 */
ce6120cc 3845int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
5817b52a 3846{
ce6120cc 3847 return snd_soc_dapm_set_pin(dapm, pin, 0);
5817b52a
MB
3848}
3849EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3850
eeec12bf 3851/**
a5302181 3852 * snd_soc_dapm_get_pin_status - get audio pin status
ce6120cc 3853 * @dapm: DAPM context
a5302181 3854 * @pin: audio signal pin endpoint (or start point)
eeec12bf 3855 *
a5302181 3856 * Get audio pin status - connected or disconnected.
eeec12bf 3857 *
a5302181 3858 * Returns 1 for connected otherwise 0.
eeec12bf 3859 */
ce6120cc
LG
3860int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3861 const char *pin)
eeec12bf 3862{
91a5fca4 3863 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
eeec12bf 3864
91a5fca4
LPC
3865 if (w)
3866 return w->connected;
a68b38ad 3867
eeec12bf
GG
3868 return 0;
3869}
a5302181 3870EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
eeec12bf 3871
1547aba9
MB
3872/**
3873 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
ce6120cc 3874 * @dapm: DAPM context
1547aba9
MB
3875 * @pin: audio signal pin endpoint (or start point)
3876 *
3877 * Mark the given endpoint or pin as ignoring suspend. When the
3878 * system is disabled a path between two endpoints flagged as ignoring
3879 * suspend will not be disabled. The path must already be enabled via
3880 * normal means at suspend time, it will not be turned on if it was not
3881 * already enabled.
3882 */
ce6120cc
LG
3883int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3884 const char *pin)
1547aba9 3885{
91a5fca4 3886 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
1547aba9 3887
91a5fca4 3888 if (!w) {
30a6a1a4 3889 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
91a5fca4 3890 return -EINVAL;
1547aba9
MB
3891 }
3892
91a5fca4
LPC
3893 w->ignore_suspend = 1;
3894
3895 return 0;
1547aba9
MB
3896}
3897EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3898
1633281b
SW
3899static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3900 struct snd_soc_dapm_widget *w)
3901{
3902 struct snd_soc_dapm_path *p;
3903
3904 list_for_each_entry(p, &card->paths, list) {
3905 if ((p->source == w) || (p->sink == w)) {
3906 dev_dbg(card->dev,
3907 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3908 p->source->name, p->source->id, p->source->dapm,
3909 p->sink->name, p->sink->id, p->sink->dapm);
3910
3911 /* Connected to something other than the codec */
3912 if (p->source->dapm != p->sink->dapm)
3913 return true;
3914 /*
3915 * Loopback connection from codec external pin to
3916 * codec external pin
3917 */
3918 if (p->sink->id == snd_soc_dapm_input) {
3919 switch (p->source->id) {
3920 case snd_soc_dapm_output:
3921 case snd_soc_dapm_micbias:
3922 return true;
3923 default:
3924 break;
3925 }
3926 }
3927 }
3928 }
3929
3930 return false;
3931}
3932
3933/**
3934 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3935 * @codec: The codec whose pins should be processed
3936 *
3937 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3938 * which are unused. Pins are used if they are connected externally to the
3939 * codec, whether that be to some other device, or a loop-back connection to
3940 * the codec itself.
3941 */
3942void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3943{
3944 struct snd_soc_card *card = codec->card;
3945 struct snd_soc_dapm_context *dapm = &codec->dapm;
3946 struct snd_soc_dapm_widget *w;
3947
30a6a1a4 3948 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
1633281b
SW
3949 &card->dapm, &codec->dapm);
3950
3951 list_for_each_entry(w, &card->widgets, list) {
3952 if (w->dapm != dapm)
3953 continue;
3954 switch (w->id) {
3955 case snd_soc_dapm_input:
3956 case snd_soc_dapm_output:
3957 case snd_soc_dapm_micbias:
30a6a1a4 3958 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
1633281b
SW
3959 w->name);
3960 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
a094b80b 3961 dev_dbg(codec->dev,
1633281b
SW
3962 "... Not in map; disabling\n");
3963 snd_soc_dapm_nc_pin(dapm, w->name);
3964 }
3965 break;
3966 default:
3967 break;
3968 }
3969 }
3970}
3971
2b97eabc
RP
3972/**
3973 * snd_soc_dapm_free - free dapm resources
728a5222 3974 * @dapm: DAPM context
2b97eabc
RP
3975 *
3976 * Free all dapm widgets and resources.
3977 */
ce6120cc 3978void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2b97eabc 3979{
ce6120cc 3980 snd_soc_dapm_sys_remove(dapm->dev);
6c45e126 3981 dapm_debugfs_cleanup(dapm);
ce6120cc 3982 dapm_free_widgets(dapm);
7be31be8 3983 list_del(&dapm->list);
2b97eabc
RP
3984}
3985EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3986
ce6120cc 3987static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
51737470 3988{
01005a72 3989 struct snd_soc_card *card = dapm->card;
51737470
MB
3990 struct snd_soc_dapm_widget *w;
3991 LIST_HEAD(down_list);
3992 int powerdown = 0;
3993
01005a72
LG
3994 mutex_lock(&card->dapm_mutex);
3995
97c866de
JN
3996 list_for_each_entry(w, &dapm->card->widgets, list) {
3997 if (w->dapm != dapm)
3998 continue;
51737470 3999 if (w->power) {
828a842f 4000 dapm_seq_insert(w, &down_list, false);
c2caa4da 4001 w->power = 0;
51737470
MB
4002 powerdown = 1;
4003 }
4004 }
4005
4006 /* If there were no widgets to power down we're already in
4007 * standby.
4008 */
4009 if (powerdown) {
7679e42e
MB
4010 if (dapm->bias_level == SND_SOC_BIAS_ON)
4011 snd_soc_dapm_set_bias_level(dapm,
4012 SND_SOC_BIAS_PREPARE);
95dd5cd6 4013 dapm_seq_run(card, &down_list, 0, false);
7679e42e
MB
4014 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4015 snd_soc_dapm_set_bias_level(dapm,
4016 SND_SOC_BIAS_STANDBY);
51737470 4017 }
01005a72
LG
4018
4019 mutex_unlock(&card->dapm_mutex);
f0fba2ad
LG
4020}
4021
4022/*
4023 * snd_soc_dapm_shutdown - callback for system shutdown
4024 */
4025void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4026{
4027 struct snd_soc_codec *codec;
4028
445632ad 4029 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
ce6120cc 4030 soc_dapm_shutdown_codec(&codec->dapm);
7679e42e
MB
4031 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4032 snd_soc_dapm_set_bias_level(&codec->dapm,
4033 SND_SOC_BIAS_OFF);
ce6120cc 4034 }
51737470
MB
4035}
4036
2b97eabc 4037/* Module information */
d331124d 4038MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2b97eabc
RP
4039MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4040MODULE_LICENSE("GPL");