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