blob: 0b8e65d56a0cac8b9af129570d36be8399f652b8 [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
Liam Girdwood612a3fe2012-02-06 16:05:29 +000017 * mic/headphone insertion events.
Richard Purdie2b97eab2006-10-06 18:32:18 +020018 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Liam Girdwood612a3fe2012-02-06 16:05:29 +000021 * o Delayed power down of audio subsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020024 */
25
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080029#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020030#include <linux/delay.h>
31#include <linux/pm.h>
32#include <linux/bitops.h>
33#include <linux/platform_device.h>
34#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020035#include <linux/debugfs.h>
Mark Brownf1aac482011-12-05 15:17:06 +000036#include <linux/pm_runtime.h>
Mark Brown62ea8742012-01-21 21:14:48 +000037#include <linux/regulator/consumer.h>
Ola Liljad7e7eb92012-05-24 15:26:25 +020038#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020040#include <sound/core.h>
41#include <sound/pcm.h>
42#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020043#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020044#include <sound/initval.h>
45
Mark Brown84e90932010-11-04 00:07:02 -040046#include <trace/events/asoc.h>
47
Mark Brownde02d072011-09-20 21:43:24 +010048#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
49
Lars-Peter Clausena3423b02015-08-11 21:38:00 +020050#define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
51 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
52
53#define snd_soc_dapm_for_each_direction(dir) \
54 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
55 (dir)++)
56
Lars-Peter Clausen57295072013-08-05 11:27:31 +020057static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
58 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
59 const char *control,
60 int (*connected)(struct snd_soc_dapm_widget *source,
61 struct snd_soc_dapm_widget *sink));
Vladimir Zapolskiy5353f652015-06-02 00:57:53 +030062
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +010063struct snd_soc_dapm_widget *
Lars-Peter Clausen57295072013-08-05 11:27:31 +020064snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
65 const struct snd_soc_dapm_widget *widget);
66
Liam Girdwood02aa78a2015-05-25 18:21:17 +010067struct snd_soc_dapm_widget *
68snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +020069 const struct snd_soc_dapm_widget *widget);
70
Richard Purdie2b97eab2006-10-06 18:32:18 +020071/* dapm power sequences - make this per codec in the future */
72static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010073 [snd_soc_dapm_pre] = 0,
Mark Brown62ea8742012-01-21 21:14:48 +000074 [snd_soc_dapm_regulator_supply] = 1,
Ola Liljad7e7eb92012-05-24 15:26:25 +020075 [snd_soc_dapm_clock_supply] = 1,
Mark Brown1dd275b2013-10-09 13:56:37 +010076 [snd_soc_dapm_supply] = 2,
77 [snd_soc_dapm_micbias] = 3,
Mark Brownc74184e2012-04-04 22:12:09 +010078 [snd_soc_dapm_dai_link] = 2,
Mark Brown1dd275b2013-10-09 13:56:37 +010079 [snd_soc_dapm_dai_in] = 4,
80 [snd_soc_dapm_dai_out] = 4,
Sudheer Papothi8f59bd42016-01-29 01:47:18 +053081 [snd_soc_dapm_adc] = 4,
Mark Brown1dd275b2013-10-09 13:56:37 +010082 [snd_soc_dapm_mic] = 5,
83 [snd_soc_dapm_mux] = 6,
Lars-Peter Clausend714f972015-05-01 18:02:43 +020084 [snd_soc_dapm_demux] = 6,
Mark Brown1dd275b2013-10-09 13:56:37 +010085 [snd_soc_dapm_dac] = 7,
86 [snd_soc_dapm_switch] = 8,
87 [snd_soc_dapm_mixer] = 8,
88 [snd_soc_dapm_mixer_named_ctl] = 8,
89 [snd_soc_dapm_pga] = 9,
Sudheer Papothi8f59bd42016-01-29 01:47:18 +053090 [snd_soc_dapm_aif_in] = 9,
91 [snd_soc_dapm_aif_out] = 9,
Mark Brown1dd275b2013-10-09 13:56:37 +010092 [snd_soc_dapm_out_drv] = 11,
93 [snd_soc_dapm_hp] = 11,
94 [snd_soc_dapm_spk] = 11,
95 [snd_soc_dapm_line] = 11,
96 [snd_soc_dapm_kcontrol] = 12,
97 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +020098};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000099
Richard Purdie2b97eab2006-10-06 18:32:18 +0200100static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +0100101 [snd_soc_dapm_pre] = 0,
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200102 [snd_soc_dapm_kcontrol] = 1,
Sudheer Papothi8f59bd42016-01-29 01:47:18 +0530103 [snd_soc_dapm_aif_in] = 2,
104 [snd_soc_dapm_aif_out] = 2,
Banajit Goswami6d6a6022014-08-21 16:18:40 -0700105 [snd_soc_dapm_adc] = 5,
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200106 [snd_soc_dapm_hp] = 3,
107 [snd_soc_dapm_spk] = 3,
108 [snd_soc_dapm_line] = 3,
109 [snd_soc_dapm_out_drv] = 3,
Mark Brown38357ab2009-06-06 19:03:23 +0100110 [snd_soc_dapm_pga] = 4,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +0200111 [snd_soc_dapm_switch] = 5,
Mark Brown38357ab2009-06-06 19:03:23 +0100112 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +0100113 [snd_soc_dapm_mixer] = 5,
114 [snd_soc_dapm_dac] = 6,
115 [snd_soc_dapm_mic] = 7,
116 [snd_soc_dapm_micbias] = 8,
117 [snd_soc_dapm_mux] = 9,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200118 [snd_soc_dapm_demux] = 9,
Mark Brown46162742013-06-05 19:36:11 +0100119 [snd_soc_dapm_dai_in] = 10,
120 [snd_soc_dapm_dai_out] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +0100121 [snd_soc_dapm_dai_link] = 11,
Mark Brownc74184e2012-04-04 22:12:09 +0100122 [snd_soc_dapm_supply] = 12,
Mark Brown1dd275b2013-10-09 13:56:37 +0100123 [snd_soc_dapm_clock_supply] = 13,
124 [snd_soc_dapm_regulator_supply] = 13,
125 [snd_soc_dapm_post] = 14,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200126};
127
Mark Brownf9fa2b12014-03-06 16:49:11 +0800128static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
129{
130 if (dapm->card && dapm->card->instantiated)
131 lockdep_assert_held(&dapm->card->dapm_mutex);
132}
133
Troy Kisky12ef1932008-10-13 17:42:14 -0700134static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100135{
136 if (pop_time)
137 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
138}
139
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200140static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100141{
142 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200143 char *buf;
144
145 if (!pop_time)
146 return;
147
148 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
149 if (buf == NULL)
150 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100151
152 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200153 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100154 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100155 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200156
157 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100158}
159
Mark Browndb432b42011-10-03 21:06:40 +0100160static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
161{
162 return !list_empty(&w->dirty);
163}
164
Mark Brown492c0a12014-03-06 16:15:48 +0800165static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100166{
Mark Brownf9fa2b12014-03-06 16:49:11 +0800167 dapm_assert_locked(w->dapm);
168
Mark Brown75c1f892011-10-04 22:28:08 +0100169 if (!dapm_dirty_widget(w)) {
170 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
171 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100172 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100173 }
Mark Browndb432b42011-10-03 21:06:40 +0100174}
175
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200176/*
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200177 * Common implementation for dapm_widget_invalidate_input_paths() and
178 * dapm_widget_invalidate_output_paths(). The function is inlined since the
179 * combined size of the two specialized functions is only marginally larger then
180 * the size of the generic function and at the same time the fast path of the
181 * specialized functions is significantly smaller than the generic function.
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200182 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200183static __always_inline void dapm_widget_invalidate_paths(
184 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200185{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200186 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
187 struct snd_soc_dapm_widget *node;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200188 struct snd_soc_dapm_path *p;
189 LIST_HEAD(list);
190
191 dapm_assert_locked(w->dapm);
192
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200193 if (w->endpoints[dir] == -1)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200194 return;
195
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200196 list_add_tail(&w->work_list, &list);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200197 w->endpoints[dir] = -1;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200198
199 list_for_each_entry(w, &list, work_list) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200200 snd_soc_dapm_widget_for_each_path(w, dir, p) {
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200201 if (p->is_supply || p->weak || !p->connect)
202 continue;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200203 node = p->node[rdir];
204 if (node->endpoints[dir] != -1) {
205 node->endpoints[dir] = -1;
206 list_add_tail(&node->work_list, &list);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200207 }
208 }
209 }
210}
211
212/*
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200213 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
214 * input paths
215 * @w: The widget for which to invalidate the cached number of input paths
216 *
217 * Resets the cached number of inputs for the specified widget and all widgets
218 * that can be reached via outcoming paths from the widget.
219 *
220 * This function must be called if the number of output paths for a widget might
221 * have changed. E.g. if the source state of a widget changes or a path is added
222 * or activated with the widget as the sink.
223 */
224static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
225{
226 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
227}
228
229/*
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200230 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
231 * output paths
232 * @w: The widget for which to invalidate the cached number of output paths
233 *
234 * Resets the cached number of outputs for the specified widget and all widgets
235 * that can be reached via incoming paths from the widget.
236 *
237 * This function must be called if the number of output paths for a widget might
238 * have changed. E.g. if the sink state of a widget changes or a path is added
239 * or activated with the widget as the source.
240 */
241static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
242{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200243 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200244}
245
246/*
247 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
248 * for the widgets connected to a path
249 * @p: The path to invalidate
250 *
251 * Resets the cached number of inputs for the sink of the path and the cached
252 * number of outputs for the source of the path.
253 *
254 * This function must be called when a path is added, removed or the connected
255 * state changes.
256 */
257static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
258{
259 /*
260 * Weak paths or supply paths do not influence the number of input or
261 * output paths of their neighbors.
262 */
263 if (p->weak || p->is_supply)
264 return;
265
266 /*
267 * The number of connected endpoints is the sum of the number of
268 * connected endpoints of all neighbors. If a node with 0 connected
269 * endpoints is either connected or disconnected that sum won't change,
270 * so there is no need to re-check the path.
271 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200272 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200273 dapm_widget_invalidate_input_paths(p->sink);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200274 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200275 dapm_widget_invalidate_output_paths(p->source);
276}
277
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200278void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
Mark Browne2d32ff2012-08-31 17:38:32 -0700279{
Mark Browne2d32ff2012-08-31 17:38:32 -0700280 struct snd_soc_dapm_widget *w;
281
282 mutex_lock(&card->dapm_mutex);
283
284 list_for_each_entry(w, &card->widgets, list) {
Sudheer Papothi9eb600c2016-01-29 02:04:49 +0530285 if (w->ignore_suspend)
286 continue;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200287 if (w->is_ep) {
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200288 dapm_mark_dirty(w, "Rechecking endpoints");
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200289 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200290 dapm_widget_invalidate_output_paths(w);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200291 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200292 dapm_widget_invalidate_input_paths(w);
293 }
Mark Browne2d32ff2012-08-31 17:38:32 -0700294 }
295
296 mutex_unlock(&card->dapm_mutex);
297}
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200298EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
Mark Browne2d32ff2012-08-31 17:38:32 -0700299
Richard Purdie2b97eab2006-10-06 18:32:18 +0200300/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100301static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200302 const struct snd_soc_dapm_widget *_widget)
303{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100304 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200305}
306
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200307struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200308 unsigned int value;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200309 struct snd_soc_dapm_widget *widget;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200310 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200311 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200312};
313
314static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100315 struct snd_kcontrol *kcontrol, const char *ctrl_name)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200316{
317 struct dapm_kcontrol_data *data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200318 struct soc_mixer_control *mc;
Charles Keepax561ed682015-05-01 12:37:26 +0100319 struct soc_enum *e;
Charles Keepax773da9b2015-05-01 12:37:25 +0100320 const char *name;
321 int ret;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200322
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200323 data = kzalloc(sizeof(*data), GFP_KERNEL);
Charles Keepax40b7bea2015-05-01 12:37:24 +0100324 if (!data)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200325 return -ENOMEM;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200326
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200327 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200328
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200329 switch (widget->id) {
330 case snd_soc_dapm_switch:
331 case snd_soc_dapm_mixer:
332 case snd_soc_dapm_mixer_named_ctl:
333 mc = (struct soc_mixer_control *)kcontrol->private_value;
334
335 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 Walleijde415c82017-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 Walleijde415c82017-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 Kandagatla1a1b2792018-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
Banajit Goswamid7672b72017-07-21 01:13:18 -0700436static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200437 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
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100723 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200724 if (!(strcmp(control_name, e->texts[i]))) {
Rasmus Villemoes98ad73c2014-10-21 17:01:15 +0200725 path->name = e->texts[i];
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100726 if (i == item)
727 path->connect = 1;
728 else
729 path->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200730 return 0;
731 }
732 }
733
734 return -ENODEV;
735}
736
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100737/* set up initial codec paths */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200738static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i)
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100739{
740 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200741 p->sink->kcontrol_news[i].private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100742 unsigned int reg = mc->reg;
743 unsigned int shift = mc->shift;
744 unsigned int max = mc->max;
745 unsigned int mask = (1 << fls(max)) - 1;
746 unsigned int invert = mc->invert;
Laxminath Kasam29af16f2016-02-24 15:40:46 +0530747 unsigned int val = 0;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100748
749 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200750 soc_dapm_read(p->sink->dapm, reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100751 val = (val >> shift) & mask;
752 if (invert)
753 val = max - val;
754 p->connect = !!val;
755 } else {
756 p->connect = 0;
757 }
758}
759
Mark Brown74b8f952009-06-06 11:26:15 +0100760/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200761static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200762 struct snd_soc_dapm_path *path, const char *control_name)
763{
764 int i;
765
766 /* search for mixer kcontrol */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200767 for (i = 0; i < path->sink->num_kcontrols; i++) {
768 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
769 path->name = path->sink->kcontrol_news[i].name;
770 dapm_set_mixer_path_status(path, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200771 return 0;
772 }
773 }
774 return -ENODEV;
775}
776
Stephen Warrenaf468002011-04-28 17:38:01 -0600777static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600778 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600779 const struct snd_kcontrol_new *kcontrol_new,
780 struct snd_kcontrol **kcontrol)
781{
782 struct snd_soc_dapm_widget *w;
783 int i;
784
785 *kcontrol = NULL;
786
787 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600788 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
789 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600790 for (i = 0; i < w->num_kcontrols; i++) {
791 if (&w->kcontrol_news[i] == kcontrol_new) {
792 if (w->kcontrols)
793 *kcontrol = w->kcontrols[i];
794 return 1;
795 }
796 }
797 }
798
799 return 0;
800}
801
Stephen Warren85762e72013-03-29 15:40:10 -0600802/*
803 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
804 * create it. Either way, add the widget into the control's widget list
805 */
Jeeja KP19a2557b2015-10-20 22:30:06 +0530806static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100807 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200808{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200809 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000810 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000811 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600812 size_t prefix_len;
813 int shared;
814 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600815 bool wname_in_long_name, kcname_in_long_name;
Daniel Macke5092c92014-10-07 13:41:24 +0200816 char *long_name = NULL;
Stephen Warren85762e72013-03-29 15:40:10 -0600817 const char *name;
Daniel Macke5092c92014-10-07 13:41:24 +0200818 int ret = 0;
Mark Brownefb7ac32011-03-08 17:23:24 +0000819
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200820 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000821 if (prefix)
822 prefix_len = strlen(prefix) + 1;
823 else
824 prefix_len = 0;
825
Stephen Warren85762e72013-03-29 15:40:10 -0600826 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
827 &kcontrol);
828
Stephen Warren85762e72013-03-29 15:40:10 -0600829 if (!kcontrol) {
830 if (shared) {
831 wname_in_long_name = false;
832 kcname_in_long_name = true;
833 } else {
834 switch (w->id) {
835 case snd_soc_dapm_switch:
836 case snd_soc_dapm_mixer:
Jeeja KP19a2557b2015-10-20 22:30:06 +0530837 case snd_soc_dapm_pga:
Chen-Yu Tsaia3930ed2016-08-27 19:28:00 +0800838 case snd_soc_dapm_out_drv:
Stephen Warren85762e72013-03-29 15:40:10 -0600839 wname_in_long_name = true;
840 kcname_in_long_name = true;
841 break;
842 case snd_soc_dapm_mixer_named_ctl:
843 wname_in_long_name = false;
844 kcname_in_long_name = true;
845 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200846 case snd_soc_dapm_demux:
Stephen Warren85762e72013-03-29 15:40:10 -0600847 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600848 wname_in_long_name = true;
849 kcname_in_long_name = false;
850 break;
851 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600852 return -EINVAL;
853 }
854 }
855
856 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600857 /*
858 * The control will get a prefix from the control
859 * creation process but we're also using the same
860 * prefix for widgets so cut the prefix off the
861 * front of the widget name.
862 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200863 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600864 w->name + prefix_len,
865 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200866 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200867 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600868
869 name = long_name;
870 } else if (wname_in_long_name) {
871 long_name = NULL;
872 name = w->name + prefix_len;
873 } else {
874 long_name = NULL;
875 name = w->kcontrol_news[kci].name;
876 }
877
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200878 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600879 prefix);
Daniel Macke5092c92014-10-07 13:41:24 +0200880 if (!kcontrol) {
881 ret = -ENOMEM;
882 goto exit_free;
883 }
884
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200885 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200886
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100887 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200888 if (ret) {
889 snd_ctl_free_one(kcontrol);
Daniel Macke5092c92014-10-07 13:41:24 +0200890 goto exit_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200891 }
892
Stephen Warren85762e72013-03-29 15:40:10 -0600893 ret = snd_ctl_add(card, kcontrol);
894 if (ret < 0) {
895 dev_err(dapm->dev,
896 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
897 w->name, name, ret);
Daniel Macke5092c92014-10-07 13:41:24 +0200898 goto exit_free;
Stephen Warren85762e72013-03-29 15:40:10 -0600899 }
Stephen Warren85762e72013-03-29 15:40:10 -0600900 }
901
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200902 ret = dapm_kcontrol_add_widget(kcontrol, w);
Daniel Macke5092c92014-10-07 13:41:24 +0200903 if (ret == 0)
904 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200905
Daniel Macke5092c92014-10-07 13:41:24 +0200906exit_free:
907 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600908
Daniel Macke5092c92014-10-07 13:41:24 +0200909 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600910}
911
912/* create new dapm mixer control */
913static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
914{
915 int i, ret;
916 struct snd_soc_dapm_path *path;
Charles Keepax561ed682015-05-01 12:37:26 +0100917 struct dapm_kcontrol_data *data;
Stephen Warren85762e72013-03-29 15:40:10 -0600918
Richard Purdie2b97eab2006-10-06 18:32:18 +0200919 /* add kcontrol */
920 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200921 /* match name */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +0200922 snd_soc_dapm_widget_for_each_source_path(w, path) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200923 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600924 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200925 continue;
926
Charles Keepax561ed682015-05-01 12:37:26 +0100927 if (!w->kcontrols[i]) {
Jeeja KP19a2557b2015-10-20 22:30:06 +0530928 ret = dapm_create_or_share_kcontrol(w, i);
Charles Keepax561ed682015-05-01 12:37:26 +0100929 if (ret < 0)
930 return ret;
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200931 }
932
Mark Brown946d92a2013-08-12 23:28:42 +0100933 dapm_kcontrol_add_path(w->kcontrols[i], path);
Charles Keepax561ed682015-05-01 12:37:26 +0100934
935 data = snd_kcontrol_chip(w->kcontrols[i]);
936 if (data->widget)
937 snd_soc_dapm_add_path(data->widget->dapm,
938 data->widget,
939 path->source,
940 NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200941 }
942 }
Stephen Warren85762e72013-03-29 15:40:10 -0600943
944 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200945}
946
947/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200948static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200949{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200950 struct snd_soc_dapm_context *dapm = w->dapm;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200951 enum snd_soc_dapm_direction dir;
Stephen Warren85762e72013-03-29 15:40:10 -0600952 struct snd_soc_dapm_path *path;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200953 const char *type;
Stephen Warrenaf468002011-04-28 17:38:01 -0600954 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200955
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200956 switch (w->id) {
957 case snd_soc_dapm_mux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200958 dir = SND_SOC_DAPM_DIR_OUT;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200959 type = "mux";
960 break;
961 case snd_soc_dapm_demux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200962 dir = SND_SOC_DAPM_DIR_IN;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200963 type = "demux";
964 break;
965 default:
966 return -EINVAL;
967 }
968
Stephen Warrenaf468002011-04-28 17:38:01 -0600969 if (w->num_kcontrols != 1) {
970 dev_err(dapm->dev,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200971 "ASoC: %s %s has incorrect number of controls\n", type,
Stephen Warrenaf468002011-04-28 17:38:01 -0600972 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200973 return -EINVAL;
974 }
975
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200976 if (list_empty(&w->edges[dir])) {
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200977 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
Stephen Warren85762e72013-03-29 15:40:10 -0600978 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600979 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200980
Jeeja KP19a2557b2015-10-20 22:30:06 +0530981 ret = dapm_create_or_share_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600982 if (ret < 0)
983 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600984
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200985 snd_soc_dapm_widget_for_each_path(w, dir, path) {
986 if (path->name)
987 dapm_kcontrol_add_path(w->kcontrols[0], path);
Lars-Peter Clausen98407ef2014-10-25 17:41:57 +0200988 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200989
Stephen Warrenaf468002011-04-28 17:38:01 -0600990 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200991}
992
993/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200994static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200995{
Jeeja KP19a2557b2015-10-20 22:30:06 +0530996 int i, ret;
997
998 for (i = 0; i < w->num_kcontrols; i++) {
999 ret = dapm_create_or_share_kcontrol(w, i);
1000 if (ret < 0)
1001 return ret;
1002 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001003
Mark Browna6c65732010-03-03 17:45:21 +00001004 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001005}
1006
Nikesh Oswalc6615082015-02-02 17:06:44 +00001007/* create new dapm dai link control */
1008static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1009{
1010 int i, ret;
1011 struct snd_kcontrol *kcontrol;
1012 struct snd_soc_dapm_context *dapm = w->dapm;
1013 struct snd_card *card = dapm->card->snd_card;
1014
1015 /* create control for links with > 1 config */
1016 if (w->num_params <= 1)
1017 return 0;
1018
1019 /* add kcontrol */
1020 for (i = 0; i < w->num_kcontrols; i++) {
1021 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1022 w->name, NULL);
1023 ret = snd_ctl_add(card, kcontrol);
1024 if (ret < 0) {
1025 dev_err(dapm->dev,
1026 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1027 w->name, w->kcontrol_news[i].name, ret);
1028 return ret;
1029 }
1030 kcontrol->private_data = w;
1031 w->kcontrols[i] = kcontrol;
1032 }
1033
1034 return 0;
1035}
1036
Mark Brown99497882010-05-07 20:24:05 +01001037/* We implement power down on suspend by checking the power state of
1038 * the ALSA card - when we are suspending the ALSA state for the card
1039 * is set to D3.
1040 */
1041static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1042{
Mark Brown12ea2c72011-03-02 18:17:32 +00001043 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +01001044
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001045 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +01001046 case SNDRV_CTL_POWER_D3hot:
1047 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +01001048 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001049 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001050 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +01001051 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +01001052 default:
1053 return 1;
1054 }
1055}
1056
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001057static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1058 struct list_head *widgets)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001059{
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001060 struct snd_soc_dapm_widget *w;
1061 struct list_head *it;
1062 unsigned int size = 0;
1063 unsigned int i = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001064
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001065 list_for_each(it, widgets)
1066 size++;
1067
1068 *list = kzalloc(sizeof(**list) + size * sizeof(*w), GFP_KERNEL);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001069 if (*list == NULL)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001070 return -ENOMEM;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001071
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001072 list_for_each_entry(w, widgets, work_list)
1073 (*list)->widgets[i++] = w;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001074
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001075 (*list)->num_widgets = i;
1076
1077 return 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001078}
1079
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001080/*
1081 * Common implementation for is_connected_output_ep() and
1082 * is_connected_input_ep(). The function is inlined since the combined size of
1083 * the two specialized functions is only marginally larger then the size of the
1084 * generic function and at the same time the fast path of the specialized
1085 * functions is significantly smaller than the generic function.
1086 */
1087static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1088 struct list_head *list, enum snd_soc_dapm_direction dir,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001089 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1090 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1091 enum snd_soc_dapm_direction)),
1092 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1093 enum snd_soc_dapm_direction))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001094{
1095 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001096 struct snd_soc_dapm_path *path;
1097 int con = 0;
1098
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001099 if (widget->endpoints[dir] >= 0)
1100 return widget->endpoints[dir];
Mark Brown024dc072011-10-09 11:52:05 +01001101
Mark Brownde02d072011-09-20 21:43:24 +01001102 DAPM_UPDATE_STAT(widget, path_checks);
1103
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001104 /* do we need to add this widget to the list ? */
1105 if (list)
1106 list_add_tail(&widget->work_list, list);
1107
Jeeja KP09464972016-06-15 11:16:55 +05301108 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
Charles Keepax156a4342019-07-18 09:43:33 +01001109 list = NULL;
1110 custom_stop_condition = NULL;
Jeeja KP09464972016-06-15 11:16:55 +05301111 }
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001112
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001113 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1114 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1115 return widget->endpoints[dir];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001116 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001117
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001118 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
Mark Browne56235e2011-09-21 18:19:14 +01001119 DAPM_UPDATE_STAT(widget, neighbour_checks);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001120
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001121 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +01001122 continue;
1123
Mark Brown8af294b2013-02-22 17:48:15 +00001124 if (path->walking)
1125 return 1;
1126
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001127 trace_snd_soc_dapm_path(widget, dir, path);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001128
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001129 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +00001130 path->walking = 1;
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001131 con += fn(path->node[dir], list, custom_stop_condition);
Mark Brown8af294b2013-02-22 17:48:15 +00001132 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001133 }
1134 }
1135
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001136 widget->endpoints[dir] = con;
Mark Brown024dc072011-10-09 11:52:05 +01001137
Richard Purdie2b97eab2006-10-06 18:32:18 +02001138 return con;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001139}
1140
1141/*
1142 * Recursively check for a completed path to an active or physically connected
1143 * output widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001144 *
1145 * Optionally, can be supplied with a function acting as a stopping condition.
1146 * This function takes the dapm widget currently being examined and the walk
Charles Keepax156a4342019-07-18 09:43:33 +01001147 * direction as an arguments, it should return true if widgets from that point
1148 * in the graph onwards should not be added to the widget list.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001149 */
1150static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001151 struct list_head *list,
1152 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1153 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001154{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001155 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001156 is_connected_output_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001157}
1158
1159/*
1160 * Recursively check for a completed path to an active or physically connected
1161 * input widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001162 *
1163 * Optionally, can be supplied with a function acting as a stopping condition.
1164 * This function takes the dapm widget currently being examined and the walk
1165 * direction as an arguments, it should return true if the walk should be
1166 * stopped and false otherwise.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001167 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001168static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001169 struct list_head *list,
1170 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1171 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001172{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001173 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001174 is_connected_input_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001175}
1176
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001177/**
1178 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1179 * @dai: the soc DAI.
1180 * @stream: stream direction.
1181 * @list: list of active widgets for this stream.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001182 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1183 * walk based on custom logic.
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001184 *
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001185 * Queries DAPM graph as to whether a valid audio stream path exists for
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001186 * the initial stream specified by name. This takes into account
1187 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1188 *
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001189 * Optionally, can be supplied with a function acting as a stopping condition.
1190 * This function takes the dapm widget currently being examined and the walk
1191 * direction as an arguments, it should return true if the walk should be
1192 * stopped and false otherwise.
1193 *
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001194 * Returns the number of valid paths or negative error.
1195 */
1196int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001197 struct snd_soc_dapm_widget_list **list,
1198 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1199 enum snd_soc_dapm_direction))
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001200{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01001201 struct snd_soc_card *card = dai->component->card;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001202 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001203 LIST_HEAD(widgets);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001204 int paths;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001205 int ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001206
1207 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001208
1209 /*
1210 * For is_connected_{output,input}_ep fully discover the graph we need
1211 * to reset the cached number of inputs and outputs.
1212 */
1213 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001214 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1215 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001216 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001217
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001218 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001219 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1220 custom_stop_condition);
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001221 else
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001222 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1223 custom_stop_condition);
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001224
1225 /* Drop starting point */
1226 list_del(widgets.next);
1227
1228 ret = dapm_widget_list_create(list, &widgets);
1229 if (ret)
Lars-Peter Clausen30abbe72015-08-11 21:37:59 +02001230 paths = ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001231
1232 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001233 mutex_unlock(&card->dapm_mutex);
1234
1235 return paths;
1236}
1237
Richard Purdie2b97eab2006-10-06 18:32:18 +02001238/*
Mark Brown62ea8742012-01-21 21:14:48 +00001239 * Handler for regulator supply widget.
1240 */
1241int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1242 struct snd_kcontrol *kcontrol, int event)
1243{
Mark Brownc05b84d2012-09-07 12:57:11 +08001244 int ret;
1245
Mark Browneb270e92013-10-09 13:52:52 +01001246 soc_dapm_async_complete(w->dapm);
1247
Mark Brownc05b84d2012-09-07 12:57:11 +08001248 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001249 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001250 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001251 if (ret != 0)
1252 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001253 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001254 w->name, ret);
1255 }
1256
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001257 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001258 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001259 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001260 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001261 if (ret != 0)
1262 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001263 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001264 w->name, ret);
1265 }
1266
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001267 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001268 }
Mark Brown62ea8742012-01-21 21:14:48 +00001269}
1270EXPORT_SYMBOL_GPL(dapm_regulator_event);
1271
Ola Liljad7e7eb92012-05-24 15:26:25 +02001272/*
1273 * Handler for clock supply widget.
1274 */
1275int dapm_clock_event(struct snd_soc_dapm_widget *w,
1276 struct snd_kcontrol *kcontrol, int event)
1277{
1278 if (!w->clk)
1279 return -EIO;
1280
Mark Browneb270e92013-10-09 13:52:52 +01001281 soc_dapm_async_complete(w->dapm);
1282
Mark Brownec029952012-06-04 08:16:20 +01001283#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001284 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001285 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001286 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001287 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001288 return 0;
1289 }
Mark Brownec029952012-06-04 08:16:20 +01001290#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001291 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001292}
1293EXPORT_SYMBOL_GPL(dapm_clock_event);
1294
Mark Brownd8050022011-09-28 18:28:23 +01001295static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1296{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001297 if (w->power_checked)
1298 return w->new_power;
1299
Mark Brownd8050022011-09-28 18:28:23 +01001300 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001301 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001302 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001303 w->new_power = w->power_check(w);
1304
1305 w->power_checked = true;
1306
1307 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001308}
1309
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001310/* Generic check to see if a widget should be powered. */
Mark Browncd0f2d42009-04-20 16:56:59 +01001311static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1312{
1313 int in, out;
1314
Mark Brownde02d072011-09-20 21:43:24 +01001315 DAPM_UPDATE_STAT(w, power_checks);
1316
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001317 in = is_connected_input_ep(w, NULL, NULL);
1318 out = is_connected_output_ep(w, NULL, NULL);
Mark Browncd0f2d42009-04-20 16:56:59 +01001319 return out != 0 && in != 0;
1320}
1321
Mark Brown246d0a12009-04-22 18:24:55 +01001322/* Check to see if a power supply is needed */
1323static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1324{
1325 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001326
Mark Brownde02d072011-09-20 21:43:24 +01001327 DAPM_UPDATE_STAT(w, power_checks);
1328
Mark Brown246d0a12009-04-22 18:24:55 +01001329 /* Check if one of our outputs is connected */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001330 snd_soc_dapm_widget_for_each_sink_path(w, path) {
Mark Browna8fdac82011-09-28 18:20:26 +01001331 DAPM_UPDATE_STAT(w, neighbour_checks);
1332
Mark Brownbf3a9e12011-06-13 16:42:29 +01001333 if (path->weak)
1334 continue;
1335
Mark Brown215edda2009-09-08 18:59:05 +01001336 if (path->connected &&
1337 !path->connected(path->source, path->sink))
1338 continue;
1339
Mark Brownf68d7e12011-10-04 22:57:50 +01001340 if (dapm_widget_power_check(path->sink))
1341 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001342 }
1343
Mark Brownf68d7e12011-10-04 22:57:50 +01001344 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001345}
1346
Mark Brown35c64bc2011-09-28 18:23:53 +01001347static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1348{
Charles Keepax20bb0182015-12-02 10:22:16 +00001349 return w->connected;
Mark Brown35c64bc2011-09-28 18:23:53 +01001350}
1351
Mark Brown38357ab2009-06-06 19:03:23 +01001352static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1353 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001354 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001355{
Mark Brown828a8422011-01-15 13:14:30 +00001356 int *sort;
1357
1358 if (power_up)
1359 sort = dapm_up_seq;
1360 else
1361 sort = dapm_down_seq;
1362
Mark Brown38357ab2009-06-06 19:03:23 +01001363 if (sort[a->id] != sort[b->id])
1364 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001365 if (a->subseq != b->subseq) {
1366 if (power_up)
1367 return a->subseq - b->subseq;
1368 else
1369 return b->subseq - a->subseq;
1370 }
Mark Brownb22ead22009-06-07 12:51:26 +01001371 if (a->reg != b->reg)
1372 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001373 if (a->dapm != b->dapm)
1374 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001375
Mark Brown38357ab2009-06-06 19:03:23 +01001376 return 0;
1377}
Mark Brown42aa3412009-03-01 19:21:10 +00001378
Mark Brown38357ab2009-06-06 19:03:23 +01001379/* Insert a widget in order into a DAPM power sequence. */
1380static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1381 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001382 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001383{
1384 struct snd_soc_dapm_widget *w;
1385
1386 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001387 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001388 list_add_tail(&new_widget->power_list, &w->power_list);
1389 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001390 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001391
Mark Brown38357ab2009-06-06 19:03:23 +01001392 list_add_tail(&new_widget->power_list, list);
1393}
Mark Brown42aa3412009-03-01 19:21:10 +00001394
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001395static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001396 struct snd_soc_dapm_widget *w, int event)
1397{
Mark Brown68f89ad2010-11-03 23:51:49 -04001398 const char *ev_name;
1399 int power, ret;
1400
1401 switch (event) {
1402 case SND_SOC_DAPM_PRE_PMU:
1403 ev_name = "PRE_PMU";
1404 power = 1;
1405 break;
1406 case SND_SOC_DAPM_POST_PMU:
1407 ev_name = "POST_PMU";
1408 power = 1;
1409 break;
1410 case SND_SOC_DAPM_PRE_PMD:
1411 ev_name = "PRE_PMD";
1412 power = 0;
1413 break;
1414 case SND_SOC_DAPM_POST_PMD:
1415 ev_name = "POST_PMD";
1416 power = 0;
1417 break;
Mark Brown80114122013-02-25 15:14:19 +00001418 case SND_SOC_DAPM_WILL_PMU:
1419 ev_name = "WILL_PMU";
1420 power = 1;
1421 break;
1422 case SND_SOC_DAPM_WILL_PMD:
1423 ev_name = "WILL_PMD";
1424 power = 0;
1425 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001426 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001427 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001428 return;
1429 }
1430
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001431 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001432 return;
1433
1434 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001435 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001436 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001437 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001438 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001439 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001440 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001441 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001442 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001443 ev_name, w->name, ret);
1444 }
1445}
1446
Mark Brownb22ead22009-06-07 12:51:26 +01001447/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001448static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001449 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001450{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001451 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001452 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001453 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001454 unsigned int value = 0;
1455 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001456
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001457 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1458 reg = w->reg;
1459 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001460
1461 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001462 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001463 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001464
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001465 mask |= w->mask << w->shift;
1466 if (w->power)
1467 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001468 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001469 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001470
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001471 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001472 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1473 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001474
Mark Brown68f89ad2010-11-03 23:51:49 -04001475 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001476 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1477 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001478 }
1479
Mark Brown81628102009-06-07 13:21:24 +01001480 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001481 /* Any widget will do, they should all be updating the
1482 * same register.
1483 */
Mark Brown29376bc2011-06-19 13:49:28 +01001484
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001485 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001486 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001487 value, mask, reg, card->pop_time);
1488 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001489 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001490 }
1491
1492 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001493 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1494 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001495 }
Mark Brown42aa3412009-03-01 19:21:10 +00001496}
1497
Mark Brownb22ead22009-06-07 12:51:26 +01001498/* Apply a DAPM power sequence.
1499 *
1500 * We walk over a pre-sorted list of widgets to apply power to. In
1501 * order to minimise the number of writes to the device required
1502 * multiple widgets will be updated in a single write where possible.
1503 * Currently anything that requires more than a single write is not
1504 * handled.
1505 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001506static void dapm_seq_run(struct snd_soc_card *card,
1507 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001508{
1509 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001510 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001511 LIST_HEAD(pending);
1512 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001513 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001514 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001515 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001516 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001517 int *sort;
1518
1519 if (power_up)
1520 sort = dapm_up_seq;
1521 else
1522 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001523
Mark Brownb22ead22009-06-07 12:51:26 +01001524 list_for_each_entry_safe(w, n, list, power_list) {
1525 ret = 0;
1526
1527 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001528 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001529 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Banajit Goswami853ed7c2015-04-10 16:50:43 -07001530 if (cur_dapm && !list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001531 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001532
Mark Brown474b62d2011-01-18 16:14:44 +00001533 if (cur_dapm && cur_dapm->seq_notifier) {
1534 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1535 if (sort[i] == cur_sort)
1536 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001537 i,
1538 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001539 }
1540
Mark Browneb270e92013-10-09 13:52:52 +01001541 if (cur_dapm && w->dapm != cur_dapm)
1542 soc_dapm_async_complete(cur_dapm);
1543
Mark Brownb22ead22009-06-07 12:51:26 +01001544 INIT_LIST_HEAD(&pending);
1545 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001546 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001547 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001548 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001549 }
1550
Mark Brown163cac02009-06-07 10:12:52 +01001551 switch (w->id) {
1552 case snd_soc_dapm_pre:
1553 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001554 list_for_each_entry_safe_continue(w, n, list,
1555 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001556
Mark Brownb22ead22009-06-07 12:51:26 +01001557 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001558 ret = w->event(w,
1559 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001560 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001561 ret = w->event(w,
1562 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001563 break;
1564
1565 case snd_soc_dapm_post:
1566 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001567 list_for_each_entry_safe_continue(w, n, list,
1568 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001569
Mark Brownb22ead22009-06-07 12:51:26 +01001570 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001571 ret = w->event(w,
1572 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001573 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001574 ret = w->event(w,
1575 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001576 break;
1577
Mark Brown163cac02009-06-07 10:12:52 +01001578 default:
Mark Brown81628102009-06-07 13:21:24 +01001579 /* Queue it up for application */
1580 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001581 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001582 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001583 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001584 list_move(&w->power_list, &pending);
1585 break;
Mark Brown163cac02009-06-07 10:12:52 +01001586 }
Mark Brownb22ead22009-06-07 12:51:26 +01001587
Banajit Goswamib2fac522013-10-10 18:17:43 -07001588 /*
1589 * Add this debug log to keep track of widgets being
1590 * powered-up and powered-down.
1591 */
1592 dev_dbg(w->dapm->dev, "dapm: powering %s widget %s\n",
1593 power_up ? "up" : "down", w->name);
1594
Mark Brownb22ead22009-06-07 12:51:26 +01001595 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001596 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001597 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001598 }
Mark Brownb22ead22009-06-07 12:51:26 +01001599
Banajit Goswami853ed7c2015-04-10 16:50:43 -07001600 if (cur_dapm && !list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001601 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001602
1603 if (cur_dapm && cur_dapm->seq_notifier) {
1604 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1605 if (sort[i] == cur_sort)
1606 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001607 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001608 }
Mark Browneb270e92013-10-09 13:52:52 +01001609
1610 list_for_each_entry(d, &card->dapm_list, list) {
1611 soc_dapm_async_complete(d);
1612 }
Mark Brown163cac02009-06-07 10:12:52 +01001613}
1614
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001615static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001616{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001617 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001618 struct snd_soc_dapm_widget_list *wlist;
1619 struct snd_soc_dapm_widget *w = NULL;
1620 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001621 int ret;
1622
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001623 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001624 return;
1625
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001626 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001627
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001628 for (wi = 0; wi < wlist->num_widgets; wi++) {
1629 w = wlist->widgets[wi];
1630
1631 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1632 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1633 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001634 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001635 w->name, ret);
1636 }
Mark Brown97404f22010-12-14 16:13:57 +00001637 }
1638
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001639 if (!w)
1640 return;
1641
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001642 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1643 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001644 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001645 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001646 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001647
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001648 for (wi = 0; wi < wlist->num_widgets; wi++) {
1649 w = wlist->widgets[wi];
1650
1651 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1652 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1653 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001654 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001655 w->name, ret);
1656 }
Mark Brown97404f22010-12-14 16:13:57 +00001657 }
1658}
1659
Mark Brown9d0624a2011-02-18 11:49:43 -08001660/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1661 * they're changing state.
1662 */
1663static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1664{
1665 struct snd_soc_dapm_context *d = data;
1666 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001667
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001668 /* If we're off and we're not supposed to go into STANDBY */
Mark Brown56fba412011-06-04 11:25:10 +01001669 if (d->bias_level == SND_SOC_BIAS_OFF &&
1670 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001671 if (d->dev)
1672 pm_runtime_get_sync(d->dev);
1673
Mark Brown9d0624a2011-02-18 11:49:43 -08001674 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1675 if (ret != 0)
1676 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001677 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001678 }
1679
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001680 /* Prepare for a transition to ON or away from ON */
1681 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1682 d->bias_level != SND_SOC_BIAS_ON) ||
1683 (d->target_bias_level != SND_SOC_BIAS_ON &&
1684 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001685 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1686 if (ret != 0)
1687 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001688 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001689 }
1690}
1691
1692/* Async callback run prior to DAPM sequences - brings to their final
1693 * state.
1694 */
1695static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1696{
1697 struct snd_soc_dapm_context *d = data;
1698 int ret;
1699
1700 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001701 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1702 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1703 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001704 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1705 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001706 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001707 ret);
1708 }
1709
1710 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001711 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1712 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001713 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1714 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001715 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1716 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001717
1718 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001719 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001720 }
1721
1722 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001723 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1724 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001725 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1726 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001727 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001728 ret);
1729 }
1730}
Mark Brown97404f22010-12-14 16:13:57 +00001731
Mark Brownfe4fda52011-10-03 22:36:57 +01001732static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1733 bool power, bool connect)
1734{
1735 /* If a connection is being made or broken then that update
1736 * will have marked the peer dirty, otherwise the widgets are
1737 * not connected and this update has no impact. */
1738 if (!connect)
1739 return;
1740
1741 /* If the peer is already in the state we're moving to then we
1742 * won't have an impact on it. */
1743 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001744 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001745}
1746
Mark Brown05623c42011-09-28 17:02:31 +01001747static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1748 struct list_head *up_list,
1749 struct list_head *down_list)
1750{
Mark Browndb432b42011-10-03 21:06:40 +01001751 struct snd_soc_dapm_path *path;
1752
Mark Brown05623c42011-09-28 17:02:31 +01001753 if (w->power == power)
1754 return;
1755
1756 trace_snd_soc_dapm_widget_power(w, power);
1757
Mark Browndb432b42011-10-03 21:06:40 +01001758 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001759 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001760 */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001761 snd_soc_dapm_widget_for_each_source_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001762 dapm_widget_set_peer_power(path->source, power, path->connect);
1763
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001764 /* Supplies can't affect their outputs, only their inputs */
1765 if (!w->is_supply) {
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001766 snd_soc_dapm_widget_for_each_sink_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001767 dapm_widget_set_peer_power(path->sink, power,
1768 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001769 }
1770
Mark Brown05623c42011-09-28 17:02:31 +01001771 if (power)
1772 dapm_seq_insert(w, up_list, true);
1773 else
1774 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001775}
1776
Mark Brown7c81beb2011-09-20 22:22:32 +01001777static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1778 struct list_head *up_list,
1779 struct list_head *down_list)
1780{
Mark Brown7c81beb2011-09-20 22:22:32 +01001781 int power;
1782
1783 switch (w->id) {
1784 case snd_soc_dapm_pre:
1785 dapm_seq_insert(w, down_list, false);
1786 break;
1787 case snd_soc_dapm_post:
1788 dapm_seq_insert(w, up_list, true);
1789 break;
1790
1791 default:
Mark Brownd8050022011-09-28 18:28:23 +01001792 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001793
Mark Brown05623c42011-09-28 17:02:31 +01001794 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001795 break;
1796 }
1797}
1798
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001799static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1800{
1801 if (dapm->idle_bias_off)
1802 return true;
1803
1804 switch (snd_power_get_state(dapm->card->snd_card)) {
1805 case SNDRV_CTL_POWER_D3hot:
1806 case SNDRV_CTL_POWER_D3cold:
1807 return dapm->suspend_bias_off;
1808 default:
1809 break;
1810 }
1811
1812 return false;
1813}
1814
Mark Brown42aa3412009-03-01 19:21:10 +00001815/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001816 * Scan each dapm widget for complete audio path.
1817 * A complete path is a route that has valid endpoints i.e.:-
1818 *
1819 * o DAC to output pin.
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001820 * o Input pin to ADC.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001821 * o Input pin to Output pin (bypass, sidetone)
1822 * o DAC to ADC (loopback).
1823 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001824static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001825{
1826 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001827 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001828 LIST_HEAD(up_list);
1829 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001830 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001831 enum snd_soc_bias_level bias;
Banajit Goswami53ae6622015-04-10 18:19:16 -07001832 struct snd_soc_platform *p;
1833 struct snd_soc_codec *c;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001834
Mark Brownf9fa2b12014-03-06 16:49:11 +08001835 lockdep_assert_held(&card->dapm_mutex);
1836
Mark Brown84e90932010-11-04 00:07:02 -04001837 trace_snd_soc_dapm_start(card);
Banajit Goswami853ed7c2015-04-10 16:50:43 -07001838 mutex_lock(&card->dapm_power_mutex);
Mark Brown84e90932010-11-04 00:07:02 -04001839
Mark Brown56fba412011-06-04 11:25:10 +01001840 list_for_each_entry(d, &card->dapm_list, list) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001841 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001842 d->target_bias_level = SND_SOC_BIAS_OFF;
1843 else
1844 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001845 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001846
Liam Girdwood6c120e12012-02-15 15:15:34 +00001847 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001848
Mark Brown6d3ddc82009-05-16 17:47:29 +01001849 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001850 * lists indicating if they should be powered up or down. We
1851 * only check widgets that have been flagged as dirty but note
1852 * that new widgets may be added to the dirty list while we
1853 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001854 */
Mark Browndb432b42011-10-03 21:06:40 +01001855 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001856 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001857 }
1858
Mark Brownf9de6d72011-09-28 17:19:47 +01001859 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001860 switch (w->id) {
1861 case snd_soc_dapm_pre:
1862 case snd_soc_dapm_post:
1863 /* These widgets always need to be powered */
1864 break;
1865 default:
1866 list_del_init(&w->dirty);
1867 break;
1868 }
Mark Browndb432b42011-10-03 21:06:40 +01001869
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001870 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001871 d = w->dapm;
1872
1873 /* Supplies and micbiases only bring the
1874 * context up to STANDBY as unless something
1875 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001876 * generally don't require full power. Signal
1877 * generators are virtual pins and have no
1878 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001879 */
1880 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001881 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001882 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001883 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001884 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001885 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001886 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001887 case snd_soc_dapm_micbias:
1888 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1889 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1890 break;
1891 default:
1892 d->target_bias_level = SND_SOC_BIAS_ON;
1893 break;
1894 }
1895 }
1896
1897 }
1898
Mark Brown85a843c2011-09-21 21:29:47 +01001899 /* Force all contexts in the card to the same bias state if
1900 * they're not ground referenced.
1901 */
Mark Brown56fba412011-06-04 11:25:10 +01001902 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001903 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001904 if (d->target_bias_level > bias)
1905 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001906 list_for_each_entry(d, &card->dapm_list, list)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001907 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01001908 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001909
Mark Brownde02d072011-09-20 21:43:24 +01001910 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001911
Xiang Xiao17282ba2014-03-02 00:04:03 +08001912 /* Run card bias changes at first */
1913 dapm_pre_sequence_async(&card->dapm, 0);
1914 /* Run other bias changes in parallel */
1915 list_for_each_entry(d, &card->dapm_list, list) {
Banajit Goswami53ae6622015-04-10 18:19:16 -07001916 p = snd_soc_dapm_to_platform(d);
1917 c = snd_soc_dapm_to_codec(d);
1918 if ((d != &card->dapm) && (c || p))
Xiang Xiao17282ba2014-03-02 00:04:03 +08001919 async_schedule_domain(dapm_pre_sequence_async, d,
1920 &async_domain);
1921 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001922 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001923
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001924 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001925 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001926 }
1927
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001928 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001929 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001930 }
1931
Mark Brown6d3ddc82009-05-16 17:47:29 +01001932 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001933 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001934
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001935 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001936
Mark Brown6d3ddc82009-05-16 17:47:29 +01001937 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001938 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001939
Mark Brown9d0624a2011-02-18 11:49:43 -08001940 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001941 list_for_each_entry(d, &card->dapm_list, list) {
Banajit Goswami53ae6622015-04-10 18:19:16 -07001942 p = snd_soc_dapm_to_platform(d);
1943 c = snd_soc_dapm_to_codec(d);
1944 if ((d != &card->dapm) && (c || p))
Xiang Xiao17282ba2014-03-02 00:04:03 +08001945 async_schedule_domain(dapm_post_sequence_async, d,
1946 &async_domain);
1947 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001948 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001949 /* Run card bias changes at last */
1950 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001951
Liam Girdwood8078d872012-02-15 15:15:35 +00001952 /* do we need to notify any clients that DAPM event is complete */
1953 list_for_each_entry(d, &card->dapm_list, list) {
1954 if (d->stream_event)
1955 d->stream_event(d, event);
1956 }
1957
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001958 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001959 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001960 pop_wait(card->pop_time);
Banajit Goswami853ed7c2015-04-10 16:50:43 -07001961 mutex_unlock(&card->dapm_power_mutex);
Mark Browncb507e72009-07-08 18:54:57 +01001962
Mark Brown84e90932010-11-04 00:07:02 -04001963 trace_snd_soc_dapm_done(card);
1964
Mark Brown42aa3412009-03-01 19:21:10 +00001965 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001966}
1967
Mark Brown79fb9382009-08-21 16:38:13 +01001968#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001969static ssize_t dapm_widget_power_read_file(struct file *file,
1970 char __user *user_buf,
1971 size_t count, loff_t *ppos)
1972{
1973 struct snd_soc_dapm_widget *w = file->private_data;
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02001974 struct snd_soc_card *card = w->dapm->card;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001975 enum snd_soc_dapm_direction dir, rdir;
Mark Brown79fb9382009-08-21 16:38:13 +01001976 char *buf;
1977 int in, out;
1978 ssize_t ret;
1979 struct snd_soc_dapm_path *p = NULL;
1980
1981 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1982 if (!buf)
1983 return -ENOMEM;
1984
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02001985 mutex_lock(&card->dapm_mutex);
1986
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001987 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
1988 if (w->is_supply) {
1989 in = 0;
1990 out = 0;
1991 } else {
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001992 in = is_connected_input_ep(w, NULL, NULL);
1993 out = is_connected_output_ep(w, NULL, NULL);
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001994 }
Mark Brown79fb9382009-08-21 16:38:13 +01001995
Silvio Cesare91890492019-01-12 16:28:43 +01001996 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
Mark Brownf13ebad2012-03-03 18:01:01 +00001997 w->name, w->power ? "On" : "Off",
1998 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001999
Mark Brownd033c362009-12-04 15:25:56 +00002000 if (w->reg >= 0)
Silvio Cesare91890492019-01-12 16:28:43 +01002001 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002002 " - R%d(0x%x) mask 0x%x",
2003 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00002004
Silvio Cesare91890492019-01-12 16:28:43 +01002005 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
Mark Brownd033c362009-12-04 15:25:56 +00002006
Mark Brown3eef08b2009-09-14 16:49:00 +01002007 if (w->sname)
Silvio Cesare91890492019-01-12 16:28:43 +01002008 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
Mark Brown3eef08b2009-09-14 16:49:00 +01002009 w->sname,
2010 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01002011
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002012 snd_soc_dapm_for_each_direction(dir) {
2013 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2014 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2015 if (p->connected && !p->connected(w, p->node[rdir]))
2016 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002017
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002018 if (!p->connect)
2019 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002020
Silvio Cesare91890492019-01-12 16:28:43 +01002021 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002022 " %s \"%s\" \"%s\"\n",
2023 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
Mark Brown79fb9382009-08-21 16:38:13 +01002024 p->name ? p->name : "static",
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002025 p->node[rdir]->name);
2026 }
Mark Brown79fb9382009-08-21 16:38:13 +01002027 }
2028
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002029 mutex_unlock(&card->dapm_mutex);
2030
Mark Brown79fb9382009-08-21 16:38:13 +01002031 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2032
2033 kfree(buf);
2034 return ret;
2035}
2036
2037static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002038 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002039 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002040 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002041};
2042
Mark Brownef49e4f2011-04-04 20:48:13 +09002043static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2044 size_t count, loff_t *ppos)
2045{
2046 struct snd_soc_dapm_context *dapm = file->private_data;
2047 char *level;
2048
2049 switch (dapm->bias_level) {
2050 case SND_SOC_BIAS_ON:
2051 level = "On\n";
2052 break;
2053 case SND_SOC_BIAS_PREPARE:
2054 level = "Prepare\n";
2055 break;
2056 case SND_SOC_BIAS_STANDBY:
2057 level = "Standby\n";
2058 break;
2059 case SND_SOC_BIAS_OFF:
2060 level = "Off\n";
2061 break;
2062 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002063 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002064 level = "Unknown\n";
2065 break;
2066 }
2067
2068 return simple_read_from_buffer(user_buf, count, ppos, level,
2069 strlen(level));
2070}
2071
2072static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002073 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002074 .read = dapm_bias_read_file,
2075 .llseek = default_llseek,
2076};
2077
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002078void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2079 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002080{
Mark Brown79fb9382009-08-21 16:38:13 +01002081 struct dentry *d;
2082
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02002083 if (!parent)
2084 return;
2085
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002086 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2087
2088 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002089 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002090 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002091 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002092 }
Mark Brown79fb9382009-08-21 16:38:13 +01002093
Mark Brownef49e4f2011-04-04 20:48:13 +09002094 d = debugfs_create_file("bias_level", 0444,
2095 dapm->debugfs_dapm, dapm,
2096 &dapm_bias_fops);
2097 if (!d)
2098 dev_warn(dapm->dev,
2099 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002100}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002101
2102static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2103{
2104 struct snd_soc_dapm_context *dapm = w->dapm;
2105 struct dentry *d;
2106
2107 if (!dapm->debugfs_dapm || !w->name)
2108 return;
2109
2110 d = debugfs_create_file(w->name, 0444,
2111 dapm->debugfs_dapm, w,
2112 &dapm_widget_power_fops);
2113 if (!d)
2114 dev_warn(w->dapm->dev,
2115 "ASoC: Failed to create %s debugfs file\n",
2116 w->name);
2117}
2118
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002119static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2120{
2121 debugfs_remove_recursive(dapm->debugfs_dapm);
2122}
2123
Mark Brown79fb9382009-08-21 16:38:13 +01002124#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002125void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2126 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002127{
2128}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002129
2130static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2131{
2132}
2133
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002134static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2135{
2136}
2137
Mark Brown79fb9382009-08-21 16:38:13 +01002138#endif
2139
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002140/*
2141 * soc_dapm_connect_path() - Connects or disconnects a path
2142 * @path: The path to update
2143 * @connect: The new connect state of the path. True if the path is connected,
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002144 * false if it is disconnected.
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002145 * @reason: The reason why the path changed (for debugging only)
2146 */
2147static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2148 bool connect, const char *reason)
2149{
2150 if (path->connect == connect)
2151 return;
2152
2153 path->connect = connect;
2154 dapm_mark_dirty(path->source, reason);
2155 dapm_mark_dirty(path->sink, reason);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002156 dapm_path_invalidate(path);
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002157}
2158
Richard Purdie2b97eab2006-10-06 18:32:18 +02002159/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002160static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002161 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002162{
2163 struct snd_soc_dapm_path *path;
2164 int found = 0;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002165 bool connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002166
Mark Brownf9fa2b12014-03-06 16:49:11 +08002167 lockdep_assert_held(&card->dapm_mutex);
2168
Richard Purdie2b97eab2006-10-06 18:32:18 +02002169 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002170 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002171 found = 1;
2172 /* we now need to match the string in the enum to the path */
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002173 if (!(strcmp(path->name, e->texts[mux])))
2174 connect = true;
2175 else
2176 connect = false;
2177
2178 soc_dapm_connect_path(path, connect, "mux update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002179 }
2180
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002181 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002182 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002183
Liam Girdwood618dae12012-04-25 12:12:51 +01002184 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002185}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002186
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002187int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002188 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2189 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002190{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002191 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002192 int ret;
2193
Liam Girdwood3cd04342012-03-09 12:02:08 +00002194 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002195 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002196 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002197 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002198 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002199 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002200 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002201 return ret;
2202}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002203EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002204
Milan plzik1b075e32008-01-10 14:39:46 +01002205/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002206static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002207 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002208{
2209 struct snd_soc_dapm_path *path;
2210 int found = 0;
2211
Mark Brownf9fa2b12014-03-06 16:49:11 +08002212 lockdep_assert_held(&card->dapm_mutex);
2213
Richard Purdie2b97eab2006-10-06 18:32:18 +02002214 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002215 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002216 found = 1;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002217 soc_dapm_connect_path(path, connect, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002218 }
2219
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002220 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002221 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002222
Liam Girdwood618dae12012-04-25 12:12:51 +01002223 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002224}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002225
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002226int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002227 struct snd_kcontrol *kcontrol, int connect,
2228 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002229{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002230 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002231 int ret;
2232
Liam Girdwood3cd04342012-03-09 12:02:08 +00002233 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002234 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002235 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002236 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002237 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002238 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002239 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002240 return ret;
2241}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002242EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002243
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002244static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2245 char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002246{
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002247 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002248 struct snd_soc_dapm_widget *w;
2249 int count = 0;
2250 char *state = "not set";
2251
Mark Brown47325072016-03-18 12:04:23 +00002252 /* card won't be set for the dummy component, as a spot fix
2253 * we're checking for that case specifically here but in future
2254 * we will ensure that the dummy component looks like others.
2255 */
2256 if (!cmpnt->card)
2257 return 0;
2258
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002259 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2260 if (w->dapm != dapm)
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002261 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002262
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002263 /* only display widgets that burn power */
Richard Purdie2b97eab2006-10-06 18:32:18 +02002264 switch (w->id) {
2265 case snd_soc_dapm_hp:
2266 case snd_soc_dapm_mic:
2267 case snd_soc_dapm_spk:
2268 case snd_soc_dapm_line:
2269 case snd_soc_dapm_micbias:
2270 case snd_soc_dapm_dac:
2271 case snd_soc_dapm_adc:
2272 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002273 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002274 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002275 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002276 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002277 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002278 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002279 if (w->name)
2280 count += sprintf(buf + count, "%s: %s\n",
2281 w->name, w->power ? "On":"Off");
2282 break;
2283 default:
2284 break;
2285 }
2286 }
2287
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002288 switch (snd_soc_dapm_get_bias_level(dapm)) {
Mark Brown0be98982008-05-19 12:31:28 +02002289 case SND_SOC_BIAS_ON:
2290 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002291 break;
Mark Brown0be98982008-05-19 12:31:28 +02002292 case SND_SOC_BIAS_PREPARE:
2293 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002294 break;
Mark Brown0be98982008-05-19 12:31:28 +02002295 case SND_SOC_BIAS_STANDBY:
2296 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002297 break;
Mark Brown0be98982008-05-19 12:31:28 +02002298 case SND_SOC_BIAS_OFF:
2299 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002300 break;
2301 }
2302 count += sprintf(buf + count, "PM State: %s\n", state);
2303
2304 return count;
2305}
2306
Benoit Cousson44ba2642014-07-08 23:19:36 +02002307/* show dapm widget status in sys fs */
2308static ssize_t dapm_widget_show(struct device *dev,
2309 struct device_attribute *attr, char *buf)
2310{
2311 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2312 int i, count = 0;
2313
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002314 mutex_lock(&rtd->card->dapm_mutex);
2315
Benoit Cousson44ba2642014-07-08 23:19:36 +02002316 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002317 struct snd_soc_component *cmpnt = rtd->codec_dais[i]->component;
2318
2319 count += dapm_widget_show_component(cmpnt, buf + count);
Benoit Cousson44ba2642014-07-08 23:19:36 +02002320 }
2321
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002322 mutex_unlock(&rtd->card->dapm_mutex);
2323
Benoit Cousson44ba2642014-07-08 23:19:36 +02002324 return count;
2325}
2326
Richard Purdie2b97eab2006-10-06 18:32:18 +02002327static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2328
Takashi Iwaid29697d2015-01-30 20:16:37 +01002329struct attribute *soc_dapm_dev_attrs[] = {
2330 &dev_attr_dapm_widget.attr,
2331 NULL
2332};
Richard Purdie2b97eab2006-10-06 18:32:18 +02002333
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002334static void dapm_free_path(struct snd_soc_dapm_path *path)
2335{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002336 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2337 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002338 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002339 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002340 kfree(path);
2341}
2342
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002343void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2344{
2345 struct snd_soc_dapm_path *p, *next_p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002346 enum snd_soc_dapm_direction dir;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002347
2348 list_del(&w->list);
2349 /*
2350 * remove source and sink paths associated to this widget.
2351 * While removing the path, remove reference to it from both
2352 * source and sink widgets so that path is removed only once.
2353 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002354 snd_soc_dapm_for_each_direction(dir) {
2355 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2356 dapm_free_path(p);
2357 }
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002358
2359 kfree(w->kcontrols);
Lars-Peter Clausen48068962015-07-21 18:11:08 +02002360 kfree_const(w->name);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002361 kfree(w);
2362}
2363
Jyri Sarhafd589a12015-11-10 18:12:42 +02002364void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2365{
2366 dapm->path_sink_cache.widget = NULL;
2367 dapm->path_source_cache.widget = NULL;
2368}
2369
Richard Purdie2b97eab2006-10-06 18:32:18 +02002370/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002371static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002372{
2373 struct snd_soc_dapm_widget *w, *next_w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002374
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002375 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2376 if (w->dapm != dapm)
2377 continue;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002378 snd_soc_dapm_free_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002379 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002380 snd_soc_dapm_reset_cache(dapm);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002381}
2382
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002383static struct snd_soc_dapm_widget *dapm_find_widget(
2384 struct snd_soc_dapm_context *dapm, const char *pin,
2385 bool search_other_contexts)
2386{
2387 struct snd_soc_dapm_widget *w;
2388 struct snd_soc_dapm_widget *fallback = NULL;
2389
2390 list_for_each_entry(w, &dapm->card->widgets, list) {
2391 if (!strcmp(w->name, pin)) {
2392 if (w->dapm == dapm)
2393 return w;
2394 else
2395 fallback = w;
2396 }
2397 }
2398
2399 if (search_other_contexts)
2400 return fallback;
2401
2402 return NULL;
2403}
2404
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002405static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002406 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002407{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002408 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002409
Mark Brownf9fa2b12014-03-06 16:49:11 +08002410 dapm_assert_locked(dapm);
2411
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002412 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002413 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002414 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002415 }
2416
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002417 if (w->connected != status) {
Mark Brown1a8b2d92012-02-16 11:50:07 -08002418 dapm_mark_dirty(w, "pin configuration");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002419 dapm_widget_invalidate_input_paths(w);
2420 dapm_widget_invalidate_output_paths(w);
2421 }
Mark Brown1a8b2d92012-02-16 11:50:07 -08002422
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002423 w->connected = status;
2424 if (status == 0)
2425 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002426
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002427 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002428}
2429
Richard Purdie2b97eab2006-10-06 18:32:18 +02002430/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002431 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2432 * @dapm: DAPM context
2433 *
2434 * Walks all dapm audio paths and powers widgets according to their
2435 * stream or path usage.
2436 *
2437 * Requires external locking.
2438 *
2439 * Returns 0 for success.
2440 */
2441int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2442{
2443 /*
2444 * Suppress early reports (eg, jacks syncing their state) to avoid
2445 * silly DAPM runs during card startup.
2446 */
2447 if (!dapm->card || !dapm->card->instantiated)
2448 return 0;
2449
2450 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2451}
2452EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2453
2454/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002455 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002456 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002457 *
2458 * Walks all dapm audio paths and powers widgets according to their
2459 * stream or path usage.
2460 *
2461 * Returns 0 for success.
2462 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002463int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002464{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002465 int ret;
2466
Liam Girdwood3cd04342012-03-09 12:02:08 +00002467 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002468 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002469 mutex_unlock(&dapm->card->dapm_mutex);
2470 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002471}
Liam Girdwooda5302182008-07-07 13:35:17 +01002472EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002473
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002474/*
2475 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2476 * @w: The widget for which to update the flags
2477 *
2478 * Some widgets have a dynamic category which depends on which neighbors they
2479 * are connected to. This function update the category for these widgets.
2480 *
2481 * This function must be called whenever a path is added or removed to a widget.
2482 */
2483static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2484{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002485 enum snd_soc_dapm_direction dir;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002486 struct snd_soc_dapm_path *p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002487 unsigned int ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002488
2489 switch (w->id) {
2490 case snd_soc_dapm_input:
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002491 /* On a fully routed card an input is never a source */
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002492 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002493 return;
2494 ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002495 snd_soc_dapm_widget_for_each_source_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002496 if (p->source->id == snd_soc_dapm_micbias ||
2497 p->source->id == snd_soc_dapm_mic ||
2498 p->source->id == snd_soc_dapm_line ||
2499 p->source->id == snd_soc_dapm_output) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002500 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002501 break;
2502 }
2503 }
2504 break;
2505 case snd_soc_dapm_output:
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002506 /* On a fully routed card a output is never a sink */
2507 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002508 return;
2509 ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002510 snd_soc_dapm_widget_for_each_sink_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002511 if (p->sink->id == snd_soc_dapm_spk ||
2512 p->sink->id == snd_soc_dapm_hp ||
2513 p->sink->id == snd_soc_dapm_line ||
2514 p->sink->id == snd_soc_dapm_input) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002515 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002516 break;
2517 }
2518 }
2519 break;
2520 case snd_soc_dapm_line:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002521 ep = 0;
2522 snd_soc_dapm_for_each_direction(dir) {
2523 if (!list_empty(&w->edges[dir]))
2524 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2525 }
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002526 break;
2527 default:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002528 return;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002529 }
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002530
2531 w->is_ep = ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002532}
2533
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002534static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2535 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2536 const char *control)
2537{
2538 bool dynamic_source = false;
2539 bool dynamic_sink = false;
2540
2541 if (!control)
2542 return 0;
2543
2544 switch (source->id) {
2545 case snd_soc_dapm_demux:
2546 dynamic_source = true;
2547 break;
2548 default:
2549 break;
2550 }
2551
2552 switch (sink->id) {
2553 case snd_soc_dapm_mux:
2554 case snd_soc_dapm_switch:
2555 case snd_soc_dapm_mixer:
2556 case snd_soc_dapm_mixer_named_ctl:
2557 dynamic_sink = true;
2558 break;
2559 default:
2560 break;
2561 }
2562
2563 if (dynamic_source && dynamic_sink) {
2564 dev_err(dapm->dev,
2565 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2566 source->name, control, sink->name);
2567 return -EINVAL;
2568 } else if (!dynamic_source && !dynamic_sink) {
2569 dev_err(dapm->dev,
2570 "Control not supported for path %s -> [%s] -> %s\n",
2571 source->name, control, sink->name);
2572 return -EINVAL;
2573 }
2574
2575 return 0;
2576}
2577
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002578static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2579 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2580 const char *control,
2581 int (*connected)(struct snd_soc_dapm_widget *source,
2582 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002583{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002584 struct snd_soc_dapm_widget *widgets[2];
2585 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002586 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002587 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002588
Lars-Peter Clausene409dfb2014-10-25 17:42:02 +02002589 if (wsink->is_supply && !wsource->is_supply) {
2590 dev_err(dapm->dev,
2591 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2592 wsource->name, wsink->name);
2593 return -EINVAL;
2594 }
2595
2596 if (connected && !wsource->is_supply) {
2597 dev_err(dapm->dev,
2598 "connected() callback only supported for supply widgets (%s -> %s)\n",
2599 wsource->name, wsink->name);
2600 return -EINVAL;
2601 }
2602
2603 if (wsource->is_supply && control) {
2604 dev_err(dapm->dev,
2605 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2606 wsource->name, control, wsink->name);
2607 return -EINVAL;
2608 }
2609
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002610 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2611 if (ret)
2612 return ret;
2613
Richard Purdie2b97eab2006-10-06 18:32:18 +02002614 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2615 if (!path)
2616 return -ENOMEM;
2617
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002618 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2619 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2620 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2621 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2622
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002623 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002624 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002625 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002626
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002627 if (wsource->is_supply || wsink->is_supply)
2628 path->is_supply = 1;
2629
Richard Purdie2b97eab2006-10-06 18:32:18 +02002630 /* connect static paths */
2631 if (control == NULL) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002632 path->connect = 1;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002633 } else {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002634 switch (wsource->id) {
2635 case snd_soc_dapm_demux:
2636 ret = dapm_connect_mux(dapm, path, control, wsource);
2637 if (ret)
2638 goto err;
2639 break;
2640 default:
2641 break;
2642 }
2643
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002644 switch (wsink->id) {
2645 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002646 ret = dapm_connect_mux(dapm, path, control, wsink);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002647 if (ret != 0)
2648 goto err;
2649 break;
2650 case snd_soc_dapm_switch:
2651 case snd_soc_dapm_mixer:
2652 case snd_soc_dapm_mixer_named_ctl:
2653 ret = dapm_connect_mixer(dapm, path, control);
2654 if (ret != 0)
2655 goto err;
2656 break;
2657 default:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002658 break;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002659 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002660 }
2661
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002662 list_add(&path->list, &dapm->card->paths);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002663 snd_soc_dapm_for_each_direction(dir)
2664 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002665
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002666 snd_soc_dapm_for_each_direction(dir) {
2667 dapm_update_widget_flags(widgets[dir]);
2668 dapm_mark_dirty(widgets[dir], "Route added");
2669 }
Mark Brownfabd0382012-07-05 17:20:06 +01002670
Sudheer Papothif8028fe2016-11-03 02:30:27 +05302671 dapm_path_invalidate(path);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002672
Richard Purdie2b97eab2006-10-06 18:32:18 +02002673 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002674err:
2675 kfree(path);
2676 return ret;
2677}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002678
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002679static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002680 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002681{
2682 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2683 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2684 const char *sink;
2685 const char *source;
2686 char prefixed_sink[80];
2687 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002688 const char *prefix;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002689 int ret;
2690
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002691 prefix = soc_dapm_prefix(dapm);
2692 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002693 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002694 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002695 sink = prefixed_sink;
2696 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002697 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002698 source = prefixed_source;
2699 } else {
2700 sink = route->sink;
2701 source = route->source;
2702 }
2703
Charles Keepax45a110a2015-05-11 13:50:30 +01002704 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2705 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2706
2707 if (wsink && wsource)
2708 goto skip_search;
2709
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002710 /*
2711 * find src and dest widgets over all widgets but favor a widget from
2712 * current DAPM context
2713 */
2714 list_for_each_entry(w, &dapm->card->widgets, list) {
2715 if (!wsink && !(strcmp(w->name, sink))) {
2716 wtsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002717 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002718 wsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002719 if (wsource)
2720 break;
2721 }
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002722 continue;
2723 }
2724 if (!wsource && !(strcmp(w->name, source))) {
2725 wtsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002726 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002727 wsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002728 if (wsink)
2729 break;
2730 }
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002731 }
2732 }
2733 /* use widget from another DAPM context if not found from this */
2734 if (!wsink)
2735 wsink = wtsink;
2736 if (!wsource)
2737 wsource = wtsource;
2738
2739 if (wsource == NULL) {
2740 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2741 route->source);
2742 return -ENODEV;
2743 }
2744 if (wsink == NULL) {
2745 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2746 route->sink);
2747 return -ENODEV;
2748 }
2749
Charles Keepax45a110a2015-05-11 13:50:30 +01002750skip_search:
2751 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2752 dapm_wcache_update(&dapm->path_source_cache, wsource);
2753
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002754 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2755 route->connected);
2756 if (ret)
2757 goto err;
2758
2759 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002760err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002761 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002762 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002763 return ret;
2764}
Mark Brown105f1c22008-05-13 14:52:19 +02002765
Mark Brownefcc3c62012-07-05 17:24:19 +01002766static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2767 const struct snd_soc_dapm_route *route)
2768{
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002769 struct snd_soc_dapm_widget *wsource, *wsink;
Mark Brownefcc3c62012-07-05 17:24:19 +01002770 struct snd_soc_dapm_path *path, *p;
2771 const char *sink;
2772 const char *source;
2773 char prefixed_sink[80];
2774 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002775 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002776
2777 if (route->control) {
2778 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002779 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002780 return -EINVAL;
2781 }
2782
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002783 prefix = soc_dapm_prefix(dapm);
2784 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002785 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002786 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002787 sink = prefixed_sink;
2788 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002789 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002790 source = prefixed_source;
2791 } else {
2792 sink = route->sink;
2793 source = route->source;
2794 }
2795
2796 path = NULL;
2797 list_for_each_entry(p, &dapm->card->paths, list) {
2798 if (strcmp(p->source->name, source) != 0)
2799 continue;
2800 if (strcmp(p->sink->name, sink) != 0)
2801 continue;
2802 path = p;
2803 break;
2804 }
2805
2806 if (path) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002807 wsource = path->source;
2808 wsink = path->sink;
2809
2810 dapm_mark_dirty(wsource, "Route removed");
2811 dapm_mark_dirty(wsink, "Route removed");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002812 if (path->connect)
2813 dapm_path_invalidate(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002814
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002815 dapm_free_path(path);
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002816
2817 /* Update any path related flags */
2818 dapm_update_widget_flags(wsource);
2819 dapm_update_widget_flags(wsink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002820 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002821 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002822 source, sink);
2823 }
2824
2825 return 0;
2826}
2827
Mark Brown105f1c22008-05-13 14:52:19 +02002828/**
Mark Brown105f1c22008-05-13 14:52:19 +02002829 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002830 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002831 * @route: audio routes
2832 * @num: number of routes
2833 *
2834 * Connects 2 dapm widgets together via a named audio path. The sink is
2835 * the widget receiving the audio signal, whilst the source is the sender
2836 * of the audio signal.
2837 *
2838 * Returns 0 for success else error. On error all resources can be freed
2839 * with a call to snd_soc_card_free().
2840 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002841int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002842 const struct snd_soc_dapm_route *route, int num)
2843{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002844 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002845
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002846 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002847 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002848 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002849 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002850 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2851 route->source,
2852 route->control ? route->control : "direct",
2853 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002854 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002855 }
2856 route++;
2857 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002858 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002859
Dan Carpenter60884c22012-04-13 22:25:43 +03002860 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002861}
2862EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2863
Mark Brownefcc3c62012-07-05 17:24:19 +01002864/**
2865 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2866 * @dapm: DAPM context
2867 * @route: audio routes
2868 * @num: number of routes
2869 *
2870 * Removes routes from the DAPM context.
2871 */
2872int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2873 const struct snd_soc_dapm_route *route, int num)
2874{
Rajan Vajae066ea22016-02-11 11:23:35 +05302875 int i;
Mark Brownefcc3c62012-07-05 17:24:19 +01002876
2877 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2878 for (i = 0; i < num; i++) {
2879 snd_soc_dapm_del_route(dapm, route);
2880 route++;
2881 }
2882 mutex_unlock(&dapm->card->dapm_mutex);
2883
Rajan Vajae066ea22016-02-11 11:23:35 +05302884 return 0;
Mark Brownefcc3c62012-07-05 17:24:19 +01002885}
2886EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2887
Mark Brownbf3a9e12011-06-13 16:42:29 +01002888static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2889 const struct snd_soc_dapm_route *route)
2890{
2891 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2892 route->source,
2893 true);
2894 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2895 route->sink,
2896 true);
2897 struct snd_soc_dapm_path *path;
2898 int count = 0;
2899
2900 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002901 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002902 route->source);
2903 return -ENODEV;
2904 }
2905
2906 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002907 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002908 route->sink);
2909 return -ENODEV;
2910 }
2911
2912 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002913 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002914 route->source, route->sink);
2915
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002916 snd_soc_dapm_widget_for_each_sink_path(source, path) {
Mark Brownbf3a9e12011-06-13 16:42:29 +01002917 if (path->sink == sink) {
2918 path->weak = 1;
2919 count++;
2920 }
2921 }
2922
2923 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002924 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002925 route->source, route->sink);
2926 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002927 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002928 count, route->source, route->sink);
2929
2930 return 0;
2931}
2932
2933/**
2934 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2935 * @dapm: DAPM context
2936 * @route: audio routes
2937 * @num: number of routes
2938 *
2939 * Mark existing routes matching those specified in the passed array
2940 * as being weak, meaning that they are ignored for the purpose of
2941 * power decisions. The main intended use case is for sidetone paths
2942 * which couple audio between other independent paths if they are both
2943 * active in order to make the combination work better at the user
2944 * level but which aren't intended to be "used".
2945 *
2946 * Note that CODEC drivers should not use this as sidetone type paths
2947 * can frequently also be used as bypass paths.
2948 */
2949int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2950 const struct snd_soc_dapm_route *route, int num)
2951{
2952 int i, err;
2953 int ret = 0;
2954
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002955 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002956 for (i = 0; i < num; i++) {
2957 err = snd_soc_dapm_weak_route(dapm, route);
2958 if (err)
2959 ret = err;
2960 route++;
2961 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002962 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002963
2964 return ret;
2965}
2966EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2967
Mark Brown105f1c22008-05-13 14:52:19 +02002968/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002969 * snd_soc_dapm_new_widgets - add new dapm widgets
Jonathan Corbet628536e2015-08-25 01:14:48 -06002970 * @card: card to be checked for new dapm widgets
Richard Purdie2b97eab2006-10-06 18:32:18 +02002971 *
2972 * Checks the codec for any new dapm widgets and creates them if found.
2973 *
2974 * Returns 0 for success.
2975 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002976int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002977{
2978 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002979 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002980
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002981 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002982
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002983 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002984 {
2985 if (w->new)
2986 continue;
2987
Stephen Warrenfad59882011-04-28 17:37:59 -06002988 if (w->num_kcontrols) {
2989 w->kcontrols = kzalloc(w->num_kcontrols *
2990 sizeof(struct snd_kcontrol *),
2991 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002992 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002993 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002994 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002995 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002996 }
2997
Richard Purdie2b97eab2006-10-06 18:32:18 +02002998 switch(w->id) {
2999 case snd_soc_dapm_switch:
3000 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00003001 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003002 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003003 break;
3004 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003005 case snd_soc_dapm_demux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003006 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003007 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003008 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06003009 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003010 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003011 break;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003012 case snd_soc_dapm_dai_link:
3013 dapm_new_dai_link(w);
3014 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01003015 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003016 break;
3017 }
Mark Brownb66a70d2011-02-09 18:04:11 +00003018
3019 /* Read the initial power state from the device */
3020 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003021 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08003022 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003023 val &= w->mask;
3024 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00003025 w->power = 1;
3026 }
3027
Richard Purdie2b97eab2006-10-06 18:32:18 +02003028 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003029
Mark Brown7508b122011-10-05 12:09:12 +01003030 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003031 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003032 }
3033
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003034 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3035 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003036 return 0;
3037}
3038EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3039
3040/**
3041 * snd_soc_dapm_get_volsw - dapm mixer get callback
3042 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003043 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003044 *
3045 * Callback to get the value of a dapm mixer control.
3046 *
3047 * Returns 0 for success.
3048 */
3049int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3050 struct snd_ctl_elem_value *ucontrol)
3051{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003052 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3053 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003054 struct soc_mixer_control *mc =
3055 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003056 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003057 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003058 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003059 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003060 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003061 unsigned int val;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003062 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003063
3064 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003065 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003066 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003067 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003068
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003069 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003070 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3071 ret = soc_dapm_read(dapm, reg, &val);
3072 val = (val >> shift) & mask;
3073 } else {
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003074 val = dapm_kcontrol_get_value(kcontrol);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003075 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003076 mutex_unlock(&card->dapm_mutex);
3077
Chen-Yu Tsai01ad5e72016-08-27 19:27:58 +08003078 if (ret)
3079 return ret;
3080
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003081 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003082 ucontrol->value.integer.value[0] = max - val;
3083 else
3084 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003085
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003086 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003087}
3088EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3089
3090/**
3091 * snd_soc_dapm_put_volsw - dapm mixer set callback
3092 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003093 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003094 *
3095 * Callback to set the value of a dapm mixer control.
3096 *
3097 * Returns 0 for success.
3098 */
3099int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3100 struct snd_ctl_elem_value *ucontrol)
3101{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003102 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3103 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003104 struct soc_mixer_control *mc =
3105 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003106 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003107 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003108 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003109 unsigned int mask = (1 << fls(max)) - 1;
3110 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07003111 unsigned int val;
Jarkko Nikula18626c72014-06-09 14:20:29 +03003112 int connect, change, reg_change = 0;
Mark Brown97404f22010-12-14 16:13:57 +00003113 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08003114 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003115
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003116 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003117 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003118 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003119 kcontrol->id.name);
3120
Richard Purdie2b97eab2006-10-06 18:32:18 +02003121 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02003122 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003123
3124 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003125 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003126
Liam Girdwood3cd04342012-03-09 12:02:08 +00003127 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003128
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003129 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown283375c2009-12-07 18:09:03 +00003130
Jarkko Nikula18626c72014-06-09 14:20:29 +03003131 if (reg != SND_SOC_NOPM) {
3132 mask = mask << shift;
3133 val = val << shift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02003134
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003135 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
Jarkko Nikula18626c72014-06-09 14:20:29 +03003136 }
3137
3138 if (change || reg_change) {
3139 if (reg_change) {
3140 update.kcontrol = kcontrol;
3141 update.reg = reg;
3142 update.mask = mask;
3143 update.val = val;
3144 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003145 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003146 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00003147
Nenghua Cao52765972013-12-13 20:13:49 +08003148 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00003149
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003150 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00003151 }
3152
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003153 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003154
3155 if (ret > 0)
3156 soc_dpcm_runtime_update(card);
3157
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003158 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003159}
3160EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3161
3162/**
3163 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3164 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003165 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003166 *
3167 * Callback to get the value of a dapm enumerated double mixer control.
3168 *
3169 * Returns 0 for success.
3170 */
3171int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3172 struct snd_ctl_elem_value *ucontrol)
3173{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003174 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Charles Keepax561ed682015-05-01 12:37:26 +01003175 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003176 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003177 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003178
Charles Keepax561ed682015-05-01 12:37:26 +01003179 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3180 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003181 int ret = soc_dapm_read(dapm, e->reg, &reg_val);
Charles Keepax964a0b82015-05-08 10:50:10 +01003182 if (ret) {
3183 mutex_unlock(&card->dapm_mutex);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003184 return ret;
Charles Keepax964a0b82015-05-08 10:50:10 +01003185 }
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003186 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003187 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003188 }
Charles Keepax561ed682015-05-01 12:37:26 +01003189 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003190
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003191 val = (reg_val >> e->shift_l) & e->mask;
3192 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3193 if (e->shift_l != e->shift_r) {
3194 val = (reg_val >> e->shift_r) & e->mask;
3195 val = snd_soc_enum_val_to_item(e, val);
3196 ucontrol->value.enumerated.item[1] = val;
3197 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003198
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003199 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003200}
3201EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3202
3203/**
3204 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3205 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003206 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003207 *
3208 * Callback to set the value of a dapm enumerated double mixer control.
3209 *
3210 * Returns 0 for success.
3211 */
3212int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3213 struct snd_ctl_elem_value *ucontrol)
3214{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003215 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3216 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003217 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003218 unsigned int *item = ucontrol->value.enumerated.item;
Charles Keepax561ed682015-05-01 12:37:26 +01003219 unsigned int val, change, reg_change = 0;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003220 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00003221 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08003222 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003223
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003224 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003225 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003226
3227 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003228 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003229 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003230 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003231 return -EINVAL;
Chen-Yu Tsai071133a2016-08-27 19:27:59 +08003232 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003233 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003234 }
3235
Liam Girdwood3cd04342012-03-09 12:02:08 +00003236 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003237
Charles Keepax561ed682015-05-01 12:37:26 +01003238 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00003239
Charles Keepax561ed682015-05-01 12:37:26 +01003240 if (e->reg != SND_SOC_NOPM)
3241 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3242
3243 if (change || reg_change) {
3244 if (reg_change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003245 update.kcontrol = kcontrol;
3246 update.reg = e->reg;
3247 update.mask = mask;
3248 update.val = val;
3249 card->update = &update;
3250 }
Charles Keepax561ed682015-05-01 12:37:26 +01003251 change |= reg_change;
Mark Brown3a655772009-10-05 17:23:30 +01003252
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003253 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003254
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003255 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003256 }
3257
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003258 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003259
3260 if (ret > 0)
3261 soc_dpcm_runtime_update(card);
3262
Mark Brown97404f22010-12-14 16:13:57 +00003263 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003264}
3265EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3266
3267/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003268 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3269 *
3270 * @kcontrol: mixer control
3271 * @uinfo: control element information
3272 *
3273 * Callback to provide information about a pin switch control.
3274 */
3275int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3276 struct snd_ctl_elem_info *uinfo)
3277{
3278 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3279 uinfo->count = 1;
3280 uinfo->value.integer.min = 0;
3281 uinfo->value.integer.max = 1;
3282
3283 return 0;
3284}
3285EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3286
3287/**
3288 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3289 *
3290 * @kcontrol: mixer control
3291 * @ucontrol: Value
3292 */
3293int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3294 struct snd_ctl_elem_value *ucontrol)
3295{
Mark Brown48a8c392012-02-14 17:11:15 -08003296 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003297 const char *pin = (const char *)kcontrol->private_value;
3298
Liam Girdwood3cd04342012-03-09 12:02:08 +00003299 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003300
3301 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003302 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003303
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003304 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003305
3306 return 0;
3307}
3308EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3309
3310/**
3311 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3312 *
3313 * @kcontrol: mixer control
3314 * @ucontrol: Value
3315 */
3316int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3317 struct snd_ctl_elem_value *ucontrol)
3318{
Mark Brown48a8c392012-02-14 17:11:15 -08003319 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003320 const char *pin = (const char *)kcontrol->private_value;
3321
Mark Brown8b37dbd2009-02-28 21:14:20 +00003322 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003323 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003324 else
Mark Brown48a8c392012-02-14 17:11:15 -08003325 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003326
Mark Brown48a8c392012-02-14 17:11:15 -08003327 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003328 return 0;
3329}
3330EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3331
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +01003332struct snd_soc_dapm_widget *
Mark Brown5ba06fc2012-02-16 11:07:13 -08003333snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003334 const struct snd_soc_dapm_widget *widget)
3335{
3336 struct snd_soc_dapm_widget *w;
3337
3338 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3339 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
Linus Walleijde415c82017-01-13 10:23:52 +01003340 /* Do not nag about probe deferrals */
3341 if (IS_ERR(w)) {
3342 int ret = PTR_ERR(w);
3343
3344 if (ret != -EPROBE_DEFER)
3345 dev_err(dapm->dev,
3346 "ASoC: Failed to create DAPM control %s (%d)\n",
3347 widget->name, ret);
3348 goto out_unlock;
3349 }
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003350 if (!w)
3351 dev_err(dapm->dev,
3352 "ASoC: Failed to create DAPM control %s\n",
3353 widget->name);
3354
Linus Walleijde415c82017-01-13 10:23:52 +01003355out_unlock:
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003356 mutex_unlock(&dapm->card->dapm_mutex);
3357 return w;
3358}
Subhransu S. Prustya5d56392016-06-27 09:18:03 +05303359EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003360
3361struct snd_soc_dapm_widget *
3362snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003363 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003364{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003365 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003366 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003367 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00003368 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003369
3370 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003371 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003372
Mark Brown62ea8742012-01-21 21:14:48 +00003373 switch (w->id) {
3374 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003375 w->regulator = devm_regulator_get(dapm->dev, w->name);
3376 if (IS_ERR(w->regulator)) {
3377 ret = PTR_ERR(w->regulator);
Linus Walleijde415c82017-01-13 10:23:52 +01003378 if (ret == -EPROBE_DEFER)
3379 return ERR_PTR(ret);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003380 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003381 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003382 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003383 }
Mark Brown8784c772013-01-10 19:33:47 +00003384
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003385 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003386 ret = regulator_allow_bypass(w->regulator, true);
3387 if (ret != 0)
3388 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003389 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003390 w->name, ret);
3391 }
Mark Brown62ea8742012-01-21 21:14:48 +00003392 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003393 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003394#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003395 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003396 if (IS_ERR(w->clk)) {
3397 ret = PTR_ERR(w->clk);
Linus Walleijde415c82017-01-13 10:23:52 +01003398 if (ret == -EPROBE_DEFER)
3399 return ERR_PTR(ret);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003400 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003401 w->name, ret);
3402 return NULL;
3403 }
Mark Brownec029952012-06-04 08:16:20 +01003404#else
3405 return NULL;
3406#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003407 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003408 default:
3409 break;
3410 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003411
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003412 prefix = soc_dapm_prefix(dapm);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003413 if (prefix)
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003414 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003415 else
Lars-Peter Clausen48068962015-07-21 18:11:08 +02003416 w->name = kstrdup_const(widget->name, GFP_KERNEL);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003417 if (w->name == NULL) {
3418 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003419 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003420 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003421
Mark Brown7ca3a182011-10-08 14:04:50 +01003422 switch (w->id) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003423 case snd_soc_dapm_mic:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003424 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003425 w->power_check = dapm_generic_check_power;
3426 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003427 case snd_soc_dapm_input:
3428 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003429 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003430 w->power_check = dapm_generic_check_power;
3431 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003432 case snd_soc_dapm_spk:
3433 case snd_soc_dapm_hp:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003434 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003435 w->power_check = dapm_generic_check_power;
3436 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003437 case snd_soc_dapm_output:
3438 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003439 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003440 w->power_check = dapm_generic_check_power;
3441 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003442 case snd_soc_dapm_vmid:
3443 case snd_soc_dapm_siggen:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003444 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003445 w->power_check = dapm_always_on_check_power;
3446 break;
Vinod Koul56b44372015-11-23 21:22:30 +05303447 case snd_soc_dapm_sink:
3448 w->is_ep = SND_SOC_DAPM_EP_SINK;
3449 w->power_check = dapm_always_on_check_power;
3450 break;
3451
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003452 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003453 case snd_soc_dapm_demux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003454 case snd_soc_dapm_switch:
3455 case snd_soc_dapm_mixer:
3456 case snd_soc_dapm_mixer_named_ctl:
Mark Brown63c69a62013-07-18 22:03:01 +01003457 case snd_soc_dapm_adc:
3458 case snd_soc_dapm_aif_out:
3459 case snd_soc_dapm_dac:
3460 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003461 case snd_soc_dapm_pga:
3462 case snd_soc_dapm_out_drv:
Mark Brown7ca3a182011-10-08 14:04:50 +01003463 case snd_soc_dapm_micbias:
Mark Brown7ca3a182011-10-08 14:04:50 +01003464 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003465 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02003466 case snd_soc_dapm_dai_out:
3467 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003468 w->power_check = dapm_generic_check_power;
3469 break;
3470 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003471 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003472 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003473 case snd_soc_dapm_kcontrol:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003474 w->is_supply = 1;
Mark Brown7ca3a182011-10-08 14:04:50 +01003475 w->power_check = dapm_supply_check_power;
3476 break;
3477 default:
3478 w->power_check = dapm_always_on_check_power;
3479 break;
3480 }
3481
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003482 w->dapm = dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003483 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003484 INIT_LIST_HEAD(&w->dirty);
Lars-Peter Clausen92fa1242015-05-01 18:02:42 +02003485 list_add_tail(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003486
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003487 snd_soc_dapm_for_each_direction(dir) {
3488 INIT_LIST_HEAD(&w->edges[dir]);
3489 w->endpoints[dir] = -1;
3490 }
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003491
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02003492 /* machine layer sets up unconnected pins and insertions */
Richard Purdie2b97eab2006-10-06 18:32:18 +02003493 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003494 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003495}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003496
3497/**
Mark Brown4ba13272008-05-13 14:51:19 +02003498 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003499 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003500 * @widget: widget array
3501 * @num: number of widgets
3502 *
3503 * Creates new DAPM controls based upon the templates.
3504 *
3505 * Returns 0 for success else error.
3506 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003507int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003508 const struct snd_soc_dapm_widget *widget,
3509 int num)
3510{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003511 struct snd_soc_dapm_widget *w;
3512 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003513 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003514
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003515 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003516 for (i = 0; i < num; i++) {
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003517 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
Linus Walleijde415c82017-01-13 10:23:52 +01003518 if (IS_ERR(w)) {
3519 ret = PTR_ERR(w);
3520 /* Do not nag about probe deferrals */
3521 if (ret == -EPROBE_DEFER)
3522 break;
3523 dev_err(dapm->dev,
3524 "ASoC: Failed to create DAPM control %s (%d)\n",
3525 widget->name, ret);
3526 break;
3527 }
Mark Brown5ba06fc2012-02-16 11:07:13 -08003528 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003529 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003530 "ASoC: Failed to create DAPM control %s\n",
3531 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003532 ret = -ENOMEM;
3533 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003534 }
Mark Brown4ba13272008-05-13 14:51:19 +02003535 widget++;
3536 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003537 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003538 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003539}
3540EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3541
Mark Brownc74184e2012-04-04 22:12:09 +01003542static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3543 struct snd_kcontrol *kcontrol, int event)
3544{
3545 struct snd_soc_dapm_path *source_p, *sink_p;
3546 struct snd_soc_dai *source, *sink;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003547 const struct snd_soc_pcm_stream *config = w->params + w->params_select;
Mark Brownc74184e2012-04-04 22:12:09 +01003548 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003549 struct snd_pcm_hw_params *params = NULL;
Nicolin Chen8053f212016-07-26 14:55:51 -07003550 struct snd_pcm_runtime *runtime = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003551 u64 fmt;
3552 int ret;
3553
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003554 if (WARN_ON(!config) ||
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003555 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3556 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003557 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003558
3559 /* We only support a single source and sink, pick the first */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003560 source_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_OUT],
3561 struct snd_soc_dapm_path,
3562 list_node[SND_SOC_DAPM_DIR_OUT]);
3563 sink_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_IN],
3564 struct snd_soc_dapm_path,
3565 list_node[SND_SOC_DAPM_DIR_IN]);
Mark Brownc74184e2012-04-04 22:12:09 +01003566
3567 source = source_p->source->priv;
3568 sink = sink_p->sink->priv;
3569
3570 /* Be a little careful as we don't want to overflow the mask array */
3571 if (config->formats) {
3572 fmt = ffs(config->formats) - 1;
3573 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003574 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003575 config->formats);
3576 fmt = 0;
3577 }
3578
3579 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003580 params = kzalloc(sizeof(*params), GFP_KERNEL);
3581 if (!params) {
3582 ret = -ENOMEM;
3583 goto out;
3584 }
3585 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003586
Mark Brown9747cec2012-04-26 19:12:21 +01003587 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003588 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003589 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003590 config->rate_max;
3591
Mark Brown9747cec2012-04-26 19:12:21 +01003592 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003593 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003594 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003595 = config->channels_max;
3596
3597 memset(&substream, 0, sizeof(substream));
3598
Nicolin Chen8053f212016-07-26 14:55:51 -07003599 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3600 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3601 if (!runtime) {
3602 ret = -ENOMEM;
3603 goto out;
3604 }
3605 substream.runtime = runtime;
3606
Mark Brownc74184e2012-04-04 22:12:09 +01003607 switch (event) {
3608 case SND_SOC_DAPM_PRE_PMU:
Benoit Cousson93e69582014-07-08 23:19:38 +02003609 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303610 if (source->driver->ops && source->driver->ops->startup) {
3611 ret = source->driver->ops->startup(&substream, source);
3612 if (ret < 0) {
3613 dev_err(source->dev,
3614 "ASoC: startup() failed: %d\n", ret);
3615 goto out;
3616 }
3617 source->active++;
3618 }
Benoit Cousson93e69582014-07-08 23:19:38 +02003619 ret = soc_dai_hw_params(&substream, params, source);
3620 if (ret < 0)
3621 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003622
Benoit Cousson93e69582014-07-08 23:19:38 +02003623 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303624 if (sink->driver->ops && sink->driver->ops->startup) {
3625 ret = sink->driver->ops->startup(&substream, sink);
3626 if (ret < 0) {
3627 dev_err(sink->dev,
3628 "ASoC: startup() failed: %d\n", ret);
3629 goto out;
3630 }
3631 sink->active++;
3632 }
Benoit Cousson93e69582014-07-08 23:19:38 +02003633 ret = soc_dai_hw_params(&substream, params, sink);
3634 if (ret < 0)
3635 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003636 break;
3637
3638 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003639 ret = snd_soc_dai_digital_mute(sink, 0,
3640 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003641 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003642 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003643 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003644 break;
3645
3646 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003647 ret = snd_soc_dai_digital_mute(sink, 1,
3648 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003649 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003650 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003651 ret = 0;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303652
3653 source->active--;
3654 if (source->driver->ops && source->driver->ops->shutdown) {
3655 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3656 source->driver->ops->shutdown(&substream, source);
3657 }
3658
3659 sink->active--;
3660 if (sink->driver->ops && sink->driver->ops->shutdown) {
3661 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3662 sink->driver->ops->shutdown(&substream, sink);
3663 }
Mark Brownc74184e2012-04-04 22:12:09 +01003664 break;
3665
3666 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003667 WARN(1, "Unknown event %d\n", event);
Sudip Mukherjee75881df2015-09-10 18:01:44 +05303668 ret = -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003669 }
3670
Mark Brown9747cec2012-04-26 19:12:21 +01003671out:
Nicolin Chen8053f212016-07-26 14:55:51 -07003672 kfree(runtime);
Mark Brown9747cec2012-04-26 19:12:21 +01003673 kfree(params);
3674 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003675}
3676
Nikesh Oswalc6615082015-02-02 17:06:44 +00003677static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3678 struct snd_ctl_elem_value *ucontrol)
3679{
3680 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3681
Takashi Iwai741338f2016-02-29 17:20:48 +01003682 ucontrol->value.enumerated.item[0] = w->params_select;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003683
3684 return 0;
3685}
3686
3687static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3688 struct snd_ctl_elem_value *ucontrol)
3689{
3690 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3691
3692 /* Can't change the config when widget is already powered */
3693 if (w->power)
3694 return -EBUSY;
3695
Takashi Iwai741338f2016-02-29 17:20:48 +01003696 if (ucontrol->value.enumerated.item[0] == w->params_select)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003697 return 0;
3698
Takashi Iwai741338f2016-02-29 17:20:48 +01003699 if (ucontrol->value.enumerated.item[0] >= w->num_params)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003700 return -EINVAL;
3701
Takashi Iwai741338f2016-02-29 17:20:48 +01003702 w->params_select = ucontrol->value.enumerated.item[0];
Nikesh Oswalc6615082015-02-02 17:06:44 +00003703
3704 return 0;
3705}
3706
Mark Brownc74184e2012-04-04 22:12:09 +01003707int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3708 const struct snd_soc_pcm_stream *params,
Nikesh Oswalc6615082015-02-02 17:06:44 +00003709 unsigned int num_params,
Mark Brownc74184e2012-04-04 22:12:09 +01003710 struct snd_soc_dapm_widget *source,
3711 struct snd_soc_dapm_widget *sink)
3712{
Mark Brownc74184e2012-04-04 22:12:09 +01003713 struct snd_soc_dapm_widget template;
3714 struct snd_soc_dapm_widget *w;
Mark Brownc74184e2012-04-04 22:12:09 +01003715 char *link_name;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003716 int ret, count;
3717 unsigned long private_value;
3718 const char **w_param_text;
3719 struct soc_enum w_param_enum[] = {
3720 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3721 };
3722 struct snd_kcontrol_new kcontrol_dai_link[] = {
3723 SOC_ENUM_EXT(NULL, w_param_enum[0],
3724 snd_soc_dapm_dai_link_get,
3725 snd_soc_dapm_dai_link_put),
3726 };
3727 const struct snd_soc_pcm_stream *config = params;
Mark Brownc74184e2012-04-04 22:12:09 +01003728
Nikesh Oswalc6615082015-02-02 17:06:44 +00003729 w_param_text = devm_kcalloc(card->dev, num_params,
3730 sizeof(char *), GFP_KERNEL);
3731 if (!w_param_text)
Mark Brownc74184e2012-04-04 22:12:09 +01003732 return -ENOMEM;
Mark Brownc74184e2012-04-04 22:12:09 +01003733
Charles Keepax46172b62015-03-25 11:22:35 +00003734 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3735 source->name, sink->name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003736 if (!link_name) {
3737 ret = -ENOMEM;
3738 goto outfree_w_param;
3739 }
Mark Brownc74184e2012-04-04 22:12:09 +01003740
Nikesh Oswalc6615082015-02-02 17:06:44 +00003741 for (count = 0 ; count < num_params; count++) {
3742 if (!config->stream_name) {
3743 dev_warn(card->dapm.dev,
3744 "ASoC: anonymous config %d for dai link %s\n",
3745 count, link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003746 w_param_text[count] =
Charles Keepax46172b62015-03-25 11:22:35 +00003747 devm_kasprintf(card->dev, GFP_KERNEL,
3748 "Anonymous Configuration %d",
3749 count);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003750 if (!w_param_text[count]) {
3751 ret = -ENOMEM;
3752 goto outfree_link_name;
3753 }
Nikesh Oswalc6615082015-02-02 17:06:44 +00003754 } else {
3755 w_param_text[count] = devm_kmemdup(card->dev,
3756 config->stream_name,
3757 strlen(config->stream_name) + 1,
3758 GFP_KERNEL);
3759 if (!w_param_text[count]) {
3760 ret = -ENOMEM;
3761 goto outfree_link_name;
3762 }
3763 }
3764 config++;
3765 }
3766 w_param_enum[0].items = num_params;
3767 w_param_enum[0].texts = w_param_text;
Mark Brownc74184e2012-04-04 22:12:09 +01003768
3769 memset(&template, 0, sizeof(template));
3770 template.reg = SND_SOC_NOPM;
3771 template.id = snd_soc_dapm_dai_link;
3772 template.name = link_name;
3773 template.event = snd_soc_dai_link_event;
3774 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3775 SND_SOC_DAPM_PRE_PMD;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003776 template.num_kcontrols = 1;
3777 /* duplicate w_param_enum on heap so that memory persists */
3778 private_value =
3779 (unsigned long) devm_kmemdup(card->dev,
3780 (void *)(kcontrol_dai_link[0].private_value),
3781 sizeof(struct soc_enum), GFP_KERNEL);
3782 if (!private_value) {
3783 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3784 link_name);
3785 ret = -ENOMEM;
3786 goto outfree_link_name;
3787 }
3788 kcontrol_dai_link[0].private_value = private_value;
3789 /* duplicate kcontrol_dai_link on heap so that memory persists */
3790 template.kcontrol_news =
3791 devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3792 sizeof(struct snd_kcontrol_new),
3793 GFP_KERNEL);
3794 if (!template.kcontrol_news) {
3795 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3796 link_name);
3797 ret = -ENOMEM;
3798 goto outfree_private_value;
3799 }
Mark Brownc74184e2012-04-04 22:12:09 +01003800
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003801 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003802
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003803 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
Linus Walleijde415c82017-01-13 10:23:52 +01003804 if (IS_ERR(w)) {
3805 ret = PTR_ERR(w);
3806 /* Do not nag about probe deferrals */
3807 if (ret != -EPROBE_DEFER)
3808 dev_err(card->dev,
3809 "ASoC: Failed to create %s widget (%d)\n",
3810 link_name, ret);
3811 goto outfree_kcontrol_news;
3812 }
Mark Brownc74184e2012-04-04 22:12:09 +01003813 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003814 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003815 link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003816 ret = -ENOMEM;
3817 goto outfree_kcontrol_news;
Mark Brownc74184e2012-04-04 22:12:09 +01003818 }
3819
3820 w->params = params;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003821 w->num_params = num_params;
Mark Brownc74184e2012-04-04 22:12:09 +01003822
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003823 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3824 if (ret)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003825 goto outfree_w;
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003826 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003827
3828outfree_w:
3829 devm_kfree(card->dev, w);
3830outfree_kcontrol_news:
3831 devm_kfree(card->dev, (void *)template.kcontrol_news);
3832outfree_private_value:
3833 devm_kfree(card->dev, (void *)private_value);
3834outfree_link_name:
3835 devm_kfree(card->dev, link_name);
3836outfree_w_param:
3837 for (count = 0 ; count < num_params; count++)
3838 devm_kfree(card->dev, (void *)w_param_text[count]);
3839 devm_kfree(card->dev, w_param_text);
3840
3841 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003842}
3843
Mark Brown888df392012-02-16 19:37:51 -08003844int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3845 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003846{
Mark Brown888df392012-02-16 19:37:51 -08003847 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003848 struct snd_soc_dapm_widget *w;
3849
Mark Brown888df392012-02-16 19:37:51 -08003850 WARN_ON(dapm->dev != dai->dev);
3851
3852 memset(&template, 0, sizeof(template));
3853 template.reg = SND_SOC_NOPM;
3854
3855 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003856 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003857 template.name = dai->driver->playback.stream_name;
3858 template.sname = dai->driver->playback.stream_name;
3859
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003860 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003861 template.name);
3862
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003863 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Linus Walleije2d1a422017-01-20 14:07:52 +01003864 if (IS_ERR(w)) {
3865 int ret = PTR_ERR(w);
3866
3867 /* Do not nag about probe deferrals */
3868 if (ret != -EPROBE_DEFER)
3869 dev_err(dapm->dev,
3870 "ASoC: Failed to create %s widget (%d)\n",
3871 dai->driver->playback.stream_name, ret);
3872 return ret;
3873 }
Mark Brown888df392012-02-16 19:37:51 -08003874 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003875 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003876 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003877 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003878 }
3879
3880 w->priv = dai;
3881 dai->playback_widget = w;
3882 }
3883
3884 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003885 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003886 template.name = dai->driver->capture.stream_name;
3887 template.sname = dai->driver->capture.stream_name;
3888
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003889 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003890 template.name);
3891
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003892 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Linus Walleije2d1a422017-01-20 14:07:52 +01003893 if (IS_ERR(w)) {
3894 int ret = PTR_ERR(w);
3895
3896 /* Do not nag about probe deferrals */
3897 if (ret != -EPROBE_DEFER)
3898 dev_err(dapm->dev,
3899 "ASoC: Failed to create %s widget (%d)\n",
3900 dai->driver->playback.stream_name, ret);
3901 return ret;
3902 }
Mark Brown888df392012-02-16 19:37:51 -08003903 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003904 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003905 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003906 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003907 }
3908
3909 w->priv = dai;
3910 dai->capture_widget = w;
3911 }
3912
3913 return 0;
3914}
3915
3916int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3917{
3918 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003919 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08003920 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003921
3922 /* For each DAI widget... */
3923 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003924 switch (dai_w->id) {
3925 case snd_soc_dapm_dai_in:
3926 case snd_soc_dapm_dai_out:
3927 break;
3928 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003929 continue;
Mark Brown46162742013-06-05 19:36:11 +01003930 }
Mark Brown888df392012-02-16 19:37:51 -08003931
Liam Girdwooddfbf5082018-06-14 20:26:42 +01003932 /* let users know there is no DAI to link */
3933 if (!dai_w->priv) {
3934 dev_dbg(card->dev, "dai widget %s has no DAI\n",
3935 dai_w->name);
3936 continue;
3937 }
3938
Mark Brown888df392012-02-16 19:37:51 -08003939 dai = dai_w->priv;
3940
3941 /* ...find all widgets with the same stream and link them */
3942 list_for_each_entry(w, &card->widgets, list) {
3943 if (w->dapm != dai_w->dapm)
3944 continue;
3945
Mark Brown46162742013-06-05 19:36:11 +01003946 switch (w->id) {
3947 case snd_soc_dapm_dai_in:
3948 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003949 continue;
Mark Brown46162742013-06-05 19:36:11 +01003950 default:
3951 break;
3952 }
Mark Brown888df392012-02-16 19:37:51 -08003953
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003954 if (!w->sname || !strstr(w->sname, dai_w->sname))
Mark Brown888df392012-02-16 19:37:51 -08003955 continue;
3956
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003957 if (dai_w->id == snd_soc_dapm_dai_in) {
3958 src = dai_w;
3959 sink = w;
3960 } else {
3961 src = w;
3962 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08003963 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003964 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3965 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003966 }
3967 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003968
Mark Brown888df392012-02-16 19:37:51 -08003969 return 0;
3970}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003971
Benoit Cousson44ba2642014-07-08 23:19:36 +02003972static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3973 struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003974{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003975 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003976 struct snd_soc_dapm_widget *sink, *source;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003977 int i;
3978
Benoit Cousson44ba2642014-07-08 23:19:36 +02003979 for (i = 0; i < rtd->num_codecs; i++) {
3980 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003981
Phani Kumar Uppalapati05e2fe82015-06-22 20:46:49 -07003982 if (!cpu_dai->component->codec)
3983 continue;
3984
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003985 /* connect BE DAI playback if widgets are valid */
3986 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003987 source = cpu_dai->playback_widget;
3988 sink = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003989 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003990 cpu_dai->component->name, source->name,
3991 codec_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003992
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003993 snd_soc_dapm_add_path(&card->dapm, source, sink,
3994 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003995 }
3996
3997 /* connect BE DAI capture if widgets are valid */
3998 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003999 source = codec_dai->capture_widget;
4000 sink = cpu_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004001 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004002 codec_dai->component->name, source->name,
4003 cpu_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004004
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02004005 snd_soc_dapm_add_path(&card->dapm, source, sink,
4006 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004007 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004008 }
4009}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004010
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004011static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4012 int event)
4013{
4014 struct snd_soc_dapm_widget *w;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004015 unsigned int ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004016
4017 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4018 w = dai->playback_widget;
4019 else
4020 w = dai->capture_widget;
4021
4022 if (w) {
4023 dapm_mark_dirty(w, "stream event");
4024
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004025 if (w->id == snd_soc_dapm_dai_in) {
4026 ep = SND_SOC_DAPM_EP_SOURCE;
4027 dapm_widget_invalidate_input_paths(w);
4028 } else {
4029 ep = SND_SOC_DAPM_EP_SINK;
4030 dapm_widget_invalidate_output_paths(w);
4031 }
4032
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004033 switch (event) {
4034 case SND_SOC_DAPM_STREAM_START:
4035 w->active = 1;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004036 w->is_ep = ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004037 break;
4038 case SND_SOC_DAPM_STREAM_STOP:
4039 w->active = 0;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004040 w->is_ep = 0;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004041 break;
4042 case SND_SOC_DAPM_STREAM_SUSPEND:
4043 case SND_SOC_DAPM_STREAM_RESUME:
4044 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4045 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4046 break;
4047 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004048 }
4049}
4050
Benoit Cousson44ba2642014-07-08 23:19:36 +02004051void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4052{
Mengdong Lin1a497982015-11-18 02:34:11 -05004053 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson44ba2642014-07-08 23:19:36 +02004054
4055 /* for each BE DAI link... */
Mengdong Lin1a497982015-11-18 02:34:11 -05004056 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson44ba2642014-07-08 23:19:36 +02004057 /*
4058 * dynamic FE links have no fixed DAI mapping.
4059 * CODEC<->CODEC links have no direct connection.
4060 */
4061 if (rtd->dai_link->dynamic || rtd->dai_link->params)
4062 continue;
4063
4064 dapm_connect_dai_link_widgets(card, rtd);
4065 }
4066}
4067
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004068static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4069 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004070{
Benoit Cousson44ba2642014-07-08 23:19:36 +02004071 int i;
4072
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004073 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
Benoit Cousson44ba2642014-07-08 23:19:36 +02004074 for (i = 0; i < rtd->num_codecs; i++)
4075 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004076
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004077 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004078}
4079
4080/**
4081 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4082 * @rtd: PCM runtime data
4083 * @stream: stream name
4084 * @event: stream event
4085 *
4086 * Sends a stream event to the dapm core. The core then makes any
4087 * necessary widget power changes.
4088 *
4089 * Returns 0 for success else error.
4090 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004091void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4092 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004093{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004094 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004095
Liam Girdwood3cd04342012-03-09 12:02:08 +00004096 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004097 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004098 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004099}
Richard Purdie2b97eab2006-10-06 18:32:18 +02004100
4101/**
Charles Keepax11391102014-02-18 15:22:14 +00004102 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4103 * @dapm: DAPM context
4104 * @pin: pin name
4105 *
4106 * Enables input/output pin and its parents or children widgets iff there is
4107 * a valid audio route and active audio stream.
4108 *
4109 * Requires external locking.
4110 *
4111 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4112 * do any widget power switching.
4113 */
4114int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4115 const char *pin)
4116{
4117 return snd_soc_dapm_set_pin(dapm, pin, 1);
4118}
4119EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4120
4121/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004122 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004123 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004124 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02004125 *
Mark Brown74b8f952009-06-06 11:26:15 +01004126 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01004127 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00004128 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004129 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4130 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02004131 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004132int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004133{
Charles Keepax11391102014-02-18 15:22:14 +00004134 int ret;
4135
4136 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4137
4138 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4139
4140 mutex_unlock(&dapm->card->dapm_mutex);
4141
4142 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004143}
Liam Girdwooda5302182008-07-07 13:35:17 +01004144EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004145
4146/**
Charles Keepax11391102014-02-18 15:22:14 +00004147 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4148 * @dapm: DAPM context
4149 * @pin: pin name
4150 *
4151 * Enables input/output pin regardless of any other state. This is
4152 * intended for use with microphone bias supplies used in microphone
4153 * jack detection.
4154 *
4155 * Requires external locking.
4156 *
4157 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4158 * do any widget power switching.
4159 */
4160int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4161 const char *pin)
4162{
4163 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4164
4165 if (!w) {
4166 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4167 return -EINVAL;
4168 }
4169
4170 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02004171 if (!w->connected) {
4172 /*
4173 * w->force does not affect the number of input or output paths,
4174 * so we only have to recheck if w->connected is changed
4175 */
4176 dapm_widget_invalidate_input_paths(w);
4177 dapm_widget_invalidate_output_paths(w);
4178 w->connected = 1;
4179 }
Charles Keepax11391102014-02-18 15:22:14 +00004180 w->force = 1;
4181 dapm_mark_dirty(w, "force enable");
4182
4183 return 0;
4184}
4185EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4186
4187/**
Mark Brownda341832010-03-15 19:23:37 +00004188 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004189 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00004190 * @pin: pin name
4191 *
4192 * Enables input/output pin regardless of any other state. This is
4193 * intended for use with microphone bias supplies used in microphone
4194 * jack detection.
4195 *
4196 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4197 * do any widget power switching.
4198 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004199int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4200 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00004201{
Charles Keepax11391102014-02-18 15:22:14 +00004202 int ret;
Mark Brownda341832010-03-15 19:23:37 +00004203
Charles Keepax11391102014-02-18 15:22:14 +00004204 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00004205
Charles Keepax11391102014-02-18 15:22:14 +00004206 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09004207
Charles Keepax11391102014-02-18 15:22:14 +00004208 mutex_unlock(&dapm->card->dapm_mutex);
4209
4210 return ret;
Mark Brownda341832010-03-15 19:23:37 +00004211}
4212EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4213
4214/**
Charles Keepax11391102014-02-18 15:22:14 +00004215 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4216 * @dapm: DAPM context
4217 * @pin: pin name
4218 *
4219 * Disables input/output pin and its parents or children widgets.
4220 *
4221 * Requires external locking.
4222 *
4223 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4224 * do any widget power switching.
4225 */
4226int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4227 const char *pin)
4228{
4229 return snd_soc_dapm_set_pin(dapm, pin, 0);
4230}
4231EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4232
4233/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004234 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004235 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004236 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004237 *
Mark Brown74b8f952009-06-06 11:26:15 +01004238 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00004239 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004240 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4241 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004242 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004243int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4244 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01004245{
Charles Keepax11391102014-02-18 15:22:14 +00004246 int ret;
4247
4248 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4249
4250 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4251
4252 mutex_unlock(&dapm->card->dapm_mutex);
4253
4254 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01004255}
4256EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4257
4258/**
Charles Keepax11391102014-02-18 15:22:14 +00004259 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4260 * @dapm: DAPM context
4261 * @pin: pin name
4262 *
4263 * Marks the specified pin as being not connected, disabling it along
4264 * any parent or child widgets. At present this is identical to
4265 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4266 * additional things such as disabling controls which only affect
4267 * paths through the pin.
4268 *
4269 * Requires external locking.
4270 *
4271 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4272 * do any widget power switching.
4273 */
4274int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4275 const char *pin)
4276{
4277 return snd_soc_dapm_set_pin(dapm, pin, 0);
4278}
4279EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4280
4281/**
Mark Brown5817b522008-09-24 11:23:11 +01004282 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004283 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01004284 * @pin: pin name
4285 *
4286 * Marks the specified pin as being not connected, disabling it along
4287 * any parent or child widgets. At present this is identical to
4288 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4289 * additional things such as disabling controls which only affect
4290 * paths through the pin.
4291 *
4292 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4293 * do any widget power switching.
4294 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004295int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01004296{
Charles Keepax11391102014-02-18 15:22:14 +00004297 int ret;
4298
4299 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4300
4301 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4302
4303 mutex_unlock(&dapm->card->dapm_mutex);
4304
4305 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01004306}
4307EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4308
4309/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004310 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004311 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004312 * @pin: audio signal pin endpoint (or start point)
4313 *
4314 * Get audio pin status - connected or disconnected.
4315 *
4316 * Returns 1 for connected otherwise 0.
4317 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004318int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4319 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004320{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004321 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004322
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004323 if (w)
4324 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06004325
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004326 return 0;
4327}
Liam Girdwooda5302182008-07-07 13:35:17 +01004328EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004329
4330/**
Mark Brown1547aba2010-05-07 21:11:40 +01004331 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004332 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01004333 * @pin: audio signal pin endpoint (or start point)
4334 *
4335 * Mark the given endpoint or pin as ignoring suspend. When the
4336 * system is disabled a path between two endpoints flagged as ignoring
4337 * suspend will not be disabled. The path must already be enabled via
4338 * normal means at suspend time, it will not be turned on if it was not
4339 * already enabled.
4340 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004341int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4342 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01004343{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004344 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01004345
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004346 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004347 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004348 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01004349 }
4350
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004351 w->ignore_suspend = 1;
4352
4353 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01004354}
4355EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4356
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02004357/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02004358 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03004359 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02004360 *
4361 * Free all dapm widgets and resources.
4362 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004363void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004364{
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02004365 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004366 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02004367 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004368}
4369EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4370
Xiang Xiao57996352014-03-02 00:04:02 +08004371static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01004372{
Liam Girdwood01005a72012-07-06 16:57:05 +01004373 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01004374 struct snd_soc_dapm_widget *w;
4375 LIST_HEAD(down_list);
4376 int powerdown = 0;
4377
Liam Girdwood01005a72012-07-06 16:57:05 +01004378 mutex_lock(&card->dapm_mutex);
4379
Jarkko Nikula97c866d2010-12-14 12:18:31 +02004380 list_for_each_entry(w, &dapm->card->widgets, list) {
4381 if (w->dapm != dapm)
4382 continue;
Mark Brown51737472009-06-22 13:16:51 +01004383 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004384 dapm_seq_insert(w, &down_list, false);
Charles Keepax28881f92020-02-28 15:31:45 +00004385 w->new_power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004386 powerdown = 1;
4387 }
4388 }
4389
4390 /* If there were no widgets to power down we're already in
4391 * standby.
4392 */
4393 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004394 if (dapm->bias_level == SND_SOC_BIAS_ON)
4395 snd_soc_dapm_set_bias_level(dapm,
4396 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004397 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004398 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4399 snd_soc_dapm_set_bias_level(dapm,
4400 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004401 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004402
4403 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004404}
Mark Brown51737472009-06-22 13:16:51 +01004405
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004406/*
4407 * snd_soc_dapm_shutdown - callback for system shutdown
4408 */
4409void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4410{
Xiang Xiao57996352014-03-02 00:04:02 +08004411 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004412
Xiang Xiao57996352014-03-02 00:04:02 +08004413 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08004414 if (dapm != &card->dapm) {
4415 soc_dapm_shutdown_dapm(dapm);
4416 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4417 snd_soc_dapm_set_bias_level(dapm,
4418 SND_SOC_BIAS_OFF);
4419 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004420 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08004421
4422 soc_dapm_shutdown_dapm(&card->dapm);
4423 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4424 snd_soc_dapm_set_bias_level(&card->dapm,
4425 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01004426}
4427
Richard Purdie2b97eab2006-10-06 18:32:18 +02004428/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004429MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004430MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4431MODULE_LICENSE("GPL");