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