blob: ca917a849c423de2348542547868f0eccac1964d [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
Richard Purdie2b97eab2006-10-06 18:32:18 +020065/* dapm power sequences - make this per codec in the future */
66static int dapm_up_seq[] = {
Pierre-Louis Bossartf13d4b52019-02-05 10:22:28 -060067 [snd_soc_dapm_pre] = 1,
68 [snd_soc_dapm_regulator_supply] = 2,
69 [snd_soc_dapm_pinctrl] = 2,
70 [snd_soc_dapm_clock_supply] = 2,
71 [snd_soc_dapm_supply] = 3,
72 [snd_soc_dapm_micbias] = 4,
73 [snd_soc_dapm_vmid] = 4,
74 [snd_soc_dapm_dai_link] = 3,
75 [snd_soc_dapm_dai_in] = 5,
76 [snd_soc_dapm_dai_out] = 5,
77 [snd_soc_dapm_aif_in] = 5,
78 [snd_soc_dapm_aif_out] = 5,
79 [snd_soc_dapm_mic] = 6,
80 [snd_soc_dapm_siggen] = 6,
81 [snd_soc_dapm_input] = 6,
82 [snd_soc_dapm_output] = 6,
83 [snd_soc_dapm_mux] = 7,
84 [snd_soc_dapm_demux] = 7,
85 [snd_soc_dapm_dac] = 8,
86 [snd_soc_dapm_switch] = 9,
87 [snd_soc_dapm_mixer] = 9,
88 [snd_soc_dapm_mixer_named_ctl] = 9,
89 [snd_soc_dapm_pga] = 10,
90 [snd_soc_dapm_buffer] = 10,
91 [snd_soc_dapm_scheduler] = 10,
92 [snd_soc_dapm_effect] = 10,
93 [snd_soc_dapm_src] = 10,
94 [snd_soc_dapm_asrc] = 10,
95 [snd_soc_dapm_encoder] = 10,
96 [snd_soc_dapm_decoder] = 10,
97 [snd_soc_dapm_adc] = 11,
98 [snd_soc_dapm_out_drv] = 12,
99 [snd_soc_dapm_hp] = 12,
100 [snd_soc_dapm_spk] = 12,
101 [snd_soc_dapm_line] = 12,
102 [snd_soc_dapm_sink] = 12,
103 [snd_soc_dapm_kcontrol] = 13,
104 [snd_soc_dapm_post] = 14,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200105};
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000106
Richard Purdie2b97eab2006-10-06 18:32:18 +0200107static int dapm_down_seq[] = {
Pierre-Louis Bossartf13d4b52019-02-05 10:22:28 -0600108 [snd_soc_dapm_pre] = 1,
109 [snd_soc_dapm_kcontrol] = 2,
110 [snd_soc_dapm_adc] = 3,
111 [snd_soc_dapm_hp] = 4,
112 [snd_soc_dapm_spk] = 4,
113 [snd_soc_dapm_line] = 4,
114 [snd_soc_dapm_out_drv] = 4,
115 [snd_soc_dapm_sink] = 4,
116 [snd_soc_dapm_pga] = 5,
117 [snd_soc_dapm_buffer] = 5,
118 [snd_soc_dapm_scheduler] = 5,
119 [snd_soc_dapm_effect] = 5,
120 [snd_soc_dapm_src] = 5,
121 [snd_soc_dapm_asrc] = 5,
122 [snd_soc_dapm_encoder] = 5,
123 [snd_soc_dapm_decoder] = 5,
124 [snd_soc_dapm_switch] = 6,
125 [snd_soc_dapm_mixer_named_ctl] = 6,
126 [snd_soc_dapm_mixer] = 6,
127 [snd_soc_dapm_dac] = 7,
128 [snd_soc_dapm_mic] = 8,
129 [snd_soc_dapm_siggen] = 8,
130 [snd_soc_dapm_input] = 8,
131 [snd_soc_dapm_output] = 8,
132 [snd_soc_dapm_micbias] = 9,
133 [snd_soc_dapm_vmid] = 9,
134 [snd_soc_dapm_mux] = 10,
135 [snd_soc_dapm_demux] = 10,
136 [snd_soc_dapm_aif_in] = 11,
137 [snd_soc_dapm_aif_out] = 11,
138 [snd_soc_dapm_dai_in] = 11,
139 [snd_soc_dapm_dai_out] = 11,
140 [snd_soc_dapm_dai_link] = 12,
141 [snd_soc_dapm_supply] = 13,
142 [snd_soc_dapm_clock_supply] = 14,
143 [snd_soc_dapm_pinctrl] = 14,
144 [snd_soc_dapm_regulator_supply] = 14,
145 [snd_soc_dapm_post] = 15,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200146};
147
Mark Brownf9fa2b12014-03-06 16:49:11 +0800148static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
149{
150 if (dapm->card && dapm->card->instantiated)
151 lockdep_assert_held(&dapm->card->dapm_mutex);
152}
153
Troy Kisky12ef1932008-10-13 17:42:14 -0700154static void pop_wait(u32 pop_time)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100155{
156 if (pop_time)
157 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
158}
159
Mathieu Malaterre595d2f72019-01-23 20:41:30 +0100160__printf(3, 4)
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200161static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100162{
163 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200164 char *buf;
165
166 if (!pop_time)
167 return;
168
169 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
170 if (buf == NULL)
171 return;
Mark Brown15e4c72f2008-07-02 11:51:20 +0100172
173 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200174 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100175 dev_info(dev, "%s", buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100176 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200177
178 kfree(buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100179}
180
Mark Browndb432b42011-10-03 21:06:40 +0100181static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
182{
183 return !list_empty(&w->dirty);
184}
185
Mark Brown492c0a12014-03-06 16:15:48 +0800186static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100187{
Mark Brownf9fa2b12014-03-06 16:49:11 +0800188 dapm_assert_locked(w->dapm);
189
Mark Brown75c1f892011-10-04 22:28:08 +0100190 if (!dapm_dirty_widget(w)) {
191 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
192 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100193 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100194 }
Mark Browndb432b42011-10-03 21:06:40 +0100195}
196
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200197/*
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200198 * Common implementation for dapm_widget_invalidate_input_paths() and
199 * dapm_widget_invalidate_output_paths(). The function is inlined since the
200 * combined size of the two specialized functions is only marginally larger then
201 * the size of the generic function and at the same time the fast path of the
202 * specialized functions is significantly smaller than the generic function.
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200203 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200204static __always_inline void dapm_widget_invalidate_paths(
205 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200206{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200207 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
208 struct snd_soc_dapm_widget *node;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200209 struct snd_soc_dapm_path *p;
210 LIST_HEAD(list);
211
212 dapm_assert_locked(w->dapm);
213
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200214 if (w->endpoints[dir] == -1)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200215 return;
216
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200217 list_add_tail(&w->work_list, &list);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200218 w->endpoints[dir] = -1;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200219
220 list_for_each_entry(w, &list, work_list) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200221 snd_soc_dapm_widget_for_each_path(w, dir, p) {
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200222 if (p->is_supply || p->weak || !p->connect)
223 continue;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200224 node = p->node[rdir];
225 if (node->endpoints[dir] != -1) {
226 node->endpoints[dir] = -1;
227 list_add_tail(&node->work_list, &list);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200228 }
229 }
230 }
231}
232
233/*
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200234 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
235 * input paths
236 * @w: The widget for which to invalidate the cached number of input paths
237 *
238 * Resets the cached number of inputs for the specified widget and all widgets
239 * that can be reached via outcoming paths from the widget.
240 *
241 * This function must be called if the number of output paths for a widget might
242 * have changed. E.g. if the source state of a widget changes or a path is added
243 * or activated with the widget as the sink.
244 */
245static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
246{
247 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
248}
249
250/*
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200251 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
252 * output paths
253 * @w: The widget for which to invalidate the cached number of output paths
254 *
255 * Resets the cached number of outputs for the specified widget and all widgets
256 * that can be reached via incoming paths from the widget.
257 *
258 * This function must be called if the number of output paths for a widget might
259 * have changed. E.g. if the sink state of a widget changes or a path is added
260 * or activated with the widget as the source.
261 */
262static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
263{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200264 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200265}
266
267/*
268 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
269 * for the widgets connected to a path
270 * @p: The path to invalidate
271 *
272 * Resets the cached number of inputs for the sink of the path and the cached
273 * number of outputs for the source of the path.
274 *
275 * This function must be called when a path is added, removed or the connected
276 * state changes.
277 */
278static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
279{
280 /*
281 * Weak paths or supply paths do not influence the number of input or
282 * output paths of their neighbors.
283 */
284 if (p->weak || p->is_supply)
285 return;
286
287 /*
288 * The number of connected endpoints is the sum of the number of
289 * connected endpoints of all neighbors. If a node with 0 connected
290 * endpoints is either connected or disconnected that sum won't change,
291 * so there is no need to re-check the path.
292 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200293 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200294 dapm_widget_invalidate_input_paths(p->sink);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200295 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200296 dapm_widget_invalidate_output_paths(p->source);
297}
298
Lars-Peter Clausen8be4da292014-10-25 17:42:01 +0200299void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
Mark Browne2d32ff2012-08-31 17:38:32 -0700300{
Mark Browne2d32ff2012-08-31 17:38:32 -0700301 struct snd_soc_dapm_widget *w;
302
303 mutex_lock(&card->dapm_mutex);
304
Kuninori Morimoto145966922020-03-09 13:08:21 +0900305 for_each_card_widgets(card, w) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200306 if (w->is_ep) {
Lars-Peter Clausen8be4da292014-10-25 17:42:01 +0200307 dapm_mark_dirty(w, "Rechecking endpoints");
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200308 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200309 dapm_widget_invalidate_output_paths(w);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200310 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200311 dapm_widget_invalidate_input_paths(w);
312 }
Mark Browne2d32ff2012-08-31 17:38:32 -0700313 }
314
315 mutex_unlock(&card->dapm_mutex);
316}
Lars-Peter Clausen8be4da292014-10-25 17:42:01 +0200317EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
Mark Browne2d32ff2012-08-31 17:38:32 -0700318
Richard Purdie2b97eab2006-10-06 18:32:18 +0200319/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100320static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200321 const struct snd_soc_dapm_widget *_widget)
322{
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -0600323 struct snd_soc_dapm_widget *w;
324
325 w = kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
326 if (!w)
327 return NULL;
328
329 /*
330 * w->name is duplicated in caller, but w->sname isn't.
331 * Duplicate it here if defined
332 */
333 if (_widget->sname) {
334 w->sname = kstrdup_const(_widget->sname, GFP_KERNEL);
Dan Carpentera6d9cef2019-02-19 15:04:27 +0300335 if (!w->sname) {
336 kfree(w);
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -0600337 return NULL;
Dan Carpentera6d9cef2019-02-19 15:04:27 +0300338 }
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -0600339 }
340 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200341}
342
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200343struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200344 unsigned int value;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200345 struct snd_soc_dapm_widget *widget;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200346 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200347 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200348};
349
350static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100351 struct snd_kcontrol *kcontrol, const char *ctrl_name)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200352{
353 struct dapm_kcontrol_data *data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200354 struct soc_mixer_control *mc;
Charles Keepax561ed682015-05-01 12:37:26 +0100355 struct soc_enum *e;
Charles Keepax773da9b2015-05-01 12:37:25 +0100356 const char *name;
357 int ret;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200358
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200359 data = kzalloc(sizeof(*data), GFP_KERNEL);
Charles Keepax40b7bea2015-05-01 12:37:24 +0100360 if (!data)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200361 return -ENOMEM;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200362
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200363 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200364
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200365 switch (widget->id) {
366 case snd_soc_dapm_switch:
367 case snd_soc_dapm_mixer:
368 case snd_soc_dapm_mixer_named_ctl:
369 mc = (struct soc_mixer_control *)kcontrol->private_value;
370
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800371 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
372 dev_warn(widget->dapm->dev,
373 "ASoC: Unsupported stereo autodisable control '%s'\n",
374 ctrl_name);
375
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200376 if (mc->autodisable) {
377 struct snd_soc_dapm_widget template;
378
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100379 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
Charles Keepax773da9b2015-05-01 12:37:25 +0100380 "Autodisable");
381 if (!name) {
382 ret = -ENOMEM;
383 goto err_data;
384 }
385
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200386 memset(&template, 0, sizeof(template));
387 template.reg = mc->reg;
388 template.mask = (1 << fls(mc->max)) - 1;
389 template.shift = mc->shift;
390 if (mc->invert)
391 template.off_val = mc->max;
392 else
393 template.off_val = 0;
394 template.on_val = template.off_val;
395 template.id = snd_soc_dapm_kcontrol;
Charles Keepax773da9b2015-05-01 12:37:25 +0100396 template.name = name;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200397
Lars-Peter Clausen2daabd72013-08-30 17:39:33 +0200398 data->value = template.on_val;
399
Liam Girdwood02aa78a2015-05-25 18:21:17 +0100400 data->widget =
401 snd_soc_dapm_new_control_unlocked(widget->dapm,
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200402 &template);
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200403 kfree(name);
Linus Walleij37e1df82017-01-13 10:23:52 +0100404 if (IS_ERR(data->widget)) {
405 ret = PTR_ERR(data->widget);
406 goto err_data;
407 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200408 }
409 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200410 case snd_soc_dapm_demux:
Charles Keepax561ed682015-05-01 12:37:26 +0100411 case snd_soc_dapm_mux:
412 e = (struct soc_enum *)kcontrol->private_value;
413
414 if (e->autodisable) {
415 struct snd_soc_dapm_widget template;
416
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100417 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
Charles Keepax561ed682015-05-01 12:37:26 +0100418 "Autodisable");
419 if (!name) {
420 ret = -ENOMEM;
421 goto err_data;
422 }
423
424 memset(&template, 0, sizeof(template));
425 template.reg = e->reg;
Gyeongtaek Leeebf14742020-04-18 13:13:20 +0900426 template.mask = e->mask;
Charles Keepax561ed682015-05-01 12:37:26 +0100427 template.shift = e->shift_l;
428 template.off_val = snd_soc_enum_item_to_val(e, 0);
429 template.on_val = template.off_val;
430 template.id = snd_soc_dapm_kcontrol;
431 template.name = name;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200432
433 data->value = template.on_val;
434
Charles Keepaxffacb482015-06-26 10:39:43 +0100435 data->widget = snd_soc_dapm_new_control_unlocked(
436 widget->dapm, &template);
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200437 kfree(name);
Linus Walleij37e1df82017-01-13 10:23:52 +0100438 if (IS_ERR(data->widget)) {
439 ret = PTR_ERR(data->widget);
440 goto err_data;
441 }
Charles Keepax561ed682015-05-01 12:37:26 +0100442
443 snd_soc_dapm_add_path(widget->dapm, data->widget,
444 widget, NULL, NULL);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200445 }
446 break;
447 default:
448 break;
449 }
450
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200451 kcontrol->private_data = data;
452
453 return 0;
Charles Keepax773da9b2015-05-01 12:37:25 +0100454
Charles Keepax773da9b2015-05-01 12:37:25 +0100455err_data:
456 kfree(data);
457 return ret;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200458}
459
460static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
461{
462 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Srinivas Kandagatlaff2faf12018-06-04 12:13:26 +0100463
464 list_del(&data->paths);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200465 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200466 kfree(data);
467}
468
469static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
470 const struct snd_kcontrol *kcontrol)
471{
472 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
473
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200474 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200475}
476
477static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
478 struct snd_soc_dapm_widget *widget)
479{
480 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200481 struct snd_soc_dapm_widget_list *new_wlist;
482 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200483
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200484 if (data->wlist)
485 n = data->wlist->num_widgets + 1;
486 else
487 n = 1;
488
489 new_wlist = krealloc(data->wlist,
Gustavo A. R. Silva07597912019-05-23 15:36:37 -0500490 struct_size(new_wlist, widgets, n),
491 GFP_KERNEL);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200492 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200493 return -ENOMEM;
494
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200495 new_wlist->widgets[n - 1] = widget;
496 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200497
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200498 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200499
500 return 0;
501}
502
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200503static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
504 struct snd_soc_dapm_path *path)
505{
506 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
507
508 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200509}
510
511static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
512{
513 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
514
515 if (!data->widget)
516 return true;
517
518 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200519}
520
521static struct list_head *dapm_kcontrol_get_path_list(
522 const struct snd_kcontrol *kcontrol)
523{
524 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
525
526 return &data->paths;
527}
528
529#define dapm_kcontrol_for_each_path(path, kcontrol) \
530 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
531 list_kcontrol)
532
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530533unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200534{
535 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
536
537 return data->value;
538}
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530539EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200540
541static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
542 unsigned int value)
543{
544 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
545
546 if (data->value == value)
547 return false;
548
Gyeongtaek Leeebf14742020-04-18 13:13:20 +0900549 if (data->widget) {
550 switch (dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->id) {
551 case snd_soc_dapm_switch:
552 case snd_soc_dapm_mixer:
553 case snd_soc_dapm_mixer_named_ctl:
554 data->widget->on_val = value & data->widget->mask;
555 break;
556 case snd_soc_dapm_demux:
557 case snd_soc_dapm_mux:
558 data->widget->on_val = value >> data->widget->shift;
559 break;
560 default:
561 data->widget->on_val = value;
562 break;
563 }
564 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200565
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200566 data->value = value;
567
568 return true;
569}
570
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200571/**
Mythri P K93e39a12015-10-20 22:30:08 +0530572 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
573 * kcontrol
574 * @kcontrol: The kcontrol
575 */
576struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
577 struct snd_kcontrol *kcontrol)
578{
579 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
580}
581EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
582
583/**
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200584 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
585 * kcontrol
586 * @kcontrol: The kcontrol
587 *
588 * Note: This function must only be used on kcontrols that are known to have
589 * been registered for a CODEC. Otherwise the behaviour is undefined.
590 */
591struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
592 struct snd_kcontrol *kcontrol)
593{
594 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
595}
596EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
597
Liam Girdwood6c120e12012-02-15 15:15:34 +0000598static void dapm_reset(struct snd_soc_card *card)
599{
600 struct snd_soc_dapm_widget *w;
601
Mark Brownf9fa2b12014-03-06 16:49:11 +0800602 lockdep_assert_held(&card->dapm_mutex);
603
Liam Girdwood6c120e12012-02-15 15:15:34 +0000604 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
605
Kuninori Morimoto145966922020-03-09 13:08:21 +0900606 for_each_card_widgets(card, w) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200607 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000608 w->power_checked = false;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000609 }
610}
611
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200612static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
613{
614 if (!dapm->component)
615 return NULL;
616 return dapm->component->name_prefix;
617}
618
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900619static unsigned int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100620{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200621 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200622 return -EIO;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900623 return snd_soc_component_read(dapm->component, reg);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100624}
625
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200626static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
Bard Liao34775012014-04-17 20:12:56 +0800627 int reg, unsigned int mask, unsigned int value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100628{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200629 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200630 return -EIO;
Mark Brownfcf6c5e2014-12-15 13:08:48 +0000631 return snd_soc_component_update_bits(dapm->component, reg,
632 mask, value);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000633}
634
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200635static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
636 int reg, unsigned int mask, unsigned int value)
637{
638 if (!dapm->component)
639 return -EIO;
640 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
641}
642
Mark Browneb270e92013-10-09 13:52:52 +0100643static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
644{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200645 if (dapm->component)
646 snd_soc_component_async_complete(dapm->component);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100647}
648
Charles Keepax45a110a2015-05-11 13:50:30 +0100649static struct snd_soc_dapm_widget *
650dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
651{
652 struct snd_soc_dapm_widget *w = wcache->widget;
Charles Keepax45a110a2015-05-11 13:50:30 +0100653
654 if (w) {
Kuninori Morimoto7453d6d2021-08-05 10:10:55 +0900655 struct list_head *wlist = &w->dapm->card->widgets;
656 const int depth = 2;
657 int i = 0;
Charles Keepax45a110a2015-05-11 13:50:30 +0100658
659 list_for_each_entry_from(w, wlist, list) {
660 if (!strcmp(name, w->name))
661 return w;
662
663 if (++i == depth)
664 break;
665 }
666 }
667
668 return NULL;
669}
670
671static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
672 struct snd_soc_dapm_widget *w)
673{
674 wcache->widget = w;
675}
676
Mark Brown452c5ea2009-05-17 21:41:23 +0100677/**
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200678 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
679 * @dapm: The DAPM context for which to set the level
680 * @level: The level to set
681 *
682 * Forces the DAPM bias level to a specific state. It will call the bias level
683 * callback of DAPM context with the specified level. This will even happen if
684 * the context is already at the same level. Furthermore it will not go through
685 * the normal bias level sequencing, meaning any intermediate states between the
686 * current and the target state will not be entered.
687 *
688 * Note that the change in bias level is only temporary and the next time
689 * snd_soc_dapm_sync() is called the state will be set to the level as
690 * determined by the DAPM core. The function is mainly intended to be used to
691 * used during probe or resume from suspend to power up the device so
692 * initialization can be done, before the DAPM core takes over.
693 */
694int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
695 enum snd_soc_bias_level level)
696{
697 int ret = 0;
698
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900699 if (dapm->component)
700 ret = snd_soc_component_set_bias_level(dapm->component, level);
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200701
Lars-Peter Clausenf4bf8d72015-04-27 22:13:25 +0200702 if (ret == 0)
703 dapm->bias_level = level;
704
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200705 return ret;
706}
707EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
708
Mark Brown452c5ea2009-05-17 21:41:23 +0100709/**
710 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800711 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100712 * @level: level to configure
713 *
714 * Configure the bias (power) levels for the SoC audio device.
715 *
716 * Returns 0 for success else error.
717 */
Mark Browned5a4c42011-02-18 11:12:42 -0800718static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200719 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100720{
Mark Browned5a4c42011-02-18 11:12:42 -0800721 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100722 int ret = 0;
723
Mark Brown84e90932010-11-04 00:07:02 -0400724 trace_snd_soc_bias_level_start(card, level);
725
Kuninori Morimoto39caefd2020-05-28 10:49:35 +0900726 ret = snd_soc_card_set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100727 if (ret != 0)
728 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100729
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200730 if (!card || dapm != &card->dapm)
731 ret = snd_soc_dapm_force_bias_level(dapm, level);
Liam Girdwood4123128e2012-07-06 16:56:16 +0100732
Mark Brown171ec6b2011-06-06 18:15:19 +0100733 if (ret != 0)
734 goto out;
735
Kuninori Morimotod41278e2020-05-28 10:50:35 +0900736 ret = snd_soc_card_set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100737out:
Mark Brown84e90932010-11-04 00:07:02 -0400738 trace_snd_soc_bias_level_done(card, level);
739
Mark Brown452c5ea2009-05-17 21:41:23 +0100740 return ret;
741}
742
Mark Brown74b8f952009-06-06 11:26:15 +0100743/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200744static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200745 struct snd_soc_dapm_path *path, const char *control_name,
746 struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200747{
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200748 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200749 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Kuninori Morimotoaf6b57a2021-08-05 10:11:00 +0900750 unsigned int item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200751 int i;
752
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100753 if (e->reg != SND_SOC_NOPM) {
Kuninori Morimotoaf6b57a2021-08-05 10:11:00 +0900754 unsigned int val;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900755 val = soc_dapm_read(dapm, e->reg);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100756 val = (val >> e->shift_l) & e->mask;
757 item = snd_soc_enum_val_to_item(e, val);
758 } else {
759 /* since a virtual mux has no backing registers to
760 * decide which path to connect, it will try to match
761 * with the first enumeration. This is to ensure
762 * that the default mux choice (the first) will be
763 * correctly powered up during initialization.
764 */
765 item = 0;
766 }
767
Xie Yishengf9e0b4a2018-05-31 19:11:23 +0800768 i = match_string(e->texts, e->items, control_name);
769 if (i < 0)
770 return -ENODEV;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200771
Xie Yishengf9e0b4a2018-05-31 19:11:23 +0800772 path->name = e->texts[i];
773 path->connect = (i == item);
774 return 0;
775
Richard Purdie2b97eab2006-10-06 18:32:18 +0200776}
777
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100778/* set up initial codec paths */
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800779static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
780 int nth_path)
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100781{
782 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200783 p->sink->kcontrol_news[i].private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100784 unsigned int reg = mc->reg;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100785 unsigned int invert = mc->invert;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100786
787 if (reg != SND_SOC_NOPM) {
Kuninori Morimoto29155bb2021-08-05 10:11:05 +0900788 unsigned int shift = mc->shift;
789 unsigned int max = mc->max;
790 unsigned int mask = (1 << fls(max)) - 1;
791 unsigned int val = soc_dapm_read(p->sink->dapm, reg);
792
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800793 /*
794 * The nth_path argument allows this function to know
795 * which path of a kcontrol it is setting the initial
796 * status for. Ideally this would support any number
797 * of paths and channels. But since kcontrols only come
798 * in mono and stereo variants, we are limited to 2
799 * channels.
800 *
801 * The following code assumes for stereo controls the
802 * first path is the left channel, and all remaining
803 * paths are the right channel.
804 */
805 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
806 if (reg != mc->rreg)
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900807 val = soc_dapm_read(p->sink->dapm, mc->rreg);
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800808 val = (val >> mc->rshift) & mask;
809 } else {
810 val = (val >> shift) & mask;
811 }
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100812 if (invert)
813 val = max - val;
814 p->connect = !!val;
815 } else {
이경택3bbbb772020-03-31 16:55:16 +0900816 /* since a virtual mixer has no backing registers to
817 * decide which path to connect, it will try to match
818 * with initial state. This is to ensure
819 * that the default mixer choice will be
820 * correctly powered up during initialization.
821 */
822 p->connect = invert;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100823 }
824}
825
Mark Brown74b8f952009-06-06 11:26:15 +0100826/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200827static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200828 struct snd_soc_dapm_path *path, const char *control_name)
829{
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800830 int i, nth_path = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200831
832 /* search for mixer kcontrol */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200833 for (i = 0; i < path->sink->num_kcontrols; i++) {
834 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
835 path->name = path->sink->kcontrol_news[i].name;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800836 dapm_set_mixer_path_status(path, i, nth_path++);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200837 return 0;
838 }
839 }
840 return -ENODEV;
841}
842
Stephen Warrenaf468002011-04-28 17:38:01 -0600843static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600844 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600845 const struct snd_kcontrol_new *kcontrol_new,
846 struct snd_kcontrol **kcontrol)
847{
848 struct snd_soc_dapm_widget *w;
849 int i;
850
851 *kcontrol = NULL;
852
Kuninori Morimoto145966922020-03-09 13:08:21 +0900853 for_each_card_widgets(dapm->card, w) {
Stephen Warren1007da02011-05-26 09:57:33 -0600854 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
855 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600856 for (i = 0; i < w->num_kcontrols; i++) {
857 if (&w->kcontrol_news[i] == kcontrol_new) {
858 if (w->kcontrols)
859 *kcontrol = w->kcontrols[i];
860 return 1;
861 }
862 }
863 }
864
865 return 0;
866}
867
Stephen Warren85762e72013-03-29 15:40:10 -0600868/*
869 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
870 * create it. Either way, add the widget into the control's widget list
871 */
Jeeja KP19a2557b2015-10-20 22:30:06 +0530872static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100873 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200874{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200875 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000876 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000877 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600878 size_t prefix_len;
879 int shared;
880 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600881 bool wname_in_long_name, kcname_in_long_name;
Daniel Macke5092c92014-10-07 13:41:24 +0200882 char *long_name = NULL;
Stephen Warren85762e72013-03-29 15:40:10 -0600883 const char *name;
Daniel Macke5092c92014-10-07 13:41:24 +0200884 int ret = 0;
Mark Brownefb7ac32011-03-08 17:23:24 +0000885
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200886 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000887 if (prefix)
888 prefix_len = strlen(prefix) + 1;
889 else
890 prefix_len = 0;
891
Stephen Warren85762e72013-03-29 15:40:10 -0600892 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
893 &kcontrol);
894
Stephen Warren85762e72013-03-29 15:40:10 -0600895 if (!kcontrol) {
896 if (shared) {
897 wname_in_long_name = false;
898 kcname_in_long_name = true;
899 } else {
900 switch (w->id) {
901 case snd_soc_dapm_switch:
902 case snd_soc_dapm_mixer:
Jeeja KP19a2557b2015-10-20 22:30:06 +0530903 case snd_soc_dapm_pga:
Seppo Ingalsuo882c8b4a2019-04-30 18:11:34 -0500904 case snd_soc_dapm_effect:
Chen-Yu Tsaia3930ed2016-08-27 19:28:00 +0800905 case snd_soc_dapm_out_drv:
Stephen Warren85762e72013-03-29 15:40:10 -0600906 wname_in_long_name = true;
907 kcname_in_long_name = true;
908 break;
909 case snd_soc_dapm_mixer_named_ctl:
910 wname_in_long_name = false;
911 kcname_in_long_name = true;
912 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200913 case snd_soc_dapm_demux:
Stephen Warren85762e72013-03-29 15:40:10 -0600914 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600915 wname_in_long_name = true;
916 kcname_in_long_name = false;
917 break;
918 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600919 return -EINVAL;
920 }
921 }
922
923 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600924 /*
925 * The control will get a prefix from the control
926 * creation process but we're also using the same
927 * prefix for widgets so cut the prefix off the
928 * front of the widget name.
929 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200930 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600931 w->name + prefix_len,
932 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200933 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200934 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600935
936 name = long_name;
937 } else if (wname_in_long_name) {
938 long_name = NULL;
939 name = w->name + prefix_len;
940 } else {
941 long_name = NULL;
942 name = w->kcontrol_news[kci].name;
943 }
944
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200945 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600946 prefix);
Daniel Macke5092c92014-10-07 13:41:24 +0200947 if (!kcontrol) {
948 ret = -ENOMEM;
949 goto exit_free;
950 }
951
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200952 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200953
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100954 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200955 if (ret) {
956 snd_ctl_free_one(kcontrol);
Daniel Macke5092c92014-10-07 13:41:24 +0200957 goto exit_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200958 }
959
Stephen Warren85762e72013-03-29 15:40:10 -0600960 ret = snd_ctl_add(card, kcontrol);
961 if (ret < 0) {
962 dev_err(dapm->dev,
963 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
964 w->name, name, ret);
Daniel Macke5092c92014-10-07 13:41:24 +0200965 goto exit_free;
Stephen Warren85762e72013-03-29 15:40:10 -0600966 }
Stephen Warren85762e72013-03-29 15:40:10 -0600967 }
968
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200969 ret = dapm_kcontrol_add_widget(kcontrol, w);
Daniel Macke5092c92014-10-07 13:41:24 +0200970 if (ret == 0)
971 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200972
Daniel Macke5092c92014-10-07 13:41:24 +0200973exit_free:
974 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600975
Daniel Macke5092c92014-10-07 13:41:24 +0200976 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600977}
978
979/* create new dapm mixer control */
980static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
981{
982 int i, ret;
983 struct snd_soc_dapm_path *path;
Charles Keepax561ed682015-05-01 12:37:26 +0100984 struct dapm_kcontrol_data *data;
Stephen Warren85762e72013-03-29 15:40:10 -0600985
Richard Purdie2b97eab2006-10-06 18:32:18 +0200986 /* add kcontrol */
987 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200988 /* match name */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +0200989 snd_soc_dapm_widget_for_each_source_path(w, path) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200990 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600991 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200992 continue;
993
Charles Keepax561ed682015-05-01 12:37:26 +0100994 if (!w->kcontrols[i]) {
Jeeja KP19a2557b2015-10-20 22:30:06 +0530995 ret = dapm_create_or_share_kcontrol(w, i);
Charles Keepax561ed682015-05-01 12:37:26 +0100996 if (ret < 0)
997 return ret;
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200998 }
999
Mark Brown946d92a2013-08-12 23:28:42 +01001000 dapm_kcontrol_add_path(w->kcontrols[i], path);
Charles Keepax561ed682015-05-01 12:37:26 +01001001
1002 data = snd_kcontrol_chip(w->kcontrols[i]);
1003 if (data->widget)
1004 snd_soc_dapm_add_path(data->widget->dapm,
1005 data->widget,
1006 path->source,
1007 NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001008 }
1009 }
Stephen Warren85762e72013-03-29 15:40:10 -06001010
1011 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001012}
1013
1014/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001015static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001016{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001017 struct snd_soc_dapm_context *dapm = w->dapm;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001018 enum snd_soc_dapm_direction dir;
Stephen Warren85762e72013-03-29 15:40:10 -06001019 struct snd_soc_dapm_path *path;
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001020 const char *type;
Stephen Warrenaf468002011-04-28 17:38:01 -06001021 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001022
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001023 switch (w->id) {
1024 case snd_soc_dapm_mux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001025 dir = SND_SOC_DAPM_DIR_OUT;
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001026 type = "mux";
1027 break;
1028 case snd_soc_dapm_demux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001029 dir = SND_SOC_DAPM_DIR_IN;
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001030 type = "demux";
1031 break;
1032 default:
1033 return -EINVAL;
1034 }
1035
Stephen Warrenaf468002011-04-28 17:38:01 -06001036 if (w->num_kcontrols != 1) {
1037 dev_err(dapm->dev,
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001038 "ASoC: %s %s has incorrect number of controls\n", type,
Stephen Warrenaf468002011-04-28 17:38:01 -06001039 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001040 return -EINVAL;
1041 }
1042
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001043 if (list_empty(&w->edges[dir])) {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001044 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
Stephen Warren85762e72013-03-29 15:40:10 -06001045 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -06001046 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001047
Jeeja KP19a2557b2015-10-20 22:30:06 +05301048 ret = dapm_create_or_share_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -06001049 if (ret < 0)
1050 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -06001051
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001052 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1053 if (path->name)
1054 dapm_kcontrol_add_path(w->kcontrols[0], path);
Lars-Peter Clausen98407ef2014-10-25 17:41:57 +02001055 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001056
Stephen Warrenaf468002011-04-28 17:38:01 -06001057 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001058}
1059
1060/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001061static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001062{
Kuninori Morimotoa16cfb12021-08-05 10:11:10 +09001063 int i;
Jeeja KP19a2557b2015-10-20 22:30:06 +05301064
1065 for (i = 0; i < w->num_kcontrols; i++) {
Kuninori Morimotoa16cfb12021-08-05 10:11:10 +09001066 int ret = dapm_create_or_share_kcontrol(w, i);
Jeeja KP19a2557b2015-10-20 22:30:06 +05301067 if (ret < 0)
1068 return ret;
1069 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001070
Mark Browna6c65732010-03-03 17:45:21 +00001071 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001072}
1073
Nikesh Oswalc6615082015-02-02 17:06:44 +00001074/* create new dapm dai link control */
1075static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1076{
Kuninori Morimoto5c52e482021-08-05 10:11:15 +09001077 int i;
Charles Keepax243bcfa2018-09-05 15:21:02 +01001078 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00001079
1080 /* create control for links with > 1 config */
Charles Keepax243bcfa2018-09-05 15:21:02 +01001081 if (rtd->dai_link->num_params <= 1)
Nikesh Oswalc6615082015-02-02 17:06:44 +00001082 return 0;
1083
1084 /* add kcontrol */
1085 for (i = 0; i < w->num_kcontrols; i++) {
Kuninori Morimoto5c52e482021-08-05 10:11:15 +09001086 struct snd_soc_dapm_context *dapm = w->dapm;
1087 struct snd_card *card = dapm->card->snd_card;
1088 struct snd_kcontrol *kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
1089 w, w->name, NULL);
1090 int ret = snd_ctl_add(card, kcontrol);
1091
Nikesh Oswalc6615082015-02-02 17:06:44 +00001092 if (ret < 0) {
1093 dev_err(dapm->dev,
1094 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1095 w->name, w->kcontrol_news[i].name, ret);
1096 return ret;
1097 }
1098 kcontrol->private_data = w;
1099 w->kcontrols[i] = kcontrol;
1100 }
1101
1102 return 0;
1103}
1104
Mark Brown9949788b2010-05-07 20:24:05 +01001105/* We implement power down on suspend by checking the power state of
1106 * the ALSA card - when we are suspending the ALSA state for the card
1107 * is set to D3.
1108 */
1109static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1110{
Mark Brown12ea2c72011-03-02 18:17:32 +00001111 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +01001112
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001113 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +01001114 case SNDRV_CTL_POWER_D3hot:
1115 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +01001116 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001117 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001118 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +01001119 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +01001120 default:
1121 return 1;
1122 }
1123}
1124
Kuninori Morimoto52645e332020-02-19 15:56:52 +09001125static void dapm_widget_list_free(struct snd_soc_dapm_widget_list **list)
1126{
1127 kfree(*list);
1128}
1129
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001130static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1131 struct list_head *widgets)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001132{
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001133 struct snd_soc_dapm_widget *w;
1134 struct list_head *it;
1135 unsigned int size = 0;
1136 unsigned int i = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001137
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001138 list_for_each(it, widgets)
1139 size++;
1140
Kees Cookacafe7e2018-05-08 13:45:50 -07001141 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001142 if (*list == NULL)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001143 return -ENOMEM;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001144
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001145 list_for_each_entry(w, widgets, work_list)
1146 (*list)->widgets[i++] = w;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001147
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001148 (*list)->num_widgets = i;
1149
1150 return 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001151}
1152
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001153/*
Szymon Mielczarek872f3ac2019-08-09 10:40:34 +02001154 * Recursively reset the cached number of inputs or outputs for the specified
1155 * widget and all widgets that can be reached via incoming or outcoming paths
1156 * from the widget.
1157 */
1158static void invalidate_paths_ep(struct snd_soc_dapm_widget *widget,
1159 enum snd_soc_dapm_direction dir)
1160{
1161 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1162 struct snd_soc_dapm_path *path;
1163
1164 widget->endpoints[dir] = -1;
1165
1166 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1167 if (path->weak || path->is_supply)
1168 continue;
1169
1170 if (path->walking)
1171 return;
1172
1173 if (path->connect) {
1174 path->walking = 1;
1175 invalidate_paths_ep(path->node[dir], dir);
1176 path->walking = 0;
1177 }
1178 }
1179}
1180
1181/*
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001182 * Common implementation for is_connected_output_ep() and
1183 * is_connected_input_ep(). The function is inlined since the combined size of
1184 * the two specialized functions is only marginally larger then the size of the
1185 * generic function and at the same time the fast path of the specialized
1186 * functions is significantly smaller than the generic function.
1187 */
1188static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1189 struct list_head *list, enum snd_soc_dapm_direction dir,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001190 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1191 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1192 enum snd_soc_dapm_direction)),
1193 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1194 enum snd_soc_dapm_direction))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001195{
1196 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001197 struct snd_soc_dapm_path *path;
1198 int con = 0;
1199
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001200 if (widget->endpoints[dir] >= 0)
1201 return widget->endpoints[dir];
Mark Brown024dc072011-10-09 11:52:05 +01001202
Mark Brownde02d072011-09-20 21:43:24 +01001203 DAPM_UPDATE_STAT(widget, path_checks);
1204
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001205 /* do we need to add this widget to the list ? */
1206 if (list)
1207 list_add_tail(&widget->work_list, list);
1208
Jeeja KP09464972016-06-15 11:16:55 +05301209 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
Charles Keepax8dd26df2019-07-18 09:43:33 +01001210 list = NULL;
1211 custom_stop_condition = NULL;
Jeeja KP09464972016-06-15 11:16:55 +05301212 }
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001213
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001214 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1215 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1216 return widget->endpoints[dir];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001217 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001218
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001219 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
Mark Browne56235e02011-09-21 18:19:14 +01001220 DAPM_UPDATE_STAT(widget, neighbour_checks);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001221
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001222 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +01001223 continue;
1224
Mark Brown8af294b2013-02-22 17:48:15 +00001225 if (path->walking)
1226 return 1;
1227
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001228 trace_snd_soc_dapm_path(widget, dir, path);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001229
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001230 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +00001231 path->walking = 1;
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001232 con += fn(path->node[dir], list, custom_stop_condition);
Mark Brown8af294b2013-02-22 17:48:15 +00001233 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001234 }
1235 }
1236
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001237 widget->endpoints[dir] = con;
Mark Brown024dc072011-10-09 11:52:05 +01001238
Richard Purdie2b97eab2006-10-06 18:32:18 +02001239 return con;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001240}
1241
1242/*
1243 * Recursively check for a completed path to an active or physically connected
1244 * output widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001245 *
1246 * Optionally, can be supplied with a function acting as a stopping condition.
1247 * This function takes the dapm widget currently being examined and the walk
Charles Keepax8dd26df2019-07-18 09:43:33 +01001248 * direction as an arguments, it should return true if widgets from that point
1249 * in the graph onwards should not be added to the widget list.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001250 */
1251static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001252 struct list_head *list,
1253 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1254 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001255{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001256 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001257 is_connected_output_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001258}
1259
1260/*
1261 * Recursively check for a completed path to an active or physically connected
1262 * input widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001263 *
1264 * Optionally, can be supplied with a function acting as a stopping condition.
1265 * This function takes the dapm widget currently being examined and the walk
1266 * direction as an arguments, it should return true if the walk should be
1267 * stopped and false otherwise.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001268 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001269static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001270 struct list_head *list,
1271 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1272 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001273{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001274 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001275 is_connected_input_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001276}
1277
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001278/**
Mauro Carvalho Chehabf7b66032020-10-23 18:33:35 +02001279 * snd_soc_dapm_dai_get_connected_widgets - query audio path and it's widgets.
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001280 * @dai: the soc DAI.
1281 * @stream: stream direction.
1282 * @list: list of active widgets for this stream.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001283 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1284 * walk based on custom logic.
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001285 *
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001286 * Queries DAPM graph as to whether a valid audio stream path exists for
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001287 * the initial stream specified by name. This takes into account
1288 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1289 *
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001290 * Optionally, can be supplied with a function acting as a stopping condition.
1291 * This function takes the dapm widget currently being examined and the walk
1292 * direction as an arguments, it should return true if the walk should be
1293 * stopped and false otherwise.
1294 *
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001295 * Returns the number of valid paths or negative error.
1296 */
1297int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001298 struct snd_soc_dapm_widget_list **list,
1299 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1300 enum snd_soc_dapm_direction))
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001301{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01001302 struct snd_soc_card *card = dai->component->card;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001303 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001304 LIST_HEAD(widgets);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001305 int paths;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001306 int ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001307
1308 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001309
Szymon Mielczarek872f3ac2019-08-09 10:40:34 +02001310 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1311 w = dai->playback_widget;
1312 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_OUT);
1313 paths = is_connected_output_ep(w, &widgets,
1314 custom_stop_condition);
1315 } else {
1316 w = dai->capture_widget;
1317 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_IN);
1318 paths = is_connected_input_ep(w, &widgets,
1319 custom_stop_condition);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001320 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001321
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001322 /* Drop starting point */
1323 list_del(widgets.next);
1324
1325 ret = dapm_widget_list_create(list, &widgets);
1326 if (ret)
Lars-Peter Clausen30abbe72015-08-11 21:37:59 +02001327 paths = ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001328
1329 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001330 mutex_unlock(&card->dapm_mutex);
1331
1332 return paths;
1333}
Ranjani Sridharand1a7af02021-09-27 15:05:10 +03001334EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_get_connected_widgets);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001335
Kuninori Morimoto52645e332020-02-19 15:56:52 +09001336void snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list **list)
1337{
1338 dapm_widget_list_free(list);
1339}
Ranjani Sridharand1a7af02021-09-27 15:05:10 +03001340EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_free_widgets);
Kuninori Morimoto52645e332020-02-19 15:56:52 +09001341
Richard Purdie2b97eab2006-10-06 18:32:18 +02001342/*
Mark Brown62ea8742012-01-21 21:14:48 +00001343 * Handler for regulator supply widget.
1344 */
1345int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1346 struct snd_kcontrol *kcontrol, int event)
1347{
Mark Brownc05b84d2012-09-07 12:57:11 +08001348 int ret;
1349
Mark Browneb270e92013-10-09 13:52:52 +01001350 soc_dapm_async_complete(w->dapm);
1351
Mark Brownc05b84d2012-09-07 12:57:11 +08001352 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001353 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001354 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001355 if (ret != 0)
1356 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001357 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001358 w->name, ret);
1359 }
1360
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001361 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001362 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001363 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001364 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001365 if (ret != 0)
1366 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001367 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001368 w->name, ret);
1369 }
1370
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001371 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001372 }
Mark Brown62ea8742012-01-21 21:14:48 +00001373}
1374EXPORT_SYMBOL_GPL(dapm_regulator_event);
1375
Ola Liljad7e7eb92012-05-24 15:26:25 +02001376/*
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00001377 * Handler for pinctrl widget.
1378 */
1379int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1380 struct snd_kcontrol *kcontrol, int event)
1381{
1382 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1383 struct pinctrl *p = w->pinctrl;
1384 struct pinctrl_state *s;
1385
1386 if (!p || !priv)
1387 return -EIO;
1388
1389 if (SND_SOC_DAPM_EVENT_ON(event))
1390 s = pinctrl_lookup_state(p, priv->active_state);
1391 else
1392 s = pinctrl_lookup_state(p, priv->sleep_state);
1393
1394 if (IS_ERR(s))
1395 return PTR_ERR(s);
1396
1397 return pinctrl_select_state(p, s);
1398}
1399EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1400
1401/*
Ola Liljad7e7eb92012-05-24 15:26:25 +02001402 * Handler for clock supply widget.
1403 */
1404int dapm_clock_event(struct snd_soc_dapm_widget *w,
1405 struct snd_kcontrol *kcontrol, int event)
1406{
1407 if (!w->clk)
1408 return -EIO;
1409
Mark Browneb270e92013-10-09 13:52:52 +01001410 soc_dapm_async_complete(w->dapm);
1411
Ola Liljad7e7eb92012-05-24 15:26:25 +02001412 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001413 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001414 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001415 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001416 return 0;
1417 }
Charles Keepaxd78b1e432018-08-28 14:35:02 +01001418
Marek Belisko98b3cf12012-07-12 23:00:16 +02001419 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001420}
1421EXPORT_SYMBOL_GPL(dapm_clock_event);
1422
Mark Brownd805002b2011-09-28 18:28:23 +01001423static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1424{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001425 if (w->power_checked)
1426 return w->new_power;
1427
Mark Brownd805002b2011-09-28 18:28:23 +01001428 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001429 w->new_power = 1;
Mark Brownd805002b2011-09-28 18:28:23 +01001430 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001431 w->new_power = w->power_check(w);
1432
1433 w->power_checked = true;
1434
1435 return w->new_power;
Mark Brownd805002b2011-09-28 18:28:23 +01001436}
1437
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001438/* Generic check to see if a widget should be powered. */
Mark Browncd0f2d42009-04-20 16:56:59 +01001439static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1440{
1441 int in, out;
1442
Mark Brownde02d072011-09-20 21:43:24 +01001443 DAPM_UPDATE_STAT(w, power_checks);
1444
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001445 in = is_connected_input_ep(w, NULL, NULL);
1446 out = is_connected_output_ep(w, NULL, NULL);
Mark Browncd0f2d42009-04-20 16:56:59 +01001447 return out != 0 && in != 0;
1448}
1449
Mark Brown246d0a12009-04-22 18:24:55 +01001450/* Check to see if a power supply is needed */
1451static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1452{
1453 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001454
Mark Brownde02d072011-09-20 21:43:24 +01001455 DAPM_UPDATE_STAT(w, power_checks);
1456
Mark Brown246d0a12009-04-22 18:24:55 +01001457 /* Check if one of our outputs is connected */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001458 snd_soc_dapm_widget_for_each_sink_path(w, path) {
Mark Browna8fdac82011-09-28 18:20:26 +01001459 DAPM_UPDATE_STAT(w, neighbour_checks);
1460
Mark Brownbf3a9e12011-06-13 16:42:29 +01001461 if (path->weak)
1462 continue;
1463
Mark Brown215edda2009-09-08 18:59:05 +01001464 if (path->connected &&
1465 !path->connected(path->source, path->sink))
1466 continue;
1467
Mark Brownf68d7e12011-10-04 22:57:50 +01001468 if (dapm_widget_power_check(path->sink))
1469 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001470 }
1471
Mark Brownf68d7e12011-10-04 22:57:50 +01001472 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001473}
1474
Mark Brown35c64bc2011-09-28 18:23:53 +01001475static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1476{
Charles Keepax20bb0182015-12-02 10:22:16 +00001477 return w->connected;
Mark Brown35c64bc2011-09-28 18:23:53 +01001478}
1479
Mark Brown38357ab2009-06-06 19:03:23 +01001480static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1481 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001482 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001483{
Mark Brown828a8422011-01-15 13:14:30 +00001484 int *sort;
1485
Pierre-Louis Bossartf13d4b52019-02-05 10:22:28 -06001486 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1487 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1488
Mark Brown828a8422011-01-15 13:14:30 +00001489 if (power_up)
1490 sort = dapm_up_seq;
1491 else
1492 sort = dapm_down_seq;
1493
Pierre-Louis Bossartf13d4b52019-02-05 10:22:28 -06001494 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1495 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1496
Mark Brown38357ab2009-06-06 19:03:23 +01001497 if (sort[a->id] != sort[b->id])
1498 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001499 if (a->subseq != b->subseq) {
1500 if (power_up)
1501 return a->subseq - b->subseq;
1502 else
1503 return b->subseq - a->subseq;
1504 }
Mark Brownb22ead22009-06-07 12:51:26 +01001505 if (a->reg != b->reg)
1506 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001507 if (a->dapm != b->dapm)
1508 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001509
Mark Brown38357ab2009-06-06 19:03:23 +01001510 return 0;
1511}
Mark Brown42aa3412009-03-01 19:21:10 +00001512
Mark Brown38357ab2009-06-06 19:03:23 +01001513/* Insert a widget in order into a DAPM power sequence. */
1514static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1515 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001516 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001517{
1518 struct snd_soc_dapm_widget *w;
1519
1520 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001521 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001522 list_add_tail(&new_widget->power_list, &w->power_list);
1523 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001524 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001525
Mark Brown38357ab2009-06-06 19:03:23 +01001526 list_add_tail(&new_widget->power_list, list);
1527}
Mark Brown42aa3412009-03-01 19:21:10 +00001528
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001529static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001530 struct snd_soc_dapm_widget *w, int event)
1531{
Mark Brown68f89ad2010-11-03 23:51:49 -04001532 const char *ev_name;
Kuninori Morimoto65f73162021-08-05 10:11:20 +09001533 int power;
Mark Brown68f89ad2010-11-03 23:51:49 -04001534
1535 switch (event) {
1536 case SND_SOC_DAPM_PRE_PMU:
1537 ev_name = "PRE_PMU";
1538 power = 1;
1539 break;
1540 case SND_SOC_DAPM_POST_PMU:
1541 ev_name = "POST_PMU";
1542 power = 1;
1543 break;
1544 case SND_SOC_DAPM_PRE_PMD:
1545 ev_name = "PRE_PMD";
1546 power = 0;
1547 break;
1548 case SND_SOC_DAPM_POST_PMD:
1549 ev_name = "POST_PMD";
1550 power = 0;
1551 break;
Mark Brown80114122013-02-25 15:14:19 +00001552 case SND_SOC_DAPM_WILL_PMU:
1553 ev_name = "WILL_PMU";
1554 power = 1;
1555 break;
1556 case SND_SOC_DAPM_WILL_PMD:
1557 ev_name = "WILL_PMD";
1558 power = 0;
1559 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001560 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001561 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001562 return;
1563 }
1564
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001565 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001566 return;
1567
1568 if (w->event && (w->event_flags & event)) {
Kuninori Morimoto65f73162021-08-05 10:11:20 +09001569 int ret;
1570
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001571 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001572 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001573 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001574 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001575 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001576 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001577 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001578 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001579 ev_name, w->name, ret);
1580 }
1581}
1582
Mark Brownb22ead22009-06-07 12:51:26 +01001583/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001584static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001585 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001586{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001587 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001588 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001589 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001590 unsigned int value = 0;
1591 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001592
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001593 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1594 reg = w->reg;
1595 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001596
1597 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001598 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001599 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001600
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001601 mask |= w->mask << w->shift;
1602 if (w->power)
1603 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001604 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001605 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001606
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001607 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001608 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1609 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001610
Mark Brown68f89ad2010-11-03 23:51:49 -04001611 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001612 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1613 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001614 }
1615
Mark Brown81628102009-06-07 13:21:24 +01001616 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001617 /* Any widget will do, they should all be updating the
1618 * same register.
1619 */
Mark Brown29376bc2011-06-19 13:49:28 +01001620
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001621 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001622 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001623 value, mask, reg, card->pop_time);
1624 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001625 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001626 }
1627
1628 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001629 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1630 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001631 }
Mark Brown42aa3412009-03-01 19:21:10 +00001632}
1633
Mark Brownb22ead22009-06-07 12:51:26 +01001634/* Apply a DAPM power sequence.
1635 *
1636 * We walk over a pre-sorted list of widgets to apply power to. In
1637 * order to minimise the number of writes to the device required
1638 * multiple widgets will be updated in a single write where possible.
1639 * Currently anything that requires more than a single write is not
1640 * handled.
1641 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001642static void dapm_seq_run(struct snd_soc_card *card,
1643 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001644{
1645 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001646 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001647 LIST_HEAD(pending);
1648 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001649 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001650 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001651 struct snd_soc_dapm_context *cur_dapm = NULL;
Kuninori Morimotoa7165792021-08-05 10:11:24 +09001652 int i;
Mark Brown828a8422011-01-15 13:14:30 +00001653 int *sort;
1654
1655 if (power_up)
1656 sort = dapm_up_seq;
1657 else
1658 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001659
Mark Brownb22ead22009-06-07 12:51:26 +01001660 list_for_each_entry_safe(w, n, list, power_list) {
Kuninori Morimotoa7165792021-08-05 10:11:24 +09001661 int ret = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001662
1663 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001664 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001665 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001666 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001667 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001668
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001669 if (cur_dapm && cur_dapm->component) {
Mark Brown474b62d2011-01-18 16:14:44 +00001670 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1671 if (sort[i] == cur_sort)
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001672 snd_soc_component_seq_notifier(
1673 cur_dapm->component,
1674 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001675 }
1676
Mark Browneb270e92013-10-09 13:52:52 +01001677 if (cur_dapm && w->dapm != cur_dapm)
1678 soc_dapm_async_complete(cur_dapm);
1679
Mark Brownb22ead22009-06-07 12:51:26 +01001680 INIT_LIST_HEAD(&pending);
1681 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001682 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001683 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001684 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001685 }
1686
Mark Brown163cac02009-06-07 10:12:52 +01001687 switch (w->id) {
1688 case snd_soc_dapm_pre:
1689 if (!w->event)
Xiaomeng Tongf730a462022-03-29 09:21:34 +08001690 continue;
Mark Brown163cac02009-06-07 10:12:52 +01001691
Mark Brownb22ead22009-06-07 12:51:26 +01001692 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001693 ret = w->event(w,
1694 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001695 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001696 ret = w->event(w,
1697 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001698 break;
1699
1700 case snd_soc_dapm_post:
1701 if (!w->event)
Xiaomeng Tongf730a462022-03-29 09:21:34 +08001702 continue;
Mark Brown163cac02009-06-07 10:12:52 +01001703
Mark Brownb22ead22009-06-07 12:51:26 +01001704 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001705 ret = w->event(w,
1706 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001707 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001708 ret = w->event(w,
1709 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001710 break;
1711
Mark Brown163cac02009-06-07 10:12:52 +01001712 default:
Mark Brown81628102009-06-07 13:21:24 +01001713 /* Queue it up for application */
1714 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001715 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001716 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001717 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001718 list_move(&w->power_list, &pending);
1719 break;
Mark Brown163cac02009-06-07 10:12:52 +01001720 }
Mark Brownb22ead22009-06-07 12:51:26 +01001721
1722 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001723 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001724 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001725 }
Mark Brownb22ead22009-06-07 12:51:26 +01001726
1727 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001728 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001729
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001730 if (cur_dapm && cur_dapm->component) {
Mark Brown474b62d2011-01-18 16:14:44 +00001731 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1732 if (sort[i] == cur_sort)
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001733 snd_soc_component_seq_notifier(
1734 cur_dapm->component,
1735 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001736 }
Mark Browneb270e92013-10-09 13:52:52 +01001737
Kuninori Morimotodf817f82020-03-09 13:08:16 +09001738 for_each_card_dapms(card, d)
Mark Browneb270e92013-10-09 13:52:52 +01001739 soc_dapm_async_complete(d);
Mark Brown163cac02009-06-07 10:12:52 +01001740}
1741
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001742static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001743{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001744 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001745 struct snd_soc_dapm_widget_list *wlist;
1746 struct snd_soc_dapm_widget *w = NULL;
1747 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001748 int ret;
1749
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001750 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001751 return;
1752
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001753 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001754
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001755 for_each_dapm_widgets(wlist, wi, w) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001756 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1757 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1758 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001759 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001760 w->name, ret);
1761 }
Mark Brown97404f22010-12-14 16:13:57 +00001762 }
1763
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001764 if (!w)
1765 return;
1766
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001767 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1768 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001769 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001770 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001771 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001772
Chen-Yu Tsaie411b0b2016-11-02 15:35:58 +08001773 if (update->has_second_set) {
1774 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1775 update->mask2, update->val2);
1776 if (ret < 0)
1777 dev_err(w->dapm->dev,
1778 "ASoC: %s DAPM update failed: %d\n",
1779 w->name, ret);
1780 }
1781
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001782 for_each_dapm_widgets(wlist, wi, w) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001783 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1784 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1785 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001786 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001787 w->name, ret);
1788 }
Mark Brown97404f22010-12-14 16:13:57 +00001789 }
1790}
1791
Mark Brown9d0624a2011-02-18 11:49:43 -08001792/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1793 * they're changing state.
1794 */
1795static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1796{
1797 struct snd_soc_dapm_context *d = data;
1798 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001799
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001800 /* If we're off and we're not supposed to go into STANDBY */
Mark Brown56fba412011-06-04 11:25:10 +01001801 if (d->bias_level == SND_SOC_BIAS_OFF &&
1802 d->target_bias_level != SND_SOC_BIAS_OFF) {
Tzung-Bi Shih3aecfc72020-07-24 15:07:31 +08001803 if (d->dev && cookie)
Mark Brownf1aac482011-12-05 15:17:06 +00001804 pm_runtime_get_sync(d->dev);
1805
Mark Brown9d0624a2011-02-18 11:49:43 -08001806 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1807 if (ret != 0)
1808 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001809 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001810 }
1811
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001812 /* Prepare for a transition to ON or away from ON */
1813 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1814 d->bias_level != SND_SOC_BIAS_ON) ||
1815 (d->target_bias_level != SND_SOC_BIAS_ON &&
1816 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001817 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1818 if (ret != 0)
1819 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001820 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001821 }
1822}
1823
1824/* Async callback run prior to DAPM sequences - brings to their final
1825 * state.
1826 */
1827static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1828{
1829 struct snd_soc_dapm_context *d = data;
1830 int ret;
1831
1832 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001833 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1834 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1835 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001836 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1837 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001838 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001839 ret);
1840 }
1841
1842 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001843 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1844 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001845 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1846 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001847 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1848 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001849
Tzung-Bi Shih3aecfc72020-07-24 15:07:31 +08001850 if (d->dev && cookie)
Mark Brownfb644e92012-01-25 19:53:58 +00001851 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001852 }
1853
1854 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001855 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1856 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001857 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1858 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001859 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001860 ret);
1861 }
1862}
Mark Brown97404f22010-12-14 16:13:57 +00001863
Mark Brownfe4fda52011-10-03 22:36:57 +01001864static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1865 bool power, bool connect)
1866{
1867 /* If a connection is being made or broken then that update
1868 * will have marked the peer dirty, otherwise the widgets are
1869 * not connected and this update has no impact. */
1870 if (!connect)
1871 return;
1872
1873 /* If the peer is already in the state we're moving to then we
1874 * won't have an impact on it. */
1875 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001876 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001877}
1878
Mark Brown05623c42011-09-28 17:02:31 +01001879static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1880 struct list_head *up_list,
1881 struct list_head *down_list)
1882{
Mark Browndb432b42011-10-03 21:06:40 +01001883 struct snd_soc_dapm_path *path;
1884
Mark Brown05623c42011-09-28 17:02:31 +01001885 if (w->power == power)
1886 return;
1887
1888 trace_snd_soc_dapm_widget_power(w, power);
1889
Mark Browndb432b42011-10-03 21:06:40 +01001890 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001891 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001892 */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001893 snd_soc_dapm_widget_for_each_source_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001894 dapm_widget_set_peer_power(path->source, power, path->connect);
1895
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001896 /* Supplies can't affect their outputs, only their inputs */
1897 if (!w->is_supply) {
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001898 snd_soc_dapm_widget_for_each_sink_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001899 dapm_widget_set_peer_power(path->sink, power,
1900 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001901 }
1902
Mark Brown05623c42011-09-28 17:02:31 +01001903 if (power)
1904 dapm_seq_insert(w, up_list, true);
1905 else
1906 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001907}
1908
Mark Brown7c81beb2011-09-20 22:22:32 +01001909static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1910 struct list_head *up_list,
1911 struct list_head *down_list)
1912{
Mark Brown7c81beb2011-09-20 22:22:32 +01001913 int power;
1914
1915 switch (w->id) {
1916 case snd_soc_dapm_pre:
1917 dapm_seq_insert(w, down_list, false);
1918 break;
1919 case snd_soc_dapm_post:
1920 dapm_seq_insert(w, up_list, true);
1921 break;
1922
1923 default:
Mark Brownd805002b2011-09-28 18:28:23 +01001924 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001925
Mark Brown05623c42011-09-28 17:02:31 +01001926 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001927 break;
1928 }
1929}
1930
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001931static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1932{
1933 if (dapm->idle_bias_off)
1934 return true;
1935
1936 switch (snd_power_get_state(dapm->card->snd_card)) {
1937 case SNDRV_CTL_POWER_D3hot:
1938 case SNDRV_CTL_POWER_D3cold:
1939 return dapm->suspend_bias_off;
1940 default:
1941 break;
1942 }
1943
1944 return false;
1945}
1946
Mark Brown42aa3412009-03-01 19:21:10 +00001947/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001948 * Scan each dapm widget for complete audio path.
1949 * A complete path is a route that has valid endpoints i.e.:-
1950 *
1951 * o DAC to output pin.
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001952 * o Input pin to ADC.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001953 * o Input pin to Output pin (bypass, sidetone)
1954 * o DAC to ADC (loopback).
1955 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001956static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001957{
1958 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001959 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001960 LIST_HEAD(up_list);
1961 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001962 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001963 enum snd_soc_bias_level bias;
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +09001964 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001965
Mark Brownf9fa2b12014-03-06 16:49:11 +08001966 lockdep_assert_held(&card->dapm_mutex);
1967
Mark Brown84e90932010-11-04 00:07:02 -04001968 trace_snd_soc_dapm_start(card);
1969
Kuninori Morimotodf817f82020-03-09 13:08:16 +09001970 for_each_card_dapms(card, d) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001971 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001972 d->target_bias_level = SND_SOC_BIAS_OFF;
1973 else
1974 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001975 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001976
Liam Girdwood6c120e12012-02-15 15:15:34 +00001977 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001978
Mark Brown6d3ddc82009-05-16 17:47:29 +01001979 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001980 * lists indicating if they should be powered up or down. We
1981 * only check widgets that have been flagged as dirty but note
1982 * that new widgets may be added to the dirty list while we
1983 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001984 */
Mark Browndb432b42011-10-03 21:06:40 +01001985 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001986 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001987 }
1988
Kuninori Morimoto145966922020-03-09 13:08:21 +09001989 for_each_card_widgets(card, w) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001990 switch (w->id) {
1991 case snd_soc_dapm_pre:
1992 case snd_soc_dapm_post:
1993 /* These widgets always need to be powered */
1994 break;
1995 default:
1996 list_del_init(&w->dirty);
1997 break;
1998 }
Mark Browndb432b42011-10-03 21:06:40 +01001999
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02002000 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01002001 d = w->dapm;
2002
2003 /* Supplies and micbiases only bring the
2004 * context up to STANDBY as unless something
2005 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00002006 * generally don't require full power. Signal
2007 * generators are virtual pins and have no
2008 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01002009 */
2010 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00002011 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02002012 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00002013 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01002014 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002015 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00002016 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002017 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01002018 case snd_soc_dapm_micbias:
2019 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
2020 d->target_bias_level = SND_SOC_BIAS_STANDBY;
2021 break;
2022 default:
2023 d->target_bias_level = SND_SOC_BIAS_ON;
2024 break;
2025 }
2026 }
2027
2028 }
2029
Mark Brown85a843c2011-09-21 21:29:47 +01002030 /* Force all contexts in the card to the same bias state if
2031 * they're not ground referenced.
2032 */
Mark Brown56fba412011-06-04 11:25:10 +01002033 bias = SND_SOC_BIAS_OFF;
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002034 for_each_card_dapms(card, d)
Mark Brown56fba412011-06-04 11:25:10 +01002035 if (d->target_bias_level > bias)
2036 bias = d->target_bias_level;
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002037 for_each_card_dapms(card, d)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02002038 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01002039 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09002040
Mark Brownde02d072011-09-20 21:43:24 +01002041 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09002042
Xiang Xiao17282ba2014-03-02 00:04:03 +08002043 /* Run card bias changes at first */
2044 dapm_pre_sequence_async(&card->dapm, 0);
2045 /* Run other bias changes in parallel */
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002046 for_each_card_dapms(card, d) {
Jon Huntere03546d2018-08-17 16:35:43 +01002047 if (d != &card->dapm && d->bias_level != d->target_bias_level)
Xiang Xiao17282ba2014-03-02 00:04:03 +08002048 async_schedule_domain(dapm_pre_sequence_async, d,
2049 &async_domain);
2050 }
Mark Brown9d0624a2011-02-18 11:49:43 -08002051 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01002052
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02002053 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002054 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00002055 }
2056
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02002057 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002058 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00002059 }
2060
Mark Brown6d3ddc82009-05-16 17:47:29 +01002061 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002062 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01002063
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002064 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00002065
Mark Brown6d3ddc82009-05-16 17:47:29 +01002066 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002067 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002068
Mark Brown9d0624a2011-02-18 11:49:43 -08002069 /* Run all the bias changes in parallel */
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002070 for_each_card_dapms(card, d) {
Jon Huntere03546d2018-08-17 16:35:43 +01002071 if (d != &card->dapm && d->bias_level != d->target_bias_level)
Xiang Xiao17282ba2014-03-02 00:04:03 +08002072 async_schedule_domain(dapm_post_sequence_async, d,
2073 &async_domain);
2074 }
Mark Brown9d0624a2011-02-18 11:49:43 -08002075 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08002076 /* Run card bias changes at last */
2077 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01002078
Liam Girdwood8078d872012-02-15 15:15:35 +00002079 /* do we need to notify any clients that DAPM event is complete */
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002080 for_each_card_dapms(card, d) {
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +09002081 if (!d->component)
2082 continue;
2083
2084 ret = snd_soc_component_stream_event(d->component, event);
2085 if (ret < 0)
2086 return ret;
Liam Girdwood8078d872012-02-15 15:15:35 +00002087 }
2088
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002089 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02002090 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02002091 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01002092
Mark Brown84e90932010-11-04 00:07:02 -04002093 trace_snd_soc_dapm_done(card);
2094
Mark Brown42aa3412009-03-01 19:21:10 +00002095 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002096}
2097
Mark Brown79fb9382009-08-21 16:38:13 +01002098#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01002099static ssize_t dapm_widget_power_read_file(struct file *file,
2100 char __user *user_buf,
2101 size_t count, loff_t *ppos)
2102{
2103 struct snd_soc_dapm_widget *w = file->private_data;
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002104 struct snd_soc_card *card = w->dapm->card;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002105 enum snd_soc_dapm_direction dir, rdir;
Mark Brown79fb9382009-08-21 16:38:13 +01002106 char *buf;
2107 int in, out;
2108 ssize_t ret;
2109 struct snd_soc_dapm_path *p = NULL;
2110
2111 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2112 if (!buf)
2113 return -ENOMEM;
2114
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002115 mutex_lock(&card->dapm_mutex);
2116
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002117 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2118 if (w->is_supply) {
2119 in = 0;
2120 out = 0;
2121 } else {
Piotr Stankiewicz67420642016-05-13 17:03:55 +01002122 in = is_connected_input_ep(w, NULL, NULL);
2123 out = is_connected_output_ep(w, NULL, NULL);
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002124 }
Mark Brown79fb9382009-08-21 16:38:13 +01002125
Silvio Cesaree581e152019-01-12 16:28:43 +01002126 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
Mark Brownf13ebad2012-03-03 18:01:01 +00002127 w->name, w->power ? "On" : "Off",
2128 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01002129
Mark Brownd033c362009-12-04 15:25:56 +00002130 if (w->reg >= 0)
Silvio Cesaree581e152019-01-12 16:28:43 +01002131 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002132 " - R%d(0x%x) mask 0x%x",
2133 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00002134
Silvio Cesaree581e152019-01-12 16:28:43 +01002135 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
Mark Brownd033c362009-12-04 15:25:56 +00002136
Mark Brown3eef08b2009-09-14 16:49:00 +01002137 if (w->sname)
Silvio Cesaree581e152019-01-12 16:28:43 +01002138 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
Mark Brown3eef08b2009-09-14 16:49:00 +01002139 w->sname,
2140 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01002141
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002142 snd_soc_dapm_for_each_direction(dir) {
2143 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2144 snd_soc_dapm_widget_for_each_path(w, dir, p) {
KaiChieh Chuang28735af2018-02-05 13:00:00 +08002145 if (p->connected && !p->connected(p->source, p->sink))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002146 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002147
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002148 if (!p->connect)
2149 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002150
Silvio Cesaree581e152019-01-12 16:28:43 +01002151 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002152 " %s \"%s\" \"%s\"\n",
2153 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
Mark Brown79fb9382009-08-21 16:38:13 +01002154 p->name ? p->name : "static",
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002155 p->node[rdir]->name);
2156 }
Mark Brown79fb9382009-08-21 16:38:13 +01002157 }
2158
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002159 mutex_unlock(&card->dapm_mutex);
2160
Mark Brown79fb9382009-08-21 16:38:13 +01002161 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2162
2163 kfree(buf);
2164 return ret;
2165}
2166
2167static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002168 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002169 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002170 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002171};
2172
Mark Brownef49e4f2011-04-04 20:48:13 +09002173static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2174 size_t count, loff_t *ppos)
2175{
2176 struct snd_soc_dapm_context *dapm = file->private_data;
2177 char *level;
2178
2179 switch (dapm->bias_level) {
2180 case SND_SOC_BIAS_ON:
2181 level = "On\n";
2182 break;
2183 case SND_SOC_BIAS_PREPARE:
2184 level = "Prepare\n";
2185 break;
2186 case SND_SOC_BIAS_STANDBY:
2187 level = "Standby\n";
2188 break;
2189 case SND_SOC_BIAS_OFF:
2190 level = "Off\n";
2191 break;
2192 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002193 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002194 level = "Unknown\n";
2195 break;
2196 }
2197
2198 return simple_read_from_buffer(user_buf, count, ppos, level,
2199 strlen(level));
2200}
2201
2202static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002203 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002204 .read = dapm_bias_read_file,
2205 .llseek = default_llseek,
2206};
2207
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002208void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2209 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002210{
Mark Brownceaea852019-06-21 12:33:57 +01002211 if (!parent || IS_ERR(parent))
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02002212 return;
2213
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002214 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2215
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +02002216 debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
2217 &dapm_bias_fops);
Mark Brown79fb9382009-08-21 16:38:13 +01002218}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002219
2220static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2221{
2222 struct snd_soc_dapm_context *dapm = w->dapm;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002223
2224 if (!dapm->debugfs_dapm || !w->name)
2225 return;
2226
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +02002227 debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
2228 &dapm_widget_power_fops);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002229}
2230
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002231static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2232{
2233 debugfs_remove_recursive(dapm->debugfs_dapm);
Kuninori Morimoto29040d12019-05-27 16:51:34 +09002234 dapm->debugfs_dapm = NULL;
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002235}
2236
Mark Brown79fb9382009-08-21 16:38:13 +01002237#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002238void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2239 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002240{
2241}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002242
2243static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2244{
2245}
2246
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002247static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2248{
2249}
2250
Mark Brown79fb9382009-08-21 16:38:13 +01002251#endif
2252
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002253/*
2254 * soc_dapm_connect_path() - Connects or disconnects a path
2255 * @path: The path to update
2256 * @connect: The new connect state of the path. True if the path is connected,
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002257 * false if it is disconnected.
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002258 * @reason: The reason why the path changed (for debugging only)
2259 */
2260static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2261 bool connect, const char *reason)
2262{
2263 if (path->connect == connect)
2264 return;
2265
2266 path->connect = connect;
2267 dapm_mark_dirty(path->source, reason);
2268 dapm_mark_dirty(path->sink, reason);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002269 dapm_path_invalidate(path);
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002270}
2271
Richard Purdie2b97eab2006-10-06 18:32:18 +02002272/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002273static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002274 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002275{
2276 struct snd_soc_dapm_path *path;
2277 int found = 0;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002278 bool connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002279
Mark Brownf9fa2b12014-03-06 16:49:11 +08002280 lockdep_assert_held(&card->dapm_mutex);
2281
Richard Purdie2b97eab2006-10-06 18:32:18 +02002282 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002283 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002284 found = 1;
2285 /* we now need to match the string in the enum to the path */
Jerome Brunetc3456a42019-05-15 15:18:55 +02002286 if (e && !(strcmp(path->name, e->texts[mux])))
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002287 connect = true;
2288 else
2289 connect = false;
2290
2291 soc_dapm_connect_path(path, connect, "mux update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002292 }
2293
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002294 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002295 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002296
Liam Girdwood618dae12012-04-25 12:12:51 +01002297 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002298}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002299
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002300int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002301 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2302 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002303{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002304 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002305 int ret;
2306
Liam Girdwood3cd04342012-03-09 12:02:08 +00002307 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002308 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002309 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002310 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002311 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002312 if (ret > 0)
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01002313 snd_soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002314 return ret;
2315}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002316EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002317
Milan plzik1b075e32008-01-10 14:39:46 +01002318/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002319static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002320 struct snd_kcontrol *kcontrol,
2321 int connect, int rconnect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002322{
2323 struct snd_soc_dapm_path *path;
2324 int found = 0;
2325
Mark Brownf9fa2b12014-03-06 16:49:11 +08002326 lockdep_assert_held(&card->dapm_mutex);
2327
Richard Purdie2b97eab2006-10-06 18:32:18 +02002328 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002329 dapm_kcontrol_for_each_path(path, kcontrol) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002330 /*
2331 * Ideally this function should support any number of
2332 * paths and channels. But since kcontrols only come
2333 * in mono and stereo variants, we are limited to 2
2334 * channels.
2335 *
2336 * The following code assumes for stereo controls the
2337 * first path (when 'found == 0') is the left channel,
2338 * and all remaining paths (when 'found == 1') are the
2339 * right channel.
2340 *
2341 * A stereo control is signified by a valid 'rconnect'
2342 * value, either 0 for unconnected, or >= 0 for connected.
2343 * This is chosen instead of using snd_soc_volsw_is_stereo,
2344 * so that the behavior of snd_soc_dapm_mixer_update_power
2345 * doesn't change even when the kcontrol passed in is
2346 * stereo.
2347 *
2348 * It passes 'connect' as the path connect status for
2349 * the left channel, and 'rconnect' for the right
2350 * channel.
2351 */
2352 if (found && rconnect >= 0)
2353 soc_dapm_connect_path(path, rconnect, "mixer update");
2354 else
2355 soc_dapm_connect_path(path, connect, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002356 found = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002357 }
2358
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002359 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002360 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002361
Liam Girdwood618dae12012-04-25 12:12:51 +01002362 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002363}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002364
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002365int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002366 struct snd_kcontrol *kcontrol, int connect,
2367 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002368{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002369 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002370 int ret;
2371
Liam Girdwood3cd04342012-03-09 12:02:08 +00002372 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002373 card->update = update;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002374 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002375 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002376 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002377 if (ret > 0)
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01002378 snd_soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002379 return ret;
2380}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002381EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002382
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002383static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2384 char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002385{
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002386 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002387 struct snd_soc_dapm_widget *w;
2388 int count = 0;
2389 char *state = "not set";
2390
Mark Brown47325072016-03-18 12:04:23 +00002391 /* card won't be set for the dummy component, as a spot fix
2392 * we're checking for that case specifically here but in future
2393 * we will ensure that the dummy component looks like others.
2394 */
2395 if (!cmpnt->card)
2396 return 0;
2397
Kuninori Morimoto145966922020-03-09 13:08:21 +09002398 for_each_card_widgets(cmpnt->card, w) {
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002399 if (w->dapm != dapm)
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002400 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002401
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002402 /* only display widgets that burn power */
Richard Purdie2b97eab2006-10-06 18:32:18 +02002403 switch (w->id) {
2404 case snd_soc_dapm_hp:
2405 case snd_soc_dapm_mic:
2406 case snd_soc_dapm_spk:
2407 case snd_soc_dapm_line:
2408 case snd_soc_dapm_micbias:
2409 case snd_soc_dapm_dac:
2410 case snd_soc_dapm_adc:
2411 case snd_soc_dapm_pga:
Seppo Ingalsuo882c8b4a2019-04-30 18:11:34 -05002412 case snd_soc_dapm_effect:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002413 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002414 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002415 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002416 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002417 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00002418 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002419 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002420 if (w->name)
2421 count += sprintf(buf + count, "%s: %s\n",
2422 w->name, w->power ? "On":"Off");
2423 break;
2424 default:
2425 break;
2426 }
2427 }
2428
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002429 switch (snd_soc_dapm_get_bias_level(dapm)) {
Mark Brown0be98982008-05-19 12:31:28 +02002430 case SND_SOC_BIAS_ON:
2431 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002432 break;
Mark Brown0be98982008-05-19 12:31:28 +02002433 case SND_SOC_BIAS_PREPARE:
2434 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002435 break;
Mark Brown0be98982008-05-19 12:31:28 +02002436 case SND_SOC_BIAS_STANDBY:
2437 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002438 break;
Mark Brown0be98982008-05-19 12:31:28 +02002439 case SND_SOC_BIAS_OFF:
2440 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002441 break;
2442 }
2443 count += sprintf(buf + count, "PM State: %s\n", state);
2444
2445 return count;
2446}
2447
Benoit Cousson44ba2642014-07-08 23:19:36 +02002448/* show dapm widget status in sys fs */
2449static ssize_t dapm_widget_show(struct device *dev,
2450 struct device_attribute *attr, char *buf)
2451{
2452 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Kuninori Morimoto0b7990e32018-09-03 02:12:56 +00002453 struct snd_soc_dai *codec_dai;
Benoit Cousson44ba2642014-07-08 23:19:36 +02002454 int i, count = 0;
2455
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002456 mutex_lock(&rtd->card->dapm_mutex);
2457
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09002458 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Kuninori Morimoto0b7990e32018-09-03 02:12:56 +00002459 struct snd_soc_component *cmpnt = codec_dai->component;
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002460
2461 count += dapm_widget_show_component(cmpnt, buf + count);
Benoit Cousson44ba2642014-07-08 23:19:36 +02002462 }
2463
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002464 mutex_unlock(&rtd->card->dapm_mutex);
2465
Benoit Cousson44ba2642014-07-08 23:19:36 +02002466 return count;
2467}
2468
Joe Perchesc828a892017-12-19 10:15:08 -08002469static DEVICE_ATTR_RO(dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002470
Takashi Iwaid29697d2015-01-30 20:16:37 +01002471struct attribute *soc_dapm_dev_attrs[] = {
2472 &dev_attr_dapm_widget.attr,
2473 NULL
2474};
Richard Purdie2b97eab2006-10-06 18:32:18 +02002475
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002476static void dapm_free_path(struct snd_soc_dapm_path *path)
2477{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002478 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2479 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002480 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002481 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002482 kfree(path);
2483}
2484
Cezary Rojewskida039802022-03-11 16:35:29 +01002485/**
2486 * snd_soc_dapm_free_widget - Free specified widget
2487 * @w: widget to free
2488 *
2489 * Removes widget from all paths and frees memory occupied by it.
2490 */
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002491void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2492{
2493 struct snd_soc_dapm_path *p, *next_p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002494 enum snd_soc_dapm_direction dir;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002495
2496 list_del(&w->list);
Thomas Hebb5c6679b2020-12-12 17:20:12 -08002497 list_del(&w->dirty);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002498 /*
2499 * remove source and sink paths associated to this widget.
2500 * While removing the path, remove reference to it from both
2501 * source and sink widgets so that path is removed only once.
2502 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002503 snd_soc_dapm_for_each_direction(dir) {
2504 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2505 dapm_free_path(p);
2506 }
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002507
2508 kfree(w->kcontrols);
Lars-Peter Clausen48068962015-07-21 18:11:08 +02002509 kfree_const(w->name);
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -06002510 kfree_const(w->sname);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002511 kfree(w);
2512}
Cezary Rojewskida039802022-03-11 16:35:29 +01002513EXPORT_SYMBOL_GPL(snd_soc_dapm_free_widget);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002514
Jyri Sarhafd589a12015-11-10 18:12:42 +02002515void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2516{
2517 dapm->path_sink_cache.widget = NULL;
2518 dapm->path_source_cache.widget = NULL;
2519}
2520
Richard Purdie2b97eab2006-10-06 18:32:18 +02002521/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002522static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002523{
2524 struct snd_soc_dapm_widget *w, *next_w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002525
Kuninori Morimoto145966922020-03-09 13:08:21 +09002526 for_each_card_widgets_safe(dapm->card, w, next_w) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002527 if (w->dapm != dapm)
2528 continue;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002529 snd_soc_dapm_free_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002530 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002531 snd_soc_dapm_reset_cache(dapm);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002532}
2533
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002534static struct snd_soc_dapm_widget *dapm_find_widget(
2535 struct snd_soc_dapm_context *dapm, const char *pin,
2536 bool search_other_contexts)
2537{
2538 struct snd_soc_dapm_widget *w;
2539 struct snd_soc_dapm_widget *fallback = NULL;
Shuming Fanae4fc532021-02-08 17:40:42 -06002540 char prefixed_pin[80];
2541 const char *pin_name;
2542 const char *prefix = soc_dapm_prefix(dapm);
2543
2544 if (prefix) {
2545 snprintf(prefixed_pin, sizeof(prefixed_pin), "%s %s",
2546 prefix, pin);
2547 pin_name = prefixed_pin;
2548 } else {
2549 pin_name = pin;
2550 }
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002551
Kuninori Morimoto145966922020-03-09 13:08:21 +09002552 for_each_card_widgets(dapm->card, w) {
Shuming Fanae4fc532021-02-08 17:40:42 -06002553 if (!strcmp(w->name, pin_name)) {
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002554 if (w->dapm == dapm)
2555 return w;
2556 else
2557 fallback = w;
2558 }
2559 }
2560
2561 if (search_other_contexts)
2562 return fallback;
2563
2564 return NULL;
2565}
2566
Takashi Iwai827b0912021-11-05 10:09:25 +01002567/*
2568 * set the DAPM pin status:
2569 * returns 1 when the value has been updated, 0 when unchanged, or a negative
2570 * error code; called from kcontrol put callback
2571 */
2572static int __snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2573 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002574{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002575 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Takashi Iwai5af82c82021-10-06 16:17:12 +02002576 int ret = 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002577
Mark Brownf9fa2b12014-03-06 16:49:11 +08002578 dapm_assert_locked(dapm);
2579
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002580 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002581 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002582 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002583 }
2584
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002585 if (w->connected != status) {
Mark Brown1a8b2d92012-02-16 11:50:07 -08002586 dapm_mark_dirty(w, "pin configuration");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002587 dapm_widget_invalidate_input_paths(w);
2588 dapm_widget_invalidate_output_paths(w);
Takashi Iwai5af82c82021-10-06 16:17:12 +02002589 ret = 1;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002590 }
Mark Brown1a8b2d92012-02-16 11:50:07 -08002591
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002592 w->connected = status;
2593 if (status == 0)
2594 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002595
Takashi Iwai5af82c82021-10-06 16:17:12 +02002596 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01002597}
2598
Takashi Iwai827b0912021-11-05 10:09:25 +01002599/*
2600 * similar as __snd_soc_dapm_set_pin(), but returns 0 when successful;
2601 * called from several API functions below
2602 */
2603static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2604 const char *pin, int status)
2605{
2606 int ret = __snd_soc_dapm_set_pin(dapm, pin, status);
2607
2608 return ret < 0 ? ret : 0;
2609}
2610
Richard Purdie2b97eab2006-10-06 18:32:18 +02002611/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002612 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2613 * @dapm: DAPM context
2614 *
2615 * Walks all dapm audio paths and powers widgets according to their
2616 * stream or path usage.
2617 *
2618 * Requires external locking.
2619 *
2620 * Returns 0 for success.
2621 */
2622int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2623{
2624 /*
2625 * Suppress early reports (eg, jacks syncing their state) to avoid
2626 * silly DAPM runs during card startup.
2627 */
2628 if (!dapm->card || !dapm->card->instantiated)
2629 return 0;
2630
2631 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2632}
2633EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2634
2635/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002636 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002637 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002638 *
2639 * Walks all dapm audio paths and powers widgets according to their
2640 * stream or path usage.
2641 *
2642 * Returns 0 for success.
2643 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002644int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002645{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002646 int ret;
2647
Liam Girdwood3cd04342012-03-09 12:02:08 +00002648 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002649 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002650 mutex_unlock(&dapm->card->dapm_mutex);
2651 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002652}
Liam Girdwooda5302182008-07-07 13:35:17 +01002653EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002654
Charles Keepax078a85f2019-01-31 13:30:18 +00002655static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2656 struct snd_soc_dapm_widget *w,
2657 int channels)
2658{
2659 switch (w->id) {
2660 case snd_soc_dapm_aif_out:
2661 case snd_soc_dapm_aif_in:
2662 break;
2663 default:
2664 return 0;
2665 }
2666
2667 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2668 w->channel < channels ? "Connecting" : "Disconnecting",
2669 p->source->name, p->sink->name);
2670
2671 if (w->channel < channels)
2672 soc_dapm_connect_path(p, true, "dai update");
2673 else
2674 soc_dapm_connect_path(p, false, "dai update");
2675
2676 return 0;
2677}
2678
2679static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2680 struct snd_pcm_hw_params *params,
2681 struct snd_soc_dai *dai)
2682{
2683 int dir = substream->stream;
2684 int channels = params_channels(params);
2685 struct snd_soc_dapm_path *p;
2686 struct snd_soc_dapm_widget *w;
2687 int ret;
2688
Kuninori Morimoto0c01f6c2020-02-19 15:56:41 +09002689 w = snd_soc_dai_get_widget(dai, dir);
Charles Keepax078a85f2019-01-31 13:30:18 +00002690
Charles Keepaxcf17a5f2019-02-06 11:13:59 +00002691 if (!w)
2692 return 0;
2693
Charles Keepax078a85f2019-01-31 13:30:18 +00002694 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2695 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2696
2697 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2698 ret = dapm_update_dai_chan(p, p->sink, channels);
2699 if (ret < 0)
2700 return ret;
2701 }
2702
2703 snd_soc_dapm_widget_for_each_source_path(w, p) {
2704 ret = dapm_update_dai_chan(p, p->source, channels);
2705 if (ret < 0)
2706 return ret;
2707 }
2708
2709 return 0;
2710}
2711
2712int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2713 struct snd_pcm_hw_params *params,
2714 struct snd_soc_dai *dai)
2715{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09002716 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Charles Keepax078a85f2019-01-31 13:30:18 +00002717 int ret;
2718
2719 mutex_lock_nested(&rtd->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2720 ret = dapm_update_dai_unlocked(substream, params, dai);
2721 mutex_unlock(&rtd->card->dapm_mutex);
2722
2723 return ret;
2724}
2725EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2726
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002727/*
2728 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2729 * @w: The widget for which to update the flags
2730 *
2731 * Some widgets have a dynamic category which depends on which neighbors they
2732 * are connected to. This function update the category for these widgets.
2733 *
2734 * This function must be called whenever a path is added or removed to a widget.
2735 */
2736static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2737{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002738 enum snd_soc_dapm_direction dir;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002739 struct snd_soc_dapm_path *p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002740 unsigned int ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002741
2742 switch (w->id) {
2743 case snd_soc_dapm_input:
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002744 /* On a fully routed card an input is never a source */
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002745 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002746 return;
2747 ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002748 snd_soc_dapm_widget_for_each_source_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002749 if (p->source->id == snd_soc_dapm_micbias ||
2750 p->source->id == snd_soc_dapm_mic ||
2751 p->source->id == snd_soc_dapm_line ||
2752 p->source->id == snd_soc_dapm_output) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002753 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002754 break;
2755 }
2756 }
2757 break;
2758 case snd_soc_dapm_output:
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002759 /* On a fully routed card a output is never a sink */
2760 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002761 return;
2762 ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002763 snd_soc_dapm_widget_for_each_sink_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002764 if (p->sink->id == snd_soc_dapm_spk ||
2765 p->sink->id == snd_soc_dapm_hp ||
2766 p->sink->id == snd_soc_dapm_line ||
2767 p->sink->id == snd_soc_dapm_input) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002768 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002769 break;
2770 }
2771 }
2772 break;
2773 case snd_soc_dapm_line:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002774 ep = 0;
2775 snd_soc_dapm_for_each_direction(dir) {
2776 if (!list_empty(&w->edges[dir]))
2777 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2778 }
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002779 break;
2780 default:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002781 return;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002782 }
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002783
2784 w->is_ep = ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002785}
2786
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002787static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2788 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2789 const char *control)
2790{
2791 bool dynamic_source = false;
2792 bool dynamic_sink = false;
2793
2794 if (!control)
2795 return 0;
2796
2797 switch (source->id) {
2798 case snd_soc_dapm_demux:
2799 dynamic_source = true;
2800 break;
2801 default:
2802 break;
2803 }
2804
2805 switch (sink->id) {
2806 case snd_soc_dapm_mux:
2807 case snd_soc_dapm_switch:
2808 case snd_soc_dapm_mixer:
2809 case snd_soc_dapm_mixer_named_ctl:
2810 dynamic_sink = true;
2811 break;
2812 default:
2813 break;
2814 }
2815
2816 if (dynamic_source && dynamic_sink) {
2817 dev_err(dapm->dev,
2818 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2819 source->name, control, sink->name);
2820 return -EINVAL;
2821 } else if (!dynamic_source && !dynamic_sink) {
2822 dev_err(dapm->dev,
2823 "Control not supported for path %s -> [%s] -> %s\n",
2824 source->name, control, sink->name);
2825 return -EINVAL;
2826 }
2827
2828 return 0;
2829}
2830
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002831static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2832 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2833 const char *control,
2834 int (*connected)(struct snd_soc_dapm_widget *source,
2835 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002836{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002837 struct snd_soc_dapm_widget *widgets[2];
2838 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002839 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002840 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002841
Lars-Peter Clausene409dfb2014-10-25 17:42:02 +02002842 if (wsink->is_supply && !wsource->is_supply) {
2843 dev_err(dapm->dev,
2844 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2845 wsource->name, wsink->name);
2846 return -EINVAL;
2847 }
2848
2849 if (connected && !wsource->is_supply) {
2850 dev_err(dapm->dev,
2851 "connected() callback only supported for supply widgets (%s -> %s)\n",
2852 wsource->name, wsink->name);
2853 return -EINVAL;
2854 }
2855
2856 if (wsource->is_supply && control) {
2857 dev_err(dapm->dev,
2858 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2859 wsource->name, control, wsink->name);
2860 return -EINVAL;
2861 }
2862
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002863 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2864 if (ret)
2865 return ret;
2866
Richard Purdie2b97eab2006-10-06 18:32:18 +02002867 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2868 if (!path)
2869 return -ENOMEM;
2870
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002871 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2872 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2873 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2874 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2875
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002876 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002877 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002878 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002879
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002880 if (wsource->is_supply || wsink->is_supply)
2881 path->is_supply = 1;
2882
Richard Purdie2b97eab2006-10-06 18:32:18 +02002883 /* connect static paths */
2884 if (control == NULL) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002885 path->connect = 1;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002886 } else {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002887 switch (wsource->id) {
2888 case snd_soc_dapm_demux:
2889 ret = dapm_connect_mux(dapm, path, control, wsource);
2890 if (ret)
2891 goto err;
2892 break;
2893 default:
2894 break;
2895 }
2896
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002897 switch (wsink->id) {
2898 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002899 ret = dapm_connect_mux(dapm, path, control, wsink);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002900 if (ret != 0)
2901 goto err;
2902 break;
2903 case snd_soc_dapm_switch:
2904 case snd_soc_dapm_mixer:
2905 case snd_soc_dapm_mixer_named_ctl:
2906 ret = dapm_connect_mixer(dapm, path, control);
2907 if (ret != 0)
2908 goto err;
2909 break;
2910 default:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002911 break;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002912 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002913 }
2914
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002915 list_add(&path->list, &dapm->card->paths);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002916 snd_soc_dapm_for_each_direction(dir)
2917 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002918
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002919 snd_soc_dapm_for_each_direction(dir) {
2920 dapm_update_widget_flags(widgets[dir]);
2921 dapm_mark_dirty(widgets[dir], "Route added");
2922 }
Mark Brownfabd0382012-07-05 17:20:06 +01002923
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002924 if (dapm->card->instantiated && path->connect)
2925 dapm_path_invalidate(path);
2926
Richard Purdie2b97eab2006-10-06 18:32:18 +02002927 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002928err:
2929 kfree(path);
2930 return ret;
2931}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002932
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002933static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002934 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002935{
2936 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2937 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2938 const char *sink;
2939 const char *source;
2940 char prefixed_sink[80];
2941 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002942 const char *prefix;
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002943 unsigned int sink_ref = 0;
2944 unsigned int source_ref = 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002945 int ret;
2946
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002947 prefix = soc_dapm_prefix(dapm);
2948 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002949 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002950 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002951 sink = prefixed_sink;
2952 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002953 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002954 source = prefixed_source;
2955 } else {
2956 sink = route->sink;
2957 source = route->source;
2958 }
2959
Charles Keepax45a110a2015-05-11 13:50:30 +01002960 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2961 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2962
2963 if (wsink && wsource)
2964 goto skip_search;
2965
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002966 /*
2967 * find src and dest widgets over all widgets but favor a widget from
2968 * current DAPM context
2969 */
Kuninori Morimoto145966922020-03-09 13:08:21 +09002970 for_each_card_widgets(dapm->card, w) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002971 if (!wsink && !(strcmp(w->name, sink))) {
2972 wtsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002973 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002974 wsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002975 if (wsource)
2976 break;
2977 }
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002978 sink_ref++;
2979 if (sink_ref > 1)
2980 dev_warn(dapm->dev,
2981 "ASoC: sink widget %s overwritten\n",
2982 w->name);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002983 continue;
2984 }
2985 if (!wsource && !(strcmp(w->name, source))) {
2986 wtsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002987 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002988 wsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002989 if (wsink)
2990 break;
2991 }
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002992 source_ref++;
2993 if (source_ref > 1)
2994 dev_warn(dapm->dev,
2995 "ASoC: source widget %s overwritten\n",
2996 w->name);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002997 }
2998 }
2999 /* use widget from another DAPM context if not found from this */
3000 if (!wsink)
3001 wsink = wtsink;
3002 if (!wsource)
3003 wsource = wtsource;
3004
3005 if (wsource == NULL) {
3006 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
3007 route->source);
3008 return -ENODEV;
3009 }
3010 if (wsink == NULL) {
3011 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
3012 route->sink);
3013 return -ENODEV;
3014 }
3015
Charles Keepax45a110a2015-05-11 13:50:30 +01003016skip_search:
3017 dapm_wcache_update(&dapm->path_sink_cache, wsink);
3018 dapm_wcache_update(&dapm->path_source_cache, wsource);
3019
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003020 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
3021 route->connected);
3022 if (ret)
3023 goto err;
3024
3025 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003026err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003027 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003028 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003029 return ret;
3030}
Mark Brown105f1c22008-05-13 14:52:19 +02003031
Mark Brownefcc3c62012-07-05 17:24:19 +01003032static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
3033 const struct snd_soc_dapm_route *route)
3034{
3035 struct snd_soc_dapm_path *path, *p;
3036 const char *sink;
3037 const char *source;
3038 char prefixed_sink[80];
3039 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003040 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01003041
3042 if (route->control) {
3043 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003044 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01003045 return -EINVAL;
3046 }
3047
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003048 prefix = soc_dapm_prefix(dapm);
3049 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01003050 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003051 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01003052 sink = prefixed_sink;
3053 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003054 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01003055 source = prefixed_source;
3056 } else {
3057 sink = route->sink;
3058 source = route->source;
3059 }
3060
3061 path = NULL;
3062 list_for_each_entry(p, &dapm->card->paths, list) {
3063 if (strcmp(p->source->name, source) != 0)
3064 continue;
3065 if (strcmp(p->sink->name, sink) != 0)
3066 continue;
3067 path = p;
3068 break;
3069 }
3070
3071 if (path) {
Kuninori Morimotofd5ad232021-08-05 10:11:28 +09003072 struct snd_soc_dapm_widget *wsource = path->source;
3073 struct snd_soc_dapm_widget *wsink = path->sink;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003074
3075 dapm_mark_dirty(wsource, "Route removed");
3076 dapm_mark_dirty(wsink, "Route removed");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003077 if (path->connect)
3078 dapm_path_invalidate(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01003079
Lars-Peter Clausen88722932013-06-14 13:16:53 +02003080 dapm_free_path(path);
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003081
3082 /* Update any path related flags */
3083 dapm_update_widget_flags(wsource);
3084 dapm_update_widget_flags(wsink);
Mark Brownefcc3c62012-07-05 17:24:19 +01003085 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003086 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01003087 source, sink);
3088 }
3089
3090 return 0;
3091}
3092
Mark Brown105f1c22008-05-13 14:52:19 +02003093/**
Mark Brown105f1c22008-05-13 14:52:19 +02003094 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003095 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02003096 * @route: audio routes
3097 * @num: number of routes
3098 *
3099 * Connects 2 dapm widgets together via a named audio path. The sink is
3100 * the widget receiving the audio signal, whilst the source is the sender
3101 * of the audio signal.
3102 *
3103 * Returns 0 for success else error. On error all resources can be freed
3104 * with a call to snd_soc_card_free().
3105 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003106int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02003107 const struct snd_soc_dapm_route *route, int num)
3108{
Kuninori Morimotofcb3f192021-08-05 10:11:33 +09003109 int i, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02003110
Stuart Hendersonf19c1812017-09-21 11:02:12 +01003111 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown105f1c22008-05-13 14:52:19 +02003112 for (i = 0; i < num; i++) {
Kuninori Morimotofcb3f192021-08-05 10:11:33 +09003113 int r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01003114 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003115 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
3116 route->source,
3117 route->control ? route->control : "direct",
3118 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01003119 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02003120 }
3121 route++;
3122 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003123 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02003124
Dan Carpenter60884c22012-04-13 22:25:43 +03003125 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02003126}
3127EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3128
Mark Brownefcc3c62012-07-05 17:24:19 +01003129/**
3130 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3131 * @dapm: DAPM context
3132 * @route: audio routes
3133 * @num: number of routes
3134 *
3135 * Removes routes from the DAPM context.
3136 */
3137int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3138 const struct snd_soc_dapm_route *route, int num)
3139{
Rajan Vajae066ea22016-02-11 11:23:35 +05303140 int i;
Mark Brownefcc3c62012-07-05 17:24:19 +01003141
Stuart Hendersonf19c1812017-09-21 11:02:12 +01003142 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownefcc3c62012-07-05 17:24:19 +01003143 for (i = 0; i < num; i++) {
3144 snd_soc_dapm_del_route(dapm, route);
3145 route++;
3146 }
3147 mutex_unlock(&dapm->card->dapm_mutex);
3148
Rajan Vajae066ea22016-02-11 11:23:35 +05303149 return 0;
Mark Brownefcc3c62012-07-05 17:24:19 +01003150}
3151EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3152
Mark Brownbf3a9e12011-06-13 16:42:29 +01003153static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3154 const struct snd_soc_dapm_route *route)
3155{
3156 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3157 route->source,
3158 true);
3159 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3160 route->sink,
3161 true);
3162 struct snd_soc_dapm_path *path;
3163 int count = 0;
3164
3165 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003166 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003167 route->source);
3168 return -ENODEV;
3169 }
3170
3171 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003172 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003173 route->sink);
3174 return -ENODEV;
3175 }
3176
3177 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003178 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003179 route->source, route->sink);
3180
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02003181 snd_soc_dapm_widget_for_each_sink_path(source, path) {
Mark Brownbf3a9e12011-06-13 16:42:29 +01003182 if (path->sink == sink) {
3183 path->weak = 1;
3184 count++;
3185 }
3186 }
3187
3188 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003189 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003190 route->source, route->sink);
3191 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003192 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003193 count, route->source, route->sink);
3194
3195 return 0;
3196}
3197
3198/**
3199 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3200 * @dapm: DAPM context
3201 * @route: audio routes
3202 * @num: number of routes
3203 *
3204 * Mark existing routes matching those specified in the passed array
3205 * as being weak, meaning that they are ignored for the purpose of
3206 * power decisions. The main intended use case is for sidetone paths
3207 * which couple audio between other independent paths if they are both
3208 * active in order to make the combination work better at the user
3209 * level but which aren't intended to be "used".
3210 *
3211 * Note that CODEC drivers should not use this as sidetone type paths
3212 * can frequently also be used as bypass paths.
3213 */
3214int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3215 const struct snd_soc_dapm_route *route, int num)
3216{
Kuninori Morimotofd136fdb2021-08-05 10:11:37 +09003217 int i;
Mark Brownbf3a9e12011-06-13 16:42:29 +01003218 int ret = 0;
3219
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003220 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01003221 for (i = 0; i < num; i++) {
Kuninori Morimotofd136fdb2021-08-05 10:11:37 +09003222 int err = snd_soc_dapm_weak_route(dapm, route);
Mark Brownbf3a9e12011-06-13 16:42:29 +01003223 if (err)
3224 ret = err;
3225 route++;
3226 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003227 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01003228
3229 return ret;
3230}
3231EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3232
Mark Brown105f1c22008-05-13 14:52:19 +02003233/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003234 * snd_soc_dapm_new_widgets - add new dapm widgets
Jonathan Corbet628536e2015-08-25 01:14:48 -06003235 * @card: card to be checked for new dapm widgets
Richard Purdie2b97eab2006-10-06 18:32:18 +02003236 *
3237 * Checks the codec for any new dapm widgets and creates them if found.
3238 *
3239 * Returns 0 for success.
3240 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02003241int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003242{
3243 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00003244 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003245
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003246 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003247
Kuninori Morimoto145966922020-03-09 13:08:21 +09003248 for_each_card_widgets(card, w)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003249 {
3250 if (w->new)
3251 continue;
3252
Stephen Warrenfad59882011-04-28 17:37:59 -06003253 if (w->num_kcontrols) {
Kees Cook6396bb22018-06-12 14:03:40 -07003254 w->kcontrols = kcalloc(w->num_kcontrols,
Stephen Warrenfad59882011-04-28 17:37:59 -06003255 sizeof(struct snd_kcontrol *),
3256 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003257 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003258 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06003259 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003260 }
Stephen Warrenfad59882011-04-28 17:37:59 -06003261 }
3262
Richard Purdie2b97eab2006-10-06 18:32:18 +02003263 switch(w->id) {
3264 case snd_soc_dapm_switch:
3265 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00003266 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003267 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003268 break;
3269 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003270 case snd_soc_dapm_demux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003271 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003272 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003273 case snd_soc_dapm_pga:
Seppo Ingalsuo882c8b4a2019-04-30 18:11:34 -05003274 case snd_soc_dapm_effect:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06003275 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003276 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003277 break;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003278 case snd_soc_dapm_dai_link:
3279 dapm_new_dai_link(w);
3280 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01003281 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003282 break;
3283 }
Mark Brownb66a70d2011-02-09 18:04:11 +00003284
3285 /* Read the initial power state from the device */
3286 if (w->reg >= 0) {
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +09003287 val = soc_dapm_read(w->dapm, w->reg);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08003288 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003289 val &= w->mask;
3290 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00003291 w->power = 1;
3292 }
3293
Richard Purdie2b97eab2006-10-06 18:32:18 +02003294 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003295
Mark Brown7508b122011-10-05 12:09:12 +01003296 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003297 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003298 }
3299
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003300 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3301 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003302 return 0;
3303}
3304EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3305
3306/**
3307 * snd_soc_dapm_get_volsw - dapm mixer get callback
3308 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003309 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003310 *
3311 * Callback to get the value of a dapm mixer control.
3312 *
3313 * Returns 0 for success.
3314 */
3315int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3316 struct snd_ctl_elem_value *ucontrol)
3317{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003318 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3319 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003320 struct soc_mixer_control *mc =
3321 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003322 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003323 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003324 int max = mc->max;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003325 unsigned int width = fls(max);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003326 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003327 unsigned int invert = mc->invert;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003328 unsigned int reg_val, val, rval = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003329
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003330 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003331 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +09003332 reg_val = soc_dapm_read(dapm, reg);
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003333 val = (reg_val >> shift) & mask;
3334
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +09003335 if (reg != mc->rreg)
3336 reg_val = soc_dapm_read(dapm, mc->rreg);
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003337
3338 if (snd_soc_volsw_is_stereo(mc))
3339 rval = (reg_val >> mc->rshift) & mask;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003340 } else {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003341 reg_val = dapm_kcontrol_get_value(kcontrol);
3342 val = reg_val & mask;
3343
3344 if (snd_soc_volsw_is_stereo(mc))
3345 rval = (reg_val >> width) & mask;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003346 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003347 mutex_unlock(&card->dapm_mutex);
3348
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003349 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003350 ucontrol->value.integer.value[0] = max - val;
3351 else
3352 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003353
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003354 if (snd_soc_volsw_is_stereo(mc)) {
3355 if (invert)
3356 ucontrol->value.integer.value[1] = max - rval;
3357 else
3358 ucontrol->value.integer.value[1] = rval;
3359 }
3360
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +09003361 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003362}
3363EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3364
3365/**
3366 * snd_soc_dapm_put_volsw - dapm mixer set callback
3367 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003368 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003369 *
3370 * Callback to set the value of a dapm mixer control.
3371 *
3372 * Returns 0 for success.
3373 */
3374int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3375 struct snd_ctl_elem_value *ucontrol)
3376{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003377 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3378 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003379 struct soc_mixer_control *mc =
3380 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003381 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003382 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003383 int max = mc->max;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003384 unsigned int width = fls(max);
3385 unsigned int mask = (1 << width) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003386 unsigned int invert = mc->invert;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003387 unsigned int val, rval = 0;
3388 int connect, rconnect = -1, change, reg_change = 0;
Fabio Estevam33d92452018-02-14 13:39:05 -02003389 struct snd_soc_dapm_update update = {};
Nenghua Cao52765972013-12-13 20:13:49 +08003390 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003391
3392 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02003393 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003394
3395 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003396 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003397
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003398 if (snd_soc_volsw_is_stereo(mc)) {
3399 rval = (ucontrol->value.integer.value[1] & mask);
3400 rconnect = !!rval;
3401 if (invert)
3402 rval = max - rval;
3403 }
3404
Liam Girdwood3cd04342012-03-09 12:02:08 +00003405 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003406
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003407 /* This assumes field width < (bits in unsigned int / 2) */
3408 if (width > sizeof(unsigned int) * 8 / 2)
3409 dev_warn(dapm->dev,
3410 "ASoC: control %s field width limit exceeded\n",
3411 kcontrol->id.name);
3412 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
Mark Brown283375c2009-12-07 18:09:03 +00003413
Jarkko Nikula18626c72014-06-09 14:20:29 +03003414 if (reg != SND_SOC_NOPM) {
Jarkko Nikula18626c72014-06-09 14:20:29 +03003415 val = val << shift;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003416 rval = rval << mc->rshift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02003417
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003418 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3419
3420 if (snd_soc_volsw_is_stereo(mc))
3421 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3422 mask << mc->rshift,
3423 rval);
Jarkko Nikula18626c72014-06-09 14:20:29 +03003424 }
3425
3426 if (change || reg_change) {
3427 if (reg_change) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003428 if (snd_soc_volsw_is_stereo(mc)) {
3429 update.has_second_set = true;
3430 update.reg2 = mc->rreg;
3431 update.mask2 = mask << mc->rshift;
3432 update.val2 = rval;
3433 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003434 update.kcontrol = kcontrol;
3435 update.reg = reg;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003436 update.mask = mask << shift;
Jarkko Nikula18626c72014-06-09 14:20:29 +03003437 update.val = val;
3438 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003439 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003440 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00003441
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003442 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3443 rconnect);
Mark Brown97404f22010-12-14 16:13:57 +00003444
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003445 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00003446 }
3447
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003448 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003449
3450 if (ret > 0)
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01003451 snd_soc_dpcm_runtime_update(card);
Nenghua Cao52765972013-12-13 20:13:49 +08003452
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003453 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003454}
3455EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3456
3457/**
3458 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3459 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003460 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003461 *
3462 * Callback to get the value of a dapm enumerated double mixer control.
3463 *
3464 * Returns 0 for success.
3465 */
3466int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3467 struct snd_ctl_elem_value *ucontrol)
3468{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003469 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Charles Keepax561ed682015-05-01 12:37:26 +01003470 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003471 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003472 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003473
Charles Keepax561ed682015-05-01 12:37:26 +01003474 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3475 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +09003476 reg_val = soc_dapm_read(dapm, e->reg);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003477 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003478 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003479 }
Charles Keepax561ed682015-05-01 12:37:26 +01003480 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003481
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003482 val = (reg_val >> e->shift_l) & e->mask;
3483 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3484 if (e->shift_l != e->shift_r) {
3485 val = (reg_val >> e->shift_r) & e->mask;
3486 val = snd_soc_enum_val_to_item(e, val);
3487 ucontrol->value.enumerated.item[1] = val;
3488 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003489
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003490 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003491}
3492EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3493
Tzung-Bi Shih8f486292020-02-14 18:57:43 +08003494/**
3495 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3496 * @kcontrol: mixer control
3497 * @ucontrol: control element information
3498 *
3499 * Callback to set the value of a dapm enumerated double mixer control.
3500 *
3501 * Returns 0 for success.
3502 */
3503int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3504 struct snd_ctl_elem_value *ucontrol)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003505{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003506 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3507 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003508 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003509 unsigned int *item = ucontrol->value.enumerated.item;
Charles Keepax561ed682015-05-01 12:37:26 +01003510 unsigned int val, change, reg_change = 0;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003511 unsigned int mask;
Fabio Estevam33d92452018-02-14 13:39:05 -02003512 struct snd_soc_dapm_update update = {};
Nenghua Cao52765972013-12-13 20:13:49 +08003513 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003514
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003515 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003516 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003517
3518 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003519 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003520 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003521 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003522 return -EINVAL;
Chen-Yu Tsai071133a2016-08-27 19:27:59 +08003523 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003524 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003525 }
3526
Tzung-Bi Shih8f486292020-02-14 18:57:43 +08003527 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003528
Charles Keepax561ed682015-05-01 12:37:26 +01003529 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00003530
Charles Keepax561ed682015-05-01 12:37:26 +01003531 if (e->reg != SND_SOC_NOPM)
3532 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3533
3534 if (change || reg_change) {
3535 if (reg_change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003536 update.kcontrol = kcontrol;
3537 update.reg = e->reg;
3538 update.mask = mask;
3539 update.val = val;
3540 card->update = &update;
3541 }
Charles Keepax561ed682015-05-01 12:37:26 +01003542 change |= reg_change;
Mark Brown3a655772009-10-05 17:23:30 +01003543
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003544 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003545
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003546 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003547 }
3548
Tzung-Bi Shih8f486292020-02-14 18:57:43 +08003549 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003550
3551 if (ret > 0)
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01003552 snd_soc_dpcm_runtime_update(card);
Nenghua Cao52765972013-12-13 20:13:49 +08003553
Mark Brown97404f22010-12-14 16:13:57 +00003554 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003555}
3556EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3557
3558/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003559 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3560 *
3561 * @kcontrol: mixer control
3562 * @uinfo: control element information
3563 *
3564 * Callback to provide information about a pin switch control.
3565 */
3566int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3567 struct snd_ctl_elem_info *uinfo)
3568{
3569 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3570 uinfo->count = 1;
3571 uinfo->value.integer.min = 0;
3572 uinfo->value.integer.max = 1;
3573
3574 return 0;
3575}
3576EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3577
3578/**
3579 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3580 *
3581 * @kcontrol: mixer control
3582 * @ucontrol: Value
3583 */
3584int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3585 struct snd_ctl_elem_value *ucontrol)
3586{
Mark Brown48a8c392012-02-14 17:11:15 -08003587 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003588 const char *pin = (const char *)kcontrol->private_value;
3589
Liam Girdwood3cd04342012-03-09 12:02:08 +00003590 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003591
3592 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003593 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003594
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003595 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003596
3597 return 0;
3598}
3599EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3600
3601/**
3602 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3603 *
3604 * @kcontrol: mixer control
3605 * @ucontrol: Value
3606 */
3607int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3608 struct snd_ctl_elem_value *ucontrol)
3609{
Mark Brown48a8c392012-02-14 17:11:15 -08003610 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003611 const char *pin = (const char *)kcontrol->private_value;
Takashi Iwai5af82c82021-10-06 16:17:12 +02003612 int ret;
Mark Brown8b37dbd2009-02-28 21:14:20 +00003613
Takashi Iwai827b0912021-11-05 10:09:25 +01003614 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3615 ret = __snd_soc_dapm_set_pin(&card->dapm, pin,
3616 !!ucontrol->value.integer.value[0]);
3617 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003618
Mark Brown48a8c392012-02-14 17:11:15 -08003619 snd_soc_dapm_sync(&card->dapm);
Takashi Iwai5af82c82021-10-06 16:17:12 +02003620 return ret;
Mark Brown8b37dbd2009-02-28 21:14:20 +00003621}
3622EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3623
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +01003624struct snd_soc_dapm_widget *
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003625snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003626 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003627{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003628 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003629 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003630 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00003631 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003632
3633 if ((w = dapm_cnew_widget(widget)) == NULL)
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003634 return ERR_PTR(-ENOMEM);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003635
Mark Brown62ea8742012-01-21 21:14:48 +00003636 switch (w->id) {
3637 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003638 w->regulator = devm_regulator_get(dapm->dev, w->name);
3639 if (IS_ERR(w->regulator)) {
3640 ret = PTR_ERR(w->regulator);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003641 goto request_failed;
Mark Brown62ea8742012-01-21 21:14:48 +00003642 }
Mark Brown8784c772013-01-10 19:33:47 +00003643
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003644 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003645 ret = regulator_allow_bypass(w->regulator, true);
3646 if (ret != 0)
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003647 dev_warn(dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003648 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003649 w->name, ret);
3650 }
Mark Brown62ea8742012-01-21 21:14:48 +00003651 break;
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003652 case snd_soc_dapm_pinctrl:
3653 w->pinctrl = devm_pinctrl_get(dapm->dev);
Charles Keepaxa5cd7e92018-08-28 14:35:03 +01003654 if (IS_ERR(w->pinctrl)) {
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003655 ret = PTR_ERR(w->pinctrl);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003656 goto request_failed;
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003657 }
Tzung-Bi Shihec06dc12020-02-19 17:38:38 +08003658
3659 /* set to sleep_state when initializing */
3660 dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD);
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003661 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003662 case snd_soc_dapm_clock_supply:
Mark Brown695594f12012-06-04 08:14:13 +01003663 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003664 if (IS_ERR(w->clk)) {
3665 ret = PTR_ERR(w->clk);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003666 goto request_failed;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003667 }
3668 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003669 default:
3670 break;
3671 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003672
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003673 prefix = soc_dapm_prefix(dapm);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003674 if (prefix)
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003675 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003676 else
Lars-Peter Clausen48068962015-07-21 18:11:08 +02003677 w->name = kstrdup_const(widget->name, GFP_KERNEL);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003678 if (w->name == NULL) {
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -06003679 kfree_const(w->sname);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003680 kfree(w);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003681 return ERR_PTR(-ENOMEM);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003682 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003683
Mark Brown7ca3a182011-10-08 14:04:50 +01003684 switch (w->id) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003685 case snd_soc_dapm_mic:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003686 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003687 w->power_check = dapm_generic_check_power;
3688 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003689 case snd_soc_dapm_input:
3690 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003691 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003692 w->power_check = dapm_generic_check_power;
3693 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003694 case snd_soc_dapm_spk:
3695 case snd_soc_dapm_hp:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003696 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003697 w->power_check = dapm_generic_check_power;
3698 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003699 case snd_soc_dapm_output:
3700 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003701 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003702 w->power_check = dapm_generic_check_power;
3703 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003704 case snd_soc_dapm_vmid:
3705 case snd_soc_dapm_siggen:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003706 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003707 w->power_check = dapm_always_on_check_power;
3708 break;
Vinod Koul56b44372015-11-23 21:22:30 +05303709 case snd_soc_dapm_sink:
3710 w->is_ep = SND_SOC_DAPM_EP_SINK;
3711 w->power_check = dapm_always_on_check_power;
3712 break;
3713
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003714 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003715 case snd_soc_dapm_demux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003716 case snd_soc_dapm_switch:
3717 case snd_soc_dapm_mixer:
3718 case snd_soc_dapm_mixer_named_ctl:
Mark Brown63c69a62013-07-18 22:03:01 +01003719 case snd_soc_dapm_adc:
3720 case snd_soc_dapm_aif_out:
3721 case snd_soc_dapm_dac:
3722 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003723 case snd_soc_dapm_pga:
Ranjani Sridharan6e3bfcf2019-03-01 19:08:53 -06003724 case snd_soc_dapm_buffer:
3725 case snd_soc_dapm_scheduler:
3726 case snd_soc_dapm_effect:
3727 case snd_soc_dapm_src:
3728 case snd_soc_dapm_asrc:
3729 case snd_soc_dapm_encoder:
3730 case snd_soc_dapm_decoder:
Mark Brown7ca3a182011-10-08 14:04:50 +01003731 case snd_soc_dapm_out_drv:
Mark Brown7ca3a182011-10-08 14:04:50 +01003732 case snd_soc_dapm_micbias:
Mark Brown7ca3a182011-10-08 14:04:50 +01003733 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003734 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02003735 case snd_soc_dapm_dai_out:
3736 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003737 w->power_check = dapm_generic_check_power;
3738 break;
3739 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003740 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003741 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003742 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003743 case snd_soc_dapm_kcontrol:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003744 w->is_supply = 1;
Mark Brown7ca3a182011-10-08 14:04:50 +01003745 w->power_check = dapm_supply_check_power;
3746 break;
3747 default:
3748 w->power_check = dapm_always_on_check_power;
3749 break;
3750 }
3751
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003752 w->dapm = dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003753 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003754 INIT_LIST_HEAD(&w->dirty);
Kuninori Morimoto145966922020-03-09 13:08:21 +09003755 /* see for_each_card_widgets */
Lars-Peter Clausen92fa1242015-05-01 18:02:42 +02003756 list_add_tail(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003757
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003758 snd_soc_dapm_for_each_direction(dir) {
3759 INIT_LIST_HEAD(&w->edges[dir]);
3760 w->endpoints[dir] = -1;
3761 }
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003762
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02003763 /* machine layer sets up unconnected pins and insertions */
Richard Purdie2b97eab2006-10-06 18:32:18 +02003764 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003765 return w;
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003766
3767request_failed:
3768 if (ret != -EPROBE_DEFER)
3769 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3770 w->name, ret);
3771
Wenwen Wang45004d62019-07-22 08:57:44 -05003772 kfree_const(w->sname);
3773 kfree(w);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003774 return ERR_PTR(ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003775}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003776
3777/**
Charles Keepax94e630a2018-09-05 15:20:59 +01003778 * snd_soc_dapm_new_control - create new dapm control
3779 * @dapm: DAPM context
3780 * @widget: widget template
3781 *
3782 * Creates new DAPM control based upon a template.
3783 *
3784 * Returns a widget pointer on success or an error pointer on failure
3785 */
3786struct snd_soc_dapm_widget *
3787snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3788 const struct snd_soc_dapm_widget *widget)
3789{
3790 struct snd_soc_dapm_widget *w;
3791
3792 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3793 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3794 mutex_unlock(&dapm->card->dapm_mutex);
3795
3796 return w;
3797}
3798EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3799
3800/**
Mark Brown4ba13272008-05-13 14:51:19 +02003801 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003802 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003803 * @widget: widget array
3804 * @num: number of widgets
3805 *
3806 * Creates new DAPM controls based upon the templates.
3807 *
3808 * Returns 0 for success else error.
3809 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003810int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003811 const struct snd_soc_dapm_widget *widget,
3812 int num)
3813{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003814 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003815 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003816
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003817 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003818 for (i = 0; i < num; i++) {
Kuninori Morimoto3dc72e422021-08-05 10:11:41 +09003819 struct snd_soc_dapm_widget *w = snd_soc_dapm_new_control_unlocked(dapm, widget);
Linus Walleij37e1df82017-01-13 10:23:52 +01003820 if (IS_ERR(w)) {
3821 ret = PTR_ERR(w);
Dan Carpenter60884c22012-04-13 22:25:43 +03003822 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003823 }
Mark Brown4ba13272008-05-13 14:51:19 +02003824 widget++;
3825 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003826 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003827 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003828}
3829EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3830
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003831static int
3832snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3833 struct snd_pcm_substream *substream)
Mark Brownc74184e2012-04-04 22:12:09 +01003834{
Charles Keepax4a75aae2018-09-05 15:21:01 +01003835 struct snd_soc_dapm_path *path;
Mark Brownc74184e2012-04-04 22:12:09 +01003836 struct snd_soc_dai *source, *sink;
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09003837 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Mark Brown9747cec2012-04-26 19:12:21 +01003838 struct snd_pcm_hw_params *params = NULL;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003839 const struct snd_soc_pcm_stream *config = NULL;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003840 struct snd_pcm_runtime *runtime = NULL;
Takashi Iwai3ba66fe2018-07-25 22:43:26 +02003841 unsigned int fmt;
Colin Ian Kingc8415832019-07-26 13:33:27 +01003842 int ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003843
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003844 params = kzalloc(sizeof(*params), GFP_KERNEL);
3845 if (!params)
3846 return -ENOMEM;
3847
Jerome Bruneta72706e2019-07-25 18:59:48 +02003848 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3849 if (!runtime) {
3850 ret = -ENOMEM;
3851 goto out;
3852 }
3853
3854 substream->runtime = runtime;
3855
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003856 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3857 snd_soc_dapm_widget_for_each_source_path(w, path) {
3858 source = path->source->priv;
3859
3860 ret = snd_soc_dai_startup(source, substream);
Kuninori Morimoto62462e02021-03-15 09:58:37 +09003861 if (ret < 0)
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003862 goto out;
Kuninori Morimoto62462e02021-03-15 09:58:37 +09003863
Kuninori Morimoto45eb8662020-05-15 09:46:33 +09003864 snd_soc_dai_activate(source, substream->stream);
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003865 }
3866
3867 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3868 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3869 sink = path->sink->priv;
3870
3871 ret = snd_soc_dai_startup(sink, substream);
Kuninori Morimoto62462e02021-03-15 09:58:37 +09003872 if (ret < 0)
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003873 goto out;
Kuninori Morimoto62462e02021-03-15 09:58:37 +09003874
Kuninori Morimoto45eb8662020-05-15 09:46:33 +09003875 snd_soc_dai_activate(sink, substream->stream);
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003876 }
3877
Jerome Bruneta72706e2019-07-25 18:59:48 +02003878 substream->hw_opened = 1;
3879
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003880 /*
3881 * Note: getting the config after .startup() gives a chance to
3882 * either party on the link to alter the configuration if
3883 * necessary
3884 */
Charles Keepax243bcfa2018-09-05 15:21:02 +01003885 config = rtd->dai_link->params + rtd->params_select;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003886 if (WARN_ON(!config)) {
3887 dev_err(w->dapm->dev, "ASoC: link config missing\n");
3888 ret = -EINVAL;
3889 goto out;
3890 }
Mark Brownc74184e2012-04-04 22:12:09 +01003891
Mark Brownc74184e2012-04-04 22:12:09 +01003892 /* Be a little careful as we don't want to overflow the mask array */
3893 if (config->formats) {
3894 fmt = ffs(config->formats) - 1;
3895 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003896 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003897 config->formats);
Mark Brownc74184e2012-04-04 22:12:09 +01003898
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003899 ret = -EINVAL;
Mark Brown9747cec2012-04-26 19:12:21 +01003900 goto out;
3901 }
Mark Brownc74184e2012-04-04 22:12:09 +01003902
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003903 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brown9747cec2012-04-26 19:12:21 +01003904 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003905 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003906 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003907 config->rate_max;
Mark Brown9747cec2012-04-26 19:12:21 +01003908 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003909 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003910 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003911 = config->channels_max;
3912
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003913 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3914 snd_soc_dapm_widget_for_each_source_path(w, path) {
3915 source = path->source->priv;
3916
3917 ret = snd_soc_dai_hw_params(source, substream, params);
3918 if (ret < 0)
3919 goto out;
3920
3921 dapm_update_dai_unlocked(substream, params, source);
3922 }
3923
3924 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3925 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3926 sink = path->sink->priv;
3927
3928 ret = snd_soc_dai_hw_params(sink, substream, params);
3929 if (ret < 0)
3930 goto out;
3931
3932 dapm_update_dai_unlocked(substream, params, sink);
3933 }
3934
Jerome Brunet9de98622019-07-25 18:59:49 +02003935 runtime->format = params_format(params);
3936 runtime->subformat = params_subformat(params);
3937 runtime->channels = params_channels(params);
3938 runtime->rate = params_rate(params);
3939
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003940out:
3941 kfree(params);
Colin Ian Kingc8415832019-07-26 13:33:27 +01003942 return ret;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003943}
3944
3945static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3946 struct snd_kcontrol *kcontrol, int event)
3947{
3948 struct snd_soc_dapm_path *path;
3949 struct snd_soc_dai *source, *sink;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003950 struct snd_pcm_substream *substream = w->priv;
3951 int ret = 0, saved_stream = substream->stream;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003952
3953 if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3954 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3955 return -EINVAL;
3956
Mark Brownc74184e2012-04-04 22:12:09 +01003957 switch (event) {
3958 case SND_SOC_DAPM_PRE_PMU:
Jerome Bruneta72706e2019-07-25 18:59:48 +02003959 ret = snd_soc_dai_link_event_pre_pmu(w, substream);
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003960 if (ret < 0)
3961 goto out;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003962
Mark Brownc74184e2012-04-04 22:12:09 +01003963 break;
3964
3965 case SND_SOC_DAPM_POST_PMU:
Charles Keepax4a75aae2018-09-05 15:21:01 +01003966 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3967 sink = path->sink->priv;
3968
Kuninori Morimoto62462e02021-03-15 09:58:37 +09003969 snd_soc_dai_digital_mute(sink, 0, SNDRV_PCM_STREAM_PLAYBACK);
Charles Keepax4a75aae2018-09-05 15:21:01 +01003970 ret = 0;
3971 }
Mark Brownc74184e2012-04-04 22:12:09 +01003972 break;
3973
3974 case SND_SOC_DAPM_PRE_PMD:
Charles Keepax4a75aae2018-09-05 15:21:01 +01003975 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3976 sink = path->sink->priv;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303977
Kuninori Morimoto62462e02021-03-15 09:58:37 +09003978 snd_soc_dai_digital_mute(sink, 1, SNDRV_PCM_STREAM_PLAYBACK);
Charles Keepax4a75aae2018-09-05 15:21:01 +01003979 ret = 0;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303980 }
3981
Jerome Bruneta72706e2019-07-25 18:59:48 +02003982 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003983 snd_soc_dapm_widget_for_each_source_path(w, path) {
3984 source = path->source->priv;
Kuninori Morimotoc304c9a2020-09-29 13:31:53 +09003985 snd_soc_dai_hw_free(source, substream, 0);
Jerome Brunet68c907f2019-07-25 18:59:44 +02003986 }
Charles Keepax3c01b0e2018-10-11 17:28:28 +01003987
Jerome Bruneta72706e2019-07-25 18:59:48 +02003988 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
Jerome Brunet68c907f2019-07-25 18:59:44 +02003989 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3990 sink = path->sink->priv;
Kuninori Morimotoc304c9a2020-09-29 13:31:53 +09003991 snd_soc_dai_hw_free(sink, substream, 0);
Jerome Brunet68c907f2019-07-25 18:59:44 +02003992 }
3993
Jerome Bruneta72706e2019-07-25 18:59:48 +02003994 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
Jerome Brunet68c907f2019-07-25 18:59:44 +02003995 snd_soc_dapm_widget_for_each_source_path(w, path) {
3996 source = path->source->priv;
Kuninori Morimoto45eb8662020-05-15 09:46:33 +09003997 snd_soc_dai_deactivate(source, substream->stream);
Kuninori Morimoto00a0b462020-09-28 09:00:40 +09003998 snd_soc_dai_shutdown(source, substream, 0);
Charles Keepax4a75aae2018-09-05 15:21:01 +01003999 }
4000
Jerome Bruneta72706e2019-07-25 18:59:48 +02004001 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
Charles Keepax4a75aae2018-09-05 15:21:01 +01004002 snd_soc_dapm_widget_for_each_sink_path(w, path) {
4003 sink = path->sink->priv;
Kuninori Morimoto45eb8662020-05-15 09:46:33 +09004004 snd_soc_dai_deactivate(sink, substream->stream);
Kuninori Morimoto00a0b462020-09-28 09:00:40 +09004005 snd_soc_dai_shutdown(sink, substream, 0);
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05304006 }
Mark Brownc74184e2012-04-04 22:12:09 +01004007 break;
4008
Jerome Bruneta72706e2019-07-25 18:59:48 +02004009 case SND_SOC_DAPM_POST_PMD:
4010 kfree(substream->runtime);
4011 break;
4012
Mark Brownc74184e2012-04-04 22:12:09 +01004013 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01004014 WARN(1, "Unknown event %d\n", event);
Sudip Mukherjee75881df2015-09-10 18:01:44 +05304015 ret = -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01004016 }
4017
Mark Brown9747cec2012-04-26 19:12:21 +01004018out:
Jerome Bruneta72706e2019-07-25 18:59:48 +02004019 /* Restore the substream direction */
4020 substream->stream = saved_stream;
Mark Brown9747cec2012-04-26 19:12:21 +01004021 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01004022}
4023
Nikesh Oswalc6615082015-02-02 17:06:44 +00004024static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
4025 struct snd_ctl_elem_value *ucontrol)
4026{
4027 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
Charles Keepax243bcfa2018-09-05 15:21:02 +01004028 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004029
Charles Keepax243bcfa2018-09-05 15:21:02 +01004030 ucontrol->value.enumerated.item[0] = rtd->params_select;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004031
4032 return 0;
4033}
4034
4035static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
4036 struct snd_ctl_elem_value *ucontrol)
4037{
4038 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
Charles Keepax243bcfa2018-09-05 15:21:02 +01004039 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004040
4041 /* Can't change the config when widget is already powered */
4042 if (w->power)
4043 return -EBUSY;
4044
Charles Keepax243bcfa2018-09-05 15:21:02 +01004045 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
Nikesh Oswalc6615082015-02-02 17:06:44 +00004046 return 0;
4047
Charles Keepax243bcfa2018-09-05 15:21:02 +01004048 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
Nikesh Oswalc6615082015-02-02 17:06:44 +00004049 return -EINVAL;
4050
Charles Keepax243bcfa2018-09-05 15:21:02 +01004051 rtd->params_select = ucontrol->value.enumerated.item[0];
Nikesh Oswalc6615082015-02-02 17:06:44 +00004052
Takashi Iwai5af82c82021-10-06 16:17:12 +02004053 return 1;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004054}
4055
Arnd Bergmannc42c5ac2017-10-10 11:20:11 +02004056static void
anish kumar19ad6832017-09-28 21:52:39 -07004057snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
4058 unsigned long *private_value,
4059 int num_params,
4060 const char **w_param_text)
Mark Brownc74184e2012-04-04 22:12:09 +01004061{
anish kumar19ad6832017-09-28 21:52:39 -07004062 int count;
4063
4064 devm_kfree(card->dev, (void *)*private_value);
Pankaj Bharadiyacacea3a2019-03-22 18:00:09 +05304065
4066 if (!w_param_text)
4067 return;
4068
anish kumar19ad6832017-09-28 21:52:39 -07004069 for (count = 0 ; count < num_params; count++)
4070 devm_kfree(card->dev, (void *)w_param_text[count]);
4071 devm_kfree(card->dev, w_param_text);
4072}
4073
4074static struct snd_kcontrol_new *
4075snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
4076 char *link_name,
4077 const struct snd_soc_pcm_stream *params,
4078 int num_params, const char **w_param_text,
4079 unsigned long *private_value)
4080{
Nikesh Oswalc6615082015-02-02 17:06:44 +00004081 struct soc_enum w_param_enum[] = {
4082 SOC_ENUM_SINGLE(0, 0, 0, NULL),
4083 };
4084 struct snd_kcontrol_new kcontrol_dai_link[] = {
4085 SOC_ENUM_EXT(NULL, w_param_enum[0],
4086 snd_soc_dapm_dai_link_get,
4087 snd_soc_dapm_dai_link_put),
4088 };
anish kumar19ad6832017-09-28 21:52:39 -07004089 struct snd_kcontrol_new *kcontrol_news;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004090 const struct snd_soc_pcm_stream *config = params;
anish kumar19ad6832017-09-28 21:52:39 -07004091 int count;
Mark Brownc74184e2012-04-04 22:12:09 +01004092
Nikesh Oswalc6615082015-02-02 17:06:44 +00004093 for (count = 0 ; count < num_params; count++) {
4094 if (!config->stream_name) {
4095 dev_warn(card->dapm.dev,
4096 "ASoC: anonymous config %d for dai link %s\n",
4097 count, link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004098 w_param_text[count] =
Charles Keepax46172b62015-03-25 11:22:35 +00004099 devm_kasprintf(card->dev, GFP_KERNEL,
4100 "Anonymous Configuration %d",
4101 count);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004102 } else {
4103 w_param_text[count] = devm_kmemdup(card->dev,
4104 config->stream_name,
4105 strlen(config->stream_name) + 1,
4106 GFP_KERNEL);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004107 }
anish kumar19ad6832017-09-28 21:52:39 -07004108 if (!w_param_text[count])
4109 goto outfree_w_param;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004110 config++;
4111 }
anish kumar19ad6832017-09-28 21:52:39 -07004112
Nikesh Oswalc6615082015-02-02 17:06:44 +00004113 w_param_enum[0].items = num_params;
4114 w_param_enum[0].texts = w_param_text;
Mark Brownc74184e2012-04-04 22:12:09 +01004115
anish kumar19ad6832017-09-28 21:52:39 -07004116 *private_value =
4117 (unsigned long) devm_kmemdup(card->dev,
4118 (void *)(kcontrol_dai_link[0].private_value),
4119 sizeof(struct soc_enum), GFP_KERNEL);
4120 if (!*private_value) {
4121 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4122 link_name);
4123 goto outfree_w_param;
4124 }
4125 kcontrol_dai_link[0].private_value = *private_value;
4126 /* duplicate kcontrol_dai_link on heap so that memory persists */
4127 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4128 sizeof(struct snd_kcontrol_new),
4129 GFP_KERNEL);
4130 if (!kcontrol_news) {
4131 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4132 link_name);
4133 goto outfree_w_param;
4134 }
4135 return kcontrol_news;
4136
4137outfree_w_param:
4138 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
4139 return NULL;
4140}
4141
Charles Keepax778ff5b2018-09-05 15:21:00 +01004142static struct snd_soc_dapm_widget *
Jerome Bruneta72706e2019-07-25 18:59:48 +02004143snd_soc_dapm_new_dai(struct snd_soc_card *card,
4144 struct snd_pcm_substream *substream,
Jerome Brunet054d6502019-07-25 18:59:45 +02004145 char *id)
anish kumar19ad6832017-09-28 21:52:39 -07004146{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09004147 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
anish kumar19ad6832017-09-28 21:52:39 -07004148 struct snd_soc_dapm_widget template;
4149 struct snd_soc_dapm_widget *w;
4150 const char **w_param_text;
Pankaj Bharadiya8633d442019-03-22 21:53:39 +05304151 unsigned long private_value = 0;
anish kumar19ad6832017-09-28 21:52:39 -07004152 char *link_name;
4153 int ret;
4154
4155 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
Jerome Brunet054d6502019-07-25 18:59:45 +02004156 rtd->dai_link->name, id);
anish kumar19ad6832017-09-28 21:52:39 -07004157 if (!link_name)
Charles Keepax778ff5b2018-09-05 15:21:00 +01004158 return ERR_PTR(-ENOMEM);
anish kumar19ad6832017-09-28 21:52:39 -07004159
Mark Brownc74184e2012-04-04 22:12:09 +01004160 memset(&template, 0, sizeof(template));
4161 template.reg = SND_SOC_NOPM;
4162 template.id = snd_soc_dapm_dai_link;
4163 template.name = link_name;
4164 template.event = snd_soc_dai_link_event;
4165 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
Jerome Bruneta72706e2019-07-25 18:59:48 +02004166 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD;
anish kumar19ad6832017-09-28 21:52:39 -07004167 template.kcontrol_news = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01004168
anish kumar19ad6832017-09-28 21:52:39 -07004169 /* allocate memory for control, only in case of multiple configs */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004170 if (rtd->dai_link->num_params > 1) {
4171 w_param_text = devm_kcalloc(card->dev,
4172 rtd->dai_link->num_params,
4173 sizeof(char *), GFP_KERNEL);
anish kumar19ad6832017-09-28 21:52:39 -07004174 if (!w_param_text) {
4175 ret = -ENOMEM;
4176 goto param_fail;
4177 }
4178
4179 template.num_kcontrols = 1;
4180 template.kcontrol_news =
4181 snd_soc_dapm_alloc_kcontrol(card,
Charles Keepax778ff5b2018-09-05 15:21:00 +01004182 link_name,
4183 rtd->dai_link->params,
4184 rtd->dai_link->num_params,
anish kumar19ad6832017-09-28 21:52:39 -07004185 w_param_text, &private_value);
4186 if (!template.kcontrol_news) {
4187 ret = -ENOMEM;
4188 goto param_fail;
4189 }
Arnd Bergmann667ebc92017-10-10 11:20:10 +02004190 } else {
4191 w_param_text = NULL;
anish kumar19ad6832017-09-28 21:52:39 -07004192 }
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004193 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01004194
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004195 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
Dan Carpenter2e558a82018-09-07 22:40:33 +03004196 if (IS_ERR(w)) {
4197 ret = PTR_ERR(w);
Charles Keepax9a1bb6002020-04-09 19:12:08 +01004198 dev_err(rtd->dev, "ASoC: Failed to create %s widget: %d\n",
4199 link_name, ret);
Linus Walleij37e1df82017-01-13 10:23:52 +01004200 goto outfree_kcontrol_news;
Dan Carpenter2e558a82018-09-07 22:40:33 +03004201 }
Mark Brownc74184e2012-04-04 22:12:09 +01004202
Jerome Bruneta72706e2019-07-25 18:59:48 +02004203 w->priv = substream;
Mark Brownc74184e2012-04-04 22:12:09 +01004204
Charles Keepax778ff5b2018-09-05 15:21:00 +01004205 return w;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004206
Nikesh Oswalc6615082015-02-02 17:06:44 +00004207outfree_kcontrol_news:
4208 devm_kfree(card->dev, (void *)template.kcontrol_news);
Charles Keepax778ff5b2018-09-05 15:21:00 +01004209 snd_soc_dapm_free_kcontrol(card, &private_value,
4210 rtd->dai_link->num_params, w_param_text);
anish kumar19ad6832017-09-28 21:52:39 -07004211param_fail:
Nikesh Oswalc6615082015-02-02 17:06:44 +00004212 devm_kfree(card->dev, link_name);
Charles Keepax778ff5b2018-09-05 15:21:00 +01004213 return ERR_PTR(ret);
Mark Brownc74184e2012-04-04 22:12:09 +01004214}
4215
Cezary Rojewskida039802022-03-11 16:35:29 +01004216/**
4217 * snd_soc_dapm_new_dai_widgets - Create new DAPM widgets
4218 * @dapm: DAPM context
4219 * @dai: parent DAI
4220 *
4221 * Returns 0 on success, error code otherwise.
4222 */
Mark Brown888df392012-02-16 19:37:51 -08004223int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4224 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004225{
Mark Brown888df392012-02-16 19:37:51 -08004226 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004227 struct snd_soc_dapm_widget *w;
4228
Mark Brown888df392012-02-16 19:37:51 -08004229 WARN_ON(dapm->dev != dai->dev);
4230
4231 memset(&template, 0, sizeof(template));
4232 template.reg = SND_SOC_NOPM;
4233
4234 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01004235 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08004236 template.name = dai->driver->playback.stream_name;
4237 template.sname = dai->driver->playback.stream_name;
4238
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004239 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08004240 template.name);
4241
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004242 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01004243 if (IS_ERR(w))
4244 return PTR_ERR(w);
Mark Brown888df392012-02-16 19:37:51 -08004245
4246 w->priv = dai;
4247 dai->playback_widget = w;
4248 }
4249
4250 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01004251 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08004252 template.name = dai->driver->capture.stream_name;
4253 template.sname = dai->driver->capture.stream_name;
4254
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004255 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08004256 template.name);
4257
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004258 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01004259 if (IS_ERR(w))
4260 return PTR_ERR(w);
Mark Brown888df392012-02-16 19:37:51 -08004261
4262 w->priv = dai;
4263 dai->capture_widget = w;
4264 }
4265
4266 return 0;
4267}
Cezary Rojewskida039802022-03-11 16:35:29 +01004268EXPORT_SYMBOL_GPL(snd_soc_dapm_new_dai_widgets);
Mark Brown888df392012-02-16 19:37:51 -08004269
4270int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4271{
4272 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004273 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08004274 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08004275
4276 /* For each DAI widget... */
Kuninori Morimoto145966922020-03-09 13:08:21 +09004277 for_each_card_widgets(card, dai_w) {
Mark Brown46162742013-06-05 19:36:11 +01004278 switch (dai_w->id) {
4279 case snd_soc_dapm_dai_in:
4280 case snd_soc_dapm_dai_out:
4281 break;
4282 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02004283 continue;
Mark Brown46162742013-06-05 19:36:11 +01004284 }
Mark Brown888df392012-02-16 19:37:51 -08004285
Liam Girdwoode01b4f62018-06-14 20:26:42 +01004286 /* let users know there is no DAI to link */
4287 if (!dai_w->priv) {
4288 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4289 dai_w->name);
4290 continue;
4291 }
4292
Mark Brown888df392012-02-16 19:37:51 -08004293 dai = dai_w->priv;
4294
4295 /* ...find all widgets with the same stream and link them */
Kuninori Morimoto145966922020-03-09 13:08:21 +09004296 for_each_card_widgets(card, w) {
Mark Brown888df392012-02-16 19:37:51 -08004297 if (w->dapm != dai_w->dapm)
4298 continue;
4299
Mark Brown46162742013-06-05 19:36:11 +01004300 switch (w->id) {
4301 case snd_soc_dapm_dai_in:
4302 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08004303 continue;
Mark Brown46162742013-06-05 19:36:11 +01004304 default:
4305 break;
4306 }
Mark Brown888df392012-02-16 19:37:51 -08004307
Lars-Peter Clausena798c242015-07-21 11:51:35 +02004308 if (!w->sname || !strstr(w->sname, dai_w->sname))
Mark Brown888df392012-02-16 19:37:51 -08004309 continue;
4310
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004311 if (dai_w->id == snd_soc_dapm_dai_in) {
4312 src = dai_w;
4313 sink = w;
4314 } else {
4315 src = w;
4316 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08004317 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004318 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4319 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004320 }
4321 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02004322
Mark Brown888df392012-02-16 19:37:51 -08004323 return 0;
4324}
Liam Girdwood64a648c2011-07-25 11:15:15 +01004325
Charles Keepax595571c2020-04-09 19:12:07 +01004326static void dapm_connect_dai_routes(struct snd_soc_dapm_context *dapm,
4327 struct snd_soc_dai *src_dai,
4328 struct snd_soc_dapm_widget *src,
4329 struct snd_soc_dapm_widget *dai,
4330 struct snd_soc_dai *sink_dai,
4331 struct snd_soc_dapm_widget *sink)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004332{
Charles Keepax595571c2020-04-09 19:12:07 +01004333 dev_dbg(dapm->dev, "connected DAI link %s:%s -> %s:%s\n",
4334 src_dai->component->name, src->name,
4335 sink_dai->component->name, sink->name);
4336
4337 if (dai) {
4338 snd_soc_dapm_add_path(dapm, src, dai, NULL, NULL);
4339 src = dai;
4340 }
4341
4342 snd_soc_dapm_add_path(dapm, src, sink, NULL, NULL);
4343}
4344
4345static void dapm_connect_dai_pair(struct snd_soc_card *card,
4346 struct snd_soc_pcm_runtime *rtd,
4347 struct snd_soc_dai *codec_dai,
4348 struct snd_soc_dai *cpu_dai)
4349{
4350 struct snd_soc_dai_link *dai_link = rtd->dai_link;
4351 struct snd_soc_dapm_widget *dai, *codec, *playback_cpu, *capture_cpu;
Jerome Bruneta72706e2019-07-25 18:59:48 +02004352 struct snd_pcm_substream *substream;
4353 struct snd_pcm_str *streams = rtd->pcm->streams;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004354
Charles Keepax595571c2020-04-09 19:12:07 +01004355 if (dai_link->params) {
Charles Keepax778ff5b2018-09-05 15:21:00 +01004356 playback_cpu = cpu_dai->capture_widget;
4357 capture_cpu = cpu_dai->playback_widget;
4358 } else {
Charles Keepax595571c2020-04-09 19:12:07 +01004359 playback_cpu = cpu_dai->playback_widget;
4360 capture_cpu = cpu_dai->capture_widget;
Charles Keepax778ff5b2018-09-05 15:21:00 +01004361 }
4362
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004363 /* connect BE DAI playback if widgets are valid */
4364 codec = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004365
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004366 if (playback_cpu && codec) {
Charles Keepaxf4aa5e22020-05-26 17:19:30 +01004367 if (dai_link->params && !rtd->playback_widget) {
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004368 substream = streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
Charles Keepax595571c2020-04-09 19:12:07 +01004369 dai = snd_soc_dapm_new_dai(card, substream, "playback");
Charles Keepax9a1bb6002020-04-09 19:12:08 +01004370 if (IS_ERR(dai))
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004371 goto capture;
Charles Keepaxf4aa5e22020-05-26 17:19:30 +01004372 rtd->playback_widget = dai;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004373 }
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004374
Charles Keepax595571c2020-04-09 19:12:07 +01004375 dapm_connect_dai_routes(&card->dapm, cpu_dai, playback_cpu,
Charles Keepaxf4aa5e22020-05-26 17:19:30 +01004376 rtd->playback_widget,
Charles Keepax595571c2020-04-09 19:12:07 +01004377 codec_dai, codec);
Charles Keepaxe36a1d02018-09-10 15:28:39 +01004378 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004379
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004380capture:
4381 /* connect BE DAI capture if widgets are valid */
4382 codec = codec_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004383
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004384 if (codec && capture_cpu) {
Charles Keepaxf4aa5e22020-05-26 17:19:30 +01004385 if (dai_link->params && !rtd->capture_widget) {
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004386 substream = streams[SNDRV_PCM_STREAM_CAPTURE].substream;
Charles Keepax595571c2020-04-09 19:12:07 +01004387 dai = snd_soc_dapm_new_dai(card, substream, "capture");
Charles Keepax9a1bb6002020-04-09 19:12:08 +01004388 if (IS_ERR(dai))
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004389 return;
Charles Keepaxf4aa5e22020-05-26 17:19:30 +01004390 rtd->capture_widget = dai;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004391 }
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004392
Charles Keepax595571c2020-04-09 19:12:07 +01004393 dapm_connect_dai_routes(&card->dapm, codec_dai, codec,
Charles Keepaxf4aa5e22020-05-26 17:19:30 +01004394 rtd->capture_widget,
Charles Keepax595571c2020-04-09 19:12:07 +01004395 cpu_dai, capture_cpu);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004396 }
4397}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004398
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004399static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4400 int event)
4401{
4402 struct snd_soc_dapm_widget *w;
4403
Kuninori Morimoto0c01f6c2020-02-19 15:56:41 +09004404 w = snd_soc_dai_get_widget(dai, stream);
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004405
4406 if (w) {
Kuninori Morimotof2ff5fb2021-08-05 10:11:44 +09004407 unsigned int ep;
4408
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004409 dapm_mark_dirty(w, "stream event");
4410
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004411 if (w->id == snd_soc_dapm_dai_in) {
4412 ep = SND_SOC_DAPM_EP_SOURCE;
4413 dapm_widget_invalidate_input_paths(w);
4414 } else {
4415 ep = SND_SOC_DAPM_EP_SINK;
4416 dapm_widget_invalidate_output_paths(w);
4417 }
4418
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004419 switch (event) {
4420 case SND_SOC_DAPM_STREAM_START:
4421 w->active = 1;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004422 w->is_ep = ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004423 break;
4424 case SND_SOC_DAPM_STREAM_STOP:
4425 w->active = 0;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004426 w->is_ep = 0;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004427 break;
4428 case SND_SOC_DAPM_STREAM_SUSPEND:
4429 case SND_SOC_DAPM_STREAM_RESUME:
4430 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4431 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4432 break;
4433 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004434 }
4435}
4436
Benoit Cousson44ba2642014-07-08 23:19:36 +02004437void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4438{
Mengdong Lin1a497982015-11-18 02:34:11 -05004439 struct snd_soc_pcm_runtime *rtd;
Charles Keepax0eaef952020-04-09 19:12:09 +01004440 struct snd_soc_dai *codec_dai;
4441 int i;
Benoit Cousson44ba2642014-07-08 23:19:36 +02004442
4443 /* for each BE DAI link... */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00004444 for_each_card_rtds(card, rtd) {
Benoit Cousson44ba2642014-07-08 23:19:36 +02004445 /*
4446 * dynamic FE links have no fixed DAI mapping.
4447 * CODEC<->CODEC links have no direct connection.
4448 */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004449 if (rtd->dai_link->dynamic)
Benoit Cousson44ba2642014-07-08 23:19:36 +02004450 continue;
4451
Charles Keepax0eaef952020-04-09 19:12:09 +01004452 if (rtd->num_cpus == 1) {
4453 for_each_rtd_codec_dais(rtd, i, codec_dai)
4454 dapm_connect_dai_pair(card, rtd, codec_dai,
Mark Brown39400f32020-04-14 18:04:08 +01004455 asoc_rtd_to_cpu(rtd, 0));
Charles Keepax0eaef952020-04-09 19:12:09 +01004456 } else if (rtd->num_codecs == rtd->num_cpus) {
4457 for_each_rtd_codec_dais(rtd, i, codec_dai)
4458 dapm_connect_dai_pair(card, rtd, codec_dai,
Mark Brown39400f32020-04-14 18:04:08 +01004459 asoc_rtd_to_cpu(rtd, i));
Charles Keepax0eaef952020-04-09 19:12:09 +01004460 } else {
4461 dev_err(card->dev,
4462 "N cpus to M codecs link is not supported yet\n");
4463 }
Benoit Cousson44ba2642014-07-08 23:19:36 +02004464 }
4465}
4466
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004467static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4468 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004469{
Kuninori Morimotoe3c3cf72020-03-16 15:37:09 +09004470 struct snd_soc_dai *dai;
Benoit Cousson44ba2642014-07-08 23:19:36 +02004471 int i;
4472
Kuninori Morimotoe3c3cf72020-03-16 15:37:09 +09004473 for_each_rtd_dais(rtd, i, dai)
4474 soc_dapm_dai_stream_event(dai, stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004475
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004476 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004477}
4478
4479/**
4480 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4481 * @rtd: PCM runtime data
4482 * @stream: stream name
4483 * @event: stream event
4484 *
4485 * Sends a stream event to the dapm core. The core then makes any
4486 * necessary widget power changes.
4487 *
4488 * Returns 0 for success else error.
4489 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004490void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4491 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004492{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004493 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004494
Liam Girdwood3cd04342012-03-09 12:02:08 +00004495 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004496 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004497 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004498}
Richard Purdie2b97eab2006-10-06 18:32:18 +02004499
Kuninori Morimoto3f4cf792020-01-10 11:36:23 +09004500void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream)
4501{
4502 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
4503 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
4504 /* powered down playback stream now */
4505 snd_soc_dapm_stream_event(rtd,
4506 SNDRV_PCM_STREAM_PLAYBACK,
4507 SND_SOC_DAPM_STREAM_STOP);
4508 } else {
4509 /* start delayed pop wq here for playback streams */
4510 rtd->pop_wait = 1;
4511 queue_delayed_work(system_power_efficient_wq,
4512 &rtd->delayed_work,
4513 msecs_to_jiffies(rtd->pmdown_time));
4514 }
4515 } else {
4516 /* capture streams can be powered down now */
4517 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
4518 SND_SOC_DAPM_STREAM_STOP);
4519 }
4520}
4521EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop);
4522
Richard Purdie2b97eab2006-10-06 18:32:18 +02004523/**
Charles Keepax11391102014-02-18 15:22:14 +00004524 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4525 * @dapm: DAPM context
4526 * @pin: pin name
4527 *
4528 * Enables input/output pin and its parents or children widgets iff there is
4529 * a valid audio route and active audio stream.
4530 *
4531 * Requires external locking.
4532 *
4533 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4534 * do any widget power switching.
4535 */
4536int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4537 const char *pin)
4538{
4539 return snd_soc_dapm_set_pin(dapm, pin, 1);
4540}
4541EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4542
4543/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004544 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004545 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004546 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02004547 *
Mark Brown74b8f952009-06-06 11:26:15 +01004548 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01004549 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00004550 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004551 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4552 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02004553 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004554int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004555{
Charles Keepax11391102014-02-18 15:22:14 +00004556 int ret;
4557
4558 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4559
4560 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4561
4562 mutex_unlock(&dapm->card->dapm_mutex);
4563
4564 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004565}
Liam Girdwooda5302182008-07-07 13:35:17 +01004566EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004567
4568/**
Charles Keepax11391102014-02-18 15:22:14 +00004569 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4570 * @dapm: DAPM context
4571 * @pin: pin name
4572 *
4573 * Enables input/output pin regardless of any other state. This is
4574 * intended for use with microphone bias supplies used in microphone
4575 * jack detection.
4576 *
4577 * Requires external locking.
4578 *
4579 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4580 * do any widget power switching.
4581 */
4582int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4583 const char *pin)
4584{
4585 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4586
4587 if (!w) {
4588 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4589 return -EINVAL;
4590 }
4591
4592 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02004593 if (!w->connected) {
4594 /*
4595 * w->force does not affect the number of input or output paths,
4596 * so we only have to recheck if w->connected is changed
4597 */
4598 dapm_widget_invalidate_input_paths(w);
4599 dapm_widget_invalidate_output_paths(w);
4600 w->connected = 1;
4601 }
Charles Keepax11391102014-02-18 15:22:14 +00004602 w->force = 1;
4603 dapm_mark_dirty(w, "force enable");
4604
4605 return 0;
4606}
4607EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4608
4609/**
Mark Brownda341832010-03-15 19:23:37 +00004610 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004611 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00004612 * @pin: pin name
4613 *
4614 * Enables input/output pin regardless of any other state. This is
4615 * intended for use with microphone bias supplies used in microphone
4616 * jack detection.
4617 *
4618 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4619 * do any widget power switching.
4620 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004621int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4622 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00004623{
Charles Keepax11391102014-02-18 15:22:14 +00004624 int ret;
Mark Brownda341832010-03-15 19:23:37 +00004625
Charles Keepax11391102014-02-18 15:22:14 +00004626 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00004627
Charles Keepax11391102014-02-18 15:22:14 +00004628 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09004629
Charles Keepax11391102014-02-18 15:22:14 +00004630 mutex_unlock(&dapm->card->dapm_mutex);
4631
4632 return ret;
Mark Brownda341832010-03-15 19:23:37 +00004633}
4634EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4635
4636/**
Charles Keepax11391102014-02-18 15:22:14 +00004637 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4638 * @dapm: DAPM context
4639 * @pin: pin name
4640 *
4641 * Disables input/output pin and its parents or children widgets.
4642 *
4643 * Requires external locking.
4644 *
4645 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4646 * do any widget power switching.
4647 */
4648int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4649 const char *pin)
4650{
4651 return snd_soc_dapm_set_pin(dapm, pin, 0);
4652}
4653EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4654
4655/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004656 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004657 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004658 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004659 *
Mark Brown74b8f952009-06-06 11:26:15 +01004660 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00004661 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004662 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4663 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004664 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004665int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4666 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01004667{
Charles Keepax11391102014-02-18 15:22:14 +00004668 int ret;
4669
4670 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4671
4672 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4673
4674 mutex_unlock(&dapm->card->dapm_mutex);
4675
4676 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01004677}
4678EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4679
4680/**
Charles Keepax11391102014-02-18 15:22:14 +00004681 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4682 * @dapm: DAPM context
4683 * @pin: pin name
4684 *
4685 * Marks the specified pin as being not connected, disabling it along
4686 * any parent or child widgets. At present this is identical to
4687 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4688 * additional things such as disabling controls which only affect
4689 * paths through the pin.
4690 *
4691 * Requires external locking.
4692 *
4693 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4694 * do any widget power switching.
4695 */
4696int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4697 const char *pin)
4698{
4699 return snd_soc_dapm_set_pin(dapm, pin, 0);
4700}
4701EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4702
4703/**
Mark Brown5817b522008-09-24 11:23:11 +01004704 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004705 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01004706 * @pin: pin name
4707 *
4708 * Marks the specified pin as being not connected, disabling it along
4709 * any parent or child widgets. At present this is identical to
4710 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4711 * additional things such as disabling controls which only affect
4712 * paths through the pin.
4713 *
4714 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4715 * do any widget power switching.
4716 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004717int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01004718{
Charles Keepax11391102014-02-18 15:22:14 +00004719 int ret;
4720
4721 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4722
4723 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4724
4725 mutex_unlock(&dapm->card->dapm_mutex);
4726
4727 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01004728}
4729EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4730
4731/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004732 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004733 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004734 * @pin: audio signal pin endpoint (or start point)
4735 *
4736 * Get audio pin status - connected or disconnected.
4737 *
4738 * Returns 1 for connected otherwise 0.
4739 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004740int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4741 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004742{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004743 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004744
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004745 if (w)
4746 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06004747
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004748 return 0;
4749}
Liam Girdwooda5302182008-07-07 13:35:17 +01004750EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004751
4752/**
Mark Brown1547aba2010-05-07 21:11:40 +01004753 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004754 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01004755 * @pin: audio signal pin endpoint (or start point)
4756 *
4757 * Mark the given endpoint or pin as ignoring suspend. When the
4758 * system is disabled a path between two endpoints flagged as ignoring
4759 * suspend will not be disabled. The path must already be enabled via
4760 * normal means at suspend time, it will not be turned on if it was not
4761 * already enabled.
4762 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004763int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4764 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01004765{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004766 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01004767
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004768 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004769 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004770 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01004771 }
4772
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004773 w->ignore_suspend = 1;
4774
4775 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01004776}
4777EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4778
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02004779/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02004780 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03004781 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02004782 *
4783 * Free all dapm widgets and resources.
4784 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004785void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004786{
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02004787 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004788 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02004789 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004790}
4791EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4792
Kuninori Morimoto95c267d2019-08-23 09:58:52 +09004793void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
4794 struct snd_soc_card *card,
4795 struct snd_soc_component *component)
4796{
4797 dapm->card = card;
4798 dapm->component = component;
4799 dapm->bias_level = SND_SOC_BIAS_OFF;
4800
4801 if (component) {
4802 dapm->dev = component->dev;
Julia Lawalla1344da2020-10-11 11:19:39 +02004803 dapm->idle_bias_off = !component->driver->idle_bias_on;
Kuninori Morimoto95c267d2019-08-23 09:58:52 +09004804 dapm->suspend_bias_off = component->driver->suspend_bias_off;
4805 } else {
4806 dapm->dev = card->dev;
4807 }
4808
4809 INIT_LIST_HEAD(&dapm->list);
Kuninori Morimotodf817f82020-03-09 13:08:16 +09004810 /* see for_each_card_dapms */
Kuninori Morimoto95c267d2019-08-23 09:58:52 +09004811 list_add(&dapm->list, &card->dapm_list);
4812}
4813EXPORT_SYMBOL_GPL(snd_soc_dapm_init);
4814
Xiang Xiao57996352014-03-02 00:04:02 +08004815static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01004816{
Liam Girdwood01005a72012-07-06 16:57:05 +01004817 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01004818 struct snd_soc_dapm_widget *w;
4819 LIST_HEAD(down_list);
4820 int powerdown = 0;
4821
Liam Girdwood01005a72012-07-06 16:57:05 +01004822 mutex_lock(&card->dapm_mutex);
4823
Kuninori Morimoto145966922020-03-09 13:08:21 +09004824 for_each_card_widgets(dapm->card, w) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02004825 if (w->dapm != dapm)
4826 continue;
Mark Brown51737472009-06-22 13:16:51 +01004827 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004828 dapm_seq_insert(w, &down_list, false);
Charles Keepax9b319302020-02-28 15:31:45 +00004829 w->new_power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004830 powerdown = 1;
4831 }
4832 }
4833
4834 /* If there were no widgets to power down we're already in
4835 * standby.
4836 */
4837 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004838 if (dapm->bias_level == SND_SOC_BIAS_ON)
4839 snd_soc_dapm_set_bias_level(dapm,
4840 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004841 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004842 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4843 snd_soc_dapm_set_bias_level(dapm,
4844 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004845 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004846
4847 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004848}
Mark Brown51737472009-06-22 13:16:51 +01004849
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004850/*
4851 * snd_soc_dapm_shutdown - callback for system shutdown
4852 */
4853void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4854{
Xiang Xiao57996352014-03-02 00:04:02 +08004855 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004856
Kuninori Morimotodf817f82020-03-09 13:08:16 +09004857 for_each_card_dapms(card, dapm) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08004858 if (dapm != &card->dapm) {
4859 soc_dapm_shutdown_dapm(dapm);
4860 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4861 snd_soc_dapm_set_bias_level(dapm,
4862 SND_SOC_BIAS_OFF);
4863 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004864 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08004865
4866 soc_dapm_shutdown_dapm(&card->dapm);
4867 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4868 snd_soc_dapm_set_bias_level(&card->dapm,
4869 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01004870}
4871
Richard Purdie2b97eab2006-10-06 18:32:18 +02004872/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004873MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004874MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4875MODULE_LICENSE("GPL");