blob: 0a738cb439becfd5c8b61ee0f377e93c5feda2ea [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>
21#include <linux/moduleparam.h>
22#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080023#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020024#include <linux/delay.h>
25#include <linux/pm.h>
26#include <linux/bitops.h>
27#include <linux/platform_device.h>
28#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020029#include <linux/debugfs.h>
Mark Brownf1aac482011-12-05 15:17:06 +000030#include <linux/pm_runtime.h>
Mark Brown62ea8742012-01-21 21:14:48 +000031#include <linux/regulator/consumer.h>
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +000032#include <linux/pinctrl/consumer.h>
Ola Liljad7e7eb92012-05-24 15:26:25 +020033#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020035#include <sound/core.h>
36#include <sound/pcm.h>
37#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020038#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020039#include <sound/initval.h>
40
Mark Brown84e90932010-11-04 00:07:02 -040041#include <trace/events/asoc.h>
42
Mark Brownde02d072011-09-20 21:43:24 +010043#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
44
Lars-Peter Clausena3423b02015-08-11 21:38:00 +020045#define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
46 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
47
48#define snd_soc_dapm_for_each_direction(dir) \
49 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
50 (dir)++)
51
Lars-Peter Clausen57295072013-08-05 11:27:31 +020052static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
53 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
54 const char *control,
55 int (*connected)(struct snd_soc_dapm_widget *source,
56 struct snd_soc_dapm_widget *sink));
Vladimir Zapolskiy5353f652015-06-02 00:57:53 +030057
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +010058struct snd_soc_dapm_widget *
Lars-Peter Clausen57295072013-08-05 11:27:31 +020059snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
60 const struct snd_soc_dapm_widget *widget);
61
Liam Girdwood02aa78a2015-05-25 18:21:17 +010062struct snd_soc_dapm_widget *
63snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +020064 const struct snd_soc_dapm_widget *widget);
65
Richard Purdie2b97eab2006-10-06 18:32:18 +020066/* dapm power sequences - make this per codec in the future */
67static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010068 [snd_soc_dapm_pre] = 0,
Mark Brown62ea8742012-01-21 21:14:48 +000069 [snd_soc_dapm_regulator_supply] = 1,
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +000070 [snd_soc_dapm_pinctrl] = 1,
Ola Liljad7e7eb92012-05-24 15:26:25 +020071 [snd_soc_dapm_clock_supply] = 1,
Mark Brown1dd275b2013-10-09 13:56:37 +010072 [snd_soc_dapm_supply] = 2,
73 [snd_soc_dapm_micbias] = 3,
Mark Brownc74184e2012-04-04 22:12:09 +010074 [snd_soc_dapm_dai_link] = 2,
Mark Brown1dd275b2013-10-09 13:56:37 +010075 [snd_soc_dapm_dai_in] = 4,
76 [snd_soc_dapm_dai_out] = 4,
77 [snd_soc_dapm_aif_in] = 4,
78 [snd_soc_dapm_aif_out] = 4,
79 [snd_soc_dapm_mic] = 5,
80 [snd_soc_dapm_mux] = 6,
Lars-Peter Clausend714f972015-05-01 18:02:43 +020081 [snd_soc_dapm_demux] = 6,
Mark Brown1dd275b2013-10-09 13:56:37 +010082 [snd_soc_dapm_dac] = 7,
83 [snd_soc_dapm_switch] = 8,
84 [snd_soc_dapm_mixer] = 8,
85 [snd_soc_dapm_mixer_named_ctl] = 8,
86 [snd_soc_dapm_pga] = 9,
87 [snd_soc_dapm_adc] = 10,
88 [snd_soc_dapm_out_drv] = 11,
89 [snd_soc_dapm_hp] = 11,
90 [snd_soc_dapm_spk] = 11,
91 [snd_soc_dapm_line] = 11,
92 [snd_soc_dapm_kcontrol] = 12,
93 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +020094};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000095
Richard Purdie2b97eab2006-10-06 18:32:18 +020096static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010097 [snd_soc_dapm_pre] = 0,
Lars-Peter Clausen57295072013-08-05 11:27:31 +020098 [snd_soc_dapm_kcontrol] = 1,
99 [snd_soc_dapm_adc] = 2,
100 [snd_soc_dapm_hp] = 3,
101 [snd_soc_dapm_spk] = 3,
102 [snd_soc_dapm_line] = 3,
103 [snd_soc_dapm_out_drv] = 3,
Mark Brown38357ab2009-06-06 19:03:23 +0100104 [snd_soc_dapm_pga] = 4,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +0200105 [snd_soc_dapm_switch] = 5,
Mark Brown38357ab2009-06-06 19:03:23 +0100106 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +0100107 [snd_soc_dapm_mixer] = 5,
108 [snd_soc_dapm_dac] = 6,
109 [snd_soc_dapm_mic] = 7,
110 [snd_soc_dapm_micbias] = 8,
111 [snd_soc_dapm_mux] = 9,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200112 [snd_soc_dapm_demux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +0100113 [snd_soc_dapm_aif_in] = 10,
114 [snd_soc_dapm_aif_out] = 10,
Mark Brown46162742013-06-05 19:36:11 +0100115 [snd_soc_dapm_dai_in] = 10,
116 [snd_soc_dapm_dai_out] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +0100117 [snd_soc_dapm_dai_link] = 11,
Mark Brownc74184e2012-04-04 22:12:09 +0100118 [snd_soc_dapm_supply] = 12,
Mark Brown1dd275b2013-10-09 13:56:37 +0100119 [snd_soc_dapm_clock_supply] = 13,
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +0000120 [snd_soc_dapm_pinctrl] = 13,
Mark Brown1dd275b2013-10-09 13:56:37 +0100121 [snd_soc_dapm_regulator_supply] = 13,
122 [snd_soc_dapm_post] = 14,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200123};
124
Mark Brownf9fa2b12014-03-06 16:49:11 +0800125static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
126{
127 if (dapm->card && dapm->card->instantiated)
128 lockdep_assert_held(&dapm->card->dapm_mutex);
129}
130
Troy Kisky12ef1932008-10-13 17:42:14 -0700131static void pop_wait(u32 pop_time)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100132{
133 if (pop_time)
134 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
135}
136
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200137static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100138{
139 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200140 char *buf;
141
142 if (!pop_time)
143 return;
144
145 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
146 if (buf == NULL)
147 return;
Mark Brown15e4c72f2008-07-02 11:51:20 +0100148
149 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200150 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100151 dev_info(dev, "%s", buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100152 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200153
154 kfree(buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100155}
156
Mark Browndb432b42011-10-03 21:06:40 +0100157static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
158{
159 return !list_empty(&w->dirty);
160}
161
Mark Brown492c0a12014-03-06 16:15:48 +0800162static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100163{
Mark Brownf9fa2b12014-03-06 16:49:11 +0800164 dapm_assert_locked(w->dapm);
165
Mark Brown75c1f892011-10-04 22:28:08 +0100166 if (!dapm_dirty_widget(w)) {
167 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
168 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100169 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100170 }
Mark Browndb432b42011-10-03 21:06:40 +0100171}
172
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200173/*
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200174 * Common implementation for dapm_widget_invalidate_input_paths() and
175 * dapm_widget_invalidate_output_paths(). The function is inlined since the
176 * combined size of the two specialized functions is only marginally larger then
177 * the size of the generic function and at the same time the fast path of the
178 * specialized functions is significantly smaller than the generic function.
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200179 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200180static __always_inline void dapm_widget_invalidate_paths(
181 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200182{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200183 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
184 struct snd_soc_dapm_widget *node;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200185 struct snd_soc_dapm_path *p;
186 LIST_HEAD(list);
187
188 dapm_assert_locked(w->dapm);
189
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200190 if (w->endpoints[dir] == -1)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200191 return;
192
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200193 list_add_tail(&w->work_list, &list);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200194 w->endpoints[dir] = -1;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200195
196 list_for_each_entry(w, &list, work_list) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200197 snd_soc_dapm_widget_for_each_path(w, dir, p) {
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200198 if (p->is_supply || p->weak || !p->connect)
199 continue;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200200 node = p->node[rdir];
201 if (node->endpoints[dir] != -1) {
202 node->endpoints[dir] = -1;
203 list_add_tail(&node->work_list, &list);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200204 }
205 }
206 }
207}
208
209/*
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200210 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
211 * input paths
212 * @w: The widget for which to invalidate the cached number of input paths
213 *
214 * Resets the cached number of inputs for the specified widget and all widgets
215 * that can be reached via outcoming paths from the widget.
216 *
217 * This function must be called if the number of output paths for a widget might
218 * have changed. E.g. if the source state of a widget changes or a path is added
219 * or activated with the widget as the sink.
220 */
221static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
222{
223 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
224}
225
226/*
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200227 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
228 * output paths
229 * @w: The widget for which to invalidate the cached number of output paths
230 *
231 * Resets the cached number of outputs for the specified widget and all widgets
232 * that can be reached via incoming paths from the widget.
233 *
234 * This function must be called if the number of output paths for a widget might
235 * have changed. E.g. if the sink state of a widget changes or a path is added
236 * or activated with the widget as the source.
237 */
238static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
239{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200240 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200241}
242
243/*
244 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
245 * for the widgets connected to a path
246 * @p: The path to invalidate
247 *
248 * Resets the cached number of inputs for the sink of the path and the cached
249 * number of outputs for the source of the path.
250 *
251 * This function must be called when a path is added, removed or the connected
252 * state changes.
253 */
254static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
255{
256 /*
257 * Weak paths or supply paths do not influence the number of input or
258 * output paths of their neighbors.
259 */
260 if (p->weak || p->is_supply)
261 return;
262
263 /*
264 * The number of connected endpoints is the sum of the number of
265 * connected endpoints of all neighbors. If a node with 0 connected
266 * endpoints is either connected or disconnected that sum won't change,
267 * so there is no need to re-check the path.
268 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200269 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200270 dapm_widget_invalidate_input_paths(p->sink);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200271 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200272 dapm_widget_invalidate_output_paths(p->source);
273}
274
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200275void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
Mark Browne2d32ff2012-08-31 17:38:32 -0700276{
Mark Browne2d32ff2012-08-31 17:38:32 -0700277 struct snd_soc_dapm_widget *w;
278
279 mutex_lock(&card->dapm_mutex);
280
281 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200282 if (w->is_ep) {
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200283 dapm_mark_dirty(w, "Rechecking endpoints");
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200284 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200285 dapm_widget_invalidate_output_paths(w);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200286 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200287 dapm_widget_invalidate_input_paths(w);
288 }
Mark Browne2d32ff2012-08-31 17:38:32 -0700289 }
290
291 mutex_unlock(&card->dapm_mutex);
292}
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200293EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
Mark Browne2d32ff2012-08-31 17:38:32 -0700294
Richard Purdie2b97eab2006-10-06 18:32:18 +0200295/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100296static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200297 const struct snd_soc_dapm_widget *_widget)
298{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100299 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200300}
301
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200302struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200303 unsigned int value;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200304 struct snd_soc_dapm_widget *widget;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200305 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200306 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200307};
308
309static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100310 struct snd_kcontrol *kcontrol, const char *ctrl_name)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200311{
312 struct dapm_kcontrol_data *data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200313 struct soc_mixer_control *mc;
Charles Keepax561ed682015-05-01 12:37:26 +0100314 struct soc_enum *e;
Charles Keepax773da9b2015-05-01 12:37:25 +0100315 const char *name;
316 int ret;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200317
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200318 data = kzalloc(sizeof(*data), GFP_KERNEL);
Charles Keepax40b7bea2015-05-01 12:37:24 +0100319 if (!data)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200320 return -ENOMEM;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200321
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200322 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200323
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200324 switch (widget->id) {
325 case snd_soc_dapm_switch:
326 case snd_soc_dapm_mixer:
327 case snd_soc_dapm_mixer_named_ctl:
328 mc = (struct soc_mixer_control *)kcontrol->private_value;
329
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800330 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
331 dev_warn(widget->dapm->dev,
332 "ASoC: Unsupported stereo autodisable control '%s'\n",
333 ctrl_name);
334
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200335 if (mc->autodisable) {
336 struct snd_soc_dapm_widget template;
337
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100338 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
Charles Keepax773da9b2015-05-01 12:37:25 +0100339 "Autodisable");
340 if (!name) {
341 ret = -ENOMEM;
342 goto err_data;
343 }
344
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200345 memset(&template, 0, sizeof(template));
346 template.reg = mc->reg;
347 template.mask = (1 << fls(mc->max)) - 1;
348 template.shift = mc->shift;
349 if (mc->invert)
350 template.off_val = mc->max;
351 else
352 template.off_val = 0;
353 template.on_val = template.off_val;
354 template.id = snd_soc_dapm_kcontrol;
Charles Keepax773da9b2015-05-01 12:37:25 +0100355 template.name = name;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200356
Lars-Peter Clausen2daabd72013-08-30 17:39:33 +0200357 data->value = template.on_val;
358
Liam Girdwood02aa78a2015-05-25 18:21:17 +0100359 data->widget =
360 snd_soc_dapm_new_control_unlocked(widget->dapm,
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200361 &template);
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200362 kfree(name);
Linus Walleij37e1df82017-01-13 10:23:52 +0100363 if (IS_ERR(data->widget)) {
364 ret = PTR_ERR(data->widget);
365 goto err_data;
366 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200367 if (!data->widget) {
Charles Keepax773da9b2015-05-01 12:37:25 +0100368 ret = -ENOMEM;
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200369 goto err_data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200370 }
371 }
372 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200373 case snd_soc_dapm_demux:
Charles Keepax561ed682015-05-01 12:37:26 +0100374 case snd_soc_dapm_mux:
375 e = (struct soc_enum *)kcontrol->private_value;
376
377 if (e->autodisable) {
378 struct snd_soc_dapm_widget template;
379
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100380 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
Charles Keepax561ed682015-05-01 12:37:26 +0100381 "Autodisable");
382 if (!name) {
383 ret = -ENOMEM;
384 goto err_data;
385 }
386
387 memset(&template, 0, sizeof(template));
388 template.reg = e->reg;
389 template.mask = e->mask << e->shift_l;
390 template.shift = e->shift_l;
391 template.off_val = snd_soc_enum_item_to_val(e, 0);
392 template.on_val = template.off_val;
393 template.id = snd_soc_dapm_kcontrol;
394 template.name = name;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200395
396 data->value = template.on_val;
397
Charles Keepaxffacb482015-06-26 10:39:43 +0100398 data->widget = snd_soc_dapm_new_control_unlocked(
399 widget->dapm, &template);
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200400 kfree(name);
Linus Walleij37e1df82017-01-13 10:23:52 +0100401 if (IS_ERR(data->widget)) {
402 ret = PTR_ERR(data->widget);
403 goto err_data;
404 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200405 if (!data->widget) {
Charles Keepax561ed682015-05-01 12:37:26 +0100406 ret = -ENOMEM;
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200407 goto err_data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200408 }
Charles Keepax561ed682015-05-01 12:37:26 +0100409
410 snd_soc_dapm_add_path(widget->dapm, data->widget,
411 widget, NULL, NULL);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200412 }
413 break;
414 default:
415 break;
416 }
417
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200418 kcontrol->private_data = data;
419
420 return 0;
Charles Keepax773da9b2015-05-01 12:37:25 +0100421
Charles Keepax773da9b2015-05-01 12:37:25 +0100422err_data:
423 kfree(data);
424 return ret;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200425}
426
427static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
428{
429 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Srinivas Kandagatlaff2faf12018-06-04 12:13:26 +0100430
431 list_del(&data->paths);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200432 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200433 kfree(data);
434}
435
436static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
437 const struct snd_kcontrol *kcontrol)
438{
439 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
440
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200441 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200442}
443
444static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
445 struct snd_soc_dapm_widget *widget)
446{
447 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200448 struct snd_soc_dapm_widget_list *new_wlist;
449 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200450
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200451 if (data->wlist)
452 n = data->wlist->num_widgets + 1;
453 else
454 n = 1;
455
456 new_wlist = krealloc(data->wlist,
457 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
458 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200459 return -ENOMEM;
460
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200461 new_wlist->widgets[n - 1] = widget;
462 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200463
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200464 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200465
466 return 0;
467}
468
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200469static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
470 struct snd_soc_dapm_path *path)
471{
472 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
473
474 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200475}
476
477static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
478{
479 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
480
481 if (!data->widget)
482 return true;
483
484 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200485}
486
487static struct list_head *dapm_kcontrol_get_path_list(
488 const struct snd_kcontrol *kcontrol)
489{
490 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
491
492 return &data->paths;
493}
494
495#define dapm_kcontrol_for_each_path(path, kcontrol) \
496 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
497 list_kcontrol)
498
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530499unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200500{
501 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
502
503 return data->value;
504}
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530505EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200506
507static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
508 unsigned int value)
509{
510 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
511
512 if (data->value == value)
513 return false;
514
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200515 if (data->widget)
516 data->widget->on_val = value;
517
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200518 data->value = value;
519
520 return true;
521}
522
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200523/**
Mythri P K93e39a12015-10-20 22:30:08 +0530524 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
525 * kcontrol
526 * @kcontrol: The kcontrol
527 */
528struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
529 struct snd_kcontrol *kcontrol)
530{
531 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
532}
533EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
534
535/**
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200536 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
537 * kcontrol
538 * @kcontrol: The kcontrol
539 *
540 * Note: This function must only be used on kcontrols that are known to have
541 * been registered for a CODEC. Otherwise the behaviour is undefined.
542 */
543struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
544 struct snd_kcontrol *kcontrol)
545{
546 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
547}
548EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
549
Liam Girdwood6c120e12012-02-15 15:15:34 +0000550static void dapm_reset(struct snd_soc_card *card)
551{
552 struct snd_soc_dapm_widget *w;
553
Mark Brownf9fa2b12014-03-06 16:49:11 +0800554 lockdep_assert_held(&card->dapm_mutex);
555
Liam Girdwood6c120e12012-02-15 15:15:34 +0000556 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
557
558 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200559 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000560 w->power_checked = false;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000561 }
562}
563
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200564static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
565{
566 if (!dapm->component)
567 return NULL;
568 return dapm->component->name_prefix;
569}
570
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200571static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800572 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100573{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200574 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200575 return -EIO;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200576 return snd_soc_component_read(dapm->component, reg, value);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100577}
578
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200579static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
Bard Liao34775012014-04-17 20:12:56 +0800580 int reg, unsigned int mask, unsigned int value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100581{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200582 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200583 return -EIO;
Mark Brownfcf6c5e2014-12-15 13:08:48 +0000584 return snd_soc_component_update_bits(dapm->component, reg,
585 mask, value);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000586}
587
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200588static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
589 int reg, unsigned int mask, unsigned int value)
590{
591 if (!dapm->component)
592 return -EIO;
593 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
594}
595
Mark Browneb270e92013-10-09 13:52:52 +0100596static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
597{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200598 if (dapm->component)
599 snd_soc_component_async_complete(dapm->component);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100600}
601
Charles Keepax45a110a2015-05-11 13:50:30 +0100602static struct snd_soc_dapm_widget *
603dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
604{
605 struct snd_soc_dapm_widget *w = wcache->widget;
606 struct list_head *wlist;
607 const int depth = 2;
608 int i = 0;
609
610 if (w) {
611 wlist = &w->dapm->card->widgets;
612
613 list_for_each_entry_from(w, wlist, list) {
614 if (!strcmp(name, w->name))
615 return w;
616
617 if (++i == depth)
618 break;
619 }
620 }
621
622 return NULL;
623}
624
625static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
626 struct snd_soc_dapm_widget *w)
627{
628 wcache->widget = w;
629}
630
Mark Brown452c5ea2009-05-17 21:41:23 +0100631/**
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200632 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
633 * @dapm: The DAPM context for which to set the level
634 * @level: The level to set
635 *
636 * Forces the DAPM bias level to a specific state. It will call the bias level
637 * callback of DAPM context with the specified level. This will even happen if
638 * the context is already at the same level. Furthermore it will not go through
639 * the normal bias level sequencing, meaning any intermediate states between the
640 * current and the target state will not be entered.
641 *
642 * Note that the change in bias level is only temporary and the next time
643 * snd_soc_dapm_sync() is called the state will be set to the level as
644 * determined by the DAPM core. The function is mainly intended to be used to
645 * used during probe or resume from suspend to power up the device so
646 * initialization can be done, before the DAPM core takes over.
647 */
648int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
649 enum snd_soc_bias_level level)
650{
651 int ret = 0;
652
653 if (dapm->set_bias_level)
654 ret = dapm->set_bias_level(dapm, level);
655
Lars-Peter Clausenf4bf8d72015-04-27 22:13:25 +0200656 if (ret == 0)
657 dapm->bias_level = level;
658
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200659 return ret;
660}
661EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
662
Mark Brown452c5ea2009-05-17 21:41:23 +0100663/**
664 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800665 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100666 * @level: level to configure
667 *
668 * Configure the bias (power) levels for the SoC audio device.
669 *
670 * Returns 0 for success else error.
671 */
Mark Browned5a4c42011-02-18 11:12:42 -0800672static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200673 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100674{
Mark Browned5a4c42011-02-18 11:12:42 -0800675 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100676 int ret = 0;
677
Mark Brown84e90932010-11-04 00:07:02 -0400678 trace_snd_soc_bias_level_start(card, level);
679
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000680 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100681 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100682 if (ret != 0)
683 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100684
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200685 if (!card || dapm != &card->dapm)
686 ret = snd_soc_dapm_force_bias_level(dapm, level);
Liam Girdwood41231282012-07-06 16:56:16 +0100687
Mark Brown171ec6b2011-06-06 18:15:19 +0100688 if (ret != 0)
689 goto out;
690
691 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100692 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100693out:
Mark Brown84e90932010-11-04 00:07:02 -0400694 trace_snd_soc_bias_level_done(card, level);
695
Mark Brown452c5ea2009-05-17 21:41:23 +0100696 return ret;
697}
698
Mark Brown74b8f952009-06-06 11:26:15 +0100699/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200700static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200701 struct snd_soc_dapm_path *path, const char *control_name,
702 struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200703{
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200704 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200705 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100706 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200707 int i;
708
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100709 if (e->reg != SND_SOC_NOPM) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200710 soc_dapm_read(dapm, e->reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100711 val = (val >> e->shift_l) & e->mask;
712 item = snd_soc_enum_val_to_item(e, val);
713 } else {
714 /* since a virtual mux has no backing registers to
715 * decide which path to connect, it will try to match
716 * with the first enumeration. This is to ensure
717 * that the default mux choice (the first) will be
718 * correctly powered up during initialization.
719 */
720 item = 0;
721 }
722
Xie Yishengf9e0b4a2018-05-31 19:11:23 +0800723 i = match_string(e->texts, e->items, control_name);
724 if (i < 0)
725 return -ENODEV;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200726
Xie Yishengf9e0b4a2018-05-31 19:11:23 +0800727 path->name = e->texts[i];
728 path->connect = (i == item);
729 return 0;
730
Richard Purdie2b97eab2006-10-06 18:32:18 +0200731}
732
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100733/* set up initial codec paths */
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800734static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
735 int nth_path)
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100736{
737 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200738 p->sink->kcontrol_news[i].private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100739 unsigned int reg = mc->reg;
740 unsigned int shift = mc->shift;
741 unsigned int max = mc->max;
742 unsigned int mask = (1 << fls(max)) - 1;
743 unsigned int invert = mc->invert;
744 unsigned int val;
745
746 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200747 soc_dapm_read(p->sink->dapm, reg, &val);
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800748 /*
749 * The nth_path argument allows this function to know
750 * which path of a kcontrol it is setting the initial
751 * status for. Ideally this would support any number
752 * of paths and channels. But since kcontrols only come
753 * in mono and stereo variants, we are limited to 2
754 * channels.
755 *
756 * The following code assumes for stereo controls the
757 * first path is the left channel, and all remaining
758 * paths are the right channel.
759 */
760 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
761 if (reg != mc->rreg)
762 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
763 val = (val >> mc->rshift) & mask;
764 } else {
765 val = (val >> shift) & mask;
766 }
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100767 if (invert)
768 val = max - val;
769 p->connect = !!val;
770 } else {
771 p->connect = 0;
772 }
773}
774
Mark Brown74b8f952009-06-06 11:26:15 +0100775/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200776static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200777 struct snd_soc_dapm_path *path, const char *control_name)
778{
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800779 int i, nth_path = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200780
781 /* search for mixer kcontrol */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200782 for (i = 0; i < path->sink->num_kcontrols; i++) {
783 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
784 path->name = path->sink->kcontrol_news[i].name;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800785 dapm_set_mixer_path_status(path, i, nth_path++);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200786 return 0;
787 }
788 }
789 return -ENODEV;
790}
791
Stephen Warrenaf468002011-04-28 17:38:01 -0600792static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600793 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600794 const struct snd_kcontrol_new *kcontrol_new,
795 struct snd_kcontrol **kcontrol)
796{
797 struct snd_soc_dapm_widget *w;
798 int i;
799
800 *kcontrol = NULL;
801
802 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600803 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
804 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600805 for (i = 0; i < w->num_kcontrols; i++) {
806 if (&w->kcontrol_news[i] == kcontrol_new) {
807 if (w->kcontrols)
808 *kcontrol = w->kcontrols[i];
809 return 1;
810 }
811 }
812 }
813
814 return 0;
815}
816
Stephen Warren85762e72013-03-29 15:40:10 -0600817/*
818 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
819 * create it. Either way, add the widget into the control's widget list
820 */
Jeeja KP19a2557b2015-10-20 22:30:06 +0530821static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100822 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200823{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200824 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000825 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000826 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600827 size_t prefix_len;
828 int shared;
829 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600830 bool wname_in_long_name, kcname_in_long_name;
Daniel Macke5092c92014-10-07 13:41:24 +0200831 char *long_name = NULL;
Stephen Warren85762e72013-03-29 15:40:10 -0600832 const char *name;
Daniel Macke5092c92014-10-07 13:41:24 +0200833 int ret = 0;
Mark Brownefb7ac32011-03-08 17:23:24 +0000834
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200835 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000836 if (prefix)
837 prefix_len = strlen(prefix) + 1;
838 else
839 prefix_len = 0;
840
Stephen Warren85762e72013-03-29 15:40:10 -0600841 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
842 &kcontrol);
843
Stephen Warren85762e72013-03-29 15:40:10 -0600844 if (!kcontrol) {
845 if (shared) {
846 wname_in_long_name = false;
847 kcname_in_long_name = true;
848 } else {
849 switch (w->id) {
850 case snd_soc_dapm_switch:
851 case snd_soc_dapm_mixer:
Jeeja KP19a2557b2015-10-20 22:30:06 +0530852 case snd_soc_dapm_pga:
Chen-Yu Tsaia3930ed2016-08-27 19:28:00 +0800853 case snd_soc_dapm_out_drv:
Stephen Warren85762e72013-03-29 15:40:10 -0600854 wname_in_long_name = true;
855 kcname_in_long_name = true;
856 break;
857 case snd_soc_dapm_mixer_named_ctl:
858 wname_in_long_name = false;
859 kcname_in_long_name = true;
860 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200861 case snd_soc_dapm_demux:
Stephen Warren85762e72013-03-29 15:40:10 -0600862 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600863 wname_in_long_name = true;
864 kcname_in_long_name = false;
865 break;
866 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600867 return -EINVAL;
868 }
869 }
870
871 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600872 /*
873 * The control will get a prefix from the control
874 * creation process but we're also using the same
875 * prefix for widgets so cut the prefix off the
876 * front of the widget name.
877 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200878 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600879 w->name + prefix_len,
880 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200881 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200882 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600883
884 name = long_name;
885 } else if (wname_in_long_name) {
886 long_name = NULL;
887 name = w->name + prefix_len;
888 } else {
889 long_name = NULL;
890 name = w->kcontrol_news[kci].name;
891 }
892
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200893 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600894 prefix);
Daniel Macke5092c92014-10-07 13:41:24 +0200895 if (!kcontrol) {
896 ret = -ENOMEM;
897 goto exit_free;
898 }
899
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200900 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200901
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100902 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200903 if (ret) {
904 snd_ctl_free_one(kcontrol);
Daniel Macke5092c92014-10-07 13:41:24 +0200905 goto exit_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200906 }
907
Stephen Warren85762e72013-03-29 15:40:10 -0600908 ret = snd_ctl_add(card, kcontrol);
909 if (ret < 0) {
910 dev_err(dapm->dev,
911 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
912 w->name, name, ret);
Daniel Macke5092c92014-10-07 13:41:24 +0200913 goto exit_free;
Stephen Warren85762e72013-03-29 15:40:10 -0600914 }
Stephen Warren85762e72013-03-29 15:40:10 -0600915 }
916
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200917 ret = dapm_kcontrol_add_widget(kcontrol, w);
Daniel Macke5092c92014-10-07 13:41:24 +0200918 if (ret == 0)
919 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200920
Daniel Macke5092c92014-10-07 13:41:24 +0200921exit_free:
922 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600923
Daniel Macke5092c92014-10-07 13:41:24 +0200924 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600925}
926
927/* create new dapm mixer control */
928static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
929{
930 int i, ret;
931 struct snd_soc_dapm_path *path;
Charles Keepax561ed682015-05-01 12:37:26 +0100932 struct dapm_kcontrol_data *data;
Stephen Warren85762e72013-03-29 15:40:10 -0600933
Richard Purdie2b97eab2006-10-06 18:32:18 +0200934 /* add kcontrol */
935 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200936 /* match name */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +0200937 snd_soc_dapm_widget_for_each_source_path(w, path) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200938 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600939 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200940 continue;
941
Charles Keepax561ed682015-05-01 12:37:26 +0100942 if (!w->kcontrols[i]) {
Jeeja KP19a2557b2015-10-20 22:30:06 +0530943 ret = dapm_create_or_share_kcontrol(w, i);
Charles Keepax561ed682015-05-01 12:37:26 +0100944 if (ret < 0)
945 return ret;
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200946 }
947
Mark Brown946d92a2013-08-12 23:28:42 +0100948 dapm_kcontrol_add_path(w->kcontrols[i], path);
Charles Keepax561ed682015-05-01 12:37:26 +0100949
950 data = snd_kcontrol_chip(w->kcontrols[i]);
951 if (data->widget)
952 snd_soc_dapm_add_path(data->widget->dapm,
953 data->widget,
954 path->source,
955 NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200956 }
957 }
Stephen Warren85762e72013-03-29 15:40:10 -0600958
959 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200960}
961
962/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200963static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200964{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200965 struct snd_soc_dapm_context *dapm = w->dapm;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200966 enum snd_soc_dapm_direction dir;
Stephen Warren85762e72013-03-29 15:40:10 -0600967 struct snd_soc_dapm_path *path;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200968 const char *type;
Stephen Warrenaf468002011-04-28 17:38:01 -0600969 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200970
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200971 switch (w->id) {
972 case snd_soc_dapm_mux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200973 dir = SND_SOC_DAPM_DIR_OUT;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200974 type = "mux";
975 break;
976 case snd_soc_dapm_demux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200977 dir = SND_SOC_DAPM_DIR_IN;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200978 type = "demux";
979 break;
980 default:
981 return -EINVAL;
982 }
983
Stephen Warrenaf468002011-04-28 17:38:01 -0600984 if (w->num_kcontrols != 1) {
985 dev_err(dapm->dev,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200986 "ASoC: %s %s has incorrect number of controls\n", type,
Stephen Warrenaf468002011-04-28 17:38:01 -0600987 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200988 return -EINVAL;
989 }
990
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200991 if (list_empty(&w->edges[dir])) {
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200992 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
Stephen Warren85762e72013-03-29 15:40:10 -0600993 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600994 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200995
Jeeja KP19a2557b2015-10-20 22:30:06 +0530996 ret = dapm_create_or_share_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600997 if (ret < 0)
998 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600999
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001000 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1001 if (path->name)
1002 dapm_kcontrol_add_path(w->kcontrols[0], path);
Lars-Peter Clausen98407ef2014-10-25 17:41:57 +02001003 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001004
Stephen Warrenaf468002011-04-28 17:38:01 -06001005 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001006}
1007
1008/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001009static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001010{
Jeeja KP19a2557b2015-10-20 22:30:06 +05301011 int i, ret;
1012
1013 for (i = 0; i < w->num_kcontrols; i++) {
1014 ret = dapm_create_or_share_kcontrol(w, i);
1015 if (ret < 0)
1016 return ret;
1017 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001018
Mark Browna6c65732010-03-03 17:45:21 +00001019 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001020}
1021
Nikesh Oswalc6615082015-02-02 17:06:44 +00001022/* create new dapm dai link control */
1023static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1024{
1025 int i, ret;
1026 struct snd_kcontrol *kcontrol;
1027 struct snd_soc_dapm_context *dapm = w->dapm;
1028 struct snd_card *card = dapm->card->snd_card;
1029
1030 /* create control for links with > 1 config */
1031 if (w->num_params <= 1)
1032 return 0;
1033
1034 /* add kcontrol */
1035 for (i = 0; i < w->num_kcontrols; i++) {
1036 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1037 w->name, NULL);
1038 ret = snd_ctl_add(card, kcontrol);
1039 if (ret < 0) {
1040 dev_err(dapm->dev,
1041 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1042 w->name, w->kcontrol_news[i].name, ret);
1043 return ret;
1044 }
1045 kcontrol->private_data = w;
1046 w->kcontrols[i] = kcontrol;
1047 }
1048
1049 return 0;
1050}
1051
Mark Brown9949788b2010-05-07 20:24:05 +01001052/* We implement power down on suspend by checking the power state of
1053 * the ALSA card - when we are suspending the ALSA state for the card
1054 * is set to D3.
1055 */
1056static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1057{
Mark Brown12ea2c72011-03-02 18:17:32 +00001058 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +01001059
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001060 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +01001061 case SNDRV_CTL_POWER_D3hot:
1062 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +01001063 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001064 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001065 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +01001066 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +01001067 default:
1068 return 1;
1069 }
1070}
1071
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001072static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1073 struct list_head *widgets)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001074{
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001075 struct snd_soc_dapm_widget *w;
1076 struct list_head *it;
1077 unsigned int size = 0;
1078 unsigned int i = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001079
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001080 list_for_each(it, widgets)
1081 size++;
1082
Kees Cookacafe7e2018-05-08 13:45:50 -07001083 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001084 if (*list == NULL)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001085 return -ENOMEM;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001086
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001087 list_for_each_entry(w, widgets, work_list)
1088 (*list)->widgets[i++] = w;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001089
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001090 (*list)->num_widgets = i;
1091
1092 return 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001093}
1094
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001095/*
1096 * Common implementation for is_connected_output_ep() and
1097 * is_connected_input_ep(). The function is inlined since the combined size of
1098 * the two specialized functions is only marginally larger then the size of the
1099 * generic function and at the same time the fast path of the specialized
1100 * functions is significantly smaller than the generic function.
1101 */
1102static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1103 struct list_head *list, enum snd_soc_dapm_direction dir,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001104 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1105 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1106 enum snd_soc_dapm_direction)),
1107 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1108 enum snd_soc_dapm_direction))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001109{
1110 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001111 struct snd_soc_dapm_path *path;
1112 int con = 0;
1113
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001114 if (widget->endpoints[dir] >= 0)
1115 return widget->endpoints[dir];
Mark Brown024dc072011-10-09 11:52:05 +01001116
Mark Brownde02d072011-09-20 21:43:24 +01001117 DAPM_UPDATE_STAT(widget, path_checks);
1118
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001119 /* do we need to add this widget to the list ? */
1120 if (list)
1121 list_add_tail(&widget->work_list, list);
1122
Jeeja KP09464972016-06-15 11:16:55 +05301123 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1124 widget->endpoints[dir] = 1;
1125 return widget->endpoints[dir];
1126 }
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001127
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001128 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1129 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1130 return widget->endpoints[dir];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001131 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001132
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001133 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
Mark Browne56235e02011-09-21 18:19:14 +01001134 DAPM_UPDATE_STAT(widget, neighbour_checks);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001135
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001136 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +01001137 continue;
1138
Mark Brown8af294b2013-02-22 17:48:15 +00001139 if (path->walking)
1140 return 1;
1141
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001142 trace_snd_soc_dapm_path(widget, dir, path);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001143
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001144 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +00001145 path->walking = 1;
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001146 con += fn(path->node[dir], list, custom_stop_condition);
Mark Brown8af294b2013-02-22 17:48:15 +00001147 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001148 }
1149 }
1150
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001151 widget->endpoints[dir] = con;
Mark Brown024dc072011-10-09 11:52:05 +01001152
Richard Purdie2b97eab2006-10-06 18:32:18 +02001153 return con;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001154}
1155
1156/*
1157 * Recursively check for a completed path to an active or physically connected
1158 * output widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001159 *
1160 * Optionally, can be supplied with a function acting as a stopping condition.
1161 * This function takes the dapm widget currently being examined and the walk
1162 * direction as an arguments, it should return true if the walk should be
1163 * stopped and false otherwise.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001164 */
1165static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001166 struct list_head *list,
1167 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1168 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001169{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001170 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001171 is_connected_output_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001172}
1173
1174/*
1175 * Recursively check for a completed path to an active or physically connected
1176 * input widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001177 *
1178 * Optionally, can be supplied with a function acting as a stopping condition.
1179 * This function takes the dapm widget currently being examined and the walk
1180 * direction as an arguments, it should return true if the walk should be
1181 * stopped and false otherwise.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001182 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001183static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001184 struct list_head *list,
1185 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1186 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001187{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001188 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001189 is_connected_input_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001190}
1191
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001192/**
1193 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1194 * @dai: the soc DAI.
1195 * @stream: stream direction.
1196 * @list: list of active widgets for this stream.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001197 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1198 * walk based on custom logic.
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001199 *
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001200 * Queries DAPM graph as to whether a valid audio stream path exists for
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001201 * the initial stream specified by name. This takes into account
1202 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1203 *
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001204 * Optionally, can be supplied with a function acting as a stopping condition.
1205 * This function takes the dapm widget currently being examined and the walk
1206 * direction as an arguments, it should return true if the walk should be
1207 * stopped and false otherwise.
1208 *
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001209 * Returns the number of valid paths or negative error.
1210 */
1211int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001212 struct snd_soc_dapm_widget_list **list,
1213 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1214 enum snd_soc_dapm_direction))
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001215{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01001216 struct snd_soc_card *card = dai->component->card;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001217 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001218 LIST_HEAD(widgets);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001219 int paths;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001220 int ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001221
1222 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001223
1224 /*
1225 * For is_connected_{output,input}_ep fully discover the graph we need
1226 * to reset the cached number of inputs and outputs.
1227 */
1228 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001229 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1230 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001231 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001232
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001233 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001234 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1235 custom_stop_condition);
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001236 else
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001237 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1238 custom_stop_condition);
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001239
1240 /* Drop starting point */
1241 list_del(widgets.next);
1242
1243 ret = dapm_widget_list_create(list, &widgets);
1244 if (ret)
Lars-Peter Clausen30abbe72015-08-11 21:37:59 +02001245 paths = ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001246
1247 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001248 mutex_unlock(&card->dapm_mutex);
1249
1250 return paths;
1251}
1252
Richard Purdie2b97eab2006-10-06 18:32:18 +02001253/*
Mark Brown62ea8742012-01-21 21:14:48 +00001254 * Handler for regulator supply widget.
1255 */
1256int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1257 struct snd_kcontrol *kcontrol, int event)
1258{
Mark Brownc05b84d2012-09-07 12:57:11 +08001259 int ret;
1260
Mark Browneb270e92013-10-09 13:52:52 +01001261 soc_dapm_async_complete(w->dapm);
1262
Mark Brownc05b84d2012-09-07 12:57:11 +08001263 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001264 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001265 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001266 if (ret != 0)
1267 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001268 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001269 w->name, ret);
1270 }
1271
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001272 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001273 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001274 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001275 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001276 if (ret != 0)
1277 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001278 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001279 w->name, ret);
1280 }
1281
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001282 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001283 }
Mark Brown62ea8742012-01-21 21:14:48 +00001284}
1285EXPORT_SYMBOL_GPL(dapm_regulator_event);
1286
Ola Liljad7e7eb92012-05-24 15:26:25 +02001287/*
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00001288 * Handler for pinctrl widget.
1289 */
1290int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1291 struct snd_kcontrol *kcontrol, int event)
1292{
1293 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1294 struct pinctrl *p = w->pinctrl;
1295 struct pinctrl_state *s;
1296
1297 if (!p || !priv)
1298 return -EIO;
1299
1300 if (SND_SOC_DAPM_EVENT_ON(event))
1301 s = pinctrl_lookup_state(p, priv->active_state);
1302 else
1303 s = pinctrl_lookup_state(p, priv->sleep_state);
1304
1305 if (IS_ERR(s))
1306 return PTR_ERR(s);
1307
1308 return pinctrl_select_state(p, s);
1309}
1310EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1311
1312/*
Ola Liljad7e7eb92012-05-24 15:26:25 +02001313 * Handler for clock supply widget.
1314 */
1315int dapm_clock_event(struct snd_soc_dapm_widget *w,
1316 struct snd_kcontrol *kcontrol, int event)
1317{
1318 if (!w->clk)
1319 return -EIO;
1320
Mark Browneb270e92013-10-09 13:52:52 +01001321 soc_dapm_async_complete(w->dapm);
1322
Ola Liljad7e7eb92012-05-24 15:26:25 +02001323 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001324 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001325 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001326 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001327 return 0;
1328 }
Charles Keepaxd78b1e432018-08-28 14:35:02 +01001329
Marek Belisko98b3cf12012-07-12 23:00:16 +02001330 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001331}
1332EXPORT_SYMBOL_GPL(dapm_clock_event);
1333
Mark Brownd805002b2011-09-28 18:28:23 +01001334static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1335{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001336 if (w->power_checked)
1337 return w->new_power;
1338
Mark Brownd805002b2011-09-28 18:28:23 +01001339 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001340 w->new_power = 1;
Mark Brownd805002b2011-09-28 18:28:23 +01001341 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001342 w->new_power = w->power_check(w);
1343
1344 w->power_checked = true;
1345
1346 return w->new_power;
Mark Brownd805002b2011-09-28 18:28:23 +01001347}
1348
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001349/* Generic check to see if a widget should be powered. */
Mark Browncd0f2d42009-04-20 16:56:59 +01001350static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1351{
1352 int in, out;
1353
Mark Brownde02d072011-09-20 21:43:24 +01001354 DAPM_UPDATE_STAT(w, power_checks);
1355
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001356 in = is_connected_input_ep(w, NULL, NULL);
1357 out = is_connected_output_ep(w, NULL, NULL);
Mark Browncd0f2d42009-04-20 16:56:59 +01001358 return out != 0 && in != 0;
1359}
1360
Mark Brown246d0a12009-04-22 18:24:55 +01001361/* Check to see if a power supply is needed */
1362static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1363{
1364 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001365
Mark Brownde02d072011-09-20 21:43:24 +01001366 DAPM_UPDATE_STAT(w, power_checks);
1367
Mark Brown246d0a12009-04-22 18:24:55 +01001368 /* Check if one of our outputs is connected */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001369 snd_soc_dapm_widget_for_each_sink_path(w, path) {
Mark Browna8fdac82011-09-28 18:20:26 +01001370 DAPM_UPDATE_STAT(w, neighbour_checks);
1371
Mark Brownbf3a9e12011-06-13 16:42:29 +01001372 if (path->weak)
1373 continue;
1374
Mark Brown215edda2009-09-08 18:59:05 +01001375 if (path->connected &&
1376 !path->connected(path->source, path->sink))
1377 continue;
1378
Mark Brownf68d7e12011-10-04 22:57:50 +01001379 if (dapm_widget_power_check(path->sink))
1380 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001381 }
1382
Mark Brownf68d7e12011-10-04 22:57:50 +01001383 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001384}
1385
Mark Brown35c64bc2011-09-28 18:23:53 +01001386static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1387{
Charles Keepax20bb0182015-12-02 10:22:16 +00001388 return w->connected;
Mark Brown35c64bc2011-09-28 18:23:53 +01001389}
1390
Mark Brown38357ab2009-06-06 19:03:23 +01001391static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1392 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001393 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001394{
Mark Brown828a8422011-01-15 13:14:30 +00001395 int *sort;
1396
1397 if (power_up)
1398 sort = dapm_up_seq;
1399 else
1400 sort = dapm_down_seq;
1401
Mark Brown38357ab2009-06-06 19:03:23 +01001402 if (sort[a->id] != sort[b->id])
1403 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001404 if (a->subseq != b->subseq) {
1405 if (power_up)
1406 return a->subseq - b->subseq;
1407 else
1408 return b->subseq - a->subseq;
1409 }
Mark Brownb22ead22009-06-07 12:51:26 +01001410 if (a->reg != b->reg)
1411 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001412 if (a->dapm != b->dapm)
1413 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001414
Mark Brown38357ab2009-06-06 19:03:23 +01001415 return 0;
1416}
Mark Brown42aa3412009-03-01 19:21:10 +00001417
Mark Brown38357ab2009-06-06 19:03:23 +01001418/* Insert a widget in order into a DAPM power sequence. */
1419static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1420 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001421 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001422{
1423 struct snd_soc_dapm_widget *w;
1424
1425 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001426 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001427 list_add_tail(&new_widget->power_list, &w->power_list);
1428 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001429 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001430
Mark Brown38357ab2009-06-06 19:03:23 +01001431 list_add_tail(&new_widget->power_list, list);
1432}
Mark Brown42aa3412009-03-01 19:21:10 +00001433
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001434static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001435 struct snd_soc_dapm_widget *w, int event)
1436{
Mark Brown68f89ad2010-11-03 23:51:49 -04001437 const char *ev_name;
1438 int power, ret;
1439
1440 switch (event) {
1441 case SND_SOC_DAPM_PRE_PMU:
1442 ev_name = "PRE_PMU";
1443 power = 1;
1444 break;
1445 case SND_SOC_DAPM_POST_PMU:
1446 ev_name = "POST_PMU";
1447 power = 1;
1448 break;
1449 case SND_SOC_DAPM_PRE_PMD:
1450 ev_name = "PRE_PMD";
1451 power = 0;
1452 break;
1453 case SND_SOC_DAPM_POST_PMD:
1454 ev_name = "POST_PMD";
1455 power = 0;
1456 break;
Mark Brown80114122013-02-25 15:14:19 +00001457 case SND_SOC_DAPM_WILL_PMU:
1458 ev_name = "WILL_PMU";
1459 power = 1;
1460 break;
1461 case SND_SOC_DAPM_WILL_PMD:
1462 ev_name = "WILL_PMD";
1463 power = 0;
1464 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001465 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001466 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001467 return;
1468 }
1469
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001470 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001471 return;
1472
1473 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001474 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001475 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001476 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001477 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001478 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001479 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001480 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001481 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001482 ev_name, w->name, ret);
1483 }
1484}
1485
Mark Brownb22ead22009-06-07 12:51:26 +01001486/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001487static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001488 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001489{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001490 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001491 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001492 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001493 unsigned int value = 0;
1494 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001495
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001496 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1497 reg = w->reg;
1498 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001499
1500 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001501 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001502 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001503
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001504 mask |= w->mask << w->shift;
1505 if (w->power)
1506 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001507 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001508 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001509
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001510 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001511 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1512 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001513
Mark Brown68f89ad2010-11-03 23:51:49 -04001514 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001515 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1516 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001517 }
1518
Mark Brown81628102009-06-07 13:21:24 +01001519 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001520 /* Any widget will do, they should all be updating the
1521 * same register.
1522 */
Mark Brown29376bc2011-06-19 13:49:28 +01001523
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001524 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001525 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001526 value, mask, reg, card->pop_time);
1527 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001528 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001529 }
1530
1531 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001532 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1533 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001534 }
Mark Brown42aa3412009-03-01 19:21:10 +00001535}
1536
Mark Brownb22ead22009-06-07 12:51:26 +01001537/* Apply a DAPM power sequence.
1538 *
1539 * We walk over a pre-sorted list of widgets to apply power to. In
1540 * order to minimise the number of writes to the device required
1541 * multiple widgets will be updated in a single write where possible.
1542 * Currently anything that requires more than a single write is not
1543 * handled.
1544 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001545static void dapm_seq_run(struct snd_soc_card *card,
1546 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001547{
1548 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001549 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001550 LIST_HEAD(pending);
1551 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001552 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001553 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001554 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001555 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001556 int *sort;
1557
1558 if (power_up)
1559 sort = dapm_up_seq;
1560 else
1561 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001562
Mark Brownb22ead22009-06-07 12:51:26 +01001563 list_for_each_entry_safe(w, n, list, power_list) {
1564 ret = 0;
1565
1566 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001567 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001568 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001569 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001570 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001571
Mark Brown474b62d2011-01-18 16:14:44 +00001572 if (cur_dapm && cur_dapm->seq_notifier) {
1573 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1574 if (sort[i] == cur_sort)
1575 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001576 i,
1577 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001578 }
1579
Mark Browneb270e92013-10-09 13:52:52 +01001580 if (cur_dapm && w->dapm != cur_dapm)
1581 soc_dapm_async_complete(cur_dapm);
1582
Mark Brownb22ead22009-06-07 12:51:26 +01001583 INIT_LIST_HEAD(&pending);
1584 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001585 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001586 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001587 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001588 }
1589
Mark Brown163cac02009-06-07 10:12:52 +01001590 switch (w->id) {
1591 case snd_soc_dapm_pre:
1592 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001593 list_for_each_entry_safe_continue(w, n, list,
1594 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001595
Mark Brownb22ead22009-06-07 12:51:26 +01001596 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001597 ret = w->event(w,
1598 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001599 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001600 ret = w->event(w,
1601 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001602 break;
1603
1604 case snd_soc_dapm_post:
1605 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001606 list_for_each_entry_safe_continue(w, n, list,
1607 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001608
Mark Brownb22ead22009-06-07 12:51:26 +01001609 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001610 ret = w->event(w,
1611 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001612 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001613 ret = w->event(w,
1614 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001615 break;
1616
Mark Brown163cac02009-06-07 10:12:52 +01001617 default:
Mark Brown81628102009-06-07 13:21:24 +01001618 /* Queue it up for application */
1619 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001620 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001621 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001622 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001623 list_move(&w->power_list, &pending);
1624 break;
Mark Brown163cac02009-06-07 10:12:52 +01001625 }
Mark Brownb22ead22009-06-07 12:51:26 +01001626
1627 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001628 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001629 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001630 }
Mark Brownb22ead22009-06-07 12:51:26 +01001631
1632 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001633 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001634
1635 if (cur_dapm && cur_dapm->seq_notifier) {
1636 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1637 if (sort[i] == cur_sort)
1638 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001639 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001640 }
Mark Browneb270e92013-10-09 13:52:52 +01001641
1642 list_for_each_entry(d, &card->dapm_list, list) {
1643 soc_dapm_async_complete(d);
1644 }
Mark Brown163cac02009-06-07 10:12:52 +01001645}
1646
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001647static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001648{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001649 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001650 struct snd_soc_dapm_widget_list *wlist;
1651 struct snd_soc_dapm_widget *w = NULL;
1652 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001653 int ret;
1654
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001655 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001656 return;
1657
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001658 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001659
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001660 for (wi = 0; wi < wlist->num_widgets; wi++) {
1661 w = wlist->widgets[wi];
1662
1663 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1664 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1665 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001666 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001667 w->name, ret);
1668 }
Mark Brown97404f22010-12-14 16:13:57 +00001669 }
1670
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001671 if (!w)
1672 return;
1673
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001674 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1675 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001676 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001677 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001678 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001679
Chen-Yu Tsaie411b0b2016-11-02 15:35:58 +08001680 if (update->has_second_set) {
1681 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1682 update->mask2, update->val2);
1683 if (ret < 0)
1684 dev_err(w->dapm->dev,
1685 "ASoC: %s DAPM update failed: %d\n",
1686 w->name, ret);
1687 }
1688
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001689 for (wi = 0; wi < wlist->num_widgets; wi++) {
1690 w = wlist->widgets[wi];
1691
1692 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1693 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1694 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001695 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001696 w->name, ret);
1697 }
Mark Brown97404f22010-12-14 16:13:57 +00001698 }
1699}
1700
Mark Brown9d0624a2011-02-18 11:49:43 -08001701/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1702 * they're changing state.
1703 */
1704static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1705{
1706 struct snd_soc_dapm_context *d = data;
1707 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001708
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001709 /* If we're off and we're not supposed to go into STANDBY */
Mark Brown56fba412011-06-04 11:25:10 +01001710 if (d->bias_level == SND_SOC_BIAS_OFF &&
1711 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001712 if (d->dev)
1713 pm_runtime_get_sync(d->dev);
1714
Mark Brown9d0624a2011-02-18 11:49:43 -08001715 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1716 if (ret != 0)
1717 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001718 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001719 }
1720
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001721 /* Prepare for a transition to ON or away from ON */
1722 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1723 d->bias_level != SND_SOC_BIAS_ON) ||
1724 (d->target_bias_level != SND_SOC_BIAS_ON &&
1725 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001726 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1727 if (ret != 0)
1728 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001729 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001730 }
1731}
1732
1733/* Async callback run prior to DAPM sequences - brings to their final
1734 * state.
1735 */
1736static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1737{
1738 struct snd_soc_dapm_context *d = data;
1739 int ret;
1740
1741 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001742 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1743 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1744 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001745 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1746 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001747 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001748 ret);
1749 }
1750
1751 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001752 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1753 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001754 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1755 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001756 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1757 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001758
1759 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001760 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001761 }
1762
1763 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001764 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1765 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001766 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1767 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001768 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001769 ret);
1770 }
1771}
Mark Brown97404f22010-12-14 16:13:57 +00001772
Mark Brownfe4fda52011-10-03 22:36:57 +01001773static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1774 bool power, bool connect)
1775{
1776 /* If a connection is being made or broken then that update
1777 * will have marked the peer dirty, otherwise the widgets are
1778 * not connected and this update has no impact. */
1779 if (!connect)
1780 return;
1781
1782 /* If the peer is already in the state we're moving to then we
1783 * won't have an impact on it. */
1784 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001785 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001786}
1787
Mark Brown05623c42011-09-28 17:02:31 +01001788static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1789 struct list_head *up_list,
1790 struct list_head *down_list)
1791{
Mark Browndb432b42011-10-03 21:06:40 +01001792 struct snd_soc_dapm_path *path;
1793
Mark Brown05623c42011-09-28 17:02:31 +01001794 if (w->power == power)
1795 return;
1796
1797 trace_snd_soc_dapm_widget_power(w, power);
1798
Mark Browndb432b42011-10-03 21:06:40 +01001799 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001800 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001801 */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001802 snd_soc_dapm_widget_for_each_source_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001803 dapm_widget_set_peer_power(path->source, power, path->connect);
1804
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001805 /* Supplies can't affect their outputs, only their inputs */
1806 if (!w->is_supply) {
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001807 snd_soc_dapm_widget_for_each_sink_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001808 dapm_widget_set_peer_power(path->sink, power,
1809 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001810 }
1811
Mark Brown05623c42011-09-28 17:02:31 +01001812 if (power)
1813 dapm_seq_insert(w, up_list, true);
1814 else
1815 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001816}
1817
Mark Brown7c81beb2011-09-20 22:22:32 +01001818static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1819 struct list_head *up_list,
1820 struct list_head *down_list)
1821{
Mark Brown7c81beb2011-09-20 22:22:32 +01001822 int power;
1823
1824 switch (w->id) {
1825 case snd_soc_dapm_pre:
1826 dapm_seq_insert(w, down_list, false);
1827 break;
1828 case snd_soc_dapm_post:
1829 dapm_seq_insert(w, up_list, true);
1830 break;
1831
1832 default:
Mark Brownd805002b2011-09-28 18:28:23 +01001833 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001834
Mark Brown05623c42011-09-28 17:02:31 +01001835 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001836 break;
1837 }
1838}
1839
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001840static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1841{
1842 if (dapm->idle_bias_off)
1843 return true;
1844
1845 switch (snd_power_get_state(dapm->card->snd_card)) {
1846 case SNDRV_CTL_POWER_D3hot:
1847 case SNDRV_CTL_POWER_D3cold:
1848 return dapm->suspend_bias_off;
1849 default:
1850 break;
1851 }
1852
1853 return false;
1854}
1855
Mark Brown42aa3412009-03-01 19:21:10 +00001856/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001857 * Scan each dapm widget for complete audio path.
1858 * A complete path is a route that has valid endpoints i.e.:-
1859 *
1860 * o DAC to output pin.
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001861 * o Input pin to ADC.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001862 * o Input pin to Output pin (bypass, sidetone)
1863 * o DAC to ADC (loopback).
1864 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001865static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001866{
1867 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001868 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001869 LIST_HEAD(up_list);
1870 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001871 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001872 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001873
Mark Brownf9fa2b12014-03-06 16:49:11 +08001874 lockdep_assert_held(&card->dapm_mutex);
1875
Mark Brown84e90932010-11-04 00:07:02 -04001876 trace_snd_soc_dapm_start(card);
1877
Mark Brown56fba412011-06-04 11:25:10 +01001878 list_for_each_entry(d, &card->dapm_list, list) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001879 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001880 d->target_bias_level = SND_SOC_BIAS_OFF;
1881 else
1882 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001883 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001884
Liam Girdwood6c120e12012-02-15 15:15:34 +00001885 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001886
Mark Brown6d3ddc82009-05-16 17:47:29 +01001887 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001888 * lists indicating if they should be powered up or down. We
1889 * only check widgets that have been flagged as dirty but note
1890 * that new widgets may be added to the dirty list while we
1891 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001892 */
Mark Browndb432b42011-10-03 21:06:40 +01001893 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001894 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001895 }
1896
Mark Brownf9de6d72011-09-28 17:19:47 +01001897 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001898 switch (w->id) {
1899 case snd_soc_dapm_pre:
1900 case snd_soc_dapm_post:
1901 /* These widgets always need to be powered */
1902 break;
1903 default:
1904 list_del_init(&w->dirty);
1905 break;
1906 }
Mark Browndb432b42011-10-03 21:06:40 +01001907
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001908 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001909 d = w->dapm;
1910
1911 /* Supplies and micbiases only bring the
1912 * context up to STANDBY as unless something
1913 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001914 * generally don't require full power. Signal
1915 * generators are virtual pins and have no
1916 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001917 */
1918 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001919 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001920 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001921 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001922 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001923 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00001924 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001925 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001926 case snd_soc_dapm_micbias:
1927 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1928 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1929 break;
1930 default:
1931 d->target_bias_level = SND_SOC_BIAS_ON;
1932 break;
1933 }
1934 }
1935
1936 }
1937
Mark Brown85a843c2011-09-21 21:29:47 +01001938 /* Force all contexts in the card to the same bias state if
1939 * they're not ground referenced.
1940 */
Mark Brown56fba412011-06-04 11:25:10 +01001941 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001942 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001943 if (d->target_bias_level > bias)
1944 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001945 list_for_each_entry(d, &card->dapm_list, list)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001946 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01001947 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001948
Mark Brownde02d072011-09-20 21:43:24 +01001949 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001950
Xiang Xiao17282ba2014-03-02 00:04:03 +08001951 /* Run card bias changes at first */
1952 dapm_pre_sequence_async(&card->dapm, 0);
1953 /* Run other bias changes in parallel */
1954 list_for_each_entry(d, &card->dapm_list, list) {
Jon Huntere03546d2018-08-17 16:35:43 +01001955 if (d != &card->dapm && d->bias_level != d->target_bias_level)
Xiang Xiao17282ba2014-03-02 00:04:03 +08001956 async_schedule_domain(dapm_pre_sequence_async, d,
1957 &async_domain);
1958 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001959 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001960
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001961 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001962 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001963 }
1964
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001965 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001966 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001967 }
1968
Mark Brown6d3ddc82009-05-16 17:47:29 +01001969 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001970 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001971
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001972 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001973
Mark Brown6d3ddc82009-05-16 17:47:29 +01001974 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001975 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001976
Mark Brown9d0624a2011-02-18 11:49:43 -08001977 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001978 list_for_each_entry(d, &card->dapm_list, list) {
Jon Huntere03546d2018-08-17 16:35:43 +01001979 if (d != &card->dapm && d->bias_level != d->target_bias_level)
Xiang Xiao17282ba2014-03-02 00:04:03 +08001980 async_schedule_domain(dapm_post_sequence_async, d,
1981 &async_domain);
1982 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001983 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001984 /* Run card bias changes at last */
1985 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001986
Liam Girdwood8078d872012-02-15 15:15:35 +00001987 /* do we need to notify any clients that DAPM event is complete */
1988 list_for_each_entry(d, &card->dapm_list, list) {
1989 if (d->stream_event)
1990 d->stream_event(d, event);
1991 }
1992
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001993 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001994 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001995 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001996
Mark Brown84e90932010-11-04 00:07:02 -04001997 trace_snd_soc_dapm_done(card);
1998
Mark Brown42aa3412009-03-01 19:21:10 +00001999 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002000}
2001
Mark Brown79fb9382009-08-21 16:38:13 +01002002#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01002003static ssize_t dapm_widget_power_read_file(struct file *file,
2004 char __user *user_buf,
2005 size_t count, loff_t *ppos)
2006{
2007 struct snd_soc_dapm_widget *w = file->private_data;
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002008 struct snd_soc_card *card = w->dapm->card;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002009 enum snd_soc_dapm_direction dir, rdir;
Mark Brown79fb9382009-08-21 16:38:13 +01002010 char *buf;
2011 int in, out;
2012 ssize_t ret;
2013 struct snd_soc_dapm_path *p = NULL;
2014
2015 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2016 if (!buf)
2017 return -ENOMEM;
2018
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002019 mutex_lock(&card->dapm_mutex);
2020
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002021 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2022 if (w->is_supply) {
2023 in = 0;
2024 out = 0;
2025 } else {
Piotr Stankiewicz67420642016-05-13 17:03:55 +01002026 in = is_connected_input_ep(w, NULL, NULL);
2027 out = is_connected_output_ep(w, NULL, NULL);
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002028 }
Mark Brown79fb9382009-08-21 16:38:13 +01002029
Mark Brownf13ebad2012-03-03 18:01:01 +00002030 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2031 w->name, w->power ? "On" : "Off",
2032 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01002033
Mark Brownd033c362009-12-04 15:25:56 +00002034 if (w->reg >= 0)
2035 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002036 " - R%d(0x%x) mask 0x%x",
2037 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00002038
2039 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
2040
Mark Brown3eef08b2009-09-14 16:49:00 +01002041 if (w->sname)
2042 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2043 w->sname,
2044 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01002045
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002046 snd_soc_dapm_for_each_direction(dir) {
2047 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2048 snd_soc_dapm_widget_for_each_path(w, dir, p) {
KaiChieh Chuang28735af2018-02-05 13:00:00 +08002049 if (p->connected && !p->connected(p->source, p->sink))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002050 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002051
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002052 if (!p->connect)
2053 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002054
Mark Brown79fb9382009-08-21 16:38:13 +01002055 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002056 " %s \"%s\" \"%s\"\n",
2057 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
Mark Brown79fb9382009-08-21 16:38:13 +01002058 p->name ? p->name : "static",
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002059 p->node[rdir]->name);
2060 }
Mark Brown79fb9382009-08-21 16:38:13 +01002061 }
2062
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002063 mutex_unlock(&card->dapm_mutex);
2064
Mark Brown79fb9382009-08-21 16:38:13 +01002065 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2066
2067 kfree(buf);
2068 return ret;
2069}
2070
2071static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002072 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002073 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002074 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002075};
2076
Mark Brownef49e4f2011-04-04 20:48:13 +09002077static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2078 size_t count, loff_t *ppos)
2079{
2080 struct snd_soc_dapm_context *dapm = file->private_data;
2081 char *level;
2082
2083 switch (dapm->bias_level) {
2084 case SND_SOC_BIAS_ON:
2085 level = "On\n";
2086 break;
2087 case SND_SOC_BIAS_PREPARE:
2088 level = "Prepare\n";
2089 break;
2090 case SND_SOC_BIAS_STANDBY:
2091 level = "Standby\n";
2092 break;
2093 case SND_SOC_BIAS_OFF:
2094 level = "Off\n";
2095 break;
2096 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002097 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002098 level = "Unknown\n";
2099 break;
2100 }
2101
2102 return simple_read_from_buffer(user_buf, count, ppos, level,
2103 strlen(level));
2104}
2105
2106static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002107 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002108 .read = dapm_bias_read_file,
2109 .llseek = default_llseek,
2110};
2111
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002112void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2113 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002114{
Mark Brown79fb9382009-08-21 16:38:13 +01002115 struct dentry *d;
2116
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02002117 if (!parent)
2118 return;
2119
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002120 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2121
2122 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002123 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002124 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002125 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002126 }
Mark Brown79fb9382009-08-21 16:38:13 +01002127
Mark Brownef49e4f2011-04-04 20:48:13 +09002128 d = debugfs_create_file("bias_level", 0444,
2129 dapm->debugfs_dapm, dapm,
2130 &dapm_bias_fops);
2131 if (!d)
2132 dev_warn(dapm->dev,
2133 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002134}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002135
2136static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2137{
2138 struct snd_soc_dapm_context *dapm = w->dapm;
2139 struct dentry *d;
2140
2141 if (!dapm->debugfs_dapm || !w->name)
2142 return;
2143
2144 d = debugfs_create_file(w->name, 0444,
2145 dapm->debugfs_dapm, w,
2146 &dapm_widget_power_fops);
2147 if (!d)
2148 dev_warn(w->dapm->dev,
2149 "ASoC: Failed to create %s debugfs file\n",
2150 w->name);
2151}
2152
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002153static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2154{
2155 debugfs_remove_recursive(dapm->debugfs_dapm);
2156}
2157
Mark Brown79fb9382009-08-21 16:38:13 +01002158#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002159void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2160 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002161{
2162}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002163
2164static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2165{
2166}
2167
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002168static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2169{
2170}
2171
Mark Brown79fb9382009-08-21 16:38:13 +01002172#endif
2173
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002174/*
2175 * soc_dapm_connect_path() - Connects or disconnects a path
2176 * @path: The path to update
2177 * @connect: The new connect state of the path. True if the path is connected,
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002178 * false if it is disconnected.
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002179 * @reason: The reason why the path changed (for debugging only)
2180 */
2181static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2182 bool connect, const char *reason)
2183{
2184 if (path->connect == connect)
2185 return;
2186
2187 path->connect = connect;
2188 dapm_mark_dirty(path->source, reason);
2189 dapm_mark_dirty(path->sink, reason);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002190 dapm_path_invalidate(path);
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002191}
2192
Richard Purdie2b97eab2006-10-06 18:32:18 +02002193/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002194static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002195 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002196{
2197 struct snd_soc_dapm_path *path;
2198 int found = 0;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002199 bool connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002200
Mark Brownf9fa2b12014-03-06 16:49:11 +08002201 lockdep_assert_held(&card->dapm_mutex);
2202
Richard Purdie2b97eab2006-10-06 18:32:18 +02002203 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002204 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002205 found = 1;
2206 /* we now need to match the string in the enum to the path */
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002207 if (!(strcmp(path->name, e->texts[mux])))
2208 connect = true;
2209 else
2210 connect = false;
2211
2212 soc_dapm_connect_path(path, connect, "mux update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002213 }
2214
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002215 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002216 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002217
Liam Girdwood618dae12012-04-25 12:12:51 +01002218 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002219}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002220
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002221int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002222 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2223 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002224{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002225 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002226 int ret;
2227
Liam Girdwood3cd04342012-03-09 12:02:08 +00002228 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002229 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002230 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002231 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002232 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002233 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002234 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002235 return ret;
2236}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002237EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002238
Milan plzik1b075e32008-01-10 14:39:46 +01002239/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002240static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002241 struct snd_kcontrol *kcontrol,
2242 int connect, int rconnect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002243{
2244 struct snd_soc_dapm_path *path;
2245 int found = 0;
2246
Mark Brownf9fa2b12014-03-06 16:49:11 +08002247 lockdep_assert_held(&card->dapm_mutex);
2248
Richard Purdie2b97eab2006-10-06 18:32:18 +02002249 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002250 dapm_kcontrol_for_each_path(path, kcontrol) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002251 /*
2252 * Ideally this function should support any number of
2253 * paths and channels. But since kcontrols only come
2254 * in mono and stereo variants, we are limited to 2
2255 * channels.
2256 *
2257 * The following code assumes for stereo controls the
2258 * first path (when 'found == 0') is the left channel,
2259 * and all remaining paths (when 'found == 1') are the
2260 * right channel.
2261 *
2262 * A stereo control is signified by a valid 'rconnect'
2263 * value, either 0 for unconnected, or >= 0 for connected.
2264 * This is chosen instead of using snd_soc_volsw_is_stereo,
2265 * so that the behavior of snd_soc_dapm_mixer_update_power
2266 * doesn't change even when the kcontrol passed in is
2267 * stereo.
2268 *
2269 * It passes 'connect' as the path connect status for
2270 * the left channel, and 'rconnect' for the right
2271 * channel.
2272 */
2273 if (found && rconnect >= 0)
2274 soc_dapm_connect_path(path, rconnect, "mixer update");
2275 else
2276 soc_dapm_connect_path(path, connect, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002277 found = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002278 }
2279
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002280 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002281 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002282
Liam Girdwood618dae12012-04-25 12:12:51 +01002283 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002284}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002285
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002286int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002287 struct snd_kcontrol *kcontrol, int connect,
2288 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002289{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002290 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002291 int ret;
2292
Liam Girdwood3cd04342012-03-09 12:02:08 +00002293 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002294 card->update = update;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002295 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002296 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002297 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002298 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002299 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002300 return ret;
2301}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002302EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002303
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002304static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2305 char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002306{
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002307 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002308 struct snd_soc_dapm_widget *w;
2309 int count = 0;
2310 char *state = "not set";
2311
Mark Brown47325072016-03-18 12:04:23 +00002312 /* card won't be set for the dummy component, as a spot fix
2313 * we're checking for that case specifically here but in future
2314 * we will ensure that the dummy component looks like others.
2315 */
2316 if (!cmpnt->card)
2317 return 0;
2318
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002319 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2320 if (w->dapm != dapm)
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002321 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002322
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002323 /* only display widgets that burn power */
Richard Purdie2b97eab2006-10-06 18:32:18 +02002324 switch (w->id) {
2325 case snd_soc_dapm_hp:
2326 case snd_soc_dapm_mic:
2327 case snd_soc_dapm_spk:
2328 case snd_soc_dapm_line:
2329 case snd_soc_dapm_micbias:
2330 case snd_soc_dapm_dac:
2331 case snd_soc_dapm_adc:
2332 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002333 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002334 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002335 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002336 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002337 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00002338 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002339 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002340 if (w->name)
2341 count += sprintf(buf + count, "%s: %s\n",
2342 w->name, w->power ? "On":"Off");
2343 break;
2344 default:
2345 break;
2346 }
2347 }
2348
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002349 switch (snd_soc_dapm_get_bias_level(dapm)) {
Mark Brown0be98982008-05-19 12:31:28 +02002350 case SND_SOC_BIAS_ON:
2351 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002352 break;
Mark Brown0be98982008-05-19 12:31:28 +02002353 case SND_SOC_BIAS_PREPARE:
2354 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002355 break;
Mark Brown0be98982008-05-19 12:31:28 +02002356 case SND_SOC_BIAS_STANDBY:
2357 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002358 break;
Mark Brown0be98982008-05-19 12:31:28 +02002359 case SND_SOC_BIAS_OFF:
2360 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002361 break;
2362 }
2363 count += sprintf(buf + count, "PM State: %s\n", state);
2364
2365 return count;
2366}
2367
Benoit Cousson44ba2642014-07-08 23:19:36 +02002368/* show dapm widget status in sys fs */
2369static ssize_t dapm_widget_show(struct device *dev,
2370 struct device_attribute *attr, char *buf)
2371{
2372 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002373 struct snd_soc_dai *codec_dai;
Benoit Cousson44ba2642014-07-08 23:19:36 +02002374 int i, count = 0;
2375
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002376 mutex_lock(&rtd->card->dapm_mutex);
2377
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002378 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2379 struct snd_soc_component *cmpnt = codec_dai->component;
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002380
2381 count += dapm_widget_show_component(cmpnt, buf + count);
Benoit Cousson44ba2642014-07-08 23:19:36 +02002382 }
2383
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002384 mutex_unlock(&rtd->card->dapm_mutex);
2385
Benoit Cousson44ba2642014-07-08 23:19:36 +02002386 return count;
2387}
2388
Joe Perchesc828a892017-12-19 10:15:08 -08002389static DEVICE_ATTR_RO(dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002390
Takashi Iwaid29697d2015-01-30 20:16:37 +01002391struct attribute *soc_dapm_dev_attrs[] = {
2392 &dev_attr_dapm_widget.attr,
2393 NULL
2394};
Richard Purdie2b97eab2006-10-06 18:32:18 +02002395
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002396static void dapm_free_path(struct snd_soc_dapm_path *path)
2397{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002398 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2399 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002400 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002401 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002402 kfree(path);
2403}
2404
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002405void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2406{
2407 struct snd_soc_dapm_path *p, *next_p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002408 enum snd_soc_dapm_direction dir;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002409
2410 list_del(&w->list);
2411 /*
2412 * remove source and sink paths associated to this widget.
2413 * While removing the path, remove reference to it from both
2414 * source and sink widgets so that path is removed only once.
2415 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002416 snd_soc_dapm_for_each_direction(dir) {
2417 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2418 dapm_free_path(p);
2419 }
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002420
2421 kfree(w->kcontrols);
Lars-Peter Clausen48068962015-07-21 18:11:08 +02002422 kfree_const(w->name);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002423 kfree(w);
2424}
2425
Jyri Sarhafd589a12015-11-10 18:12:42 +02002426void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2427{
2428 dapm->path_sink_cache.widget = NULL;
2429 dapm->path_source_cache.widget = NULL;
2430}
2431
Richard Purdie2b97eab2006-10-06 18:32:18 +02002432/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002433static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002434{
2435 struct snd_soc_dapm_widget *w, *next_w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002436
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002437 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2438 if (w->dapm != dapm)
2439 continue;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002440 snd_soc_dapm_free_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002441 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002442 snd_soc_dapm_reset_cache(dapm);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002443}
2444
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002445static struct snd_soc_dapm_widget *dapm_find_widget(
2446 struct snd_soc_dapm_context *dapm, const char *pin,
2447 bool search_other_contexts)
2448{
2449 struct snd_soc_dapm_widget *w;
2450 struct snd_soc_dapm_widget *fallback = NULL;
2451
2452 list_for_each_entry(w, &dapm->card->widgets, list) {
2453 if (!strcmp(w->name, pin)) {
2454 if (w->dapm == dapm)
2455 return w;
2456 else
2457 fallback = w;
2458 }
2459 }
2460
2461 if (search_other_contexts)
2462 return fallback;
2463
2464 return NULL;
2465}
2466
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002467static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002468 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002469{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002470 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002471
Mark Brownf9fa2b12014-03-06 16:49:11 +08002472 dapm_assert_locked(dapm);
2473
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002474 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002475 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002476 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002477 }
2478
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002479 if (w->connected != status) {
Mark Brown1a8b2d92012-02-16 11:50:07 -08002480 dapm_mark_dirty(w, "pin configuration");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002481 dapm_widget_invalidate_input_paths(w);
2482 dapm_widget_invalidate_output_paths(w);
2483 }
Mark Brown1a8b2d92012-02-16 11:50:07 -08002484
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002485 w->connected = status;
2486 if (status == 0)
2487 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002488
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002489 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002490}
2491
Richard Purdie2b97eab2006-10-06 18:32:18 +02002492/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002493 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2494 * @dapm: DAPM context
2495 *
2496 * Walks all dapm audio paths and powers widgets according to their
2497 * stream or path usage.
2498 *
2499 * Requires external locking.
2500 *
2501 * Returns 0 for success.
2502 */
2503int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2504{
2505 /*
2506 * Suppress early reports (eg, jacks syncing their state) to avoid
2507 * silly DAPM runs during card startup.
2508 */
2509 if (!dapm->card || !dapm->card->instantiated)
2510 return 0;
2511
2512 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2513}
2514EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2515
2516/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002517 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002518 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002519 *
2520 * Walks all dapm audio paths and powers widgets according to their
2521 * stream or path usage.
2522 *
2523 * Returns 0 for success.
2524 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002525int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002526{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002527 int ret;
2528
Liam Girdwood3cd04342012-03-09 12:02:08 +00002529 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002530 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002531 mutex_unlock(&dapm->card->dapm_mutex);
2532 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002533}
Liam Girdwooda5302182008-07-07 13:35:17 +01002534EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002535
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002536/*
2537 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2538 * @w: The widget for which to update the flags
2539 *
2540 * Some widgets have a dynamic category which depends on which neighbors they
2541 * are connected to. This function update the category for these widgets.
2542 *
2543 * This function must be called whenever a path is added or removed to a widget.
2544 */
2545static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2546{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002547 enum snd_soc_dapm_direction dir;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002548 struct snd_soc_dapm_path *p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002549 unsigned int ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002550
2551 switch (w->id) {
2552 case snd_soc_dapm_input:
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002553 /* On a fully routed card an input is never a source */
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002554 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002555 return;
2556 ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002557 snd_soc_dapm_widget_for_each_source_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002558 if (p->source->id == snd_soc_dapm_micbias ||
2559 p->source->id == snd_soc_dapm_mic ||
2560 p->source->id == snd_soc_dapm_line ||
2561 p->source->id == snd_soc_dapm_output) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002562 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002563 break;
2564 }
2565 }
2566 break;
2567 case snd_soc_dapm_output:
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002568 /* On a fully routed card a output is never a sink */
2569 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002570 return;
2571 ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002572 snd_soc_dapm_widget_for_each_sink_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002573 if (p->sink->id == snd_soc_dapm_spk ||
2574 p->sink->id == snd_soc_dapm_hp ||
2575 p->sink->id == snd_soc_dapm_line ||
2576 p->sink->id == snd_soc_dapm_input) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002577 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002578 break;
2579 }
2580 }
2581 break;
2582 case snd_soc_dapm_line:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002583 ep = 0;
2584 snd_soc_dapm_for_each_direction(dir) {
2585 if (!list_empty(&w->edges[dir]))
2586 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2587 }
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002588 break;
2589 default:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002590 return;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002591 }
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002592
2593 w->is_ep = ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002594}
2595
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002596static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2597 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2598 const char *control)
2599{
2600 bool dynamic_source = false;
2601 bool dynamic_sink = false;
2602
2603 if (!control)
2604 return 0;
2605
2606 switch (source->id) {
2607 case snd_soc_dapm_demux:
2608 dynamic_source = true;
2609 break;
2610 default:
2611 break;
2612 }
2613
2614 switch (sink->id) {
2615 case snd_soc_dapm_mux:
2616 case snd_soc_dapm_switch:
2617 case snd_soc_dapm_mixer:
2618 case snd_soc_dapm_mixer_named_ctl:
2619 dynamic_sink = true;
2620 break;
2621 default:
2622 break;
2623 }
2624
2625 if (dynamic_source && dynamic_sink) {
2626 dev_err(dapm->dev,
2627 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2628 source->name, control, sink->name);
2629 return -EINVAL;
2630 } else if (!dynamic_source && !dynamic_sink) {
2631 dev_err(dapm->dev,
2632 "Control not supported for path %s -> [%s] -> %s\n",
2633 source->name, control, sink->name);
2634 return -EINVAL;
2635 }
2636
2637 return 0;
2638}
2639
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002640static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2641 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2642 const char *control,
2643 int (*connected)(struct snd_soc_dapm_widget *source,
2644 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002645{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002646 struct snd_soc_dapm_widget *widgets[2];
2647 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002648 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002649 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002650
Lars-Peter Clausene409dfb2014-10-25 17:42:02 +02002651 if (wsink->is_supply && !wsource->is_supply) {
2652 dev_err(dapm->dev,
2653 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2654 wsource->name, wsink->name);
2655 return -EINVAL;
2656 }
2657
2658 if (connected && !wsource->is_supply) {
2659 dev_err(dapm->dev,
2660 "connected() callback only supported for supply widgets (%s -> %s)\n",
2661 wsource->name, wsink->name);
2662 return -EINVAL;
2663 }
2664
2665 if (wsource->is_supply && control) {
2666 dev_err(dapm->dev,
2667 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2668 wsource->name, control, wsink->name);
2669 return -EINVAL;
2670 }
2671
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002672 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2673 if (ret)
2674 return ret;
2675
Richard Purdie2b97eab2006-10-06 18:32:18 +02002676 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2677 if (!path)
2678 return -ENOMEM;
2679
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002680 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2681 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2682 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2683 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2684
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002685 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002686 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002687 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002688
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002689 if (wsource->is_supply || wsink->is_supply)
2690 path->is_supply = 1;
2691
Richard Purdie2b97eab2006-10-06 18:32:18 +02002692 /* connect static paths */
2693 if (control == NULL) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002694 path->connect = 1;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002695 } else {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002696 switch (wsource->id) {
2697 case snd_soc_dapm_demux:
2698 ret = dapm_connect_mux(dapm, path, control, wsource);
2699 if (ret)
2700 goto err;
2701 break;
2702 default:
2703 break;
2704 }
2705
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002706 switch (wsink->id) {
2707 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002708 ret = dapm_connect_mux(dapm, path, control, wsink);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002709 if (ret != 0)
2710 goto err;
2711 break;
2712 case snd_soc_dapm_switch:
2713 case snd_soc_dapm_mixer:
2714 case snd_soc_dapm_mixer_named_ctl:
2715 ret = dapm_connect_mixer(dapm, path, control);
2716 if (ret != 0)
2717 goto err;
2718 break;
2719 default:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002720 break;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002721 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002722 }
2723
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002724 list_add(&path->list, &dapm->card->paths);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002725 snd_soc_dapm_for_each_direction(dir)
2726 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002727
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002728 snd_soc_dapm_for_each_direction(dir) {
2729 dapm_update_widget_flags(widgets[dir]);
2730 dapm_mark_dirty(widgets[dir], "Route added");
2731 }
Mark Brownfabd0382012-07-05 17:20:06 +01002732
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002733 if (dapm->card->instantiated && path->connect)
2734 dapm_path_invalidate(path);
2735
Richard Purdie2b97eab2006-10-06 18:32:18 +02002736 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002737err:
2738 kfree(path);
2739 return ret;
2740}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002741
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002742static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002743 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002744{
2745 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2746 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2747 const char *sink;
2748 const char *source;
2749 char prefixed_sink[80];
2750 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002751 const char *prefix;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002752 int ret;
2753
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002754 prefix = soc_dapm_prefix(dapm);
2755 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002756 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002757 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002758 sink = prefixed_sink;
2759 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002760 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002761 source = prefixed_source;
2762 } else {
2763 sink = route->sink;
2764 source = route->source;
2765 }
2766
Charles Keepax45a110a2015-05-11 13:50:30 +01002767 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2768 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2769
2770 if (wsink && wsource)
2771 goto skip_search;
2772
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002773 /*
2774 * find src and dest widgets over all widgets but favor a widget from
2775 * current DAPM context
2776 */
2777 list_for_each_entry(w, &dapm->card->widgets, list) {
2778 if (!wsink && !(strcmp(w->name, sink))) {
2779 wtsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002780 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002781 wsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002782 if (wsource)
2783 break;
2784 }
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002785 continue;
2786 }
2787 if (!wsource && !(strcmp(w->name, source))) {
2788 wtsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002789 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002790 wsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002791 if (wsink)
2792 break;
2793 }
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002794 }
2795 }
2796 /* use widget from another DAPM context if not found from this */
2797 if (!wsink)
2798 wsink = wtsink;
2799 if (!wsource)
2800 wsource = wtsource;
2801
2802 if (wsource == NULL) {
2803 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2804 route->source);
2805 return -ENODEV;
2806 }
2807 if (wsink == NULL) {
2808 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2809 route->sink);
2810 return -ENODEV;
2811 }
2812
Charles Keepax45a110a2015-05-11 13:50:30 +01002813skip_search:
2814 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2815 dapm_wcache_update(&dapm->path_source_cache, wsource);
2816
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002817 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2818 route->connected);
2819 if (ret)
2820 goto err;
2821
2822 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002823err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002824 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002825 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002826 return ret;
2827}
Mark Brown105f1c22008-05-13 14:52:19 +02002828
Mark Brownefcc3c62012-07-05 17:24:19 +01002829static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2830 const struct snd_soc_dapm_route *route)
2831{
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002832 struct snd_soc_dapm_widget *wsource, *wsink;
Mark Brownefcc3c62012-07-05 17:24:19 +01002833 struct snd_soc_dapm_path *path, *p;
2834 const char *sink;
2835 const char *source;
2836 char prefixed_sink[80];
2837 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002838 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002839
2840 if (route->control) {
2841 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002842 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002843 return -EINVAL;
2844 }
2845
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002846 prefix = soc_dapm_prefix(dapm);
2847 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002848 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002849 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002850 sink = prefixed_sink;
2851 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002852 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002853 source = prefixed_source;
2854 } else {
2855 sink = route->sink;
2856 source = route->source;
2857 }
2858
2859 path = NULL;
2860 list_for_each_entry(p, &dapm->card->paths, list) {
2861 if (strcmp(p->source->name, source) != 0)
2862 continue;
2863 if (strcmp(p->sink->name, sink) != 0)
2864 continue;
2865 path = p;
2866 break;
2867 }
2868
2869 if (path) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002870 wsource = path->source;
2871 wsink = path->sink;
2872
2873 dapm_mark_dirty(wsource, "Route removed");
2874 dapm_mark_dirty(wsink, "Route removed");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002875 if (path->connect)
2876 dapm_path_invalidate(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002877
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002878 dapm_free_path(path);
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002879
2880 /* Update any path related flags */
2881 dapm_update_widget_flags(wsource);
2882 dapm_update_widget_flags(wsink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002883 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002884 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002885 source, sink);
2886 }
2887
2888 return 0;
2889}
2890
Mark Brown105f1c22008-05-13 14:52:19 +02002891/**
Mark Brown105f1c22008-05-13 14:52:19 +02002892 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002893 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002894 * @route: audio routes
2895 * @num: number of routes
2896 *
2897 * Connects 2 dapm widgets together via a named audio path. The sink is
2898 * the widget receiving the audio signal, whilst the source is the sender
2899 * of the audio signal.
2900 *
2901 * Returns 0 for success else error. On error all resources can be freed
2902 * with a call to snd_soc_card_free().
2903 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002904int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002905 const struct snd_soc_dapm_route *route, int num)
2906{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002907 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002908
Stuart Hendersonf19c1812017-09-21 11:02:12 +01002909 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown105f1c22008-05-13 14:52:19 +02002910 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002911 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002912 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002913 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2914 route->source,
2915 route->control ? route->control : "direct",
2916 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002917 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002918 }
2919 route++;
2920 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002921 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002922
Dan Carpenter60884c22012-04-13 22:25:43 +03002923 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002924}
2925EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2926
Mark Brownefcc3c62012-07-05 17:24:19 +01002927/**
2928 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2929 * @dapm: DAPM context
2930 * @route: audio routes
2931 * @num: number of routes
2932 *
2933 * Removes routes from the DAPM context.
2934 */
2935int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2936 const struct snd_soc_dapm_route *route, int num)
2937{
Rajan Vajae066ea22016-02-11 11:23:35 +05302938 int i;
Mark Brownefcc3c62012-07-05 17:24:19 +01002939
Stuart Hendersonf19c1812017-09-21 11:02:12 +01002940 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownefcc3c62012-07-05 17:24:19 +01002941 for (i = 0; i < num; i++) {
2942 snd_soc_dapm_del_route(dapm, route);
2943 route++;
2944 }
2945 mutex_unlock(&dapm->card->dapm_mutex);
2946
Rajan Vajae066ea22016-02-11 11:23:35 +05302947 return 0;
Mark Brownefcc3c62012-07-05 17:24:19 +01002948}
2949EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2950
Mark Brownbf3a9e12011-06-13 16:42:29 +01002951static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2952 const struct snd_soc_dapm_route *route)
2953{
2954 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2955 route->source,
2956 true);
2957 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2958 route->sink,
2959 true);
2960 struct snd_soc_dapm_path *path;
2961 int count = 0;
2962
2963 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002964 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002965 route->source);
2966 return -ENODEV;
2967 }
2968
2969 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002970 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002971 route->sink);
2972 return -ENODEV;
2973 }
2974
2975 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002976 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002977 route->source, route->sink);
2978
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002979 snd_soc_dapm_widget_for_each_sink_path(source, path) {
Mark Brownbf3a9e12011-06-13 16:42:29 +01002980 if (path->sink == sink) {
2981 path->weak = 1;
2982 count++;
2983 }
2984 }
2985
2986 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002987 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002988 route->source, route->sink);
2989 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002990 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002991 count, route->source, route->sink);
2992
2993 return 0;
2994}
2995
2996/**
2997 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2998 * @dapm: DAPM context
2999 * @route: audio routes
3000 * @num: number of routes
3001 *
3002 * Mark existing routes matching those specified in the passed array
3003 * as being weak, meaning that they are ignored for the purpose of
3004 * power decisions. The main intended use case is for sidetone paths
3005 * which couple audio between other independent paths if they are both
3006 * active in order to make the combination work better at the user
3007 * level but which aren't intended to be "used".
3008 *
3009 * Note that CODEC drivers should not use this as sidetone type paths
3010 * can frequently also be used as bypass paths.
3011 */
3012int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3013 const struct snd_soc_dapm_route *route, int num)
3014{
3015 int i, err;
3016 int ret = 0;
3017
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003018 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01003019 for (i = 0; i < num; i++) {
3020 err = snd_soc_dapm_weak_route(dapm, route);
3021 if (err)
3022 ret = err;
3023 route++;
3024 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003025 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01003026
3027 return ret;
3028}
3029EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3030
Mark Brown105f1c22008-05-13 14:52:19 +02003031/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003032 * snd_soc_dapm_new_widgets - add new dapm widgets
Jonathan Corbet628536e2015-08-25 01:14:48 -06003033 * @card: card to be checked for new dapm widgets
Richard Purdie2b97eab2006-10-06 18:32:18 +02003034 *
3035 * Checks the codec for any new dapm widgets and creates them if found.
3036 *
3037 * Returns 0 for success.
3038 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02003039int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003040{
3041 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00003042 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003043
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003044 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003045
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003046 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003047 {
3048 if (w->new)
3049 continue;
3050
Stephen Warrenfad59882011-04-28 17:37:59 -06003051 if (w->num_kcontrols) {
Kees Cook6396bb22018-06-12 14:03:40 -07003052 w->kcontrols = kcalloc(w->num_kcontrols,
Stephen Warrenfad59882011-04-28 17:37:59 -06003053 sizeof(struct snd_kcontrol *),
3054 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003055 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003056 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06003057 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003058 }
Stephen Warrenfad59882011-04-28 17:37:59 -06003059 }
3060
Richard Purdie2b97eab2006-10-06 18:32:18 +02003061 switch(w->id) {
3062 case snd_soc_dapm_switch:
3063 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00003064 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003065 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003066 break;
3067 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003068 case snd_soc_dapm_demux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003069 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003070 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003071 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06003072 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003073 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003074 break;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003075 case snd_soc_dapm_dai_link:
3076 dapm_new_dai_link(w);
3077 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01003078 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003079 break;
3080 }
Mark Brownb66a70d2011-02-09 18:04:11 +00003081
3082 /* Read the initial power state from the device */
3083 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003084 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08003085 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003086 val &= w->mask;
3087 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00003088 w->power = 1;
3089 }
3090
Richard Purdie2b97eab2006-10-06 18:32:18 +02003091 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003092
Mark Brown7508b122011-10-05 12:09:12 +01003093 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003094 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003095 }
3096
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003097 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3098 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003099 return 0;
3100}
3101EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3102
3103/**
3104 * snd_soc_dapm_get_volsw - dapm mixer get callback
3105 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003106 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003107 *
3108 * Callback to get the value of a dapm mixer control.
3109 *
3110 * Returns 0 for success.
3111 */
3112int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3113 struct snd_ctl_elem_value *ucontrol)
3114{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003115 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3116 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003117 struct soc_mixer_control *mc =
3118 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003119 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003120 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003121 int max = mc->max;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003122 unsigned int width = fls(max);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003123 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003124 unsigned int invert = mc->invert;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003125 unsigned int reg_val, val, rval = 0;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003126 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003127
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003128 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003129 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003130 ret = soc_dapm_read(dapm, reg, &reg_val);
3131 val = (reg_val >> shift) & mask;
3132
3133 if (ret == 0 && reg != mc->rreg)
3134 ret = soc_dapm_read(dapm, mc->rreg, &reg_val);
3135
3136 if (snd_soc_volsw_is_stereo(mc))
3137 rval = (reg_val >> mc->rshift) & mask;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003138 } else {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003139 reg_val = dapm_kcontrol_get_value(kcontrol);
3140 val = reg_val & mask;
3141
3142 if (snd_soc_volsw_is_stereo(mc))
3143 rval = (reg_val >> width) & mask;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003144 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003145 mutex_unlock(&card->dapm_mutex);
3146
Chen-Yu Tsai01ad5e72016-08-27 19:27:58 +08003147 if (ret)
3148 return ret;
3149
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003150 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003151 ucontrol->value.integer.value[0] = max - val;
3152 else
3153 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003154
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003155 if (snd_soc_volsw_is_stereo(mc)) {
3156 if (invert)
3157 ucontrol->value.integer.value[1] = max - rval;
3158 else
3159 ucontrol->value.integer.value[1] = rval;
3160 }
3161
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003162 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003163}
3164EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3165
3166/**
3167 * snd_soc_dapm_put_volsw - dapm mixer set callback
3168 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003169 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003170 *
3171 * Callback to set the value of a dapm mixer control.
3172 *
3173 * Returns 0 for success.
3174 */
3175int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3176 struct snd_ctl_elem_value *ucontrol)
3177{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003178 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3179 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003180 struct soc_mixer_control *mc =
3181 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003182 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003183 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003184 int max = mc->max;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003185 unsigned int width = fls(max);
3186 unsigned int mask = (1 << width) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003187 unsigned int invert = mc->invert;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003188 unsigned int val, rval = 0;
3189 int connect, rconnect = -1, change, reg_change = 0;
Fabio Estevam33d92452018-02-14 13:39:05 -02003190 struct snd_soc_dapm_update update = {};
Nenghua Cao52765972013-12-13 20:13:49 +08003191 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003192
3193 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02003194 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003195
3196 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003197 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003198
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003199 if (snd_soc_volsw_is_stereo(mc)) {
3200 rval = (ucontrol->value.integer.value[1] & mask);
3201 rconnect = !!rval;
3202 if (invert)
3203 rval = max - rval;
3204 }
3205
Liam Girdwood3cd04342012-03-09 12:02:08 +00003206 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003207
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003208 /* This assumes field width < (bits in unsigned int / 2) */
3209 if (width > sizeof(unsigned int) * 8 / 2)
3210 dev_warn(dapm->dev,
3211 "ASoC: control %s field width limit exceeded\n",
3212 kcontrol->id.name);
3213 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
Mark Brown283375c2009-12-07 18:09:03 +00003214
Jarkko Nikula18626c72014-06-09 14:20:29 +03003215 if (reg != SND_SOC_NOPM) {
Jarkko Nikula18626c72014-06-09 14:20:29 +03003216 val = val << shift;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003217 rval = rval << mc->rshift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02003218
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003219 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3220
3221 if (snd_soc_volsw_is_stereo(mc))
3222 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3223 mask << mc->rshift,
3224 rval);
Jarkko Nikula18626c72014-06-09 14:20:29 +03003225 }
3226
3227 if (change || reg_change) {
3228 if (reg_change) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003229 if (snd_soc_volsw_is_stereo(mc)) {
3230 update.has_second_set = true;
3231 update.reg2 = mc->rreg;
3232 update.mask2 = mask << mc->rshift;
3233 update.val2 = rval;
3234 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003235 update.kcontrol = kcontrol;
3236 update.reg = reg;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003237 update.mask = mask << shift;
Jarkko Nikula18626c72014-06-09 14:20:29 +03003238 update.val = val;
3239 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003240 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003241 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00003242
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003243 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3244 rconnect);
Mark Brown97404f22010-12-14 16:13:57 +00003245
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003246 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00003247 }
3248
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003249 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003250
3251 if (ret > 0)
3252 soc_dpcm_runtime_update(card);
3253
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003254 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003255}
3256EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3257
3258/**
3259 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3260 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003261 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003262 *
3263 * Callback to get the value of a dapm enumerated double mixer control.
3264 *
3265 * Returns 0 for success.
3266 */
3267int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3268 struct snd_ctl_elem_value *ucontrol)
3269{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003270 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Charles Keepax561ed682015-05-01 12:37:26 +01003271 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003272 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003273 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003274
Charles Keepax561ed682015-05-01 12:37:26 +01003275 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3276 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003277 int ret = soc_dapm_read(dapm, e->reg, &reg_val);
Charles Keepax964a0b82015-05-08 10:50:10 +01003278 if (ret) {
3279 mutex_unlock(&card->dapm_mutex);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003280 return ret;
Charles Keepax964a0b82015-05-08 10:50:10 +01003281 }
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003282 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003283 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003284 }
Charles Keepax561ed682015-05-01 12:37:26 +01003285 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003286
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003287 val = (reg_val >> e->shift_l) & e->mask;
3288 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3289 if (e->shift_l != e->shift_r) {
3290 val = (reg_val >> e->shift_r) & e->mask;
3291 val = snd_soc_enum_val_to_item(e, val);
3292 ucontrol->value.enumerated.item[1] = val;
3293 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003294
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003295 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003296}
3297EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3298
3299/**
3300 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3301 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003302 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003303 *
3304 * Callback to set the value of a dapm enumerated double mixer control.
3305 *
3306 * Returns 0 for success.
3307 */
3308int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3309 struct snd_ctl_elem_value *ucontrol)
3310{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003311 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3312 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003313 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003314 unsigned int *item = ucontrol->value.enumerated.item;
Charles Keepax561ed682015-05-01 12:37:26 +01003315 unsigned int val, change, reg_change = 0;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003316 unsigned int mask;
Fabio Estevam33d92452018-02-14 13:39:05 -02003317 struct snd_soc_dapm_update update = {};
Nenghua Cao52765972013-12-13 20:13:49 +08003318 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003319
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003320 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003321 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003322
3323 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003324 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003325 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003326 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003327 return -EINVAL;
Chen-Yu Tsai071133a2016-08-27 19:27:59 +08003328 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003329 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003330 }
3331
Liam Girdwood3cd04342012-03-09 12:02:08 +00003332 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003333
Charles Keepax561ed682015-05-01 12:37:26 +01003334 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00003335
Charles Keepax561ed682015-05-01 12:37:26 +01003336 if (e->reg != SND_SOC_NOPM)
3337 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3338
3339 if (change || reg_change) {
3340 if (reg_change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003341 update.kcontrol = kcontrol;
3342 update.reg = e->reg;
3343 update.mask = mask;
3344 update.val = val;
3345 card->update = &update;
3346 }
Charles Keepax561ed682015-05-01 12:37:26 +01003347 change |= reg_change;
Mark Brown3a655772009-10-05 17:23:30 +01003348
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003349 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003350
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003351 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003352 }
3353
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003354 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003355
3356 if (ret > 0)
3357 soc_dpcm_runtime_update(card);
3358
Mark Brown97404f22010-12-14 16:13:57 +00003359 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003360}
3361EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3362
3363/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003364 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3365 *
3366 * @kcontrol: mixer control
3367 * @uinfo: control element information
3368 *
3369 * Callback to provide information about a pin switch control.
3370 */
3371int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3372 struct snd_ctl_elem_info *uinfo)
3373{
3374 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3375 uinfo->count = 1;
3376 uinfo->value.integer.min = 0;
3377 uinfo->value.integer.max = 1;
3378
3379 return 0;
3380}
3381EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3382
3383/**
3384 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3385 *
3386 * @kcontrol: mixer control
3387 * @ucontrol: Value
3388 */
3389int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3390 struct snd_ctl_elem_value *ucontrol)
3391{
Mark Brown48a8c392012-02-14 17:11:15 -08003392 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003393 const char *pin = (const char *)kcontrol->private_value;
3394
Liam Girdwood3cd04342012-03-09 12:02:08 +00003395 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003396
3397 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003398 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003399
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003400 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003401
3402 return 0;
3403}
3404EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3405
3406/**
3407 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3408 *
3409 * @kcontrol: mixer control
3410 * @ucontrol: Value
3411 */
3412int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3413 struct snd_ctl_elem_value *ucontrol)
3414{
Mark Brown48a8c392012-02-14 17:11:15 -08003415 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003416 const char *pin = (const char *)kcontrol->private_value;
3417
Mark Brown8b37dbd2009-02-28 21:14:20 +00003418 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003419 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003420 else
Mark Brown48a8c392012-02-14 17:11:15 -08003421 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003422
Mark Brown48a8c392012-02-14 17:11:15 -08003423 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003424 return 0;
3425}
3426EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3427
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +01003428struct snd_soc_dapm_widget *
Mark Brown5ba06fc2012-02-16 11:07:13 -08003429snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003430 const struct snd_soc_dapm_widget *widget)
3431{
3432 struct snd_soc_dapm_widget *w;
3433
3434 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3435 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
Linus Walleij37e1df82017-01-13 10:23:52 +01003436 /* Do not nag about probe deferrals */
3437 if (IS_ERR(w)) {
3438 int ret = PTR_ERR(w);
3439
3440 if (ret != -EPROBE_DEFER)
3441 dev_err(dapm->dev,
3442 "ASoC: Failed to create DAPM control %s (%d)\n",
3443 widget->name, ret);
3444 goto out_unlock;
3445 }
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003446 if (!w)
3447 dev_err(dapm->dev,
3448 "ASoC: Failed to create DAPM control %s\n",
3449 widget->name);
3450
Linus Walleij37e1df82017-01-13 10:23:52 +01003451out_unlock:
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003452 mutex_unlock(&dapm->card->dapm_mutex);
3453 return w;
3454}
Subhransu S. Prustya5d56392016-06-27 09:18:03 +05303455EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003456
3457struct snd_soc_dapm_widget *
3458snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003459 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003460{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003461 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003462 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003463 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00003464 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003465
3466 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003467 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003468
Mark Brown62ea8742012-01-21 21:14:48 +00003469 switch (w->id) {
3470 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003471 w->regulator = devm_regulator_get(dapm->dev, w->name);
3472 if (IS_ERR(w->regulator)) {
3473 ret = PTR_ERR(w->regulator);
Linus Walleij37e1df82017-01-13 10:23:52 +01003474 if (ret == -EPROBE_DEFER)
3475 return ERR_PTR(ret);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003476 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003477 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003478 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003479 }
Mark Brown8784c772013-01-10 19:33:47 +00003480
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003481 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003482 ret = regulator_allow_bypass(w->regulator, true);
3483 if (ret != 0)
3484 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003485 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003486 w->name, ret);
3487 }
Mark Brown62ea8742012-01-21 21:14:48 +00003488 break;
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003489 case snd_soc_dapm_pinctrl:
3490 w->pinctrl = devm_pinctrl_get(dapm->dev);
Charles Keepaxa5cd7e92018-08-28 14:35:03 +01003491 if (IS_ERR(w->pinctrl)) {
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003492 ret = PTR_ERR(w->pinctrl);
3493 if (ret == -EPROBE_DEFER)
3494 return ERR_PTR(ret);
3495 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3496 w->name, ret);
3497 return NULL;
3498 }
3499 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003500 case snd_soc_dapm_clock_supply:
Mark Brown695594f12012-06-04 08:14:13 +01003501 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003502 if (IS_ERR(w->clk)) {
3503 ret = PTR_ERR(w->clk);
Linus Walleij37e1df82017-01-13 10:23:52 +01003504 if (ret == -EPROBE_DEFER)
3505 return ERR_PTR(ret);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003506 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003507 w->name, ret);
3508 return NULL;
3509 }
3510 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003511 default:
3512 break;
3513 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003514
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003515 prefix = soc_dapm_prefix(dapm);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003516 if (prefix)
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003517 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003518 else
Lars-Peter Clausen48068962015-07-21 18:11:08 +02003519 w->name = kstrdup_const(widget->name, GFP_KERNEL);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003520 if (w->name == NULL) {
3521 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003522 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003523 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003524
Mark Brown7ca3a182011-10-08 14:04:50 +01003525 switch (w->id) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003526 case snd_soc_dapm_mic:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003527 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003528 w->power_check = dapm_generic_check_power;
3529 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003530 case snd_soc_dapm_input:
3531 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003532 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003533 w->power_check = dapm_generic_check_power;
3534 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003535 case snd_soc_dapm_spk:
3536 case snd_soc_dapm_hp:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003537 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003538 w->power_check = dapm_generic_check_power;
3539 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003540 case snd_soc_dapm_output:
3541 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003542 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003543 w->power_check = dapm_generic_check_power;
3544 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003545 case snd_soc_dapm_vmid:
3546 case snd_soc_dapm_siggen:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003547 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003548 w->power_check = dapm_always_on_check_power;
3549 break;
Vinod Koul56b44372015-11-23 21:22:30 +05303550 case snd_soc_dapm_sink:
3551 w->is_ep = SND_SOC_DAPM_EP_SINK;
3552 w->power_check = dapm_always_on_check_power;
3553 break;
3554
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003555 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003556 case snd_soc_dapm_demux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003557 case snd_soc_dapm_switch:
3558 case snd_soc_dapm_mixer:
3559 case snd_soc_dapm_mixer_named_ctl:
Mark Brown63c69a62013-07-18 22:03:01 +01003560 case snd_soc_dapm_adc:
3561 case snd_soc_dapm_aif_out:
3562 case snd_soc_dapm_dac:
3563 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003564 case snd_soc_dapm_pga:
3565 case snd_soc_dapm_out_drv:
Mark Brown7ca3a182011-10-08 14:04:50 +01003566 case snd_soc_dapm_micbias:
Mark Brown7ca3a182011-10-08 14:04:50 +01003567 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003568 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02003569 case snd_soc_dapm_dai_out:
3570 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003571 w->power_check = dapm_generic_check_power;
3572 break;
3573 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003574 case snd_soc_dapm_regulator_supply:
Srinivas Kandagatla5b2d15b2018-03-10 02:37:27 +00003575 case snd_soc_dapm_pinctrl:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003576 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003577 case snd_soc_dapm_kcontrol:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003578 w->is_supply = 1;
Mark Brown7ca3a182011-10-08 14:04:50 +01003579 w->power_check = dapm_supply_check_power;
3580 break;
3581 default:
3582 w->power_check = dapm_always_on_check_power;
3583 break;
3584 }
3585
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003586 w->dapm = dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003587 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003588 INIT_LIST_HEAD(&w->dirty);
Lars-Peter Clausen92fa1242015-05-01 18:02:42 +02003589 list_add_tail(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003590
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003591 snd_soc_dapm_for_each_direction(dir) {
3592 INIT_LIST_HEAD(&w->edges[dir]);
3593 w->endpoints[dir] = -1;
3594 }
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003595
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02003596 /* machine layer sets up unconnected pins and insertions */
Richard Purdie2b97eab2006-10-06 18:32:18 +02003597 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003598 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003599}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003600
3601/**
Mark Brown4ba13272008-05-13 14:51:19 +02003602 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003603 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003604 * @widget: widget array
3605 * @num: number of widgets
3606 *
3607 * Creates new DAPM controls based upon the templates.
3608 *
3609 * Returns 0 for success else error.
3610 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003611int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003612 const struct snd_soc_dapm_widget *widget,
3613 int num)
3614{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003615 struct snd_soc_dapm_widget *w;
3616 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003617 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003618
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003619 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003620 for (i = 0; i < num; i++) {
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003621 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
Linus Walleij37e1df82017-01-13 10:23:52 +01003622 if (IS_ERR(w)) {
3623 ret = PTR_ERR(w);
3624 /* Do not nag about probe deferrals */
3625 if (ret == -EPROBE_DEFER)
3626 break;
3627 dev_err(dapm->dev,
3628 "ASoC: Failed to create DAPM control %s (%d)\n",
3629 widget->name, ret);
3630 break;
3631 }
Mark Brown5ba06fc2012-02-16 11:07:13 -08003632 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003633 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003634 "ASoC: Failed to create DAPM control %s\n",
3635 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003636 ret = -ENOMEM;
3637 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003638 }
Mark Brown4ba13272008-05-13 14:51:19 +02003639 widget++;
3640 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003641 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003642 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003643}
3644EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3645
Mark Brownc74184e2012-04-04 22:12:09 +01003646static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3647 struct snd_kcontrol *kcontrol, int event)
3648{
3649 struct snd_soc_dapm_path *source_p, *sink_p;
3650 struct snd_soc_dai *source, *sink;
Charles Keepax249dc492018-08-15 13:11:35 +01003651 struct snd_soc_pcm_runtime *rtd = w->priv;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003652 const struct snd_soc_pcm_stream *config = w->params + w->params_select;
Mark Brownc74184e2012-04-04 22:12:09 +01003653 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003654 struct snd_pcm_hw_params *params = NULL;
Nicolin Chen8053f212016-07-26 14:55:51 -07003655 struct snd_pcm_runtime *runtime = NULL;
Takashi Iwai3ba66fe2018-07-25 22:43:26 +02003656 unsigned int fmt;
Mark Brownc74184e2012-04-04 22:12:09 +01003657 int ret;
3658
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003659 if (WARN_ON(!config) ||
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003660 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3661 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003662 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003663
3664 /* We only support a single source and sink, pick the first */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003665 source_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_OUT],
3666 struct snd_soc_dapm_path,
3667 list_node[SND_SOC_DAPM_DIR_OUT]);
3668 sink_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_IN],
3669 struct snd_soc_dapm_path,
3670 list_node[SND_SOC_DAPM_DIR_IN]);
Mark Brownc74184e2012-04-04 22:12:09 +01003671
3672 source = source_p->source->priv;
3673 sink = sink_p->sink->priv;
3674
3675 /* Be a little careful as we don't want to overflow the mask array */
3676 if (config->formats) {
3677 fmt = ffs(config->formats) - 1;
3678 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003679 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003680 config->formats);
3681 fmt = 0;
3682 }
3683
3684 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003685 params = kzalloc(sizeof(*params), GFP_KERNEL);
3686 if (!params) {
3687 ret = -ENOMEM;
3688 goto out;
3689 }
3690 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003691
Mark Brown9747cec2012-04-26 19:12:21 +01003692 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003693 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003694 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003695 config->rate_max;
3696
Mark Brown9747cec2012-04-26 19:12:21 +01003697 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003698 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003699 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003700 = config->channels_max;
3701
3702 memset(&substream, 0, sizeof(substream));
3703
Nicolin Chen8053f212016-07-26 14:55:51 -07003704 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3705 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3706 if (!runtime) {
3707 ret = -ENOMEM;
3708 goto out;
3709 }
3710 substream.runtime = runtime;
Charles Keepax249dc492018-08-15 13:11:35 +01003711 substream.private_data = rtd;
Nicolin Chen8053f212016-07-26 14:55:51 -07003712
Mark Brownc74184e2012-04-04 22:12:09 +01003713 switch (event) {
3714 case SND_SOC_DAPM_PRE_PMU:
Benoit Cousson93e69582014-07-08 23:19:38 +02003715 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
Kuninori Morimoto9900a422017-09-25 01:38:54 +00003716 if (source->driver->ops->startup) {
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303717 ret = source->driver->ops->startup(&substream, source);
3718 if (ret < 0) {
3719 dev_err(source->dev,
3720 "ASoC: startup() failed: %d\n", ret);
3721 goto out;
3722 }
3723 source->active++;
3724 }
Benoit Cousson93e69582014-07-08 23:19:38 +02003725 ret = soc_dai_hw_params(&substream, params, source);
3726 if (ret < 0)
3727 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003728
Benoit Cousson93e69582014-07-08 23:19:38 +02003729 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Kuninori Morimoto9900a422017-09-25 01:38:54 +00003730 if (sink->driver->ops->startup) {
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303731 ret = sink->driver->ops->startup(&substream, sink);
3732 if (ret < 0) {
3733 dev_err(sink->dev,
3734 "ASoC: startup() failed: %d\n", ret);
3735 goto out;
3736 }
3737 sink->active++;
3738 }
Benoit Cousson93e69582014-07-08 23:19:38 +02003739 ret = soc_dai_hw_params(&substream, params, sink);
3740 if (ret < 0)
3741 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003742 break;
3743
3744 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003745 ret = snd_soc_dai_digital_mute(sink, 0,
3746 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003747 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003748 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003749 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003750 break;
3751
3752 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003753 ret = snd_soc_dai_digital_mute(sink, 1,
3754 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003755 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003756 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003757 ret = 0;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303758
3759 source->active--;
Kuninori Morimoto9900a422017-09-25 01:38:54 +00003760 if (source->driver->ops->shutdown) {
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303761 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3762 source->driver->ops->shutdown(&substream, source);
3763 }
3764
3765 sink->active--;
Kuninori Morimoto9900a422017-09-25 01:38:54 +00003766 if (sink->driver->ops->shutdown) {
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303767 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3768 sink->driver->ops->shutdown(&substream, sink);
3769 }
Mark Brownc74184e2012-04-04 22:12:09 +01003770 break;
3771
3772 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003773 WARN(1, "Unknown event %d\n", event);
Sudip Mukherjee75881df2015-09-10 18:01:44 +05303774 ret = -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003775 }
3776
Mark Brown9747cec2012-04-26 19:12:21 +01003777out:
Nicolin Chen8053f212016-07-26 14:55:51 -07003778 kfree(runtime);
Mark Brown9747cec2012-04-26 19:12:21 +01003779 kfree(params);
3780 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003781}
3782
Nikesh Oswalc6615082015-02-02 17:06:44 +00003783static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3784 struct snd_ctl_elem_value *ucontrol)
3785{
3786 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3787
Takashi Iwai741338f2016-02-29 17:20:48 +01003788 ucontrol->value.enumerated.item[0] = w->params_select;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003789
3790 return 0;
3791}
3792
3793static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3794 struct snd_ctl_elem_value *ucontrol)
3795{
3796 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3797
3798 /* Can't change the config when widget is already powered */
3799 if (w->power)
3800 return -EBUSY;
3801
Takashi Iwai741338f2016-02-29 17:20:48 +01003802 if (ucontrol->value.enumerated.item[0] == w->params_select)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003803 return 0;
3804
Takashi Iwai741338f2016-02-29 17:20:48 +01003805 if (ucontrol->value.enumerated.item[0] >= w->num_params)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003806 return -EINVAL;
3807
Takashi Iwai741338f2016-02-29 17:20:48 +01003808 w->params_select = ucontrol->value.enumerated.item[0];
Nikesh Oswalc6615082015-02-02 17:06:44 +00003809
3810 return 0;
3811}
3812
Arnd Bergmannc42c5ac2017-10-10 11:20:11 +02003813static void
anish kumar19ad6832017-09-28 21:52:39 -07003814snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
3815 unsigned long *private_value,
3816 int num_params,
3817 const char **w_param_text)
Mark Brownc74184e2012-04-04 22:12:09 +01003818{
anish kumar19ad6832017-09-28 21:52:39 -07003819 int count;
3820
3821 devm_kfree(card->dev, (void *)*private_value);
3822 for (count = 0 ; count < num_params; count++)
3823 devm_kfree(card->dev, (void *)w_param_text[count]);
3824 devm_kfree(card->dev, w_param_text);
3825}
3826
3827static struct snd_kcontrol_new *
3828snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
3829 char *link_name,
3830 const struct snd_soc_pcm_stream *params,
3831 int num_params, const char **w_param_text,
3832 unsigned long *private_value)
3833{
Nikesh Oswalc6615082015-02-02 17:06:44 +00003834 struct soc_enum w_param_enum[] = {
3835 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3836 };
3837 struct snd_kcontrol_new kcontrol_dai_link[] = {
3838 SOC_ENUM_EXT(NULL, w_param_enum[0],
3839 snd_soc_dapm_dai_link_get,
3840 snd_soc_dapm_dai_link_put),
3841 };
anish kumar19ad6832017-09-28 21:52:39 -07003842 struct snd_kcontrol_new *kcontrol_news;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003843 const struct snd_soc_pcm_stream *config = params;
anish kumar19ad6832017-09-28 21:52:39 -07003844 int count;
Mark Brownc74184e2012-04-04 22:12:09 +01003845
Nikesh Oswalc6615082015-02-02 17:06:44 +00003846 for (count = 0 ; count < num_params; count++) {
3847 if (!config->stream_name) {
3848 dev_warn(card->dapm.dev,
3849 "ASoC: anonymous config %d for dai link %s\n",
3850 count, link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003851 w_param_text[count] =
Charles Keepax46172b62015-03-25 11:22:35 +00003852 devm_kasprintf(card->dev, GFP_KERNEL,
3853 "Anonymous Configuration %d",
3854 count);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003855 } else {
3856 w_param_text[count] = devm_kmemdup(card->dev,
3857 config->stream_name,
3858 strlen(config->stream_name) + 1,
3859 GFP_KERNEL);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003860 }
anish kumar19ad6832017-09-28 21:52:39 -07003861 if (!w_param_text[count])
3862 goto outfree_w_param;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003863 config++;
3864 }
anish kumar19ad6832017-09-28 21:52:39 -07003865
Nikesh Oswalc6615082015-02-02 17:06:44 +00003866 w_param_enum[0].items = num_params;
3867 w_param_enum[0].texts = w_param_text;
Mark Brownc74184e2012-04-04 22:12:09 +01003868
anish kumar19ad6832017-09-28 21:52:39 -07003869 *private_value =
3870 (unsigned long) devm_kmemdup(card->dev,
3871 (void *)(kcontrol_dai_link[0].private_value),
3872 sizeof(struct soc_enum), GFP_KERNEL);
3873 if (!*private_value) {
3874 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3875 link_name);
3876 goto outfree_w_param;
3877 }
3878 kcontrol_dai_link[0].private_value = *private_value;
3879 /* duplicate kcontrol_dai_link on heap so that memory persists */
3880 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3881 sizeof(struct snd_kcontrol_new),
3882 GFP_KERNEL);
3883 if (!kcontrol_news) {
3884 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3885 link_name);
3886 goto outfree_w_param;
3887 }
3888 return kcontrol_news;
3889
3890outfree_w_param:
3891 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
3892 return NULL;
3893}
3894
3895int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
Charles Keepax249dc492018-08-15 13:11:35 +01003896 struct snd_soc_pcm_runtime *rtd,
anish kumar19ad6832017-09-28 21:52:39 -07003897 const struct snd_soc_pcm_stream *params,
3898 unsigned int num_params,
3899 struct snd_soc_dapm_widget *source,
3900 struct snd_soc_dapm_widget *sink)
3901{
3902 struct snd_soc_dapm_widget template;
3903 struct snd_soc_dapm_widget *w;
3904 const char **w_param_text;
3905 unsigned long private_value;
3906 char *link_name;
3907 int ret;
3908
3909 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3910 source->name, sink->name);
3911 if (!link_name)
3912 return -ENOMEM;
3913
Mark Brownc74184e2012-04-04 22:12:09 +01003914 memset(&template, 0, sizeof(template));
3915 template.reg = SND_SOC_NOPM;
3916 template.id = snd_soc_dapm_dai_link;
3917 template.name = link_name;
3918 template.event = snd_soc_dai_link_event;
3919 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3920 SND_SOC_DAPM_PRE_PMD;
anish kumar19ad6832017-09-28 21:52:39 -07003921 template.kcontrol_news = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003922
anish kumar19ad6832017-09-28 21:52:39 -07003923 /* allocate memory for control, only in case of multiple configs */
3924 if (num_params > 1) {
3925 w_param_text = devm_kcalloc(card->dev, num_params,
3926 sizeof(char *), GFP_KERNEL);
3927 if (!w_param_text) {
3928 ret = -ENOMEM;
3929 goto param_fail;
3930 }
3931
3932 template.num_kcontrols = 1;
3933 template.kcontrol_news =
3934 snd_soc_dapm_alloc_kcontrol(card,
3935 link_name, params, num_params,
3936 w_param_text, &private_value);
3937 if (!template.kcontrol_news) {
3938 ret = -ENOMEM;
3939 goto param_fail;
3940 }
Arnd Bergmann667ebc92017-10-10 11:20:10 +02003941 } else {
3942 w_param_text = NULL;
anish kumar19ad6832017-09-28 21:52:39 -07003943 }
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003944 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003945
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003946 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
Linus Walleij37e1df82017-01-13 10:23:52 +01003947 if (IS_ERR(w)) {
3948 ret = PTR_ERR(w);
3949 /* Do not nag about probe deferrals */
3950 if (ret != -EPROBE_DEFER)
3951 dev_err(card->dev,
3952 "ASoC: Failed to create %s widget (%d)\n",
3953 link_name, ret);
3954 goto outfree_kcontrol_news;
3955 }
Mark Brownc74184e2012-04-04 22:12:09 +01003956 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003957 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003958 link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003959 ret = -ENOMEM;
3960 goto outfree_kcontrol_news;
Mark Brownc74184e2012-04-04 22:12:09 +01003961 }
3962
3963 w->params = params;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003964 w->num_params = num_params;
Charles Keepax249dc492018-08-15 13:11:35 +01003965 w->priv = rtd;
Mark Brownc74184e2012-04-04 22:12:09 +01003966
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003967 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3968 if (ret)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003969 goto outfree_w;
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003970 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003971
3972outfree_w:
3973 devm_kfree(card->dev, w);
3974outfree_kcontrol_news:
3975 devm_kfree(card->dev, (void *)template.kcontrol_news);
anish kumar19ad6832017-09-28 21:52:39 -07003976 snd_soc_dapm_free_kcontrol(card, &private_value, num_params, w_param_text);
3977param_fail:
Nikesh Oswalc6615082015-02-02 17:06:44 +00003978 devm_kfree(card->dev, link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003979 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003980}
3981
Mark Brown888df392012-02-16 19:37:51 -08003982int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3983 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003984{
Mark Brown888df392012-02-16 19:37:51 -08003985 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003986 struct snd_soc_dapm_widget *w;
3987
Mark Brown888df392012-02-16 19:37:51 -08003988 WARN_ON(dapm->dev != dai->dev);
3989
3990 memset(&template, 0, sizeof(template));
3991 template.reg = SND_SOC_NOPM;
3992
3993 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003994 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003995 template.name = dai->driver->playback.stream_name;
3996 template.sname = dai->driver->playback.stream_name;
3997
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003998 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003999 template.name);
4000
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004001 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Linus Walleij639467c2017-01-20 14:07:52 +01004002 if (IS_ERR(w)) {
4003 int ret = PTR_ERR(w);
4004
4005 /* Do not nag about probe deferrals */
4006 if (ret != -EPROBE_DEFER)
4007 dev_err(dapm->dev,
4008 "ASoC: Failed to create %s widget (%d)\n",
4009 dai->driver->playback.stream_name, ret);
4010 return ret;
4011 }
Mark Brown888df392012-02-16 19:37:51 -08004012 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004013 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08004014 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01004015 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08004016 }
4017
4018 w->priv = dai;
4019 dai->playback_widget = w;
4020 }
4021
4022 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01004023 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08004024 template.name = dai->driver->capture.stream_name;
4025 template.sname = dai->driver->capture.stream_name;
4026
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004027 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08004028 template.name);
4029
Liam Girdwood02aa78a2015-05-25 18:21:17 +01004030 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Linus Walleij639467c2017-01-20 14:07:52 +01004031 if (IS_ERR(w)) {
4032 int ret = PTR_ERR(w);
4033
4034 /* Do not nag about probe deferrals */
4035 if (ret != -EPROBE_DEFER)
4036 dev_err(dapm->dev,
4037 "ASoC: Failed to create %s widget (%d)\n",
4038 dai->driver->playback.stream_name, ret);
4039 return ret;
4040 }
Mark Brown888df392012-02-16 19:37:51 -08004041 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004042 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08004043 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01004044 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08004045 }
4046
4047 w->priv = dai;
4048 dai->capture_widget = w;
4049 }
4050
4051 return 0;
4052}
4053
4054int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4055{
4056 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004057 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08004058 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08004059
4060 /* For each DAI widget... */
4061 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01004062 switch (dai_w->id) {
4063 case snd_soc_dapm_dai_in:
4064 case snd_soc_dapm_dai_out:
4065 break;
4066 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02004067 continue;
Mark Brown46162742013-06-05 19:36:11 +01004068 }
Mark Brown888df392012-02-16 19:37:51 -08004069
Liam Girdwoode01b4f62018-06-14 20:26:42 +01004070 /* let users know there is no DAI to link */
4071 if (!dai_w->priv) {
4072 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4073 dai_w->name);
4074 continue;
4075 }
4076
Mark Brown888df392012-02-16 19:37:51 -08004077 dai = dai_w->priv;
4078
4079 /* ...find all widgets with the same stream and link them */
4080 list_for_each_entry(w, &card->widgets, list) {
4081 if (w->dapm != dai_w->dapm)
4082 continue;
4083
Mark Brown46162742013-06-05 19:36:11 +01004084 switch (w->id) {
4085 case snd_soc_dapm_dai_in:
4086 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08004087 continue;
Mark Brown46162742013-06-05 19:36:11 +01004088 default:
4089 break;
4090 }
Mark Brown888df392012-02-16 19:37:51 -08004091
Lars-Peter Clausena798c242015-07-21 11:51:35 +02004092 if (!w->sname || !strstr(w->sname, dai_w->sname))
Mark Brown888df392012-02-16 19:37:51 -08004093 continue;
4094
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004095 if (dai_w->id == snd_soc_dapm_dai_in) {
4096 src = dai_w;
4097 sink = w;
4098 } else {
4099 src = w;
4100 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08004101 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02004102 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4103 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004104 }
4105 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02004106
Mark Brown888df392012-02-16 19:37:51 -08004107 return 0;
4108}
Liam Girdwood64a648c2011-07-25 11:15:15 +01004109
Benoit Cousson44ba2642014-07-08 23:19:36 +02004110static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4111 struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004112{
Benoit Cousson44ba2642014-07-08 23:19:36 +02004113 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00004114 struct snd_soc_dai *codec_dai;
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02004115 struct snd_soc_dapm_widget *sink, *source;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004116 int i;
4117
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00004118 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004119
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004120 /* connect BE DAI playback if widgets are valid */
4121 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02004122 source = cpu_dai->playback_widget;
4123 sink = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004124 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004125 cpu_dai->component->name, source->name,
4126 codec_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004127
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02004128 snd_soc_dapm_add_path(&card->dapm, source, sink,
4129 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004130 }
4131
4132 /* connect BE DAI capture if widgets are valid */
4133 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02004134 source = codec_dai->capture_widget;
4135 sink = cpu_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004136 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004137 codec_dai->component->name, source->name,
4138 cpu_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004139
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02004140 snd_soc_dapm_add_path(&card->dapm, source, sink,
4141 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004142 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004143 }
4144}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004145
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004146static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4147 int event)
4148{
4149 struct snd_soc_dapm_widget *w;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004150 unsigned int ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004151
4152 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4153 w = dai->playback_widget;
4154 else
4155 w = dai->capture_widget;
4156
4157 if (w) {
4158 dapm_mark_dirty(w, "stream event");
4159
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004160 if (w->id == snd_soc_dapm_dai_in) {
4161 ep = SND_SOC_DAPM_EP_SOURCE;
4162 dapm_widget_invalidate_input_paths(w);
4163 } else {
4164 ep = SND_SOC_DAPM_EP_SINK;
4165 dapm_widget_invalidate_output_paths(w);
4166 }
4167
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004168 switch (event) {
4169 case SND_SOC_DAPM_STREAM_START:
4170 w->active = 1;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004171 w->is_ep = ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004172 break;
4173 case SND_SOC_DAPM_STREAM_STOP:
4174 w->active = 0;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004175 w->is_ep = 0;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004176 break;
4177 case SND_SOC_DAPM_STREAM_SUSPEND:
4178 case SND_SOC_DAPM_STREAM_RESUME:
4179 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4180 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4181 break;
4182 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004183 }
4184}
4185
Benoit Cousson44ba2642014-07-08 23:19:36 +02004186void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4187{
Mengdong Lin1a497982015-11-18 02:34:11 -05004188 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson44ba2642014-07-08 23:19:36 +02004189
4190 /* for each BE DAI link... */
Mengdong Lin1a497982015-11-18 02:34:11 -05004191 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson44ba2642014-07-08 23:19:36 +02004192 /*
4193 * dynamic FE links have no fixed DAI mapping.
4194 * CODEC<->CODEC links have no direct connection.
4195 */
4196 if (rtd->dai_link->dynamic || rtd->dai_link->params)
4197 continue;
4198
4199 dapm_connect_dai_link_widgets(card, rtd);
4200 }
4201}
4202
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004203static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4204 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004205{
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00004206 struct snd_soc_dai *codec_dai;
Benoit Cousson44ba2642014-07-08 23:19:36 +02004207 int i;
4208
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004209 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00004210 for_each_rtd_codec_dai(rtd, i, codec_dai)
4211 soc_dapm_dai_stream_event(codec_dai, stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004212
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004213 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004214}
4215
4216/**
4217 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4218 * @rtd: PCM runtime data
4219 * @stream: stream name
4220 * @event: stream event
4221 *
4222 * Sends a stream event to the dapm core. The core then makes any
4223 * necessary widget power changes.
4224 *
4225 * Returns 0 for success else error.
4226 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004227void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4228 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004229{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004230 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004231
Liam Girdwood3cd04342012-03-09 12:02:08 +00004232 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004233 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004234 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004235}
Richard Purdie2b97eab2006-10-06 18:32:18 +02004236
4237/**
Charles Keepax11391102014-02-18 15:22:14 +00004238 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4239 * @dapm: DAPM context
4240 * @pin: pin name
4241 *
4242 * Enables input/output pin and its parents or children widgets iff there is
4243 * a valid audio route and active audio stream.
4244 *
4245 * Requires external locking.
4246 *
4247 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4248 * do any widget power switching.
4249 */
4250int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4251 const char *pin)
4252{
4253 return snd_soc_dapm_set_pin(dapm, pin, 1);
4254}
4255EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4256
4257/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004258 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004259 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004260 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02004261 *
Mark Brown74b8f952009-06-06 11:26:15 +01004262 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01004263 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00004264 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004265 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4266 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02004267 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004268int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004269{
Charles Keepax11391102014-02-18 15:22:14 +00004270 int ret;
4271
4272 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4273
4274 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4275
4276 mutex_unlock(&dapm->card->dapm_mutex);
4277
4278 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004279}
Liam Girdwooda5302182008-07-07 13:35:17 +01004280EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004281
4282/**
Charles Keepax11391102014-02-18 15:22:14 +00004283 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4284 * @dapm: DAPM context
4285 * @pin: pin name
4286 *
4287 * Enables input/output pin regardless of any other state. This is
4288 * intended for use with microphone bias supplies used in microphone
4289 * jack detection.
4290 *
4291 * Requires external locking.
4292 *
4293 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4294 * do any widget power switching.
4295 */
4296int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4297 const char *pin)
4298{
4299 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4300
4301 if (!w) {
4302 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4303 return -EINVAL;
4304 }
4305
4306 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02004307 if (!w->connected) {
4308 /*
4309 * w->force does not affect the number of input or output paths,
4310 * so we only have to recheck if w->connected is changed
4311 */
4312 dapm_widget_invalidate_input_paths(w);
4313 dapm_widget_invalidate_output_paths(w);
4314 w->connected = 1;
4315 }
Charles Keepax11391102014-02-18 15:22:14 +00004316 w->force = 1;
4317 dapm_mark_dirty(w, "force enable");
4318
4319 return 0;
4320}
4321EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4322
4323/**
Mark Brownda341832010-03-15 19:23:37 +00004324 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004325 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00004326 * @pin: pin name
4327 *
4328 * Enables input/output pin regardless of any other state. This is
4329 * intended for use with microphone bias supplies used in microphone
4330 * jack detection.
4331 *
4332 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4333 * do any widget power switching.
4334 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004335int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4336 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00004337{
Charles Keepax11391102014-02-18 15:22:14 +00004338 int ret;
Mark Brownda341832010-03-15 19:23:37 +00004339
Charles Keepax11391102014-02-18 15:22:14 +00004340 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00004341
Charles Keepax11391102014-02-18 15:22:14 +00004342 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09004343
Charles Keepax11391102014-02-18 15:22:14 +00004344 mutex_unlock(&dapm->card->dapm_mutex);
4345
4346 return ret;
Mark Brownda341832010-03-15 19:23:37 +00004347}
4348EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4349
4350/**
Charles Keepax11391102014-02-18 15:22:14 +00004351 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4352 * @dapm: DAPM context
4353 * @pin: pin name
4354 *
4355 * Disables input/output pin and its parents or children widgets.
4356 *
4357 * Requires external locking.
4358 *
4359 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4360 * do any widget power switching.
4361 */
4362int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4363 const char *pin)
4364{
4365 return snd_soc_dapm_set_pin(dapm, pin, 0);
4366}
4367EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4368
4369/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004370 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004371 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004372 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004373 *
Mark Brown74b8f952009-06-06 11:26:15 +01004374 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00004375 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004376 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4377 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004378 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004379int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4380 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01004381{
Charles Keepax11391102014-02-18 15:22:14 +00004382 int ret;
4383
4384 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4385
4386 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4387
4388 mutex_unlock(&dapm->card->dapm_mutex);
4389
4390 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01004391}
4392EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4393
4394/**
Charles Keepax11391102014-02-18 15:22:14 +00004395 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4396 * @dapm: DAPM context
4397 * @pin: pin name
4398 *
4399 * Marks the specified pin as being not connected, disabling it along
4400 * any parent or child widgets. At present this is identical to
4401 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4402 * additional things such as disabling controls which only affect
4403 * paths through the pin.
4404 *
4405 * Requires external locking.
4406 *
4407 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4408 * do any widget power switching.
4409 */
4410int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4411 const char *pin)
4412{
4413 return snd_soc_dapm_set_pin(dapm, pin, 0);
4414}
4415EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4416
4417/**
Mark Brown5817b522008-09-24 11:23:11 +01004418 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004419 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01004420 * @pin: pin name
4421 *
4422 * Marks the specified pin as being not connected, disabling it along
4423 * any parent or child widgets. At present this is identical to
4424 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4425 * additional things such as disabling controls which only affect
4426 * paths through the pin.
4427 *
4428 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4429 * do any widget power switching.
4430 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004431int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01004432{
Charles Keepax11391102014-02-18 15:22:14 +00004433 int ret;
4434
4435 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4436
4437 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4438
4439 mutex_unlock(&dapm->card->dapm_mutex);
4440
4441 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01004442}
4443EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4444
4445/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004446 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004447 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004448 * @pin: audio signal pin endpoint (or start point)
4449 *
4450 * Get audio pin status - connected or disconnected.
4451 *
4452 * Returns 1 for connected otherwise 0.
4453 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004454int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4455 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004456{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004457 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004458
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004459 if (w)
4460 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06004461
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004462 return 0;
4463}
Liam Girdwooda5302182008-07-07 13:35:17 +01004464EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004465
4466/**
Mark Brown1547aba2010-05-07 21:11:40 +01004467 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004468 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01004469 * @pin: audio signal pin endpoint (or start point)
4470 *
4471 * Mark the given endpoint or pin as ignoring suspend. When the
4472 * system is disabled a path between two endpoints flagged as ignoring
4473 * suspend will not be disabled. The path must already be enabled via
4474 * normal means at suspend time, it will not be turned on if it was not
4475 * already enabled.
4476 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004477int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4478 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01004479{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004480 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01004481
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004482 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004483 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004484 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01004485 }
4486
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004487 w->ignore_suspend = 1;
4488
4489 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01004490}
4491EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4492
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02004493/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02004494 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03004495 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02004496 *
4497 * Free all dapm widgets and resources.
4498 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004499void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004500{
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02004501 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004502 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02004503 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004504}
4505EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4506
Xiang Xiao57996352014-03-02 00:04:02 +08004507static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01004508{
Liam Girdwood01005a72012-07-06 16:57:05 +01004509 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01004510 struct snd_soc_dapm_widget *w;
4511 LIST_HEAD(down_list);
4512 int powerdown = 0;
4513
Liam Girdwood01005a72012-07-06 16:57:05 +01004514 mutex_lock(&card->dapm_mutex);
4515
Jarkko Nikula97c866d2010-12-14 12:18:31 +02004516 list_for_each_entry(w, &dapm->card->widgets, list) {
4517 if (w->dapm != dapm)
4518 continue;
Mark Brown51737472009-06-22 13:16:51 +01004519 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004520 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01004521 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004522 powerdown = 1;
4523 }
4524 }
4525
4526 /* If there were no widgets to power down we're already in
4527 * standby.
4528 */
4529 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004530 if (dapm->bias_level == SND_SOC_BIAS_ON)
4531 snd_soc_dapm_set_bias_level(dapm,
4532 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004533 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004534 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4535 snd_soc_dapm_set_bias_level(dapm,
4536 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004537 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004538
4539 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004540}
Mark Brown51737472009-06-22 13:16:51 +01004541
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004542/*
4543 * snd_soc_dapm_shutdown - callback for system shutdown
4544 */
4545void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4546{
Xiang Xiao57996352014-03-02 00:04:02 +08004547 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004548
Xiang Xiao57996352014-03-02 00:04:02 +08004549 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08004550 if (dapm != &card->dapm) {
4551 soc_dapm_shutdown_dapm(dapm);
4552 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4553 snd_soc_dapm_set_bias_level(dapm,
4554 SND_SOC_BIAS_OFF);
4555 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004556 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08004557
4558 soc_dapm_shutdown_dapm(&card->dapm);
4559 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4560 snd_soc_dapm_set_bias_level(&card->dapm,
4561 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01004562}
4563
Richard Purdie2b97eab2006-10-06 18:32:18 +02004564/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004565MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004566MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4567MODULE_LICENSE("GPL");