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