]>
Commit | Line | Data |
---|---|---|
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 RP |
16 | * o Platform power domain - can support external components i.e. amps and |
17 | * mic/meadphone insertion events. | |
18 | * o Automatic Mic Bias support | |
19 | * o Jack insertion power event initiation - e.g. hp insertion will enable | |
20 | * sinks, dacs, etc | |
3a4fa0a2 | 21 | * o Delayed powerdown of audio susbsystem to reduce pops between a quick |
2b97eabc RP |
22 | * device reopen. |
23 | * | |
24 | * Todo: | |
25 | * o DAPM power change sequencing - allow for configurable per | |
26 | * codec sequences. | |
27 | * o Support for analogue bias optimisation. | |
28 | * o Support for reduced codec oversampling rates. | |
29 | * o Support for reduced codec bias currents. | |
30 | */ | |
31 | ||
32 | #include <linux/module.h> | |
33 | #include <linux/moduleparam.h> | |
34 | #include <linux/init.h> | |
9d0624a7 | 35 | #include <linux/async.h> |
2b97eabc RP |
36 | #include <linux/delay.h> |
37 | #include <linux/pm.h> | |
38 | #include <linux/bitops.h> | |
39 | #include <linux/platform_device.h> | |
40 | #include <linux/jiffies.h> | |
20496ff3 | 41 | #include <linux/debugfs.h> |
5a0e3ad6 | 42 | #include <linux/slab.h> |
2b97eabc RP |
43 | #include <sound/core.h> |
44 | #include <sound/pcm.h> | |
45 | #include <sound/pcm_params.h> | |
ce6120cc | 46 | #include <sound/soc.h> |
2b97eabc RP |
47 | #include <sound/initval.h> |
48 | ||
84e90930 MB |
49 | #include <trace/events/asoc.h> |
50 | ||
2b97eabc RP |
51 | /* dapm power sequences - make this per codec in the future */ |
52 | static int dapm_up_seq[] = { | |
38357ab2 MB |
53 | [snd_soc_dapm_pre] = 0, |
54 | [snd_soc_dapm_supply] = 1, | |
55 | [snd_soc_dapm_micbias] = 2, | |
010ff262 MB |
56 | [snd_soc_dapm_aif_in] = 3, |
57 | [snd_soc_dapm_aif_out] = 3, | |
58 | [snd_soc_dapm_mic] = 4, | |
59 | [snd_soc_dapm_mux] = 5, | |
24ff33ac | 60 | [snd_soc_dapm_virt_mux] = 5, |
010ff262 MB |
61 | [snd_soc_dapm_value_mux] = 5, |
62 | [snd_soc_dapm_dac] = 6, | |
63 | [snd_soc_dapm_mixer] = 7, | |
64 | [snd_soc_dapm_mixer_named_ctl] = 7, | |
65 | [snd_soc_dapm_pga] = 8, | |
66 | [snd_soc_dapm_adc] = 9, | |
d88429a6 | 67 | [snd_soc_dapm_out_drv] = 10, |
010ff262 MB |
68 | [snd_soc_dapm_hp] = 10, |
69 | [snd_soc_dapm_spk] = 10, | |
70 | [snd_soc_dapm_post] = 11, | |
2b97eabc | 71 | }; |
ca9c1aae | 72 | |
2b97eabc | 73 | static int dapm_down_seq[] = { |
38357ab2 MB |
74 | [snd_soc_dapm_pre] = 0, |
75 | [snd_soc_dapm_adc] = 1, | |
76 | [snd_soc_dapm_hp] = 2, | |
1ca04065 | 77 | [snd_soc_dapm_spk] = 2, |
d88429a6 | 78 | [snd_soc_dapm_out_drv] = 2, |
38357ab2 MB |
79 | [snd_soc_dapm_pga] = 4, |
80 | [snd_soc_dapm_mixer_named_ctl] = 5, | |
e3d4dabd MB |
81 | [snd_soc_dapm_mixer] = 5, |
82 | [snd_soc_dapm_dac] = 6, | |
83 | [snd_soc_dapm_mic] = 7, | |
84 | [snd_soc_dapm_micbias] = 8, | |
85 | [snd_soc_dapm_mux] = 9, | |
24ff33ac | 86 | [snd_soc_dapm_virt_mux] = 9, |
e3d4dabd | 87 | [snd_soc_dapm_value_mux] = 9, |
010ff262 MB |
88 | [snd_soc_dapm_aif_in] = 10, |
89 | [snd_soc_dapm_aif_out] = 10, | |
90 | [snd_soc_dapm_supply] = 11, | |
91 | [snd_soc_dapm_post] = 12, | |
2b97eabc RP |
92 | }; |
93 | ||
12ef193d | 94 | static void pop_wait(u32 pop_time) |
15e4c72f MB |
95 | { |
96 | if (pop_time) | |
97 | schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time)); | |
98 | } | |
99 | ||
fd8d3bc0 | 100 | static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...) |
15e4c72f MB |
101 | { |
102 | va_list args; | |
fd8d3bc0 | 103 | char *buf; |
15e4c72f | 104 | |
fd8d3bc0 JN |
105 | if (!pop_time) |
106 | return; | |
15e4c72f | 107 | |
fd8d3bc0 JN |
108 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
109 | if (buf == NULL) | |
110 | return; | |
15e4c72f | 111 | |
fd8d3bc0 JN |
112 | va_start(args, fmt); |
113 | vsnprintf(buf, PAGE_SIZE, fmt, args); | |
9d01df06 | 114 | dev_info(dev, "%s", buf); |
15e4c72f | 115 | va_end(args); |
fd8d3bc0 JN |
116 | |
117 | kfree(buf); | |
15e4c72f MB |
118 | } |
119 | ||
2b97eabc | 120 | /* create a new dapm widget */ |
88cb4290 | 121 | static inline struct snd_soc_dapm_widget *dapm_cnew_widget( |
2b97eabc RP |
122 | const struct snd_soc_dapm_widget *_widget) |
123 | { | |
88cb4290 | 124 | return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL); |
2b97eabc RP |
125 | } |
126 | ||
452c5eaa MB |
127 | /** |
128 | * snd_soc_dapm_set_bias_level - set the bias level for the system | |
ed5a4c47 | 129 | * @dapm: DAPM context |
452c5eaa MB |
130 | * @level: level to configure |
131 | * | |
132 | * Configure the bias (power) levels for the SoC audio device. | |
133 | * | |
134 | * Returns 0 for success else error. | |
135 | */ | |
ed5a4c47 | 136 | static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm, |
ce6120cc | 137 | enum snd_soc_bias_level level) |
452c5eaa | 138 | { |
ed5a4c47 | 139 | struct snd_soc_card *card = dapm->card; |
452c5eaa MB |
140 | int ret = 0; |
141 | ||
f83fba8b MB |
142 | switch (level) { |
143 | case SND_SOC_BIAS_ON: | |
ce6120cc | 144 | dev_dbg(dapm->dev, "Setting full bias\n"); |
f83fba8b MB |
145 | break; |
146 | case SND_SOC_BIAS_PREPARE: | |
ce6120cc | 147 | dev_dbg(dapm->dev, "Setting bias prepare\n"); |
f83fba8b MB |
148 | break; |
149 | case SND_SOC_BIAS_STANDBY: | |
ce6120cc | 150 | dev_dbg(dapm->dev, "Setting standby bias\n"); |
f83fba8b MB |
151 | break; |
152 | case SND_SOC_BIAS_OFF: | |
ce6120cc | 153 | dev_dbg(dapm->dev, "Setting bias off\n"); |
f83fba8b MB |
154 | break; |
155 | default: | |
ce6120cc | 156 | dev_err(dapm->dev, "Setting invalid bias %d\n", level); |
f83fba8b MB |
157 | return -EINVAL; |
158 | } | |
159 | ||
84e90930 MB |
160 | trace_snd_soc_bias_level_start(card, level); |
161 | ||
f0fba2ad | 162 | if (card && card->set_bias_level) |
452c5eaa | 163 | ret = card->set_bias_level(card, level); |
474e09ca | 164 | if (ret == 0) { |
ce6120cc LG |
165 | if (dapm->codec && dapm->codec->driver->set_bias_level) |
166 | ret = dapm->codec->driver->set_bias_level(dapm->codec, level); | |
474e09ca | 167 | else |
ce6120cc | 168 | dapm->bias_level = level; |
474e09ca | 169 | } |
1badabd9 MB |
170 | if (ret == 0) { |
171 | if (card && card->set_bias_level_post) | |
172 | ret = card->set_bias_level_post(card, level); | |
173 | } | |
452c5eaa | 174 | |
84e90930 MB |
175 | trace_snd_soc_bias_level_done(card, level); |
176 | ||
452c5eaa MB |
177 | return ret; |
178 | } | |
179 | ||
2b97eabc RP |
180 | /* set up initial codec paths */ |
181 | static void dapm_set_path_status(struct snd_soc_dapm_widget *w, | |
182 | struct snd_soc_dapm_path *p, int i) | |
183 | { | |
184 | switch (w->id) { | |
185 | case snd_soc_dapm_switch: | |
ca9c1aae IM |
186 | case snd_soc_dapm_mixer: |
187 | case snd_soc_dapm_mixer_named_ctl: { | |
2b97eabc | 188 | int val; |
4eaa9819 | 189 | struct soc_mixer_control *mc = (struct soc_mixer_control *) |
82cfecdc | 190 | w->kcontrol_news[i].private_value; |
815ecf8d JS |
191 | unsigned int reg = mc->reg; |
192 | unsigned int shift = mc->shift; | |
4eaa9819 | 193 | int max = mc->max; |
815ecf8d JS |
194 | unsigned int mask = (1 << fls(max)) - 1; |
195 | unsigned int invert = mc->invert; | |
2b97eabc RP |
196 | |
197 | val = snd_soc_read(w->codec, reg); | |
198 | val = (val >> shift) & mask; | |
199 | ||
200 | if ((invert && !val) || (!invert && val)) | |
201 | p->connect = 1; | |
202 | else | |
203 | p->connect = 0; | |
204 | } | |
205 | break; | |
206 | case snd_soc_dapm_mux: { | |
82cfecdc SW |
207 | struct soc_enum *e = (struct soc_enum *) |
208 | w->kcontrol_news[i].private_value; | |
2b97eabc RP |
209 | int val, item, bitmask; |
210 | ||
f8ba0b7b | 211 | for (bitmask = 1; bitmask < e->max; bitmask <<= 1) |
2b97eabc RP |
212 | ; |
213 | val = snd_soc_read(w->codec, e->reg); | |
214 | item = (val >> e->shift_l) & (bitmask - 1); | |
215 | ||
216 | p->connect = 0; | |
f8ba0b7b | 217 | for (i = 0; i < e->max; i++) { |
2b97eabc RP |
218 | if (!(strcmp(p->name, e->texts[i])) && item == i) |
219 | p->connect = 1; | |
220 | } | |
221 | } | |
222 | break; | |
24ff33ac | 223 | case snd_soc_dapm_virt_mux: { |
82cfecdc SW |
224 | struct soc_enum *e = (struct soc_enum *) |
225 | w->kcontrol_news[i].private_value; | |
24ff33ac DP |
226 | |
227 | p->connect = 0; | |
228 | /* since a virtual mux has no backing registers to | |
229 | * decide which path to connect, it will try to match | |
230 | * with the first enumeration. This is to ensure | |
231 | * that the default mux choice (the first) will be | |
232 | * correctly powered up during initialization. | |
233 | */ | |
234 | if (!strcmp(p->name, e->texts[0])) | |
235 | p->connect = 1; | |
236 | } | |
237 | break; | |
2e72f8e3 | 238 | case snd_soc_dapm_value_mux: { |
74155556 | 239 | struct soc_enum *e = (struct soc_enum *) |
82cfecdc | 240 | w->kcontrol_news[i].private_value; |
2e72f8e3 PU |
241 | int val, item; |
242 | ||
243 | val = snd_soc_read(w->codec, e->reg); | |
244 | val = (val >> e->shift_l) & e->mask; | |
245 | for (item = 0; item < e->max; item++) { | |
246 | if (val == e->values[item]) | |
247 | break; | |
248 | } | |
249 | ||
250 | p->connect = 0; | |
251 | for (i = 0; i < e->max; i++) { | |
252 | if (!(strcmp(p->name, e->texts[i])) && item == i) | |
253 | p->connect = 1; | |
254 | } | |
255 | } | |
256 | break; | |
2b97eabc RP |
257 | /* does not effect routing - always connected */ |
258 | case snd_soc_dapm_pga: | |
d88429a6 | 259 | case snd_soc_dapm_out_drv: |
2b97eabc RP |
260 | case snd_soc_dapm_output: |
261 | case snd_soc_dapm_adc: | |
262 | case snd_soc_dapm_input: | |
263 | case snd_soc_dapm_dac: | |
264 | case snd_soc_dapm_micbias: | |
265 | case snd_soc_dapm_vmid: | |
246d0a17 | 266 | case snd_soc_dapm_supply: |
010ff262 MB |
267 | case snd_soc_dapm_aif_in: |
268 | case snd_soc_dapm_aif_out: | |
2b97eabc RP |
269 | p->connect = 1; |
270 | break; | |
271 | /* does effect routing - dynamically connected */ | |
272 | case snd_soc_dapm_hp: | |
273 | case snd_soc_dapm_mic: | |
274 | case snd_soc_dapm_spk: | |
275 | case snd_soc_dapm_line: | |
276 | case snd_soc_dapm_pre: | |
277 | case snd_soc_dapm_post: | |
278 | p->connect = 0; | |
279 | break; | |
280 | } | |
281 | } | |
282 | ||
74b8f955 | 283 | /* connect mux widget to its interconnecting audio paths */ |
ce6120cc | 284 | static int dapm_connect_mux(struct snd_soc_dapm_context *dapm, |
2b97eabc RP |
285 | struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest, |
286 | struct snd_soc_dapm_path *path, const char *control_name, | |
287 | const struct snd_kcontrol_new *kcontrol) | |
288 | { | |
289 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; | |
290 | int i; | |
291 | ||
f8ba0b7b | 292 | for (i = 0; i < e->max; i++) { |
2b97eabc | 293 | if (!(strcmp(control_name, e->texts[i]))) { |
8ddab3f5 | 294 | list_add(&path->list, &dapm->card->paths); |
2b97eabc RP |
295 | list_add(&path->list_sink, &dest->sources); |
296 | list_add(&path->list_source, &src->sinks); | |
297 | path->name = (char*)e->texts[i]; | |
298 | dapm_set_path_status(dest, path, 0); | |
299 | return 0; | |
300 | } | |
301 | } | |
302 | ||
303 | return -ENODEV; | |
304 | } | |
305 | ||
74b8f955 | 306 | /* connect mixer widget to its interconnecting audio paths */ |
ce6120cc | 307 | static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm, |
2b97eabc RP |
308 | struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest, |
309 | struct snd_soc_dapm_path *path, const char *control_name) | |
310 | { | |
311 | int i; | |
312 | ||
313 | /* search for mixer kcontrol */ | |
314 | for (i = 0; i < dest->num_kcontrols; i++) { | |
82cfecdc | 315 | if (!strcmp(control_name, dest->kcontrol_news[i].name)) { |
8ddab3f5 | 316 | list_add(&path->list, &dapm->card->paths); |
2b97eabc RP |
317 | list_add(&path->list_sink, &dest->sources); |
318 | list_add(&path->list_source, &src->sinks); | |
82cfecdc | 319 | path->name = dest->kcontrol_news[i].name; |
2b97eabc RP |
320 | dapm_set_path_status(dest, path, i); |
321 | return 0; | |
322 | } | |
323 | } | |
324 | return -ENODEV; | |
325 | } | |
326 | ||
2b97eabc | 327 | /* create new dapm mixer control */ |
ce6120cc | 328 | static int dapm_new_mixer(struct snd_soc_dapm_context *dapm, |
2b97eabc RP |
329 | struct snd_soc_dapm_widget *w) |
330 | { | |
331 | int i, ret = 0; | |
3e5ff4df | 332 | size_t name_len, prefix_len; |
2b97eabc | 333 | struct snd_soc_dapm_path *path; |
12ea2c78 | 334 | struct snd_card *card = dapm->card->snd_card; |
efb7ac3f | 335 | const char *prefix; |
fafd2176 SW |
336 | struct snd_soc_dapm_widget_list *wlist; |
337 | size_t wlistsize; | |
efb7ac3f MB |
338 | |
339 | if (dapm->codec) | |
340 | prefix = dapm->codec->name_prefix; | |
341 | else | |
342 | prefix = NULL; | |
2b97eabc | 343 | |
3e5ff4df MB |
344 | if (prefix) |
345 | prefix_len = strlen(prefix) + 1; | |
346 | else | |
347 | prefix_len = 0; | |
348 | ||
2b97eabc RP |
349 | /* add kcontrol */ |
350 | for (i = 0; i < w->num_kcontrols; i++) { | |
351 | ||
352 | /* match name */ | |
353 | list_for_each_entry(path, &w->sources, list_sink) { | |
354 | ||
355 | /* mixer/mux paths name must match control name */ | |
82cfecdc | 356 | if (path->name != (char *)w->kcontrol_news[i].name) |
2b97eabc RP |
357 | continue; |
358 | ||
fafd2176 SW |
359 | wlistsize = sizeof(struct snd_soc_dapm_widget_list) + |
360 | sizeof(struct snd_soc_dapm_widget *), | |
361 | wlist = kzalloc(wlistsize, GFP_KERNEL); | |
362 | if (wlist == NULL) { | |
363 | dev_err(dapm->dev, | |
364 | "asoc: can't allocate widget list for %s\n", | |
365 | w->name); | |
366 | return -ENOMEM; | |
367 | } | |
368 | wlist->num_widgets = 1; | |
369 | wlist->widgets[0] = w; | |
370 | ||
ca9c1aae IM |
371 | /* add dapm control with long name. |
372 | * for dapm_mixer this is the concatenation of the | |
373 | * mixer and kcontrol name. | |
374 | * for dapm_mixer_named_ctl this is simply the | |
375 | * kcontrol name. | |
376 | */ | |
82cfecdc | 377 | name_len = strlen(w->kcontrol_news[i].name) + 1; |
07495f3e | 378 | if (w->id != snd_soc_dapm_mixer_named_ctl) |
ca9c1aae IM |
379 | name_len += 1 + strlen(w->name); |
380 | ||
219b93f5 | 381 | path->long_name = kmalloc(name_len, GFP_KERNEL); |
ca9c1aae | 382 | |
fafd2176 SW |
383 | if (path->long_name == NULL) { |
384 | kfree(wlist); | |
2b97eabc | 385 | return -ENOMEM; |
fafd2176 | 386 | } |
2b97eabc | 387 | |
ca9c1aae | 388 | switch (w->id) { |
ca9c1aae | 389 | default: |
3e5ff4df MB |
390 | /* The control will get a prefix from |
391 | * the control creation process but | |
392 | * we're also using the same prefix | |
393 | * for widgets so cut the prefix off | |
394 | * the front of the widget name. | |
395 | */ | |
ca9c1aae | 396 | snprintf(path->long_name, name_len, "%s %s", |
3e5ff4df | 397 | w->name + prefix_len, |
82cfecdc | 398 | w->kcontrol_news[i].name); |
07495f3e | 399 | break; |
ca9c1aae IM |
400 | case snd_soc_dapm_mixer_named_ctl: |
401 | snprintf(path->long_name, name_len, "%s", | |
82cfecdc | 402 | w->kcontrol_news[i].name); |
07495f3e | 403 | break; |
ca9c1aae IM |
404 | } |
405 | ||
219b93f5 MB |
406 | path->long_name[name_len - 1] = '\0'; |
407 | ||
fafd2176 SW |
408 | path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i], |
409 | wlist, path->long_name, | |
410 | prefix); | |
ce6120cc | 411 | ret = snd_ctl_add(card, path->kcontrol); |
2b97eabc | 412 | if (ret < 0) { |
f7d41ae8 JN |
413 | dev_err(dapm->dev, |
414 | "asoc: failed to add dapm kcontrol %s: %d\n", | |
415 | path->long_name, ret); | |
fafd2176 | 416 | kfree(wlist); |
2b97eabc RP |
417 | kfree(path->long_name); |
418 | path->long_name = NULL; | |
419 | return ret; | |
420 | } | |
fad59888 | 421 | w->kcontrols[i] = path->kcontrol; |
2b97eabc RP |
422 | } |
423 | } | |
424 | return ret; | |
425 | } | |
426 | ||
427 | /* create new dapm mux control */ | |
ce6120cc | 428 | static int dapm_new_mux(struct snd_soc_dapm_context *dapm, |
2b97eabc RP |
429 | struct snd_soc_dapm_widget *w) |
430 | { | |
431 | struct snd_soc_dapm_path *path = NULL; | |
432 | struct snd_kcontrol *kcontrol; | |
12ea2c78 | 433 | struct snd_card *card = dapm->card->snd_card; |
efb7ac3f | 434 | const char *prefix; |
3e5ff4df | 435 | size_t prefix_len; |
2b97eabc | 436 | int ret = 0; |
fafd2176 SW |
437 | struct snd_soc_dapm_widget_list *wlist; |
438 | size_t wlistsize; | |
2b97eabc RP |
439 | |
440 | if (!w->num_kcontrols) { | |
f7d41ae8 | 441 | dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name); |
2b97eabc RP |
442 | return -EINVAL; |
443 | } | |
444 | ||
fafd2176 SW |
445 | wlistsize = sizeof(struct snd_soc_dapm_widget_list) + |
446 | sizeof(struct snd_soc_dapm_widget *), | |
447 | wlist = kzalloc(wlistsize, GFP_KERNEL); | |
448 | if (wlist == NULL) { | |
449 | dev_err(dapm->dev, | |
450 | "asoc: can't allocate widget list for %s\n", w->name); | |
451 | return -ENOMEM; | |
452 | } | |
453 | wlist->num_widgets = 1; | |
454 | wlist->widgets[0] = w; | |
455 | ||
efb7ac3f MB |
456 | if (dapm->codec) |
457 | prefix = dapm->codec->name_prefix; | |
458 | else | |
459 | prefix = NULL; | |
460 | ||
3e5ff4df MB |
461 | if (prefix) |
462 | prefix_len = strlen(prefix) + 1; | |
463 | else | |
464 | prefix_len = 0; | |
465 | ||
466 | /* The control will get a prefix from the control creation | |
467 | * process but we're also using the same prefix for widgets so | |
468 | * cut the prefix off the front of the widget name. | |
469 | */ | |
fafd2176 SW |
470 | kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist, |
471 | w->name + prefix_len, prefix); | |
ce6120cc LG |
472 | ret = snd_ctl_add(card, kcontrol); |
473 | ||
2b97eabc RP |
474 | if (ret < 0) |
475 | goto err; | |
476 | ||
fad59888 SW |
477 | w->kcontrols[0] = kcontrol; |
478 | ||
2b97eabc RP |
479 | list_for_each_entry(path, &w->sources, list_sink) |
480 | path->kcontrol = kcontrol; | |
481 | ||
482 | return ret; | |
483 | ||
484 | err: | |
f7d41ae8 | 485 | dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name); |
fafd2176 | 486 | kfree(wlist); |
2b97eabc RP |
487 | return ret; |
488 | } | |
489 | ||
490 | /* create new dapm volume control */ | |
ce6120cc | 491 | static int dapm_new_pga(struct snd_soc_dapm_context *dapm, |
2b97eabc RP |
492 | struct snd_soc_dapm_widget *w) |
493 | { | |
a6c65736 | 494 | if (w->num_kcontrols) |
f7d41ae8 JN |
495 | dev_err(w->dapm->dev, |
496 | "asoc: PGA controls not supported: '%s'\n", w->name); | |
2b97eabc | 497 | |
a6c65736 | 498 | return 0; |
2b97eabc RP |
499 | } |
500 | ||
501 | /* reset 'walked' bit for each dapm path */ | |
ce6120cc | 502 | static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm) |
2b97eabc RP |
503 | { |
504 | struct snd_soc_dapm_path *p; | |
505 | ||
8ddab3f5 | 506 | list_for_each_entry(p, &dapm->card->paths, list) |
2b97eabc RP |
507 | p->walked = 0; |
508 | } | |
509 | ||
9949788b MB |
510 | /* We implement power down on suspend by checking the power state of |
511 | * the ALSA card - when we are suspending the ALSA state for the card | |
512 | * is set to D3. | |
513 | */ | |
514 | static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget) | |
515 | { | |
12ea2c78 | 516 | int level = snd_power_get_state(widget->dapm->card->snd_card); |
9949788b | 517 | |
f0fba2ad | 518 | switch (level) { |
9949788b MB |
519 | case SNDRV_CTL_POWER_D3hot: |
520 | case SNDRV_CTL_POWER_D3cold: | |
1547aba9 | 521 | if (widget->ignore_suspend) |
f7d41ae8 JN |
522 | dev_dbg(widget->dapm->dev, "%s ignoring suspend\n", |
523 | widget->name); | |
1547aba9 | 524 | return widget->ignore_suspend; |
9949788b MB |
525 | default: |
526 | return 1; | |
527 | } | |
528 | } | |
529 | ||
2b97eabc RP |
530 | /* |
531 | * Recursively check for a completed path to an active or physically connected | |
532 | * output widget. Returns number of complete paths. | |
533 | */ | |
534 | static int is_connected_output_ep(struct snd_soc_dapm_widget *widget) | |
535 | { | |
536 | struct snd_soc_dapm_path *path; | |
537 | int con = 0; | |
538 | ||
246d0a17 MB |
539 | if (widget->id == snd_soc_dapm_supply) |
540 | return 0; | |
541 | ||
010ff262 MB |
542 | switch (widget->id) { |
543 | case snd_soc_dapm_adc: | |
544 | case snd_soc_dapm_aif_out: | |
545 | if (widget->active) | |
9949788b | 546 | return snd_soc_dapm_suspend_check(widget); |
010ff262 MB |
547 | default: |
548 | break; | |
549 | } | |
2b97eabc RP |
550 | |
551 | if (widget->connected) { | |
552 | /* connected pin ? */ | |
553 | if (widget->id == snd_soc_dapm_output && !widget->ext) | |
9949788b | 554 | return snd_soc_dapm_suspend_check(widget); |
2b97eabc RP |
555 | |
556 | /* connected jack or spk ? */ | |
557 | if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk || | |
eaeae5d9 | 558 | (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources))) |
9949788b | 559 | return snd_soc_dapm_suspend_check(widget); |
2b97eabc RP |
560 | } |
561 | ||
562 | list_for_each_entry(path, &widget->sinks, list_source) { | |
563 | if (path->walked) | |
564 | continue; | |
565 | ||
566 | if (path->sink && path->connect) { | |
567 | path->walked = 1; | |
568 | con += is_connected_output_ep(path->sink); | |
569 | } | |
570 | } | |
571 | ||
572 | return con; | |
573 | } | |
574 | ||
575 | /* | |
576 | * Recursively check for a completed path to an active or physically connected | |
577 | * input widget. Returns number of complete paths. | |
578 | */ | |
579 | static int is_connected_input_ep(struct snd_soc_dapm_widget *widget) | |
580 | { | |
581 | struct snd_soc_dapm_path *path; | |
582 | int con = 0; | |
583 | ||
246d0a17 MB |
584 | if (widget->id == snd_soc_dapm_supply) |
585 | return 0; | |
586 | ||
2b97eabc | 587 | /* active stream ? */ |
010ff262 MB |
588 | switch (widget->id) { |
589 | case snd_soc_dapm_dac: | |
590 | case snd_soc_dapm_aif_in: | |
591 | if (widget->active) | |
9949788b | 592 | return snd_soc_dapm_suspend_check(widget); |
010ff262 MB |
593 | default: |
594 | break; | |
595 | } | |
2b97eabc RP |
596 | |
597 | if (widget->connected) { | |
598 | /* connected pin ? */ | |
599 | if (widget->id == snd_soc_dapm_input && !widget->ext) | |
9949788b | 600 | return snd_soc_dapm_suspend_check(widget); |
2b97eabc RP |
601 | |
602 | /* connected VMID/Bias for lower pops */ | |
603 | if (widget->id == snd_soc_dapm_vmid) | |
9949788b | 604 | return snd_soc_dapm_suspend_check(widget); |
2b97eabc RP |
605 | |
606 | /* connected jack ? */ | |
eaeae5d9 PU |
607 | if (widget->id == snd_soc_dapm_mic || |
608 | (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks))) | |
9949788b | 609 | return snd_soc_dapm_suspend_check(widget); |
2b97eabc RP |
610 | } |
611 | ||
612 | list_for_each_entry(path, &widget->sources, list_sink) { | |
613 | if (path->walked) | |
614 | continue; | |
615 | ||
616 | if (path->source && path->connect) { | |
617 | path->walked = 1; | |
618 | con += is_connected_input_ep(path->source); | |
619 | } | |
620 | } | |
621 | ||
622 | return con; | |
623 | } | |
624 | ||
e2be2ccf JN |
625 | /* |
626 | * Handler for generic register modifier widget. | |
627 | */ | |
628 | int dapm_reg_event(struct snd_soc_dapm_widget *w, | |
629 | struct snd_kcontrol *kcontrol, int event) | |
630 | { | |
631 | unsigned int val; | |
632 | ||
633 | if (SND_SOC_DAPM_EVENT_ON(event)) | |
634 | val = w->on_val; | |
635 | else | |
636 | val = w->off_val; | |
637 | ||
638 | snd_soc_update_bits(w->codec, -(w->reg + 1), | |
639 | w->mask << w->shift, val << w->shift); | |
640 | ||
641 | return 0; | |
642 | } | |
11589418 | 643 | EXPORT_SYMBOL_GPL(dapm_reg_event); |
e2be2ccf | 644 | |
cd0f2d47 MB |
645 | /* Generic check to see if a widget should be powered. |
646 | */ | |
647 | static int dapm_generic_check_power(struct snd_soc_dapm_widget *w) | |
648 | { | |
649 | int in, out; | |
650 | ||
651 | in = is_connected_input_ep(w); | |
ce6120cc | 652 | dapm_clear_walk(w->dapm); |
cd0f2d47 | 653 | out = is_connected_output_ep(w); |
ce6120cc | 654 | dapm_clear_walk(w->dapm); |
cd0f2d47 MB |
655 | return out != 0 && in != 0; |
656 | } | |
657 | ||
6ea31b9f MB |
658 | /* Check to see if an ADC has power */ |
659 | static int dapm_adc_check_power(struct snd_soc_dapm_widget *w) | |
660 | { | |
661 | int in; | |
662 | ||
663 | if (w->active) { | |
664 | in = is_connected_input_ep(w); | |
ce6120cc | 665 | dapm_clear_walk(w->dapm); |
6ea31b9f MB |
666 | return in != 0; |
667 | } else { | |
668 | return dapm_generic_check_power(w); | |
669 | } | |
670 | } | |
671 | ||
672 | /* Check to see if a DAC has power */ | |
673 | static int dapm_dac_check_power(struct snd_soc_dapm_widget *w) | |
674 | { | |
675 | int out; | |
676 | ||
677 | if (w->active) { | |
678 | out = is_connected_output_ep(w); | |
ce6120cc | 679 | dapm_clear_walk(w->dapm); |
6ea31b9f MB |
680 | return out != 0; |
681 | } else { | |
682 | return dapm_generic_check_power(w); | |
683 | } | |
684 | } | |
685 | ||
246d0a17 MB |
686 | /* Check to see if a power supply is needed */ |
687 | static int dapm_supply_check_power(struct snd_soc_dapm_widget *w) | |
688 | { | |
689 | struct snd_soc_dapm_path *path; | |
690 | int power = 0; | |
691 | ||
692 | /* Check if one of our outputs is connected */ | |
693 | list_for_each_entry(path, &w->sinks, list_source) { | |
215edda3 MB |
694 | if (path->connected && |
695 | !path->connected(path->source, path->sink)) | |
696 | continue; | |
697 | ||
3017358a MB |
698 | if (!path->sink) |
699 | continue; | |
700 | ||
701 | if (path->sink->force) { | |
702 | power = 1; | |
703 | break; | |
704 | } | |
705 | ||
706 | if (path->sink->power_check && | |
246d0a17 MB |
707 | path->sink->power_check(path->sink)) { |
708 | power = 1; | |
709 | break; | |
710 | } | |
711 | } | |
712 | ||
ce6120cc | 713 | dapm_clear_walk(w->dapm); |
246d0a17 MB |
714 | |
715 | return power; | |
716 | } | |
717 | ||
38357ab2 MB |
718 | static int dapm_seq_compare(struct snd_soc_dapm_widget *a, |
719 | struct snd_soc_dapm_widget *b, | |
828a842f | 720 | bool power_up) |
42aa3418 | 721 | { |
828a842f MB |
722 | int *sort; |
723 | ||
724 | if (power_up) | |
725 | sort = dapm_up_seq; | |
726 | else | |
727 | sort = dapm_down_seq; | |
728 | ||
38357ab2 MB |
729 | if (sort[a->id] != sort[b->id]) |
730 | return sort[a->id] - sort[b->id]; | |
20e4859d MB |
731 | if (a->subseq != b->subseq) { |
732 | if (power_up) | |
733 | return a->subseq - b->subseq; | |
734 | else | |
735 | return b->subseq - a->subseq; | |
736 | } | |
b22ead2a MB |
737 | if (a->reg != b->reg) |
738 | return a->reg - b->reg; | |
84dab567 MB |
739 | if (a->dapm != b->dapm) |
740 | return (unsigned long)a->dapm - (unsigned long)b->dapm; | |
42aa3418 | 741 | |
38357ab2 MB |
742 | return 0; |
743 | } | |
42aa3418 | 744 | |
38357ab2 MB |
745 | /* Insert a widget in order into a DAPM power sequence. */ |
746 | static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget, | |
747 | struct list_head *list, | |
828a842f | 748 | bool power_up) |
38357ab2 MB |
749 | { |
750 | struct snd_soc_dapm_widget *w; | |
751 | ||
752 | list_for_each_entry(w, list, power_list) | |
828a842f | 753 | if (dapm_seq_compare(new_widget, w, power_up) < 0) { |
38357ab2 MB |
754 | list_add_tail(&new_widget->power_list, &w->power_list); |
755 | return; | |
756 | } | |
757 | ||
758 | list_add_tail(&new_widget->power_list, list); | |
759 | } | |
760 | ||
68f89ad8 MB |
761 | static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm, |
762 | struct snd_soc_dapm_widget *w, int event) | |
763 | { | |
764 | struct snd_soc_card *card = dapm->card; | |
765 | const char *ev_name; | |
766 | int power, ret; | |
767 | ||
768 | switch (event) { | |
769 | case SND_SOC_DAPM_PRE_PMU: | |
770 | ev_name = "PRE_PMU"; | |
771 | power = 1; | |
772 | break; | |
773 | case SND_SOC_DAPM_POST_PMU: | |
774 | ev_name = "POST_PMU"; | |
775 | power = 1; | |
776 | break; | |
777 | case SND_SOC_DAPM_PRE_PMD: | |
778 | ev_name = "PRE_PMD"; | |
779 | power = 0; | |
780 | break; | |
781 | case SND_SOC_DAPM_POST_PMD: | |
782 | ev_name = "POST_PMD"; | |
783 | power = 0; | |
784 | break; | |
785 | default: | |
786 | BUG(); | |
787 | return; | |
788 | } | |
789 | ||
790 | if (w->power != power) | |
791 | return; | |
792 | ||
793 | if (w->event && (w->event_flags & event)) { | |
794 | pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n", | |
795 | w->name, ev_name); | |
84e90930 | 796 | trace_snd_soc_dapm_widget_event_start(w, event); |
68f89ad8 | 797 | ret = w->event(w, NULL, event); |
84e90930 | 798 | trace_snd_soc_dapm_widget_event_done(w, event); |
68f89ad8 MB |
799 | if (ret < 0) |
800 | pr_err("%s: %s event failed: %d\n", | |
801 | ev_name, w->name, ret); | |
802 | } | |
803 | } | |
804 | ||
b22ead2a | 805 | /* Apply the coalesced changes from a DAPM sequence */ |
ce6120cc | 806 | static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm, |
b22ead2a | 807 | struct list_head *pending) |
163cac06 | 808 | { |
3a45b867 | 809 | struct snd_soc_card *card = dapm->card; |
68f89ad8 MB |
810 | struct snd_soc_dapm_widget *w; |
811 | int reg, power; | |
b22ead2a MB |
812 | unsigned int value = 0; |
813 | unsigned int mask = 0; | |
814 | unsigned int cur_mask; | |
815 | ||
816 | reg = list_first_entry(pending, struct snd_soc_dapm_widget, | |
817 | power_list)->reg; | |
818 | ||
819 | list_for_each_entry(w, pending, power_list) { | |
820 | cur_mask = 1 << w->shift; | |
821 | BUG_ON(reg != w->reg); | |
822 | ||
823 | if (w->invert) | |
824 | power = !w->power; | |
825 | else | |
826 | power = w->power; | |
827 | ||
828 | mask |= cur_mask; | |
829 | if (power) | |
830 | value |= cur_mask; | |
831 | ||
fd8d3bc0 | 832 | pop_dbg(dapm->dev, card->pop_time, |
b22ead2a MB |
833 | "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n", |
834 | w->name, reg, value, mask); | |
81628103 | 835 | |
68f89ad8 MB |
836 | /* Check for events */ |
837 | dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU); | |
838 | dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD); | |
81628103 MB |
839 | } |
840 | ||
841 | if (reg >= 0) { | |
fd8d3bc0 | 842 | pop_dbg(dapm->dev, card->pop_time, |
81628103 | 843 | "pop test : Applying 0x%x/0x%x to %x in %dms\n", |
3a45b867 JN |
844 | value, mask, reg, card->pop_time); |
845 | pop_wait(card->pop_time); | |
ce6120cc | 846 | snd_soc_update_bits(dapm->codec, reg, mask, value); |
b22ead2a MB |
847 | } |
848 | ||
81628103 | 849 | list_for_each_entry(w, pending, power_list) { |
68f89ad8 MB |
850 | dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU); |
851 | dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD); | |
81628103 | 852 | } |
b22ead2a | 853 | } |
42aa3418 | 854 | |
b22ead2a MB |
855 | /* Apply a DAPM power sequence. |
856 | * | |
857 | * We walk over a pre-sorted list of widgets to apply power to. In | |
858 | * order to minimise the number of writes to the device required | |
859 | * multiple widgets will be updated in a single write where possible. | |
860 | * Currently anything that requires more than a single write is not | |
861 | * handled. | |
862 | */ | |
ce6120cc | 863 | static void dapm_seq_run(struct snd_soc_dapm_context *dapm, |
828a842f | 864 | struct list_head *list, int event, bool power_up) |
b22ead2a MB |
865 | { |
866 | struct snd_soc_dapm_widget *w, *n; | |
867 | LIST_HEAD(pending); | |
868 | int cur_sort = -1; | |
20e4859d | 869 | int cur_subseq = -1; |
b22ead2a | 870 | int cur_reg = SND_SOC_NOPM; |
7be31be8 | 871 | struct snd_soc_dapm_context *cur_dapm = NULL; |
474b62d6 | 872 | int ret, i; |
828a842f MB |
873 | int *sort; |
874 | ||
875 | if (power_up) | |
876 | sort = dapm_up_seq; | |
877 | else | |
878 | sort = dapm_down_seq; | |
163cac06 | 879 | |
b22ead2a MB |
880 | list_for_each_entry_safe(w, n, list, power_list) { |
881 | ret = 0; | |
882 | ||
883 | /* Do we need to apply any queued changes? */ | |
7be31be8 | 884 | if (sort[w->id] != cur_sort || w->reg != cur_reg || |
20e4859d | 885 | w->dapm != cur_dapm || w->subseq != cur_subseq) { |
b22ead2a | 886 | if (!list_empty(&pending)) |
7be31be8 | 887 | dapm_seq_run_coalesced(cur_dapm, &pending); |
b22ead2a | 888 | |
474b62d6 MB |
889 | if (cur_dapm && cur_dapm->seq_notifier) { |
890 | for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++) | |
891 | if (sort[i] == cur_sort) | |
892 | cur_dapm->seq_notifier(cur_dapm, | |
f85a9e0d MB |
893 | i, |
894 | cur_subseq); | |
474b62d6 MB |
895 | } |
896 | ||
b22ead2a MB |
897 | INIT_LIST_HEAD(&pending); |
898 | cur_sort = -1; | |
20e4859d | 899 | cur_subseq = -1; |
b22ead2a | 900 | cur_reg = SND_SOC_NOPM; |
7be31be8 | 901 | cur_dapm = NULL; |
b22ead2a MB |
902 | } |
903 | ||
163cac06 MB |
904 | switch (w->id) { |
905 | case snd_soc_dapm_pre: | |
906 | if (!w->event) | |
b22ead2a MB |
907 | list_for_each_entry_safe_continue(w, n, list, |
908 | power_list); | |
163cac06 | 909 | |
b22ead2a | 910 | if (event == SND_SOC_DAPM_STREAM_START) |
163cac06 MB |
911 | ret = w->event(w, |
912 | NULL, SND_SOC_DAPM_PRE_PMU); | |
b22ead2a | 913 | else if (event == SND_SOC_DAPM_STREAM_STOP) |
163cac06 MB |
914 | ret = w->event(w, |
915 | NULL, SND_SOC_DAPM_PRE_PMD); | |
163cac06 MB |
916 | break; |
917 | ||
918 | case snd_soc_dapm_post: | |
919 | if (!w->event) | |
b22ead2a MB |
920 | list_for_each_entry_safe_continue(w, n, list, |
921 | power_list); | |
163cac06 | 922 | |
b22ead2a | 923 | if (event == SND_SOC_DAPM_STREAM_START) |
163cac06 MB |
924 | ret = w->event(w, |
925 | NULL, SND_SOC_DAPM_POST_PMU); | |
b22ead2a | 926 | else if (event == SND_SOC_DAPM_STREAM_STOP) |
163cac06 MB |
927 | ret = w->event(w, |
928 | NULL, SND_SOC_DAPM_POST_PMD); | |
163cac06 MB |
929 | break; |
930 | ||
b22ead2a | 931 | default: |
81628103 MB |
932 | /* Queue it up for application */ |
933 | cur_sort = sort[w->id]; | |
20e4859d | 934 | cur_subseq = w->subseq; |
81628103 | 935 | cur_reg = w->reg; |
7be31be8 | 936 | cur_dapm = w->dapm; |
81628103 MB |
937 | list_move(&w->power_list, &pending); |
938 | break; | |
163cac06 | 939 | } |
b22ead2a MB |
940 | |
941 | if (ret < 0) | |
f7d41ae8 JN |
942 | dev_err(w->dapm->dev, |
943 | "Failed to apply widget power: %d\n", ret); | |
6ea31b9f | 944 | } |
b22ead2a MB |
945 | |
946 | if (!list_empty(&pending)) | |
28e86808 | 947 | dapm_seq_run_coalesced(cur_dapm, &pending); |
474b62d6 MB |
948 | |
949 | if (cur_dapm && cur_dapm->seq_notifier) { | |
950 | for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++) | |
951 | if (sort[i] == cur_sort) | |
952 | cur_dapm->seq_notifier(cur_dapm, | |
f85a9e0d | 953 | i, cur_subseq); |
474b62d6 | 954 | } |
42aa3418 MB |
955 | } |
956 | ||
97404f2e MB |
957 | static void dapm_widget_update(struct snd_soc_dapm_context *dapm) |
958 | { | |
959 | struct snd_soc_dapm_update *update = dapm->update; | |
960 | struct snd_soc_dapm_widget *w; | |
961 | int ret; | |
962 | ||
963 | if (!update) | |
964 | return; | |
965 | ||
966 | w = update->widget; | |
967 | ||
968 | if (w->event && | |
969 | (w->event_flags & SND_SOC_DAPM_PRE_REG)) { | |
970 | ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG); | |
971 | if (ret != 0) | |
972 | pr_err("%s DAPM pre-event failed: %d\n", | |
973 | w->name, ret); | |
974 | } | |
975 | ||
976 | ret = snd_soc_update_bits(w->codec, update->reg, update->mask, | |
977 | update->val); | |
978 | if (ret < 0) | |
979 | pr_err("%s DAPM update failed: %d\n", w->name, ret); | |
980 | ||
981 | if (w->event && | |
982 | (w->event_flags & SND_SOC_DAPM_POST_REG)) { | |
983 | ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG); | |
984 | if (ret != 0) | |
985 | pr_err("%s DAPM post-event failed: %d\n", | |
986 | w->name, ret); | |
987 | } | |
988 | } | |
989 | ||
9d0624a7 MB |
990 | /* Async callback run prior to DAPM sequences - brings to _PREPARE if |
991 | * they're changing state. | |
992 | */ | |
993 | static void dapm_pre_sequence_async(void *data, async_cookie_t cookie) | |
994 | { | |
995 | struct snd_soc_dapm_context *d = data; | |
996 | int ret; | |
997 | ||
998 | if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) { | |
999 | ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY); | |
1000 | if (ret != 0) | |
1001 | dev_err(d->dev, | |
1002 | "Failed to turn on bias: %d\n", ret); | |
1003 | } | |
1004 | ||
1005 | /* If we're changing to all on or all off then prepare */ | |
1006 | if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) || | |
1007 | (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) { | |
1008 | ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE); | |
1009 | if (ret != 0) | |
1010 | dev_err(d->dev, | |
1011 | "Failed to prepare bias: %d\n", ret); | |
1012 | } | |
1013 | } | |
1014 | ||
1015 | /* Async callback run prior to DAPM sequences - brings to their final | |
1016 | * state. | |
1017 | */ | |
1018 | static void dapm_post_sequence_async(void *data, async_cookie_t cookie) | |
1019 | { | |
1020 | struct snd_soc_dapm_context *d = data; | |
1021 | int ret; | |
1022 | ||
1023 | /* If we just powered the last thing off drop to standby bias */ | |
1024 | if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) { | |
1025 | ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY); | |
1026 | if (ret != 0) | |
1027 | dev_err(d->dev, "Failed to apply standby bias: %d\n", | |
1028 | ret); | |
1029 | } | |
97404f2e | 1030 | |
9d0624a7 MB |
1031 | /* If we're in standby and can support bias off then do that */ |
1032 | if (d->bias_level == SND_SOC_BIAS_STANDBY && d->idle_bias_off) { | |
1033 | ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF); | |
1034 | if (ret != 0) | |
1035 | dev_err(d->dev, "Failed to turn off bias: %d\n", ret); | |
1036 | } | |
1037 | ||
1038 | /* If we just powered up then move to active bias */ | |
1039 | if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) { | |
1040 | ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON); | |
1041 | if (ret != 0) | |
1042 | dev_err(d->dev, "Failed to apply active bias: %d\n", | |
1043 | ret); | |
1044 | } | |
1045 | } | |
97404f2e | 1046 | |
2b97eabc RP |
1047 | /* |
1048 | * Scan each dapm widget for complete audio path. | |
1049 | * A complete path is a route that has valid endpoints i.e.:- | |
1050 | * | |
1051 | * o DAC to output pin. | |
1052 | * o Input Pin to ADC. | |
1053 | * o Input pin to Output pin (bypass, sidetone) | |
1054 | * o DAC to ADC (loopback). | |
1055 | */ | |
ce6120cc | 1056 | static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event) |
2b97eabc | 1057 | { |
12ea2c78 | 1058 | struct snd_soc_card *card = dapm->card; |
2b97eabc | 1059 | struct snd_soc_dapm_widget *w; |
7be31be8 | 1060 | struct snd_soc_dapm_context *d; |
291f3bbc MB |
1061 | LIST_HEAD(up_list); |
1062 | LIST_HEAD(down_list); | |
9d0624a7 | 1063 | LIST_HEAD(async_domain); |
38357ab2 | 1064 | int power; |
6d3ddc81 | 1065 | |
84e90930 MB |
1066 | trace_snd_soc_dapm_start(card); |
1067 | ||
7be31be8 JN |
1068 | list_for_each_entry(d, &card->dapm_list, list) |
1069 | if (d->n_widgets) | |
1070 | d->dev_power = 0; | |
1071 | ||
6d3ddc81 MB |
1072 | /* Check which widgets we need to power and store them in |
1073 | * lists indicating if they should be powered up or down. | |
1074 | */ | |
97c866de | 1075 | list_for_each_entry(w, &card->widgets, list) { |
6d3ddc81 MB |
1076 | switch (w->id) { |
1077 | case snd_soc_dapm_pre: | |
828a842f | 1078 | dapm_seq_insert(w, &down_list, false); |
6d3ddc81 MB |
1079 | break; |
1080 | case snd_soc_dapm_post: | |
828a842f | 1081 | dapm_seq_insert(w, &up_list, true); |
6d3ddc81 MB |
1082 | break; |
1083 | ||
1084 | default: | |
1085 | if (!w->power_check) | |
1086 | continue; | |
1087 | ||
9949788b | 1088 | if (!w->force) |
50b6bce5 | 1089 | power = w->power_check(w); |
9949788b MB |
1090 | else |
1091 | power = 1; | |
1092 | if (power) | |
7be31be8 | 1093 | w->dapm->dev_power = 1; |
452c5eaa | 1094 | |
6d3ddc81 MB |
1095 | if (w->power == power) |
1096 | continue; | |
1097 | ||
84e90930 MB |
1098 | trace_snd_soc_dapm_widget_power(w, power); |
1099 | ||
6d3ddc81 | 1100 | if (power) |
828a842f | 1101 | dapm_seq_insert(w, &up_list, true); |
6d3ddc81 | 1102 | else |
828a842f | 1103 | dapm_seq_insert(w, &down_list, false); |
6d3ddc81 MB |
1104 | |
1105 | w->power = power; | |
1106 | break; | |
1107 | } | |
2b97eabc RP |
1108 | } |
1109 | ||
b14b76a5 MB |
1110 | /* If there are no DAPM widgets then try to figure out power from the |
1111 | * event type. | |
1112 | */ | |
97c866de | 1113 | if (!dapm->n_widgets) { |
b14b76a5 MB |
1114 | switch (event) { |
1115 | case SND_SOC_DAPM_STREAM_START: | |
1116 | case SND_SOC_DAPM_STREAM_RESUME: | |
7be31be8 | 1117 | dapm->dev_power = 1; |
b14b76a5 | 1118 | break; |
862af8ad | 1119 | case SND_SOC_DAPM_STREAM_STOP: |
7be31be8 | 1120 | dapm->dev_power = !!dapm->codec->active; |
862af8ad | 1121 | break; |
50b6bce5 | 1122 | case SND_SOC_DAPM_STREAM_SUSPEND: |
7be31be8 | 1123 | dapm->dev_power = 0; |
50b6bce5 | 1124 | break; |
b14b76a5 | 1125 | case SND_SOC_DAPM_STREAM_NOP: |
ce6120cc | 1126 | switch (dapm->bias_level) { |
a96ca338 MB |
1127 | case SND_SOC_BIAS_STANDBY: |
1128 | case SND_SOC_BIAS_OFF: | |
7be31be8 | 1129 | dapm->dev_power = 0; |
a96ca338 MB |
1130 | break; |
1131 | default: | |
7be31be8 | 1132 | dapm->dev_power = 1; |
a96ca338 MB |
1133 | break; |
1134 | } | |
50b6bce5 | 1135 | break; |
b14b76a5 MB |
1136 | default: |
1137 | break; | |
1138 | } | |
1139 | } | |
1140 | ||
52ba67bf MB |
1141 | /* Force all contexts in the card to the same bias state */ |
1142 | power = 0; | |
1143 | list_for_each_entry(d, &card->dapm_list, list) | |
1144 | if (d->dev_power) | |
1145 | power = 1; | |
1146 | list_for_each_entry(d, &card->dapm_list, list) | |
1147 | d->dev_power = power; | |
1148 | ||
1149 | ||
9d0624a7 MB |
1150 | /* Run all the bias changes in parallel */ |
1151 | list_for_each_entry(d, &dapm->card->dapm_list, list) | |
1152 | async_schedule_domain(dapm_pre_sequence_async, d, | |
1153 | &async_domain); | |
1154 | async_synchronize_full_domain(&async_domain); | |
452c5eaa | 1155 | |
6d3ddc81 | 1156 | /* Power down widgets first; try to avoid amplifying pops. */ |
828a842f | 1157 | dapm_seq_run(dapm, &down_list, event, false); |
2b97eabc | 1158 | |
97404f2e MB |
1159 | dapm_widget_update(dapm); |
1160 | ||
6d3ddc81 | 1161 | /* Now power up. */ |
828a842f | 1162 | dapm_seq_run(dapm, &up_list, event, true); |
2b97eabc | 1163 | |
9d0624a7 MB |
1164 | /* Run all the bias changes in parallel */ |
1165 | list_for_each_entry(d, &dapm->card->dapm_list, list) | |
1166 | async_schedule_domain(dapm_post_sequence_async, d, | |
1167 | &async_domain); | |
1168 | async_synchronize_full_domain(&async_domain); | |
452c5eaa | 1169 | |
fd8d3bc0 JN |
1170 | pop_dbg(dapm->dev, card->pop_time, |
1171 | "DAPM sequencing finished, waiting %dms\n", card->pop_time); | |
3a45b867 | 1172 | pop_wait(card->pop_time); |
cb507e7e | 1173 | |
84e90930 MB |
1174 | trace_snd_soc_dapm_done(card); |
1175 | ||
42aa3418 | 1176 | return 0; |
2b97eabc RP |
1177 | } |
1178 | ||
79fb9387 MB |
1179 | #ifdef CONFIG_DEBUG_FS |
1180 | static int dapm_widget_power_open_file(struct inode *inode, struct file *file) | |
1181 | { | |
1182 | file->private_data = inode->i_private; | |
1183 | return 0; | |
1184 | } | |
1185 | ||
1186 | static ssize_t dapm_widget_power_read_file(struct file *file, | |
1187 | char __user *user_buf, | |
1188 | size_t count, loff_t *ppos) | |
1189 | { | |
1190 | struct snd_soc_dapm_widget *w = file->private_data; | |
1191 | char *buf; | |
1192 | int in, out; | |
1193 | ssize_t ret; | |
1194 | struct snd_soc_dapm_path *p = NULL; | |
1195 | ||
1196 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
1197 | if (!buf) | |
1198 | return -ENOMEM; | |
1199 | ||
1200 | in = is_connected_input_ep(w); | |
ce6120cc | 1201 | dapm_clear_walk(w->dapm); |
79fb9387 | 1202 | out = is_connected_output_ep(w); |
ce6120cc | 1203 | dapm_clear_walk(w->dapm); |
79fb9387 | 1204 | |
d033c36a | 1205 | ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d", |
79fb9387 MB |
1206 | w->name, w->power ? "On" : "Off", in, out); |
1207 | ||
d033c36a MB |
1208 | if (w->reg >= 0) |
1209 | ret += snprintf(buf + ret, PAGE_SIZE - ret, | |
1210 | " - R%d(0x%x) bit %d", | |
1211 | w->reg, w->reg, w->shift); | |
1212 | ||
1213 | ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n"); | |
1214 | ||
3eef08ba MB |
1215 | if (w->sname) |
1216 | ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n", | |
1217 | w->sname, | |
1218 | w->active ? "active" : "inactive"); | |
79fb9387 MB |
1219 | |
1220 | list_for_each_entry(p, &w->sources, list_sink) { | |
215edda3 MB |
1221 | if (p->connected && !p->connected(w, p->sink)) |
1222 | continue; | |
1223 | ||
79fb9387 MB |
1224 | if (p->connect) |
1225 | ret += snprintf(buf + ret, PAGE_SIZE - ret, | |
67f5ed6e | 1226 | " in \"%s\" \"%s\"\n", |
79fb9387 MB |
1227 | p->name ? p->name : "static", |
1228 | p->source->name); | |
1229 | } | |
1230 | list_for_each_entry(p, &w->sinks, list_source) { | |
215edda3 MB |
1231 | if (p->connected && !p->connected(w, p->sink)) |
1232 | continue; | |
1233 | ||
79fb9387 MB |
1234 | if (p->connect) |
1235 | ret += snprintf(buf + ret, PAGE_SIZE - ret, | |
67f5ed6e | 1236 | " out \"%s\" \"%s\"\n", |
79fb9387 MB |
1237 | p->name ? p->name : "static", |
1238 | p->sink->name); | |
1239 | } | |
1240 | ||
1241 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); | |
1242 | ||
1243 | kfree(buf); | |
1244 | return ret; | |
1245 | } | |
1246 | ||
1247 | static const struct file_operations dapm_widget_power_fops = { | |
1248 | .open = dapm_widget_power_open_file, | |
1249 | .read = dapm_widget_power_read_file, | |
6038f373 | 1250 | .llseek = default_llseek, |
79fb9387 MB |
1251 | }; |
1252 | ||
ef49e4fa MB |
1253 | static int dapm_bias_open_file(struct inode *inode, struct file *file) |
1254 | { | |
1255 | file->private_data = inode->i_private; | |
1256 | return 0; | |
1257 | } | |
1258 | ||
1259 | static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf, | |
1260 | size_t count, loff_t *ppos) | |
1261 | { | |
1262 | struct snd_soc_dapm_context *dapm = file->private_data; | |
1263 | char *level; | |
1264 | ||
1265 | switch (dapm->bias_level) { | |
1266 | case SND_SOC_BIAS_ON: | |
1267 | level = "On\n"; | |
1268 | break; | |
1269 | case SND_SOC_BIAS_PREPARE: | |
1270 | level = "Prepare\n"; | |
1271 | break; | |
1272 | case SND_SOC_BIAS_STANDBY: | |
1273 | level = "Standby\n"; | |
1274 | break; | |
1275 | case SND_SOC_BIAS_OFF: | |
1276 | level = "Off\n"; | |
1277 | break; | |
1278 | default: | |
1279 | BUG(); | |
1280 | level = "Unknown\n"; | |
1281 | break; | |
1282 | } | |
1283 | ||
1284 | return simple_read_from_buffer(user_buf, count, ppos, level, | |
1285 | strlen(level)); | |
1286 | } | |
1287 | ||
1288 | static const struct file_operations dapm_bias_fops = { | |
1289 | .open = dapm_bias_open_file, | |
1290 | .read = dapm_bias_read_file, | |
1291 | .llseek = default_llseek, | |
1292 | }; | |
1293 | ||
8eecaf62 LPC |
1294 | void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm, |
1295 | struct dentry *parent) | |
79fb9387 | 1296 | { |
79fb9387 MB |
1297 | struct dentry *d; |
1298 | ||
8eecaf62 LPC |
1299 | dapm->debugfs_dapm = debugfs_create_dir("dapm", parent); |
1300 | ||
1301 | if (!dapm->debugfs_dapm) { | |
1302 | printk(KERN_WARNING | |
1303 | "Failed to create DAPM debugfs directory\n"); | |
79fb9387 | 1304 | return; |
8eecaf62 | 1305 | } |
79fb9387 | 1306 | |
ef49e4fa MB |
1307 | d = debugfs_create_file("bias_level", 0444, |
1308 | dapm->debugfs_dapm, dapm, | |
1309 | &dapm_bias_fops); | |
1310 | if (!d) | |
1311 | dev_warn(dapm->dev, | |
1312 | "ASoC: Failed to create bias level debugfs file\n"); | |
d5d1e0be | 1313 | } |
ef49e4fa | 1314 | |
d5d1e0be LPC |
1315 | static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w) |
1316 | { | |
1317 | struct snd_soc_dapm_context *dapm = w->dapm; | |
1318 | struct dentry *d; | |
79fb9387 | 1319 | |
d5d1e0be LPC |
1320 | if (!dapm->debugfs_dapm || !w->name) |
1321 | return; | |
1322 | ||
1323 | d = debugfs_create_file(w->name, 0444, | |
1324 | dapm->debugfs_dapm, w, | |
1325 | &dapm_widget_power_fops); | |
1326 | if (!d) | |
1327 | dev_warn(w->dapm->dev, | |
1328 | "ASoC: Failed to create %s debugfs file\n", | |
1329 | w->name); | |
79fb9387 | 1330 | } |
d5d1e0be | 1331 | |
6c45e126 LPC |
1332 | static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm) |
1333 | { | |
1334 | debugfs_remove_recursive(dapm->debugfs_dapm); | |
1335 | } | |
1336 | ||
79fb9387 | 1337 | #else |
8eecaf62 LPC |
1338 | void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm, |
1339 | struct dentry *parent) | |
79fb9387 MB |
1340 | { |
1341 | } | |
d5d1e0be LPC |
1342 | |
1343 | static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w) | |
1344 | { | |
1345 | } | |
1346 | ||
6c45e126 LPC |
1347 | static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm) |
1348 | { | |
1349 | } | |
1350 | ||
79fb9387 MB |
1351 | #endif |
1352 | ||
2b97eabc | 1353 | /* test and update the power status of a mux widget */ |
d9c96cf3 | 1354 | static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget, |
3a65577d MB |
1355 | struct snd_kcontrol *kcontrol, int change, |
1356 | int mux, struct soc_enum *e) | |
2b97eabc RP |
1357 | { |
1358 | struct snd_soc_dapm_path *path; | |
1359 | int found = 0; | |
1360 | ||
eff317d0 | 1361 | if (widget->id != snd_soc_dapm_mux && |
24ff33ac | 1362 | widget->id != snd_soc_dapm_virt_mux && |
eff317d0 | 1363 | widget->id != snd_soc_dapm_value_mux) |
2b97eabc RP |
1364 | return -ENODEV; |
1365 | ||
3a65577d | 1366 | if (!change) |
2b97eabc RP |
1367 | return 0; |
1368 | ||
1369 | /* find dapm widget path assoc with kcontrol */ | |
8ddab3f5 | 1370 | list_for_each_entry(path, &widget->dapm->card->paths, list) { |
2b97eabc RP |
1371 | if (path->kcontrol != kcontrol) |
1372 | continue; | |
1373 | ||
cb01e2b9 | 1374 | if (!path->name || !e->texts[mux]) |
2b97eabc RP |
1375 | continue; |
1376 | ||
1377 | found = 1; | |
1378 | /* we now need to match the string in the enum to the path */ | |
cb01e2b9 | 1379 | if (!(strcmp(path->name, e->texts[mux]))) |
2b97eabc RP |
1380 | path->connect = 1; /* new connection */ |
1381 | else | |
1382 | path->connect = 0; /* old connection must be powered down */ | |
1383 | } | |
1384 | ||
b91b8fa0 | 1385 | if (found) |
ce6120cc | 1386 | dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP); |
2b97eabc RP |
1387 | |
1388 | return 0; | |
1389 | } | |
2b97eabc | 1390 | |
1b075e3f | 1391 | /* test and update the power status of a mixer or switch widget */ |
d9c96cf3 | 1392 | static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget, |
283375ce | 1393 | struct snd_kcontrol *kcontrol, int connect) |
2b97eabc RP |
1394 | { |
1395 | struct snd_soc_dapm_path *path; | |
1396 | int found = 0; | |
1397 | ||
1b075e3f | 1398 | if (widget->id != snd_soc_dapm_mixer && |
ca9c1aae | 1399 | widget->id != snd_soc_dapm_mixer_named_ctl && |
1b075e3f | 1400 | widget->id != snd_soc_dapm_switch) |
2b97eabc RP |
1401 | return -ENODEV; |
1402 | ||
2b97eabc | 1403 | /* find dapm widget path assoc with kcontrol */ |
8ddab3f5 | 1404 | list_for_each_entry(path, &widget->dapm->card->paths, list) { |
2b97eabc RP |
1405 | if (path->kcontrol != kcontrol) |
1406 | continue; | |
1407 | ||
1408 | /* found, now check type */ | |
1409 | found = 1; | |
283375ce | 1410 | path->connect = connect; |
2b97eabc RP |
1411 | break; |
1412 | } | |
1413 | ||
b91b8fa0 | 1414 | if (found) |
ce6120cc | 1415 | dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP); |
2b97eabc RP |
1416 | |
1417 | return 0; | |
1418 | } | |
2b97eabc RP |
1419 | |
1420 | /* show dapm widget status in sys fs */ | |
1421 | static ssize_t dapm_widget_show(struct device *dev, | |
1422 | struct device_attribute *attr, char *buf) | |
1423 | { | |
f0fba2ad LG |
1424 | struct snd_soc_pcm_runtime *rtd = |
1425 | container_of(dev, struct snd_soc_pcm_runtime, dev); | |
1426 | struct snd_soc_codec *codec =rtd->codec; | |
2b97eabc RP |
1427 | struct snd_soc_dapm_widget *w; |
1428 | int count = 0; | |
1429 | char *state = "not set"; | |
1430 | ||
97c866de JN |
1431 | list_for_each_entry(w, &codec->card->widgets, list) { |
1432 | if (w->dapm != &codec->dapm) | |
1433 | continue; | |
2b97eabc RP |
1434 | |
1435 | /* only display widgets that burnm power */ | |
1436 | switch (w->id) { | |
1437 | case snd_soc_dapm_hp: | |
1438 | case snd_soc_dapm_mic: | |
1439 | case snd_soc_dapm_spk: | |
1440 | case snd_soc_dapm_line: | |
1441 | case snd_soc_dapm_micbias: | |
1442 | case snd_soc_dapm_dac: | |
1443 | case snd_soc_dapm_adc: | |
1444 | case snd_soc_dapm_pga: | |
d88429a6 | 1445 | case snd_soc_dapm_out_drv: |
2b97eabc | 1446 | case snd_soc_dapm_mixer: |
ca9c1aae | 1447 | case snd_soc_dapm_mixer_named_ctl: |
246d0a17 | 1448 | case snd_soc_dapm_supply: |
2b97eabc RP |
1449 | if (w->name) |
1450 | count += sprintf(buf + count, "%s: %s\n", | |
1451 | w->name, w->power ? "On":"Off"); | |
1452 | break; | |
1453 | default: | |
1454 | break; | |
1455 | } | |
1456 | } | |
1457 | ||
ce6120cc | 1458 | switch (codec->dapm.bias_level) { |
0be9898a MB |
1459 | case SND_SOC_BIAS_ON: |
1460 | state = "On"; | |
2b97eabc | 1461 | break; |
0be9898a MB |
1462 | case SND_SOC_BIAS_PREPARE: |
1463 | state = "Prepare"; | |
2b97eabc | 1464 | break; |
0be9898a MB |
1465 | case SND_SOC_BIAS_STANDBY: |
1466 | state = "Standby"; | |
2b97eabc | 1467 | break; |
0be9898a MB |
1468 | case SND_SOC_BIAS_OFF: |
1469 | state = "Off"; | |
2b97eabc RP |
1470 | break; |
1471 | } | |
1472 | count += sprintf(buf + count, "PM State: %s\n", state); | |
1473 | ||
1474 | return count; | |
1475 | } | |
1476 | ||
1477 | static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL); | |
1478 | ||
1479 | int snd_soc_dapm_sys_add(struct device *dev) | |
1480 | { | |
12ef193d | 1481 | return device_create_file(dev, &dev_attr_dapm_widget); |
2b97eabc RP |
1482 | } |
1483 | ||
1484 | static void snd_soc_dapm_sys_remove(struct device *dev) | |
1485 | { | |
aef90843 | 1486 | device_remove_file(dev, &dev_attr_dapm_widget); |
2b97eabc RP |
1487 | } |
1488 | ||
1489 | /* free all dapm widgets and resources */ | |
ce6120cc | 1490 | static void dapm_free_widgets(struct snd_soc_dapm_context *dapm) |
2b97eabc RP |
1491 | { |
1492 | struct snd_soc_dapm_widget *w, *next_w; | |
1493 | struct snd_soc_dapm_path *p, *next_p; | |
1494 | ||
97c866de JN |
1495 | list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) { |
1496 | if (w->dapm != dapm) | |
1497 | continue; | |
2b97eabc | 1498 | list_del(&w->list); |
8ddab3f5 JN |
1499 | /* |
1500 | * remove source and sink paths associated to this widget. | |
1501 | * While removing the path, remove reference to it from both | |
1502 | * source and sink widgets so that path is removed only once. | |
1503 | */ | |
1504 | list_for_each_entry_safe(p, next_p, &w->sources, list_sink) { | |
1505 | list_del(&p->list_sink); | |
1506 | list_del(&p->list_source); | |
1507 | list_del(&p->list); | |
1508 | kfree(p->long_name); | |
1509 | kfree(p); | |
1510 | } | |
1511 | list_for_each_entry_safe(p, next_p, &w->sinks, list_source) { | |
1512 | list_del(&p->list_sink); | |
1513 | list_del(&p->list_source); | |
1514 | list_del(&p->list); | |
1515 | kfree(p->long_name); | |
1516 | kfree(p); | |
1517 | } | |
fad59888 | 1518 | kfree(w->kcontrols); |
ead9b919 | 1519 | kfree(w->name); |
2b97eabc RP |
1520 | kfree(w); |
1521 | } | |
2b97eabc RP |
1522 | } |
1523 | ||
91a5fca4 LPC |
1524 | static struct snd_soc_dapm_widget *dapm_find_widget( |
1525 | struct snd_soc_dapm_context *dapm, const char *pin, | |
1526 | bool search_other_contexts) | |
a5302181 LG |
1527 | { |
1528 | struct snd_soc_dapm_widget *w; | |
91a5fca4 | 1529 | struct snd_soc_dapm_widget *fallback = NULL; |
a5302181 | 1530 | |
97c866de | 1531 | list_for_each_entry(w, &dapm->card->widgets, list) { |
a5302181 | 1532 | if (!strcmp(w->name, pin)) { |
91a5fca4 LPC |
1533 | if (w->dapm == dapm) |
1534 | return w; | |
1535 | else | |
1536 | fallback = w; | |
a5302181 LG |
1537 | } |
1538 | } | |
1539 | ||
91a5fca4 LPC |
1540 | if (search_other_contexts) |
1541 | return fallback; | |
1542 | ||
1543 | return NULL; | |
1544 | } | |
1545 | ||
1546 | static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm, | |
1547 | const char *pin, int status) | |
1548 | { | |
1549 | struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true); | |
1550 | ||
1551 | if (!w) { | |
1552 | dev_err(dapm->dev, "dapm: unknown pin %s\n", pin); | |
1553 | return -EINVAL; | |
0d86733c MB |
1554 | } |
1555 | ||
91a5fca4 LPC |
1556 | w->connected = status; |
1557 | if (status == 0) | |
1558 | w->force = 0; | |
1559 | ||
1560 | return 0; | |
a5302181 LG |
1561 | } |
1562 | ||
2b97eabc | 1563 | /** |
a5302181 | 1564 | * snd_soc_dapm_sync - scan and power dapm paths |
ce6120cc | 1565 | * @dapm: DAPM context |
2b97eabc RP |
1566 | * |
1567 | * Walks all dapm audio paths and powers widgets according to their | |
1568 | * stream or path usage. | |
1569 | * | |
1570 | * Returns 0 for success. | |
1571 | */ | |
ce6120cc | 1572 | int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm) |
2b97eabc | 1573 | { |
ce6120cc | 1574 | return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP); |
2b97eabc | 1575 | } |
a5302181 | 1576 | EXPORT_SYMBOL_GPL(snd_soc_dapm_sync); |
2b97eabc | 1577 | |
ce6120cc | 1578 | static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm, |
215edda3 | 1579 | const struct snd_soc_dapm_route *route) |
2b97eabc RP |
1580 | { |
1581 | struct snd_soc_dapm_path *path; | |
1582 | struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w; | |
97c866de | 1583 | struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL; |
ead9b919 | 1584 | const char *sink; |
215edda3 | 1585 | const char *control = route->control; |
ead9b919 JN |
1586 | const char *source; |
1587 | char prefixed_sink[80]; | |
1588 | char prefixed_source[80]; | |
2b97eabc RP |
1589 | int ret = 0; |
1590 | ||
88e8b9a8 | 1591 | if (dapm->codec && dapm->codec->name_prefix) { |
ead9b919 JN |
1592 | snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s", |
1593 | dapm->codec->name_prefix, route->sink); | |
1594 | sink = prefixed_sink; | |
1595 | snprintf(prefixed_source, sizeof(prefixed_source), "%s %s", | |
1596 | dapm->codec->name_prefix, route->source); | |
1597 | source = prefixed_source; | |
1598 | } else { | |
1599 | sink = route->sink; | |
1600 | source = route->source; | |
1601 | } | |
1602 | ||
97c866de JN |
1603 | /* |
1604 | * find src and dest widgets over all widgets but favor a widget from | |
1605 | * current DAPM context | |
1606 | */ | |
1607 | list_for_each_entry(w, &dapm->card->widgets, list) { | |
2b97eabc | 1608 | if (!wsink && !(strcmp(w->name, sink))) { |
97c866de JN |
1609 | wtsink = w; |
1610 | if (w->dapm == dapm) | |
1611 | wsink = w; | |
2b97eabc RP |
1612 | continue; |
1613 | } | |
1614 | if (!wsource && !(strcmp(w->name, source))) { | |
97c866de JN |
1615 | wtsource = w; |
1616 | if (w->dapm == dapm) | |
1617 | wsource = w; | |
2b97eabc RP |
1618 | } |
1619 | } | |
97c866de JN |
1620 | /* use widget from another DAPM context if not found from this */ |
1621 | if (!wsink) | |
1622 | wsink = wtsink; | |
1623 | if (!wsource) | |
1624 | wsource = wtsource; | |
2b97eabc RP |
1625 | |
1626 | if (wsource == NULL || wsink == NULL) | |
1627 | return -ENODEV; | |
1628 | ||
1629 | path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL); | |
1630 | if (!path) | |
1631 | return -ENOMEM; | |
1632 | ||
1633 | path->source = wsource; | |
1634 | path->sink = wsink; | |
215edda3 | 1635 | path->connected = route->connected; |
2b97eabc RP |
1636 | INIT_LIST_HEAD(&path->list); |
1637 | INIT_LIST_HEAD(&path->list_source); | |
1638 | INIT_LIST_HEAD(&path->list_sink); | |
1639 | ||
1640 | /* check for external widgets */ | |
1641 | if (wsink->id == snd_soc_dapm_input) { | |
1642 | if (wsource->id == snd_soc_dapm_micbias || | |
1643 | wsource->id == snd_soc_dapm_mic || | |
087d53ab RC |
1644 | wsource->id == snd_soc_dapm_line || |
1645 | wsource->id == snd_soc_dapm_output) | |
2b97eabc RP |
1646 | wsink->ext = 1; |
1647 | } | |
1648 | if (wsource->id == snd_soc_dapm_output) { | |
1649 | if (wsink->id == snd_soc_dapm_spk || | |
1650 | wsink->id == snd_soc_dapm_hp || | |
1e39221e SF |
1651 | wsink->id == snd_soc_dapm_line || |
1652 | wsink->id == snd_soc_dapm_input) | |
2b97eabc RP |
1653 | wsource->ext = 1; |
1654 | } | |
1655 | ||
1656 | /* connect static paths */ | |
1657 | if (control == NULL) { | |
8ddab3f5 | 1658 | list_add(&path->list, &dapm->card->paths); |
2b97eabc RP |
1659 | list_add(&path->list_sink, &wsink->sources); |
1660 | list_add(&path->list_source, &wsource->sinks); | |
1661 | path->connect = 1; | |
1662 | return 0; | |
1663 | } | |
1664 | ||
1665 | /* connect dynamic paths */ | |
dc2bea61 | 1666 | switch (wsink->id) { |
2b97eabc RP |
1667 | case snd_soc_dapm_adc: |
1668 | case snd_soc_dapm_dac: | |
1669 | case snd_soc_dapm_pga: | |
d88429a6 | 1670 | case snd_soc_dapm_out_drv: |
2b97eabc RP |
1671 | case snd_soc_dapm_input: |
1672 | case snd_soc_dapm_output: | |
1673 | case snd_soc_dapm_micbias: | |
1674 | case snd_soc_dapm_vmid: | |
1675 | case snd_soc_dapm_pre: | |
1676 | case snd_soc_dapm_post: | |
246d0a17 | 1677 | case snd_soc_dapm_supply: |
010ff262 MB |
1678 | case snd_soc_dapm_aif_in: |
1679 | case snd_soc_dapm_aif_out: | |
8ddab3f5 | 1680 | list_add(&path->list, &dapm->card->paths); |
2b97eabc RP |
1681 | list_add(&path->list_sink, &wsink->sources); |
1682 | list_add(&path->list_source, &wsource->sinks); | |
1683 | path->connect = 1; | |
1684 | return 0; | |
1685 | case snd_soc_dapm_mux: | |
24ff33ac | 1686 | case snd_soc_dapm_virt_mux: |
74155556 | 1687 | case snd_soc_dapm_value_mux: |
ce6120cc | 1688 | ret = dapm_connect_mux(dapm, wsource, wsink, path, control, |
82cfecdc | 1689 | &wsink->kcontrol_news[0]); |
2b97eabc RP |
1690 | if (ret != 0) |
1691 | goto err; | |
1692 | break; | |
1693 | case snd_soc_dapm_switch: | |
1694 | case snd_soc_dapm_mixer: | |
ca9c1aae | 1695 | case snd_soc_dapm_mixer_named_ctl: |
ce6120cc | 1696 | ret = dapm_connect_mixer(dapm, wsource, wsink, path, control); |
2b97eabc RP |
1697 | if (ret != 0) |
1698 | goto err; | |
1699 | break; | |
1700 | case snd_soc_dapm_hp: | |
1701 | case snd_soc_dapm_mic: | |
1702 | case snd_soc_dapm_line: | |
1703 | case snd_soc_dapm_spk: | |
8ddab3f5 | 1704 | list_add(&path->list, &dapm->card->paths); |
2b97eabc RP |
1705 | list_add(&path->list_sink, &wsink->sources); |
1706 | list_add(&path->list_source, &wsource->sinks); | |
1707 | path->connect = 0; | |
1708 | return 0; | |
1709 | } | |
1710 | return 0; | |
1711 | ||
1712 | err: | |
f7d41ae8 JN |
1713 | dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n", |
1714 | source, control, sink); | |
2b97eabc RP |
1715 | kfree(path); |
1716 | return ret; | |
1717 | } | |
105f1c28 | 1718 | |
105f1c28 MB |
1719 | /** |
1720 | * snd_soc_dapm_add_routes - Add routes between DAPM widgets | |
ce6120cc | 1721 | * @dapm: DAPM context |
105f1c28 MB |
1722 | * @route: audio routes |
1723 | * @num: number of routes | |
1724 | * | |
1725 | * Connects 2 dapm widgets together via a named audio path. The sink is | |
1726 | * the widget receiving the audio signal, whilst the source is the sender | |
1727 | * of the audio signal. | |
1728 | * | |
1729 | * Returns 0 for success else error. On error all resources can be freed | |
1730 | * with a call to snd_soc_card_free(). | |
1731 | */ | |
ce6120cc | 1732 | int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm, |
105f1c28 MB |
1733 | const struct snd_soc_dapm_route *route, int num) |
1734 | { | |
1735 | int i, ret; | |
1736 | ||
1737 | for (i = 0; i < num; i++) { | |
ce6120cc | 1738 | ret = snd_soc_dapm_add_route(dapm, route); |
105f1c28 | 1739 | if (ret < 0) { |
f7d41ae8 JN |
1740 | dev_err(dapm->dev, "Failed to add route %s->%s\n", |
1741 | route->source, route->sink); | |
105f1c28 MB |
1742 | return ret; |
1743 | } | |
1744 | route++; | |
1745 | } | |
1746 | ||
1747 | return 0; | |
1748 | } | |
1749 | EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes); | |
1750 | ||
2b97eabc RP |
1751 | /** |
1752 | * snd_soc_dapm_new_widgets - add new dapm widgets | |
ce6120cc | 1753 | * @dapm: DAPM context |
2b97eabc RP |
1754 | * |
1755 | * Checks the codec for any new dapm widgets and creates them if found. | |
1756 | * | |
1757 | * Returns 0 for success. | |
1758 | */ | |
ce6120cc | 1759 | int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm) |
2b97eabc RP |
1760 | { |
1761 | struct snd_soc_dapm_widget *w; | |
b66a70d5 | 1762 | unsigned int val; |
2b97eabc | 1763 | |
97c866de | 1764 | list_for_each_entry(w, &dapm->card->widgets, list) |
2b97eabc RP |
1765 | { |
1766 | if (w->new) | |
1767 | continue; | |
1768 | ||
fad59888 SW |
1769 | if (w->num_kcontrols) { |
1770 | w->kcontrols = kzalloc(w->num_kcontrols * | |
1771 | sizeof(struct snd_kcontrol *), | |
1772 | GFP_KERNEL); | |
1773 | if (!w->kcontrols) | |
1774 | return -ENOMEM; | |
1775 | } | |
1776 | ||
2b97eabc RP |
1777 | switch(w->id) { |
1778 | case snd_soc_dapm_switch: | |
1779 | case snd_soc_dapm_mixer: | |
ca9c1aae | 1780 | case snd_soc_dapm_mixer_named_ctl: |
b75576d7 | 1781 | w->power_check = dapm_generic_check_power; |
ce6120cc | 1782 | dapm_new_mixer(dapm, w); |
2b97eabc RP |
1783 | break; |
1784 | case snd_soc_dapm_mux: | |
24ff33ac | 1785 | case snd_soc_dapm_virt_mux: |
2e72f8e3 | 1786 | case snd_soc_dapm_value_mux: |
b75576d7 | 1787 | w->power_check = dapm_generic_check_power; |
ce6120cc | 1788 | dapm_new_mux(dapm, w); |
2b97eabc RP |
1789 | break; |
1790 | case snd_soc_dapm_adc: | |
010ff262 | 1791 | case snd_soc_dapm_aif_out: |
b75576d7 MB |
1792 | w->power_check = dapm_adc_check_power; |
1793 | break; | |
2b97eabc | 1794 | case snd_soc_dapm_dac: |
010ff262 | 1795 | case snd_soc_dapm_aif_in: |
b75576d7 MB |
1796 | w->power_check = dapm_dac_check_power; |
1797 | break; | |
2b97eabc | 1798 | case snd_soc_dapm_pga: |
d88429a6 | 1799 | case snd_soc_dapm_out_drv: |
b75576d7 | 1800 | w->power_check = dapm_generic_check_power; |
ce6120cc | 1801 | dapm_new_pga(dapm, w); |
2b97eabc RP |
1802 | break; |
1803 | case snd_soc_dapm_input: | |
1804 | case snd_soc_dapm_output: | |
1805 | case snd_soc_dapm_micbias: | |
1806 | case snd_soc_dapm_spk: | |
1807 | case snd_soc_dapm_hp: | |
1808 | case snd_soc_dapm_mic: | |
1809 | case snd_soc_dapm_line: | |
b75576d7 MB |
1810 | w->power_check = dapm_generic_check_power; |
1811 | break; | |
246d0a17 MB |
1812 | case snd_soc_dapm_supply: |
1813 | w->power_check = dapm_supply_check_power; | |
2b97eabc RP |
1814 | case snd_soc_dapm_vmid: |
1815 | case snd_soc_dapm_pre: | |
1816 | case snd_soc_dapm_post: | |
1817 | break; | |
1818 | } | |
b66a70d5 MB |
1819 | |
1820 | /* Read the initial power state from the device */ | |
1821 | if (w->reg >= 0) { | |
1822 | val = snd_soc_read(w->codec, w->reg); | |
1823 | val &= 1 << w->shift; | |
1824 | if (w->invert) | |
1825 | val = !val; | |
1826 | ||
1827 | if (val) | |
1828 | w->power = 1; | |
1829 | } | |
1830 | ||
2b97eabc | 1831 | w->new = 1; |
d5d1e0be LPC |
1832 | |
1833 | dapm_debugfs_add_widget(w); | |
2b97eabc RP |
1834 | } |
1835 | ||
ce6120cc | 1836 | dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP); |
2b97eabc RP |
1837 | return 0; |
1838 | } | |
1839 | EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets); | |
1840 | ||
1841 | /** | |
1842 | * snd_soc_dapm_get_volsw - dapm mixer get callback | |
1843 | * @kcontrol: mixer control | |
ac11a2b3 | 1844 | * @ucontrol: control element information |
2b97eabc RP |
1845 | * |
1846 | * Callback to get the value of a dapm mixer control. | |
1847 | * | |
1848 | * Returns 0 for success. | |
1849 | */ | |
1850 | int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol, | |
1851 | struct snd_ctl_elem_value *ucontrol) | |
1852 | { | |
fafd2176 SW |
1853 | struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol); |
1854 | struct snd_soc_dapm_widget *widget = wlist->widgets[0]; | |
4eaa9819 JS |
1855 | struct soc_mixer_control *mc = |
1856 | (struct soc_mixer_control *)kcontrol->private_value; | |
815ecf8d JS |
1857 | unsigned int reg = mc->reg; |
1858 | unsigned int shift = mc->shift; | |
1859 | unsigned int rshift = mc->rshift; | |
4eaa9819 | 1860 | int max = mc->max; |
815ecf8d JS |
1861 | unsigned int invert = mc->invert; |
1862 | unsigned int mask = (1 << fls(max)) - 1; | |
2b97eabc | 1863 | |
2b97eabc RP |
1864 | ucontrol->value.integer.value[0] = |
1865 | (snd_soc_read(widget->codec, reg) >> shift) & mask; | |
1866 | if (shift != rshift) | |
1867 | ucontrol->value.integer.value[1] = | |
1868 | (snd_soc_read(widget->codec, reg) >> rshift) & mask; | |
1869 | if (invert) { | |
1870 | ucontrol->value.integer.value[0] = | |
a7a4ac86 | 1871 | max - ucontrol->value.integer.value[0]; |
2b97eabc RP |
1872 | if (shift != rshift) |
1873 | ucontrol->value.integer.value[1] = | |
a7a4ac86 | 1874 | max - ucontrol->value.integer.value[1]; |
2b97eabc RP |
1875 | } |
1876 | ||
1877 | return 0; | |
1878 | } | |
1879 | EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw); | |
1880 | ||
1881 | /** | |
1882 | * snd_soc_dapm_put_volsw - dapm mixer set callback | |
1883 | * @kcontrol: mixer control | |
ac11a2b3 | 1884 | * @ucontrol: control element information |
2b97eabc RP |
1885 | * |
1886 | * Callback to set the value of a dapm mixer control. | |
1887 | * | |
1888 | * Returns 0 for success. | |
1889 | */ | |
1890 | int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol, | |
1891 | struct snd_ctl_elem_value *ucontrol) | |
1892 | { | |
fafd2176 SW |
1893 | struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol); |
1894 | struct snd_soc_dapm_widget *widget = wlist->widgets[0]; | |
1895 | struct snd_soc_codec *codec = widget->codec; | |
4eaa9819 JS |
1896 | struct soc_mixer_control *mc = |
1897 | (struct soc_mixer_control *)kcontrol->private_value; | |
815ecf8d JS |
1898 | unsigned int reg = mc->reg; |
1899 | unsigned int shift = mc->shift; | |
4eaa9819 | 1900 | int max = mc->max; |
815ecf8d JS |
1901 | unsigned int mask = (1 << fls(max)) - 1; |
1902 | unsigned int invert = mc->invert; | |
e9cf7049 | 1903 | unsigned int val; |
97404f2e MB |
1904 | int connect, change; |
1905 | struct snd_soc_dapm_update update; | |
fafd2176 | 1906 | int wi; |
2b97eabc RP |
1907 | |
1908 | val = (ucontrol->value.integer.value[0] & mask); | |
1909 | ||
1910 | if (invert) | |
a7a4ac86 | 1911 | val = max - val; |
e9cf7049 | 1912 | mask = mask << shift; |
2b97eabc | 1913 | val = val << shift; |
2b97eabc | 1914 | |
fafd2176 SW |
1915 | if (val) |
1916 | /* new connection */ | |
1917 | connect = invert ? 0 : 1; | |
1918 | else | |
1919 | /* old connection must be powered down */ | |
1920 | connect = invert ? 1 : 0; | |
1921 | ||
1922 | mutex_lock(&codec->mutex); | |
2b97eabc | 1923 | |
e9cf7049 | 1924 | change = snd_soc_test_bits(widget->codec, reg, mask, val); |
97404f2e | 1925 | if (change) { |
fafd2176 SW |
1926 | for (wi = 0; wi < wlist->num_widgets; wi++) { |
1927 | widget = wlist->widgets[wi]; | |
283375ce | 1928 | |
fafd2176 | 1929 | widget->value = val; |
97404f2e | 1930 | |
fafd2176 SW |
1931 | update.kcontrol = kcontrol; |
1932 | update.widget = widget; | |
1933 | update.reg = reg; | |
1934 | update.mask = mask; | |
1935 | update.val = val; | |
1936 | widget->dapm->update = &update; | |
97404f2e | 1937 | |
fafd2176 SW |
1938 | dapm_mixer_update_power(widget, kcontrol, connect); |
1939 | ||
1940 | widget->dapm->update = NULL; | |
1941 | } | |
283375ce MB |
1942 | } |
1943 | ||
fafd2176 | 1944 | mutex_unlock(&codec->mutex); |
97404f2e | 1945 | return 0; |
2b97eabc RP |
1946 | } |
1947 | EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw); | |
1948 | ||
1949 | /** | |
1950 | * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback | |
1951 | * @kcontrol: mixer control | |
ac11a2b3 | 1952 | * @ucontrol: control element information |
2b97eabc RP |
1953 | * |
1954 | * Callback to get the value of a dapm enumerated double mixer control. | |
1955 | * | |
1956 | * Returns 0 for success. | |
1957 | */ | |
1958 | int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol, | |
1959 | struct snd_ctl_elem_value *ucontrol) | |
1960 | { | |
fafd2176 SW |
1961 | struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol); |
1962 | struct snd_soc_dapm_widget *widget = wlist->widgets[0]; | |
2b97eabc | 1963 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
46f5822f | 1964 | unsigned int val, bitmask; |
2b97eabc | 1965 | |
f8ba0b7b | 1966 | for (bitmask = 1; bitmask < e->max; bitmask <<= 1) |
2b97eabc RP |
1967 | ; |
1968 | val = snd_soc_read(widget->codec, e->reg); | |
1969 | ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1); | |
1970 | if (e->shift_l != e->shift_r) | |
1971 | ucontrol->value.enumerated.item[1] = | |
1972 | (val >> e->shift_r) & (bitmask - 1); | |
1973 | ||
1974 | return 0; | |
1975 | } | |
1976 | EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double); | |
1977 | ||
1978 | /** | |
1979 | * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback | |
1980 | * @kcontrol: mixer control | |
ac11a2b3 | 1981 | * @ucontrol: control element information |
2b97eabc RP |
1982 | * |
1983 | * Callback to set the value of a dapm enumerated double mixer control. | |
1984 | * | |
1985 | * Returns 0 for success. | |
1986 | */ | |
1987 | int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol, | |
1988 | struct snd_ctl_elem_value *ucontrol) | |
1989 | { | |
fafd2176 SW |
1990 | struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol); |
1991 | struct snd_soc_dapm_widget *widget = wlist->widgets[0]; | |
1992 | struct snd_soc_codec *codec = widget->codec; | |
2b97eabc | 1993 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
3a65577d | 1994 | unsigned int val, mux, change; |
46f5822f | 1995 | unsigned int mask, bitmask; |
97404f2e | 1996 | struct snd_soc_dapm_update update; |
fafd2176 | 1997 | int wi; |
2b97eabc | 1998 | |
f8ba0b7b | 1999 | for (bitmask = 1; bitmask < e->max; bitmask <<= 1) |
2b97eabc | 2000 | ; |
f8ba0b7b | 2001 | if (ucontrol->value.enumerated.item[0] > e->max - 1) |
2b97eabc RP |
2002 | return -EINVAL; |
2003 | mux = ucontrol->value.enumerated.item[0]; | |
2004 | val = mux << e->shift_l; | |
2005 | mask = (bitmask - 1) << e->shift_l; | |
2006 | if (e->shift_l != e->shift_r) { | |
f8ba0b7b | 2007 | if (ucontrol->value.enumerated.item[1] > e->max - 1) |
2b97eabc RP |
2008 | return -EINVAL; |
2009 | val |= ucontrol->value.enumerated.item[1] << e->shift_r; | |
2010 | mask |= (bitmask - 1) << e->shift_r; | |
2011 | } | |
2012 | ||
fafd2176 SW |
2013 | mutex_lock(&codec->mutex); |
2014 | ||
3a65577d | 2015 | change = snd_soc_test_bits(widget->codec, e->reg, mask, val); |
fafd2176 SW |
2016 | if (change) { |
2017 | for (wi = 0; wi < wlist->num_widgets; wi++) { | |
2018 | widget = wlist->widgets[wi]; | |
2019 | ||
2020 | widget->value = val; | |
1642e3d4 | 2021 | |
fafd2176 SW |
2022 | update.kcontrol = kcontrol; |
2023 | update.widget = widget; | |
2024 | update.reg = e->reg; | |
2025 | update.mask = mask; | |
2026 | update.val = val; | |
2027 | widget->dapm->update = &update; | |
1642e3d4 | 2028 | |
fafd2176 | 2029 | dapm_mux_update_power(widget, kcontrol, change, mux, e); |
1642e3d4 | 2030 | |
fafd2176 SW |
2031 | widget->dapm->update = NULL; |
2032 | } | |
2033 | } | |
2b97eabc | 2034 | |
fafd2176 | 2035 | mutex_unlock(&codec->mutex); |
97404f2e | 2036 | return change; |
2b97eabc RP |
2037 | } |
2038 | EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double); | |
2039 | ||
d2b247a8 MB |
2040 | /** |
2041 | * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux | |
2042 | * @kcontrol: mixer control | |
2043 | * @ucontrol: control element information | |
2044 | * | |
2045 | * Returns 0 for success. | |
2046 | */ | |
2047 | int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol, | |
2048 | struct snd_ctl_elem_value *ucontrol) | |
2049 | { | |
fafd2176 SW |
2050 | struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol); |
2051 | struct snd_soc_dapm_widget *widget = wlist->widgets[0]; | |
d2b247a8 MB |
2052 | |
2053 | ucontrol->value.enumerated.item[0] = widget->value; | |
2054 | ||
2055 | return 0; | |
2056 | } | |
2057 | EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt); | |
2058 | ||
2059 | /** | |
2060 | * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux | |
2061 | * @kcontrol: mixer control | |
2062 | * @ucontrol: control element information | |
2063 | * | |
2064 | * Returns 0 for success. | |
2065 | */ | |
2066 | int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol, | |
2067 | struct snd_ctl_elem_value *ucontrol) | |
2068 | { | |
fafd2176 SW |
2069 | struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol); |
2070 | struct snd_soc_dapm_widget *widget = wlist->widgets[0]; | |
2071 | struct snd_soc_codec *codec = widget->codec; | |
d2b247a8 MB |
2072 | struct soc_enum *e = |
2073 | (struct soc_enum *)kcontrol->private_value; | |
2074 | int change; | |
2075 | int ret = 0; | |
fafd2176 | 2076 | int wi; |
d2b247a8 MB |
2077 | |
2078 | if (ucontrol->value.enumerated.item[0] >= e->max) | |
2079 | return -EINVAL; | |
2080 | ||
fafd2176 | 2081 | mutex_lock(&codec->mutex); |
d2b247a8 MB |
2082 | |
2083 | change = widget->value != ucontrol->value.enumerated.item[0]; | |
fafd2176 SW |
2084 | if (change) { |
2085 | for (wi = 0; wi < wlist->num_widgets; wi++) { | |
2086 | widget = wlist->widgets[wi]; | |
2087 | ||
2088 | widget->value = ucontrol->value.enumerated.item[0]; | |
2089 | ||
2090 | dapm_mux_update_power(widget, kcontrol, change, | |
2091 | widget->value, e); | |
2092 | } | |
2093 | } | |
d2b247a8 | 2094 | |
fafd2176 | 2095 | mutex_unlock(&codec->mutex); |
d2b247a8 MB |
2096 | return ret; |
2097 | } | |
2098 | EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt); | |
2099 | ||
2e72f8e3 PU |
2100 | /** |
2101 | * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get | |
2102 | * callback | |
2103 | * @kcontrol: mixer control | |
2104 | * @ucontrol: control element information | |
2105 | * | |
2106 | * Callback to get the value of a dapm semi enumerated double mixer control. | |
2107 | * | |
2108 | * Semi enumerated mixer: the enumerated items are referred as values. Can be | |
2109 | * used for handling bitfield coded enumeration for example. | |
2110 | * | |
2111 | * Returns 0 for success. | |
2112 | */ | |
2113 | int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol, | |
2114 | struct snd_ctl_elem_value *ucontrol) | |
2115 | { | |
fafd2176 SW |
2116 | struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol); |
2117 | struct snd_soc_dapm_widget *widget = wlist->widgets[0]; | |
74155556 | 2118 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
46f5822f | 2119 | unsigned int reg_val, val, mux; |
2e72f8e3 PU |
2120 | |
2121 | reg_val = snd_soc_read(widget->codec, e->reg); | |
2122 | val = (reg_val >> e->shift_l) & e->mask; | |
2123 | for (mux = 0; mux < e->max; mux++) { | |
2124 | if (val == e->values[mux]) | |
2125 | break; | |
2126 | } | |
2127 | ucontrol->value.enumerated.item[0] = mux; | |
2128 | if (e->shift_l != e->shift_r) { | |
2129 | val = (reg_val >> e->shift_r) & e->mask; | |
2130 | for (mux = 0; mux < e->max; mux++) { | |
2131 | if (val == e->values[mux]) | |
2132 | break; | |
2133 | } | |
2134 | ucontrol->value.enumerated.item[1] = mux; | |
2135 | } | |
2136 | ||
2137 | return 0; | |
2138 | } | |
2139 | EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double); | |
2140 | ||
2141 | /** | |
2142 | * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set | |
2143 | * callback | |
2144 | * @kcontrol: mixer control | |
2145 | * @ucontrol: control element information | |
2146 | * | |
2147 | * Callback to set the value of a dapm semi enumerated double mixer control. | |
2148 | * | |
2149 | * Semi enumerated mixer: the enumerated items are referred as values. Can be | |
2150 | * used for handling bitfield coded enumeration for example. | |
2151 | * | |
2152 | * Returns 0 for success. | |
2153 | */ | |
2154 | int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol, | |
2155 | struct snd_ctl_elem_value *ucontrol) | |
2156 | { | |
fafd2176 SW |
2157 | struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol); |
2158 | struct snd_soc_dapm_widget *widget = wlist->widgets[0]; | |
2159 | struct snd_soc_codec *codec = widget->codec; | |
74155556 | 2160 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
3a65577d | 2161 | unsigned int val, mux, change; |
46f5822f | 2162 | unsigned int mask; |
97404f2e | 2163 | struct snd_soc_dapm_update update; |
fafd2176 | 2164 | int wi; |
2e72f8e3 PU |
2165 | |
2166 | if (ucontrol->value.enumerated.item[0] > e->max - 1) | |
2167 | return -EINVAL; | |
2168 | mux = ucontrol->value.enumerated.item[0]; | |
2169 | val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l; | |
2170 | mask = e->mask << e->shift_l; | |
2171 | if (e->shift_l != e->shift_r) { | |
2172 | if (ucontrol->value.enumerated.item[1] > e->max - 1) | |
2173 | return -EINVAL; | |
2174 | val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r; | |
2175 | mask |= e->mask << e->shift_r; | |
2176 | } | |
2177 | ||
fafd2176 SW |
2178 | mutex_lock(&codec->mutex); |
2179 | ||
3a65577d | 2180 | change = snd_soc_test_bits(widget->codec, e->reg, mask, val); |
fafd2176 SW |
2181 | if (change) { |
2182 | for (wi = 0; wi < wlist->num_widgets; wi++) { | |
2183 | widget = wlist->widgets[wi]; | |
1642e3d4 | 2184 | |
fafd2176 | 2185 | widget->value = val; |
1642e3d4 | 2186 | |
fafd2176 SW |
2187 | update.kcontrol = kcontrol; |
2188 | update.widget = widget; | |
2189 | update.reg = e->reg; | |
2190 | update.mask = mask; | |
2191 | update.val = val; | |
2192 | widget->dapm->update = &update; | |
1642e3d4 | 2193 | |
fafd2176 SW |
2194 | dapm_mux_update_power(widget, kcontrol, change, mux, e); |
2195 | ||
2196 | widget->dapm->update = NULL; | |
2197 | } | |
2198 | } | |
2e72f8e3 | 2199 | |
fafd2176 | 2200 | mutex_unlock(&codec->mutex); |
97404f2e | 2201 | return change; |
2e72f8e3 PU |
2202 | } |
2203 | EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double); | |
2204 | ||
8b37dbd2 MB |
2205 | /** |
2206 | * snd_soc_dapm_info_pin_switch - Info for a pin switch | |
2207 | * | |
2208 | * @kcontrol: mixer control | |
2209 | * @uinfo: control element information | |
2210 | * | |
2211 | * Callback to provide information about a pin switch control. | |
2212 | */ | |
2213 | int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol, | |
2214 | struct snd_ctl_elem_info *uinfo) | |
2215 | { | |
2216 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; | |
2217 | uinfo->count = 1; | |
2218 | uinfo->value.integer.min = 0; | |
2219 | uinfo->value.integer.max = 1; | |
2220 | ||
2221 | return 0; | |
2222 | } | |
2223 | EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch); | |
2224 | ||
2225 | /** | |
2226 | * snd_soc_dapm_get_pin_switch - Get information for a pin switch | |
2227 | * | |
2228 | * @kcontrol: mixer control | |
2229 | * @ucontrol: Value | |
2230 | */ | |
2231 | int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol, | |
2232 | struct snd_ctl_elem_value *ucontrol) | |
2233 | { | |
2234 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
2235 | const char *pin = (const char *)kcontrol->private_value; | |
2236 | ||
2237 | mutex_lock(&codec->mutex); | |
2238 | ||
2239 | ucontrol->value.integer.value[0] = | |
ce6120cc | 2240 | snd_soc_dapm_get_pin_status(&codec->dapm, pin); |
8b37dbd2 MB |
2241 | |
2242 | mutex_unlock(&codec->mutex); | |
2243 | ||
2244 | return 0; | |
2245 | } | |
2246 | EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch); | |
2247 | ||
2248 | /** | |
2249 | * snd_soc_dapm_put_pin_switch - Set information for a pin switch | |
2250 | * | |
2251 | * @kcontrol: mixer control | |
2252 | * @ucontrol: Value | |
2253 | */ | |
2254 | int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol, | |
2255 | struct snd_ctl_elem_value *ucontrol) | |
2256 | { | |
2257 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
2258 | const char *pin = (const char *)kcontrol->private_value; | |
2259 | ||
2260 | mutex_lock(&codec->mutex); | |
2261 | ||
2262 | if (ucontrol->value.integer.value[0]) | |
ce6120cc | 2263 | snd_soc_dapm_enable_pin(&codec->dapm, pin); |
8b37dbd2 | 2264 | else |
ce6120cc | 2265 | snd_soc_dapm_disable_pin(&codec->dapm, pin); |
8b37dbd2 | 2266 | |
ce6120cc | 2267 | snd_soc_dapm_sync(&codec->dapm); |
8b37dbd2 MB |
2268 | |
2269 | mutex_unlock(&codec->mutex); | |
2270 | ||
2271 | return 0; | |
2272 | } | |
2273 | EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch); | |
2274 | ||
2b97eabc RP |
2275 | /** |
2276 | * snd_soc_dapm_new_control - create new dapm control | |
ce6120cc | 2277 | * @dapm: DAPM context |
2b97eabc RP |
2278 | * @widget: widget template |
2279 | * | |
2280 | * Creates a new dapm control based upon the template. | |
2281 | * | |
2282 | * Returns 0 for success else error. | |
2283 | */ | |
ce6120cc | 2284 | int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm, |
2b97eabc RP |
2285 | const struct snd_soc_dapm_widget *widget) |
2286 | { | |
2287 | struct snd_soc_dapm_widget *w; | |
ead9b919 | 2288 | size_t name_len; |
2b97eabc RP |
2289 | |
2290 | if ((w = dapm_cnew_widget(widget)) == NULL) | |
2291 | return -ENOMEM; | |
2292 | ||
ead9b919 | 2293 | name_len = strlen(widget->name) + 1; |
88e8b9a8 | 2294 | if (dapm->codec && dapm->codec->name_prefix) |
ead9b919 JN |
2295 | name_len += 1 + strlen(dapm->codec->name_prefix); |
2296 | w->name = kmalloc(name_len, GFP_KERNEL); | |
2297 | if (w->name == NULL) { | |
2298 | kfree(w); | |
2299 | return -ENOMEM; | |
2300 | } | |
88e8b9a8 | 2301 | if (dapm->codec && dapm->codec->name_prefix) |
ead9b919 JN |
2302 | snprintf(w->name, name_len, "%s %s", |
2303 | dapm->codec->name_prefix, widget->name); | |
2304 | else | |
2305 | snprintf(w->name, name_len, "%s", widget->name); | |
2306 | ||
97c866de | 2307 | dapm->n_widgets++; |
ce6120cc LG |
2308 | w->dapm = dapm; |
2309 | w->codec = dapm->codec; | |
2b97eabc RP |
2310 | INIT_LIST_HEAD(&w->sources); |
2311 | INIT_LIST_HEAD(&w->sinks); | |
2312 | INIT_LIST_HEAD(&w->list); | |
97c866de | 2313 | list_add(&w->list, &dapm->card->widgets); |
2b97eabc RP |
2314 | |
2315 | /* machine layer set ups unconnected pins and insertions */ | |
2316 | w->connected = 1; | |
2317 | return 0; | |
2318 | } | |
2319 | EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control); | |
2320 | ||
4ba1327a MB |
2321 | /** |
2322 | * snd_soc_dapm_new_controls - create new dapm controls | |
ce6120cc | 2323 | * @dapm: DAPM context |
4ba1327a MB |
2324 | * @widget: widget array |
2325 | * @num: number of widgets | |
2326 | * | |
2327 | * Creates new DAPM controls based upon the templates. | |
2328 | * | |
2329 | * Returns 0 for success else error. | |
2330 | */ | |
ce6120cc | 2331 | int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm, |
4ba1327a MB |
2332 | const struct snd_soc_dapm_widget *widget, |
2333 | int num) | |
2334 | { | |
2335 | int i, ret; | |
2336 | ||
2337 | for (i = 0; i < num; i++) { | |
ce6120cc | 2338 | ret = snd_soc_dapm_new_control(dapm, widget); |
b8b33cb5 | 2339 | if (ret < 0) { |
f7d41ae8 JN |
2340 | dev_err(dapm->dev, |
2341 | "ASoC: Failed to create DAPM control %s: %d\n", | |
2342 | widget->name, ret); | |
4ba1327a | 2343 | return ret; |
b8b33cb5 | 2344 | } |
4ba1327a MB |
2345 | widget++; |
2346 | } | |
2347 | return 0; | |
2348 | } | |
2349 | EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls); | |
2350 | ||
ce6120cc | 2351 | static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm, |
f0fba2ad | 2352 | const char *stream, int event) |
2b97eabc RP |
2353 | { |
2354 | struct snd_soc_dapm_widget *w; | |
2355 | ||
97c866de | 2356 | list_for_each_entry(w, &dapm->card->widgets, list) |
2b97eabc | 2357 | { |
97c866de | 2358 | if (!w->sname || w->dapm != dapm) |
2b97eabc | 2359 | continue; |
f7d41ae8 JN |
2360 | dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n", |
2361 | w->name, w->sname, stream, event); | |
2b97eabc RP |
2362 | if (strstr(w->sname, stream)) { |
2363 | switch(event) { | |
2364 | case SND_SOC_DAPM_STREAM_START: | |
2365 | w->active = 1; | |
2366 | break; | |
2367 | case SND_SOC_DAPM_STREAM_STOP: | |
2368 | w->active = 0; | |
2369 | break; | |
2370 | case SND_SOC_DAPM_STREAM_SUSPEND: | |
2b97eabc | 2371 | case SND_SOC_DAPM_STREAM_RESUME: |
2b97eabc | 2372 | case SND_SOC_DAPM_STREAM_PAUSE_PUSH: |
2b97eabc RP |
2373 | case SND_SOC_DAPM_STREAM_PAUSE_RELEASE: |
2374 | break; | |
2375 | } | |
2376 | } | |
2377 | } | |
2b97eabc | 2378 | |
ce6120cc LG |
2379 | dapm_power_widgets(dapm, event); |
2380 | } | |
2381 | ||
2382 | /** | |
2383 | * snd_soc_dapm_stream_event - send a stream event to the dapm core | |
2384 | * @rtd: PCM runtime data | |
2385 | * @stream: stream name | |
2386 | * @event: stream event | |
2387 | * | |
2388 | * Sends a stream event to the dapm core. The core then makes any | |
2389 | * necessary widget power changes. | |
2390 | * | |
2391 | * Returns 0 for success else error. | |
2392 | */ | |
2393 | int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, | |
2394 | const char *stream, int event) | |
2395 | { | |
2396 | struct snd_soc_codec *codec = rtd->codec; | |
2397 | ||
2398 | if (stream == NULL) | |
2399 | return 0; | |
2400 | ||
2401 | mutex_lock(&codec->mutex); | |
2402 | soc_dapm_stream_event(&codec->dapm, stream, event); | |
8e8b2d67 | 2403 | mutex_unlock(&codec->mutex); |
2b97eabc RP |
2404 | return 0; |
2405 | } | |
2b97eabc RP |
2406 | |
2407 | /** | |
a5302181 | 2408 | * snd_soc_dapm_enable_pin - enable pin. |
ce6120cc | 2409 | * @dapm: DAPM context |
a5302181 | 2410 | * @pin: pin name |
2b97eabc | 2411 | * |
74b8f955 | 2412 | * Enables input/output pin and its parents or children widgets iff there is |
a5302181 LG |
2413 | * a valid audio route and active audio stream. |
2414 | * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to | |
2415 | * do any widget power switching. | |
2b97eabc | 2416 | */ |
ce6120cc | 2417 | int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin) |
2b97eabc | 2418 | { |
ce6120cc | 2419 | return snd_soc_dapm_set_pin(dapm, pin, 1); |
a5302181 LG |
2420 | } |
2421 | EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin); | |
2b97eabc | 2422 | |
da34183e MB |
2423 | /** |
2424 | * snd_soc_dapm_force_enable_pin - force a pin to be enabled | |
ce6120cc | 2425 | * @dapm: DAPM context |
da34183e MB |
2426 | * @pin: pin name |
2427 | * | |
2428 | * Enables input/output pin regardless of any other state. This is | |
2429 | * intended for use with microphone bias supplies used in microphone | |
2430 | * jack detection. | |
2431 | * | |
2432 | * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to | |
2433 | * do any widget power switching. | |
2434 | */ | |
ce6120cc LG |
2435 | int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm, |
2436 | const char *pin) | |
da34183e | 2437 | { |
91a5fca4 | 2438 | struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true); |
da34183e | 2439 | |
91a5fca4 LPC |
2440 | if (!w) { |
2441 | dev_err(dapm->dev, "dapm: unknown pin %s\n", pin); | |
2442 | return -EINVAL; | |
0d86733c MB |
2443 | } |
2444 | ||
91a5fca4 LPC |
2445 | dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin); |
2446 | w->connected = 1; | |
2447 | w->force = 1; | |
da34183e | 2448 | |
91a5fca4 | 2449 | return 0; |
da34183e MB |
2450 | } |
2451 | EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin); | |
2452 | ||
a5302181 LG |
2453 | /** |
2454 | * snd_soc_dapm_disable_pin - disable pin. | |
ce6120cc | 2455 | * @dapm: DAPM context |
a5302181 LG |
2456 | * @pin: pin name |
2457 | * | |
74b8f955 | 2458 | * Disables input/output pin and its parents or children widgets. |
a5302181 LG |
2459 | * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to |
2460 | * do any widget power switching. | |
2461 | */ | |
ce6120cc LG |
2462 | int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm, |
2463 | const char *pin) | |
a5302181 | 2464 | { |
ce6120cc | 2465 | return snd_soc_dapm_set_pin(dapm, pin, 0); |
2b97eabc | 2466 | } |
a5302181 | 2467 | EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin); |
2b97eabc | 2468 | |
5817b52a MB |
2469 | /** |
2470 | * snd_soc_dapm_nc_pin - permanently disable pin. | |
ce6120cc | 2471 | * @dapm: DAPM context |
5817b52a MB |
2472 | * @pin: pin name |
2473 | * | |
2474 | * Marks the specified pin as being not connected, disabling it along | |
2475 | * any parent or child widgets. At present this is identical to | |
2476 | * snd_soc_dapm_disable_pin() but in future it will be extended to do | |
2477 | * additional things such as disabling controls which only affect | |
2478 | * paths through the pin. | |
2479 | * | |
2480 | * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to | |
2481 | * do any widget power switching. | |
2482 | */ | |
ce6120cc | 2483 | int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin) |
5817b52a | 2484 | { |
ce6120cc | 2485 | return snd_soc_dapm_set_pin(dapm, pin, 0); |
5817b52a MB |
2486 | } |
2487 | EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin); | |
2488 | ||
eeec12bf | 2489 | /** |
a5302181 | 2490 | * snd_soc_dapm_get_pin_status - get audio pin status |
ce6120cc | 2491 | * @dapm: DAPM context |
a5302181 | 2492 | * @pin: audio signal pin endpoint (or start point) |
eeec12bf | 2493 | * |
a5302181 | 2494 | * Get audio pin status - connected or disconnected. |
eeec12bf | 2495 | * |
a5302181 | 2496 | * Returns 1 for connected otherwise 0. |
eeec12bf | 2497 | */ |
ce6120cc LG |
2498 | int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm, |
2499 | const char *pin) | |
eeec12bf | 2500 | { |
91a5fca4 | 2501 | struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true); |
eeec12bf | 2502 | |
91a5fca4 LPC |
2503 | if (w) |
2504 | return w->connected; | |
a68b38ad | 2505 | |
eeec12bf GG |
2506 | return 0; |
2507 | } | |
a5302181 | 2508 | EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status); |
eeec12bf | 2509 | |
1547aba9 MB |
2510 | /** |
2511 | * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint | |
ce6120cc | 2512 | * @dapm: DAPM context |
1547aba9 MB |
2513 | * @pin: audio signal pin endpoint (or start point) |
2514 | * | |
2515 | * Mark the given endpoint or pin as ignoring suspend. When the | |
2516 | * system is disabled a path between two endpoints flagged as ignoring | |
2517 | * suspend will not be disabled. The path must already be enabled via | |
2518 | * normal means at suspend time, it will not be turned on if it was not | |
2519 | * already enabled. | |
2520 | */ | |
ce6120cc LG |
2521 | int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm, |
2522 | const char *pin) | |
1547aba9 | 2523 | { |
91a5fca4 | 2524 | struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false); |
1547aba9 | 2525 | |
91a5fca4 LPC |
2526 | if (!w) { |
2527 | dev_err(dapm->dev, "dapm: unknown pin %s\n", pin); | |
2528 | return -EINVAL; | |
1547aba9 MB |
2529 | } |
2530 | ||
91a5fca4 LPC |
2531 | w->ignore_suspend = 1; |
2532 | ||
2533 | return 0; | |
1547aba9 MB |
2534 | } |
2535 | EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend); | |
2536 | ||
2b97eabc RP |
2537 | /** |
2538 | * snd_soc_dapm_free - free dapm resources | |
f0fba2ad | 2539 | * @card: SoC device |
2b97eabc RP |
2540 | * |
2541 | * Free all dapm widgets and resources. | |
2542 | */ | |
ce6120cc | 2543 | void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm) |
2b97eabc | 2544 | { |
ce6120cc | 2545 | snd_soc_dapm_sys_remove(dapm->dev); |
6c45e126 | 2546 | dapm_debugfs_cleanup(dapm); |
ce6120cc | 2547 | dapm_free_widgets(dapm); |
7be31be8 | 2548 | list_del(&dapm->list); |
2b97eabc RP |
2549 | } |
2550 | EXPORT_SYMBOL_GPL(snd_soc_dapm_free); | |
2551 | ||
ce6120cc | 2552 | static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm) |
51737470 | 2553 | { |
51737470 MB |
2554 | struct snd_soc_dapm_widget *w; |
2555 | LIST_HEAD(down_list); | |
2556 | int powerdown = 0; | |
2557 | ||
97c866de JN |
2558 | list_for_each_entry(w, &dapm->card->widgets, list) { |
2559 | if (w->dapm != dapm) | |
2560 | continue; | |
51737470 | 2561 | if (w->power) { |
828a842f | 2562 | dapm_seq_insert(w, &down_list, false); |
c2caa4da | 2563 | w->power = 0; |
51737470 MB |
2564 | powerdown = 1; |
2565 | } | |
2566 | } | |
2567 | ||
2568 | /* If there were no widgets to power down we're already in | |
2569 | * standby. | |
2570 | */ | |
2571 | if (powerdown) { | |
ed5a4c47 | 2572 | snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE); |
828a842f | 2573 | dapm_seq_run(dapm, &down_list, 0, false); |
ed5a4c47 | 2574 | snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY); |
51737470 | 2575 | } |
f0fba2ad LG |
2576 | } |
2577 | ||
2578 | /* | |
2579 | * snd_soc_dapm_shutdown - callback for system shutdown | |
2580 | */ | |
2581 | void snd_soc_dapm_shutdown(struct snd_soc_card *card) | |
2582 | { | |
2583 | struct snd_soc_codec *codec; | |
2584 | ||
ce6120cc LG |
2585 | list_for_each_entry(codec, &card->codec_dev_list, list) { |
2586 | soc_dapm_shutdown_codec(&codec->dapm); | |
ed5a4c47 | 2587 | snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF); |
ce6120cc | 2588 | } |
51737470 MB |
2589 | } |
2590 | ||
2b97eabc | 2591 | /* Module information */ |
d331124d | 2592 | MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); |
2b97eabc RP |
2593 | MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC"); |
2594 | MODULE_LICENSE("GPL"); |