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