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