blob: 2d183e2d23de12f2536a972926fc38ee3a8029d6 [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
305 list_for_each_entry(w, &card->widgets, list) {
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
592 list_for_each_entry(w, &card->widgets, list) {
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
687 if (dapm->set_bias_level)
688 ret = dapm->set_bias_level(dapm, level);
689
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 {
805 p->connect = 0;
806 }
807}
808
Mark Brown74b8f952009-06-06 11:26:15 +0100809/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200810static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200811 struct snd_soc_dapm_path *path, const char *control_name)
812{
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800813 int i, nth_path = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200814
815 /* search for mixer kcontrol */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200816 for (i = 0; i < path->sink->num_kcontrols; i++) {
817 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
818 path->name = path->sink->kcontrol_news[i].name;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800819 dapm_set_mixer_path_status(path, i, nth_path++);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200820 return 0;
821 }
822 }
823 return -ENODEV;
824}
825
Stephen Warrenaf468002011-04-28 17:38:01 -0600826static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600827 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600828 const struct snd_kcontrol_new *kcontrol_new,
829 struct snd_kcontrol **kcontrol)
830{
831 struct snd_soc_dapm_widget *w;
832 int i;
833
834 *kcontrol = NULL;
835
836 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600837 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
838 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600839 for (i = 0; i < w->num_kcontrols; i++) {
840 if (&w->kcontrol_news[i] == kcontrol_new) {
841 if (w->kcontrols)
842 *kcontrol = w->kcontrols[i];
843 return 1;
844 }
845 }
846 }
847
848 return 0;
849}
850
Stephen Warren85762e72013-03-29 15:40:10 -0600851/*
852 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
853 * create it. Either way, add the widget into the control's widget list
854 */
Jeeja KP19a2557b2015-10-20 22:30:06 +0530855static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100856 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200857{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200858 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000859 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000860 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600861 size_t prefix_len;
862 int shared;
863 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600864 bool wname_in_long_name, kcname_in_long_name;
Daniel Macke5092c92014-10-07 13:41:24 +0200865 char *long_name = NULL;
Stephen Warren85762e72013-03-29 15:40:10 -0600866 const char *name;
Daniel Macke5092c92014-10-07 13:41:24 +0200867 int ret = 0;
Mark Brownefb7ac32011-03-08 17:23:24 +0000868
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200869 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000870 if (prefix)
871 prefix_len = strlen(prefix) + 1;
872 else
873 prefix_len = 0;
874
Stephen Warren85762e72013-03-29 15:40:10 -0600875 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
876 &kcontrol);
877
Stephen Warren85762e72013-03-29 15:40:10 -0600878 if (!kcontrol) {
879 if (shared) {
880 wname_in_long_name = false;
881 kcname_in_long_name = true;
882 } else {
883 switch (w->id) {
884 case snd_soc_dapm_switch:
885 case snd_soc_dapm_mixer:
Jeeja KP19a2557b2015-10-20 22:30:06 +0530886 case snd_soc_dapm_pga:
Seppo Ingalsuo882c8b4a2019-04-30 18:11:34 -0500887 case snd_soc_dapm_effect:
Chen-Yu Tsaia3930ed2016-08-27 19:28:00 +0800888 case snd_soc_dapm_out_drv:
Stephen Warren85762e72013-03-29 15:40:10 -0600889 wname_in_long_name = true;
890 kcname_in_long_name = true;
891 break;
892 case snd_soc_dapm_mixer_named_ctl:
893 wname_in_long_name = false;
894 kcname_in_long_name = true;
895 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200896 case snd_soc_dapm_demux:
Stephen Warren85762e72013-03-29 15:40:10 -0600897 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600898 wname_in_long_name = true;
899 kcname_in_long_name = false;
900 break;
901 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600902 return -EINVAL;
903 }
904 }
905
906 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600907 /*
908 * The control will get a prefix from the control
909 * creation process but we're also using the same
910 * prefix for widgets so cut the prefix off the
911 * front of the widget name.
912 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200913 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600914 w->name + prefix_len,
915 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200916 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200917 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600918
919 name = long_name;
920 } else if (wname_in_long_name) {
921 long_name = NULL;
922 name = w->name + prefix_len;
923 } else {
924 long_name = NULL;
925 name = w->kcontrol_news[kci].name;
926 }
927
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200928 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600929 prefix);
Daniel Macke5092c92014-10-07 13:41:24 +0200930 if (!kcontrol) {
931 ret = -ENOMEM;
932 goto exit_free;
933 }
934
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200935 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200936
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100937 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200938 if (ret) {
939 snd_ctl_free_one(kcontrol);
Daniel Macke5092c92014-10-07 13:41:24 +0200940 goto exit_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200941 }
942
Stephen Warren85762e72013-03-29 15:40:10 -0600943 ret = snd_ctl_add(card, kcontrol);
944 if (ret < 0) {
945 dev_err(dapm->dev,
946 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
947 w->name, name, ret);
Daniel Macke5092c92014-10-07 13:41:24 +0200948 goto exit_free;
Stephen Warren85762e72013-03-29 15:40:10 -0600949 }
Stephen Warren85762e72013-03-29 15:40:10 -0600950 }
951
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200952 ret = dapm_kcontrol_add_widget(kcontrol, w);
Daniel Macke5092c92014-10-07 13:41:24 +0200953 if (ret == 0)
954 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200955
Daniel Macke5092c92014-10-07 13:41:24 +0200956exit_free:
957 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600958
Daniel Macke5092c92014-10-07 13:41:24 +0200959 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600960}
961
962/* create new dapm mixer control */
963static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
964{
965 int i, ret;
966 struct snd_soc_dapm_path *path;
Charles Keepax561ed682015-05-01 12:37:26 +0100967 struct dapm_kcontrol_data *data;
Stephen Warren85762e72013-03-29 15:40:10 -0600968
Richard Purdie2b97eab2006-10-06 18:32:18 +0200969 /* add kcontrol */
970 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200971 /* match name */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +0200972 snd_soc_dapm_widget_for_each_source_path(w, path) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200973 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600974 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200975 continue;
976
Charles Keepax561ed682015-05-01 12:37:26 +0100977 if (!w->kcontrols[i]) {
Jeeja KP19a2557b2015-10-20 22:30:06 +0530978 ret = dapm_create_or_share_kcontrol(w, i);
Charles Keepax561ed682015-05-01 12:37:26 +0100979 if (ret < 0)
980 return ret;
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200981 }
982
Mark Brown946d92a2013-08-12 23:28:42 +0100983 dapm_kcontrol_add_path(w->kcontrols[i], path);
Charles Keepax561ed682015-05-01 12:37:26 +0100984
985 data = snd_kcontrol_chip(w->kcontrols[i]);
986 if (data->widget)
987 snd_soc_dapm_add_path(data->widget->dapm,
988 data->widget,
989 path->source,
990 NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200991 }
992 }
Stephen Warren85762e72013-03-29 15:40:10 -0600993
994 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200995}
996
997/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200998static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200999{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001000 struct snd_soc_dapm_context *dapm = w->dapm;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001001 enum snd_soc_dapm_direction dir;
Stephen Warren85762e72013-03-29 15:40:10 -06001002 struct snd_soc_dapm_path *path;
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001003 const char *type;
Stephen Warrenaf468002011-04-28 17:38:01 -06001004 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001005
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001006 switch (w->id) {
1007 case snd_soc_dapm_mux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001008 dir = SND_SOC_DAPM_DIR_OUT;
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001009 type = "mux";
1010 break;
1011 case snd_soc_dapm_demux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001012 dir = SND_SOC_DAPM_DIR_IN;
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001013 type = "demux";
1014 break;
1015 default:
1016 return -EINVAL;
1017 }
1018
Stephen Warrenaf468002011-04-28 17:38:01 -06001019 if (w->num_kcontrols != 1) {
1020 dev_err(dapm->dev,
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001021 "ASoC: %s %s has incorrect number of controls\n", type,
Stephen Warrenaf468002011-04-28 17:38:01 -06001022 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001023 return -EINVAL;
1024 }
1025
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001026 if (list_empty(&w->edges[dir])) {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001027 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
Stephen Warren85762e72013-03-29 15:40:10 -06001028 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -06001029 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001030
Jeeja KP19a2557b2015-10-20 22:30:06 +05301031 ret = dapm_create_or_share_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -06001032 if (ret < 0)
1033 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -06001034
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001035 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1036 if (path->name)
1037 dapm_kcontrol_add_path(w->kcontrols[0], path);
Lars-Peter Clausen98407ef2014-10-25 17:41:57 +02001038 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001039
Stephen Warrenaf468002011-04-28 17:38:01 -06001040 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001041}
1042
1043/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001044static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001045{
Jeeja KP19a2557b2015-10-20 22:30:06 +05301046 int i, ret;
1047
1048 for (i = 0; i < w->num_kcontrols; i++) {
1049 ret = dapm_create_or_share_kcontrol(w, i);
1050 if (ret < 0)
1051 return ret;
1052 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001053
Mark Browna6c65732010-03-03 17:45:21 +00001054 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001055}
1056
Nikesh Oswalc6615082015-02-02 17:06:44 +00001057/* create new dapm dai link control */
1058static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1059{
1060 int i, ret;
1061 struct snd_kcontrol *kcontrol;
1062 struct snd_soc_dapm_context *dapm = w->dapm;
1063 struct snd_card *card = dapm->card->snd_card;
Charles Keepax243bcfa2018-09-05 15:21:02 +01001064 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00001065
1066 /* create control for links with > 1 config */
Charles Keepax243bcfa2018-09-05 15:21:02 +01001067 if (rtd->dai_link->num_params <= 1)
Nikesh Oswalc6615082015-02-02 17:06:44 +00001068 return 0;
1069
1070 /* add kcontrol */
1071 for (i = 0; i < w->num_kcontrols; i++) {
1072 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1073 w->name, NULL);
1074 ret = snd_ctl_add(card, kcontrol);
1075 if (ret < 0) {
1076 dev_err(dapm->dev,
1077 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1078 w->name, w->kcontrol_news[i].name, ret);
1079 return ret;
1080 }
1081 kcontrol->private_data = w;
1082 w->kcontrols[i] = kcontrol;
1083 }
1084
1085 return 0;
1086}
1087
Mark Brown9949788b2010-05-07 20:24:05 +01001088/* We implement power down on suspend by checking the power state of
1089 * the ALSA card - when we are suspending the ALSA state for the card
1090 * is set to D3.
1091 */
1092static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1093{
Mark Brown12ea2c72011-03-02 18:17:32 +00001094 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +01001095
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001096 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +01001097 case SNDRV_CTL_POWER_D3hot:
1098 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +01001099 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001100 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001101 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +01001102 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +01001103 default:
1104 return 1;
1105 }
1106}
1107
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001108static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1109 struct list_head *widgets)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001110{
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001111 struct snd_soc_dapm_widget *w;
1112 struct list_head *it;
1113 unsigned int size = 0;
1114 unsigned int i = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001115
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001116 list_for_each(it, widgets)
1117 size++;
1118
Kees Cookacafe7e2018-05-08 13:45:50 -07001119 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001120 if (*list == NULL)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001121 return -ENOMEM;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001122
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001123 list_for_each_entry(w, widgets, work_list)
1124 (*list)->widgets[i++] = w;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001125
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001126 (*list)->num_widgets = i;
1127
1128 return 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001129}
1130
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001131/*
1132 * Common implementation for is_connected_output_ep() and
1133 * is_connected_input_ep(). The function is inlined since the combined size of
1134 * the two specialized functions is only marginally larger then the size of the
1135 * generic function and at the same time the fast path of the specialized
1136 * functions is significantly smaller than the generic function.
1137 */
1138static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1139 struct list_head *list, enum snd_soc_dapm_direction dir,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001140 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1141 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1142 enum snd_soc_dapm_direction)),
1143 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1144 enum snd_soc_dapm_direction))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001145{
1146 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001147 struct snd_soc_dapm_path *path;
1148 int con = 0;
1149
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001150 if (widget->endpoints[dir] >= 0)
1151 return widget->endpoints[dir];
Mark Brown024dc072011-10-09 11:52:05 +01001152
Mark Brownde02d072011-09-20 21:43:24 +01001153 DAPM_UPDATE_STAT(widget, path_checks);
1154
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001155 /* do we need to add this widget to the list ? */
1156 if (list)
1157 list_add_tail(&widget->work_list, list);
1158
Jeeja KP09464972016-06-15 11:16:55 +05301159 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
Charles Keepax8dd26df2019-07-18 09:43:33 +01001160 list = NULL;
1161 custom_stop_condition = NULL;
Jeeja KP09464972016-06-15 11:16:55 +05301162 }
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001163
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001164 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1165 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1166 return widget->endpoints[dir];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001167 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001168
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001169 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
Mark Browne56235e02011-09-21 18:19:14 +01001170 DAPM_UPDATE_STAT(widget, neighbour_checks);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001171
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001172 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +01001173 continue;
1174
Mark Brown8af294b2013-02-22 17:48:15 +00001175 if (path->walking)
1176 return 1;
1177
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001178 trace_snd_soc_dapm_path(widget, dir, path);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001179
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001180 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +00001181 path->walking = 1;
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001182 con += fn(path->node[dir], list, custom_stop_condition);
Mark Brown8af294b2013-02-22 17:48:15 +00001183 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001184 }
1185 }
1186
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001187 widget->endpoints[dir] = con;
Mark Brown024dc072011-10-09 11:52:05 +01001188
Richard Purdie2b97eab2006-10-06 18:32:18 +02001189 return con;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001190}
1191
1192/*
1193 * Recursively check for a completed path to an active or physically connected
1194 * output widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001195 *
1196 * Optionally, can be supplied with a function acting as a stopping condition.
1197 * This function takes the dapm widget currently being examined and the walk
Charles Keepax8dd26df2019-07-18 09:43:33 +01001198 * direction as an arguments, it should return true if widgets from that point
1199 * in the graph onwards should not be added to the widget list.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001200 */
1201static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001202 struct list_head *list,
1203 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1204 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001205{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001206 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001207 is_connected_output_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001208}
1209
1210/*
1211 * Recursively check for a completed path to an active or physically connected
1212 * input widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001213 *
1214 * Optionally, can be supplied with a function acting as a stopping condition.
1215 * This function takes the dapm widget currently being examined and the walk
1216 * direction as an arguments, it should return true if the walk should be
1217 * stopped and false otherwise.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001218 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001219static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001220 struct list_head *list,
1221 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1222 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001223{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001224 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001225 is_connected_input_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001226}
1227
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001228/**
1229 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1230 * @dai: the soc DAI.
1231 * @stream: stream direction.
1232 * @list: list of active widgets for this stream.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001233 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1234 * walk based on custom logic.
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001235 *
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001236 * Queries DAPM graph as to whether a valid audio stream path exists for
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001237 * the initial stream specified by name. This takes into account
1238 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1239 *
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001240 * Optionally, can be supplied with a function acting as a stopping condition.
1241 * This function takes the dapm widget currently being examined and the walk
1242 * direction as an arguments, it should return true if the walk should be
1243 * stopped and false otherwise.
1244 *
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001245 * Returns the number of valid paths or negative error.
1246 */
1247int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001248 struct snd_soc_dapm_widget_list **list,
1249 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1250 enum snd_soc_dapm_direction))
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001251{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01001252 struct snd_soc_card *card = dai->component->card;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001253 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001254 LIST_HEAD(widgets);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001255 int paths;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001256 int ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001257
1258 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001259
1260 /*
1261 * For is_connected_{output,input}_ep fully discover the graph we need
1262 * to reset the cached number of inputs and outputs.
1263 */
1264 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001265 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1266 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001267 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001268
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001269 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001270 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1271 custom_stop_condition);
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001272 else
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001273 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1274 custom_stop_condition);
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001275
1276 /* Drop starting point */
1277 list_del(widgets.next);
1278
1279 ret = dapm_widget_list_create(list, &widgets);
1280 if (ret)
Lars-Peter Clausen30abbe72015-08-11 21:37:59 +02001281 paths = ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001282
1283 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001284 mutex_unlock(&card->dapm_mutex);
1285
1286 return paths;
1287}
1288
Richard Purdie2b97eab2006-10-06 18:32:18 +02001289/*
Mark Brown62ea8742012-01-21 21:14:48 +00001290 * Handler for regulator supply widget.
1291 */
1292int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1293 struct snd_kcontrol *kcontrol, int event)
1294{
Mark Brownc05b84d2012-09-07 12:57:11 +08001295 int ret;
1296
Mark Browneb270e92013-10-09 13:52:52 +01001297 soc_dapm_async_complete(w->dapm);
1298
Mark Brownc05b84d2012-09-07 12:57:11 +08001299 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001300 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001301 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001302 if (ret != 0)
1303 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001304 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001305 w->name, ret);
1306 }
1307
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001308 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001309 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001310 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001311 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001312 if (ret != 0)
1313 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001314 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001315 w->name, ret);
1316 }
1317
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001318 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001319 }
Mark Brown62ea8742012-01-21 21:14:48 +00001320}
1321EXPORT_SYMBOL_GPL(dapm_regulator_event);
1322
Ola Liljad7e7eb92012-05-24 15:26:25 +02001323/*
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00001324 * Handler for pinctrl widget.
1325 */
1326int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1327 struct snd_kcontrol *kcontrol, int event)
1328{
1329 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1330 struct pinctrl *p = w->pinctrl;
1331 struct pinctrl_state *s;
1332
1333 if (!p || !priv)
1334 return -EIO;
1335
1336 if (SND_SOC_DAPM_EVENT_ON(event))
1337 s = pinctrl_lookup_state(p, priv->active_state);
1338 else
1339 s = pinctrl_lookup_state(p, priv->sleep_state);
1340
1341 if (IS_ERR(s))
1342 return PTR_ERR(s);
1343
1344 return pinctrl_select_state(p, s);
1345}
1346EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1347
1348/*
Ola Liljad7e7eb92012-05-24 15:26:25 +02001349 * Handler for clock supply widget.
1350 */
1351int dapm_clock_event(struct snd_soc_dapm_widget *w,
1352 struct snd_kcontrol *kcontrol, int event)
1353{
1354 if (!w->clk)
1355 return -EIO;
1356
Mark Browneb270e92013-10-09 13:52:52 +01001357 soc_dapm_async_complete(w->dapm);
1358
Ola Liljad7e7eb92012-05-24 15:26:25 +02001359 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001360 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001361 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001362 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001363 return 0;
1364 }
Charles Keepaxd78b1e432018-08-28 14:35:02 +01001365
Marek Belisko98b3cf12012-07-12 23:00:16 +02001366 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001367}
1368EXPORT_SYMBOL_GPL(dapm_clock_event);
1369
Mark Brownd805002b2011-09-28 18:28:23 +01001370static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1371{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001372 if (w->power_checked)
1373 return w->new_power;
1374
Mark Brownd805002b2011-09-28 18:28:23 +01001375 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001376 w->new_power = 1;
Mark Brownd805002b2011-09-28 18:28:23 +01001377 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001378 w->new_power = w->power_check(w);
1379
1380 w->power_checked = true;
1381
1382 return w->new_power;
Mark Brownd805002b2011-09-28 18:28:23 +01001383}
1384
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001385/* Generic check to see if a widget should be powered. */
Mark Browncd0f2d42009-04-20 16:56:59 +01001386static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1387{
1388 int in, out;
1389
Mark Brownde02d072011-09-20 21:43:24 +01001390 DAPM_UPDATE_STAT(w, power_checks);
1391
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001392 in = is_connected_input_ep(w, NULL, NULL);
1393 out = is_connected_output_ep(w, NULL, NULL);
Mark Browncd0f2d42009-04-20 16:56:59 +01001394 return out != 0 && in != 0;
1395}
1396
Mark Brown246d0a12009-04-22 18:24:55 +01001397/* Check to see if a power supply is needed */
1398static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1399{
1400 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001401
Mark Brownde02d072011-09-20 21:43:24 +01001402 DAPM_UPDATE_STAT(w, power_checks);
1403
Mark Brown246d0a12009-04-22 18:24:55 +01001404 /* Check if one of our outputs is connected */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001405 snd_soc_dapm_widget_for_each_sink_path(w, path) {
Mark Browna8fdac82011-09-28 18:20:26 +01001406 DAPM_UPDATE_STAT(w, neighbour_checks);
1407
Mark Brownbf3a9e12011-06-13 16:42:29 +01001408 if (path->weak)
1409 continue;
1410
Mark Brown215edda2009-09-08 18:59:05 +01001411 if (path->connected &&
1412 !path->connected(path->source, path->sink))
1413 continue;
1414
Mark Brownf68d7e12011-10-04 22:57:50 +01001415 if (dapm_widget_power_check(path->sink))
1416 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001417 }
1418
Mark Brownf68d7e12011-10-04 22:57:50 +01001419 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001420}
1421
Mark Brown35c64bc2011-09-28 18:23:53 +01001422static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1423{
Charles Keepax20bb0182015-12-02 10:22:16 +00001424 return w->connected;
Mark Brown35c64bc2011-09-28 18:23:53 +01001425}
1426
Mark Brown38357ab2009-06-06 19:03:23 +01001427static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1428 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001429 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001430{
Mark Brown828a8422011-01-15 13:14:30 +00001431 int *sort;
1432
Pierre-Louis Bossartf13d4b52019-02-05 10:22:28 -06001433 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1434 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1435
Mark Brown828a8422011-01-15 13:14:30 +00001436 if (power_up)
1437 sort = dapm_up_seq;
1438 else
1439 sort = dapm_down_seq;
1440
Pierre-Louis Bossartf13d4b52019-02-05 10:22:28 -06001441 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1442 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1443
Mark Brown38357ab2009-06-06 19:03:23 +01001444 if (sort[a->id] != sort[b->id])
1445 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001446 if (a->subseq != b->subseq) {
1447 if (power_up)
1448 return a->subseq - b->subseq;
1449 else
1450 return b->subseq - a->subseq;
1451 }
Mark Brownb22ead22009-06-07 12:51:26 +01001452 if (a->reg != b->reg)
1453 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001454 if (a->dapm != b->dapm)
1455 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001456
Mark Brown38357ab2009-06-06 19:03:23 +01001457 return 0;
1458}
Mark Brown42aa3412009-03-01 19:21:10 +00001459
Mark Brown38357ab2009-06-06 19:03:23 +01001460/* Insert a widget in order into a DAPM power sequence. */
1461static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1462 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001463 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001464{
1465 struct snd_soc_dapm_widget *w;
1466
1467 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001468 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001469 list_add_tail(&new_widget->power_list, &w->power_list);
1470 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001471 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001472
Mark Brown38357ab2009-06-06 19:03:23 +01001473 list_add_tail(&new_widget->power_list, list);
1474}
Mark Brown42aa3412009-03-01 19:21:10 +00001475
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001476static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001477 struct snd_soc_dapm_widget *w, int event)
1478{
Mark Brown68f89ad2010-11-03 23:51:49 -04001479 const char *ev_name;
1480 int power, ret;
1481
1482 switch (event) {
1483 case SND_SOC_DAPM_PRE_PMU:
1484 ev_name = "PRE_PMU";
1485 power = 1;
1486 break;
1487 case SND_SOC_DAPM_POST_PMU:
1488 ev_name = "POST_PMU";
1489 power = 1;
1490 break;
1491 case SND_SOC_DAPM_PRE_PMD:
1492 ev_name = "PRE_PMD";
1493 power = 0;
1494 break;
1495 case SND_SOC_DAPM_POST_PMD:
1496 ev_name = "POST_PMD";
1497 power = 0;
1498 break;
Mark Brown80114122013-02-25 15:14:19 +00001499 case SND_SOC_DAPM_WILL_PMU:
1500 ev_name = "WILL_PMU";
1501 power = 1;
1502 break;
1503 case SND_SOC_DAPM_WILL_PMD:
1504 ev_name = "WILL_PMD";
1505 power = 0;
1506 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001507 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001508 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001509 return;
1510 }
1511
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001512 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001513 return;
1514
1515 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001516 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001517 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001518 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001519 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001520 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001521 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001522 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001523 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001524 ev_name, w->name, ret);
1525 }
1526}
1527
Mark Brownb22ead22009-06-07 12:51:26 +01001528/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001529static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001530 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001531{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001532 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001533 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001534 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001535 unsigned int value = 0;
1536 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001537
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001538 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1539 reg = w->reg;
1540 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001541
1542 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001543 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001544 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001545
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001546 mask |= w->mask << w->shift;
1547 if (w->power)
1548 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001549 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001550 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001551
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001552 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001553 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1554 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001555
Mark Brown68f89ad2010-11-03 23:51:49 -04001556 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001557 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1558 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001559 }
1560
Mark Brown81628102009-06-07 13:21:24 +01001561 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001562 /* Any widget will do, they should all be updating the
1563 * same register.
1564 */
Mark Brown29376bc2011-06-19 13:49:28 +01001565
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001566 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001567 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001568 value, mask, reg, card->pop_time);
1569 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001570 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001571 }
1572
1573 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001574 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1575 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001576 }
Mark Brown42aa3412009-03-01 19:21:10 +00001577}
1578
Mark Brownb22ead22009-06-07 12:51:26 +01001579/* Apply a DAPM power sequence.
1580 *
1581 * We walk over a pre-sorted list of widgets to apply power to. In
1582 * order to minimise the number of writes to the device required
1583 * multiple widgets will be updated in a single write where possible.
1584 * Currently anything that requires more than a single write is not
1585 * handled.
1586 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001587static void dapm_seq_run(struct snd_soc_card *card,
1588 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001589{
1590 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001591 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001592 LIST_HEAD(pending);
1593 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001594 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001595 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001596 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001597 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001598 int *sort;
1599
1600 if (power_up)
1601 sort = dapm_up_seq;
1602 else
1603 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001604
Mark Brownb22ead22009-06-07 12:51:26 +01001605 list_for_each_entry_safe(w, n, list, power_list) {
1606 ret = 0;
1607
1608 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001609 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001610 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001611 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001612 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001613
Mark Brown474b62d2011-01-18 16:14:44 +00001614 if (cur_dapm && cur_dapm->seq_notifier) {
1615 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1616 if (sort[i] == cur_sort)
1617 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001618 i,
1619 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001620 }
1621
Mark Browneb270e92013-10-09 13:52:52 +01001622 if (cur_dapm && w->dapm != cur_dapm)
1623 soc_dapm_async_complete(cur_dapm);
1624
Mark Brownb22ead22009-06-07 12:51:26 +01001625 INIT_LIST_HEAD(&pending);
1626 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001627 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001628 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001629 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001630 }
1631
Mark Brown163cac02009-06-07 10:12:52 +01001632 switch (w->id) {
1633 case snd_soc_dapm_pre:
1634 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001635 list_for_each_entry_safe_continue(w, n, list,
1636 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001637
Mark Brownb22ead22009-06-07 12:51:26 +01001638 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001639 ret = w->event(w,
1640 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001641 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001642 ret = w->event(w,
1643 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001644 break;
1645
1646 case snd_soc_dapm_post:
1647 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001648 list_for_each_entry_safe_continue(w, n, list,
1649 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001650
Mark Brownb22ead22009-06-07 12:51:26 +01001651 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001652 ret = w->event(w,
1653 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001654 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001655 ret = w->event(w,
1656 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001657 break;
1658
Mark Brown163cac02009-06-07 10:12:52 +01001659 default:
Mark Brown81628102009-06-07 13:21:24 +01001660 /* Queue it up for application */
1661 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001662 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001663 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001664 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001665 list_move(&w->power_list, &pending);
1666 break;
Mark Brown163cac02009-06-07 10:12:52 +01001667 }
Mark Brownb22ead22009-06-07 12:51:26 +01001668
1669 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001670 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001671 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001672 }
Mark Brownb22ead22009-06-07 12:51:26 +01001673
1674 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001675 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001676
1677 if (cur_dapm && cur_dapm->seq_notifier) {
1678 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1679 if (sort[i] == cur_sort)
1680 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001681 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001682 }
Mark Browneb270e92013-10-09 13:52:52 +01001683
1684 list_for_each_entry(d, &card->dapm_list, list) {
1685 soc_dapm_async_complete(d);
1686 }
Mark Brown163cac02009-06-07 10:12:52 +01001687}
1688
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001689static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001690{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001691 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001692 struct snd_soc_dapm_widget_list *wlist;
1693 struct snd_soc_dapm_widget *w = NULL;
1694 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001695 int ret;
1696
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001697 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001698 return;
1699
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001700 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001701
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001702 for (wi = 0; wi < wlist->num_widgets; wi++) {
1703 w = wlist->widgets[wi];
1704
1705 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1706 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1707 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001708 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001709 w->name, ret);
1710 }
Mark Brown97404f22010-12-14 16:13:57 +00001711 }
1712
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001713 if (!w)
1714 return;
1715
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001716 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1717 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001718 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001719 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001720 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001721
Chen-Yu Tsaie411b0b2016-11-02 15:35:58 +08001722 if (update->has_second_set) {
1723 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1724 update->mask2, update->val2);
1725 if (ret < 0)
1726 dev_err(w->dapm->dev,
1727 "ASoC: %s DAPM update failed: %d\n",
1728 w->name, ret);
1729 }
1730
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001731 for (wi = 0; wi < wlist->num_widgets; wi++) {
1732 w = wlist->widgets[wi];
1733
1734 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1735 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1736 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001737 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001738 w->name, ret);
1739 }
Mark Brown97404f22010-12-14 16:13:57 +00001740 }
1741}
1742
Mark Brown9d0624a2011-02-18 11:49:43 -08001743/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1744 * they're changing state.
1745 */
1746static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1747{
1748 struct snd_soc_dapm_context *d = data;
1749 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001750
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001751 /* If we're off and we're not supposed to go into STANDBY */
Mark Brown56fba412011-06-04 11:25:10 +01001752 if (d->bias_level == SND_SOC_BIAS_OFF &&
1753 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001754 if (d->dev)
1755 pm_runtime_get_sync(d->dev);
1756
Mark Brown9d0624a2011-02-18 11:49:43 -08001757 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1758 if (ret != 0)
1759 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001760 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001761 }
1762
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001763 /* Prepare for a transition to ON or away from ON */
1764 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1765 d->bias_level != SND_SOC_BIAS_ON) ||
1766 (d->target_bias_level != SND_SOC_BIAS_ON &&
1767 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001768 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1769 if (ret != 0)
1770 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001771 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001772 }
1773}
1774
1775/* Async callback run prior to DAPM sequences - brings to their final
1776 * state.
1777 */
1778static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1779{
1780 struct snd_soc_dapm_context *d = data;
1781 int ret;
1782
1783 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001784 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1785 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1786 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001787 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1788 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001789 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001790 ret);
1791 }
1792
1793 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001794 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1795 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001796 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1797 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001798 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1799 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001800
1801 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001802 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001803 }
1804
1805 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001806 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1807 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001808 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1809 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001810 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001811 ret);
1812 }
1813}
Mark Brown97404f22010-12-14 16:13:57 +00001814
Mark Brownfe4fda52011-10-03 22:36:57 +01001815static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1816 bool power, bool connect)
1817{
1818 /* If a connection is being made or broken then that update
1819 * will have marked the peer dirty, otherwise the widgets are
1820 * not connected and this update has no impact. */
1821 if (!connect)
1822 return;
1823
1824 /* If the peer is already in the state we're moving to then we
1825 * won't have an impact on it. */
1826 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001827 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001828}
1829
Mark Brown05623c42011-09-28 17:02:31 +01001830static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1831 struct list_head *up_list,
1832 struct list_head *down_list)
1833{
Mark Browndb432b42011-10-03 21:06:40 +01001834 struct snd_soc_dapm_path *path;
1835
Mark Brown05623c42011-09-28 17:02:31 +01001836 if (w->power == power)
1837 return;
1838
1839 trace_snd_soc_dapm_widget_power(w, power);
1840
Mark Browndb432b42011-10-03 21:06:40 +01001841 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001842 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001843 */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001844 snd_soc_dapm_widget_for_each_source_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001845 dapm_widget_set_peer_power(path->source, power, path->connect);
1846
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001847 /* Supplies can't affect their outputs, only their inputs */
1848 if (!w->is_supply) {
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001849 snd_soc_dapm_widget_for_each_sink_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001850 dapm_widget_set_peer_power(path->sink, power,
1851 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001852 }
1853
Mark Brown05623c42011-09-28 17:02:31 +01001854 if (power)
1855 dapm_seq_insert(w, up_list, true);
1856 else
1857 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001858}
1859
Mark Brown7c81beb2011-09-20 22:22:32 +01001860static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1861 struct list_head *up_list,
1862 struct list_head *down_list)
1863{
Mark Brown7c81beb2011-09-20 22:22:32 +01001864 int power;
1865
1866 switch (w->id) {
1867 case snd_soc_dapm_pre:
1868 dapm_seq_insert(w, down_list, false);
1869 break;
1870 case snd_soc_dapm_post:
1871 dapm_seq_insert(w, up_list, true);
1872 break;
1873
1874 default:
Mark Brownd805002b2011-09-28 18:28:23 +01001875 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001876
Mark Brown05623c42011-09-28 17:02:31 +01001877 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001878 break;
1879 }
1880}
1881
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001882static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1883{
1884 if (dapm->idle_bias_off)
1885 return true;
1886
1887 switch (snd_power_get_state(dapm->card->snd_card)) {
1888 case SNDRV_CTL_POWER_D3hot:
1889 case SNDRV_CTL_POWER_D3cold:
1890 return dapm->suspend_bias_off;
1891 default:
1892 break;
1893 }
1894
1895 return false;
1896}
1897
Mark Brown42aa3412009-03-01 19:21:10 +00001898/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001899 * Scan each dapm widget for complete audio path.
1900 * A complete path is a route that has valid endpoints i.e.:-
1901 *
1902 * o DAC to output pin.
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001903 * o Input pin to ADC.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001904 * o Input pin to Output pin (bypass, sidetone)
1905 * o DAC to ADC (loopback).
1906 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001907static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001908{
1909 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001910 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001911 LIST_HEAD(up_list);
1912 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001913 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001914 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001915
Mark Brownf9fa2b12014-03-06 16:49:11 +08001916 lockdep_assert_held(&card->dapm_mutex);
1917
Mark Brown84e90932010-11-04 00:07:02 -04001918 trace_snd_soc_dapm_start(card);
1919
Mark Brown56fba412011-06-04 11:25:10 +01001920 list_for_each_entry(d, &card->dapm_list, list) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001921 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001922 d->target_bias_level = SND_SOC_BIAS_OFF;
1923 else
1924 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001925 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001926
Liam Girdwood6c120e12012-02-15 15:15:34 +00001927 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001928
Mark Brown6d3ddc82009-05-16 17:47:29 +01001929 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001930 * lists indicating if they should be powered up or down. We
1931 * only check widgets that have been flagged as dirty but note
1932 * that new widgets may be added to the dirty list while we
1933 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001934 */
Mark Browndb432b42011-10-03 21:06:40 +01001935 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001936 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001937 }
1938
Mark Brownf9de6d72011-09-28 17:19:47 +01001939 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001940 switch (w->id) {
1941 case snd_soc_dapm_pre:
1942 case snd_soc_dapm_post:
1943 /* These widgets always need to be powered */
1944 break;
1945 default:
1946 list_del_init(&w->dirty);
1947 break;
1948 }
Mark Browndb432b42011-10-03 21:06:40 +01001949
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001950 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001951 d = w->dapm;
1952
1953 /* Supplies and micbiases only bring the
1954 * context up to STANDBY as unless something
1955 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001956 * generally don't require full power. Signal
1957 * generators are virtual pins and have no
1958 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001959 */
1960 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001961 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001962 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001963 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001964 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001965 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00001966 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001967 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001968 case snd_soc_dapm_micbias:
1969 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1970 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1971 break;
1972 default:
1973 d->target_bias_level = SND_SOC_BIAS_ON;
1974 break;
1975 }
1976 }
1977
1978 }
1979
Mark Brown85a843c2011-09-21 21:29:47 +01001980 /* Force all contexts in the card to the same bias state if
1981 * they're not ground referenced.
1982 */
Mark Brown56fba412011-06-04 11:25:10 +01001983 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001984 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001985 if (d->target_bias_level > bias)
1986 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001987 list_for_each_entry(d, &card->dapm_list, list)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001988 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01001989 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001990
Mark Brownde02d072011-09-20 21:43:24 +01001991 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001992
Xiang Xiao17282ba2014-03-02 00:04:03 +08001993 /* Run card bias changes at first */
1994 dapm_pre_sequence_async(&card->dapm, 0);
1995 /* Run other bias changes in parallel */
1996 list_for_each_entry(d, &card->dapm_list, list) {
Jon Huntere03546d2018-08-17 16:35:43 +01001997 if (d != &card->dapm && d->bias_level != d->target_bias_level)
Xiang Xiao17282ba2014-03-02 00:04:03 +08001998 async_schedule_domain(dapm_pre_sequence_async, d,
1999 &async_domain);
2000 }
Mark Brown9d0624a2011-02-18 11:49:43 -08002001 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01002002
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02002003 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002004 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00002005 }
2006
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02002007 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002008 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00002009 }
2010
Mark Brown6d3ddc82009-05-16 17:47:29 +01002011 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002012 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01002013
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002014 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00002015
Mark Brown6d3ddc82009-05-16 17:47:29 +01002016 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002017 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002018
Mark Brown9d0624a2011-02-18 11:49:43 -08002019 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08002020 list_for_each_entry(d, &card->dapm_list, list) {
Jon Huntere03546d2018-08-17 16:35:43 +01002021 if (d != &card->dapm && d->bias_level != d->target_bias_level)
Xiang Xiao17282ba2014-03-02 00:04:03 +08002022 async_schedule_domain(dapm_post_sequence_async, d,
2023 &async_domain);
2024 }
Mark Brown9d0624a2011-02-18 11:49:43 -08002025 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08002026 /* Run card bias changes at last */
2027 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01002028
Liam Girdwood8078d872012-02-15 15:15:35 +00002029 /* do we need to notify any clients that DAPM event is complete */
2030 list_for_each_entry(d, &card->dapm_list, list) {
2031 if (d->stream_event)
2032 d->stream_event(d, event);
2033 }
2034
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002035 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02002036 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02002037 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01002038
Mark Brown84e90932010-11-04 00:07:02 -04002039 trace_snd_soc_dapm_done(card);
2040
Mark Brown42aa3412009-03-01 19:21:10 +00002041 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002042}
2043
Mark Brown79fb9382009-08-21 16:38:13 +01002044#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01002045static ssize_t dapm_widget_power_read_file(struct file *file,
2046 char __user *user_buf,
2047 size_t count, loff_t *ppos)
2048{
2049 struct snd_soc_dapm_widget *w = file->private_data;
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002050 struct snd_soc_card *card = w->dapm->card;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002051 enum snd_soc_dapm_direction dir, rdir;
Mark Brown79fb9382009-08-21 16:38:13 +01002052 char *buf;
2053 int in, out;
2054 ssize_t ret;
2055 struct snd_soc_dapm_path *p = NULL;
2056
2057 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2058 if (!buf)
2059 return -ENOMEM;
2060
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002061 mutex_lock(&card->dapm_mutex);
2062
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002063 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2064 if (w->is_supply) {
2065 in = 0;
2066 out = 0;
2067 } else {
Piotr Stankiewicz67420642016-05-13 17:03:55 +01002068 in = is_connected_input_ep(w, NULL, NULL);
2069 out = is_connected_output_ep(w, NULL, NULL);
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002070 }
Mark Brown79fb9382009-08-21 16:38:13 +01002071
Silvio Cesaree581e152019-01-12 16:28:43 +01002072 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
Mark Brownf13ebad2012-03-03 18:01:01 +00002073 w->name, w->power ? "On" : "Off",
2074 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01002075
Mark Brownd033c362009-12-04 15:25:56 +00002076 if (w->reg >= 0)
Silvio Cesaree581e152019-01-12 16:28:43 +01002077 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002078 " - R%d(0x%x) mask 0x%x",
2079 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00002080
Silvio Cesaree581e152019-01-12 16:28:43 +01002081 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
Mark Brownd033c362009-12-04 15:25:56 +00002082
Mark Brown3eef08b2009-09-14 16:49:00 +01002083 if (w->sname)
Silvio Cesaree581e152019-01-12 16:28:43 +01002084 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
Mark Brown3eef08b2009-09-14 16:49:00 +01002085 w->sname,
2086 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01002087
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002088 snd_soc_dapm_for_each_direction(dir) {
2089 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2090 snd_soc_dapm_widget_for_each_path(w, dir, p) {
KaiChieh Chuang28735af2018-02-05 13:00:00 +08002091 if (p->connected && !p->connected(p->source, p->sink))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002092 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002093
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002094 if (!p->connect)
2095 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002096
Silvio Cesaree581e152019-01-12 16:28:43 +01002097 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002098 " %s \"%s\" \"%s\"\n",
2099 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
Mark Brown79fb9382009-08-21 16:38:13 +01002100 p->name ? p->name : "static",
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002101 p->node[rdir]->name);
2102 }
Mark Brown79fb9382009-08-21 16:38:13 +01002103 }
2104
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002105 mutex_unlock(&card->dapm_mutex);
2106
Mark Brown79fb9382009-08-21 16:38:13 +01002107 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2108
2109 kfree(buf);
2110 return ret;
2111}
2112
2113static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002114 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002115 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002116 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002117};
2118
Mark Brownef49e4f2011-04-04 20:48:13 +09002119static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2120 size_t count, loff_t *ppos)
2121{
2122 struct snd_soc_dapm_context *dapm = file->private_data;
2123 char *level;
2124
2125 switch (dapm->bias_level) {
2126 case SND_SOC_BIAS_ON:
2127 level = "On\n";
2128 break;
2129 case SND_SOC_BIAS_PREPARE:
2130 level = "Prepare\n";
2131 break;
2132 case SND_SOC_BIAS_STANDBY:
2133 level = "Standby\n";
2134 break;
2135 case SND_SOC_BIAS_OFF:
2136 level = "Off\n";
2137 break;
2138 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002139 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002140 level = "Unknown\n";
2141 break;
2142 }
2143
2144 return simple_read_from_buffer(user_buf, count, ppos, level,
2145 strlen(level));
2146}
2147
2148static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002149 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002150 .read = dapm_bias_read_file,
2151 .llseek = default_llseek,
2152};
2153
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002154void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2155 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002156{
Mark Brown79fb9382009-08-21 16:38:13 +01002157 struct dentry *d;
2158
Mark Brownceaea852019-06-21 12:33:57 +01002159 if (!parent || IS_ERR(parent))
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02002160 return;
2161
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002162 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2163
Mark Brownceaea852019-06-21 12:33:57 +01002164 if (IS_ERR(dapm->debugfs_dapm)) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002165 dev_warn(dapm->dev,
Mark Brownceaea852019-06-21 12:33:57 +01002166 "ASoC: Failed to create DAPM debugfs directory %ld\n",
2167 PTR_ERR(dapm->debugfs_dapm));
Mark Brown79fb9382009-08-21 16:38:13 +01002168 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002169 }
Mark Brown79fb9382009-08-21 16:38:13 +01002170
Mark Brownef49e4f2011-04-04 20:48:13 +09002171 d = debugfs_create_file("bias_level", 0444,
2172 dapm->debugfs_dapm, dapm,
2173 &dapm_bias_fops);
Mark Brownceaea852019-06-21 12:33:57 +01002174 if (IS_ERR(d))
Mark Brownef49e4f2011-04-04 20:48:13 +09002175 dev_warn(dapm->dev,
Mark Brownceaea852019-06-21 12:33:57 +01002176 "ASoC: Failed to create bias level debugfs file: %ld\n",
2177 PTR_ERR(d));
Mark Brown79fb9382009-08-21 16:38:13 +01002178}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002179
2180static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2181{
2182 struct snd_soc_dapm_context *dapm = w->dapm;
2183 struct dentry *d;
2184
2185 if (!dapm->debugfs_dapm || !w->name)
2186 return;
2187
2188 d = debugfs_create_file(w->name, 0444,
2189 dapm->debugfs_dapm, w,
2190 &dapm_widget_power_fops);
Mark Brownceaea852019-06-21 12:33:57 +01002191 if (IS_ERR(d))
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002192 dev_warn(w->dapm->dev,
Mark Brownceaea852019-06-21 12:33:57 +01002193 "ASoC: Failed to create %s debugfs file: %ld\n",
2194 w->name, PTR_ERR(d));
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002195}
2196
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002197static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2198{
Kuninori Morimoto29040d12019-05-27 16:51:34 +09002199 if (!dapm->debugfs_dapm)
2200 return;
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002201 debugfs_remove_recursive(dapm->debugfs_dapm);
Kuninori Morimoto29040d12019-05-27 16:51:34 +09002202 dapm->debugfs_dapm = NULL;
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002203}
2204
Mark Brown79fb9382009-08-21 16:38:13 +01002205#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002206void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2207 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002208{
2209}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002210
2211static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2212{
2213}
2214
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002215static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2216{
2217}
2218
Mark Brown79fb9382009-08-21 16:38:13 +01002219#endif
2220
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002221/*
2222 * soc_dapm_connect_path() - Connects or disconnects a path
2223 * @path: The path to update
2224 * @connect: The new connect state of the path. True if the path is connected,
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002225 * false if it is disconnected.
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002226 * @reason: The reason why the path changed (for debugging only)
2227 */
2228static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2229 bool connect, const char *reason)
2230{
2231 if (path->connect == connect)
2232 return;
2233
2234 path->connect = connect;
2235 dapm_mark_dirty(path->source, reason);
2236 dapm_mark_dirty(path->sink, reason);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002237 dapm_path_invalidate(path);
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002238}
2239
Richard Purdie2b97eab2006-10-06 18:32:18 +02002240/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002241static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002242 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002243{
2244 struct snd_soc_dapm_path *path;
2245 int found = 0;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002246 bool connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002247
Mark Brownf9fa2b12014-03-06 16:49:11 +08002248 lockdep_assert_held(&card->dapm_mutex);
2249
Richard Purdie2b97eab2006-10-06 18:32:18 +02002250 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002251 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002252 found = 1;
2253 /* we now need to match the string in the enum to the path */
Jerome Brunetc3456a42019-05-15 15:18:55 +02002254 if (e && !(strcmp(path->name, e->texts[mux])))
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002255 connect = true;
2256 else
2257 connect = false;
2258
2259 soc_dapm_connect_path(path, connect, "mux update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002260 }
2261
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002262 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002263 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002264
Liam Girdwood618dae12012-04-25 12:12:51 +01002265 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002266}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002267
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002268int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002269 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2270 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002271{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002272 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002273 int ret;
2274
Liam Girdwood3cd04342012-03-09 12:02:08 +00002275 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002276 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002277 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002278 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002279 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002280 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002281 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002282 return ret;
2283}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002284EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002285
Milan plzik1b075e32008-01-10 14:39:46 +01002286/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002287static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002288 struct snd_kcontrol *kcontrol,
2289 int connect, int rconnect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002290{
2291 struct snd_soc_dapm_path *path;
2292 int found = 0;
2293
Mark Brownf9fa2b12014-03-06 16:49:11 +08002294 lockdep_assert_held(&card->dapm_mutex);
2295
Richard Purdie2b97eab2006-10-06 18:32:18 +02002296 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002297 dapm_kcontrol_for_each_path(path, kcontrol) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002298 /*
2299 * Ideally this function should support any number of
2300 * paths and channels. But since kcontrols only come
2301 * in mono and stereo variants, we are limited to 2
2302 * channels.
2303 *
2304 * The following code assumes for stereo controls the
2305 * first path (when 'found == 0') is the left channel,
2306 * and all remaining paths (when 'found == 1') are the
2307 * right channel.
2308 *
2309 * A stereo control is signified by a valid 'rconnect'
2310 * value, either 0 for unconnected, or >= 0 for connected.
2311 * This is chosen instead of using snd_soc_volsw_is_stereo,
2312 * so that the behavior of snd_soc_dapm_mixer_update_power
2313 * doesn't change even when the kcontrol passed in is
2314 * stereo.
2315 *
2316 * It passes 'connect' as the path connect status for
2317 * the left channel, and 'rconnect' for the right
2318 * channel.
2319 */
2320 if (found && rconnect >= 0)
2321 soc_dapm_connect_path(path, rconnect, "mixer update");
2322 else
2323 soc_dapm_connect_path(path, connect, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002324 found = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002325 }
2326
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002327 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002328 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002329
Liam Girdwood618dae12012-04-25 12:12:51 +01002330 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002331}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002332
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002333int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002334 struct snd_kcontrol *kcontrol, int connect,
2335 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002336{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002337 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002338 int ret;
2339
Liam Girdwood3cd04342012-03-09 12:02:08 +00002340 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002341 card->update = update;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002342 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002343 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002344 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002345 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002346 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002347 return ret;
2348}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002349EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002350
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002351static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2352 char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002353{
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002354 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002355 struct snd_soc_dapm_widget *w;
2356 int count = 0;
2357 char *state = "not set";
2358
Mark Brown47325072016-03-18 12:04:23 +00002359 /* card won't be set for the dummy component, as a spot fix
2360 * we're checking for that case specifically here but in future
2361 * we will ensure that the dummy component looks like others.
2362 */
2363 if (!cmpnt->card)
2364 return 0;
2365
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002366 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2367 if (w->dapm != dapm)
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002368 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002369
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002370 /* only display widgets that burn power */
Richard Purdie2b97eab2006-10-06 18:32:18 +02002371 switch (w->id) {
2372 case snd_soc_dapm_hp:
2373 case snd_soc_dapm_mic:
2374 case snd_soc_dapm_spk:
2375 case snd_soc_dapm_line:
2376 case snd_soc_dapm_micbias:
2377 case snd_soc_dapm_dac:
2378 case snd_soc_dapm_adc:
2379 case snd_soc_dapm_pga:
Seppo Ingalsuo882c8b4a2019-04-30 18:11:34 -05002380 case snd_soc_dapm_effect:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002381 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002382 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002383 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002384 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002385 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00002386 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002387 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002388 if (w->name)
2389 count += sprintf(buf + count, "%s: %s\n",
2390 w->name, w->power ? "On":"Off");
2391 break;
2392 default:
2393 break;
2394 }
2395 }
2396
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002397 switch (snd_soc_dapm_get_bias_level(dapm)) {
Mark Brown0be98982008-05-19 12:31:28 +02002398 case SND_SOC_BIAS_ON:
2399 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002400 break;
Mark Brown0be98982008-05-19 12:31:28 +02002401 case SND_SOC_BIAS_PREPARE:
2402 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002403 break;
Mark Brown0be98982008-05-19 12:31:28 +02002404 case SND_SOC_BIAS_STANDBY:
2405 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002406 break;
Mark Brown0be98982008-05-19 12:31:28 +02002407 case SND_SOC_BIAS_OFF:
2408 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002409 break;
2410 }
2411 count += sprintf(buf + count, "PM State: %s\n", state);
2412
2413 return count;
2414}
2415
Benoit Cousson44ba2642014-07-08 23:19:36 +02002416/* show dapm widget status in sys fs */
2417static ssize_t dapm_widget_show(struct device *dev,
2418 struct device_attribute *attr, char *buf)
2419{
2420 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002421 struct snd_soc_dai *codec_dai;
Benoit Cousson44ba2642014-07-08 23:19:36 +02002422 int i, count = 0;
2423
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002424 mutex_lock(&rtd->card->dapm_mutex);
2425
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002426 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2427 struct snd_soc_component *cmpnt = codec_dai->component;
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002428
2429 count += dapm_widget_show_component(cmpnt, buf + count);
Benoit Cousson44ba2642014-07-08 23:19:36 +02002430 }
2431
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002432 mutex_unlock(&rtd->card->dapm_mutex);
2433
Benoit Cousson44ba2642014-07-08 23:19:36 +02002434 return count;
2435}
2436
Joe Perchesc828a892017-12-19 10:15:08 -08002437static DEVICE_ATTR_RO(dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002438
Takashi Iwaid29697d2015-01-30 20:16:37 +01002439struct attribute *soc_dapm_dev_attrs[] = {
2440 &dev_attr_dapm_widget.attr,
2441 NULL
2442};
Richard Purdie2b97eab2006-10-06 18:32:18 +02002443
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002444static void dapm_free_path(struct snd_soc_dapm_path *path)
2445{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002446 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2447 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002448 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002449 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002450 kfree(path);
2451}
2452
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002453void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2454{
2455 struct snd_soc_dapm_path *p, *next_p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002456 enum snd_soc_dapm_direction dir;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002457
2458 list_del(&w->list);
2459 /*
2460 * remove source and sink paths associated to this widget.
2461 * While removing the path, remove reference to it from both
2462 * source and sink widgets so that path is removed only once.
2463 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002464 snd_soc_dapm_for_each_direction(dir) {
2465 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2466 dapm_free_path(p);
2467 }
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002468
2469 kfree(w->kcontrols);
Lars-Peter Clausen48068962015-07-21 18:11:08 +02002470 kfree_const(w->name);
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -06002471 kfree_const(w->sname);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002472 kfree(w);
2473}
2474
Jyri Sarhafd589a12015-11-10 18:12:42 +02002475void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2476{
2477 dapm->path_sink_cache.widget = NULL;
2478 dapm->path_source_cache.widget = NULL;
2479}
2480
Richard Purdie2b97eab2006-10-06 18:32:18 +02002481/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002482static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002483{
2484 struct snd_soc_dapm_widget *w, *next_w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002485
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002486 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2487 if (w->dapm != dapm)
2488 continue;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002489 snd_soc_dapm_free_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002490 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002491 snd_soc_dapm_reset_cache(dapm);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002492}
2493
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002494static struct snd_soc_dapm_widget *dapm_find_widget(
2495 struct snd_soc_dapm_context *dapm, const char *pin,
2496 bool search_other_contexts)
2497{
2498 struct snd_soc_dapm_widget *w;
2499 struct snd_soc_dapm_widget *fallback = NULL;
2500
2501 list_for_each_entry(w, &dapm->card->widgets, list) {
2502 if (!strcmp(w->name, pin)) {
2503 if (w->dapm == dapm)
2504 return w;
2505 else
2506 fallback = w;
2507 }
2508 }
2509
2510 if (search_other_contexts)
2511 return fallback;
2512
2513 return NULL;
2514}
2515
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002516static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002517 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002518{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002519 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002520
Mark Brownf9fa2b12014-03-06 16:49:11 +08002521 dapm_assert_locked(dapm);
2522
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002523 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002524 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002525 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002526 }
2527
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002528 if (w->connected != status) {
Mark Brown1a8b2d92012-02-16 11:50:07 -08002529 dapm_mark_dirty(w, "pin configuration");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002530 dapm_widget_invalidate_input_paths(w);
2531 dapm_widget_invalidate_output_paths(w);
2532 }
Mark Brown1a8b2d92012-02-16 11:50:07 -08002533
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002534 w->connected = status;
2535 if (status == 0)
2536 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002537
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002538 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002539}
2540
Richard Purdie2b97eab2006-10-06 18:32:18 +02002541/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002542 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2543 * @dapm: DAPM context
2544 *
2545 * Walks all dapm audio paths and powers widgets according to their
2546 * stream or path usage.
2547 *
2548 * Requires external locking.
2549 *
2550 * Returns 0 for success.
2551 */
2552int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2553{
2554 /*
2555 * Suppress early reports (eg, jacks syncing their state) to avoid
2556 * silly DAPM runs during card startup.
2557 */
2558 if (!dapm->card || !dapm->card->instantiated)
2559 return 0;
2560
2561 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2562}
2563EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2564
2565/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002566 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002567 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002568 *
2569 * Walks all dapm audio paths and powers widgets according to their
2570 * stream or path usage.
2571 *
2572 * Returns 0 for success.
2573 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002574int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002575{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002576 int ret;
2577
Liam Girdwood3cd04342012-03-09 12:02:08 +00002578 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002579 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002580 mutex_unlock(&dapm->card->dapm_mutex);
2581 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002582}
Liam Girdwooda5302182008-07-07 13:35:17 +01002583EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002584
Charles Keepax078a85f2019-01-31 13:30:18 +00002585static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2586 struct snd_soc_dapm_widget *w,
2587 int channels)
2588{
2589 switch (w->id) {
2590 case snd_soc_dapm_aif_out:
2591 case snd_soc_dapm_aif_in:
2592 break;
2593 default:
2594 return 0;
2595 }
2596
2597 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2598 w->channel < channels ? "Connecting" : "Disconnecting",
2599 p->source->name, p->sink->name);
2600
2601 if (w->channel < channels)
2602 soc_dapm_connect_path(p, true, "dai update");
2603 else
2604 soc_dapm_connect_path(p, false, "dai update");
2605
2606 return 0;
2607}
2608
2609static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2610 struct snd_pcm_hw_params *params,
2611 struct snd_soc_dai *dai)
2612{
2613 int dir = substream->stream;
2614 int channels = params_channels(params);
2615 struct snd_soc_dapm_path *p;
2616 struct snd_soc_dapm_widget *w;
2617 int ret;
2618
2619 if (dir == SNDRV_PCM_STREAM_PLAYBACK)
2620 w = dai->playback_widget;
2621 else
2622 w = dai->capture_widget;
2623
Charles Keepaxcf17a5f2019-02-06 11:13:59 +00002624 if (!w)
2625 return 0;
2626
Charles Keepax078a85f2019-01-31 13:30:18 +00002627 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2628 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2629
2630 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2631 ret = dapm_update_dai_chan(p, p->sink, channels);
2632 if (ret < 0)
2633 return ret;
2634 }
2635
2636 snd_soc_dapm_widget_for_each_source_path(w, p) {
2637 ret = dapm_update_dai_chan(p, p->source, channels);
2638 if (ret < 0)
2639 return ret;
2640 }
2641
2642 return 0;
2643}
2644
2645int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2646 struct snd_pcm_hw_params *params,
2647 struct snd_soc_dai *dai)
2648{
2649 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2650 int ret;
2651
2652 mutex_lock_nested(&rtd->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2653 ret = dapm_update_dai_unlocked(substream, params, dai);
2654 mutex_unlock(&rtd->card->dapm_mutex);
2655
2656 return ret;
2657}
2658EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2659
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002660/*
2661 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2662 * @w: The widget for which to update the flags
2663 *
2664 * Some widgets have a dynamic category which depends on which neighbors they
2665 * are connected to. This function update the category for these widgets.
2666 *
2667 * This function must be called whenever a path is added or removed to a widget.
2668 */
2669static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2670{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002671 enum snd_soc_dapm_direction dir;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002672 struct snd_soc_dapm_path *p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002673 unsigned int ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002674
2675 switch (w->id) {
2676 case snd_soc_dapm_input:
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002677 /* On a fully routed card an input is never a source */
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002678 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002679 return;
2680 ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002681 snd_soc_dapm_widget_for_each_source_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002682 if (p->source->id == snd_soc_dapm_micbias ||
2683 p->source->id == snd_soc_dapm_mic ||
2684 p->source->id == snd_soc_dapm_line ||
2685 p->source->id == snd_soc_dapm_output) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002686 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002687 break;
2688 }
2689 }
2690 break;
2691 case snd_soc_dapm_output:
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002692 /* On a fully routed card a output is never a sink */
2693 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002694 return;
2695 ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002696 snd_soc_dapm_widget_for_each_sink_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002697 if (p->sink->id == snd_soc_dapm_spk ||
2698 p->sink->id == snd_soc_dapm_hp ||
2699 p->sink->id == snd_soc_dapm_line ||
2700 p->sink->id == snd_soc_dapm_input) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002701 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002702 break;
2703 }
2704 }
2705 break;
2706 case snd_soc_dapm_line:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002707 ep = 0;
2708 snd_soc_dapm_for_each_direction(dir) {
2709 if (!list_empty(&w->edges[dir]))
2710 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2711 }
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002712 break;
2713 default:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002714 return;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002715 }
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002716
2717 w->is_ep = ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002718}
2719
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002720static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2721 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2722 const char *control)
2723{
2724 bool dynamic_source = false;
2725 bool dynamic_sink = false;
2726
2727 if (!control)
2728 return 0;
2729
2730 switch (source->id) {
2731 case snd_soc_dapm_demux:
2732 dynamic_source = true;
2733 break;
2734 default:
2735 break;
2736 }
2737
2738 switch (sink->id) {
2739 case snd_soc_dapm_mux:
2740 case snd_soc_dapm_switch:
2741 case snd_soc_dapm_mixer:
2742 case snd_soc_dapm_mixer_named_ctl:
2743 dynamic_sink = true;
2744 break;
2745 default:
2746 break;
2747 }
2748
2749 if (dynamic_source && dynamic_sink) {
2750 dev_err(dapm->dev,
2751 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2752 source->name, control, sink->name);
2753 return -EINVAL;
2754 } else if (!dynamic_source && !dynamic_sink) {
2755 dev_err(dapm->dev,
2756 "Control not supported for path %s -> [%s] -> %s\n",
2757 source->name, control, sink->name);
2758 return -EINVAL;
2759 }
2760
2761 return 0;
2762}
2763
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002764static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2765 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2766 const char *control,
2767 int (*connected)(struct snd_soc_dapm_widget *source,
2768 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002769{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002770 struct snd_soc_dapm_widget *widgets[2];
2771 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002772 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002773 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002774
Lars-Peter Clausene409dfb2014-10-25 17:42:02 +02002775 if (wsink->is_supply && !wsource->is_supply) {
2776 dev_err(dapm->dev,
2777 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2778 wsource->name, wsink->name);
2779 return -EINVAL;
2780 }
2781
2782 if (connected && !wsource->is_supply) {
2783 dev_err(dapm->dev,
2784 "connected() callback only supported for supply widgets (%s -> %s)\n",
2785 wsource->name, wsink->name);
2786 return -EINVAL;
2787 }
2788
2789 if (wsource->is_supply && control) {
2790 dev_err(dapm->dev,
2791 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2792 wsource->name, control, wsink->name);
2793 return -EINVAL;
2794 }
2795
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002796 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2797 if (ret)
2798 return ret;
2799
Richard Purdie2b97eab2006-10-06 18:32:18 +02002800 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2801 if (!path)
2802 return -ENOMEM;
2803
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002804 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2805 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2806 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2807 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2808
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002809 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002810 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002811 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002812
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002813 if (wsource->is_supply || wsink->is_supply)
2814 path->is_supply = 1;
2815
Richard Purdie2b97eab2006-10-06 18:32:18 +02002816 /* connect static paths */
2817 if (control == NULL) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002818 path->connect = 1;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002819 } else {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002820 switch (wsource->id) {
2821 case snd_soc_dapm_demux:
2822 ret = dapm_connect_mux(dapm, path, control, wsource);
2823 if (ret)
2824 goto err;
2825 break;
2826 default:
2827 break;
2828 }
2829
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002830 switch (wsink->id) {
2831 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002832 ret = dapm_connect_mux(dapm, path, control, wsink);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002833 if (ret != 0)
2834 goto err;
2835 break;
2836 case snd_soc_dapm_switch:
2837 case snd_soc_dapm_mixer:
2838 case snd_soc_dapm_mixer_named_ctl:
2839 ret = dapm_connect_mixer(dapm, path, control);
2840 if (ret != 0)
2841 goto err;
2842 break;
2843 default:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002844 break;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002845 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002846 }
2847
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002848 list_add(&path->list, &dapm->card->paths);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002849 snd_soc_dapm_for_each_direction(dir)
2850 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002851
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002852 snd_soc_dapm_for_each_direction(dir) {
2853 dapm_update_widget_flags(widgets[dir]);
2854 dapm_mark_dirty(widgets[dir], "Route added");
2855 }
Mark Brownfabd0382012-07-05 17:20:06 +01002856
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002857 if (dapm->card->instantiated && path->connect)
2858 dapm_path_invalidate(path);
2859
Richard Purdie2b97eab2006-10-06 18:32:18 +02002860 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002861err:
2862 kfree(path);
2863 return ret;
2864}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002865
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002866static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002867 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002868{
2869 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2870 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2871 const char *sink;
2872 const char *source;
2873 char prefixed_sink[80];
2874 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002875 const char *prefix;
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002876 unsigned int sink_ref = 0;
2877 unsigned int source_ref = 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002878 int ret;
2879
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002880 prefix = soc_dapm_prefix(dapm);
2881 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002882 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002883 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002884 sink = prefixed_sink;
2885 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002886 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002887 source = prefixed_source;
2888 } else {
2889 sink = route->sink;
2890 source = route->source;
2891 }
2892
Charles Keepax45a110a2015-05-11 13:50:30 +01002893 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2894 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2895
2896 if (wsink && wsource)
2897 goto skip_search;
2898
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002899 /*
2900 * find src and dest widgets over all widgets but favor a widget from
2901 * current DAPM context
2902 */
2903 list_for_each_entry(w, &dapm->card->widgets, list) {
2904 if (!wsink && !(strcmp(w->name, sink))) {
2905 wtsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002906 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002907 wsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002908 if (wsource)
2909 break;
2910 }
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002911 sink_ref++;
2912 if (sink_ref > 1)
2913 dev_warn(dapm->dev,
2914 "ASoC: sink widget %s overwritten\n",
2915 w->name);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002916 continue;
2917 }
2918 if (!wsource && !(strcmp(w->name, source))) {
2919 wtsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002920 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002921 wsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002922 if (wsink)
2923 break;
2924 }
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002925 source_ref++;
2926 if (source_ref > 1)
2927 dev_warn(dapm->dev,
2928 "ASoC: source widget %s overwritten\n",
2929 w->name);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002930 }
2931 }
2932 /* use widget from another DAPM context if not found from this */
2933 if (!wsink)
2934 wsink = wtsink;
2935 if (!wsource)
2936 wsource = wtsource;
2937
2938 if (wsource == NULL) {
2939 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2940 route->source);
2941 return -ENODEV;
2942 }
2943 if (wsink == NULL) {
2944 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2945 route->sink);
2946 return -ENODEV;
2947 }
2948
Charles Keepax45a110a2015-05-11 13:50:30 +01002949skip_search:
2950 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2951 dapm_wcache_update(&dapm->path_source_cache, wsource);
2952
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002953 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2954 route->connected);
2955 if (ret)
2956 goto err;
2957
2958 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002959err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002960 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002961 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002962 return ret;
2963}
Mark Brown105f1c22008-05-13 14:52:19 +02002964
Mark Brownefcc3c62012-07-05 17:24:19 +01002965static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2966 const struct snd_soc_dapm_route *route)
2967{
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002968 struct snd_soc_dapm_widget *wsource, *wsink;
Mark Brownefcc3c62012-07-05 17:24:19 +01002969 struct snd_soc_dapm_path *path, *p;
2970 const char *sink;
2971 const char *source;
2972 char prefixed_sink[80];
2973 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002974 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002975
2976 if (route->control) {
2977 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002978 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002979 return -EINVAL;
2980 }
2981
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002982 prefix = soc_dapm_prefix(dapm);
2983 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002984 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002985 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002986 sink = prefixed_sink;
2987 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002988 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002989 source = prefixed_source;
2990 } else {
2991 sink = route->sink;
2992 source = route->source;
2993 }
2994
2995 path = NULL;
2996 list_for_each_entry(p, &dapm->card->paths, list) {
2997 if (strcmp(p->source->name, source) != 0)
2998 continue;
2999 if (strcmp(p->sink->name, sink) != 0)
3000 continue;
3001 path = p;
3002 break;
3003 }
3004
3005 if (path) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003006 wsource = path->source;
3007 wsink = path->sink;
3008
3009 dapm_mark_dirty(wsource, "Route removed");
3010 dapm_mark_dirty(wsink, "Route removed");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003011 if (path->connect)
3012 dapm_path_invalidate(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01003013
Lars-Peter Clausen88722932013-06-14 13:16:53 +02003014 dapm_free_path(path);
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003015
3016 /* Update any path related flags */
3017 dapm_update_widget_flags(wsource);
3018 dapm_update_widget_flags(wsink);
Mark Brownefcc3c62012-07-05 17:24:19 +01003019 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003020 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01003021 source, sink);
3022 }
3023
3024 return 0;
3025}
3026
Mark Brown105f1c22008-05-13 14:52:19 +02003027/**
Mark Brown105f1c22008-05-13 14:52:19 +02003028 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003029 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02003030 * @route: audio routes
3031 * @num: number of routes
3032 *
3033 * Connects 2 dapm widgets together via a named audio path. The sink is
3034 * the widget receiving the audio signal, whilst the source is the sender
3035 * of the audio signal.
3036 *
3037 * Returns 0 for success else error. On error all resources can be freed
3038 * with a call to snd_soc_card_free().
3039 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003040int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02003041 const struct snd_soc_dapm_route *route, int num)
3042{
Mark Brown62d4a4b2012-06-22 12:21:49 +01003043 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02003044
Stuart Hendersonf19c1812017-09-21 11:02:12 +01003045 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown105f1c22008-05-13 14:52:19 +02003046 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02003047 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01003048 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003049 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
3050 route->source,
3051 route->control ? route->control : "direct",
3052 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01003053 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02003054 }
3055 route++;
3056 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003057 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02003058
Dan Carpenter60884c22012-04-13 22:25:43 +03003059 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02003060}
3061EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3062
Mark Brownefcc3c62012-07-05 17:24:19 +01003063/**
3064 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3065 * @dapm: DAPM context
3066 * @route: audio routes
3067 * @num: number of routes
3068 *
3069 * Removes routes from the DAPM context.
3070 */
3071int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3072 const struct snd_soc_dapm_route *route, int num)
3073{
Rajan Vajae066ea22016-02-11 11:23:35 +05303074 int i;
Mark Brownefcc3c62012-07-05 17:24:19 +01003075
Stuart Hendersonf19c1812017-09-21 11:02:12 +01003076 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownefcc3c62012-07-05 17:24:19 +01003077 for (i = 0; i < num; i++) {
3078 snd_soc_dapm_del_route(dapm, route);
3079 route++;
3080 }
3081 mutex_unlock(&dapm->card->dapm_mutex);
3082
Rajan Vajae066ea22016-02-11 11:23:35 +05303083 return 0;
Mark Brownefcc3c62012-07-05 17:24:19 +01003084}
3085EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3086
Mark Brownbf3a9e12011-06-13 16:42:29 +01003087static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3088 const struct snd_soc_dapm_route *route)
3089{
3090 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3091 route->source,
3092 true);
3093 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3094 route->sink,
3095 true);
3096 struct snd_soc_dapm_path *path;
3097 int count = 0;
3098
3099 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003100 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003101 route->source);
3102 return -ENODEV;
3103 }
3104
3105 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003106 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003107 route->sink);
3108 return -ENODEV;
3109 }
3110
3111 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003112 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003113 route->source, route->sink);
3114
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02003115 snd_soc_dapm_widget_for_each_sink_path(source, path) {
Mark Brownbf3a9e12011-06-13 16:42:29 +01003116 if (path->sink == sink) {
3117 path->weak = 1;
3118 count++;
3119 }
3120 }
3121
3122 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003123 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003124 route->source, route->sink);
3125 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003126 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003127 count, route->source, route->sink);
3128
3129 return 0;
3130}
3131
3132/**
3133 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3134 * @dapm: DAPM context
3135 * @route: audio routes
3136 * @num: number of routes
3137 *
3138 * Mark existing routes matching those specified in the passed array
3139 * as being weak, meaning that they are ignored for the purpose of
3140 * power decisions. The main intended use case is for sidetone paths
3141 * which couple audio between other independent paths if they are both
3142 * active in order to make the combination work better at the user
3143 * level but which aren't intended to be "used".
3144 *
3145 * Note that CODEC drivers should not use this as sidetone type paths
3146 * can frequently also be used as bypass paths.
3147 */
3148int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3149 const struct snd_soc_dapm_route *route, int num)
3150{
3151 int i, err;
3152 int ret = 0;
3153
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003154 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01003155 for (i = 0; i < num; i++) {
3156 err = snd_soc_dapm_weak_route(dapm, route);
3157 if (err)
3158 ret = err;
3159 route++;
3160 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003161 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01003162
3163 return ret;
3164}
3165EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3166
Mark Brown105f1c22008-05-13 14:52:19 +02003167/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003168 * snd_soc_dapm_new_widgets - add new dapm widgets
Jonathan Corbet628536e2015-08-25 01:14:48 -06003169 * @card: card to be checked for new dapm widgets
Richard Purdie2b97eab2006-10-06 18:32:18 +02003170 *
3171 * Checks the codec for any new dapm widgets and creates them if found.
3172 *
3173 * Returns 0 for success.
3174 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02003175int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003176{
3177 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00003178 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003179
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003180 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003181
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003182 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003183 {
3184 if (w->new)
3185 continue;
3186
Stephen Warrenfad59882011-04-28 17:37:59 -06003187 if (w->num_kcontrols) {
Kees Cook6396bb22018-06-12 14:03:40 -07003188 w->kcontrols = kcalloc(w->num_kcontrols,
Stephen Warrenfad59882011-04-28 17:37:59 -06003189 sizeof(struct snd_kcontrol *),
3190 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003191 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003192 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06003193 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003194 }
Stephen Warrenfad59882011-04-28 17:37:59 -06003195 }
3196
Richard Purdie2b97eab2006-10-06 18:32:18 +02003197 switch(w->id) {
3198 case snd_soc_dapm_switch:
3199 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00003200 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003201 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003202 break;
3203 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003204 case snd_soc_dapm_demux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003205 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003206 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003207 case snd_soc_dapm_pga:
Seppo Ingalsuo882c8b4a2019-04-30 18:11:34 -05003208 case snd_soc_dapm_effect:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06003209 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003210 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003211 break;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003212 case snd_soc_dapm_dai_link:
3213 dapm_new_dai_link(w);
3214 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01003215 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003216 break;
3217 }
Mark Brownb66a70d2011-02-09 18:04:11 +00003218
3219 /* Read the initial power state from the device */
3220 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003221 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08003222 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003223 val &= w->mask;
3224 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00003225 w->power = 1;
3226 }
3227
Richard Purdie2b97eab2006-10-06 18:32:18 +02003228 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003229
Mark Brown7508b122011-10-05 12:09:12 +01003230 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003231 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003232 }
3233
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003234 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3235 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003236 return 0;
3237}
3238EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3239
3240/**
3241 * snd_soc_dapm_get_volsw - dapm mixer get callback
3242 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003243 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003244 *
3245 * Callback to get the value of a dapm mixer control.
3246 *
3247 * Returns 0 for success.
3248 */
3249int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3250 struct snd_ctl_elem_value *ucontrol)
3251{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003252 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3253 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003254 struct soc_mixer_control *mc =
3255 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003256 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003257 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003258 int max = mc->max;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003259 unsigned int width = fls(max);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003260 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003261 unsigned int invert = mc->invert;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003262 unsigned int reg_val, val, rval = 0;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003263 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003264
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003265 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003266 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003267 ret = soc_dapm_read(dapm, reg, &reg_val);
3268 val = (reg_val >> shift) & mask;
3269
3270 if (ret == 0 && reg != mc->rreg)
3271 ret = soc_dapm_read(dapm, mc->rreg, &reg_val);
3272
3273 if (snd_soc_volsw_is_stereo(mc))
3274 rval = (reg_val >> mc->rshift) & mask;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003275 } else {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003276 reg_val = dapm_kcontrol_get_value(kcontrol);
3277 val = reg_val & mask;
3278
3279 if (snd_soc_volsw_is_stereo(mc))
3280 rval = (reg_val >> width) & mask;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003281 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003282 mutex_unlock(&card->dapm_mutex);
3283
Chen-Yu Tsai01ad5e72016-08-27 19:27:58 +08003284 if (ret)
3285 return ret;
3286
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003287 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003288 ucontrol->value.integer.value[0] = max - val;
3289 else
3290 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003291
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003292 if (snd_soc_volsw_is_stereo(mc)) {
3293 if (invert)
3294 ucontrol->value.integer.value[1] = max - rval;
3295 else
3296 ucontrol->value.integer.value[1] = rval;
3297 }
3298
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003299 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003300}
3301EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3302
3303/**
3304 * snd_soc_dapm_put_volsw - dapm mixer set callback
3305 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003306 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003307 *
3308 * Callback to set the value of a dapm mixer control.
3309 *
3310 * Returns 0 for success.
3311 */
3312int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3313 struct snd_ctl_elem_value *ucontrol)
3314{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003315 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3316 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003317 struct soc_mixer_control *mc =
3318 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003319 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003320 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003321 int max = mc->max;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003322 unsigned int width = fls(max);
3323 unsigned int mask = (1 << width) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003324 unsigned int invert = mc->invert;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003325 unsigned int val, rval = 0;
3326 int connect, rconnect = -1, change, reg_change = 0;
Fabio Estevam33d92452018-02-14 13:39:05 -02003327 struct snd_soc_dapm_update update = {};
Nenghua Cao52765972013-12-13 20:13:49 +08003328 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003329
3330 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02003331 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003332
3333 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003334 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003335
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003336 if (snd_soc_volsw_is_stereo(mc)) {
3337 rval = (ucontrol->value.integer.value[1] & mask);
3338 rconnect = !!rval;
3339 if (invert)
3340 rval = max - rval;
3341 }
3342
Liam Girdwood3cd04342012-03-09 12:02:08 +00003343 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003344
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003345 /* This assumes field width < (bits in unsigned int / 2) */
3346 if (width > sizeof(unsigned int) * 8 / 2)
3347 dev_warn(dapm->dev,
3348 "ASoC: control %s field width limit exceeded\n",
3349 kcontrol->id.name);
3350 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
Mark Brown283375c2009-12-07 18:09:03 +00003351
Jarkko Nikula18626c72014-06-09 14:20:29 +03003352 if (reg != SND_SOC_NOPM) {
Jarkko Nikula18626c72014-06-09 14:20:29 +03003353 val = val << shift;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003354 rval = rval << mc->rshift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02003355
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003356 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3357
3358 if (snd_soc_volsw_is_stereo(mc))
3359 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3360 mask << mc->rshift,
3361 rval);
Jarkko Nikula18626c72014-06-09 14:20:29 +03003362 }
3363
3364 if (change || reg_change) {
3365 if (reg_change) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003366 if (snd_soc_volsw_is_stereo(mc)) {
3367 update.has_second_set = true;
3368 update.reg2 = mc->rreg;
3369 update.mask2 = mask << mc->rshift;
3370 update.val2 = rval;
3371 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003372 update.kcontrol = kcontrol;
3373 update.reg = reg;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003374 update.mask = mask << shift;
Jarkko Nikula18626c72014-06-09 14:20:29 +03003375 update.val = val;
3376 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003377 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003378 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00003379
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003380 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3381 rconnect);
Mark Brown97404f22010-12-14 16:13:57 +00003382
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003383 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00003384 }
3385
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003386 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003387
3388 if (ret > 0)
3389 soc_dpcm_runtime_update(card);
3390
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003391 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003392}
3393EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3394
3395/**
3396 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3397 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003398 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003399 *
3400 * Callback to get the value of a dapm enumerated double mixer control.
3401 *
3402 * Returns 0 for success.
3403 */
3404int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3405 struct snd_ctl_elem_value *ucontrol)
3406{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003407 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Charles Keepax561ed682015-05-01 12:37:26 +01003408 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003409 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003410 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003411
Charles Keepax561ed682015-05-01 12:37:26 +01003412 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3413 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003414 int ret = soc_dapm_read(dapm, e->reg, &reg_val);
Charles Keepax964a0b82015-05-08 10:50:10 +01003415 if (ret) {
3416 mutex_unlock(&card->dapm_mutex);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003417 return ret;
Charles Keepax964a0b82015-05-08 10:50:10 +01003418 }
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003419 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003420 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003421 }
Charles Keepax561ed682015-05-01 12:37:26 +01003422 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003423
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003424 val = (reg_val >> e->shift_l) & e->mask;
3425 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3426 if (e->shift_l != e->shift_r) {
3427 val = (reg_val >> e->shift_r) & e->mask;
3428 val = snd_soc_enum_val_to_item(e, val);
3429 ucontrol->value.enumerated.item[1] = val;
3430 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003431
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003432 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003433}
3434EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3435
3436/**
3437 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3438 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003439 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003440 *
3441 * Callback to set the value of a dapm enumerated double mixer control.
3442 *
3443 * Returns 0 for success.
3444 */
3445int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3446 struct snd_ctl_elem_value *ucontrol)
3447{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003448 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3449 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003450 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003451 unsigned int *item = ucontrol->value.enumerated.item;
Charles Keepax561ed682015-05-01 12:37:26 +01003452 unsigned int val, change, reg_change = 0;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003453 unsigned int mask;
Fabio Estevam33d92452018-02-14 13:39:05 -02003454 struct snd_soc_dapm_update update = {};
Nenghua Cao52765972013-12-13 20:13:49 +08003455 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003456
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003457 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003458 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003459
3460 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003461 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003462 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003463 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003464 return -EINVAL;
Chen-Yu Tsai071133a2016-08-27 19:27:59 +08003465 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003466 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003467 }
3468
Liam Girdwood3cd04342012-03-09 12:02:08 +00003469 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003470
Charles Keepax561ed682015-05-01 12:37:26 +01003471 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00003472
Charles Keepax561ed682015-05-01 12:37:26 +01003473 if (e->reg != SND_SOC_NOPM)
3474 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3475
3476 if (change || reg_change) {
3477 if (reg_change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003478 update.kcontrol = kcontrol;
3479 update.reg = e->reg;
3480 update.mask = mask;
3481 update.val = val;
3482 card->update = &update;
3483 }
Charles Keepax561ed682015-05-01 12:37:26 +01003484 change |= reg_change;
Mark Brown3a655772009-10-05 17:23:30 +01003485
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003486 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003487
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003488 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003489 }
3490
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003491 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003492
3493 if (ret > 0)
3494 soc_dpcm_runtime_update(card);
3495
Mark Brown97404f22010-12-14 16:13:57 +00003496 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003497}
3498EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3499
3500/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003501 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3502 *
3503 * @kcontrol: mixer control
3504 * @uinfo: control element information
3505 *
3506 * Callback to provide information about a pin switch control.
3507 */
3508int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3509 struct snd_ctl_elem_info *uinfo)
3510{
3511 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3512 uinfo->count = 1;
3513 uinfo->value.integer.min = 0;
3514 uinfo->value.integer.max = 1;
3515
3516 return 0;
3517}
3518EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3519
3520/**
3521 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3522 *
3523 * @kcontrol: mixer control
3524 * @ucontrol: Value
3525 */
3526int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3527 struct snd_ctl_elem_value *ucontrol)
3528{
Mark Brown48a8c392012-02-14 17:11:15 -08003529 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003530 const char *pin = (const char *)kcontrol->private_value;
3531
Liam Girdwood3cd04342012-03-09 12:02:08 +00003532 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003533
3534 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003535 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003536
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003537 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003538
3539 return 0;
3540}
3541EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3542
3543/**
3544 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3545 *
3546 * @kcontrol: mixer control
3547 * @ucontrol: Value
3548 */
3549int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3550 struct snd_ctl_elem_value *ucontrol)
3551{
Mark Brown48a8c392012-02-14 17:11:15 -08003552 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003553 const char *pin = (const char *)kcontrol->private_value;
3554
Mark Brown8b37dbd2009-02-28 21:14:20 +00003555 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003556 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003557 else
Mark Brown48a8c392012-02-14 17:11:15 -08003558 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003559
Mark Brown48a8c392012-02-14 17:11:15 -08003560 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003561 return 0;
3562}
3563EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3564
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +01003565struct snd_soc_dapm_widget *
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003566snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003567 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003568{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003569 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003570 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003571 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00003572 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003573
3574 if ((w = dapm_cnew_widget(widget)) == NULL)
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003575 return ERR_PTR(-ENOMEM);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003576
Mark Brown62ea8742012-01-21 21:14:48 +00003577 switch (w->id) {
3578 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003579 w->regulator = devm_regulator_get(dapm->dev, w->name);
3580 if (IS_ERR(w->regulator)) {
3581 ret = PTR_ERR(w->regulator);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003582 goto request_failed;
Mark Brown62ea8742012-01-21 21:14:48 +00003583 }
Mark Brown8784c772013-01-10 19:33:47 +00003584
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003585 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003586 ret = regulator_allow_bypass(w->regulator, true);
3587 if (ret != 0)
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003588 dev_warn(dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003589 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003590 w->name, ret);
3591 }
Mark Brown62ea8742012-01-21 21:14:48 +00003592 break;
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003593 case snd_soc_dapm_pinctrl:
3594 w->pinctrl = devm_pinctrl_get(dapm->dev);
Charles Keepaxa5cd7e92018-08-28 14:35:03 +01003595 if (IS_ERR(w->pinctrl)) {
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003596 ret = PTR_ERR(w->pinctrl);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003597 goto request_failed;
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003598 }
3599 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003600 case snd_soc_dapm_clock_supply:
Mark Brown695594f12012-06-04 08:14:13 +01003601 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003602 if (IS_ERR(w->clk)) {
3603 ret = PTR_ERR(w->clk);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003604 goto request_failed;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003605 }
3606 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003607 default:
3608 break;
3609 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003610
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003611 prefix = soc_dapm_prefix(dapm);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003612 if (prefix)
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003613 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003614 else
Lars-Peter Clausen48068962015-07-21 18:11:08 +02003615 w->name = kstrdup_const(widget->name, GFP_KERNEL);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003616 if (w->name == NULL) {
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -06003617 kfree_const(w->sname);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003618 kfree(w);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003619 return ERR_PTR(-ENOMEM);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003620 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003621
Mark Brown7ca3a182011-10-08 14:04:50 +01003622 switch (w->id) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003623 case snd_soc_dapm_mic:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003624 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003625 w->power_check = dapm_generic_check_power;
3626 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003627 case snd_soc_dapm_input:
3628 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003629 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003630 w->power_check = dapm_generic_check_power;
3631 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003632 case snd_soc_dapm_spk:
3633 case snd_soc_dapm_hp:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003634 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003635 w->power_check = dapm_generic_check_power;
3636 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003637 case snd_soc_dapm_output:
3638 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003639 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003640 w->power_check = dapm_generic_check_power;
3641 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003642 case snd_soc_dapm_vmid:
3643 case snd_soc_dapm_siggen:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003644 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003645 w->power_check = dapm_always_on_check_power;
3646 break;
Vinod Koul56b44372015-11-23 21:22:30 +05303647 case snd_soc_dapm_sink:
3648 w->is_ep = SND_SOC_DAPM_EP_SINK;
3649 w->power_check = dapm_always_on_check_power;
3650 break;
3651
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003652 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003653 case snd_soc_dapm_demux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003654 case snd_soc_dapm_switch:
3655 case snd_soc_dapm_mixer:
3656 case snd_soc_dapm_mixer_named_ctl:
Mark Brown63c69a62013-07-18 22:03:01 +01003657 case snd_soc_dapm_adc:
3658 case snd_soc_dapm_aif_out:
3659 case snd_soc_dapm_dac:
3660 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003661 case snd_soc_dapm_pga:
Ranjani Sridharan6e3bfcf2019-03-01 19:08:53 -06003662 case snd_soc_dapm_buffer:
3663 case snd_soc_dapm_scheduler:
3664 case snd_soc_dapm_effect:
3665 case snd_soc_dapm_src:
3666 case snd_soc_dapm_asrc:
3667 case snd_soc_dapm_encoder:
3668 case snd_soc_dapm_decoder:
Mark Brown7ca3a182011-10-08 14:04:50 +01003669 case snd_soc_dapm_out_drv:
Mark Brown7ca3a182011-10-08 14:04:50 +01003670 case snd_soc_dapm_micbias:
Mark Brown7ca3a182011-10-08 14:04:50 +01003671 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003672 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02003673 case snd_soc_dapm_dai_out:
3674 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003675 w->power_check = dapm_generic_check_power;
3676 break;
3677 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003678 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003679 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003680 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003681 case snd_soc_dapm_kcontrol:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003682 w->is_supply = 1;
Mark Brown7ca3a182011-10-08 14:04:50 +01003683 w->power_check = dapm_supply_check_power;
3684 break;
3685 default:
3686 w->power_check = dapm_always_on_check_power;
3687 break;
3688 }
3689
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003690 w->dapm = dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003691 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003692 INIT_LIST_HEAD(&w->dirty);
Lars-Peter Clausen92fa1242015-05-01 18:02:42 +02003693 list_add_tail(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003694
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003695 snd_soc_dapm_for_each_direction(dir) {
3696 INIT_LIST_HEAD(&w->edges[dir]);
3697 w->endpoints[dir] = -1;
3698 }
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003699
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02003700 /* machine layer sets up unconnected pins and insertions */
Richard Purdie2b97eab2006-10-06 18:32:18 +02003701 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003702 return w;
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003703
3704request_failed:
3705 if (ret != -EPROBE_DEFER)
3706 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3707 w->name, ret);
3708
Wenwen Wang45004d62019-07-22 08:57:44 -05003709 kfree_const(w->sname);
3710 kfree(w);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003711 return ERR_PTR(ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003712}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003713
3714/**
Charles Keepax94e630a2018-09-05 15:20:59 +01003715 * snd_soc_dapm_new_control - create new dapm control
3716 * @dapm: DAPM context
3717 * @widget: widget template
3718 *
3719 * Creates new DAPM control based upon a template.
3720 *
3721 * Returns a widget pointer on success or an error pointer on failure
3722 */
3723struct snd_soc_dapm_widget *
3724snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3725 const struct snd_soc_dapm_widget *widget)
3726{
3727 struct snd_soc_dapm_widget *w;
3728
3729 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3730 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3731 mutex_unlock(&dapm->card->dapm_mutex);
3732
3733 return w;
3734}
3735EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3736
3737/**
Mark Brown4ba13272008-05-13 14:51:19 +02003738 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003739 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003740 * @widget: widget array
3741 * @num: number of widgets
3742 *
3743 * Creates new DAPM controls based upon the templates.
3744 *
3745 * Returns 0 for success else error.
3746 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003747int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003748 const struct snd_soc_dapm_widget *widget,
3749 int num)
3750{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003751 struct snd_soc_dapm_widget *w;
3752 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003753 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003754
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003755 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003756 for (i = 0; i < num; i++) {
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003757 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
Linus Walleij37e1df82017-01-13 10:23:52 +01003758 if (IS_ERR(w)) {
3759 ret = PTR_ERR(w);
Dan Carpenter60884c22012-04-13 22:25:43 +03003760 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003761 }
Mark Brown4ba13272008-05-13 14:51:19 +02003762 widget++;
3763 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003764 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003765 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003766}
3767EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3768
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003769static int
3770snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3771 struct snd_pcm_substream *substream)
Mark Brownc74184e2012-04-04 22:12:09 +01003772{
Charles Keepax4a75aae2018-09-05 15:21:01 +01003773 struct snd_soc_dapm_path *path;
Mark Brownc74184e2012-04-04 22:12:09 +01003774 struct snd_soc_dai *source, *sink;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003775 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Mark Brown9747cec2012-04-26 19:12:21 +01003776 struct snd_pcm_hw_params *params = NULL;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003777 const struct snd_soc_pcm_stream *config = NULL;
Takashi Iwai3ba66fe2018-07-25 22:43:26 +02003778 unsigned int fmt;
Colin Ian Kingc8415832019-07-26 13:33:27 +01003779 int ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003780
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003781 params = kzalloc(sizeof(*params), GFP_KERNEL);
3782 if (!params)
3783 return -ENOMEM;
3784
3785 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3786 snd_soc_dapm_widget_for_each_source_path(w, path) {
3787 source = path->source->priv;
3788
3789 ret = snd_soc_dai_startup(source, substream);
3790 if (ret < 0) {
3791 dev_err(source->dev,
3792 "ASoC: startup() failed: %d\n", ret);
3793 goto out;
3794 }
3795 source->active++;
3796 }
3797
3798 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3799 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3800 sink = path->sink->priv;
3801
3802 ret = snd_soc_dai_startup(sink, substream);
3803 if (ret < 0) {
3804 dev_err(sink->dev,
3805 "ASoC: startup() failed: %d\n", ret);
3806 goto out;
3807 }
3808 sink->active++;
3809 }
3810
3811 /*
3812 * Note: getting the config after .startup() gives a chance to
3813 * either party on the link to alter the configuration if
3814 * necessary
3815 */
Charles Keepax243bcfa2018-09-05 15:21:02 +01003816 config = rtd->dai_link->params + rtd->params_select;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003817 if (WARN_ON(!config)) {
3818 dev_err(w->dapm->dev, "ASoC: link config missing\n");
3819 ret = -EINVAL;
3820 goto out;
3821 }
Mark Brownc74184e2012-04-04 22:12:09 +01003822
Mark Brownc74184e2012-04-04 22:12:09 +01003823 /* Be a little careful as we don't want to overflow the mask array */
3824 if (config->formats) {
3825 fmt = ffs(config->formats) - 1;
3826 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003827 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003828 config->formats);
Mark Brownc74184e2012-04-04 22:12:09 +01003829
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003830 ret = -EINVAL;
Mark Brown9747cec2012-04-26 19:12:21 +01003831 goto out;
3832 }
Mark Brownc74184e2012-04-04 22:12:09 +01003833
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003834 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brown9747cec2012-04-26 19:12:21 +01003835 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003836 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003837 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003838 config->rate_max;
Mark Brown9747cec2012-04-26 19:12:21 +01003839 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003840 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003841 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003842 = config->channels_max;
3843
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003844 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3845 snd_soc_dapm_widget_for_each_source_path(w, path) {
3846 source = path->source->priv;
3847
3848 ret = snd_soc_dai_hw_params(source, substream, params);
3849 if (ret < 0)
3850 goto out;
3851
3852 dapm_update_dai_unlocked(substream, params, source);
3853 }
3854
3855 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3856 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3857 sink = path->sink->priv;
3858
3859 ret = snd_soc_dai_hw_params(sink, substream, params);
3860 if (ret < 0)
3861 goto out;
3862
3863 dapm_update_dai_unlocked(substream, params, sink);
3864 }
3865
3866out:
3867 kfree(params);
Colin Ian Kingc8415832019-07-26 13:33:27 +01003868 return ret;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003869}
3870
3871static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3872 struct snd_kcontrol *kcontrol, int event)
3873{
3874 struct snd_soc_dapm_path *path;
3875 struct snd_soc_dai *source, *sink;
3876 struct snd_soc_pcm_runtime *rtd = w->priv;
3877 struct snd_pcm_substream substream;
3878 struct snd_pcm_runtime *runtime = NULL;
3879 int ret = 0;
3880
3881 if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3882 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3883 return -EINVAL;
3884
Mark Brownc74184e2012-04-04 22:12:09 +01003885 memset(&substream, 0, sizeof(substream));
3886
Nicolin Chen8053f212016-07-26 14:55:51 -07003887 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3888 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3889 if (!runtime) {
3890 ret = -ENOMEM;
3891 goto out;
3892 }
3893 substream.runtime = runtime;
Charles Keepax249dc492018-08-15 13:11:35 +01003894 substream.private_data = rtd;
Nicolin Chen8053f212016-07-26 14:55:51 -07003895
Mark Brownc74184e2012-04-04 22:12:09 +01003896 switch (event) {
3897 case SND_SOC_DAPM_PRE_PMU:
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003898 ret = snd_soc_dai_link_event_pre_pmu(w, &substream);
3899 if (ret < 0)
3900 goto out;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003901
Mark Brownc74184e2012-04-04 22:12:09 +01003902 break;
3903
3904 case SND_SOC_DAPM_POST_PMU:
Charles Keepax4a75aae2018-09-05 15:21:01 +01003905 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3906 sink = path->sink->priv;
3907
3908 ret = snd_soc_dai_digital_mute(sink, 0,
3909 SNDRV_PCM_STREAM_PLAYBACK);
3910 if (ret != 0 && ret != -ENOTSUPP)
3911 dev_warn(sink->dev,
3912 "ASoC: Failed to unmute: %d\n", ret);
3913 ret = 0;
3914 }
Mark Brownc74184e2012-04-04 22:12:09 +01003915 break;
3916
3917 case SND_SOC_DAPM_PRE_PMD:
Charles Keepax4a75aae2018-09-05 15:21:01 +01003918 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3919 sink = path->sink->priv;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303920
Charles Keepax4a75aae2018-09-05 15:21:01 +01003921 ret = snd_soc_dai_digital_mute(sink, 1,
3922 SNDRV_PCM_STREAM_PLAYBACK);
3923 if (ret != 0 && ret != -ENOTSUPP)
3924 dev_warn(sink->dev,
3925 "ASoC: Failed to mute: %d\n", ret);
3926 ret = 0;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303927 }
3928
Charles Keepax3c01b0e2018-10-11 17:28:28 +01003929 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003930 snd_soc_dapm_widget_for_each_source_path(w, path) {
3931 source = path->source->priv;
Kuninori Morimoto846faae2019-07-22 10:33:19 +09003932 snd_soc_dai_hw_free(source, &substream);
Jerome Brunet68c907f2019-07-25 18:59:44 +02003933 }
Charles Keepax3c01b0e2018-10-11 17:28:28 +01003934
Jerome Brunet68c907f2019-07-25 18:59:44 +02003935 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3936 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3937 sink = path->sink->priv;
3938 snd_soc_dai_hw_free(sink, &substream);
3939 }
3940
3941 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3942 snd_soc_dapm_widget_for_each_source_path(w, path) {
3943 source = path->source->priv;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003944 source->active--;
Kuninori Morimoto330fcb52019-07-22 10:33:39 +09003945 snd_soc_dai_shutdown(source, &substream);
Charles Keepax4a75aae2018-09-05 15:21:01 +01003946 }
3947
Charles Keepax3c01b0e2018-10-11 17:28:28 +01003948 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003949 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3950 sink = path->sink->priv;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003951 sink->active--;
Kuninori Morimoto330fcb52019-07-22 10:33:39 +09003952 snd_soc_dai_shutdown(sink, &substream);
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303953 }
Mark Brownc74184e2012-04-04 22:12:09 +01003954 break;
3955
3956 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003957 WARN(1, "Unknown event %d\n", event);
Sudip Mukherjee75881df2015-09-10 18:01:44 +05303958 ret = -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003959 }
3960
Mark Brown9747cec2012-04-26 19:12:21 +01003961out:
Nicolin Chen8053f212016-07-26 14:55:51 -07003962 kfree(runtime);
Mark Brown9747cec2012-04-26 19:12:21 +01003963 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003964}
3965
Nikesh Oswalc6615082015-02-02 17:06:44 +00003966static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3967 struct snd_ctl_elem_value *ucontrol)
3968{
3969 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
Charles Keepax243bcfa2018-09-05 15:21:02 +01003970 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003971
Charles Keepax243bcfa2018-09-05 15:21:02 +01003972 ucontrol->value.enumerated.item[0] = rtd->params_select;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003973
3974 return 0;
3975}
3976
3977static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3978 struct snd_ctl_elem_value *ucontrol)
3979{
3980 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
Charles Keepax243bcfa2018-09-05 15:21:02 +01003981 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003982
3983 /* Can't change the config when widget is already powered */
3984 if (w->power)
3985 return -EBUSY;
3986
Charles Keepax243bcfa2018-09-05 15:21:02 +01003987 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003988 return 0;
3989
Charles Keepax243bcfa2018-09-05 15:21:02 +01003990 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003991 return -EINVAL;
3992
Charles Keepax243bcfa2018-09-05 15:21:02 +01003993 rtd->params_select = ucontrol->value.enumerated.item[0];
Nikesh Oswalc6615082015-02-02 17:06:44 +00003994
3995 return 0;
3996}
3997
Arnd Bergmannc42c5ac2017-10-10 11:20:11 +02003998static void
anish kumar19ad6832017-09-28 21:52:39 -07003999snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
4000 unsigned long *private_value,
4001 int num_params,
4002 const char **w_param_text)
Mark Brownc74184e2012-04-04 22:12:09 +01004003{
anish kumar19ad6832017-09-28 21:52:39 -07004004 int count;
4005
4006 devm_kfree(card->dev, (void *)*private_value);
Pankaj Bharadiyacacea3a2019-03-22 18:00:09 +05304007
4008 if (!w_param_text)
4009 return;
4010
anish kumar19ad6832017-09-28 21:52:39 -07004011 for (count = 0 ; count < num_params; count++)
4012 devm_kfree(card->dev, (void *)w_param_text[count]);
4013 devm_kfree(card->dev, w_param_text);
4014}
4015
4016static struct snd_kcontrol_new *
4017snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
4018 char *link_name,
4019 const struct snd_soc_pcm_stream *params,
4020 int num_params, const char **w_param_text,
4021 unsigned long *private_value)
4022{
Nikesh Oswalc6615082015-02-02 17:06:44 +00004023 struct soc_enum w_param_enum[] = {
4024 SOC_ENUM_SINGLE(0, 0, 0, NULL),
4025 };
4026 struct snd_kcontrol_new kcontrol_dai_link[] = {
4027 SOC_ENUM_EXT(NULL, w_param_enum[0],
4028 snd_soc_dapm_dai_link_get,
4029 snd_soc_dapm_dai_link_put),
4030 };
anish kumar19ad6832017-09-28 21:52:39 -07004031 struct snd_kcontrol_new *kcontrol_news;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004032 const struct snd_soc_pcm_stream *config = params;
anish kumar19ad6832017-09-28 21:52:39 -07004033 int count;
Mark Brownc74184e2012-04-04 22:12:09 +01004034
Nikesh Oswalc6615082015-02-02 17:06:44 +00004035 for (count = 0 ; count < num_params; count++) {
4036 if (!config->stream_name) {
4037 dev_warn(card->dapm.dev,
4038 "ASoC: anonymous config %d for dai link %s\n",
4039 count, link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004040 w_param_text[count] =
Charles Keepax46172b62015-03-25 11:22:35 +00004041 devm_kasprintf(card->dev, GFP_KERNEL,
4042 "Anonymous Configuration %d",
4043 count);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004044 } else {
4045 w_param_text[count] = devm_kmemdup(card->dev,
4046 config->stream_name,
4047 strlen(config->stream_name) + 1,
4048 GFP_KERNEL);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004049 }
anish kumar19ad6832017-09-28 21:52:39 -07004050 if (!w_param_text[count])
4051 goto outfree_w_param;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004052 config++;
4053 }
anish kumar19ad6832017-09-28 21:52:39 -07004054
Nikesh Oswalc6615082015-02-02 17:06:44 +00004055 w_param_enum[0].items = num_params;
4056 w_param_enum[0].texts = w_param_text;
Mark Brownc74184e2012-04-04 22:12:09 +01004057
anish kumar19ad6832017-09-28 21:52:39 -07004058 *private_value =
4059 (unsigned long) devm_kmemdup(card->dev,
4060 (void *)(kcontrol_dai_link[0].private_value),
4061 sizeof(struct soc_enum), GFP_KERNEL);
4062 if (!*private_value) {
4063 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4064 link_name);
4065 goto outfree_w_param;
4066 }
4067 kcontrol_dai_link[0].private_value = *private_value;
4068 /* duplicate kcontrol_dai_link on heap so that memory persists */
4069 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4070 sizeof(struct snd_kcontrol_new),
4071 GFP_KERNEL);
4072 if (!kcontrol_news) {
4073 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4074 link_name);
4075 goto outfree_w_param;
4076 }
4077 return kcontrol_news;
4078
4079outfree_w_param:
4080 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
4081 return NULL;
4082}
4083
Charles Keepax778ff5b2018-09-05 15:21:00 +01004084static struct snd_soc_dapm_widget *
4085snd_soc_dapm_new_dai(struct snd_soc_card *card, struct snd_soc_pcm_runtime *rtd,
Jerome Brunet054d6502019-07-25 18:59:45 +02004086 char *id)
anish kumar19ad6832017-09-28 21:52:39 -07004087{
4088 struct snd_soc_dapm_widget template;
4089 struct snd_soc_dapm_widget *w;
4090 const char **w_param_text;
Pankaj Bharadiya8633d442019-03-22 21:53:39 +05304091 unsigned long private_value = 0;
anish kumar19ad6832017-09-28 21:52:39 -07004092 char *link_name;
4093 int ret;
4094
4095 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
Jerome Brunet054d6502019-07-25 18:59:45 +02004096 rtd->dai_link->name, id);
anish kumar19ad6832017-09-28 21:52:39 -07004097 if (!link_name)
Charles Keepax778ff5b2018-09-05 15:21:00 +01004098 return ERR_PTR(-ENOMEM);
anish kumar19ad6832017-09-28 21:52:39 -07004099
Mark Brownc74184e2012-04-04 22:12:09 +01004100 memset(&template, 0, sizeof(template));
4101 template.reg = SND_SOC_NOPM;
4102 template.id = snd_soc_dapm_dai_link;
4103 template.name = link_name;
4104 template.event = snd_soc_dai_link_event;
4105 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
4106 SND_SOC_DAPM_PRE_PMD;
anish kumar19ad6832017-09-28 21:52:39 -07004107 template.kcontrol_news = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01004108
anish kumar19ad6832017-09-28 21:52:39 -07004109 /* allocate memory for control, only in case of multiple configs */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004110 if (rtd->dai_link->num_params > 1) {
4111 w_param_text = devm_kcalloc(card->dev,
4112 rtd->dai_link->num_params,
4113 sizeof(char *), GFP_KERNEL);
anish kumar19ad6832017-09-28 21:52:39 -07004114 if (!w_param_text) {
4115 ret = -ENOMEM;
4116 goto param_fail;
4117 }
4118
4119 template.num_kcontrols = 1;
4120 template.kcontrol_news =
4121 snd_soc_dapm_alloc_kcontrol(card,
Charles Keepax778ff5b2018-09-05 15:21:00 +01004122 link_name,
4123 rtd->dai_link->params,
4124 rtd->dai_link->num_params,
anish kumar19ad6832017-09-28 21:52:39 -07004125 w_param_text, &private_value);
4126 if (!template.kcontrol_news) {
4127 ret = -ENOMEM;
4128 goto param_fail;
4129 }
Arnd Bergmann667ebc92017-10-10 11:20:10 +02004130 } else {
4131 w_param_text = NULL;
anish kumar19ad6832017-09-28 21:52:39 -07004132 }
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004133 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01004134
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004135 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
Dan Carpenter2e558a82018-09-07 22:40:33 +03004136 if (IS_ERR(w)) {
4137 ret = PTR_ERR(w);
Linus Walleij37e1df82017-01-13 10:23:52 +01004138 goto outfree_kcontrol_news;
Dan Carpenter2e558a82018-09-07 22:40:33 +03004139 }
Mark Brownc74184e2012-04-04 22:12:09 +01004140
Charles Keepax249dc492018-08-15 13:11:35 +01004141 w->priv = rtd;
Mark Brownc74184e2012-04-04 22:12:09 +01004142
Charles Keepax778ff5b2018-09-05 15:21:00 +01004143 return w;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004144
Nikesh Oswalc6615082015-02-02 17:06:44 +00004145outfree_kcontrol_news:
4146 devm_kfree(card->dev, (void *)template.kcontrol_news);
Charles Keepax778ff5b2018-09-05 15:21:00 +01004147 snd_soc_dapm_free_kcontrol(card, &private_value,
4148 rtd->dai_link->num_params, w_param_text);
anish kumar19ad6832017-09-28 21:52:39 -07004149param_fail:
Nikesh Oswalc6615082015-02-02 17:06:44 +00004150 devm_kfree(card->dev, link_name);
Charles Keepax778ff5b2018-09-05 15:21:00 +01004151 return ERR_PTR(ret);
Mark Brownc74184e2012-04-04 22:12:09 +01004152}
4153
Mark Brown888df392012-02-16 19:37:51 -08004154int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4155 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004156{
Mark Brown888df392012-02-16 19:37:51 -08004157 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004158 struct snd_soc_dapm_widget *w;
4159
Mark Brown888df392012-02-16 19:37:51 -08004160 WARN_ON(dapm->dev != dai->dev);
4161
4162 memset(&template, 0, sizeof(template));
4163 template.reg = SND_SOC_NOPM;
4164
4165 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01004166 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08004167 template.name = dai->driver->playback.stream_name;
4168 template.sname = dai->driver->playback.stream_name;
4169
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004170 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08004171 template.name);
4172
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004173 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01004174 if (IS_ERR(w))
4175 return PTR_ERR(w);
Mark Brown888df392012-02-16 19:37:51 -08004176
4177 w->priv = dai;
4178 dai->playback_widget = w;
4179 }
4180
4181 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01004182 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08004183 template.name = dai->driver->capture.stream_name;
4184 template.sname = dai->driver->capture.stream_name;
4185
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004186 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08004187 template.name);
4188
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004189 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01004190 if (IS_ERR(w))
4191 return PTR_ERR(w);
Mark Brown888df392012-02-16 19:37:51 -08004192
4193 w->priv = dai;
4194 dai->capture_widget = w;
4195 }
4196
4197 return 0;
4198}
4199
4200int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4201{
4202 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004203 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08004204 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08004205
4206 /* For each DAI widget... */
4207 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01004208 switch (dai_w->id) {
4209 case snd_soc_dapm_dai_in:
4210 case snd_soc_dapm_dai_out:
4211 break;
4212 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02004213 continue;
Mark Brown46162742013-06-05 19:36:11 +01004214 }
Mark Brown888df392012-02-16 19:37:51 -08004215
Liam Girdwoode01b4f62018-06-14 20:26:42 +01004216 /* let users know there is no DAI to link */
4217 if (!dai_w->priv) {
4218 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4219 dai_w->name);
4220 continue;
4221 }
4222
Mark Brown888df392012-02-16 19:37:51 -08004223 dai = dai_w->priv;
4224
4225 /* ...find all widgets with the same stream and link them */
4226 list_for_each_entry(w, &card->widgets, list) {
4227 if (w->dapm != dai_w->dapm)
4228 continue;
4229
Mark Brown46162742013-06-05 19:36:11 +01004230 switch (w->id) {
4231 case snd_soc_dapm_dai_in:
4232 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08004233 continue;
Mark Brown46162742013-06-05 19:36:11 +01004234 default:
4235 break;
4236 }
Mark Brown888df392012-02-16 19:37:51 -08004237
Lars-Peter Clausena798c242015-07-21 11:51:35 +02004238 if (!w->sname || !strstr(w->sname, dai_w->sname))
Mark Brown888df392012-02-16 19:37:51 -08004239 continue;
4240
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004241 if (dai_w->id == snd_soc_dapm_dai_in) {
4242 src = dai_w;
4243 sink = w;
4244 } else {
4245 src = w;
4246 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08004247 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004248 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4249 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004250 }
4251 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02004252
Mark Brown888df392012-02-16 19:37:51 -08004253 return 0;
4254}
Liam Girdwood64a648c2011-07-25 11:15:15 +01004255
Benoit Cousson44ba2642014-07-08 23:19:36 +02004256static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4257 struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004258{
Benoit Cousson44ba2642014-07-08 23:19:36 +02004259 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00004260 struct snd_soc_dai *codec_dai;
Charles Keepax778ff5b2018-09-05 15:21:00 +01004261 struct snd_soc_dapm_widget *playback = NULL, *capture = NULL;
4262 struct snd_soc_dapm_widget *codec, *playback_cpu, *capture_cpu;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004263 int i;
4264
Charles Keepax778ff5b2018-09-05 15:21:00 +01004265 if (rtd->dai_link->params) {
Charles Keepax778ff5b2018-09-05 15:21:00 +01004266 playback_cpu = cpu_dai->capture_widget;
4267 capture_cpu = cpu_dai->playback_widget;
4268 } else {
4269 playback = cpu_dai->playback_widget;
4270 capture = cpu_dai->capture_widget;
4271 playback_cpu = playback;
4272 capture_cpu = capture;
4273 }
4274
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00004275 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004276 /* connect BE DAI playback if widgets are valid */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004277 codec = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004278
Charles Keepax778ff5b2018-09-05 15:21:00 +01004279 if (playback_cpu && codec) {
4280 if (!playback) {
4281 playback = snd_soc_dapm_new_dai(card, rtd,
Jerome Brunet054d6502019-07-25 18:59:45 +02004282 "playback");
Charles Keepaxe36a1d02018-09-10 15:28:39 +01004283 if (IS_ERR(playback)) {
4284 dev_err(rtd->dev,
4285 "ASoC: Failed to create DAI %s: %ld\n",
4286 codec_dai->name,
4287 PTR_ERR(playback));
4288 continue;
4289 }
Charles Keepax778ff5b2018-09-05 15:21:00 +01004290
4291 snd_soc_dapm_add_path(&card->dapm, playback_cpu,
4292 playback, NULL, NULL);
4293 }
4294
4295 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4296 cpu_dai->component->name, playback_cpu->name,
4297 codec_dai->component->name, codec->name);
4298
4299 snd_soc_dapm_add_path(&card->dapm, playback, codec,
4300 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004301 }
Charles Keepaxe36a1d02018-09-10 15:28:39 +01004302 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004303
Charles Keepaxe36a1d02018-09-10 15:28:39 +01004304 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004305 /* connect BE DAI capture if widgets are valid */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004306 codec = codec_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004307
Charles Keepax778ff5b2018-09-05 15:21:00 +01004308 if (codec && capture_cpu) {
4309 if (!capture) {
4310 capture = snd_soc_dapm_new_dai(card, rtd,
Jerome Brunet054d6502019-07-25 18:59:45 +02004311 "capture");
Charles Keepaxe36a1d02018-09-10 15:28:39 +01004312 if (IS_ERR(capture)) {
4313 dev_err(rtd->dev,
4314 "ASoC: Failed to create DAI %s: %ld\n",
4315 codec_dai->name,
4316 PTR_ERR(capture));
4317 continue;
4318 }
Charles Keepax778ff5b2018-09-05 15:21:00 +01004319
4320 snd_soc_dapm_add_path(&card->dapm, capture,
4321 capture_cpu, NULL, NULL);
4322 }
4323
4324 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4325 codec_dai->component->name, codec->name,
4326 cpu_dai->component->name, capture_cpu->name);
4327
4328 snd_soc_dapm_add_path(&card->dapm, codec, capture,
4329 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004330 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004331 }
4332}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004333
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004334static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4335 int event)
4336{
4337 struct snd_soc_dapm_widget *w;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004338 unsigned int ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004339
4340 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4341 w = dai->playback_widget;
4342 else
4343 w = dai->capture_widget;
4344
4345 if (w) {
4346 dapm_mark_dirty(w, "stream event");
4347
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004348 if (w->id == snd_soc_dapm_dai_in) {
4349 ep = SND_SOC_DAPM_EP_SOURCE;
4350 dapm_widget_invalidate_input_paths(w);
4351 } else {
4352 ep = SND_SOC_DAPM_EP_SINK;
4353 dapm_widget_invalidate_output_paths(w);
4354 }
4355
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004356 switch (event) {
4357 case SND_SOC_DAPM_STREAM_START:
4358 w->active = 1;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004359 w->is_ep = ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004360 break;
4361 case SND_SOC_DAPM_STREAM_STOP:
4362 w->active = 0;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004363 w->is_ep = 0;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004364 break;
4365 case SND_SOC_DAPM_STREAM_SUSPEND:
4366 case SND_SOC_DAPM_STREAM_RESUME:
4367 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4368 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4369 break;
4370 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004371 }
4372}
4373
Benoit Cousson44ba2642014-07-08 23:19:36 +02004374void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4375{
Mengdong Lin1a497982015-11-18 02:34:11 -05004376 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson44ba2642014-07-08 23:19:36 +02004377
4378 /* for each BE DAI link... */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00004379 for_each_card_rtds(card, rtd) {
Benoit Cousson44ba2642014-07-08 23:19:36 +02004380 /*
4381 * dynamic FE links have no fixed DAI mapping.
4382 * CODEC<->CODEC links have no direct connection.
4383 */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004384 if (rtd->dai_link->dynamic)
Benoit Cousson44ba2642014-07-08 23:19:36 +02004385 continue;
4386
4387 dapm_connect_dai_link_widgets(card, rtd);
4388 }
4389}
4390
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004391static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4392 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004393{
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00004394 struct snd_soc_dai *codec_dai;
Benoit Cousson44ba2642014-07-08 23:19:36 +02004395 int i;
4396
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004397 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00004398 for_each_rtd_codec_dai(rtd, i, codec_dai)
4399 soc_dapm_dai_stream_event(codec_dai, stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004400
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004401 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004402}
4403
4404/**
4405 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4406 * @rtd: PCM runtime data
4407 * @stream: stream name
4408 * @event: stream event
4409 *
4410 * Sends a stream event to the dapm core. The core then makes any
4411 * necessary widget power changes.
4412 *
4413 * Returns 0 for success else error.
4414 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004415void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4416 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004417{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004418 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004419
Liam Girdwood3cd04342012-03-09 12:02:08 +00004420 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004421 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004422 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004423}
Richard Purdie2b97eab2006-10-06 18:32:18 +02004424
4425/**
Charles Keepax11391102014-02-18 15:22:14 +00004426 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4427 * @dapm: DAPM context
4428 * @pin: pin name
4429 *
4430 * Enables input/output pin and its parents or children widgets iff there is
4431 * a valid audio route and active audio stream.
4432 *
4433 * Requires external locking.
4434 *
4435 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4436 * do any widget power switching.
4437 */
4438int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4439 const char *pin)
4440{
4441 return snd_soc_dapm_set_pin(dapm, pin, 1);
4442}
4443EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4444
4445/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004446 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004447 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004448 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02004449 *
Mark Brown74b8f952009-06-06 11:26:15 +01004450 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01004451 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00004452 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004453 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4454 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02004455 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004456int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004457{
Charles Keepax11391102014-02-18 15:22:14 +00004458 int ret;
4459
4460 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4461
4462 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4463
4464 mutex_unlock(&dapm->card->dapm_mutex);
4465
4466 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004467}
Liam Girdwooda5302182008-07-07 13:35:17 +01004468EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004469
4470/**
Charles Keepax11391102014-02-18 15:22:14 +00004471 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4472 * @dapm: DAPM context
4473 * @pin: pin name
4474 *
4475 * Enables input/output pin regardless of any other state. This is
4476 * intended for use with microphone bias supplies used in microphone
4477 * jack detection.
4478 *
4479 * Requires external locking.
4480 *
4481 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4482 * do any widget power switching.
4483 */
4484int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4485 const char *pin)
4486{
4487 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4488
4489 if (!w) {
4490 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4491 return -EINVAL;
4492 }
4493
4494 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02004495 if (!w->connected) {
4496 /*
4497 * w->force does not affect the number of input or output paths,
4498 * so we only have to recheck if w->connected is changed
4499 */
4500 dapm_widget_invalidate_input_paths(w);
4501 dapm_widget_invalidate_output_paths(w);
4502 w->connected = 1;
4503 }
Charles Keepax11391102014-02-18 15:22:14 +00004504 w->force = 1;
4505 dapm_mark_dirty(w, "force enable");
4506
4507 return 0;
4508}
4509EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4510
4511/**
Mark Brownda341832010-03-15 19:23:37 +00004512 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004513 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00004514 * @pin: pin name
4515 *
4516 * Enables input/output pin regardless of any other state. This is
4517 * intended for use with microphone bias supplies used in microphone
4518 * jack detection.
4519 *
4520 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4521 * do any widget power switching.
4522 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004523int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4524 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00004525{
Charles Keepax11391102014-02-18 15:22:14 +00004526 int ret;
Mark Brownda341832010-03-15 19:23:37 +00004527
Charles Keepax11391102014-02-18 15:22:14 +00004528 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00004529
Charles Keepax11391102014-02-18 15:22:14 +00004530 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09004531
Charles Keepax11391102014-02-18 15:22:14 +00004532 mutex_unlock(&dapm->card->dapm_mutex);
4533
4534 return ret;
Mark Brownda341832010-03-15 19:23:37 +00004535}
4536EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4537
4538/**
Charles Keepax11391102014-02-18 15:22:14 +00004539 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4540 * @dapm: DAPM context
4541 * @pin: pin name
4542 *
4543 * Disables input/output pin and its parents or children widgets.
4544 *
4545 * Requires external locking.
4546 *
4547 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4548 * do any widget power switching.
4549 */
4550int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4551 const char *pin)
4552{
4553 return snd_soc_dapm_set_pin(dapm, pin, 0);
4554}
4555EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4556
4557/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004558 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004559 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004560 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004561 *
Mark Brown74b8f952009-06-06 11:26:15 +01004562 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00004563 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004564 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4565 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004566 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004567int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4568 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01004569{
Charles Keepax11391102014-02-18 15:22:14 +00004570 int ret;
4571
4572 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4573
4574 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4575
4576 mutex_unlock(&dapm->card->dapm_mutex);
4577
4578 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01004579}
4580EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4581
4582/**
Charles Keepax11391102014-02-18 15:22:14 +00004583 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4584 * @dapm: DAPM context
4585 * @pin: pin name
4586 *
4587 * Marks the specified pin as being not connected, disabling it along
4588 * any parent or child widgets. At present this is identical to
4589 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4590 * additional things such as disabling controls which only affect
4591 * paths through the pin.
4592 *
4593 * Requires external locking.
4594 *
4595 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4596 * do any widget power switching.
4597 */
4598int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4599 const char *pin)
4600{
4601 return snd_soc_dapm_set_pin(dapm, pin, 0);
4602}
4603EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4604
4605/**
Mark Brown5817b522008-09-24 11:23:11 +01004606 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004607 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01004608 * @pin: pin name
4609 *
4610 * Marks the specified pin as being not connected, disabling it along
4611 * any parent or child widgets. At present this is identical to
4612 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4613 * additional things such as disabling controls which only affect
4614 * paths through the pin.
4615 *
4616 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4617 * do any widget power switching.
4618 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004619int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01004620{
Charles Keepax11391102014-02-18 15:22:14 +00004621 int ret;
4622
4623 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4624
4625 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4626
4627 mutex_unlock(&dapm->card->dapm_mutex);
4628
4629 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01004630}
4631EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4632
4633/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004634 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004635 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004636 * @pin: audio signal pin endpoint (or start point)
4637 *
4638 * Get audio pin status - connected or disconnected.
4639 *
4640 * Returns 1 for connected otherwise 0.
4641 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004642int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4643 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004644{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004645 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004646
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004647 if (w)
4648 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06004649
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004650 return 0;
4651}
Liam Girdwooda5302182008-07-07 13:35:17 +01004652EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004653
4654/**
Mark Brown1547aba2010-05-07 21:11:40 +01004655 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004656 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01004657 * @pin: audio signal pin endpoint (or start point)
4658 *
4659 * Mark the given endpoint or pin as ignoring suspend. When the
4660 * system is disabled a path between two endpoints flagged as ignoring
4661 * suspend will not be disabled. The path must already be enabled via
4662 * normal means at suspend time, it will not be turned on if it was not
4663 * already enabled.
4664 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004665int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4666 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01004667{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004668 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01004669
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004670 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004671 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004672 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01004673 }
4674
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004675 w->ignore_suspend = 1;
4676
4677 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01004678}
4679EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4680
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02004681/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02004682 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03004683 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02004684 *
4685 * Free all dapm widgets and resources.
4686 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004687void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004688{
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02004689 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004690 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02004691 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004692}
4693EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4694
Xiang Xiao57996352014-03-02 00:04:02 +08004695static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01004696{
Liam Girdwood01005a72012-07-06 16:57:05 +01004697 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01004698 struct snd_soc_dapm_widget *w;
4699 LIST_HEAD(down_list);
4700 int powerdown = 0;
4701
Liam Girdwood01005a72012-07-06 16:57:05 +01004702 mutex_lock(&card->dapm_mutex);
4703
Jarkko Nikula97c866d2010-12-14 12:18:31 +02004704 list_for_each_entry(w, &dapm->card->widgets, list) {
4705 if (w->dapm != dapm)
4706 continue;
Mark Brown51737472009-06-22 13:16:51 +01004707 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004708 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01004709 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004710 powerdown = 1;
4711 }
4712 }
4713
4714 /* If there were no widgets to power down we're already in
4715 * standby.
4716 */
4717 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004718 if (dapm->bias_level == SND_SOC_BIAS_ON)
4719 snd_soc_dapm_set_bias_level(dapm,
4720 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004721 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004722 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4723 snd_soc_dapm_set_bias_level(dapm,
4724 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004725 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004726
4727 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004728}
Mark Brown51737472009-06-22 13:16:51 +01004729
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004730/*
4731 * snd_soc_dapm_shutdown - callback for system shutdown
4732 */
4733void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4734{
Xiang Xiao57996352014-03-02 00:04:02 +08004735 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004736
Xiang Xiao57996352014-03-02 00:04:02 +08004737 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08004738 if (dapm != &card->dapm) {
4739 soc_dapm_shutdown_dapm(dapm);
4740 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4741 snd_soc_dapm_set_bias_level(dapm,
4742 SND_SOC_BIAS_OFF);
4743 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004744 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08004745
4746 soc_dapm_shutdown_dapm(&card->dapm);
4747 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4748 snd_soc_dapm_set_bias_level(&card->dapm,
4749 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01004750}
4751
Richard Purdie2b97eab2006-10-06 18:32:18 +02004752/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004753MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004754MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4755MODULE_LICENSE("GPL");