blob: 980f2c330b87ff595efe20fd54614cdad231d8bb [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 Clausen8be4da22014-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 Morimoto14596692020-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 Clausen8be4da22014-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 Clausen8be4da22014-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 Morimoto14596692020-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;
653 struct list_head *wlist;
654 const int depth = 2;
655 int i = 0;
656
657 if (w) {
658 wlist = &w->dapm->card->widgets;
659
660 list_for_each_entry_from(w, wlist, list) {
661 if (!strcmp(name, w->name))
662 return w;
663
664 if (++i == depth)
665 break;
666 }
667 }
668
669 return NULL;
670}
671
672static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
673 struct snd_soc_dapm_widget *w)
674{
675 wcache->widget = w;
676}
677
Mark Brown452c5ea2009-05-17 21:41:23 +0100678/**
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200679 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
680 * @dapm: The DAPM context for which to set the level
681 * @level: The level to set
682 *
683 * Forces the DAPM bias level to a specific state. It will call the bias level
684 * callback of DAPM context with the specified level. This will even happen if
685 * the context is already at the same level. Furthermore it will not go through
686 * the normal bias level sequencing, meaning any intermediate states between the
687 * current and the target state will not be entered.
688 *
689 * Note that the change in bias level is only temporary and the next time
690 * snd_soc_dapm_sync() is called the state will be set to the level as
691 * determined by the DAPM core. The function is mainly intended to be used to
692 * used during probe or resume from suspend to power up the device so
693 * initialization can be done, before the DAPM core takes over.
694 */
695int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
696 enum snd_soc_bias_level level)
697{
698 int ret = 0;
699
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900700 if (dapm->component)
701 ret = snd_soc_component_set_bias_level(dapm->component, level);
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200702
Lars-Peter Clausenf4bf8d72015-04-27 22:13:25 +0200703 if (ret == 0)
704 dapm->bias_level = level;
705
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200706 return ret;
707}
708EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
709
Mark Brown452c5ea2009-05-17 21:41:23 +0100710/**
711 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800712 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100713 * @level: level to configure
714 *
715 * Configure the bias (power) levels for the SoC audio device.
716 *
717 * Returns 0 for success else error.
718 */
Mark Browned5a4c42011-02-18 11:12:42 -0800719static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200720 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100721{
Mark Browned5a4c42011-02-18 11:12:42 -0800722 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100723 int ret = 0;
724
Mark Brown84e90932010-11-04 00:07:02 -0400725 trace_snd_soc_bias_level_start(card, level);
726
Kuninori Morimoto39caefd2020-05-28 10:49:35 +0900727 ret = snd_soc_card_set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100728 if (ret != 0)
729 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100730
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200731 if (!card || dapm != &card->dapm)
732 ret = snd_soc_dapm_force_bias_level(dapm, level);
Liam Girdwood41231282012-07-06 16:56:16 +0100733
Mark Brown171ec6b2011-06-06 18:15:19 +0100734 if (ret != 0)
735 goto out;
736
Kuninori Morimotod41278e2020-05-28 10:50:35 +0900737 ret = snd_soc_card_set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100738out:
Mark Brown84e90932010-11-04 00:07:02 -0400739 trace_snd_soc_bias_level_done(card, level);
740
Mark Brown452c5ea2009-05-17 21:41:23 +0100741 return ret;
742}
743
Mark Brown74b8f952009-06-06 11:26:15 +0100744/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200745static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200746 struct snd_soc_dapm_path *path, const char *control_name,
747 struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200748{
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200749 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200750 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100751 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200752 int i;
753
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100754 if (e->reg != SND_SOC_NOPM) {
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;
785 unsigned int shift = mc->shift;
786 unsigned int max = mc->max;
787 unsigned int mask = (1 << fls(max)) - 1;
788 unsigned int invert = mc->invert;
789 unsigned int val;
790
791 if (reg != SND_SOC_NOPM) {
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900792 val = soc_dapm_read(p->sink->dapm, reg);
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 Morimoto14596692020-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{
Jeeja KP19a2557b2015-10-20 22:30:06 +05301063 int i, ret;
1064
1065 for (i = 0; i < w->num_kcontrols; i++) {
1066 ret = dapm_create_or_share_kcontrol(w, i);
1067 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{
1077 int i, ret;
1078 struct snd_kcontrol *kcontrol;
1079 struct snd_soc_dapm_context *dapm = w->dapm;
1080 struct snd_card *card = dapm->card->snd_card;
Charles Keepax243bcfa2018-09-05 15:21:02 +01001081 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00001082
1083 /* create control for links with > 1 config */
Charles Keepax243bcfa2018-09-05 15:21:02 +01001084 if (rtd->dai_link->num_params <= 1)
Nikesh Oswalc6615082015-02-02 17:06:44 +00001085 return 0;
1086
1087 /* add kcontrol */
1088 for (i = 0; i < w->num_kcontrols; i++) {
1089 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1090 w->name, NULL);
1091 ret = snd_ctl_add(card, kcontrol);
1092 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/**
1279 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1280 * @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}
1334
Kuninori Morimoto52645e332020-02-19 15:56:52 +09001335void snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list **list)
1336{
1337 dapm_widget_list_free(list);
1338}
1339
Richard Purdie2b97eab2006-10-06 18:32:18 +02001340/*
Mark Brown62ea8742012-01-21 21:14:48 +00001341 * Handler for regulator supply widget.
1342 */
1343int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1344 struct snd_kcontrol *kcontrol, int event)
1345{
Mark Brownc05b84d2012-09-07 12:57:11 +08001346 int ret;
1347
Mark Browneb270e92013-10-09 13:52:52 +01001348 soc_dapm_async_complete(w->dapm);
1349
Mark Brownc05b84d2012-09-07 12:57:11 +08001350 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001351 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001352 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001353 if (ret != 0)
1354 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001355 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001356 w->name, ret);
1357 }
1358
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001359 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001360 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001361 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001362 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001363 if (ret != 0)
1364 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001365 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001366 w->name, ret);
1367 }
1368
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001369 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001370 }
Mark Brown62ea8742012-01-21 21:14:48 +00001371}
1372EXPORT_SYMBOL_GPL(dapm_regulator_event);
1373
Ola Liljad7e7eb92012-05-24 15:26:25 +02001374/*
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00001375 * Handler for pinctrl widget.
1376 */
1377int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1378 struct snd_kcontrol *kcontrol, int event)
1379{
1380 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1381 struct pinctrl *p = w->pinctrl;
1382 struct pinctrl_state *s;
1383
1384 if (!p || !priv)
1385 return -EIO;
1386
1387 if (SND_SOC_DAPM_EVENT_ON(event))
1388 s = pinctrl_lookup_state(p, priv->active_state);
1389 else
1390 s = pinctrl_lookup_state(p, priv->sleep_state);
1391
1392 if (IS_ERR(s))
1393 return PTR_ERR(s);
1394
1395 return pinctrl_select_state(p, s);
1396}
1397EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1398
1399/*
Ola Liljad7e7eb92012-05-24 15:26:25 +02001400 * Handler for clock supply widget.
1401 */
1402int dapm_clock_event(struct snd_soc_dapm_widget *w,
1403 struct snd_kcontrol *kcontrol, int event)
1404{
1405 if (!w->clk)
1406 return -EIO;
1407
Mark Browneb270e92013-10-09 13:52:52 +01001408 soc_dapm_async_complete(w->dapm);
1409
Ola Liljad7e7eb92012-05-24 15:26:25 +02001410 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001411 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001412 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001413 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001414 return 0;
1415 }
Charles Keepaxd78b1e432018-08-28 14:35:02 +01001416
Marek Belisko98b3cf12012-07-12 23:00:16 +02001417 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001418}
1419EXPORT_SYMBOL_GPL(dapm_clock_event);
1420
Mark Brownd805002b2011-09-28 18:28:23 +01001421static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1422{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001423 if (w->power_checked)
1424 return w->new_power;
1425
Mark Brownd805002b2011-09-28 18:28:23 +01001426 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001427 w->new_power = 1;
Mark Brownd805002b2011-09-28 18:28:23 +01001428 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001429 w->new_power = w->power_check(w);
1430
1431 w->power_checked = true;
1432
1433 return w->new_power;
Mark Brownd805002b2011-09-28 18:28:23 +01001434}
1435
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001436/* Generic check to see if a widget should be powered. */
Mark Browncd0f2d42009-04-20 16:56:59 +01001437static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1438{
1439 int in, out;
1440
Mark Brownde02d072011-09-20 21:43:24 +01001441 DAPM_UPDATE_STAT(w, power_checks);
1442
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001443 in = is_connected_input_ep(w, NULL, NULL);
1444 out = is_connected_output_ep(w, NULL, NULL);
Mark Browncd0f2d42009-04-20 16:56:59 +01001445 return out != 0 && in != 0;
1446}
1447
Mark Brown246d0a12009-04-22 18:24:55 +01001448/* Check to see if a power supply is needed */
1449static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1450{
1451 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001452
Mark Brownde02d072011-09-20 21:43:24 +01001453 DAPM_UPDATE_STAT(w, power_checks);
1454
Mark Brown246d0a12009-04-22 18:24:55 +01001455 /* Check if one of our outputs is connected */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001456 snd_soc_dapm_widget_for_each_sink_path(w, path) {
Mark Browna8fdac82011-09-28 18:20:26 +01001457 DAPM_UPDATE_STAT(w, neighbour_checks);
1458
Mark Brownbf3a9e12011-06-13 16:42:29 +01001459 if (path->weak)
1460 continue;
1461
Mark Brown215edda2009-09-08 18:59:05 +01001462 if (path->connected &&
1463 !path->connected(path->source, path->sink))
1464 continue;
1465
Mark Brownf68d7e12011-10-04 22:57:50 +01001466 if (dapm_widget_power_check(path->sink))
1467 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001468 }
1469
Mark Brownf68d7e12011-10-04 22:57:50 +01001470 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001471}
1472
Mark Brown35c64bc2011-09-28 18:23:53 +01001473static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1474{
Charles Keepax20bb0182015-12-02 10:22:16 +00001475 return w->connected;
Mark Brown35c64bc2011-09-28 18:23:53 +01001476}
1477
Mark Brown38357ab2009-06-06 19:03:23 +01001478static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1479 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001480 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001481{
Mark Brown828a8422011-01-15 13:14:30 +00001482 int *sort;
1483
Pierre-Louis Bossartf13d4b52019-02-05 10:22:28 -06001484 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1485 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1486
Mark Brown828a8422011-01-15 13:14:30 +00001487 if (power_up)
1488 sort = dapm_up_seq;
1489 else
1490 sort = dapm_down_seq;
1491
Pierre-Louis Bossartf13d4b52019-02-05 10:22:28 -06001492 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1493 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1494
Mark Brown38357ab2009-06-06 19:03:23 +01001495 if (sort[a->id] != sort[b->id])
1496 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001497 if (a->subseq != b->subseq) {
1498 if (power_up)
1499 return a->subseq - b->subseq;
1500 else
1501 return b->subseq - a->subseq;
1502 }
Mark Brownb22ead22009-06-07 12:51:26 +01001503 if (a->reg != b->reg)
1504 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001505 if (a->dapm != b->dapm)
1506 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001507
Mark Brown38357ab2009-06-06 19:03:23 +01001508 return 0;
1509}
Mark Brown42aa3412009-03-01 19:21:10 +00001510
Mark Brown38357ab2009-06-06 19:03:23 +01001511/* Insert a widget in order into a DAPM power sequence. */
1512static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1513 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001514 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001515{
1516 struct snd_soc_dapm_widget *w;
1517
1518 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001519 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001520 list_add_tail(&new_widget->power_list, &w->power_list);
1521 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001522 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001523
Mark Brown38357ab2009-06-06 19:03:23 +01001524 list_add_tail(&new_widget->power_list, list);
1525}
Mark Brown42aa3412009-03-01 19:21:10 +00001526
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001527static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001528 struct snd_soc_dapm_widget *w, int event)
1529{
Mark Brown68f89ad2010-11-03 23:51:49 -04001530 const char *ev_name;
1531 int power, ret;
1532
1533 switch (event) {
1534 case SND_SOC_DAPM_PRE_PMU:
1535 ev_name = "PRE_PMU";
1536 power = 1;
1537 break;
1538 case SND_SOC_DAPM_POST_PMU:
1539 ev_name = "POST_PMU";
1540 power = 1;
1541 break;
1542 case SND_SOC_DAPM_PRE_PMD:
1543 ev_name = "PRE_PMD";
1544 power = 0;
1545 break;
1546 case SND_SOC_DAPM_POST_PMD:
1547 ev_name = "POST_PMD";
1548 power = 0;
1549 break;
Mark Brown80114122013-02-25 15:14:19 +00001550 case SND_SOC_DAPM_WILL_PMU:
1551 ev_name = "WILL_PMU";
1552 power = 1;
1553 break;
1554 case SND_SOC_DAPM_WILL_PMD:
1555 ev_name = "WILL_PMD";
1556 power = 0;
1557 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001558 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001559 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001560 return;
1561 }
1562
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001563 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001564 return;
1565
1566 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001567 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001568 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001569 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001570 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001571 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001572 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001573 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001574 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001575 ev_name, w->name, ret);
1576 }
1577}
1578
Mark Brownb22ead22009-06-07 12:51:26 +01001579/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001580static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001581 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001582{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001583 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001584 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001585 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001586 unsigned int value = 0;
1587 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001588
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001589 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1590 reg = w->reg;
1591 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001592
1593 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001594 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001595 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001596
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001597 mask |= w->mask << w->shift;
1598 if (w->power)
1599 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001600 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001601 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001602
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001603 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001604 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1605 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001606
Mark Brown68f89ad2010-11-03 23:51:49 -04001607 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001608 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1609 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001610 }
1611
Mark Brown81628102009-06-07 13:21:24 +01001612 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001613 /* Any widget will do, they should all be updating the
1614 * same register.
1615 */
Mark Brown29376bc2011-06-19 13:49:28 +01001616
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001617 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001618 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001619 value, mask, reg, card->pop_time);
1620 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001621 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001622 }
1623
1624 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001625 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1626 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001627 }
Mark Brown42aa3412009-03-01 19:21:10 +00001628}
1629
Mark Brownb22ead22009-06-07 12:51:26 +01001630/* Apply a DAPM power sequence.
1631 *
1632 * We walk over a pre-sorted list of widgets to apply power to. In
1633 * order to minimise the number of writes to the device required
1634 * multiple widgets will be updated in a single write where possible.
1635 * Currently anything that requires more than a single write is not
1636 * handled.
1637 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001638static void dapm_seq_run(struct snd_soc_card *card,
1639 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001640{
1641 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001642 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001643 LIST_HEAD(pending);
1644 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001645 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001646 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001647 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001648 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001649 int *sort;
1650
1651 if (power_up)
1652 sort = dapm_up_seq;
1653 else
1654 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001655
Mark Brownb22ead22009-06-07 12:51:26 +01001656 list_for_each_entry_safe(w, n, list, power_list) {
1657 ret = 0;
1658
1659 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001660 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001661 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001662 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001663 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001664
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001665 if (cur_dapm && cur_dapm->component) {
Mark Brown474b62d2011-01-18 16:14:44 +00001666 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1667 if (sort[i] == cur_sort)
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001668 snd_soc_component_seq_notifier(
1669 cur_dapm->component,
1670 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001671 }
1672
Mark Browneb270e92013-10-09 13:52:52 +01001673 if (cur_dapm && w->dapm != cur_dapm)
1674 soc_dapm_async_complete(cur_dapm);
1675
Mark Brownb22ead22009-06-07 12:51:26 +01001676 INIT_LIST_HEAD(&pending);
1677 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001678 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001679 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001680 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001681 }
1682
Mark Brown163cac02009-06-07 10:12:52 +01001683 switch (w->id) {
1684 case snd_soc_dapm_pre:
1685 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001686 list_for_each_entry_safe_continue(w, n, list,
1687 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001688
Mark Brownb22ead22009-06-07 12:51:26 +01001689 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001690 ret = w->event(w,
1691 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001692 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001693 ret = w->event(w,
1694 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001695 break;
1696
1697 case snd_soc_dapm_post:
1698 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001699 list_for_each_entry_safe_continue(w, n, list,
1700 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001701
Mark Brownb22ead22009-06-07 12:51:26 +01001702 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001703 ret = w->event(w,
1704 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001705 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001706 ret = w->event(w,
1707 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001708 break;
1709
Mark Brown163cac02009-06-07 10:12:52 +01001710 default:
Mark Brown81628102009-06-07 13:21:24 +01001711 /* Queue it up for application */
1712 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001713 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001714 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001715 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001716 list_move(&w->power_list, &pending);
1717 break;
Mark Brown163cac02009-06-07 10:12:52 +01001718 }
Mark Brownb22ead22009-06-07 12:51:26 +01001719
1720 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001721 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001722 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001723 }
Mark Brownb22ead22009-06-07 12:51:26 +01001724
1725 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001726 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001727
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001728 if (cur_dapm && cur_dapm->component) {
Mark Brown474b62d2011-01-18 16:14:44 +00001729 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1730 if (sort[i] == cur_sort)
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001731 snd_soc_component_seq_notifier(
1732 cur_dapm->component,
1733 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001734 }
Mark Browneb270e92013-10-09 13:52:52 +01001735
Kuninori Morimotodf817f82020-03-09 13:08:16 +09001736 for_each_card_dapms(card, d)
Mark Browneb270e92013-10-09 13:52:52 +01001737 soc_dapm_async_complete(d);
Mark Brown163cac02009-06-07 10:12:52 +01001738}
1739
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001740static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001741{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001742 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001743 struct snd_soc_dapm_widget_list *wlist;
1744 struct snd_soc_dapm_widget *w = NULL;
1745 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001746 int ret;
1747
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001748 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001749 return;
1750
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001751 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001752
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001753 for_each_dapm_widgets(wlist, wi, w) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001754 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1755 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1756 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001757 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001758 w->name, ret);
1759 }
Mark Brown97404f22010-12-14 16:13:57 +00001760 }
1761
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001762 if (!w)
1763 return;
1764
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001765 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1766 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001767 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001768 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001769 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001770
Chen-Yu Tsaie411b0b2016-11-02 15:35:58 +08001771 if (update->has_second_set) {
1772 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1773 update->mask2, update->val2);
1774 if (ret < 0)
1775 dev_err(w->dapm->dev,
1776 "ASoC: %s DAPM update failed: %d\n",
1777 w->name, ret);
1778 }
1779
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001780 for_each_dapm_widgets(wlist, wi, w) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001781 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1782 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1783 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001784 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001785 w->name, ret);
1786 }
Mark Brown97404f22010-12-14 16:13:57 +00001787 }
1788}
1789
Mark Brown9d0624a2011-02-18 11:49:43 -08001790/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1791 * they're changing state.
1792 */
1793static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1794{
1795 struct snd_soc_dapm_context *d = data;
1796 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001797
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001798 /* If we're off and we're not supposed to go into STANDBY */
Mark Brown56fba412011-06-04 11:25:10 +01001799 if (d->bias_level == SND_SOC_BIAS_OFF &&
1800 d->target_bias_level != SND_SOC_BIAS_OFF) {
Tzung-Bi Shih3aecfc72020-07-24 15:07:31 +08001801 if (d->dev && cookie)
Mark Brownf1aac482011-12-05 15:17:06 +00001802 pm_runtime_get_sync(d->dev);
1803
Mark Brown9d0624a2011-02-18 11:49:43 -08001804 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1805 if (ret != 0)
1806 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001807 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001808 }
1809
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001810 /* Prepare for a transition to ON or away from ON */
1811 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1812 d->bias_level != SND_SOC_BIAS_ON) ||
1813 (d->target_bias_level != SND_SOC_BIAS_ON &&
1814 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001815 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1816 if (ret != 0)
1817 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001818 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001819 }
1820}
1821
1822/* Async callback run prior to DAPM sequences - brings to their final
1823 * state.
1824 */
1825static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1826{
1827 struct snd_soc_dapm_context *d = data;
1828 int ret;
1829
1830 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001831 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1832 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1833 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001834 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1835 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001836 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001837 ret);
1838 }
1839
1840 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001841 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1842 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001843 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1844 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001845 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1846 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001847
Tzung-Bi Shih3aecfc72020-07-24 15:07:31 +08001848 if (d->dev && cookie)
Mark Brownfb644e92012-01-25 19:53:58 +00001849 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001850 }
1851
1852 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001853 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1854 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001855 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1856 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001857 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001858 ret);
1859 }
1860}
Mark Brown97404f22010-12-14 16:13:57 +00001861
Mark Brownfe4fda52011-10-03 22:36:57 +01001862static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1863 bool power, bool connect)
1864{
1865 /* If a connection is being made or broken then that update
1866 * will have marked the peer dirty, otherwise the widgets are
1867 * not connected and this update has no impact. */
1868 if (!connect)
1869 return;
1870
1871 /* If the peer is already in the state we're moving to then we
1872 * won't have an impact on it. */
1873 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001874 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001875}
1876
Mark Brown05623c42011-09-28 17:02:31 +01001877static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1878 struct list_head *up_list,
1879 struct list_head *down_list)
1880{
Mark Browndb432b42011-10-03 21:06:40 +01001881 struct snd_soc_dapm_path *path;
1882
Mark Brown05623c42011-09-28 17:02:31 +01001883 if (w->power == power)
1884 return;
1885
1886 trace_snd_soc_dapm_widget_power(w, power);
1887
Mark Browndb432b42011-10-03 21:06:40 +01001888 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001889 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001890 */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001891 snd_soc_dapm_widget_for_each_source_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001892 dapm_widget_set_peer_power(path->source, power, path->connect);
1893
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001894 /* Supplies can't affect their outputs, only their inputs */
1895 if (!w->is_supply) {
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001896 snd_soc_dapm_widget_for_each_sink_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001897 dapm_widget_set_peer_power(path->sink, power,
1898 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001899 }
1900
Mark Brown05623c42011-09-28 17:02:31 +01001901 if (power)
1902 dapm_seq_insert(w, up_list, true);
1903 else
1904 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001905}
1906
Mark Brown7c81beb2011-09-20 22:22:32 +01001907static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1908 struct list_head *up_list,
1909 struct list_head *down_list)
1910{
Mark Brown7c81beb2011-09-20 22:22:32 +01001911 int power;
1912
1913 switch (w->id) {
1914 case snd_soc_dapm_pre:
1915 dapm_seq_insert(w, down_list, false);
1916 break;
1917 case snd_soc_dapm_post:
1918 dapm_seq_insert(w, up_list, true);
1919 break;
1920
1921 default:
Mark Brownd805002b2011-09-28 18:28:23 +01001922 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001923
Mark Brown05623c42011-09-28 17:02:31 +01001924 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001925 break;
1926 }
1927}
1928
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001929static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1930{
1931 if (dapm->idle_bias_off)
1932 return true;
1933
1934 switch (snd_power_get_state(dapm->card->snd_card)) {
1935 case SNDRV_CTL_POWER_D3hot:
1936 case SNDRV_CTL_POWER_D3cold:
1937 return dapm->suspend_bias_off;
1938 default:
1939 break;
1940 }
1941
1942 return false;
1943}
1944
Mark Brown42aa3412009-03-01 19:21:10 +00001945/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001946 * Scan each dapm widget for complete audio path.
1947 * A complete path is a route that has valid endpoints i.e.:-
1948 *
1949 * o DAC to output pin.
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001950 * o Input pin to ADC.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001951 * o Input pin to Output pin (bypass, sidetone)
1952 * o DAC to ADC (loopback).
1953 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001954static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001955{
1956 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001957 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001958 LIST_HEAD(up_list);
1959 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001960 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001961 enum snd_soc_bias_level bias;
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +09001962 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001963
Mark Brownf9fa2b12014-03-06 16:49:11 +08001964 lockdep_assert_held(&card->dapm_mutex);
1965
Mark Brown84e90932010-11-04 00:07:02 -04001966 trace_snd_soc_dapm_start(card);
1967
Kuninori Morimotodf817f82020-03-09 13:08:16 +09001968 for_each_card_dapms(card, d) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001969 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001970 d->target_bias_level = SND_SOC_BIAS_OFF;
1971 else
1972 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001973 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001974
Liam Girdwood6c120e12012-02-15 15:15:34 +00001975 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001976
Mark Brown6d3ddc82009-05-16 17:47:29 +01001977 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001978 * lists indicating if they should be powered up or down. We
1979 * only check widgets that have been flagged as dirty but note
1980 * that new widgets may be added to the dirty list while we
1981 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001982 */
Mark Browndb432b42011-10-03 21:06:40 +01001983 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001984 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001985 }
1986
Kuninori Morimoto14596692020-03-09 13:08:21 +09001987 for_each_card_widgets(card, w) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001988 switch (w->id) {
1989 case snd_soc_dapm_pre:
1990 case snd_soc_dapm_post:
1991 /* These widgets always need to be powered */
1992 break;
1993 default:
1994 list_del_init(&w->dirty);
1995 break;
1996 }
Mark Browndb432b42011-10-03 21:06:40 +01001997
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001998 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001999 d = w->dapm;
2000
2001 /* Supplies and micbiases only bring the
2002 * context up to STANDBY as unless something
2003 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00002004 * generally don't require full power. Signal
2005 * generators are virtual pins and have no
2006 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01002007 */
2008 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00002009 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02002010 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00002011 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01002012 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002013 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00002014 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002015 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01002016 case snd_soc_dapm_micbias:
2017 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
2018 d->target_bias_level = SND_SOC_BIAS_STANDBY;
2019 break;
2020 default:
2021 d->target_bias_level = SND_SOC_BIAS_ON;
2022 break;
2023 }
2024 }
2025
2026 }
2027
Mark Brown85a843c2011-09-21 21:29:47 +01002028 /* Force all contexts in the card to the same bias state if
2029 * they're not ground referenced.
2030 */
Mark Brown56fba412011-06-04 11:25:10 +01002031 bias = SND_SOC_BIAS_OFF;
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002032 for_each_card_dapms(card, d)
Mark Brown56fba412011-06-04 11:25:10 +01002033 if (d->target_bias_level > bias)
2034 bias = d->target_bias_level;
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002035 for_each_card_dapms(card, d)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02002036 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01002037 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09002038
Mark Brownde02d072011-09-20 21:43:24 +01002039 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09002040
Xiang Xiao17282ba2014-03-02 00:04:03 +08002041 /* Run card bias changes at first */
2042 dapm_pre_sequence_async(&card->dapm, 0);
2043 /* Run other bias changes in parallel */
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002044 for_each_card_dapms(card, d) {
Jon Huntere03546d2018-08-17 16:35:43 +01002045 if (d != &card->dapm && d->bias_level != d->target_bias_level)
Xiang Xiao17282ba2014-03-02 00:04:03 +08002046 async_schedule_domain(dapm_pre_sequence_async, d,
2047 &async_domain);
2048 }
Mark Brown9d0624a2011-02-18 11:49:43 -08002049 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01002050
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02002051 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002052 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00002053 }
2054
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02002055 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002056 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00002057 }
2058
Mark Brown6d3ddc82009-05-16 17:47:29 +01002059 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002060 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01002061
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002062 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00002063
Mark Brown6d3ddc82009-05-16 17:47:29 +01002064 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002065 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002066
Mark Brown9d0624a2011-02-18 11:49:43 -08002067 /* Run all the bias changes in parallel */
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002068 for_each_card_dapms(card, d) {
Jon Huntere03546d2018-08-17 16:35:43 +01002069 if (d != &card->dapm && d->bias_level != d->target_bias_level)
Xiang Xiao17282ba2014-03-02 00:04:03 +08002070 async_schedule_domain(dapm_post_sequence_async, d,
2071 &async_domain);
2072 }
Mark Brown9d0624a2011-02-18 11:49:43 -08002073 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08002074 /* Run card bias changes at last */
2075 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01002076
Liam Girdwood8078d872012-02-15 15:15:35 +00002077 /* do we need to notify any clients that DAPM event is complete */
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002078 for_each_card_dapms(card, d) {
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +09002079 if (!d->component)
2080 continue;
2081
2082 ret = snd_soc_component_stream_event(d->component, event);
2083 if (ret < 0)
2084 return ret;
Liam Girdwood8078d872012-02-15 15:15:35 +00002085 }
2086
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002087 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02002088 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02002089 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01002090
Mark Brown84e90932010-11-04 00:07:02 -04002091 trace_snd_soc_dapm_done(card);
2092
Mark Brown42aa3412009-03-01 19:21:10 +00002093 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002094}
2095
Mark Brown79fb9382009-08-21 16:38:13 +01002096#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01002097static ssize_t dapm_widget_power_read_file(struct file *file,
2098 char __user *user_buf,
2099 size_t count, loff_t *ppos)
2100{
2101 struct snd_soc_dapm_widget *w = file->private_data;
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002102 struct snd_soc_card *card = w->dapm->card;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002103 enum snd_soc_dapm_direction dir, rdir;
Mark Brown79fb9382009-08-21 16:38:13 +01002104 char *buf;
2105 int in, out;
2106 ssize_t ret;
2107 struct snd_soc_dapm_path *p = NULL;
2108
2109 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2110 if (!buf)
2111 return -ENOMEM;
2112
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002113 mutex_lock(&card->dapm_mutex);
2114
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002115 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2116 if (w->is_supply) {
2117 in = 0;
2118 out = 0;
2119 } else {
Piotr Stankiewicz67420642016-05-13 17:03:55 +01002120 in = is_connected_input_ep(w, NULL, NULL);
2121 out = is_connected_output_ep(w, NULL, NULL);
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002122 }
Mark Brown79fb9382009-08-21 16:38:13 +01002123
Silvio Cesaree581e152019-01-12 16:28:43 +01002124 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
Mark Brownf13ebad2012-03-03 18:01:01 +00002125 w->name, w->power ? "On" : "Off",
2126 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01002127
Mark Brownd033c362009-12-04 15:25:56 +00002128 if (w->reg >= 0)
Silvio Cesaree581e152019-01-12 16:28:43 +01002129 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002130 " - R%d(0x%x) mask 0x%x",
2131 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00002132
Silvio Cesaree581e152019-01-12 16:28:43 +01002133 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
Mark Brownd033c362009-12-04 15:25:56 +00002134
Mark Brown3eef08b2009-09-14 16:49:00 +01002135 if (w->sname)
Silvio Cesaree581e152019-01-12 16:28:43 +01002136 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
Mark Brown3eef08b2009-09-14 16:49:00 +01002137 w->sname,
2138 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01002139
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002140 snd_soc_dapm_for_each_direction(dir) {
2141 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2142 snd_soc_dapm_widget_for_each_path(w, dir, p) {
KaiChieh Chuang28735af2018-02-05 13:00:00 +08002143 if (p->connected && !p->connected(p->source, p->sink))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002144 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002145
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002146 if (!p->connect)
2147 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002148
Silvio Cesaree581e152019-01-12 16:28:43 +01002149 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002150 " %s \"%s\" \"%s\"\n",
2151 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
Mark Brown79fb9382009-08-21 16:38:13 +01002152 p->name ? p->name : "static",
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002153 p->node[rdir]->name);
2154 }
Mark Brown79fb9382009-08-21 16:38:13 +01002155 }
2156
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002157 mutex_unlock(&card->dapm_mutex);
2158
Mark Brown79fb9382009-08-21 16:38:13 +01002159 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2160
2161 kfree(buf);
2162 return ret;
2163}
2164
2165static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002166 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002167 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002168 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002169};
2170
Mark Brownef49e4f2011-04-04 20:48:13 +09002171static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2172 size_t count, loff_t *ppos)
2173{
2174 struct snd_soc_dapm_context *dapm = file->private_data;
2175 char *level;
2176
2177 switch (dapm->bias_level) {
2178 case SND_SOC_BIAS_ON:
2179 level = "On\n";
2180 break;
2181 case SND_SOC_BIAS_PREPARE:
2182 level = "Prepare\n";
2183 break;
2184 case SND_SOC_BIAS_STANDBY:
2185 level = "Standby\n";
2186 break;
2187 case SND_SOC_BIAS_OFF:
2188 level = "Off\n";
2189 break;
2190 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002191 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002192 level = "Unknown\n";
2193 break;
2194 }
2195
2196 return simple_read_from_buffer(user_buf, count, ppos, level,
2197 strlen(level));
2198}
2199
2200static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002201 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002202 .read = dapm_bias_read_file,
2203 .llseek = default_llseek,
2204};
2205
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002206void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2207 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002208{
Mark Brownceaea852019-06-21 12:33:57 +01002209 if (!parent || IS_ERR(parent))
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02002210 return;
2211
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002212 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2213
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +02002214 debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
2215 &dapm_bias_fops);
Mark Brown79fb9382009-08-21 16:38:13 +01002216}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002217
2218static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2219{
2220 struct snd_soc_dapm_context *dapm = w->dapm;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002221
2222 if (!dapm->debugfs_dapm || !w->name)
2223 return;
2224
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +02002225 debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
2226 &dapm_widget_power_fops);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002227}
2228
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002229static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2230{
2231 debugfs_remove_recursive(dapm->debugfs_dapm);
Kuninori Morimoto29040d12019-05-27 16:51:34 +09002232 dapm->debugfs_dapm = NULL;
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002233}
2234
Mark Brown79fb9382009-08-21 16:38:13 +01002235#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002236void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2237 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002238{
2239}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002240
2241static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2242{
2243}
2244
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002245static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2246{
2247}
2248
Mark Brown79fb9382009-08-21 16:38:13 +01002249#endif
2250
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002251/*
2252 * soc_dapm_connect_path() - Connects or disconnects a path
2253 * @path: The path to update
2254 * @connect: The new connect state of the path. True if the path is connected,
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002255 * false if it is disconnected.
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002256 * @reason: The reason why the path changed (for debugging only)
2257 */
2258static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2259 bool connect, const char *reason)
2260{
2261 if (path->connect == connect)
2262 return;
2263
2264 path->connect = connect;
2265 dapm_mark_dirty(path->source, reason);
2266 dapm_mark_dirty(path->sink, reason);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002267 dapm_path_invalidate(path);
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002268}
2269
Richard Purdie2b97eab2006-10-06 18:32:18 +02002270/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002271static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002272 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002273{
2274 struct snd_soc_dapm_path *path;
2275 int found = 0;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002276 bool connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002277
Mark Brownf9fa2b12014-03-06 16:49:11 +08002278 lockdep_assert_held(&card->dapm_mutex);
2279
Richard Purdie2b97eab2006-10-06 18:32:18 +02002280 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002281 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002282 found = 1;
2283 /* we now need to match the string in the enum to the path */
Jerome Brunetc3456a42019-05-15 15:18:55 +02002284 if (e && !(strcmp(path->name, e->texts[mux])))
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002285 connect = true;
2286 else
2287 connect = false;
2288
2289 soc_dapm_connect_path(path, connect, "mux update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002290 }
2291
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002292 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002293 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002294
Liam Girdwood618dae12012-04-25 12:12:51 +01002295 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002296}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002297
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002298int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002299 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2300 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002301{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002302 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002303 int ret;
2304
Liam Girdwood3cd04342012-03-09 12:02:08 +00002305 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002306 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002307 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002308 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002309 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002310 if (ret > 0)
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01002311 snd_soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002312 return ret;
2313}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002314EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002315
Milan plzik1b075e32008-01-10 14:39:46 +01002316/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002317static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002318 struct snd_kcontrol *kcontrol,
2319 int connect, int rconnect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002320{
2321 struct snd_soc_dapm_path *path;
2322 int found = 0;
2323
Mark Brownf9fa2b12014-03-06 16:49:11 +08002324 lockdep_assert_held(&card->dapm_mutex);
2325
Richard Purdie2b97eab2006-10-06 18:32:18 +02002326 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002327 dapm_kcontrol_for_each_path(path, kcontrol) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002328 /*
2329 * Ideally this function should support any number of
2330 * paths and channels. But since kcontrols only come
2331 * in mono and stereo variants, we are limited to 2
2332 * channels.
2333 *
2334 * The following code assumes for stereo controls the
2335 * first path (when 'found == 0') is the left channel,
2336 * and all remaining paths (when 'found == 1') are the
2337 * right channel.
2338 *
2339 * A stereo control is signified by a valid 'rconnect'
2340 * value, either 0 for unconnected, or >= 0 for connected.
2341 * This is chosen instead of using snd_soc_volsw_is_stereo,
2342 * so that the behavior of snd_soc_dapm_mixer_update_power
2343 * doesn't change even when the kcontrol passed in is
2344 * stereo.
2345 *
2346 * It passes 'connect' as the path connect status for
2347 * the left channel, and 'rconnect' for the right
2348 * channel.
2349 */
2350 if (found && rconnect >= 0)
2351 soc_dapm_connect_path(path, rconnect, "mixer update");
2352 else
2353 soc_dapm_connect_path(path, connect, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002354 found = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002355 }
2356
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002357 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002358 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002359
Liam Girdwood618dae12012-04-25 12:12:51 +01002360 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002361}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002362
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002363int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002364 struct snd_kcontrol *kcontrol, int connect,
2365 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002366{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002367 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002368 int ret;
2369
Liam Girdwood3cd04342012-03-09 12:02:08 +00002370 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002371 card->update = update;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002372 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002373 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002374 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002375 if (ret > 0)
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01002376 snd_soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002377 return ret;
2378}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002379EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002380
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002381static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2382 char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002383{
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002384 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002385 struct snd_soc_dapm_widget *w;
2386 int count = 0;
2387 char *state = "not set";
2388
Mark Brown47325072016-03-18 12:04:23 +00002389 /* card won't be set for the dummy component, as a spot fix
2390 * we're checking for that case specifically here but in future
2391 * we will ensure that the dummy component looks like others.
2392 */
2393 if (!cmpnt->card)
2394 return 0;
2395
Kuninori Morimoto14596692020-03-09 13:08:21 +09002396 for_each_card_widgets(cmpnt->card, w) {
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002397 if (w->dapm != dapm)
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002398 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002399
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002400 /* only display widgets that burn power */
Richard Purdie2b97eab2006-10-06 18:32:18 +02002401 switch (w->id) {
2402 case snd_soc_dapm_hp:
2403 case snd_soc_dapm_mic:
2404 case snd_soc_dapm_spk:
2405 case snd_soc_dapm_line:
2406 case snd_soc_dapm_micbias:
2407 case snd_soc_dapm_dac:
2408 case snd_soc_dapm_adc:
2409 case snd_soc_dapm_pga:
Seppo Ingalsuo882c8b4a2019-04-30 18:11:34 -05002410 case snd_soc_dapm_effect:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002411 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002412 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002413 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002414 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002415 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00002416 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002417 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002418 if (w->name)
2419 count += sprintf(buf + count, "%s: %s\n",
2420 w->name, w->power ? "On":"Off");
2421 break;
2422 default:
2423 break;
2424 }
2425 }
2426
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002427 switch (snd_soc_dapm_get_bias_level(dapm)) {
Mark Brown0be98982008-05-19 12:31:28 +02002428 case SND_SOC_BIAS_ON:
2429 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002430 break;
Mark Brown0be98982008-05-19 12:31:28 +02002431 case SND_SOC_BIAS_PREPARE:
2432 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002433 break;
Mark Brown0be98982008-05-19 12:31:28 +02002434 case SND_SOC_BIAS_STANDBY:
2435 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002436 break;
Mark Brown0be98982008-05-19 12:31:28 +02002437 case SND_SOC_BIAS_OFF:
2438 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002439 break;
2440 }
2441 count += sprintf(buf + count, "PM State: %s\n", state);
2442
2443 return count;
2444}
2445
Benoit Cousson44ba2642014-07-08 23:19:36 +02002446/* show dapm widget status in sys fs */
2447static ssize_t dapm_widget_show(struct device *dev,
2448 struct device_attribute *attr, char *buf)
2449{
2450 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002451 struct snd_soc_dai *codec_dai;
Benoit Cousson44ba2642014-07-08 23:19:36 +02002452 int i, count = 0;
2453
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002454 mutex_lock(&rtd->card->dapm_mutex);
2455
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09002456 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002457 struct snd_soc_component *cmpnt = codec_dai->component;
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002458
2459 count += dapm_widget_show_component(cmpnt, buf + count);
Benoit Cousson44ba2642014-07-08 23:19:36 +02002460 }
2461
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002462 mutex_unlock(&rtd->card->dapm_mutex);
2463
Benoit Cousson44ba2642014-07-08 23:19:36 +02002464 return count;
2465}
2466
Joe Perchesc828a892017-12-19 10:15:08 -08002467static DEVICE_ATTR_RO(dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002468
Takashi Iwaid29697d2015-01-30 20:16:37 +01002469struct attribute *soc_dapm_dev_attrs[] = {
2470 &dev_attr_dapm_widget.attr,
2471 NULL
2472};
Richard Purdie2b97eab2006-10-06 18:32:18 +02002473
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002474static void dapm_free_path(struct snd_soc_dapm_path *path)
2475{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002476 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2477 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002478 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002479 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002480 kfree(path);
2481}
2482
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002483void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2484{
2485 struct snd_soc_dapm_path *p, *next_p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002486 enum snd_soc_dapm_direction dir;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002487
2488 list_del(&w->list);
2489 /*
2490 * remove source and sink paths associated to this widget.
2491 * While removing the path, remove reference to it from both
2492 * source and sink widgets so that path is removed only once.
2493 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002494 snd_soc_dapm_for_each_direction(dir) {
2495 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2496 dapm_free_path(p);
2497 }
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002498
2499 kfree(w->kcontrols);
Lars-Peter Clausen48068962015-07-21 18:11:08 +02002500 kfree_const(w->name);
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -06002501 kfree_const(w->sname);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002502 kfree(w);
2503}
2504
Jyri Sarhafd589a12015-11-10 18:12:42 +02002505void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2506{
2507 dapm->path_sink_cache.widget = NULL;
2508 dapm->path_source_cache.widget = NULL;
2509}
2510
Richard Purdie2b97eab2006-10-06 18:32:18 +02002511/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002512static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002513{
2514 struct snd_soc_dapm_widget *w, *next_w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002515
Kuninori Morimoto14596692020-03-09 13:08:21 +09002516 for_each_card_widgets_safe(dapm->card, w, next_w) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002517 if (w->dapm != dapm)
2518 continue;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002519 snd_soc_dapm_free_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002520 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002521 snd_soc_dapm_reset_cache(dapm);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002522}
2523
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002524static struct snd_soc_dapm_widget *dapm_find_widget(
2525 struct snd_soc_dapm_context *dapm, const char *pin,
2526 bool search_other_contexts)
2527{
2528 struct snd_soc_dapm_widget *w;
2529 struct snd_soc_dapm_widget *fallback = NULL;
2530
Kuninori Morimoto14596692020-03-09 13:08:21 +09002531 for_each_card_widgets(dapm->card, w) {
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002532 if (!strcmp(w->name, pin)) {
2533 if (w->dapm == dapm)
2534 return w;
2535 else
2536 fallback = w;
2537 }
2538 }
2539
2540 if (search_other_contexts)
2541 return fallback;
2542
2543 return NULL;
2544}
2545
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002546static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002547 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002548{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002549 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002550
Mark Brownf9fa2b12014-03-06 16:49:11 +08002551 dapm_assert_locked(dapm);
2552
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002553 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002554 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002555 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002556 }
2557
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002558 if (w->connected != status) {
Mark Brown1a8b2d92012-02-16 11:50:07 -08002559 dapm_mark_dirty(w, "pin configuration");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002560 dapm_widget_invalidate_input_paths(w);
2561 dapm_widget_invalidate_output_paths(w);
2562 }
Mark Brown1a8b2d92012-02-16 11:50:07 -08002563
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002564 w->connected = status;
2565 if (status == 0)
2566 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002567
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002568 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002569}
2570
Richard Purdie2b97eab2006-10-06 18:32:18 +02002571/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002572 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2573 * @dapm: DAPM context
2574 *
2575 * Walks all dapm audio paths and powers widgets according to their
2576 * stream or path usage.
2577 *
2578 * Requires external locking.
2579 *
2580 * Returns 0 for success.
2581 */
2582int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2583{
2584 /*
2585 * Suppress early reports (eg, jacks syncing their state) to avoid
2586 * silly DAPM runs during card startup.
2587 */
2588 if (!dapm->card || !dapm->card->instantiated)
2589 return 0;
2590
2591 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2592}
2593EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2594
2595/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002596 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002597 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002598 *
2599 * Walks all dapm audio paths and powers widgets according to their
2600 * stream or path usage.
2601 *
2602 * Returns 0 for success.
2603 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002604int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002605{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002606 int ret;
2607
Liam Girdwood3cd04342012-03-09 12:02:08 +00002608 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002609 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002610 mutex_unlock(&dapm->card->dapm_mutex);
2611 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002612}
Liam Girdwooda5302182008-07-07 13:35:17 +01002613EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002614
Charles Keepax078a85f2019-01-31 13:30:18 +00002615static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2616 struct snd_soc_dapm_widget *w,
2617 int channels)
2618{
2619 switch (w->id) {
2620 case snd_soc_dapm_aif_out:
2621 case snd_soc_dapm_aif_in:
2622 break;
2623 default:
2624 return 0;
2625 }
2626
2627 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2628 w->channel < channels ? "Connecting" : "Disconnecting",
2629 p->source->name, p->sink->name);
2630
2631 if (w->channel < channels)
2632 soc_dapm_connect_path(p, true, "dai update");
2633 else
2634 soc_dapm_connect_path(p, false, "dai update");
2635
2636 return 0;
2637}
2638
2639static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2640 struct snd_pcm_hw_params *params,
2641 struct snd_soc_dai *dai)
2642{
2643 int dir = substream->stream;
2644 int channels = params_channels(params);
2645 struct snd_soc_dapm_path *p;
2646 struct snd_soc_dapm_widget *w;
2647 int ret;
2648
Kuninori Morimoto0c01f6c2020-02-19 15:56:41 +09002649 w = snd_soc_dai_get_widget(dai, dir);
Charles Keepax078a85f2019-01-31 13:30:18 +00002650
Charles Keepaxcf17a5f2019-02-06 11:13:59 +00002651 if (!w)
2652 return 0;
2653
Charles Keepax078a85f2019-01-31 13:30:18 +00002654 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2655 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2656
2657 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2658 ret = dapm_update_dai_chan(p, p->sink, channels);
2659 if (ret < 0)
2660 return ret;
2661 }
2662
2663 snd_soc_dapm_widget_for_each_source_path(w, p) {
2664 ret = dapm_update_dai_chan(p, p->source, channels);
2665 if (ret < 0)
2666 return ret;
2667 }
2668
2669 return 0;
2670}
2671
2672int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2673 struct snd_pcm_hw_params *params,
2674 struct snd_soc_dai *dai)
2675{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09002676 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Charles Keepax078a85f2019-01-31 13:30:18 +00002677 int ret;
2678
2679 mutex_lock_nested(&rtd->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2680 ret = dapm_update_dai_unlocked(substream, params, dai);
2681 mutex_unlock(&rtd->card->dapm_mutex);
2682
2683 return ret;
2684}
2685EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2686
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002687/*
2688 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2689 * @w: The widget for which to update the flags
2690 *
2691 * Some widgets have a dynamic category which depends on which neighbors they
2692 * are connected to. This function update the category for these widgets.
2693 *
2694 * This function must be called whenever a path is added or removed to a widget.
2695 */
2696static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2697{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002698 enum snd_soc_dapm_direction dir;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002699 struct snd_soc_dapm_path *p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002700 unsigned int ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002701
2702 switch (w->id) {
2703 case snd_soc_dapm_input:
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002704 /* On a fully routed card an input is never a source */
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002705 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002706 return;
2707 ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002708 snd_soc_dapm_widget_for_each_source_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002709 if (p->source->id == snd_soc_dapm_micbias ||
2710 p->source->id == snd_soc_dapm_mic ||
2711 p->source->id == snd_soc_dapm_line ||
2712 p->source->id == snd_soc_dapm_output) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002713 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002714 break;
2715 }
2716 }
2717 break;
2718 case snd_soc_dapm_output:
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002719 /* On a fully routed card a output is never a sink */
2720 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002721 return;
2722 ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002723 snd_soc_dapm_widget_for_each_sink_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002724 if (p->sink->id == snd_soc_dapm_spk ||
2725 p->sink->id == snd_soc_dapm_hp ||
2726 p->sink->id == snd_soc_dapm_line ||
2727 p->sink->id == snd_soc_dapm_input) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002728 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002729 break;
2730 }
2731 }
2732 break;
2733 case snd_soc_dapm_line:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002734 ep = 0;
2735 snd_soc_dapm_for_each_direction(dir) {
2736 if (!list_empty(&w->edges[dir]))
2737 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2738 }
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002739 break;
2740 default:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002741 return;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002742 }
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002743
2744 w->is_ep = ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002745}
2746
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002747static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2748 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2749 const char *control)
2750{
2751 bool dynamic_source = false;
2752 bool dynamic_sink = false;
2753
2754 if (!control)
2755 return 0;
2756
2757 switch (source->id) {
2758 case snd_soc_dapm_demux:
2759 dynamic_source = true;
2760 break;
2761 default:
2762 break;
2763 }
2764
2765 switch (sink->id) {
2766 case snd_soc_dapm_mux:
2767 case snd_soc_dapm_switch:
2768 case snd_soc_dapm_mixer:
2769 case snd_soc_dapm_mixer_named_ctl:
2770 dynamic_sink = true;
2771 break;
2772 default:
2773 break;
2774 }
2775
2776 if (dynamic_source && dynamic_sink) {
2777 dev_err(dapm->dev,
2778 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2779 source->name, control, sink->name);
2780 return -EINVAL;
2781 } else if (!dynamic_source && !dynamic_sink) {
2782 dev_err(dapm->dev,
2783 "Control not supported for path %s -> [%s] -> %s\n",
2784 source->name, control, sink->name);
2785 return -EINVAL;
2786 }
2787
2788 return 0;
2789}
2790
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002791static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2792 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2793 const char *control,
2794 int (*connected)(struct snd_soc_dapm_widget *source,
2795 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002796{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002797 struct snd_soc_dapm_widget *widgets[2];
2798 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002799 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002800 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002801
Lars-Peter Clausene409dfb2014-10-25 17:42:02 +02002802 if (wsink->is_supply && !wsource->is_supply) {
2803 dev_err(dapm->dev,
2804 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2805 wsource->name, wsink->name);
2806 return -EINVAL;
2807 }
2808
2809 if (connected && !wsource->is_supply) {
2810 dev_err(dapm->dev,
2811 "connected() callback only supported for supply widgets (%s -> %s)\n",
2812 wsource->name, wsink->name);
2813 return -EINVAL;
2814 }
2815
2816 if (wsource->is_supply && control) {
2817 dev_err(dapm->dev,
2818 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2819 wsource->name, control, wsink->name);
2820 return -EINVAL;
2821 }
2822
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002823 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2824 if (ret)
2825 return ret;
2826
Richard Purdie2b97eab2006-10-06 18:32:18 +02002827 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2828 if (!path)
2829 return -ENOMEM;
2830
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002831 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2832 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2833 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2834 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2835
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002836 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002837 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002838 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002839
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002840 if (wsource->is_supply || wsink->is_supply)
2841 path->is_supply = 1;
2842
Richard Purdie2b97eab2006-10-06 18:32:18 +02002843 /* connect static paths */
2844 if (control == NULL) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002845 path->connect = 1;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002846 } else {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002847 switch (wsource->id) {
2848 case snd_soc_dapm_demux:
2849 ret = dapm_connect_mux(dapm, path, control, wsource);
2850 if (ret)
2851 goto err;
2852 break;
2853 default:
2854 break;
2855 }
2856
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002857 switch (wsink->id) {
2858 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002859 ret = dapm_connect_mux(dapm, path, control, wsink);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002860 if (ret != 0)
2861 goto err;
2862 break;
2863 case snd_soc_dapm_switch:
2864 case snd_soc_dapm_mixer:
2865 case snd_soc_dapm_mixer_named_ctl:
2866 ret = dapm_connect_mixer(dapm, path, control);
2867 if (ret != 0)
2868 goto err;
2869 break;
2870 default:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002871 break;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002872 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002873 }
2874
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002875 list_add(&path->list, &dapm->card->paths);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002876 snd_soc_dapm_for_each_direction(dir)
2877 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002878
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002879 snd_soc_dapm_for_each_direction(dir) {
2880 dapm_update_widget_flags(widgets[dir]);
2881 dapm_mark_dirty(widgets[dir], "Route added");
2882 }
Mark Brownfabd0382012-07-05 17:20:06 +01002883
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002884 if (dapm->card->instantiated && path->connect)
2885 dapm_path_invalidate(path);
2886
Richard Purdie2b97eab2006-10-06 18:32:18 +02002887 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002888err:
2889 kfree(path);
2890 return ret;
2891}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002892
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002893static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002894 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002895{
2896 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2897 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2898 const char *sink;
2899 const char *source;
2900 char prefixed_sink[80];
2901 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002902 const char *prefix;
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002903 unsigned int sink_ref = 0;
2904 unsigned int source_ref = 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002905 int ret;
2906
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002907 prefix = soc_dapm_prefix(dapm);
2908 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002909 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002910 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002911 sink = prefixed_sink;
2912 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002913 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002914 source = prefixed_source;
2915 } else {
2916 sink = route->sink;
2917 source = route->source;
2918 }
2919
Charles Keepax45a110a2015-05-11 13:50:30 +01002920 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2921 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2922
2923 if (wsink && wsource)
2924 goto skip_search;
2925
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002926 /*
2927 * find src and dest widgets over all widgets but favor a widget from
2928 * current DAPM context
2929 */
Kuninori Morimoto14596692020-03-09 13:08:21 +09002930 for_each_card_widgets(dapm->card, w) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002931 if (!wsink && !(strcmp(w->name, sink))) {
2932 wtsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002933 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002934 wsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002935 if (wsource)
2936 break;
2937 }
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002938 sink_ref++;
2939 if (sink_ref > 1)
2940 dev_warn(dapm->dev,
2941 "ASoC: sink widget %s overwritten\n",
2942 w->name);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002943 continue;
2944 }
2945 if (!wsource && !(strcmp(w->name, source))) {
2946 wtsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002947 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002948 wsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002949 if (wsink)
2950 break;
2951 }
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002952 source_ref++;
2953 if (source_ref > 1)
2954 dev_warn(dapm->dev,
2955 "ASoC: source widget %s overwritten\n",
2956 w->name);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002957 }
2958 }
2959 /* use widget from another DAPM context if not found from this */
2960 if (!wsink)
2961 wsink = wtsink;
2962 if (!wsource)
2963 wsource = wtsource;
2964
2965 if (wsource == NULL) {
2966 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2967 route->source);
2968 return -ENODEV;
2969 }
2970 if (wsink == NULL) {
2971 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2972 route->sink);
2973 return -ENODEV;
2974 }
2975
Charles Keepax45a110a2015-05-11 13:50:30 +01002976skip_search:
2977 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2978 dapm_wcache_update(&dapm->path_source_cache, wsource);
2979
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002980 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2981 route->connected);
2982 if (ret)
2983 goto err;
2984
2985 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002986err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002987 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002988 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002989 return ret;
2990}
Mark Brown105f1c22008-05-13 14:52:19 +02002991
Mark Brownefcc3c62012-07-05 17:24:19 +01002992static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2993 const struct snd_soc_dapm_route *route)
2994{
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002995 struct snd_soc_dapm_widget *wsource, *wsink;
Mark Brownefcc3c62012-07-05 17:24:19 +01002996 struct snd_soc_dapm_path *path, *p;
2997 const char *sink;
2998 const char *source;
2999 char prefixed_sink[80];
3000 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003001 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01003002
3003 if (route->control) {
3004 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003005 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01003006 return -EINVAL;
3007 }
3008
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003009 prefix = soc_dapm_prefix(dapm);
3010 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01003011 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003012 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01003013 sink = prefixed_sink;
3014 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003015 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01003016 source = prefixed_source;
3017 } else {
3018 sink = route->sink;
3019 source = route->source;
3020 }
3021
3022 path = NULL;
3023 list_for_each_entry(p, &dapm->card->paths, list) {
3024 if (strcmp(p->source->name, source) != 0)
3025 continue;
3026 if (strcmp(p->sink->name, sink) != 0)
3027 continue;
3028 path = p;
3029 break;
3030 }
3031
3032 if (path) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003033 wsource = path->source;
3034 wsink = path->sink;
3035
3036 dapm_mark_dirty(wsource, "Route removed");
3037 dapm_mark_dirty(wsink, "Route removed");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003038 if (path->connect)
3039 dapm_path_invalidate(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01003040
Lars-Peter Clausen88722932013-06-14 13:16:53 +02003041 dapm_free_path(path);
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003042
3043 /* Update any path related flags */
3044 dapm_update_widget_flags(wsource);
3045 dapm_update_widget_flags(wsink);
Mark Brownefcc3c62012-07-05 17:24:19 +01003046 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003047 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01003048 source, sink);
3049 }
3050
3051 return 0;
3052}
3053
Mark Brown105f1c22008-05-13 14:52:19 +02003054/**
Mark Brown105f1c22008-05-13 14:52:19 +02003055 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003056 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02003057 * @route: audio routes
3058 * @num: number of routes
3059 *
3060 * Connects 2 dapm widgets together via a named audio path. The sink is
3061 * the widget receiving the audio signal, whilst the source is the sender
3062 * of the audio signal.
3063 *
3064 * Returns 0 for success else error. On error all resources can be freed
3065 * with a call to snd_soc_card_free().
3066 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003067int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02003068 const struct snd_soc_dapm_route *route, int num)
3069{
Mark Brown62d4a4b2012-06-22 12:21:49 +01003070 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02003071
Stuart Hendersonf19c1812017-09-21 11:02:12 +01003072 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown105f1c22008-05-13 14:52:19 +02003073 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02003074 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01003075 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003076 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
3077 route->source,
3078 route->control ? route->control : "direct",
3079 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01003080 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02003081 }
3082 route++;
3083 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003084 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02003085
Dan Carpenter60884c22012-04-13 22:25:43 +03003086 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02003087}
3088EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3089
Mark Brownefcc3c62012-07-05 17:24:19 +01003090/**
3091 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3092 * @dapm: DAPM context
3093 * @route: audio routes
3094 * @num: number of routes
3095 *
3096 * Removes routes from the DAPM context.
3097 */
3098int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3099 const struct snd_soc_dapm_route *route, int num)
3100{
Rajan Vajae066ea22016-02-11 11:23:35 +05303101 int i;
Mark Brownefcc3c62012-07-05 17:24:19 +01003102
Stuart Hendersonf19c1812017-09-21 11:02:12 +01003103 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownefcc3c62012-07-05 17:24:19 +01003104 for (i = 0; i < num; i++) {
3105 snd_soc_dapm_del_route(dapm, route);
3106 route++;
3107 }
3108 mutex_unlock(&dapm->card->dapm_mutex);
3109
Rajan Vajae066ea22016-02-11 11:23:35 +05303110 return 0;
Mark Brownefcc3c62012-07-05 17:24:19 +01003111}
3112EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3113
Mark Brownbf3a9e12011-06-13 16:42:29 +01003114static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3115 const struct snd_soc_dapm_route *route)
3116{
3117 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3118 route->source,
3119 true);
3120 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3121 route->sink,
3122 true);
3123 struct snd_soc_dapm_path *path;
3124 int count = 0;
3125
3126 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003127 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003128 route->source);
3129 return -ENODEV;
3130 }
3131
3132 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003133 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003134 route->sink);
3135 return -ENODEV;
3136 }
3137
3138 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003139 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003140 route->source, route->sink);
3141
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02003142 snd_soc_dapm_widget_for_each_sink_path(source, path) {
Mark Brownbf3a9e12011-06-13 16:42:29 +01003143 if (path->sink == sink) {
3144 path->weak = 1;
3145 count++;
3146 }
3147 }
3148
3149 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003150 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003151 route->source, route->sink);
3152 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003153 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003154 count, route->source, route->sink);
3155
3156 return 0;
3157}
3158
3159/**
3160 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3161 * @dapm: DAPM context
3162 * @route: audio routes
3163 * @num: number of routes
3164 *
3165 * Mark existing routes matching those specified in the passed array
3166 * as being weak, meaning that they are ignored for the purpose of
3167 * power decisions. The main intended use case is for sidetone paths
3168 * which couple audio between other independent paths if they are both
3169 * active in order to make the combination work better at the user
3170 * level but which aren't intended to be "used".
3171 *
3172 * Note that CODEC drivers should not use this as sidetone type paths
3173 * can frequently also be used as bypass paths.
3174 */
3175int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3176 const struct snd_soc_dapm_route *route, int num)
3177{
3178 int i, err;
3179 int ret = 0;
3180
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003181 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01003182 for (i = 0; i < num; i++) {
3183 err = snd_soc_dapm_weak_route(dapm, route);
3184 if (err)
3185 ret = err;
3186 route++;
3187 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003188 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01003189
3190 return ret;
3191}
3192EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3193
Mark Brown105f1c22008-05-13 14:52:19 +02003194/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003195 * snd_soc_dapm_new_widgets - add new dapm widgets
Jonathan Corbet628536e2015-08-25 01:14:48 -06003196 * @card: card to be checked for new dapm widgets
Richard Purdie2b97eab2006-10-06 18:32:18 +02003197 *
3198 * Checks the codec for any new dapm widgets and creates them if found.
3199 *
3200 * Returns 0 for success.
3201 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02003202int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003203{
3204 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00003205 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003206
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003207 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003208
Kuninori Morimoto14596692020-03-09 13:08:21 +09003209 for_each_card_widgets(card, w)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003210 {
3211 if (w->new)
3212 continue;
3213
Stephen Warrenfad59882011-04-28 17:37:59 -06003214 if (w->num_kcontrols) {
Kees Cook6396bb22018-06-12 14:03:40 -07003215 w->kcontrols = kcalloc(w->num_kcontrols,
Stephen Warrenfad59882011-04-28 17:37:59 -06003216 sizeof(struct snd_kcontrol *),
3217 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003218 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003219 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06003220 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003221 }
Stephen Warrenfad59882011-04-28 17:37:59 -06003222 }
3223
Richard Purdie2b97eab2006-10-06 18:32:18 +02003224 switch(w->id) {
3225 case snd_soc_dapm_switch:
3226 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00003227 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003228 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003229 break;
3230 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003231 case snd_soc_dapm_demux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003232 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003233 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003234 case snd_soc_dapm_pga:
Seppo Ingalsuo882c8b4a2019-04-30 18:11:34 -05003235 case snd_soc_dapm_effect:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06003236 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003237 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003238 break;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003239 case snd_soc_dapm_dai_link:
3240 dapm_new_dai_link(w);
3241 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01003242 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003243 break;
3244 }
Mark Brownb66a70d2011-02-09 18:04:11 +00003245
3246 /* Read the initial power state from the device */
3247 if (w->reg >= 0) {
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +09003248 val = soc_dapm_read(w->dapm, w->reg);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08003249 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003250 val &= w->mask;
3251 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00003252 w->power = 1;
3253 }
3254
Richard Purdie2b97eab2006-10-06 18:32:18 +02003255 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003256
Mark Brown7508b122011-10-05 12:09:12 +01003257 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003258 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003259 }
3260
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003261 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3262 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003263 return 0;
3264}
3265EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3266
3267/**
3268 * snd_soc_dapm_get_volsw - dapm mixer get callback
3269 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003270 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003271 *
3272 * Callback to get the value of a dapm mixer control.
3273 *
3274 * Returns 0 for success.
3275 */
3276int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3277 struct snd_ctl_elem_value *ucontrol)
3278{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003279 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3280 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003281 struct soc_mixer_control *mc =
3282 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003283 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003284 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003285 int max = mc->max;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003286 unsigned int width = fls(max);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003287 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003288 unsigned int invert = mc->invert;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003289 unsigned int reg_val, val, rval = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003290
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003291 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003292 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +09003293 reg_val = soc_dapm_read(dapm, reg);
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003294 val = (reg_val >> shift) & mask;
3295
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +09003296 if (reg != mc->rreg)
3297 reg_val = soc_dapm_read(dapm, mc->rreg);
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003298
3299 if (snd_soc_volsw_is_stereo(mc))
3300 rval = (reg_val >> mc->rshift) & mask;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003301 } else {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003302 reg_val = dapm_kcontrol_get_value(kcontrol);
3303 val = reg_val & mask;
3304
3305 if (snd_soc_volsw_is_stereo(mc))
3306 rval = (reg_val >> width) & mask;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003307 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003308 mutex_unlock(&card->dapm_mutex);
3309
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003310 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003311 ucontrol->value.integer.value[0] = max - val;
3312 else
3313 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003314
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003315 if (snd_soc_volsw_is_stereo(mc)) {
3316 if (invert)
3317 ucontrol->value.integer.value[1] = max - rval;
3318 else
3319 ucontrol->value.integer.value[1] = rval;
3320 }
3321
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +09003322 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003323}
3324EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3325
3326/**
3327 * snd_soc_dapm_put_volsw - dapm mixer set callback
3328 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003329 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003330 *
3331 * Callback to set the value of a dapm mixer control.
3332 *
3333 * Returns 0 for success.
3334 */
3335int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3336 struct snd_ctl_elem_value *ucontrol)
3337{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003338 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3339 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003340 struct soc_mixer_control *mc =
3341 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003342 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003343 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003344 int max = mc->max;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003345 unsigned int width = fls(max);
3346 unsigned int mask = (1 << width) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003347 unsigned int invert = mc->invert;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003348 unsigned int val, rval = 0;
3349 int connect, rconnect = -1, change, reg_change = 0;
Fabio Estevam33d92452018-02-14 13:39:05 -02003350 struct snd_soc_dapm_update update = {};
Nenghua Cao52765972013-12-13 20:13:49 +08003351 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003352
3353 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02003354 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003355
3356 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003357 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003358
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003359 if (snd_soc_volsw_is_stereo(mc)) {
3360 rval = (ucontrol->value.integer.value[1] & mask);
3361 rconnect = !!rval;
3362 if (invert)
3363 rval = max - rval;
3364 }
3365
Liam Girdwood3cd04342012-03-09 12:02:08 +00003366 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003367
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003368 /* This assumes field width < (bits in unsigned int / 2) */
3369 if (width > sizeof(unsigned int) * 8 / 2)
3370 dev_warn(dapm->dev,
3371 "ASoC: control %s field width limit exceeded\n",
3372 kcontrol->id.name);
3373 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
Mark Brown283375c2009-12-07 18:09:03 +00003374
Jarkko Nikula18626c72014-06-09 14:20:29 +03003375 if (reg != SND_SOC_NOPM) {
Jarkko Nikula18626c72014-06-09 14:20:29 +03003376 val = val << shift;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003377 rval = rval << mc->rshift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02003378
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003379 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3380
3381 if (snd_soc_volsw_is_stereo(mc))
3382 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3383 mask << mc->rshift,
3384 rval);
Jarkko Nikula18626c72014-06-09 14:20:29 +03003385 }
3386
3387 if (change || reg_change) {
3388 if (reg_change) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003389 if (snd_soc_volsw_is_stereo(mc)) {
3390 update.has_second_set = true;
3391 update.reg2 = mc->rreg;
3392 update.mask2 = mask << mc->rshift;
3393 update.val2 = rval;
3394 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003395 update.kcontrol = kcontrol;
3396 update.reg = reg;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003397 update.mask = mask << shift;
Jarkko Nikula18626c72014-06-09 14:20:29 +03003398 update.val = val;
3399 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003400 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003401 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00003402
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003403 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3404 rconnect);
Mark Brown97404f22010-12-14 16:13:57 +00003405
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003406 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00003407 }
3408
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003409 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003410
3411 if (ret > 0)
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01003412 snd_soc_dpcm_runtime_update(card);
Nenghua Cao52765972013-12-13 20:13:49 +08003413
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003414 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003415}
3416EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3417
3418/**
3419 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3420 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003421 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003422 *
3423 * Callback to get the value of a dapm enumerated double mixer control.
3424 *
3425 * Returns 0 for success.
3426 */
3427int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3428 struct snd_ctl_elem_value *ucontrol)
3429{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003430 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Charles Keepax561ed682015-05-01 12:37:26 +01003431 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003432 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003433 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003434
Charles Keepax561ed682015-05-01 12:37:26 +01003435 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3436 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +09003437 reg_val = soc_dapm_read(dapm, e->reg);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003438 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003439 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003440 }
Charles Keepax561ed682015-05-01 12:37:26 +01003441 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003442
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003443 val = (reg_val >> e->shift_l) & e->mask;
3444 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3445 if (e->shift_l != e->shift_r) {
3446 val = (reg_val >> e->shift_r) & e->mask;
3447 val = snd_soc_enum_val_to_item(e, val);
3448 ucontrol->value.enumerated.item[1] = val;
3449 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003450
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003451 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003452}
3453EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3454
Tzung-Bi Shih8f486292020-02-14 18:57:43 +08003455/**
3456 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3457 * @kcontrol: mixer control
3458 * @ucontrol: control element information
3459 *
3460 * Callback to set the value of a dapm enumerated double mixer control.
3461 *
3462 * Returns 0 for success.
3463 */
3464int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3465 struct snd_ctl_elem_value *ucontrol)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003466{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003467 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3468 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003469 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003470 unsigned int *item = ucontrol->value.enumerated.item;
Charles Keepax561ed682015-05-01 12:37:26 +01003471 unsigned int val, change, reg_change = 0;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003472 unsigned int mask;
Fabio Estevam33d92452018-02-14 13:39:05 -02003473 struct snd_soc_dapm_update update = {};
Nenghua Cao52765972013-12-13 20:13:49 +08003474 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003475
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003476 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003477 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003478
3479 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003480 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003481 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003482 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003483 return -EINVAL;
Chen-Yu Tsai071133a2016-08-27 19:27:59 +08003484 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003485 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003486 }
3487
Tzung-Bi Shih8f486292020-02-14 18:57:43 +08003488 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003489
Charles Keepax561ed682015-05-01 12:37:26 +01003490 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00003491
Charles Keepax561ed682015-05-01 12:37:26 +01003492 if (e->reg != SND_SOC_NOPM)
3493 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3494
3495 if (change || reg_change) {
3496 if (reg_change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003497 update.kcontrol = kcontrol;
3498 update.reg = e->reg;
3499 update.mask = mask;
3500 update.val = val;
3501 card->update = &update;
3502 }
Charles Keepax561ed682015-05-01 12:37:26 +01003503 change |= reg_change;
Mark Brown3a655772009-10-05 17:23:30 +01003504
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003505 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003506
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003507 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003508 }
3509
Tzung-Bi Shih8f486292020-02-14 18:57:43 +08003510 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003511
3512 if (ret > 0)
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01003513 snd_soc_dpcm_runtime_update(card);
Nenghua Cao52765972013-12-13 20:13:49 +08003514
Mark Brown97404f22010-12-14 16:13:57 +00003515 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003516}
3517EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3518
3519/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003520 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3521 *
3522 * @kcontrol: mixer control
3523 * @uinfo: control element information
3524 *
3525 * Callback to provide information about a pin switch control.
3526 */
3527int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3528 struct snd_ctl_elem_info *uinfo)
3529{
3530 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3531 uinfo->count = 1;
3532 uinfo->value.integer.min = 0;
3533 uinfo->value.integer.max = 1;
3534
3535 return 0;
3536}
3537EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3538
3539/**
3540 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3541 *
3542 * @kcontrol: mixer control
3543 * @ucontrol: Value
3544 */
3545int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3546 struct snd_ctl_elem_value *ucontrol)
3547{
Mark Brown48a8c392012-02-14 17:11:15 -08003548 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003549 const char *pin = (const char *)kcontrol->private_value;
3550
Liam Girdwood3cd04342012-03-09 12:02:08 +00003551 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003552
3553 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003554 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003555
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003556 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003557
3558 return 0;
3559}
3560EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3561
3562/**
3563 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3564 *
3565 * @kcontrol: mixer control
3566 * @ucontrol: Value
3567 */
3568int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3569 struct snd_ctl_elem_value *ucontrol)
3570{
Mark Brown48a8c392012-02-14 17:11:15 -08003571 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003572 const char *pin = (const char *)kcontrol->private_value;
3573
Mark Brown8b37dbd2009-02-28 21:14:20 +00003574 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003575 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003576 else
Mark Brown48a8c392012-02-14 17:11:15 -08003577 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003578
Mark Brown48a8c392012-02-14 17:11:15 -08003579 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003580 return 0;
3581}
3582EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3583
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +01003584struct snd_soc_dapm_widget *
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003585snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003586 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003587{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003588 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003589 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003590 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00003591 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003592
3593 if ((w = dapm_cnew_widget(widget)) == NULL)
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003594 return ERR_PTR(-ENOMEM);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003595
Mark Brown62ea8742012-01-21 21:14:48 +00003596 switch (w->id) {
3597 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003598 w->regulator = devm_regulator_get(dapm->dev, w->name);
3599 if (IS_ERR(w->regulator)) {
3600 ret = PTR_ERR(w->regulator);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003601 goto request_failed;
Mark Brown62ea8742012-01-21 21:14:48 +00003602 }
Mark Brown8784c772013-01-10 19:33:47 +00003603
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003604 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003605 ret = regulator_allow_bypass(w->regulator, true);
3606 if (ret != 0)
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003607 dev_warn(dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003608 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003609 w->name, ret);
3610 }
Mark Brown62ea8742012-01-21 21:14:48 +00003611 break;
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003612 case snd_soc_dapm_pinctrl:
3613 w->pinctrl = devm_pinctrl_get(dapm->dev);
Charles Keepaxa5cd7e92018-08-28 14:35:03 +01003614 if (IS_ERR(w->pinctrl)) {
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003615 ret = PTR_ERR(w->pinctrl);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003616 goto request_failed;
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003617 }
Tzung-Bi Shihec06dc12020-02-19 17:38:38 +08003618
3619 /* set to sleep_state when initializing */
3620 dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD);
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003621 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003622 case snd_soc_dapm_clock_supply:
Mark Brown695594f12012-06-04 08:14:13 +01003623 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003624 if (IS_ERR(w->clk)) {
3625 ret = PTR_ERR(w->clk);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003626 goto request_failed;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003627 }
3628 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003629 default:
3630 break;
3631 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003632
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003633 prefix = soc_dapm_prefix(dapm);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003634 if (prefix)
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003635 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003636 else
Lars-Peter Clausen48068962015-07-21 18:11:08 +02003637 w->name = kstrdup_const(widget->name, GFP_KERNEL);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003638 if (w->name == NULL) {
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -06003639 kfree_const(w->sname);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003640 kfree(w);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003641 return ERR_PTR(-ENOMEM);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003642 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003643
Mark Brown7ca3a182011-10-08 14:04:50 +01003644 switch (w->id) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003645 case snd_soc_dapm_mic:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003646 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003647 w->power_check = dapm_generic_check_power;
3648 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003649 case snd_soc_dapm_input:
3650 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003651 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003652 w->power_check = dapm_generic_check_power;
3653 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003654 case snd_soc_dapm_spk:
3655 case snd_soc_dapm_hp:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003656 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003657 w->power_check = dapm_generic_check_power;
3658 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003659 case snd_soc_dapm_output:
3660 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003661 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003662 w->power_check = dapm_generic_check_power;
3663 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003664 case snd_soc_dapm_vmid:
3665 case snd_soc_dapm_siggen:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003666 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003667 w->power_check = dapm_always_on_check_power;
3668 break;
Vinod Koul56b44372015-11-23 21:22:30 +05303669 case snd_soc_dapm_sink:
3670 w->is_ep = SND_SOC_DAPM_EP_SINK;
3671 w->power_check = dapm_always_on_check_power;
3672 break;
3673
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003674 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003675 case snd_soc_dapm_demux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003676 case snd_soc_dapm_switch:
3677 case snd_soc_dapm_mixer:
3678 case snd_soc_dapm_mixer_named_ctl:
Mark Brown63c69a62013-07-18 22:03:01 +01003679 case snd_soc_dapm_adc:
3680 case snd_soc_dapm_aif_out:
3681 case snd_soc_dapm_dac:
3682 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003683 case snd_soc_dapm_pga:
Ranjani Sridharan6e3bfcf2019-03-01 19:08:53 -06003684 case snd_soc_dapm_buffer:
3685 case snd_soc_dapm_scheduler:
3686 case snd_soc_dapm_effect:
3687 case snd_soc_dapm_src:
3688 case snd_soc_dapm_asrc:
3689 case snd_soc_dapm_encoder:
3690 case snd_soc_dapm_decoder:
Mark Brown7ca3a182011-10-08 14:04:50 +01003691 case snd_soc_dapm_out_drv:
Mark Brown7ca3a182011-10-08 14:04:50 +01003692 case snd_soc_dapm_micbias:
Mark Brown7ca3a182011-10-08 14:04:50 +01003693 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003694 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02003695 case snd_soc_dapm_dai_out:
3696 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003697 w->power_check = dapm_generic_check_power;
3698 break;
3699 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003700 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003701 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003702 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003703 case snd_soc_dapm_kcontrol:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003704 w->is_supply = 1;
Mark Brown7ca3a182011-10-08 14:04:50 +01003705 w->power_check = dapm_supply_check_power;
3706 break;
3707 default:
3708 w->power_check = dapm_always_on_check_power;
3709 break;
3710 }
3711
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003712 w->dapm = dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003713 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003714 INIT_LIST_HEAD(&w->dirty);
Kuninori Morimoto14596692020-03-09 13:08:21 +09003715 /* see for_each_card_widgets */
Lars-Peter Clausen92fa1242015-05-01 18:02:42 +02003716 list_add_tail(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003717
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003718 snd_soc_dapm_for_each_direction(dir) {
3719 INIT_LIST_HEAD(&w->edges[dir]);
3720 w->endpoints[dir] = -1;
3721 }
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003722
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02003723 /* machine layer sets up unconnected pins and insertions */
Richard Purdie2b97eab2006-10-06 18:32:18 +02003724 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003725 return w;
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003726
3727request_failed:
3728 if (ret != -EPROBE_DEFER)
3729 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3730 w->name, ret);
3731
Wenwen Wang45004d62019-07-22 08:57:44 -05003732 kfree_const(w->sname);
3733 kfree(w);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003734 return ERR_PTR(ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003735}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003736
3737/**
Charles Keepax94e630a2018-09-05 15:20:59 +01003738 * snd_soc_dapm_new_control - create new dapm control
3739 * @dapm: DAPM context
3740 * @widget: widget template
3741 *
3742 * Creates new DAPM control based upon a template.
3743 *
3744 * Returns a widget pointer on success or an error pointer on failure
3745 */
3746struct snd_soc_dapm_widget *
3747snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3748 const struct snd_soc_dapm_widget *widget)
3749{
3750 struct snd_soc_dapm_widget *w;
3751
3752 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3753 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3754 mutex_unlock(&dapm->card->dapm_mutex);
3755
3756 return w;
3757}
3758EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3759
3760/**
Mark Brown4ba13272008-05-13 14:51:19 +02003761 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003762 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003763 * @widget: widget array
3764 * @num: number of widgets
3765 *
3766 * Creates new DAPM controls based upon the templates.
3767 *
3768 * Returns 0 for success else error.
3769 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003770int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003771 const struct snd_soc_dapm_widget *widget,
3772 int num)
3773{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003774 struct snd_soc_dapm_widget *w;
3775 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003776 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003777
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003778 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003779 for (i = 0; i < num; i++) {
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003780 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
Linus Walleij37e1df82017-01-13 10:23:52 +01003781 if (IS_ERR(w)) {
3782 ret = PTR_ERR(w);
Dan Carpenter60884c22012-04-13 22:25:43 +03003783 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003784 }
Mark Brown4ba13272008-05-13 14:51:19 +02003785 widget++;
3786 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003787 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003788 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003789}
3790EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3791
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003792static int
3793snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3794 struct snd_pcm_substream *substream)
Mark Brownc74184e2012-04-04 22:12:09 +01003795{
Charles Keepax4a75aae2018-09-05 15:21:01 +01003796 struct snd_soc_dapm_path *path;
Mark Brownc74184e2012-04-04 22:12:09 +01003797 struct snd_soc_dai *source, *sink;
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09003798 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Mark Brown9747cec2012-04-26 19:12:21 +01003799 struct snd_pcm_hw_params *params = NULL;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003800 const struct snd_soc_pcm_stream *config = NULL;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003801 struct snd_pcm_runtime *runtime = NULL;
Takashi Iwai3ba66fe2018-07-25 22:43:26 +02003802 unsigned int fmt;
Colin Ian Kingc8415832019-07-26 13:33:27 +01003803 int ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003804
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003805 params = kzalloc(sizeof(*params), GFP_KERNEL);
3806 if (!params)
3807 return -ENOMEM;
3808
Jerome Bruneta72706e2019-07-25 18:59:48 +02003809 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3810 if (!runtime) {
3811 ret = -ENOMEM;
3812 goto out;
3813 }
3814
3815 substream->runtime = runtime;
3816
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003817 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3818 snd_soc_dapm_widget_for_each_source_path(w, path) {
3819 source = path->source->priv;
3820
3821 ret = snd_soc_dai_startup(source, substream);
3822 if (ret < 0) {
3823 dev_err(source->dev,
3824 "ASoC: startup() failed: %d\n", ret);
3825 goto out;
3826 }
Kuninori Morimoto45eb8662020-05-15 09:46:33 +09003827 snd_soc_dai_activate(source, substream->stream);
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003828 }
3829
3830 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3831 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3832 sink = path->sink->priv;
3833
3834 ret = snd_soc_dai_startup(sink, substream);
3835 if (ret < 0) {
3836 dev_err(sink->dev,
3837 "ASoC: startup() failed: %d\n", ret);
3838 goto out;
3839 }
Kuninori Morimoto45eb8662020-05-15 09:46:33 +09003840 snd_soc_dai_activate(sink, substream->stream);
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003841 }
3842
Jerome Bruneta72706e2019-07-25 18:59:48 +02003843 substream->hw_opened = 1;
3844
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003845 /*
3846 * Note: getting the config after .startup() gives a chance to
3847 * either party on the link to alter the configuration if
3848 * necessary
3849 */
Charles Keepax243bcfa2018-09-05 15:21:02 +01003850 config = rtd->dai_link->params + rtd->params_select;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003851 if (WARN_ON(!config)) {
3852 dev_err(w->dapm->dev, "ASoC: link config missing\n");
3853 ret = -EINVAL;
3854 goto out;
3855 }
Mark Brownc74184e2012-04-04 22:12:09 +01003856
Mark Brownc74184e2012-04-04 22:12:09 +01003857 /* Be a little careful as we don't want to overflow the mask array */
3858 if (config->formats) {
3859 fmt = ffs(config->formats) - 1;
3860 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003861 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003862 config->formats);
Mark Brownc74184e2012-04-04 22:12:09 +01003863
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003864 ret = -EINVAL;
Mark Brown9747cec2012-04-26 19:12:21 +01003865 goto out;
3866 }
Mark Brownc74184e2012-04-04 22:12:09 +01003867
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003868 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brown9747cec2012-04-26 19:12:21 +01003869 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003870 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003871 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003872 config->rate_max;
Mark Brown9747cec2012-04-26 19:12:21 +01003873 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003874 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003875 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003876 = config->channels_max;
3877
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003878 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3879 snd_soc_dapm_widget_for_each_source_path(w, path) {
3880 source = path->source->priv;
3881
3882 ret = snd_soc_dai_hw_params(source, substream, params);
3883 if (ret < 0)
3884 goto out;
3885
3886 dapm_update_dai_unlocked(substream, params, source);
3887 }
3888
3889 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3890 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3891 sink = path->sink->priv;
3892
3893 ret = snd_soc_dai_hw_params(sink, substream, params);
3894 if (ret < 0)
3895 goto out;
3896
3897 dapm_update_dai_unlocked(substream, params, sink);
3898 }
3899
Jerome Brunet9de98622019-07-25 18:59:49 +02003900 runtime->format = params_format(params);
3901 runtime->subformat = params_subformat(params);
3902 runtime->channels = params_channels(params);
3903 runtime->rate = params_rate(params);
3904
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003905out:
3906 kfree(params);
Colin Ian Kingc8415832019-07-26 13:33:27 +01003907 return ret;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003908}
3909
3910static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3911 struct snd_kcontrol *kcontrol, int event)
3912{
3913 struct snd_soc_dapm_path *path;
3914 struct snd_soc_dai *source, *sink;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003915 struct snd_pcm_substream *substream = w->priv;
3916 int ret = 0, saved_stream = substream->stream;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003917
3918 if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3919 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3920 return -EINVAL;
3921
Mark Brownc74184e2012-04-04 22:12:09 +01003922 switch (event) {
3923 case SND_SOC_DAPM_PRE_PMU:
Jerome Bruneta72706e2019-07-25 18:59:48 +02003924 ret = snd_soc_dai_link_event_pre_pmu(w, substream);
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003925 if (ret < 0)
3926 goto out;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003927
Mark Brownc74184e2012-04-04 22:12:09 +01003928 break;
3929
3930 case SND_SOC_DAPM_POST_PMU:
Charles Keepax4a75aae2018-09-05 15:21:01 +01003931 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3932 sink = path->sink->priv;
3933
3934 ret = snd_soc_dai_digital_mute(sink, 0,
3935 SNDRV_PCM_STREAM_PLAYBACK);
3936 if (ret != 0 && ret != -ENOTSUPP)
3937 dev_warn(sink->dev,
3938 "ASoC: Failed to unmute: %d\n", ret);
3939 ret = 0;
3940 }
Mark Brownc74184e2012-04-04 22:12:09 +01003941 break;
3942
3943 case SND_SOC_DAPM_PRE_PMD:
Charles Keepax4a75aae2018-09-05 15:21:01 +01003944 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3945 sink = path->sink->priv;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303946
Charles Keepax4a75aae2018-09-05 15:21:01 +01003947 ret = snd_soc_dai_digital_mute(sink, 1,
3948 SNDRV_PCM_STREAM_PLAYBACK);
3949 if (ret != 0 && ret != -ENOTSUPP)
3950 dev_warn(sink->dev,
3951 "ASoC: Failed to mute: %d\n", ret);
3952 ret = 0;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303953 }
3954
Jerome Bruneta72706e2019-07-25 18:59:48 +02003955 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003956 snd_soc_dapm_widget_for_each_source_path(w, path) {
3957 source = path->source->priv;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003958 snd_soc_dai_hw_free(source, substream);
Jerome Brunet68c907f2019-07-25 18:59:44 +02003959 }
Charles Keepax3c01b0e2018-10-11 17:28:28 +01003960
Jerome Bruneta72706e2019-07-25 18:59:48 +02003961 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
Jerome Brunet68c907f2019-07-25 18:59:44 +02003962 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3963 sink = path->sink->priv;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003964 snd_soc_dai_hw_free(sink, substream);
Jerome Brunet68c907f2019-07-25 18:59:44 +02003965 }
3966
Jerome Bruneta72706e2019-07-25 18:59:48 +02003967 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
Jerome Brunet68c907f2019-07-25 18:59:44 +02003968 snd_soc_dapm_widget_for_each_source_path(w, path) {
3969 source = path->source->priv;
Kuninori Morimoto45eb8662020-05-15 09:46:33 +09003970 snd_soc_dai_deactivate(source, substream->stream);
Kuninori Morimoto00a0b462020-09-28 09:00:40 +09003971 snd_soc_dai_shutdown(source, substream, 0);
Charles Keepax4a75aae2018-09-05 15:21:01 +01003972 }
3973
Jerome Bruneta72706e2019-07-25 18:59:48 +02003974 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003975 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3976 sink = path->sink->priv;
Kuninori Morimoto45eb8662020-05-15 09:46:33 +09003977 snd_soc_dai_deactivate(sink, substream->stream);
Kuninori Morimoto00a0b462020-09-28 09:00:40 +09003978 snd_soc_dai_shutdown(sink, substream, 0);
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303979 }
Mark Brownc74184e2012-04-04 22:12:09 +01003980 break;
3981
Jerome Bruneta72706e2019-07-25 18:59:48 +02003982 case SND_SOC_DAPM_POST_PMD:
3983 kfree(substream->runtime);
3984 break;
3985
Mark Brownc74184e2012-04-04 22:12:09 +01003986 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003987 WARN(1, "Unknown event %d\n", event);
Sudip Mukherjee75881df2015-09-10 18:01:44 +05303988 ret = -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003989 }
3990
Mark Brown9747cec2012-04-26 19:12:21 +01003991out:
Jerome Bruneta72706e2019-07-25 18:59:48 +02003992 /* Restore the substream direction */
3993 substream->stream = saved_stream;
Mark Brown9747cec2012-04-26 19:12:21 +01003994 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003995}
3996
Nikesh Oswalc6615082015-02-02 17:06:44 +00003997static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3998 struct snd_ctl_elem_value *ucontrol)
3999{
4000 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
Charles Keepax243bcfa2018-09-05 15:21:02 +01004001 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004002
Charles Keepax243bcfa2018-09-05 15:21:02 +01004003 ucontrol->value.enumerated.item[0] = rtd->params_select;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004004
4005 return 0;
4006}
4007
4008static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
4009 struct snd_ctl_elem_value *ucontrol)
4010{
4011 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
Charles Keepax243bcfa2018-09-05 15:21:02 +01004012 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004013
4014 /* Can't change the config when widget is already powered */
4015 if (w->power)
4016 return -EBUSY;
4017
Charles Keepax243bcfa2018-09-05 15:21:02 +01004018 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
Nikesh Oswalc6615082015-02-02 17:06:44 +00004019 return 0;
4020
Charles Keepax243bcfa2018-09-05 15:21:02 +01004021 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
Nikesh Oswalc6615082015-02-02 17:06:44 +00004022 return -EINVAL;
4023
Charles Keepax243bcfa2018-09-05 15:21:02 +01004024 rtd->params_select = ucontrol->value.enumerated.item[0];
Nikesh Oswalc6615082015-02-02 17:06:44 +00004025
4026 return 0;
4027}
4028
Arnd Bergmannc42c5ac2017-10-10 11:20:11 +02004029static void
anish kumar19ad6832017-09-28 21:52:39 -07004030snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
4031 unsigned long *private_value,
4032 int num_params,
4033 const char **w_param_text)
Mark Brownc74184e2012-04-04 22:12:09 +01004034{
anish kumar19ad6832017-09-28 21:52:39 -07004035 int count;
4036
4037 devm_kfree(card->dev, (void *)*private_value);
Pankaj Bharadiyacacea3a2019-03-22 18:00:09 +05304038
4039 if (!w_param_text)
4040 return;
4041
anish kumar19ad6832017-09-28 21:52:39 -07004042 for (count = 0 ; count < num_params; count++)
4043 devm_kfree(card->dev, (void *)w_param_text[count]);
4044 devm_kfree(card->dev, w_param_text);
4045}
4046
4047static struct snd_kcontrol_new *
4048snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
4049 char *link_name,
4050 const struct snd_soc_pcm_stream *params,
4051 int num_params, const char **w_param_text,
4052 unsigned long *private_value)
4053{
Nikesh Oswalc6615082015-02-02 17:06:44 +00004054 struct soc_enum w_param_enum[] = {
4055 SOC_ENUM_SINGLE(0, 0, 0, NULL),
4056 };
4057 struct snd_kcontrol_new kcontrol_dai_link[] = {
4058 SOC_ENUM_EXT(NULL, w_param_enum[0],
4059 snd_soc_dapm_dai_link_get,
4060 snd_soc_dapm_dai_link_put),
4061 };
anish kumar19ad6832017-09-28 21:52:39 -07004062 struct snd_kcontrol_new *kcontrol_news;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004063 const struct snd_soc_pcm_stream *config = params;
anish kumar19ad6832017-09-28 21:52:39 -07004064 int count;
Mark Brownc74184e2012-04-04 22:12:09 +01004065
Nikesh Oswalc6615082015-02-02 17:06:44 +00004066 for (count = 0 ; count < num_params; count++) {
4067 if (!config->stream_name) {
4068 dev_warn(card->dapm.dev,
4069 "ASoC: anonymous config %d for dai link %s\n",
4070 count, link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004071 w_param_text[count] =
Charles Keepax46172b62015-03-25 11:22:35 +00004072 devm_kasprintf(card->dev, GFP_KERNEL,
4073 "Anonymous Configuration %d",
4074 count);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004075 } else {
4076 w_param_text[count] = devm_kmemdup(card->dev,
4077 config->stream_name,
4078 strlen(config->stream_name) + 1,
4079 GFP_KERNEL);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004080 }
anish kumar19ad6832017-09-28 21:52:39 -07004081 if (!w_param_text[count])
4082 goto outfree_w_param;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004083 config++;
4084 }
anish kumar19ad6832017-09-28 21:52:39 -07004085
Nikesh Oswalc6615082015-02-02 17:06:44 +00004086 w_param_enum[0].items = num_params;
4087 w_param_enum[0].texts = w_param_text;
Mark Brownc74184e2012-04-04 22:12:09 +01004088
anish kumar19ad6832017-09-28 21:52:39 -07004089 *private_value =
4090 (unsigned long) devm_kmemdup(card->dev,
4091 (void *)(kcontrol_dai_link[0].private_value),
4092 sizeof(struct soc_enum), GFP_KERNEL);
4093 if (!*private_value) {
4094 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4095 link_name);
4096 goto outfree_w_param;
4097 }
4098 kcontrol_dai_link[0].private_value = *private_value;
4099 /* duplicate kcontrol_dai_link on heap so that memory persists */
4100 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4101 sizeof(struct snd_kcontrol_new),
4102 GFP_KERNEL);
4103 if (!kcontrol_news) {
4104 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4105 link_name);
4106 goto outfree_w_param;
4107 }
4108 return kcontrol_news;
4109
4110outfree_w_param:
4111 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
4112 return NULL;
4113}
4114
Charles Keepax778ff5b2018-09-05 15:21:00 +01004115static struct snd_soc_dapm_widget *
Jerome Bruneta72706e2019-07-25 18:59:48 +02004116snd_soc_dapm_new_dai(struct snd_soc_card *card,
4117 struct snd_pcm_substream *substream,
Jerome Brunet054d6502019-07-25 18:59:45 +02004118 char *id)
anish kumar19ad6832017-09-28 21:52:39 -07004119{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09004120 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
anish kumar19ad6832017-09-28 21:52:39 -07004121 struct snd_soc_dapm_widget template;
4122 struct snd_soc_dapm_widget *w;
4123 const char **w_param_text;
Pankaj Bharadiya8633d442019-03-22 21:53:39 +05304124 unsigned long private_value = 0;
anish kumar19ad6832017-09-28 21:52:39 -07004125 char *link_name;
4126 int ret;
4127
4128 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
Jerome Brunet054d6502019-07-25 18:59:45 +02004129 rtd->dai_link->name, id);
anish kumar19ad6832017-09-28 21:52:39 -07004130 if (!link_name)
Charles Keepax778ff5b2018-09-05 15:21:00 +01004131 return ERR_PTR(-ENOMEM);
anish kumar19ad6832017-09-28 21:52:39 -07004132
Mark Brownc74184e2012-04-04 22:12:09 +01004133 memset(&template, 0, sizeof(template));
4134 template.reg = SND_SOC_NOPM;
4135 template.id = snd_soc_dapm_dai_link;
4136 template.name = link_name;
4137 template.event = snd_soc_dai_link_event;
4138 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
Jerome Bruneta72706e2019-07-25 18:59:48 +02004139 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD;
anish kumar19ad6832017-09-28 21:52:39 -07004140 template.kcontrol_news = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01004141
anish kumar19ad6832017-09-28 21:52:39 -07004142 /* allocate memory for control, only in case of multiple configs */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004143 if (rtd->dai_link->num_params > 1) {
4144 w_param_text = devm_kcalloc(card->dev,
4145 rtd->dai_link->num_params,
4146 sizeof(char *), GFP_KERNEL);
anish kumar19ad6832017-09-28 21:52:39 -07004147 if (!w_param_text) {
4148 ret = -ENOMEM;
4149 goto param_fail;
4150 }
4151
4152 template.num_kcontrols = 1;
4153 template.kcontrol_news =
4154 snd_soc_dapm_alloc_kcontrol(card,
Charles Keepax778ff5b2018-09-05 15:21:00 +01004155 link_name,
4156 rtd->dai_link->params,
4157 rtd->dai_link->num_params,
anish kumar19ad6832017-09-28 21:52:39 -07004158 w_param_text, &private_value);
4159 if (!template.kcontrol_news) {
4160 ret = -ENOMEM;
4161 goto param_fail;
4162 }
Arnd Bergmann667ebc92017-10-10 11:20:10 +02004163 } else {
4164 w_param_text = NULL;
anish kumar19ad6832017-09-28 21:52:39 -07004165 }
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004166 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01004167
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004168 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
Dan Carpenter2e558a82018-09-07 22:40:33 +03004169 if (IS_ERR(w)) {
4170 ret = PTR_ERR(w);
Charles Keepax9a1bb6002020-04-09 19:12:08 +01004171 dev_err(rtd->dev, "ASoC: Failed to create %s widget: %d\n",
4172 link_name, ret);
Linus Walleij37e1df82017-01-13 10:23:52 +01004173 goto outfree_kcontrol_news;
Dan Carpenter2e558a82018-09-07 22:40:33 +03004174 }
Mark Brownc74184e2012-04-04 22:12:09 +01004175
Jerome Bruneta72706e2019-07-25 18:59:48 +02004176 w->priv = substream;
Mark Brownc74184e2012-04-04 22:12:09 +01004177
Charles Keepax778ff5b2018-09-05 15:21:00 +01004178 return w;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004179
Nikesh Oswalc6615082015-02-02 17:06:44 +00004180outfree_kcontrol_news:
4181 devm_kfree(card->dev, (void *)template.kcontrol_news);
Charles Keepax778ff5b2018-09-05 15:21:00 +01004182 snd_soc_dapm_free_kcontrol(card, &private_value,
4183 rtd->dai_link->num_params, w_param_text);
anish kumar19ad6832017-09-28 21:52:39 -07004184param_fail:
Nikesh Oswalc6615082015-02-02 17:06:44 +00004185 devm_kfree(card->dev, link_name);
Charles Keepax778ff5b2018-09-05 15:21:00 +01004186 return ERR_PTR(ret);
Mark Brownc74184e2012-04-04 22:12:09 +01004187}
4188
Mark Brown888df392012-02-16 19:37:51 -08004189int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4190 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004191{
Mark Brown888df392012-02-16 19:37:51 -08004192 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004193 struct snd_soc_dapm_widget *w;
4194
Mark Brown888df392012-02-16 19:37:51 -08004195 WARN_ON(dapm->dev != dai->dev);
4196
4197 memset(&template, 0, sizeof(template));
4198 template.reg = SND_SOC_NOPM;
4199
4200 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01004201 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08004202 template.name = dai->driver->playback.stream_name;
4203 template.sname = dai->driver->playback.stream_name;
4204
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004205 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08004206 template.name);
4207
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004208 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01004209 if (IS_ERR(w))
4210 return PTR_ERR(w);
Mark Brown888df392012-02-16 19:37:51 -08004211
4212 w->priv = dai;
4213 dai->playback_widget = w;
4214 }
4215
4216 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01004217 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08004218 template.name = dai->driver->capture.stream_name;
4219 template.sname = dai->driver->capture.stream_name;
4220
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004221 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08004222 template.name);
4223
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004224 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01004225 if (IS_ERR(w))
4226 return PTR_ERR(w);
Mark Brown888df392012-02-16 19:37:51 -08004227
4228 w->priv = dai;
4229 dai->capture_widget = w;
4230 }
4231
4232 return 0;
4233}
4234
4235int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4236{
4237 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004238 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08004239 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08004240
4241 /* For each DAI widget... */
Kuninori Morimoto14596692020-03-09 13:08:21 +09004242 for_each_card_widgets(card, dai_w) {
Mark Brown46162742013-06-05 19:36:11 +01004243 switch (dai_w->id) {
4244 case snd_soc_dapm_dai_in:
4245 case snd_soc_dapm_dai_out:
4246 break;
4247 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02004248 continue;
Mark Brown46162742013-06-05 19:36:11 +01004249 }
Mark Brown888df392012-02-16 19:37:51 -08004250
Liam Girdwoode01b4f62018-06-14 20:26:42 +01004251 /* let users know there is no DAI to link */
4252 if (!dai_w->priv) {
4253 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4254 dai_w->name);
4255 continue;
4256 }
4257
Mark Brown888df392012-02-16 19:37:51 -08004258 dai = dai_w->priv;
4259
4260 /* ...find all widgets with the same stream and link them */
Kuninori Morimoto14596692020-03-09 13:08:21 +09004261 for_each_card_widgets(card, w) {
Mark Brown888df392012-02-16 19:37:51 -08004262 if (w->dapm != dai_w->dapm)
4263 continue;
4264
Mark Brown46162742013-06-05 19:36:11 +01004265 switch (w->id) {
4266 case snd_soc_dapm_dai_in:
4267 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08004268 continue;
Mark Brown46162742013-06-05 19:36:11 +01004269 default:
4270 break;
4271 }
Mark Brown888df392012-02-16 19:37:51 -08004272
Lars-Peter Clausena798c242015-07-21 11:51:35 +02004273 if (!w->sname || !strstr(w->sname, dai_w->sname))
Mark Brown888df392012-02-16 19:37:51 -08004274 continue;
4275
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004276 if (dai_w->id == snd_soc_dapm_dai_in) {
4277 src = dai_w;
4278 sink = w;
4279 } else {
4280 src = w;
4281 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08004282 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004283 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4284 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004285 }
4286 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02004287
Mark Brown888df392012-02-16 19:37:51 -08004288 return 0;
4289}
Liam Girdwood64a648c2011-07-25 11:15:15 +01004290
Charles Keepax595571c2020-04-09 19:12:07 +01004291static void dapm_connect_dai_routes(struct snd_soc_dapm_context *dapm,
4292 struct snd_soc_dai *src_dai,
4293 struct snd_soc_dapm_widget *src,
4294 struct snd_soc_dapm_widget *dai,
4295 struct snd_soc_dai *sink_dai,
4296 struct snd_soc_dapm_widget *sink)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004297{
Charles Keepax595571c2020-04-09 19:12:07 +01004298 dev_dbg(dapm->dev, "connected DAI link %s:%s -> %s:%s\n",
4299 src_dai->component->name, src->name,
4300 sink_dai->component->name, sink->name);
4301
4302 if (dai) {
4303 snd_soc_dapm_add_path(dapm, src, dai, NULL, NULL);
4304 src = dai;
4305 }
4306
4307 snd_soc_dapm_add_path(dapm, src, sink, NULL, NULL);
4308}
4309
4310static void dapm_connect_dai_pair(struct snd_soc_card *card,
4311 struct snd_soc_pcm_runtime *rtd,
4312 struct snd_soc_dai *codec_dai,
4313 struct snd_soc_dai *cpu_dai)
4314{
4315 struct snd_soc_dai_link *dai_link = rtd->dai_link;
4316 struct snd_soc_dapm_widget *dai, *codec, *playback_cpu, *capture_cpu;
Jerome Bruneta72706e2019-07-25 18:59:48 +02004317 struct snd_pcm_substream *substream;
4318 struct snd_pcm_str *streams = rtd->pcm->streams;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004319
Charles Keepax595571c2020-04-09 19:12:07 +01004320 if (dai_link->params) {
Charles Keepax778ff5b2018-09-05 15:21:00 +01004321 playback_cpu = cpu_dai->capture_widget;
4322 capture_cpu = cpu_dai->playback_widget;
4323 } else {
Charles Keepax595571c2020-04-09 19:12:07 +01004324 playback_cpu = cpu_dai->playback_widget;
4325 capture_cpu = cpu_dai->capture_widget;
Charles Keepax778ff5b2018-09-05 15:21:00 +01004326 }
4327
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004328 /* connect BE DAI playback if widgets are valid */
4329 codec = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004330
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004331 if (playback_cpu && codec) {
Charles Keepaxf4aa5e22020-05-26 17:19:30 +01004332 if (dai_link->params && !rtd->playback_widget) {
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004333 substream = streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
Charles Keepax595571c2020-04-09 19:12:07 +01004334 dai = snd_soc_dapm_new_dai(card, substream, "playback");
Charles Keepax9a1bb6002020-04-09 19:12:08 +01004335 if (IS_ERR(dai))
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004336 goto capture;
Charles Keepaxf4aa5e22020-05-26 17:19:30 +01004337 rtd->playback_widget = dai;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004338 }
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004339
Charles Keepax595571c2020-04-09 19:12:07 +01004340 dapm_connect_dai_routes(&card->dapm, cpu_dai, playback_cpu,
Charles Keepaxf4aa5e22020-05-26 17:19:30 +01004341 rtd->playback_widget,
Charles Keepax595571c2020-04-09 19:12:07 +01004342 codec_dai, codec);
Charles Keepaxe36a1d02018-09-10 15:28:39 +01004343 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004344
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004345capture:
4346 /* connect BE DAI capture if widgets are valid */
4347 codec = codec_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004348
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004349 if (codec && capture_cpu) {
Charles Keepaxf4aa5e22020-05-26 17:19:30 +01004350 if (dai_link->params && !rtd->capture_widget) {
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004351 substream = streams[SNDRV_PCM_STREAM_CAPTURE].substream;
Charles Keepax595571c2020-04-09 19:12:07 +01004352 dai = snd_soc_dapm_new_dai(card, substream, "capture");
Charles Keepax9a1bb6002020-04-09 19:12:08 +01004353 if (IS_ERR(dai))
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004354 return;
Charles Keepaxf4aa5e22020-05-26 17:19:30 +01004355 rtd->capture_widget = dai;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004356 }
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004357
Charles Keepax595571c2020-04-09 19:12:07 +01004358 dapm_connect_dai_routes(&card->dapm, codec_dai, codec,
Charles Keepaxf4aa5e22020-05-26 17:19:30 +01004359 rtd->capture_widget,
Charles Keepax595571c2020-04-09 19:12:07 +01004360 cpu_dai, capture_cpu);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004361 }
4362}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004363
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004364static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4365 int event)
4366{
4367 struct snd_soc_dapm_widget *w;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004368 unsigned int ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004369
Kuninori Morimoto0c01f6c2020-02-19 15:56:41 +09004370 w = snd_soc_dai_get_widget(dai, stream);
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004371
4372 if (w) {
4373 dapm_mark_dirty(w, "stream event");
4374
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004375 if (w->id == snd_soc_dapm_dai_in) {
4376 ep = SND_SOC_DAPM_EP_SOURCE;
4377 dapm_widget_invalidate_input_paths(w);
4378 } else {
4379 ep = SND_SOC_DAPM_EP_SINK;
4380 dapm_widget_invalidate_output_paths(w);
4381 }
4382
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004383 switch (event) {
4384 case SND_SOC_DAPM_STREAM_START:
4385 w->active = 1;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004386 w->is_ep = ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004387 break;
4388 case SND_SOC_DAPM_STREAM_STOP:
4389 w->active = 0;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004390 w->is_ep = 0;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004391 break;
4392 case SND_SOC_DAPM_STREAM_SUSPEND:
4393 case SND_SOC_DAPM_STREAM_RESUME:
4394 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4395 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4396 break;
4397 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004398 }
4399}
4400
Benoit Cousson44ba2642014-07-08 23:19:36 +02004401void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4402{
Mengdong Lin1a497982015-11-18 02:34:11 -05004403 struct snd_soc_pcm_runtime *rtd;
Charles Keepax0eaef952020-04-09 19:12:09 +01004404 struct snd_soc_dai *codec_dai;
4405 int i;
Benoit Cousson44ba2642014-07-08 23:19:36 +02004406
4407 /* for each BE DAI link... */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00004408 for_each_card_rtds(card, rtd) {
Benoit Cousson44ba2642014-07-08 23:19:36 +02004409 /*
4410 * dynamic FE links have no fixed DAI mapping.
4411 * CODEC<->CODEC links have no direct connection.
4412 */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004413 if (rtd->dai_link->dynamic)
Benoit Cousson44ba2642014-07-08 23:19:36 +02004414 continue;
4415
Charles Keepax0eaef952020-04-09 19:12:09 +01004416 if (rtd->num_cpus == 1) {
4417 for_each_rtd_codec_dais(rtd, i, codec_dai)
4418 dapm_connect_dai_pair(card, rtd, codec_dai,
Mark Brown39400f32020-04-14 18:04:08 +01004419 asoc_rtd_to_cpu(rtd, 0));
Charles Keepax0eaef952020-04-09 19:12:09 +01004420 } else if (rtd->num_codecs == rtd->num_cpus) {
4421 for_each_rtd_codec_dais(rtd, i, codec_dai)
4422 dapm_connect_dai_pair(card, rtd, codec_dai,
Mark Brown39400f32020-04-14 18:04:08 +01004423 asoc_rtd_to_cpu(rtd, i));
Charles Keepax0eaef952020-04-09 19:12:09 +01004424 } else {
4425 dev_err(card->dev,
4426 "N cpus to M codecs link is not supported yet\n");
4427 }
Benoit Cousson44ba2642014-07-08 23:19:36 +02004428 }
4429}
4430
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004431static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4432 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004433{
Kuninori Morimotoe3c3cf72020-03-16 15:37:09 +09004434 struct snd_soc_dai *dai;
Benoit Cousson44ba2642014-07-08 23:19:36 +02004435 int i;
4436
Kuninori Morimotoe3c3cf72020-03-16 15:37:09 +09004437 for_each_rtd_dais(rtd, i, dai)
4438 soc_dapm_dai_stream_event(dai, stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004439
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004440 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004441}
4442
4443/**
4444 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4445 * @rtd: PCM runtime data
4446 * @stream: stream name
4447 * @event: stream event
4448 *
4449 * Sends a stream event to the dapm core. The core then makes any
4450 * necessary widget power changes.
4451 *
4452 * Returns 0 for success else error.
4453 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004454void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4455 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004456{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004457 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004458
Liam Girdwood3cd04342012-03-09 12:02:08 +00004459 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004460 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004461 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004462}
Richard Purdie2b97eab2006-10-06 18:32:18 +02004463
Kuninori Morimoto3f4cf792020-01-10 11:36:23 +09004464void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream)
4465{
4466 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
4467 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
4468 /* powered down playback stream now */
4469 snd_soc_dapm_stream_event(rtd,
4470 SNDRV_PCM_STREAM_PLAYBACK,
4471 SND_SOC_DAPM_STREAM_STOP);
4472 } else {
4473 /* start delayed pop wq here for playback streams */
4474 rtd->pop_wait = 1;
4475 queue_delayed_work(system_power_efficient_wq,
4476 &rtd->delayed_work,
4477 msecs_to_jiffies(rtd->pmdown_time));
4478 }
4479 } else {
4480 /* capture streams can be powered down now */
4481 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
4482 SND_SOC_DAPM_STREAM_STOP);
4483 }
4484}
4485EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop);
4486
Richard Purdie2b97eab2006-10-06 18:32:18 +02004487/**
Charles Keepax11391102014-02-18 15:22:14 +00004488 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4489 * @dapm: DAPM context
4490 * @pin: pin name
4491 *
4492 * Enables input/output pin and its parents or children widgets iff there is
4493 * a valid audio route and active audio stream.
4494 *
4495 * Requires external locking.
4496 *
4497 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4498 * do any widget power switching.
4499 */
4500int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4501 const char *pin)
4502{
4503 return snd_soc_dapm_set_pin(dapm, pin, 1);
4504}
4505EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4506
4507/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004508 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004509 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004510 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02004511 *
Mark Brown74b8f952009-06-06 11:26:15 +01004512 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01004513 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00004514 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004515 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4516 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02004517 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004518int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004519{
Charles Keepax11391102014-02-18 15:22:14 +00004520 int ret;
4521
4522 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4523
4524 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4525
4526 mutex_unlock(&dapm->card->dapm_mutex);
4527
4528 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004529}
Liam Girdwooda5302182008-07-07 13:35:17 +01004530EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004531
4532/**
Charles Keepax11391102014-02-18 15:22:14 +00004533 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4534 * @dapm: DAPM context
4535 * @pin: pin name
4536 *
4537 * Enables input/output pin regardless of any other state. This is
4538 * intended for use with microphone bias supplies used in microphone
4539 * jack detection.
4540 *
4541 * Requires external locking.
4542 *
4543 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4544 * do any widget power switching.
4545 */
4546int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4547 const char *pin)
4548{
4549 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4550
4551 if (!w) {
4552 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4553 return -EINVAL;
4554 }
4555
4556 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02004557 if (!w->connected) {
4558 /*
4559 * w->force does not affect the number of input or output paths,
4560 * so we only have to recheck if w->connected is changed
4561 */
4562 dapm_widget_invalidate_input_paths(w);
4563 dapm_widget_invalidate_output_paths(w);
4564 w->connected = 1;
4565 }
Charles Keepax11391102014-02-18 15:22:14 +00004566 w->force = 1;
4567 dapm_mark_dirty(w, "force enable");
4568
4569 return 0;
4570}
4571EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4572
4573/**
Mark Brownda341832010-03-15 19:23:37 +00004574 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004575 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00004576 * @pin: pin name
4577 *
4578 * Enables input/output pin regardless of any other state. This is
4579 * intended for use with microphone bias supplies used in microphone
4580 * jack detection.
4581 *
4582 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4583 * do any widget power switching.
4584 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004585int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4586 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00004587{
Charles Keepax11391102014-02-18 15:22:14 +00004588 int ret;
Mark Brownda341832010-03-15 19:23:37 +00004589
Charles Keepax11391102014-02-18 15:22:14 +00004590 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00004591
Charles Keepax11391102014-02-18 15:22:14 +00004592 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09004593
Charles Keepax11391102014-02-18 15:22:14 +00004594 mutex_unlock(&dapm->card->dapm_mutex);
4595
4596 return ret;
Mark Brownda341832010-03-15 19:23:37 +00004597}
4598EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4599
4600/**
Charles Keepax11391102014-02-18 15:22:14 +00004601 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4602 * @dapm: DAPM context
4603 * @pin: pin name
4604 *
4605 * Disables input/output pin and its parents or children widgets.
4606 *
4607 * Requires external locking.
4608 *
4609 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4610 * do any widget power switching.
4611 */
4612int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4613 const char *pin)
4614{
4615 return snd_soc_dapm_set_pin(dapm, pin, 0);
4616}
4617EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4618
4619/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004620 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004621 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004622 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004623 *
Mark Brown74b8f952009-06-06 11:26:15 +01004624 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00004625 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004626 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4627 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004628 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004629int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4630 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01004631{
Charles Keepax11391102014-02-18 15:22:14 +00004632 int ret;
4633
4634 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4635
4636 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4637
4638 mutex_unlock(&dapm->card->dapm_mutex);
4639
4640 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01004641}
4642EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4643
4644/**
Charles Keepax11391102014-02-18 15:22:14 +00004645 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4646 * @dapm: DAPM context
4647 * @pin: pin name
4648 *
4649 * Marks the specified pin as being not connected, disabling it along
4650 * any parent or child widgets. At present this is identical to
4651 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4652 * additional things such as disabling controls which only affect
4653 * paths through the pin.
4654 *
4655 * Requires external locking.
4656 *
4657 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4658 * do any widget power switching.
4659 */
4660int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4661 const char *pin)
4662{
4663 return snd_soc_dapm_set_pin(dapm, pin, 0);
4664}
4665EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4666
4667/**
Mark Brown5817b522008-09-24 11:23:11 +01004668 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004669 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01004670 * @pin: pin name
4671 *
4672 * Marks the specified pin as being not connected, disabling it along
4673 * any parent or child widgets. At present this is identical to
4674 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4675 * additional things such as disabling controls which only affect
4676 * paths through the pin.
4677 *
4678 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4679 * do any widget power switching.
4680 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004681int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01004682{
Charles Keepax11391102014-02-18 15:22:14 +00004683 int ret;
4684
4685 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4686
4687 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4688
4689 mutex_unlock(&dapm->card->dapm_mutex);
4690
4691 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01004692}
4693EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4694
4695/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004696 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004697 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004698 * @pin: audio signal pin endpoint (or start point)
4699 *
4700 * Get audio pin status - connected or disconnected.
4701 *
4702 * Returns 1 for connected otherwise 0.
4703 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004704int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4705 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004706{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004707 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004708
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004709 if (w)
4710 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06004711
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004712 return 0;
4713}
Liam Girdwooda5302182008-07-07 13:35:17 +01004714EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004715
4716/**
Mark Brown1547aba2010-05-07 21:11:40 +01004717 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004718 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01004719 * @pin: audio signal pin endpoint (or start point)
4720 *
4721 * Mark the given endpoint or pin as ignoring suspend. When the
4722 * system is disabled a path between two endpoints flagged as ignoring
4723 * suspend will not be disabled. The path must already be enabled via
4724 * normal means at suspend time, it will not be turned on if it was not
4725 * already enabled.
4726 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004727int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4728 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01004729{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004730 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01004731
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004732 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004733 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004734 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01004735 }
4736
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004737 w->ignore_suspend = 1;
4738
4739 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01004740}
4741EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4742
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02004743/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02004744 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03004745 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02004746 *
4747 * Free all dapm widgets and resources.
4748 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004749void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004750{
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02004751 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004752 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02004753 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004754}
4755EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4756
Kuninori Morimoto95c267d2019-08-23 09:58:52 +09004757void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
4758 struct snd_soc_card *card,
4759 struct snd_soc_component *component)
4760{
4761 dapm->card = card;
4762 dapm->component = component;
4763 dapm->bias_level = SND_SOC_BIAS_OFF;
4764
4765 if (component) {
4766 dapm->dev = component->dev;
4767 dapm->idle_bias_off = !component->driver->idle_bias_on,
4768 dapm->suspend_bias_off = component->driver->suspend_bias_off;
4769 } else {
4770 dapm->dev = card->dev;
4771 }
4772
4773 INIT_LIST_HEAD(&dapm->list);
Kuninori Morimotodf817f82020-03-09 13:08:16 +09004774 /* see for_each_card_dapms */
Kuninori Morimoto95c267d2019-08-23 09:58:52 +09004775 list_add(&dapm->list, &card->dapm_list);
4776}
4777EXPORT_SYMBOL_GPL(snd_soc_dapm_init);
4778
Xiang Xiao57996352014-03-02 00:04:02 +08004779static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01004780{
Liam Girdwood01005a72012-07-06 16:57:05 +01004781 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01004782 struct snd_soc_dapm_widget *w;
4783 LIST_HEAD(down_list);
4784 int powerdown = 0;
4785
Liam Girdwood01005a72012-07-06 16:57:05 +01004786 mutex_lock(&card->dapm_mutex);
4787
Kuninori Morimoto14596692020-03-09 13:08:21 +09004788 for_each_card_widgets(dapm->card, w) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02004789 if (w->dapm != dapm)
4790 continue;
Mark Brown51737472009-06-22 13:16:51 +01004791 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004792 dapm_seq_insert(w, &down_list, false);
Charles Keepax9b319302020-02-28 15:31:45 +00004793 w->new_power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004794 powerdown = 1;
4795 }
4796 }
4797
4798 /* If there were no widgets to power down we're already in
4799 * standby.
4800 */
4801 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004802 if (dapm->bias_level == SND_SOC_BIAS_ON)
4803 snd_soc_dapm_set_bias_level(dapm,
4804 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004805 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004806 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4807 snd_soc_dapm_set_bias_level(dapm,
4808 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004809 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004810
4811 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004812}
Mark Brown51737472009-06-22 13:16:51 +01004813
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004814/*
4815 * snd_soc_dapm_shutdown - callback for system shutdown
4816 */
4817void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4818{
Xiang Xiao57996352014-03-02 00:04:02 +08004819 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004820
Kuninori Morimotodf817f82020-03-09 13:08:16 +09004821 for_each_card_dapms(card, dapm) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08004822 if (dapm != &card->dapm) {
4823 soc_dapm_shutdown_dapm(dapm);
4824 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4825 snd_soc_dapm_set_bias_level(dapm,
4826 SND_SOC_BIAS_OFF);
4827 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004828 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08004829
4830 soc_dapm_shutdown_dapm(&card->dapm);
4831 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4832 snd_soc_dapm_set_bias_level(&card->dapm,
4833 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01004834}
4835
Richard Purdie2b97eab2006-10-06 18:32:18 +02004836/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004837MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004838MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4839MODULE_LICENSE("GPL");