1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 //
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 //
8 // Features:
9 // o Changes power status of internal codec blocks depending on the
10 // dynamic configuration of codec internal audio paths and active
11 // DACs/ADCs.
12 // o Platform power domain - can support external components i.e. amps and
13 // mic/headphone insertion events.
14 // o Automatic Mic Bias support
15 // o Jack insertion power event initiation - e.g. hp insertion will enable
16 // sinks, dacs, etc
17 // o Delayed power down of audio subsystem to reduce pops between a quick
18 // device reopen.
19
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/async.h>
23 #include <linux/cleanup.h>
24 #include <linux/delay.h>
25 #include <linux/pm.h>
26 #include <linux/bitops.h>
27 #include <linux/platform_device.h>
28 #include <linux/jiffies.h>
29 #include <linux/debugfs.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/pinctrl/consumer.h>
33 #include <linux/clk.h>
34 #include <linux/slab.h>
35 #include <sound/core.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
39 #include <sound/initval.h>
40
41 #include <trace/events/asoc.h>
42
43 /* DAPM context */
44 struct snd_soc_dapm_context {
45 enum snd_soc_bias_level bias_level;
46
47 bool idle_bias; /* Use BIAS_OFF instead of STANDBY when false */
48
49 struct snd_soc_component *component; /* parent component */
50 struct snd_soc_card *card; /* parent card */
51
52 /* used during DAPM updates */
53 enum snd_soc_bias_level target_bias_level;
54 struct list_head list;
55
56 struct snd_soc_dapm_widget *wcache_sink;
57 struct snd_soc_dapm_widget *wcache_source;
58
59 #ifdef CONFIG_DEBUG_FS
60 struct dentry *debugfs_dapm;
61 #endif
62 };
63
64 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
65
66 #define DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
67 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
68
69 #define dapm_for_each_direction(dir) \
70 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
71 (dir)++)
72
73 /* dapm power sequences - make this per codec in the future */
74 static int dapm_up_seq[] = {
75 [snd_soc_dapm_pre] = 1,
76 [snd_soc_dapm_regulator_supply] = 2,
77 [snd_soc_dapm_pinctrl] = 2,
78 [snd_soc_dapm_clock_supply] = 2,
79 [snd_soc_dapm_supply] = 3,
80 [snd_soc_dapm_dai_link] = 3,
81 [snd_soc_dapm_micbias] = 4,
82 [snd_soc_dapm_vmid] = 4,
83 [snd_soc_dapm_dai_in] = 5,
84 [snd_soc_dapm_dai_out] = 5,
85 [snd_soc_dapm_aif_in] = 5,
86 [snd_soc_dapm_aif_out] = 5,
87 [snd_soc_dapm_mic] = 6,
88 [snd_soc_dapm_siggen] = 6,
89 [snd_soc_dapm_input] = 6,
90 [snd_soc_dapm_output] = 6,
91 [snd_soc_dapm_mux] = 7,
92 [snd_soc_dapm_demux] = 7,
93 [snd_soc_dapm_dac] = 8,
94 [snd_soc_dapm_switch] = 9,
95 [snd_soc_dapm_mixer] = 9,
96 [snd_soc_dapm_mixer_named_ctl] = 9,
97 [snd_soc_dapm_pga] = 10,
98 [snd_soc_dapm_buffer] = 10,
99 [snd_soc_dapm_scheduler] = 10,
100 [snd_soc_dapm_effect] = 10,
101 [snd_soc_dapm_src] = 10,
102 [snd_soc_dapm_asrc] = 10,
103 [snd_soc_dapm_encoder] = 10,
104 [snd_soc_dapm_decoder] = 10,
105 [snd_soc_dapm_adc] = 11,
106 [snd_soc_dapm_out_drv] = 12,
107 [snd_soc_dapm_hp] = 12,
108 [snd_soc_dapm_line] = 12,
109 [snd_soc_dapm_sink] = 12,
110 [snd_soc_dapm_spk] = 13,
111 [snd_soc_dapm_kcontrol] = 14,
112 [snd_soc_dapm_post] = 15,
113 };
114
115 static int dapm_down_seq[] = {
116 [snd_soc_dapm_pre] = 1,
117 [snd_soc_dapm_kcontrol] = 2,
118 [snd_soc_dapm_adc] = 3,
119 [snd_soc_dapm_spk] = 4,
120 [snd_soc_dapm_hp] = 5,
121 [snd_soc_dapm_line] = 5,
122 [snd_soc_dapm_out_drv] = 5,
123 [snd_soc_dapm_sink] = 6,
124 [snd_soc_dapm_pga] = 6,
125 [snd_soc_dapm_buffer] = 6,
126 [snd_soc_dapm_scheduler] = 6,
127 [snd_soc_dapm_effect] = 6,
128 [snd_soc_dapm_src] = 6,
129 [snd_soc_dapm_asrc] = 6,
130 [snd_soc_dapm_encoder] = 6,
131 [snd_soc_dapm_decoder] = 6,
132 [snd_soc_dapm_switch] = 7,
133 [snd_soc_dapm_mixer_named_ctl] = 7,
134 [snd_soc_dapm_mixer] = 7,
135 [snd_soc_dapm_dac] = 8,
136 [snd_soc_dapm_mic] = 9,
137 [snd_soc_dapm_siggen] = 9,
138 [snd_soc_dapm_input] = 9,
139 [snd_soc_dapm_output] = 9,
140 [snd_soc_dapm_micbias] = 10,
141 [snd_soc_dapm_vmid] = 10,
142 [snd_soc_dapm_mux] = 11,
143 [snd_soc_dapm_demux] = 11,
144 [snd_soc_dapm_aif_in] = 12,
145 [snd_soc_dapm_aif_out] = 12,
146 [snd_soc_dapm_dai_in] = 12,
147 [snd_soc_dapm_dai_out] = 12,
148 [snd_soc_dapm_dai_link] = 13,
149 [snd_soc_dapm_supply] = 14,
150 [snd_soc_dapm_clock_supply] = 15,
151 [snd_soc_dapm_pinctrl] = 15,
152 [snd_soc_dapm_regulator_supply] = 15,
153 [snd_soc_dapm_post] = 16,
154 };
155
dapm_assert_locked(struct snd_soc_dapm_context * dapm)156 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
157 {
158 if (snd_soc_card_is_instantiated(dapm->card))
159 snd_soc_dapm_mutex_assert_held(dapm);
160 }
161
dapm_pop_wait(u32 pop_time)162 static void dapm_pop_wait(u32 pop_time)
163 {
164 if (pop_time)
165 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
166 }
167
168 __printf(3, 4)
dapm_pop_dbg(struct device * dev,u32 pop_time,const char * fmt,...)169 static void dapm_pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
170 {
171 va_list args;
172 char *buf;
173
174 if (!pop_time)
175 return;
176
177 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
178 if (buf == NULL)
179 return;
180
181 va_start(args, fmt);
182 vsnprintf(buf, PAGE_SIZE, fmt, args);
183 dev_info(dev, "%s", buf);
184 va_end(args);
185
186 kfree(buf);
187 }
188
snd_soc_dapm_alloc(struct device * dev)189 struct snd_soc_dapm_context *snd_soc_dapm_alloc(struct device *dev)
190 {
191 return devm_kzalloc(dev, sizeof(struct snd_soc_dapm_context), GFP_KERNEL);
192 }
193
snd_soc_dapm_to_dev(struct snd_soc_dapm_context * dapm)194 struct device *snd_soc_dapm_to_dev(struct snd_soc_dapm_context *dapm)
195 {
196 if (dapm->component)
197 return dapm->component->dev;
198
199 return dapm->card->dev;
200 }
201 EXPORT_SYMBOL_GPL(snd_soc_dapm_to_dev);
202
snd_soc_dapm_to_card(struct snd_soc_dapm_context * dapm)203 struct snd_soc_card *snd_soc_dapm_to_card(struct snd_soc_dapm_context *dapm)
204 {
205 return dapm->card;
206 }
207 EXPORT_SYMBOL_GPL(snd_soc_dapm_to_card);
208
snd_soc_dapm_to_component(struct snd_soc_dapm_context * dapm)209 struct snd_soc_component *snd_soc_dapm_to_component(struct snd_soc_dapm_context *dapm)
210 {
211 return dapm->component;
212 }
213 EXPORT_SYMBOL_GPL(snd_soc_dapm_to_component);
214
dapm_dirty_widget(struct snd_soc_dapm_widget * w)215 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
216 {
217 return !list_empty(&w->dirty);
218 }
219
dapm_mark_dirty(struct snd_soc_dapm_widget * w,const char * reason)220 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
221 {
222 struct device *dev = snd_soc_dapm_to_dev(w->dapm);
223
224 dapm_assert_locked(w->dapm);
225
226 if (!dapm_dirty_widget(w)) {
227 dev_vdbg(dev, "Marking %s dirty due to %s\n",
228 w->name, reason);
229 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
230 }
231 }
232
233 /*
234 * Common implementation for dapm_widget_invalidate_input_paths() and
235 * dapm_widget_invalidate_output_paths(). The function is inlined since the
236 * combined size of the two specialized functions is only marginally larger then
237 * the size of the generic function and at the same time the fast path of the
238 * specialized functions is significantly smaller than the generic function.
239 */
dapm_widget_invalidate_paths(struct snd_soc_dapm_widget * w,enum snd_soc_dapm_direction dir)240 static __always_inline void dapm_widget_invalidate_paths(
241 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
242 {
243 enum snd_soc_dapm_direction rdir = DAPM_DIR_REVERSE(dir);
244 struct snd_soc_dapm_widget *node;
245 struct snd_soc_dapm_path *p;
246 LIST_HEAD(list);
247
248 dapm_assert_locked(w->dapm);
249
250 if (w->endpoints[dir] == -1)
251 return;
252
253 list_add_tail(&w->work_list, &list);
254 w->endpoints[dir] = -1;
255
256 list_for_each_entry(w, &list, work_list) {
257 snd_soc_dapm_widget_for_each_path(w, dir, p) {
258 if (p->is_supply || !p->connect)
259 continue;
260 node = p->node[rdir];
261 if (node->endpoints[dir] != -1) {
262 node->endpoints[dir] = -1;
263 list_add_tail(&node->work_list, &list);
264 }
265 }
266 }
267 }
268
269 /*
270 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
271 * input paths
272 * @w: The widget for which to invalidate the cached number of input paths
273 *
274 * Resets the cached number of inputs for the specified widget and all widgets
275 * that can be reached via outcoming paths from the widget.
276 *
277 * This function must be called if the number of output paths for a widget might
278 * have changed. E.g. if the source state of a widget changes or a path is added
279 * or activated with the widget as the sink.
280 */
dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget * w)281 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
282 {
283 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
284 }
285
286 /*
287 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
288 * output paths
289 * @w: The widget for which to invalidate the cached number of output paths
290 *
291 * Resets the cached number of outputs for the specified widget and all widgets
292 * that can be reached via incoming paths from the widget.
293 *
294 * This function must be called if the number of output paths for a widget might
295 * have changed. E.g. if the sink state of a widget changes or a path is added
296 * or activated with the widget as the source.
297 */
dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget * w)298 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
299 {
300 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
301 }
302
303 /*
304 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
305 * for the widgets connected to a path
306 * @p: The path to invalidate
307 *
308 * Resets the cached number of inputs for the sink of the path and the cached
309 * number of outputs for the source of the path.
310 *
311 * This function must be called when a path is added, removed or the connected
312 * state changes.
313 */
dapm_path_invalidate(struct snd_soc_dapm_path * p)314 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
315 {
316 /*
317 * Weak paths or supply paths do not influence the number of input or
318 * output paths of their neighbors.
319 */
320 if (p->is_supply)
321 return;
322
323 /*
324 * The number of connected endpoints is the sum of the number of
325 * connected endpoints of all neighbors. If a node with 0 connected
326 * endpoints is either connected or disconnected that sum won't change,
327 * so there is no need to re-check the path.
328 */
329 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
330 dapm_widget_invalidate_input_paths(p->sink);
331 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
332 dapm_widget_invalidate_output_paths(p->source);
333 }
334
snd_soc_dapm_mark_endpoints_dirty(struct snd_soc_card * card)335 void snd_soc_dapm_mark_endpoints_dirty(struct snd_soc_card *card)
336 {
337 struct snd_soc_dapm_widget *w;
338
339 snd_soc_dapm_mutex_lock_root(card);
340
341 for_each_card_widgets(card, w) {
342 if (w->is_ep) {
343 dapm_mark_dirty(w, "Rechecking endpoints");
344 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
345 dapm_widget_invalidate_output_paths(w);
346 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
347 dapm_widget_invalidate_input_paths(w);
348 }
349 }
350
351 snd_soc_dapm_mutex_unlock(card);
352 }
353
354 /* create a new dapm widget */
dapm_cnew_widget(const struct snd_soc_dapm_widget * _widget,const char * prefix)355 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
356 const struct snd_soc_dapm_widget *_widget,
357 const char *prefix)
358 {
359 struct snd_soc_dapm_widget *w __free(kfree) = kmemdup(_widget,
360 sizeof(*_widget),
361 GFP_KERNEL);
362 if (!w)
363 return NULL;
364
365 if (prefix)
366 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, _widget->name);
367 else
368 w->name = kstrdup_const(_widget->name, GFP_KERNEL);
369 if (!w->name)
370 return NULL;
371
372 if (_widget->sname) {
373 w->sname = kstrdup_const(_widget->sname, GFP_KERNEL);
374 if (!w->sname) {
375 kfree_const(w->name);
376 return NULL;
377 }
378 }
379
380 return_ptr(w);
381 }
382
383 struct dapm_kcontrol_data {
384 unsigned int value;
385 struct snd_soc_dapm_widget *widget;
386 struct list_head paths;
387 struct snd_soc_dapm_widget_list *wlist;
388 };
389
dapm_read(struct snd_soc_dapm_context * dapm,int reg)390 static unsigned int dapm_read(struct snd_soc_dapm_context *dapm, int reg)
391 {
392 if (!dapm->component)
393 return -EIO;
394 return snd_soc_component_read(dapm->component, reg);
395 }
396
397 /* set up initial codec paths */
dapm_set_mixer_path_status(struct snd_soc_dapm_path * p,int i,int nth_path)398 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
399 int nth_path)
400 {
401 struct soc_mixer_control *mc = (struct soc_mixer_control *)
402 p->sink->kcontrol_news[i].private_value;
403 unsigned int reg = mc->reg;
404 unsigned int invert = mc->invert;
405
406 if (reg != SND_SOC_NOPM) {
407 unsigned int shift = mc->shift;
408 unsigned int max = mc->max;
409 unsigned int mask = (1 << fls(max)) - 1;
410 unsigned int val = dapm_read(p->sink->dapm, reg);
411
412 /*
413 * The nth_path argument allows this function to know
414 * which path of a kcontrol it is setting the initial
415 * status for. Ideally this would support any number
416 * of paths and channels. But since kcontrols only come
417 * in mono and stereo variants, we are limited to 2
418 * channels.
419 *
420 * The following code assumes for stereo controls the
421 * first path is the left channel, and all remaining
422 * paths are the right channel.
423 */
424 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
425 if (reg != mc->rreg)
426 val = dapm_read(p->sink->dapm, mc->rreg);
427 val = (val >> mc->rshift) & mask;
428 } else {
429 val = (val >> shift) & mask;
430 }
431 if (invert)
432 val = max - val;
433 p->connect = !!val;
434 } else {
435 /* since a virtual mixer has no backing registers to
436 * decide which path to connect, it will try to match
437 * with initial state. This is to ensure
438 * that the default mixer choice will be
439 * correctly powered up during initialization.
440 */
441 p->connect = invert;
442 }
443 }
444
445 /* connect mux widget to its interconnecting audio paths */
dapm_connect_mux(struct snd_soc_dapm_context * dapm,struct snd_soc_dapm_path * path,const char * control_name,struct snd_soc_dapm_widget * w)446 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
447 struct snd_soc_dapm_path *path, const char *control_name,
448 struct snd_soc_dapm_widget *w)
449 {
450 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
451 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
452 unsigned int item;
453 int i;
454
455 if (e->reg != SND_SOC_NOPM) {
456 unsigned int val;
457
458 val = dapm_read(dapm, e->reg);
459 val = (val >> e->shift_l) & e->mask;
460 item = snd_soc_enum_val_to_item(e, val);
461 } else {
462 /* since a virtual mux has no backing registers to
463 * decide which path to connect, it will try to match
464 * with the first enumeration. This is to ensure
465 * that the default mux choice (the first) will be
466 * correctly powered up during initialization.
467 */
468 item = 0;
469 }
470
471 i = match_string(e->texts, e->items, control_name);
472 if (i < 0)
473 return -ENODEV;
474
475 path->name = e->texts[i];
476 path->connect = (i == item);
477 return 0;
478
479 }
480
481 /* connect mixer widget to its interconnecting audio paths */
dapm_connect_mixer(struct snd_soc_dapm_context * dapm,struct snd_soc_dapm_path * path,const char * control_name)482 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
483 struct snd_soc_dapm_path *path, const char *control_name)
484 {
485 int i, nth_path = 0;
486
487 /* search for mixer kcontrol */
488 for (i = 0; i < path->sink->num_kcontrols; i++) {
489 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
490 path->name = path->sink->kcontrol_news[i].name;
491 dapm_set_mixer_path_status(path, i, nth_path++);
492 return 0;
493 }
494 }
495 return -ENODEV;
496 }
497
498 /*
499 * dapm_update_widget_flags() - Re-compute widget sink and source flags
500 * @w: The widget for which to update the flags
501 *
502 * Some widgets have a dynamic category which depends on which neighbors they
503 * are connected to. This function update the category for these widgets.
504 *
505 * This function must be called whenever a path is added or removed to a widget.
506 */
dapm_update_widget_flags(struct snd_soc_dapm_widget * w)507 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
508 {
509 enum snd_soc_dapm_direction dir;
510 struct snd_soc_dapm_path *p;
511 unsigned int ep;
512
513 switch (w->id) {
514 case snd_soc_dapm_input:
515 /* On a fully routed card an input is never a source */
516 if (w->dapm->card->fully_routed)
517 return;
518 ep = SND_SOC_DAPM_EP_SOURCE;
519 snd_soc_dapm_widget_for_each_source_path(w, p) {
520 if (p->source->id == snd_soc_dapm_micbias ||
521 p->source->id == snd_soc_dapm_mic ||
522 p->source->id == snd_soc_dapm_line ||
523 p->source->id == snd_soc_dapm_output) {
524 ep = 0;
525 break;
526 }
527 }
528 break;
529 case snd_soc_dapm_output:
530 /* On a fully routed card a output is never a sink */
531 if (w->dapm->card->fully_routed)
532 return;
533 ep = SND_SOC_DAPM_EP_SINK;
534 snd_soc_dapm_widget_for_each_sink_path(w, p) {
535 if (p->sink->id == snd_soc_dapm_spk ||
536 p->sink->id == snd_soc_dapm_hp ||
537 p->sink->id == snd_soc_dapm_line ||
538 p->sink->id == snd_soc_dapm_input) {
539 ep = 0;
540 break;
541 }
542 }
543 break;
544 case snd_soc_dapm_line:
545 ep = 0;
546 dapm_for_each_direction(dir) {
547 if (!list_empty(&w->edges[dir]))
548 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
549 }
550 break;
551 default:
552 return;
553 }
554
555 w->is_ep = ep;
556 }
557
dapm_check_dynamic_path(struct snd_soc_dapm_context * dapm,struct snd_soc_dapm_widget * source,struct snd_soc_dapm_widget * sink,const char * control)558 static int dapm_check_dynamic_path(
559 struct snd_soc_dapm_context *dapm,
560 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
561 const char *control)
562 {
563 struct device *dev = snd_soc_dapm_to_dev(dapm);
564 bool dynamic_source = false;
565 bool dynamic_sink = false;
566
567 if (!control)
568 return 0;
569
570 switch (source->id) {
571 case snd_soc_dapm_demux:
572 dynamic_source = true;
573 break;
574 default:
575 break;
576 }
577
578 switch (sink->id) {
579 case snd_soc_dapm_mux:
580 case snd_soc_dapm_switch:
581 case snd_soc_dapm_mixer:
582 case snd_soc_dapm_mixer_named_ctl:
583 dynamic_sink = true;
584 break;
585 default:
586 break;
587 }
588
589 if (dynamic_source && dynamic_sink) {
590 dev_err(dev,
591 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
592 source->name, control, sink->name);
593 return -EINVAL;
594 } else if (!dynamic_source && !dynamic_sink) {
595 dev_err(dev,
596 "Control not supported for path %s -> [%s] -> %s\n",
597 source->name, control, sink->name);
598 return -EINVAL;
599 }
600
601 return 0;
602 }
603
dapm_add_path(struct snd_soc_dapm_context * dapm,struct snd_soc_dapm_widget * wsource,struct snd_soc_dapm_widget * wsink,const char * control,int (* connected)(struct snd_soc_dapm_widget * source,struct snd_soc_dapm_widget * sink))604 static int dapm_add_path(
605 struct snd_soc_dapm_context *dapm,
606 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
607 const char *control,
608 int (*connected)(struct snd_soc_dapm_widget *source,
609 struct snd_soc_dapm_widget *sink))
610 {
611 struct device *dev = snd_soc_dapm_to_dev(dapm);
612 enum snd_soc_dapm_direction dir;
613 struct snd_soc_dapm_path *path;
614 int ret;
615
616 if (wsink->is_supply && !wsource->is_supply) {
617 dev_err(dev,
618 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
619 wsource->name, wsink->name);
620 return -EINVAL;
621 }
622
623 if (connected && !wsource->is_supply) {
624 dev_err(dev,
625 "connected() callback only supported for supply widgets (%s -> %s)\n",
626 wsource->name, wsink->name);
627 return -EINVAL;
628 }
629
630 if (wsource->is_supply && control) {
631 dev_err(dev,
632 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
633 wsource->name, control, wsink->name);
634 return -EINVAL;
635 }
636
637 ret = dapm_check_dynamic_path(dapm, wsource, wsink, control);
638 if (ret)
639 return ret;
640
641 path = kzalloc_obj(struct snd_soc_dapm_path);
642 if (!path)
643 return -ENOMEM;
644
645 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
646 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
647
648 path->connected = connected;
649 INIT_LIST_HEAD(&path->list);
650 INIT_LIST_HEAD(&path->list_kcontrol);
651
652 if (wsource->is_supply || wsink->is_supply)
653 path->is_supply = 1;
654
655 /* connect static paths */
656 if (control == NULL) {
657 path->connect = 1;
658 } else {
659 switch (wsource->id) {
660 case snd_soc_dapm_demux:
661 ret = dapm_connect_mux(dapm, path, control, wsource);
662 if (ret)
663 goto err;
664 break;
665 default:
666 break;
667 }
668
669 switch (wsink->id) {
670 case snd_soc_dapm_mux:
671 ret = dapm_connect_mux(dapm, path, control, wsink);
672 if (ret != 0)
673 goto err;
674 break;
675 case snd_soc_dapm_switch:
676 case snd_soc_dapm_mixer:
677 case snd_soc_dapm_mixer_named_ctl:
678 ret = dapm_connect_mixer(dapm, path, control);
679 if (ret != 0)
680 goto err;
681 break;
682 default:
683 break;
684 }
685 }
686
687 list_add(&path->list, &dapm->card->paths);
688
689 dapm_for_each_direction(dir)
690 list_add(&path->list_node[dir], &path->node[dir]->edges[dir]);
691
692 dapm_for_each_direction(dir) {
693 dapm_update_widget_flags(path->node[dir]);
694 dapm_mark_dirty(path->node[dir], "Route added");
695 }
696
697 if (snd_soc_card_is_instantiated(dapm->card) && path->connect)
698 dapm_path_invalidate(path);
699
700 return 0;
701 err:
702 kfree(path);
703 return ret;
704 }
705
dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget * widget,struct snd_kcontrol * kcontrol,const char * ctrl_name)706 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
707 struct snd_kcontrol *kcontrol, const char *ctrl_name)
708 {
709 struct device *dev = snd_soc_dapm_to_dev(widget->dapm);
710 struct dapm_kcontrol_data *data;
711 struct soc_mixer_control *mc;
712 struct soc_enum *e;
713 const char *name;
714 int ret;
715
716 data = kzalloc_obj(*data);
717 if (!data)
718 return -ENOMEM;
719
720 INIT_LIST_HEAD(&data->paths);
721
722 switch (widget->id) {
723 case snd_soc_dapm_switch:
724 case snd_soc_dapm_mixer:
725 case snd_soc_dapm_mixer_named_ctl:
726 mc = (struct soc_mixer_control *)kcontrol->private_value;
727
728 if (mc->autodisable) {
729 struct snd_soc_dapm_widget template;
730
731 if (snd_soc_volsw_is_stereo(mc))
732 dev_warn(dev,
733 "ASoC: Unsupported stereo autodisable control '%s'\n",
734 ctrl_name);
735
736 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
737 "Autodisable");
738 if (!name) {
739 ret = -ENOMEM;
740 goto err_data;
741 }
742
743 memset(&template, 0, sizeof(template));
744 template.reg = mc->reg;
745 template.mask = (1 << fls(mc->max)) - 1;
746 template.shift = mc->shift;
747 if (mc->invert)
748 template.off_val = mc->max;
749 else
750 template.off_val = 0;
751 template.on_val = template.off_val;
752 template.id = snd_soc_dapm_kcontrol;
753 template.name = name;
754
755 data->value = template.on_val;
756
757 data->widget =
758 snd_soc_dapm_new_control_unlocked(widget->dapm,
759 &template);
760 kfree(name);
761 if (IS_ERR(data->widget)) {
762 ret = PTR_ERR(data->widget);
763 goto err_data;
764 }
765 }
766 break;
767 case snd_soc_dapm_demux:
768 case snd_soc_dapm_mux:
769 e = (struct soc_enum *)kcontrol->private_value;
770
771 if (e->autodisable) {
772 struct snd_soc_dapm_widget template;
773
774 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
775 "Autodisable");
776 if (!name) {
777 ret = -ENOMEM;
778 goto err_data;
779 }
780
781 memset(&template, 0, sizeof(template));
782 template.reg = e->reg;
783 template.mask = e->mask;
784 template.shift = e->shift_l;
785 template.off_val = snd_soc_enum_item_to_val(e, 0);
786 template.on_val = template.off_val;
787 template.id = snd_soc_dapm_kcontrol;
788 template.name = name;
789
790 data->value = template.on_val;
791
792 data->widget = snd_soc_dapm_new_control_unlocked(
793 widget->dapm, &template);
794 kfree(name);
795 if (IS_ERR(data->widget)) {
796 ret = PTR_ERR(data->widget);
797 goto err_data;
798 }
799
800 dapm_add_path(widget->dapm, data->widget,
801 widget, NULL, NULL);
802 } else if (e->reg != SND_SOC_NOPM) {
803 data->value = dapm_read(widget->dapm, e->reg) &
804 (e->mask << e->shift_l);
805 }
806 break;
807 default:
808 break;
809 }
810
811 kcontrol->private_data = data;
812
813 return 0;
814
815 err_data:
816 kfree(data);
817 return ret;
818 }
819
dapm_kcontrol_free(struct snd_kcontrol * kctl)820 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
821 {
822 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
823
824 list_del(&data->paths);
825 kfree(data->wlist);
826 kfree(data);
827 }
828
dapm_kcontrol_get_wlist(const struct snd_kcontrol * kcontrol)829 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
830 const struct snd_kcontrol *kcontrol)
831 {
832 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
833
834 return data->wlist;
835 }
836
dapm_kcontrol_add_widget(struct snd_kcontrol * kcontrol,struct snd_soc_dapm_widget * widget)837 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
838 struct snd_soc_dapm_widget *widget)
839 {
840 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
841 struct snd_soc_dapm_widget_list *new_wlist;
842 unsigned int n;
843
844 if (data->wlist)
845 n = data->wlist->num_widgets + 1;
846 else
847 n = 1;
848
849 new_wlist = krealloc(data->wlist,
850 struct_size(new_wlist, widgets, n),
851 GFP_KERNEL);
852 if (!new_wlist)
853 return -ENOMEM;
854
855 new_wlist->num_widgets = n;
856 new_wlist->widgets[n - 1] = widget;
857
858 data->wlist = new_wlist;
859
860 return 0;
861 }
862
dapm_kcontrol_add_path(const struct snd_kcontrol * kcontrol,struct snd_soc_dapm_path * path)863 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
864 struct snd_soc_dapm_path *path)
865 {
866 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
867
868 list_add_tail(&path->list_kcontrol, &data->paths);
869 }
870
dapm_kcontrol_is_powered(const struct snd_kcontrol * kcontrol)871 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
872 {
873 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
874
875 if (!data->widget)
876 return true;
877
878 return data->widget->power;
879 }
880
dapm_kcontrol_get_path_list(const struct snd_kcontrol * kcontrol)881 static struct list_head *dapm_kcontrol_get_path_list(
882 const struct snd_kcontrol *kcontrol)
883 {
884 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
885
886 return &data->paths;
887 }
888
889 #define dapm_kcontrol_for_each_path(path, kcontrol) \
890 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
891 list_kcontrol)
892
snd_soc_dapm_kcontrol_get_value(const struct snd_kcontrol * kcontrol)893 unsigned int snd_soc_dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
894 {
895 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
896
897 return data->value;
898 }
899 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_get_value);
900
dapm_kcontrol_set_value(const struct snd_kcontrol * kcontrol,unsigned int value)901 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
902 unsigned int value)
903 {
904 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
905
906 if (data->value == value)
907 return false;
908
909 if (data->widget) {
910 switch (dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->id) {
911 case snd_soc_dapm_switch:
912 case snd_soc_dapm_mixer:
913 case snd_soc_dapm_mixer_named_ctl:
914 data->widget->on_val = value & data->widget->mask;
915 break;
916 case snd_soc_dapm_demux:
917 case snd_soc_dapm_mux:
918 data->widget->on_val = value >> data->widget->shift;
919 break;
920 default:
921 data->widget->on_val = value;
922 break;
923 }
924 }
925
926 data->value = value;
927
928 return true;
929 }
930
931 /**
932 * snd_soc_dapm_kcontrol_to_widget() - Returns the widget associated to a
933 * kcontrol
934 * @kcontrol: The kcontrol
935 */
snd_soc_dapm_kcontrol_to_widget(struct snd_kcontrol * kcontrol)936 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_to_widget(struct snd_kcontrol *kcontrol)
937 {
938 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
939 }
940 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_to_widget);
941
942 /**
943 * snd_soc_dapm_kcontrol_to_dapm() - Returns the dapm context associated to a kcontrol
944 * @kcontrol: The kcontrol
945 *
946 * Note: This function must only be used on kcontrols that are known to have
947 * been registered for a CODEC. Otherwise the behaviour is undefined.
948 */
snd_soc_dapm_kcontrol_to_dapm(struct snd_kcontrol * kcontrol)949 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_to_dapm(struct snd_kcontrol *kcontrol)
950 {
951 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
952 }
953 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_to_dapm);
954
955 /**
956 * snd_soc_dapm_kcontrol_to_component() - Returns the component associated to a
957 * kcontrol
958 * @kcontrol: The kcontrol
959 *
960 * This function must only be used on DAPM contexts that are known to be part of
961 * a COMPONENT (e.g. in a COMPONENT driver). Otherwise the behavior is undefined
962 */
snd_soc_dapm_kcontrol_to_component(struct snd_kcontrol * kcontrol)963 struct snd_soc_component *snd_soc_dapm_kcontrol_to_component(struct snd_kcontrol *kcontrol)
964 {
965 return snd_soc_dapm_to_component(snd_soc_dapm_kcontrol_to_dapm(kcontrol));
966 }
967 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_to_component);
968
dapm_reset(struct snd_soc_card * card)969 static void dapm_reset(struct snd_soc_card *card)
970 {
971 struct snd_soc_dapm_widget *w;
972
973 snd_soc_dapm_mutex_assert_held(card);
974
975 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
976
977 for_each_card_widgets(card, w) {
978 w->new_power = w->power;
979 w->power_checked = false;
980 }
981 }
982
dapm_prefix(struct snd_soc_dapm_context * dapm)983 static const char *dapm_prefix(struct snd_soc_dapm_context *dapm)
984 {
985 if (!dapm->component)
986 return NULL;
987 return dapm->component->name_prefix;
988 }
989
dapm_update_bits(struct snd_soc_dapm_context * dapm,int reg,unsigned int mask,unsigned int value)990 static int dapm_update_bits(struct snd_soc_dapm_context *dapm,
991 int reg, unsigned int mask, unsigned int value)
992 {
993 if (!dapm->component)
994 return -EIO;
995 return snd_soc_component_update_bits(dapm->component, reg,
996 mask, value);
997 }
998
dapm_test_bits(struct snd_soc_dapm_context * dapm,int reg,unsigned int mask,unsigned int value)999 static int dapm_test_bits(struct snd_soc_dapm_context *dapm,
1000 int reg, unsigned int mask, unsigned int value)
1001 {
1002 if (!dapm->component)
1003 return -EIO;
1004 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
1005 }
1006
dapm_async_complete(struct snd_soc_dapm_context * dapm)1007 static void dapm_async_complete(struct snd_soc_dapm_context *dapm)
1008 {
1009 if (dapm->component)
1010 snd_soc_component_async_complete(dapm->component);
1011 }
1012
1013 static struct snd_soc_dapm_widget *
dapm_wcache_lookup(struct snd_soc_dapm_widget * w,const char * name)1014 dapm_wcache_lookup(struct snd_soc_dapm_widget *w, const char *name)
1015 {
1016 if (w) {
1017 struct list_head *wlist = &w->dapm->card->widgets;
1018 const int depth = 2;
1019 int i = 0;
1020
1021 list_for_each_entry_from(w, wlist, list) {
1022 if (!strcmp(name, w->name))
1023 return w;
1024
1025 if (++i == depth)
1026 break;
1027 }
1028 }
1029
1030 return NULL;
1031 }
1032
1033 /**
1034 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
1035 * @dapm: The DAPM context for which to set the level
1036 * @level: The level to set
1037 *
1038 * Forces the DAPM bias level to a specific state. It will call the bias level
1039 * callback of DAPM context with the specified level. This will even happen if
1040 * the context is already at the same level. Furthermore it will not go through
1041 * the normal bias level sequencing, meaning any intermediate states between the
1042 * current and the target state will not be entered.
1043 *
1044 * Note that the change in bias level is only temporary and the next time
1045 * snd_soc_dapm_sync() is called the state will be set to the level as
1046 * determined by the DAPM core. The function is mainly intended to be used to
1047 * used during probe or resume from suspend to power up the device so
1048 * initialization can be done, before the DAPM core takes over.
1049 */
snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context * dapm,enum snd_soc_bias_level level)1050 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
1051 enum snd_soc_bias_level level)
1052 {
1053 int ret = 0;
1054
1055 if (dapm->component)
1056 ret = snd_soc_component_set_bias_level(dapm->component, level);
1057
1058 if (ret == 0)
1059 dapm->bias_level = level;
1060
1061 return ret;
1062 }
1063 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
1064
1065 /**
1066 * snd_soc_dapm_init_bias_level() - Initialize DAPM bias level
1067 * @dapm: The DAPM context to initialize
1068 * @level: The DAPM level to initialize to
1069 *
1070 * This function only sets the driver internal state of the DAPM level and will
1071 * not modify the state of the device. Hence it should not be used during normal
1072 * operation, but only to synchronize the internal state to the device state.
1073 * E.g. during driver probe to set the DAPM level to the one corresponding with
1074 * the power-on reset state of the device.
1075 *
1076 * To change the DAPM state of the device use snd_soc_dapm_set_bias_level().
1077 */
snd_soc_dapm_init_bias_level(struct snd_soc_dapm_context * dapm,enum snd_soc_bias_level level)1078 void snd_soc_dapm_init_bias_level(struct snd_soc_dapm_context *dapm, enum snd_soc_bias_level level)
1079 {
1080 dapm->bias_level = level;
1081 }
1082 EXPORT_SYMBOL_GPL(snd_soc_dapm_init_bias_level);
1083
1084 /**
1085 * snd_soc_dapm_set_bias_level - set the bias level for the system
1086 * @dapm: DAPM context
1087 * @level: level to configure
1088 *
1089 * Configure the bias (power) levels for the SoC audio device.
1090 *
1091 * Returns 0 for success else error.
1092 */
snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context * dapm,enum snd_soc_bias_level level)1093 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
1094 enum snd_soc_bias_level level)
1095 {
1096 struct snd_soc_card *card = dapm->card;
1097 int ret = 0;
1098
1099 trace_snd_soc_bias_level_start(dapm, level);
1100
1101 ret = snd_soc_card_set_bias_level(card, dapm, level);
1102 if (ret != 0)
1103 goto out;
1104
1105 if (dapm != card->dapm)
1106 ret = snd_soc_dapm_force_bias_level(dapm, level);
1107
1108 if (ret != 0)
1109 goto out;
1110
1111 ret = snd_soc_card_set_bias_level_post(card, dapm, level);
1112 out:
1113 trace_snd_soc_bias_level_done(dapm, level);
1114
1115 /* success */
1116 if (ret == 0)
1117 snd_soc_dapm_init_bias_level(dapm, level);
1118
1119 return ret;
1120 }
1121
1122 /**
1123 * snd_soc_dapm_get_bias_level() - Get current DAPM bias level
1124 * @dapm: The context for which to get the bias level
1125 *
1126 * Returns: The current bias level of the passed DAPM context.
1127 */
snd_soc_dapm_get_bias_level(struct snd_soc_dapm_context * dapm)1128 enum snd_soc_bias_level snd_soc_dapm_get_bias_level(struct snd_soc_dapm_context *dapm)
1129 {
1130 return dapm->bias_level;
1131 }
1132 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_bias_level);
1133
dapm_is_shared_kcontrol(struct snd_soc_dapm_context * dapm,struct snd_soc_dapm_widget * kcontrolw,const struct snd_kcontrol_new * kcontrol_new,struct snd_kcontrol ** kcontrol)1134 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
1135 struct snd_soc_dapm_widget *kcontrolw,
1136 const struct snd_kcontrol_new *kcontrol_new,
1137 struct snd_kcontrol **kcontrol)
1138 {
1139 struct snd_soc_dapm_widget *w;
1140 int i;
1141
1142 *kcontrol = NULL;
1143
1144 for_each_card_widgets(dapm->card, w) {
1145 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
1146 continue;
1147 for (i = 0; i < w->num_kcontrols; i++) {
1148 if (&w->kcontrol_news[i] == kcontrol_new) {
1149 if (w->kcontrols)
1150 *kcontrol = w->kcontrols[i];
1151 return 1;
1152 }
1153 }
1154 }
1155
1156 return 0;
1157 }
1158
1159 /*
1160 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
1161 * create it. Either way, add the widget into the control's widget list
1162 */
dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget * w,int kci)1163 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
1164 int kci)
1165 {
1166 struct snd_soc_dapm_context *dapm = w->dapm;
1167 struct device *dev = snd_soc_dapm_to_dev(dapm);
1168 struct snd_card *card = dapm->card->snd_card;
1169 const char *prefix;
1170 size_t prefix_len;
1171 int shared;
1172 struct snd_kcontrol *kcontrol;
1173 bool wname_in_long_name, kcname_in_long_name;
1174 char *long_name = NULL;
1175 const char *name;
1176 int ret = 0;
1177
1178 prefix = dapm_prefix(dapm);
1179 if (prefix)
1180 prefix_len = strlen(prefix) + 1;
1181 else
1182 prefix_len = 0;
1183
1184 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
1185 &kcontrol);
1186
1187 if (!kcontrol) {
1188 if (shared) {
1189 wname_in_long_name = false;
1190 kcname_in_long_name = true;
1191 } else {
1192 switch (w->id) {
1193 case snd_soc_dapm_switch:
1194 case snd_soc_dapm_mixer:
1195 case snd_soc_dapm_pga:
1196 case snd_soc_dapm_effect:
1197 case snd_soc_dapm_out_drv:
1198 wname_in_long_name = true;
1199 kcname_in_long_name = true;
1200 break;
1201 case snd_soc_dapm_mixer_named_ctl:
1202 wname_in_long_name = false;
1203 kcname_in_long_name = true;
1204 break;
1205 case snd_soc_dapm_demux:
1206 case snd_soc_dapm_mux:
1207 wname_in_long_name = true;
1208 kcname_in_long_name = false;
1209 break;
1210 default:
1211 return -EINVAL;
1212 }
1213 }
1214 if (w->no_wname_in_kcontrol_name)
1215 wname_in_long_name = false;
1216
1217 if (wname_in_long_name && kcname_in_long_name) {
1218 /*
1219 * The control will get a prefix from the control
1220 * creation process but we're also using the same
1221 * prefix for widgets so cut the prefix off the
1222 * front of the widget name.
1223 */
1224 long_name = kasprintf(GFP_KERNEL, "%s %s",
1225 w->name + prefix_len,
1226 w->kcontrol_news[kci].name);
1227 if (long_name == NULL)
1228 return -ENOMEM;
1229
1230 name = long_name;
1231 } else if (wname_in_long_name) {
1232 long_name = NULL;
1233 name = w->name + prefix_len;
1234 } else {
1235 long_name = NULL;
1236 name = w->kcontrol_news[kci].name;
1237 }
1238
1239 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
1240 prefix);
1241 if (!kcontrol) {
1242 ret = -ENOMEM;
1243 goto exit_free;
1244 }
1245
1246 kcontrol->private_free = dapm_kcontrol_free;
1247
1248 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
1249 if (ret) {
1250 snd_ctl_free_one(kcontrol);
1251 goto exit_free;
1252 }
1253
1254 ret = snd_ctl_add(card, kcontrol);
1255 if (ret < 0) {
1256 dev_err(dev,
1257 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1258 w->name, name, ret);
1259 goto exit_free;
1260 }
1261 }
1262
1263 ret = dapm_kcontrol_add_widget(kcontrol, w);
1264 if (ret == 0)
1265 w->kcontrols[kci] = kcontrol;
1266
1267 exit_free:
1268 kfree(long_name);
1269
1270 return ret;
1271 }
1272
1273 /* create new dapm mixer control */
dapm_new_mixer(struct snd_soc_dapm_widget * w)1274 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
1275 {
1276 int i, ret;
1277 struct snd_soc_dapm_path *path;
1278 struct dapm_kcontrol_data *data;
1279
1280 /* add kcontrol */
1281 for (i = 0; i < w->num_kcontrols; i++) {
1282 /* match name */
1283 snd_soc_dapm_widget_for_each_source_path(w, path) {
1284 /* mixer/mux paths name must match control name */
1285 if (path->name != (char *)w->kcontrol_news[i].name)
1286 continue;
1287
1288 if (!w->kcontrols[i]) {
1289 ret = dapm_create_or_share_kcontrol(w, i);
1290 if (ret < 0)
1291 return ret;
1292 }
1293
1294 dapm_kcontrol_add_path(w->kcontrols[i], path);
1295
1296 data = snd_kcontrol_chip(w->kcontrols[i]);
1297 if (data->widget)
1298 dapm_add_path(data->widget->dapm,
1299 data->widget,
1300 path->source,
1301 NULL, NULL);
1302 }
1303 }
1304
1305 return 0;
1306 }
1307
1308 /* create new dapm mux control */
dapm_new_mux(struct snd_soc_dapm_widget * w)1309 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
1310 {
1311 struct snd_soc_dapm_context *dapm = w->dapm;
1312 struct device *dev = snd_soc_dapm_to_dev(dapm);
1313 enum snd_soc_dapm_direction dir;
1314 struct snd_soc_dapm_path *path;
1315 const char *type;
1316 int ret;
1317
1318 switch (w->id) {
1319 case snd_soc_dapm_mux:
1320 dir = SND_SOC_DAPM_DIR_OUT;
1321 type = "mux";
1322 break;
1323 case snd_soc_dapm_demux:
1324 dir = SND_SOC_DAPM_DIR_IN;
1325 type = "demux";
1326 break;
1327 default:
1328 return -EINVAL;
1329 }
1330
1331 if (w->num_kcontrols != 1) {
1332 dev_err(dev,
1333 "ASoC: %s %s has incorrect number of controls\n", type,
1334 w->name);
1335 return -EINVAL;
1336 }
1337
1338 if (list_empty(&w->edges[dir])) {
1339 dev_err(dev, "ASoC: %s %s has no paths\n", type, w->name);
1340 return -EINVAL;
1341 }
1342
1343 ret = dapm_create_or_share_kcontrol(w, 0);
1344 if (ret < 0)
1345 return ret;
1346
1347 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1348 if (path->name)
1349 dapm_kcontrol_add_path(w->kcontrols[0], path);
1350 }
1351
1352 return 0;
1353 }
1354
1355 /* create new dapm volume control */
dapm_new_pga(struct snd_soc_dapm_widget * w)1356 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1357 {
1358 int i;
1359
1360 for (i = 0; i < w->num_kcontrols; i++) {
1361 int ret = dapm_create_or_share_kcontrol(w, i);
1362 if (ret < 0)
1363 return ret;
1364 }
1365
1366 return 0;
1367 }
1368
1369 /* create new dapm dai link control */
dapm_new_dai_link(struct snd_soc_dapm_widget * w)1370 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1371 {
1372 int i;
1373 struct snd_soc_pcm_runtime *rtd = w->priv;
1374
1375 /* create control for links with > 1 config */
1376 if (rtd->dai_link->num_c2c_params <= 1)
1377 return 0;
1378
1379 /* add kcontrol */
1380 for (i = 0; i < w->num_kcontrols; i++) {
1381 struct snd_soc_dapm_context *dapm = w->dapm;
1382 struct device *dev = snd_soc_dapm_to_dev(dapm);
1383 struct snd_card *card = dapm->card->snd_card;
1384 struct snd_kcontrol *kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
1385 w, w->name, NULL);
1386 int ret = snd_ctl_add(card, kcontrol);
1387
1388 if (ret < 0) {
1389 dev_err(dev,
1390 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1391 w->name, w->kcontrol_news[i].name, ret);
1392 return ret;
1393 }
1394 kcontrol->private_data = w;
1395 w->kcontrols[i] = kcontrol;
1396 }
1397
1398 return 0;
1399 }
1400
1401 /* We implement power down on suspend by checking the power state of
1402 * the ALSA card - when we are suspending the ALSA state for the card
1403 * is set to D3.
1404 */
dapm_suspend_check(struct snd_soc_dapm_widget * widget)1405 static int dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1406 {
1407 struct device *dev = snd_soc_dapm_to_dev(widget->dapm);
1408 int level = snd_power_get_state(widget->dapm->card->snd_card);
1409
1410 switch (level) {
1411 case SNDRV_CTL_POWER_D3hot:
1412 case SNDRV_CTL_POWER_D3cold:
1413 if (widget->ignore_suspend)
1414 dev_dbg(dev, "ASoC: %s ignoring suspend\n",
1415 widget->name);
1416 return widget->ignore_suspend;
1417 default:
1418 return 1;
1419 }
1420 }
1421
dapm_widget_list_free(struct snd_soc_dapm_widget_list ** list)1422 static void dapm_widget_list_free(struct snd_soc_dapm_widget_list **list)
1423 {
1424 kfree(*list);
1425 }
1426
dapm_widget_list_create(struct snd_soc_dapm_widget_list ** list,struct list_head * widgets)1427 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1428 struct list_head *widgets)
1429 {
1430 struct snd_soc_dapm_widget *w;
1431 struct list_head *it;
1432 unsigned int size = 0;
1433 unsigned int i = 0;
1434
1435 list_for_each(it, widgets)
1436 size++;
1437
1438 *list = kzalloc_flex(**list, widgets, size);
1439 if (*list == NULL)
1440 return -ENOMEM;
1441
1442 (*list)->num_widgets = size;
1443
1444 list_for_each_entry(w, widgets, work_list)
1445 (*list)->widgets[i++] = w;
1446
1447 (*list)->num_widgets = i;
1448
1449 return 0;
1450 }
1451
1452 /*
1453 * Recursively reset the cached number of inputs or outputs for the specified
1454 * widget and all widgets that can be reached via incoming or outcoming paths
1455 * from the widget.
1456 */
dapm_invalidate_paths_ep(struct snd_soc_dapm_widget * widget,enum snd_soc_dapm_direction dir)1457 static void dapm_invalidate_paths_ep(struct snd_soc_dapm_widget *widget,
1458 enum snd_soc_dapm_direction dir)
1459 {
1460 enum snd_soc_dapm_direction rdir = DAPM_DIR_REVERSE(dir);
1461 struct snd_soc_dapm_path *path;
1462
1463 widget->endpoints[dir] = -1;
1464
1465 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1466 if (path->is_supply)
1467 continue;
1468
1469 if (path->walking)
1470 return;
1471
1472 if (path->connect) {
1473 path->walking = 1;
1474 dapm_invalidate_paths_ep(path->node[dir], dir);
1475 path->walking = 0;
1476 }
1477 }
1478 }
1479
1480 /*
1481 * Common implementation for is_connected_output_ep() and
1482 * is_connected_input_ep(). The function is inlined since the combined size of
1483 * the two specialized functions is only marginally larger then the size of the
1484 * generic function and at the same time the fast path of the specialized
1485 * functions is significantly smaller than the generic function.
1486 */
dapm_is_connected_ep(struct snd_soc_dapm_widget * widget,struct list_head * list,enum snd_soc_dapm_direction dir,int (* fn)(struct snd_soc_dapm_widget *,struct list_head *,bool (* custom_stop_condition)(struct snd_soc_dapm_widget *,enum snd_soc_dapm_direction)),bool (* custom_stop_condition)(struct snd_soc_dapm_widget *,enum snd_soc_dapm_direction))1487 static __always_inline int dapm_is_connected_ep(struct snd_soc_dapm_widget *widget,
1488 struct list_head *list, enum snd_soc_dapm_direction dir,
1489 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1490 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1491 enum snd_soc_dapm_direction)),
1492 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1493 enum snd_soc_dapm_direction))
1494 {
1495 enum snd_soc_dapm_direction rdir = DAPM_DIR_REVERSE(dir);
1496 struct snd_soc_dapm_path *path;
1497 int con = 0;
1498
1499 if (widget->endpoints[dir] >= 0)
1500 return widget->endpoints[dir];
1501
1502 DAPM_UPDATE_STAT(widget, path_checks);
1503
1504 /* do we need to add this widget to the list ? */
1505 if (list)
1506 list_add_tail(&widget->work_list, list);
1507
1508 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1509 list = NULL;
1510 custom_stop_condition = NULL;
1511 }
1512
1513 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1514 widget->endpoints[dir] = dapm_suspend_check(widget);
1515 return widget->endpoints[dir];
1516 }
1517
1518 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1519 DAPM_UPDATE_STAT(widget, neighbour_checks);
1520
1521 if (path->is_supply)
1522 continue;
1523
1524 if (path->walking)
1525 return 1;
1526
1527 trace_snd_soc_dapm_path(widget, dir, path);
1528
1529 if (path->connect) {
1530 path->walking = 1;
1531 con += fn(path->node[dir], list, custom_stop_condition);
1532 path->walking = 0;
1533 }
1534 }
1535
1536 widget->endpoints[dir] = con;
1537
1538 return con;
1539 }
1540
1541 /*
1542 * Recursively check for a completed path to an active or physically connected
1543 * output widget. Returns number of complete paths.
1544 *
1545 * Optionally, can be supplied with a function acting as a stopping condition.
1546 * This function takes the dapm widget currently being examined and the walk
1547 * direction as an arguments, it should return true if widgets from that point
1548 * in the graph onwards should not be added to the widget list.
1549 */
dapm_is_connected_output_ep(struct snd_soc_dapm_widget * widget,struct list_head * list,bool (* custom_stop_condition)(struct snd_soc_dapm_widget * i,enum snd_soc_dapm_direction))1550 static int dapm_is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1551 struct list_head *list,
1552 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1553 enum snd_soc_dapm_direction))
1554 {
1555 return dapm_is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1556 dapm_is_connected_output_ep, custom_stop_condition);
1557 }
1558
1559 /*
1560 * Recursively check for a completed path to an active or physically connected
1561 * input widget. Returns number of complete paths.
1562 *
1563 * Optionally, can be supplied with a function acting as a stopping condition.
1564 * This function takes the dapm widget currently being examined and the walk
1565 * direction as an arguments, it should return true if the walk should be
1566 * stopped and false otherwise.
1567 */
dapm_is_connected_input_ep(struct snd_soc_dapm_widget * widget,struct list_head * list,bool (* custom_stop_condition)(struct snd_soc_dapm_widget * i,enum snd_soc_dapm_direction))1568 static int dapm_is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1569 struct list_head *list,
1570 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1571 enum snd_soc_dapm_direction))
1572 {
1573 return dapm_is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1574 dapm_is_connected_input_ep, custom_stop_condition);
1575 }
1576
1577 /**
1578 * snd_soc_dapm_dai_get_connected_widgets - query audio path and it's widgets.
1579 * @dai: the soc DAI.
1580 * @stream: stream direction.
1581 * @list: list of active widgets for this stream.
1582 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1583 * walk based on custom logic.
1584 *
1585 * Queries DAPM graph as to whether a valid audio stream path exists for
1586 * the initial stream specified by name. This takes into account
1587 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1588 *
1589 * Optionally, can be supplied with a function acting as a stopping condition.
1590 * This function takes the dapm widget currently being examined and the walk
1591 * direction as an arguments, it should return true if the walk should be
1592 * stopped and false otherwise.
1593 *
1594 * Returns the number of valid paths or negative error.
1595 */
snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai * dai,int stream,struct snd_soc_dapm_widget_list ** list,bool (* custom_stop_condition)(struct snd_soc_dapm_widget *,enum snd_soc_dapm_direction))1596 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1597 struct snd_soc_dapm_widget_list **list,
1598 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1599 enum snd_soc_dapm_direction))
1600 {
1601 struct snd_soc_card *card = dai->component->card;
1602 struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(dai, stream);
1603 LIST_HEAD(widgets);
1604 int paths;
1605 int ret;
1606
1607 snd_soc_dapm_mutex_lock(card);
1608
1609 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1610 dapm_invalidate_paths_ep(w, SND_SOC_DAPM_DIR_OUT);
1611 paths = dapm_is_connected_output_ep(w, &widgets,
1612 custom_stop_condition);
1613 } else {
1614 dapm_invalidate_paths_ep(w, SND_SOC_DAPM_DIR_IN);
1615 paths = dapm_is_connected_input_ep(w, &widgets,
1616 custom_stop_condition);
1617 }
1618
1619 /* Drop starting point */
1620 list_del(widgets.next);
1621
1622 ret = dapm_widget_list_create(list, &widgets);
1623 if (ret)
1624 paths = ret;
1625
1626 trace_snd_soc_dapm_connected(paths, stream);
1627 snd_soc_dapm_mutex_unlock(card);
1628
1629 return paths;
1630 }
1631 EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_get_connected_widgets);
1632
snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list ** list)1633 void snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list **list)
1634 {
1635 dapm_widget_list_free(list);
1636 }
1637 EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_free_widgets);
1638
1639 /*
1640 * Handler for regulator supply widget.
1641 */
snd_soc_dapm_regulator_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)1642 int snd_soc_dapm_regulator_event(struct snd_soc_dapm_widget *w,
1643 struct snd_kcontrol *kcontrol, int event)
1644 {
1645 struct device *dev = snd_soc_dapm_to_dev(w->dapm);
1646 int ret;
1647
1648 dapm_async_complete(w->dapm);
1649
1650 if (SND_SOC_DAPM_EVENT_ON(event)) {
1651 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1652 ret = regulator_allow_bypass(w->regulator, false);
1653 if (ret != 0)
1654 dev_warn(dev,
1655 "ASoC: Failed to unbypass %s: %d\n",
1656 w->name, ret);
1657 }
1658
1659 return regulator_enable(w->regulator);
1660 } else {
1661 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1662 ret = regulator_allow_bypass(w->regulator, true);
1663 if (ret != 0)
1664 dev_warn(dev,
1665 "ASoC: Failed to bypass %s: %d\n",
1666 w->name, ret);
1667 }
1668
1669 return regulator_disable_deferred(w->regulator, w->shift);
1670 }
1671 }
1672 EXPORT_SYMBOL_GPL(snd_soc_dapm_regulator_event);
1673
1674 /*
1675 * Handler for pinctrl widget.
1676 */
snd_soc_dapm_pinctrl_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)1677 int snd_soc_dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1678 struct snd_kcontrol *kcontrol, int event)
1679 {
1680 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1681 struct pinctrl *p = w->pinctrl;
1682 struct pinctrl_state *s;
1683
1684 if (!p || !priv)
1685 return -EIO;
1686
1687 if (SND_SOC_DAPM_EVENT_ON(event))
1688 s = pinctrl_lookup_state(p, priv->active_state);
1689 else
1690 s = pinctrl_lookup_state(p, priv->sleep_state);
1691
1692 if (IS_ERR(s))
1693 return PTR_ERR(s);
1694
1695 return pinctrl_select_state(p, s);
1696 }
1697 EXPORT_SYMBOL_GPL(snd_soc_dapm_pinctrl_event);
1698
1699 /*
1700 * Handler for clock supply widget.
1701 */
snd_soc_dapm_clock_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)1702 int snd_soc_dapm_clock_event(struct snd_soc_dapm_widget *w,
1703 struct snd_kcontrol *kcontrol, int event)
1704 {
1705 if (!w->clk)
1706 return -EIO;
1707
1708 dapm_async_complete(w->dapm);
1709
1710 if (SND_SOC_DAPM_EVENT_ON(event)) {
1711 return clk_prepare_enable(w->clk);
1712 } else {
1713 clk_disable_unprepare(w->clk);
1714 return 0;
1715 }
1716
1717 return 0;
1718 }
1719 EXPORT_SYMBOL_GPL(snd_soc_dapm_clock_event);
1720
dapm_widget_power_check(struct snd_soc_dapm_widget * w)1721 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1722 {
1723 if (w->power_checked)
1724 return w->new_power;
1725
1726 if (w->force)
1727 w->new_power = 1;
1728 else
1729 w->new_power = w->power_check(w);
1730
1731 w->power_checked = true;
1732
1733 return w->new_power;
1734 }
1735
1736 /* Generic check to see if a widget should be powered. */
dapm_generic_check_power(struct snd_soc_dapm_widget * w)1737 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1738 {
1739 int in, out;
1740
1741 DAPM_UPDATE_STAT(w, power_checks);
1742
1743 in = dapm_is_connected_input_ep(w, NULL, NULL);
1744 out = dapm_is_connected_output_ep(w, NULL, NULL);
1745 return out != 0 && in != 0;
1746 }
1747
1748 /* Check to see if a power supply is needed */
dapm_supply_check_power(struct snd_soc_dapm_widget * w)1749 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1750 {
1751 struct snd_soc_dapm_path *path;
1752
1753 DAPM_UPDATE_STAT(w, power_checks);
1754
1755 /* Check if one of our outputs is connected */
1756 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1757 DAPM_UPDATE_STAT(w, neighbour_checks);
1758
1759 if (path->connected &&
1760 !path->connected(path->source, path->sink))
1761 continue;
1762
1763 if (dapm_widget_power_check(path->sink))
1764 return 1;
1765 }
1766
1767 return 0;
1768 }
1769
dapm_always_on_check_power(struct snd_soc_dapm_widget * w)1770 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1771 {
1772 return w->connected;
1773 }
1774
dapm_seq_compare(struct snd_soc_dapm_widget * a,struct snd_soc_dapm_widget * b,bool power_up)1775 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1776 struct snd_soc_dapm_widget *b,
1777 bool power_up)
1778 {
1779 int *sort;
1780
1781 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1782 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1783
1784 if (power_up)
1785 sort = dapm_up_seq;
1786 else
1787 sort = dapm_down_seq;
1788
1789 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1790 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1791
1792 if (sort[a->id] != sort[b->id])
1793 return sort[a->id] - sort[b->id];
1794 if (a->subseq != b->subseq) {
1795 if (power_up)
1796 return a->subseq - b->subseq;
1797 else
1798 return b->subseq - a->subseq;
1799 }
1800 if (a->reg != b->reg)
1801 return a->reg - b->reg;
1802 if (a->dapm != b->dapm)
1803 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1804
1805 return 0;
1806 }
1807
1808 /* Insert a widget in order into a DAPM power sequence. */
dapm_seq_insert(struct snd_soc_dapm_widget * new_widget,struct list_head * list,bool power_up)1809 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1810 struct list_head *list,
1811 bool power_up)
1812 {
1813 struct snd_soc_dapm_widget *w;
1814
1815 list_for_each_entry(w, list, power_list)
1816 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1817 list_add_tail(&new_widget->power_list, &w->power_list);
1818 return;
1819 }
1820
1821 list_add_tail(&new_widget->power_list, list);
1822 }
1823
dapm_seq_check_event(struct snd_soc_card * card,struct snd_soc_dapm_widget * w,int event)1824 static void dapm_seq_check_event(struct snd_soc_card *card,
1825 struct snd_soc_dapm_widget *w, int event)
1826 {
1827 struct device *dev = card->dev;
1828 const char *ev_name;
1829 int power;
1830
1831 switch (event) {
1832 case SND_SOC_DAPM_PRE_PMU:
1833 ev_name = "PRE_PMU";
1834 power = 1;
1835 break;
1836 case SND_SOC_DAPM_POST_PMU:
1837 ev_name = "POST_PMU";
1838 power = 1;
1839 break;
1840 case SND_SOC_DAPM_PRE_PMD:
1841 ev_name = "PRE_PMD";
1842 power = 0;
1843 break;
1844 case SND_SOC_DAPM_POST_PMD:
1845 ev_name = "POST_PMD";
1846 power = 0;
1847 break;
1848 case SND_SOC_DAPM_WILL_PMU:
1849 ev_name = "WILL_PMU";
1850 power = 1;
1851 break;
1852 case SND_SOC_DAPM_WILL_PMD:
1853 ev_name = "WILL_PMD";
1854 power = 0;
1855 break;
1856 default:
1857 WARN(1, "Unknown event %d\n", event);
1858 return;
1859 }
1860
1861 if (w->new_power != power)
1862 return;
1863
1864 if (w->event && (w->event_flags & event)) {
1865 int ret;
1866
1867 dapm_pop_dbg(dev, card->pop_time, "pop test : %s %s\n",
1868 w->name, ev_name);
1869 dapm_async_complete(w->dapm);
1870 trace_snd_soc_dapm_widget_event_start(w, event);
1871 ret = w->event(w, NULL, event);
1872 trace_snd_soc_dapm_widget_event_done(w, event);
1873 if (ret < 0)
1874 dev_err(dev, "ASoC: %s: %s event failed: %d\n",
1875 ev_name, w->name, ret);
1876 }
1877 }
1878
1879 /* Apply the coalesced changes from a DAPM sequence */
dapm_seq_run_coalesced(struct snd_soc_card * card,struct list_head * pending)1880 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1881 struct list_head *pending)
1882 {
1883 struct device *dev = card->dev;
1884 struct snd_soc_dapm_context *dapm;
1885 struct snd_soc_dapm_widget *w;
1886 int reg;
1887 unsigned int value = 0;
1888 unsigned int mask = 0;
1889
1890 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1891 reg = w->reg;
1892 dapm = w->dapm;
1893
1894 list_for_each_entry(w, pending, power_list) {
1895 WARN_ON(reg != w->reg || dapm != w->dapm);
1896 w->power = w->new_power;
1897
1898 mask |= w->mask << w->shift;
1899 if (w->power)
1900 value |= w->on_val << w->shift;
1901 else
1902 value |= w->off_val << w->shift;
1903
1904 dapm_pop_dbg(dev, card->pop_time,
1905 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1906 w->name, reg, value, mask);
1907
1908 /* Check for events */
1909 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1910 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1911 }
1912
1913 if (reg >= 0) {
1914 /* Any widget will do, they should all be updating the
1915 * same register.
1916 */
1917
1918 dapm_pop_dbg(dev, card->pop_time,
1919 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1920 value, mask, reg, card->pop_time);
1921 dapm_pop_wait(card->pop_time);
1922 dapm_update_bits(dapm, reg, mask, value);
1923 }
1924
1925 list_for_each_entry(w, pending, power_list) {
1926 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1927 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1928 }
1929 }
1930
1931 /* Apply a DAPM power sequence.
1932 *
1933 * We walk over a pre-sorted list of widgets to apply power to. In
1934 * order to minimise the number of writes to the device required
1935 * multiple widgets will be updated in a single write where possible.
1936 * Currently anything that requires more than a single write is not
1937 * handled.
1938 */
dapm_seq_run(struct snd_soc_card * card,struct list_head * list,int event,bool power_up)1939 static void dapm_seq_run(struct snd_soc_card *card,
1940 struct list_head *list, int event, bool power_up)
1941 {
1942 struct device *dev = card->dev;
1943 struct snd_soc_dapm_widget *w, *n;
1944 struct snd_soc_dapm_context *d;
1945 LIST_HEAD(pending);
1946 int cur_sort = -1;
1947 int cur_subseq = -1;
1948 int cur_reg = SND_SOC_NOPM;
1949 struct snd_soc_dapm_context *cur_dapm = NULL;
1950 int i;
1951 int *sort;
1952
1953 if (power_up)
1954 sort = dapm_up_seq;
1955 else
1956 sort = dapm_down_seq;
1957
1958 list_for_each_entry_safe(w, n, list, power_list) {
1959 int ret = 0;
1960
1961 /* Do we need to apply any queued changes? */
1962 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1963 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1964 if (!list_empty(&pending))
1965 dapm_seq_run_coalesced(card, &pending);
1966
1967 if (cur_dapm && cur_dapm->component) {
1968 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1969 if (sort[i] == cur_sort)
1970 snd_soc_component_seq_notifier(
1971 cur_dapm->component,
1972 i, cur_subseq);
1973 }
1974
1975 if (cur_dapm && w->dapm != cur_dapm)
1976 dapm_async_complete(cur_dapm);
1977
1978 INIT_LIST_HEAD(&pending);
1979 cur_sort = -1;
1980 cur_subseq = INT_MIN;
1981 cur_reg = SND_SOC_NOPM;
1982 cur_dapm = NULL;
1983 }
1984
1985 switch (w->id) {
1986 case snd_soc_dapm_pre:
1987 if (!w->event)
1988 continue;
1989
1990 if (event == SND_SOC_DAPM_STREAM_START)
1991 ret = w->event(w,
1992 NULL, SND_SOC_DAPM_PRE_PMU);
1993 else if (event == SND_SOC_DAPM_STREAM_STOP)
1994 ret = w->event(w,
1995 NULL, SND_SOC_DAPM_PRE_PMD);
1996 break;
1997
1998 case snd_soc_dapm_post:
1999 if (!w->event)
2000 continue;
2001
2002 if (event == SND_SOC_DAPM_STREAM_START)
2003 ret = w->event(w,
2004 NULL, SND_SOC_DAPM_POST_PMU);
2005 else if (event == SND_SOC_DAPM_STREAM_STOP)
2006 ret = w->event(w,
2007 NULL, SND_SOC_DAPM_POST_PMD);
2008 break;
2009
2010 default:
2011 /* Queue it up for application */
2012 cur_sort = sort[w->id];
2013 cur_subseq = w->subseq;
2014 cur_reg = w->reg;
2015 cur_dapm = w->dapm;
2016 list_move(&w->power_list, &pending);
2017 break;
2018 }
2019
2020 if (ret < 0)
2021 dev_err(dev,
2022 "ASoC: Failed to apply widget power: %d\n", ret);
2023 }
2024
2025 if (!list_empty(&pending))
2026 dapm_seq_run_coalesced(card, &pending);
2027
2028 if (cur_dapm && cur_dapm->component) {
2029 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
2030 if (sort[i] == cur_sort)
2031 snd_soc_component_seq_notifier(
2032 cur_dapm->component,
2033 i, cur_subseq);
2034 }
2035
2036 for_each_card_dapms(card, d)
2037 dapm_async_complete(d);
2038 }
2039
dapm_widget_update(struct snd_soc_card * card,struct snd_soc_dapm_update * update)2040 static void dapm_widget_update(struct snd_soc_card *card, struct snd_soc_dapm_update *update)
2041 {
2042 struct device *dev = card->dev;
2043 struct snd_soc_dapm_widget_list *wlist;
2044 struct snd_soc_dapm_widget *w = NULL;
2045 unsigned int wi;
2046 int ret;
2047
2048 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
2049 return;
2050
2051 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
2052
2053 for_each_dapm_widgets(wlist, wi, w) {
2054 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
2055 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
2056 if (ret != 0)
2057 dev_err(dev, "ASoC: %s DAPM pre-event failed: %d\n",
2058 w->name, ret);
2059 }
2060 }
2061
2062 if (!w)
2063 return;
2064
2065 ret = dapm_update_bits(w->dapm, update->reg, update->mask,
2066 update->val);
2067 if (ret < 0)
2068 dev_err(dev, "ASoC: %s DAPM update failed: %d\n",
2069 w->name, ret);
2070
2071 if (update->has_second_set) {
2072 ret = dapm_update_bits(w->dapm, update->reg2,
2073 update->mask2, update->val2);
2074 if (ret < 0)
2075 dev_err(dev,
2076 "ASoC: %s DAPM update failed: %d\n",
2077 w->name, ret);
2078 }
2079
2080 for_each_dapm_widgets(wlist, wi, w) {
2081 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
2082 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
2083 if (ret != 0)
2084 dev_err(dev, "ASoC: %s DAPM post-event failed: %d\n",
2085 w->name, ret);
2086 }
2087 }
2088 }
2089
2090 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
2091 * they're changing state.
2092 */
dapm_pre_sequence_async(void * data,async_cookie_t cookie)2093 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
2094 {
2095 struct snd_soc_dapm_context *dapm = data;
2096 struct device *dev = snd_soc_dapm_to_dev(dapm);
2097 int ret;
2098
2099 /* If we're off and we're not supposed to go into STANDBY */
2100 if (dapm->bias_level == SND_SOC_BIAS_OFF &&
2101 dapm->target_bias_level != SND_SOC_BIAS_OFF) {
2102 if (dev && cookie)
2103 pm_runtime_get_sync(dev);
2104
2105 ret = snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
2106 if (ret != 0)
2107 dev_err(dev,
2108 "ASoC: Failed to turn on bias: %d\n", ret);
2109 }
2110
2111 /* Prepare for a transition to ON or away from ON */
2112 if ((dapm->target_bias_level == SND_SOC_BIAS_ON &&
2113 dapm->bias_level != SND_SOC_BIAS_ON) ||
2114 (dapm->target_bias_level != SND_SOC_BIAS_ON &&
2115 dapm->bias_level == SND_SOC_BIAS_ON)) {
2116 ret = snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
2117 if (ret != 0)
2118 dev_err(dev,
2119 "ASoC: Failed to prepare bias: %d\n", ret);
2120 }
2121 }
2122
2123 /* Async callback run prior to DAPM sequences - brings to their final
2124 * state.
2125 */
dapm_post_sequence_async(void * data,async_cookie_t cookie)2126 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
2127 {
2128 struct snd_soc_dapm_context *dapm = data;
2129 struct device *dev = snd_soc_dapm_to_dev(dapm);
2130 int ret;
2131
2132 /* If we just powered the last thing off drop to standby bias */
2133 if (dapm->bias_level == SND_SOC_BIAS_PREPARE &&
2134 (dapm->target_bias_level == SND_SOC_BIAS_STANDBY ||
2135 dapm->target_bias_level == SND_SOC_BIAS_OFF)) {
2136 ret = snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
2137 if (ret != 0)
2138 dev_err(dev, "ASoC: Failed to apply standby bias: %d\n", ret);
2139 }
2140
2141 /* If we're in standby and can support bias off then do that */
2142 if (dapm->bias_level == SND_SOC_BIAS_STANDBY &&
2143 dapm->target_bias_level == SND_SOC_BIAS_OFF) {
2144 ret = snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_OFF);
2145 if (ret != 0)
2146 dev_err(dev, "ASoC: Failed to turn off bias: %d\n", ret);
2147
2148 if (dev && cookie)
2149 pm_runtime_put(dev);
2150 }
2151
2152 /* If we just powered up then move to active bias */
2153 if (dapm->bias_level == SND_SOC_BIAS_PREPARE &&
2154 dapm->target_bias_level == SND_SOC_BIAS_ON) {
2155 ret = snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_ON);
2156 if (ret != 0)
2157 dev_err(dev, "ASoC: Failed to apply active bias: %d\n", ret);
2158 }
2159 }
2160
dapm_widget_set_peer_power(struct snd_soc_dapm_widget * peer,bool power,bool connect)2161 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
2162 bool power, bool connect)
2163 {
2164 /* If a connection is being made or broken then that update
2165 * will have marked the peer dirty, otherwise the widgets are
2166 * not connected and this update has no impact. */
2167 if (!connect)
2168 return;
2169
2170 /* If the peer is already in the state we're moving to then we
2171 * won't have an impact on it. */
2172 if (power != peer->power)
2173 dapm_mark_dirty(peer, "peer state change");
2174 }
2175
dapm_power_one_widget(struct snd_soc_dapm_widget * w,struct list_head * up_list,struct list_head * down_list)2176 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
2177 struct list_head *up_list,
2178 struct list_head *down_list)
2179 {
2180 struct snd_soc_dapm_path *path;
2181 int power;
2182
2183 switch (w->id) {
2184 case snd_soc_dapm_pre:
2185 power = 0;
2186 goto end;
2187 case snd_soc_dapm_post:
2188 power = 1;
2189 goto end;
2190 default:
2191 break;
2192 }
2193
2194 power = dapm_widget_power_check(w);
2195
2196 if (w->power == power)
2197 return;
2198
2199 trace_snd_soc_dapm_widget_power(w, power);
2200
2201 /*
2202 * If we changed our power state perhaps our neigbours
2203 * changed also.
2204 */
2205 snd_soc_dapm_widget_for_each_source_path(w, path)
2206 dapm_widget_set_peer_power(path->source, power, path->connect);
2207
2208 /*
2209 * Supplies can't affect their outputs, only their inputs
2210 */
2211 if (!w->is_supply)
2212 snd_soc_dapm_widget_for_each_sink_path(w, path)
2213 dapm_widget_set_peer_power(path->sink, power, path->connect);
2214
2215 end:
2216 if (power)
2217 dapm_seq_insert(w, up_list, true);
2218 else
2219 dapm_seq_insert(w, down_list, false);
2220 }
2221
snd_soc_dapm_get_idle_bias(struct snd_soc_dapm_context * dapm)2222 bool snd_soc_dapm_get_idle_bias(struct snd_soc_dapm_context *dapm)
2223 {
2224 if (dapm->idle_bias) {
2225 struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
2226 unsigned int state = snd_power_get_state(dapm->card->snd_card);
2227
2228 if ((state == SNDRV_CTL_POWER_D3hot || (state == SNDRV_CTL_POWER_D3cold)) &&
2229 component)
2230 return !component->driver->suspend_bias_off;
2231 }
2232
2233 return dapm->idle_bias;
2234 }
2235 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_idle_bias);
2236
snd_soc_dapm_set_idle_bias(struct snd_soc_dapm_context * dapm,bool on)2237 void snd_soc_dapm_set_idle_bias(struct snd_soc_dapm_context *dapm, bool on)
2238 {
2239 dapm->idle_bias = on;
2240 }
2241 EXPORT_SYMBOL_GPL(snd_soc_dapm_set_idle_bias);
2242
2243 /*
2244 * Scan each dapm widget for complete audio path.
2245 * A complete path is a route that has valid endpoints i.e.:-
2246 *
2247 * o DAC to output pin.
2248 * o Input pin to ADC.
2249 * o Input pin to Output pin (bypass, sidetone)
2250 * o DAC to ADC (loopback).
2251 */
dapm_power_widgets(struct snd_soc_card * card,int event,struct snd_soc_dapm_update * update)2252 static int dapm_power_widgets(struct snd_soc_card *card, int event,
2253 struct snd_soc_dapm_update *update)
2254 {
2255 struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
2256 struct snd_soc_dapm_widget *w;
2257 struct snd_soc_dapm_context *d;
2258 LIST_HEAD(up_list);
2259 LIST_HEAD(down_list);
2260 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
2261 enum snd_soc_bias_level bias;
2262 int ret;
2263
2264 snd_soc_dapm_mutex_assert_held(card);
2265
2266 trace_snd_soc_dapm_start(card, event);
2267
2268 for_each_card_dapms(card, d) {
2269 if (snd_soc_dapm_get_idle_bias(d))
2270 d->target_bias_level = SND_SOC_BIAS_STANDBY;
2271 else
2272 d->target_bias_level = SND_SOC_BIAS_OFF;
2273 }
2274
2275 dapm_reset(card);
2276
2277 /* Check which widgets we need to power and store them in
2278 * lists indicating if they should be powered up or down. We
2279 * only check widgets that have been flagged as dirty but note
2280 * that new widgets may be added to the dirty list while we
2281 * iterate.
2282 */
2283 list_for_each_entry(w, &card->dapm_dirty, dirty) {
2284 dapm_power_one_widget(w, &up_list, &down_list);
2285 }
2286
2287 for_each_card_widgets(card, w) {
2288 switch (w->id) {
2289 case snd_soc_dapm_pre:
2290 case snd_soc_dapm_post:
2291 /* These widgets always need to be powered */
2292 break;
2293 default:
2294 list_del_init(&w->dirty);
2295 break;
2296 }
2297
2298 if (w->new_power) {
2299 d = w->dapm;
2300
2301 /* Supplies and micbiases only bring the
2302 * context up to STANDBY as unless something
2303 * else is active and passing audio they
2304 * generally don't require full power. Signal
2305 * generators are virtual pins and have no
2306 * power impact themselves.
2307 */
2308 switch (w->id) {
2309 case snd_soc_dapm_siggen:
2310 case snd_soc_dapm_vmid:
2311 break;
2312 case snd_soc_dapm_supply:
2313 case snd_soc_dapm_regulator_supply:
2314 case snd_soc_dapm_pinctrl:
2315 case snd_soc_dapm_clock_supply:
2316 case snd_soc_dapm_micbias:
2317 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
2318 d->target_bias_level = SND_SOC_BIAS_STANDBY;
2319 break;
2320 default:
2321 d->target_bias_level = SND_SOC_BIAS_ON;
2322 break;
2323 }
2324 }
2325
2326 }
2327
2328 /* Force all contexts in the card to the same bias state if
2329 * they're not ground referenced.
2330 */
2331 bias = SND_SOC_BIAS_OFF;
2332 for_each_card_dapms(card, d)
2333 if (d->target_bias_level > bias)
2334 bias = d->target_bias_level;
2335 for_each_card_dapms(card, d)
2336 if (snd_soc_dapm_get_idle_bias(d))
2337 d->target_bias_level = bias;
2338
2339 trace_snd_soc_dapm_walk_done(card);
2340
2341 /* Run card bias changes at first */
2342 dapm_pre_sequence_async(dapm, 0);
2343 /* Run other bias changes in parallel */
2344 for_each_card_dapms(card, d) {
2345 if (d != dapm && d->bias_level != d->target_bias_level)
2346 async_schedule_domain(dapm_pre_sequence_async, d,
2347 &async_domain);
2348 }
2349 async_synchronize_full_domain(&async_domain);
2350
2351 list_for_each_entry(w, &down_list, power_list) {
2352 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
2353 }
2354
2355 list_for_each_entry(w, &up_list, power_list) {
2356 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
2357 }
2358
2359 /* Power down widgets first; try to avoid amplifying pops. */
2360 dapm_seq_run(card, &down_list, event, false);
2361
2362 dapm_widget_update(card, update);
2363
2364 /* Now power up. */
2365 dapm_seq_run(card, &up_list, event, true);
2366
2367 /* Run all the bias changes in parallel */
2368 for_each_card_dapms(card, d) {
2369 if (d != dapm && d->bias_level != d->target_bias_level)
2370 async_schedule_domain(dapm_post_sequence_async, d,
2371 &async_domain);
2372 }
2373 async_synchronize_full_domain(&async_domain);
2374 /* Run card bias changes at last */
2375 dapm_post_sequence_async(dapm, 0);
2376
2377 /* do we need to notify any clients that DAPM event is complete */
2378 for_each_card_dapms(card, d) {
2379 if (!d->component)
2380 continue;
2381
2382 ret = snd_soc_component_stream_event(d->component, event);
2383 if (ret < 0)
2384 return ret;
2385 }
2386
2387 dapm_pop_dbg(card->dev, card->pop_time,
2388 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2389 dapm_pop_wait(card->pop_time);
2390
2391 trace_snd_soc_dapm_done(card, event);
2392
2393 return 0;
2394 }
2395
2396 #ifdef CONFIG_DEBUG_FS
2397
2398 static const char * const dapm_type_name[] = {
2399 [snd_soc_dapm_input] = "input",
2400 [snd_soc_dapm_output] = "output",
2401 [snd_soc_dapm_mux] = "mux",
2402 [snd_soc_dapm_demux] = "demux",
2403 [snd_soc_dapm_mixer] = "mixer",
2404 [snd_soc_dapm_mixer_named_ctl] = "mixer_named_ctl",
2405 [snd_soc_dapm_pga] = "pga",
2406 [snd_soc_dapm_out_drv] = "out_drv",
2407 [snd_soc_dapm_adc] = "adc",
2408 [snd_soc_dapm_dac] = "dac",
2409 [snd_soc_dapm_micbias] = "micbias",
2410 [snd_soc_dapm_mic] = "mic",
2411 [snd_soc_dapm_hp] = "hp",
2412 [snd_soc_dapm_spk] = "spk",
2413 [snd_soc_dapm_line] = "line",
2414 [snd_soc_dapm_switch] = "switch",
2415 [snd_soc_dapm_vmid] = "vmid",
2416 [snd_soc_dapm_pre] = "pre",
2417 [snd_soc_dapm_post] = "post",
2418 [snd_soc_dapm_supply] = "supply",
2419 [snd_soc_dapm_pinctrl] = "pinctrl",
2420 [snd_soc_dapm_regulator_supply] = "regulator_supply",
2421 [snd_soc_dapm_clock_supply] = "clock_supply",
2422 [snd_soc_dapm_aif_in] = "aif_in",
2423 [snd_soc_dapm_aif_out] = "aif_out",
2424 [snd_soc_dapm_siggen] = "siggen",
2425 [snd_soc_dapm_sink] = "sink",
2426 [snd_soc_dapm_dai_in] = "dai_in",
2427 [snd_soc_dapm_dai_out] = "dai_out",
2428 [snd_soc_dapm_dai_link] = "dai_link",
2429 [snd_soc_dapm_kcontrol] = "kcontrol",
2430 [snd_soc_dapm_buffer] = "buffer",
2431 [snd_soc_dapm_scheduler] = "scheduler",
2432 [snd_soc_dapm_effect] = "effect",
2433 [snd_soc_dapm_src] = "src",
2434 [snd_soc_dapm_asrc] = "asrc",
2435 [snd_soc_dapm_encoder] = "encoder",
2436 [snd_soc_dapm_decoder] = "decoder",
2437 };
2438
dapm_widget_power_read_file(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)2439 static ssize_t dapm_widget_power_read_file(struct file *file,
2440 char __user *user_buf,
2441 size_t count, loff_t *ppos)
2442 {
2443 struct snd_soc_dapm_widget *w = file->private_data;
2444 enum snd_soc_dapm_direction dir, rdir;
2445 char *buf;
2446 int in, out;
2447 ssize_t ret;
2448 struct snd_soc_dapm_path *p = NULL;
2449 const char *c_name;
2450
2451 BUILD_BUG_ON(ARRAY_SIZE(dapm_type_name) != SND_SOC_DAPM_TYPE_COUNT);
2452
2453 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2454 if (!buf)
2455 return -ENOMEM;
2456
2457 snd_soc_dapm_mutex_lock_root(w->dapm);
2458
2459 /* Supply widgets are not handled by dapm_is_connected_{input,output}_ep() */
2460 if (w->is_supply) {
2461 in = 0;
2462 out = 0;
2463 } else {
2464 in = dapm_is_connected_input_ep(w, NULL, NULL);
2465 out = dapm_is_connected_output_ep(w, NULL, NULL);
2466 }
2467
2468 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2469 w->name, w->power ? "On" : "Off",
2470 w->force ? " (forced)" : "", in, out);
2471
2472 if (w->reg >= 0)
2473 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2474 " - R%d(0x%x) mask 0x%x",
2475 w->reg, w->reg, w->mask << w->shift);
2476
2477 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2478
2479 if (w->sname)
2480 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2481 w->sname,
2482 w->active ? "active" : "inactive");
2483
2484 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " widget-type %s\n",
2485 dapm_type_name[w->id]);
2486
2487 dapm_for_each_direction(dir) {
2488 rdir = DAPM_DIR_REVERSE(dir);
2489 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2490 if (p->connected && !p->connected(p->source, p->sink))
2491 continue;
2492
2493 if (!p->connect)
2494 continue;
2495
2496 c_name = p->node[rdir]->dapm->component ?
2497 p->node[rdir]->dapm->component->name : NULL;
2498 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2499 " %s \"%s\" \"%s\" \"%s\"\n",
2500 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2501 p->name ? p->name : "static",
2502 p->node[rdir]->name, c_name);
2503 }
2504 }
2505
2506 snd_soc_dapm_mutex_unlock(w->dapm);
2507
2508 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2509
2510 kfree(buf);
2511 return ret;
2512 }
2513
2514 static const struct file_operations dapm_widget_power_fops = {
2515 .open = simple_open,
2516 .read = dapm_widget_power_read_file,
2517 .llseek = default_llseek,
2518 };
2519
dapm_bias_read_file(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)2520 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2521 size_t count, loff_t *ppos)
2522 {
2523 struct snd_soc_dapm_context *dapm = file->private_data;
2524 char *level;
2525
2526 switch (dapm->bias_level) {
2527 case SND_SOC_BIAS_ON:
2528 level = "On\n";
2529 break;
2530 case SND_SOC_BIAS_PREPARE:
2531 level = "Prepare\n";
2532 break;
2533 case SND_SOC_BIAS_STANDBY:
2534 level = "Standby\n";
2535 break;
2536 case SND_SOC_BIAS_OFF:
2537 level = "Off\n";
2538 break;
2539 default:
2540 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2541 level = "Unknown\n";
2542 break;
2543 }
2544
2545 return simple_read_from_buffer(user_buf, count, ppos, level,
2546 strlen(level));
2547 }
2548
2549 static const struct file_operations dapm_bias_fops = {
2550 .open = simple_open,
2551 .read = dapm_bias_read_file,
2552 .llseek = default_llseek,
2553 };
2554
snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context * dapm,struct dentry * parent)2555 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2556 struct dentry *parent)
2557 {
2558 if (IS_ERR_OR_NULL(parent))
2559 return;
2560
2561 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2562
2563 debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
2564 &dapm_bias_fops);
2565 }
2566
dapm_debugfs_add_widget(struct snd_soc_dapm_widget * w)2567 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2568 {
2569 struct snd_soc_dapm_context *dapm = w->dapm;
2570
2571 if (!dapm->debugfs_dapm || !w->name)
2572 return;
2573
2574 debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
2575 &dapm_widget_power_fops);
2576 }
2577
dapm_debugfs_free_widget(struct snd_soc_dapm_widget * w)2578 static void dapm_debugfs_free_widget(struct snd_soc_dapm_widget *w)
2579 {
2580 struct snd_soc_dapm_context *dapm = w->dapm;
2581
2582 if (!dapm->debugfs_dapm || !w->name)
2583 return;
2584
2585 debugfs_lookup_and_remove(w->name, dapm->debugfs_dapm);
2586 }
2587
dapm_debugfs_cleanup(struct snd_soc_dapm_context * dapm)2588 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2589 {
2590 debugfs_remove_recursive(dapm->debugfs_dapm);
2591 dapm->debugfs_dapm = NULL;
2592 }
2593
2594 #else
snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context * dapm,struct dentry * parent)2595 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2596 struct dentry *parent)
2597 {
2598 }
2599
dapm_debugfs_add_widget(struct snd_soc_dapm_widget * w)2600 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2601 {
2602 }
2603
dapm_debugfs_free_widget(struct snd_soc_dapm_widget * w)2604 static inline void dapm_debugfs_free_widget(struct snd_soc_dapm_widget *w)
2605 {
2606 }
2607
dapm_debugfs_cleanup(struct snd_soc_dapm_context * dapm)2608 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2609 {
2610 }
2611
2612 #endif
2613
2614 /*
2615 * dapm_connect_path() - Connects or disconnects a path
2616 * @path: The path to update
2617 * @connect: The new connect state of the path. True if the path is connected,
2618 * false if it is disconnected.
2619 * @reason: The reason why the path changed (for debugging only)
2620 */
dapm_connect_path(struct snd_soc_dapm_path * path,bool connect,const char * reason)2621 static void dapm_connect_path(struct snd_soc_dapm_path *path,
2622 bool connect, const char *reason)
2623 {
2624 if (path->connect == connect)
2625 return;
2626
2627 path->connect = connect;
2628 dapm_mark_dirty(path->source, reason);
2629 dapm_mark_dirty(path->sink, reason);
2630 dapm_path_invalidate(path);
2631 }
2632
2633 /* test and update the power status of a mux widget */
dapm_mux_update_power(struct snd_soc_card * card,struct snd_kcontrol * kcontrol,struct snd_soc_dapm_update * update,int mux,struct soc_enum * e)2634 static int dapm_mux_update_power(struct snd_soc_card *card,
2635 struct snd_kcontrol *kcontrol,
2636 struct snd_soc_dapm_update *update,
2637 int mux, struct soc_enum *e)
2638 {
2639 struct snd_soc_dapm_path *path;
2640 int found = 0;
2641 bool connect;
2642
2643 snd_soc_dapm_mutex_assert_held(card);
2644
2645 /* find dapm widget path assoc with kcontrol */
2646 dapm_kcontrol_for_each_path(path, kcontrol) {
2647 found = 1;
2648 /* we now need to match the string in the enum to the path */
2649 if (e && !(strcmp(path->name, e->texts[mux])))
2650 connect = true;
2651 else
2652 connect = false;
2653
2654 dapm_connect_path(path, connect, "mux update");
2655 }
2656
2657 if (found)
2658 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP, update);
2659
2660 return found;
2661 }
2662
snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context * dapm,struct snd_kcontrol * kcontrol,int mux,struct soc_enum * e,struct snd_soc_dapm_update * update)2663 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2664 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2665 struct snd_soc_dapm_update *update)
2666 {
2667 struct snd_soc_card *card = dapm->card;
2668 int ret;
2669
2670 snd_soc_dapm_mutex_lock(card);
2671 ret = dapm_mux_update_power(card, kcontrol, update, mux, e);
2672 snd_soc_dapm_mutex_unlock(card);
2673 if (ret > 0)
2674 snd_soc_dpcm_runtime_update(card);
2675 return ret;
2676 }
2677 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2678
2679 /* test and update the power status of a mixer or switch widget */
dapm_mixer_update_power(struct snd_soc_card * card,struct snd_kcontrol * kcontrol,struct snd_soc_dapm_update * update,int connect,int rconnect)2680 static int dapm_mixer_update_power(struct snd_soc_card *card,
2681 struct snd_kcontrol *kcontrol,
2682 struct snd_soc_dapm_update *update,
2683 int connect, int rconnect)
2684 {
2685 struct snd_soc_dapm_path *path;
2686 int found = 0;
2687
2688 snd_soc_dapm_mutex_assert_held(card);
2689
2690 /* find dapm widget path assoc with kcontrol */
2691 dapm_kcontrol_for_each_path(path, kcontrol) {
2692 /*
2693 * Ideally this function should support any number of
2694 * paths and channels. But since kcontrols only come
2695 * in mono and stereo variants, we are limited to 2
2696 * channels.
2697 *
2698 * The following code assumes for stereo controls the
2699 * first path (when 'found == 0') is the left channel,
2700 * and all remaining paths (when 'found == 1') are the
2701 * right channel.
2702 *
2703 * A stereo control is signified by a valid 'rconnect'
2704 * value, either 0 for unconnected, or >= 0 for connected.
2705 * This is chosen instead of using snd_soc_volsw_is_stereo,
2706 * so that the behavior of snd_soc_dapm_mixer_update_power
2707 * doesn't change even when the kcontrol passed in is
2708 * stereo.
2709 *
2710 * It passes 'connect' as the path connect status for
2711 * the left channel, and 'rconnect' for the right
2712 * channel.
2713 */
2714 if (found && rconnect >= 0)
2715 dapm_connect_path(path, rconnect, "mixer update");
2716 else
2717 dapm_connect_path(path, connect, "mixer update");
2718 found = 1;
2719 }
2720
2721 if (found)
2722 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP, update);
2723
2724 return found;
2725 }
2726
snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context * dapm,struct snd_kcontrol * kcontrol,int connect,struct snd_soc_dapm_update * update)2727 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2728 struct snd_kcontrol *kcontrol, int connect,
2729 struct snd_soc_dapm_update *update)
2730 {
2731 struct snd_soc_card *card = dapm->card;
2732 int ret;
2733
2734 snd_soc_dapm_mutex_lock(card);
2735 ret = dapm_mixer_update_power(card, kcontrol, update, connect, -1);
2736 snd_soc_dapm_mutex_unlock(card);
2737 if (ret > 0)
2738 snd_soc_dpcm_runtime_update(card);
2739 return ret;
2740 }
2741 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2742
dapm_widget_show_component(struct snd_soc_component * component,char * buf,int count)2743 static ssize_t dapm_widget_show_component(struct snd_soc_component *component,
2744 char *buf, int count)
2745 {
2746 struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
2747 struct snd_soc_dapm_widget *w;
2748 char *state = "not set";
2749
2750 /* card won't be set for the dummy component, as a spot fix
2751 * we're checking for that case specifically here but in future
2752 * we will ensure that the dummy component looks like others.
2753 */
2754 if (!component->card)
2755 return 0;
2756
2757 for_each_card_widgets(component->card, w) {
2758 if (w->dapm != dapm)
2759 continue;
2760
2761 /* only display widgets that burn power */
2762 switch (w->id) {
2763 case snd_soc_dapm_hp:
2764 case snd_soc_dapm_mic:
2765 case snd_soc_dapm_spk:
2766 case snd_soc_dapm_line:
2767 case snd_soc_dapm_micbias:
2768 case snd_soc_dapm_dac:
2769 case snd_soc_dapm_adc:
2770 case snd_soc_dapm_pga:
2771 case snd_soc_dapm_effect:
2772 case snd_soc_dapm_out_drv:
2773 case snd_soc_dapm_mixer:
2774 case snd_soc_dapm_mixer_named_ctl:
2775 case snd_soc_dapm_supply:
2776 case snd_soc_dapm_regulator_supply:
2777 case snd_soc_dapm_pinctrl:
2778 case snd_soc_dapm_clock_supply:
2779 if (w->name)
2780 count += sysfs_emit_at(buf, count, "%s: %s\n",
2781 w->name, w->power ? "On":"Off");
2782 break;
2783 default:
2784 break;
2785 }
2786 }
2787
2788 switch (snd_soc_dapm_get_bias_level(dapm)) {
2789 case SND_SOC_BIAS_ON:
2790 state = "On";
2791 break;
2792 case SND_SOC_BIAS_PREPARE:
2793 state = "Prepare";
2794 break;
2795 case SND_SOC_BIAS_STANDBY:
2796 state = "Standby";
2797 break;
2798 case SND_SOC_BIAS_OFF:
2799 state = "Off";
2800 break;
2801 }
2802 count += sysfs_emit_at(buf, count, "PM State: %s\n", state);
2803
2804 return count;
2805 }
2806
2807 /* show dapm widget status in sys fs */
dapm_widget_show(struct device * dev,struct device_attribute * attr,char * buf)2808 static ssize_t dapm_widget_show(struct device *dev,
2809 struct device_attribute *attr, char *buf)
2810 {
2811 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2812 struct snd_soc_dai *codec_dai;
2813 int i, count = 0;
2814
2815 snd_soc_dapm_mutex_lock_root(rtd->card);
2816
2817 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2818 struct snd_soc_component *component = codec_dai->component;
2819
2820 count = dapm_widget_show_component(component, buf, count);
2821 }
2822
2823 snd_soc_dapm_mutex_unlock(rtd->card);
2824
2825 return count;
2826 }
2827
2828 static DEVICE_ATTR_RO(dapm_widget);
2829
2830 struct attribute *snd_soc_dapm_dev_attrs[] = {
2831 &dev_attr_dapm_widget.attr,
2832 NULL
2833 };
2834
dapm_free_path(struct snd_soc_dapm_path * path)2835 static void dapm_free_path(struct snd_soc_dapm_path *path)
2836 {
2837 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2838 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2839 list_del(&path->list_kcontrol);
2840 list_del(&path->list);
2841 kfree(path);
2842 }
2843
2844 /**
2845 * snd_soc_dapm_free_widget - Free specified widget
2846 * @w: widget to free
2847 *
2848 * Removes widget from all paths and frees memory occupied by it.
2849 */
snd_soc_dapm_free_widget(struct snd_soc_dapm_widget * w)2850 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2851 {
2852 struct snd_soc_dapm_path *p, *next_p;
2853 enum snd_soc_dapm_direction dir;
2854
2855 if (!w)
2856 return;
2857
2858 list_del(&w->list);
2859 list_del(&w->dirty);
2860 /*
2861 * remove source and sink paths associated to this widget.
2862 * While removing the path, remove reference to it from both
2863 * source and sink widgets so that path is removed only once.
2864 */
2865 dapm_for_each_direction(dir) {
2866 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2867 dapm_free_path(p);
2868 }
2869
2870 dapm_debugfs_free_widget(w);
2871
2872 kfree(w->kcontrols);
2873 kfree_const(w->name);
2874 kfree_const(w->sname);
2875 kfree(w);
2876 }
2877 EXPORT_SYMBOL_GPL(snd_soc_dapm_free_widget);
2878
2879 /* free all dapm widgets and resources */
dapm_free_widgets(struct snd_soc_dapm_context * dapm)2880 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2881 {
2882 struct snd_soc_dapm_widget *w, *next_w;
2883
2884 for_each_card_widgets_safe(dapm->card, w, next_w) {
2885 if (w->dapm != dapm)
2886 continue;
2887 snd_soc_dapm_free_widget(w);
2888 }
2889
2890 dapm->wcache_sink = NULL;
2891 dapm->wcache_source = NULL;
2892 }
2893
dapm_find_widget(struct snd_soc_dapm_context * dapm,const char * pin,bool search_other_contexts)2894 static struct snd_soc_dapm_widget *dapm_find_widget(
2895 struct snd_soc_dapm_context *dapm, const char *pin,
2896 bool search_other_contexts)
2897 {
2898 struct snd_soc_dapm_widget *w;
2899 struct snd_soc_dapm_widget *fallback = NULL;
2900 char prefixed_pin[80];
2901 const char *pin_name;
2902 const char *prefix = dapm_prefix(dapm);
2903
2904 if (prefix) {
2905 snprintf(prefixed_pin, sizeof(prefixed_pin), "%s %s",
2906 prefix, pin);
2907 pin_name = prefixed_pin;
2908 } else {
2909 pin_name = pin;
2910 }
2911
2912 for_each_card_widgets(dapm->card, w) {
2913 if (!strcmp(w->name, pin_name)) {
2914 if (w->dapm == dapm)
2915 return w;
2916 else
2917 fallback = w;
2918 }
2919 }
2920
2921 if (search_other_contexts)
2922 return fallback;
2923
2924 return NULL;
2925 }
2926
2927 /*
2928 * set the DAPM pin status:
2929 * returns 1 when the value has been updated, 0 when unchanged, or a negative
2930 * error code; called from kcontrol put callback
2931 */
__dapm_set_pin(struct snd_soc_dapm_context * dapm,const char * pin,int status)2932 static int __dapm_set_pin(struct snd_soc_dapm_context *dapm,
2933 const char *pin, int status)
2934 {
2935 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2936 struct device *dev = snd_soc_dapm_to_dev(dapm);
2937 int ret = 0;
2938
2939 dapm_assert_locked(dapm);
2940
2941 if (!w) {
2942 dev_err(dev, "ASoC: DAPM unknown pin %s\n", pin);
2943 return -EINVAL;
2944 }
2945
2946 if (w->connected != status) {
2947 dapm_mark_dirty(w, "pin configuration");
2948 dapm_widget_invalidate_input_paths(w);
2949 dapm_widget_invalidate_output_paths(w);
2950 ret = 1;
2951 }
2952
2953 w->connected = status;
2954 if (status == 0)
2955 w->force = 0;
2956
2957 return ret;
2958 }
2959
2960 /*
2961 * similar as __dapm_set_pin(), but returns 0 when successful;
2962 * called from several API functions below
2963 */
dapm_set_pin(struct snd_soc_dapm_context * dapm,const char * pin,int status)2964 static int dapm_set_pin(struct snd_soc_dapm_context *dapm,
2965 const char *pin, int status)
2966 {
2967 int ret = __dapm_set_pin(dapm, pin, status);
2968
2969 return ret < 0 ? ret : 0;
2970 }
2971
2972 /**
2973 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2974 * @dapm: DAPM context
2975 *
2976 * Walks all dapm audio paths and powers widgets according to their
2977 * stream or path usage.
2978 *
2979 * Requires external locking.
2980 *
2981 * Returns 0 for success.
2982 */
snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context * dapm)2983 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2984 {
2985 /*
2986 * Suppress early reports (eg, jacks syncing their state) to avoid
2987 * silly DAPM runs during card startup.
2988 */
2989 if (!snd_soc_card_is_instantiated(dapm->card))
2990 return 0;
2991
2992 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP, NULL);
2993 }
2994 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2995
2996 /**
2997 * snd_soc_dapm_sync - scan and power dapm paths
2998 * @dapm: DAPM context
2999 *
3000 * Walks all dapm audio paths and powers widgets according to their
3001 * stream or path usage.
3002 *
3003 * Returns 0 for success.
3004 */
snd_soc_dapm_sync(struct snd_soc_dapm_context * dapm)3005 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
3006 {
3007 int ret;
3008
3009 snd_soc_dapm_mutex_lock(dapm);
3010 ret = snd_soc_dapm_sync_unlocked(dapm);
3011 snd_soc_dapm_mutex_unlock(dapm);
3012 return ret;
3013 }
3014 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
3015
dapm_update_dai_chan(struct snd_soc_dapm_path * p,struct snd_soc_dapm_widget * w,int channels)3016 static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
3017 struct snd_soc_dapm_widget *w,
3018 int channels)
3019 {
3020 struct device *dev = snd_soc_dapm_to_dev(w->dapm);
3021
3022 switch (w->id) {
3023 case snd_soc_dapm_aif_out:
3024 case snd_soc_dapm_aif_in:
3025 break;
3026 default:
3027 return 0;
3028 }
3029
3030 dev_dbg(dev, "%s DAI route %s -> %s\n",
3031 w->channel < channels ? "Connecting" : "Disconnecting",
3032 p->source->name, p->sink->name);
3033
3034 if (w->channel < channels)
3035 dapm_connect_path(p, true, "dai update");
3036 else
3037 dapm_connect_path(p, false, "dai update");
3038
3039 return 0;
3040 }
3041
dapm_update_dai_unlocked(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)3042 static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
3043 struct snd_pcm_hw_params *params,
3044 struct snd_soc_dai *dai)
3045 {
3046 int dir = substream->stream;
3047 int channels = params_channels(params);
3048 struct snd_soc_dapm_path *p;
3049 struct snd_soc_dapm_widget *w;
3050 int ret;
3051
3052 w = snd_soc_dai_get_widget(dai, dir);
3053
3054 if (!w)
3055 return 0;
3056
3057 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name, snd_pcm_direction_name(dir));
3058
3059 snd_soc_dapm_widget_for_each_sink_path(w, p) {
3060 ret = dapm_update_dai_chan(p, p->sink, channels);
3061 if (ret < 0)
3062 return ret;
3063 }
3064
3065 snd_soc_dapm_widget_for_each_source_path(w, p) {
3066 ret = dapm_update_dai_chan(p, p->source, channels);
3067 if (ret < 0)
3068 return ret;
3069 }
3070
3071 return 0;
3072 }
3073
snd_soc_dapm_update_dai(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)3074 int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
3075 struct snd_pcm_hw_params *params,
3076 struct snd_soc_dai *dai)
3077 {
3078 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
3079 int ret;
3080
3081 snd_soc_dapm_mutex_lock(rtd->card);
3082 ret = dapm_update_dai_unlocked(substream, params, dai);
3083 snd_soc_dapm_mutex_unlock(rtd->card);
3084
3085 return ret;
3086 }
3087
snd_soc_dapm_widget_name_cmp(struct snd_soc_dapm_widget * widget,const char * s)3088 int snd_soc_dapm_widget_name_cmp(struct snd_soc_dapm_widget *widget, const char *s)
3089 {
3090 struct snd_soc_component *component = widget->dapm->component;
3091 const char *wname = widget->name;
3092
3093 if (component && component->name_prefix)
3094 wname += strlen(component->name_prefix) + 1; /* plus space */
3095
3096 return strcmp(wname, s);
3097 }
3098 EXPORT_SYMBOL_GPL(snd_soc_dapm_widget_name_cmp);
3099
snd_soc_dapm_add_route(struct snd_soc_dapm_context * dapm,const struct snd_soc_dapm_route * route)3100 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
3101 const struct snd_soc_dapm_route *route)
3102 {
3103 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
3104 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
3105 struct device *dev = snd_soc_dapm_to_dev(dapm);
3106 const char *sink;
3107 const char *source;
3108 char prefixed_sink[80];
3109 char prefixed_source[80];
3110 const char *prefix;
3111 unsigned int sink_ref = 0;
3112 unsigned int source_ref = 0;
3113 int ret;
3114
3115 prefix = dapm_prefix(dapm);
3116 if (prefix) {
3117 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
3118 prefix, route->sink);
3119 sink = prefixed_sink;
3120 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
3121 prefix, route->source);
3122 source = prefixed_source;
3123 } else {
3124 sink = route->sink;
3125 source = route->source;
3126 }
3127
3128 wsource = dapm_wcache_lookup(dapm->wcache_source, source);
3129 wsink = dapm_wcache_lookup(dapm->wcache_sink, sink);
3130
3131 if (wsink && wsource)
3132 goto skip_search;
3133
3134 /*
3135 * find src and dest widgets over all widgets but favor a widget from
3136 * current DAPM context
3137 */
3138 for_each_card_widgets(dapm->card, w) {
3139 if (!wsink && !(strcmp(w->name, sink))) {
3140 wtsink = w;
3141 if (w->dapm == dapm) {
3142 wsink = w;
3143 if (wsource)
3144 break;
3145 }
3146 sink_ref++;
3147 if (sink_ref > 1)
3148 dev_warn(dev,
3149 "ASoC: sink widget %s overwritten\n",
3150 w->name);
3151 continue;
3152 }
3153 if (!wsource && !(strcmp(w->name, source))) {
3154 wtsource = w;
3155 if (w->dapm == dapm) {
3156 wsource = w;
3157 if (wsink)
3158 break;
3159 }
3160 source_ref++;
3161 if (source_ref > 1)
3162 dev_warn(dev,
3163 "ASoC: source widget %s overwritten\n",
3164 w->name);
3165 }
3166 }
3167 /* use widget from another DAPM context if not found from this */
3168 if (!wsink)
3169 wsink = wtsink;
3170 if (!wsource)
3171 wsource = wtsource;
3172
3173 ret = -ENODEV;
3174 if (!wsource)
3175 goto err;
3176 if (!wsink)
3177 goto err;
3178
3179 skip_search:
3180 /* update cache */
3181 dapm->wcache_sink = wsink;
3182 dapm->wcache_source = wsource;
3183
3184 ret = dapm_add_path(dapm, wsource, wsink, route->control,
3185 route->connected);
3186 err:
3187 if (ret)
3188 dev_err(dev, "ASoC: Failed to add route %s%s -%s%s%s> %s%s\n",
3189 source, !wsource ? "(*)" : "",
3190 !route->control ? "" : "> [",
3191 !route->control ? "" : route->control,
3192 !route->control ? "" : "] -",
3193 sink, !wsink ? "(*)" : "");
3194 return ret;
3195 }
3196
snd_soc_dapm_del_route(struct snd_soc_dapm_context * dapm,const struct snd_soc_dapm_route * route)3197 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
3198 const struct snd_soc_dapm_route *route)
3199 {
3200 struct device *dev = snd_soc_dapm_to_dev(dapm);
3201 struct snd_soc_dapm_path *path, *p;
3202 const char *sink;
3203 const char *source;
3204 char prefixed_sink[80];
3205 char prefixed_source[80];
3206 const char *prefix;
3207
3208 if (route->control) {
3209 dev_err(dev,
3210 "ASoC: Removal of routes with controls not supported\n");
3211 return -EINVAL;
3212 }
3213
3214 prefix = dapm_prefix(dapm);
3215 if (prefix) {
3216 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
3217 prefix, route->sink);
3218 sink = prefixed_sink;
3219 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
3220 prefix, route->source);
3221 source = prefixed_source;
3222 } else {
3223 sink = route->sink;
3224 source = route->source;
3225 }
3226
3227 path = NULL;
3228 list_for_each_entry(p, &dapm->card->paths, list) {
3229 if (strcmp(p->source->name, source) != 0)
3230 continue;
3231 if (strcmp(p->sink->name, sink) != 0)
3232 continue;
3233 path = p;
3234 break;
3235 }
3236
3237 if (path) {
3238 struct snd_soc_dapm_widget *wsource = path->source;
3239 struct snd_soc_dapm_widget *wsink = path->sink;
3240
3241 dapm_mark_dirty(wsource, "Route removed");
3242 dapm_mark_dirty(wsink, "Route removed");
3243 if (path->connect)
3244 dapm_path_invalidate(path);
3245
3246 dapm_free_path(path);
3247
3248 /* Update any path related flags */
3249 dapm_update_widget_flags(wsource);
3250 dapm_update_widget_flags(wsink);
3251 } else {
3252 dev_warn(dev, "ASoC: Route %s->%s does not exist\n",
3253 source, sink);
3254 }
3255
3256 return 0;
3257 }
3258
3259 /**
3260 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
3261 * @dapm: DAPM context
3262 * @route: audio routes
3263 * @num: number of routes
3264 *
3265 * Connects 2 dapm widgets together via a named audio path. The sink is
3266 * the widget receiving the audio signal, whilst the source is the sender
3267 * of the audio signal.
3268 *
3269 * Returns 0 for success else error. On error all resources can be freed
3270 * with a call to snd_soc_card_free().
3271 */
snd_soc_dapm_add_routes(struct snd_soc_dapm_context * dapm,const struct snd_soc_dapm_route * route,int num)3272 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
3273 const struct snd_soc_dapm_route *route, int num)
3274 {
3275 int i, ret = 0;
3276
3277 snd_soc_dapm_mutex_lock(dapm);
3278 for (i = 0; i < num; i++) {
3279 int r = snd_soc_dapm_add_route(dapm, route);
3280 if (r < 0)
3281 ret = r;
3282 route++;
3283 }
3284 snd_soc_dapm_mutex_unlock(dapm);
3285
3286 return ret;
3287 }
3288 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3289
3290 /**
3291 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3292 * @dapm: DAPM context
3293 * @route: audio routes
3294 * @num: number of routes
3295 *
3296 * Removes routes from the DAPM context.
3297 */
snd_soc_dapm_del_routes(struct snd_soc_dapm_context * dapm,const struct snd_soc_dapm_route * route,int num)3298 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3299 const struct snd_soc_dapm_route *route, int num)
3300 {
3301 int i;
3302
3303 snd_soc_dapm_mutex_lock(dapm);
3304 for (i = 0; i < num; i++) {
3305 snd_soc_dapm_del_route(dapm, route);
3306 route++;
3307 }
3308 snd_soc_dapm_mutex_unlock(dapm);
3309
3310 return 0;
3311 }
3312 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3313
3314 /**
3315 * snd_soc_dapm_new_widgets - add new dapm widgets
3316 * @card: card to be checked for new dapm widgets
3317 *
3318 * Checks the codec for any new dapm widgets and creates them if found.
3319 *
3320 * Returns 0 for success.
3321 */
snd_soc_dapm_new_widgets(struct snd_soc_card * card)3322 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3323 {
3324 struct snd_soc_dapm_widget *w;
3325 unsigned int val;
3326
3327 snd_soc_dapm_mutex_lock_root(card);
3328
3329 for_each_card_widgets(card, w)
3330 {
3331 if (w->new)
3332 continue;
3333
3334 if (w->num_kcontrols) {
3335 w->kcontrols = kzalloc_objs(struct snd_kcontrol *,
3336 w->num_kcontrols);
3337 if (!w->kcontrols) {
3338 snd_soc_dapm_mutex_unlock(card);
3339 return -ENOMEM;
3340 }
3341 }
3342
3343 switch(w->id) {
3344 case snd_soc_dapm_switch:
3345 case snd_soc_dapm_mixer:
3346 case snd_soc_dapm_mixer_named_ctl:
3347 dapm_new_mixer(w);
3348 break;
3349 case snd_soc_dapm_mux:
3350 case snd_soc_dapm_demux:
3351 dapm_new_mux(w);
3352 break;
3353 case snd_soc_dapm_pga:
3354 case snd_soc_dapm_effect:
3355 case snd_soc_dapm_out_drv:
3356 dapm_new_pga(w);
3357 break;
3358 case snd_soc_dapm_dai_link:
3359 dapm_new_dai_link(w);
3360 break;
3361 default:
3362 break;
3363 }
3364
3365 /* Read the initial power state from the device */
3366 if (w->reg >= 0) {
3367 val = dapm_read(w->dapm, w->reg);
3368 val = val >> w->shift;
3369 val &= w->mask;
3370 if (val == w->on_val)
3371 w->power = 1;
3372 }
3373
3374 w->new = 1;
3375
3376 dapm_mark_dirty(w, "new widget");
3377 dapm_debugfs_add_widget(w);
3378 }
3379
3380 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP, NULL);
3381 snd_soc_dapm_mutex_unlock(card);
3382 return 0;
3383 }
3384 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3385
3386 /**
3387 * snd_soc_dapm_get_volsw - dapm mixer get callback
3388 * @kcontrol: mixer control
3389 * @ucontrol: control element information
3390 *
3391 * Callback to get the value of a dapm mixer control.
3392 *
3393 * Returns 0 for success.
3394 */
snd_soc_dapm_get_volsw(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3395 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3396 struct snd_ctl_elem_value *ucontrol)
3397 {
3398 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
3399 struct soc_mixer_control *mc =
3400 (struct soc_mixer_control *)kcontrol->private_value;
3401 int reg = mc->reg;
3402 unsigned int shift = mc->shift;
3403 int max = mc->max;
3404 unsigned int width = fls(max);
3405 unsigned int mask = (1 << fls(max)) - 1;
3406 unsigned int invert = mc->invert;
3407 unsigned int reg_val, val, rval = 0;
3408
3409 snd_soc_dapm_mutex_lock(dapm);
3410 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3411 reg_val = dapm_read(dapm, reg);
3412 val = (reg_val >> shift) & mask;
3413
3414 if (reg != mc->rreg)
3415 reg_val = dapm_read(dapm, mc->rreg);
3416
3417 if (snd_soc_volsw_is_stereo(mc))
3418 rval = (reg_val >> mc->rshift) & mask;
3419 } else {
3420 reg_val = snd_soc_dapm_kcontrol_get_value(kcontrol);
3421 val = reg_val & mask;
3422
3423 if (snd_soc_volsw_is_stereo(mc))
3424 rval = (reg_val >> width) & mask;
3425 }
3426 snd_soc_dapm_mutex_unlock(dapm);
3427
3428 if (invert)
3429 ucontrol->value.integer.value[0] = max - val;
3430 else
3431 ucontrol->value.integer.value[0] = val;
3432
3433 if (snd_soc_volsw_is_stereo(mc)) {
3434 if (invert)
3435 ucontrol->value.integer.value[1] = max - rval;
3436 else
3437 ucontrol->value.integer.value[1] = rval;
3438 }
3439
3440 return 0;
3441 }
3442 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3443
3444 /**
3445 * snd_soc_dapm_put_volsw - dapm mixer set callback
3446 * @kcontrol: mixer control
3447 * @ucontrol: control element information
3448 *
3449 * Callback to set the value of a dapm mixer control.
3450 *
3451 * Returns 0 for success.
3452 */
snd_soc_dapm_put_volsw(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3453 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3454 struct snd_ctl_elem_value *ucontrol)
3455 {
3456 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
3457 struct device *dev = snd_soc_dapm_to_dev(dapm);
3458 struct snd_soc_card *card = dapm->card;
3459 struct soc_mixer_control *mc =
3460 (struct soc_mixer_control *)kcontrol->private_value;
3461 int reg = mc->reg;
3462 unsigned int shift = mc->shift;
3463 int max = mc->max;
3464 unsigned int width = fls(max);
3465 unsigned int mask = (1 << width) - 1;
3466 unsigned int invert = mc->invert;
3467 unsigned int val, rval = 0;
3468 int connect, rconnect = -1, change, reg_change = 0;
3469 struct snd_soc_dapm_update update = {};
3470 struct snd_soc_dapm_update *pupdate = NULL;
3471 int ret = 0;
3472
3473 val = (ucontrol->value.integer.value[0] & mask);
3474 connect = !!val;
3475
3476 if (invert)
3477 val = max - val;
3478
3479 if (snd_soc_volsw_is_stereo(mc)) {
3480 rval = (ucontrol->value.integer.value[1] & mask);
3481 rconnect = !!rval;
3482 if (invert)
3483 rval = max - rval;
3484 }
3485
3486 snd_soc_dapm_mutex_lock(card);
3487
3488 /* This assumes field width < (bits in unsigned int / 2) */
3489 if (width > sizeof(unsigned int) * 8 / 2)
3490 dev_warn(dev,
3491 "ASoC: control %s field width limit exceeded\n",
3492 kcontrol->id.name);
3493 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3494
3495 if (reg != SND_SOC_NOPM) {
3496 val = val << shift;
3497 rval = rval << mc->rshift;
3498
3499 reg_change = dapm_test_bits(dapm, reg, mask << shift, val);
3500
3501 if (snd_soc_volsw_is_stereo(mc))
3502 reg_change |= dapm_test_bits(dapm, mc->rreg,
3503 mask << mc->rshift,
3504 rval);
3505 }
3506
3507 if (change || reg_change) {
3508 if (reg_change) {
3509 if (snd_soc_volsw_is_stereo(mc)) {
3510 update.has_second_set = true;
3511 update.reg2 = mc->rreg;
3512 update.mask2 = mask << mc->rshift;
3513 update.val2 = rval;
3514 }
3515 update.kcontrol = kcontrol;
3516 update.reg = reg;
3517 update.mask = mask << shift;
3518 update.val = val;
3519 pupdate = &update;
3520 }
3521 ret = dapm_mixer_update_power(card, kcontrol, pupdate, connect, rconnect);
3522 }
3523
3524 snd_soc_dapm_mutex_unlock(card);
3525
3526 if (ret > 0)
3527 snd_soc_dpcm_runtime_update(card);
3528
3529 return change;
3530 }
3531 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3532
3533 /**
3534 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3535 * @kcontrol: mixer control
3536 * @ucontrol: control element information
3537 *
3538 * Callback to get the value of a dapm enumerated double mixer control.
3539 *
3540 * Returns 0 for success.
3541 */
snd_soc_dapm_get_enum_double(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3542 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3543 struct snd_ctl_elem_value *ucontrol)
3544 {
3545 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
3546 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3547 unsigned int reg_val, val;
3548
3549 snd_soc_dapm_mutex_lock(dapm);
3550 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3551 reg_val = dapm_read(dapm, e->reg);
3552 } else {
3553 reg_val = snd_soc_dapm_kcontrol_get_value(kcontrol);
3554 }
3555 snd_soc_dapm_mutex_unlock(dapm);
3556
3557 val = (reg_val >> e->shift_l) & e->mask;
3558 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3559 if (e->shift_l != e->shift_r) {
3560 val = (reg_val >> e->shift_r) & e->mask;
3561 val = snd_soc_enum_val_to_item(e, val);
3562 ucontrol->value.enumerated.item[1] = val;
3563 }
3564
3565 return 0;
3566 }
3567 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3568
3569 /**
3570 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3571 * @kcontrol: mixer control
3572 * @ucontrol: control element information
3573 *
3574 * Callback to set the value of a dapm enumerated double mixer control.
3575 *
3576 * Returns 0 for success.
3577 */
snd_soc_dapm_put_enum_double(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3578 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3579 struct snd_ctl_elem_value *ucontrol)
3580 {
3581 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
3582 struct snd_soc_card *card = dapm->card;
3583 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3584 unsigned int *item = ucontrol->value.enumerated.item;
3585 unsigned int val, change, reg_change = 0;
3586 unsigned int mask;
3587 struct snd_soc_dapm_update update = {};
3588 struct snd_soc_dapm_update *pupdate = NULL;
3589 int ret = 0;
3590
3591 if (item[0] >= e->items)
3592 return -EINVAL;
3593
3594 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3595 mask = e->mask << e->shift_l;
3596 if (e->shift_l != e->shift_r) {
3597 if (item[1] > e->items)
3598 return -EINVAL;
3599 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3600 mask |= e->mask << e->shift_r;
3601 }
3602
3603 snd_soc_dapm_mutex_lock(card);
3604
3605 change = dapm_kcontrol_set_value(kcontrol, val);
3606
3607 if (e->reg != SND_SOC_NOPM)
3608 reg_change = dapm_test_bits(dapm, e->reg, mask, val);
3609
3610 if (change || reg_change) {
3611 if (reg_change) {
3612 update.kcontrol = kcontrol;
3613 update.reg = e->reg;
3614 update.mask = mask;
3615 update.val = val;
3616 pupdate = &update;
3617 }
3618 ret = dapm_mux_update_power(card, kcontrol, pupdate, item[0], e);
3619 }
3620
3621 snd_soc_dapm_mutex_unlock(card);
3622
3623 if (ret > 0)
3624 snd_soc_dpcm_runtime_update(card);
3625
3626 return change;
3627 }
3628 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3629
3630 /**
3631 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3632 *
3633 * @kcontrol: mixer control
3634 * @uinfo: control element information
3635 *
3636 * Callback to provide information about a pin switch control.
3637 */
snd_soc_dapm_info_pin_switch(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)3638 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3639 struct snd_ctl_elem_info *uinfo)
3640 {
3641 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3642 uinfo->count = 1;
3643 uinfo->value.integer.min = 0;
3644 uinfo->value.integer.max = 1;
3645
3646 return 0;
3647 }
3648 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3649
__snd_soc_dapm_get_pin_switch(struct snd_soc_dapm_context * dapm,const char * pin,struct snd_ctl_elem_value * ucontrol)3650 static int __snd_soc_dapm_get_pin_switch(struct snd_soc_dapm_context *dapm,
3651 const char *pin,
3652 struct snd_ctl_elem_value *ucontrol)
3653 {
3654 snd_soc_dapm_mutex_lock(dapm);
3655 ucontrol->value.integer.value[0] = snd_soc_dapm_get_pin_status(dapm, pin);
3656 snd_soc_dapm_mutex_unlock(dapm);
3657
3658 return 0;
3659 }
3660
3661 /**
3662 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3663 *
3664 * @kcontrol: mixer control
3665 * @ucontrol: Value
3666 *
3667 * Callback to provide information for a pin switch added at the card
3668 * level.
3669 */
snd_soc_dapm_get_pin_switch(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3670 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3671 struct snd_ctl_elem_value *ucontrol)
3672 {
3673 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3674 struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
3675 const char *pin = (const char *)kcontrol->private_value;
3676
3677 return __snd_soc_dapm_get_pin_switch(dapm, pin, ucontrol);
3678 }
3679 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3680
3681 /**
3682 * snd_soc_dapm_get_component_pin_switch - Get information for a pin switch
3683 *
3684 * @kcontrol: mixer control
3685 * @ucontrol: Value
3686 *
3687 * Callback to provide information for a pin switch added at the component
3688 * level.
3689 */
snd_soc_dapm_get_component_pin_switch(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3690 int snd_soc_dapm_get_component_pin_switch(struct snd_kcontrol *kcontrol,
3691 struct snd_ctl_elem_value *ucontrol)
3692 {
3693 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3694 struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
3695 const char *pin = (const char *)kcontrol->private_value;
3696
3697 return __snd_soc_dapm_get_pin_switch(dapm, pin, ucontrol);
3698 }
3699 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_component_pin_switch);
3700
__dapm_put_pin_switch(struct snd_soc_dapm_context * dapm,const char * pin,struct snd_ctl_elem_value * ucontrol)3701 static int __dapm_put_pin_switch(struct snd_soc_dapm_context *dapm,
3702 const char *pin,
3703 struct snd_ctl_elem_value *ucontrol)
3704 {
3705 int ret;
3706
3707 snd_soc_dapm_mutex_lock(dapm);
3708 ret = __dapm_set_pin(dapm, pin, !!ucontrol->value.integer.value[0]);
3709 snd_soc_dapm_mutex_unlock(dapm);
3710
3711 snd_soc_dapm_sync(dapm);
3712
3713 return ret;
3714 }
3715
3716 /**
3717 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3718 *
3719 * @kcontrol: mixer control
3720 * @ucontrol: Value
3721 *
3722 * Callback to provide information for a pin switch added at the card
3723 * level.
3724 */
snd_soc_dapm_put_pin_switch(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3725 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3726 struct snd_ctl_elem_value *ucontrol)
3727 {
3728 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3729 struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
3730 const char *pin = (const char *)kcontrol->private_value;
3731
3732 return __dapm_put_pin_switch(dapm, pin, ucontrol);
3733 }
3734 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3735
3736 /**
3737 * snd_soc_dapm_put_component_pin_switch - Set information for a pin switch
3738 *
3739 * @kcontrol: mixer control
3740 * @ucontrol: Value
3741 *
3742 * Callback to provide information for a pin switch added at the component
3743 * level.
3744 */
snd_soc_dapm_put_component_pin_switch(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3745 int snd_soc_dapm_put_component_pin_switch(struct snd_kcontrol *kcontrol,
3746 struct snd_ctl_elem_value *ucontrol)
3747 {
3748 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3749 struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
3750 const char *pin = (const char *)kcontrol->private_value;
3751
3752 return __dapm_put_pin_switch(dapm, pin, ucontrol);
3753 }
3754 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_component_pin_switch);
3755
3756 struct snd_soc_dapm_widget *
snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context * dapm,const struct snd_soc_dapm_widget * widget)3757 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3758 const struct snd_soc_dapm_widget *widget)
3759 {
3760 struct device *dev = snd_soc_dapm_to_dev(dapm);
3761 enum snd_soc_dapm_direction dir;
3762 struct snd_soc_dapm_widget *w;
3763 int ret = -ENOMEM;
3764
3765 w = dapm_cnew_widget(widget, dapm_prefix(dapm));
3766 if (!w)
3767 goto cnew_failed;
3768
3769 switch (w->id) {
3770 case snd_soc_dapm_regulator_supply:
3771 w->regulator = devm_regulator_get(dev, widget->name);
3772 if (IS_ERR(w->regulator)) {
3773 ret = PTR_ERR(w->regulator);
3774 goto request_failed;
3775 }
3776
3777 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3778 ret = regulator_allow_bypass(w->regulator, true);
3779 if (ret != 0)
3780 dev_warn(dev,
3781 "ASoC: Failed to bypass %s: %d\n",
3782 w->name, ret);
3783 }
3784 break;
3785 case snd_soc_dapm_pinctrl:
3786 w->pinctrl = devm_pinctrl_get(dev);
3787 if (IS_ERR(w->pinctrl)) {
3788 ret = PTR_ERR(w->pinctrl);
3789 goto request_failed;
3790 }
3791
3792 /* set to sleep_state when initializing */
3793 snd_soc_dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD);
3794 break;
3795 case snd_soc_dapm_clock_supply:
3796 w->clk = devm_clk_get(dev, widget->name);
3797 if (IS_ERR(w->clk)) {
3798 ret = PTR_ERR(w->clk);
3799 goto request_failed;
3800 }
3801 break;
3802 default:
3803 break;
3804 }
3805
3806 switch (w->id) {
3807 case snd_soc_dapm_mic:
3808 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3809 w->power_check = dapm_generic_check_power;
3810 break;
3811 case snd_soc_dapm_input:
3812 if (!dapm->card->fully_routed)
3813 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3814 w->power_check = dapm_generic_check_power;
3815 break;
3816 case snd_soc_dapm_spk:
3817 case snd_soc_dapm_hp:
3818 w->is_ep = SND_SOC_DAPM_EP_SINK;
3819 w->power_check = dapm_generic_check_power;
3820 break;
3821 case snd_soc_dapm_output:
3822 if (!dapm->card->fully_routed)
3823 w->is_ep = SND_SOC_DAPM_EP_SINK;
3824 w->power_check = dapm_generic_check_power;
3825 break;
3826 case snd_soc_dapm_vmid:
3827 case snd_soc_dapm_siggen:
3828 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3829 w->power_check = dapm_always_on_check_power;
3830 break;
3831 case snd_soc_dapm_sink:
3832 w->is_ep = SND_SOC_DAPM_EP_SINK;
3833 w->power_check = dapm_always_on_check_power;
3834 break;
3835
3836 case snd_soc_dapm_mux:
3837 case snd_soc_dapm_demux:
3838 case snd_soc_dapm_switch:
3839 case snd_soc_dapm_mixer:
3840 case snd_soc_dapm_mixer_named_ctl:
3841 case snd_soc_dapm_adc:
3842 case snd_soc_dapm_aif_out:
3843 case snd_soc_dapm_dac:
3844 case snd_soc_dapm_aif_in:
3845 case snd_soc_dapm_pga:
3846 case snd_soc_dapm_buffer:
3847 case snd_soc_dapm_scheduler:
3848 case snd_soc_dapm_effect:
3849 case snd_soc_dapm_src:
3850 case snd_soc_dapm_asrc:
3851 case snd_soc_dapm_encoder:
3852 case snd_soc_dapm_decoder:
3853 case snd_soc_dapm_out_drv:
3854 case snd_soc_dapm_micbias:
3855 case snd_soc_dapm_line:
3856 case snd_soc_dapm_dai_link:
3857 case snd_soc_dapm_dai_out:
3858 case snd_soc_dapm_dai_in:
3859 w->power_check = dapm_generic_check_power;
3860 break;
3861 case snd_soc_dapm_supply:
3862 case snd_soc_dapm_regulator_supply:
3863 case snd_soc_dapm_pinctrl:
3864 case snd_soc_dapm_clock_supply:
3865 case snd_soc_dapm_kcontrol:
3866 w->is_supply = 1;
3867 w->power_check = dapm_supply_check_power;
3868 break;
3869 default:
3870 w->power_check = dapm_always_on_check_power;
3871 break;
3872 }
3873
3874 w->dapm = dapm;
3875 INIT_LIST_HEAD(&w->list);
3876 INIT_LIST_HEAD(&w->dirty);
3877 /* see for_each_card_widgets */
3878 list_add_tail(&w->list, &dapm->card->widgets);
3879
3880 dapm_for_each_direction(dir) {
3881 INIT_LIST_HEAD(&w->edges[dir]);
3882 w->endpoints[dir] = -1;
3883 }
3884
3885 /* machine layer sets up unconnected pins and insertions */
3886 w->connected = 1;
3887 return w;
3888
3889 request_failed:
3890 dev_err_probe(dev, ret, "ASoC: Failed to request %s\n",
3891 w->name);
3892 kfree_const(w->name);
3893 kfree_const(w->sname);
3894 kfree(w);
3895 cnew_failed:
3896 return ERR_PTR(ret);
3897 }
3898
3899 /**
3900 * snd_soc_dapm_new_control - create new dapm control
3901 * @dapm: DAPM context
3902 * @widget: widget template
3903 *
3904 * Creates new DAPM control based upon a template.
3905 *
3906 * Returns a widget pointer on success or an error pointer on failure
3907 */
3908 struct snd_soc_dapm_widget *
snd_soc_dapm_new_control(struct snd_soc_dapm_context * dapm,const struct snd_soc_dapm_widget * widget)3909 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3910 const struct snd_soc_dapm_widget *widget)
3911 {
3912 struct snd_soc_dapm_widget *w;
3913
3914 snd_soc_dapm_mutex_lock(dapm);
3915 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3916 snd_soc_dapm_mutex_unlock(dapm);
3917
3918 return w;
3919 }
3920 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3921
3922 /**
3923 * snd_soc_dapm_new_controls - create new dapm controls
3924 * @dapm: DAPM context
3925 * @widget: widget array
3926 * @num: number of widgets
3927 *
3928 * Creates new DAPM controls based upon the templates.
3929 *
3930 * Returns 0 for success else error.
3931 */
snd_soc_dapm_new_controls(struct snd_soc_dapm_context * dapm,const struct snd_soc_dapm_widget * widget,unsigned int num)3932 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3933 const struct snd_soc_dapm_widget *widget,
3934 unsigned int num)
3935 {
3936 int i;
3937 int ret = 0;
3938
3939 snd_soc_dapm_mutex_lock_root(dapm);
3940 for (i = 0; i < num; i++) {
3941 struct snd_soc_dapm_widget *w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3942 if (IS_ERR(w)) {
3943 ret = PTR_ERR(w);
3944 break;
3945 }
3946 widget++;
3947 }
3948 snd_soc_dapm_mutex_unlock(dapm);
3949 return ret;
3950 }
3951 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3952
dapm_dai_link_event_pre_pmu(struct snd_soc_dapm_widget * w,struct snd_pcm_substream * substream)3953 static int dapm_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3954 struct snd_pcm_substream *substream)
3955 {
3956 struct device *dev = snd_soc_dapm_to_dev(w->dapm);
3957 struct snd_soc_dapm_path *path;
3958 struct snd_soc_dai *source, *sink;
3959 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
3960 const struct snd_soc_pcm_stream *config = NULL;
3961 struct snd_pcm_runtime *runtime = NULL;
3962 unsigned int fmt;
3963 int ret;
3964
3965 /*
3966 * NOTE
3967 *
3968 * snd_pcm_hw_params is quite large (608 bytes on arm64) and is
3969 * starting to get a bit excessive for allocation on the stack,
3970 * especially when you're building with some of the KASAN type
3971 * stuff that increases stack usage.
3972 * So, we use kzalloc()/kfree() for params in this function.
3973 */
3974 struct snd_pcm_hw_params *params __free(kfree) = kzalloc_obj(*params);
3975 if (!params)
3976 return -ENOMEM;
3977
3978 runtime = kzalloc_obj(*runtime);
3979 if (!runtime)
3980 return -ENOMEM;
3981
3982 substream->runtime = runtime;
3983
3984 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3985 snd_soc_dapm_widget_for_each_source_path(w, path) {
3986 source = path->source->priv;
3987
3988 ret = snd_soc_dai_startup(source, substream);
3989 if (ret < 0)
3990 return ret;
3991
3992 snd_soc_dai_activate(source, substream->stream);
3993 }
3994
3995 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3996 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3997 sink = path->sink->priv;
3998
3999 ret = snd_soc_dai_startup(sink, substream);
4000 if (ret < 0)
4001 return ret;
4002
4003 snd_soc_dai_activate(sink, substream->stream);
4004 }
4005
4006 substream->hw_opened = 1;
4007
4008 /*
4009 * Note: getting the config after .startup() gives a chance to
4010 * either party on the link to alter the configuration if
4011 * necessary
4012 */
4013 config = rtd->dai_link->c2c_params + rtd->c2c_params_select;
4014 if (!config) {
4015 dev_err(dev, "ASoC: link config missing\n");
4016 return -EINVAL;
4017 }
4018
4019 /* Be a little careful as we don't want to overflow the mask array */
4020 if (!config->formats) {
4021 dev_warn(dev, "ASoC: Invalid format was specified\n");
4022
4023 return -EINVAL;
4024 }
4025
4026 fmt = ffs(config->formats) - 1;
4027
4028 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
4029 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
4030 config->rate_min;
4031 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
4032 config->rate_max;
4033 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
4034 = config->channels_min;
4035 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
4036 = config->channels_max;
4037
4038 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
4039 snd_soc_dapm_widget_for_each_source_path(w, path) {
4040 source = path->source->priv;
4041
4042 ret = snd_soc_dai_hw_params(source, substream, params);
4043 if (ret < 0)
4044 return ret;
4045
4046 dapm_update_dai_unlocked(substream, params, source);
4047 }
4048
4049 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
4050 snd_soc_dapm_widget_for_each_sink_path(w, path) {
4051 sink = path->sink->priv;
4052
4053 ret = snd_soc_dai_hw_params(sink, substream, params);
4054 if (ret < 0)
4055 return ret;
4056
4057 dapm_update_dai_unlocked(substream, params, sink);
4058 }
4059
4060 runtime->format = params_format(params);
4061 runtime->subformat = params_subformat(params);
4062 runtime->channels = params_channels(params);
4063 runtime->rate = params_rate(params);
4064
4065 return 0;
4066 }
4067
dapm_dai_link_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)4068 static int dapm_dai_link_event(struct snd_soc_dapm_widget *w,
4069 struct snd_kcontrol *kcontrol, int event)
4070 {
4071 struct snd_soc_dapm_path *path;
4072 struct snd_soc_dai *source, *sink;
4073 struct snd_pcm_substream *substream = w->priv;
4074 int ret = 0, saved_stream = substream->stream;
4075
4076 if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
4077 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
4078 return -EINVAL;
4079
4080 switch (event) {
4081 case SND_SOC_DAPM_PRE_PMU:
4082 ret = dapm_dai_link_event_pre_pmu(w, substream);
4083 if (ret < 0)
4084 goto out;
4085
4086 break;
4087
4088 case SND_SOC_DAPM_POST_PMU:
4089 snd_soc_dapm_widget_for_each_source_path(w, path) {
4090 source = path->source->priv;
4091
4092 snd_soc_dai_prepare(source, substream);
4093 }
4094
4095 snd_soc_dapm_widget_for_each_sink_path(w, path) {
4096 sink = path->sink->priv;
4097
4098 snd_soc_dai_prepare(sink, substream);
4099 }
4100
4101 snd_soc_dapm_widget_for_each_sink_path(w, path) {
4102 sink = path->sink->priv;
4103
4104 snd_soc_dai_digital_mute(sink, 0, SNDRV_PCM_STREAM_PLAYBACK);
4105 ret = 0;
4106 }
4107 break;
4108
4109 case SND_SOC_DAPM_PRE_PMD:
4110 snd_soc_dapm_widget_for_each_sink_path(w, path) {
4111 sink = path->sink->priv;
4112
4113 snd_soc_dai_digital_mute(sink, 1, SNDRV_PCM_STREAM_PLAYBACK);
4114 ret = 0;
4115 }
4116
4117 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
4118 snd_soc_dapm_widget_for_each_source_path(w, path) {
4119 source = path->source->priv;
4120 snd_soc_dai_hw_free(source, substream, 0);
4121 }
4122
4123 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
4124 snd_soc_dapm_widget_for_each_sink_path(w, path) {
4125 sink = path->sink->priv;
4126 snd_soc_dai_hw_free(sink, substream, 0);
4127 }
4128
4129 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
4130 snd_soc_dapm_widget_for_each_source_path(w, path) {
4131 source = path->source->priv;
4132 snd_soc_dai_deactivate(source, substream->stream);
4133 snd_soc_dai_shutdown(source, substream, 0);
4134 }
4135
4136 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
4137 snd_soc_dapm_widget_for_each_sink_path(w, path) {
4138 sink = path->sink->priv;
4139 snd_soc_dai_deactivate(sink, substream->stream);
4140 snd_soc_dai_shutdown(sink, substream, 0);
4141 }
4142 break;
4143
4144 case SND_SOC_DAPM_POST_PMD:
4145 kfree(substream->runtime);
4146 substream->runtime = NULL;
4147 break;
4148
4149 default:
4150 WARN(1, "Unknown event %d\n", event);
4151 ret = -EINVAL;
4152 }
4153
4154 out:
4155 /* Restore the substream direction */
4156 substream->stream = saved_stream;
4157 return ret;
4158 }
4159
dapm_dai_link_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)4160 static int dapm_dai_link_get(struct snd_kcontrol *kcontrol,
4161 struct snd_ctl_elem_value *ucontrol)
4162 {
4163 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4164 struct snd_soc_pcm_runtime *rtd = w->priv;
4165
4166 ucontrol->value.enumerated.item[0] = rtd->c2c_params_select;
4167
4168 return 0;
4169 }
4170
dapm_dai_link_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)4171 static int dapm_dai_link_put(struct snd_kcontrol *kcontrol,
4172 struct snd_ctl_elem_value *ucontrol)
4173 {
4174 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4175 struct snd_soc_pcm_runtime *rtd = w->priv;
4176
4177 /* Can't change the config when widget is already powered */
4178 if (w->power)
4179 return -EBUSY;
4180
4181 if (ucontrol->value.enumerated.item[0] == rtd->c2c_params_select)
4182 return 0;
4183
4184 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_c2c_params)
4185 return -EINVAL;
4186
4187 rtd->c2c_params_select = ucontrol->value.enumerated.item[0];
4188
4189 return 1;
4190 }
4191
dapm_free_kcontrol(struct snd_soc_card * card,unsigned long * private_value,int num_c2c_params,const char ** w_param_text)4192 static void dapm_free_kcontrol(struct snd_soc_card *card,
4193 unsigned long *private_value,
4194 int num_c2c_params,
4195 const char **w_param_text)
4196 {
4197 int count;
4198
4199 devm_kfree(card->dev, (void *)*private_value);
4200
4201 if (!w_param_text)
4202 return;
4203
4204 for (count = 0 ; count < num_c2c_params; count++)
4205 devm_kfree(card->dev, (void *)w_param_text[count]);
4206 devm_kfree(card->dev, w_param_text);
4207 }
4208
4209 static struct snd_kcontrol_new *
dapm_alloc_kcontrol(struct snd_soc_card * card,char * link_name,const struct snd_soc_pcm_stream * c2c_params,int num_c2c_params,const char ** w_param_text,unsigned long * private_value)4210 dapm_alloc_kcontrol(struct snd_soc_card *card,
4211 char *link_name,
4212 const struct snd_soc_pcm_stream *c2c_params,
4213 int num_c2c_params, const char **w_param_text,
4214 unsigned long *private_value)
4215 {
4216 struct soc_enum w_param_enum[] = {
4217 SOC_ENUM_SINGLE(0, 0, 0, NULL),
4218 };
4219 struct snd_kcontrol_new kcontrol_dai_link[] = {
4220 SOC_ENUM_EXT(NULL, w_param_enum[0],
4221 dapm_dai_link_get,
4222 dapm_dai_link_put),
4223 };
4224 struct snd_kcontrol_new *kcontrol_news;
4225 const struct snd_soc_pcm_stream *config = c2c_params;
4226 int count;
4227
4228 for (count = 0 ; count < num_c2c_params; count++) {
4229 if (!config->stream_name) {
4230 dev_warn(card->dev,
4231 "ASoC: anonymous config %d for dai link %s\n",
4232 count, link_name);
4233 w_param_text[count] =
4234 devm_kasprintf(card->dev, GFP_KERNEL,
4235 "Anonymous Configuration %d",
4236 count);
4237 } else {
4238 w_param_text[count] = devm_kmemdup(card->dev,
4239 config->stream_name,
4240 strlen(config->stream_name) + 1,
4241 GFP_KERNEL);
4242 }
4243 if (!w_param_text[count])
4244 goto outfree_w_param;
4245 config++;
4246 }
4247
4248 w_param_enum[0].items = num_c2c_params;
4249 w_param_enum[0].texts = w_param_text;
4250
4251 *private_value =
4252 (unsigned long) devm_kmemdup(card->dev,
4253 (void *)(kcontrol_dai_link[0].private_value),
4254 sizeof(struct soc_enum), GFP_KERNEL);
4255 if (!*private_value) {
4256 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4257 link_name);
4258 goto outfree_w_param;
4259 }
4260 kcontrol_dai_link[0].private_value = *private_value;
4261 /* duplicate kcontrol_dai_link on heap so that memory persists */
4262 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4263 sizeof(struct snd_kcontrol_new),
4264 GFP_KERNEL);
4265 if (!kcontrol_news) {
4266 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4267 link_name);
4268 goto outfree_w_param;
4269 }
4270 return kcontrol_news;
4271
4272 outfree_w_param:
4273 dapm_free_kcontrol(card, private_value, num_c2c_params, w_param_text);
4274
4275 return NULL;
4276 }
4277
dapm_new_dai(struct snd_soc_card * card,struct snd_pcm_substream * substream,char * id)4278 static struct snd_soc_dapm_widget *dapm_new_dai(struct snd_soc_card *card,
4279 struct snd_pcm_substream *substream,
4280 char *id)
4281 {
4282 struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
4283 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
4284 struct snd_soc_dapm_widget template;
4285 struct snd_soc_dapm_widget *w;
4286 const struct snd_kcontrol_new *kcontrol_news;
4287 int num_kcontrols;
4288 const char **w_param_text;
4289 unsigned long private_value = 0;
4290 char *link_name;
4291 int ret = -ENOMEM;
4292
4293 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
4294 rtd->dai_link->name, id);
4295 if (!link_name)
4296 goto name_fail;
4297
4298 /* allocate memory for control, only in case of multiple configs */
4299 w_param_text = NULL;
4300 kcontrol_news = NULL;
4301 num_kcontrols = 0;
4302 if (rtd->dai_link->num_c2c_params > 1) {
4303 w_param_text = devm_kcalloc(card->dev,
4304 rtd->dai_link->num_c2c_params,
4305 sizeof(char *), GFP_KERNEL);
4306 if (!w_param_text)
4307 goto param_fail;
4308
4309 num_kcontrols = 1;
4310 kcontrol_news = dapm_alloc_kcontrol(card, link_name,
4311 rtd->dai_link->c2c_params,
4312 rtd->dai_link->num_c2c_params,
4313 w_param_text, &private_value);
4314 if (!kcontrol_news)
4315 goto param_fail;
4316 }
4317
4318 memset(&template, 0, sizeof(template));
4319 template.reg = SND_SOC_NOPM;
4320 template.id = snd_soc_dapm_dai_link;
4321 template.name = link_name;
4322 template.event = dapm_dai_link_event;
4323 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
4324 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD;
4325 template.kcontrol_news = kcontrol_news;
4326 template.num_kcontrols = num_kcontrols;
4327
4328 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
4329
4330 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4331 if (IS_ERR(w)) {
4332 ret = PTR_ERR(w);
4333 goto outfree_kcontrol_news;
4334 }
4335
4336 w->priv = substream;
4337
4338 return w;
4339
4340 outfree_kcontrol_news:
4341 devm_kfree(card->dev, (void *)template.kcontrol_news);
4342 dapm_free_kcontrol(card, &private_value,
4343 rtd->dai_link->num_c2c_params, w_param_text);
4344 param_fail:
4345 devm_kfree(card->dev, link_name);
4346 name_fail:
4347 dev_err(rtd->dev, "ASoC: Failed to create %s-%s widget: %d\n",
4348 rtd->dai_link->name, id, ret);
4349 return ERR_PTR(ret);
4350 }
4351
4352 /**
4353 * snd_soc_dapm_new_dai_widgets - Create new DAPM widgets
4354 * @dapm: DAPM context
4355 * @dai: parent DAI
4356 *
4357 * Returns 0 on success, error code otherwise.
4358 */
snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context * dapm,struct snd_soc_dai * dai)4359 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4360 struct snd_soc_dai *dai)
4361 {
4362 struct device *dev = snd_soc_dapm_to_dev(dapm);
4363 struct snd_soc_dapm_widget template;
4364 struct snd_soc_dapm_widget *w;
4365
4366 WARN_ON(dev != dai->dev);
4367
4368 memset(&template, 0, sizeof(template));
4369 template.reg = SND_SOC_NOPM;
4370
4371 if (dai->driver->playback.stream_name) {
4372 template.id = snd_soc_dapm_dai_in;
4373 template.name = dai->driver->playback.stream_name;
4374 template.sname = dai->driver->playback.stream_name;
4375
4376 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4377 template.name);
4378
4379 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4380 if (IS_ERR(w))
4381 return PTR_ERR(w);
4382
4383 w->priv = dai;
4384 snd_soc_dai_set_widget_playback(dai, w);
4385 }
4386
4387 if (dai->driver->capture.stream_name) {
4388 template.id = snd_soc_dapm_dai_out;
4389 template.name = dai->driver->capture.stream_name;
4390 template.sname = dai->driver->capture.stream_name;
4391
4392 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4393 template.name);
4394
4395 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4396 if (IS_ERR(w))
4397 return PTR_ERR(w);
4398
4399 w->priv = dai;
4400 snd_soc_dai_set_widget_capture(dai, w);
4401 }
4402
4403 return 0;
4404 }
4405 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_dai_widgets);
4406
snd_soc_dapm_link_dai_widgets(struct snd_soc_card * card)4407 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4408 {
4409 struct snd_soc_dapm_widget *dai_w, *w;
4410 struct snd_soc_dapm_widget *src, *sink;
4411 struct snd_soc_dai *dai;
4412
4413 /* For each DAI widget... */
4414 for_each_card_widgets(card, dai_w) {
4415 switch (dai_w->id) {
4416 case snd_soc_dapm_dai_in:
4417 case snd_soc_dapm_dai_out:
4418 break;
4419 default:
4420 continue;
4421 }
4422
4423 /* let users know there is no DAI to link */
4424 if (!dai_w->priv) {
4425 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4426 dai_w->name);
4427 continue;
4428 }
4429
4430 dai = dai_w->priv;
4431
4432 /* ...find all widgets with the same stream and link them */
4433 for_each_card_widgets(card, w) {
4434 if (w->dapm != dai_w->dapm)
4435 continue;
4436
4437 switch (w->id) {
4438 case snd_soc_dapm_dai_in:
4439 case snd_soc_dapm_dai_out:
4440 continue;
4441 default:
4442 break;
4443 }
4444
4445 if (!w->sname || !strstr(w->sname, dai_w->sname))
4446 continue;
4447
4448 if (dai_w->id == snd_soc_dapm_dai_in) {
4449 src = dai_w;
4450 sink = w;
4451 } else {
4452 src = w;
4453 sink = dai_w;
4454 }
4455 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4456 dapm_add_path(w->dapm, src, sink, NULL, NULL);
4457 }
4458 }
4459
4460 return 0;
4461 }
4462
dapm_connect_dai_routes(struct snd_soc_dapm_context * dapm,struct snd_soc_dai * src_dai,struct snd_soc_dapm_widget * src,struct snd_soc_dapm_widget * dai,struct snd_soc_dai * sink_dai,struct snd_soc_dapm_widget * sink)4463 static void dapm_connect_dai_routes(struct snd_soc_dapm_context *dapm,
4464 struct snd_soc_dai *src_dai,
4465 struct snd_soc_dapm_widget *src,
4466 struct snd_soc_dapm_widget *dai,
4467 struct snd_soc_dai *sink_dai,
4468 struct snd_soc_dapm_widget *sink)
4469 {
4470 struct device *dev = snd_soc_dapm_to_dev(dapm);
4471
4472 dev_dbg(dev, "connected DAI link %s:%s -> %s:%s\n",
4473 src_dai->component->name, src->name,
4474 sink_dai->component->name, sink->name);
4475
4476 if (dai) {
4477 dapm_add_path(dapm, src, dai, NULL, NULL);
4478 src = dai;
4479 }
4480
4481 dapm_add_path(dapm, src, sink, NULL, NULL);
4482 }
4483
dapm_connect_dai_pair(struct snd_soc_card * card,struct snd_soc_pcm_runtime * rtd,struct snd_soc_dai * codec_dai,struct snd_soc_dai * cpu_dai)4484 static void dapm_connect_dai_pair(struct snd_soc_card *card,
4485 struct snd_soc_pcm_runtime *rtd,
4486 struct snd_soc_dai *codec_dai,
4487 struct snd_soc_dai *cpu_dai)
4488 {
4489 struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
4490 struct snd_soc_dai_link *dai_link = rtd->dai_link;
4491 struct snd_soc_dapm_widget *codec, *cpu;
4492 struct snd_soc_dai *src_dai[] = { cpu_dai, codec_dai };
4493 struct snd_soc_dai *sink_dai[] = { codec_dai, cpu_dai };
4494 struct snd_soc_dapm_widget **src[] = { &cpu, &codec };
4495 struct snd_soc_dapm_widget **sink[] = { &codec, &cpu };
4496 char *widget_name[] = { "playback", "capture" };
4497 int stream;
4498
4499 for_each_pcm_streams(stream) {
4500 int stream_cpu, stream_codec;
4501
4502 stream_cpu = snd_soc_get_stream_cpu(dai_link, stream);
4503 stream_codec = stream;
4504
4505 /* connect BE DAI playback if widgets are valid */
4506 cpu = snd_soc_dai_get_widget(cpu_dai, stream_cpu);
4507 codec = snd_soc_dai_get_widget(codec_dai, stream_codec);
4508
4509 if (!cpu || !codec)
4510 continue;
4511
4512 /* special handling for [Codec2Codec] */
4513 if (dai_link->c2c_params && !rtd->c2c_widget[stream]) {
4514 struct snd_pcm_substream *substream = rtd->pcm->streams[stream].substream;
4515 struct snd_soc_dapm_widget *dai = dapm_new_dai(card, substream,
4516 widget_name[stream]);
4517
4518 if (IS_ERR(dai))
4519 continue;
4520
4521 rtd->c2c_widget[stream] = dai;
4522 }
4523
4524 dapm_connect_dai_routes(dapm, src_dai[stream], *src[stream],
4525 rtd->c2c_widget[stream],
4526 sink_dai[stream], *sink[stream]);
4527 }
4528 }
4529
dapm_dai_stream_event(struct snd_soc_dai * dai,int stream,int event)4530 static void dapm_dai_stream_event(struct snd_soc_dai *dai, int stream, int event)
4531 {
4532 struct snd_soc_dapm_widget *w;
4533
4534 w = snd_soc_dai_get_widget(dai, stream);
4535
4536 if (w) {
4537 unsigned int ep;
4538
4539 dapm_mark_dirty(w, "stream event");
4540
4541 if (w->id == snd_soc_dapm_dai_in) {
4542 ep = SND_SOC_DAPM_EP_SOURCE;
4543 dapm_widget_invalidate_input_paths(w);
4544 } else {
4545 ep = SND_SOC_DAPM_EP_SINK;
4546 dapm_widget_invalidate_output_paths(w);
4547 }
4548
4549 switch (event) {
4550 case SND_SOC_DAPM_STREAM_START:
4551 w->active = 1;
4552 w->is_ep = ep;
4553 break;
4554 case SND_SOC_DAPM_STREAM_STOP:
4555 w->active = 0;
4556 w->is_ep = 0;
4557 break;
4558 case SND_SOC_DAPM_STREAM_SUSPEND:
4559 case SND_SOC_DAPM_STREAM_RESUME:
4560 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4561 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4562 break;
4563 }
4564 }
4565 }
4566
snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card * card)4567 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4568 {
4569 struct snd_soc_pcm_runtime *rtd;
4570 struct snd_soc_dai *cpu_dai;
4571 struct snd_soc_dai *codec_dai;
4572
4573 /* for each BE DAI link... */
4574 for_each_card_rtds(card, rtd) {
4575 struct snd_soc_dai_link_ch_map *ch_maps;
4576 int i;
4577
4578 /*
4579 * dynamic FE links have no fixed DAI mapping.
4580 * CODEC<->CODEC links have no direct connection.
4581 */
4582 if (rtd->dai_link->dynamic)
4583 continue;
4584
4585 /*
4586 * see
4587 * soc.h :: [dai_link->ch_maps Image sample]
4588 */
4589 for_each_rtd_ch_maps(rtd, i, ch_maps) {
4590 cpu_dai = snd_soc_rtd_to_cpu(rtd, ch_maps->cpu);
4591 codec_dai = snd_soc_rtd_to_codec(rtd, ch_maps->codec);
4592
4593 dapm_connect_dai_pair(card, rtd, codec_dai, cpu_dai);
4594 }
4595 }
4596 }
4597
dapm_stream_event(struct snd_soc_pcm_runtime * rtd,int stream,int event)4598 static void dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream, int event)
4599 {
4600 struct snd_soc_dai *dai;
4601 int i;
4602
4603 for_each_rtd_dais(rtd, i, dai)
4604 dapm_dai_stream_event(dai, stream, event);
4605
4606 dapm_power_widgets(rtd->card, event, NULL);
4607 }
4608
4609 /**
4610 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4611 * @rtd: PCM runtime data
4612 * @stream: stream name
4613 * @event: stream event
4614 *
4615 * Sends a stream event to the dapm core. The core then makes any
4616 * necessary widget power changes.
4617 *
4618 * Returns 0 for success else error.
4619 */
snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime * rtd,int stream,int event)4620 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4621 int event)
4622 {
4623 struct snd_soc_card *card = rtd->card;
4624
4625 snd_soc_dapm_mutex_lock(card);
4626 dapm_stream_event(rtd, stream, event);
4627 snd_soc_dapm_mutex_unlock(card);
4628 }
4629
snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime * rtd,int stream)4630 void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream)
4631 {
4632 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
4633 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
4634 /* powered down playback stream now */
4635 snd_soc_dapm_stream_event(rtd,
4636 SNDRV_PCM_STREAM_PLAYBACK,
4637 SND_SOC_DAPM_STREAM_STOP);
4638 } else {
4639 /* start delayed pop wq here for playback streams */
4640 rtd->pop_wait = 1;
4641 queue_delayed_work(system_power_efficient_wq,
4642 &rtd->delayed_work,
4643 msecs_to_jiffies(rtd->pmdown_time));
4644 }
4645 } else {
4646 /* capture streams can be powered down now */
4647 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
4648 SND_SOC_DAPM_STREAM_STOP);
4649 }
4650 }
4651 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop);
4652
4653 /**
4654 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4655 * @dapm: DAPM context
4656 * @pin: pin name
4657 *
4658 * Enables input/output pin and its parents or children widgets iff there is
4659 * a valid audio route and active audio stream.
4660 *
4661 * Requires external locking.
4662 *
4663 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4664 * do any widget power switching.
4665 */
snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context * dapm,const char * pin)4666 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4667 const char *pin)
4668 {
4669 return dapm_set_pin(dapm, pin, 1);
4670 }
4671 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4672
4673 /**
4674 * snd_soc_dapm_enable_pin - enable pin.
4675 * @dapm: DAPM context
4676 * @pin: pin name
4677 *
4678 * Enables input/output pin and its parents or children widgets iff there is
4679 * a valid audio route and active audio stream.
4680 *
4681 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4682 * do any widget power switching.
4683 */
snd_soc_dapm_enable_pin(struct snd_soc_dapm_context * dapm,const char * pin)4684 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4685 {
4686 int ret;
4687
4688 snd_soc_dapm_mutex_lock(dapm);
4689
4690 ret = dapm_set_pin(dapm, pin, 1);
4691
4692 snd_soc_dapm_mutex_unlock(dapm);
4693
4694 return ret;
4695 }
4696 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4697
4698 /**
4699 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4700 * @dapm: DAPM context
4701 * @pin: pin name
4702 *
4703 * Enables input/output pin regardless of any other state. This is
4704 * intended for use with microphone bias supplies used in microphone
4705 * jack detection.
4706 *
4707 * Requires external locking.
4708 *
4709 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4710 * do any widget power switching.
4711 */
snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context * dapm,const char * pin)4712 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4713 const char *pin)
4714 {
4715 struct device *dev;
4716 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4717
4718 if (!w) {
4719 dev = snd_soc_dapm_to_dev(dapm);
4720
4721 dev_err(dev, "ASoC: unknown pin %s\n", pin);
4722 return -EINVAL;
4723 }
4724
4725 dev = snd_soc_dapm_to_dev(w->dapm);
4726
4727 dev_dbg(dev, "ASoC: force enable pin %s\n", pin);
4728 if (!w->connected) {
4729 /*
4730 * w->force does not affect the number of input or output paths,
4731 * so we only have to recheck if w->connected is changed
4732 */
4733 dapm_widget_invalidate_input_paths(w);
4734 dapm_widget_invalidate_output_paths(w);
4735 w->connected = 1;
4736 }
4737 w->force = 1;
4738 dapm_mark_dirty(w, "force enable");
4739
4740 return 0;
4741 }
4742 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4743
4744 /**
4745 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4746 * @dapm: DAPM context
4747 * @pin: pin name
4748 *
4749 * Enables input/output pin regardless of any other state. This is
4750 * intended for use with microphone bias supplies used in microphone
4751 * jack detection.
4752 *
4753 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4754 * do any widget power switching.
4755 */
snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context * dapm,const char * pin)4756 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4757 const char *pin)
4758 {
4759 int ret;
4760
4761 snd_soc_dapm_mutex_lock(dapm);
4762
4763 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4764
4765 snd_soc_dapm_mutex_unlock(dapm);
4766
4767 return ret;
4768 }
4769 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4770
4771 /**
4772 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4773 * @dapm: DAPM context
4774 * @pin: pin name
4775 *
4776 * Disables input/output pin and its parents or children widgets.
4777 *
4778 * Requires external locking.
4779 *
4780 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4781 * do any widget power switching.
4782 */
snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context * dapm,const char * pin)4783 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4784 const char *pin)
4785 {
4786 return dapm_set_pin(dapm, pin, 0);
4787 }
4788 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4789
4790 /**
4791 * snd_soc_dapm_disable_pin - disable pin.
4792 * @dapm: DAPM context
4793 * @pin: pin name
4794 *
4795 * Disables input/output pin and its parents or children widgets.
4796 *
4797 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4798 * do any widget power switching.
4799 */
snd_soc_dapm_disable_pin(struct snd_soc_dapm_context * dapm,const char * pin)4800 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4801 const char *pin)
4802 {
4803 int ret;
4804
4805 snd_soc_dapm_mutex_lock(dapm);
4806
4807 ret = dapm_set_pin(dapm, pin, 0);
4808
4809 snd_soc_dapm_mutex_unlock(dapm);
4810
4811 return ret;
4812 }
4813 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4814
4815 /**
4816 * snd_soc_dapm_get_pin_status - get audio pin status
4817 * @dapm: DAPM context
4818 * @pin: audio signal pin endpoint (or start point)
4819 *
4820 * Get audio pin status - connected or disconnected.
4821 *
4822 * Returns 1 for connected otherwise 0.
4823 */
snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context * dapm,const char * pin)4824 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4825 const char *pin)
4826 {
4827 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4828
4829 if (w)
4830 return w->connected;
4831
4832 return 0;
4833 }
4834 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4835
4836 /**
4837 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4838 * @dapm: DAPM context
4839 * @pin: audio signal pin endpoint (or start point)
4840 *
4841 * Mark the given endpoint or pin as ignoring suspend. When the
4842 * system is disabled a path between two endpoints flagged as ignoring
4843 * suspend will not be disabled. The path must already be enabled via
4844 * normal means at suspend time, it will not be turned on if it was not
4845 * already enabled.
4846 */
snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context * dapm,const char * pin)4847 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4848 const char *pin)
4849 {
4850 struct device *dev = snd_soc_dapm_to_dev(dapm);
4851 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4852
4853 if (!w) {
4854 dev_err(dev, "ASoC: unknown pin %s\n", pin);
4855 return -EINVAL;
4856 }
4857
4858 w->ignore_suspend = 1;
4859
4860 return 0;
4861 }
4862 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4863
4864 /**
4865 * snd_soc_dapm_free - free dapm resources
4866 * @dapm: DAPM context
4867 *
4868 * Free all dapm widgets and resources.
4869 */
snd_soc_dapm_free(struct snd_soc_dapm_context * dapm)4870 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4871 {
4872 dapm_debugfs_cleanup(dapm);
4873 dapm_free_widgets(dapm);
4874 list_del(&dapm->list);
4875 }
4876
snd_soc_dapm_init(struct snd_soc_dapm_context * dapm,struct snd_soc_card * card,struct snd_soc_component * component)4877 void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
4878 struct snd_soc_card *card,
4879 struct snd_soc_component *component)
4880 {
4881 dapm->card = card;
4882 dapm->component = component;
4883 dapm->bias_level = SND_SOC_BIAS_OFF;
4884
4885 if (component)
4886 dapm->idle_bias = component->driver->idle_bias_on;
4887
4888 INIT_LIST_HEAD(&dapm->list);
4889 /* see for_each_card_dapms */
4890 list_add(&dapm->list, &card->dapm_list);
4891 }
4892
dapm_shutdown(struct snd_soc_dapm_context * dapm)4893 static void dapm_shutdown(struct snd_soc_dapm_context *dapm)
4894 {
4895 struct snd_soc_card *card = dapm->card;
4896 struct snd_soc_dapm_widget *w;
4897 LIST_HEAD(down_list);
4898 int powerdown = 0;
4899
4900 snd_soc_dapm_mutex_lock_root(card);
4901
4902 for_each_card_widgets(dapm->card, w) {
4903 if (w->dapm != dapm)
4904 continue;
4905 if (w->power) {
4906 dapm_seq_insert(w, &down_list, false);
4907 w->new_power = 0;
4908 powerdown = 1;
4909 }
4910 }
4911
4912 /* If there were no widgets to power down we're already in
4913 * standby.
4914 */
4915 if (powerdown) {
4916 if (dapm->bias_level == SND_SOC_BIAS_ON)
4917 snd_soc_dapm_set_bias_level(dapm,
4918 SND_SOC_BIAS_PREPARE);
4919 dapm_seq_run(card, &down_list, 0, false);
4920 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4921 snd_soc_dapm_set_bias_level(dapm,
4922 SND_SOC_BIAS_STANDBY);
4923 }
4924
4925 snd_soc_dapm_mutex_unlock(card);
4926 }
4927
4928 /*
4929 * snd_soc_dapm_shutdown - callback for system shutdown
4930 */
snd_soc_dapm_shutdown(struct snd_soc_card * card)4931 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4932 {
4933 struct snd_soc_dapm_context *card_dapm = snd_soc_card_to_dapm(card);
4934 struct snd_soc_dapm_context *dapm;
4935
4936 for_each_card_dapms(card, dapm) {
4937 if (dapm != card_dapm) {
4938 dapm_shutdown(dapm);
4939 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4940 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_OFF);
4941 }
4942 }
4943
4944 dapm_shutdown(card_dapm);
4945 if (card_dapm->bias_level == SND_SOC_BIAS_STANDBY)
4946 snd_soc_dapm_set_bias_level(card_dapm, SND_SOC_BIAS_OFF);
4947 }
4948
4949 /* Module information */
4950 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4951 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4952 MODULE_LICENSE("GPL");
4953