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