blob: b6378f025836c6b7c91cd6c1f476afbecaa5e81e [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
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900687 if (dapm->component)
688 ret = snd_soc_component_set_bias_level(dapm->component, level);
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200689
Lars-Peter Clausenf4bf8d72015-04-27 22:13:25 +0200690 if (ret == 0)
691 dapm->bias_level = level;
692
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200693 return ret;
694}
695EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
696
Mark Brown452c5ea2009-05-17 21:41:23 +0100697/**
698 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800699 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100700 * @level: level to configure
701 *
702 * Configure the bias (power) levels for the SoC audio device.
703 *
704 * Returns 0 for success else error.
705 */
Mark Browned5a4c42011-02-18 11:12:42 -0800706static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200707 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100708{
Mark Browned5a4c42011-02-18 11:12:42 -0800709 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100710 int ret = 0;
711
Mark Brown84e90932010-11-04 00:07:02 -0400712 trace_snd_soc_bias_level_start(card, level);
713
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000714 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100715 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100716 if (ret != 0)
717 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100718
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200719 if (!card || dapm != &card->dapm)
720 ret = snd_soc_dapm_force_bias_level(dapm, level);
Liam Girdwood41231282012-07-06 16:56:16 +0100721
Mark Brown171ec6b2011-06-06 18:15:19 +0100722 if (ret != 0)
723 goto out;
724
725 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100726 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100727out:
Mark Brown84e90932010-11-04 00:07:02 -0400728 trace_snd_soc_bias_level_done(card, level);
729
Mark Brown452c5ea2009-05-17 21:41:23 +0100730 return ret;
731}
732
Mark Brown74b8f952009-06-06 11:26:15 +0100733/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200734static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200735 struct snd_soc_dapm_path *path, const char *control_name,
736 struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200737{
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200738 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200739 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100740 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200741 int i;
742
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100743 if (e->reg != SND_SOC_NOPM) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200744 soc_dapm_read(dapm, e->reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100745 val = (val >> e->shift_l) & e->mask;
746 item = snd_soc_enum_val_to_item(e, val);
747 } else {
748 /* since a virtual mux has no backing registers to
749 * decide which path to connect, it will try to match
750 * with the first enumeration. This is to ensure
751 * that the default mux choice (the first) will be
752 * correctly powered up during initialization.
753 */
754 item = 0;
755 }
756
Xie Yishengf9e0b4a2018-05-31 19:11:23 +0800757 i = match_string(e->texts, e->items, control_name);
758 if (i < 0)
759 return -ENODEV;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200760
Xie Yishengf9e0b4a2018-05-31 19:11:23 +0800761 path->name = e->texts[i];
762 path->connect = (i == item);
763 return 0;
764
Richard Purdie2b97eab2006-10-06 18:32:18 +0200765}
766
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100767/* set up initial codec paths */
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800768static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
769 int nth_path)
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100770{
771 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200772 p->sink->kcontrol_news[i].private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100773 unsigned int reg = mc->reg;
774 unsigned int shift = mc->shift;
775 unsigned int max = mc->max;
776 unsigned int mask = (1 << fls(max)) - 1;
777 unsigned int invert = mc->invert;
778 unsigned int val;
779
780 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200781 soc_dapm_read(p->sink->dapm, reg, &val);
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800782 /*
783 * The nth_path argument allows this function to know
784 * which path of a kcontrol it is setting the initial
785 * status for. Ideally this would support any number
786 * of paths and channels. But since kcontrols only come
787 * in mono and stereo variants, we are limited to 2
788 * channels.
789 *
790 * The following code assumes for stereo controls the
791 * first path is the left channel, and all remaining
792 * paths are the right channel.
793 */
794 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
795 if (reg != mc->rreg)
796 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
797 val = (val >> mc->rshift) & mask;
798 } else {
799 val = (val >> shift) & mask;
800 }
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100801 if (invert)
802 val = max - val;
803 p->connect = !!val;
804 } else {
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/*
Szymon Mielczarek872f3ac2019-08-09 10:40:34 +02001132 * Recursively reset the cached number of inputs or outputs for the specified
1133 * widget and all widgets that can be reached via incoming or outcoming paths
1134 * from the widget.
1135 */
1136static void invalidate_paths_ep(struct snd_soc_dapm_widget *widget,
1137 enum snd_soc_dapm_direction dir)
1138{
1139 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1140 struct snd_soc_dapm_path *path;
1141
1142 widget->endpoints[dir] = -1;
1143
1144 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1145 if (path->weak || path->is_supply)
1146 continue;
1147
1148 if (path->walking)
1149 return;
1150
1151 if (path->connect) {
1152 path->walking = 1;
1153 invalidate_paths_ep(path->node[dir], dir);
1154 path->walking = 0;
1155 }
1156 }
1157}
1158
1159/*
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001160 * Common implementation for is_connected_output_ep() and
1161 * is_connected_input_ep(). The function is inlined since the combined size of
1162 * the two specialized functions is only marginally larger then the size of the
1163 * generic function and at the same time the fast path of the specialized
1164 * functions is significantly smaller than the generic function.
1165 */
1166static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1167 struct list_head *list, enum snd_soc_dapm_direction dir,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001168 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1169 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1170 enum snd_soc_dapm_direction)),
1171 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1172 enum snd_soc_dapm_direction))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001173{
1174 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001175 struct snd_soc_dapm_path *path;
1176 int con = 0;
1177
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001178 if (widget->endpoints[dir] >= 0)
1179 return widget->endpoints[dir];
Mark Brown024dc072011-10-09 11:52:05 +01001180
Mark Brownde02d072011-09-20 21:43:24 +01001181 DAPM_UPDATE_STAT(widget, path_checks);
1182
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001183 /* do we need to add this widget to the list ? */
1184 if (list)
1185 list_add_tail(&widget->work_list, list);
1186
Jeeja KP09464972016-06-15 11:16:55 +05301187 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
Charles Keepax8dd26df2019-07-18 09:43:33 +01001188 list = NULL;
1189 custom_stop_condition = NULL;
Jeeja KP09464972016-06-15 11:16:55 +05301190 }
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001191
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001192 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1193 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1194 return widget->endpoints[dir];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001195 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001196
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001197 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
Mark Browne56235e02011-09-21 18:19:14 +01001198 DAPM_UPDATE_STAT(widget, neighbour_checks);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001199
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001200 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +01001201 continue;
1202
Mark Brown8af294b2013-02-22 17:48:15 +00001203 if (path->walking)
1204 return 1;
1205
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001206 trace_snd_soc_dapm_path(widget, dir, path);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001207
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001208 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +00001209 path->walking = 1;
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001210 con += fn(path->node[dir], list, custom_stop_condition);
Mark Brown8af294b2013-02-22 17:48:15 +00001211 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001212 }
1213 }
1214
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001215 widget->endpoints[dir] = con;
Mark Brown024dc072011-10-09 11:52:05 +01001216
Richard Purdie2b97eab2006-10-06 18:32:18 +02001217 return con;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001218}
1219
1220/*
1221 * Recursively check for a completed path to an active or physically connected
1222 * output widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001223 *
1224 * Optionally, can be supplied with a function acting as a stopping condition.
1225 * This function takes the dapm widget currently being examined and the walk
Charles Keepax8dd26df2019-07-18 09:43:33 +01001226 * direction as an arguments, it should return true if widgets from that point
1227 * in the graph onwards should not be added to the widget list.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001228 */
1229static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001230 struct list_head *list,
1231 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1232 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001233{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001234 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001235 is_connected_output_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001236}
1237
1238/*
1239 * Recursively check for a completed path to an active or physically connected
1240 * input widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001241 *
1242 * Optionally, can be supplied with a function acting as a stopping condition.
1243 * This function takes the dapm widget currently being examined and the walk
1244 * direction as an arguments, it should return true if the walk should be
1245 * stopped and false otherwise.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001246 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001247static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001248 struct list_head *list,
1249 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1250 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001251{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001252 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001253 is_connected_input_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001254}
1255
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001256/**
1257 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1258 * @dai: the soc DAI.
1259 * @stream: stream direction.
1260 * @list: list of active widgets for this stream.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001261 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1262 * walk based on custom logic.
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001263 *
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001264 * Queries DAPM graph as to whether a valid audio stream path exists for
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001265 * the initial stream specified by name. This takes into account
1266 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1267 *
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001268 * Optionally, can be supplied with a function acting as a stopping condition.
1269 * This function takes the dapm widget currently being examined and the walk
1270 * direction as an arguments, it should return true if the walk should be
1271 * stopped and false otherwise.
1272 *
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001273 * Returns the number of valid paths or negative error.
1274 */
1275int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001276 struct snd_soc_dapm_widget_list **list,
1277 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1278 enum snd_soc_dapm_direction))
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001279{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01001280 struct snd_soc_card *card = dai->component->card;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001281 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001282 LIST_HEAD(widgets);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001283 int paths;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001284 int ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001285
1286 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001287
Szymon Mielczarek872f3ac2019-08-09 10:40:34 +02001288 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1289 w = dai->playback_widget;
1290 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_OUT);
1291 paths = is_connected_output_ep(w, &widgets,
1292 custom_stop_condition);
1293 } else {
1294 w = dai->capture_widget;
1295 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_IN);
1296 paths = is_connected_input_ep(w, &widgets,
1297 custom_stop_condition);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001298 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001299
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001300 /* Drop starting point */
1301 list_del(widgets.next);
1302
1303 ret = dapm_widget_list_create(list, &widgets);
1304 if (ret)
Lars-Peter Clausen30abbe72015-08-11 21:37:59 +02001305 paths = ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001306
1307 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001308 mutex_unlock(&card->dapm_mutex);
1309
1310 return paths;
1311}
1312
Richard Purdie2b97eab2006-10-06 18:32:18 +02001313/*
Mark Brown62ea8742012-01-21 21:14:48 +00001314 * Handler for regulator supply widget.
1315 */
1316int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1317 struct snd_kcontrol *kcontrol, int event)
1318{
Mark Brownc05b84d2012-09-07 12:57:11 +08001319 int ret;
1320
Mark Browneb270e92013-10-09 13:52:52 +01001321 soc_dapm_async_complete(w->dapm);
1322
Mark Brownc05b84d2012-09-07 12:57:11 +08001323 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001324 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001325 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001326 if (ret != 0)
1327 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001328 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001329 w->name, ret);
1330 }
1331
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001332 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001333 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001334 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001335 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001336 if (ret != 0)
1337 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001338 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001339 w->name, ret);
1340 }
1341
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001342 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001343 }
Mark Brown62ea8742012-01-21 21:14:48 +00001344}
1345EXPORT_SYMBOL_GPL(dapm_regulator_event);
1346
Ola Liljad7e7eb92012-05-24 15:26:25 +02001347/*
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00001348 * Handler for pinctrl widget.
1349 */
1350int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1351 struct snd_kcontrol *kcontrol, int event)
1352{
1353 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1354 struct pinctrl *p = w->pinctrl;
1355 struct pinctrl_state *s;
1356
1357 if (!p || !priv)
1358 return -EIO;
1359
1360 if (SND_SOC_DAPM_EVENT_ON(event))
1361 s = pinctrl_lookup_state(p, priv->active_state);
1362 else
1363 s = pinctrl_lookup_state(p, priv->sleep_state);
1364
1365 if (IS_ERR(s))
1366 return PTR_ERR(s);
1367
1368 return pinctrl_select_state(p, s);
1369}
1370EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1371
1372/*
Ola Liljad7e7eb92012-05-24 15:26:25 +02001373 * Handler for clock supply widget.
1374 */
1375int dapm_clock_event(struct snd_soc_dapm_widget *w,
1376 struct snd_kcontrol *kcontrol, int event)
1377{
1378 if (!w->clk)
1379 return -EIO;
1380
Mark Browneb270e92013-10-09 13:52:52 +01001381 soc_dapm_async_complete(w->dapm);
1382
Ola Liljad7e7eb92012-05-24 15:26:25 +02001383 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001384 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001385 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001386 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001387 return 0;
1388 }
Charles Keepaxd78b1e432018-08-28 14:35:02 +01001389
Marek Belisko98b3cf12012-07-12 23:00:16 +02001390 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001391}
1392EXPORT_SYMBOL_GPL(dapm_clock_event);
1393
Mark Brownd805002b2011-09-28 18:28:23 +01001394static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1395{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001396 if (w->power_checked)
1397 return w->new_power;
1398
Mark Brownd805002b2011-09-28 18:28:23 +01001399 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001400 w->new_power = 1;
Mark Brownd805002b2011-09-28 18:28:23 +01001401 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001402 w->new_power = w->power_check(w);
1403
1404 w->power_checked = true;
1405
1406 return w->new_power;
Mark Brownd805002b2011-09-28 18:28:23 +01001407}
1408
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001409/* Generic check to see if a widget should be powered. */
Mark Browncd0f2d42009-04-20 16:56:59 +01001410static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1411{
1412 int in, out;
1413
Mark Brownde02d072011-09-20 21:43:24 +01001414 DAPM_UPDATE_STAT(w, power_checks);
1415
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001416 in = is_connected_input_ep(w, NULL, NULL);
1417 out = is_connected_output_ep(w, NULL, NULL);
Mark Browncd0f2d42009-04-20 16:56:59 +01001418 return out != 0 && in != 0;
1419}
1420
Mark Brown246d0a12009-04-22 18:24:55 +01001421/* Check to see if a power supply is needed */
1422static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1423{
1424 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001425
Mark Brownde02d072011-09-20 21:43:24 +01001426 DAPM_UPDATE_STAT(w, power_checks);
1427
Mark Brown246d0a12009-04-22 18:24:55 +01001428 /* Check if one of our outputs is connected */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001429 snd_soc_dapm_widget_for_each_sink_path(w, path) {
Mark Browna8fdac82011-09-28 18:20:26 +01001430 DAPM_UPDATE_STAT(w, neighbour_checks);
1431
Mark Brownbf3a9e12011-06-13 16:42:29 +01001432 if (path->weak)
1433 continue;
1434
Mark Brown215edda2009-09-08 18:59:05 +01001435 if (path->connected &&
1436 !path->connected(path->source, path->sink))
1437 continue;
1438
Mark Brownf68d7e12011-10-04 22:57:50 +01001439 if (dapm_widget_power_check(path->sink))
1440 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001441 }
1442
Mark Brownf68d7e12011-10-04 22:57:50 +01001443 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001444}
1445
Mark Brown35c64bc2011-09-28 18:23:53 +01001446static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1447{
Charles Keepax20bb0182015-12-02 10:22:16 +00001448 return w->connected;
Mark Brown35c64bc2011-09-28 18:23:53 +01001449}
1450
Mark Brown38357ab2009-06-06 19:03:23 +01001451static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1452 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001453 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001454{
Mark Brown828a8422011-01-15 13:14:30 +00001455 int *sort;
1456
Pierre-Louis Bossartf13d4b52019-02-05 10:22:28 -06001457 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1458 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1459
Mark Brown828a8422011-01-15 13:14:30 +00001460 if (power_up)
1461 sort = dapm_up_seq;
1462 else
1463 sort = dapm_down_seq;
1464
Pierre-Louis Bossartf13d4b52019-02-05 10:22:28 -06001465 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1466 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1467
Mark Brown38357ab2009-06-06 19:03:23 +01001468 if (sort[a->id] != sort[b->id])
1469 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001470 if (a->subseq != b->subseq) {
1471 if (power_up)
1472 return a->subseq - b->subseq;
1473 else
1474 return b->subseq - a->subseq;
1475 }
Mark Brownb22ead22009-06-07 12:51:26 +01001476 if (a->reg != b->reg)
1477 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001478 if (a->dapm != b->dapm)
1479 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001480
Mark Brown38357ab2009-06-06 19:03:23 +01001481 return 0;
1482}
Mark Brown42aa3412009-03-01 19:21:10 +00001483
Mark Brown38357ab2009-06-06 19:03:23 +01001484/* Insert a widget in order into a DAPM power sequence. */
1485static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1486 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001487 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001488{
1489 struct snd_soc_dapm_widget *w;
1490
1491 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001492 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001493 list_add_tail(&new_widget->power_list, &w->power_list);
1494 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001495 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001496
Mark Brown38357ab2009-06-06 19:03:23 +01001497 list_add_tail(&new_widget->power_list, list);
1498}
Mark Brown42aa3412009-03-01 19:21:10 +00001499
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001500static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001501 struct snd_soc_dapm_widget *w, int event)
1502{
Mark Brown68f89ad2010-11-03 23:51:49 -04001503 const char *ev_name;
1504 int power, ret;
1505
1506 switch (event) {
1507 case SND_SOC_DAPM_PRE_PMU:
1508 ev_name = "PRE_PMU";
1509 power = 1;
1510 break;
1511 case SND_SOC_DAPM_POST_PMU:
1512 ev_name = "POST_PMU";
1513 power = 1;
1514 break;
1515 case SND_SOC_DAPM_PRE_PMD:
1516 ev_name = "PRE_PMD";
1517 power = 0;
1518 break;
1519 case SND_SOC_DAPM_POST_PMD:
1520 ev_name = "POST_PMD";
1521 power = 0;
1522 break;
Mark Brown80114122013-02-25 15:14:19 +00001523 case SND_SOC_DAPM_WILL_PMU:
1524 ev_name = "WILL_PMU";
1525 power = 1;
1526 break;
1527 case SND_SOC_DAPM_WILL_PMD:
1528 ev_name = "WILL_PMD";
1529 power = 0;
1530 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001531 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001532 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001533 return;
1534 }
1535
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001536 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001537 return;
1538
1539 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001540 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001541 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001542 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001543 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001544 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001545 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001546 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001547 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001548 ev_name, w->name, ret);
1549 }
1550}
1551
Mark Brownb22ead22009-06-07 12:51:26 +01001552/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001553static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001554 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001555{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001556 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001557 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001558 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001559 unsigned int value = 0;
1560 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001561
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001562 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1563 reg = w->reg;
1564 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001565
1566 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001567 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001568 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001569
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001570 mask |= w->mask << w->shift;
1571 if (w->power)
1572 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001573 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001574 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001575
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001576 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001577 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1578 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001579
Mark Brown68f89ad2010-11-03 23:51:49 -04001580 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001581 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1582 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001583 }
1584
Mark Brown81628102009-06-07 13:21:24 +01001585 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001586 /* Any widget will do, they should all be updating the
1587 * same register.
1588 */
Mark Brown29376bc2011-06-19 13:49:28 +01001589
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001590 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001591 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001592 value, mask, reg, card->pop_time);
1593 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001594 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001595 }
1596
1597 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001598 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1599 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001600 }
Mark Brown42aa3412009-03-01 19:21:10 +00001601}
1602
Mark Brownb22ead22009-06-07 12:51:26 +01001603/* Apply a DAPM power sequence.
1604 *
1605 * We walk over a pre-sorted list of widgets to apply power to. In
1606 * order to minimise the number of writes to the device required
1607 * multiple widgets will be updated in a single write where possible.
1608 * Currently anything that requires more than a single write is not
1609 * handled.
1610 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001611static void dapm_seq_run(struct snd_soc_card *card,
1612 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001613{
1614 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001615 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001616 LIST_HEAD(pending);
1617 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001618 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001619 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001620 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001621 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001622 int *sort;
1623
1624 if (power_up)
1625 sort = dapm_up_seq;
1626 else
1627 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001628
Mark Brownb22ead22009-06-07 12:51:26 +01001629 list_for_each_entry_safe(w, n, list, power_list) {
1630 ret = 0;
1631
1632 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001633 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001634 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001635 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001636 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001637
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001638 if (cur_dapm && cur_dapm->component) {
Mark Brown474b62d2011-01-18 16:14:44 +00001639 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1640 if (sort[i] == cur_sort)
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001641 snd_soc_component_seq_notifier(
1642 cur_dapm->component,
1643 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001644 }
1645
Mark Browneb270e92013-10-09 13:52:52 +01001646 if (cur_dapm && w->dapm != cur_dapm)
1647 soc_dapm_async_complete(cur_dapm);
1648
Mark Brownb22ead22009-06-07 12:51:26 +01001649 INIT_LIST_HEAD(&pending);
1650 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001651 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001652 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001653 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001654 }
1655
Mark Brown163cac02009-06-07 10:12:52 +01001656 switch (w->id) {
1657 case snd_soc_dapm_pre:
1658 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001659 list_for_each_entry_safe_continue(w, n, list,
1660 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001661
Mark Brownb22ead22009-06-07 12:51:26 +01001662 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001663 ret = w->event(w,
1664 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001665 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001666 ret = w->event(w,
1667 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001668 break;
1669
1670 case snd_soc_dapm_post:
1671 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001672 list_for_each_entry_safe_continue(w, n, list,
1673 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001674
Mark Brownb22ead22009-06-07 12:51:26 +01001675 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001676 ret = w->event(w,
1677 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001678 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001679 ret = w->event(w,
1680 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001681 break;
1682
Mark Brown163cac02009-06-07 10:12:52 +01001683 default:
Mark Brown81628102009-06-07 13:21:24 +01001684 /* Queue it up for application */
1685 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001686 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001687 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001688 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001689 list_move(&w->power_list, &pending);
1690 break;
Mark Brown163cac02009-06-07 10:12:52 +01001691 }
Mark Brownb22ead22009-06-07 12:51:26 +01001692
1693 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001694 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001695 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001696 }
Mark Brownb22ead22009-06-07 12:51:26 +01001697
1698 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001699 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001700
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001701 if (cur_dapm && cur_dapm->component) {
Mark Brown474b62d2011-01-18 16:14:44 +00001702 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1703 if (sort[i] == cur_sort)
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +09001704 snd_soc_component_seq_notifier(
1705 cur_dapm->component,
1706 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001707 }
Mark Browneb270e92013-10-09 13:52:52 +01001708
1709 list_for_each_entry(d, &card->dapm_list, list) {
1710 soc_dapm_async_complete(d);
1711 }
Mark Brown163cac02009-06-07 10:12:52 +01001712}
1713
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001714static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001715{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001716 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001717 struct snd_soc_dapm_widget_list *wlist;
1718 struct snd_soc_dapm_widget *w = NULL;
1719 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001720 int ret;
1721
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001722 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001723 return;
1724
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001725 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001726
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001727 for (wi = 0; wi < wlist->num_widgets; wi++) {
1728 w = wlist->widgets[wi];
1729
1730 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1731 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1732 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001733 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001734 w->name, ret);
1735 }
Mark Brown97404f22010-12-14 16:13:57 +00001736 }
1737
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001738 if (!w)
1739 return;
1740
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001741 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1742 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001743 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001744 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001745 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001746
Chen-Yu Tsaie411b0b2016-11-02 15:35:58 +08001747 if (update->has_second_set) {
1748 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1749 update->mask2, update->val2);
1750 if (ret < 0)
1751 dev_err(w->dapm->dev,
1752 "ASoC: %s DAPM update failed: %d\n",
1753 w->name, ret);
1754 }
1755
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001756 for (wi = 0; wi < wlist->num_widgets; wi++) {
1757 w = wlist->widgets[wi];
1758
1759 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1760 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1761 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001762 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001763 w->name, ret);
1764 }
Mark Brown97404f22010-12-14 16:13:57 +00001765 }
1766}
1767
Mark Brown9d0624a2011-02-18 11:49:43 -08001768/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1769 * they're changing state.
1770 */
1771static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1772{
1773 struct snd_soc_dapm_context *d = data;
1774 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001775
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001776 /* If we're off and we're not supposed to go into STANDBY */
Mark Brown56fba412011-06-04 11:25:10 +01001777 if (d->bias_level == SND_SOC_BIAS_OFF &&
1778 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001779 if (d->dev)
1780 pm_runtime_get_sync(d->dev);
1781
Mark Brown9d0624a2011-02-18 11:49:43 -08001782 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1783 if (ret != 0)
1784 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001785 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001786 }
1787
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001788 /* Prepare for a transition to ON or away from ON */
1789 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1790 d->bias_level != SND_SOC_BIAS_ON) ||
1791 (d->target_bias_level != SND_SOC_BIAS_ON &&
1792 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001793 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1794 if (ret != 0)
1795 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001796 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001797 }
1798}
1799
1800/* Async callback run prior to DAPM sequences - brings to their final
1801 * state.
1802 */
1803static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1804{
1805 struct snd_soc_dapm_context *d = data;
1806 int ret;
1807
1808 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001809 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1810 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1811 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001812 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1813 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001814 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001815 ret);
1816 }
1817
1818 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001819 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1820 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001821 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1822 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001823 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1824 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001825
1826 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001827 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001828 }
1829
1830 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001831 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1832 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001833 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1834 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001835 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001836 ret);
1837 }
1838}
Mark Brown97404f22010-12-14 16:13:57 +00001839
Mark Brownfe4fda52011-10-03 22:36:57 +01001840static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1841 bool power, bool connect)
1842{
1843 /* If a connection is being made or broken then that update
1844 * will have marked the peer dirty, otherwise the widgets are
1845 * not connected and this update has no impact. */
1846 if (!connect)
1847 return;
1848
1849 /* If the peer is already in the state we're moving to then we
1850 * won't have an impact on it. */
1851 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001852 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001853}
1854
Mark Brown05623c42011-09-28 17:02:31 +01001855static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1856 struct list_head *up_list,
1857 struct list_head *down_list)
1858{
Mark Browndb432b42011-10-03 21:06:40 +01001859 struct snd_soc_dapm_path *path;
1860
Mark Brown05623c42011-09-28 17:02:31 +01001861 if (w->power == power)
1862 return;
1863
1864 trace_snd_soc_dapm_widget_power(w, power);
1865
Mark Browndb432b42011-10-03 21:06:40 +01001866 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001867 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001868 */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001869 snd_soc_dapm_widget_for_each_source_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001870 dapm_widget_set_peer_power(path->source, power, path->connect);
1871
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001872 /* Supplies can't affect their outputs, only their inputs */
1873 if (!w->is_supply) {
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001874 snd_soc_dapm_widget_for_each_sink_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001875 dapm_widget_set_peer_power(path->sink, power,
1876 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001877 }
1878
Mark Brown05623c42011-09-28 17:02:31 +01001879 if (power)
1880 dapm_seq_insert(w, up_list, true);
1881 else
1882 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001883}
1884
Mark Brown7c81beb2011-09-20 22:22:32 +01001885static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1886 struct list_head *up_list,
1887 struct list_head *down_list)
1888{
Mark Brown7c81beb2011-09-20 22:22:32 +01001889 int power;
1890
1891 switch (w->id) {
1892 case snd_soc_dapm_pre:
1893 dapm_seq_insert(w, down_list, false);
1894 break;
1895 case snd_soc_dapm_post:
1896 dapm_seq_insert(w, up_list, true);
1897 break;
1898
1899 default:
Mark Brownd805002b2011-09-28 18:28:23 +01001900 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001901
Mark Brown05623c42011-09-28 17:02:31 +01001902 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001903 break;
1904 }
1905}
1906
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001907static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1908{
1909 if (dapm->idle_bias_off)
1910 return true;
1911
1912 switch (snd_power_get_state(dapm->card->snd_card)) {
1913 case SNDRV_CTL_POWER_D3hot:
1914 case SNDRV_CTL_POWER_D3cold:
1915 return dapm->suspend_bias_off;
1916 default:
1917 break;
1918 }
1919
1920 return false;
1921}
1922
Mark Brown42aa3412009-03-01 19:21:10 +00001923/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001924 * Scan each dapm widget for complete audio path.
1925 * A complete path is a route that has valid endpoints i.e.:-
1926 *
1927 * o DAC to output pin.
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001928 * o Input pin to ADC.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001929 * o Input pin to Output pin (bypass, sidetone)
1930 * o DAC to ADC (loopback).
1931 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001932static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001933{
1934 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001935 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001936 LIST_HEAD(up_list);
1937 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001938 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001939 enum snd_soc_bias_level bias;
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +09001940 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001941
Mark Brownf9fa2b12014-03-06 16:49:11 +08001942 lockdep_assert_held(&card->dapm_mutex);
1943
Mark Brown84e90932010-11-04 00:07:02 -04001944 trace_snd_soc_dapm_start(card);
1945
Mark Brown56fba412011-06-04 11:25:10 +01001946 list_for_each_entry(d, &card->dapm_list, list) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001947 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001948 d->target_bias_level = SND_SOC_BIAS_OFF;
1949 else
1950 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001951 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001952
Liam Girdwood6c120e12012-02-15 15:15:34 +00001953 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001954
Mark Brown6d3ddc82009-05-16 17:47:29 +01001955 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001956 * lists indicating if they should be powered up or down. We
1957 * only check widgets that have been flagged as dirty but note
1958 * that new widgets may be added to the dirty list while we
1959 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001960 */
Mark Browndb432b42011-10-03 21:06:40 +01001961 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001962 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001963 }
1964
Mark Brownf9de6d72011-09-28 17:19:47 +01001965 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001966 switch (w->id) {
1967 case snd_soc_dapm_pre:
1968 case snd_soc_dapm_post:
1969 /* These widgets always need to be powered */
1970 break;
1971 default:
1972 list_del_init(&w->dirty);
1973 break;
1974 }
Mark Browndb432b42011-10-03 21:06:40 +01001975
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001976 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001977 d = w->dapm;
1978
1979 /* Supplies and micbiases only bring the
1980 * context up to STANDBY as unless something
1981 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001982 * generally don't require full power. Signal
1983 * generators are virtual pins and have no
1984 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001985 */
1986 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001987 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001988 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001989 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001990 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001991 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00001992 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001993 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001994 case snd_soc_dapm_micbias:
1995 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1996 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1997 break;
1998 default:
1999 d->target_bias_level = SND_SOC_BIAS_ON;
2000 break;
2001 }
2002 }
2003
2004 }
2005
Mark Brown85a843c2011-09-21 21:29:47 +01002006 /* Force all contexts in the card to the same bias state if
2007 * they're not ground referenced.
2008 */
Mark Brown56fba412011-06-04 11:25:10 +01002009 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09002010 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01002011 if (d->target_bias_level > bias)
2012 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09002013 list_for_each_entry(d, &card->dapm_list, list)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02002014 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01002015 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09002016
Mark Brownde02d072011-09-20 21:43:24 +01002017 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09002018
Xiang Xiao17282ba2014-03-02 00:04:03 +08002019 /* Run card bias changes at first */
2020 dapm_pre_sequence_async(&card->dapm, 0);
2021 /* Run other bias changes in parallel */
2022 list_for_each_entry(d, &card->dapm_list, list) {
Jon Huntere03546d2018-08-17 16:35:43 +01002023 if (d != &card->dapm && d->bias_level != d->target_bias_level)
Xiang Xiao17282ba2014-03-02 00:04:03 +08002024 async_schedule_domain(dapm_pre_sequence_async, d,
2025 &async_domain);
2026 }
Mark Brown9d0624a2011-02-18 11:49:43 -08002027 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01002028
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02002029 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002030 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00002031 }
2032
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02002033 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002034 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00002035 }
2036
Mark Brown6d3ddc82009-05-16 17:47:29 +01002037 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002038 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01002039
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002040 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00002041
Mark Brown6d3ddc82009-05-16 17:47:29 +01002042 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002043 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002044
Mark Brown9d0624a2011-02-18 11:49:43 -08002045 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08002046 list_for_each_entry(d, &card->dapm_list, list) {
Jon Huntere03546d2018-08-17 16:35:43 +01002047 if (d != &card->dapm && d->bias_level != d->target_bias_level)
Xiang Xiao17282ba2014-03-02 00:04:03 +08002048 async_schedule_domain(dapm_post_sequence_async, d,
2049 &async_domain);
2050 }
Mark Brown9d0624a2011-02-18 11:49:43 -08002051 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08002052 /* Run card bias changes at last */
2053 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01002054
Liam Girdwood8078d872012-02-15 15:15:35 +00002055 /* do we need to notify any clients that DAPM event is complete */
2056 list_for_each_entry(d, &card->dapm_list, list) {
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +09002057 if (!d->component)
2058 continue;
2059
2060 ret = snd_soc_component_stream_event(d->component, event);
2061 if (ret < 0)
2062 return ret;
Liam Girdwood8078d872012-02-15 15:15:35 +00002063 }
2064
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002065 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02002066 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02002067 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01002068
Mark Brown84e90932010-11-04 00:07:02 -04002069 trace_snd_soc_dapm_done(card);
2070
Mark Brown42aa3412009-03-01 19:21:10 +00002071 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002072}
2073
Mark Brown79fb9382009-08-21 16:38:13 +01002074#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01002075static ssize_t dapm_widget_power_read_file(struct file *file,
2076 char __user *user_buf,
2077 size_t count, loff_t *ppos)
2078{
2079 struct snd_soc_dapm_widget *w = file->private_data;
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002080 struct snd_soc_card *card = w->dapm->card;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002081 enum snd_soc_dapm_direction dir, rdir;
Mark Brown79fb9382009-08-21 16:38:13 +01002082 char *buf;
2083 int in, out;
2084 ssize_t ret;
2085 struct snd_soc_dapm_path *p = NULL;
2086
2087 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2088 if (!buf)
2089 return -ENOMEM;
2090
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002091 mutex_lock(&card->dapm_mutex);
2092
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002093 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2094 if (w->is_supply) {
2095 in = 0;
2096 out = 0;
2097 } else {
Piotr Stankiewicz67420642016-05-13 17:03:55 +01002098 in = is_connected_input_ep(w, NULL, NULL);
2099 out = is_connected_output_ep(w, NULL, NULL);
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002100 }
Mark Brown79fb9382009-08-21 16:38:13 +01002101
Silvio Cesaree581e152019-01-12 16:28:43 +01002102 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
Mark Brownf13ebad2012-03-03 18:01:01 +00002103 w->name, w->power ? "On" : "Off",
2104 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01002105
Mark Brownd033c362009-12-04 15:25:56 +00002106 if (w->reg >= 0)
Silvio Cesaree581e152019-01-12 16:28:43 +01002107 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002108 " - R%d(0x%x) mask 0x%x",
2109 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00002110
Silvio Cesaree581e152019-01-12 16:28:43 +01002111 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
Mark Brownd033c362009-12-04 15:25:56 +00002112
Mark Brown3eef08b2009-09-14 16:49:00 +01002113 if (w->sname)
Silvio Cesaree581e152019-01-12 16:28:43 +01002114 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
Mark Brown3eef08b2009-09-14 16:49:00 +01002115 w->sname,
2116 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01002117
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002118 snd_soc_dapm_for_each_direction(dir) {
2119 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2120 snd_soc_dapm_widget_for_each_path(w, dir, p) {
KaiChieh Chuang28735af2018-02-05 13:00:00 +08002121 if (p->connected && !p->connected(p->source, p->sink))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002122 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002123
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002124 if (!p->connect)
2125 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002126
Silvio Cesaree581e152019-01-12 16:28:43 +01002127 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002128 " %s \"%s\" \"%s\"\n",
2129 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
Mark Brown79fb9382009-08-21 16:38:13 +01002130 p->name ? p->name : "static",
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002131 p->node[rdir]->name);
2132 }
Mark Brown79fb9382009-08-21 16:38:13 +01002133 }
2134
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002135 mutex_unlock(&card->dapm_mutex);
2136
Mark Brown79fb9382009-08-21 16:38:13 +01002137 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2138
2139 kfree(buf);
2140 return ret;
2141}
2142
2143static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002144 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002145 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002146 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002147};
2148
Mark Brownef49e4f2011-04-04 20:48:13 +09002149static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2150 size_t count, loff_t *ppos)
2151{
2152 struct snd_soc_dapm_context *dapm = file->private_data;
2153 char *level;
2154
2155 switch (dapm->bias_level) {
2156 case SND_SOC_BIAS_ON:
2157 level = "On\n";
2158 break;
2159 case SND_SOC_BIAS_PREPARE:
2160 level = "Prepare\n";
2161 break;
2162 case SND_SOC_BIAS_STANDBY:
2163 level = "Standby\n";
2164 break;
2165 case SND_SOC_BIAS_OFF:
2166 level = "Off\n";
2167 break;
2168 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002169 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002170 level = "Unknown\n";
2171 break;
2172 }
2173
2174 return simple_read_from_buffer(user_buf, count, ppos, level,
2175 strlen(level));
2176}
2177
2178static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002179 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002180 .read = dapm_bias_read_file,
2181 .llseek = default_llseek,
2182};
2183
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002184void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2185 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002186{
Mark Brownceaea852019-06-21 12:33:57 +01002187 if (!parent || IS_ERR(parent))
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02002188 return;
2189
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002190 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2191
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +02002192 debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
2193 &dapm_bias_fops);
Mark Brown79fb9382009-08-21 16:38:13 +01002194}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002195
2196static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2197{
2198 struct snd_soc_dapm_context *dapm = w->dapm;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002199
2200 if (!dapm->debugfs_dapm || !w->name)
2201 return;
2202
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +02002203 debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
2204 &dapm_widget_power_fops);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002205}
2206
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002207static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2208{
2209 debugfs_remove_recursive(dapm->debugfs_dapm);
Kuninori Morimoto29040d12019-05-27 16:51:34 +09002210 dapm->debugfs_dapm = NULL;
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002211}
2212
Mark Brown79fb9382009-08-21 16:38:13 +01002213#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002214void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2215 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002216{
2217}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002218
2219static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2220{
2221}
2222
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002223static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2224{
2225}
2226
Mark Brown79fb9382009-08-21 16:38:13 +01002227#endif
2228
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002229/*
2230 * soc_dapm_connect_path() - Connects or disconnects a path
2231 * @path: The path to update
2232 * @connect: The new connect state of the path. True if the path is connected,
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002233 * false if it is disconnected.
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002234 * @reason: The reason why the path changed (for debugging only)
2235 */
2236static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2237 bool connect, const char *reason)
2238{
2239 if (path->connect == connect)
2240 return;
2241
2242 path->connect = connect;
2243 dapm_mark_dirty(path->source, reason);
2244 dapm_mark_dirty(path->sink, reason);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002245 dapm_path_invalidate(path);
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002246}
2247
Richard Purdie2b97eab2006-10-06 18:32:18 +02002248/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002249static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002250 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002251{
2252 struct snd_soc_dapm_path *path;
2253 int found = 0;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002254 bool connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002255
Mark Brownf9fa2b12014-03-06 16:49:11 +08002256 lockdep_assert_held(&card->dapm_mutex);
2257
Richard Purdie2b97eab2006-10-06 18:32:18 +02002258 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002259 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002260 found = 1;
2261 /* we now need to match the string in the enum to the path */
Jerome Brunetc3456a42019-05-15 15:18:55 +02002262 if (e && !(strcmp(path->name, e->texts[mux])))
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002263 connect = true;
2264 else
2265 connect = false;
2266
2267 soc_dapm_connect_path(path, connect, "mux update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002268 }
2269
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002270 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002271 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002272
Liam Girdwood618dae12012-04-25 12:12:51 +01002273 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002274}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002275
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002276int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002277 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2278 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002279{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002280 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002281 int ret;
2282
Liam Girdwood3cd04342012-03-09 12:02:08 +00002283 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002284 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002285 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002286 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002287 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002288 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002289 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002290 return ret;
2291}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002292EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002293
Milan plzik1b075e32008-01-10 14:39:46 +01002294/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002295static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002296 struct snd_kcontrol *kcontrol,
2297 int connect, int rconnect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002298{
2299 struct snd_soc_dapm_path *path;
2300 int found = 0;
2301
Mark Brownf9fa2b12014-03-06 16:49:11 +08002302 lockdep_assert_held(&card->dapm_mutex);
2303
Richard Purdie2b97eab2006-10-06 18:32:18 +02002304 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002305 dapm_kcontrol_for_each_path(path, kcontrol) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002306 /*
2307 * Ideally this function should support any number of
2308 * paths and channels. But since kcontrols only come
2309 * in mono and stereo variants, we are limited to 2
2310 * channels.
2311 *
2312 * The following code assumes for stereo controls the
2313 * first path (when 'found == 0') is the left channel,
2314 * and all remaining paths (when 'found == 1') are the
2315 * right channel.
2316 *
2317 * A stereo control is signified by a valid 'rconnect'
2318 * value, either 0 for unconnected, or >= 0 for connected.
2319 * This is chosen instead of using snd_soc_volsw_is_stereo,
2320 * so that the behavior of snd_soc_dapm_mixer_update_power
2321 * doesn't change even when the kcontrol passed in is
2322 * stereo.
2323 *
2324 * It passes 'connect' as the path connect status for
2325 * the left channel, and 'rconnect' for the right
2326 * channel.
2327 */
2328 if (found && rconnect >= 0)
2329 soc_dapm_connect_path(path, rconnect, "mixer update");
2330 else
2331 soc_dapm_connect_path(path, connect, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002332 found = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002333 }
2334
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002335 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002336 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002337
Liam Girdwood618dae12012-04-25 12:12:51 +01002338 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002339}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002340
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002341int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002342 struct snd_kcontrol *kcontrol, int connect,
2343 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002344{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002345 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002346 int ret;
2347
Liam Girdwood3cd04342012-03-09 12:02:08 +00002348 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002349 card->update = update;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002350 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002351 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002352 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002353 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002354 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002355 return ret;
2356}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002357EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002358
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002359static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2360 char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002361{
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002362 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002363 struct snd_soc_dapm_widget *w;
2364 int count = 0;
2365 char *state = "not set";
2366
Mark Brown47325072016-03-18 12:04:23 +00002367 /* card won't be set for the dummy component, as a spot fix
2368 * we're checking for that case specifically here but in future
2369 * we will ensure that the dummy component looks like others.
2370 */
2371 if (!cmpnt->card)
2372 return 0;
2373
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002374 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2375 if (w->dapm != dapm)
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002376 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002377
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002378 /* only display widgets that burn power */
Richard Purdie2b97eab2006-10-06 18:32:18 +02002379 switch (w->id) {
2380 case snd_soc_dapm_hp:
2381 case snd_soc_dapm_mic:
2382 case snd_soc_dapm_spk:
2383 case snd_soc_dapm_line:
2384 case snd_soc_dapm_micbias:
2385 case snd_soc_dapm_dac:
2386 case snd_soc_dapm_adc:
2387 case snd_soc_dapm_pga:
Seppo Ingalsuo882c8b4a2019-04-30 18:11:34 -05002388 case snd_soc_dapm_effect:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002389 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002390 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002391 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002392 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002393 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00002394 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002395 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002396 if (w->name)
2397 count += sprintf(buf + count, "%s: %s\n",
2398 w->name, w->power ? "On":"Off");
2399 break;
2400 default:
2401 break;
2402 }
2403 }
2404
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002405 switch (snd_soc_dapm_get_bias_level(dapm)) {
Mark Brown0be98982008-05-19 12:31:28 +02002406 case SND_SOC_BIAS_ON:
2407 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002408 break;
Mark Brown0be98982008-05-19 12:31:28 +02002409 case SND_SOC_BIAS_PREPARE:
2410 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002411 break;
Mark Brown0be98982008-05-19 12:31:28 +02002412 case SND_SOC_BIAS_STANDBY:
2413 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002414 break;
Mark Brown0be98982008-05-19 12:31:28 +02002415 case SND_SOC_BIAS_OFF:
2416 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002417 break;
2418 }
2419 count += sprintf(buf + count, "PM State: %s\n", state);
2420
2421 return count;
2422}
2423
Benoit Cousson44ba2642014-07-08 23:19:36 +02002424/* show dapm widget status in sys fs */
2425static ssize_t dapm_widget_show(struct device *dev,
2426 struct device_attribute *attr, char *buf)
2427{
2428 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002429 struct snd_soc_dai *codec_dai;
Benoit Cousson44ba2642014-07-08 23:19:36 +02002430 int i, count = 0;
2431
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002432 mutex_lock(&rtd->card->dapm_mutex);
2433
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002434 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2435 struct snd_soc_component *cmpnt = codec_dai->component;
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002436
2437 count += dapm_widget_show_component(cmpnt, buf + count);
Benoit Cousson44ba2642014-07-08 23:19:36 +02002438 }
2439
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002440 mutex_unlock(&rtd->card->dapm_mutex);
2441
Benoit Cousson44ba2642014-07-08 23:19:36 +02002442 return count;
2443}
2444
Joe Perchesc828a892017-12-19 10:15:08 -08002445static DEVICE_ATTR_RO(dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002446
Takashi Iwaid29697d2015-01-30 20:16:37 +01002447struct attribute *soc_dapm_dev_attrs[] = {
2448 &dev_attr_dapm_widget.attr,
2449 NULL
2450};
Richard Purdie2b97eab2006-10-06 18:32:18 +02002451
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002452static void dapm_free_path(struct snd_soc_dapm_path *path)
2453{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002454 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2455 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002456 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002457 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002458 kfree(path);
2459}
2460
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002461void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2462{
2463 struct snd_soc_dapm_path *p, *next_p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002464 enum snd_soc_dapm_direction dir;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002465
2466 list_del(&w->list);
2467 /*
2468 * remove source and sink paths associated to this widget.
2469 * While removing the path, remove reference to it from both
2470 * source and sink widgets so that path is removed only once.
2471 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002472 snd_soc_dapm_for_each_direction(dir) {
2473 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2474 dapm_free_path(p);
2475 }
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002476
2477 kfree(w->kcontrols);
Lars-Peter Clausen48068962015-07-21 18:11:08 +02002478 kfree_const(w->name);
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -06002479 kfree_const(w->sname);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002480 kfree(w);
2481}
2482
Jyri Sarhafd589a12015-11-10 18:12:42 +02002483void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2484{
2485 dapm->path_sink_cache.widget = NULL;
2486 dapm->path_source_cache.widget = NULL;
2487}
2488
Richard Purdie2b97eab2006-10-06 18:32:18 +02002489/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002490static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002491{
2492 struct snd_soc_dapm_widget *w, *next_w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002493
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002494 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2495 if (w->dapm != dapm)
2496 continue;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002497 snd_soc_dapm_free_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002498 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002499 snd_soc_dapm_reset_cache(dapm);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002500}
2501
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002502static struct snd_soc_dapm_widget *dapm_find_widget(
2503 struct snd_soc_dapm_context *dapm, const char *pin,
2504 bool search_other_contexts)
2505{
2506 struct snd_soc_dapm_widget *w;
2507 struct snd_soc_dapm_widget *fallback = NULL;
2508
2509 list_for_each_entry(w, &dapm->card->widgets, list) {
2510 if (!strcmp(w->name, pin)) {
2511 if (w->dapm == dapm)
2512 return w;
2513 else
2514 fallback = w;
2515 }
2516 }
2517
2518 if (search_other_contexts)
2519 return fallback;
2520
2521 return NULL;
2522}
2523
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002524static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002525 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002526{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002527 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002528
Mark Brownf9fa2b12014-03-06 16:49:11 +08002529 dapm_assert_locked(dapm);
2530
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002531 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002532 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002533 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002534 }
2535
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002536 if (w->connected != status) {
Mark Brown1a8b2d92012-02-16 11:50:07 -08002537 dapm_mark_dirty(w, "pin configuration");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002538 dapm_widget_invalidate_input_paths(w);
2539 dapm_widget_invalidate_output_paths(w);
2540 }
Mark Brown1a8b2d92012-02-16 11:50:07 -08002541
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002542 w->connected = status;
2543 if (status == 0)
2544 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002545
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002546 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002547}
2548
Richard Purdie2b97eab2006-10-06 18:32:18 +02002549/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002550 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2551 * @dapm: DAPM context
2552 *
2553 * Walks all dapm audio paths and powers widgets according to their
2554 * stream or path usage.
2555 *
2556 * Requires external locking.
2557 *
2558 * Returns 0 for success.
2559 */
2560int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2561{
2562 /*
2563 * Suppress early reports (eg, jacks syncing their state) to avoid
2564 * silly DAPM runs during card startup.
2565 */
2566 if (!dapm->card || !dapm->card->instantiated)
2567 return 0;
2568
2569 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2570}
2571EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2572
2573/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002574 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002575 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002576 *
2577 * Walks all dapm audio paths and powers widgets according to their
2578 * stream or path usage.
2579 *
2580 * Returns 0 for success.
2581 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002582int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002583{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002584 int ret;
2585
Liam Girdwood3cd04342012-03-09 12:02:08 +00002586 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002587 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002588 mutex_unlock(&dapm->card->dapm_mutex);
2589 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002590}
Liam Girdwooda5302182008-07-07 13:35:17 +01002591EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002592
Charles Keepax078a85f2019-01-31 13:30:18 +00002593static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2594 struct snd_soc_dapm_widget *w,
2595 int channels)
2596{
2597 switch (w->id) {
2598 case snd_soc_dapm_aif_out:
2599 case snd_soc_dapm_aif_in:
2600 break;
2601 default:
2602 return 0;
2603 }
2604
2605 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2606 w->channel < channels ? "Connecting" : "Disconnecting",
2607 p->source->name, p->sink->name);
2608
2609 if (w->channel < channels)
2610 soc_dapm_connect_path(p, true, "dai update");
2611 else
2612 soc_dapm_connect_path(p, false, "dai update");
2613
2614 return 0;
2615}
2616
2617static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2618 struct snd_pcm_hw_params *params,
2619 struct snd_soc_dai *dai)
2620{
2621 int dir = substream->stream;
2622 int channels = params_channels(params);
2623 struct snd_soc_dapm_path *p;
2624 struct snd_soc_dapm_widget *w;
2625 int ret;
2626
2627 if (dir == SNDRV_PCM_STREAM_PLAYBACK)
2628 w = dai->playback_widget;
2629 else
2630 w = dai->capture_widget;
2631
Charles Keepaxcf17a5f2019-02-06 11:13:59 +00002632 if (!w)
2633 return 0;
2634
Charles Keepax078a85f2019-01-31 13:30:18 +00002635 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2636 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2637
2638 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2639 ret = dapm_update_dai_chan(p, p->sink, channels);
2640 if (ret < 0)
2641 return ret;
2642 }
2643
2644 snd_soc_dapm_widget_for_each_source_path(w, p) {
2645 ret = dapm_update_dai_chan(p, p->source, channels);
2646 if (ret < 0)
2647 return ret;
2648 }
2649
2650 return 0;
2651}
2652
2653int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2654 struct snd_pcm_hw_params *params,
2655 struct snd_soc_dai *dai)
2656{
2657 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2658 int ret;
2659
2660 mutex_lock_nested(&rtd->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2661 ret = dapm_update_dai_unlocked(substream, params, dai);
2662 mutex_unlock(&rtd->card->dapm_mutex);
2663
2664 return ret;
2665}
2666EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2667
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002668/*
2669 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2670 * @w: The widget for which to update the flags
2671 *
2672 * Some widgets have a dynamic category which depends on which neighbors they
2673 * are connected to. This function update the category for these widgets.
2674 *
2675 * This function must be called whenever a path is added or removed to a widget.
2676 */
2677static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2678{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002679 enum snd_soc_dapm_direction dir;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002680 struct snd_soc_dapm_path *p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002681 unsigned int ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002682
2683 switch (w->id) {
2684 case snd_soc_dapm_input:
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002685 /* On a fully routed card an input is never a source */
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002686 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002687 return;
2688 ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002689 snd_soc_dapm_widget_for_each_source_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002690 if (p->source->id == snd_soc_dapm_micbias ||
2691 p->source->id == snd_soc_dapm_mic ||
2692 p->source->id == snd_soc_dapm_line ||
2693 p->source->id == snd_soc_dapm_output) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002694 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002695 break;
2696 }
2697 }
2698 break;
2699 case snd_soc_dapm_output:
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002700 /* On a fully routed card a output is never a sink */
2701 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002702 return;
2703 ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002704 snd_soc_dapm_widget_for_each_sink_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002705 if (p->sink->id == snd_soc_dapm_spk ||
2706 p->sink->id == snd_soc_dapm_hp ||
2707 p->sink->id == snd_soc_dapm_line ||
2708 p->sink->id == snd_soc_dapm_input) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002709 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002710 break;
2711 }
2712 }
2713 break;
2714 case snd_soc_dapm_line:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002715 ep = 0;
2716 snd_soc_dapm_for_each_direction(dir) {
2717 if (!list_empty(&w->edges[dir]))
2718 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2719 }
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002720 break;
2721 default:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002722 return;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002723 }
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002724
2725 w->is_ep = ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002726}
2727
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002728static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2729 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2730 const char *control)
2731{
2732 bool dynamic_source = false;
2733 bool dynamic_sink = false;
2734
2735 if (!control)
2736 return 0;
2737
2738 switch (source->id) {
2739 case snd_soc_dapm_demux:
2740 dynamic_source = true;
2741 break;
2742 default:
2743 break;
2744 }
2745
2746 switch (sink->id) {
2747 case snd_soc_dapm_mux:
2748 case snd_soc_dapm_switch:
2749 case snd_soc_dapm_mixer:
2750 case snd_soc_dapm_mixer_named_ctl:
2751 dynamic_sink = true;
2752 break;
2753 default:
2754 break;
2755 }
2756
2757 if (dynamic_source && dynamic_sink) {
2758 dev_err(dapm->dev,
2759 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2760 source->name, control, sink->name);
2761 return -EINVAL;
2762 } else if (!dynamic_source && !dynamic_sink) {
2763 dev_err(dapm->dev,
2764 "Control not supported for path %s -> [%s] -> %s\n",
2765 source->name, control, sink->name);
2766 return -EINVAL;
2767 }
2768
2769 return 0;
2770}
2771
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002772static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2773 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2774 const char *control,
2775 int (*connected)(struct snd_soc_dapm_widget *source,
2776 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002777{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002778 struct snd_soc_dapm_widget *widgets[2];
2779 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002780 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002781 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002782
Lars-Peter Clausene409dfb2014-10-25 17:42:02 +02002783 if (wsink->is_supply && !wsource->is_supply) {
2784 dev_err(dapm->dev,
2785 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2786 wsource->name, wsink->name);
2787 return -EINVAL;
2788 }
2789
2790 if (connected && !wsource->is_supply) {
2791 dev_err(dapm->dev,
2792 "connected() callback only supported for supply widgets (%s -> %s)\n",
2793 wsource->name, wsink->name);
2794 return -EINVAL;
2795 }
2796
2797 if (wsource->is_supply && control) {
2798 dev_err(dapm->dev,
2799 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2800 wsource->name, control, wsink->name);
2801 return -EINVAL;
2802 }
2803
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002804 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2805 if (ret)
2806 return ret;
2807
Richard Purdie2b97eab2006-10-06 18:32:18 +02002808 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2809 if (!path)
2810 return -ENOMEM;
2811
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002812 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2813 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2814 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2815 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2816
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002817 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002818 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002819 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002820
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002821 if (wsource->is_supply || wsink->is_supply)
2822 path->is_supply = 1;
2823
Richard Purdie2b97eab2006-10-06 18:32:18 +02002824 /* connect static paths */
2825 if (control == NULL) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002826 path->connect = 1;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002827 } else {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002828 switch (wsource->id) {
2829 case snd_soc_dapm_demux:
2830 ret = dapm_connect_mux(dapm, path, control, wsource);
2831 if (ret)
2832 goto err;
2833 break;
2834 default:
2835 break;
2836 }
2837
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002838 switch (wsink->id) {
2839 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002840 ret = dapm_connect_mux(dapm, path, control, wsink);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002841 if (ret != 0)
2842 goto err;
2843 break;
2844 case snd_soc_dapm_switch:
2845 case snd_soc_dapm_mixer:
2846 case snd_soc_dapm_mixer_named_ctl:
2847 ret = dapm_connect_mixer(dapm, path, control);
2848 if (ret != 0)
2849 goto err;
2850 break;
2851 default:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002852 break;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002853 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002854 }
2855
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002856 list_add(&path->list, &dapm->card->paths);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002857 snd_soc_dapm_for_each_direction(dir)
2858 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002859
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002860 snd_soc_dapm_for_each_direction(dir) {
2861 dapm_update_widget_flags(widgets[dir]);
2862 dapm_mark_dirty(widgets[dir], "Route added");
2863 }
Mark Brownfabd0382012-07-05 17:20:06 +01002864
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002865 if (dapm->card->instantiated && path->connect)
2866 dapm_path_invalidate(path);
2867
Richard Purdie2b97eab2006-10-06 18:32:18 +02002868 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002869err:
2870 kfree(path);
2871 return ret;
2872}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002873
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002874static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002875 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002876{
2877 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2878 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2879 const char *sink;
2880 const char *source;
2881 char prefixed_sink[80];
2882 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002883 const char *prefix;
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002884 unsigned int sink_ref = 0;
2885 unsigned int source_ref = 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002886 int ret;
2887
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002888 prefix = soc_dapm_prefix(dapm);
2889 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002890 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002891 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002892 sink = prefixed_sink;
2893 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002894 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002895 source = prefixed_source;
2896 } else {
2897 sink = route->sink;
2898 source = route->source;
2899 }
2900
Charles Keepax45a110a2015-05-11 13:50:30 +01002901 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2902 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2903
2904 if (wsink && wsource)
2905 goto skip_search;
2906
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002907 /*
2908 * find src and dest widgets over all widgets but favor a widget from
2909 * current DAPM context
2910 */
2911 list_for_each_entry(w, &dapm->card->widgets, list) {
2912 if (!wsink && !(strcmp(w->name, sink))) {
2913 wtsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002914 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002915 wsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002916 if (wsource)
2917 break;
2918 }
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002919 sink_ref++;
2920 if (sink_ref > 1)
2921 dev_warn(dapm->dev,
2922 "ASoC: sink widget %s overwritten\n",
2923 w->name);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002924 continue;
2925 }
2926 if (!wsource && !(strcmp(w->name, source))) {
2927 wtsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002928 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002929 wsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002930 if (wsink)
2931 break;
2932 }
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002933 source_ref++;
2934 if (source_ref > 1)
2935 dev_warn(dapm->dev,
2936 "ASoC: source widget %s overwritten\n",
2937 w->name);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002938 }
2939 }
2940 /* use widget from another DAPM context if not found from this */
2941 if (!wsink)
2942 wsink = wtsink;
2943 if (!wsource)
2944 wsource = wtsource;
2945
2946 if (wsource == NULL) {
2947 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2948 route->source);
2949 return -ENODEV;
2950 }
2951 if (wsink == NULL) {
2952 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2953 route->sink);
2954 return -ENODEV;
2955 }
2956
Charles Keepax45a110a2015-05-11 13:50:30 +01002957skip_search:
2958 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2959 dapm_wcache_update(&dapm->path_source_cache, wsource);
2960
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002961 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2962 route->connected);
2963 if (ret)
2964 goto err;
2965
2966 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002967err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002968 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002969 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002970 return ret;
2971}
Mark Brown105f1c22008-05-13 14:52:19 +02002972
Mark Brownefcc3c62012-07-05 17:24:19 +01002973static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2974 const struct snd_soc_dapm_route *route)
2975{
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002976 struct snd_soc_dapm_widget *wsource, *wsink;
Mark Brownefcc3c62012-07-05 17:24:19 +01002977 struct snd_soc_dapm_path *path, *p;
2978 const char *sink;
2979 const char *source;
2980 char prefixed_sink[80];
2981 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002982 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002983
2984 if (route->control) {
2985 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002986 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002987 return -EINVAL;
2988 }
2989
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002990 prefix = soc_dapm_prefix(dapm);
2991 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002992 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002993 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002994 sink = prefixed_sink;
2995 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002996 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002997 source = prefixed_source;
2998 } else {
2999 sink = route->sink;
3000 source = route->source;
3001 }
3002
3003 path = NULL;
3004 list_for_each_entry(p, &dapm->card->paths, list) {
3005 if (strcmp(p->source->name, source) != 0)
3006 continue;
3007 if (strcmp(p->sink->name, sink) != 0)
3008 continue;
3009 path = p;
3010 break;
3011 }
3012
3013 if (path) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003014 wsource = path->source;
3015 wsink = path->sink;
3016
3017 dapm_mark_dirty(wsource, "Route removed");
3018 dapm_mark_dirty(wsink, "Route removed");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003019 if (path->connect)
3020 dapm_path_invalidate(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01003021
Lars-Peter Clausen88722932013-06-14 13:16:53 +02003022 dapm_free_path(path);
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003023
3024 /* Update any path related flags */
3025 dapm_update_widget_flags(wsource);
3026 dapm_update_widget_flags(wsink);
Mark Brownefcc3c62012-07-05 17:24:19 +01003027 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003028 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01003029 source, sink);
3030 }
3031
3032 return 0;
3033}
3034
Mark Brown105f1c22008-05-13 14:52:19 +02003035/**
Mark Brown105f1c22008-05-13 14:52:19 +02003036 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003037 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02003038 * @route: audio routes
3039 * @num: number of routes
3040 *
3041 * Connects 2 dapm widgets together via a named audio path. The sink is
3042 * the widget receiving the audio signal, whilst the source is the sender
3043 * of the audio signal.
3044 *
3045 * Returns 0 for success else error. On error all resources can be freed
3046 * with a call to snd_soc_card_free().
3047 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003048int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02003049 const struct snd_soc_dapm_route *route, int num)
3050{
Mark Brown62d4a4b2012-06-22 12:21:49 +01003051 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02003052
Stuart Hendersonf19c1812017-09-21 11:02:12 +01003053 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown105f1c22008-05-13 14:52:19 +02003054 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02003055 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01003056 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003057 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
3058 route->source,
3059 route->control ? route->control : "direct",
3060 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01003061 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02003062 }
3063 route++;
3064 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003065 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02003066
Dan Carpenter60884c22012-04-13 22:25:43 +03003067 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02003068}
3069EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3070
Mark Brownefcc3c62012-07-05 17:24:19 +01003071/**
3072 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3073 * @dapm: DAPM context
3074 * @route: audio routes
3075 * @num: number of routes
3076 *
3077 * Removes routes from the DAPM context.
3078 */
3079int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3080 const struct snd_soc_dapm_route *route, int num)
3081{
Rajan Vajae066ea22016-02-11 11:23:35 +05303082 int i;
Mark Brownefcc3c62012-07-05 17:24:19 +01003083
Stuart Hendersonf19c1812017-09-21 11:02:12 +01003084 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownefcc3c62012-07-05 17:24:19 +01003085 for (i = 0; i < num; i++) {
3086 snd_soc_dapm_del_route(dapm, route);
3087 route++;
3088 }
3089 mutex_unlock(&dapm->card->dapm_mutex);
3090
Rajan Vajae066ea22016-02-11 11:23:35 +05303091 return 0;
Mark Brownefcc3c62012-07-05 17:24:19 +01003092}
3093EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3094
Mark Brownbf3a9e12011-06-13 16:42:29 +01003095static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3096 const struct snd_soc_dapm_route *route)
3097{
3098 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3099 route->source,
3100 true);
3101 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3102 route->sink,
3103 true);
3104 struct snd_soc_dapm_path *path;
3105 int count = 0;
3106
3107 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003108 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003109 route->source);
3110 return -ENODEV;
3111 }
3112
3113 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003114 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003115 route->sink);
3116 return -ENODEV;
3117 }
3118
3119 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003120 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003121 route->source, route->sink);
3122
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02003123 snd_soc_dapm_widget_for_each_sink_path(source, path) {
Mark Brownbf3a9e12011-06-13 16:42:29 +01003124 if (path->sink == sink) {
3125 path->weak = 1;
3126 count++;
3127 }
3128 }
3129
3130 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003131 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003132 route->source, route->sink);
3133 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003134 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003135 count, route->source, route->sink);
3136
3137 return 0;
3138}
3139
3140/**
3141 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3142 * @dapm: DAPM context
3143 * @route: audio routes
3144 * @num: number of routes
3145 *
3146 * Mark existing routes matching those specified in the passed array
3147 * as being weak, meaning that they are ignored for the purpose of
3148 * power decisions. The main intended use case is for sidetone paths
3149 * which couple audio between other independent paths if they are both
3150 * active in order to make the combination work better at the user
3151 * level but which aren't intended to be "used".
3152 *
3153 * Note that CODEC drivers should not use this as sidetone type paths
3154 * can frequently also be used as bypass paths.
3155 */
3156int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3157 const struct snd_soc_dapm_route *route, int num)
3158{
3159 int i, err;
3160 int ret = 0;
3161
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003162 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01003163 for (i = 0; i < num; i++) {
3164 err = snd_soc_dapm_weak_route(dapm, route);
3165 if (err)
3166 ret = err;
3167 route++;
3168 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003169 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01003170
3171 return ret;
3172}
3173EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3174
Mark Brown105f1c22008-05-13 14:52:19 +02003175/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003176 * snd_soc_dapm_new_widgets - add new dapm widgets
Jonathan Corbet628536e2015-08-25 01:14:48 -06003177 * @card: card to be checked for new dapm widgets
Richard Purdie2b97eab2006-10-06 18:32:18 +02003178 *
3179 * Checks the codec for any new dapm widgets and creates them if found.
3180 *
3181 * Returns 0 for success.
3182 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02003183int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003184{
3185 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00003186 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003187
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003188 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003189
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003190 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003191 {
3192 if (w->new)
3193 continue;
3194
Stephen Warrenfad59882011-04-28 17:37:59 -06003195 if (w->num_kcontrols) {
Kees Cook6396bb22018-06-12 14:03:40 -07003196 w->kcontrols = kcalloc(w->num_kcontrols,
Stephen Warrenfad59882011-04-28 17:37:59 -06003197 sizeof(struct snd_kcontrol *),
3198 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003199 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003200 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06003201 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003202 }
Stephen Warrenfad59882011-04-28 17:37:59 -06003203 }
3204
Richard Purdie2b97eab2006-10-06 18:32:18 +02003205 switch(w->id) {
3206 case snd_soc_dapm_switch:
3207 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00003208 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003209 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003210 break;
3211 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003212 case snd_soc_dapm_demux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003213 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003214 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003215 case snd_soc_dapm_pga:
Seppo Ingalsuo882c8b4a2019-04-30 18:11:34 -05003216 case snd_soc_dapm_effect:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06003217 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003218 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003219 break;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003220 case snd_soc_dapm_dai_link:
3221 dapm_new_dai_link(w);
3222 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01003223 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003224 break;
3225 }
Mark Brownb66a70d2011-02-09 18:04:11 +00003226
3227 /* Read the initial power state from the device */
3228 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003229 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08003230 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003231 val &= w->mask;
3232 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00003233 w->power = 1;
3234 }
3235
Richard Purdie2b97eab2006-10-06 18:32:18 +02003236 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003237
Mark Brown7508b122011-10-05 12:09:12 +01003238 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003239 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003240 }
3241
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003242 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3243 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003244 return 0;
3245}
3246EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3247
3248/**
3249 * snd_soc_dapm_get_volsw - dapm mixer get callback
3250 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003251 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003252 *
3253 * Callback to get the value of a dapm mixer control.
3254 *
3255 * Returns 0 for success.
3256 */
3257int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3258 struct snd_ctl_elem_value *ucontrol)
3259{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003260 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3261 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003262 struct soc_mixer_control *mc =
3263 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003264 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003265 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003266 int max = mc->max;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003267 unsigned int width = fls(max);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003268 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003269 unsigned int invert = mc->invert;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003270 unsigned int reg_val, val, rval = 0;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003271 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003272
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003273 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003274 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003275 ret = soc_dapm_read(dapm, reg, &reg_val);
3276 val = (reg_val >> shift) & mask;
3277
3278 if (ret == 0 && reg != mc->rreg)
3279 ret = soc_dapm_read(dapm, mc->rreg, &reg_val);
3280
3281 if (snd_soc_volsw_is_stereo(mc))
3282 rval = (reg_val >> mc->rshift) & mask;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003283 } else {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003284 reg_val = dapm_kcontrol_get_value(kcontrol);
3285 val = reg_val & mask;
3286
3287 if (snd_soc_volsw_is_stereo(mc))
3288 rval = (reg_val >> width) & mask;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003289 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003290 mutex_unlock(&card->dapm_mutex);
3291
Chen-Yu Tsai01ad5e72016-08-27 19:27:58 +08003292 if (ret)
3293 return ret;
3294
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003295 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003296 ucontrol->value.integer.value[0] = max - val;
3297 else
3298 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003299
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003300 if (snd_soc_volsw_is_stereo(mc)) {
3301 if (invert)
3302 ucontrol->value.integer.value[1] = max - rval;
3303 else
3304 ucontrol->value.integer.value[1] = rval;
3305 }
3306
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003307 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003308}
3309EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3310
3311/**
3312 * snd_soc_dapm_put_volsw - dapm mixer set callback
3313 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003314 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003315 *
3316 * Callback to set the value of a dapm mixer control.
3317 *
3318 * Returns 0 for success.
3319 */
3320int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3321 struct snd_ctl_elem_value *ucontrol)
3322{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003323 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3324 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003325 struct soc_mixer_control *mc =
3326 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003327 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003328 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003329 int max = mc->max;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003330 unsigned int width = fls(max);
3331 unsigned int mask = (1 << width) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003332 unsigned int invert = mc->invert;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003333 unsigned int val, rval = 0;
3334 int connect, rconnect = -1, change, reg_change = 0;
Fabio Estevam33d92452018-02-14 13:39:05 -02003335 struct snd_soc_dapm_update update = {};
Nenghua Cao52765972013-12-13 20:13:49 +08003336 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003337
3338 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02003339 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003340
3341 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003342 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003343
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003344 if (snd_soc_volsw_is_stereo(mc)) {
3345 rval = (ucontrol->value.integer.value[1] & mask);
3346 rconnect = !!rval;
3347 if (invert)
3348 rval = max - rval;
3349 }
3350
Liam Girdwood3cd04342012-03-09 12:02:08 +00003351 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003352
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003353 /* This assumes field width < (bits in unsigned int / 2) */
3354 if (width > sizeof(unsigned int) * 8 / 2)
3355 dev_warn(dapm->dev,
3356 "ASoC: control %s field width limit exceeded\n",
3357 kcontrol->id.name);
3358 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
Mark Brown283375c2009-12-07 18:09:03 +00003359
Jarkko Nikula18626c72014-06-09 14:20:29 +03003360 if (reg != SND_SOC_NOPM) {
Jarkko Nikula18626c72014-06-09 14:20:29 +03003361 val = val << shift;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003362 rval = rval << mc->rshift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02003363
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003364 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3365
3366 if (snd_soc_volsw_is_stereo(mc))
3367 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3368 mask << mc->rshift,
3369 rval);
Jarkko Nikula18626c72014-06-09 14:20:29 +03003370 }
3371
3372 if (change || reg_change) {
3373 if (reg_change) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003374 if (snd_soc_volsw_is_stereo(mc)) {
3375 update.has_second_set = true;
3376 update.reg2 = mc->rreg;
3377 update.mask2 = mask << mc->rshift;
3378 update.val2 = rval;
3379 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003380 update.kcontrol = kcontrol;
3381 update.reg = reg;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003382 update.mask = mask << shift;
Jarkko Nikula18626c72014-06-09 14:20:29 +03003383 update.val = val;
3384 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003385 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003386 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00003387
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003388 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3389 rconnect);
Mark Brown97404f22010-12-14 16:13:57 +00003390
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003391 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00003392 }
3393
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003394 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003395
3396 if (ret > 0)
3397 soc_dpcm_runtime_update(card);
3398
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003399 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003400}
3401EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3402
3403/**
3404 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3405 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003406 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003407 *
3408 * Callback to get the value of a dapm enumerated double mixer control.
3409 *
3410 * Returns 0 for success.
3411 */
3412int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3413 struct snd_ctl_elem_value *ucontrol)
3414{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003415 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Charles Keepax561ed682015-05-01 12:37:26 +01003416 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003417 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003418 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003419
Charles Keepax561ed682015-05-01 12:37:26 +01003420 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3421 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003422 int ret = soc_dapm_read(dapm, e->reg, &reg_val);
Charles Keepax964a0b82015-05-08 10:50:10 +01003423 if (ret) {
3424 mutex_unlock(&card->dapm_mutex);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003425 return ret;
Charles Keepax964a0b82015-05-08 10:50:10 +01003426 }
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003427 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003428 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003429 }
Charles Keepax561ed682015-05-01 12:37:26 +01003430 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003431
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003432 val = (reg_val >> e->shift_l) & e->mask;
3433 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3434 if (e->shift_l != e->shift_r) {
3435 val = (reg_val >> e->shift_r) & e->mask;
3436 val = snd_soc_enum_val_to_item(e, val);
3437 ucontrol->value.enumerated.item[1] = val;
3438 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003439
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003440 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003441}
3442EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3443
3444/**
3445 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3446 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003447 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003448 *
3449 * Callback to set the value of a dapm enumerated double mixer control.
3450 *
3451 * Returns 0 for success.
3452 */
3453int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3454 struct snd_ctl_elem_value *ucontrol)
3455{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003456 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3457 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003458 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003459 unsigned int *item = ucontrol->value.enumerated.item;
Charles Keepax561ed682015-05-01 12:37:26 +01003460 unsigned int val, change, reg_change = 0;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003461 unsigned int mask;
Fabio Estevam33d92452018-02-14 13:39:05 -02003462 struct snd_soc_dapm_update update = {};
Nenghua Cao52765972013-12-13 20:13:49 +08003463 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003464
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003465 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003466 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003467
3468 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003469 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003470 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003471 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003472 return -EINVAL;
Chen-Yu Tsai071133a2016-08-27 19:27:59 +08003473 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003474 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003475 }
3476
Liam Girdwood3cd04342012-03-09 12:02:08 +00003477 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003478
Charles Keepax561ed682015-05-01 12:37:26 +01003479 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00003480
Charles Keepax561ed682015-05-01 12:37:26 +01003481 if (e->reg != SND_SOC_NOPM)
3482 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3483
3484 if (change || reg_change) {
3485 if (reg_change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003486 update.kcontrol = kcontrol;
3487 update.reg = e->reg;
3488 update.mask = mask;
3489 update.val = val;
3490 card->update = &update;
3491 }
Charles Keepax561ed682015-05-01 12:37:26 +01003492 change |= reg_change;
Mark Brown3a655772009-10-05 17:23:30 +01003493
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003494 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003495
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003496 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003497 }
3498
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003499 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003500
3501 if (ret > 0)
3502 soc_dpcm_runtime_update(card);
3503
Mark Brown97404f22010-12-14 16:13:57 +00003504 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003505}
3506EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3507
3508/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003509 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3510 *
3511 * @kcontrol: mixer control
3512 * @uinfo: control element information
3513 *
3514 * Callback to provide information about a pin switch control.
3515 */
3516int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3517 struct snd_ctl_elem_info *uinfo)
3518{
3519 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3520 uinfo->count = 1;
3521 uinfo->value.integer.min = 0;
3522 uinfo->value.integer.max = 1;
3523
3524 return 0;
3525}
3526EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3527
3528/**
3529 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3530 *
3531 * @kcontrol: mixer control
3532 * @ucontrol: Value
3533 */
3534int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3535 struct snd_ctl_elem_value *ucontrol)
3536{
Mark Brown48a8c392012-02-14 17:11:15 -08003537 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003538 const char *pin = (const char *)kcontrol->private_value;
3539
Liam Girdwood3cd04342012-03-09 12:02:08 +00003540 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003541
3542 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003543 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003544
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003545 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003546
3547 return 0;
3548}
3549EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3550
3551/**
3552 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3553 *
3554 * @kcontrol: mixer control
3555 * @ucontrol: Value
3556 */
3557int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3558 struct snd_ctl_elem_value *ucontrol)
3559{
Mark Brown48a8c392012-02-14 17:11:15 -08003560 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003561 const char *pin = (const char *)kcontrol->private_value;
3562
Mark Brown8b37dbd2009-02-28 21:14:20 +00003563 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003564 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003565 else
Mark Brown48a8c392012-02-14 17:11:15 -08003566 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003567
Mark Brown48a8c392012-02-14 17:11:15 -08003568 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003569 return 0;
3570}
3571EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3572
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +01003573struct snd_soc_dapm_widget *
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003574snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003575 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003576{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003577 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003578 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003579 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00003580 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003581
3582 if ((w = dapm_cnew_widget(widget)) == NULL)
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003583 return ERR_PTR(-ENOMEM);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003584
Mark Brown62ea8742012-01-21 21:14:48 +00003585 switch (w->id) {
3586 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003587 w->regulator = devm_regulator_get(dapm->dev, w->name);
3588 if (IS_ERR(w->regulator)) {
3589 ret = PTR_ERR(w->regulator);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003590 goto request_failed;
Mark Brown62ea8742012-01-21 21:14:48 +00003591 }
Mark Brown8784c772013-01-10 19:33:47 +00003592
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003593 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003594 ret = regulator_allow_bypass(w->regulator, true);
3595 if (ret != 0)
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003596 dev_warn(dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003597 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003598 w->name, ret);
3599 }
Mark Brown62ea8742012-01-21 21:14:48 +00003600 break;
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003601 case snd_soc_dapm_pinctrl:
3602 w->pinctrl = devm_pinctrl_get(dapm->dev);
Charles Keepaxa5cd7e92018-08-28 14:35:03 +01003603 if (IS_ERR(w->pinctrl)) {
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003604 ret = PTR_ERR(w->pinctrl);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003605 goto request_failed;
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003606 }
3607 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003608 case snd_soc_dapm_clock_supply:
Mark Brown695594f12012-06-04 08:14:13 +01003609 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003610 if (IS_ERR(w->clk)) {
3611 ret = PTR_ERR(w->clk);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003612 goto request_failed;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003613 }
3614 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003615 default:
3616 break;
3617 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003618
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003619 prefix = soc_dapm_prefix(dapm);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003620 if (prefix)
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003621 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003622 else
Lars-Peter Clausen48068962015-07-21 18:11:08 +02003623 w->name = kstrdup_const(widget->name, GFP_KERNEL);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003624 if (w->name == NULL) {
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -06003625 kfree_const(w->sname);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003626 kfree(w);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003627 return ERR_PTR(-ENOMEM);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003628 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003629
Mark Brown7ca3a182011-10-08 14:04:50 +01003630 switch (w->id) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003631 case snd_soc_dapm_mic:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003632 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003633 w->power_check = dapm_generic_check_power;
3634 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003635 case snd_soc_dapm_input:
3636 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003637 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003638 w->power_check = dapm_generic_check_power;
3639 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003640 case snd_soc_dapm_spk:
3641 case snd_soc_dapm_hp:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003642 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003643 w->power_check = dapm_generic_check_power;
3644 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003645 case snd_soc_dapm_output:
3646 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003647 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003648 w->power_check = dapm_generic_check_power;
3649 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003650 case snd_soc_dapm_vmid:
3651 case snd_soc_dapm_siggen:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003652 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003653 w->power_check = dapm_always_on_check_power;
3654 break;
Vinod Koul56b44372015-11-23 21:22:30 +05303655 case snd_soc_dapm_sink:
3656 w->is_ep = SND_SOC_DAPM_EP_SINK;
3657 w->power_check = dapm_always_on_check_power;
3658 break;
3659
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003660 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003661 case snd_soc_dapm_demux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003662 case snd_soc_dapm_switch:
3663 case snd_soc_dapm_mixer:
3664 case snd_soc_dapm_mixer_named_ctl:
Mark Brown63c69a62013-07-18 22:03:01 +01003665 case snd_soc_dapm_adc:
3666 case snd_soc_dapm_aif_out:
3667 case snd_soc_dapm_dac:
3668 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003669 case snd_soc_dapm_pga:
Ranjani Sridharan6e3bfcf2019-03-01 19:08:53 -06003670 case snd_soc_dapm_buffer:
3671 case snd_soc_dapm_scheduler:
3672 case snd_soc_dapm_effect:
3673 case snd_soc_dapm_src:
3674 case snd_soc_dapm_asrc:
3675 case snd_soc_dapm_encoder:
3676 case snd_soc_dapm_decoder:
Mark Brown7ca3a182011-10-08 14:04:50 +01003677 case snd_soc_dapm_out_drv:
Mark Brown7ca3a182011-10-08 14:04:50 +01003678 case snd_soc_dapm_micbias:
Mark Brown7ca3a182011-10-08 14:04:50 +01003679 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003680 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02003681 case snd_soc_dapm_dai_out:
3682 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003683 w->power_check = dapm_generic_check_power;
3684 break;
3685 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003686 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003687 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003688 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003689 case snd_soc_dapm_kcontrol:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003690 w->is_supply = 1;
Mark Brown7ca3a182011-10-08 14:04:50 +01003691 w->power_check = dapm_supply_check_power;
3692 break;
3693 default:
3694 w->power_check = dapm_always_on_check_power;
3695 break;
3696 }
3697
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003698 w->dapm = dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003699 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003700 INIT_LIST_HEAD(&w->dirty);
Lars-Peter Clausen92fa1242015-05-01 18:02:42 +02003701 list_add_tail(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003702
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003703 snd_soc_dapm_for_each_direction(dir) {
3704 INIT_LIST_HEAD(&w->edges[dir]);
3705 w->endpoints[dir] = -1;
3706 }
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003707
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02003708 /* machine layer sets up unconnected pins and insertions */
Richard Purdie2b97eab2006-10-06 18:32:18 +02003709 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003710 return w;
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003711
3712request_failed:
3713 if (ret != -EPROBE_DEFER)
3714 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3715 w->name, ret);
3716
Wenwen Wang45004d62019-07-22 08:57:44 -05003717 kfree_const(w->sname);
3718 kfree(w);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003719 return ERR_PTR(ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003720}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003721
3722/**
Charles Keepax94e630a2018-09-05 15:20:59 +01003723 * snd_soc_dapm_new_control - create new dapm control
3724 * @dapm: DAPM context
3725 * @widget: widget template
3726 *
3727 * Creates new DAPM control based upon a template.
3728 *
3729 * Returns a widget pointer on success or an error pointer on failure
3730 */
3731struct snd_soc_dapm_widget *
3732snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3733 const struct snd_soc_dapm_widget *widget)
3734{
3735 struct snd_soc_dapm_widget *w;
3736
3737 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3738 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3739 mutex_unlock(&dapm->card->dapm_mutex);
3740
3741 return w;
3742}
3743EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3744
3745/**
Mark Brown4ba13272008-05-13 14:51:19 +02003746 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003747 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003748 * @widget: widget array
3749 * @num: number of widgets
3750 *
3751 * Creates new DAPM controls based upon the templates.
3752 *
3753 * Returns 0 for success else error.
3754 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003755int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003756 const struct snd_soc_dapm_widget *widget,
3757 int num)
3758{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003759 struct snd_soc_dapm_widget *w;
3760 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003761 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003762
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003763 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003764 for (i = 0; i < num; i++) {
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003765 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
Linus Walleij37e1df82017-01-13 10:23:52 +01003766 if (IS_ERR(w)) {
3767 ret = PTR_ERR(w);
Dan Carpenter60884c22012-04-13 22:25:43 +03003768 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003769 }
Mark Brown4ba13272008-05-13 14:51:19 +02003770 widget++;
3771 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003772 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003773 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003774}
3775EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3776
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003777static int
3778snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3779 struct snd_pcm_substream *substream)
Mark Brownc74184e2012-04-04 22:12:09 +01003780{
Charles Keepax4a75aae2018-09-05 15:21:01 +01003781 struct snd_soc_dapm_path *path;
Mark Brownc74184e2012-04-04 22:12:09 +01003782 struct snd_soc_dai *source, *sink;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003783 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Mark Brown9747cec2012-04-26 19:12:21 +01003784 struct snd_pcm_hw_params *params = NULL;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003785 const struct snd_soc_pcm_stream *config = NULL;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003786 struct snd_pcm_runtime *runtime = NULL;
Takashi Iwai3ba66fe2018-07-25 22:43:26 +02003787 unsigned int fmt;
Colin Ian Kingc8415832019-07-26 13:33:27 +01003788 int ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003789
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003790 params = kzalloc(sizeof(*params), GFP_KERNEL);
3791 if (!params)
3792 return -ENOMEM;
3793
Jerome Bruneta72706e2019-07-25 18:59:48 +02003794 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3795 if (!runtime) {
3796 ret = -ENOMEM;
3797 goto out;
3798 }
3799
3800 substream->runtime = runtime;
3801
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003802 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3803 snd_soc_dapm_widget_for_each_source_path(w, path) {
3804 source = path->source->priv;
3805
3806 ret = snd_soc_dai_startup(source, substream);
3807 if (ret < 0) {
3808 dev_err(source->dev,
3809 "ASoC: startup() failed: %d\n", ret);
3810 goto out;
3811 }
3812 source->active++;
3813 }
3814
3815 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3816 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3817 sink = path->sink->priv;
3818
3819 ret = snd_soc_dai_startup(sink, substream);
3820 if (ret < 0) {
3821 dev_err(sink->dev,
3822 "ASoC: startup() failed: %d\n", ret);
3823 goto out;
3824 }
3825 sink->active++;
3826 }
3827
Jerome Bruneta72706e2019-07-25 18:59:48 +02003828 substream->hw_opened = 1;
3829
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003830 /*
3831 * Note: getting the config after .startup() gives a chance to
3832 * either party on the link to alter the configuration if
3833 * necessary
3834 */
Charles Keepax243bcfa2018-09-05 15:21:02 +01003835 config = rtd->dai_link->params + rtd->params_select;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003836 if (WARN_ON(!config)) {
3837 dev_err(w->dapm->dev, "ASoC: link config missing\n");
3838 ret = -EINVAL;
3839 goto out;
3840 }
Mark Brownc74184e2012-04-04 22:12:09 +01003841
Mark Brownc74184e2012-04-04 22:12:09 +01003842 /* Be a little careful as we don't want to overflow the mask array */
3843 if (config->formats) {
3844 fmt = ffs(config->formats) - 1;
3845 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003846 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003847 config->formats);
Mark Brownc74184e2012-04-04 22:12:09 +01003848
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003849 ret = -EINVAL;
Mark Brown9747cec2012-04-26 19:12:21 +01003850 goto out;
3851 }
Mark Brownc74184e2012-04-04 22:12:09 +01003852
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003853 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brown9747cec2012-04-26 19:12:21 +01003854 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003855 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003856 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003857 config->rate_max;
Mark Brown9747cec2012-04-26 19:12:21 +01003858 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003859 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003860 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003861 = config->channels_max;
3862
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003863 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3864 snd_soc_dapm_widget_for_each_source_path(w, path) {
3865 source = path->source->priv;
3866
3867 ret = snd_soc_dai_hw_params(source, substream, params);
3868 if (ret < 0)
3869 goto out;
3870
3871 dapm_update_dai_unlocked(substream, params, source);
3872 }
3873
3874 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3875 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3876 sink = path->sink->priv;
3877
3878 ret = snd_soc_dai_hw_params(sink, substream, params);
3879 if (ret < 0)
3880 goto out;
3881
3882 dapm_update_dai_unlocked(substream, params, sink);
3883 }
3884
Jerome Brunet9de98622019-07-25 18:59:49 +02003885 runtime->format = params_format(params);
3886 runtime->subformat = params_subformat(params);
3887 runtime->channels = params_channels(params);
3888 runtime->rate = params_rate(params);
3889
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003890out:
Jerome Bruneta72706e2019-07-25 18:59:48 +02003891 if (ret < 0)
3892 kfree(runtime);
3893
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003894 kfree(params);
Colin Ian Kingc8415832019-07-26 13:33:27 +01003895 return ret;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003896}
3897
3898static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3899 struct snd_kcontrol *kcontrol, int event)
3900{
3901 struct snd_soc_dapm_path *path;
3902 struct snd_soc_dai *source, *sink;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003903 struct snd_pcm_substream *substream = w->priv;
3904 int ret = 0, saved_stream = substream->stream;
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003905
3906 if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3907 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3908 return -EINVAL;
3909
Mark Brownc74184e2012-04-04 22:12:09 +01003910 switch (event) {
3911 case SND_SOC_DAPM_PRE_PMU:
Jerome Bruneta72706e2019-07-25 18:59:48 +02003912 ret = snd_soc_dai_link_event_pre_pmu(w, substream);
Jerome Brunet3dcfb392019-07-25 18:59:46 +02003913 if (ret < 0)
3914 goto out;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003915
Mark Brownc74184e2012-04-04 22:12:09 +01003916 break;
3917
3918 case SND_SOC_DAPM_POST_PMU:
Charles Keepax4a75aae2018-09-05 15:21:01 +01003919 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3920 sink = path->sink->priv;
3921
3922 ret = snd_soc_dai_digital_mute(sink, 0,
3923 SNDRV_PCM_STREAM_PLAYBACK);
3924 if (ret != 0 && ret != -ENOTSUPP)
3925 dev_warn(sink->dev,
3926 "ASoC: Failed to unmute: %d\n", ret);
3927 ret = 0;
3928 }
Mark Brownc74184e2012-04-04 22:12:09 +01003929 break;
3930
3931 case SND_SOC_DAPM_PRE_PMD:
Charles Keepax4a75aae2018-09-05 15:21:01 +01003932 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3933 sink = path->sink->priv;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303934
Charles Keepax4a75aae2018-09-05 15:21:01 +01003935 ret = snd_soc_dai_digital_mute(sink, 1,
3936 SNDRV_PCM_STREAM_PLAYBACK);
3937 if (ret != 0 && ret != -ENOTSUPP)
3938 dev_warn(sink->dev,
3939 "ASoC: Failed to mute: %d\n", ret);
3940 ret = 0;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303941 }
3942
Jerome Bruneta72706e2019-07-25 18:59:48 +02003943 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003944 snd_soc_dapm_widget_for_each_source_path(w, path) {
3945 source = path->source->priv;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003946 snd_soc_dai_hw_free(source, substream);
Jerome Brunet68c907f2019-07-25 18:59:44 +02003947 }
Charles Keepax3c01b0e2018-10-11 17:28:28 +01003948
Jerome Bruneta72706e2019-07-25 18:59:48 +02003949 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
Jerome Brunet68c907f2019-07-25 18:59:44 +02003950 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3951 sink = path->sink->priv;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003952 snd_soc_dai_hw_free(sink, substream);
Jerome Brunet68c907f2019-07-25 18:59:44 +02003953 }
3954
Jerome Bruneta72706e2019-07-25 18:59:48 +02003955 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
Jerome Brunet68c907f2019-07-25 18:59:44 +02003956 snd_soc_dapm_widget_for_each_source_path(w, path) {
3957 source = path->source->priv;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003958 source->active--;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003959 snd_soc_dai_shutdown(source, substream);
Charles Keepax4a75aae2018-09-05 15:21:01 +01003960 }
3961
Jerome Bruneta72706e2019-07-25 18:59:48 +02003962 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003963 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3964 sink = path->sink->priv;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003965 sink->active--;
Jerome Bruneta72706e2019-07-25 18:59:48 +02003966 snd_soc_dai_shutdown(sink, substream);
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303967 }
Mark Brownc74184e2012-04-04 22:12:09 +01003968 break;
3969
Jerome Bruneta72706e2019-07-25 18:59:48 +02003970 case SND_SOC_DAPM_POST_PMD:
3971 kfree(substream->runtime);
3972 break;
3973
Mark Brownc74184e2012-04-04 22:12:09 +01003974 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003975 WARN(1, "Unknown event %d\n", event);
Sudip Mukherjee75881df2015-09-10 18:01:44 +05303976 ret = -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003977 }
3978
Mark Brown9747cec2012-04-26 19:12:21 +01003979out:
Jerome Bruneta72706e2019-07-25 18:59:48 +02003980 /* Restore the substream direction */
3981 substream->stream = saved_stream;
Mark Brown9747cec2012-04-26 19:12:21 +01003982 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003983}
3984
Nikesh Oswalc6615082015-02-02 17:06:44 +00003985static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3986 struct snd_ctl_elem_value *ucontrol)
3987{
3988 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
Charles Keepax243bcfa2018-09-05 15:21:02 +01003989 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003990
Charles Keepax243bcfa2018-09-05 15:21:02 +01003991 ucontrol->value.enumerated.item[0] = rtd->params_select;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003992
3993 return 0;
3994}
3995
3996static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3997 struct snd_ctl_elem_value *ucontrol)
3998{
3999 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
Charles Keepax243bcfa2018-09-05 15:21:02 +01004000 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004001
4002 /* Can't change the config when widget is already powered */
4003 if (w->power)
4004 return -EBUSY;
4005
Charles Keepax243bcfa2018-09-05 15:21:02 +01004006 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
Nikesh Oswalc6615082015-02-02 17:06:44 +00004007 return 0;
4008
Charles Keepax243bcfa2018-09-05 15:21:02 +01004009 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
Nikesh Oswalc6615082015-02-02 17:06:44 +00004010 return -EINVAL;
4011
Charles Keepax243bcfa2018-09-05 15:21:02 +01004012 rtd->params_select = ucontrol->value.enumerated.item[0];
Nikesh Oswalc6615082015-02-02 17:06:44 +00004013
4014 return 0;
4015}
4016
Arnd Bergmannc42c5ac2017-10-10 11:20:11 +02004017static void
anish kumar19ad6832017-09-28 21:52:39 -07004018snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
4019 unsigned long *private_value,
4020 int num_params,
4021 const char **w_param_text)
Mark Brownc74184e2012-04-04 22:12:09 +01004022{
anish kumar19ad6832017-09-28 21:52:39 -07004023 int count;
4024
4025 devm_kfree(card->dev, (void *)*private_value);
Pankaj Bharadiyacacea3a2019-03-22 18:00:09 +05304026
4027 if (!w_param_text)
4028 return;
4029
anish kumar19ad6832017-09-28 21:52:39 -07004030 for (count = 0 ; count < num_params; count++)
4031 devm_kfree(card->dev, (void *)w_param_text[count]);
4032 devm_kfree(card->dev, w_param_text);
4033}
4034
4035static struct snd_kcontrol_new *
4036snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
4037 char *link_name,
4038 const struct snd_soc_pcm_stream *params,
4039 int num_params, const char **w_param_text,
4040 unsigned long *private_value)
4041{
Nikesh Oswalc6615082015-02-02 17:06:44 +00004042 struct soc_enum w_param_enum[] = {
4043 SOC_ENUM_SINGLE(0, 0, 0, NULL),
4044 };
4045 struct snd_kcontrol_new kcontrol_dai_link[] = {
4046 SOC_ENUM_EXT(NULL, w_param_enum[0],
4047 snd_soc_dapm_dai_link_get,
4048 snd_soc_dapm_dai_link_put),
4049 };
anish kumar19ad6832017-09-28 21:52:39 -07004050 struct snd_kcontrol_new *kcontrol_news;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004051 const struct snd_soc_pcm_stream *config = params;
anish kumar19ad6832017-09-28 21:52:39 -07004052 int count;
Mark Brownc74184e2012-04-04 22:12:09 +01004053
Nikesh Oswalc6615082015-02-02 17:06:44 +00004054 for (count = 0 ; count < num_params; count++) {
4055 if (!config->stream_name) {
4056 dev_warn(card->dapm.dev,
4057 "ASoC: anonymous config %d for dai link %s\n",
4058 count, link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004059 w_param_text[count] =
Charles Keepax46172b62015-03-25 11:22:35 +00004060 devm_kasprintf(card->dev, GFP_KERNEL,
4061 "Anonymous Configuration %d",
4062 count);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004063 } else {
4064 w_param_text[count] = devm_kmemdup(card->dev,
4065 config->stream_name,
4066 strlen(config->stream_name) + 1,
4067 GFP_KERNEL);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004068 }
anish kumar19ad6832017-09-28 21:52:39 -07004069 if (!w_param_text[count])
4070 goto outfree_w_param;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004071 config++;
4072 }
anish kumar19ad6832017-09-28 21:52:39 -07004073
Nikesh Oswalc6615082015-02-02 17:06:44 +00004074 w_param_enum[0].items = num_params;
4075 w_param_enum[0].texts = w_param_text;
Mark Brownc74184e2012-04-04 22:12:09 +01004076
anish kumar19ad6832017-09-28 21:52:39 -07004077 *private_value =
4078 (unsigned long) devm_kmemdup(card->dev,
4079 (void *)(kcontrol_dai_link[0].private_value),
4080 sizeof(struct soc_enum), GFP_KERNEL);
4081 if (!*private_value) {
4082 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4083 link_name);
4084 goto outfree_w_param;
4085 }
4086 kcontrol_dai_link[0].private_value = *private_value;
4087 /* duplicate kcontrol_dai_link on heap so that memory persists */
4088 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4089 sizeof(struct snd_kcontrol_new),
4090 GFP_KERNEL);
4091 if (!kcontrol_news) {
4092 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4093 link_name);
4094 goto outfree_w_param;
4095 }
4096 return kcontrol_news;
4097
4098outfree_w_param:
4099 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
4100 return NULL;
4101}
4102
Charles Keepax778ff5b2018-09-05 15:21:00 +01004103static struct snd_soc_dapm_widget *
Jerome Bruneta72706e2019-07-25 18:59:48 +02004104snd_soc_dapm_new_dai(struct snd_soc_card *card,
4105 struct snd_pcm_substream *substream,
Jerome Brunet054d6502019-07-25 18:59:45 +02004106 char *id)
anish kumar19ad6832017-09-28 21:52:39 -07004107{
Jerome Bruneta72706e2019-07-25 18:59:48 +02004108 struct snd_soc_pcm_runtime *rtd = substream->private_data;
anish kumar19ad6832017-09-28 21:52:39 -07004109 struct snd_soc_dapm_widget template;
4110 struct snd_soc_dapm_widget *w;
4111 const char **w_param_text;
Pankaj Bharadiya8633d442019-03-22 21:53:39 +05304112 unsigned long private_value = 0;
anish kumar19ad6832017-09-28 21:52:39 -07004113 char *link_name;
4114 int ret;
4115
4116 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
Jerome Brunet054d6502019-07-25 18:59:45 +02004117 rtd->dai_link->name, id);
anish kumar19ad6832017-09-28 21:52:39 -07004118 if (!link_name)
Charles Keepax778ff5b2018-09-05 15:21:00 +01004119 return ERR_PTR(-ENOMEM);
anish kumar19ad6832017-09-28 21:52:39 -07004120
Mark Brownc74184e2012-04-04 22:12:09 +01004121 memset(&template, 0, sizeof(template));
4122 template.reg = SND_SOC_NOPM;
4123 template.id = snd_soc_dapm_dai_link;
4124 template.name = link_name;
4125 template.event = snd_soc_dai_link_event;
4126 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
Jerome Bruneta72706e2019-07-25 18:59:48 +02004127 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD;
anish kumar19ad6832017-09-28 21:52:39 -07004128 template.kcontrol_news = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01004129
anish kumar19ad6832017-09-28 21:52:39 -07004130 /* allocate memory for control, only in case of multiple configs */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004131 if (rtd->dai_link->num_params > 1) {
4132 w_param_text = devm_kcalloc(card->dev,
4133 rtd->dai_link->num_params,
4134 sizeof(char *), GFP_KERNEL);
anish kumar19ad6832017-09-28 21:52:39 -07004135 if (!w_param_text) {
4136 ret = -ENOMEM;
4137 goto param_fail;
4138 }
4139
4140 template.num_kcontrols = 1;
4141 template.kcontrol_news =
4142 snd_soc_dapm_alloc_kcontrol(card,
Charles Keepax778ff5b2018-09-05 15:21:00 +01004143 link_name,
4144 rtd->dai_link->params,
4145 rtd->dai_link->num_params,
anish kumar19ad6832017-09-28 21:52:39 -07004146 w_param_text, &private_value);
4147 if (!template.kcontrol_news) {
4148 ret = -ENOMEM;
4149 goto param_fail;
4150 }
Arnd Bergmann667ebc92017-10-10 11:20:10 +02004151 } else {
4152 w_param_text = NULL;
anish kumar19ad6832017-09-28 21:52:39 -07004153 }
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004154 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01004155
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004156 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
Dan Carpenter2e558a82018-09-07 22:40:33 +03004157 if (IS_ERR(w)) {
4158 ret = PTR_ERR(w);
Linus Walleij37e1df82017-01-13 10:23:52 +01004159 goto outfree_kcontrol_news;
Dan Carpenter2e558a82018-09-07 22:40:33 +03004160 }
Mark Brownc74184e2012-04-04 22:12:09 +01004161
Jerome Bruneta72706e2019-07-25 18:59:48 +02004162 w->priv = substream;
Mark Brownc74184e2012-04-04 22:12:09 +01004163
Charles Keepax778ff5b2018-09-05 15:21:00 +01004164 return w;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004165
Nikesh Oswalc6615082015-02-02 17:06:44 +00004166outfree_kcontrol_news:
4167 devm_kfree(card->dev, (void *)template.kcontrol_news);
Charles Keepax778ff5b2018-09-05 15:21:00 +01004168 snd_soc_dapm_free_kcontrol(card, &private_value,
4169 rtd->dai_link->num_params, w_param_text);
anish kumar19ad6832017-09-28 21:52:39 -07004170param_fail:
Nikesh Oswalc6615082015-02-02 17:06:44 +00004171 devm_kfree(card->dev, link_name);
Charles Keepax778ff5b2018-09-05 15:21:00 +01004172 return ERR_PTR(ret);
Mark Brownc74184e2012-04-04 22:12:09 +01004173}
4174
Mark Brown888df392012-02-16 19:37:51 -08004175int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4176 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004177{
Mark Brown888df392012-02-16 19:37:51 -08004178 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004179 struct snd_soc_dapm_widget *w;
4180
Mark Brown888df392012-02-16 19:37:51 -08004181 WARN_ON(dapm->dev != dai->dev);
4182
4183 memset(&template, 0, sizeof(template));
4184 template.reg = SND_SOC_NOPM;
4185
4186 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01004187 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08004188 template.name = dai->driver->playback.stream_name;
4189 template.sname = dai->driver->playback.stream_name;
4190
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004191 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08004192 template.name);
4193
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004194 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01004195 if (IS_ERR(w))
4196 return PTR_ERR(w);
Mark Brown888df392012-02-16 19:37:51 -08004197
4198 w->priv = dai;
4199 dai->playback_widget = w;
4200 }
4201
4202 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01004203 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08004204 template.name = dai->driver->capture.stream_name;
4205 template.sname = dai->driver->capture.stream_name;
4206
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004207 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08004208 template.name);
4209
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004210 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01004211 if (IS_ERR(w))
4212 return PTR_ERR(w);
Mark Brown888df392012-02-16 19:37:51 -08004213
4214 w->priv = dai;
4215 dai->capture_widget = w;
4216 }
4217
4218 return 0;
4219}
4220
4221int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4222{
4223 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004224 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08004225 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08004226
4227 /* For each DAI widget... */
4228 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01004229 switch (dai_w->id) {
4230 case snd_soc_dapm_dai_in:
4231 case snd_soc_dapm_dai_out:
4232 break;
4233 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02004234 continue;
Mark Brown46162742013-06-05 19:36:11 +01004235 }
Mark Brown888df392012-02-16 19:37:51 -08004236
Liam Girdwoode01b4f62018-06-14 20:26:42 +01004237 /* let users know there is no DAI to link */
4238 if (!dai_w->priv) {
4239 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4240 dai_w->name);
4241 continue;
4242 }
4243
Mark Brown888df392012-02-16 19:37:51 -08004244 dai = dai_w->priv;
4245
4246 /* ...find all widgets with the same stream and link them */
4247 list_for_each_entry(w, &card->widgets, list) {
4248 if (w->dapm != dai_w->dapm)
4249 continue;
4250
Mark Brown46162742013-06-05 19:36:11 +01004251 switch (w->id) {
4252 case snd_soc_dapm_dai_in:
4253 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08004254 continue;
Mark Brown46162742013-06-05 19:36:11 +01004255 default:
4256 break;
4257 }
Mark Brown888df392012-02-16 19:37:51 -08004258
Lars-Peter Clausena798c242015-07-21 11:51:35 +02004259 if (!w->sname || !strstr(w->sname, dai_w->sname))
Mark Brown888df392012-02-16 19:37:51 -08004260 continue;
4261
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004262 if (dai_w->id == snd_soc_dapm_dai_in) {
4263 src = dai_w;
4264 sink = w;
4265 } else {
4266 src = w;
4267 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08004268 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004269 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4270 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004271 }
4272 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02004273
Mark Brown888df392012-02-16 19:37:51 -08004274 return 0;
4275}
Liam Girdwood64a648c2011-07-25 11:15:15 +01004276
Benoit Cousson44ba2642014-07-08 23:19:36 +02004277static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4278 struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004279{
Benoit Cousson44ba2642014-07-08 23:19:36 +02004280 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00004281 struct snd_soc_dai *codec_dai;
Charles Keepax778ff5b2018-09-05 15:21:00 +01004282 struct snd_soc_dapm_widget *playback = NULL, *capture = NULL;
4283 struct snd_soc_dapm_widget *codec, *playback_cpu, *capture_cpu;
Jerome Bruneta72706e2019-07-25 18:59:48 +02004284 struct snd_pcm_substream *substream;
4285 struct snd_pcm_str *streams = rtd->pcm->streams;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004286 int i;
4287
Charles Keepax778ff5b2018-09-05 15:21:00 +01004288 if (rtd->dai_link->params) {
Charles Keepax778ff5b2018-09-05 15:21:00 +01004289 playback_cpu = cpu_dai->capture_widget;
4290 capture_cpu = cpu_dai->playback_widget;
4291 } else {
4292 playback = cpu_dai->playback_widget;
4293 capture = cpu_dai->capture_widget;
4294 playback_cpu = playback;
4295 capture_cpu = capture;
4296 }
4297
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00004298 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004299 /* connect BE DAI playback if widgets are valid */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004300 codec = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004301
Charles Keepax778ff5b2018-09-05 15:21:00 +01004302 if (playback_cpu && codec) {
4303 if (!playback) {
Jerome Bruneta72706e2019-07-25 18:59:48 +02004304 substream = streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
4305 playback = snd_soc_dapm_new_dai(card, substream,
Jerome Brunet054d6502019-07-25 18:59:45 +02004306 "playback");
Charles Keepaxe36a1d02018-09-10 15:28:39 +01004307 if (IS_ERR(playback)) {
4308 dev_err(rtd->dev,
4309 "ASoC: Failed to create DAI %s: %ld\n",
4310 codec_dai->name,
4311 PTR_ERR(playback));
4312 continue;
4313 }
Charles Keepax778ff5b2018-09-05 15:21:00 +01004314
4315 snd_soc_dapm_add_path(&card->dapm, playback_cpu,
4316 playback, NULL, NULL);
4317 }
4318
4319 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4320 cpu_dai->component->name, playback_cpu->name,
4321 codec_dai->component->name, codec->name);
4322
4323 snd_soc_dapm_add_path(&card->dapm, playback, codec,
4324 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004325 }
Charles Keepaxe36a1d02018-09-10 15:28:39 +01004326 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004327
Charles Keepaxe36a1d02018-09-10 15:28:39 +01004328 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004329 /* connect BE DAI capture if widgets are valid */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004330 codec = codec_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004331
Charles Keepax778ff5b2018-09-05 15:21:00 +01004332 if (codec && capture_cpu) {
4333 if (!capture) {
Jerome Bruneta72706e2019-07-25 18:59:48 +02004334 substream = streams[SNDRV_PCM_STREAM_CAPTURE].substream;
4335 capture = snd_soc_dapm_new_dai(card, substream,
Jerome Brunet054d6502019-07-25 18:59:45 +02004336 "capture");
Charles Keepaxe36a1d02018-09-10 15:28:39 +01004337 if (IS_ERR(capture)) {
4338 dev_err(rtd->dev,
4339 "ASoC: Failed to create DAI %s: %ld\n",
4340 codec_dai->name,
4341 PTR_ERR(capture));
4342 continue;
4343 }
Charles Keepax778ff5b2018-09-05 15:21:00 +01004344
4345 snd_soc_dapm_add_path(&card->dapm, capture,
4346 capture_cpu, NULL, NULL);
4347 }
4348
4349 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4350 codec_dai->component->name, codec->name,
4351 cpu_dai->component->name, capture_cpu->name);
4352
4353 snd_soc_dapm_add_path(&card->dapm, codec, capture,
4354 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004355 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004356 }
4357}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004358
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004359static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4360 int event)
4361{
4362 struct snd_soc_dapm_widget *w;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004363 unsigned int ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004364
4365 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4366 w = dai->playback_widget;
4367 else
4368 w = dai->capture_widget;
4369
4370 if (w) {
4371 dapm_mark_dirty(w, "stream event");
4372
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004373 if (w->id == snd_soc_dapm_dai_in) {
4374 ep = SND_SOC_DAPM_EP_SOURCE;
4375 dapm_widget_invalidate_input_paths(w);
4376 } else {
4377 ep = SND_SOC_DAPM_EP_SINK;
4378 dapm_widget_invalidate_output_paths(w);
4379 }
4380
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004381 switch (event) {
4382 case SND_SOC_DAPM_STREAM_START:
4383 w->active = 1;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004384 w->is_ep = ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004385 break;
4386 case SND_SOC_DAPM_STREAM_STOP:
4387 w->active = 0;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004388 w->is_ep = 0;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004389 break;
4390 case SND_SOC_DAPM_STREAM_SUSPEND:
4391 case SND_SOC_DAPM_STREAM_RESUME:
4392 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4393 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4394 break;
4395 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004396 }
4397}
4398
Benoit Cousson44ba2642014-07-08 23:19:36 +02004399void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4400{
Mengdong Lin1a497982015-11-18 02:34:11 -05004401 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson44ba2642014-07-08 23:19:36 +02004402
4403 /* for each BE DAI link... */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00004404 for_each_card_rtds(card, rtd) {
Benoit Cousson44ba2642014-07-08 23:19:36 +02004405 /*
4406 * dynamic FE links have no fixed DAI mapping.
4407 * CODEC<->CODEC links have no direct connection.
4408 */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004409 if (rtd->dai_link->dynamic)
Benoit Cousson44ba2642014-07-08 23:19:36 +02004410 continue;
4411
4412 dapm_connect_dai_link_widgets(card, rtd);
4413 }
4414}
4415
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004416static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4417 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004418{
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00004419 struct snd_soc_dai *codec_dai;
Benoit Cousson44ba2642014-07-08 23:19:36 +02004420 int i;
4421
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004422 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00004423 for_each_rtd_codec_dai(rtd, i, codec_dai)
4424 soc_dapm_dai_stream_event(codec_dai, stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004425
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004426 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004427}
4428
4429/**
4430 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4431 * @rtd: PCM runtime data
4432 * @stream: stream name
4433 * @event: stream event
4434 *
4435 * Sends a stream event to the dapm core. The core then makes any
4436 * necessary widget power changes.
4437 *
4438 * Returns 0 for success else error.
4439 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004440void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4441 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004442{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004443 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004444
Liam Girdwood3cd04342012-03-09 12:02:08 +00004445 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004446 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004447 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004448}
Richard Purdie2b97eab2006-10-06 18:32:18 +02004449
4450/**
Charles Keepax11391102014-02-18 15:22:14 +00004451 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4452 * @dapm: DAPM context
4453 * @pin: pin name
4454 *
4455 * Enables input/output pin and its parents or children widgets iff there is
4456 * a valid audio route and active audio stream.
4457 *
4458 * Requires external locking.
4459 *
4460 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4461 * do any widget power switching.
4462 */
4463int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4464 const char *pin)
4465{
4466 return snd_soc_dapm_set_pin(dapm, pin, 1);
4467}
4468EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4469
4470/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004471 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004472 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004473 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02004474 *
Mark Brown74b8f952009-06-06 11:26:15 +01004475 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01004476 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00004477 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004478 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4479 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02004480 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004481int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004482{
Charles Keepax11391102014-02-18 15:22:14 +00004483 int ret;
4484
4485 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4486
4487 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4488
4489 mutex_unlock(&dapm->card->dapm_mutex);
4490
4491 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004492}
Liam Girdwooda5302182008-07-07 13:35:17 +01004493EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004494
4495/**
Charles Keepax11391102014-02-18 15:22:14 +00004496 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4497 * @dapm: DAPM context
4498 * @pin: pin name
4499 *
4500 * Enables input/output pin regardless of any other state. This is
4501 * intended for use with microphone bias supplies used in microphone
4502 * jack detection.
4503 *
4504 * Requires external locking.
4505 *
4506 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4507 * do any widget power switching.
4508 */
4509int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4510 const char *pin)
4511{
4512 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4513
4514 if (!w) {
4515 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4516 return -EINVAL;
4517 }
4518
4519 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02004520 if (!w->connected) {
4521 /*
4522 * w->force does not affect the number of input or output paths,
4523 * so we only have to recheck if w->connected is changed
4524 */
4525 dapm_widget_invalidate_input_paths(w);
4526 dapm_widget_invalidate_output_paths(w);
4527 w->connected = 1;
4528 }
Charles Keepax11391102014-02-18 15:22:14 +00004529 w->force = 1;
4530 dapm_mark_dirty(w, "force enable");
4531
4532 return 0;
4533}
4534EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4535
4536/**
Mark Brownda341832010-03-15 19:23:37 +00004537 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004538 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00004539 * @pin: pin name
4540 *
4541 * Enables input/output pin regardless of any other state. This is
4542 * intended for use with microphone bias supplies used in microphone
4543 * jack detection.
4544 *
4545 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4546 * do any widget power switching.
4547 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004548int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4549 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00004550{
Charles Keepax11391102014-02-18 15:22:14 +00004551 int ret;
Mark Brownda341832010-03-15 19:23:37 +00004552
Charles Keepax11391102014-02-18 15:22:14 +00004553 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00004554
Charles Keepax11391102014-02-18 15:22:14 +00004555 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09004556
Charles Keepax11391102014-02-18 15:22:14 +00004557 mutex_unlock(&dapm->card->dapm_mutex);
4558
4559 return ret;
Mark Brownda341832010-03-15 19:23:37 +00004560}
4561EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4562
4563/**
Charles Keepax11391102014-02-18 15:22:14 +00004564 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4565 * @dapm: DAPM context
4566 * @pin: pin name
4567 *
4568 * Disables input/output pin and its parents or children widgets.
4569 *
4570 * Requires external locking.
4571 *
4572 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4573 * do any widget power switching.
4574 */
4575int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4576 const char *pin)
4577{
4578 return snd_soc_dapm_set_pin(dapm, pin, 0);
4579}
4580EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4581
4582/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004583 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004584 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004585 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004586 *
Mark Brown74b8f952009-06-06 11:26:15 +01004587 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00004588 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004589 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4590 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004591 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004592int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4593 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01004594{
Charles Keepax11391102014-02-18 15:22:14 +00004595 int ret;
4596
4597 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4598
4599 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4600
4601 mutex_unlock(&dapm->card->dapm_mutex);
4602
4603 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01004604}
4605EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4606
4607/**
Charles Keepax11391102014-02-18 15:22:14 +00004608 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4609 * @dapm: DAPM context
4610 * @pin: pin name
4611 *
4612 * Marks the specified pin as being not connected, disabling it along
4613 * any parent or child widgets. At present this is identical to
4614 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4615 * additional things such as disabling controls which only affect
4616 * paths through the pin.
4617 *
4618 * Requires external locking.
4619 *
4620 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4621 * do any widget power switching.
4622 */
4623int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4624 const char *pin)
4625{
4626 return snd_soc_dapm_set_pin(dapm, pin, 0);
4627}
4628EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4629
4630/**
Mark Brown5817b522008-09-24 11:23:11 +01004631 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004632 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01004633 * @pin: pin name
4634 *
4635 * Marks the specified pin as being not connected, disabling it along
4636 * any parent or child widgets. At present this is identical to
4637 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4638 * additional things such as disabling controls which only affect
4639 * paths through the pin.
4640 *
4641 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4642 * do any widget power switching.
4643 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004644int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01004645{
Charles Keepax11391102014-02-18 15:22:14 +00004646 int ret;
4647
4648 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4649
4650 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4651
4652 mutex_unlock(&dapm->card->dapm_mutex);
4653
4654 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01004655}
4656EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4657
4658/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004659 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004660 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004661 * @pin: audio signal pin endpoint (or start point)
4662 *
4663 * Get audio pin status - connected or disconnected.
4664 *
4665 * Returns 1 for connected otherwise 0.
4666 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004667int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4668 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004669{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004670 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004671
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004672 if (w)
4673 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06004674
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004675 return 0;
4676}
Liam Girdwooda5302182008-07-07 13:35:17 +01004677EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004678
4679/**
Mark Brown1547aba2010-05-07 21:11:40 +01004680 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004681 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01004682 * @pin: audio signal pin endpoint (or start point)
4683 *
4684 * Mark the given endpoint or pin as ignoring suspend. When the
4685 * system is disabled a path between two endpoints flagged as ignoring
4686 * suspend will not be disabled. The path must already be enabled via
4687 * normal means at suspend time, it will not be turned on if it was not
4688 * already enabled.
4689 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004690int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4691 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01004692{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004693 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01004694
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004695 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004696 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004697 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01004698 }
4699
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004700 w->ignore_suspend = 1;
4701
4702 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01004703}
4704EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4705
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02004706/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02004707 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03004708 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02004709 *
4710 * Free all dapm widgets and resources.
4711 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004712void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004713{
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02004714 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004715 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02004716 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004717}
4718EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4719
Kuninori Morimoto95c267d2019-08-23 09:58:52 +09004720void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
4721 struct snd_soc_card *card,
4722 struct snd_soc_component *component)
4723{
4724 dapm->card = card;
4725 dapm->component = component;
4726 dapm->bias_level = SND_SOC_BIAS_OFF;
4727
4728 if (component) {
4729 dapm->dev = component->dev;
4730 dapm->idle_bias_off = !component->driver->idle_bias_on,
4731 dapm->suspend_bias_off = component->driver->suspend_bias_off;
4732 } else {
4733 dapm->dev = card->dev;
4734 }
4735
4736 INIT_LIST_HEAD(&dapm->list);
4737 list_add(&dapm->list, &card->dapm_list);
4738}
4739EXPORT_SYMBOL_GPL(snd_soc_dapm_init);
4740
Xiang Xiao57996352014-03-02 00:04:02 +08004741static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01004742{
Liam Girdwood01005a72012-07-06 16:57:05 +01004743 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01004744 struct snd_soc_dapm_widget *w;
4745 LIST_HEAD(down_list);
4746 int powerdown = 0;
4747
Liam Girdwood01005a72012-07-06 16:57:05 +01004748 mutex_lock(&card->dapm_mutex);
4749
Jarkko Nikula97c866d2010-12-14 12:18:31 +02004750 list_for_each_entry(w, &dapm->card->widgets, list) {
4751 if (w->dapm != dapm)
4752 continue;
Mark Brown51737472009-06-22 13:16:51 +01004753 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004754 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01004755 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004756 powerdown = 1;
4757 }
4758 }
4759
4760 /* If there were no widgets to power down we're already in
4761 * standby.
4762 */
4763 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004764 if (dapm->bias_level == SND_SOC_BIAS_ON)
4765 snd_soc_dapm_set_bias_level(dapm,
4766 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004767 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004768 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4769 snd_soc_dapm_set_bias_level(dapm,
4770 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004771 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004772
4773 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004774}
Mark Brown51737472009-06-22 13:16:51 +01004775
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004776/*
4777 * snd_soc_dapm_shutdown - callback for system shutdown
4778 */
4779void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4780{
Xiang Xiao57996352014-03-02 00:04:02 +08004781 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004782
Xiang Xiao57996352014-03-02 00:04:02 +08004783 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08004784 if (dapm != &card->dapm) {
4785 soc_dapm_shutdown_dapm(dapm);
4786 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4787 snd_soc_dapm_set_bias_level(dapm,
4788 SND_SOC_BIAS_OFF);
4789 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004790 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08004791
4792 soc_dapm_shutdown_dapm(&card->dapm);
4793 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4794 snd_soc_dapm_set_bias_level(&card->dapm,
4795 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01004796}
4797
Richard Purdie2b97eab2006-10-06 18:32:18 +02004798/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004799MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004800MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4801MODULE_LICENSE("GPL");