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