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