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