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