blob: 679ed60d850ec6e00946c54ecc4c9aece98b1614 [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;
426 template.mask = e->mask << e->shift_l;
427 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
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200549 if (data->widget)
550 data->widget->on_val = value;
551
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200552 data->value = value;
553
554 return true;
555}
556
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200557/**
Mythri P K93e39a12015-10-20 22:30:08 +0530558 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
559 * kcontrol
560 * @kcontrol: The kcontrol
561 */
562struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
563 struct snd_kcontrol *kcontrol)
564{
565 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
566}
567EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
568
569/**
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200570 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
571 * kcontrol
572 * @kcontrol: The kcontrol
573 *
574 * Note: This function must only be used on kcontrols that are known to have
575 * been registered for a CODEC. Otherwise the behaviour is undefined.
576 */
577struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
578 struct snd_kcontrol *kcontrol)
579{
580 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
581}
582EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
583
Liam Girdwood6c120e12012-02-15 15:15:34 +0000584static void dapm_reset(struct snd_soc_card *card)
585{
586 struct snd_soc_dapm_widget *w;
587
Mark Brownf9fa2b12014-03-06 16:49:11 +0800588 lockdep_assert_held(&card->dapm_mutex);
589
Liam Girdwood6c120e12012-02-15 15:15:34 +0000590 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
591
Kuninori Morimoto14596692020-03-09 13:08:21 +0900592 for_each_card_widgets(card, w) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200593 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000594 w->power_checked = false;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000595 }
596}
597
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200598static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
599{
600 if (!dapm->component)
601 return NULL;
602 return dapm->component->name_prefix;
603}
604
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200605static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800606 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100607{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200608 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200609 return -EIO;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200610 return snd_soc_component_read(dapm->component, reg, value);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100611}
612
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200613static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
Bard Liao34775012014-04-17 20:12:56 +0800614 int reg, unsigned int mask, unsigned int value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100615{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200616 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200617 return -EIO;
Mark Brownfcf6c5e2014-12-15 13:08:48 +0000618 return snd_soc_component_update_bits(dapm->component, reg,
619 mask, value);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000620}
621
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200622static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
623 int reg, unsigned int mask, unsigned int value)
624{
625 if (!dapm->component)
626 return -EIO;
627 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
628}
629
Mark Browneb270e92013-10-09 13:52:52 +0100630static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
631{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200632 if (dapm->component)
633 snd_soc_component_async_complete(dapm->component);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100634}
635
Charles Keepax45a110a2015-05-11 13:50:30 +0100636static struct snd_soc_dapm_widget *
637dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
638{
639 struct snd_soc_dapm_widget *w = wcache->widget;
640 struct list_head *wlist;
641 const int depth = 2;
642 int i = 0;
643
644 if (w) {
645 wlist = &w->dapm->card->widgets;
646
647 list_for_each_entry_from(w, wlist, list) {
648 if (!strcmp(name, w->name))
649 return w;
650
651 if (++i == depth)
652 break;
653 }
654 }
655
656 return NULL;
657}
658
659static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
660 struct snd_soc_dapm_widget *w)
661{
662 wcache->widget = w;
663}
664
Mark Brown452c5ea2009-05-17 21:41:23 +0100665/**
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200666 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
667 * @dapm: The DAPM context for which to set the level
668 * @level: The level to set
669 *
670 * Forces the DAPM bias level to a specific state. It will call the bias level
671 * callback of DAPM context with the specified level. This will even happen if
672 * the context is already at the same level. Furthermore it will not go through
673 * the normal bias level sequencing, meaning any intermediate states between the
674 * current and the target state will not be entered.
675 *
676 * Note that the change in bias level is only temporary and the next time
677 * snd_soc_dapm_sync() is called the state will be set to the level as
678 * determined by the DAPM core. The function is mainly intended to be used to
679 * used during probe or resume from suspend to power up the device so
680 * initialization can be done, before the DAPM core takes over.
681 */
682int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
683 enum snd_soc_bias_level level)
684{
685 int ret = 0;
686
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900687 if (dapm->component)
688 ret = snd_soc_component_set_bias_level(dapm->component, level);
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200689
Lars-Peter Clausenf4bf8d72015-04-27 22:13:25 +0200690 if (ret == 0)
691 dapm->bias_level = level;
692
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200693 return ret;
694}
695EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
696
Mark Brown452c5ea2009-05-17 21:41:23 +0100697/**
698 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800699 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100700 * @level: level to configure
701 *
702 * Configure the bias (power) levels for the SoC audio device.
703 *
704 * Returns 0 for success else error.
705 */
Mark Browned5a4c42011-02-18 11:12:42 -0800706static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200707 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100708{
Mark Browned5a4c42011-02-18 11:12:42 -0800709 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100710 int ret = 0;
711
Mark Brown84e90932010-11-04 00:07:02 -0400712 trace_snd_soc_bias_level_start(card, level);
713
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000714 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100715 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100716 if (ret != 0)
717 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100718
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200719 if (!card || dapm != &card->dapm)
720 ret = snd_soc_dapm_force_bias_level(dapm, level);
Liam Girdwood41231282012-07-06 16:56:16 +0100721
Mark Brown171ec6b2011-06-06 18:15:19 +0100722 if (ret != 0)
723 goto out;
724
725 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100726 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100727out:
Mark Brown84e90932010-11-04 00:07:02 -0400728 trace_snd_soc_bias_level_done(card, level);
729
Mark Brown452c5ea2009-05-17 21:41:23 +0100730 return ret;
731}
732
Mark Brown74b8f952009-06-06 11:26:15 +0100733/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200734static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200735 struct snd_soc_dapm_path *path, const char *control_name,
736 struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200737{
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200738 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200739 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100740 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200741 int i;
742
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100743 if (e->reg != SND_SOC_NOPM) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200744 soc_dapm_read(dapm, e->reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100745 val = (val >> e->shift_l) & e->mask;
746 item = snd_soc_enum_val_to_item(e, val);
747 } else {
748 /* since a virtual mux has no backing registers to
749 * decide which path to connect, it will try to match
750 * with the first enumeration. This is to ensure
751 * that the default mux choice (the first) will be
752 * correctly powered up during initialization.
753 */
754 item = 0;
755 }
756
Xie Yishengf9e0b4a2018-05-31 19:11:23 +0800757 i = match_string(e->texts, e->items, control_name);
758 if (i < 0)
759 return -ENODEV;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200760
Xie Yishengf9e0b4a2018-05-31 19:11:23 +0800761 path->name = e->texts[i];
762 path->connect = (i == item);
763 return 0;
764
Richard Purdie2b97eab2006-10-06 18:32:18 +0200765}
766
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100767/* set up initial codec paths */
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800768static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
769 int nth_path)
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100770{
771 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200772 p->sink->kcontrol_news[i].private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100773 unsigned int reg = mc->reg;
774 unsigned int shift = mc->shift;
775 unsigned int max = mc->max;
776 unsigned int mask = (1 << fls(max)) - 1;
777 unsigned int invert = mc->invert;
778 unsigned int val;
779
780 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200781 soc_dapm_read(p->sink->dapm, reg, &val);
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800782 /*
783 * The nth_path argument allows this function to know
784 * which path of a kcontrol it is setting the initial
785 * status for. Ideally this would support any number
786 * of paths and channels. But since kcontrols only come
787 * in mono and stereo variants, we are limited to 2
788 * channels.
789 *
790 * The following code assumes for stereo controls the
791 * first path is the left channel, and all remaining
792 * paths are the right channel.
793 */
794 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
795 if (reg != mc->rreg)
796 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
797 val = (val >> mc->rshift) & mask;
798 } else {
799 val = (val >> shift) & mask;
800 }
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100801 if (invert)
802 val = max - val;
803 p->connect = !!val;
804 } else {
이경택3bbbb772020-03-31 16:55:16 +0900805 /* since a virtual mixer has no backing registers to
806 * decide which path to connect, it will try to match
807 * with initial state. This is to ensure
808 * that the default mixer choice will be
809 * correctly powered up during initialization.
810 */
811 p->connect = invert;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100812 }
813}
814
Mark Brown74b8f952009-06-06 11:26:15 +0100815/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200816static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200817 struct snd_soc_dapm_path *path, const char *control_name)
818{
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800819 int i, nth_path = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200820
821 /* search for mixer kcontrol */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200822 for (i = 0; i < path->sink->num_kcontrols; i++) {
823 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
824 path->name = path->sink->kcontrol_news[i].name;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800825 dapm_set_mixer_path_status(path, i, nth_path++);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200826 return 0;
827 }
828 }
829 return -ENODEV;
830}
831
Stephen Warrenaf468002011-04-28 17:38:01 -0600832static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600833 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600834 const struct snd_kcontrol_new *kcontrol_new,
835 struct snd_kcontrol **kcontrol)
836{
837 struct snd_soc_dapm_widget *w;
838 int i;
839
840 *kcontrol = NULL;
841
Kuninori Morimoto14596692020-03-09 13:08:21 +0900842 for_each_card_widgets(dapm->card, w) {
Stephen Warren1007da02011-05-26 09:57:33 -0600843 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
844 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600845 for (i = 0; i < w->num_kcontrols; i++) {
846 if (&w->kcontrol_news[i] == kcontrol_new) {
847 if (w->kcontrols)
848 *kcontrol = w->kcontrols[i];
849 return 1;
850 }
851 }
852 }
853
854 return 0;
855}
856
Stephen Warren85762e72013-03-29 15:40:10 -0600857/*
858 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
859 * create it. Either way, add the widget into the control's widget list
860 */
Jeeja KP19a2557b2015-10-20 22:30:06 +0530861static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100862 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200863{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200864 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000865 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000866 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600867 size_t prefix_len;
868 int shared;
869 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600870 bool wname_in_long_name, kcname_in_long_name;
Daniel Macke5092c92014-10-07 13:41:24 +0200871 char *long_name = NULL;
Stephen Warren85762e72013-03-29 15:40:10 -0600872 const char *name;
Daniel Macke5092c92014-10-07 13:41:24 +0200873 int ret = 0;
Mark Brownefb7ac32011-03-08 17:23:24 +0000874
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200875 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000876 if (prefix)
877 prefix_len = strlen(prefix) + 1;
878 else
879 prefix_len = 0;
880
Stephen Warren85762e72013-03-29 15:40:10 -0600881 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
882 &kcontrol);
883
Stephen Warren85762e72013-03-29 15:40:10 -0600884 if (!kcontrol) {
885 if (shared) {
886 wname_in_long_name = false;
887 kcname_in_long_name = true;
888 } else {
889 switch (w->id) {
890 case snd_soc_dapm_switch:
891 case snd_soc_dapm_mixer:
Jeeja KP19a2557b2015-10-20 22:30:06 +0530892 case snd_soc_dapm_pga:
Seppo Ingalsuo882c8b4a2019-04-30 18:11:34 -0500893 case snd_soc_dapm_effect:
Chen-Yu Tsaia3930ed2016-08-27 19:28:00 +0800894 case snd_soc_dapm_out_drv:
Stephen Warren85762e72013-03-29 15:40:10 -0600895 wname_in_long_name = true;
896 kcname_in_long_name = true;
897 break;
898 case snd_soc_dapm_mixer_named_ctl:
899 wname_in_long_name = false;
900 kcname_in_long_name = true;
901 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200902 case snd_soc_dapm_demux:
Stephen Warren85762e72013-03-29 15:40:10 -0600903 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600904 wname_in_long_name = true;
905 kcname_in_long_name = false;
906 break;
907 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600908 return -EINVAL;
909 }
910 }
911
912 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600913 /*
914 * The control will get a prefix from the control
915 * creation process but we're also using the same
916 * prefix for widgets so cut the prefix off the
917 * front of the widget name.
918 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200919 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600920 w->name + prefix_len,
921 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200922 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200923 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600924
925 name = long_name;
926 } else if (wname_in_long_name) {
927 long_name = NULL;
928 name = w->name + prefix_len;
929 } else {
930 long_name = NULL;
931 name = w->kcontrol_news[kci].name;
932 }
933
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200934 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600935 prefix);
Daniel Macke5092c92014-10-07 13:41:24 +0200936 if (!kcontrol) {
937 ret = -ENOMEM;
938 goto exit_free;
939 }
940
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200941 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200942
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100943 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200944 if (ret) {
945 snd_ctl_free_one(kcontrol);
Daniel Macke5092c92014-10-07 13:41:24 +0200946 goto exit_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200947 }
948
Stephen Warren85762e72013-03-29 15:40:10 -0600949 ret = snd_ctl_add(card, kcontrol);
950 if (ret < 0) {
951 dev_err(dapm->dev,
952 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
953 w->name, name, ret);
Daniel Macke5092c92014-10-07 13:41:24 +0200954 goto exit_free;
Stephen Warren85762e72013-03-29 15:40:10 -0600955 }
Stephen Warren85762e72013-03-29 15:40:10 -0600956 }
957
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200958 ret = dapm_kcontrol_add_widget(kcontrol, w);
Daniel Macke5092c92014-10-07 13:41:24 +0200959 if (ret == 0)
960 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200961
Daniel Macke5092c92014-10-07 13:41:24 +0200962exit_free:
963 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600964
Daniel Macke5092c92014-10-07 13:41:24 +0200965 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600966}
967
968/* create new dapm mixer control */
969static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
970{
971 int i, ret;
972 struct snd_soc_dapm_path *path;
Charles Keepax561ed682015-05-01 12:37:26 +0100973 struct dapm_kcontrol_data *data;
Stephen Warren85762e72013-03-29 15:40:10 -0600974
Richard Purdie2b97eab2006-10-06 18:32:18 +0200975 /* add kcontrol */
976 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200977 /* match name */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +0200978 snd_soc_dapm_widget_for_each_source_path(w, path) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200979 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600980 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200981 continue;
982
Charles Keepax561ed682015-05-01 12:37:26 +0100983 if (!w->kcontrols[i]) {
Jeeja KP19a2557b2015-10-20 22:30:06 +0530984 ret = dapm_create_or_share_kcontrol(w, i);
Charles Keepax561ed682015-05-01 12:37:26 +0100985 if (ret < 0)
986 return ret;
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200987 }
988
Mark Brown946d92a2013-08-12 23:28:42 +0100989 dapm_kcontrol_add_path(w->kcontrols[i], path);
Charles Keepax561ed682015-05-01 12:37:26 +0100990
991 data = snd_kcontrol_chip(w->kcontrols[i]);
992 if (data->widget)
993 snd_soc_dapm_add_path(data->widget->dapm,
994 data->widget,
995 path->source,
996 NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200997 }
998 }
Stephen Warren85762e72013-03-29 15:40:10 -0600999
1000 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001001}
1002
1003/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001004static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001005{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001006 struct snd_soc_dapm_context *dapm = w->dapm;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001007 enum snd_soc_dapm_direction dir;
Stephen Warren85762e72013-03-29 15:40:10 -06001008 struct snd_soc_dapm_path *path;
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001009 const char *type;
Stephen Warrenaf468002011-04-28 17:38:01 -06001010 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001011
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001012 switch (w->id) {
1013 case snd_soc_dapm_mux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001014 dir = SND_SOC_DAPM_DIR_OUT;
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001015 type = "mux";
1016 break;
1017 case snd_soc_dapm_demux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001018 dir = SND_SOC_DAPM_DIR_IN;
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001019 type = "demux";
1020 break;
1021 default:
1022 return -EINVAL;
1023 }
1024
Stephen Warrenaf468002011-04-28 17:38:01 -06001025 if (w->num_kcontrols != 1) {
1026 dev_err(dapm->dev,
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001027 "ASoC: %s %s has incorrect number of controls\n", type,
Stephen Warrenaf468002011-04-28 17:38:01 -06001028 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001029 return -EINVAL;
1030 }
1031
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001032 if (list_empty(&w->edges[dir])) {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001033 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
Stephen Warren85762e72013-03-29 15:40:10 -06001034 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -06001035 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001036
Jeeja KP19a2557b2015-10-20 22:30:06 +05301037 ret = dapm_create_or_share_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -06001038 if (ret < 0)
1039 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -06001040
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001041 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1042 if (path->name)
1043 dapm_kcontrol_add_path(w->kcontrols[0], path);
Lars-Peter Clausen98407ef2014-10-25 17:41:57 +02001044 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001045
Stephen Warrenaf468002011-04-28 17:38:01 -06001046 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001047}
1048
1049/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001050static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001051{
Jeeja KP19a2557b2015-10-20 22:30:06 +05301052 int i, ret;
1053
1054 for (i = 0; i < w->num_kcontrols; i++) {
1055 ret = dapm_create_or_share_kcontrol(w, i);
1056 if (ret < 0)
1057 return ret;
1058 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001059
Mark Browna6c65732010-03-03 17:45:21 +00001060 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001061}
1062
Nikesh Oswalc6615082015-02-02 17:06:44 +00001063/* create new dapm dai link control */
1064static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1065{
1066 int i, ret;
1067 struct snd_kcontrol *kcontrol;
1068 struct snd_soc_dapm_context *dapm = w->dapm;
1069 struct snd_card *card = dapm->card->snd_card;
Charles Keepax243bcfa2018-09-05 15:21:02 +01001070 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00001071
1072 /* create control for links with > 1 config */
Charles Keepax243bcfa2018-09-05 15:21:02 +01001073 if (rtd->dai_link->num_params <= 1)
Nikesh Oswalc6615082015-02-02 17:06:44 +00001074 return 0;
1075
1076 /* add kcontrol */
1077 for (i = 0; i < w->num_kcontrols; i++) {
1078 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1079 w->name, NULL);
1080 ret = snd_ctl_add(card, kcontrol);
1081 if (ret < 0) {
1082 dev_err(dapm->dev,
1083 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1084 w->name, w->kcontrol_news[i].name, ret);
1085 return ret;
1086 }
1087 kcontrol->private_data = w;
1088 w->kcontrols[i] = kcontrol;
1089 }
1090
1091 return 0;
1092}
1093
Mark Brown9949788b2010-05-07 20:24:05 +01001094/* We implement power down on suspend by checking the power state of
1095 * the ALSA card - when we are suspending the ALSA state for the card
1096 * is set to D3.
1097 */
1098static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1099{
Mark Brown12ea2c72011-03-02 18:17:32 +00001100 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +01001101
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001102 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +01001103 case SNDRV_CTL_POWER_D3hot:
1104 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +01001105 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001106 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001107 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +01001108 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +01001109 default:
1110 return 1;
1111 }
1112}
1113
Kuninori Morimoto52645e332020-02-19 15:56:52 +09001114static void dapm_widget_list_free(struct snd_soc_dapm_widget_list **list)
1115{
1116 kfree(*list);
1117}
1118
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001119static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1120 struct list_head *widgets)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001121{
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001122 struct snd_soc_dapm_widget *w;
1123 struct list_head *it;
1124 unsigned int size = 0;
1125 unsigned int i = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001126
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001127 list_for_each(it, widgets)
1128 size++;
1129
Kees Cookacafe7e2018-05-08 13:45:50 -07001130 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001131 if (*list == NULL)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001132 return -ENOMEM;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001133
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001134 list_for_each_entry(w, widgets, work_list)
1135 (*list)->widgets[i++] = w;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001136
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001137 (*list)->num_widgets = i;
1138
1139 return 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001140}
1141
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001142/*
Szymon Mielczarek872f3ac2019-08-09 10:40:34 +02001143 * Recursively reset the cached number of inputs or outputs for the specified
1144 * widget and all widgets that can be reached via incoming or outcoming paths
1145 * from the widget.
1146 */
1147static void invalidate_paths_ep(struct snd_soc_dapm_widget *widget,
1148 enum snd_soc_dapm_direction dir)
1149{
1150 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1151 struct snd_soc_dapm_path *path;
1152
1153 widget->endpoints[dir] = -1;
1154
1155 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1156 if (path->weak || path->is_supply)
1157 continue;
1158
1159 if (path->walking)
1160 return;
1161
1162 if (path->connect) {
1163 path->walking = 1;
1164 invalidate_paths_ep(path->node[dir], dir);
1165 path->walking = 0;
1166 }
1167 }
1168}
1169
1170/*
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001171 * Common implementation for is_connected_output_ep() and
1172 * is_connected_input_ep(). The function is inlined since the combined size of
1173 * the two specialized functions is only marginally larger then the size of the
1174 * generic function and at the same time the fast path of the specialized
1175 * functions is significantly smaller than the generic function.
1176 */
1177static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1178 struct list_head *list, enum snd_soc_dapm_direction dir,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001179 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1180 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1181 enum snd_soc_dapm_direction)),
1182 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1183 enum snd_soc_dapm_direction))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001184{
1185 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001186 struct snd_soc_dapm_path *path;
1187 int con = 0;
1188
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001189 if (widget->endpoints[dir] >= 0)
1190 return widget->endpoints[dir];
Mark Brown024dc072011-10-09 11:52:05 +01001191
Mark Brownde02d072011-09-20 21:43:24 +01001192 DAPM_UPDATE_STAT(widget, path_checks);
1193
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001194 /* do we need to add this widget to the list ? */
1195 if (list)
1196 list_add_tail(&widget->work_list, list);
1197
Jeeja KP09464972016-06-15 11:16:55 +05301198 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
Charles Keepax8dd26df2019-07-18 09:43:33 +01001199 list = NULL;
1200 custom_stop_condition = NULL;
Jeeja KP09464972016-06-15 11:16:55 +05301201 }
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001202
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001203 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1204 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1205 return widget->endpoints[dir];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001206 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001207
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001208 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
Mark Browne56235e02011-09-21 18:19:14 +01001209 DAPM_UPDATE_STAT(widget, neighbour_checks);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001210
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001211 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +01001212 continue;
1213
Mark Brown8af294b2013-02-22 17:48:15 +00001214 if (path->walking)
1215 return 1;
1216
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001217 trace_snd_soc_dapm_path(widget, dir, path);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001218
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001219 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +00001220 path->walking = 1;
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001221 con += fn(path->node[dir], list, custom_stop_condition);
Mark Brown8af294b2013-02-22 17:48:15 +00001222 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001223 }
1224 }
1225
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001226 widget->endpoints[dir] = con;
Mark Brown024dc072011-10-09 11:52:05 +01001227
Richard Purdie2b97eab2006-10-06 18:32:18 +02001228 return con;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001229}
1230
1231/*
1232 * Recursively check for a completed path to an active or physically connected
1233 * output widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001234 *
1235 * Optionally, can be supplied with a function acting as a stopping condition.
1236 * This function takes the dapm widget currently being examined and the walk
Charles Keepax8dd26df2019-07-18 09:43:33 +01001237 * direction as an arguments, it should return true if widgets from that point
1238 * in the graph onwards should not be added to the widget list.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001239 */
1240static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001241 struct list_head *list,
1242 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1243 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001244{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001245 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001246 is_connected_output_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001247}
1248
1249/*
1250 * Recursively check for a completed path to an active or physically connected
1251 * input widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001252 *
1253 * Optionally, can be supplied with a function acting as a stopping condition.
1254 * This function takes the dapm widget currently being examined and the walk
1255 * direction as an arguments, it should return true if the walk should be
1256 * stopped and false otherwise.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001257 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001258static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001259 struct list_head *list,
1260 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1261 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001262{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001263 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001264 is_connected_input_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001265}
1266
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001267/**
1268 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1269 * @dai: the soc DAI.
1270 * @stream: stream direction.
1271 * @list: list of active widgets for this stream.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001272 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1273 * walk based on custom logic.
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001274 *
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001275 * Queries DAPM graph as to whether a valid audio stream path exists for
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001276 * the initial stream specified by name. This takes into account
1277 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1278 *
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001279 * Optionally, can be supplied with a function acting as a stopping condition.
1280 * This function takes the dapm widget currently being examined and the walk
1281 * direction as an arguments, it should return true if the walk should be
1282 * stopped and false otherwise.
1283 *
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001284 * Returns the number of valid paths or negative error.
1285 */
1286int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001287 struct snd_soc_dapm_widget_list **list,
1288 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1289 enum snd_soc_dapm_direction))
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001290{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01001291 struct snd_soc_card *card = dai->component->card;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001292 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001293 LIST_HEAD(widgets);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001294 int paths;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001295 int ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001296
1297 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001298
Szymon Mielczarek872f3ac2019-08-09 10:40:34 +02001299 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1300 w = dai->playback_widget;
1301 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_OUT);
1302 paths = is_connected_output_ep(w, &widgets,
1303 custom_stop_condition);
1304 } else {
1305 w = dai->capture_widget;
1306 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_IN);
1307 paths = is_connected_input_ep(w, &widgets,
1308 custom_stop_condition);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001309 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001310
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001311 /* Drop starting point */
1312 list_del(widgets.next);
1313
1314 ret = dapm_widget_list_create(list, &widgets);
1315 if (ret)
Lars-Peter Clausen30abbe72015-08-11 21:37:59 +02001316 paths = ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001317
1318 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001319 mutex_unlock(&card->dapm_mutex);
1320
1321 return paths;
1322}
1323
Kuninori Morimoto52645e332020-02-19 15:56:52 +09001324void snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list **list)
1325{
1326 dapm_widget_list_free(list);
1327}
1328
Richard Purdie2b97eab2006-10-06 18:32:18 +02001329/*
Mark Brown62ea8742012-01-21 21:14:48 +00001330 * Handler for regulator supply widget.
1331 */
1332int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1333 struct snd_kcontrol *kcontrol, int event)
1334{
Mark Brownc05b84d2012-09-07 12:57:11 +08001335 int ret;
1336
Mark Browneb270e92013-10-09 13:52:52 +01001337 soc_dapm_async_complete(w->dapm);
1338
Mark Brownc05b84d2012-09-07 12:57:11 +08001339 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001340 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001341 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001342 if (ret != 0)
1343 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001344 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001345 w->name, ret);
1346 }
1347
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001348 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001349 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001350 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001351 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001352 if (ret != 0)
1353 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001354 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001355 w->name, ret);
1356 }
1357
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001358 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001359 }
Mark Brown62ea8742012-01-21 21:14:48 +00001360}
1361EXPORT_SYMBOL_GPL(dapm_regulator_event);
1362
Ola Liljad7e7eb92012-05-24 15:26:25 +02001363/*
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00001364 * Handler for pinctrl widget.
1365 */
1366int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1367 struct snd_kcontrol *kcontrol, int event)
1368{
1369 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1370 struct pinctrl *p = w->pinctrl;
1371 struct pinctrl_state *s;
1372
1373 if (!p || !priv)
1374 return -EIO;
1375
1376 if (SND_SOC_DAPM_EVENT_ON(event))
1377 s = pinctrl_lookup_state(p, priv->active_state);
1378 else
1379 s = pinctrl_lookup_state(p, priv->sleep_state);
1380
1381 if (IS_ERR(s))
1382 return PTR_ERR(s);
1383
1384 return pinctrl_select_state(p, s);
1385}
1386EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1387
1388/*
Ola Liljad7e7eb92012-05-24 15:26:25 +02001389 * Handler for clock supply widget.
1390 */
1391int dapm_clock_event(struct snd_soc_dapm_widget *w,
1392 struct snd_kcontrol *kcontrol, int event)
1393{
1394 if (!w->clk)
1395 return -EIO;
1396
Mark Browneb270e92013-10-09 13:52:52 +01001397 soc_dapm_async_complete(w->dapm);
1398
Ola Liljad7e7eb92012-05-24 15:26:25 +02001399 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001400 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001401 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001402 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001403 return 0;
1404 }
Charles Keepaxd78b1e432018-08-28 14:35:02 +01001405
Marek Belisko98b3cf12012-07-12 23:00:16 +02001406 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001407}
1408EXPORT_SYMBOL_GPL(dapm_clock_event);
1409
Mark Brownd805002b2011-09-28 18:28:23 +01001410static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1411{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001412 if (w->power_checked)
1413 return w->new_power;
1414
Mark Brownd805002b2011-09-28 18:28:23 +01001415 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001416 w->new_power = 1;
Mark Brownd805002b2011-09-28 18:28:23 +01001417 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001418 w->new_power = w->power_check(w);
1419
1420 w->power_checked = true;
1421
1422 return w->new_power;
Mark Brownd805002b2011-09-28 18:28:23 +01001423}
1424
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001425/* Generic check to see if a widget should be powered. */
Mark Browncd0f2d42009-04-20 16:56:59 +01001426static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1427{
1428 int in, out;
1429
Mark Brownde02d072011-09-20 21:43:24 +01001430 DAPM_UPDATE_STAT(w, power_checks);
1431
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001432 in = is_connected_input_ep(w, NULL, NULL);
1433 out = is_connected_output_ep(w, NULL, NULL);
Mark Browncd0f2d42009-04-20 16:56:59 +01001434 return out != 0 && in != 0;
1435}
1436
Mark Brown246d0a12009-04-22 18:24:55 +01001437/* Check to see if a power supply is needed */
1438static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1439{
1440 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001441
Mark Brownde02d072011-09-20 21:43:24 +01001442 DAPM_UPDATE_STAT(w, power_checks);
1443
Mark Brown246d0a12009-04-22 18:24:55 +01001444 /* Check if one of our outputs is connected */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001445 snd_soc_dapm_widget_for_each_sink_path(w, path) {
Mark Browna8fdac82011-09-28 18:20:26 +01001446 DAPM_UPDATE_STAT(w, neighbour_checks);
1447
Mark Brownbf3a9e12011-06-13 16:42:29 +01001448 if (path->weak)
1449 continue;
1450
Mark Brown215edda2009-09-08 18:59:05 +01001451 if (path->connected &&
1452 !path->connected(path->source, path->sink))
1453 continue;
1454
Mark Brownf68d7e12011-10-04 22:57:50 +01001455 if (dapm_widget_power_check(path->sink))
1456 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001457 }
1458
Mark Brownf68d7e12011-10-04 22:57:50 +01001459 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001460}
1461
Mark Brown35c64bc2011-09-28 18:23:53 +01001462static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1463{
Charles Keepax20bb0182015-12-02 10:22:16 +00001464 return w->connected;
Mark Brown35c64bc2011-09-28 18:23:53 +01001465}
1466
Mark Brown38357ab2009-06-06 19:03:23 +01001467static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1468 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001469 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001470{
Mark Brown828a8422011-01-15 13:14:30 +00001471 int *sort;
1472
Pierre-Louis Bossartf13d4b52019-02-05 10:22:28 -06001473 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1474 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1475
Mark Brown828a8422011-01-15 13:14:30 +00001476 if (power_up)
1477 sort = dapm_up_seq;
1478 else
1479 sort = dapm_down_seq;
1480
Pierre-Louis Bossartf13d4b52019-02-05 10:22:28 -06001481 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1482 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1483
Mark Brown38357ab2009-06-06 19:03:23 +01001484 if (sort[a->id] != sort[b->id])
1485 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001486 if (a->subseq != b->subseq) {
1487 if (power_up)
1488 return a->subseq - b->subseq;
1489 else
1490 return b->subseq - a->subseq;
1491 }
Mark Brownb22ead22009-06-07 12:51:26 +01001492 if (a->reg != b->reg)
1493 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001494 if (a->dapm != b->dapm)
1495 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001496
Mark Brown38357ab2009-06-06 19:03:23 +01001497 return 0;
1498}
Mark Brown42aa3412009-03-01 19:21:10 +00001499
Mark Brown38357ab2009-06-06 19:03:23 +01001500/* Insert a widget in order into a DAPM power sequence. */
1501static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1502 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001503 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001504{
1505 struct snd_soc_dapm_widget *w;
1506
1507 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001508 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001509 list_add_tail(&new_widget->power_list, &w->power_list);
1510 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001511 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001512
Mark Brown38357ab2009-06-06 19:03:23 +01001513 list_add_tail(&new_widget->power_list, list);
1514}
Mark Brown42aa3412009-03-01 19:21:10 +00001515
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001516static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001517 struct snd_soc_dapm_widget *w, int event)
1518{
Mark Brown68f89ad2010-11-03 23:51:49 -04001519 const char *ev_name;
1520 int power, ret;
1521
1522 switch (event) {
1523 case SND_SOC_DAPM_PRE_PMU:
1524 ev_name = "PRE_PMU";
1525 power = 1;
1526 break;
1527 case SND_SOC_DAPM_POST_PMU:
1528 ev_name = "POST_PMU";
1529 power = 1;
1530 break;
1531 case SND_SOC_DAPM_PRE_PMD:
1532 ev_name = "PRE_PMD";
1533 power = 0;
1534 break;
1535 case SND_SOC_DAPM_POST_PMD:
1536 ev_name = "POST_PMD";
1537 power = 0;
1538 break;
Mark Brown80114122013-02-25 15:14:19 +00001539 case SND_SOC_DAPM_WILL_PMU:
1540 ev_name = "WILL_PMU";
1541 power = 1;
1542 break;
1543 case SND_SOC_DAPM_WILL_PMD:
1544 ev_name = "WILL_PMD";
1545 power = 0;
1546 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001547 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001548 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001549 return;
1550 }
1551
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001552 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001553 return;
1554
1555 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001556 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001557 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001558 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001559 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001560 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001561 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001562 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001563 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001564 ev_name, w->name, ret);
1565 }
1566}
1567
Mark Brownb22ead22009-06-07 12:51:26 +01001568/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001569static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001570 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001571{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001572 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001573 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001574 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001575 unsigned int value = 0;
1576 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001577
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001578 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1579 reg = w->reg;
1580 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001581
1582 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001583 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001584 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001585
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001586 mask |= w->mask << w->shift;
1587 if (w->power)
1588 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001589 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001590 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001591
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001592 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001593 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1594 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001595
Mark Brown68f89ad2010-11-03 23:51:49 -04001596 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001597 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1598 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001599 }
1600
Mark Brown81628102009-06-07 13:21:24 +01001601 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001602 /* Any widget will do, they should all be updating the
1603 * same register.
1604 */
Mark Brown29376bc2011-06-19 13:49:28 +01001605
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001606 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001607 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001608 value, mask, reg, card->pop_time);
1609 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001610 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001611 }
1612
1613 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001614 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1615 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001616 }
Mark Brown42aa3412009-03-01 19:21:10 +00001617}
1618
Mark Brownb22ead22009-06-07 12:51:26 +01001619/* Apply a DAPM power sequence.
1620 *
1621 * We walk over a pre-sorted list of widgets to apply power to. In
1622 * order to minimise the number of writes to the device required
1623 * multiple widgets will be updated in a single write where possible.
1624 * Currently anything that requires more than a single write is not
1625 * handled.
1626 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001627static void dapm_seq_run(struct snd_soc_card *card,
1628 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001629{
1630 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001631 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001632 LIST_HEAD(pending);
1633 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001634 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001635 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001636 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001637 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001638 int *sort;
1639
1640 if (power_up)
1641 sort = dapm_up_seq;
1642 else
1643 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001644
Mark Brownb22ead22009-06-07 12:51:26 +01001645 list_for_each_entry_safe(w, n, list, power_list) {
1646 ret = 0;
1647
1648 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001649 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001650 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001651 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001652 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001653
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001654 if (cur_dapm && cur_dapm->component) {
Mark Brown474b62d2011-01-18 16:14:44 +00001655 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1656 if (sort[i] == cur_sort)
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001657 snd_soc_component_seq_notifier(
1658 cur_dapm->component,
1659 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001660 }
1661
Mark Browneb270e92013-10-09 13:52:52 +01001662 if (cur_dapm && w->dapm != cur_dapm)
1663 soc_dapm_async_complete(cur_dapm);
1664
Mark Brownb22ead22009-06-07 12:51:26 +01001665 INIT_LIST_HEAD(&pending);
1666 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001667 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001668 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001669 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001670 }
1671
Mark Brown163cac02009-06-07 10:12:52 +01001672 switch (w->id) {
1673 case snd_soc_dapm_pre:
1674 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001675 list_for_each_entry_safe_continue(w, n, list,
1676 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001677
Mark Brownb22ead22009-06-07 12:51:26 +01001678 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001679 ret = w->event(w,
1680 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001681 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001682 ret = w->event(w,
1683 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001684 break;
1685
1686 case snd_soc_dapm_post:
1687 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001688 list_for_each_entry_safe_continue(w, n, list,
1689 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001690
Mark Brownb22ead22009-06-07 12:51:26 +01001691 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001692 ret = w->event(w,
1693 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001694 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001695 ret = w->event(w,
1696 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001697 break;
1698
Mark Brown163cac02009-06-07 10:12:52 +01001699 default:
Mark Brown81628102009-06-07 13:21:24 +01001700 /* Queue it up for application */
1701 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001702 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001703 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001704 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001705 list_move(&w->power_list, &pending);
1706 break;
Mark Brown163cac02009-06-07 10:12:52 +01001707 }
Mark Brownb22ead22009-06-07 12:51:26 +01001708
1709 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001710 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001711 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001712 }
Mark Brownb22ead22009-06-07 12:51:26 +01001713
1714 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001715 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001716
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001717 if (cur_dapm && cur_dapm->component) {
Mark Brown474b62d2011-01-18 16:14:44 +00001718 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1719 if (sort[i] == cur_sort)
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001720 snd_soc_component_seq_notifier(
1721 cur_dapm->component,
1722 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001723 }
Mark Browneb270e92013-10-09 13:52:52 +01001724
Kuninori Morimotodf817f82020-03-09 13:08:16 +09001725 for_each_card_dapms(card, d)
Mark Browneb270e92013-10-09 13:52:52 +01001726 soc_dapm_async_complete(d);
Mark Brown163cac02009-06-07 10:12:52 +01001727}
1728
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001729static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001730{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001731 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001732 struct snd_soc_dapm_widget_list *wlist;
1733 struct snd_soc_dapm_widget *w = NULL;
1734 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001735 int ret;
1736
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001737 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001738 return;
1739
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001740 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001741
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001742 for_each_dapm_widgets(wlist, wi, w) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001743 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1744 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1745 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001746 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001747 w->name, ret);
1748 }
Mark Brown97404f22010-12-14 16:13:57 +00001749 }
1750
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001751 if (!w)
1752 return;
1753
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001754 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1755 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001756 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001757 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001758 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001759
Chen-Yu Tsaie411b0b2016-11-02 15:35:58 +08001760 if (update->has_second_set) {
1761 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1762 update->mask2, update->val2);
1763 if (ret < 0)
1764 dev_err(w->dapm->dev,
1765 "ASoC: %s DAPM update failed: %d\n",
1766 w->name, ret);
1767 }
1768
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001769 for_each_dapm_widgets(wlist, wi, w) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001770 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1771 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1772 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001773 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001774 w->name, ret);
1775 }
Mark Brown97404f22010-12-14 16:13:57 +00001776 }
1777}
1778
Mark Brown9d0624a2011-02-18 11:49:43 -08001779/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1780 * they're changing state.
1781 */
1782static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1783{
1784 struct snd_soc_dapm_context *d = data;
1785 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001786
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001787 /* If we're off and we're not supposed to go into STANDBY */
Mark Brown56fba412011-06-04 11:25:10 +01001788 if (d->bias_level == SND_SOC_BIAS_OFF &&
1789 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001790 if (d->dev)
1791 pm_runtime_get_sync(d->dev);
1792
Mark Brown9d0624a2011-02-18 11:49:43 -08001793 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1794 if (ret != 0)
1795 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001796 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001797 }
1798
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001799 /* Prepare for a transition to ON or away from ON */
1800 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1801 d->bias_level != SND_SOC_BIAS_ON) ||
1802 (d->target_bias_level != SND_SOC_BIAS_ON &&
1803 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001804 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1805 if (ret != 0)
1806 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001807 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001808 }
1809}
1810
1811/* Async callback run prior to DAPM sequences - brings to their final
1812 * state.
1813 */
1814static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1815{
1816 struct snd_soc_dapm_context *d = data;
1817 int ret;
1818
1819 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001820 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1821 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1822 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001823 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1824 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001825 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001826 ret);
1827 }
1828
1829 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001830 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1831 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001832 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1833 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001834 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1835 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001836
1837 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001838 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001839 }
1840
1841 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001842 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1843 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001844 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1845 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001846 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001847 ret);
1848 }
1849}
Mark Brown97404f22010-12-14 16:13:57 +00001850
Mark Brownfe4fda52011-10-03 22:36:57 +01001851static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1852 bool power, bool connect)
1853{
1854 /* If a connection is being made or broken then that update
1855 * will have marked the peer dirty, otherwise the widgets are
1856 * not connected and this update has no impact. */
1857 if (!connect)
1858 return;
1859
1860 /* If the peer is already in the state we're moving to then we
1861 * won't have an impact on it. */
1862 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001863 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001864}
1865
Mark Brown05623c42011-09-28 17:02:31 +01001866static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1867 struct list_head *up_list,
1868 struct list_head *down_list)
1869{
Mark Browndb432b42011-10-03 21:06:40 +01001870 struct snd_soc_dapm_path *path;
1871
Mark Brown05623c42011-09-28 17:02:31 +01001872 if (w->power == power)
1873 return;
1874
1875 trace_snd_soc_dapm_widget_power(w, power);
1876
Mark Browndb432b42011-10-03 21:06:40 +01001877 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001878 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001879 */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001880 snd_soc_dapm_widget_for_each_source_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001881 dapm_widget_set_peer_power(path->source, power, path->connect);
1882
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001883 /* Supplies can't affect their outputs, only their inputs */
1884 if (!w->is_supply) {
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001885 snd_soc_dapm_widget_for_each_sink_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001886 dapm_widget_set_peer_power(path->sink, power,
1887 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001888 }
1889
Mark Brown05623c42011-09-28 17:02:31 +01001890 if (power)
1891 dapm_seq_insert(w, up_list, true);
1892 else
1893 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001894}
1895
Mark Brown7c81beb2011-09-20 22:22:32 +01001896static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1897 struct list_head *up_list,
1898 struct list_head *down_list)
1899{
Mark Brown7c81beb2011-09-20 22:22:32 +01001900 int power;
1901
1902 switch (w->id) {
1903 case snd_soc_dapm_pre:
1904 dapm_seq_insert(w, down_list, false);
1905 break;
1906 case snd_soc_dapm_post:
1907 dapm_seq_insert(w, up_list, true);
1908 break;
1909
1910 default:
Mark Brownd805002b2011-09-28 18:28:23 +01001911 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001912
Mark Brown05623c42011-09-28 17:02:31 +01001913 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001914 break;
1915 }
1916}
1917
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001918static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1919{
1920 if (dapm->idle_bias_off)
1921 return true;
1922
1923 switch (snd_power_get_state(dapm->card->snd_card)) {
1924 case SNDRV_CTL_POWER_D3hot:
1925 case SNDRV_CTL_POWER_D3cold:
1926 return dapm->suspend_bias_off;
1927 default:
1928 break;
1929 }
1930
1931 return false;
1932}
1933
Mark Brown42aa3412009-03-01 19:21:10 +00001934/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001935 * Scan each dapm widget for complete audio path.
1936 * A complete path is a route that has valid endpoints i.e.:-
1937 *
1938 * o DAC to output pin.
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001939 * o Input pin to ADC.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001940 * o Input pin to Output pin (bypass, sidetone)
1941 * o DAC to ADC (loopback).
1942 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001943static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001944{
1945 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001946 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001947 LIST_HEAD(up_list);
1948 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001949 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001950 enum snd_soc_bias_level bias;
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +09001951 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001952
Mark Brownf9fa2b12014-03-06 16:49:11 +08001953 lockdep_assert_held(&card->dapm_mutex);
1954
Mark Brown84e90932010-11-04 00:07:02 -04001955 trace_snd_soc_dapm_start(card);
1956
Kuninori Morimotodf817f82020-03-09 13:08:16 +09001957 for_each_card_dapms(card, d) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001958 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001959 d->target_bias_level = SND_SOC_BIAS_OFF;
1960 else
1961 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001962 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001963
Liam Girdwood6c120e12012-02-15 15:15:34 +00001964 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001965
Mark Brown6d3ddc82009-05-16 17:47:29 +01001966 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001967 * lists indicating if they should be powered up or down. We
1968 * only check widgets that have been flagged as dirty but note
1969 * that new widgets may be added to the dirty list while we
1970 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001971 */
Mark Browndb432b42011-10-03 21:06:40 +01001972 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001973 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001974 }
1975
Kuninori Morimoto14596692020-03-09 13:08:21 +09001976 for_each_card_widgets(card, w) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001977 switch (w->id) {
1978 case snd_soc_dapm_pre:
1979 case snd_soc_dapm_post:
1980 /* These widgets always need to be powered */
1981 break;
1982 default:
1983 list_del_init(&w->dirty);
1984 break;
1985 }
Mark Browndb432b42011-10-03 21:06:40 +01001986
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001987 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001988 d = w->dapm;
1989
1990 /* Supplies and micbiases only bring the
1991 * context up to STANDBY as unless something
1992 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001993 * generally don't require full power. Signal
1994 * generators are virtual pins and have no
1995 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001996 */
1997 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001998 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001999 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00002000 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01002001 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002002 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00002003 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002004 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01002005 case snd_soc_dapm_micbias:
2006 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
2007 d->target_bias_level = SND_SOC_BIAS_STANDBY;
2008 break;
2009 default:
2010 d->target_bias_level = SND_SOC_BIAS_ON;
2011 break;
2012 }
2013 }
2014
2015 }
2016
Mark Brown85a843c2011-09-21 21:29:47 +01002017 /* Force all contexts in the card to the same bias state if
2018 * they're not ground referenced.
2019 */
Mark Brown56fba412011-06-04 11:25:10 +01002020 bias = SND_SOC_BIAS_OFF;
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002021 for_each_card_dapms(card, d)
Mark Brown56fba412011-06-04 11:25:10 +01002022 if (d->target_bias_level > bias)
2023 bias = d->target_bias_level;
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002024 for_each_card_dapms(card, d)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02002025 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01002026 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09002027
Mark Brownde02d072011-09-20 21:43:24 +01002028 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09002029
Xiang Xiao17282ba2014-03-02 00:04:03 +08002030 /* Run card bias changes at first */
2031 dapm_pre_sequence_async(&card->dapm, 0);
2032 /* Run other bias changes in parallel */
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002033 for_each_card_dapms(card, d) {
Jon Huntere03546d2018-08-17 16:35:43 +01002034 if (d != &card->dapm && d->bias_level != d->target_bias_level)
Xiang Xiao17282ba2014-03-02 00:04:03 +08002035 async_schedule_domain(dapm_pre_sequence_async, d,
2036 &async_domain);
2037 }
Mark Brown9d0624a2011-02-18 11:49:43 -08002038 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01002039
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02002040 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002041 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00002042 }
2043
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02002044 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002045 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00002046 }
2047
Mark Brown6d3ddc82009-05-16 17:47:29 +01002048 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002049 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01002050
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002051 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00002052
Mark Brown6d3ddc82009-05-16 17:47:29 +01002053 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002054 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002055
Mark Brown9d0624a2011-02-18 11:49:43 -08002056 /* Run all the bias changes in parallel */
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002057 for_each_card_dapms(card, d) {
Jon Huntere03546d2018-08-17 16:35:43 +01002058 if (d != &card->dapm && d->bias_level != d->target_bias_level)
Xiang Xiao17282ba2014-03-02 00:04:03 +08002059 async_schedule_domain(dapm_post_sequence_async, d,
2060 &async_domain);
2061 }
Mark Brown9d0624a2011-02-18 11:49:43 -08002062 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08002063 /* Run card bias changes at last */
2064 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01002065
Liam Girdwood8078d872012-02-15 15:15:35 +00002066 /* do we need to notify any clients that DAPM event is complete */
Kuninori Morimotodf817f82020-03-09 13:08:16 +09002067 for_each_card_dapms(card, d) {
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +09002068 if (!d->component)
2069 continue;
2070
2071 ret = snd_soc_component_stream_event(d->component, event);
2072 if (ret < 0)
2073 return ret;
Liam Girdwood8078d872012-02-15 15:15:35 +00002074 }
2075
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002076 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02002077 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02002078 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01002079
Mark Brown84e90932010-11-04 00:07:02 -04002080 trace_snd_soc_dapm_done(card);
2081
Mark Brown42aa3412009-03-01 19:21:10 +00002082 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002083}
2084
Mark Brown79fb9382009-08-21 16:38:13 +01002085#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01002086static ssize_t dapm_widget_power_read_file(struct file *file,
2087 char __user *user_buf,
2088 size_t count, loff_t *ppos)
2089{
2090 struct snd_soc_dapm_widget *w = file->private_data;
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002091 struct snd_soc_card *card = w->dapm->card;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002092 enum snd_soc_dapm_direction dir, rdir;
Mark Brown79fb9382009-08-21 16:38:13 +01002093 char *buf;
2094 int in, out;
2095 ssize_t ret;
2096 struct snd_soc_dapm_path *p = NULL;
2097
2098 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2099 if (!buf)
2100 return -ENOMEM;
2101
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002102 mutex_lock(&card->dapm_mutex);
2103
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002104 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2105 if (w->is_supply) {
2106 in = 0;
2107 out = 0;
2108 } else {
Piotr Stankiewicz67420642016-05-13 17:03:55 +01002109 in = is_connected_input_ep(w, NULL, NULL);
2110 out = is_connected_output_ep(w, NULL, NULL);
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002111 }
Mark Brown79fb9382009-08-21 16:38:13 +01002112
Silvio Cesaree581e152019-01-12 16:28:43 +01002113 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
Mark Brownf13ebad2012-03-03 18:01:01 +00002114 w->name, w->power ? "On" : "Off",
2115 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01002116
Mark Brownd033c362009-12-04 15:25:56 +00002117 if (w->reg >= 0)
Silvio Cesaree581e152019-01-12 16:28:43 +01002118 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002119 " - R%d(0x%x) mask 0x%x",
2120 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00002121
Silvio Cesaree581e152019-01-12 16:28:43 +01002122 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
Mark Brownd033c362009-12-04 15:25:56 +00002123
Mark Brown3eef08b2009-09-14 16:49:00 +01002124 if (w->sname)
Silvio Cesaree581e152019-01-12 16:28:43 +01002125 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
Mark Brown3eef08b2009-09-14 16:49:00 +01002126 w->sname,
2127 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01002128
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002129 snd_soc_dapm_for_each_direction(dir) {
2130 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2131 snd_soc_dapm_widget_for_each_path(w, dir, p) {
KaiChieh Chuang28735af2018-02-05 13:00:00 +08002132 if (p->connected && !p->connected(p->source, p->sink))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002133 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002134
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002135 if (!p->connect)
2136 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002137
Silvio Cesaree581e152019-01-12 16:28:43 +01002138 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002139 " %s \"%s\" \"%s\"\n",
2140 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
Mark Brown79fb9382009-08-21 16:38:13 +01002141 p->name ? p->name : "static",
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002142 p->node[rdir]->name);
2143 }
Mark Brown79fb9382009-08-21 16:38:13 +01002144 }
2145
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002146 mutex_unlock(&card->dapm_mutex);
2147
Mark Brown79fb9382009-08-21 16:38:13 +01002148 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2149
2150 kfree(buf);
2151 return ret;
2152}
2153
2154static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002155 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002156 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002157 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002158};
2159
Mark Brownef49e4f2011-04-04 20:48:13 +09002160static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2161 size_t count, loff_t *ppos)
2162{
2163 struct snd_soc_dapm_context *dapm = file->private_data;
2164 char *level;
2165
2166 switch (dapm->bias_level) {
2167 case SND_SOC_BIAS_ON:
2168 level = "On\n";
2169 break;
2170 case SND_SOC_BIAS_PREPARE:
2171 level = "Prepare\n";
2172 break;
2173 case SND_SOC_BIAS_STANDBY:
2174 level = "Standby\n";
2175 break;
2176 case SND_SOC_BIAS_OFF:
2177 level = "Off\n";
2178 break;
2179 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002180 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002181 level = "Unknown\n";
2182 break;
2183 }
2184
2185 return simple_read_from_buffer(user_buf, count, ppos, level,
2186 strlen(level));
2187}
2188
2189static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002190 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002191 .read = dapm_bias_read_file,
2192 .llseek = default_llseek,
2193};
2194
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002195void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2196 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002197{
Mark Brownceaea852019-06-21 12:33:57 +01002198 if (!parent || IS_ERR(parent))
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02002199 return;
2200
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002201 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2202
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +02002203 debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
2204 &dapm_bias_fops);
Mark Brown79fb9382009-08-21 16:38:13 +01002205}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002206
2207static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2208{
2209 struct snd_soc_dapm_context *dapm = w->dapm;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002210
2211 if (!dapm->debugfs_dapm || !w->name)
2212 return;
2213
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +02002214 debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
2215 &dapm_widget_power_fops);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002216}
2217
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002218static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2219{
2220 debugfs_remove_recursive(dapm->debugfs_dapm);
Kuninori Morimoto29040d12019-05-27 16:51:34 +09002221 dapm->debugfs_dapm = NULL;
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002222}
2223
Mark Brown79fb9382009-08-21 16:38:13 +01002224#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002225void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2226 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002227{
2228}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002229
2230static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2231{
2232}
2233
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002234static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2235{
2236}
2237
Mark Brown79fb9382009-08-21 16:38:13 +01002238#endif
2239
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002240/*
2241 * soc_dapm_connect_path() - Connects or disconnects a path
2242 * @path: The path to update
2243 * @connect: The new connect state of the path. True if the path is connected,
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002244 * false if it is disconnected.
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002245 * @reason: The reason why the path changed (for debugging only)
2246 */
2247static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2248 bool connect, const char *reason)
2249{
2250 if (path->connect == connect)
2251 return;
2252
2253 path->connect = connect;
2254 dapm_mark_dirty(path->source, reason);
2255 dapm_mark_dirty(path->sink, reason);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002256 dapm_path_invalidate(path);
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002257}
2258
Richard Purdie2b97eab2006-10-06 18:32:18 +02002259/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002260static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002261 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002262{
2263 struct snd_soc_dapm_path *path;
2264 int found = 0;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002265 bool connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002266
Mark Brownf9fa2b12014-03-06 16:49:11 +08002267 lockdep_assert_held(&card->dapm_mutex);
2268
Richard Purdie2b97eab2006-10-06 18:32:18 +02002269 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002270 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002271 found = 1;
2272 /* we now need to match the string in the enum to the path */
Jerome Brunetc3456a42019-05-15 15:18:55 +02002273 if (e && !(strcmp(path->name, e->texts[mux])))
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002274 connect = true;
2275 else
2276 connect = false;
2277
2278 soc_dapm_connect_path(path, connect, "mux update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002279 }
2280
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002281 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002282 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002283
Liam Girdwood618dae12012-04-25 12:12:51 +01002284 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002285}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002286
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002287int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002288 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2289 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002290{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002291 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002292 int ret;
2293
Liam Girdwood3cd04342012-03-09 12:02:08 +00002294 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002295 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002296 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002297 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002298 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002299 if (ret > 0)
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01002300 snd_soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002301 return ret;
2302}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002303EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002304
Milan plzik1b075e32008-01-10 14:39:46 +01002305/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002306static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002307 struct snd_kcontrol *kcontrol,
2308 int connect, int rconnect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002309{
2310 struct snd_soc_dapm_path *path;
2311 int found = 0;
2312
Mark Brownf9fa2b12014-03-06 16:49:11 +08002313 lockdep_assert_held(&card->dapm_mutex);
2314
Richard Purdie2b97eab2006-10-06 18:32:18 +02002315 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002316 dapm_kcontrol_for_each_path(path, kcontrol) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002317 /*
2318 * Ideally this function should support any number of
2319 * paths and channels. But since kcontrols only come
2320 * in mono and stereo variants, we are limited to 2
2321 * channels.
2322 *
2323 * The following code assumes for stereo controls the
2324 * first path (when 'found == 0') is the left channel,
2325 * and all remaining paths (when 'found == 1') are the
2326 * right channel.
2327 *
2328 * A stereo control is signified by a valid 'rconnect'
2329 * value, either 0 for unconnected, or >= 0 for connected.
2330 * This is chosen instead of using snd_soc_volsw_is_stereo,
2331 * so that the behavior of snd_soc_dapm_mixer_update_power
2332 * doesn't change even when the kcontrol passed in is
2333 * stereo.
2334 *
2335 * It passes 'connect' as the path connect status for
2336 * the left channel, and 'rconnect' for the right
2337 * channel.
2338 */
2339 if (found && rconnect >= 0)
2340 soc_dapm_connect_path(path, rconnect, "mixer update");
2341 else
2342 soc_dapm_connect_path(path, connect, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002343 found = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002344 }
2345
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002346 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002347 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002348
Liam Girdwood618dae12012-04-25 12:12:51 +01002349 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002350}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002351
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002352int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002353 struct snd_kcontrol *kcontrol, int connect,
2354 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002355{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002356 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002357 int ret;
2358
Liam Girdwood3cd04342012-03-09 12:02:08 +00002359 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002360 card->update = update;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002361 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002362 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002363 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002364 if (ret > 0)
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01002365 snd_soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002366 return ret;
2367}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002368EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002369
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002370static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2371 char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002372{
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002373 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002374 struct snd_soc_dapm_widget *w;
2375 int count = 0;
2376 char *state = "not set";
2377
Mark Brown47325072016-03-18 12:04:23 +00002378 /* card won't be set for the dummy component, as a spot fix
2379 * we're checking for that case specifically here but in future
2380 * we will ensure that the dummy component looks like others.
2381 */
2382 if (!cmpnt->card)
2383 return 0;
2384
Kuninori Morimoto14596692020-03-09 13:08:21 +09002385 for_each_card_widgets(cmpnt->card, w) {
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002386 if (w->dapm != dapm)
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002387 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002388
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002389 /* only display widgets that burn power */
Richard Purdie2b97eab2006-10-06 18:32:18 +02002390 switch (w->id) {
2391 case snd_soc_dapm_hp:
2392 case snd_soc_dapm_mic:
2393 case snd_soc_dapm_spk:
2394 case snd_soc_dapm_line:
2395 case snd_soc_dapm_micbias:
2396 case snd_soc_dapm_dac:
2397 case snd_soc_dapm_adc:
2398 case snd_soc_dapm_pga:
Seppo Ingalsuo882c8b4a2019-04-30 18:11:34 -05002399 case snd_soc_dapm_effect:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002400 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002401 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002402 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002403 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002404 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00002405 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002406 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002407 if (w->name)
2408 count += sprintf(buf + count, "%s: %s\n",
2409 w->name, w->power ? "On":"Off");
2410 break;
2411 default:
2412 break;
2413 }
2414 }
2415
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002416 switch (snd_soc_dapm_get_bias_level(dapm)) {
Mark Brown0be98982008-05-19 12:31:28 +02002417 case SND_SOC_BIAS_ON:
2418 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002419 break;
Mark Brown0be98982008-05-19 12:31:28 +02002420 case SND_SOC_BIAS_PREPARE:
2421 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002422 break;
Mark Brown0be98982008-05-19 12:31:28 +02002423 case SND_SOC_BIAS_STANDBY:
2424 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002425 break;
Mark Brown0be98982008-05-19 12:31:28 +02002426 case SND_SOC_BIAS_OFF:
2427 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002428 break;
2429 }
2430 count += sprintf(buf + count, "PM State: %s\n", state);
2431
2432 return count;
2433}
2434
Benoit Cousson44ba2642014-07-08 23:19:36 +02002435/* show dapm widget status in sys fs */
2436static ssize_t dapm_widget_show(struct device *dev,
2437 struct device_attribute *attr, char *buf)
2438{
2439 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002440 struct snd_soc_dai *codec_dai;
Benoit Cousson44ba2642014-07-08 23:19:36 +02002441 int i, count = 0;
2442
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002443 mutex_lock(&rtd->card->dapm_mutex);
2444
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09002445 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002446 struct snd_soc_component *cmpnt = codec_dai->component;
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002447
2448 count += dapm_widget_show_component(cmpnt, buf + count);
Benoit Cousson44ba2642014-07-08 23:19:36 +02002449 }
2450
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002451 mutex_unlock(&rtd->card->dapm_mutex);
2452
Benoit Cousson44ba2642014-07-08 23:19:36 +02002453 return count;
2454}
2455
Joe Perchesc828a892017-12-19 10:15:08 -08002456static DEVICE_ATTR_RO(dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002457
Takashi Iwaid29697d2015-01-30 20:16:37 +01002458struct attribute *soc_dapm_dev_attrs[] = {
2459 &dev_attr_dapm_widget.attr,
2460 NULL
2461};
Richard Purdie2b97eab2006-10-06 18:32:18 +02002462
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002463static void dapm_free_path(struct snd_soc_dapm_path *path)
2464{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002465 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2466 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002467 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002468 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002469 kfree(path);
2470}
2471
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002472void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2473{
2474 struct snd_soc_dapm_path *p, *next_p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002475 enum snd_soc_dapm_direction dir;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002476
2477 list_del(&w->list);
2478 /*
2479 * remove source and sink paths associated to this widget.
2480 * While removing the path, remove reference to it from both
2481 * source and sink widgets so that path is removed only once.
2482 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002483 snd_soc_dapm_for_each_direction(dir) {
2484 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2485 dapm_free_path(p);
2486 }
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002487
2488 kfree(w->kcontrols);
Lars-Peter Clausen48068962015-07-21 18:11:08 +02002489 kfree_const(w->name);
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -06002490 kfree_const(w->sname);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002491 kfree(w);
2492}
2493
Jyri Sarhafd589a12015-11-10 18:12:42 +02002494void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2495{
2496 dapm->path_sink_cache.widget = NULL;
2497 dapm->path_source_cache.widget = NULL;
2498}
2499
Richard Purdie2b97eab2006-10-06 18:32:18 +02002500/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002501static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002502{
2503 struct snd_soc_dapm_widget *w, *next_w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002504
Kuninori Morimoto14596692020-03-09 13:08:21 +09002505 for_each_card_widgets_safe(dapm->card, w, next_w) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002506 if (w->dapm != dapm)
2507 continue;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002508 snd_soc_dapm_free_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002509 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002510 snd_soc_dapm_reset_cache(dapm);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002511}
2512
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002513static struct snd_soc_dapm_widget *dapm_find_widget(
2514 struct snd_soc_dapm_context *dapm, const char *pin,
2515 bool search_other_contexts)
2516{
2517 struct snd_soc_dapm_widget *w;
2518 struct snd_soc_dapm_widget *fallback = NULL;
2519
Kuninori Morimoto14596692020-03-09 13:08:21 +09002520 for_each_card_widgets(dapm->card, w) {
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002521 if (!strcmp(w->name, pin)) {
2522 if (w->dapm == dapm)
2523 return w;
2524 else
2525 fallback = w;
2526 }
2527 }
2528
2529 if (search_other_contexts)
2530 return fallback;
2531
2532 return NULL;
2533}
2534
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002535static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002536 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002537{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002538 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002539
Mark Brownf9fa2b12014-03-06 16:49:11 +08002540 dapm_assert_locked(dapm);
2541
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002542 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002543 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002544 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002545 }
2546
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002547 if (w->connected != status) {
Mark Brown1a8b2d92012-02-16 11:50:07 -08002548 dapm_mark_dirty(w, "pin configuration");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002549 dapm_widget_invalidate_input_paths(w);
2550 dapm_widget_invalidate_output_paths(w);
2551 }
Mark Brown1a8b2d92012-02-16 11:50:07 -08002552
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002553 w->connected = status;
2554 if (status == 0)
2555 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002556
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002557 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002558}
2559
Richard Purdie2b97eab2006-10-06 18:32:18 +02002560/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002561 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2562 * @dapm: DAPM context
2563 *
2564 * Walks all dapm audio paths and powers widgets according to their
2565 * stream or path usage.
2566 *
2567 * Requires external locking.
2568 *
2569 * Returns 0 for success.
2570 */
2571int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2572{
2573 /*
2574 * Suppress early reports (eg, jacks syncing their state) to avoid
2575 * silly DAPM runs during card startup.
2576 */
2577 if (!dapm->card || !dapm->card->instantiated)
2578 return 0;
2579
2580 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2581}
2582EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2583
2584/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002585 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002586 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002587 *
2588 * Walks all dapm audio paths and powers widgets according to their
2589 * stream or path usage.
2590 *
2591 * Returns 0 for success.
2592 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002593int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002594{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002595 int ret;
2596
Liam Girdwood3cd04342012-03-09 12:02:08 +00002597 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002598 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002599 mutex_unlock(&dapm->card->dapm_mutex);
2600 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002601}
Liam Girdwooda5302182008-07-07 13:35:17 +01002602EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002603
Charles Keepax078a85f2019-01-31 13:30:18 +00002604static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2605 struct snd_soc_dapm_widget *w,
2606 int channels)
2607{
2608 switch (w->id) {
2609 case snd_soc_dapm_aif_out:
2610 case snd_soc_dapm_aif_in:
2611 break;
2612 default:
2613 return 0;
2614 }
2615
2616 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2617 w->channel < channels ? "Connecting" : "Disconnecting",
2618 p->source->name, p->sink->name);
2619
2620 if (w->channel < channels)
2621 soc_dapm_connect_path(p, true, "dai update");
2622 else
2623 soc_dapm_connect_path(p, false, "dai update");
2624
2625 return 0;
2626}
2627
2628static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2629 struct snd_pcm_hw_params *params,
2630 struct snd_soc_dai *dai)
2631{
2632 int dir = substream->stream;
2633 int channels = params_channels(params);
2634 struct snd_soc_dapm_path *p;
2635 struct snd_soc_dapm_widget *w;
2636 int ret;
2637
Kuninori Morimoto0c01f6c2020-02-19 15:56:41 +09002638 w = snd_soc_dai_get_widget(dai, dir);
Charles Keepax078a85f2019-01-31 13:30:18 +00002639
Charles Keepaxcf17a5f2019-02-06 11:13:59 +00002640 if (!w)
2641 return 0;
2642
Charles Keepax078a85f2019-01-31 13:30:18 +00002643 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2644 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2645
2646 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2647 ret = dapm_update_dai_chan(p, p->sink, channels);
2648 if (ret < 0)
2649 return ret;
2650 }
2651
2652 snd_soc_dapm_widget_for_each_source_path(w, p) {
2653 ret = dapm_update_dai_chan(p, p->source, channels);
2654 if (ret < 0)
2655 return ret;
2656 }
2657
2658 return 0;
2659}
2660
2661int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2662 struct snd_pcm_hw_params *params,
2663 struct snd_soc_dai *dai)
2664{
2665 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2666 int ret;
2667
2668 mutex_lock_nested(&rtd->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2669 ret = dapm_update_dai_unlocked(substream, params, dai);
2670 mutex_unlock(&rtd->card->dapm_mutex);
2671
2672 return ret;
2673}
2674EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2675
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002676/*
2677 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2678 * @w: The widget for which to update the flags
2679 *
2680 * Some widgets have a dynamic category which depends on which neighbors they
2681 * are connected to. This function update the category for these widgets.
2682 *
2683 * This function must be called whenever a path is added or removed to a widget.
2684 */
2685static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2686{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002687 enum snd_soc_dapm_direction dir;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002688 struct snd_soc_dapm_path *p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002689 unsigned int ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002690
2691 switch (w->id) {
2692 case snd_soc_dapm_input:
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002693 /* On a fully routed card an input is never a source */
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002694 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002695 return;
2696 ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002697 snd_soc_dapm_widget_for_each_source_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002698 if (p->source->id == snd_soc_dapm_micbias ||
2699 p->source->id == snd_soc_dapm_mic ||
2700 p->source->id == snd_soc_dapm_line ||
2701 p->source->id == snd_soc_dapm_output) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002702 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002703 break;
2704 }
2705 }
2706 break;
2707 case snd_soc_dapm_output:
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002708 /* On a fully routed card a output is never a sink */
2709 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002710 return;
2711 ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002712 snd_soc_dapm_widget_for_each_sink_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002713 if (p->sink->id == snd_soc_dapm_spk ||
2714 p->sink->id == snd_soc_dapm_hp ||
2715 p->sink->id == snd_soc_dapm_line ||
2716 p->sink->id == snd_soc_dapm_input) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002717 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002718 break;
2719 }
2720 }
2721 break;
2722 case snd_soc_dapm_line:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002723 ep = 0;
2724 snd_soc_dapm_for_each_direction(dir) {
2725 if (!list_empty(&w->edges[dir]))
2726 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2727 }
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002728 break;
2729 default:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002730 return;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002731 }
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002732
2733 w->is_ep = ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002734}
2735
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002736static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2737 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2738 const char *control)
2739{
2740 bool dynamic_source = false;
2741 bool dynamic_sink = false;
2742
2743 if (!control)
2744 return 0;
2745
2746 switch (source->id) {
2747 case snd_soc_dapm_demux:
2748 dynamic_source = true;
2749 break;
2750 default:
2751 break;
2752 }
2753
2754 switch (sink->id) {
2755 case snd_soc_dapm_mux:
2756 case snd_soc_dapm_switch:
2757 case snd_soc_dapm_mixer:
2758 case snd_soc_dapm_mixer_named_ctl:
2759 dynamic_sink = true;
2760 break;
2761 default:
2762 break;
2763 }
2764
2765 if (dynamic_source && dynamic_sink) {
2766 dev_err(dapm->dev,
2767 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2768 source->name, control, sink->name);
2769 return -EINVAL;
2770 } else if (!dynamic_source && !dynamic_sink) {
2771 dev_err(dapm->dev,
2772 "Control not supported for path %s -> [%s] -> %s\n",
2773 source->name, control, sink->name);
2774 return -EINVAL;
2775 }
2776
2777 return 0;
2778}
2779
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002780static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2781 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2782 const char *control,
2783 int (*connected)(struct snd_soc_dapm_widget *source,
2784 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002785{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002786 struct snd_soc_dapm_widget *widgets[2];
2787 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002788 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002789 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002790
Lars-Peter Clausene409dfb2014-10-25 17:42:02 +02002791 if (wsink->is_supply && !wsource->is_supply) {
2792 dev_err(dapm->dev,
2793 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2794 wsource->name, wsink->name);
2795 return -EINVAL;
2796 }
2797
2798 if (connected && !wsource->is_supply) {
2799 dev_err(dapm->dev,
2800 "connected() callback only supported for supply widgets (%s -> %s)\n",
2801 wsource->name, wsink->name);
2802 return -EINVAL;
2803 }
2804
2805 if (wsource->is_supply && control) {
2806 dev_err(dapm->dev,
2807 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2808 wsource->name, control, wsink->name);
2809 return -EINVAL;
2810 }
2811
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002812 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2813 if (ret)
2814 return ret;
2815
Richard Purdie2b97eab2006-10-06 18:32:18 +02002816 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2817 if (!path)
2818 return -ENOMEM;
2819
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002820 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2821 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2822 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2823 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2824
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002825 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002826 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002827 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002828
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002829 if (wsource->is_supply || wsink->is_supply)
2830 path->is_supply = 1;
2831
Richard Purdie2b97eab2006-10-06 18:32:18 +02002832 /* connect static paths */
2833 if (control == NULL) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002834 path->connect = 1;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002835 } else {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002836 switch (wsource->id) {
2837 case snd_soc_dapm_demux:
2838 ret = dapm_connect_mux(dapm, path, control, wsource);
2839 if (ret)
2840 goto err;
2841 break;
2842 default:
2843 break;
2844 }
2845
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002846 switch (wsink->id) {
2847 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002848 ret = dapm_connect_mux(dapm, path, control, wsink);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002849 if (ret != 0)
2850 goto err;
2851 break;
2852 case snd_soc_dapm_switch:
2853 case snd_soc_dapm_mixer:
2854 case snd_soc_dapm_mixer_named_ctl:
2855 ret = dapm_connect_mixer(dapm, path, control);
2856 if (ret != 0)
2857 goto err;
2858 break;
2859 default:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002860 break;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002861 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002862 }
2863
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002864 list_add(&path->list, &dapm->card->paths);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002865 snd_soc_dapm_for_each_direction(dir)
2866 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002867
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002868 snd_soc_dapm_for_each_direction(dir) {
2869 dapm_update_widget_flags(widgets[dir]);
2870 dapm_mark_dirty(widgets[dir], "Route added");
2871 }
Mark Brownfabd0382012-07-05 17:20:06 +01002872
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002873 if (dapm->card->instantiated && path->connect)
2874 dapm_path_invalidate(path);
2875
Richard Purdie2b97eab2006-10-06 18:32:18 +02002876 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002877err:
2878 kfree(path);
2879 return ret;
2880}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002881
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002882static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002883 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002884{
2885 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2886 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2887 const char *sink;
2888 const char *source;
2889 char prefixed_sink[80];
2890 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002891 const char *prefix;
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002892 unsigned int sink_ref = 0;
2893 unsigned int source_ref = 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002894 int ret;
2895
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002896 prefix = soc_dapm_prefix(dapm);
2897 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002898 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002899 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002900 sink = prefixed_sink;
2901 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002902 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002903 source = prefixed_source;
2904 } else {
2905 sink = route->sink;
2906 source = route->source;
2907 }
2908
Charles Keepax45a110a2015-05-11 13:50:30 +01002909 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2910 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2911
2912 if (wsink && wsource)
2913 goto skip_search;
2914
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002915 /*
2916 * find src and dest widgets over all widgets but favor a widget from
2917 * current DAPM context
2918 */
Kuninori Morimoto14596692020-03-09 13:08:21 +09002919 for_each_card_widgets(dapm->card, w) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002920 if (!wsink && !(strcmp(w->name, sink))) {
2921 wtsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002922 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002923 wsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002924 if (wsource)
2925 break;
2926 }
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002927 sink_ref++;
2928 if (sink_ref > 1)
2929 dev_warn(dapm->dev,
2930 "ASoC: sink widget %s overwritten\n",
2931 w->name);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002932 continue;
2933 }
2934 if (!wsource && !(strcmp(w->name, source))) {
2935 wtsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002936 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002937 wsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002938 if (wsink)
2939 break;
2940 }
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002941 source_ref++;
2942 if (source_ref > 1)
2943 dev_warn(dapm->dev,
2944 "ASoC: source widget %s overwritten\n",
2945 w->name);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002946 }
2947 }
2948 /* use widget from another DAPM context if not found from this */
2949 if (!wsink)
2950 wsink = wtsink;
2951 if (!wsource)
2952 wsource = wtsource;
2953
2954 if (wsource == NULL) {
2955 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2956 route->source);
2957 return -ENODEV;
2958 }
2959 if (wsink == NULL) {
2960 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2961 route->sink);
2962 return -ENODEV;
2963 }
2964
Charles Keepax45a110a2015-05-11 13:50:30 +01002965skip_search:
2966 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2967 dapm_wcache_update(&dapm->path_source_cache, wsource);
2968
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002969 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2970 route->connected);
2971 if (ret)
2972 goto err;
2973
2974 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002975err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002976 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002977 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002978 return ret;
2979}
Mark Brown105f1c22008-05-13 14:52:19 +02002980
Mark Brownefcc3c62012-07-05 17:24:19 +01002981static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2982 const struct snd_soc_dapm_route *route)
2983{
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002984 struct snd_soc_dapm_widget *wsource, *wsink;
Mark Brownefcc3c62012-07-05 17:24:19 +01002985 struct snd_soc_dapm_path *path, *p;
2986 const char *sink;
2987 const char *source;
2988 char prefixed_sink[80];
2989 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002990 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002991
2992 if (route->control) {
2993 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002994 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002995 return -EINVAL;
2996 }
2997
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002998 prefix = soc_dapm_prefix(dapm);
2999 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01003000 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003001 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01003002 sink = prefixed_sink;
3003 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003004 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01003005 source = prefixed_source;
3006 } else {
3007 sink = route->sink;
3008 source = route->source;
3009 }
3010
3011 path = NULL;
3012 list_for_each_entry(p, &dapm->card->paths, list) {
3013 if (strcmp(p->source->name, source) != 0)
3014 continue;
3015 if (strcmp(p->sink->name, sink) != 0)
3016 continue;
3017 path = p;
3018 break;
3019 }
3020
3021 if (path) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003022 wsource = path->source;
3023 wsink = path->sink;
3024
3025 dapm_mark_dirty(wsource, "Route removed");
3026 dapm_mark_dirty(wsink, "Route removed");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003027 if (path->connect)
3028 dapm_path_invalidate(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01003029
Lars-Peter Clausen88722932013-06-14 13:16:53 +02003030 dapm_free_path(path);
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003031
3032 /* Update any path related flags */
3033 dapm_update_widget_flags(wsource);
3034 dapm_update_widget_flags(wsink);
Mark Brownefcc3c62012-07-05 17:24:19 +01003035 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003036 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01003037 source, sink);
3038 }
3039
3040 return 0;
3041}
3042
Mark Brown105f1c22008-05-13 14:52:19 +02003043/**
Mark Brown105f1c22008-05-13 14:52:19 +02003044 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003045 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02003046 * @route: audio routes
3047 * @num: number of routes
3048 *
3049 * Connects 2 dapm widgets together via a named audio path. The sink is
3050 * the widget receiving the audio signal, whilst the source is the sender
3051 * of the audio signal.
3052 *
3053 * Returns 0 for success else error. On error all resources can be freed
3054 * with a call to snd_soc_card_free().
3055 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003056int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02003057 const struct snd_soc_dapm_route *route, int num)
3058{
Mark Brown62d4a4b2012-06-22 12:21:49 +01003059 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02003060
Stuart Hendersonf19c1812017-09-21 11:02:12 +01003061 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown105f1c22008-05-13 14:52:19 +02003062 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02003063 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01003064 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003065 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
3066 route->source,
3067 route->control ? route->control : "direct",
3068 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01003069 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02003070 }
3071 route++;
3072 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003073 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02003074
Dan Carpenter60884c22012-04-13 22:25:43 +03003075 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02003076}
3077EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3078
Mark Brownefcc3c62012-07-05 17:24:19 +01003079/**
3080 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3081 * @dapm: DAPM context
3082 * @route: audio routes
3083 * @num: number of routes
3084 *
3085 * Removes routes from the DAPM context.
3086 */
3087int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3088 const struct snd_soc_dapm_route *route, int num)
3089{
Rajan Vajae066ea22016-02-11 11:23:35 +05303090 int i;
Mark Brownefcc3c62012-07-05 17:24:19 +01003091
Stuart Hendersonf19c1812017-09-21 11:02:12 +01003092 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownefcc3c62012-07-05 17:24:19 +01003093 for (i = 0; i < num; i++) {
3094 snd_soc_dapm_del_route(dapm, route);
3095 route++;
3096 }
3097 mutex_unlock(&dapm->card->dapm_mutex);
3098
Rajan Vajae066ea22016-02-11 11:23:35 +05303099 return 0;
Mark Brownefcc3c62012-07-05 17:24:19 +01003100}
3101EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3102
Mark Brownbf3a9e12011-06-13 16:42:29 +01003103static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3104 const struct snd_soc_dapm_route *route)
3105{
3106 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3107 route->source,
3108 true);
3109 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3110 route->sink,
3111 true);
3112 struct snd_soc_dapm_path *path;
3113 int count = 0;
3114
3115 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003116 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003117 route->source);
3118 return -ENODEV;
3119 }
3120
3121 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003122 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003123 route->sink);
3124 return -ENODEV;
3125 }
3126
3127 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003128 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003129 route->source, route->sink);
3130
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02003131 snd_soc_dapm_widget_for_each_sink_path(source, path) {
Mark Brownbf3a9e12011-06-13 16:42:29 +01003132 if (path->sink == sink) {
3133 path->weak = 1;
3134 count++;
3135 }
3136 }
3137
3138 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003139 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003140 route->source, route->sink);
3141 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003142 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003143 count, route->source, route->sink);
3144
3145 return 0;
3146}
3147
3148/**
3149 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3150 * @dapm: DAPM context
3151 * @route: audio routes
3152 * @num: number of routes
3153 *
3154 * Mark existing routes matching those specified in the passed array
3155 * as being weak, meaning that they are ignored for the purpose of
3156 * power decisions. The main intended use case is for sidetone paths
3157 * which couple audio between other independent paths if they are both
3158 * active in order to make the combination work better at the user
3159 * level but which aren't intended to be "used".
3160 *
3161 * Note that CODEC drivers should not use this as sidetone type paths
3162 * can frequently also be used as bypass paths.
3163 */
3164int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3165 const struct snd_soc_dapm_route *route, int num)
3166{
3167 int i, err;
3168 int ret = 0;
3169
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003170 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01003171 for (i = 0; i < num; i++) {
3172 err = snd_soc_dapm_weak_route(dapm, route);
3173 if (err)
3174 ret = err;
3175 route++;
3176 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003177 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01003178
3179 return ret;
3180}
3181EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3182
Mark Brown105f1c22008-05-13 14:52:19 +02003183/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003184 * snd_soc_dapm_new_widgets - add new dapm widgets
Jonathan Corbet628536e2015-08-25 01:14:48 -06003185 * @card: card to be checked for new dapm widgets
Richard Purdie2b97eab2006-10-06 18:32:18 +02003186 *
3187 * Checks the codec for any new dapm widgets and creates them if found.
3188 *
3189 * Returns 0 for success.
3190 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02003191int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003192{
3193 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00003194 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003195
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003196 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003197
Kuninori Morimoto14596692020-03-09 13:08:21 +09003198 for_each_card_widgets(card, w)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003199 {
3200 if (w->new)
3201 continue;
3202
Stephen Warrenfad59882011-04-28 17:37:59 -06003203 if (w->num_kcontrols) {
Kees Cook6396bb22018-06-12 14:03:40 -07003204 w->kcontrols = kcalloc(w->num_kcontrols,
Stephen Warrenfad59882011-04-28 17:37:59 -06003205 sizeof(struct snd_kcontrol *),
3206 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003207 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003208 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06003209 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003210 }
Stephen Warrenfad59882011-04-28 17:37:59 -06003211 }
3212
Richard Purdie2b97eab2006-10-06 18:32:18 +02003213 switch(w->id) {
3214 case snd_soc_dapm_switch:
3215 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00003216 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003217 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003218 break;
3219 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003220 case snd_soc_dapm_demux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003221 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003222 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003223 case snd_soc_dapm_pga:
Seppo Ingalsuo882c8b4a2019-04-30 18:11:34 -05003224 case snd_soc_dapm_effect:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06003225 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003226 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003227 break;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003228 case snd_soc_dapm_dai_link:
3229 dapm_new_dai_link(w);
3230 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01003231 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003232 break;
3233 }
Mark Brownb66a70d2011-02-09 18:04:11 +00003234
3235 /* Read the initial power state from the device */
3236 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003237 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08003238 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003239 val &= w->mask;
3240 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00003241 w->power = 1;
3242 }
3243
Richard Purdie2b97eab2006-10-06 18:32:18 +02003244 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003245
Mark Brown7508b122011-10-05 12:09:12 +01003246 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003247 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003248 }
3249
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003250 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3251 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003252 return 0;
3253}
3254EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3255
3256/**
3257 * snd_soc_dapm_get_volsw - dapm mixer get callback
3258 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003259 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003260 *
3261 * Callback to get the value of a dapm mixer control.
3262 *
3263 * Returns 0 for success.
3264 */
3265int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3266 struct snd_ctl_elem_value *ucontrol)
3267{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003268 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3269 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003270 struct soc_mixer_control *mc =
3271 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003272 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003273 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003274 int max = mc->max;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003275 unsigned int width = fls(max);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003276 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003277 unsigned int invert = mc->invert;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003278 unsigned int reg_val, val, rval = 0;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003279 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003280
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003281 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003282 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003283 ret = soc_dapm_read(dapm, reg, &reg_val);
3284 val = (reg_val >> shift) & mask;
3285
3286 if (ret == 0 && reg != mc->rreg)
3287 ret = soc_dapm_read(dapm, mc->rreg, &reg_val);
3288
3289 if (snd_soc_volsw_is_stereo(mc))
3290 rval = (reg_val >> mc->rshift) & mask;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003291 } else {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003292 reg_val = dapm_kcontrol_get_value(kcontrol);
3293 val = reg_val & mask;
3294
3295 if (snd_soc_volsw_is_stereo(mc))
3296 rval = (reg_val >> width) & mask;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003297 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003298 mutex_unlock(&card->dapm_mutex);
3299
Chen-Yu Tsai01ad5e72016-08-27 19:27:58 +08003300 if (ret)
3301 return ret;
3302
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003303 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003304 ucontrol->value.integer.value[0] = max - val;
3305 else
3306 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003307
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003308 if (snd_soc_volsw_is_stereo(mc)) {
3309 if (invert)
3310 ucontrol->value.integer.value[1] = max - rval;
3311 else
3312 ucontrol->value.integer.value[1] = rval;
3313 }
3314
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003315 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003316}
3317EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3318
3319/**
3320 * snd_soc_dapm_put_volsw - dapm mixer set callback
3321 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003322 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003323 *
3324 * Callback to set the value of a dapm mixer control.
3325 *
3326 * Returns 0 for success.
3327 */
3328int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3329 struct snd_ctl_elem_value *ucontrol)
3330{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003331 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3332 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003333 struct soc_mixer_control *mc =
3334 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003335 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003336 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003337 int max = mc->max;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003338 unsigned int width = fls(max);
3339 unsigned int mask = (1 << width) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003340 unsigned int invert = mc->invert;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003341 unsigned int val, rval = 0;
3342 int connect, rconnect = -1, change, reg_change = 0;
Fabio Estevam33d92452018-02-14 13:39:05 -02003343 struct snd_soc_dapm_update update = {};
Nenghua Cao52765972013-12-13 20:13:49 +08003344 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003345
3346 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02003347 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003348
3349 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003350 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003351
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003352 if (snd_soc_volsw_is_stereo(mc)) {
3353 rval = (ucontrol->value.integer.value[1] & mask);
3354 rconnect = !!rval;
3355 if (invert)
3356 rval = max - rval;
3357 }
3358
Liam Girdwood3cd04342012-03-09 12:02:08 +00003359 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003360
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003361 /* This assumes field width < (bits in unsigned int / 2) */
3362 if (width > sizeof(unsigned int) * 8 / 2)
3363 dev_warn(dapm->dev,
3364 "ASoC: control %s field width limit exceeded\n",
3365 kcontrol->id.name);
3366 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
Mark Brown283375c2009-12-07 18:09:03 +00003367
Jarkko Nikula18626c72014-06-09 14:20:29 +03003368 if (reg != SND_SOC_NOPM) {
Jarkko Nikula18626c72014-06-09 14:20:29 +03003369 val = val << shift;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003370 rval = rval << mc->rshift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02003371
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003372 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3373
3374 if (snd_soc_volsw_is_stereo(mc))
3375 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3376 mask << mc->rshift,
3377 rval);
Jarkko Nikula18626c72014-06-09 14:20:29 +03003378 }
3379
3380 if (change || reg_change) {
3381 if (reg_change) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003382 if (snd_soc_volsw_is_stereo(mc)) {
3383 update.has_second_set = true;
3384 update.reg2 = mc->rreg;
3385 update.mask2 = mask << mc->rshift;
3386 update.val2 = rval;
3387 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003388 update.kcontrol = kcontrol;
3389 update.reg = reg;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003390 update.mask = mask << shift;
Jarkko Nikula18626c72014-06-09 14:20:29 +03003391 update.val = val;
3392 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003393 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003394 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00003395
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003396 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3397 rconnect);
Mark Brown97404f22010-12-14 16:13:57 +00003398
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003399 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00003400 }
3401
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003402 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003403
3404 if (ret > 0)
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01003405 snd_soc_dpcm_runtime_update(card);
Nenghua Cao52765972013-12-13 20:13:49 +08003406
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003407 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003408}
3409EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3410
3411/**
3412 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3413 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003414 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003415 *
3416 * Callback to get the value of a dapm enumerated double mixer control.
3417 *
3418 * Returns 0 for success.
3419 */
3420int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3421 struct snd_ctl_elem_value *ucontrol)
3422{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003423 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Charles Keepax561ed682015-05-01 12:37:26 +01003424 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003425 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003426 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003427
Charles Keepax561ed682015-05-01 12:37:26 +01003428 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3429 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003430 int ret = soc_dapm_read(dapm, e->reg, &reg_val);
Charles Keepax964a0b82015-05-08 10:50:10 +01003431 if (ret) {
3432 mutex_unlock(&card->dapm_mutex);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003433 return ret;
Charles Keepax964a0b82015-05-08 10:50:10 +01003434 }
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003435 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003436 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003437 }
Charles Keepax561ed682015-05-01 12:37:26 +01003438 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003439
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003440 val = (reg_val >> e->shift_l) & e->mask;
3441 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3442 if (e->shift_l != e->shift_r) {
3443 val = (reg_val >> e->shift_r) & e->mask;
3444 val = snd_soc_enum_val_to_item(e, val);
3445 ucontrol->value.enumerated.item[1] = val;
3446 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003447
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003448 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003449}
3450EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3451
Tzung-Bi Shih8f486292020-02-14 18:57:43 +08003452/**
3453 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3454 * @kcontrol: mixer control
3455 * @ucontrol: control element information
3456 *
3457 * Callback to set the value of a dapm enumerated double mixer control.
3458 *
3459 * Returns 0 for success.
3460 */
3461int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3462 struct snd_ctl_elem_value *ucontrol)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003463{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003464 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3465 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003466 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003467 unsigned int *item = ucontrol->value.enumerated.item;
Charles Keepax561ed682015-05-01 12:37:26 +01003468 unsigned int val, change, reg_change = 0;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003469 unsigned int mask;
Fabio Estevam33d92452018-02-14 13:39:05 -02003470 struct snd_soc_dapm_update update = {};
Nenghua Cao52765972013-12-13 20:13:49 +08003471 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003472
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003473 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003474 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003475
3476 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003477 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003478 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003479 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003480 return -EINVAL;
Chen-Yu Tsai071133a2016-08-27 19:27:59 +08003481 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003482 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003483 }
3484
Tzung-Bi Shih8f486292020-02-14 18:57:43 +08003485 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003486
Charles Keepax561ed682015-05-01 12:37:26 +01003487 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00003488
Charles Keepax561ed682015-05-01 12:37:26 +01003489 if (e->reg != SND_SOC_NOPM)
3490 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3491
3492 if (change || reg_change) {
3493 if (reg_change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003494 update.kcontrol = kcontrol;
3495 update.reg = e->reg;
3496 update.mask = mask;
3497 update.val = val;
3498 card->update = &update;
3499 }
Charles Keepax561ed682015-05-01 12:37:26 +01003500 change |= reg_change;
Mark Brown3a655772009-10-05 17:23:30 +01003501
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003502 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003503
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003504 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003505 }
3506
Tzung-Bi Shih8f486292020-02-14 18:57:43 +08003507 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003508
3509 if (ret > 0)
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01003510 snd_soc_dpcm_runtime_update(card);
Nenghua Cao52765972013-12-13 20:13:49 +08003511
Mark Brown97404f22010-12-14 16:13:57 +00003512 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003513}
3514EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3515
3516/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003517 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3518 *
3519 * @kcontrol: mixer control
3520 * @uinfo: control element information
3521 *
3522 * Callback to provide information about a pin switch control.
3523 */
3524int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3525 struct snd_ctl_elem_info *uinfo)
3526{
3527 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3528 uinfo->count = 1;
3529 uinfo->value.integer.min = 0;
3530 uinfo->value.integer.max = 1;
3531
3532 return 0;
3533}
3534EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3535
3536/**
3537 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3538 *
3539 * @kcontrol: mixer control
3540 * @ucontrol: Value
3541 */
3542int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3543 struct snd_ctl_elem_value *ucontrol)
3544{
Mark Brown48a8c392012-02-14 17:11:15 -08003545 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003546 const char *pin = (const char *)kcontrol->private_value;
3547
Liam Girdwood3cd04342012-03-09 12:02:08 +00003548 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003549
3550 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003551 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003552
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003553 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003554
3555 return 0;
3556}
3557EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3558
3559/**
3560 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3561 *
3562 * @kcontrol: mixer control
3563 * @ucontrol: Value
3564 */
3565int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3566 struct snd_ctl_elem_value *ucontrol)
3567{
Mark Brown48a8c392012-02-14 17:11:15 -08003568 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003569 const char *pin = (const char *)kcontrol->private_value;
3570
Mark Brown8b37dbd2009-02-28 21:14:20 +00003571 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003572 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003573 else
Mark Brown48a8c392012-02-14 17:11:15 -08003574 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003575
Mark Brown48a8c392012-02-14 17:11:15 -08003576 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003577 return 0;
3578}
3579EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3580
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +01003581struct snd_soc_dapm_widget *
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003582snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003583 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003584{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003585 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003586 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003587 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00003588 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003589
3590 if ((w = dapm_cnew_widget(widget)) == NULL)
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003591 return ERR_PTR(-ENOMEM);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003592
Mark Brown62ea8742012-01-21 21:14:48 +00003593 switch (w->id) {
3594 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003595 w->regulator = devm_regulator_get(dapm->dev, w->name);
3596 if (IS_ERR(w->regulator)) {
3597 ret = PTR_ERR(w->regulator);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003598 goto request_failed;
Mark Brown62ea8742012-01-21 21:14:48 +00003599 }
Mark Brown8784c772013-01-10 19:33:47 +00003600
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003601 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003602 ret = regulator_allow_bypass(w->regulator, true);
3603 if (ret != 0)
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003604 dev_warn(dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003605 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003606 w->name, ret);
3607 }
Mark Brown62ea8742012-01-21 21:14:48 +00003608 break;
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003609 case snd_soc_dapm_pinctrl:
3610 w->pinctrl = devm_pinctrl_get(dapm->dev);
Charles Keepaxa5cd7e92018-08-28 14:35:03 +01003611 if (IS_ERR(w->pinctrl)) {
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003612 ret = PTR_ERR(w->pinctrl);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003613 goto request_failed;
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003614 }
Tzung-Bi Shihec06dc12020-02-19 17:38:38 +08003615
3616 /* set to sleep_state when initializing */
3617 dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD);
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003618 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003619 case snd_soc_dapm_clock_supply:
Mark Brown695594f12012-06-04 08:14:13 +01003620 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003621 if (IS_ERR(w->clk)) {
3622 ret = PTR_ERR(w->clk);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003623 goto request_failed;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003624 }
3625 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003626 default:
3627 break;
3628 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003629
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003630 prefix = soc_dapm_prefix(dapm);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003631 if (prefix)
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003632 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003633 else
Lars-Peter Clausen48068962015-07-21 18:11:08 +02003634 w->name = kstrdup_const(widget->name, GFP_KERNEL);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003635 if (w->name == NULL) {
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -06003636 kfree_const(w->sname);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003637 kfree(w);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003638 return ERR_PTR(-ENOMEM);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003639 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003640
Mark Brown7ca3a182011-10-08 14:04:50 +01003641 switch (w->id) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003642 case snd_soc_dapm_mic:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003643 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003644 w->power_check = dapm_generic_check_power;
3645 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003646 case snd_soc_dapm_input:
3647 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003648 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003649 w->power_check = dapm_generic_check_power;
3650 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003651 case snd_soc_dapm_spk:
3652 case snd_soc_dapm_hp:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003653 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003654 w->power_check = dapm_generic_check_power;
3655 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003656 case snd_soc_dapm_output:
3657 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003658 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003659 w->power_check = dapm_generic_check_power;
3660 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003661 case snd_soc_dapm_vmid:
3662 case snd_soc_dapm_siggen:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003663 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003664 w->power_check = dapm_always_on_check_power;
3665 break;
Vinod Koul56b44372015-11-23 21:22:30 +05303666 case snd_soc_dapm_sink:
3667 w->is_ep = SND_SOC_DAPM_EP_SINK;
3668 w->power_check = dapm_always_on_check_power;
3669 break;
3670
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003671 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003672 case snd_soc_dapm_demux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003673 case snd_soc_dapm_switch:
3674 case snd_soc_dapm_mixer:
3675 case snd_soc_dapm_mixer_named_ctl:
Mark Brown63c69a62013-07-18 22:03:01 +01003676 case snd_soc_dapm_adc:
3677 case snd_soc_dapm_aif_out:
3678 case snd_soc_dapm_dac:
3679 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003680 case snd_soc_dapm_pga:
Ranjani Sridharan6e3bfcf2019-03-01 19:08:53 -06003681 case snd_soc_dapm_buffer:
3682 case snd_soc_dapm_scheduler:
3683 case snd_soc_dapm_effect:
3684 case snd_soc_dapm_src:
3685 case snd_soc_dapm_asrc:
3686 case snd_soc_dapm_encoder:
3687 case snd_soc_dapm_decoder:
Mark Brown7ca3a182011-10-08 14:04:50 +01003688 case snd_soc_dapm_out_drv:
Mark Brown7ca3a182011-10-08 14:04:50 +01003689 case snd_soc_dapm_micbias:
Mark Brown7ca3a182011-10-08 14:04:50 +01003690 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003691 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02003692 case snd_soc_dapm_dai_out:
3693 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003694 w->power_check = dapm_generic_check_power;
3695 break;
3696 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003697 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003698 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003699 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003700 case snd_soc_dapm_kcontrol:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003701 w->is_supply = 1;
Mark Brown7ca3a182011-10-08 14:04:50 +01003702 w->power_check = dapm_supply_check_power;
3703 break;
3704 default:
3705 w->power_check = dapm_always_on_check_power;
3706 break;
3707 }
3708
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003709 w->dapm = dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003710 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003711 INIT_LIST_HEAD(&w->dirty);
Kuninori Morimoto14596692020-03-09 13:08:21 +09003712 /* see for_each_card_widgets */
Lars-Peter Clausen92fa1242015-05-01 18:02:42 +02003713 list_add_tail(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003714
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003715 snd_soc_dapm_for_each_direction(dir) {
3716 INIT_LIST_HEAD(&w->edges[dir]);
3717 w->endpoints[dir] = -1;
3718 }
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003719
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02003720 /* machine layer sets up unconnected pins and insertions */
Richard Purdie2b97eab2006-10-06 18:32:18 +02003721 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003722 return w;
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003723
3724request_failed:
3725 if (ret != -EPROBE_DEFER)
3726 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3727 w->name, ret);
3728
Wenwen Wang45004d62019-07-22 08:57:44 -05003729 kfree_const(w->sname);
3730 kfree(w);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003731 return ERR_PTR(ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003732}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003733
3734/**
Charles Keepax94e630a2018-09-05 15:20:59 +01003735 * snd_soc_dapm_new_control - create new dapm control
3736 * @dapm: DAPM context
3737 * @widget: widget template
3738 *
3739 * Creates new DAPM control based upon a template.
3740 *
3741 * Returns a widget pointer on success or an error pointer on failure
3742 */
3743struct snd_soc_dapm_widget *
3744snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3745 const struct snd_soc_dapm_widget *widget)
3746{
3747 struct snd_soc_dapm_widget *w;
3748
3749 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3750 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3751 mutex_unlock(&dapm->card->dapm_mutex);
3752
3753 return w;
3754}
3755EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3756
3757/**
Mark Brown4ba13272008-05-13 14:51:19 +02003758 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003759 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003760 * @widget: widget array
3761 * @num: number of widgets
3762 *
3763 * Creates new DAPM controls based upon the templates.
3764 *
3765 * Returns 0 for success else error.
3766 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003767int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003768 const struct snd_soc_dapm_widget *widget,
3769 int num)
3770{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003771 struct snd_soc_dapm_widget *w;
3772 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003773 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003774
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003775 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003776 for (i = 0; i < num; i++) {
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003777 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
Linus Walleij37e1df82017-01-13 10:23:52 +01003778 if (IS_ERR(w)) {
3779 ret = PTR_ERR(w);
Dan Carpenter60884c22012-04-13 22:25:43 +03003780 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003781 }
Mark Brown4ba13272008-05-13 14:51:19 +02003782 widget++;
3783 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003784 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003785 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003786}
3787EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3788
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003789static int
3790snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3791 struct snd_pcm_substream *substream)
Mark Brownc74184e2012-04-04 22:12:09 +01003792{
Charles Keepax4a75aae2018-09-05 15:21:01 +01003793 struct snd_soc_dapm_path *path;
Mark Brownc74184e2012-04-04 22:12:09 +01003794 struct snd_soc_dai *source, *sink;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003795 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Mark Brown9747cec2012-04-26 19:12:21 +01003796 struct snd_pcm_hw_params *params = NULL;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003797 const struct snd_soc_pcm_stream *config = NULL;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003798 struct snd_pcm_runtime *runtime = NULL;
Takashi Iwai3ba66fe2018-07-25 22:43:26 +02003799 unsigned int fmt;
Colin Ian Kingc8415832019-07-26 13:33:27 +01003800 int ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003801
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003802 params = kzalloc(sizeof(*params), GFP_KERNEL);
3803 if (!params)
3804 return -ENOMEM;
3805
Jerome Bruneta72706e2019-07-25 18:59:48 +02003806 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3807 if (!runtime) {
3808 ret = -ENOMEM;
3809 goto out;
3810 }
3811
3812 substream->runtime = runtime;
3813
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003814 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3815 snd_soc_dapm_widget_for_each_source_path(w, path) {
3816 source = path->source->priv;
3817
3818 ret = snd_soc_dai_startup(source, substream);
3819 if (ret < 0) {
3820 dev_err(source->dev,
3821 "ASoC: startup() failed: %d\n", ret);
3822 goto out;
3823 }
3824 source->active++;
3825 }
3826
3827 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3828 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3829 sink = path->sink->priv;
3830
3831 ret = snd_soc_dai_startup(sink, substream);
3832 if (ret < 0) {
3833 dev_err(sink->dev,
3834 "ASoC: startup() failed: %d\n", ret);
3835 goto out;
3836 }
3837 sink->active++;
3838 }
3839
Jerome Bruneta72706e2019-07-25 18:59:48 +02003840 substream->hw_opened = 1;
3841
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003842 /*
3843 * Note: getting the config after .startup() gives a chance to
3844 * either party on the link to alter the configuration if
3845 * necessary
3846 */
Charles Keepax243bcfa2018-09-05 15:21:02 +01003847 config = rtd->dai_link->params + rtd->params_select;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003848 if (WARN_ON(!config)) {
3849 dev_err(w->dapm->dev, "ASoC: link config missing\n");
3850 ret = -EINVAL;
3851 goto out;
3852 }
Mark Brownc74184e2012-04-04 22:12:09 +01003853
Mark Brownc74184e2012-04-04 22:12:09 +01003854 /* Be a little careful as we don't want to overflow the mask array */
3855 if (config->formats) {
3856 fmt = ffs(config->formats) - 1;
3857 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003858 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003859 config->formats);
Mark Brownc74184e2012-04-04 22:12:09 +01003860
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003861 ret = -EINVAL;
Mark Brown9747cec2012-04-26 19:12:21 +01003862 goto out;
3863 }
Mark Brownc74184e2012-04-04 22:12:09 +01003864
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003865 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brown9747cec2012-04-26 19:12:21 +01003866 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003867 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003868 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003869 config->rate_max;
Mark Brown9747cec2012-04-26 19:12:21 +01003870 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003871 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003872 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003873 = config->channels_max;
3874
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003875 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3876 snd_soc_dapm_widget_for_each_source_path(w, path) {
3877 source = path->source->priv;
3878
3879 ret = snd_soc_dai_hw_params(source, substream, params);
3880 if (ret < 0)
3881 goto out;
3882
3883 dapm_update_dai_unlocked(substream, params, source);
3884 }
3885
3886 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3887 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3888 sink = path->sink->priv;
3889
3890 ret = snd_soc_dai_hw_params(sink, substream, params);
3891 if (ret < 0)
3892 goto out;
3893
3894 dapm_update_dai_unlocked(substream, params, sink);
3895 }
3896
Jerome Brunet9de98622019-07-25 18:59:49 +02003897 runtime->format = params_format(params);
3898 runtime->subformat = params_subformat(params);
3899 runtime->channels = params_channels(params);
3900 runtime->rate = params_rate(params);
3901
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003902out:
3903 kfree(params);
Colin Ian Kingc8415832019-07-26 13:33:27 +01003904 return ret;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003905}
3906
3907static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3908 struct snd_kcontrol *kcontrol, int event)
3909{
3910 struct snd_soc_dapm_path *path;
3911 struct snd_soc_dai *source, *sink;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003912 struct snd_pcm_substream *substream = w->priv;
3913 int ret = 0, saved_stream = substream->stream;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003914
3915 if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3916 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3917 return -EINVAL;
3918
Mark Brownc74184e2012-04-04 22:12:09 +01003919 switch (event) {
3920 case SND_SOC_DAPM_PRE_PMU:
Jerome Bruneta72706e2019-07-25 18:59:48 +02003921 ret = snd_soc_dai_link_event_pre_pmu(w, substream);
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003922 if (ret < 0)
3923 goto out;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003924
Mark Brownc74184e2012-04-04 22:12:09 +01003925 break;
3926
3927 case SND_SOC_DAPM_POST_PMU:
Charles Keepax4a75aae2018-09-05 15:21:01 +01003928 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3929 sink = path->sink->priv;
3930
3931 ret = snd_soc_dai_digital_mute(sink, 0,
3932 SNDRV_PCM_STREAM_PLAYBACK);
3933 if (ret != 0 && ret != -ENOTSUPP)
3934 dev_warn(sink->dev,
3935 "ASoC: Failed to unmute: %d\n", ret);
3936 ret = 0;
3937 }
Mark Brownc74184e2012-04-04 22:12:09 +01003938 break;
3939
3940 case SND_SOC_DAPM_PRE_PMD:
Charles Keepax4a75aae2018-09-05 15:21:01 +01003941 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3942 sink = path->sink->priv;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303943
Charles Keepax4a75aae2018-09-05 15:21:01 +01003944 ret = snd_soc_dai_digital_mute(sink, 1,
3945 SNDRV_PCM_STREAM_PLAYBACK);
3946 if (ret != 0 && ret != -ENOTSUPP)
3947 dev_warn(sink->dev,
3948 "ASoC: Failed to mute: %d\n", ret);
3949 ret = 0;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303950 }
3951
Jerome Bruneta72706e2019-07-25 18:59:48 +02003952 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003953 snd_soc_dapm_widget_for_each_source_path(w, path) {
3954 source = path->source->priv;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003955 snd_soc_dai_hw_free(source, substream);
Jerome Brunet68c907f2019-07-25 18:59:44 +02003956 }
Charles Keepax3c01b0e2018-10-11 17:28:28 +01003957
Jerome Bruneta72706e2019-07-25 18:59:48 +02003958 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
Jerome Brunet68c907f2019-07-25 18:59:44 +02003959 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3960 sink = path->sink->priv;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003961 snd_soc_dai_hw_free(sink, substream);
Jerome Brunet68c907f2019-07-25 18:59:44 +02003962 }
3963
Jerome Bruneta72706e2019-07-25 18:59:48 +02003964 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
Jerome Brunet68c907f2019-07-25 18:59:44 +02003965 snd_soc_dapm_widget_for_each_source_path(w, path) {
3966 source = path->source->priv;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003967 source->active--;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003968 snd_soc_dai_shutdown(source, substream);
Charles Keepax4a75aae2018-09-05 15:21:01 +01003969 }
3970
Jerome Bruneta72706e2019-07-25 18:59:48 +02003971 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003972 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3973 sink = path->sink->priv;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003974 sink->active--;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003975 snd_soc_dai_shutdown(sink, substream);
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303976 }
Mark Brownc74184e2012-04-04 22:12:09 +01003977 break;
3978
Jerome Bruneta72706e2019-07-25 18:59:48 +02003979 case SND_SOC_DAPM_POST_PMD:
3980 kfree(substream->runtime);
3981 break;
3982
Mark Brownc74184e2012-04-04 22:12:09 +01003983 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003984 WARN(1, "Unknown event %d\n", event);
Sudip Mukherjee75881df2015-09-10 18:01:44 +05303985 ret = -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003986 }
3987
Mark Brown9747cec2012-04-26 19:12:21 +01003988out:
Jerome Bruneta72706e2019-07-25 18:59:48 +02003989 /* Restore the substream direction */
3990 substream->stream = saved_stream;
Mark Brown9747cec2012-04-26 19:12:21 +01003991 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003992}
3993
Nikesh Oswalc6615082015-02-02 17:06:44 +00003994static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3995 struct snd_ctl_elem_value *ucontrol)
3996{
3997 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
Charles Keepax243bcfa2018-09-05 15:21:02 +01003998 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003999
Charles Keepax243bcfa2018-09-05 15:21:02 +01004000 ucontrol->value.enumerated.item[0] = rtd->params_select;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004001
4002 return 0;
4003}
4004
4005static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
4006 struct snd_ctl_elem_value *ucontrol)
4007{
4008 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
Charles Keepax243bcfa2018-09-05 15:21:02 +01004009 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004010
4011 /* Can't change the config when widget is already powered */
4012 if (w->power)
4013 return -EBUSY;
4014
Charles Keepax243bcfa2018-09-05 15:21:02 +01004015 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
Nikesh Oswalc6615082015-02-02 17:06:44 +00004016 return 0;
4017
Charles Keepax243bcfa2018-09-05 15:21:02 +01004018 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
Nikesh Oswalc6615082015-02-02 17:06:44 +00004019 return -EINVAL;
4020
Charles Keepax243bcfa2018-09-05 15:21:02 +01004021 rtd->params_select = ucontrol->value.enumerated.item[0];
Nikesh Oswalc6615082015-02-02 17:06:44 +00004022
4023 return 0;
4024}
4025
Arnd Bergmannc42c5ac2017-10-10 11:20:11 +02004026static void
anish kumar19ad6832017-09-28 21:52:39 -07004027snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
4028 unsigned long *private_value,
4029 int num_params,
4030 const char **w_param_text)
Mark Brownc74184e2012-04-04 22:12:09 +01004031{
anish kumar19ad6832017-09-28 21:52:39 -07004032 int count;
4033
4034 devm_kfree(card->dev, (void *)*private_value);
Pankaj Bharadiyacacea3a2019-03-22 18:00:09 +05304035
4036 if (!w_param_text)
4037 return;
4038
anish kumar19ad6832017-09-28 21:52:39 -07004039 for (count = 0 ; count < num_params; count++)
4040 devm_kfree(card->dev, (void *)w_param_text[count]);
4041 devm_kfree(card->dev, w_param_text);
4042}
4043
4044static struct snd_kcontrol_new *
4045snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
4046 char *link_name,
4047 const struct snd_soc_pcm_stream *params,
4048 int num_params, const char **w_param_text,
4049 unsigned long *private_value)
4050{
Nikesh Oswalc6615082015-02-02 17:06:44 +00004051 struct soc_enum w_param_enum[] = {
4052 SOC_ENUM_SINGLE(0, 0, 0, NULL),
4053 };
4054 struct snd_kcontrol_new kcontrol_dai_link[] = {
4055 SOC_ENUM_EXT(NULL, w_param_enum[0],
4056 snd_soc_dapm_dai_link_get,
4057 snd_soc_dapm_dai_link_put),
4058 };
anish kumar19ad6832017-09-28 21:52:39 -07004059 struct snd_kcontrol_new *kcontrol_news;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004060 const struct snd_soc_pcm_stream *config = params;
anish kumar19ad6832017-09-28 21:52:39 -07004061 int count;
Mark Brownc74184e2012-04-04 22:12:09 +01004062
Nikesh Oswalc6615082015-02-02 17:06:44 +00004063 for (count = 0 ; count < num_params; count++) {
4064 if (!config->stream_name) {
4065 dev_warn(card->dapm.dev,
4066 "ASoC: anonymous config %d for dai link %s\n",
4067 count, link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004068 w_param_text[count] =
Charles Keepax46172b62015-03-25 11:22:35 +00004069 devm_kasprintf(card->dev, GFP_KERNEL,
4070 "Anonymous Configuration %d",
4071 count);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004072 } else {
4073 w_param_text[count] = devm_kmemdup(card->dev,
4074 config->stream_name,
4075 strlen(config->stream_name) + 1,
4076 GFP_KERNEL);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004077 }
anish kumar19ad6832017-09-28 21:52:39 -07004078 if (!w_param_text[count])
4079 goto outfree_w_param;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004080 config++;
4081 }
anish kumar19ad6832017-09-28 21:52:39 -07004082
Nikesh Oswalc6615082015-02-02 17:06:44 +00004083 w_param_enum[0].items = num_params;
4084 w_param_enum[0].texts = w_param_text;
Mark Brownc74184e2012-04-04 22:12:09 +01004085
anish kumar19ad6832017-09-28 21:52:39 -07004086 *private_value =
4087 (unsigned long) devm_kmemdup(card->dev,
4088 (void *)(kcontrol_dai_link[0].private_value),
4089 sizeof(struct soc_enum), GFP_KERNEL);
4090 if (!*private_value) {
4091 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4092 link_name);
4093 goto outfree_w_param;
4094 }
4095 kcontrol_dai_link[0].private_value = *private_value;
4096 /* duplicate kcontrol_dai_link on heap so that memory persists */
4097 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4098 sizeof(struct snd_kcontrol_new),
4099 GFP_KERNEL);
4100 if (!kcontrol_news) {
4101 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4102 link_name);
4103 goto outfree_w_param;
4104 }
4105 return kcontrol_news;
4106
4107outfree_w_param:
4108 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
4109 return NULL;
4110}
4111
Charles Keepax778ff5b2018-09-05 15:21:00 +01004112static struct snd_soc_dapm_widget *
Jerome Bruneta72706e2019-07-25 18:59:48 +02004113snd_soc_dapm_new_dai(struct snd_soc_card *card,
4114 struct snd_pcm_substream *substream,
Jerome Brunet054d6502019-07-25 18:59:45 +02004115 char *id)
anish kumar19ad6832017-09-28 21:52:39 -07004116{
Jerome Bruneta72706e2019-07-25 18:59:48 +02004117 struct snd_soc_pcm_runtime *rtd = substream->private_data;
anish kumar19ad6832017-09-28 21:52:39 -07004118 struct snd_soc_dapm_widget template;
4119 struct snd_soc_dapm_widget *w;
4120 const char **w_param_text;
Pankaj Bharadiya8633d442019-03-22 21:53:39 +05304121 unsigned long private_value = 0;
anish kumar19ad6832017-09-28 21:52:39 -07004122 char *link_name;
4123 int ret;
4124
4125 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
Jerome Brunet054d6502019-07-25 18:59:45 +02004126 rtd->dai_link->name, id);
anish kumar19ad6832017-09-28 21:52:39 -07004127 if (!link_name)
Charles Keepax778ff5b2018-09-05 15:21:00 +01004128 return ERR_PTR(-ENOMEM);
anish kumar19ad6832017-09-28 21:52:39 -07004129
Mark Brownc74184e2012-04-04 22:12:09 +01004130 memset(&template, 0, sizeof(template));
4131 template.reg = SND_SOC_NOPM;
4132 template.id = snd_soc_dapm_dai_link;
4133 template.name = link_name;
4134 template.event = snd_soc_dai_link_event;
4135 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
Jerome Bruneta72706e2019-07-25 18:59:48 +02004136 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD;
anish kumar19ad6832017-09-28 21:52:39 -07004137 template.kcontrol_news = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01004138
anish kumar19ad6832017-09-28 21:52:39 -07004139 /* allocate memory for control, only in case of multiple configs */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004140 if (rtd->dai_link->num_params > 1) {
4141 w_param_text = devm_kcalloc(card->dev,
4142 rtd->dai_link->num_params,
4143 sizeof(char *), GFP_KERNEL);
anish kumar19ad6832017-09-28 21:52:39 -07004144 if (!w_param_text) {
4145 ret = -ENOMEM;
4146 goto param_fail;
4147 }
4148
4149 template.num_kcontrols = 1;
4150 template.kcontrol_news =
4151 snd_soc_dapm_alloc_kcontrol(card,
Charles Keepax778ff5b2018-09-05 15:21:00 +01004152 link_name,
4153 rtd->dai_link->params,
4154 rtd->dai_link->num_params,
anish kumar19ad6832017-09-28 21:52:39 -07004155 w_param_text, &private_value);
4156 if (!template.kcontrol_news) {
4157 ret = -ENOMEM;
4158 goto param_fail;
4159 }
Arnd Bergmann667ebc92017-10-10 11:20:10 +02004160 } else {
4161 w_param_text = NULL;
anish kumar19ad6832017-09-28 21:52:39 -07004162 }
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004163 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01004164
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004165 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
Dan Carpenter2e558a82018-09-07 22:40:33 +03004166 if (IS_ERR(w)) {
4167 ret = PTR_ERR(w);
Linus Walleij37e1df82017-01-13 10:23:52 +01004168 goto outfree_kcontrol_news;
Dan Carpenter2e558a82018-09-07 22:40:33 +03004169 }
Mark Brownc74184e2012-04-04 22:12:09 +01004170
Jerome Bruneta72706e2019-07-25 18:59:48 +02004171 w->priv = substream;
Mark Brownc74184e2012-04-04 22:12:09 +01004172
Charles Keepax778ff5b2018-09-05 15:21:00 +01004173 return w;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004174
Nikesh Oswalc6615082015-02-02 17:06:44 +00004175outfree_kcontrol_news:
4176 devm_kfree(card->dev, (void *)template.kcontrol_news);
Charles Keepax778ff5b2018-09-05 15:21:00 +01004177 snd_soc_dapm_free_kcontrol(card, &private_value,
4178 rtd->dai_link->num_params, w_param_text);
anish kumar19ad6832017-09-28 21:52:39 -07004179param_fail:
Nikesh Oswalc6615082015-02-02 17:06:44 +00004180 devm_kfree(card->dev, link_name);
Charles Keepax778ff5b2018-09-05 15:21:00 +01004181 return ERR_PTR(ret);
Mark Brownc74184e2012-04-04 22:12:09 +01004182}
4183
Mark Brown888df392012-02-16 19:37:51 -08004184int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4185 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004186{
Mark Brown888df392012-02-16 19:37:51 -08004187 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004188 struct snd_soc_dapm_widget *w;
4189
Mark Brown888df392012-02-16 19:37:51 -08004190 WARN_ON(dapm->dev != dai->dev);
4191
4192 memset(&template, 0, sizeof(template));
4193 template.reg = SND_SOC_NOPM;
4194
4195 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01004196 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08004197 template.name = dai->driver->playback.stream_name;
4198 template.sname = dai->driver->playback.stream_name;
4199
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004200 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08004201 template.name);
4202
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004203 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01004204 if (IS_ERR(w))
4205 return PTR_ERR(w);
Mark Brown888df392012-02-16 19:37:51 -08004206
4207 w->priv = dai;
4208 dai->playback_widget = w;
4209 }
4210
4211 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01004212 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08004213 template.name = dai->driver->capture.stream_name;
4214 template.sname = dai->driver->capture.stream_name;
4215
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004216 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08004217 template.name);
4218
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004219 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01004220 if (IS_ERR(w))
4221 return PTR_ERR(w);
Mark Brown888df392012-02-16 19:37:51 -08004222
4223 w->priv = dai;
4224 dai->capture_widget = w;
4225 }
4226
4227 return 0;
4228}
4229
4230int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4231{
4232 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004233 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08004234 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08004235
4236 /* For each DAI widget... */
Kuninori Morimoto14596692020-03-09 13:08:21 +09004237 for_each_card_widgets(card, dai_w) {
Mark Brown46162742013-06-05 19:36:11 +01004238 switch (dai_w->id) {
4239 case snd_soc_dapm_dai_in:
4240 case snd_soc_dapm_dai_out:
4241 break;
4242 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02004243 continue;
Mark Brown46162742013-06-05 19:36:11 +01004244 }
Mark Brown888df392012-02-16 19:37:51 -08004245
Liam Girdwoode01b4f62018-06-14 20:26:42 +01004246 /* let users know there is no DAI to link */
4247 if (!dai_w->priv) {
4248 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4249 dai_w->name);
4250 continue;
4251 }
4252
Mark Brown888df392012-02-16 19:37:51 -08004253 dai = dai_w->priv;
4254
4255 /* ...find all widgets with the same stream and link them */
Kuninori Morimoto14596692020-03-09 13:08:21 +09004256 for_each_card_widgets(card, w) {
Mark Brown888df392012-02-16 19:37:51 -08004257 if (w->dapm != dai_w->dapm)
4258 continue;
4259
Mark Brown46162742013-06-05 19:36:11 +01004260 switch (w->id) {
4261 case snd_soc_dapm_dai_in:
4262 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08004263 continue;
Mark Brown46162742013-06-05 19:36:11 +01004264 default:
4265 break;
4266 }
Mark Brown888df392012-02-16 19:37:51 -08004267
Lars-Peter Clausena798c242015-07-21 11:51:35 +02004268 if (!w->sname || !strstr(w->sname, dai_w->sname))
Mark Brown888df392012-02-16 19:37:51 -08004269 continue;
4270
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004271 if (dai_w->id == snd_soc_dapm_dai_in) {
4272 src = dai_w;
4273 sink = w;
4274 } else {
4275 src = w;
4276 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08004277 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004278 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4279 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004280 }
4281 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02004282
Mark Brown888df392012-02-16 19:37:51 -08004283 return 0;
4284}
Liam Girdwood64a648c2011-07-25 11:15:15 +01004285
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004286static void dapm_add_valid_dai_widget(struct snd_soc_card *card,
4287 struct snd_soc_pcm_runtime *rtd,
4288 struct snd_soc_dai *codec_dai,
4289 struct snd_soc_dai *cpu_dai)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004290{
Charles Keepax778ff5b2018-09-05 15:21:00 +01004291 struct snd_soc_dapm_widget *playback = NULL, *capture = NULL;
4292 struct snd_soc_dapm_widget *codec, *playback_cpu, *capture_cpu;
Jerome Bruneta72706e2019-07-25 18:59:48 +02004293 struct snd_pcm_substream *substream;
4294 struct snd_pcm_str *streams = rtd->pcm->streams;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004295
Charles Keepax778ff5b2018-09-05 15:21:00 +01004296 if (rtd->dai_link->params) {
Charles Keepax778ff5b2018-09-05 15:21:00 +01004297 playback_cpu = cpu_dai->capture_widget;
4298 capture_cpu = cpu_dai->playback_widget;
4299 } else {
4300 playback = cpu_dai->playback_widget;
4301 capture = cpu_dai->capture_widget;
4302 playback_cpu = playback;
4303 capture_cpu = capture;
4304 }
4305
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004306 /* connect BE DAI playback if widgets are valid */
4307 codec = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004308
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004309 if (playback_cpu && codec) {
4310 if (!playback) {
4311 substream = streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
4312 playback = snd_soc_dapm_new_dai(card, substream,
4313 "playback");
4314 if (IS_ERR(playback)) {
4315 dev_err(rtd->dev,
4316 "ASoC: Failed to create DAI %s: %ld\n",
4317 codec_dai->name,
4318 PTR_ERR(playback));
4319 goto capture;
Charles Keepax778ff5b2018-09-05 15:21:00 +01004320 }
4321
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004322 snd_soc_dapm_add_path(&card->dapm, playback_cpu,
4323 playback, NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004324 }
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004325
4326 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4327 cpu_dai->component->name, playback_cpu->name,
4328 codec_dai->component->name, codec->name);
4329
4330 snd_soc_dapm_add_path(&card->dapm, playback, codec,
4331 NULL, NULL);
Charles Keepaxe36a1d02018-09-10 15:28:39 +01004332 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004333
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004334capture:
4335 /* connect BE DAI capture if widgets are valid */
4336 codec = codec_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004337
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004338 if (codec && capture_cpu) {
4339 if (!capture) {
4340 substream = streams[SNDRV_PCM_STREAM_CAPTURE].substream;
4341 capture = snd_soc_dapm_new_dai(card, substream,
4342 "capture");
4343 if (IS_ERR(capture)) {
4344 dev_err(rtd->dev,
4345 "ASoC: Failed to create DAI %s: %ld\n",
4346 codec_dai->name,
4347 PTR_ERR(capture));
4348 return;
Charles Keepax778ff5b2018-09-05 15:21:00 +01004349 }
4350
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004351 snd_soc_dapm_add_path(&card->dapm, capture,
4352 capture_cpu, NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004353 }
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004354
4355 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4356 codec_dai->component->name, codec->name,
4357 cpu_dai->component->name, capture_cpu->name);
4358
4359 snd_soc_dapm_add_path(&card->dapm, codec, capture,
4360 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004361 }
4362}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004363
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004364static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4365 struct snd_soc_pcm_runtime *rtd)
4366{
4367 struct snd_soc_dai *codec_dai;
4368 int i;
4369
Shreyas NCde6214a2020-02-25 21:39:15 +08004370 if (rtd->num_cpus == 1) {
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09004371 for_each_rtd_codec_dais(rtd, i, codec_dai)
Shreyas NCde6214a2020-02-25 21:39:15 +08004372 dapm_add_valid_dai_widget(card, rtd, codec_dai,
4373 rtd->cpu_dais[0]);
4374 } else if (rtd->num_codecs == rtd->num_cpus) {
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09004375 for_each_rtd_codec_dais(rtd, i, codec_dai)
Shreyas NCde6214a2020-02-25 21:39:15 +08004376 dapm_add_valid_dai_widget(card, rtd, codec_dai,
4377 rtd->cpu_dais[i]);
4378 } else {
4379 dev_err(card->dev,
4380 "N cpus to M codecs link is not supported yet\n");
4381 }
4382
Shreyas NC6c4b13b2020-02-25 21:39:14 +08004383}
4384
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004385static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4386 int event)
4387{
4388 struct snd_soc_dapm_widget *w;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004389 unsigned int ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004390
Kuninori Morimoto0c01f6c2020-02-19 15:56:41 +09004391 w = snd_soc_dai_get_widget(dai, stream);
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004392
4393 if (w) {
4394 dapm_mark_dirty(w, "stream event");
4395
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004396 if (w->id == snd_soc_dapm_dai_in) {
4397 ep = SND_SOC_DAPM_EP_SOURCE;
4398 dapm_widget_invalidate_input_paths(w);
4399 } else {
4400 ep = SND_SOC_DAPM_EP_SINK;
4401 dapm_widget_invalidate_output_paths(w);
4402 }
4403
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004404 switch (event) {
4405 case SND_SOC_DAPM_STREAM_START:
4406 w->active = 1;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004407 w->is_ep = ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004408 break;
4409 case SND_SOC_DAPM_STREAM_STOP:
4410 w->active = 0;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004411 w->is_ep = 0;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004412 break;
4413 case SND_SOC_DAPM_STREAM_SUSPEND:
4414 case SND_SOC_DAPM_STREAM_RESUME:
4415 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4416 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4417 break;
4418 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004419 }
4420}
4421
Benoit Cousson44ba2642014-07-08 23:19:36 +02004422void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4423{
Mengdong Lin1a497982015-11-18 02:34:11 -05004424 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson44ba2642014-07-08 23:19:36 +02004425
4426 /* for each BE DAI link... */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00004427 for_each_card_rtds(card, rtd) {
Benoit Cousson44ba2642014-07-08 23:19:36 +02004428 /*
4429 * dynamic FE links have no fixed DAI mapping.
4430 * CODEC<->CODEC links have no direct connection.
4431 */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004432 if (rtd->dai_link->dynamic)
Benoit Cousson44ba2642014-07-08 23:19:36 +02004433 continue;
4434
4435 dapm_connect_dai_link_widgets(card, rtd);
4436 }
4437}
4438
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004439static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4440 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004441{
Kuninori Morimotoe3c3cf72020-03-16 15:37:09 +09004442 struct snd_soc_dai *dai;
Benoit Cousson44ba2642014-07-08 23:19:36 +02004443 int i;
4444
Kuninori Morimotoe3c3cf72020-03-16 15:37:09 +09004445 for_each_rtd_dais(rtd, i, dai)
4446 soc_dapm_dai_stream_event(dai, stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004447
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004448 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004449}
4450
4451/**
4452 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4453 * @rtd: PCM runtime data
4454 * @stream: stream name
4455 * @event: stream event
4456 *
4457 * Sends a stream event to the dapm core. The core then makes any
4458 * necessary widget power changes.
4459 *
4460 * Returns 0 for success else error.
4461 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004462void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4463 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004464{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004465 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004466
Liam Girdwood3cd04342012-03-09 12:02:08 +00004467 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004468 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004469 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004470}
Richard Purdie2b97eab2006-10-06 18:32:18 +02004471
Kuninori Morimoto3f4cf792020-01-10 11:36:23 +09004472void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream)
4473{
4474 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
4475 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
4476 /* powered down playback stream now */
4477 snd_soc_dapm_stream_event(rtd,
4478 SNDRV_PCM_STREAM_PLAYBACK,
4479 SND_SOC_DAPM_STREAM_STOP);
4480 } else {
4481 /* start delayed pop wq here for playback streams */
4482 rtd->pop_wait = 1;
4483 queue_delayed_work(system_power_efficient_wq,
4484 &rtd->delayed_work,
4485 msecs_to_jiffies(rtd->pmdown_time));
4486 }
4487 } else {
4488 /* capture streams can be powered down now */
4489 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
4490 SND_SOC_DAPM_STREAM_STOP);
4491 }
4492}
4493EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop);
4494
Richard Purdie2b97eab2006-10-06 18:32:18 +02004495/**
Charles Keepax11391102014-02-18 15:22:14 +00004496 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4497 * @dapm: DAPM context
4498 * @pin: pin name
4499 *
4500 * Enables input/output pin and its parents or children widgets iff there is
4501 * a valid audio route and active audio stream.
4502 *
4503 * Requires external locking.
4504 *
4505 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4506 * do any widget power switching.
4507 */
4508int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4509 const char *pin)
4510{
4511 return snd_soc_dapm_set_pin(dapm, pin, 1);
4512}
4513EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4514
4515/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004516 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004517 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004518 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02004519 *
Mark Brown74b8f952009-06-06 11:26:15 +01004520 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01004521 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00004522 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004523 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4524 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02004525 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004526int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004527{
Charles Keepax11391102014-02-18 15:22:14 +00004528 int ret;
4529
4530 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4531
4532 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4533
4534 mutex_unlock(&dapm->card->dapm_mutex);
4535
4536 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004537}
Liam Girdwooda5302182008-07-07 13:35:17 +01004538EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004539
4540/**
Charles Keepax11391102014-02-18 15:22:14 +00004541 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4542 * @dapm: DAPM context
4543 * @pin: pin name
4544 *
4545 * Enables input/output pin regardless of any other state. This is
4546 * intended for use with microphone bias supplies used in microphone
4547 * jack detection.
4548 *
4549 * Requires external locking.
4550 *
4551 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4552 * do any widget power switching.
4553 */
4554int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4555 const char *pin)
4556{
4557 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4558
4559 if (!w) {
4560 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4561 return -EINVAL;
4562 }
4563
4564 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02004565 if (!w->connected) {
4566 /*
4567 * w->force does not affect the number of input or output paths,
4568 * so we only have to recheck if w->connected is changed
4569 */
4570 dapm_widget_invalidate_input_paths(w);
4571 dapm_widget_invalidate_output_paths(w);
4572 w->connected = 1;
4573 }
Charles Keepax11391102014-02-18 15:22:14 +00004574 w->force = 1;
4575 dapm_mark_dirty(w, "force enable");
4576
4577 return 0;
4578}
4579EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4580
4581/**
Mark Brownda341832010-03-15 19:23:37 +00004582 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004583 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00004584 * @pin: pin name
4585 *
4586 * Enables input/output pin regardless of any other state. This is
4587 * intended for use with microphone bias supplies used in microphone
4588 * jack detection.
4589 *
4590 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4591 * do any widget power switching.
4592 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004593int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4594 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00004595{
Charles Keepax11391102014-02-18 15:22:14 +00004596 int ret;
Mark Brownda341832010-03-15 19:23:37 +00004597
Charles Keepax11391102014-02-18 15:22:14 +00004598 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00004599
Charles Keepax11391102014-02-18 15:22:14 +00004600 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09004601
Charles Keepax11391102014-02-18 15:22:14 +00004602 mutex_unlock(&dapm->card->dapm_mutex);
4603
4604 return ret;
Mark Brownda341832010-03-15 19:23:37 +00004605}
4606EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4607
4608/**
Charles Keepax11391102014-02-18 15:22:14 +00004609 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4610 * @dapm: DAPM context
4611 * @pin: pin name
4612 *
4613 * Disables input/output pin and its parents or children widgets.
4614 *
4615 * Requires external locking.
4616 *
4617 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4618 * do any widget power switching.
4619 */
4620int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4621 const char *pin)
4622{
4623 return snd_soc_dapm_set_pin(dapm, pin, 0);
4624}
4625EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4626
4627/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004628 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004629 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004630 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004631 *
Mark Brown74b8f952009-06-06 11:26:15 +01004632 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00004633 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004634 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4635 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004636 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004637int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4638 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01004639{
Charles Keepax11391102014-02-18 15:22:14 +00004640 int ret;
4641
4642 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4643
4644 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4645
4646 mutex_unlock(&dapm->card->dapm_mutex);
4647
4648 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01004649}
4650EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4651
4652/**
Charles Keepax11391102014-02-18 15:22:14 +00004653 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4654 * @dapm: DAPM context
4655 * @pin: pin name
4656 *
4657 * Marks the specified pin as being not connected, disabling it along
4658 * any parent or child widgets. At present this is identical to
4659 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4660 * additional things such as disabling controls which only affect
4661 * paths through the pin.
4662 *
4663 * Requires external locking.
4664 *
4665 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4666 * do any widget power switching.
4667 */
4668int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4669 const char *pin)
4670{
4671 return snd_soc_dapm_set_pin(dapm, pin, 0);
4672}
4673EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4674
4675/**
Mark Brown5817b522008-09-24 11:23:11 +01004676 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004677 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01004678 * @pin: pin name
4679 *
4680 * Marks the specified pin as being not connected, disabling it along
4681 * any parent or child widgets. At present this is identical to
4682 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4683 * additional things such as disabling controls which only affect
4684 * paths through the pin.
4685 *
4686 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4687 * do any widget power switching.
4688 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004689int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01004690{
Charles Keepax11391102014-02-18 15:22:14 +00004691 int ret;
4692
4693 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4694
4695 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4696
4697 mutex_unlock(&dapm->card->dapm_mutex);
4698
4699 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01004700}
4701EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4702
4703/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004704 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004705 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004706 * @pin: audio signal pin endpoint (or start point)
4707 *
4708 * Get audio pin status - connected or disconnected.
4709 *
4710 * Returns 1 for connected otherwise 0.
4711 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004712int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4713 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004714{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004715 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004716
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004717 if (w)
4718 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06004719
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004720 return 0;
4721}
Liam Girdwooda5302182008-07-07 13:35:17 +01004722EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004723
4724/**
Mark Brown1547aba2010-05-07 21:11:40 +01004725 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004726 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01004727 * @pin: audio signal pin endpoint (or start point)
4728 *
4729 * Mark the given endpoint or pin as ignoring suspend. When the
4730 * system is disabled a path between two endpoints flagged as ignoring
4731 * suspend will not be disabled. The path must already be enabled via
4732 * normal means at suspend time, it will not be turned on if it was not
4733 * already enabled.
4734 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004735int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4736 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01004737{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004738 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01004739
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004740 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004741 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004742 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01004743 }
4744
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004745 w->ignore_suspend = 1;
4746
4747 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01004748}
4749EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4750
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02004751/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02004752 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03004753 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02004754 *
4755 * Free all dapm widgets and resources.
4756 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004757void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004758{
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02004759 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004760 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02004761 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004762}
4763EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4764
Kuninori Morimoto95c267d2019-08-23 09:58:52 +09004765void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
4766 struct snd_soc_card *card,
4767 struct snd_soc_component *component)
4768{
4769 dapm->card = card;
4770 dapm->component = component;
4771 dapm->bias_level = SND_SOC_BIAS_OFF;
4772
4773 if (component) {
4774 dapm->dev = component->dev;
4775 dapm->idle_bias_off = !component->driver->idle_bias_on,
4776 dapm->suspend_bias_off = component->driver->suspend_bias_off;
4777 } else {
4778 dapm->dev = card->dev;
4779 }
4780
4781 INIT_LIST_HEAD(&dapm->list);
Kuninori Morimotodf817f82020-03-09 13:08:16 +09004782 /* see for_each_card_dapms */
Kuninori Morimoto95c267d2019-08-23 09:58:52 +09004783 list_add(&dapm->list, &card->dapm_list);
4784}
4785EXPORT_SYMBOL_GPL(snd_soc_dapm_init);
4786
Xiang Xiao57996352014-03-02 00:04:02 +08004787static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01004788{
Liam Girdwood01005a72012-07-06 16:57:05 +01004789 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01004790 struct snd_soc_dapm_widget *w;
4791 LIST_HEAD(down_list);
4792 int powerdown = 0;
4793
Liam Girdwood01005a72012-07-06 16:57:05 +01004794 mutex_lock(&card->dapm_mutex);
4795
Kuninori Morimoto14596692020-03-09 13:08:21 +09004796 for_each_card_widgets(dapm->card, w) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02004797 if (w->dapm != dapm)
4798 continue;
Mark Brown51737472009-06-22 13:16:51 +01004799 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004800 dapm_seq_insert(w, &down_list, false);
Charles Keepax9b319302020-02-28 15:31:45 +00004801 w->new_power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004802 powerdown = 1;
4803 }
4804 }
4805
4806 /* If there were no widgets to power down we're already in
4807 * standby.
4808 */
4809 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004810 if (dapm->bias_level == SND_SOC_BIAS_ON)
4811 snd_soc_dapm_set_bias_level(dapm,
4812 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004813 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004814 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4815 snd_soc_dapm_set_bias_level(dapm,
4816 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004817 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004818
4819 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004820}
Mark Brown51737472009-06-22 13:16:51 +01004821
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004822/*
4823 * snd_soc_dapm_shutdown - callback for system shutdown
4824 */
4825void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4826{
Xiang Xiao57996352014-03-02 00:04:02 +08004827 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004828
Kuninori Morimotodf817f82020-03-09 13:08:16 +09004829 for_each_card_dapms(card, dapm) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08004830 if (dapm != &card->dapm) {
4831 soc_dapm_shutdown_dapm(dapm);
4832 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4833 snd_soc_dapm_set_bias_level(dapm,
4834 SND_SOC_BIAS_OFF);
4835 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004836 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08004837
4838 soc_dapm_shutdown_dapm(&card->dapm);
4839 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4840 snd_soc_dapm_set_bias_level(&card->dapm,
4841 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01004842}
4843
Richard Purdie2b97eab2006-10-06 18:32:18 +02004844/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004845MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004846MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4847MODULE_LICENSE("GPL");