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