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