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