blob: 81a7a12196ffed084305126b14b31deca9f4be8e [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,
490 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
491 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200492 return -ENOMEM;
493
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200494 new_wlist->widgets[n - 1] = widget;
495 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200496
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200497 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200498
499 return 0;
500}
501
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200502static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
503 struct snd_soc_dapm_path *path)
504{
505 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
506
507 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200508}
509
510static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
511{
512 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
513
514 if (!data->widget)
515 return true;
516
517 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200518}
519
520static struct list_head *dapm_kcontrol_get_path_list(
521 const struct snd_kcontrol *kcontrol)
522{
523 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
524
525 return &data->paths;
526}
527
528#define dapm_kcontrol_for_each_path(path, kcontrol) \
529 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
530 list_kcontrol)
531
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530532unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200533{
534 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
535
536 return data->value;
537}
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530538EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200539
540static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
541 unsigned int value)
542{
543 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
544
545 if (data->value == value)
546 return false;
547
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200548 if (data->widget)
549 data->widget->on_val = value;
550
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200551 data->value = value;
552
553 return true;
554}
555
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200556/**
Mythri P K93e39a12015-10-20 22:30:08 +0530557 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
558 * kcontrol
559 * @kcontrol: The kcontrol
560 */
561struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
562 struct snd_kcontrol *kcontrol)
563{
564 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
565}
566EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
567
568/**
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200569 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
570 * kcontrol
571 * @kcontrol: The kcontrol
572 *
573 * Note: This function must only be used on kcontrols that are known to have
574 * been registered for a CODEC. Otherwise the behaviour is undefined.
575 */
576struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
577 struct snd_kcontrol *kcontrol)
578{
579 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
580}
581EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
582
Liam Girdwood6c120e12012-02-15 15:15:34 +0000583static void dapm_reset(struct snd_soc_card *card)
584{
585 struct snd_soc_dapm_widget *w;
586
Mark Brownf9fa2b12014-03-06 16:49:11 +0800587 lockdep_assert_held(&card->dapm_mutex);
588
Liam Girdwood6c120e12012-02-15 15:15:34 +0000589 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
590
591 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200592 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000593 w->power_checked = false;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000594 }
595}
596
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200597static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
598{
599 if (!dapm->component)
600 return NULL;
601 return dapm->component->name_prefix;
602}
603
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200604static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800605 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100606{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200607 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200608 return -EIO;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200609 return snd_soc_component_read(dapm->component, reg, value);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100610}
611
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200612static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
Bard Liao34775012014-04-17 20:12:56 +0800613 int reg, unsigned int mask, unsigned int value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100614{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200615 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200616 return -EIO;
Mark Brownfcf6c5e2014-12-15 13:08:48 +0000617 return snd_soc_component_update_bits(dapm->component, reg,
618 mask, value);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000619}
620
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200621static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
622 int reg, unsigned int mask, unsigned int value)
623{
624 if (!dapm->component)
625 return -EIO;
626 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
627}
628
Mark Browneb270e92013-10-09 13:52:52 +0100629static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
630{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200631 if (dapm->component)
632 snd_soc_component_async_complete(dapm->component);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100633}
634
Charles Keepax45a110a2015-05-11 13:50:30 +0100635static struct snd_soc_dapm_widget *
636dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
637{
638 struct snd_soc_dapm_widget *w = wcache->widget;
639 struct list_head *wlist;
640 const int depth = 2;
641 int i = 0;
642
643 if (w) {
644 wlist = &w->dapm->card->widgets;
645
646 list_for_each_entry_from(w, wlist, list) {
647 if (!strcmp(name, w->name))
648 return w;
649
650 if (++i == depth)
651 break;
652 }
653 }
654
655 return NULL;
656}
657
658static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
659 struct snd_soc_dapm_widget *w)
660{
661 wcache->widget = w;
662}
663
Mark Brown452c5ea2009-05-17 21:41:23 +0100664/**
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200665 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
666 * @dapm: The DAPM context for which to set the level
667 * @level: The level to set
668 *
669 * Forces the DAPM bias level to a specific state. It will call the bias level
670 * callback of DAPM context with the specified level. This will even happen if
671 * the context is already at the same level. Furthermore it will not go through
672 * the normal bias level sequencing, meaning any intermediate states between the
673 * current and the target state will not be entered.
674 *
675 * Note that the change in bias level is only temporary and the next time
676 * snd_soc_dapm_sync() is called the state will be set to the level as
677 * determined by the DAPM core. The function is mainly intended to be used to
678 * used during probe or resume from suspend to power up the device so
679 * initialization can be done, before the DAPM core takes over.
680 */
681int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
682 enum snd_soc_bias_level level)
683{
684 int ret = 0;
685
686 if (dapm->set_bias_level)
687 ret = dapm->set_bias_level(dapm, level);
688
Lars-Peter Clausenf4bf8d72015-04-27 22:13:25 +0200689 if (ret == 0)
690 dapm->bias_level = level;
691
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200692 return ret;
693}
694EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
695
Mark Brown452c5ea2009-05-17 21:41:23 +0100696/**
697 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800698 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100699 * @level: level to configure
700 *
701 * Configure the bias (power) levels for the SoC audio device.
702 *
703 * Returns 0 for success else error.
704 */
Mark Browned5a4c42011-02-18 11:12:42 -0800705static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200706 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100707{
Mark Browned5a4c42011-02-18 11:12:42 -0800708 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100709 int ret = 0;
710
Mark Brown84e90932010-11-04 00:07:02 -0400711 trace_snd_soc_bias_level_start(card, level);
712
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000713 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100714 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100715 if (ret != 0)
716 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100717
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200718 if (!card || dapm != &card->dapm)
719 ret = snd_soc_dapm_force_bias_level(dapm, level);
Liam Girdwood41231282012-07-06 16:56:16 +0100720
Mark Brown171ec6b2011-06-06 18:15:19 +0100721 if (ret != 0)
722 goto out;
723
724 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100725 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100726out:
Mark Brown84e90932010-11-04 00:07:02 -0400727 trace_snd_soc_bias_level_done(card, level);
728
Mark Brown452c5ea2009-05-17 21:41:23 +0100729 return ret;
730}
731
Mark Brown74b8f952009-06-06 11:26:15 +0100732/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200733static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200734 struct snd_soc_dapm_path *path, const char *control_name,
735 struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200736{
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200737 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200738 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100739 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200740 int i;
741
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100742 if (e->reg != SND_SOC_NOPM) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200743 soc_dapm_read(dapm, e->reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100744 val = (val >> e->shift_l) & e->mask;
745 item = snd_soc_enum_val_to_item(e, val);
746 } else {
747 /* since a virtual mux has no backing registers to
748 * decide which path to connect, it will try to match
749 * with the first enumeration. This is to ensure
750 * that the default mux choice (the first) will be
751 * correctly powered up during initialization.
752 */
753 item = 0;
754 }
755
Xie Yishengf9e0b4a2018-05-31 19:11:23 +0800756 i = match_string(e->texts, e->items, control_name);
757 if (i < 0)
758 return -ENODEV;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200759
Xie Yishengf9e0b4a2018-05-31 19:11:23 +0800760 path->name = e->texts[i];
761 path->connect = (i == item);
762 return 0;
763
Richard Purdie2b97eab2006-10-06 18:32:18 +0200764}
765
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100766/* set up initial codec paths */
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800767static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
768 int nth_path)
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100769{
770 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200771 p->sink->kcontrol_news[i].private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100772 unsigned int reg = mc->reg;
773 unsigned int shift = mc->shift;
774 unsigned int max = mc->max;
775 unsigned int mask = (1 << fls(max)) - 1;
776 unsigned int invert = mc->invert;
777 unsigned int val;
778
779 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200780 soc_dapm_read(p->sink->dapm, reg, &val);
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800781 /*
782 * The nth_path argument allows this function to know
783 * which path of a kcontrol it is setting the initial
784 * status for. Ideally this would support any number
785 * of paths and channels. But since kcontrols only come
786 * in mono and stereo variants, we are limited to 2
787 * channels.
788 *
789 * The following code assumes for stereo controls the
790 * first path is the left channel, and all remaining
791 * paths are the right channel.
792 */
793 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
794 if (reg != mc->rreg)
795 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
796 val = (val >> mc->rshift) & mask;
797 } else {
798 val = (val >> shift) & mask;
799 }
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100800 if (invert)
801 val = max - val;
802 p->connect = !!val;
803 } else {
804 p->connect = 0;
805 }
806}
807
Mark Brown74b8f952009-06-06 11:26:15 +0100808/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200809static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200810 struct snd_soc_dapm_path *path, const char *control_name)
811{
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800812 int i, nth_path = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200813
814 /* search for mixer kcontrol */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200815 for (i = 0; i < path->sink->num_kcontrols; i++) {
816 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
817 path->name = path->sink->kcontrol_news[i].name;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800818 dapm_set_mixer_path_status(path, i, nth_path++);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200819 return 0;
820 }
821 }
822 return -ENODEV;
823}
824
Stephen Warrenaf468002011-04-28 17:38:01 -0600825static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600826 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600827 const struct snd_kcontrol_new *kcontrol_new,
828 struct snd_kcontrol **kcontrol)
829{
830 struct snd_soc_dapm_widget *w;
831 int i;
832
833 *kcontrol = NULL;
834
835 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600836 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
837 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600838 for (i = 0; i < w->num_kcontrols; i++) {
839 if (&w->kcontrol_news[i] == kcontrol_new) {
840 if (w->kcontrols)
841 *kcontrol = w->kcontrols[i];
842 return 1;
843 }
844 }
845 }
846
847 return 0;
848}
849
Stephen Warren85762e72013-03-29 15:40:10 -0600850/*
851 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
852 * create it. Either way, add the widget into the control's widget list
853 */
Jeeja KP19a2557b2015-10-20 22:30:06 +0530854static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100855 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200856{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200857 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000858 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000859 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600860 size_t prefix_len;
861 int shared;
862 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600863 bool wname_in_long_name, kcname_in_long_name;
Daniel Macke5092c92014-10-07 13:41:24 +0200864 char *long_name = NULL;
Stephen Warren85762e72013-03-29 15:40:10 -0600865 const char *name;
Daniel Macke5092c92014-10-07 13:41:24 +0200866 int ret = 0;
Mark Brownefb7ac32011-03-08 17:23:24 +0000867
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200868 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000869 if (prefix)
870 prefix_len = strlen(prefix) + 1;
871 else
872 prefix_len = 0;
873
Stephen Warren85762e72013-03-29 15:40:10 -0600874 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
875 &kcontrol);
876
Stephen Warren85762e72013-03-29 15:40:10 -0600877 if (!kcontrol) {
878 if (shared) {
879 wname_in_long_name = false;
880 kcname_in_long_name = true;
881 } else {
882 switch (w->id) {
883 case snd_soc_dapm_switch:
884 case snd_soc_dapm_mixer:
Jeeja KP19a2557b2015-10-20 22:30:06 +0530885 case snd_soc_dapm_pga:
Seppo Ingalsuo882c8b4a2019-04-30 18:11:34 -0500886 case snd_soc_dapm_effect:
Chen-Yu Tsaia3930ed2016-08-27 19:28:00 +0800887 case snd_soc_dapm_out_drv:
Stephen Warren85762e72013-03-29 15:40:10 -0600888 wname_in_long_name = true;
889 kcname_in_long_name = true;
890 break;
891 case snd_soc_dapm_mixer_named_ctl:
892 wname_in_long_name = false;
893 kcname_in_long_name = true;
894 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200895 case snd_soc_dapm_demux:
Stephen Warren85762e72013-03-29 15:40:10 -0600896 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600897 wname_in_long_name = true;
898 kcname_in_long_name = false;
899 break;
900 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600901 return -EINVAL;
902 }
903 }
904
905 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600906 /*
907 * The control will get a prefix from the control
908 * creation process but we're also using the same
909 * prefix for widgets so cut the prefix off the
910 * front of the widget name.
911 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200912 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600913 w->name + prefix_len,
914 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200915 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200916 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600917
918 name = long_name;
919 } else if (wname_in_long_name) {
920 long_name = NULL;
921 name = w->name + prefix_len;
922 } else {
923 long_name = NULL;
924 name = w->kcontrol_news[kci].name;
925 }
926
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200927 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600928 prefix);
Daniel Macke5092c92014-10-07 13:41:24 +0200929 if (!kcontrol) {
930 ret = -ENOMEM;
931 goto exit_free;
932 }
933
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200934 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200935
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100936 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200937 if (ret) {
938 snd_ctl_free_one(kcontrol);
Daniel Macke5092c92014-10-07 13:41:24 +0200939 goto exit_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200940 }
941
Stephen Warren85762e72013-03-29 15:40:10 -0600942 ret = snd_ctl_add(card, kcontrol);
943 if (ret < 0) {
944 dev_err(dapm->dev,
945 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
946 w->name, name, ret);
Daniel Macke5092c92014-10-07 13:41:24 +0200947 goto exit_free;
Stephen Warren85762e72013-03-29 15:40:10 -0600948 }
Stephen Warren85762e72013-03-29 15:40:10 -0600949 }
950
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200951 ret = dapm_kcontrol_add_widget(kcontrol, w);
Daniel Macke5092c92014-10-07 13:41:24 +0200952 if (ret == 0)
953 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200954
Daniel Macke5092c92014-10-07 13:41:24 +0200955exit_free:
956 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600957
Daniel Macke5092c92014-10-07 13:41:24 +0200958 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600959}
960
961/* create new dapm mixer control */
962static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
963{
964 int i, ret;
965 struct snd_soc_dapm_path *path;
Charles Keepax561ed682015-05-01 12:37:26 +0100966 struct dapm_kcontrol_data *data;
Stephen Warren85762e72013-03-29 15:40:10 -0600967
Richard Purdie2b97eab2006-10-06 18:32:18 +0200968 /* add kcontrol */
969 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200970 /* match name */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +0200971 snd_soc_dapm_widget_for_each_source_path(w, path) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200972 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600973 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200974 continue;
975
Charles Keepax561ed682015-05-01 12:37:26 +0100976 if (!w->kcontrols[i]) {
Jeeja KP19a2557b2015-10-20 22:30:06 +0530977 ret = dapm_create_or_share_kcontrol(w, i);
Charles Keepax561ed682015-05-01 12:37:26 +0100978 if (ret < 0)
979 return ret;
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200980 }
981
Mark Brown946d92a2013-08-12 23:28:42 +0100982 dapm_kcontrol_add_path(w->kcontrols[i], path);
Charles Keepax561ed682015-05-01 12:37:26 +0100983
984 data = snd_kcontrol_chip(w->kcontrols[i]);
985 if (data->widget)
986 snd_soc_dapm_add_path(data->widget->dapm,
987 data->widget,
988 path->source,
989 NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200990 }
991 }
Stephen Warren85762e72013-03-29 15:40:10 -0600992
993 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200994}
995
996/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200997static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200998{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200999 struct snd_soc_dapm_context *dapm = w->dapm;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001000 enum snd_soc_dapm_direction dir;
Stephen Warren85762e72013-03-29 15:40:10 -06001001 struct snd_soc_dapm_path *path;
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001002 const char *type;
Stephen Warrenaf468002011-04-28 17:38:01 -06001003 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001004
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001005 switch (w->id) {
1006 case snd_soc_dapm_mux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001007 dir = SND_SOC_DAPM_DIR_OUT;
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001008 type = "mux";
1009 break;
1010 case snd_soc_dapm_demux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001011 dir = SND_SOC_DAPM_DIR_IN;
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001012 type = "demux";
1013 break;
1014 default:
1015 return -EINVAL;
1016 }
1017
Stephen Warrenaf468002011-04-28 17:38:01 -06001018 if (w->num_kcontrols != 1) {
1019 dev_err(dapm->dev,
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001020 "ASoC: %s %s has incorrect number of controls\n", type,
Stephen Warrenaf468002011-04-28 17:38:01 -06001021 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001022 return -EINVAL;
1023 }
1024
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001025 if (list_empty(&w->edges[dir])) {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02001026 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
Stephen Warren85762e72013-03-29 15:40:10 -06001027 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -06001028 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001029
Jeeja KP19a2557b2015-10-20 22:30:06 +05301030 ret = dapm_create_or_share_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -06001031 if (ret < 0)
1032 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -06001033
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001034 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1035 if (path->name)
1036 dapm_kcontrol_add_path(w->kcontrols[0], path);
Lars-Peter Clausen98407ef2014-10-25 17:41:57 +02001037 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001038
Stephen Warrenaf468002011-04-28 17:38:01 -06001039 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001040}
1041
1042/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001043static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001044{
Jeeja KP19a2557b2015-10-20 22:30:06 +05301045 int i, ret;
1046
1047 for (i = 0; i < w->num_kcontrols; i++) {
1048 ret = dapm_create_or_share_kcontrol(w, i);
1049 if (ret < 0)
1050 return ret;
1051 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001052
Mark Browna6c65732010-03-03 17:45:21 +00001053 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001054}
1055
Nikesh Oswalc6615082015-02-02 17:06:44 +00001056/* create new dapm dai link control */
1057static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1058{
1059 int i, ret;
1060 struct snd_kcontrol *kcontrol;
1061 struct snd_soc_dapm_context *dapm = w->dapm;
1062 struct snd_card *card = dapm->card->snd_card;
Charles Keepax243bcfa2018-09-05 15:21:02 +01001063 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00001064
1065 /* create control for links with > 1 config */
Charles Keepax243bcfa2018-09-05 15:21:02 +01001066 if (rtd->dai_link->num_params <= 1)
Nikesh Oswalc6615082015-02-02 17:06:44 +00001067 return 0;
1068
1069 /* add kcontrol */
1070 for (i = 0; i < w->num_kcontrols; i++) {
1071 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1072 w->name, NULL);
1073 ret = snd_ctl_add(card, kcontrol);
1074 if (ret < 0) {
1075 dev_err(dapm->dev,
1076 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1077 w->name, w->kcontrol_news[i].name, ret);
1078 return ret;
1079 }
1080 kcontrol->private_data = w;
1081 w->kcontrols[i] = kcontrol;
1082 }
1083
1084 return 0;
1085}
1086
Mark Brown9949788b2010-05-07 20:24:05 +01001087/* We implement power down on suspend by checking the power state of
1088 * the ALSA card - when we are suspending the ALSA state for the card
1089 * is set to D3.
1090 */
1091static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1092{
Mark Brown12ea2c72011-03-02 18:17:32 +00001093 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +01001094
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001095 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +01001096 case SNDRV_CTL_POWER_D3hot:
1097 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +01001098 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001099 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001100 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +01001101 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +01001102 default:
1103 return 1;
1104 }
1105}
1106
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001107static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1108 struct list_head *widgets)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001109{
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001110 struct snd_soc_dapm_widget *w;
1111 struct list_head *it;
1112 unsigned int size = 0;
1113 unsigned int i = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001114
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001115 list_for_each(it, widgets)
1116 size++;
1117
Kees Cookacafe7e2018-05-08 13:45:50 -07001118 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001119 if (*list == NULL)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001120 return -ENOMEM;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001121
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001122 list_for_each_entry(w, widgets, work_list)
1123 (*list)->widgets[i++] = w;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001124
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001125 (*list)->num_widgets = i;
1126
1127 return 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001128}
1129
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001130/*
1131 * Common implementation for is_connected_output_ep() and
1132 * is_connected_input_ep(). The function is inlined since the combined size of
1133 * the two specialized functions is only marginally larger then the size of the
1134 * generic function and at the same time the fast path of the specialized
1135 * functions is significantly smaller than the generic function.
1136 */
1137static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1138 struct list_head *list, enum snd_soc_dapm_direction dir,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001139 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1140 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1141 enum snd_soc_dapm_direction)),
1142 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1143 enum snd_soc_dapm_direction))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001144{
1145 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001146 struct snd_soc_dapm_path *path;
1147 int con = 0;
1148
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001149 if (widget->endpoints[dir] >= 0)
1150 return widget->endpoints[dir];
Mark Brown024dc072011-10-09 11:52:05 +01001151
Mark Brownde02d072011-09-20 21:43:24 +01001152 DAPM_UPDATE_STAT(widget, path_checks);
1153
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001154 /* do we need to add this widget to the list ? */
1155 if (list)
1156 list_add_tail(&widget->work_list, list);
1157
Jeeja KP09464972016-06-15 11:16:55 +05301158 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1159 widget->endpoints[dir] = 1;
1160 return widget->endpoints[dir];
1161 }
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001162
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001163 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1164 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1165 return widget->endpoints[dir];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001166 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001167
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001168 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
Mark Browne56235e02011-09-21 18:19:14 +01001169 DAPM_UPDATE_STAT(widget, neighbour_checks);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001170
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001171 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +01001172 continue;
1173
Mark Brown8af294b2013-02-22 17:48:15 +00001174 if (path->walking)
1175 return 1;
1176
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001177 trace_snd_soc_dapm_path(widget, dir, path);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001178
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001179 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +00001180 path->walking = 1;
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001181 con += fn(path->node[dir], list, custom_stop_condition);
Mark Brown8af294b2013-02-22 17:48:15 +00001182 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001183 }
1184 }
1185
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001186 widget->endpoints[dir] = con;
Mark Brown024dc072011-10-09 11:52:05 +01001187
Richard Purdie2b97eab2006-10-06 18:32:18 +02001188 return con;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001189}
1190
1191/*
1192 * Recursively check for a completed path to an active or physically connected
1193 * output widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001194 *
1195 * Optionally, can be supplied with a function acting as a stopping condition.
1196 * This function takes the dapm widget currently being examined and the walk
1197 * direction as an arguments, it should return true if the walk should be
1198 * stopped and false otherwise.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001199 */
1200static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001201 struct list_head *list,
1202 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1203 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001204{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001205 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001206 is_connected_output_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001207}
1208
1209/*
1210 * Recursively check for a completed path to an active or physically connected
1211 * input widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001212 *
1213 * Optionally, can be supplied with a function acting as a stopping condition.
1214 * This function takes the dapm widget currently being examined and the walk
1215 * direction as an arguments, it should return true if the walk should be
1216 * stopped and false otherwise.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001217 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001218static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001219 struct list_head *list,
1220 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1221 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001222{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001223 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001224 is_connected_input_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001225}
1226
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001227/**
1228 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1229 * @dai: the soc DAI.
1230 * @stream: stream direction.
1231 * @list: list of active widgets for this stream.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001232 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1233 * walk based on custom logic.
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001234 *
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001235 * Queries DAPM graph as to whether a valid audio stream path exists for
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001236 * the initial stream specified by name. This takes into account
1237 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1238 *
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001239 * Optionally, can be supplied with a function acting as a stopping condition.
1240 * This function takes the dapm widget currently being examined and the walk
1241 * direction as an arguments, it should return true if the walk should be
1242 * stopped and false otherwise.
1243 *
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001244 * Returns the number of valid paths or negative error.
1245 */
1246int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001247 struct snd_soc_dapm_widget_list **list,
1248 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1249 enum snd_soc_dapm_direction))
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001250{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01001251 struct snd_soc_card *card = dai->component->card;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001252 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001253 LIST_HEAD(widgets);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001254 int paths;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001255 int ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001256
1257 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001258
1259 /*
1260 * For is_connected_{output,input}_ep fully discover the graph we need
1261 * to reset the cached number of inputs and outputs.
1262 */
1263 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001264 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1265 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001266 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001267
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001268 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001269 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1270 custom_stop_condition);
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001271 else
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001272 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1273 custom_stop_condition);
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001274
1275 /* Drop starting point */
1276 list_del(widgets.next);
1277
1278 ret = dapm_widget_list_create(list, &widgets);
1279 if (ret)
Lars-Peter Clausen30abbe72015-08-11 21:37:59 +02001280 paths = ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001281
1282 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001283 mutex_unlock(&card->dapm_mutex);
1284
1285 return paths;
1286}
1287
Richard Purdie2b97eab2006-10-06 18:32:18 +02001288/*
Mark Brown62ea8742012-01-21 21:14:48 +00001289 * Handler for regulator supply widget.
1290 */
1291int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1292 struct snd_kcontrol *kcontrol, int event)
1293{
Mark Brownc05b84d2012-09-07 12:57:11 +08001294 int ret;
1295
Mark Browneb270e92013-10-09 13:52:52 +01001296 soc_dapm_async_complete(w->dapm);
1297
Mark Brownc05b84d2012-09-07 12:57:11 +08001298 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001299 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001300 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001301 if (ret != 0)
1302 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001303 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001304 w->name, ret);
1305 }
1306
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001307 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001308 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001309 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001310 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001311 if (ret != 0)
1312 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001313 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001314 w->name, ret);
1315 }
1316
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001317 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001318 }
Mark Brown62ea8742012-01-21 21:14:48 +00001319}
1320EXPORT_SYMBOL_GPL(dapm_regulator_event);
1321
Ola Liljad7e7eb92012-05-24 15:26:25 +02001322/*
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00001323 * Handler for pinctrl widget.
1324 */
1325int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1326 struct snd_kcontrol *kcontrol, int event)
1327{
1328 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1329 struct pinctrl *p = w->pinctrl;
1330 struct pinctrl_state *s;
1331
1332 if (!p || !priv)
1333 return -EIO;
1334
1335 if (SND_SOC_DAPM_EVENT_ON(event))
1336 s = pinctrl_lookup_state(p, priv->active_state);
1337 else
1338 s = pinctrl_lookup_state(p, priv->sleep_state);
1339
1340 if (IS_ERR(s))
1341 return PTR_ERR(s);
1342
1343 return pinctrl_select_state(p, s);
1344}
1345EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1346
1347/*
Ola Liljad7e7eb92012-05-24 15:26:25 +02001348 * Handler for clock supply widget.
1349 */
1350int dapm_clock_event(struct snd_soc_dapm_widget *w,
1351 struct snd_kcontrol *kcontrol, int event)
1352{
1353 if (!w->clk)
1354 return -EIO;
1355
Mark Browneb270e92013-10-09 13:52:52 +01001356 soc_dapm_async_complete(w->dapm);
1357
Ola Liljad7e7eb92012-05-24 15:26:25 +02001358 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001359 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001360 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001361 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001362 return 0;
1363 }
Charles Keepaxd78b1e432018-08-28 14:35:02 +01001364
Marek Belisko98b3cf12012-07-12 23:00:16 +02001365 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001366}
1367EXPORT_SYMBOL_GPL(dapm_clock_event);
1368
Mark Brownd805002b2011-09-28 18:28:23 +01001369static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1370{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001371 if (w->power_checked)
1372 return w->new_power;
1373
Mark Brownd805002b2011-09-28 18:28:23 +01001374 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001375 w->new_power = 1;
Mark Brownd805002b2011-09-28 18:28:23 +01001376 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001377 w->new_power = w->power_check(w);
1378
1379 w->power_checked = true;
1380
1381 return w->new_power;
Mark Brownd805002b2011-09-28 18:28:23 +01001382}
1383
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001384/* Generic check to see if a widget should be powered. */
Mark Browncd0f2d42009-04-20 16:56:59 +01001385static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1386{
1387 int in, out;
1388
Mark Brownde02d072011-09-20 21:43:24 +01001389 DAPM_UPDATE_STAT(w, power_checks);
1390
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001391 in = is_connected_input_ep(w, NULL, NULL);
1392 out = is_connected_output_ep(w, NULL, NULL);
Mark Browncd0f2d42009-04-20 16:56:59 +01001393 return out != 0 && in != 0;
1394}
1395
Mark Brown246d0a12009-04-22 18:24:55 +01001396/* Check to see if a power supply is needed */
1397static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1398{
1399 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001400
Mark Brownde02d072011-09-20 21:43:24 +01001401 DAPM_UPDATE_STAT(w, power_checks);
1402
Mark Brown246d0a12009-04-22 18:24:55 +01001403 /* Check if one of our outputs is connected */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001404 snd_soc_dapm_widget_for_each_sink_path(w, path) {
Mark Browna8fdac82011-09-28 18:20:26 +01001405 DAPM_UPDATE_STAT(w, neighbour_checks);
1406
Mark Brownbf3a9e12011-06-13 16:42:29 +01001407 if (path->weak)
1408 continue;
1409
Mark Brown215edda2009-09-08 18:59:05 +01001410 if (path->connected &&
1411 !path->connected(path->source, path->sink))
1412 continue;
1413
Mark Brownf68d7e12011-10-04 22:57:50 +01001414 if (dapm_widget_power_check(path->sink))
1415 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001416 }
1417
Mark Brownf68d7e12011-10-04 22:57:50 +01001418 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001419}
1420
Mark Brown35c64bc2011-09-28 18:23:53 +01001421static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1422{
Charles Keepax20bb0182015-12-02 10:22:16 +00001423 return w->connected;
Mark Brown35c64bc2011-09-28 18:23:53 +01001424}
1425
Mark Brown38357ab2009-06-06 19:03:23 +01001426static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1427 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001428 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001429{
Mark Brown828a8422011-01-15 13:14:30 +00001430 int *sort;
1431
Pierre-Louis Bossartf13d4b52019-02-05 10:22:28 -06001432 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1433 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1434
Mark Brown828a8422011-01-15 13:14:30 +00001435 if (power_up)
1436 sort = dapm_up_seq;
1437 else
1438 sort = dapm_down_seq;
1439
Pierre-Louis Bossartf13d4b52019-02-05 10:22:28 -06001440 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1441 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1442
Mark Brown38357ab2009-06-06 19:03:23 +01001443 if (sort[a->id] != sort[b->id])
1444 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001445 if (a->subseq != b->subseq) {
1446 if (power_up)
1447 return a->subseq - b->subseq;
1448 else
1449 return b->subseq - a->subseq;
1450 }
Mark Brownb22ead22009-06-07 12:51:26 +01001451 if (a->reg != b->reg)
1452 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001453 if (a->dapm != b->dapm)
1454 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001455
Mark Brown38357ab2009-06-06 19:03:23 +01001456 return 0;
1457}
Mark Brown42aa3412009-03-01 19:21:10 +00001458
Mark Brown38357ab2009-06-06 19:03:23 +01001459/* Insert a widget in order into a DAPM power sequence. */
1460static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1461 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001462 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001463{
1464 struct snd_soc_dapm_widget *w;
1465
1466 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001467 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001468 list_add_tail(&new_widget->power_list, &w->power_list);
1469 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001470 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001471
Mark Brown38357ab2009-06-06 19:03:23 +01001472 list_add_tail(&new_widget->power_list, list);
1473}
Mark Brown42aa3412009-03-01 19:21:10 +00001474
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001475static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001476 struct snd_soc_dapm_widget *w, int event)
1477{
Mark Brown68f89ad2010-11-03 23:51:49 -04001478 const char *ev_name;
1479 int power, ret;
1480
1481 switch (event) {
1482 case SND_SOC_DAPM_PRE_PMU:
1483 ev_name = "PRE_PMU";
1484 power = 1;
1485 break;
1486 case SND_SOC_DAPM_POST_PMU:
1487 ev_name = "POST_PMU";
1488 power = 1;
1489 break;
1490 case SND_SOC_DAPM_PRE_PMD:
1491 ev_name = "PRE_PMD";
1492 power = 0;
1493 break;
1494 case SND_SOC_DAPM_POST_PMD:
1495 ev_name = "POST_PMD";
1496 power = 0;
1497 break;
Mark Brown80114122013-02-25 15:14:19 +00001498 case SND_SOC_DAPM_WILL_PMU:
1499 ev_name = "WILL_PMU";
1500 power = 1;
1501 break;
1502 case SND_SOC_DAPM_WILL_PMD:
1503 ev_name = "WILL_PMD";
1504 power = 0;
1505 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001506 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001507 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001508 return;
1509 }
1510
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001511 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001512 return;
1513
1514 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001515 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001516 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001517 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001518 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001519 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001520 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001521 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001522 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001523 ev_name, w->name, ret);
1524 }
1525}
1526
Mark Brownb22ead22009-06-07 12:51:26 +01001527/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001528static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001529 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001530{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001531 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001532 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001533 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001534 unsigned int value = 0;
1535 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001536
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001537 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1538 reg = w->reg;
1539 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001540
1541 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001542 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001543 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001544
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001545 mask |= w->mask << w->shift;
1546 if (w->power)
1547 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001548 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001549 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001550
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001551 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001552 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1553 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001554
Mark Brown68f89ad2010-11-03 23:51:49 -04001555 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001556 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1557 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001558 }
1559
Mark Brown81628102009-06-07 13:21:24 +01001560 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001561 /* Any widget will do, they should all be updating the
1562 * same register.
1563 */
Mark Brown29376bc2011-06-19 13:49:28 +01001564
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001565 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001566 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001567 value, mask, reg, card->pop_time);
1568 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001569 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001570 }
1571
1572 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001573 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1574 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001575 }
Mark Brown42aa3412009-03-01 19:21:10 +00001576}
1577
Mark Brownb22ead22009-06-07 12:51:26 +01001578/* Apply a DAPM power sequence.
1579 *
1580 * We walk over a pre-sorted list of widgets to apply power to. In
1581 * order to minimise the number of writes to the device required
1582 * multiple widgets will be updated in a single write where possible.
1583 * Currently anything that requires more than a single write is not
1584 * handled.
1585 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001586static void dapm_seq_run(struct snd_soc_card *card,
1587 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001588{
1589 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001590 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001591 LIST_HEAD(pending);
1592 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001593 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001594 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001595 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001596 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001597 int *sort;
1598
1599 if (power_up)
1600 sort = dapm_up_seq;
1601 else
1602 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001603
Mark Brownb22ead22009-06-07 12:51:26 +01001604 list_for_each_entry_safe(w, n, list, power_list) {
1605 ret = 0;
1606
1607 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001608 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001609 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001610 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001611 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001612
Mark Brown474b62d2011-01-18 16:14:44 +00001613 if (cur_dapm && cur_dapm->seq_notifier) {
1614 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1615 if (sort[i] == cur_sort)
1616 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001617 i,
1618 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001619 }
1620
Mark Browneb270e92013-10-09 13:52:52 +01001621 if (cur_dapm && w->dapm != cur_dapm)
1622 soc_dapm_async_complete(cur_dapm);
1623
Mark Brownb22ead22009-06-07 12:51:26 +01001624 INIT_LIST_HEAD(&pending);
1625 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001626 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001627 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001628 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001629 }
1630
Mark Brown163cac02009-06-07 10:12:52 +01001631 switch (w->id) {
1632 case snd_soc_dapm_pre:
1633 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001634 list_for_each_entry_safe_continue(w, n, list,
1635 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001636
Mark Brownb22ead22009-06-07 12:51:26 +01001637 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001638 ret = w->event(w,
1639 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001640 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001641 ret = w->event(w,
1642 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001643 break;
1644
1645 case snd_soc_dapm_post:
1646 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001647 list_for_each_entry_safe_continue(w, n, list,
1648 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001649
Mark Brownb22ead22009-06-07 12:51:26 +01001650 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001651 ret = w->event(w,
1652 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001653 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001654 ret = w->event(w,
1655 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001656 break;
1657
Mark Brown163cac02009-06-07 10:12:52 +01001658 default:
Mark Brown81628102009-06-07 13:21:24 +01001659 /* Queue it up for application */
1660 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001661 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001662 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001663 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001664 list_move(&w->power_list, &pending);
1665 break;
Mark Brown163cac02009-06-07 10:12:52 +01001666 }
Mark Brownb22ead22009-06-07 12:51:26 +01001667
1668 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001669 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001670 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001671 }
Mark Brownb22ead22009-06-07 12:51:26 +01001672
1673 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001674 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001675
1676 if (cur_dapm && cur_dapm->seq_notifier) {
1677 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1678 if (sort[i] == cur_sort)
1679 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001680 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001681 }
Mark Browneb270e92013-10-09 13:52:52 +01001682
1683 list_for_each_entry(d, &card->dapm_list, list) {
1684 soc_dapm_async_complete(d);
1685 }
Mark Brown163cac02009-06-07 10:12:52 +01001686}
1687
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001688static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001689{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001690 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001691 struct snd_soc_dapm_widget_list *wlist;
1692 struct snd_soc_dapm_widget *w = NULL;
1693 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001694 int ret;
1695
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001696 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001697 return;
1698
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001699 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001700
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001701 for (wi = 0; wi < wlist->num_widgets; wi++) {
1702 w = wlist->widgets[wi];
1703
1704 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1705 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1706 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001707 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001708 w->name, ret);
1709 }
Mark Brown97404f22010-12-14 16:13:57 +00001710 }
1711
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001712 if (!w)
1713 return;
1714
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001715 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1716 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001717 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001718 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001719 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001720
Chen-Yu Tsaie411b0b2016-11-02 15:35:58 +08001721 if (update->has_second_set) {
1722 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1723 update->mask2, update->val2);
1724 if (ret < 0)
1725 dev_err(w->dapm->dev,
1726 "ASoC: %s DAPM update failed: %d\n",
1727 w->name, ret);
1728 }
1729
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001730 for (wi = 0; wi < wlist->num_widgets; wi++) {
1731 w = wlist->widgets[wi];
1732
1733 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1734 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1735 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001736 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001737 w->name, ret);
1738 }
Mark Brown97404f22010-12-14 16:13:57 +00001739 }
1740}
1741
Mark Brown9d0624a2011-02-18 11:49:43 -08001742/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1743 * they're changing state.
1744 */
1745static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1746{
1747 struct snd_soc_dapm_context *d = data;
1748 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001749
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001750 /* If we're off and we're not supposed to go into STANDBY */
Mark Brown56fba412011-06-04 11:25:10 +01001751 if (d->bias_level == SND_SOC_BIAS_OFF &&
1752 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001753 if (d->dev)
1754 pm_runtime_get_sync(d->dev);
1755
Mark Brown9d0624a2011-02-18 11:49:43 -08001756 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1757 if (ret != 0)
1758 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001759 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001760 }
1761
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001762 /* Prepare for a transition to ON or away from ON */
1763 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1764 d->bias_level != SND_SOC_BIAS_ON) ||
1765 (d->target_bias_level != SND_SOC_BIAS_ON &&
1766 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001767 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1768 if (ret != 0)
1769 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001770 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001771 }
1772}
1773
1774/* Async callback run prior to DAPM sequences - brings to their final
1775 * state.
1776 */
1777static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1778{
1779 struct snd_soc_dapm_context *d = data;
1780 int ret;
1781
1782 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001783 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1784 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1785 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001786 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1787 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001788 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001789 ret);
1790 }
1791
1792 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001793 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1794 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001795 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1796 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001797 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1798 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001799
1800 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001801 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001802 }
1803
1804 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001805 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1806 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001807 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1808 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001809 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001810 ret);
1811 }
1812}
Mark Brown97404f22010-12-14 16:13:57 +00001813
Mark Brownfe4fda52011-10-03 22:36:57 +01001814static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1815 bool power, bool connect)
1816{
1817 /* If a connection is being made or broken then that update
1818 * will have marked the peer dirty, otherwise the widgets are
1819 * not connected and this update has no impact. */
1820 if (!connect)
1821 return;
1822
1823 /* If the peer is already in the state we're moving to then we
1824 * won't have an impact on it. */
1825 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001826 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001827}
1828
Mark Brown05623c42011-09-28 17:02:31 +01001829static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1830 struct list_head *up_list,
1831 struct list_head *down_list)
1832{
Mark Browndb432b42011-10-03 21:06:40 +01001833 struct snd_soc_dapm_path *path;
1834
Mark Brown05623c42011-09-28 17:02:31 +01001835 if (w->power == power)
1836 return;
1837
1838 trace_snd_soc_dapm_widget_power(w, power);
1839
Mark Browndb432b42011-10-03 21:06:40 +01001840 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001841 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001842 */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001843 snd_soc_dapm_widget_for_each_source_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001844 dapm_widget_set_peer_power(path->source, power, path->connect);
1845
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001846 /* Supplies can't affect their outputs, only their inputs */
1847 if (!w->is_supply) {
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001848 snd_soc_dapm_widget_for_each_sink_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001849 dapm_widget_set_peer_power(path->sink, power,
1850 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001851 }
1852
Mark Brown05623c42011-09-28 17:02:31 +01001853 if (power)
1854 dapm_seq_insert(w, up_list, true);
1855 else
1856 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001857}
1858
Mark Brown7c81beb2011-09-20 22:22:32 +01001859static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1860 struct list_head *up_list,
1861 struct list_head *down_list)
1862{
Mark Brown7c81beb2011-09-20 22:22:32 +01001863 int power;
1864
1865 switch (w->id) {
1866 case snd_soc_dapm_pre:
1867 dapm_seq_insert(w, down_list, false);
1868 break;
1869 case snd_soc_dapm_post:
1870 dapm_seq_insert(w, up_list, true);
1871 break;
1872
1873 default:
Mark Brownd805002b2011-09-28 18:28:23 +01001874 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001875
Mark Brown05623c42011-09-28 17:02:31 +01001876 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001877 break;
1878 }
1879}
1880
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001881static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1882{
1883 if (dapm->idle_bias_off)
1884 return true;
1885
1886 switch (snd_power_get_state(dapm->card->snd_card)) {
1887 case SNDRV_CTL_POWER_D3hot:
1888 case SNDRV_CTL_POWER_D3cold:
1889 return dapm->suspend_bias_off;
1890 default:
1891 break;
1892 }
1893
1894 return false;
1895}
1896
Mark Brown42aa3412009-03-01 19:21:10 +00001897/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001898 * Scan each dapm widget for complete audio path.
1899 * A complete path is a route that has valid endpoints i.e.:-
1900 *
1901 * o DAC to output pin.
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001902 * o Input pin to ADC.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001903 * o Input pin to Output pin (bypass, sidetone)
1904 * o DAC to ADC (loopback).
1905 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001906static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001907{
1908 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001909 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001910 LIST_HEAD(up_list);
1911 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001912 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001913 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001914
Mark Brownf9fa2b12014-03-06 16:49:11 +08001915 lockdep_assert_held(&card->dapm_mutex);
1916
Mark Brown84e90932010-11-04 00:07:02 -04001917 trace_snd_soc_dapm_start(card);
1918
Mark Brown56fba412011-06-04 11:25:10 +01001919 list_for_each_entry(d, &card->dapm_list, list) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001920 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001921 d->target_bias_level = SND_SOC_BIAS_OFF;
1922 else
1923 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001924 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001925
Liam Girdwood6c120e12012-02-15 15:15:34 +00001926 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001927
Mark Brown6d3ddc82009-05-16 17:47:29 +01001928 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001929 * lists indicating if they should be powered up or down. We
1930 * only check widgets that have been flagged as dirty but note
1931 * that new widgets may be added to the dirty list while we
1932 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001933 */
Mark Browndb432b42011-10-03 21:06:40 +01001934 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001935 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001936 }
1937
Mark Brownf9de6d72011-09-28 17:19:47 +01001938 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001939 switch (w->id) {
1940 case snd_soc_dapm_pre:
1941 case snd_soc_dapm_post:
1942 /* These widgets always need to be powered */
1943 break;
1944 default:
1945 list_del_init(&w->dirty);
1946 break;
1947 }
Mark Browndb432b42011-10-03 21:06:40 +01001948
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001949 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001950 d = w->dapm;
1951
1952 /* Supplies and micbiases only bring the
1953 * context up to STANDBY as unless something
1954 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001955 * generally don't require full power. Signal
1956 * generators are virtual pins and have no
1957 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001958 */
1959 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001960 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001961 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001962 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001963 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001964 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00001965 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001966 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001967 case snd_soc_dapm_micbias:
1968 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1969 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1970 break;
1971 default:
1972 d->target_bias_level = SND_SOC_BIAS_ON;
1973 break;
1974 }
1975 }
1976
1977 }
1978
Mark Brown85a843c2011-09-21 21:29:47 +01001979 /* Force all contexts in the card to the same bias state if
1980 * they're not ground referenced.
1981 */
Mark Brown56fba412011-06-04 11:25:10 +01001982 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001983 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001984 if (d->target_bias_level > bias)
1985 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001986 list_for_each_entry(d, &card->dapm_list, list)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001987 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01001988 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001989
Mark Brownde02d072011-09-20 21:43:24 +01001990 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001991
Xiang Xiao17282ba2014-03-02 00:04:03 +08001992 /* Run card bias changes at first */
1993 dapm_pre_sequence_async(&card->dapm, 0);
1994 /* Run other bias changes in parallel */
1995 list_for_each_entry(d, &card->dapm_list, list) {
Jon Huntere03546d2018-08-17 16:35:43 +01001996 if (d != &card->dapm && d->bias_level != d->target_bias_level)
Xiang Xiao17282ba2014-03-02 00:04:03 +08001997 async_schedule_domain(dapm_pre_sequence_async, d,
1998 &async_domain);
1999 }
Mark Brown9d0624a2011-02-18 11:49:43 -08002000 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01002001
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02002002 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002003 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00002004 }
2005
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02002006 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002007 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00002008 }
2009
Mark Brown6d3ddc82009-05-16 17:47:29 +01002010 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002011 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01002012
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002013 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00002014
Mark Brown6d3ddc82009-05-16 17:47:29 +01002015 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002016 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002017
Mark Brown9d0624a2011-02-18 11:49:43 -08002018 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08002019 list_for_each_entry(d, &card->dapm_list, list) {
Jon Huntere03546d2018-08-17 16:35:43 +01002020 if (d != &card->dapm && d->bias_level != d->target_bias_level)
Xiang Xiao17282ba2014-03-02 00:04:03 +08002021 async_schedule_domain(dapm_post_sequence_async, d,
2022 &async_domain);
2023 }
Mark Brown9d0624a2011-02-18 11:49:43 -08002024 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08002025 /* Run card bias changes at last */
2026 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01002027
Liam Girdwood8078d872012-02-15 15:15:35 +00002028 /* do we need to notify any clients that DAPM event is complete */
2029 list_for_each_entry(d, &card->dapm_list, list) {
2030 if (d->stream_event)
2031 d->stream_event(d, event);
2032 }
2033
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002034 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02002035 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02002036 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01002037
Mark Brown84e90932010-11-04 00:07:02 -04002038 trace_snd_soc_dapm_done(card);
2039
Mark Brown42aa3412009-03-01 19:21:10 +00002040 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002041}
2042
Mark Brown79fb9382009-08-21 16:38:13 +01002043#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01002044static ssize_t dapm_widget_power_read_file(struct file *file,
2045 char __user *user_buf,
2046 size_t count, loff_t *ppos)
2047{
2048 struct snd_soc_dapm_widget *w = file->private_data;
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002049 struct snd_soc_card *card = w->dapm->card;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002050 enum snd_soc_dapm_direction dir, rdir;
Mark Brown79fb9382009-08-21 16:38:13 +01002051 char *buf;
2052 int in, out;
2053 ssize_t ret;
2054 struct snd_soc_dapm_path *p = NULL;
2055
2056 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2057 if (!buf)
2058 return -ENOMEM;
2059
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002060 mutex_lock(&card->dapm_mutex);
2061
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002062 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2063 if (w->is_supply) {
2064 in = 0;
2065 out = 0;
2066 } else {
Piotr Stankiewicz67420642016-05-13 17:03:55 +01002067 in = is_connected_input_ep(w, NULL, NULL);
2068 out = is_connected_output_ep(w, NULL, NULL);
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002069 }
Mark Brown79fb9382009-08-21 16:38:13 +01002070
Silvio Cesaree581e152019-01-12 16:28:43 +01002071 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
Mark Brownf13ebad2012-03-03 18:01:01 +00002072 w->name, w->power ? "On" : "Off",
2073 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01002074
Mark Brownd033c362009-12-04 15:25:56 +00002075 if (w->reg >= 0)
Silvio Cesaree581e152019-01-12 16:28:43 +01002076 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002077 " - R%d(0x%x) mask 0x%x",
2078 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00002079
Silvio Cesaree581e152019-01-12 16:28:43 +01002080 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
Mark Brownd033c362009-12-04 15:25:56 +00002081
Mark Brown3eef08b2009-09-14 16:49:00 +01002082 if (w->sname)
Silvio Cesaree581e152019-01-12 16:28:43 +01002083 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
Mark Brown3eef08b2009-09-14 16:49:00 +01002084 w->sname,
2085 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01002086
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002087 snd_soc_dapm_for_each_direction(dir) {
2088 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2089 snd_soc_dapm_widget_for_each_path(w, dir, p) {
KaiChieh Chuang28735af2018-02-05 13:00:00 +08002090 if (p->connected && !p->connected(p->source, p->sink))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002091 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002092
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002093 if (!p->connect)
2094 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002095
Silvio Cesaree581e152019-01-12 16:28:43 +01002096 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002097 " %s \"%s\" \"%s\"\n",
2098 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
Mark Brown79fb9382009-08-21 16:38:13 +01002099 p->name ? p->name : "static",
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002100 p->node[rdir]->name);
2101 }
Mark Brown79fb9382009-08-21 16:38:13 +01002102 }
2103
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002104 mutex_unlock(&card->dapm_mutex);
2105
Mark Brown79fb9382009-08-21 16:38:13 +01002106 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2107
2108 kfree(buf);
2109 return ret;
2110}
2111
2112static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002113 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002114 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002115 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002116};
2117
Mark Brownef49e4f2011-04-04 20:48:13 +09002118static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2119 size_t count, loff_t *ppos)
2120{
2121 struct snd_soc_dapm_context *dapm = file->private_data;
2122 char *level;
2123
2124 switch (dapm->bias_level) {
2125 case SND_SOC_BIAS_ON:
2126 level = "On\n";
2127 break;
2128 case SND_SOC_BIAS_PREPARE:
2129 level = "Prepare\n";
2130 break;
2131 case SND_SOC_BIAS_STANDBY:
2132 level = "Standby\n";
2133 break;
2134 case SND_SOC_BIAS_OFF:
2135 level = "Off\n";
2136 break;
2137 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002138 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002139 level = "Unknown\n";
2140 break;
2141 }
2142
2143 return simple_read_from_buffer(user_buf, count, ppos, level,
2144 strlen(level));
2145}
2146
2147static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002148 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002149 .read = dapm_bias_read_file,
2150 .llseek = default_llseek,
2151};
2152
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002153void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2154 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002155{
Mark Brown79fb9382009-08-21 16:38:13 +01002156 struct dentry *d;
2157
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02002158 if (!parent)
2159 return;
2160
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002161 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2162
2163 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002164 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002165 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002166 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002167 }
Mark Brown79fb9382009-08-21 16:38:13 +01002168
Mark Brownef49e4f2011-04-04 20:48:13 +09002169 d = debugfs_create_file("bias_level", 0444,
2170 dapm->debugfs_dapm, dapm,
2171 &dapm_bias_fops);
2172 if (!d)
2173 dev_warn(dapm->dev,
2174 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002175}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002176
2177static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2178{
2179 struct snd_soc_dapm_context *dapm = w->dapm;
2180 struct dentry *d;
2181
2182 if (!dapm->debugfs_dapm || !w->name)
2183 return;
2184
2185 d = debugfs_create_file(w->name, 0444,
2186 dapm->debugfs_dapm, w,
2187 &dapm_widget_power_fops);
2188 if (!d)
2189 dev_warn(w->dapm->dev,
2190 "ASoC: Failed to create %s debugfs file\n",
2191 w->name);
2192}
2193
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002194static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2195{
2196 debugfs_remove_recursive(dapm->debugfs_dapm);
2197}
2198
Mark Brown79fb9382009-08-21 16:38:13 +01002199#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002200void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2201 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002202{
2203}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002204
2205static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2206{
2207}
2208
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002209static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2210{
2211}
2212
Mark Brown79fb9382009-08-21 16:38:13 +01002213#endif
2214
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002215/*
2216 * soc_dapm_connect_path() - Connects or disconnects a path
2217 * @path: The path to update
2218 * @connect: The new connect state of the path. True if the path is connected,
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002219 * false if it is disconnected.
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002220 * @reason: The reason why the path changed (for debugging only)
2221 */
2222static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2223 bool connect, const char *reason)
2224{
2225 if (path->connect == connect)
2226 return;
2227
2228 path->connect = connect;
2229 dapm_mark_dirty(path->source, reason);
2230 dapm_mark_dirty(path->sink, reason);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002231 dapm_path_invalidate(path);
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002232}
2233
Richard Purdie2b97eab2006-10-06 18:32:18 +02002234/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002235static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002236 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002237{
2238 struct snd_soc_dapm_path *path;
2239 int found = 0;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002240 bool connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002241
Mark Brownf9fa2b12014-03-06 16:49:11 +08002242 lockdep_assert_held(&card->dapm_mutex);
2243
Richard Purdie2b97eab2006-10-06 18:32:18 +02002244 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002245 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002246 found = 1;
2247 /* we now need to match the string in the enum to the path */
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002248 if (!(strcmp(path->name, e->texts[mux])))
2249 connect = true;
2250 else
2251 connect = false;
2252
2253 soc_dapm_connect_path(path, connect, "mux update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002254 }
2255
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002256 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002257 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002258
Liam Girdwood618dae12012-04-25 12:12:51 +01002259 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002260}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002261
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002262int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002263 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2264 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002265{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002266 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002267 int ret;
2268
Liam Girdwood3cd04342012-03-09 12:02:08 +00002269 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002270 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002271 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002272 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002273 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002274 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002275 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002276 return ret;
2277}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002278EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002279
Milan plzik1b075e32008-01-10 14:39:46 +01002280/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002281static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002282 struct snd_kcontrol *kcontrol,
2283 int connect, int rconnect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002284{
2285 struct snd_soc_dapm_path *path;
2286 int found = 0;
2287
Mark Brownf9fa2b12014-03-06 16:49:11 +08002288 lockdep_assert_held(&card->dapm_mutex);
2289
Richard Purdie2b97eab2006-10-06 18:32:18 +02002290 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002291 dapm_kcontrol_for_each_path(path, kcontrol) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002292 /*
2293 * Ideally this function should support any number of
2294 * paths and channels. But since kcontrols only come
2295 * in mono and stereo variants, we are limited to 2
2296 * channels.
2297 *
2298 * The following code assumes for stereo controls the
2299 * first path (when 'found == 0') is the left channel,
2300 * and all remaining paths (when 'found == 1') are the
2301 * right channel.
2302 *
2303 * A stereo control is signified by a valid 'rconnect'
2304 * value, either 0 for unconnected, or >= 0 for connected.
2305 * This is chosen instead of using snd_soc_volsw_is_stereo,
2306 * so that the behavior of snd_soc_dapm_mixer_update_power
2307 * doesn't change even when the kcontrol passed in is
2308 * stereo.
2309 *
2310 * It passes 'connect' as the path connect status for
2311 * the left channel, and 'rconnect' for the right
2312 * channel.
2313 */
2314 if (found && rconnect >= 0)
2315 soc_dapm_connect_path(path, rconnect, "mixer update");
2316 else
2317 soc_dapm_connect_path(path, connect, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002318 found = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002319 }
2320
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002321 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002322 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002323
Liam Girdwood618dae12012-04-25 12:12:51 +01002324 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002325}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002326
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002327int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002328 struct snd_kcontrol *kcontrol, int connect,
2329 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002330{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002331 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002332 int ret;
2333
Liam Girdwood3cd04342012-03-09 12:02:08 +00002334 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002335 card->update = update;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002336 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002337 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002338 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002339 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002340 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002341 return ret;
2342}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002343EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002344
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002345static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2346 char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002347{
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002348 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002349 struct snd_soc_dapm_widget *w;
2350 int count = 0;
2351 char *state = "not set";
2352
Mark Brown47325072016-03-18 12:04:23 +00002353 /* card won't be set for the dummy component, as a spot fix
2354 * we're checking for that case specifically here but in future
2355 * we will ensure that the dummy component looks like others.
2356 */
2357 if (!cmpnt->card)
2358 return 0;
2359
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002360 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2361 if (w->dapm != dapm)
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002362 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002363
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002364 /* only display widgets that burn power */
Richard Purdie2b97eab2006-10-06 18:32:18 +02002365 switch (w->id) {
2366 case snd_soc_dapm_hp:
2367 case snd_soc_dapm_mic:
2368 case snd_soc_dapm_spk:
2369 case snd_soc_dapm_line:
2370 case snd_soc_dapm_micbias:
2371 case snd_soc_dapm_dac:
2372 case snd_soc_dapm_adc:
2373 case snd_soc_dapm_pga:
Seppo Ingalsuo882c8b4a2019-04-30 18:11:34 -05002374 case snd_soc_dapm_effect:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002375 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002376 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002377 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002378 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002379 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00002380 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002381 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002382 if (w->name)
2383 count += sprintf(buf + count, "%s: %s\n",
2384 w->name, w->power ? "On":"Off");
2385 break;
2386 default:
2387 break;
2388 }
2389 }
2390
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002391 switch (snd_soc_dapm_get_bias_level(dapm)) {
Mark Brown0be98982008-05-19 12:31:28 +02002392 case SND_SOC_BIAS_ON:
2393 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002394 break;
Mark Brown0be98982008-05-19 12:31:28 +02002395 case SND_SOC_BIAS_PREPARE:
2396 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002397 break;
Mark Brown0be98982008-05-19 12:31:28 +02002398 case SND_SOC_BIAS_STANDBY:
2399 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002400 break;
Mark Brown0be98982008-05-19 12:31:28 +02002401 case SND_SOC_BIAS_OFF:
2402 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002403 break;
2404 }
2405 count += sprintf(buf + count, "PM State: %s\n", state);
2406
2407 return count;
2408}
2409
Benoit Cousson44ba2642014-07-08 23:19:36 +02002410/* show dapm widget status in sys fs */
2411static ssize_t dapm_widget_show(struct device *dev,
2412 struct device_attribute *attr, char *buf)
2413{
2414 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002415 struct snd_soc_dai *codec_dai;
Benoit Cousson44ba2642014-07-08 23:19:36 +02002416 int i, count = 0;
2417
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002418 mutex_lock(&rtd->card->dapm_mutex);
2419
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002420 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2421 struct snd_soc_component *cmpnt = codec_dai->component;
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002422
2423 count += dapm_widget_show_component(cmpnt, buf + count);
Benoit Cousson44ba2642014-07-08 23:19:36 +02002424 }
2425
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002426 mutex_unlock(&rtd->card->dapm_mutex);
2427
Benoit Cousson44ba2642014-07-08 23:19:36 +02002428 return count;
2429}
2430
Joe Perchesc828a892017-12-19 10:15:08 -08002431static DEVICE_ATTR_RO(dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002432
Takashi Iwaid29697d2015-01-30 20:16:37 +01002433struct attribute *soc_dapm_dev_attrs[] = {
2434 &dev_attr_dapm_widget.attr,
2435 NULL
2436};
Richard Purdie2b97eab2006-10-06 18:32:18 +02002437
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002438static void dapm_free_path(struct snd_soc_dapm_path *path)
2439{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002440 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2441 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002442 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002443 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002444 kfree(path);
2445}
2446
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002447void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2448{
2449 struct snd_soc_dapm_path *p, *next_p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002450 enum snd_soc_dapm_direction dir;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002451
2452 list_del(&w->list);
2453 /*
2454 * remove source and sink paths associated to this widget.
2455 * While removing the path, remove reference to it from both
2456 * source and sink widgets so that path is removed only once.
2457 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002458 snd_soc_dapm_for_each_direction(dir) {
2459 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2460 dapm_free_path(p);
2461 }
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002462
2463 kfree(w->kcontrols);
Lars-Peter Clausen48068962015-07-21 18:11:08 +02002464 kfree_const(w->name);
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -06002465 kfree_const(w->sname);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002466 kfree(w);
2467}
2468
Jyri Sarhafd589a12015-11-10 18:12:42 +02002469void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2470{
2471 dapm->path_sink_cache.widget = NULL;
2472 dapm->path_source_cache.widget = NULL;
2473}
2474
Richard Purdie2b97eab2006-10-06 18:32:18 +02002475/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002476static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002477{
2478 struct snd_soc_dapm_widget *w, *next_w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002479
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002480 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2481 if (w->dapm != dapm)
2482 continue;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002483 snd_soc_dapm_free_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002484 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002485 snd_soc_dapm_reset_cache(dapm);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002486}
2487
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002488static struct snd_soc_dapm_widget *dapm_find_widget(
2489 struct snd_soc_dapm_context *dapm, const char *pin,
2490 bool search_other_contexts)
2491{
2492 struct snd_soc_dapm_widget *w;
2493 struct snd_soc_dapm_widget *fallback = NULL;
2494
2495 list_for_each_entry(w, &dapm->card->widgets, list) {
2496 if (!strcmp(w->name, pin)) {
2497 if (w->dapm == dapm)
2498 return w;
2499 else
2500 fallback = w;
2501 }
2502 }
2503
2504 if (search_other_contexts)
2505 return fallback;
2506
2507 return NULL;
2508}
2509
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002510static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002511 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002512{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002513 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002514
Mark Brownf9fa2b12014-03-06 16:49:11 +08002515 dapm_assert_locked(dapm);
2516
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002517 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002518 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002519 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002520 }
2521
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002522 if (w->connected != status) {
Mark Brown1a8b2d92012-02-16 11:50:07 -08002523 dapm_mark_dirty(w, "pin configuration");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002524 dapm_widget_invalidate_input_paths(w);
2525 dapm_widget_invalidate_output_paths(w);
2526 }
Mark Brown1a8b2d92012-02-16 11:50:07 -08002527
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002528 w->connected = status;
2529 if (status == 0)
2530 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002531
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002532 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002533}
2534
Richard Purdie2b97eab2006-10-06 18:32:18 +02002535/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002536 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2537 * @dapm: DAPM context
2538 *
2539 * Walks all dapm audio paths and powers widgets according to their
2540 * stream or path usage.
2541 *
2542 * Requires external locking.
2543 *
2544 * Returns 0 for success.
2545 */
2546int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2547{
2548 /*
2549 * Suppress early reports (eg, jacks syncing their state) to avoid
2550 * silly DAPM runs during card startup.
2551 */
2552 if (!dapm->card || !dapm->card->instantiated)
2553 return 0;
2554
2555 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2556}
2557EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2558
2559/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002560 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002561 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002562 *
2563 * Walks all dapm audio paths and powers widgets according to their
2564 * stream or path usage.
2565 *
2566 * Returns 0 for success.
2567 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002568int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002569{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002570 int ret;
2571
Liam Girdwood3cd04342012-03-09 12:02:08 +00002572 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002573 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002574 mutex_unlock(&dapm->card->dapm_mutex);
2575 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002576}
Liam Girdwooda5302182008-07-07 13:35:17 +01002577EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002578
Charles Keepax078a85f2019-01-31 13:30:18 +00002579static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2580 struct snd_soc_dapm_widget *w,
2581 int channels)
2582{
2583 switch (w->id) {
2584 case snd_soc_dapm_aif_out:
2585 case snd_soc_dapm_aif_in:
2586 break;
2587 default:
2588 return 0;
2589 }
2590
2591 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2592 w->channel < channels ? "Connecting" : "Disconnecting",
2593 p->source->name, p->sink->name);
2594
2595 if (w->channel < channels)
2596 soc_dapm_connect_path(p, true, "dai update");
2597 else
2598 soc_dapm_connect_path(p, false, "dai update");
2599
2600 return 0;
2601}
2602
2603static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2604 struct snd_pcm_hw_params *params,
2605 struct snd_soc_dai *dai)
2606{
2607 int dir = substream->stream;
2608 int channels = params_channels(params);
2609 struct snd_soc_dapm_path *p;
2610 struct snd_soc_dapm_widget *w;
2611 int ret;
2612
2613 if (dir == SNDRV_PCM_STREAM_PLAYBACK)
2614 w = dai->playback_widget;
2615 else
2616 w = dai->capture_widget;
2617
Charles Keepaxcf17a5f2019-02-06 11:13:59 +00002618 if (!w)
2619 return 0;
2620
Charles Keepax078a85f2019-01-31 13:30:18 +00002621 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2622 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2623
2624 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2625 ret = dapm_update_dai_chan(p, p->sink, channels);
2626 if (ret < 0)
2627 return ret;
2628 }
2629
2630 snd_soc_dapm_widget_for_each_source_path(w, p) {
2631 ret = dapm_update_dai_chan(p, p->source, channels);
2632 if (ret < 0)
2633 return ret;
2634 }
2635
2636 return 0;
2637}
2638
2639int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2640 struct snd_pcm_hw_params *params,
2641 struct snd_soc_dai *dai)
2642{
2643 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2644 int ret;
2645
2646 mutex_lock_nested(&rtd->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2647 ret = dapm_update_dai_unlocked(substream, params, dai);
2648 mutex_unlock(&rtd->card->dapm_mutex);
2649
2650 return ret;
2651}
2652EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2653
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002654/*
2655 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2656 * @w: The widget for which to update the flags
2657 *
2658 * Some widgets have a dynamic category which depends on which neighbors they
2659 * are connected to. This function update the category for these widgets.
2660 *
2661 * This function must be called whenever a path is added or removed to a widget.
2662 */
2663static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2664{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002665 enum snd_soc_dapm_direction dir;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002666 struct snd_soc_dapm_path *p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002667 unsigned int ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002668
2669 switch (w->id) {
2670 case snd_soc_dapm_input:
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002671 /* On a fully routed card an input is never a source */
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002672 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002673 return;
2674 ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002675 snd_soc_dapm_widget_for_each_source_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002676 if (p->source->id == snd_soc_dapm_micbias ||
2677 p->source->id == snd_soc_dapm_mic ||
2678 p->source->id == snd_soc_dapm_line ||
2679 p->source->id == snd_soc_dapm_output) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002680 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002681 break;
2682 }
2683 }
2684 break;
2685 case snd_soc_dapm_output:
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002686 /* On a fully routed card a output is never a sink */
2687 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002688 return;
2689 ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002690 snd_soc_dapm_widget_for_each_sink_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002691 if (p->sink->id == snd_soc_dapm_spk ||
2692 p->sink->id == snd_soc_dapm_hp ||
2693 p->sink->id == snd_soc_dapm_line ||
2694 p->sink->id == snd_soc_dapm_input) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002695 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002696 break;
2697 }
2698 }
2699 break;
2700 case snd_soc_dapm_line:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002701 ep = 0;
2702 snd_soc_dapm_for_each_direction(dir) {
2703 if (!list_empty(&w->edges[dir]))
2704 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2705 }
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002706 break;
2707 default:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002708 return;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002709 }
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002710
2711 w->is_ep = ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002712}
2713
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002714static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2715 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2716 const char *control)
2717{
2718 bool dynamic_source = false;
2719 bool dynamic_sink = false;
2720
2721 if (!control)
2722 return 0;
2723
2724 switch (source->id) {
2725 case snd_soc_dapm_demux:
2726 dynamic_source = true;
2727 break;
2728 default:
2729 break;
2730 }
2731
2732 switch (sink->id) {
2733 case snd_soc_dapm_mux:
2734 case snd_soc_dapm_switch:
2735 case snd_soc_dapm_mixer:
2736 case snd_soc_dapm_mixer_named_ctl:
2737 dynamic_sink = true;
2738 break;
2739 default:
2740 break;
2741 }
2742
2743 if (dynamic_source && dynamic_sink) {
2744 dev_err(dapm->dev,
2745 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2746 source->name, control, sink->name);
2747 return -EINVAL;
2748 } else if (!dynamic_source && !dynamic_sink) {
2749 dev_err(dapm->dev,
2750 "Control not supported for path %s -> [%s] -> %s\n",
2751 source->name, control, sink->name);
2752 return -EINVAL;
2753 }
2754
2755 return 0;
2756}
2757
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002758static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2759 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2760 const char *control,
2761 int (*connected)(struct snd_soc_dapm_widget *source,
2762 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002763{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002764 struct snd_soc_dapm_widget *widgets[2];
2765 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002766 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002767 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002768
Lars-Peter Clausene409dfb2014-10-25 17:42:02 +02002769 if (wsink->is_supply && !wsource->is_supply) {
2770 dev_err(dapm->dev,
2771 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2772 wsource->name, wsink->name);
2773 return -EINVAL;
2774 }
2775
2776 if (connected && !wsource->is_supply) {
2777 dev_err(dapm->dev,
2778 "connected() callback only supported for supply widgets (%s -> %s)\n",
2779 wsource->name, wsink->name);
2780 return -EINVAL;
2781 }
2782
2783 if (wsource->is_supply && control) {
2784 dev_err(dapm->dev,
2785 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2786 wsource->name, control, wsink->name);
2787 return -EINVAL;
2788 }
2789
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002790 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2791 if (ret)
2792 return ret;
2793
Richard Purdie2b97eab2006-10-06 18:32:18 +02002794 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2795 if (!path)
2796 return -ENOMEM;
2797
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002798 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2799 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2800 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2801 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2802
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002803 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002804 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002805 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002806
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002807 if (wsource->is_supply || wsink->is_supply)
2808 path->is_supply = 1;
2809
Richard Purdie2b97eab2006-10-06 18:32:18 +02002810 /* connect static paths */
2811 if (control == NULL) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002812 path->connect = 1;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002813 } else {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002814 switch (wsource->id) {
2815 case snd_soc_dapm_demux:
2816 ret = dapm_connect_mux(dapm, path, control, wsource);
2817 if (ret)
2818 goto err;
2819 break;
2820 default:
2821 break;
2822 }
2823
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002824 switch (wsink->id) {
2825 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002826 ret = dapm_connect_mux(dapm, path, control, wsink);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002827 if (ret != 0)
2828 goto err;
2829 break;
2830 case snd_soc_dapm_switch:
2831 case snd_soc_dapm_mixer:
2832 case snd_soc_dapm_mixer_named_ctl:
2833 ret = dapm_connect_mixer(dapm, path, control);
2834 if (ret != 0)
2835 goto err;
2836 break;
2837 default:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002838 break;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002839 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002840 }
2841
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002842 list_add(&path->list, &dapm->card->paths);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002843 snd_soc_dapm_for_each_direction(dir)
2844 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002845
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002846 snd_soc_dapm_for_each_direction(dir) {
2847 dapm_update_widget_flags(widgets[dir]);
2848 dapm_mark_dirty(widgets[dir], "Route added");
2849 }
Mark Brownfabd0382012-07-05 17:20:06 +01002850
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002851 if (dapm->card->instantiated && path->connect)
2852 dapm_path_invalidate(path);
2853
Richard Purdie2b97eab2006-10-06 18:32:18 +02002854 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002855err:
2856 kfree(path);
2857 return ret;
2858}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002859
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002860static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002861 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002862{
2863 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2864 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2865 const char *sink;
2866 const char *source;
2867 char prefixed_sink[80];
2868 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002869 const char *prefix;
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002870 unsigned int sink_ref = 0;
2871 unsigned int source_ref = 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002872 int ret;
2873
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002874 prefix = soc_dapm_prefix(dapm);
2875 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002876 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002877 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002878 sink = prefixed_sink;
2879 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002880 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002881 source = prefixed_source;
2882 } else {
2883 sink = route->sink;
2884 source = route->source;
2885 }
2886
Charles Keepax45a110a2015-05-11 13:50:30 +01002887 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2888 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2889
2890 if (wsink && wsource)
2891 goto skip_search;
2892
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002893 /*
2894 * find src and dest widgets over all widgets but favor a widget from
2895 * current DAPM context
2896 */
2897 list_for_each_entry(w, &dapm->card->widgets, list) {
2898 if (!wsink && !(strcmp(w->name, sink))) {
2899 wtsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002900 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002901 wsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002902 if (wsource)
2903 break;
2904 }
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002905 sink_ref++;
2906 if (sink_ref > 1)
2907 dev_warn(dapm->dev,
2908 "ASoC: sink widget %s overwritten\n",
2909 w->name);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002910 continue;
2911 }
2912 if (!wsource && !(strcmp(w->name, source))) {
2913 wtsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002914 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002915 wsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002916 if (wsink)
2917 break;
2918 }
Zhiwei Jiang411db2a2019-01-31 19:30:05 +08002919 source_ref++;
2920 if (source_ref > 1)
2921 dev_warn(dapm->dev,
2922 "ASoC: source widget %s overwritten\n",
2923 w->name);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002924 }
2925 }
2926 /* use widget from another DAPM context if not found from this */
2927 if (!wsink)
2928 wsink = wtsink;
2929 if (!wsource)
2930 wsource = wtsource;
2931
2932 if (wsource == NULL) {
2933 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2934 route->source);
2935 return -ENODEV;
2936 }
2937 if (wsink == NULL) {
2938 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2939 route->sink);
2940 return -ENODEV;
2941 }
2942
Charles Keepax45a110a2015-05-11 13:50:30 +01002943skip_search:
2944 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2945 dapm_wcache_update(&dapm->path_source_cache, wsource);
2946
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002947 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2948 route->connected);
2949 if (ret)
2950 goto err;
2951
2952 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002953err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002954 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002955 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002956 return ret;
2957}
Mark Brown105f1c22008-05-13 14:52:19 +02002958
Mark Brownefcc3c62012-07-05 17:24:19 +01002959static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2960 const struct snd_soc_dapm_route *route)
2961{
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002962 struct snd_soc_dapm_widget *wsource, *wsink;
Mark Brownefcc3c62012-07-05 17:24:19 +01002963 struct snd_soc_dapm_path *path, *p;
2964 const char *sink;
2965 const char *source;
2966 char prefixed_sink[80];
2967 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002968 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002969
2970 if (route->control) {
2971 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002972 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002973 return -EINVAL;
2974 }
2975
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002976 prefix = soc_dapm_prefix(dapm);
2977 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002978 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002979 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002980 sink = prefixed_sink;
2981 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002982 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002983 source = prefixed_source;
2984 } else {
2985 sink = route->sink;
2986 source = route->source;
2987 }
2988
2989 path = NULL;
2990 list_for_each_entry(p, &dapm->card->paths, list) {
2991 if (strcmp(p->source->name, source) != 0)
2992 continue;
2993 if (strcmp(p->sink->name, sink) != 0)
2994 continue;
2995 path = p;
2996 break;
2997 }
2998
2999 if (path) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003000 wsource = path->source;
3001 wsink = path->sink;
3002
3003 dapm_mark_dirty(wsource, "Route removed");
3004 dapm_mark_dirty(wsink, "Route removed");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003005 if (path->connect)
3006 dapm_path_invalidate(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01003007
Lars-Peter Clausen88722932013-06-14 13:16:53 +02003008 dapm_free_path(path);
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003009
3010 /* Update any path related flags */
3011 dapm_update_widget_flags(wsource);
3012 dapm_update_widget_flags(wsink);
Mark Brownefcc3c62012-07-05 17:24:19 +01003013 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003014 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01003015 source, sink);
3016 }
3017
3018 return 0;
3019}
3020
Mark Brown105f1c22008-05-13 14:52:19 +02003021/**
Mark Brown105f1c22008-05-13 14:52:19 +02003022 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003023 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02003024 * @route: audio routes
3025 * @num: number of routes
3026 *
3027 * Connects 2 dapm widgets together via a named audio path. The sink is
3028 * the widget receiving the audio signal, whilst the source is the sender
3029 * of the audio signal.
3030 *
3031 * Returns 0 for success else error. On error all resources can be freed
3032 * with a call to snd_soc_card_free().
3033 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003034int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02003035 const struct snd_soc_dapm_route *route, int num)
3036{
Mark Brown62d4a4b2012-06-22 12:21:49 +01003037 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02003038
Stuart Hendersonf19c1812017-09-21 11:02:12 +01003039 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown105f1c22008-05-13 14:52:19 +02003040 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02003041 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01003042 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003043 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
3044 route->source,
3045 route->control ? route->control : "direct",
3046 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01003047 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02003048 }
3049 route++;
3050 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003051 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02003052
Dan Carpenter60884c22012-04-13 22:25:43 +03003053 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02003054}
3055EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3056
Mark Brownefcc3c62012-07-05 17:24:19 +01003057/**
3058 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3059 * @dapm: DAPM context
3060 * @route: audio routes
3061 * @num: number of routes
3062 *
3063 * Removes routes from the DAPM context.
3064 */
3065int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3066 const struct snd_soc_dapm_route *route, int num)
3067{
Rajan Vajae066ea22016-02-11 11:23:35 +05303068 int i;
Mark Brownefcc3c62012-07-05 17:24:19 +01003069
Stuart Hendersonf19c1812017-09-21 11:02:12 +01003070 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownefcc3c62012-07-05 17:24:19 +01003071 for (i = 0; i < num; i++) {
3072 snd_soc_dapm_del_route(dapm, route);
3073 route++;
3074 }
3075 mutex_unlock(&dapm->card->dapm_mutex);
3076
Rajan Vajae066ea22016-02-11 11:23:35 +05303077 return 0;
Mark Brownefcc3c62012-07-05 17:24:19 +01003078}
3079EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3080
Mark Brownbf3a9e12011-06-13 16:42:29 +01003081static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3082 const struct snd_soc_dapm_route *route)
3083{
3084 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3085 route->source,
3086 true);
3087 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3088 route->sink,
3089 true);
3090 struct snd_soc_dapm_path *path;
3091 int count = 0;
3092
3093 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003094 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003095 route->source);
3096 return -ENODEV;
3097 }
3098
3099 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003100 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003101 route->sink);
3102 return -ENODEV;
3103 }
3104
3105 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003106 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003107 route->source, route->sink);
3108
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02003109 snd_soc_dapm_widget_for_each_sink_path(source, path) {
Mark Brownbf3a9e12011-06-13 16:42:29 +01003110 if (path->sink == sink) {
3111 path->weak = 1;
3112 count++;
3113 }
3114 }
3115
3116 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003117 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003118 route->source, route->sink);
3119 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003120 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01003121 count, route->source, route->sink);
3122
3123 return 0;
3124}
3125
3126/**
3127 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3128 * @dapm: DAPM context
3129 * @route: audio routes
3130 * @num: number of routes
3131 *
3132 * Mark existing routes matching those specified in the passed array
3133 * as being weak, meaning that they are ignored for the purpose of
3134 * power decisions. The main intended use case is for sidetone paths
3135 * which couple audio between other independent paths if they are both
3136 * active in order to make the combination work better at the user
3137 * level but which aren't intended to be "used".
3138 *
3139 * Note that CODEC drivers should not use this as sidetone type paths
3140 * can frequently also be used as bypass paths.
3141 */
3142int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3143 const struct snd_soc_dapm_route *route, int num)
3144{
3145 int i, err;
3146 int ret = 0;
3147
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003148 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01003149 for (i = 0; i < num; i++) {
3150 err = snd_soc_dapm_weak_route(dapm, route);
3151 if (err)
3152 ret = err;
3153 route++;
3154 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003155 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01003156
3157 return ret;
3158}
3159EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3160
Mark Brown105f1c22008-05-13 14:52:19 +02003161/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003162 * snd_soc_dapm_new_widgets - add new dapm widgets
Jonathan Corbet628536e2015-08-25 01:14:48 -06003163 * @card: card to be checked for new dapm widgets
Richard Purdie2b97eab2006-10-06 18:32:18 +02003164 *
3165 * Checks the codec for any new dapm widgets and creates them if found.
3166 *
3167 * Returns 0 for success.
3168 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02003169int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003170{
3171 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00003172 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003173
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003174 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003175
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003176 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003177 {
3178 if (w->new)
3179 continue;
3180
Stephen Warrenfad59882011-04-28 17:37:59 -06003181 if (w->num_kcontrols) {
Kees Cook6396bb22018-06-12 14:03:40 -07003182 w->kcontrols = kcalloc(w->num_kcontrols,
Stephen Warrenfad59882011-04-28 17:37:59 -06003183 sizeof(struct snd_kcontrol *),
3184 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003185 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003186 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06003187 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003188 }
Stephen Warrenfad59882011-04-28 17:37:59 -06003189 }
3190
Richard Purdie2b97eab2006-10-06 18:32:18 +02003191 switch(w->id) {
3192 case snd_soc_dapm_switch:
3193 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00003194 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003195 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003196 break;
3197 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003198 case snd_soc_dapm_demux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003199 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003200 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003201 case snd_soc_dapm_pga:
Seppo Ingalsuo882c8b4a2019-04-30 18:11:34 -05003202 case snd_soc_dapm_effect:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06003203 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003204 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003205 break;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003206 case snd_soc_dapm_dai_link:
3207 dapm_new_dai_link(w);
3208 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01003209 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003210 break;
3211 }
Mark Brownb66a70d2011-02-09 18:04:11 +00003212
3213 /* Read the initial power state from the device */
3214 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003215 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08003216 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003217 val &= w->mask;
3218 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00003219 w->power = 1;
3220 }
3221
Richard Purdie2b97eab2006-10-06 18:32:18 +02003222 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003223
Mark Brown7508b122011-10-05 12:09:12 +01003224 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003225 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003226 }
3227
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003228 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3229 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003230 return 0;
3231}
3232EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3233
3234/**
3235 * snd_soc_dapm_get_volsw - dapm mixer get callback
3236 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003237 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003238 *
3239 * Callback to get the value of a dapm mixer control.
3240 *
3241 * Returns 0 for success.
3242 */
3243int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3244 struct snd_ctl_elem_value *ucontrol)
3245{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003246 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3247 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003248 struct soc_mixer_control *mc =
3249 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003250 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003251 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003252 int max = mc->max;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003253 unsigned int width = fls(max);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003254 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003255 unsigned int invert = mc->invert;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003256 unsigned int reg_val, val, rval = 0;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003257 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003258
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003259 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003260 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003261 ret = soc_dapm_read(dapm, reg, &reg_val);
3262 val = (reg_val >> shift) & mask;
3263
3264 if (ret == 0 && reg != mc->rreg)
3265 ret = soc_dapm_read(dapm, mc->rreg, &reg_val);
3266
3267 if (snd_soc_volsw_is_stereo(mc))
3268 rval = (reg_val >> mc->rshift) & mask;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003269 } else {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003270 reg_val = dapm_kcontrol_get_value(kcontrol);
3271 val = reg_val & mask;
3272
3273 if (snd_soc_volsw_is_stereo(mc))
3274 rval = (reg_val >> width) & mask;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003275 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003276 mutex_unlock(&card->dapm_mutex);
3277
Chen-Yu Tsai01ad5e72016-08-27 19:27:58 +08003278 if (ret)
3279 return ret;
3280
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003281 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003282 ucontrol->value.integer.value[0] = max - val;
3283 else
3284 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003285
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003286 if (snd_soc_volsw_is_stereo(mc)) {
3287 if (invert)
3288 ucontrol->value.integer.value[1] = max - rval;
3289 else
3290 ucontrol->value.integer.value[1] = rval;
3291 }
3292
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003293 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003294}
3295EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3296
3297/**
3298 * snd_soc_dapm_put_volsw - dapm mixer set callback
3299 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003300 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003301 *
3302 * Callback to set the value of a dapm mixer control.
3303 *
3304 * Returns 0 for success.
3305 */
3306int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3307 struct snd_ctl_elem_value *ucontrol)
3308{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003309 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3310 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003311 struct soc_mixer_control *mc =
3312 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003313 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003314 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003315 int max = mc->max;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003316 unsigned int width = fls(max);
3317 unsigned int mask = (1 << width) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003318 unsigned int invert = mc->invert;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003319 unsigned int val, rval = 0;
3320 int connect, rconnect = -1, change, reg_change = 0;
Fabio Estevam33d92452018-02-14 13:39:05 -02003321 struct snd_soc_dapm_update update = {};
Nenghua Cao52765972013-12-13 20:13:49 +08003322 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003323
3324 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02003325 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003326
3327 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003328 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003329
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003330 if (snd_soc_volsw_is_stereo(mc)) {
3331 rval = (ucontrol->value.integer.value[1] & mask);
3332 rconnect = !!rval;
3333 if (invert)
3334 rval = max - rval;
3335 }
3336
Liam Girdwood3cd04342012-03-09 12:02:08 +00003337 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003338
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003339 /* This assumes field width < (bits in unsigned int / 2) */
3340 if (width > sizeof(unsigned int) * 8 / 2)
3341 dev_warn(dapm->dev,
3342 "ASoC: control %s field width limit exceeded\n",
3343 kcontrol->id.name);
3344 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
Mark Brown283375c2009-12-07 18:09:03 +00003345
Jarkko Nikula18626c72014-06-09 14:20:29 +03003346 if (reg != SND_SOC_NOPM) {
Jarkko Nikula18626c72014-06-09 14:20:29 +03003347 val = val << shift;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003348 rval = rval << mc->rshift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02003349
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003350 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3351
3352 if (snd_soc_volsw_is_stereo(mc))
3353 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3354 mask << mc->rshift,
3355 rval);
Jarkko Nikula18626c72014-06-09 14:20:29 +03003356 }
3357
3358 if (change || reg_change) {
3359 if (reg_change) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003360 if (snd_soc_volsw_is_stereo(mc)) {
3361 update.has_second_set = true;
3362 update.reg2 = mc->rreg;
3363 update.mask2 = mask << mc->rshift;
3364 update.val2 = rval;
3365 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003366 update.kcontrol = kcontrol;
3367 update.reg = reg;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003368 update.mask = mask << shift;
Jarkko Nikula18626c72014-06-09 14:20:29 +03003369 update.val = val;
3370 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003371 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003372 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00003373
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003374 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3375 rconnect);
Mark Brown97404f22010-12-14 16:13:57 +00003376
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003377 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00003378 }
3379
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003380 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003381
3382 if (ret > 0)
3383 soc_dpcm_runtime_update(card);
3384
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003385 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003386}
3387EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3388
3389/**
3390 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3391 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003392 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003393 *
3394 * Callback to get the value of a dapm enumerated double mixer control.
3395 *
3396 * Returns 0 for success.
3397 */
3398int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3399 struct snd_ctl_elem_value *ucontrol)
3400{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003401 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Charles Keepax561ed682015-05-01 12:37:26 +01003402 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003403 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003404 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003405
Charles Keepax561ed682015-05-01 12:37:26 +01003406 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3407 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003408 int ret = soc_dapm_read(dapm, e->reg, &reg_val);
Charles Keepax964a0b82015-05-08 10:50:10 +01003409 if (ret) {
3410 mutex_unlock(&card->dapm_mutex);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003411 return ret;
Charles Keepax964a0b82015-05-08 10:50:10 +01003412 }
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003413 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003414 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003415 }
Charles Keepax561ed682015-05-01 12:37:26 +01003416 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003417
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003418 val = (reg_val >> e->shift_l) & e->mask;
3419 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3420 if (e->shift_l != e->shift_r) {
3421 val = (reg_val >> e->shift_r) & e->mask;
3422 val = snd_soc_enum_val_to_item(e, val);
3423 ucontrol->value.enumerated.item[1] = val;
3424 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003425
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003426 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003427}
3428EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3429
3430/**
3431 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3432 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003433 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003434 *
3435 * Callback to set the value of a dapm enumerated double mixer control.
3436 *
3437 * Returns 0 for success.
3438 */
3439int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3440 struct snd_ctl_elem_value *ucontrol)
3441{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003442 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3443 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003444 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003445 unsigned int *item = ucontrol->value.enumerated.item;
Charles Keepax561ed682015-05-01 12:37:26 +01003446 unsigned int val, change, reg_change = 0;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003447 unsigned int mask;
Fabio Estevam33d92452018-02-14 13:39:05 -02003448 struct snd_soc_dapm_update update = {};
Nenghua Cao52765972013-12-13 20:13:49 +08003449 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003450
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003451 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003452 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003453
3454 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003455 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003456 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003457 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003458 return -EINVAL;
Chen-Yu Tsai071133a2016-08-27 19:27:59 +08003459 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003460 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003461 }
3462
Liam Girdwood3cd04342012-03-09 12:02:08 +00003463 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003464
Charles Keepax561ed682015-05-01 12:37:26 +01003465 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00003466
Charles Keepax561ed682015-05-01 12:37:26 +01003467 if (e->reg != SND_SOC_NOPM)
3468 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3469
3470 if (change || reg_change) {
3471 if (reg_change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003472 update.kcontrol = kcontrol;
3473 update.reg = e->reg;
3474 update.mask = mask;
3475 update.val = val;
3476 card->update = &update;
3477 }
Charles Keepax561ed682015-05-01 12:37:26 +01003478 change |= reg_change;
Mark Brown3a655772009-10-05 17:23:30 +01003479
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003480 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003481
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003482 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003483 }
3484
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003485 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003486
3487 if (ret > 0)
3488 soc_dpcm_runtime_update(card);
3489
Mark Brown97404f22010-12-14 16:13:57 +00003490 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003491}
3492EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3493
3494/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003495 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3496 *
3497 * @kcontrol: mixer control
3498 * @uinfo: control element information
3499 *
3500 * Callback to provide information about a pin switch control.
3501 */
3502int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3503 struct snd_ctl_elem_info *uinfo)
3504{
3505 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3506 uinfo->count = 1;
3507 uinfo->value.integer.min = 0;
3508 uinfo->value.integer.max = 1;
3509
3510 return 0;
3511}
3512EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3513
3514/**
3515 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3516 *
3517 * @kcontrol: mixer control
3518 * @ucontrol: Value
3519 */
3520int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3521 struct snd_ctl_elem_value *ucontrol)
3522{
Mark Brown48a8c392012-02-14 17:11:15 -08003523 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003524 const char *pin = (const char *)kcontrol->private_value;
3525
Liam Girdwood3cd04342012-03-09 12:02:08 +00003526 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003527
3528 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003529 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003530
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003531 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003532
3533 return 0;
3534}
3535EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3536
3537/**
3538 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3539 *
3540 * @kcontrol: mixer control
3541 * @ucontrol: Value
3542 */
3543int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3544 struct snd_ctl_elem_value *ucontrol)
3545{
Mark Brown48a8c392012-02-14 17:11:15 -08003546 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003547 const char *pin = (const char *)kcontrol->private_value;
3548
Mark Brown8b37dbd2009-02-28 21:14:20 +00003549 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003550 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003551 else
Mark Brown48a8c392012-02-14 17:11:15 -08003552 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003553
Mark Brown48a8c392012-02-14 17:11:15 -08003554 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003555 return 0;
3556}
3557EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3558
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +01003559struct snd_soc_dapm_widget *
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003560snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003561 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003562{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003563 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003564 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003565 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00003566 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003567
3568 if ((w = dapm_cnew_widget(widget)) == NULL)
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003569 return ERR_PTR(-ENOMEM);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003570
Mark Brown62ea8742012-01-21 21:14:48 +00003571 switch (w->id) {
3572 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003573 w->regulator = devm_regulator_get(dapm->dev, w->name);
3574 if (IS_ERR(w->regulator)) {
3575 ret = PTR_ERR(w->regulator);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003576 goto request_failed;
Mark Brown62ea8742012-01-21 21:14:48 +00003577 }
Mark Brown8784c772013-01-10 19:33:47 +00003578
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003579 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003580 ret = regulator_allow_bypass(w->regulator, true);
3581 if (ret != 0)
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003582 dev_warn(dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003583 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003584 w->name, ret);
3585 }
Mark Brown62ea8742012-01-21 21:14:48 +00003586 break;
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003587 case snd_soc_dapm_pinctrl:
3588 w->pinctrl = devm_pinctrl_get(dapm->dev);
Charles Keepaxa5cd7e92018-08-28 14:35:03 +01003589 if (IS_ERR(w->pinctrl)) {
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003590 ret = PTR_ERR(w->pinctrl);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003591 goto request_failed;
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003592 }
3593 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003594 case snd_soc_dapm_clock_supply:
Mark Brown695594f12012-06-04 08:14:13 +01003595 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003596 if (IS_ERR(w->clk)) {
3597 ret = PTR_ERR(w->clk);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003598 goto request_failed;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003599 }
3600 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003601 default:
3602 break;
3603 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003604
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003605 prefix = soc_dapm_prefix(dapm);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003606 if (prefix)
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003607 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003608 else
Lars-Peter Clausen48068962015-07-21 18:11:08 +02003609 w->name = kstrdup_const(widget->name, GFP_KERNEL);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003610 if (w->name == NULL) {
Pierre-Louis Bossart199ed3e2019-02-01 11:05:12 -06003611 kfree_const(w->sname);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003612 kfree(w);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003613 return ERR_PTR(-ENOMEM);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003614 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003615
Mark Brown7ca3a182011-10-08 14:04:50 +01003616 switch (w->id) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003617 case snd_soc_dapm_mic:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003618 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003619 w->power_check = dapm_generic_check_power;
3620 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003621 case snd_soc_dapm_input:
3622 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003623 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003624 w->power_check = dapm_generic_check_power;
3625 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003626 case snd_soc_dapm_spk:
3627 case snd_soc_dapm_hp:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003628 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003629 w->power_check = dapm_generic_check_power;
3630 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003631 case snd_soc_dapm_output:
3632 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003633 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003634 w->power_check = dapm_generic_check_power;
3635 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003636 case snd_soc_dapm_vmid:
3637 case snd_soc_dapm_siggen:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003638 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003639 w->power_check = dapm_always_on_check_power;
3640 break;
Vinod Koul56b44372015-11-23 21:22:30 +05303641 case snd_soc_dapm_sink:
3642 w->is_ep = SND_SOC_DAPM_EP_SINK;
3643 w->power_check = dapm_always_on_check_power;
3644 break;
3645
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003646 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003647 case snd_soc_dapm_demux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003648 case snd_soc_dapm_switch:
3649 case snd_soc_dapm_mixer:
3650 case snd_soc_dapm_mixer_named_ctl:
Mark Brown63c69a62013-07-18 22:03:01 +01003651 case snd_soc_dapm_adc:
3652 case snd_soc_dapm_aif_out:
3653 case snd_soc_dapm_dac:
3654 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003655 case snd_soc_dapm_pga:
Ranjani Sridharan6e3bfcf2019-03-01 19:08:53 -06003656 case snd_soc_dapm_buffer:
3657 case snd_soc_dapm_scheduler:
3658 case snd_soc_dapm_effect:
3659 case snd_soc_dapm_src:
3660 case snd_soc_dapm_asrc:
3661 case snd_soc_dapm_encoder:
3662 case snd_soc_dapm_decoder:
Mark Brown7ca3a182011-10-08 14:04:50 +01003663 case snd_soc_dapm_out_drv:
Mark Brown7ca3a182011-10-08 14:04:50 +01003664 case snd_soc_dapm_micbias:
Mark Brown7ca3a182011-10-08 14:04:50 +01003665 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003666 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02003667 case snd_soc_dapm_dai_out:
3668 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003669 w->power_check = dapm_generic_check_power;
3670 break;
3671 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003672 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003673 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003674 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003675 case snd_soc_dapm_kcontrol:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003676 w->is_supply = 1;
Mark Brown7ca3a182011-10-08 14:04:50 +01003677 w->power_check = dapm_supply_check_power;
3678 break;
3679 default:
3680 w->power_check = dapm_always_on_check_power;
3681 break;
3682 }
3683
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003684 w->dapm = dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003685 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003686 INIT_LIST_HEAD(&w->dirty);
Lars-Peter Clausen92fa1242015-05-01 18:02:42 +02003687 list_add_tail(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003688
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003689 snd_soc_dapm_for_each_direction(dir) {
3690 INIT_LIST_HEAD(&w->edges[dir]);
3691 w->endpoints[dir] = -1;
3692 }
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003693
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02003694 /* machine layer sets up unconnected pins and insertions */
Richard Purdie2b97eab2006-10-06 18:32:18 +02003695 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003696 return w;
Charles Keepax3bbf5d32018-09-05 15:20:58 +01003697
3698request_failed:
3699 if (ret != -EPROBE_DEFER)
3700 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3701 w->name, ret);
3702
3703 return ERR_PTR(ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003704}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003705
3706/**
Charles Keepax94e630a2018-09-05 15:20:59 +01003707 * snd_soc_dapm_new_control - create new dapm control
3708 * @dapm: DAPM context
3709 * @widget: widget template
3710 *
3711 * Creates new DAPM control based upon a template.
3712 *
3713 * Returns a widget pointer on success or an error pointer on failure
3714 */
3715struct snd_soc_dapm_widget *
3716snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3717 const struct snd_soc_dapm_widget *widget)
3718{
3719 struct snd_soc_dapm_widget *w;
3720
3721 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3722 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3723 mutex_unlock(&dapm->card->dapm_mutex);
3724
3725 return w;
3726}
3727EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3728
3729/**
Mark Brown4ba13272008-05-13 14:51:19 +02003730 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003731 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003732 * @widget: widget array
3733 * @num: number of widgets
3734 *
3735 * Creates new DAPM controls based upon the templates.
3736 *
3737 * Returns 0 for success else error.
3738 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003739int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003740 const struct snd_soc_dapm_widget *widget,
3741 int num)
3742{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003743 struct snd_soc_dapm_widget *w;
3744 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003745 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003746
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003747 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003748 for (i = 0; i < num; i++) {
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003749 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
Linus Walleij37e1df82017-01-13 10:23:52 +01003750 if (IS_ERR(w)) {
3751 ret = PTR_ERR(w);
Dan Carpenter60884c22012-04-13 22:25:43 +03003752 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003753 }
Mark Brown4ba13272008-05-13 14:51:19 +02003754 widget++;
3755 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003756 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003757 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003758}
3759EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3760
Mark Brownc74184e2012-04-04 22:12:09 +01003761static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3762 struct snd_kcontrol *kcontrol, int event)
3763{
Charles Keepax4a75aae2018-09-05 15:21:01 +01003764 struct snd_soc_dapm_path *path;
Mark Brownc74184e2012-04-04 22:12:09 +01003765 struct snd_soc_dai *source, *sink;
Charles Keepax249dc492018-08-15 13:11:35 +01003766 struct snd_soc_pcm_runtime *rtd = w->priv;
Charles Keepax243bcfa2018-09-05 15:21:02 +01003767 const struct snd_soc_pcm_stream *config;
Mark Brownc74184e2012-04-04 22:12:09 +01003768 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003769 struct snd_pcm_hw_params *params = NULL;
Nicolin Chen8053f212016-07-26 14:55:51 -07003770 struct snd_pcm_runtime *runtime = NULL;
Takashi Iwai3ba66fe2018-07-25 22:43:26 +02003771 unsigned int fmt;
Charles Keepaxfc269c02018-09-06 17:41:55 +01003772 int ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003773
Charles Keepax243bcfa2018-09-05 15:21:02 +01003774 config = rtd->dai_link->params + rtd->params_select;
3775
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003776 if (WARN_ON(!config) ||
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003777 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3778 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003779 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003780
Mark Brownc74184e2012-04-04 22:12:09 +01003781 /* Be a little careful as we don't want to overflow the mask array */
3782 if (config->formats) {
3783 fmt = ffs(config->formats) - 1;
3784 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003785 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003786 config->formats);
3787 fmt = 0;
3788 }
3789
3790 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003791 params = kzalloc(sizeof(*params), GFP_KERNEL);
3792 if (!params) {
3793 ret = -ENOMEM;
3794 goto out;
3795 }
3796 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003797
Mark Brown9747cec2012-04-26 19:12:21 +01003798 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003799 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003800 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003801 config->rate_max;
3802
Mark Brown9747cec2012-04-26 19:12:21 +01003803 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003804 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003805 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003806 = config->channels_max;
3807
3808 memset(&substream, 0, sizeof(substream));
3809
Nicolin Chen8053f212016-07-26 14:55:51 -07003810 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3811 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3812 if (!runtime) {
3813 ret = -ENOMEM;
3814 goto out;
3815 }
3816 substream.runtime = runtime;
Charles Keepax249dc492018-08-15 13:11:35 +01003817 substream.private_data = rtd;
Nicolin Chen8053f212016-07-26 14:55:51 -07003818
Mark Brownc74184e2012-04-04 22:12:09 +01003819 switch (event) {
3820 case SND_SOC_DAPM_PRE_PMU:
Benoit Cousson93e69582014-07-08 23:19:38 +02003821 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003822 snd_soc_dapm_widget_for_each_source_path(w, path) {
3823 source = path->source->priv;
3824
3825 if (source->driver->ops->startup) {
3826 ret = source->driver->ops->startup(&substream,
3827 source);
3828 if (ret < 0) {
3829 dev_err(source->dev,
3830 "ASoC: startup() failed: %d\n",
3831 ret);
3832 goto out;
3833 }
3834 source->active++;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303835 }
Charles Keepax4a75aae2018-09-05 15:21:01 +01003836 ret = soc_dai_hw_params(&substream, params, source);
3837 if (ret < 0)
3838 goto out;
Charles Keepax078a85f2019-01-31 13:30:18 +00003839
3840 dapm_update_dai_unlocked(&substream, params, source);
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303841 }
Mark Brownc74184e2012-04-04 22:12:09 +01003842
Benoit Cousson93e69582014-07-08 23:19:38 +02003843 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003844 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3845 sink = path->sink->priv;
3846
3847 if (sink->driver->ops->startup) {
3848 ret = sink->driver->ops->startup(&substream,
3849 sink);
3850 if (ret < 0) {
3851 dev_err(sink->dev,
3852 "ASoC: startup() failed: %d\n",
3853 ret);
3854 goto out;
3855 }
3856 sink->active++;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303857 }
Charles Keepax4a75aae2018-09-05 15:21:01 +01003858 ret = soc_dai_hw_params(&substream, params, sink);
3859 if (ret < 0)
3860 goto out;
Charles Keepax078a85f2019-01-31 13:30:18 +00003861
3862 dapm_update_dai_unlocked(&substream, params, sink);
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303863 }
Mark Brownc74184e2012-04-04 22:12:09 +01003864 break;
3865
3866 case SND_SOC_DAPM_POST_PMU:
Charles Keepax4a75aae2018-09-05 15:21:01 +01003867 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3868 sink = path->sink->priv;
3869
3870 ret = snd_soc_dai_digital_mute(sink, 0,
3871 SNDRV_PCM_STREAM_PLAYBACK);
3872 if (ret != 0 && ret != -ENOTSUPP)
3873 dev_warn(sink->dev,
3874 "ASoC: Failed to unmute: %d\n", ret);
3875 ret = 0;
3876 }
Mark Brownc74184e2012-04-04 22:12:09 +01003877 break;
3878
3879 case SND_SOC_DAPM_PRE_PMD:
Charles Keepax4a75aae2018-09-05 15:21:01 +01003880 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3881 sink = path->sink->priv;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303882
Charles Keepax4a75aae2018-09-05 15:21:01 +01003883 ret = snd_soc_dai_digital_mute(sink, 1,
3884 SNDRV_PCM_STREAM_PLAYBACK);
3885 if (ret != 0 && ret != -ENOTSUPP)
3886 dev_warn(sink->dev,
3887 "ASoC: Failed to mute: %d\n", ret);
3888 ret = 0;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303889 }
3890
Charles Keepax3c01b0e2018-10-11 17:28:28 +01003891 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003892 snd_soc_dapm_widget_for_each_source_path(w, path) {
3893 source = path->source->priv;
3894
Charles Keepax3c01b0e2018-10-11 17:28:28 +01003895 if (source->driver->ops->hw_free)
3896 source->driver->ops->hw_free(&substream,
3897 source);
3898
Charles Keepax4a75aae2018-09-05 15:21:01 +01003899 source->active--;
Charles Keepax3c01b0e2018-10-11 17:28:28 +01003900 if (source->driver->ops->shutdown)
Charles Keepax4a75aae2018-09-05 15:21:01 +01003901 source->driver->ops->shutdown(&substream,
3902 source);
Charles Keepax4a75aae2018-09-05 15:21:01 +01003903 }
3904
Charles Keepax3c01b0e2018-10-11 17:28:28 +01003905 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Charles Keepax4a75aae2018-09-05 15:21:01 +01003906 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3907 sink = path->sink->priv;
3908
Charles Keepax3c01b0e2018-10-11 17:28:28 +01003909 if (sink->driver->ops->hw_free)
3910 sink->driver->ops->hw_free(&substream, sink);
3911
Charles Keepax4a75aae2018-09-05 15:21:01 +01003912 sink->active--;
Charles Keepax3c01b0e2018-10-11 17:28:28 +01003913 if (sink->driver->ops->shutdown)
Charles Keepax4a75aae2018-09-05 15:21:01 +01003914 sink->driver->ops->shutdown(&substream, sink);
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303915 }
Mark Brownc74184e2012-04-04 22:12:09 +01003916 break;
3917
3918 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003919 WARN(1, "Unknown event %d\n", event);
Sudip Mukherjee75881df2015-09-10 18:01:44 +05303920 ret = -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003921 }
3922
Mark Brown9747cec2012-04-26 19:12:21 +01003923out:
Nicolin Chen8053f212016-07-26 14:55:51 -07003924 kfree(runtime);
Mark Brown9747cec2012-04-26 19:12:21 +01003925 kfree(params);
3926 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003927}
3928
Nikesh Oswalc6615082015-02-02 17:06:44 +00003929static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3930 struct snd_ctl_elem_value *ucontrol)
3931{
3932 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
Charles Keepax243bcfa2018-09-05 15:21:02 +01003933 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003934
Charles Keepax243bcfa2018-09-05 15:21:02 +01003935 ucontrol->value.enumerated.item[0] = rtd->params_select;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003936
3937 return 0;
3938}
3939
3940static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3941 struct snd_ctl_elem_value *ucontrol)
3942{
3943 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
Charles Keepax243bcfa2018-09-05 15:21:02 +01003944 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003945
3946 /* Can't change the config when widget is already powered */
3947 if (w->power)
3948 return -EBUSY;
3949
Charles Keepax243bcfa2018-09-05 15:21:02 +01003950 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003951 return 0;
3952
Charles Keepax243bcfa2018-09-05 15:21:02 +01003953 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003954 return -EINVAL;
3955
Charles Keepax243bcfa2018-09-05 15:21:02 +01003956 rtd->params_select = ucontrol->value.enumerated.item[0];
Nikesh Oswalc6615082015-02-02 17:06:44 +00003957
3958 return 0;
3959}
3960
Arnd Bergmannc42c5ac2017-10-10 11:20:11 +02003961static void
anish kumar19ad6832017-09-28 21:52:39 -07003962snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
3963 unsigned long *private_value,
3964 int num_params,
3965 const char **w_param_text)
Mark Brownc74184e2012-04-04 22:12:09 +01003966{
anish kumar19ad6832017-09-28 21:52:39 -07003967 int count;
3968
3969 devm_kfree(card->dev, (void *)*private_value);
Pankaj Bharadiyacacea3a2019-03-22 18:00:09 +05303970
3971 if (!w_param_text)
3972 return;
3973
anish kumar19ad6832017-09-28 21:52:39 -07003974 for (count = 0 ; count < num_params; count++)
3975 devm_kfree(card->dev, (void *)w_param_text[count]);
3976 devm_kfree(card->dev, w_param_text);
3977}
3978
3979static struct snd_kcontrol_new *
3980snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
3981 char *link_name,
3982 const struct snd_soc_pcm_stream *params,
3983 int num_params, const char **w_param_text,
3984 unsigned long *private_value)
3985{
Nikesh Oswalc6615082015-02-02 17:06:44 +00003986 struct soc_enum w_param_enum[] = {
3987 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3988 };
3989 struct snd_kcontrol_new kcontrol_dai_link[] = {
3990 SOC_ENUM_EXT(NULL, w_param_enum[0],
3991 snd_soc_dapm_dai_link_get,
3992 snd_soc_dapm_dai_link_put),
3993 };
anish kumar19ad6832017-09-28 21:52:39 -07003994 struct snd_kcontrol_new *kcontrol_news;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003995 const struct snd_soc_pcm_stream *config = params;
anish kumar19ad6832017-09-28 21:52:39 -07003996 int count;
Mark Brownc74184e2012-04-04 22:12:09 +01003997
Nikesh Oswalc6615082015-02-02 17:06:44 +00003998 for (count = 0 ; count < num_params; count++) {
3999 if (!config->stream_name) {
4000 dev_warn(card->dapm.dev,
4001 "ASoC: anonymous config %d for dai link %s\n",
4002 count, link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004003 w_param_text[count] =
Charles Keepax46172b62015-03-25 11:22:35 +00004004 devm_kasprintf(card->dev, GFP_KERNEL,
4005 "Anonymous Configuration %d",
4006 count);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004007 } else {
4008 w_param_text[count] = devm_kmemdup(card->dev,
4009 config->stream_name,
4010 strlen(config->stream_name) + 1,
4011 GFP_KERNEL);
Nikesh Oswalc6615082015-02-02 17:06:44 +00004012 }
anish kumar19ad6832017-09-28 21:52:39 -07004013 if (!w_param_text[count])
4014 goto outfree_w_param;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004015 config++;
4016 }
anish kumar19ad6832017-09-28 21:52:39 -07004017
Nikesh Oswalc6615082015-02-02 17:06:44 +00004018 w_param_enum[0].items = num_params;
4019 w_param_enum[0].texts = w_param_text;
Mark Brownc74184e2012-04-04 22:12:09 +01004020
anish kumar19ad6832017-09-28 21:52:39 -07004021 *private_value =
4022 (unsigned long) devm_kmemdup(card->dev,
4023 (void *)(kcontrol_dai_link[0].private_value),
4024 sizeof(struct soc_enum), GFP_KERNEL);
4025 if (!*private_value) {
4026 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4027 link_name);
4028 goto outfree_w_param;
4029 }
4030 kcontrol_dai_link[0].private_value = *private_value;
4031 /* duplicate kcontrol_dai_link on heap so that memory persists */
4032 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4033 sizeof(struct snd_kcontrol_new),
4034 GFP_KERNEL);
4035 if (!kcontrol_news) {
4036 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4037 link_name);
4038 goto outfree_w_param;
4039 }
4040 return kcontrol_news;
4041
4042outfree_w_param:
4043 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
4044 return NULL;
4045}
4046
Charles Keepax778ff5b2018-09-05 15:21:00 +01004047static struct snd_soc_dapm_widget *
4048snd_soc_dapm_new_dai(struct snd_soc_card *card, struct snd_soc_pcm_runtime *rtd,
4049 struct snd_soc_dapm_widget *source,
4050 struct snd_soc_dapm_widget *sink)
anish kumar19ad6832017-09-28 21:52:39 -07004051{
4052 struct snd_soc_dapm_widget template;
4053 struct snd_soc_dapm_widget *w;
4054 const char **w_param_text;
Pankaj Bharadiya8633d442019-03-22 21:53:39 +05304055 unsigned long private_value = 0;
anish kumar19ad6832017-09-28 21:52:39 -07004056 char *link_name;
4057 int ret;
4058
4059 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
4060 source->name, sink->name);
4061 if (!link_name)
Charles Keepax778ff5b2018-09-05 15:21:00 +01004062 return ERR_PTR(-ENOMEM);
anish kumar19ad6832017-09-28 21:52:39 -07004063
Mark Brownc74184e2012-04-04 22:12:09 +01004064 memset(&template, 0, sizeof(template));
4065 template.reg = SND_SOC_NOPM;
4066 template.id = snd_soc_dapm_dai_link;
4067 template.name = link_name;
4068 template.event = snd_soc_dai_link_event;
4069 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
4070 SND_SOC_DAPM_PRE_PMD;
anish kumar19ad6832017-09-28 21:52:39 -07004071 template.kcontrol_news = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01004072
anish kumar19ad6832017-09-28 21:52:39 -07004073 /* allocate memory for control, only in case of multiple configs */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004074 if (rtd->dai_link->num_params > 1) {
4075 w_param_text = devm_kcalloc(card->dev,
4076 rtd->dai_link->num_params,
4077 sizeof(char *), GFP_KERNEL);
anish kumar19ad6832017-09-28 21:52:39 -07004078 if (!w_param_text) {
4079 ret = -ENOMEM;
4080 goto param_fail;
4081 }
4082
4083 template.num_kcontrols = 1;
4084 template.kcontrol_news =
4085 snd_soc_dapm_alloc_kcontrol(card,
Charles Keepax778ff5b2018-09-05 15:21:00 +01004086 link_name,
4087 rtd->dai_link->params,
4088 rtd->dai_link->num_params,
anish kumar19ad6832017-09-28 21:52:39 -07004089 w_param_text, &private_value);
4090 if (!template.kcontrol_news) {
4091 ret = -ENOMEM;
4092 goto param_fail;
4093 }
Arnd Bergmann667ebc92017-10-10 11:20:10 +02004094 } else {
4095 w_param_text = NULL;
anish kumar19ad6832017-09-28 21:52:39 -07004096 }
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004097 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01004098
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004099 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
Dan Carpenter2e558a82018-09-07 22:40:33 +03004100 if (IS_ERR(w)) {
4101 ret = PTR_ERR(w);
Linus Walleij37e1df82017-01-13 10:23:52 +01004102 goto outfree_kcontrol_news;
Dan Carpenter2e558a82018-09-07 22:40:33 +03004103 }
Mark Brownc74184e2012-04-04 22:12:09 +01004104
Charles Keepax249dc492018-08-15 13:11:35 +01004105 w->priv = rtd;
Mark Brownc74184e2012-04-04 22:12:09 +01004106
Charles Keepax778ff5b2018-09-05 15:21:00 +01004107 return w;
Nikesh Oswalc6615082015-02-02 17:06:44 +00004108
Nikesh Oswalc6615082015-02-02 17:06:44 +00004109outfree_kcontrol_news:
4110 devm_kfree(card->dev, (void *)template.kcontrol_news);
Charles Keepax778ff5b2018-09-05 15:21:00 +01004111 snd_soc_dapm_free_kcontrol(card, &private_value,
4112 rtd->dai_link->num_params, w_param_text);
anish kumar19ad6832017-09-28 21:52:39 -07004113param_fail:
Nikesh Oswalc6615082015-02-02 17:06:44 +00004114 devm_kfree(card->dev, link_name);
Charles Keepax778ff5b2018-09-05 15:21:00 +01004115 return ERR_PTR(ret);
Mark Brownc74184e2012-04-04 22:12:09 +01004116}
4117
Mark Brown888df392012-02-16 19:37:51 -08004118int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4119 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004120{
Mark Brown888df392012-02-16 19:37:51 -08004121 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004122 struct snd_soc_dapm_widget *w;
4123
Mark Brown888df392012-02-16 19:37:51 -08004124 WARN_ON(dapm->dev != dai->dev);
4125
4126 memset(&template, 0, sizeof(template));
4127 template.reg = SND_SOC_NOPM;
4128
4129 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01004130 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08004131 template.name = dai->driver->playback.stream_name;
4132 template.sname = dai->driver->playback.stream_name;
4133
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004134 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08004135 template.name);
4136
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004137 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01004138 if (IS_ERR(w))
4139 return PTR_ERR(w);
Mark Brown888df392012-02-16 19:37:51 -08004140
4141 w->priv = dai;
4142 dai->playback_widget = w;
4143 }
4144
4145 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01004146 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08004147 template.name = dai->driver->capture.stream_name;
4148 template.sname = dai->driver->capture.stream_name;
4149
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004150 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08004151 template.name);
4152
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004153 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Charles Keepax3bbf5d32018-09-05 15:20:58 +01004154 if (IS_ERR(w))
4155 return PTR_ERR(w);
Mark Brown888df392012-02-16 19:37:51 -08004156
4157 w->priv = dai;
4158 dai->capture_widget = w;
4159 }
4160
4161 return 0;
4162}
4163
4164int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4165{
4166 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004167 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08004168 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08004169
4170 /* For each DAI widget... */
4171 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01004172 switch (dai_w->id) {
4173 case snd_soc_dapm_dai_in:
4174 case snd_soc_dapm_dai_out:
4175 break;
4176 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02004177 continue;
Mark Brown46162742013-06-05 19:36:11 +01004178 }
Mark Brown888df392012-02-16 19:37:51 -08004179
Liam Girdwoode01b4f62018-06-14 20:26:42 +01004180 /* let users know there is no DAI to link */
4181 if (!dai_w->priv) {
4182 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4183 dai_w->name);
4184 continue;
4185 }
4186
Mark Brown888df392012-02-16 19:37:51 -08004187 dai = dai_w->priv;
4188
4189 /* ...find all widgets with the same stream and link them */
4190 list_for_each_entry(w, &card->widgets, list) {
4191 if (w->dapm != dai_w->dapm)
4192 continue;
4193
Mark Brown46162742013-06-05 19:36:11 +01004194 switch (w->id) {
4195 case snd_soc_dapm_dai_in:
4196 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08004197 continue;
Mark Brown46162742013-06-05 19:36:11 +01004198 default:
4199 break;
4200 }
Mark Brown888df392012-02-16 19:37:51 -08004201
Lars-Peter Clausena798c242015-07-21 11:51:35 +02004202 if (!w->sname || !strstr(w->sname, dai_w->sname))
Mark Brown888df392012-02-16 19:37:51 -08004203 continue;
4204
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004205 if (dai_w->id == snd_soc_dapm_dai_in) {
4206 src = dai_w;
4207 sink = w;
4208 } else {
4209 src = w;
4210 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08004211 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004212 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4213 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004214 }
4215 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02004216
Mark Brown888df392012-02-16 19:37:51 -08004217 return 0;
4218}
Liam Girdwood64a648c2011-07-25 11:15:15 +01004219
Benoit Cousson44ba2642014-07-08 23:19:36 +02004220static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4221 struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004222{
Benoit Cousson44ba2642014-07-08 23:19:36 +02004223 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00004224 struct snd_soc_dai *codec_dai;
Charles Keepax778ff5b2018-09-05 15:21:00 +01004225 struct snd_soc_dapm_widget *playback = NULL, *capture = NULL;
4226 struct snd_soc_dapm_widget *codec, *playback_cpu, *capture_cpu;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004227 int i;
4228
Charles Keepax778ff5b2018-09-05 15:21:00 +01004229 if (rtd->dai_link->params) {
Charles Keepax778ff5b2018-09-05 15:21:00 +01004230 playback_cpu = cpu_dai->capture_widget;
4231 capture_cpu = cpu_dai->playback_widget;
4232 } else {
4233 playback = cpu_dai->playback_widget;
4234 capture = cpu_dai->capture_widget;
4235 playback_cpu = playback;
4236 capture_cpu = capture;
4237 }
4238
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00004239 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004240
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004241 /* connect BE DAI playback if widgets are valid */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004242 codec = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004243
Charles Keepax778ff5b2018-09-05 15:21:00 +01004244 if (playback_cpu && codec) {
4245 if (!playback) {
4246 playback = snd_soc_dapm_new_dai(card, rtd,
4247 playback_cpu,
4248 codec);
Charles Keepaxe36a1d02018-09-10 15:28:39 +01004249 if (IS_ERR(playback)) {
4250 dev_err(rtd->dev,
4251 "ASoC: Failed to create DAI %s: %ld\n",
4252 codec_dai->name,
4253 PTR_ERR(playback));
4254 continue;
4255 }
Charles Keepax778ff5b2018-09-05 15:21:00 +01004256
4257 snd_soc_dapm_add_path(&card->dapm, playback_cpu,
4258 playback, NULL, NULL);
4259 }
4260
4261 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4262 cpu_dai->component->name, playback_cpu->name,
4263 codec_dai->component->name, codec->name);
4264
4265 snd_soc_dapm_add_path(&card->dapm, playback, codec,
4266 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004267 }
Charles Keepaxe36a1d02018-09-10 15:28:39 +01004268 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004269
Charles Keepaxe36a1d02018-09-10 15:28:39 +01004270 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004271 /* connect BE DAI capture if widgets are valid */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004272 codec = codec_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004273
Charles Keepax778ff5b2018-09-05 15:21:00 +01004274 if (codec && capture_cpu) {
4275 if (!capture) {
4276 capture = snd_soc_dapm_new_dai(card, rtd,
4277 codec,
4278 capture_cpu);
Charles Keepaxe36a1d02018-09-10 15:28:39 +01004279 if (IS_ERR(capture)) {
4280 dev_err(rtd->dev,
4281 "ASoC: Failed to create DAI %s: %ld\n",
4282 codec_dai->name,
4283 PTR_ERR(capture));
4284 continue;
4285 }
Charles Keepax778ff5b2018-09-05 15:21:00 +01004286
4287 snd_soc_dapm_add_path(&card->dapm, capture,
4288 capture_cpu, NULL, NULL);
4289 }
4290
4291 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4292 codec_dai->component->name, codec->name,
4293 cpu_dai->component->name, capture_cpu->name);
4294
4295 snd_soc_dapm_add_path(&card->dapm, codec, capture,
4296 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004297 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004298 }
4299}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004300
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004301static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4302 int event)
4303{
4304 struct snd_soc_dapm_widget *w;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004305 unsigned int ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004306
4307 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4308 w = dai->playback_widget;
4309 else
4310 w = dai->capture_widget;
4311
4312 if (w) {
4313 dapm_mark_dirty(w, "stream event");
4314
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004315 if (w->id == snd_soc_dapm_dai_in) {
4316 ep = SND_SOC_DAPM_EP_SOURCE;
4317 dapm_widget_invalidate_input_paths(w);
4318 } else {
4319 ep = SND_SOC_DAPM_EP_SINK;
4320 dapm_widget_invalidate_output_paths(w);
4321 }
4322
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004323 switch (event) {
4324 case SND_SOC_DAPM_STREAM_START:
4325 w->active = 1;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004326 w->is_ep = ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004327 break;
4328 case SND_SOC_DAPM_STREAM_STOP:
4329 w->active = 0;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004330 w->is_ep = 0;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004331 break;
4332 case SND_SOC_DAPM_STREAM_SUSPEND:
4333 case SND_SOC_DAPM_STREAM_RESUME:
4334 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4335 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4336 break;
4337 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004338 }
4339}
4340
Benoit Cousson44ba2642014-07-08 23:19:36 +02004341void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4342{
Mengdong Lin1a497982015-11-18 02:34:11 -05004343 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson44ba2642014-07-08 23:19:36 +02004344
4345 /* for each BE DAI link... */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00004346 for_each_card_rtds(card, rtd) {
Benoit Cousson44ba2642014-07-08 23:19:36 +02004347 /*
4348 * dynamic FE links have no fixed DAI mapping.
4349 * CODEC<->CODEC links have no direct connection.
4350 */
Charles Keepax778ff5b2018-09-05 15:21:00 +01004351 if (rtd->dai_link->dynamic)
Benoit Cousson44ba2642014-07-08 23:19:36 +02004352 continue;
4353
4354 dapm_connect_dai_link_widgets(card, rtd);
4355 }
4356}
4357
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004358static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4359 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004360{
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00004361 struct snd_soc_dai *codec_dai;
Benoit Cousson44ba2642014-07-08 23:19:36 +02004362 int i;
4363
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004364 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00004365 for_each_rtd_codec_dai(rtd, i, codec_dai)
4366 soc_dapm_dai_stream_event(codec_dai, stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004367
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004368 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004369}
4370
4371/**
4372 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4373 * @rtd: PCM runtime data
4374 * @stream: stream name
4375 * @event: stream event
4376 *
4377 * Sends a stream event to the dapm core. The core then makes any
4378 * necessary widget power changes.
4379 *
4380 * Returns 0 for success else error.
4381 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004382void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4383 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004384{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004385 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004386
Liam Girdwood3cd04342012-03-09 12:02:08 +00004387 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004388 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004389 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004390}
Richard Purdie2b97eab2006-10-06 18:32:18 +02004391
4392/**
Charles Keepax11391102014-02-18 15:22:14 +00004393 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4394 * @dapm: DAPM context
4395 * @pin: pin name
4396 *
4397 * Enables input/output pin and its parents or children widgets iff there is
4398 * a valid audio route and active audio stream.
4399 *
4400 * Requires external locking.
4401 *
4402 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4403 * do any widget power switching.
4404 */
4405int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4406 const char *pin)
4407{
4408 return snd_soc_dapm_set_pin(dapm, pin, 1);
4409}
4410EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4411
4412/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004413 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004414 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004415 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02004416 *
Mark Brown74b8f952009-06-06 11:26:15 +01004417 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01004418 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00004419 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004420 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4421 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02004422 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004423int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004424{
Charles Keepax11391102014-02-18 15:22:14 +00004425 int ret;
4426
4427 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4428
4429 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4430
4431 mutex_unlock(&dapm->card->dapm_mutex);
4432
4433 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004434}
Liam Girdwooda5302182008-07-07 13:35:17 +01004435EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004436
4437/**
Charles Keepax11391102014-02-18 15:22:14 +00004438 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4439 * @dapm: DAPM context
4440 * @pin: pin name
4441 *
4442 * Enables input/output pin regardless of any other state. This is
4443 * intended for use with microphone bias supplies used in microphone
4444 * jack detection.
4445 *
4446 * Requires external locking.
4447 *
4448 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4449 * do any widget power switching.
4450 */
4451int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4452 const char *pin)
4453{
4454 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4455
4456 if (!w) {
4457 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4458 return -EINVAL;
4459 }
4460
4461 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02004462 if (!w->connected) {
4463 /*
4464 * w->force does not affect the number of input or output paths,
4465 * so we only have to recheck if w->connected is changed
4466 */
4467 dapm_widget_invalidate_input_paths(w);
4468 dapm_widget_invalidate_output_paths(w);
4469 w->connected = 1;
4470 }
Charles Keepax11391102014-02-18 15:22:14 +00004471 w->force = 1;
4472 dapm_mark_dirty(w, "force enable");
4473
4474 return 0;
4475}
4476EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4477
4478/**
Mark Brownda341832010-03-15 19:23:37 +00004479 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004480 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00004481 * @pin: pin name
4482 *
4483 * Enables input/output pin regardless of any other state. This is
4484 * intended for use with microphone bias supplies used in microphone
4485 * jack detection.
4486 *
4487 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4488 * do any widget power switching.
4489 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004490int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4491 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00004492{
Charles Keepax11391102014-02-18 15:22:14 +00004493 int ret;
Mark Brownda341832010-03-15 19:23:37 +00004494
Charles Keepax11391102014-02-18 15:22:14 +00004495 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00004496
Charles Keepax11391102014-02-18 15:22:14 +00004497 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09004498
Charles Keepax11391102014-02-18 15:22:14 +00004499 mutex_unlock(&dapm->card->dapm_mutex);
4500
4501 return ret;
Mark Brownda341832010-03-15 19:23:37 +00004502}
4503EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4504
4505/**
Charles Keepax11391102014-02-18 15:22:14 +00004506 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4507 * @dapm: DAPM context
4508 * @pin: pin name
4509 *
4510 * Disables input/output pin and its parents or children widgets.
4511 *
4512 * Requires external locking.
4513 *
4514 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4515 * do any widget power switching.
4516 */
4517int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4518 const char *pin)
4519{
4520 return snd_soc_dapm_set_pin(dapm, pin, 0);
4521}
4522EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4523
4524/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004525 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004526 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004527 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004528 *
Mark Brown74b8f952009-06-06 11:26:15 +01004529 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00004530 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004531 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4532 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004533 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004534int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4535 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01004536{
Charles Keepax11391102014-02-18 15:22:14 +00004537 int ret;
4538
4539 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4540
4541 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4542
4543 mutex_unlock(&dapm->card->dapm_mutex);
4544
4545 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01004546}
4547EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4548
4549/**
Charles Keepax11391102014-02-18 15:22:14 +00004550 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4551 * @dapm: DAPM context
4552 * @pin: pin name
4553 *
4554 * Marks the specified pin as being not connected, disabling it along
4555 * any parent or child widgets. At present this is identical to
4556 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4557 * additional things such as disabling controls which only affect
4558 * paths through the pin.
4559 *
4560 * Requires external locking.
4561 *
4562 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4563 * do any widget power switching.
4564 */
4565int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4566 const char *pin)
4567{
4568 return snd_soc_dapm_set_pin(dapm, pin, 0);
4569}
4570EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4571
4572/**
Mark Brown5817b522008-09-24 11:23:11 +01004573 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004574 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01004575 * @pin: pin name
4576 *
4577 * Marks the specified pin as being not connected, disabling it along
4578 * any parent or child widgets. At present this is identical to
4579 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4580 * additional things such as disabling controls which only affect
4581 * paths through the pin.
4582 *
4583 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4584 * do any widget power switching.
4585 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004586int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01004587{
Charles Keepax11391102014-02-18 15:22:14 +00004588 int ret;
4589
4590 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4591
4592 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4593
4594 mutex_unlock(&dapm->card->dapm_mutex);
4595
4596 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01004597}
4598EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4599
4600/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004601 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004602 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004603 * @pin: audio signal pin endpoint (or start point)
4604 *
4605 * Get audio pin status - connected or disconnected.
4606 *
4607 * Returns 1 for connected otherwise 0.
4608 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004609int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4610 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004611{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004612 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004613
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004614 if (w)
4615 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06004616
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004617 return 0;
4618}
Liam Girdwooda5302182008-07-07 13:35:17 +01004619EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004620
4621/**
Mark Brown1547aba2010-05-07 21:11:40 +01004622 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004623 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01004624 * @pin: audio signal pin endpoint (or start point)
4625 *
4626 * Mark the given endpoint or pin as ignoring suspend. When the
4627 * system is disabled a path between two endpoints flagged as ignoring
4628 * suspend will not be disabled. The path must already be enabled via
4629 * normal means at suspend time, it will not be turned on if it was not
4630 * already enabled.
4631 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004632int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4633 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01004634{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004635 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01004636
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004637 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004638 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004639 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01004640 }
4641
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004642 w->ignore_suspend = 1;
4643
4644 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01004645}
4646EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4647
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02004648/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02004649 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03004650 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02004651 *
4652 * Free all dapm widgets and resources.
4653 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004654void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004655{
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02004656 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004657 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02004658 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004659}
4660EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4661
Xiang Xiao57996352014-03-02 00:04:02 +08004662static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01004663{
Liam Girdwood01005a72012-07-06 16:57:05 +01004664 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01004665 struct snd_soc_dapm_widget *w;
4666 LIST_HEAD(down_list);
4667 int powerdown = 0;
4668
Liam Girdwood01005a72012-07-06 16:57:05 +01004669 mutex_lock(&card->dapm_mutex);
4670
Jarkko Nikula97c866d2010-12-14 12:18:31 +02004671 list_for_each_entry(w, &dapm->card->widgets, list) {
4672 if (w->dapm != dapm)
4673 continue;
Mark Brown51737472009-06-22 13:16:51 +01004674 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004675 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01004676 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004677 powerdown = 1;
4678 }
4679 }
4680
4681 /* If there were no widgets to power down we're already in
4682 * standby.
4683 */
4684 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004685 if (dapm->bias_level == SND_SOC_BIAS_ON)
4686 snd_soc_dapm_set_bias_level(dapm,
4687 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004688 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004689 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4690 snd_soc_dapm_set_bias_level(dapm,
4691 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004692 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004693
4694 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004695}
Mark Brown51737472009-06-22 13:16:51 +01004696
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004697/*
4698 * snd_soc_dapm_shutdown - callback for system shutdown
4699 */
4700void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4701{
Xiang Xiao57996352014-03-02 00:04:02 +08004702 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004703
Xiang Xiao57996352014-03-02 00:04:02 +08004704 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08004705 if (dapm != &card->dapm) {
4706 soc_dapm_shutdown_dapm(dapm);
4707 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4708 snd_soc_dapm_set_bias_level(dapm,
4709 SND_SOC_BIAS_OFF);
4710 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004711 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08004712
4713 soc_dapm_shutdown_dapm(&card->dapm);
4714 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4715 snd_soc_dapm_set_bias_level(&card->dapm,
4716 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01004717}
4718
Richard Purdie2b97eab2006-10-06 18:32:18 +02004719/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004720MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004721MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4722MODULE_LICENSE("GPL");