blob: a8e842e02cdc2435626bfa71b4c4f124ababa42e [file] [log] [blame]
Kuninori Morimotoc01f3af2018-07-02 06:24:31 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4//
5// Copyright 2005 Wolfson Microelectronics PLC.
6// Author: Liam Girdwood <lrg@slimlogic.co.uk>
7//
8// Features:
9// o Changes power status of internal codec blocks depending on the
10// dynamic configuration of codec internal audio paths and active
11// DACs/ADCs.
12// o Platform power domain - can support external components i.e. amps and
13// mic/headphone insertion events.
14// o Automatic Mic Bias support
15// o Jack insertion power event initiation - e.g. hp insertion will enable
16// sinks, dacs, etc
17// o Delayed power down of audio subsystem to reduce pops between a quick
18// device reopen.
Richard Purdie2b97eab2006-10-06 18:32:18 +020019
20#include <linux/module.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020021#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080022#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020023#include <linux/delay.h>
24#include <linux/pm.h>
25#include <linux/bitops.h>
26#include <linux/platform_device.h>
27#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020028#include <linux/debugfs.h>
Mark Brownf1aac482011-12-05 15:17:06 +000029#include <linux/pm_runtime.h>
Mark Brown62ea8742012-01-21 21:14:48 +000030#include <linux/regulator/consumer.h>
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +000031#include <linux/pinctrl/consumer.h>
Ola Liljad7e7eb92012-05-24 15:26:25 +020032#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020034#include <sound/core.h>
35#include <sound/pcm.h>
36#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020037#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020038#include <sound/initval.h>
39
Mark Brown84e90932010-11-04 00:07:02 -040040#include <trace/events/asoc.h>
41
Mark Brownde02d072011-09-20 21:43:24 +010042#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
43
Lars-Peter Clausena3423b02015-08-11 21:38:00 +020044#define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
45 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
46
47#define snd_soc_dapm_for_each_direction(dir) \
48 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
49 (dir)++)
50
Lars-Peter Clausen57295072013-08-05 11:27:31 +020051static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
52 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
53 const char *control,
54 int (*connected)(struct snd_soc_dapm_widget *source,
55 struct snd_soc_dapm_widget *sink));
Vladimir Zapolskiy5353f652015-06-02 00:57:53 +030056
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +010057struct snd_soc_dapm_widget *
Lars-Peter Clausen57295072013-08-05 11:27:31 +020058snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
59 const struct snd_soc_dapm_widget *widget);
60
Liam Girdwood02aa78a2015-05-25 18:21:17 +010061struct snd_soc_dapm_widget *
62snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +020063 const struct snd_soc_dapm_widget *widget);
64
Charles Keepax11d7a122022-06-23 11:51:15 +010065static unsigned int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg);
66
Richard Purdie2b97eab2006-10-06 18:32:18 +020067/* dapm power sequences - make this per codec in the future */
68static int dapm_up_seq[] = {
Pierre-Louis Bossartf13d4b52019-02-05 10:22:28 -060069 [snd_soc_dapm_pre] = 1,
70 [snd_soc_dapm_regulator_supply] = 2,
71 [snd_soc_dapm_pinctrl] = 2,
72 [snd_soc_dapm_clock_supply] = 2,
73 [snd_soc_dapm_supply] = 3,
74 [snd_soc_dapm_micbias] = 4,
75 [snd_soc_dapm_vmid] = 4,
76 [snd_soc_dapm_dai_link] = 3,
77 [snd_soc_dapm_dai_in] = 5,
78 [snd_soc_dapm_dai_out] = 5,
79 [snd_soc_dapm_aif_in] = 5,
80 [snd_soc_dapm_aif_out] = 5,
81 [snd_soc_dapm_mic] = 6,
82 [snd_soc_dapm_siggen] = 6,
83 [snd_soc_dapm_input] = 6,
84 [snd_soc_dapm_output] = 6,
85 [snd_soc_dapm_mux] = 7,
86 [snd_soc_dapm_demux] = 7,
87 [snd_soc_dapm_dac] = 8,
88 [snd_soc_dapm_switch] = 9,
89 [snd_soc_dapm_mixer] = 9,
90 [snd_soc_dapm_mixer_named_ctl] = 9,
91 [snd_soc_dapm_pga] = 10,
92 [snd_soc_dapm_buffer] = 10,
93 [snd_soc_dapm_scheduler] = 10,
94 [snd_soc_dapm_effect] = 10,
95 [snd_soc_dapm_src] = 10,
96 [snd_soc_dapm_asrc] = 10,
97 [snd_soc_dapm_encoder] = 10,
98 [snd_soc_dapm_decoder] = 10,
99 [snd_soc_dapm_adc] = 11,
100 [snd_soc_dapm_out_drv] = 12,
101 [snd_soc_dapm_hp] = 12,
102 [snd_soc_dapm_spk] = 12,
103 [snd_soc_dapm_line] = 12,
104 [snd_soc_dapm_sink] = 12,
105 [snd_soc_dapm_kcontrol] = 13,
106 [snd_soc_dapm_post] = 14,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200107};
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000108
Richard Purdie2b97eab2006-10-06 18:32:18 +0200109static int dapm_down_seq[] = {
Pierre-Louis Bossartf13d4b52019-02-05 10:22:28 -0600110 [snd_soc_dapm_pre] = 1,
111 [snd_soc_dapm_kcontrol] = 2,
112 [snd_soc_dapm_adc] = 3,
113 [snd_soc_dapm_hp] = 4,
114 [snd_soc_dapm_spk] = 4,
115 [snd_soc_dapm_line] = 4,
116 [snd_soc_dapm_out_drv] = 4,
117 [snd_soc_dapm_sink] = 4,
118 [snd_soc_dapm_pga] = 5,
119 [snd_soc_dapm_buffer] = 5,
120 [snd_soc_dapm_scheduler] = 5,
121 [snd_soc_dapm_effect] = 5,
122 [snd_soc_dapm_src] = 5,
123 [snd_soc_dapm_asrc] = 5,
124 [snd_soc_dapm_encoder] = 5,
125 [snd_soc_dapm_decoder] = 5,
126 [snd_soc_dapm_switch] = 6,
127 [snd_soc_dapm_mixer_named_ctl] = 6,
128 [snd_soc_dapm_mixer] = 6,
129 [snd_soc_dapm_dac] = 7,
130 [snd_soc_dapm_mic] = 8,
131 [snd_soc_dapm_siggen] = 8,
132 [snd_soc_dapm_input] = 8,
133 [snd_soc_dapm_output] = 8,
134 [snd_soc_dapm_micbias] = 9,
135 [snd_soc_dapm_vmid] = 9,
136 [snd_soc_dapm_mux] = 10,
137 [snd_soc_dapm_demux] = 10,
138 [snd_soc_dapm_aif_in] = 11,
139 [snd_soc_dapm_aif_out] = 11,
140 [snd_soc_dapm_dai_in] = 11,
141 [snd_soc_dapm_dai_out] = 11,
142 [snd_soc_dapm_dai_link] = 12,
143 [snd_soc_dapm_supply] = 13,
144 [snd_soc_dapm_clock_supply] = 14,
145 [snd_soc_dapm_pinctrl] = 14,
146 [snd_soc_dapm_regulator_supply] = 14,
147 [snd_soc_dapm_post] = 15,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200148};
149
Mark Brownf9fa2b12014-03-06 16:49:11 +0800150static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
151{
152 if (dapm->card && dapm->card->instantiated)
153 lockdep_assert_held(&dapm->card->dapm_mutex);
154}
155
Troy Kisky12ef1932008-10-13 17:42:14 -0700156static void pop_wait(u32 pop_time)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100157{
158 if (pop_time)
159 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
160}
161
Mathieu Malaterre595d2f72019-01-23 20:41:30 +0100162__printf(3, 4)
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200163static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100164{
165 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200166 char *buf;
167
168 if (!pop_time)
169 return;
170
171 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
172 if (buf == NULL)
173 return;
Mark Brown15e4c72f2008-07-02 11:51:20 +0100174
175 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200176 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100177 dev_info(dev, "%s", buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100178 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200179
180 kfree(buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100181}
182
Mark Browndb432b42011-10-03 21:06:40 +0100183static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
184{
185 return !list_empty(&w->dirty);
186}
187
Mark Brown492c0a12014-03-06 16:15:48 +0800188static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100189{
Mark Brownf9fa2b12014-03-06 16:49:11 +0800190 dapm_assert_locked(w->dapm);
191
Mark Brown75c1f892011-10-04 22:28:08 +0100192 if (!dapm_dirty_widget(w)) {
193 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
194 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100195 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100196 }
Mark Browndb432b42011-10-03 21:06:40 +0100197}
198
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200199/*
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200200 * Common implementation for dapm_widget_invalidate_input_paths() and
201 * dapm_widget_invalidate_output_paths(). The function is inlined since the
202 * combined size of the two specialized functions is only marginally larger then
203 * the size of the generic function and at the same time the fast path of the
204 * specialized functions is significantly smaller than the generic function.
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200205 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200206static __always_inline void dapm_widget_invalidate_paths(
207 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200208{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200209 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
210 struct snd_soc_dapm_widget *node;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200211 struct snd_soc_dapm_path *p;
212 LIST_HEAD(list);
213
214 dapm_assert_locked(w->dapm);
215
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200216 if (w->endpoints[dir] == -1)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200217 return;
218
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200219 list_add_tail(&w->work_list, &list);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200220 w->endpoints[dir] = -1;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200221
222 list_for_each_entry(w, &list, work_list) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200223 snd_soc_dapm_widget_for_each_path(w, dir, p) {
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200224 if (p->is_supply || p->weak || !p->connect)
225 continue;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200226 node = p->node[rdir];
227 if (node->endpoints[dir] != -1) {
228 node->endpoints[dir] = -1;
229 list_add_tail(&node->work_list, &list);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200230 }
231 }
232 }
233}
234
235/*
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200236 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
237 * input paths
238 * @w: The widget for which to invalidate the cached number of input paths
239 *
240 * Resets the cached number of inputs for the specified widget and all widgets
241 * that can be reached via outcoming paths from the widget.
242 *
243 * This function must be called if the number of output paths for a widget might
244 * have changed. E.g. if the source state of a widget changes or a path is added
245 * or activated with the widget as the sink.
246 */
247static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
248{
249 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
250}
251
252/*
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200253 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
254 * output paths
255 * @w: The widget for which to invalidate the cached number of output paths
256 *
257 * Resets the cached number of outputs for the specified widget and all widgets
258 * that can be reached via incoming paths from the widget.
259 *
260 * This function must be called if the number of output paths for a widget might
261 * have changed. E.g. if the sink state of a widget changes or a path is added
262 * or activated with the widget as the source.
263 */
264static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
265{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200266 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200267}
268
269/*
270 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
271 * for the widgets connected to a path
272 * @p: The path to invalidate
273 *
274 * Resets the cached number of inputs for the sink of the path and the cached
275 * number of outputs for the source of the path.
276 *
277 * This function must be called when a path is added, removed or the connected
278 * state changes.
279 */
280static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
281{
282 /*
283 * Weak paths or supply paths do not influence the number of input or
284 * output paths of their neighbors.
285 */
286 if (p->weak || p->is_supply)
287 return;
288
289 /*
290 * The number of connected endpoints is the sum of the number of
291 * connected endpoints of all neighbors. If a node with 0 connected
292 * endpoints is either connected or disconnected that sum won't change,
293 * so there is no need to re-check the path.
294 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200295 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200296 dapm_widget_invalidate_input_paths(p->sink);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200297 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200298 dapm_widget_invalidate_output_paths(p->source);
299}
300
Lars-Peter Clausen8be4da292014-10-25 17:42:01 +0200301void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
Mark Browne2d32ff2012-08-31 17:38:32 -0700302{
Mark Browne2d32ff2012-08-31 17:38:32 -0700303 struct snd_soc_dapm_widget *w;
304
305 mutex_lock(&card->dapm_mutex);
306
Kuninori Morimoto145966922020-03-09 13:08:21 +0900307 for_each_card_widgets(card, w) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200308 if (w->is_ep) {
Lars-Peter Clausen8be4da292014-10-25 17:42:01 +0200309 dapm_mark_dirty(w, "Rechecking endpoints");
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200310 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200311 dapm_widget_invalidate_output_paths(w);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200312 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200313 dapm_widget_invalidate_input_paths(w);
314 }
Mark Browne2d32ff2012-08-31 17:38:32 -0700315 }
316
317 mutex_unlock(&card->dapm_mutex);
318}
Lars-Peter Clausen8be4da292014-10-25 17:42:01 +0200319EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
Mark Browne2d32ff2012-08-31 17:38:32 -0700320
Richard Purdie2b97eab2006-10-06 18:32:18 +0200321/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100322static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200323 const struct snd_soc_dapm_widget *_widget)
324{
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -0600325 struct snd_soc_dapm_widget *w;
326
327 w = kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
328 if (!w)
329 return NULL;
330
331 /*
332 * w->name is duplicated in caller, but w->sname isn't.
333 * Duplicate it here if defined
334 */
335 if (_widget->sname) {
336 w->sname = kstrdup_const(_widget->sname, GFP_KERNEL);
Dan Carpentera6d9cef2019-02-19 15:04:27 +0300337 if (!w->sname) {
338 kfree(w);
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -0600339 return NULL;
Dan Carpentera6d9cef2019-02-19 15:04:27 +0300340 }
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -0600341 }
342 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200343}
344
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200345struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200346 unsigned int value;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200347 struct snd_soc_dapm_widget *widget;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200348 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200349 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200350};
351
352static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100353 struct snd_kcontrol *kcontrol, const char *ctrl_name)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200354{
355 struct dapm_kcontrol_data *data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200356 struct soc_mixer_control *mc;
Charles Keepax561ed682015-05-01 12:37:26 +0100357 struct soc_enum *e;
Charles Keepax773da9b2015-05-01 12:37:25 +0100358 const char *name;
359 int ret;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200360
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200361 data = kzalloc(sizeof(*data), GFP_KERNEL);
Charles Keepax40b7bea2015-05-01 12:37:24 +0100362 if (!data)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200363 return -ENOMEM;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200364
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200365 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200366
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200367 switch (widget->id) {
368 case snd_soc_dapm_switch:
369 case snd_soc_dapm_mixer:
370 case snd_soc_dapm_mixer_named_ctl:
371 mc = (struct soc_mixer_control *)kcontrol->private_value;
372
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800373 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
374 dev_warn(widget->dapm->dev,
375 "ASoC: Unsupported stereo autodisable control '%s'\n",
376 ctrl_name);
377
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200378 if (mc->autodisable) {
379 struct snd_soc_dapm_widget template;
380
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100381 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
Charles Keepax773da9b2015-05-01 12:37:25 +0100382 "Autodisable");
383 if (!name) {
384 ret = -ENOMEM;
385 goto err_data;
386 }
387
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200388 memset(&template, 0, sizeof(template));
389 template.reg = mc->reg;
390 template.mask = (1 << fls(mc->max)) - 1;
391 template.shift = mc->shift;
392 if (mc->invert)
393 template.off_val = mc->max;
394 else
395 template.off_val = 0;
396 template.on_val = template.off_val;
397 template.id = snd_soc_dapm_kcontrol;
Charles Keepax773da9b2015-05-01 12:37:25 +0100398 template.name = name;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200399
Lars-Peter Clausen2daabd72013-08-30 17:39:33 +0200400 data->value = template.on_val;
401
Liam Girdwood02aa78a2015-05-25 18:21:17 +0100402 data->widget =
403 snd_soc_dapm_new_control_unlocked(widget->dapm,
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200404 &template);
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200405 kfree(name);
Linus Walleij37e1df82017-01-13 10:23:52 +0100406 if (IS_ERR(data->widget)) {
407 ret = PTR_ERR(data->widget);
408 goto err_data;
409 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200410 }
411 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200412 case snd_soc_dapm_demux:
Charles Keepax561ed682015-05-01 12:37:26 +0100413 case snd_soc_dapm_mux:
414 e = (struct soc_enum *)kcontrol->private_value;
415
416 if (e->autodisable) {
417 struct snd_soc_dapm_widget template;
418
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100419 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
Charles Keepax561ed682015-05-01 12:37:26 +0100420 "Autodisable");
421 if (!name) {
422 ret = -ENOMEM;
423 goto err_data;
424 }
425
426 memset(&template, 0, sizeof(template));
427 template.reg = e->reg;
Gyeongtaek Leeebf14742020-04-18 13:13:20 +0900428 template.mask = e->mask;
Charles Keepax561ed682015-05-01 12:37:26 +0100429 template.shift = e->shift_l;
430 template.off_val = snd_soc_enum_item_to_val(e, 0);
431 template.on_val = template.off_val;
432 template.id = snd_soc_dapm_kcontrol;
433 template.name = name;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200434
435 data->value = template.on_val;
436
Charles Keepaxffacb482015-06-26 10:39:43 +0100437 data->widget = snd_soc_dapm_new_control_unlocked(
438 widget->dapm, &template);
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200439 kfree(name);
Linus Walleij37e1df82017-01-13 10:23:52 +0100440 if (IS_ERR(data->widget)) {
441 ret = PTR_ERR(data->widget);
442 goto err_data;
443 }
Charles Keepax561ed682015-05-01 12:37:26 +0100444
445 snd_soc_dapm_add_path(widget->dapm, data->widget,
446 widget, NULL, NULL);
Charles Keepax11d7a122022-06-23 11:51:15 +0100447 } else if (e->reg != SND_SOC_NOPM) {
448 data->value = soc_dapm_read(widget->dapm, e->reg) &
449 (e->mask << e->shift_l);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200450 }
451 break;
452 default:
453 break;
454 }
455
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200456 kcontrol->private_data = data;
457
458 return 0;
Charles Keepax773da9b2015-05-01 12:37:25 +0100459
Charles Keepax773da9b2015-05-01 12:37:25 +0100460err_data:
461 kfree(data);
462 return ret;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200463}
464
465static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
466{
467 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Srinivas Kandagatlaff2faf12018-06-04 12:13:26 +0100468
469 list_del(&data->paths);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200470 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200471 kfree(data);
472}
473
474static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
475 const struct snd_kcontrol *kcontrol)
476{
477 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
478
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200479 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200480}
481
482static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
483 struct snd_soc_dapm_widget *widget)
484{
485 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200486 struct snd_soc_dapm_widget_list *new_wlist;
487 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200488
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200489 if (data->wlist)
490 n = data->wlist->num_widgets + 1;
491 else
492 n = 1;
493
494 new_wlist = krealloc(data->wlist,
Gustavo A. R. Silva07597912019-05-23 15:36:37 -0500495 struct_size(new_wlist, widgets, n),
496 GFP_KERNEL);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200497 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200498 return -ENOMEM;
499
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200500 new_wlist->widgets[n - 1] = widget;
501 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200502
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200503 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200504
505 return 0;
506}
507
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200508static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
509 struct snd_soc_dapm_path *path)
510{
511 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
512
513 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200514}
515
516static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
517{
518 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
519
520 if (!data->widget)
521 return true;
522
523 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200524}
525
526static struct list_head *dapm_kcontrol_get_path_list(
527 const struct snd_kcontrol *kcontrol)
528{
529 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
530
531 return &data->paths;
532}
533
534#define dapm_kcontrol_for_each_path(path, kcontrol) \
535 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
536 list_kcontrol)
537
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530538unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200539{
540 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
541
542 return data->value;
543}
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530544EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200545
546static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
547 unsigned int value)
548{
549 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
550
551 if (data->value == value)
552 return false;
553
Gyeongtaek Leeebf14742020-04-18 13:13:20 +0900554 if (data->widget) {
555 switch (dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->id) {
556 case snd_soc_dapm_switch:
557 case snd_soc_dapm_mixer:
558 case snd_soc_dapm_mixer_named_ctl:
559 data->widget->on_val = value & data->widget->mask;
560 break;
561 case snd_soc_dapm_demux:
562 case snd_soc_dapm_mux:
563 data->widget->on_val = value >> data->widget->shift;
564 break;
565 default:
566 data->widget->on_val = value;
567 break;
568 }
569 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200570
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200571 data->value = value;
572
573 return true;
574}
575
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200576/**
Mythri P K93e39a12015-10-20 22:30:08 +0530577 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
578 * kcontrol
579 * @kcontrol: The kcontrol
580 */
581struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
582 struct snd_kcontrol *kcontrol)
583{
584 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
585}
586EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
587
588/**
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200589 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
590 * kcontrol
591 * @kcontrol: The kcontrol
592 *
593 * Note: This function must only be used on kcontrols that are known to have
594 * been registered for a CODEC. Otherwise the behaviour is undefined.
595 */
596struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
597 struct snd_kcontrol *kcontrol)
598{
599 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
600}
601EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
602
Liam Girdwood6c120e12012-02-15 15:15:34 +0000603static void dapm_reset(struct snd_soc_card *card)
604{
605 struct snd_soc_dapm_widget *w;
606
Mark Brownf9fa2b12014-03-06 16:49:11 +0800607 lockdep_assert_held(&card->dapm_mutex);
608
Liam Girdwood6c120e12012-02-15 15:15:34 +0000609 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
610
Kuninori Morimoto145966922020-03-09 13:08:21 +0900611 for_each_card_widgets(card, w) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200612 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000613 w->power_checked = false;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000614 }
615}
616
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200617static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
618{
619 if (!dapm->component)
620 return NULL;
621 return dapm->component->name_prefix;
622}
623
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900624static unsigned int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100625{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200626 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200627 return -EIO;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900628 return snd_soc_component_read(dapm->component, reg);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100629}
630
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200631static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
Bard Liao34775012014-04-17 20:12:56 +0800632 int reg, unsigned int mask, unsigned int value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100633{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200634 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200635 return -EIO;
Mark Brownfcf6c5e2014-12-15 13:08:48 +0000636 return snd_soc_component_update_bits(dapm->component, reg,
637 mask, value);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000638}
639
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200640static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
641 int reg, unsigned int mask, unsigned int value)
642{
643 if (!dapm->component)
644 return -EIO;
645 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
646}
647
Mark Browneb270e92013-10-09 13:52:52 +0100648static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
649{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200650 if (dapm->component)
651 snd_soc_component_async_complete(dapm->component);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100652}
653
Charles Keepax45a110a2015-05-11 13:50:30 +0100654static struct snd_soc_dapm_widget *
655dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
656{
657 struct snd_soc_dapm_widget *w = wcache->widget;
Charles Keepax45a110a2015-05-11 13:50:30 +0100658
659 if (w) {
Kuninori Morimoto7453d6d2021-08-05 10:10:55 +0900660 struct list_head *wlist = &w->dapm->card->widgets;
661 const int depth = 2;
662 int i = 0;
Charles Keepax45a110a2015-05-11 13:50:30 +0100663
664 list_for_each_entry_from(w, wlist, list) {
665 if (!strcmp(name, w->name))
666 return w;
667
668 if (++i == depth)
669 break;
670 }
671 }
672
673 return NULL;
674}
675
676static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
677 struct snd_soc_dapm_widget *w)
678{
679 wcache->widget = w;
680}
681
Mark Brown452c5ea2009-05-17 21:41:23 +0100682/**
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200683 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
684 * @dapm: The DAPM context for which to set the level
685 * @level: The level to set
686 *
687 * Forces the DAPM bias level to a specific state. It will call the bias level
688 * callback of DAPM context with the specified level. This will even happen if
689 * the context is already at the same level. Furthermore it will not go through
690 * the normal bias level sequencing, meaning any intermediate states between the
691 * current and the target state will not be entered.
692 *
693 * Note that the change in bias level is only temporary and the next time
694 * snd_soc_dapm_sync() is called the state will be set to the level as
695 * determined by the DAPM core. The function is mainly intended to be used to
696 * used during probe or resume from suspend to power up the device so
697 * initialization can be done, before the DAPM core takes over.
698 */
699int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
700 enum snd_soc_bias_level level)
701{
702 int ret = 0;
703
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900704 if (dapm->component)
705 ret = snd_soc_component_set_bias_level(dapm->component, level);
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200706
Lars-Peter Clausenf4bf8d72015-04-27 22:13:25 +0200707 if (ret == 0)
708 dapm->bias_level = level;
709
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200710 return ret;
711}
712EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
713
Mark Brown452c5ea2009-05-17 21:41:23 +0100714/**
715 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800716 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100717 * @level: level to configure
718 *
719 * Configure the bias (power) levels for the SoC audio device.
720 *
721 * Returns 0 for success else error.
722 */
Mark Browned5a4c42011-02-18 11:12:42 -0800723static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200724 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100725{
Mark Browned5a4c42011-02-18 11:12:42 -0800726 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100727 int ret = 0;
728
Mark Brown84e90932010-11-04 00:07:02 -0400729 trace_snd_soc_bias_level_start(card, level);
730
Kuninori Morimoto39caefd2020-05-28 10:49:35 +0900731 ret = snd_soc_card_set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100732 if (ret != 0)
733 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100734
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200735 if (!card || dapm != &card->dapm)
736 ret = snd_soc_dapm_force_bias_level(dapm, level);
Liam Girdwood4123128e2012-07-06 16:56:16 +0100737
Mark Brown171ec6b2011-06-06 18:15:19 +0100738 if (ret != 0)
739 goto out;
740
Kuninori Morimotod41278e2020-05-28 10:50:35 +0900741 ret = snd_soc_card_set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100742out:
Mark Brown84e90932010-11-04 00:07:02 -0400743 trace_snd_soc_bias_level_done(card, level);
744
Mark Brown452c5ea2009-05-17 21:41:23 +0100745 return ret;
746}
747
Mark Brown74b8f952009-06-06 11:26:15 +0100748/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200749static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200750 struct snd_soc_dapm_path *path, const char *control_name,
751 struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200752{
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200753 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200754 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Kuninori Morimotoaf6b57a2021-08-05 10:11:00 +0900755 unsigned int item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200756 int i;
757
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100758 if (e->reg != SND_SOC_NOPM) {
Kuninori Morimotoaf6b57a2021-08-05 10:11:00 +0900759 unsigned int val;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900760 val = soc_dapm_read(dapm, e->reg);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100761 val = (val >> e->shift_l) & e->mask;
762 item = snd_soc_enum_val_to_item(e, val);
763 } else {
764 /* since a virtual mux has no backing registers to
765 * decide which path to connect, it will try to match
766 * with the first enumeration. This is to ensure
767 * that the default mux choice (the first) will be
768 * correctly powered up during initialization.
769 */
770 item = 0;
771 }
772
Xie Yishengf9e0b4a2018-05-31 19:11:23 +0800773 i = match_string(e->texts, e->items, control_name);
774 if (i < 0)
775 return -ENODEV;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200776
Xie Yishengf9e0b4a2018-05-31 19:11:23 +0800777 path->name = e->texts[i];
778 path->connect = (i == item);
779 return 0;
780
Richard Purdie2b97eab2006-10-06 18:32:18 +0200781}
782
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100783/* set up initial codec paths */
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800784static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
785 int nth_path)
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100786{
787 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200788 p->sink->kcontrol_news[i].private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100789 unsigned int reg = mc->reg;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100790 unsigned int invert = mc->invert;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100791
792 if (reg != SND_SOC_NOPM) {
Kuninori Morimoto29155bb2021-08-05 10:11:05 +0900793 unsigned int shift = mc->shift;
794 unsigned int max = mc->max;
795 unsigned int mask = (1 << fls(max)) - 1;
796 unsigned int val = soc_dapm_read(p->sink->dapm, reg);
797
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800798 /*
799 * The nth_path argument allows this function to know
800 * which path of a kcontrol it is setting the initial
801 * status for. Ideally this would support any number
802 * of paths and channels. But since kcontrols only come
803 * in mono and stereo variants, we are limited to 2
804 * channels.
805 *
806 * The following code assumes for stereo controls the
807 * first path is the left channel, and all remaining
808 * paths are the right channel.
809 */
810 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
811 if (reg != mc->rreg)
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900812 val = soc_dapm_read(p->sink->dapm, mc->rreg);
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800813 val = (val >> mc->rshift) & mask;
814 } else {
815 val = (val >> shift) & mask;
816 }
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100817 if (invert)
818 val = max - val;
819 p->connect = !!val;
820 } else {
이경택3bbbb772020-03-31 16:55:16 +0900821 /* since a virtual mixer has no backing registers to
822 * decide which path to connect, it will try to match
823 * with initial state. This is to ensure
824 * that the default mixer choice will be
825 * correctly powered up during initialization.
826 */
827 p->connect = invert;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100828 }
829}
830
Mark Brown74b8f952009-06-06 11:26:15 +0100831/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200832static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200833 struct snd_soc_dapm_path *path, const char *control_name)
834{
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800835 int i, nth_path = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200836
837 /* search for mixer kcontrol */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200838 for (i = 0; i < path->sink->num_kcontrols; i++) {
839 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
840 path->name = path->sink->kcontrol_news[i].name;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800841 dapm_set_mixer_path_status(path, i, nth_path++);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200842 return 0;
843 }
844 }
845 return -ENODEV;
846}
847
Stephen Warrenaf468002011-04-28 17:38:01 -0600848static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600849 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600850 const struct snd_kcontrol_new *kcontrol_new,
851 struct snd_kcontrol **kcontrol)
852{
853 struct snd_soc_dapm_widget *w;
854 int i;
855
856 *kcontrol = NULL;
857
Kuninori Morimoto145966922020-03-09 13:08:21 +0900858 for_each_card_widgets(dapm->card, w) {
Stephen Warren1007da02011-05-26 09:57:33 -0600859 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
860 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600861 for (i = 0; i < w->num_kcontrols; i++) {
862 if (&w->kcontrol_news[i] == kcontrol_new) {
863 if (w->kcontrols)
864 *kcontrol = w->kcontrols[i];
865 return 1;
866 }
867 }
868 }
869
870 return 0;
871}
872
Stephen Warren85762e72013-03-29 15:40:10 -0600873/*
874 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
875 * create it. Either way, add the widget into the control's widget list
876 */
Jeeja KP19a2557b2015-10-20 22:30:06 +0530877static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100878 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200879{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200880 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000881 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000882 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600883 size_t prefix_len;
884 int shared;
885 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600886 bool wname_in_long_name, kcname_in_long_name;
Daniel Macke5092c92014-10-07 13:41:24 +0200887 char *long_name = NULL;
Stephen Warren85762e72013-03-29 15:40:10 -0600888 const char *name;
Daniel Macke5092c92014-10-07 13:41:24 +0200889 int ret = 0;
Mark Brownefb7ac32011-03-08 17:23:24 +0000890
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200891 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000892 if (prefix)
893 prefix_len = strlen(prefix) + 1;
894 else
895 prefix_len = 0;
896
Stephen Warren85762e72013-03-29 15:40:10 -0600897 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
898 &kcontrol);
899
Stephen Warren85762e72013-03-29 15:40:10 -0600900 if (!kcontrol) {
901 if (shared) {
902 wname_in_long_name = false;
903 kcname_in_long_name = true;
904 } else {
905 switch (w->id) {
906 case snd_soc_dapm_switch:
907 case snd_soc_dapm_mixer:
Jeeja KP19a2557b2015-10-20 22:30:06 +0530908 case snd_soc_dapm_pga:
Seppo Ingalsuo882c8b4a2019-04-30 18:11:34 -0500909 case snd_soc_dapm_effect:
Chen-Yu Tsaia3930ed2016-08-27 19:28:00 +0800910 case snd_soc_dapm_out_drv:
Stephen Warren85762e72013-03-29 15:40:10 -0600911 wname_in_long_name = true;
912 kcname_in_long_name = true;
913 break;
914 case snd_soc_dapm_mixer_named_ctl:
915 wname_in_long_name = false;
916 kcname_in_long_name = true;
917 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200918 case snd_soc_dapm_demux:
Stephen Warren85762e72013-03-29 15:40:10 -0600919 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600920 wname_in_long_name = true;
921 kcname_in_long_name = false;
922 break;
923 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600924 return -EINVAL;
925 }
926 }
927
928 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600929 /*
930 * The control will get a prefix from the control
931 * creation process but we're also using the same
932 * prefix for widgets so cut the prefix off the
933 * front of the widget name.
934 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200935 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600936 w->name + prefix_len,
937 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200938 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200939 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600940
941 name = long_name;
942 } else if (wname_in_long_name) {
943 long_name = NULL;
944 name = w->name + prefix_len;
945 } else {
946 long_name = NULL;
947 name = w->kcontrol_news[kci].name;
948 }
949
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200950 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600951 prefix);
Daniel Macke5092c92014-10-07 13:41:24 +0200952 if (!kcontrol) {
953 ret = -ENOMEM;
954 goto exit_free;
955 }
956
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200957 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200958
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100959 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200960 if (ret) {
961 snd_ctl_free_one(kcontrol);
Daniel Macke5092c92014-10-07 13:41:24 +0200962 goto exit_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200963 }
964
Stephen Warren85762e72013-03-29 15:40:10 -0600965 ret = snd_ctl_add(card, kcontrol);
966 if (ret < 0) {
967 dev_err(dapm->dev,
968 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
969 w->name, name, ret);
Daniel Macke5092c92014-10-07 13:41:24 +0200970 goto exit_free;
Stephen Warren85762e72013-03-29 15:40:10 -0600971 }
Stephen Warren85762e72013-03-29 15:40:10 -0600972 }
973
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200974 ret = dapm_kcontrol_add_widget(kcontrol, w);
Daniel Macke5092c92014-10-07 13:41:24 +0200975 if (ret == 0)
976 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200977
Daniel Macke5092c92014-10-07 13:41:24 +0200978exit_free:
979 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600980
Daniel Macke5092c92014-10-07 13:41:24 +0200981 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600982}
983
984/* create new dapm mixer control */
985static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
986{
987 int i, ret;
988 struct snd_soc_dapm_path *path;
Charles Keepax561ed682015-05-01 12:37:26 +0100989 struct dapm_kcontrol_data *data;
Stephen Warren85762e72013-03-29 15:40:10 -0600990
Richard Purdie2b97eab2006-10-06 18:32:18 +0200991 /* add kcontrol */
992 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200993 /* match name */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +0200994 snd_soc_dapm_widget_for_each_source_path(w, path) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200995 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600996 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200997 continue;
998
Charles Keepax561ed682015-05-01 12:37:26 +0100999 if (!w->kcontrols[i]) {
Jeeja KP19a2557b2015-10-20 22:30:06 +05301000 ret = dapm_create_or_share_kcontrol(w, i);
Charles Keepax561ed682015-05-01 12:37:26 +01001001 if (ret < 0)
1002 return ret;
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +02001003 }
1004
Mark Brown946d92a2013-08-12 23:28:42 +01001005 dapm_kcontrol_add_path(w->kcontrols[i], path);
Charles Keepax561ed682015-05-01 12:37:26 +01001006
1007 data = snd_kcontrol_chip(w->kcontrols[i]);
1008 if (data->widget)
1009 snd_soc_dapm_add_path(data->widget->dapm,
1010 data->widget,
1011 path->source,
1012 NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001013 }
1014 }
Stephen Warren85762e72013-03-29 15:40:10 -06001015
1016 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001017}
1018
1019/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001020static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001021{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001022 struct snd_soc_dapm_context *dapm = w->dapm;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001023 enum snd_soc_dapm_direction dir;
Stephen Warren85762e72013-03-29 15:40:10 -06001024 struct snd_soc_dapm_path *path;
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001025 const char *type;
Stephen Warrenaf468002011-04-28 17:38:01 -06001026 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001027
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001028 switch (w->id) {
1029 case snd_soc_dapm_mux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001030 dir = SND_SOC_DAPM_DIR_OUT;
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001031 type = "mux";
1032 break;
1033 case snd_soc_dapm_demux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001034 dir = SND_SOC_DAPM_DIR_IN;
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001035 type = "demux";
1036 break;
1037 default:
1038 return -EINVAL;
1039 }
1040
Stephen Warrenaf468002011-04-28 17:38:01 -06001041 if (w->num_kcontrols != 1) {
1042 dev_err(dapm->dev,
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001043 "ASoC: %s %s has incorrect number of controls\n", type,
Stephen Warrenaf468002011-04-28 17:38:01 -06001044 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001045 return -EINVAL;
1046 }
1047
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001048 if (list_empty(&w->edges[dir])) {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001049 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
Stephen Warren85762e72013-03-29 15:40:10 -06001050 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -06001051 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001052
Jeeja KP19a2557b2015-10-20 22:30:06 +05301053 ret = dapm_create_or_share_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -06001054 if (ret < 0)
1055 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -06001056
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001057 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1058 if (path->name)
1059 dapm_kcontrol_add_path(w->kcontrols[0], path);
Lars-Peter Clausen98407ef2014-10-25 17:41:57 +02001060 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001061
Stephen Warrenaf468002011-04-28 17:38:01 -06001062 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001063}
1064
1065/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001066static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001067{
Kuninori Morimotoa16cfb12021-08-05 10:11:10 +09001068 int i;
Jeeja KP19a2557b2015-10-20 22:30:06 +05301069
1070 for (i = 0; i < w->num_kcontrols; i++) {
Kuninori Morimotoa16cfb12021-08-05 10:11:10 +09001071 int ret = dapm_create_or_share_kcontrol(w, i);
Jeeja KP19a2557b2015-10-20 22:30:06 +05301072 if (ret < 0)
1073 return ret;
1074 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001075
Mark Browna6c65732010-03-03 17:45:21 +00001076 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001077}
1078
Nikesh Oswalc6615082015-02-02 17:06:44 +00001079/* create new dapm dai link control */
1080static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1081{
Kuninori Morimoto5c52e482021-08-05 10:11:15 +09001082 int i;
Charles Keepax243bcfa2018-09-05 15:21:02 +01001083 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00001084
1085 /* create control for links with > 1 config */
Charles Keepax243bcfa2018-09-05 15:21:02 +01001086 if (rtd->dai_link->num_params <= 1)
Nikesh Oswalc6615082015-02-02 17:06:44 +00001087 return 0;
1088
1089 /* add kcontrol */
1090 for (i = 0; i < w->num_kcontrols; i++) {
Kuninori Morimoto5c52e482021-08-05 10:11:15 +09001091 struct snd_soc_dapm_context *dapm = w->dapm;
1092 struct snd_card *card = dapm->card->snd_card;
1093 struct snd_kcontrol *kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
1094 w, w->name, NULL);
1095 int ret = snd_ctl_add(card, kcontrol);
1096
Nikesh Oswalc6615082015-02-02 17:06:44 +00001097 if (ret < 0) {
1098 dev_err(dapm->dev,
1099 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1100 w->name, w->kcontrol_news[i].name, ret);
1101 return ret;
1102 }
1103 kcontrol->private_data = w;
1104 w->kcontrols[i] = kcontrol;
1105 }
1106
1107 return 0;
1108}
1109
Mark Brown9949788b2010-05-07 20:24:05 +01001110/* We implement power down on suspend by checking the power state of
1111 * the ALSA card - when we are suspending the ALSA state for the card
1112 * is set to D3.
1113 */
1114static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1115{
Mark Brown12ea2c72011-03-02 18:17:32 +00001116 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +01001117
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001118 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +01001119 case SNDRV_CTL_POWER_D3hot:
1120 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +01001121 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001122 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001123 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +01001124 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +01001125 default:
1126 return 1;
1127 }
1128}
1129
Kuninori Morimoto52645e332020-02-19 15:56:52 +09001130static void dapm_widget_list_free(struct snd_soc_dapm_widget_list **list)
1131{
1132 kfree(*list);
1133}
1134
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001135static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1136 struct list_head *widgets)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001137{
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001138 struct snd_soc_dapm_widget *w;
1139 struct list_head *it;
1140 unsigned int size = 0;
1141 unsigned int i = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001142
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001143 list_for_each(it, widgets)
1144 size++;
1145
Kees Cookacafe7e2018-05-08 13:45:50 -07001146 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001147 if (*list == NULL)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001148 return -ENOMEM;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001149
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001150 list_for_each_entry(w, widgets, work_list)
1151 (*list)->widgets[i++] = w;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001152
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001153 (*list)->num_widgets = i;
1154
1155 return 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001156}
1157
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001158/*
Szymon Mielczarek872f3ac2019-08-09 10:40:34 +02001159 * Recursively reset the cached number of inputs or outputs for the specified
1160 * widget and all widgets that can be reached via incoming or outcoming paths
1161 * from the widget.
1162 */
1163static void invalidate_paths_ep(struct snd_soc_dapm_widget *widget,
1164 enum snd_soc_dapm_direction dir)
1165{
1166 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1167 struct snd_soc_dapm_path *path;
1168
1169 widget->endpoints[dir] = -1;
1170
1171 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1172 if (path->weak || path->is_supply)
1173 continue;
1174
1175 if (path->walking)
1176 return;
1177
1178 if (path->connect) {
1179 path->walking = 1;
1180 invalidate_paths_ep(path->node[dir], dir);
1181 path->walking = 0;
1182 }
1183 }
1184}
1185
1186/*
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001187 * Common implementation for is_connected_output_ep() and
1188 * is_connected_input_ep(). The function is inlined since the combined size of
1189 * the two specialized functions is only marginally larger then the size of the
1190 * generic function and at the same time the fast path of the specialized
1191 * functions is significantly smaller than the generic function.
1192 */
1193static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1194 struct list_head *list, enum snd_soc_dapm_direction dir,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001195 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1196 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1197 enum snd_soc_dapm_direction)),
1198 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1199 enum snd_soc_dapm_direction))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001200{
1201 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001202 struct snd_soc_dapm_path *path;
1203 int con = 0;
1204
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001205 if (widget->endpoints[dir] >= 0)
1206 return widget->endpoints[dir];
Mark Brown024dc072011-10-09 11:52:05 +01001207
Mark Brownde02d072011-09-20 21:43:24 +01001208 DAPM_UPDATE_STAT(widget, path_checks);
1209
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001210 /* do we need to add this widget to the list ? */
1211 if (list)
1212 list_add_tail(&widget->work_list, list);
1213
Jeeja KP09464972016-06-15 11:16:55 +05301214 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
Charles Keepax8dd26df2019-07-18 09:43:33 +01001215 list = NULL;
1216 custom_stop_condition = NULL;
Jeeja KP09464972016-06-15 11:16:55 +05301217 }
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001218
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001219 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1220 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1221 return widget->endpoints[dir];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001222 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001223
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001224 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
Mark Browne56235e02011-09-21 18:19:14 +01001225 DAPM_UPDATE_STAT(widget, neighbour_checks);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001226
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001227 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +01001228 continue;
1229
Mark Brown8af294b2013-02-22 17:48:15 +00001230 if (path->walking)
1231 return 1;
1232
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001233 trace_snd_soc_dapm_path(widget, dir, path);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001234
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001235 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +00001236 path->walking = 1;
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001237 con += fn(path->node[dir], list, custom_stop_condition);
Mark Brown8af294b2013-02-22 17:48:15 +00001238 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001239 }
1240 }
1241
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001242 widget->endpoints[dir] = con;
Mark Brown024dc072011-10-09 11:52:05 +01001243
Richard Purdie2b97eab2006-10-06 18:32:18 +02001244 return con;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001245}
1246
1247/*
1248 * Recursively check for a completed path to an active or physically connected
1249 * output widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001250 *
1251 * Optionally, can be supplied with a function acting as a stopping condition.
1252 * This function takes the dapm widget currently being examined and the walk
Charles Keepax8dd26df2019-07-18 09:43:33 +01001253 * direction as an arguments, it should return true if widgets from that point
1254 * in the graph onwards should not be added to the widget list.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001255 */
1256static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001257 struct list_head *list,
1258 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1259 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001260{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001261 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001262 is_connected_output_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001263}
1264
1265/*
1266 * Recursively check for a completed path to an active or physically connected
1267 * input widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001268 *
1269 * Optionally, can be supplied with a function acting as a stopping condition.
1270 * This function takes the dapm widget currently being examined and the walk
1271 * direction as an arguments, it should return true if the walk should be
1272 * stopped and false otherwise.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001273 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001274static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001275 struct list_head *list,
1276 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1277 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001278{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001279 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001280 is_connected_input_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001281}
1282
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001283/**
Mauro Carvalho Chehabf7b66032020-10-23 18:33:35 +02001284 * snd_soc_dapm_dai_get_connected_widgets - query audio path and it's widgets.
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001285 * @dai: the soc DAI.
1286 * @stream: stream direction.
1287 * @list: list of active widgets for this stream.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001288 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1289 * walk based on custom logic.
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001290 *
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001291 * Queries DAPM graph as to whether a valid audio stream path exists for
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001292 * the initial stream specified by name. This takes into account
1293 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1294 *
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001295 * Optionally, can be supplied with a function acting as a stopping condition.
1296 * This function takes the dapm widget currently being examined and the walk
1297 * direction as an arguments, it should return true if the walk should be
1298 * stopped and false otherwise.
1299 *
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001300 * Returns the number of valid paths or negative error.
1301 */
1302int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001303 struct snd_soc_dapm_widget_list **list,
1304 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1305 enum snd_soc_dapm_direction))
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001306{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01001307 struct snd_soc_card *card = dai->component->card;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001308 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001309 LIST_HEAD(widgets);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001310 int paths;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001311 int ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001312
1313 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001314
Szymon Mielczarek872f3ac2019-08-09 10:40:34 +02001315 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1316 w = dai->playback_widget;
1317 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_OUT);
1318 paths = is_connected_output_ep(w, &widgets,
1319 custom_stop_condition);
1320 } else {
1321 w = dai->capture_widget;
1322 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_IN);
1323 paths = is_connected_input_ep(w, &widgets,
1324 custom_stop_condition);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001325 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001326
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001327 /* Drop starting point */
1328 list_del(widgets.next);
1329
1330 ret = dapm_widget_list_create(list, &widgets);
1331 if (ret)
Lars-Peter Clausen30abbe72015-08-11 21:37:59 +02001332 paths = ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001333
1334 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001335 mutex_unlock(&card->dapm_mutex);
1336
1337 return paths;
1338}
Ranjani Sridharand1a7af02021-09-27 15:05:10 +03001339EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_get_connected_widgets);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001340
Kuninori Morimoto52645e332020-02-19 15:56:52 +09001341void snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list **list)
1342{
1343 dapm_widget_list_free(list);
1344}
Ranjani Sridharand1a7af02021-09-27 15:05:10 +03001345EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_free_widgets);
Kuninori Morimoto52645e332020-02-19 15:56:52 +09001346
Richard Purdie2b97eab2006-10-06 18:32:18 +02001347/*
Mark Brown62ea8742012-01-21 21:14:48 +00001348 * Handler for regulator supply widget.
1349 */
1350int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1351 struct snd_kcontrol *kcontrol, int event)
1352{
Mark Brownc05b84d2012-09-07 12:57:11 +08001353 int ret;
1354
Mark Browneb270e92013-10-09 13:52:52 +01001355 soc_dapm_async_complete(w->dapm);
1356
Mark Brownc05b84d2012-09-07 12:57:11 +08001357 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001358 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001359 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001360 if (ret != 0)
1361 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001362 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001363 w->name, ret);
1364 }
1365
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001366 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001367 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001368 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001369 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001370 if (ret != 0)
1371 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001372 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001373 w->name, ret);
1374 }
1375
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001376 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001377 }
Mark Brown62ea8742012-01-21 21:14:48 +00001378}
1379EXPORT_SYMBOL_GPL(dapm_regulator_event);
1380
Ola Liljad7e7eb92012-05-24 15:26:25 +02001381/*
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00001382 * Handler for pinctrl widget.
1383 */
1384int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1385 struct snd_kcontrol *kcontrol, int event)
1386{
1387 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1388 struct pinctrl *p = w->pinctrl;
1389 struct pinctrl_state *s;
1390
1391 if (!p || !priv)
1392 return -EIO;
1393
1394 if (SND_SOC_DAPM_EVENT_ON(event))
1395 s = pinctrl_lookup_state(p, priv->active_state);
1396 else
1397 s = pinctrl_lookup_state(p, priv->sleep_state);
1398
1399 if (IS_ERR(s))
1400 return PTR_ERR(s);
1401
1402 return pinctrl_select_state(p, s);
1403}
1404EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1405
1406/*
Ola Liljad7e7eb92012-05-24 15:26:25 +02001407 * Handler for clock supply widget.
1408 */
1409int dapm_clock_event(struct snd_soc_dapm_widget *w,
1410 struct snd_kcontrol *kcontrol, int event)
1411{
1412 if (!w->clk)
1413 return -EIO;
1414
Mark Browneb270e92013-10-09 13:52:52 +01001415 soc_dapm_async_complete(w->dapm);
1416
Ola Liljad7e7eb92012-05-24 15:26:25 +02001417 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001418 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001419 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001420 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001421 return 0;
1422 }
Charles Keepaxd78b1e432018-08-28 14:35:02 +01001423
Marek Belisko98b3cf12012-07-12 23:00:16 +02001424 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001425}
1426EXPORT_SYMBOL_GPL(dapm_clock_event);
1427
Mark Brownd805002b2011-09-28 18:28:23 +01001428static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1429{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001430 if (w->power_checked)
1431 return w->new_power;
1432
Mark Brownd805002b2011-09-28 18:28:23 +01001433 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001434 w->new_power = 1;
Mark Brownd805002b2011-09-28 18:28:23 +01001435 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001436 w->new_power = w->power_check(w);
1437
1438 w->power_checked = true;
1439
1440 return w->new_power;
Mark Brownd805002b2011-09-28 18:28:23 +01001441}
1442
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001443/* Generic check to see if a widget should be powered. */
Mark Browncd0f2d42009-04-20 16:56:59 +01001444static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1445{
1446 int in, out;
1447
Mark Brownde02d072011-09-20 21:43:24 +01001448 DAPM_UPDATE_STAT(w, power_checks);
1449
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001450 in = is_connected_input_ep(w, NULL, NULL);
1451 out = is_connected_output_ep(w, NULL, NULL);
Mark Browncd0f2d42009-04-20 16:56:59 +01001452 return out != 0 && in != 0;
1453}
1454
Mark Brown246d0a12009-04-22 18:24:55 +01001455/* Check to see if a power supply is needed */
1456static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1457{
1458 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001459
Mark Brownde02d072011-09-20 21:43:24 +01001460 DAPM_UPDATE_STAT(w, power_checks);
1461
Mark Brown246d0a12009-04-22 18:24:55 +01001462 /* Check if one of our outputs is connected */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001463 snd_soc_dapm_widget_for_each_sink_path(w, path) {
Mark Browna8fdac82011-09-28 18:20:26 +01001464 DAPM_UPDATE_STAT(w, neighbour_checks);
1465
Mark Brownbf3a9e12011-06-13 16:42:29 +01001466 if (path->weak)
1467 continue;
1468
Mark Brown215edda2009-09-08 18:59:05 +01001469 if (path->connected &&
1470 !path->connected(path->source, path->sink))
1471 continue;
1472
Mark Brownf68d7e12011-10-04 22:57:50 +01001473 if (dapm_widget_power_check(path->sink))
1474 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001475 }
1476
Mark Brownf68d7e12011-10-04 22:57:50 +01001477 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001478}
1479
Mark Brown35c64bc2011-09-28 18:23:53 +01001480static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1481{
Charles Keepax20bb0182015-12-02 10:22:16 +00001482 return w->connected;
Mark Brown35c64bc2011-09-28 18:23:53 +01001483}
1484
Mark Brown38357ab2009-06-06 19:03:23 +01001485static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1486 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001487 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001488{
Mark Brown828a8422011-01-15 13:14:30 +00001489 int *sort;
1490
Pierre-Louis Bossartf13d4b52019-02-05 10:22:28 -06001491 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1492 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1493
Mark Brown828a8422011-01-15 13:14:30 +00001494 if (power_up)
1495 sort = dapm_up_seq;
1496 else
1497 sort = dapm_down_seq;
1498
Pierre-Louis Bossartf13d4b52019-02-05 10:22:28 -06001499 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1500 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1501
Mark Brown38357ab2009-06-06 19:03:23 +01001502 if (sort[a->id] != sort[b->id])
1503 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001504 if (a->subseq != b->subseq) {
1505 if (power_up)
1506 return a->subseq - b->subseq;
1507 else
1508 return b->subseq - a->subseq;
1509 }
Mark Brownb22ead22009-06-07 12:51:26 +01001510 if (a->reg != b->reg)
1511 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001512 if (a->dapm != b->dapm)
1513 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001514
Mark Brown38357ab2009-06-06 19:03:23 +01001515 return 0;
1516}
Mark Brown42aa3412009-03-01 19:21:10 +00001517
Mark Brown38357ab2009-06-06 19:03:23 +01001518/* Insert a widget in order into a DAPM power sequence. */
1519static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1520 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001521 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001522{
1523 struct snd_soc_dapm_widget *w;
1524
1525 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001526 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001527 list_add_tail(&new_widget->power_list, &w->power_list);
1528 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001529 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001530
Mark Brown38357ab2009-06-06 19:03:23 +01001531 list_add_tail(&new_widget->power_list, list);
1532}
Mark Brown42aa3412009-03-01 19:21:10 +00001533
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001534static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001535 struct snd_soc_dapm_widget *w, int event)
1536{
Mark Brown68f89ad2010-11-03 23:51:49 -04001537 const char *ev_name;
Kuninori Morimoto65f73162021-08-05 10:11:20 +09001538 int power;
Mark Brown68f89ad2010-11-03 23:51:49 -04001539
1540 switch (event) {
1541 case SND_SOC_DAPM_PRE_PMU:
1542 ev_name = "PRE_PMU";
1543 power = 1;
1544 break;
1545 case SND_SOC_DAPM_POST_PMU:
1546 ev_name = "POST_PMU";
1547 power = 1;
1548 break;
1549 case SND_SOC_DAPM_PRE_PMD:
1550 ev_name = "PRE_PMD";
1551 power = 0;
1552 break;
1553 case SND_SOC_DAPM_POST_PMD:
1554 ev_name = "POST_PMD";
1555 power = 0;
1556 break;
Mark Brown80114122013-02-25 15:14:19 +00001557 case SND_SOC_DAPM_WILL_PMU:
1558 ev_name = "WILL_PMU";
1559 power = 1;
1560 break;
1561 case SND_SOC_DAPM_WILL_PMD:
1562 ev_name = "WILL_PMD";
1563 power = 0;
1564 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001565 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001566 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001567 return;
1568 }
1569
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001570 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001571 return;
1572
1573 if (w->event && (w->event_flags & event)) {
Kuninori Morimoto65f73162021-08-05 10:11:20 +09001574 int ret;
1575
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001576 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001577 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001578 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001579 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001580 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001581 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001582 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001583 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001584 ev_name, w->name, ret);
1585 }
1586}
1587
Mark Brownb22ead22009-06-07 12:51:26 +01001588/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001589static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001590 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001591{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001592 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001593 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001594 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001595 unsigned int value = 0;
1596 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001597
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001598 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1599 reg = w->reg;
1600 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001601
1602 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001603 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001604 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001605
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001606 mask |= w->mask << w->shift;
1607 if (w->power)
1608 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001609 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001610 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001611
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001612 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001613 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1614 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001615
Mark Brown68f89ad2010-11-03 23:51:49 -04001616 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001617 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1618 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001619 }
1620
Mark Brown81628102009-06-07 13:21:24 +01001621 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001622 /* Any widget will do, they should all be updating the
1623 * same register.
1624 */
Mark Brown29376bc2011-06-19 13:49:28 +01001625
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001626 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001627 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001628 value, mask, reg, card->pop_time);
1629 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001630 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001631 }
1632
1633 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001634 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1635 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001636 }
Mark Brown42aa3412009-03-01 19:21:10 +00001637}
1638
Mark Brownb22ead22009-06-07 12:51:26 +01001639/* Apply a DAPM power sequence.
1640 *
1641 * We walk over a pre-sorted list of widgets to apply power to. In
1642 * order to minimise the number of writes to the device required
1643 * multiple widgets will be updated in a single write where possible.
1644 * Currently anything that requires more than a single write is not
1645 * handled.
1646 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001647static void dapm_seq_run(struct snd_soc_card *card,
1648 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001649{
1650 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001651 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001652 LIST_HEAD(pending);
1653 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001654 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001655 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001656 struct snd_soc_dapm_context *cur_dapm = NULL;
Kuninori Morimotoa7165792021-08-05 10:11:24 +09001657 int i;
Mark Brown828a8422011-01-15 13:14:30 +00001658 int *sort;
1659
1660 if (power_up)
1661 sort = dapm_up_seq;
1662 else
1663 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001664
Mark Brownb22ead22009-06-07 12:51:26 +01001665 list_for_each_entry_safe(w, n, list, power_list) {
Kuninori Morimotoa7165792021-08-05 10:11:24 +09001666 int ret = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001667
1668 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001669 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001670 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001671 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001672 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001673
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001674 if (cur_dapm && cur_dapm->component) {
Mark Brown474b62d2011-01-18 16:14:44 +00001675 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1676 if (sort[i] == cur_sort)
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001677 snd_soc_component_seq_notifier(
1678 cur_dapm->component,
1679 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001680 }
1681
Mark Browneb270e92013-10-09 13:52:52 +01001682 if (cur_dapm && w->dapm != cur_dapm)
1683 soc_dapm_async_complete(cur_dapm);
1684
Mark Brownb22ead22009-06-07 12:51:26 +01001685 INIT_LIST_HEAD(&pending);
1686 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001687 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001688 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001689 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001690 }
1691
Mark Brown163cac02009-06-07 10:12:52 +01001692 switch (w->id) {
1693 case snd_soc_dapm_pre:
1694 if (!w->event)
Xiaomeng Tongf730a462022-03-29 09:21:34 +08001695 continue;
Mark Brown163cac02009-06-07 10:12:52 +01001696
Mark Brownb22ead22009-06-07 12:51:26 +01001697 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001698 ret = w->event(w,
1699 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001700 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001701 ret = w->event(w,
1702 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001703 break;
1704
1705 case snd_soc_dapm_post:
1706 if (!w->event)
Xiaomeng Tongf730a462022-03-29 09:21:34 +08001707 continue;
Mark Brown163cac02009-06-07 10:12:52 +01001708
Mark Brownb22ead22009-06-07 12:51:26 +01001709 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001710 ret = w->event(w,
1711 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001712 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001713 ret = w->event(w,
1714 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001715 break;
1716
Mark Brown163cac02009-06-07 10:12:52 +01001717 default:
Mark Brown81628102009-06-07 13:21:24 +01001718 /* Queue it up for application */
1719 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001720 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001721 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001722 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001723 list_move(&w->power_list, &pending);
1724 break;
Mark Brown163cac02009-06-07 10:12:52 +01001725 }
Mark Brownb22ead22009-06-07 12:51:26 +01001726
1727 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001728 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001729 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001730 }
Mark Brownb22ead22009-06-07 12:51:26 +01001731
1732 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001733 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001734
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001735 if (cur_dapm && cur_dapm->component) {
Mark Brown474b62d2011-01-18 16:14:44 +00001736 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1737 if (sort[i] == cur_sort)
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001738 snd_soc_component_seq_notifier(
1739 cur_dapm->component,
1740 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001741 }
Mark Browneb270e92013-10-09 13:52:52 +01001742
Kuninori Morimotodf817f82020-03-09 13:08:16 +09001743 for_each_card_dapms(card, d)
Mark Browneb270e92013-10-09 13:52:52 +01001744 soc_dapm_async_complete(d);
Mark Brown163cac02009-06-07 10:12:52 +01001745}
1746
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001747static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001748{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001749 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001750 struct snd_soc_dapm_widget_list *wlist;
1751 struct snd_soc_dapm_widget *w = NULL;
1752 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001753 int ret;
1754
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001755 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001756 return;
1757
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001758 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001759
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001760 for_each_dapm_widgets(wlist, wi, w) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001761 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1762 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1763 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001764 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001765 w->name, ret);
1766 }
Mark Brown97404f22010-12-14 16:13:57 +00001767 }
1768
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001769 if (!w)
1770 return;
1771
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001772 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1773 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001774 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001775 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001776 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001777
Chen-Yu Tsaie411b0b2016-11-02 15:35:58 +08001778 if (update->has_second_set) {
1779 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1780 update->mask2, update->val2);
1781 if (ret < 0)
1782 dev_err(w->dapm->dev,
1783 "ASoC: %s DAPM update failed: %d\n",
1784 w->name, ret);
1785 }
1786
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001787 for_each_dapm_widgets(wlist, wi, w) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001788 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1789 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1790 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001791 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001792 w->name, ret);
1793 }
Mark Brown97404f22010-12-14 16:13:57 +00001794 }
1795}
1796
Mark Brown9d0624a2011-02-18 11:49:43 -08001797/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1798 * they're changing state.
1799 */
1800static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1801{
1802 struct snd_soc_dapm_context *d = data;
1803 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001804
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001805 /* If we're off and we're not supposed to go into STANDBY */
Mark Brown56fba412011-06-04 11:25:10 +01001806 if (d->bias_level == SND_SOC_BIAS_OFF &&
1807 d->target_bias_level != SND_SOC_BIAS_OFF) {
Tzung-Bi Shih3aecfc72020-07-24 15:07:31 +08001808 if (d->dev && cookie)
Mark Brownf1aac482011-12-05 15:17:06 +00001809 pm_runtime_get_sync(d->dev);
1810
Mark Brown9d0624a2011-02-18 11:49:43 -08001811 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1812 if (ret != 0)
1813 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001814 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001815 }
1816
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001817 /* Prepare for a transition to ON or away from ON */
1818 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1819 d->bias_level != SND_SOC_BIAS_ON) ||
1820 (d->target_bias_level != SND_SOC_BIAS_ON &&
1821 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001822 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1823 if (ret != 0)
1824 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001825 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001826 }
1827}
1828
1829/* Async callback run prior to DAPM sequences - brings to their final
1830 * state.
1831 */
1832static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1833{
1834 struct snd_soc_dapm_context *d = data;
1835 int ret;
1836
1837 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001838 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1839 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1840 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001841 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1842 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001843 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001844 ret);
1845 }
1846
1847 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001848 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1849 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001850 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1851 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001852 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1853 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001854
Tzung-Bi Shih3aecfc72020-07-24 15:07:31 +08001855 if (d->dev && cookie)
Mark Brownfb644e92012-01-25 19:53:58 +00001856 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001857 }
1858
1859 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001860 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1861 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001862 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1863 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001864 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001865 ret);
1866 }
1867}
Mark Brown97404f22010-12-14 16:13:57 +00001868
Mark Brownfe4fda52011-10-03 22:36:57 +01001869static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1870 bool power, bool connect)
1871{
1872 /* If a connection is being made or broken then that update
1873 * will have marked the peer dirty, otherwise the widgets are
1874 * not connected and this update has no impact. */
1875 if (!connect)
1876 return;
1877
1878 /* If the peer is already in the state we're moving to then we
1879 * won't have an impact on it. */
1880 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001881 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001882}
1883
Mark Brown05623c42011-09-28 17:02:31 +01001884static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1885 struct list_head *up_list,
1886 struct list_head *down_list)
1887{
Mark Browndb432b42011-10-03 21:06:40 +01001888 struct snd_soc_dapm_path *path;
1889
Mark Brown05623c42011-09-28 17:02:31 +01001890 if (w->power == power)
1891 return;
1892
1893 trace_snd_soc_dapm_widget_power(w, power);
1894
Mark Browndb432b42011-10-03 21:06:40 +01001895 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001896 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001897 */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001898 snd_soc_dapm_widget_for_each_source_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001899 dapm_widget_set_peer_power(path->source, power, path->connect);
1900
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001901 /* Supplies can't affect their outputs, only their inputs */
1902 if (!w->is_supply) {
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001903 snd_soc_dapm_widget_for_each_sink_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001904 dapm_widget_set_peer_power(path->sink, power,
1905 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001906 }
1907
Mark Brown05623c42011-09-28 17:02:31 +01001908 if (power)
1909 dapm_seq_insert(w, up_list, true);
1910 else
1911 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001912}
1913
Mark Brown7c81beb2011-09-20 22:22:32 +01001914static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1915 struct list_head *up_list,
1916 struct list_head *down_list)
1917{
Mark Brown7c81beb2011-09-20 22:22:32 +01001918 int power;
1919
1920 switch (w->id) {
1921 case snd_soc_dapm_pre:
1922 dapm_seq_insert(w, down_list, false);
1923 break;
1924 case snd_soc_dapm_post:
1925 dapm_seq_insert(w, up_list, true);
1926 break;
1927
1928 default:
Mark Brownd805002b2011-09-28 18:28:23 +01001929 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001930
Mark Brown05623c42011-09-28 17:02:31 +01001931 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001932 break;
1933 }
1934}
1935
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001936static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1937{
1938 if (dapm->idle_bias_off)
1939 return true;
1940
1941 switch (snd_power_get_state(dapm->card->snd_card)) {
1942 case SNDRV_CTL_POWER_D3hot:
1943 case SNDRV_CTL_POWER_D3cold:
1944 return dapm->suspend_bias_off;
1945 default:
1946 break;
1947 }
1948
1949 return false;
1950}
1951
Mark Brown42aa3412009-03-01 19:21:10 +00001952/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001953 * Scan each dapm widget for complete audio path.
1954 * A complete path is a route that has valid endpoints i.e.:-
1955 *
1956 * o DAC to output pin.
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001957 * o Input pin to ADC.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001958 * o Input pin to Output pin (bypass, sidetone)
1959 * o DAC to ADC (loopback).
1960 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001961static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001962{
1963 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001964 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001965 LIST_HEAD(up_list);
1966 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001967 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001968 enum snd_soc_bias_level bias;
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +09001969 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001970
Mark Brownf9fa2b12014-03-06 16:49:11 +08001971 lockdep_assert_held(&card->dapm_mutex);
1972
Mark Brown84e90932010-11-04 00:07:02 -04001973 trace_snd_soc_dapm_start(card);
1974
Kuninori Morimotodf817f82020-03-09 13:08:16 +09001975 for_each_card_dapms(card, d) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001976 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001977 d->target_bias_level = SND_SOC_BIAS_OFF;
1978 else
1979 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001980 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001981
Liam Girdwood6c120e12012-02-15 15:15:34 +00001982 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001983
Mark Brown6d3ddc82009-05-16 17:47:29 +01001984 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001985 * lists indicating if they should be powered up or down. We
1986 * only check widgets that have been flagged as dirty but note
1987 * that new widgets may be added to the dirty list while we
1988 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001989 */
Mark Browndb432b42011-10-03 21:06:40 +01001990 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001991 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001992 }
1993
Kuninori Morimoto145966922020-03-09 13:08:21 +09001994 for_each_card_widgets(card, w) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001995 switch (w->id) {
1996 case snd_soc_dapm_pre:
1997 case snd_soc_dapm_post:
1998 /* These widgets always need to be powered */
1999 break;
2000 default:
2001 list_del_init(&w->dirty);
2002 break;
2003 }
Mark Browndb432b42011-10-03 21:06:40 +01002004
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02002005 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01002006 d = w->dapm;
2007
2008 /* Supplies and micbiases only bring the
2009 * context up to STANDBY as unless something
2010 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00002011 * generally don't require full power. Signal
2012 * generators are virtual pins and have no
2013 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01002014 */
2015 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00002016 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02002017 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00002018 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01002019 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002020 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00002021 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002022 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01002023 case snd_soc_dapm_micbias:
2024 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
2025 d->target_bias_level = SND_SOC_BIAS_STANDBY;
2026 break;
2027 default:
2028 d->target_bias_level = SND_SOC_BIAS_ON;
2029 break;
2030 }
2031 }
2032
2033 }
2034
Mark Brown85a843c2011-09-21 21:29:47 +01002035 /* Force all contexts in the card to the same bias state if
2036 * they're not ground referenced.
2037 */
Mark Brown56fba412011-06-04 11:25:10 +01002038 bias = SND_SOC_BIAS_OFF;
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002039 for_each_card_dapms(card, d)
Mark Brown56fba412011-06-04 11:25:10 +01002040 if (d->target_bias_level > bias)
2041 bias = d->target_bias_level;
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002042 for_each_card_dapms(card, d)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02002043 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01002044 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09002045
Mark Brownde02d072011-09-20 21:43:24 +01002046 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09002047
Xiang Xiao17282ba2014-03-02 00:04:03 +08002048 /* Run card bias changes at first */
2049 dapm_pre_sequence_async(&card->dapm, 0);
2050 /* Run other bias changes in parallel */
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002051 for_each_card_dapms(card, d) {
Jon Huntere03546d2018-08-17 16:35:43 +01002052 if (d != &card->dapm && d->bias_level != d->target_bias_level)
Xiang Xiao17282ba2014-03-02 00:04:03 +08002053 async_schedule_domain(dapm_pre_sequence_async, d,
2054 &async_domain);
2055 }
Mark Brown9d0624a2011-02-18 11:49:43 -08002056 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01002057
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02002058 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002059 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00002060 }
2061
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02002062 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002063 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00002064 }
2065
Mark Brown6d3ddc82009-05-16 17:47:29 +01002066 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002067 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01002068
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002069 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00002070
Mark Brown6d3ddc82009-05-16 17:47:29 +01002071 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002072 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002073
Mark Brown9d0624a2011-02-18 11:49:43 -08002074 /* Run all the bias changes in parallel */
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002075 for_each_card_dapms(card, d) {
Jon Huntere03546d2018-08-17 16:35:43 +01002076 if (d != &card->dapm && d->bias_level != d->target_bias_level)
Xiang Xiao17282ba2014-03-02 00:04:03 +08002077 async_schedule_domain(dapm_post_sequence_async, d,
2078 &async_domain);
2079 }
Mark Brown9d0624a2011-02-18 11:49:43 -08002080 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08002081 /* Run card bias changes at last */
2082 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01002083
Liam Girdwood8078d872012-02-15 15:15:35 +00002084 /* do we need to notify any clients that DAPM event is complete */
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002085 for_each_card_dapms(card, d) {
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +09002086 if (!d->component)
2087 continue;
2088
2089 ret = snd_soc_component_stream_event(d->component, event);
2090 if (ret < 0)
2091 return ret;
Liam Girdwood8078d872012-02-15 15:15:35 +00002092 }
2093
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002094 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02002095 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02002096 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01002097
Mark Brown84e90932010-11-04 00:07:02 -04002098 trace_snd_soc_dapm_done(card);
2099
Mark Brown42aa3412009-03-01 19:21:10 +00002100 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002101}
2102
Mark Brown79fb9382009-08-21 16:38:13 +01002103#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01002104static ssize_t dapm_widget_power_read_file(struct file *file,
2105 char __user *user_buf,
2106 size_t count, loff_t *ppos)
2107{
2108 struct snd_soc_dapm_widget *w = file->private_data;
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002109 struct snd_soc_card *card = w->dapm->card;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002110 enum snd_soc_dapm_direction dir, rdir;
Mark Brown79fb9382009-08-21 16:38:13 +01002111 char *buf;
2112 int in, out;
2113 ssize_t ret;
2114 struct snd_soc_dapm_path *p = NULL;
2115
2116 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2117 if (!buf)
2118 return -ENOMEM;
2119
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002120 mutex_lock(&card->dapm_mutex);
2121
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002122 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2123 if (w->is_supply) {
2124 in = 0;
2125 out = 0;
2126 } else {
Piotr Stankiewicz67420642016-05-13 17:03:55 +01002127 in = is_connected_input_ep(w, NULL, NULL);
2128 out = is_connected_output_ep(w, NULL, NULL);
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002129 }
Mark Brown79fb9382009-08-21 16:38:13 +01002130
Silvio Cesaree581e152019-01-12 16:28:43 +01002131 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
Mark Brownf13ebad2012-03-03 18:01:01 +00002132 w->name, w->power ? "On" : "Off",
2133 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01002134
Mark Brownd033c362009-12-04 15:25:56 +00002135 if (w->reg >= 0)
Silvio Cesaree581e152019-01-12 16:28:43 +01002136 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002137 " - R%d(0x%x) mask 0x%x",
2138 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00002139
Silvio Cesaree581e152019-01-12 16:28:43 +01002140 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
Mark Brownd033c362009-12-04 15:25:56 +00002141
Mark Brown3eef08b2009-09-14 16:49:00 +01002142 if (w->sname)
Silvio Cesaree581e152019-01-12 16:28:43 +01002143 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
Mark Brown3eef08b2009-09-14 16:49:00 +01002144 w->sname,
2145 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01002146
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002147 snd_soc_dapm_for_each_direction(dir) {
2148 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2149 snd_soc_dapm_widget_for_each_path(w, dir, p) {
KaiChieh Chuang28735af2018-02-05 13:00:00 +08002150 if (p->connected && !p->connected(p->source, p->sink))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002151 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002152
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002153 if (!p->connect)
2154 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002155
Silvio Cesaree581e152019-01-12 16:28:43 +01002156 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002157 " %s \"%s\" \"%s\"\n",
2158 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
Mark Brown79fb9382009-08-21 16:38:13 +01002159 p->name ? p->name : "static",
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002160 p->node[rdir]->name);
2161 }
Mark Brown79fb9382009-08-21 16:38:13 +01002162 }
2163
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002164 mutex_unlock(&card->dapm_mutex);
2165
Mark Brown79fb9382009-08-21 16:38:13 +01002166 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2167
2168 kfree(buf);
2169 return ret;
2170}
2171
2172static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002173 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002174 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002175 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002176};
2177
Mark Brownef49e4f2011-04-04 20:48:13 +09002178static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2179 size_t count, loff_t *ppos)
2180{
2181 struct snd_soc_dapm_context *dapm = file->private_data;
2182 char *level;
2183
2184 switch (dapm->bias_level) {
2185 case SND_SOC_BIAS_ON:
2186 level = "On\n";
2187 break;
2188 case SND_SOC_BIAS_PREPARE:
2189 level = "Prepare\n";
2190 break;
2191 case SND_SOC_BIAS_STANDBY:
2192 level = "Standby\n";
2193 break;
2194 case SND_SOC_BIAS_OFF:
2195 level = "Off\n";
2196 break;
2197 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002198 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002199 level = "Unknown\n";
2200 break;
2201 }
2202
2203 return simple_read_from_buffer(user_buf, count, ppos, level,
2204 strlen(level));
2205}
2206
2207static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002208 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002209 .read = dapm_bias_read_file,
2210 .llseek = default_llseek,
2211};
2212
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002213void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2214 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002215{
Mark Brownceaea852019-06-21 12:33:57 +01002216 if (!parent || IS_ERR(parent))
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02002217 return;
2218
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002219 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2220
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +02002221 debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
2222 &dapm_bias_fops);
Mark Brown79fb9382009-08-21 16:38:13 +01002223}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002224
2225static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2226{
2227 struct snd_soc_dapm_context *dapm = w->dapm;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002228
2229 if (!dapm->debugfs_dapm || !w->name)
2230 return;
2231
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +02002232 debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
2233 &dapm_widget_power_fops);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002234}
2235
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002236static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2237{
2238 debugfs_remove_recursive(dapm->debugfs_dapm);
Kuninori Morimoto29040d12019-05-27 16:51:34 +09002239 dapm->debugfs_dapm = NULL;
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002240}
2241
Mark Brown79fb9382009-08-21 16:38:13 +01002242#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002243void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2244 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002245{
2246}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002247
2248static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2249{
2250}
2251
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002252static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2253{
2254}
2255
Mark Brown79fb9382009-08-21 16:38:13 +01002256#endif
2257
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002258/*
2259 * soc_dapm_connect_path() - Connects or disconnects a path
2260 * @path: The path to update
2261 * @connect: The new connect state of the path. True if the path is connected,
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002262 * false if it is disconnected.
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002263 * @reason: The reason why the path changed (for debugging only)
2264 */
2265static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2266 bool connect, const char *reason)
2267{
2268 if (path->connect == connect)
2269 return;
2270
2271 path->connect = connect;
2272 dapm_mark_dirty(path->source, reason);
2273 dapm_mark_dirty(path->sink, reason);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002274 dapm_path_invalidate(path);
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002275}
2276
Richard Purdie2b97eab2006-10-06 18:32:18 +02002277/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002278static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002279 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002280{
2281 struct snd_soc_dapm_path *path;
2282 int found = 0;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002283 bool connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002284
Mark Brownf9fa2b12014-03-06 16:49:11 +08002285 lockdep_assert_held(&card->dapm_mutex);
2286
Richard Purdie2b97eab2006-10-06 18:32:18 +02002287 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002288 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002289 found = 1;
2290 /* we now need to match the string in the enum to the path */
Jerome Brunetc3456a42019-05-15 15:18:55 +02002291 if (e && !(strcmp(path->name, e->texts[mux])))
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002292 connect = true;
2293 else
2294 connect = false;
2295
2296 soc_dapm_connect_path(path, connect, "mux update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002297 }
2298
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002299 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002300 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002301
Liam Girdwood618dae12012-04-25 12:12:51 +01002302 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002303}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002304
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002305int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002306 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2307 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002308{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002309 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002310 int ret;
2311
Liam Girdwood3cd04342012-03-09 12:02:08 +00002312 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002313 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002314 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002315 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002316 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002317 if (ret > 0)
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01002318 snd_soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002319 return ret;
2320}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002321EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002322
Milan plzik1b075e32008-01-10 14:39:46 +01002323/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002324static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002325 struct snd_kcontrol *kcontrol,
2326 int connect, int rconnect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002327{
2328 struct snd_soc_dapm_path *path;
2329 int found = 0;
2330
Mark Brownf9fa2b12014-03-06 16:49:11 +08002331 lockdep_assert_held(&card->dapm_mutex);
2332
Richard Purdie2b97eab2006-10-06 18:32:18 +02002333 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002334 dapm_kcontrol_for_each_path(path, kcontrol) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002335 /*
2336 * Ideally this function should support any number of
2337 * paths and channels. But since kcontrols only come
2338 * in mono and stereo variants, we are limited to 2
2339 * channels.
2340 *
2341 * The following code assumes for stereo controls the
2342 * first path (when 'found == 0') is the left channel,
2343 * and all remaining paths (when 'found == 1') are the
2344 * right channel.
2345 *
2346 * A stereo control is signified by a valid 'rconnect'
2347 * value, either 0 for unconnected, or >= 0 for connected.
2348 * This is chosen instead of using snd_soc_volsw_is_stereo,
2349 * so that the behavior of snd_soc_dapm_mixer_update_power
2350 * doesn't change even when the kcontrol passed in is
2351 * stereo.
2352 *
2353 * It passes 'connect' as the path connect status for
2354 * the left channel, and 'rconnect' for the right
2355 * channel.
2356 */
2357 if (found && rconnect >= 0)
2358 soc_dapm_connect_path(path, rconnect, "mixer update");
2359 else
2360 soc_dapm_connect_path(path, connect, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002361 found = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002362 }
2363
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002364 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002365 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002366
Liam Girdwood618dae12012-04-25 12:12:51 +01002367 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002368}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002369
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002370int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002371 struct snd_kcontrol *kcontrol, int connect,
2372 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002373{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002374 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002375 int ret;
2376
Liam Girdwood3cd04342012-03-09 12:02:08 +00002377 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002378 card->update = update;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002379 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002380 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002381 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002382 if (ret > 0)
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01002383 snd_soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002384 return ret;
2385}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002386EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002387
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002388static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2389 char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002390{
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002391 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002392 struct snd_soc_dapm_widget *w;
2393 int count = 0;
2394 char *state = "not set";
2395
Mark Brown47325072016-03-18 12:04:23 +00002396 /* card won't be set for the dummy component, as a spot fix
2397 * we're checking for that case specifically here but in future
2398 * we will ensure that the dummy component looks like others.
2399 */
2400 if (!cmpnt->card)
2401 return 0;
2402
Kuninori Morimoto145966922020-03-09 13:08:21 +09002403 for_each_card_widgets(cmpnt->card, w) {
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002404 if (w->dapm != dapm)
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002405 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002406
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002407 /* only display widgets that burn power */
Richard Purdie2b97eab2006-10-06 18:32:18 +02002408 switch (w->id) {
2409 case snd_soc_dapm_hp:
2410 case snd_soc_dapm_mic:
2411 case snd_soc_dapm_spk:
2412 case snd_soc_dapm_line:
2413 case snd_soc_dapm_micbias:
2414 case snd_soc_dapm_dac:
2415 case snd_soc_dapm_adc:
2416 case snd_soc_dapm_pga:
Seppo Ingalsuo882c8b4a2019-04-30 18:11:34 -05002417 case snd_soc_dapm_effect:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002418 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002419 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002420 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002421 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002422 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00002423 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002424 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002425 if (w->name)
2426 count += sprintf(buf + count, "%s: %s\n",
2427 w->name, w->power ? "On":"Off");
2428 break;
2429 default:
2430 break;
2431 }
2432 }
2433
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002434 switch (snd_soc_dapm_get_bias_level(dapm)) {
Mark Brown0be98982008-05-19 12:31:28 +02002435 case SND_SOC_BIAS_ON:
2436 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002437 break;
Mark Brown0be98982008-05-19 12:31:28 +02002438 case SND_SOC_BIAS_PREPARE:
2439 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002440 break;
Mark Brown0be98982008-05-19 12:31:28 +02002441 case SND_SOC_BIAS_STANDBY:
2442 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002443 break;
Mark Brown0be98982008-05-19 12:31:28 +02002444 case SND_SOC_BIAS_OFF:
2445 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002446 break;
2447 }
2448 count += sprintf(buf + count, "PM State: %s\n", state);
2449
2450 return count;
2451}
2452
Benoit Cousson44ba2642014-07-08 23:19:36 +02002453/* show dapm widget status in sys fs */
2454static ssize_t dapm_widget_show(struct device *dev,
2455 struct device_attribute *attr, char *buf)
2456{
2457 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Kuninori Morimoto0b7990e32018-09-03 02:12:56 +00002458 struct snd_soc_dai *codec_dai;
Benoit Cousson44ba2642014-07-08 23:19:36 +02002459 int i, count = 0;
2460
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002461 mutex_lock(&rtd->card->dapm_mutex);
2462
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09002463 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Kuninori Morimoto0b7990e32018-09-03 02:12:56 +00002464 struct snd_soc_component *cmpnt = codec_dai->component;
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002465
2466 count += dapm_widget_show_component(cmpnt, buf + count);
Benoit Cousson44ba2642014-07-08 23:19:36 +02002467 }
2468
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002469 mutex_unlock(&rtd->card->dapm_mutex);
2470
Benoit Cousson44ba2642014-07-08 23:19:36 +02002471 return count;
2472}
2473
Joe Perchesc828a892017-12-19 10:15:08 -08002474static DEVICE_ATTR_RO(dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002475
Takashi Iwaid29697d2015-01-30 20:16:37 +01002476struct attribute *soc_dapm_dev_attrs[] = {
2477 &dev_attr_dapm_widget.attr,
2478 NULL
2479};
Richard Purdie2b97eab2006-10-06 18:32:18 +02002480
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002481static void dapm_free_path(struct snd_soc_dapm_path *path)
2482{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002483 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2484 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002485 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002486 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002487 kfree(path);
2488}
2489
Cezary Rojewskida039802022-03-11 16:35:29 +01002490/**
2491 * snd_soc_dapm_free_widget - Free specified widget
2492 * @w: widget to free
2493 *
2494 * Removes widget from all paths and frees memory occupied by it.
2495 */
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002496void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2497{
2498 struct snd_soc_dapm_path *p, *next_p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002499 enum snd_soc_dapm_direction dir;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002500
2501 list_del(&w->list);
Thomas Hebb5c6679b2020-12-12 17:20:12 -08002502 list_del(&w->dirty);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002503 /*
2504 * remove source and sink paths associated to this widget.
2505 * While removing the path, remove reference to it from both
2506 * source and sink widgets so that path is removed only once.
2507 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002508 snd_soc_dapm_for_each_direction(dir) {
2509 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2510 dapm_free_path(p);
2511 }
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002512
2513 kfree(w->kcontrols);
Lars-Peter Clausen48068962015-07-21 18:11:08 +02002514 kfree_const(w->name);
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -06002515 kfree_const(w->sname);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002516 kfree(w);
2517}
Cezary Rojewskida039802022-03-11 16:35:29 +01002518EXPORT_SYMBOL_GPL(snd_soc_dapm_free_widget);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002519
Jyri Sarhafd589a12015-11-10 18:12:42 +02002520void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2521{
2522 dapm->path_sink_cache.widget = NULL;
2523 dapm->path_source_cache.widget = NULL;
2524}
2525
Richard Purdie2b97eab2006-10-06 18:32:18 +02002526/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002527static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002528{
2529 struct snd_soc_dapm_widget *w, *next_w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002530
Kuninori Morimoto145966922020-03-09 13:08:21 +09002531 for_each_card_widgets_safe(dapm->card, w, next_w) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002532 if (w->dapm != dapm)
2533 continue;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002534 snd_soc_dapm_free_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002535 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002536 snd_soc_dapm_reset_cache(dapm);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002537}
2538
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002539static struct snd_soc_dapm_widget *dapm_find_widget(
2540 struct snd_soc_dapm_context *dapm, const char *pin,
2541 bool search_other_contexts)
2542{
2543 struct snd_soc_dapm_widget *w;
2544 struct snd_soc_dapm_widget *fallback = NULL;
Shuming Fanae4fc532021-02-08 17:40:42 -06002545 char prefixed_pin[80];
2546 const char *pin_name;
2547 const char *prefix = soc_dapm_prefix(dapm);
2548
2549 if (prefix) {
2550 snprintf(prefixed_pin, sizeof(prefixed_pin), "%s %s",
2551 prefix, pin);
2552 pin_name = prefixed_pin;
2553 } else {
2554 pin_name = pin;
2555 }
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002556
Kuninori Morimoto145966922020-03-09 13:08:21 +09002557 for_each_card_widgets(dapm->card, w) {
Shuming Fanae4fc532021-02-08 17:40:42 -06002558 if (!strcmp(w->name, pin_name)) {
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002559 if (w->dapm == dapm)
2560 return w;
2561 else
2562 fallback = w;
2563 }
2564 }
2565
2566 if (search_other_contexts)
2567 return fallback;
2568
2569 return NULL;
2570}
2571
Takashi Iwai827b0912021-11-05 10:09:25 +01002572/*
2573 * set the DAPM pin status:
2574 * returns 1 when the value has been updated, 0 when unchanged, or a negative
2575 * error code; called from kcontrol put callback
2576 */
2577static int __snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2578 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002579{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002580 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Takashi Iwai5af82c82021-10-06 16:17:12 +02002581 int ret = 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002582
Mark Brownf9fa2b12014-03-06 16:49:11 +08002583 dapm_assert_locked(dapm);
2584
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002585 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002586 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002587 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002588 }
2589
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002590 if (w->connected != status) {
Mark Brown1a8b2d92012-02-16 11:50:07 -08002591 dapm_mark_dirty(w, "pin configuration");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002592 dapm_widget_invalidate_input_paths(w);
2593 dapm_widget_invalidate_output_paths(w);
Takashi Iwai5af82c82021-10-06 16:17:12 +02002594 ret = 1;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002595 }
Mark Brown1a8b2d92012-02-16 11:50:07 -08002596
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002597 w->connected = status;
2598 if (status == 0)
2599 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002600
Takashi Iwai5af82c82021-10-06 16:17:12 +02002601 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01002602}
2603
Takashi Iwai827b0912021-11-05 10:09:25 +01002604/*
2605 * similar as __snd_soc_dapm_set_pin(), but returns 0 when successful;
2606 * called from several API functions below
2607 */
2608static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2609 const char *pin, int status)
2610{
2611 int ret = __snd_soc_dapm_set_pin(dapm, pin, status);
2612
2613 return ret < 0 ? ret : 0;
2614}
2615
Richard Purdie2b97eab2006-10-06 18:32:18 +02002616/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002617 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2618 * @dapm: DAPM context
2619 *
2620 * Walks all dapm audio paths and powers widgets according to their
2621 * stream or path usage.
2622 *
2623 * Requires external locking.
2624 *
2625 * Returns 0 for success.
2626 */
2627int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2628{
2629 /*
2630 * Suppress early reports (eg, jacks syncing their state) to avoid
2631 * silly DAPM runs during card startup.
2632 */
2633 if (!dapm->card || !dapm->card->instantiated)
2634 return 0;
2635
2636 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2637}
2638EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2639
2640/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002641 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002642 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002643 *
2644 * Walks all dapm audio paths and powers widgets according to their
2645 * stream or path usage.
2646 *
2647 * Returns 0 for success.
2648 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002649int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002650{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002651 int ret;
2652
Liam Girdwood3cd04342012-03-09 12:02:08 +00002653 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002654 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002655 mutex_unlock(&dapm->card->dapm_mutex);
2656 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002657}
Liam Girdwooda5302182008-07-07 13:35:17 +01002658EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002659
Charles Keepax078a85f2019-01-31 13:30:18 +00002660static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2661 struct snd_soc_dapm_widget *w,
2662 int channels)
2663{
2664 switch (w->id) {
2665 case snd_soc_dapm_aif_out:
2666 case snd_soc_dapm_aif_in:
2667 break;
2668 default:
2669 return 0;
2670 }
2671
2672 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2673 w->channel < channels ? "Connecting" : "Disconnecting",
2674 p->source->name, p->sink->name);
2675
2676 if (w->channel < channels)
2677 soc_dapm_connect_path(p, true, "dai update");
2678 else
2679 soc_dapm_connect_path(p, false, "dai update");
2680
2681 return 0;
2682}
2683
2684static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2685 struct snd_pcm_hw_params *params,
2686 struct snd_soc_dai *dai)
2687{
2688 int dir = substream->stream;
2689 int channels = params_channels(params);
2690 struct snd_soc_dapm_path *p;
2691 struct snd_soc_dapm_widget *w;
2692 int ret;
2693
Kuninori Morimoto0c01f6c2020-02-19 15:56:41 +09002694 w = snd_soc_dai_get_widget(dai, dir);
Charles Keepax078a85f2019-01-31 13:30:18 +00002695
Charles Keepaxcf17a5f2019-02-06 11:13:59 +00002696 if (!w)
2697 return 0;
2698
Charles Keepax078a85f2019-01-31 13:30:18 +00002699 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2700 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2701
2702 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2703 ret = dapm_update_dai_chan(p, p->sink, channels);
2704 if (ret < 0)
2705 return ret;
2706 }
2707
2708 snd_soc_dapm_widget_for_each_source_path(w, p) {
2709 ret = dapm_update_dai_chan(p, p->source, channels);
2710 if (ret < 0)
2711 return ret;
2712 }
2713
2714 return 0;
2715}
2716
2717int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2718 struct snd_pcm_hw_params *params,
2719 struct snd_soc_dai *dai)
2720{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09002721 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Charles Keepax078a85f2019-01-31 13:30:18 +00002722 int ret;
2723
2724 mutex_lock_nested(&rtd->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2725 ret = dapm_update_dai_unlocked(substream, params, dai);
2726 mutex_unlock(&rtd->card->dapm_mutex);
2727
2728 return ret;
2729}
2730EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2731
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002732/*
2733 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2734 * @w: The widget for which to update the flags
2735 *
2736 * Some widgets have a dynamic category which depends on which neighbors they
2737 * are connected to. This function update the category for these widgets.
2738 *
2739 * This function must be called whenever a path is added or removed to a widget.
2740 */
2741static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2742{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002743 enum snd_soc_dapm_direction dir;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002744 struct snd_soc_dapm_path *p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002745 unsigned int ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002746
2747 switch (w->id) {
2748 case snd_soc_dapm_input:
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002749 /* On a fully routed card an input is never a source */
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002750 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002751 return;
2752 ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002753 snd_soc_dapm_widget_for_each_source_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002754 if (p->source->id == snd_soc_dapm_micbias ||
2755 p->source->id == snd_soc_dapm_mic ||
2756 p->source->id == snd_soc_dapm_line ||
2757 p->source->id == snd_soc_dapm_output) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002758 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002759 break;
2760 }
2761 }
2762 break;
2763 case snd_soc_dapm_output:
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002764 /* On a fully routed card a output is never a sink */
2765 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002766 return;
2767 ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002768 snd_soc_dapm_widget_for_each_sink_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002769 if (p->sink->id == snd_soc_dapm_spk ||
2770 p->sink->id == snd_soc_dapm_hp ||
2771 p->sink->id == snd_soc_dapm_line ||
2772 p->sink->id == snd_soc_dapm_input) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002773 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002774 break;
2775 }
2776 }
2777 break;
2778 case snd_soc_dapm_line:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002779 ep = 0;
2780 snd_soc_dapm_for_each_direction(dir) {
2781 if (!list_empty(&w->edges[dir]))
2782 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2783 }
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002784 break;
2785 default:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002786 return;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002787 }
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002788
2789 w->is_ep = ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002790}
2791
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002792static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2793 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2794 const char *control)
2795{
2796 bool dynamic_source = false;
2797 bool dynamic_sink = false;
2798
2799 if (!control)
2800 return 0;
2801
2802 switch (source->id) {
2803 case snd_soc_dapm_demux:
2804 dynamic_source = true;
2805 break;
2806 default:
2807 break;
2808 }
2809
2810 switch (sink->id) {
2811 case snd_soc_dapm_mux:
2812 case snd_soc_dapm_switch:
2813 case snd_soc_dapm_mixer:
2814 case snd_soc_dapm_mixer_named_ctl:
2815 dynamic_sink = true;
2816 break;
2817 default:
2818 break;
2819 }
2820
2821 if (dynamic_source && dynamic_sink) {
2822 dev_err(dapm->dev,
2823 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2824 source->name, control, sink->name);
2825 return -EINVAL;
2826 } else if (!dynamic_source && !dynamic_sink) {
2827 dev_err(dapm->dev,
2828 "Control not supported for path %s -> [%s] -> %s\n",
2829 source->name, control, sink->name);
2830 return -EINVAL;
2831 }
2832
2833 return 0;
2834}
2835
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002836static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2837 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2838 const char *control,
2839 int (*connected)(struct snd_soc_dapm_widget *source,
2840 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002841{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002842 struct snd_soc_dapm_widget *widgets[2];
2843 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002844 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002845 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002846
Lars-Peter Clausene409dfb2014-10-25 17:42:02 +02002847 if (wsink->is_supply && !wsource->is_supply) {
2848 dev_err(dapm->dev,
2849 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2850 wsource->name, wsink->name);
2851 return -EINVAL;
2852 }
2853
2854 if (connected && !wsource->is_supply) {
2855 dev_err(dapm->dev,
2856 "connected() callback only supported for supply widgets (%s -> %s)\n",
2857 wsource->name, wsink->name);
2858 return -EINVAL;
2859 }
2860
2861 if (wsource->is_supply && control) {
2862 dev_err(dapm->dev,
2863 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2864 wsource->name, control, wsink->name);
2865 return -EINVAL;
2866 }
2867
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002868 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2869 if (ret)
2870 return ret;
2871
Richard Purdie2b97eab2006-10-06 18:32:18 +02002872 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2873 if (!path)
2874 return -ENOMEM;
2875
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002876 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2877 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2878 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2879 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2880
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002881 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002882 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002883 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002884
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002885 if (wsource->is_supply || wsink->is_supply)
2886 path->is_supply = 1;
2887
Richard Purdie2b97eab2006-10-06 18:32:18 +02002888 /* connect static paths */
2889 if (control == NULL) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002890 path->connect = 1;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002891 } else {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002892 switch (wsource->id) {
2893 case snd_soc_dapm_demux:
2894 ret = dapm_connect_mux(dapm, path, control, wsource);
2895 if (ret)
2896 goto err;
2897 break;
2898 default:
2899 break;
2900 }
2901
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002902 switch (wsink->id) {
2903 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002904 ret = dapm_connect_mux(dapm, path, control, wsink);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002905 if (ret != 0)
2906 goto err;
2907 break;
2908 case snd_soc_dapm_switch:
2909 case snd_soc_dapm_mixer:
2910 case snd_soc_dapm_mixer_named_ctl:
2911 ret = dapm_connect_mixer(dapm, path, control);
2912 if (ret != 0)
2913 goto err;
2914 break;
2915 default:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002916 break;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002917 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002918 }
2919
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002920 list_add(&path->list, &dapm->card->paths);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002921 snd_soc_dapm_for_each_direction(dir)
2922 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002923
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002924 snd_soc_dapm_for_each_direction(dir) {
2925 dapm_update_widget_flags(widgets[dir]);
2926 dapm_mark_dirty(widgets[dir], "Route added");
2927 }
Mark Brownfabd0382012-07-05 17:20:06 +01002928
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002929 if (dapm->card->instantiated && path->connect)
2930 dapm_path_invalidate(path);
2931
Richard Purdie2b97eab2006-10-06 18:32:18 +02002932 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002933err:
2934 kfree(path);
2935 return ret;
2936}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002937
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002938static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002939 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002940{
2941 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2942 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2943 const char *sink;
2944 const char *source;
2945 char prefixed_sink[80];
2946 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002947 const char *prefix;
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002948 unsigned int sink_ref = 0;
2949 unsigned int source_ref = 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002950 int ret;
2951
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002952 prefix = soc_dapm_prefix(dapm);
2953 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002954 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002955 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002956 sink = prefixed_sink;
2957 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002958 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002959 source = prefixed_source;
2960 } else {
2961 sink = route->sink;
2962 source = route->source;
2963 }
2964
Charles Keepax45a110a2015-05-11 13:50:30 +01002965 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2966 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2967
2968 if (wsink && wsource)
2969 goto skip_search;
2970
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002971 /*
2972 * find src and dest widgets over all widgets but favor a widget from
2973 * current DAPM context
2974 */
Kuninori Morimoto145966922020-03-09 13:08:21 +09002975 for_each_card_widgets(dapm->card, w) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002976 if (!wsink && !(strcmp(w->name, sink))) {
2977 wtsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002978 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002979 wsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002980 if (wsource)
2981 break;
2982 }
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002983 sink_ref++;
2984 if (sink_ref > 1)
2985 dev_warn(dapm->dev,
2986 "ASoC: sink widget %s overwritten\n",
2987 w->name);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002988 continue;
2989 }
2990 if (!wsource && !(strcmp(w->name, source))) {
2991 wtsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002992 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002993 wsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002994 if (wsink)
2995 break;
2996 }
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002997 source_ref++;
2998 if (source_ref > 1)
2999 dev_warn(dapm->dev,
3000 "ASoC: source widget %s overwritten\n",
3001 w->name);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003002 }
3003 }
3004 /* use widget from another DAPM context if not found from this */
3005 if (!wsink)
3006 wsink = wtsink;
3007 if (!wsource)
3008 wsource = wtsource;
3009
3010 if (wsource == NULL) {
3011 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
3012 route->source);
3013 return -ENODEV;
3014 }
3015 if (wsink == NULL) {
3016 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
3017 route->sink);
3018 return -ENODEV;
3019 }
3020
Charles Keepax45a110a2015-05-11 13:50:30 +01003021skip_search:
3022 dapm_wcache_update(&dapm->path_sink_cache, wsink);
3023 dapm_wcache_update(&dapm->path_source_cache, wsource);
3024
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003025 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
3026 route->connected);
3027 if (ret)
3028 goto err;
3029
3030 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003031err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003032 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003033 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003034 return ret;
3035}
Mark Brown105f1c22008-05-13 14:52:19 +02003036
Mark Brownefcc3c62012-07-05 17:24:19 +01003037static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
3038 const struct snd_soc_dapm_route *route)
3039{
3040 struct snd_soc_dapm_path *path, *p;
3041 const char *sink;
3042 const char *source;
3043 char prefixed_sink[80];
3044 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003045 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01003046
3047 if (route->control) {
3048 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003049 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01003050 return -EINVAL;
3051 }
3052
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003053 prefix = soc_dapm_prefix(dapm);
3054 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01003055 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003056 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01003057 sink = prefixed_sink;
3058 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003059 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01003060 source = prefixed_source;
3061 } else {
3062 sink = route->sink;
3063 source = route->source;
3064 }
3065
3066 path = NULL;
3067 list_for_each_entry(p, &dapm->card->paths, list) {
3068 if (strcmp(p->source->name, source) != 0)
3069 continue;
3070 if (strcmp(p->sink->name, sink) != 0)
3071 continue;
3072 path = p;
3073 break;
3074 }
3075
3076 if (path) {
Kuninori Morimotofd5ad232021-08-05 10:11:28 +09003077 struct snd_soc_dapm_widget *wsource = path->source;
3078 struct snd_soc_dapm_widget *wsink = path->sink;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003079
3080 dapm_mark_dirty(wsource, "Route removed");
3081 dapm_mark_dirty(wsink, "Route removed");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003082 if (path->connect)
3083 dapm_path_invalidate(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01003084
Lars-Peter Clausen88722932013-06-14 13:16:53 +02003085 dapm_free_path(path);
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003086
3087 /* Update any path related flags */
3088 dapm_update_widget_flags(wsource);
3089 dapm_update_widget_flags(wsink);
Mark Brownefcc3c62012-07-05 17:24:19 +01003090 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003091 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01003092 source, sink);
3093 }
3094
3095 return 0;
3096}
3097
Mark Brown105f1c22008-05-13 14:52:19 +02003098/**
Mark Brown105f1c22008-05-13 14:52:19 +02003099 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003100 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02003101 * @route: audio routes
3102 * @num: number of routes
3103 *
3104 * Connects 2 dapm widgets together via a named audio path. The sink is
3105 * the widget receiving the audio signal, whilst the source is the sender
3106 * of the audio signal.
3107 *
3108 * Returns 0 for success else error. On error all resources can be freed
3109 * with a call to snd_soc_card_free().
3110 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003111int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02003112 const struct snd_soc_dapm_route *route, int num)
3113{
Kuninori Morimotofcb3f192021-08-05 10:11:33 +09003114 int i, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02003115
Stuart Hendersonf19c1812017-09-21 11:02:12 +01003116 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown105f1c22008-05-13 14:52:19 +02003117 for (i = 0; i < num; i++) {
Kuninori Morimotofcb3f192021-08-05 10:11:33 +09003118 int r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01003119 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003120 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
3121 route->source,
3122 route->control ? route->control : "direct",
3123 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01003124 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02003125 }
3126 route++;
3127 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003128 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02003129
Dan Carpenter60884c22012-04-13 22:25:43 +03003130 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02003131}
3132EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3133
Mark Brownefcc3c62012-07-05 17:24:19 +01003134/**
3135 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3136 * @dapm: DAPM context
3137 * @route: audio routes
3138 * @num: number of routes
3139 *
3140 * Removes routes from the DAPM context.
3141 */
3142int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3143 const struct snd_soc_dapm_route *route, int num)
3144{
Rajan Vajae066ea22016-02-11 11:23:35 +05303145 int i;
Mark Brownefcc3c62012-07-05 17:24:19 +01003146
Stuart Hendersonf19c1812017-09-21 11:02:12 +01003147 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownefcc3c62012-07-05 17:24:19 +01003148 for (i = 0; i < num; i++) {
3149 snd_soc_dapm_del_route(dapm, route);
3150 route++;
3151 }
3152 mutex_unlock(&dapm->card->dapm_mutex);
3153
Rajan Vajae066ea22016-02-11 11:23:35 +05303154 return 0;
Mark Brownefcc3c62012-07-05 17:24:19 +01003155}
3156EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3157
Mark Brownbf3a9e12011-06-13 16:42:29 +01003158static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3159 const struct snd_soc_dapm_route *route)
3160{
3161 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3162 route->source,
3163 true);
3164 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3165 route->sink,
3166 true);
3167 struct snd_soc_dapm_path *path;
3168 int count = 0;
3169
3170 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003171 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003172 route->source);
3173 return -ENODEV;
3174 }
3175
3176 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003177 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003178 route->sink);
3179 return -ENODEV;
3180 }
3181
3182 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003183 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003184 route->source, route->sink);
3185
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02003186 snd_soc_dapm_widget_for_each_sink_path(source, path) {
Mark Brownbf3a9e12011-06-13 16:42:29 +01003187 if (path->sink == sink) {
3188 path->weak = 1;
3189 count++;
3190 }
3191 }
3192
3193 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003194 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003195 route->source, route->sink);
3196 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003197 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003198 count, route->source, route->sink);
3199
3200 return 0;
3201}
3202
3203/**
3204 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3205 * @dapm: DAPM context
3206 * @route: audio routes
3207 * @num: number of routes
3208 *
3209 * Mark existing routes matching those specified in the passed array
3210 * as being weak, meaning that they are ignored for the purpose of
3211 * power decisions. The main intended use case is for sidetone paths
3212 * which couple audio between other independent paths if they are both
3213 * active in order to make the combination work better at the user
3214 * level but which aren't intended to be "used".
3215 *
3216 * Note that CODEC drivers should not use this as sidetone type paths
3217 * can frequently also be used as bypass paths.
3218 */
3219int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3220 const struct snd_soc_dapm_route *route, int num)
3221{
Kuninori Morimotofd136fdb2021-08-05 10:11:37 +09003222 int i;
Mark Brownbf3a9e12011-06-13 16:42:29 +01003223 int ret = 0;
3224
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003225 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01003226 for (i = 0; i < num; i++) {
Kuninori Morimotofd136fdb2021-08-05 10:11:37 +09003227 int err = snd_soc_dapm_weak_route(dapm, route);
Mark Brownbf3a9e12011-06-13 16:42:29 +01003228 if (err)
3229 ret = err;
3230 route++;
3231 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003232 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01003233
3234 return ret;
3235}
3236EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3237
Mark Brown105f1c22008-05-13 14:52:19 +02003238/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003239 * snd_soc_dapm_new_widgets - add new dapm widgets
Jonathan Corbet628536e2015-08-25 01:14:48 -06003240 * @card: card to be checked for new dapm widgets
Richard Purdie2b97eab2006-10-06 18:32:18 +02003241 *
3242 * Checks the codec for any new dapm widgets and creates them if found.
3243 *
3244 * Returns 0 for success.
3245 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02003246int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003247{
3248 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00003249 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003250
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003251 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003252
Kuninori Morimoto145966922020-03-09 13:08:21 +09003253 for_each_card_widgets(card, w)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003254 {
3255 if (w->new)
3256 continue;
3257
Stephen Warrenfad59882011-04-28 17:37:59 -06003258 if (w->num_kcontrols) {
Kees Cook6396bb22018-06-12 14:03:40 -07003259 w->kcontrols = kcalloc(w->num_kcontrols,
Stephen Warrenfad59882011-04-28 17:37:59 -06003260 sizeof(struct snd_kcontrol *),
3261 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003262 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003263 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06003264 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003265 }
Stephen Warrenfad59882011-04-28 17:37:59 -06003266 }
3267
Richard Purdie2b97eab2006-10-06 18:32:18 +02003268 switch(w->id) {
3269 case snd_soc_dapm_switch:
3270 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00003271 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003272 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003273 break;
3274 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003275 case snd_soc_dapm_demux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003276 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003277 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003278 case snd_soc_dapm_pga:
Seppo Ingalsuo882c8b4a2019-04-30 18:11:34 -05003279 case snd_soc_dapm_effect:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06003280 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003281 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003282 break;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003283 case snd_soc_dapm_dai_link:
3284 dapm_new_dai_link(w);
3285 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01003286 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003287 break;
3288 }
Mark Brownb66a70d2011-02-09 18:04:11 +00003289
3290 /* Read the initial power state from the device */
3291 if (w->reg >= 0) {
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +09003292 val = soc_dapm_read(w->dapm, w->reg);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08003293 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003294 val &= w->mask;
3295 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00003296 w->power = 1;
3297 }
3298
Richard Purdie2b97eab2006-10-06 18:32:18 +02003299 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003300
Mark Brown7508b122011-10-05 12:09:12 +01003301 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003302 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003303 }
3304
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003305 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3306 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003307 return 0;
3308}
3309EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3310
3311/**
3312 * snd_soc_dapm_get_volsw - dapm mixer get callback
3313 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003314 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003315 *
3316 * Callback to get the value of a dapm mixer control.
3317 *
3318 * Returns 0 for success.
3319 */
3320int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3321 struct snd_ctl_elem_value *ucontrol)
3322{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003323 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3324 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003325 struct soc_mixer_control *mc =
3326 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003327 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003328 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003329 int max = mc->max;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003330 unsigned int width = fls(max);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003331 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003332 unsigned int invert = mc->invert;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003333 unsigned int reg_val, val, rval = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003334
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003335 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003336 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +09003337 reg_val = soc_dapm_read(dapm, reg);
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003338 val = (reg_val >> shift) & mask;
3339
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +09003340 if (reg != mc->rreg)
3341 reg_val = soc_dapm_read(dapm, mc->rreg);
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003342
3343 if (snd_soc_volsw_is_stereo(mc))
3344 rval = (reg_val >> mc->rshift) & mask;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003345 } else {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003346 reg_val = dapm_kcontrol_get_value(kcontrol);
3347 val = reg_val & mask;
3348
3349 if (snd_soc_volsw_is_stereo(mc))
3350 rval = (reg_val >> width) & mask;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003351 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003352 mutex_unlock(&card->dapm_mutex);
3353
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003354 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003355 ucontrol->value.integer.value[0] = max - val;
3356 else
3357 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003358
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003359 if (snd_soc_volsw_is_stereo(mc)) {
3360 if (invert)
3361 ucontrol->value.integer.value[1] = max - rval;
3362 else
3363 ucontrol->value.integer.value[1] = rval;
3364 }
3365
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +09003366 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003367}
3368EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3369
3370/**
3371 * snd_soc_dapm_put_volsw - dapm mixer set callback
3372 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003373 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003374 *
3375 * Callback to set the value of a dapm mixer control.
3376 *
3377 * Returns 0 for success.
3378 */
3379int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3380 struct snd_ctl_elem_value *ucontrol)
3381{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003382 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3383 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003384 struct soc_mixer_control *mc =
3385 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003386 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003387 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003388 int max = mc->max;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003389 unsigned int width = fls(max);
3390 unsigned int mask = (1 << width) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003391 unsigned int invert = mc->invert;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003392 unsigned int val, rval = 0;
3393 int connect, rconnect = -1, change, reg_change = 0;
Fabio Estevam33d92452018-02-14 13:39:05 -02003394 struct snd_soc_dapm_update update = {};
Nenghua Cao52765972013-12-13 20:13:49 +08003395 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003396
3397 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02003398 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003399
3400 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003401 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003402
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003403 if (snd_soc_volsw_is_stereo(mc)) {
3404 rval = (ucontrol->value.integer.value[1] & mask);
3405 rconnect = !!rval;
3406 if (invert)
3407 rval = max - rval;
3408 }
3409
Liam Girdwood3cd04342012-03-09 12:02:08 +00003410 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003411
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003412 /* This assumes field width < (bits in unsigned int / 2) */
3413 if (width > sizeof(unsigned int) * 8 / 2)
3414 dev_warn(dapm->dev,
3415 "ASoC: control %s field width limit exceeded\n",
3416 kcontrol->id.name);
3417 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
Mark Brown283375c2009-12-07 18:09:03 +00003418
Jarkko Nikula18626c72014-06-09 14:20:29 +03003419 if (reg != SND_SOC_NOPM) {
Jarkko Nikula18626c72014-06-09 14:20:29 +03003420 val = val << shift;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003421 rval = rval << mc->rshift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02003422
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003423 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3424
3425 if (snd_soc_volsw_is_stereo(mc))
3426 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3427 mask << mc->rshift,
3428 rval);
Jarkko Nikula18626c72014-06-09 14:20:29 +03003429 }
3430
3431 if (change || reg_change) {
3432 if (reg_change) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003433 if (snd_soc_volsw_is_stereo(mc)) {
3434 update.has_second_set = true;
3435 update.reg2 = mc->rreg;
3436 update.mask2 = mask << mc->rshift;
3437 update.val2 = rval;
3438 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003439 update.kcontrol = kcontrol;
3440 update.reg = reg;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003441 update.mask = mask << shift;
Jarkko Nikula18626c72014-06-09 14:20:29 +03003442 update.val = val;
3443 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003444 }
Mark Brown97404f22010-12-14 16:13:57 +00003445
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003446 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3447 rconnect);
Mark Brown97404f22010-12-14 16:13:57 +00003448
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003449 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00003450 }
3451
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003452 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003453
3454 if (ret > 0)
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01003455 snd_soc_dpcm_runtime_update(card);
Nenghua Cao52765972013-12-13 20:13:49 +08003456
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003457 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003458}
3459EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3460
3461/**
3462 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3463 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003464 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003465 *
3466 * Callback to get the value of a dapm enumerated double mixer control.
3467 *
3468 * Returns 0 for success.
3469 */
3470int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3471 struct snd_ctl_elem_value *ucontrol)
3472{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003473 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Charles Keepax561ed682015-05-01 12:37:26 +01003474 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003475 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003476 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003477
Charles Keepax561ed682015-05-01 12:37:26 +01003478 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3479 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +09003480 reg_val = soc_dapm_read(dapm, e->reg);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003481 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003482 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003483 }
Charles Keepax561ed682015-05-01 12:37:26 +01003484 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003485
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003486 val = (reg_val >> e->shift_l) & e->mask;
3487 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3488 if (e->shift_l != e->shift_r) {
3489 val = (reg_val >> e->shift_r) & e->mask;
3490 val = snd_soc_enum_val_to_item(e, val);
3491 ucontrol->value.enumerated.item[1] = val;
3492 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003493
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003494 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003495}
3496EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3497
Tzung-Bi Shih8f486292020-02-14 18:57:43 +08003498/**
3499 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3500 * @kcontrol: mixer control
3501 * @ucontrol: control element information
3502 *
3503 * Callback to set the value of a dapm enumerated double mixer control.
3504 *
3505 * Returns 0 for success.
3506 */
3507int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3508 struct snd_ctl_elem_value *ucontrol)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003509{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003510 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3511 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003512 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003513 unsigned int *item = ucontrol->value.enumerated.item;
Charles Keepax561ed682015-05-01 12:37:26 +01003514 unsigned int val, change, reg_change = 0;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003515 unsigned int mask;
Fabio Estevam33d92452018-02-14 13:39:05 -02003516 struct snd_soc_dapm_update update = {};
Nenghua Cao52765972013-12-13 20:13:49 +08003517 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003518
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003519 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003520 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003521
3522 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003523 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003524 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003525 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003526 return -EINVAL;
Chen-Yu Tsai071133a2016-08-27 19:27:59 +08003527 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003528 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003529 }
3530
Tzung-Bi Shih8f486292020-02-14 18:57:43 +08003531 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003532
Charles Keepax561ed682015-05-01 12:37:26 +01003533 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00003534
Charles Keepax561ed682015-05-01 12:37:26 +01003535 if (e->reg != SND_SOC_NOPM)
3536 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3537
3538 if (change || reg_change) {
3539 if (reg_change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003540 update.kcontrol = kcontrol;
3541 update.reg = e->reg;
3542 update.mask = mask;
3543 update.val = val;
3544 card->update = &update;
3545 }
Mark Brown3a655772009-10-05 17:23:30 +01003546
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003547 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003548
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003549 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003550 }
3551
Tzung-Bi Shih8f486292020-02-14 18:57:43 +08003552 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003553
3554 if (ret > 0)
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01003555 snd_soc_dpcm_runtime_update(card);
Nenghua Cao52765972013-12-13 20:13:49 +08003556
Mark Brown97404f22010-12-14 16:13:57 +00003557 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003558}
3559EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3560
3561/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003562 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3563 *
3564 * @kcontrol: mixer control
3565 * @uinfo: control element information
3566 *
3567 * Callback to provide information about a pin switch control.
3568 */
3569int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3570 struct snd_ctl_elem_info *uinfo)
3571{
3572 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3573 uinfo->count = 1;
3574 uinfo->value.integer.min = 0;
3575 uinfo->value.integer.max = 1;
3576
3577 return 0;
3578}
3579EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3580
3581/**
3582 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3583 *
3584 * @kcontrol: mixer control
3585 * @ucontrol: Value
3586 */
3587int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3588 struct snd_ctl_elem_value *ucontrol)
3589{
Mark Brown48a8c392012-02-14 17:11:15 -08003590 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003591 const char *pin = (const char *)kcontrol->private_value;
3592
Liam Girdwood3cd04342012-03-09 12:02:08 +00003593 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003594
3595 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003596 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003597
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003598 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003599
3600 return 0;
3601}
3602EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3603
3604/**
3605 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3606 *
3607 * @kcontrol: mixer control
3608 * @ucontrol: Value
3609 */
3610int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3611 struct snd_ctl_elem_value *ucontrol)
3612{
Mark Brown48a8c392012-02-14 17:11:15 -08003613 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003614 const char *pin = (const char *)kcontrol->private_value;
Takashi Iwai5af82c82021-10-06 16:17:12 +02003615 int ret;
Mark Brown8b37dbd2009-02-28 21:14:20 +00003616
Takashi Iwai827b0912021-11-05 10:09:25 +01003617 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3618 ret = __snd_soc_dapm_set_pin(&card->dapm, pin,
3619 !!ucontrol->value.integer.value[0]);
3620 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003621
Mark Brown48a8c392012-02-14 17:11:15 -08003622 snd_soc_dapm_sync(&card->dapm);
Takashi Iwai5af82c82021-10-06 16:17:12 +02003623 return ret;
Mark Brown8b37dbd2009-02-28 21:14:20 +00003624}
3625EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3626
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +01003627struct snd_soc_dapm_widget *
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003628snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003629 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003630{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003631 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003632 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003633 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00003634 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003635
3636 if ((w = dapm_cnew_widget(widget)) == NULL)
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003637 return ERR_PTR(-ENOMEM);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003638
Mark Brown62ea8742012-01-21 21:14:48 +00003639 switch (w->id) {
3640 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003641 w->regulator = devm_regulator_get(dapm->dev, w->name);
3642 if (IS_ERR(w->regulator)) {
3643 ret = PTR_ERR(w->regulator);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003644 goto request_failed;
Mark Brown62ea8742012-01-21 21:14:48 +00003645 }
Mark Brown8784c772013-01-10 19:33:47 +00003646
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003647 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003648 ret = regulator_allow_bypass(w->regulator, true);
3649 if (ret != 0)
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003650 dev_warn(dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003651 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003652 w->name, ret);
3653 }
Mark Brown62ea8742012-01-21 21:14:48 +00003654 break;
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003655 case snd_soc_dapm_pinctrl:
3656 w->pinctrl = devm_pinctrl_get(dapm->dev);
Charles Keepaxa5cd7e92018-08-28 14:35:03 +01003657 if (IS_ERR(w->pinctrl)) {
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003658 ret = PTR_ERR(w->pinctrl);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003659 goto request_failed;
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003660 }
Tzung-Bi Shihec06dc12020-02-19 17:38:38 +08003661
3662 /* set to sleep_state when initializing */
3663 dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD);
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003664 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003665 case snd_soc_dapm_clock_supply:
Mark Brown695594f12012-06-04 08:14:13 +01003666 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003667 if (IS_ERR(w->clk)) {
3668 ret = PTR_ERR(w->clk);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003669 goto request_failed;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003670 }
3671 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003672 default:
3673 break;
3674 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003675
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003676 prefix = soc_dapm_prefix(dapm);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003677 if (prefix)
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003678 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003679 else
Lars-Peter Clausen48068962015-07-21 18:11:08 +02003680 w->name = kstrdup_const(widget->name, GFP_KERNEL);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003681 if (w->name == NULL) {
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -06003682 kfree_const(w->sname);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003683 kfree(w);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003684 return ERR_PTR(-ENOMEM);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003685 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003686
Mark Brown7ca3a182011-10-08 14:04:50 +01003687 switch (w->id) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003688 case snd_soc_dapm_mic:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003689 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003690 w->power_check = dapm_generic_check_power;
3691 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003692 case snd_soc_dapm_input:
3693 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003694 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003695 w->power_check = dapm_generic_check_power;
3696 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003697 case snd_soc_dapm_spk:
3698 case snd_soc_dapm_hp:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003699 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003700 w->power_check = dapm_generic_check_power;
3701 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003702 case snd_soc_dapm_output:
3703 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003704 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003705 w->power_check = dapm_generic_check_power;
3706 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003707 case snd_soc_dapm_vmid:
3708 case snd_soc_dapm_siggen:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003709 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003710 w->power_check = dapm_always_on_check_power;
3711 break;
Vinod Koul56b44372015-11-23 21:22:30 +05303712 case snd_soc_dapm_sink:
3713 w->is_ep = SND_SOC_DAPM_EP_SINK;
3714 w->power_check = dapm_always_on_check_power;
3715 break;
3716
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003717 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003718 case snd_soc_dapm_demux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003719 case snd_soc_dapm_switch:
3720 case snd_soc_dapm_mixer:
3721 case snd_soc_dapm_mixer_named_ctl:
Mark Brown63c69a62013-07-18 22:03:01 +01003722 case snd_soc_dapm_adc:
3723 case snd_soc_dapm_aif_out:
3724 case snd_soc_dapm_dac:
3725 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003726 case snd_soc_dapm_pga:
Ranjani Sridharan6e3bfcf2019-03-01 19:08:53 -06003727 case snd_soc_dapm_buffer:
3728 case snd_soc_dapm_scheduler:
3729 case snd_soc_dapm_effect:
3730 case snd_soc_dapm_src:
3731 case snd_soc_dapm_asrc:
3732 case snd_soc_dapm_encoder:
3733 case snd_soc_dapm_decoder:
Mark Brown7ca3a182011-10-08 14:04:50 +01003734 case snd_soc_dapm_out_drv:
Mark Brown7ca3a182011-10-08 14:04:50 +01003735 case snd_soc_dapm_micbias:
Mark Brown7ca3a182011-10-08 14:04:50 +01003736 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003737 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02003738 case snd_soc_dapm_dai_out:
3739 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003740 w->power_check = dapm_generic_check_power;
3741 break;
3742 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003743 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003744 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003745 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003746 case snd_soc_dapm_kcontrol:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003747 w->is_supply = 1;
Mark Brown7ca3a182011-10-08 14:04:50 +01003748 w->power_check = dapm_supply_check_power;
3749 break;
3750 default:
3751 w->power_check = dapm_always_on_check_power;
3752 break;
3753 }
3754
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003755 w->dapm = dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003756 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003757 INIT_LIST_HEAD(&w->dirty);
Kuninori Morimoto145966922020-03-09 13:08:21 +09003758 /* see for_each_card_widgets */
Lars-Peter Clausen92fa1242015-05-01 18:02:42 +02003759 list_add_tail(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003760
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003761 snd_soc_dapm_for_each_direction(dir) {
3762 INIT_LIST_HEAD(&w->edges[dir]);
3763 w->endpoints[dir] = -1;
3764 }
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003765
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02003766 /* machine layer sets up unconnected pins and insertions */
Richard Purdie2b97eab2006-10-06 18:32:18 +02003767 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003768 return w;
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003769
3770request_failed:
3771 if (ret != -EPROBE_DEFER)
3772 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3773 w->name, ret);
3774
Wenwen Wang45004d62019-07-22 08:57:44 -05003775 kfree_const(w->sname);
3776 kfree(w);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003777 return ERR_PTR(ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003778}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003779
3780/**
Charles Keepax94e630a2018-09-05 15:20:59 +01003781 * snd_soc_dapm_new_control - create new dapm control
3782 * @dapm: DAPM context
3783 * @widget: widget template
3784 *
3785 * Creates new DAPM control based upon a template.
3786 *
3787 * Returns a widget pointer on success or an error pointer on failure
3788 */
3789struct snd_soc_dapm_widget *
3790snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3791 const struct snd_soc_dapm_widget *widget)
3792{
3793 struct snd_soc_dapm_widget *w;
3794
3795 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3796 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3797 mutex_unlock(&dapm->card->dapm_mutex);
3798
3799 return w;
3800}
3801EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3802
3803/**
Mark Brown4ba13272008-05-13 14:51:19 +02003804 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003805 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003806 * @widget: widget array
3807 * @num: number of widgets
3808 *
3809 * Creates new DAPM controls based upon the templates.
3810 *
3811 * Returns 0 for success else error.
3812 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003813int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003814 const struct snd_soc_dapm_widget *widget,
3815 int num)
3816{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003817 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003818 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003819
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003820 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003821 for (i = 0; i < num; i++) {
Kuninori Morimoto3dc72e422021-08-05 10:11:41 +09003822 struct snd_soc_dapm_widget *w = snd_soc_dapm_new_control_unlocked(dapm, widget);
Linus Walleij37e1df82017-01-13 10:23:52 +01003823 if (IS_ERR(w)) {
3824 ret = PTR_ERR(w);
Dan Carpenter60884c22012-04-13 22:25:43 +03003825 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003826 }
Mark Brown4ba13272008-05-13 14:51:19 +02003827 widget++;
3828 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003829 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003830 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003831}
3832EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3833
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003834static int
3835snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3836 struct snd_pcm_substream *substream)
Mark Brownc74184e2012-04-04 22:12:09 +01003837{
Charles Keepax4a75aae2018-09-05 15:21:01 +01003838 struct snd_soc_dapm_path *path;
Mark Brownc74184e2012-04-04 22:12:09 +01003839 struct snd_soc_dai *source, *sink;
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09003840 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Mark Brown9747cec2012-04-26 19:12:21 +01003841 struct snd_pcm_hw_params *params = NULL;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003842 const struct snd_soc_pcm_stream *config = NULL;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003843 struct snd_pcm_runtime *runtime = NULL;
Takashi Iwai3ba66fe2018-07-25 22:43:26 +02003844 unsigned int fmt;
Colin Ian Kingc8415832019-07-26 13:33:27 +01003845 int ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003846
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003847 params = kzalloc(sizeof(*params), GFP_KERNEL);
3848 if (!params)
3849 return -ENOMEM;
3850
Jerome Bruneta72706e2019-07-25 18:59:48 +02003851 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3852 if (!runtime) {
3853 ret = -ENOMEM;
3854 goto out;
3855 }
3856
3857 substream->runtime = runtime;
3858
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003859 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3860 snd_soc_dapm_widget_for_each_source_path(w, path) {
3861 source = path->source->priv;
3862
3863 ret = snd_soc_dai_startup(source, substream);
Kuninori Morimoto62462e02021-03-15 09:58:37 +09003864 if (ret < 0)
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003865 goto out;
Kuninori Morimoto62462e02021-03-15 09:58:37 +09003866
Kuninori Morimoto45eb8662020-05-15 09:46:33 +09003867 snd_soc_dai_activate(source, substream->stream);
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003868 }
3869
3870 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3871 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3872 sink = path->sink->priv;
3873
3874 ret = snd_soc_dai_startup(sink, substream);
Kuninori Morimoto62462e02021-03-15 09:58:37 +09003875 if (ret < 0)
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003876 goto out;
Kuninori Morimoto62462e02021-03-15 09:58:37 +09003877
Kuninori Morimoto45eb8662020-05-15 09:46:33 +09003878 snd_soc_dai_activate(sink, substream->stream);
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003879 }
3880
Jerome Bruneta72706e2019-07-25 18:59:48 +02003881 substream->hw_opened = 1;
3882
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003883 /*
3884 * Note: getting the config after .startup() gives a chance to
3885 * either party on the link to alter the configuration if
3886 * necessary
3887 */
Charles Keepax243bcfa2018-09-05 15:21:02 +01003888 config = rtd->dai_link->params + rtd->params_select;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003889 if (WARN_ON(!config)) {
3890 dev_err(w->dapm->dev, "ASoC: link config missing\n");
3891 ret = -EINVAL;
3892 goto out;
3893 }
Mark Brownc74184e2012-04-04 22:12:09 +01003894
Mark Brownc74184e2012-04-04 22:12:09 +01003895 /* Be a little careful as we don't want to overflow the mask array */
3896 if (config->formats) {
3897 fmt = ffs(config->formats) - 1;
3898 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003899 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003900 config->formats);
Mark Brownc74184e2012-04-04 22:12:09 +01003901
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003902 ret = -EINVAL;
Mark Brown9747cec2012-04-26 19:12:21 +01003903 goto out;
3904 }
Mark Brownc74184e2012-04-04 22:12:09 +01003905
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003906 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brown9747cec2012-04-26 19:12:21 +01003907 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003908 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003909 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003910 config->rate_max;
Mark Brown9747cec2012-04-26 19:12:21 +01003911 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003912 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003913 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003914 = config->channels_max;
3915
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003916 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3917 snd_soc_dapm_widget_for_each_source_path(w, path) {
3918 source = path->source->priv;
3919
3920 ret = snd_soc_dai_hw_params(source, substream, params);
3921 if (ret < 0)
3922 goto out;
3923
3924 dapm_update_dai_unlocked(substream, params, source);
3925 }
3926
3927 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3928 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3929 sink = path->sink->priv;
3930
3931 ret = snd_soc_dai_hw_params(sink, substream, params);
3932 if (ret < 0)
3933 goto out;
3934
3935 dapm_update_dai_unlocked(substream, params, sink);
3936 }
3937
Jerome Brunet9de98622019-07-25 18:59:49 +02003938 runtime->format = params_format(params);
3939 runtime->subformat = params_subformat(params);
3940 runtime->channels = params_channels(params);
3941 runtime->rate = params_rate(params);
3942
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003943out:
3944 kfree(params);
Colin Ian Kingc8415832019-07-26 13:33:27 +01003945 return ret;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003946}
3947
3948static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3949 struct snd_kcontrol *kcontrol, int event)
3950{
3951 struct snd_soc_dapm_path *path;
3952 struct snd_soc_dai *source, *sink;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003953 struct snd_pcm_substream *substream = w->priv;
3954 int ret = 0, saved_stream = substream->stream;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003955
3956 if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3957 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3958 return -EINVAL;
3959
Mark Brownc74184e2012-04-04 22:12:09 +01003960 switch (event) {
3961 case SND_SOC_DAPM_PRE_PMU:
Jerome Bruneta72706e2019-07-25 18:59:48 +02003962 ret = snd_soc_dai_link_event_pre_pmu(w, substream);
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003963 if (ret < 0)
3964 goto out;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003965
Mark Brownc74184e2012-04-04 22:12:09 +01003966 break;
3967
3968 case SND_SOC_DAPM_POST_PMU:
Charles Keepax4a75aae2018-09-05 15:21:01 +01003969 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3970 sink = path->sink->priv;
3971
Kuninori Morimoto62462e02021-03-15 09:58:37 +09003972 snd_soc_dai_digital_mute(sink, 0, SNDRV_PCM_STREAM_PLAYBACK);
Charles Keepax4a75aae2018-09-05 15:21:01 +01003973 ret = 0;
3974 }
Mark Brownc74184e2012-04-04 22:12:09 +01003975 break;
3976
3977 case SND_SOC_DAPM_PRE_PMD:
Charles Keepax4a75aae2018-09-05 15:21:01 +01003978 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3979 sink = path->sink->priv;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303980
Kuninori Morimoto62462e02021-03-15 09:58:37 +09003981 snd_soc_dai_digital_mute(sink, 1, SNDRV_PCM_STREAM_PLAYBACK);
Charles Keepax4a75aae2018-09-05 15:21:01 +01003982 ret = 0;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303983 }
3984
Jerome Bruneta72706e2019-07-25 18:59:48 +02003985 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003986 snd_soc_dapm_widget_for_each_source_path(w, path) {
3987 source = path->source->priv;
Kuninori Morimotoc304c9a2020-09-29 13:31:53 +09003988 snd_soc_dai_hw_free(source, substream, 0);
Jerome Brunet68c907f2019-07-25 18:59:44 +02003989 }
Charles Keepax3c01b0e2018-10-11 17:28:28 +01003990
Jerome Bruneta72706e2019-07-25 18:59:48 +02003991 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
Jerome Brunet68c907f2019-07-25 18:59:44 +02003992 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3993 sink = path->sink->priv;
Kuninori Morimotoc304c9a2020-09-29 13:31:53 +09003994 snd_soc_dai_hw_free(sink, substream, 0);
Jerome Brunet68c907f2019-07-25 18:59:44 +02003995 }
3996
Jerome Bruneta72706e2019-07-25 18:59:48 +02003997 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
Jerome Brunet68c907f2019-07-25 18:59:44 +02003998 snd_soc_dapm_widget_for_each_source_path(w, path) {
3999 source = path->source->priv;
Kuninori Morimoto45eb8662020-05-15 09:46:33 +09004000 snd_soc_dai_deactivate(source, substream->stream);
Kuninori Morimoto00a0b462020-09-28 09:00:40 +09004001 snd_soc_dai_shutdown(source, substream, 0);
Charles Keepax4a75aae2018-09-05 15:21:01 +01004002 }
4003
Jerome Bruneta72706e2019-07-25 18:59:48 +02004004 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
Charles Keepax4a75aae2018-09-05 15:21:01 +01004005 snd_soc_dapm_widget_for_each_sink_path(w, path) {
4006 sink = path->sink->priv;
Kuninori Morimoto45eb8662020-05-15 09:46:33 +09004007 snd_soc_dai_deactivate(sink, substream->stream);
Kuninori Morimoto00a0b462020-09-28 09:00:40 +09004008 snd_soc_dai_shutdown(sink, substream, 0);
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05304009 }
Mark Brownc74184e2012-04-04 22:12:09 +01004010 break;
4011
Jerome Bruneta72706e2019-07-25 18:59:48 +02004012 case SND_SOC_DAPM_POST_PMD:
4013 kfree(substream->runtime);
4014 break;
4015
Mark Brownc74184e2012-04-04 22:12:09 +01004016 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01004017 WARN(1, "Unknown event %d\n", event);
Sudip Mukherjee75881df2015-09-10 18:01:44 +05304018 ret = -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01004019 }
4020
Mark Brown9747cec2012-04-26 19:12:21 +01004021out:
Jerome Bruneta72706e2019-07-25 18:59:48 +02004022 /* Restore the substream direction */
4023 substream->stream = saved_stream;
Mark Brown9747cec2012-04-26 19:12:21 +01004024 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01004025}
4026
Nikesh Oswalc6615082015-02-02 17:06:44 +00004027static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
4028 struct snd_ctl_elem_value *ucontrol)
4029{
4030 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
Charles Keepax243bcfa2018-09-05 15:21:02 +01004031 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004032
Charles Keepax243bcfa2018-09-05 15:21:02 +01004033 ucontrol->value.enumerated.item[0] = rtd->params_select;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004034
4035 return 0;
4036}
4037
4038static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
4039 struct snd_ctl_elem_value *ucontrol)
4040{
4041 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
Charles Keepax243bcfa2018-09-05 15:21:02 +01004042 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004043
4044 /* Can't change the config when widget is already powered */
4045 if (w->power)
4046 return -EBUSY;
4047
Charles Keepax243bcfa2018-09-05 15:21:02 +01004048 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
Nikesh Oswalc6615082015-02-02 17:06:44 +00004049 return 0;
4050
Charles Keepax243bcfa2018-09-05 15:21:02 +01004051 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
Nikesh Oswalc6615082015-02-02 17:06:44 +00004052 return -EINVAL;
4053
Charles Keepax243bcfa2018-09-05 15:21:02 +01004054 rtd->params_select = ucontrol->value.enumerated.item[0];
Nikesh Oswalc6615082015-02-02 17:06:44 +00004055
Takashi Iwai5af82c82021-10-06 16:17:12 +02004056 return 1;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004057}
4058
Arnd Bergmannc42c5ac2017-10-10 11:20:11 +02004059static void
anish kumar19ad6832017-09-28 21:52:39 -07004060snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
4061 unsigned long *private_value,
4062 int num_params,
4063 const char **w_param_text)
Mark Brownc74184e2012-04-04 22:12:09 +01004064{
anish kumar19ad6832017-09-28 21:52:39 -07004065 int count;
4066
4067 devm_kfree(card->dev, (void *)*private_value);
Pankaj Bharadiyacacea3a2019-03-22 18:00:09 +05304068
4069 if (!w_param_text)
4070 return;
4071
anish kumar19ad6832017-09-28 21:52:39 -07004072 for (count = 0 ; count < num_params; count++)
4073 devm_kfree(card->dev, (void *)w_param_text[count]);
4074 devm_kfree(card->dev, w_param_text);
4075}
4076
4077static struct snd_kcontrol_new *
4078snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
4079 char *link_name,
4080 const struct snd_soc_pcm_stream *params,
4081 int num_params, const char **w_param_text,
4082 unsigned long *private_value)
4083{
Nikesh Oswalc6615082015-02-02 17:06:44 +00004084 struct soc_enum w_param_enum[] = {
4085 SOC_ENUM_SINGLE(0, 0, 0, NULL),
4086 };
4087 struct snd_kcontrol_new kcontrol_dai_link[] = {
4088 SOC_ENUM_EXT(NULL, w_param_enum[0],
4089 snd_soc_dapm_dai_link_get,
4090 snd_soc_dapm_dai_link_put),
4091 };
anish kumar19ad6832017-09-28 21:52:39 -07004092 struct snd_kcontrol_new *kcontrol_news;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004093 const struct snd_soc_pcm_stream *config = params;
anish kumar19ad6832017-09-28 21:52:39 -07004094 int count;
Mark Brownc74184e2012-04-04 22:12:09 +01004095
Nikesh Oswalc6615082015-02-02 17:06:44 +00004096 for (count = 0 ; count < num_params; count++) {
4097 if (!config->stream_name) {
4098 dev_warn(card->dapm.dev,
4099 "ASoC: anonymous config %d for dai link %s\n",
4100 count, link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004101 w_param_text[count] =
Charles Keepax46172b62015-03-25 11:22:35 +00004102 devm_kasprintf(card->dev, GFP_KERNEL,
4103 "Anonymous Configuration %d",
4104 count);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004105 } else {
4106 w_param_text[count] = devm_kmemdup(card->dev,
4107 config->stream_name,
4108 strlen(config->stream_name) + 1,
4109 GFP_KERNEL);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004110 }
anish kumar19ad6832017-09-28 21:52:39 -07004111 if (!w_param_text[count])
4112 goto outfree_w_param;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004113 config++;
4114 }
anish kumar19ad6832017-09-28 21:52:39 -07004115
Nikesh Oswalc6615082015-02-02 17:06:44 +00004116 w_param_enum[0].items = num_params;
4117 w_param_enum[0].texts = w_param_text;
Mark Brownc74184e2012-04-04 22:12:09 +01004118
anish kumar19ad6832017-09-28 21:52:39 -07004119 *private_value =
4120 (unsigned long) devm_kmemdup(card->dev,
4121 (void *)(kcontrol_dai_link[0].private_value),
4122 sizeof(struct soc_enum), GFP_KERNEL);
4123 if (!*private_value) {
4124 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4125 link_name);
4126 goto outfree_w_param;
4127 }
4128 kcontrol_dai_link[0].private_value = *private_value;
4129 /* duplicate kcontrol_dai_link on heap so that memory persists */
4130 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4131 sizeof(struct snd_kcontrol_new),
4132 GFP_KERNEL);
4133 if (!kcontrol_news) {
4134 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4135 link_name);
4136 goto outfree_w_param;
4137 }
4138 return kcontrol_news;
4139
4140outfree_w_param:
4141 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
4142 return NULL;
4143}
4144
Charles Keepax778ff5b2018-09-05 15:21:00 +01004145static struct snd_soc_dapm_widget *
Jerome Bruneta72706e2019-07-25 18:59:48 +02004146snd_soc_dapm_new_dai(struct snd_soc_card *card,
4147 struct snd_pcm_substream *substream,
Jerome Brunet054d6502019-07-25 18:59:45 +02004148 char *id)
anish kumar19ad6832017-09-28 21:52:39 -07004149{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09004150 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
anish kumar19ad6832017-09-28 21:52:39 -07004151 struct snd_soc_dapm_widget template;
4152 struct snd_soc_dapm_widget *w;
4153 const char **w_param_text;
Pankaj Bharadiya8633d442019-03-22 21:53:39 +05304154 unsigned long private_value = 0;
anish kumar19ad6832017-09-28 21:52:39 -07004155 char *link_name;
4156 int ret;
4157
4158 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
Jerome Brunet054d6502019-07-25 18:59:45 +02004159 rtd->dai_link->name, id);
anish kumar19ad6832017-09-28 21:52:39 -07004160 if (!link_name)
Charles Keepax778ff5b2018-09-05 15:21:00 +01004161 return ERR_PTR(-ENOMEM);
anish kumar19ad6832017-09-28 21:52:39 -07004162
Mark Brownc74184e2012-04-04 22:12:09 +01004163 memset(&template, 0, sizeof(template));
4164 template.reg = SND_SOC_NOPM;
4165 template.id = snd_soc_dapm_dai_link;
4166 template.name = link_name;
4167 template.event = snd_soc_dai_link_event;
4168 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
Jerome Bruneta72706e2019-07-25 18:59:48 +02004169 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD;
anish kumar19ad6832017-09-28 21:52:39 -07004170 template.kcontrol_news = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01004171
anish kumar19ad6832017-09-28 21:52:39 -07004172 /* allocate memory for control, only in case of multiple configs */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004173 if (rtd->dai_link->num_params > 1) {
4174 w_param_text = devm_kcalloc(card->dev,
4175 rtd->dai_link->num_params,
4176 sizeof(char *), GFP_KERNEL);
anish kumar19ad6832017-09-28 21:52:39 -07004177 if (!w_param_text) {
4178 ret = -ENOMEM;
4179 goto param_fail;
4180 }
4181
4182 template.num_kcontrols = 1;
4183 template.kcontrol_news =
4184 snd_soc_dapm_alloc_kcontrol(card,
Charles Keepax778ff5b2018-09-05 15:21:00 +01004185 link_name,
4186 rtd->dai_link->params,
4187 rtd->dai_link->num_params,
anish kumar19ad6832017-09-28 21:52:39 -07004188 w_param_text, &private_value);
4189 if (!template.kcontrol_news) {
4190 ret = -ENOMEM;
4191 goto param_fail;
4192 }
Arnd Bergmann667ebc92017-10-10 11:20:10 +02004193 } else {
4194 w_param_text = NULL;
anish kumar19ad6832017-09-28 21:52:39 -07004195 }
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004196 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01004197
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004198 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
Dan Carpenter2e558a82018-09-07 22:40:33 +03004199 if (IS_ERR(w)) {
4200 ret = PTR_ERR(w);
Charles Keepax9a1bb6002020-04-09 19:12:08 +01004201 dev_err(rtd->dev, "ASoC: Failed to create %s widget: %d\n",
4202 link_name, ret);
Linus Walleij37e1df82017-01-13 10:23:52 +01004203 goto outfree_kcontrol_news;
Dan Carpenter2e558a82018-09-07 22:40:33 +03004204 }
Mark Brownc74184e2012-04-04 22:12:09 +01004205
Jerome Bruneta72706e2019-07-25 18:59:48 +02004206 w->priv = substream;
Mark Brownc74184e2012-04-04 22:12:09 +01004207
Charles Keepax778ff5b2018-09-05 15:21:00 +01004208 return w;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004209
Nikesh Oswalc6615082015-02-02 17:06:44 +00004210outfree_kcontrol_news:
4211 devm_kfree(card->dev, (void *)template.kcontrol_news);
Charles Keepax778ff5b2018-09-05 15:21:00 +01004212 snd_soc_dapm_free_kcontrol(card, &private_value,
4213 rtd->dai_link->num_params, w_param_text);
anish kumar19ad6832017-09-28 21:52:39 -07004214param_fail:
Nikesh Oswalc6615082015-02-02 17:06:44 +00004215 devm_kfree(card->dev, link_name);
Charles Keepax778ff5b2018-09-05 15:21:00 +01004216 return ERR_PTR(ret);
Mark Brownc74184e2012-04-04 22:12:09 +01004217}
4218
Cezary Rojewskida039802022-03-11 16:35:29 +01004219/**
4220 * snd_soc_dapm_new_dai_widgets - Create new DAPM widgets
4221 * @dapm: DAPM context
4222 * @dai: parent DAI
4223 *
4224 * Returns 0 on success, error code otherwise.
4225 */
Mark Brown888df392012-02-16 19:37:51 -08004226int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4227 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004228{
Mark Brown888df392012-02-16 19:37:51 -08004229 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004230 struct snd_soc_dapm_widget *w;
4231
Mark Brown888df392012-02-16 19:37:51 -08004232 WARN_ON(dapm->dev != dai->dev);
4233
4234 memset(&template, 0, sizeof(template));
4235 template.reg = SND_SOC_NOPM;
4236
4237 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01004238 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08004239 template.name = dai->driver->playback.stream_name;
4240 template.sname = dai->driver->playback.stream_name;
4241
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004242 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08004243 template.name);
4244
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004245 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01004246 if (IS_ERR(w))
4247 return PTR_ERR(w);
Mark Brown888df392012-02-16 19:37:51 -08004248
4249 w->priv = dai;
4250 dai->playback_widget = w;
4251 }
4252
4253 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01004254 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08004255 template.name = dai->driver->capture.stream_name;
4256 template.sname = dai->driver->capture.stream_name;
4257
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004258 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08004259 template.name);
4260
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004261 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01004262 if (IS_ERR(w))
4263 return PTR_ERR(w);
Mark Brown888df392012-02-16 19:37:51 -08004264
4265 w->priv = dai;
4266 dai->capture_widget = w;
4267 }
4268
4269 return 0;
4270}
Cezary Rojewskida039802022-03-11 16:35:29 +01004271EXPORT_SYMBOL_GPL(snd_soc_dapm_new_dai_widgets);
Mark Brown888df392012-02-16 19:37:51 -08004272
4273int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4274{
4275 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004276 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08004277 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08004278
4279 /* For each DAI widget... */
Kuninori Morimoto145966922020-03-09 13:08:21 +09004280 for_each_card_widgets(card, dai_w) {
Mark Brown46162742013-06-05 19:36:11 +01004281 switch (dai_w->id) {
4282 case snd_soc_dapm_dai_in:
4283 case snd_soc_dapm_dai_out:
4284 break;
4285 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02004286 continue;
Mark Brown46162742013-06-05 19:36:11 +01004287 }
Mark Brown888df392012-02-16 19:37:51 -08004288
Liam Girdwoode01b4f62018-06-14 20:26:42 +01004289 /* let users know there is no DAI to link */
4290 if (!dai_w->priv) {
4291 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4292 dai_w->name);
4293 continue;
4294 }
4295
Mark Brown888df392012-02-16 19:37:51 -08004296 dai = dai_w->priv;
4297
4298 /* ...find all widgets with the same stream and link them */
Kuninori Morimoto145966922020-03-09 13:08:21 +09004299 for_each_card_widgets(card, w) {
Mark Brown888df392012-02-16 19:37:51 -08004300 if (w->dapm != dai_w->dapm)
4301 continue;
4302
Mark Brown46162742013-06-05 19:36:11 +01004303 switch (w->id) {
4304 case snd_soc_dapm_dai_in:
4305 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08004306 continue;
Mark Brown46162742013-06-05 19:36:11 +01004307 default:
4308 break;
4309 }
Mark Brown888df392012-02-16 19:37:51 -08004310
Lars-Peter Clausena798c242015-07-21 11:51:35 +02004311 if (!w->sname || !strstr(w->sname, dai_w->sname))
Mark Brown888df392012-02-16 19:37:51 -08004312 continue;
4313
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004314 if (dai_w->id == snd_soc_dapm_dai_in) {
4315 src = dai_w;
4316 sink = w;
4317 } else {
4318 src = w;
4319 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08004320 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004321 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4322 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004323 }
4324 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02004325
Mark Brown888df392012-02-16 19:37:51 -08004326 return 0;
4327}
Liam Girdwood64a648c2011-07-25 11:15:15 +01004328
Charles Keepax595571c2020-04-09 19:12:07 +01004329static void dapm_connect_dai_routes(struct snd_soc_dapm_context *dapm,
4330 struct snd_soc_dai *src_dai,
4331 struct snd_soc_dapm_widget *src,
4332 struct snd_soc_dapm_widget *dai,
4333 struct snd_soc_dai *sink_dai,
4334 struct snd_soc_dapm_widget *sink)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004335{
Charles Keepax595571c2020-04-09 19:12:07 +01004336 dev_dbg(dapm->dev, "connected DAI link %s:%s -> %s:%s\n",
4337 src_dai->component->name, src->name,
4338 sink_dai->component->name, sink->name);
4339
4340 if (dai) {
4341 snd_soc_dapm_add_path(dapm, src, dai, NULL, NULL);
4342 src = dai;
4343 }
4344
4345 snd_soc_dapm_add_path(dapm, src, sink, NULL, NULL);
4346}
4347
4348static void dapm_connect_dai_pair(struct snd_soc_card *card,
4349 struct snd_soc_pcm_runtime *rtd,
4350 struct snd_soc_dai *codec_dai,
4351 struct snd_soc_dai *cpu_dai)
4352{
4353 struct snd_soc_dai_link *dai_link = rtd->dai_link;
4354 struct snd_soc_dapm_widget *dai, *codec, *playback_cpu, *capture_cpu;
Jerome Bruneta72706e2019-07-25 18:59:48 +02004355 struct snd_pcm_substream *substream;
4356 struct snd_pcm_str *streams = rtd->pcm->streams;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004357
Charles Keepax595571c2020-04-09 19:12:07 +01004358 if (dai_link->params) {
Charles Keepax778ff5b2018-09-05 15:21:00 +01004359 playback_cpu = cpu_dai->capture_widget;
4360 capture_cpu = cpu_dai->playback_widget;
4361 } else {
Charles Keepax595571c2020-04-09 19:12:07 +01004362 playback_cpu = cpu_dai->playback_widget;
4363 capture_cpu = cpu_dai->capture_widget;
Charles Keepax778ff5b2018-09-05 15:21:00 +01004364 }
4365
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004366 /* connect BE DAI playback if widgets are valid */
4367 codec = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004368
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004369 if (playback_cpu && codec) {
Charles Keepaxf4aa5e22020-05-26 17:19:30 +01004370 if (dai_link->params && !rtd->playback_widget) {
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004371 substream = streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
Charles Keepax595571c2020-04-09 19:12:07 +01004372 dai = snd_soc_dapm_new_dai(card, substream, "playback");
Charles Keepax9a1bb6002020-04-09 19:12:08 +01004373 if (IS_ERR(dai))
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004374 goto capture;
Charles Keepaxf4aa5e22020-05-26 17:19:30 +01004375 rtd->playback_widget = dai;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004376 }
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004377
Charles Keepax595571c2020-04-09 19:12:07 +01004378 dapm_connect_dai_routes(&card->dapm, cpu_dai, playback_cpu,
Charles Keepaxf4aa5e22020-05-26 17:19:30 +01004379 rtd->playback_widget,
Charles Keepax595571c2020-04-09 19:12:07 +01004380 codec_dai, codec);
Charles Keepaxe36a1d02018-09-10 15:28:39 +01004381 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004382
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004383capture:
4384 /* connect BE DAI capture if widgets are valid */
4385 codec = codec_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004386
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004387 if (codec && capture_cpu) {
Charles Keepaxf4aa5e22020-05-26 17:19:30 +01004388 if (dai_link->params && !rtd->capture_widget) {
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004389 substream = streams[SNDRV_PCM_STREAM_CAPTURE].substream;
Charles Keepax595571c2020-04-09 19:12:07 +01004390 dai = snd_soc_dapm_new_dai(card, substream, "capture");
Charles Keepax9a1bb6002020-04-09 19:12:08 +01004391 if (IS_ERR(dai))
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004392 return;
Charles Keepaxf4aa5e22020-05-26 17:19:30 +01004393 rtd->capture_widget = dai;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004394 }
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004395
Charles Keepax595571c2020-04-09 19:12:07 +01004396 dapm_connect_dai_routes(&card->dapm, codec_dai, codec,
Charles Keepaxf4aa5e22020-05-26 17:19:30 +01004397 rtd->capture_widget,
Charles Keepax595571c2020-04-09 19:12:07 +01004398 cpu_dai, capture_cpu);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004399 }
4400}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004401
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004402static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4403 int event)
4404{
4405 struct snd_soc_dapm_widget *w;
4406
Kuninori Morimoto0c01f6c2020-02-19 15:56:41 +09004407 w = snd_soc_dai_get_widget(dai, stream);
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004408
4409 if (w) {
Kuninori Morimotof2ff5fb2021-08-05 10:11:44 +09004410 unsigned int ep;
4411
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004412 dapm_mark_dirty(w, "stream event");
4413
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004414 if (w->id == snd_soc_dapm_dai_in) {
4415 ep = SND_SOC_DAPM_EP_SOURCE;
4416 dapm_widget_invalidate_input_paths(w);
4417 } else {
4418 ep = SND_SOC_DAPM_EP_SINK;
4419 dapm_widget_invalidate_output_paths(w);
4420 }
4421
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004422 switch (event) {
4423 case SND_SOC_DAPM_STREAM_START:
4424 w->active = 1;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004425 w->is_ep = ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004426 break;
4427 case SND_SOC_DAPM_STREAM_STOP:
4428 w->active = 0;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004429 w->is_ep = 0;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004430 break;
4431 case SND_SOC_DAPM_STREAM_SUSPEND:
4432 case SND_SOC_DAPM_STREAM_RESUME:
4433 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4434 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4435 break;
4436 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004437 }
4438}
4439
Benoit Cousson44ba2642014-07-08 23:19:36 +02004440void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4441{
Mengdong Lin1a497982015-11-18 02:34:11 -05004442 struct snd_soc_pcm_runtime *rtd;
Charles Keepax0eaef952020-04-09 19:12:09 +01004443 struct snd_soc_dai *codec_dai;
4444 int i;
Benoit Cousson44ba2642014-07-08 23:19:36 +02004445
4446 /* for each BE DAI link... */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00004447 for_each_card_rtds(card, rtd) {
Benoit Cousson44ba2642014-07-08 23:19:36 +02004448 /*
4449 * dynamic FE links have no fixed DAI mapping.
4450 * CODEC<->CODEC links have no direct connection.
4451 */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004452 if (rtd->dai_link->dynamic)
Benoit Cousson44ba2642014-07-08 23:19:36 +02004453 continue;
4454
Charles Keepax0eaef952020-04-09 19:12:09 +01004455 if (rtd->num_cpus == 1) {
4456 for_each_rtd_codec_dais(rtd, i, codec_dai)
4457 dapm_connect_dai_pair(card, rtd, codec_dai,
Mark Brown39400f32020-04-14 18:04:08 +01004458 asoc_rtd_to_cpu(rtd, 0));
Charles Keepax0eaef952020-04-09 19:12:09 +01004459 } else if (rtd->num_codecs == rtd->num_cpus) {
4460 for_each_rtd_codec_dais(rtd, i, codec_dai)
4461 dapm_connect_dai_pair(card, rtd, codec_dai,
Mark Brown39400f32020-04-14 18:04:08 +01004462 asoc_rtd_to_cpu(rtd, i));
Charles Keepax0eaef952020-04-09 19:12:09 +01004463 } else {
4464 dev_err(card->dev,
4465 "N cpus to M codecs link is not supported yet\n");
4466 }
Benoit Cousson44ba2642014-07-08 23:19:36 +02004467 }
4468}
4469
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004470static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4471 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004472{
Kuninori Morimotoe3c3cf72020-03-16 15:37:09 +09004473 struct snd_soc_dai *dai;
Benoit Cousson44ba2642014-07-08 23:19:36 +02004474 int i;
4475
Kuninori Morimotoe3c3cf72020-03-16 15:37:09 +09004476 for_each_rtd_dais(rtd, i, dai)
4477 soc_dapm_dai_stream_event(dai, stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004478
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004479 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004480}
4481
4482/**
4483 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4484 * @rtd: PCM runtime data
4485 * @stream: stream name
4486 * @event: stream event
4487 *
4488 * Sends a stream event to the dapm core. The core then makes any
4489 * necessary widget power changes.
4490 *
4491 * Returns 0 for success else error.
4492 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004493void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4494 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004495{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004496 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004497
Liam Girdwood3cd04342012-03-09 12:02:08 +00004498 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004499 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004500 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004501}
Richard Purdie2b97eab2006-10-06 18:32:18 +02004502
Kuninori Morimoto3f4cf792020-01-10 11:36:23 +09004503void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream)
4504{
4505 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
4506 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
4507 /* powered down playback stream now */
4508 snd_soc_dapm_stream_event(rtd,
4509 SNDRV_PCM_STREAM_PLAYBACK,
4510 SND_SOC_DAPM_STREAM_STOP);
4511 } else {
4512 /* start delayed pop wq here for playback streams */
4513 rtd->pop_wait = 1;
4514 queue_delayed_work(system_power_efficient_wq,
4515 &rtd->delayed_work,
4516 msecs_to_jiffies(rtd->pmdown_time));
4517 }
4518 } else {
4519 /* capture streams can be powered down now */
4520 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
4521 SND_SOC_DAPM_STREAM_STOP);
4522 }
4523}
4524EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop);
4525
Richard Purdie2b97eab2006-10-06 18:32:18 +02004526/**
Charles Keepax11391102014-02-18 15:22:14 +00004527 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4528 * @dapm: DAPM context
4529 * @pin: pin name
4530 *
4531 * Enables input/output pin and its parents or children widgets iff there is
4532 * a valid audio route and active audio stream.
4533 *
4534 * Requires external locking.
4535 *
4536 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4537 * do any widget power switching.
4538 */
4539int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4540 const char *pin)
4541{
4542 return snd_soc_dapm_set_pin(dapm, pin, 1);
4543}
4544EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4545
4546/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004547 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004548 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004549 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02004550 *
Mark Brown74b8f952009-06-06 11:26:15 +01004551 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01004552 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00004553 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004554 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4555 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02004556 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004557int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004558{
Charles Keepax11391102014-02-18 15:22:14 +00004559 int ret;
4560
4561 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4562
4563 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4564
4565 mutex_unlock(&dapm->card->dapm_mutex);
4566
4567 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004568}
Liam Girdwooda5302182008-07-07 13:35:17 +01004569EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004570
4571/**
Charles Keepax11391102014-02-18 15:22:14 +00004572 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4573 * @dapm: DAPM context
4574 * @pin: pin name
4575 *
4576 * Enables input/output pin regardless of any other state. This is
4577 * intended for use with microphone bias supplies used in microphone
4578 * jack detection.
4579 *
4580 * Requires external locking.
4581 *
4582 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4583 * do any widget power switching.
4584 */
4585int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4586 const char *pin)
4587{
4588 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4589
4590 if (!w) {
4591 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4592 return -EINVAL;
4593 }
4594
4595 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02004596 if (!w->connected) {
4597 /*
4598 * w->force does not affect the number of input or output paths,
4599 * so we only have to recheck if w->connected is changed
4600 */
4601 dapm_widget_invalidate_input_paths(w);
4602 dapm_widget_invalidate_output_paths(w);
4603 w->connected = 1;
4604 }
Charles Keepax11391102014-02-18 15:22:14 +00004605 w->force = 1;
4606 dapm_mark_dirty(w, "force enable");
4607
4608 return 0;
4609}
4610EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4611
4612/**
Mark Brownda341832010-03-15 19:23:37 +00004613 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004614 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00004615 * @pin: pin name
4616 *
4617 * Enables input/output pin regardless of any other state. This is
4618 * intended for use with microphone bias supplies used in microphone
4619 * jack detection.
4620 *
4621 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4622 * do any widget power switching.
4623 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004624int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4625 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00004626{
Charles Keepax11391102014-02-18 15:22:14 +00004627 int ret;
Mark Brownda341832010-03-15 19:23:37 +00004628
Charles Keepax11391102014-02-18 15:22:14 +00004629 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00004630
Charles Keepax11391102014-02-18 15:22:14 +00004631 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09004632
Charles Keepax11391102014-02-18 15:22:14 +00004633 mutex_unlock(&dapm->card->dapm_mutex);
4634
4635 return ret;
Mark Brownda341832010-03-15 19:23:37 +00004636}
4637EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4638
4639/**
Charles Keepax11391102014-02-18 15:22:14 +00004640 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4641 * @dapm: DAPM context
4642 * @pin: pin name
4643 *
4644 * Disables input/output pin and its parents or children widgets.
4645 *
4646 * Requires external locking.
4647 *
4648 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4649 * do any widget power switching.
4650 */
4651int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4652 const char *pin)
4653{
4654 return snd_soc_dapm_set_pin(dapm, pin, 0);
4655}
4656EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4657
4658/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004659 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004660 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004661 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004662 *
Mark Brown74b8f952009-06-06 11:26:15 +01004663 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00004664 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004665 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4666 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004667 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004668int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4669 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01004670{
Charles Keepax11391102014-02-18 15:22:14 +00004671 int ret;
4672
4673 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4674
4675 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4676
4677 mutex_unlock(&dapm->card->dapm_mutex);
4678
4679 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01004680}
4681EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4682
4683/**
Charles Keepax11391102014-02-18 15:22:14 +00004684 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4685 * @dapm: DAPM context
4686 * @pin: pin name
4687 *
4688 * Marks the specified pin as being not connected, disabling it along
4689 * any parent or child widgets. At present this is identical to
4690 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4691 * additional things such as disabling controls which only affect
4692 * paths through the pin.
4693 *
4694 * Requires external locking.
4695 *
4696 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4697 * do any widget power switching.
4698 */
4699int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4700 const char *pin)
4701{
4702 return snd_soc_dapm_set_pin(dapm, pin, 0);
4703}
4704EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4705
4706/**
Mark Brown5817b522008-09-24 11:23:11 +01004707 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004708 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01004709 * @pin: pin name
4710 *
4711 * Marks the specified pin as being not connected, disabling it along
4712 * any parent or child widgets. At present this is identical to
4713 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4714 * additional things such as disabling controls which only affect
4715 * paths through the pin.
4716 *
4717 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4718 * do any widget power switching.
4719 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004720int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01004721{
Charles Keepax11391102014-02-18 15:22:14 +00004722 int ret;
4723
4724 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4725
4726 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4727
4728 mutex_unlock(&dapm->card->dapm_mutex);
4729
4730 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01004731}
4732EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4733
4734/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004735 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004736 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004737 * @pin: audio signal pin endpoint (or start point)
4738 *
4739 * Get audio pin status - connected or disconnected.
4740 *
4741 * Returns 1 for connected otherwise 0.
4742 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004743int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4744 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004745{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004746 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004747
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004748 if (w)
4749 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06004750
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004751 return 0;
4752}
Liam Girdwooda5302182008-07-07 13:35:17 +01004753EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004754
4755/**
Mark Brown1547aba2010-05-07 21:11:40 +01004756 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004757 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01004758 * @pin: audio signal pin endpoint (or start point)
4759 *
4760 * Mark the given endpoint or pin as ignoring suspend. When the
4761 * system is disabled a path between two endpoints flagged as ignoring
4762 * suspend will not be disabled. The path must already be enabled via
4763 * normal means at suspend time, it will not be turned on if it was not
4764 * already enabled.
4765 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004766int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4767 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01004768{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004769 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01004770
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004771 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004772 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004773 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01004774 }
4775
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004776 w->ignore_suspend = 1;
4777
4778 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01004779}
4780EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4781
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02004782/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02004783 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03004784 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02004785 *
4786 * Free all dapm widgets and resources.
4787 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004788void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004789{
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02004790 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004791 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02004792 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004793}
4794EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4795
Kuninori Morimoto95c267d2019-08-23 09:58:52 +09004796void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
4797 struct snd_soc_card *card,
4798 struct snd_soc_component *component)
4799{
4800 dapm->card = card;
4801 dapm->component = component;
4802 dapm->bias_level = SND_SOC_BIAS_OFF;
4803
4804 if (component) {
4805 dapm->dev = component->dev;
Julia Lawalla1344da2020-10-11 11:19:39 +02004806 dapm->idle_bias_off = !component->driver->idle_bias_on;
Kuninori Morimoto95c267d2019-08-23 09:58:52 +09004807 dapm->suspend_bias_off = component->driver->suspend_bias_off;
4808 } else {
4809 dapm->dev = card->dev;
4810 }
4811
4812 INIT_LIST_HEAD(&dapm->list);
Kuninori Morimotodf817f82020-03-09 13:08:16 +09004813 /* see for_each_card_dapms */
Kuninori Morimoto95c267d2019-08-23 09:58:52 +09004814 list_add(&dapm->list, &card->dapm_list);
4815}
4816EXPORT_SYMBOL_GPL(snd_soc_dapm_init);
4817
Xiang Xiao57996352014-03-02 00:04:02 +08004818static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01004819{
Liam Girdwood01005a72012-07-06 16:57:05 +01004820 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01004821 struct snd_soc_dapm_widget *w;
4822 LIST_HEAD(down_list);
4823 int powerdown = 0;
4824
Liam Girdwood01005a72012-07-06 16:57:05 +01004825 mutex_lock(&card->dapm_mutex);
4826
Kuninori Morimoto145966922020-03-09 13:08:21 +09004827 for_each_card_widgets(dapm->card, w) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02004828 if (w->dapm != dapm)
4829 continue;
Mark Brown51737472009-06-22 13:16:51 +01004830 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004831 dapm_seq_insert(w, &down_list, false);
Charles Keepax9b319302020-02-28 15:31:45 +00004832 w->new_power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004833 powerdown = 1;
4834 }
4835 }
4836
4837 /* If there were no widgets to power down we're already in
4838 * standby.
4839 */
4840 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004841 if (dapm->bias_level == SND_SOC_BIAS_ON)
4842 snd_soc_dapm_set_bias_level(dapm,
4843 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004844 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004845 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4846 snd_soc_dapm_set_bias_level(dapm,
4847 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004848 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004849
4850 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004851}
Mark Brown51737472009-06-22 13:16:51 +01004852
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004853/*
4854 * snd_soc_dapm_shutdown - callback for system shutdown
4855 */
4856void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4857{
Xiang Xiao57996352014-03-02 00:04:02 +08004858 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004859
Kuninori Morimotodf817f82020-03-09 13:08:16 +09004860 for_each_card_dapms(card, dapm) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08004861 if (dapm != &card->dapm) {
4862 soc_dapm_shutdown_dapm(dapm);
4863 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4864 snd_soc_dapm_set_bias_level(dapm,
4865 SND_SOC_BIAS_OFF);
4866 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004867 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08004868
4869 soc_dapm_shutdown_dapm(&card->dapm);
4870 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4871 snd_soc_dapm_set_bias_level(&card->dapm,
4872 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01004873}
4874
Richard Purdie2b97eab2006-10-06 18:32:18 +02004875/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004876MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004877MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4878MODULE_LICENSE("GPL");