blob: ab647f1fe11bd8e9ede191ba291840e591bb74ec [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,
81 [snd_soc_dapm_aif_in] = 4,
82 [snd_soc_dapm_aif_out] = 4,
83 [snd_soc_dapm_mic] = 5,
84 [snd_soc_dapm_mux] = 6,
Lars-Peter Clausend714f972015-05-01 18:02:43 +020085 [snd_soc_dapm_demux] = 6,
Mark Brown1dd275b2013-10-09 13:56:37 +010086 [snd_soc_dapm_dac] = 7,
87 [snd_soc_dapm_switch] = 8,
88 [snd_soc_dapm_mixer] = 8,
89 [snd_soc_dapm_mixer_named_ctl] = 8,
90 [snd_soc_dapm_pga] = 9,
91 [snd_soc_dapm_adc] = 10,
92 [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,
103 [snd_soc_dapm_adc] = 2,
104 [snd_soc_dapm_hp] = 3,
105 [snd_soc_dapm_spk] = 3,
106 [snd_soc_dapm_line] = 3,
107 [snd_soc_dapm_out_drv] = 3,
Mark Brown38357ab2009-06-06 19:03:23 +0100108 [snd_soc_dapm_pga] = 4,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +0200109 [snd_soc_dapm_switch] = 5,
Mark Brown38357ab2009-06-06 19:03:23 +0100110 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +0100111 [snd_soc_dapm_mixer] = 5,
112 [snd_soc_dapm_dac] = 6,
113 [snd_soc_dapm_mic] = 7,
114 [snd_soc_dapm_micbias] = 8,
115 [snd_soc_dapm_mux] = 9,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200116 [snd_soc_dapm_demux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +0100117 [snd_soc_dapm_aif_in] = 10,
118 [snd_soc_dapm_aif_out] = 10,
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) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200285 if (w->is_ep) {
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200286 dapm_mark_dirty(w, "Rechecking endpoints");
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200287 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200288 dapm_widget_invalidate_output_paths(w);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200289 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200290 dapm_widget_invalidate_input_paths(w);
291 }
Mark Browne2d32ff2012-08-31 17:38:32 -0700292 }
293
294 mutex_unlock(&card->dapm_mutex);
295}
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200296EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
Mark Browne2d32ff2012-08-31 17:38:32 -0700297
Richard Purdie2b97eab2006-10-06 18:32:18 +0200298/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100299static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200300 const struct snd_soc_dapm_widget *_widget)
301{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100302 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200303}
304
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200305struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200306 unsigned int value;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200307 struct snd_soc_dapm_widget *widget;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200308 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200309 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200310};
311
312static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100313 struct snd_kcontrol *kcontrol, const char *ctrl_name)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200314{
315 struct dapm_kcontrol_data *data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200316 struct soc_mixer_control *mc;
Charles Keepax561ed682015-05-01 12:37:26 +0100317 struct soc_enum *e;
Charles Keepax773da9b2015-05-01 12:37:25 +0100318 const char *name;
319 int ret;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200320
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200321 data = kzalloc(sizeof(*data), GFP_KERNEL);
Charles Keepax40b7bea2015-05-01 12:37:24 +0100322 if (!data)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200323 return -ENOMEM;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200324
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200325 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200326
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200327 switch (widget->id) {
328 case snd_soc_dapm_switch:
329 case snd_soc_dapm_mixer:
330 case snd_soc_dapm_mixer_named_ctl:
331 mc = (struct soc_mixer_control *)kcontrol->private_value;
332
333 if (mc->autodisable) {
334 struct snd_soc_dapm_widget template;
335
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100336 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
Charles Keepax773da9b2015-05-01 12:37:25 +0100337 "Autodisable");
338 if (!name) {
339 ret = -ENOMEM;
340 goto err_data;
341 }
342
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200343 memset(&template, 0, sizeof(template));
344 template.reg = mc->reg;
345 template.mask = (1 << fls(mc->max)) - 1;
346 template.shift = mc->shift;
347 if (mc->invert)
348 template.off_val = mc->max;
349 else
350 template.off_val = 0;
351 template.on_val = template.off_val;
352 template.id = snd_soc_dapm_kcontrol;
Charles Keepax773da9b2015-05-01 12:37:25 +0100353 template.name = name;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200354
Lars-Peter Clausen2daabd72013-08-30 17:39:33 +0200355 data->value = template.on_val;
356
Liam Girdwood02aa78a2015-05-25 18:21:17 +0100357 data->widget =
358 snd_soc_dapm_new_control_unlocked(widget->dapm,
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200359 &template);
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200360 kfree(name);
Linus Walleijde415c82017-01-13 10:23:52 +0100361 if (IS_ERR(data->widget)) {
362 ret = PTR_ERR(data->widget);
363 goto err_data;
364 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200365 if (!data->widget) {
Charles Keepax773da9b2015-05-01 12:37:25 +0100366 ret = -ENOMEM;
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200367 goto err_data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200368 }
369 }
370 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200371 case snd_soc_dapm_demux:
Charles Keepax561ed682015-05-01 12:37:26 +0100372 case snd_soc_dapm_mux:
373 e = (struct soc_enum *)kcontrol->private_value;
374
375 if (e->autodisable) {
376 struct snd_soc_dapm_widget template;
377
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100378 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
Charles Keepax561ed682015-05-01 12:37:26 +0100379 "Autodisable");
380 if (!name) {
381 ret = -ENOMEM;
382 goto err_data;
383 }
384
385 memset(&template, 0, sizeof(template));
386 template.reg = e->reg;
387 template.mask = e->mask << e->shift_l;
388 template.shift = e->shift_l;
389 template.off_val = snd_soc_enum_item_to_val(e, 0);
390 template.on_val = template.off_val;
391 template.id = snd_soc_dapm_kcontrol;
392 template.name = name;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200393
394 data->value = template.on_val;
395
Charles Keepaxffacb482015-06-26 10:39:43 +0100396 data->widget = snd_soc_dapm_new_control_unlocked(
397 widget->dapm, &template);
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200398 kfree(name);
Linus Walleijde415c82017-01-13 10:23:52 +0100399 if (IS_ERR(data->widget)) {
400 ret = PTR_ERR(data->widget);
401 goto err_data;
402 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200403 if (!data->widget) {
Charles Keepax561ed682015-05-01 12:37:26 +0100404 ret = -ENOMEM;
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200405 goto err_data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200406 }
Charles Keepax561ed682015-05-01 12:37:26 +0100407
408 snd_soc_dapm_add_path(widget->dapm, data->widget,
409 widget, NULL, NULL);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200410 }
411 break;
412 default:
413 break;
414 }
415
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200416 kcontrol->private_data = data;
417
418 return 0;
Charles Keepax773da9b2015-05-01 12:37:25 +0100419
Charles Keepax773da9b2015-05-01 12:37:25 +0100420err_data:
421 kfree(data);
422 return ret;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200423}
424
425static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
426{
427 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Srinivas Kandagatla1a1b2792018-06-04 12:13:26 +0100428
429 list_del(&data->paths);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200430 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200431 kfree(data);
432}
433
434static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
435 const struct snd_kcontrol *kcontrol)
436{
437 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
438
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200439 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200440}
441
442static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
443 struct snd_soc_dapm_widget *widget)
444{
445 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200446 struct snd_soc_dapm_widget_list *new_wlist;
447 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200448
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200449 if (data->wlist)
450 n = data->wlist->num_widgets + 1;
451 else
452 n = 1;
453
454 new_wlist = krealloc(data->wlist,
455 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
456 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200457 return -ENOMEM;
458
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200459 new_wlist->widgets[n - 1] = widget;
460 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200461
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200462 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200463
464 return 0;
465}
466
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200467static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
468 struct snd_soc_dapm_path *path)
469{
470 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
471
472 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200473}
474
475static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
476{
477 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
478
479 if (!data->widget)
480 return true;
481
482 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200483}
484
485static struct list_head *dapm_kcontrol_get_path_list(
486 const struct snd_kcontrol *kcontrol)
487{
488 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
489
490 return &data->paths;
491}
492
493#define dapm_kcontrol_for_each_path(path, kcontrol) \
494 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
495 list_kcontrol)
496
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530497unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200498{
499 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
500
501 return data->value;
502}
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530503EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200504
505static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
506 unsigned int value)
507{
508 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
509
510 if (data->value == value)
511 return false;
512
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200513 if (data->widget)
514 data->widget->on_val = value;
515
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200516 data->value = value;
517
518 return true;
519}
520
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200521/**
Mythri P K93e39a12015-10-20 22:30:08 +0530522 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
523 * kcontrol
524 * @kcontrol: The kcontrol
525 */
526struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
527 struct snd_kcontrol *kcontrol)
528{
529 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
530}
531EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
532
533/**
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200534 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
535 * kcontrol
536 * @kcontrol: The kcontrol
537 *
538 * Note: This function must only be used on kcontrols that are known to have
539 * been registered for a CODEC. Otherwise the behaviour is undefined.
540 */
541struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
542 struct snd_kcontrol *kcontrol)
543{
544 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
545}
546EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
547
Liam Girdwood6c120e12012-02-15 15:15:34 +0000548static void dapm_reset(struct snd_soc_card *card)
549{
550 struct snd_soc_dapm_widget *w;
551
Mark Brownf9fa2b12014-03-06 16:49:11 +0800552 lockdep_assert_held(&card->dapm_mutex);
553
Liam Girdwood6c120e12012-02-15 15:15:34 +0000554 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
555
556 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200557 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000558 w->power_checked = false;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000559 }
560}
561
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200562static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
563{
564 if (!dapm->component)
565 return NULL;
566 return dapm->component->name_prefix;
567}
568
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200569static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800570 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100571{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200572 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200573 return -EIO;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200574 return snd_soc_component_read(dapm->component, reg, value);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100575}
576
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200577static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
Bard Liao34775012014-04-17 20:12:56 +0800578 int reg, unsigned int mask, unsigned int value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100579{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200580 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200581 return -EIO;
Mark Brownfcf6c5e2014-12-15 13:08:48 +0000582 return snd_soc_component_update_bits(dapm->component, reg,
583 mask, value);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000584}
585
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200586static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
587 int reg, unsigned int mask, unsigned int value)
588{
589 if (!dapm->component)
590 return -EIO;
591 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
592}
593
Mark Browneb270e92013-10-09 13:52:52 +0100594static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
595{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200596 if (dapm->component)
597 snd_soc_component_async_complete(dapm->component);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100598}
599
Charles Keepax45a110a2015-05-11 13:50:30 +0100600static struct snd_soc_dapm_widget *
601dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
602{
603 struct snd_soc_dapm_widget *w = wcache->widget;
604 struct list_head *wlist;
605 const int depth = 2;
606 int i = 0;
607
608 if (w) {
609 wlist = &w->dapm->card->widgets;
610
611 list_for_each_entry_from(w, wlist, list) {
612 if (!strcmp(name, w->name))
613 return w;
614
615 if (++i == depth)
616 break;
617 }
618 }
619
620 return NULL;
621}
622
623static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
624 struct snd_soc_dapm_widget *w)
625{
626 wcache->widget = w;
627}
628
Mark Brown452c5ea2009-05-17 21:41:23 +0100629/**
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200630 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
631 * @dapm: The DAPM context for which to set the level
632 * @level: The level to set
633 *
634 * Forces the DAPM bias level to a specific state. It will call the bias level
635 * callback of DAPM context with the specified level. This will even happen if
636 * the context is already at the same level. Furthermore it will not go through
637 * the normal bias level sequencing, meaning any intermediate states between the
638 * current and the target state will not be entered.
639 *
640 * Note that the change in bias level is only temporary and the next time
641 * snd_soc_dapm_sync() is called the state will be set to the level as
642 * determined by the DAPM core. The function is mainly intended to be used to
643 * used during probe or resume from suspend to power up the device so
644 * initialization can be done, before the DAPM core takes over.
645 */
646int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
647 enum snd_soc_bias_level level)
648{
649 int ret = 0;
650
651 if (dapm->set_bias_level)
652 ret = dapm->set_bias_level(dapm, level);
653
Lars-Peter Clausenf4bf8d72015-04-27 22:13:25 +0200654 if (ret == 0)
655 dapm->bias_level = level;
656
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200657 return ret;
658}
659EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
660
Mark Brown452c5ea2009-05-17 21:41:23 +0100661/**
662 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800663 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100664 * @level: level to configure
665 *
666 * Configure the bias (power) levels for the SoC audio device.
667 *
668 * Returns 0 for success else error.
669 */
Mark Browned5a4c42011-02-18 11:12:42 -0800670static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200671 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100672{
Mark Browned5a4c42011-02-18 11:12:42 -0800673 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100674 int ret = 0;
675
Mark Brown84e90932010-11-04 00:07:02 -0400676 trace_snd_soc_bias_level_start(card, level);
677
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000678 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100679 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100680 if (ret != 0)
681 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100682
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200683 if (!card || dapm != &card->dapm)
684 ret = snd_soc_dapm_force_bias_level(dapm, level);
Liam Girdwood41231282012-07-06 16:56:16 +0100685
Mark Brown171ec6b2011-06-06 18:15:19 +0100686 if (ret != 0)
687 goto out;
688
689 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100690 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100691out:
Mark Brown84e90932010-11-04 00:07:02 -0400692 trace_snd_soc_bias_level_done(card, level);
693
Mark Brown452c5ea2009-05-17 21:41:23 +0100694 return ret;
695}
696
Mark Brown74b8f952009-06-06 11:26:15 +0100697/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200698static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200699 struct snd_soc_dapm_path *path, const char *control_name,
700 struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200701{
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200702 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200703 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100704 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200705 int i;
706
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100707 if (e->reg != SND_SOC_NOPM) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200708 soc_dapm_read(dapm, e->reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100709 val = (val >> e->shift_l) & e->mask;
710 item = snd_soc_enum_val_to_item(e, val);
711 } else {
712 /* since a virtual mux has no backing registers to
713 * decide which path to connect, it will try to match
714 * with the first enumeration. This is to ensure
715 * that the default mux choice (the first) will be
716 * correctly powered up during initialization.
717 */
718 item = 0;
719 }
720
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100721 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200722 if (!(strcmp(control_name, e->texts[i]))) {
Rasmus Villemoes98ad73c2014-10-21 17:01:15 +0200723 path->name = e->texts[i];
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100724 if (i == item)
725 path->connect = 1;
726 else
727 path->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200728 return 0;
729 }
730 }
731
732 return -ENODEV;
733}
734
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100735/* set up initial codec paths */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200736static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i)
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100737{
738 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200739 p->sink->kcontrol_news[i].private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100740 unsigned int reg = mc->reg;
741 unsigned int shift = mc->shift;
742 unsigned int max = mc->max;
743 unsigned int mask = (1 << fls(max)) - 1;
744 unsigned int invert = mc->invert;
745 unsigned int val;
746
747 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200748 soc_dapm_read(p->sink->dapm, reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100749 val = (val >> shift) & mask;
750 if (invert)
751 val = max - val;
752 p->connect = !!val;
753 } else {
754 p->connect = 0;
755 }
756}
757
Mark Brown74b8f952009-06-06 11:26:15 +0100758/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200759static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200760 struct snd_soc_dapm_path *path, const char *control_name)
761{
762 int i;
763
764 /* search for mixer kcontrol */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200765 for (i = 0; i < path->sink->num_kcontrols; i++) {
766 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
767 path->name = path->sink->kcontrol_news[i].name;
768 dapm_set_mixer_path_status(path, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200769 return 0;
770 }
771 }
772 return -ENODEV;
773}
774
Stephen Warrenaf468002011-04-28 17:38:01 -0600775static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600776 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600777 const struct snd_kcontrol_new *kcontrol_new,
778 struct snd_kcontrol **kcontrol)
779{
780 struct snd_soc_dapm_widget *w;
781 int i;
782
783 *kcontrol = NULL;
784
785 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600786 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
787 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600788 for (i = 0; i < w->num_kcontrols; i++) {
789 if (&w->kcontrol_news[i] == kcontrol_new) {
790 if (w->kcontrols)
791 *kcontrol = w->kcontrols[i];
792 return 1;
793 }
794 }
795 }
796
797 return 0;
798}
799
Stephen Warren85762e72013-03-29 15:40:10 -0600800/*
801 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
802 * create it. Either way, add the widget into the control's widget list
803 */
Jeeja KP19a2557b2015-10-20 22:30:06 +0530804static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100805 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200806{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200807 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000808 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000809 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600810 size_t prefix_len;
811 int shared;
812 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600813 bool wname_in_long_name, kcname_in_long_name;
Daniel Macke5092c92014-10-07 13:41:24 +0200814 char *long_name = NULL;
Stephen Warren85762e72013-03-29 15:40:10 -0600815 const char *name;
Daniel Macke5092c92014-10-07 13:41:24 +0200816 int ret = 0;
Mark Brownefb7ac32011-03-08 17:23:24 +0000817
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200818 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000819 if (prefix)
820 prefix_len = strlen(prefix) + 1;
821 else
822 prefix_len = 0;
823
Stephen Warren85762e72013-03-29 15:40:10 -0600824 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
825 &kcontrol);
826
Stephen Warren85762e72013-03-29 15:40:10 -0600827 if (!kcontrol) {
828 if (shared) {
829 wname_in_long_name = false;
830 kcname_in_long_name = true;
831 } else {
832 switch (w->id) {
833 case snd_soc_dapm_switch:
834 case snd_soc_dapm_mixer:
Jeeja KP19a2557b2015-10-20 22:30:06 +0530835 case snd_soc_dapm_pga:
Chen-Yu Tsaia3930ed2016-08-27 19:28:00 +0800836 case snd_soc_dapm_out_drv:
Stephen Warren85762e72013-03-29 15:40:10 -0600837 wname_in_long_name = true;
838 kcname_in_long_name = true;
839 break;
840 case snd_soc_dapm_mixer_named_ctl:
841 wname_in_long_name = false;
842 kcname_in_long_name = true;
843 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200844 case snd_soc_dapm_demux:
Stephen Warren85762e72013-03-29 15:40:10 -0600845 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600846 wname_in_long_name = true;
847 kcname_in_long_name = false;
848 break;
849 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600850 return -EINVAL;
851 }
852 }
853
854 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600855 /*
856 * The control will get a prefix from the control
857 * creation process but we're also using the same
858 * prefix for widgets so cut the prefix off the
859 * front of the widget name.
860 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200861 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600862 w->name + prefix_len,
863 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200864 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200865 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600866
867 name = long_name;
868 } else if (wname_in_long_name) {
869 long_name = NULL;
870 name = w->name + prefix_len;
871 } else {
872 long_name = NULL;
873 name = w->kcontrol_news[kci].name;
874 }
875
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200876 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600877 prefix);
Daniel Macke5092c92014-10-07 13:41:24 +0200878 if (!kcontrol) {
879 ret = -ENOMEM;
880 goto exit_free;
881 }
882
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200883 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200884
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100885 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200886 if (ret) {
887 snd_ctl_free_one(kcontrol);
Daniel Macke5092c92014-10-07 13:41:24 +0200888 goto exit_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200889 }
890
Stephen Warren85762e72013-03-29 15:40:10 -0600891 ret = snd_ctl_add(card, kcontrol);
892 if (ret < 0) {
893 dev_err(dapm->dev,
894 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
895 w->name, name, ret);
Daniel Macke5092c92014-10-07 13:41:24 +0200896 goto exit_free;
Stephen Warren85762e72013-03-29 15:40:10 -0600897 }
Stephen Warren85762e72013-03-29 15:40:10 -0600898 }
899
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200900 ret = dapm_kcontrol_add_widget(kcontrol, w);
Daniel Macke5092c92014-10-07 13:41:24 +0200901 if (ret == 0)
902 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200903
Daniel Macke5092c92014-10-07 13:41:24 +0200904exit_free:
905 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600906
Daniel Macke5092c92014-10-07 13:41:24 +0200907 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600908}
909
910/* create new dapm mixer control */
911static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
912{
913 int i, ret;
914 struct snd_soc_dapm_path *path;
Charles Keepax561ed682015-05-01 12:37:26 +0100915 struct dapm_kcontrol_data *data;
Stephen Warren85762e72013-03-29 15:40:10 -0600916
Richard Purdie2b97eab2006-10-06 18:32:18 +0200917 /* add kcontrol */
918 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200919 /* match name */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +0200920 snd_soc_dapm_widget_for_each_source_path(w, path) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200921 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600922 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200923 continue;
924
Charles Keepax561ed682015-05-01 12:37:26 +0100925 if (!w->kcontrols[i]) {
Jeeja KP19a2557b2015-10-20 22:30:06 +0530926 ret = dapm_create_or_share_kcontrol(w, i);
Charles Keepax561ed682015-05-01 12:37:26 +0100927 if (ret < 0)
928 return ret;
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200929 }
930
Mark Brown946d92a2013-08-12 23:28:42 +0100931 dapm_kcontrol_add_path(w->kcontrols[i], path);
Charles Keepax561ed682015-05-01 12:37:26 +0100932
933 data = snd_kcontrol_chip(w->kcontrols[i]);
934 if (data->widget)
935 snd_soc_dapm_add_path(data->widget->dapm,
936 data->widget,
937 path->source,
938 NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200939 }
940 }
Stephen Warren85762e72013-03-29 15:40:10 -0600941
942 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200943}
944
945/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200946static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200947{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200948 struct snd_soc_dapm_context *dapm = w->dapm;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200949 enum snd_soc_dapm_direction dir;
Stephen Warren85762e72013-03-29 15:40:10 -0600950 struct snd_soc_dapm_path *path;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200951 const char *type;
Stephen Warrenaf468002011-04-28 17:38:01 -0600952 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200953
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200954 switch (w->id) {
955 case snd_soc_dapm_mux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200956 dir = SND_SOC_DAPM_DIR_OUT;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200957 type = "mux";
958 break;
959 case snd_soc_dapm_demux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200960 dir = SND_SOC_DAPM_DIR_IN;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200961 type = "demux";
962 break;
963 default:
964 return -EINVAL;
965 }
966
Stephen Warrenaf468002011-04-28 17:38:01 -0600967 if (w->num_kcontrols != 1) {
968 dev_err(dapm->dev,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200969 "ASoC: %s %s has incorrect number of controls\n", type,
Stephen Warrenaf468002011-04-28 17:38:01 -0600970 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200971 return -EINVAL;
972 }
973
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200974 if (list_empty(&w->edges[dir])) {
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200975 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
Stephen Warren85762e72013-03-29 15:40:10 -0600976 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600977 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200978
Jeeja KP19a2557b2015-10-20 22:30:06 +0530979 ret = dapm_create_or_share_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600980 if (ret < 0)
981 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600982
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200983 snd_soc_dapm_widget_for_each_path(w, dir, path) {
984 if (path->name)
985 dapm_kcontrol_add_path(w->kcontrols[0], path);
Lars-Peter Clausen98407ef2014-10-25 17:41:57 +0200986 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200987
Stephen Warrenaf468002011-04-28 17:38:01 -0600988 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200989}
990
991/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200992static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200993{
Jeeja KP19a2557b2015-10-20 22:30:06 +0530994 int i, ret;
995
996 for (i = 0; i < w->num_kcontrols; i++) {
997 ret = dapm_create_or_share_kcontrol(w, i);
998 if (ret < 0)
999 return ret;
1000 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001001
Mark Browna6c65732010-03-03 17:45:21 +00001002 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001003}
1004
Nikesh Oswalc6615082015-02-02 17:06:44 +00001005/* create new dapm dai link control */
1006static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1007{
1008 int i, ret;
1009 struct snd_kcontrol *kcontrol;
1010 struct snd_soc_dapm_context *dapm = w->dapm;
1011 struct snd_card *card = dapm->card->snd_card;
1012
1013 /* create control for links with > 1 config */
1014 if (w->num_params <= 1)
1015 return 0;
1016
1017 /* add kcontrol */
1018 for (i = 0; i < w->num_kcontrols; i++) {
1019 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1020 w->name, NULL);
1021 ret = snd_ctl_add(card, kcontrol);
1022 if (ret < 0) {
1023 dev_err(dapm->dev,
1024 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1025 w->name, w->kcontrol_news[i].name, ret);
1026 return ret;
1027 }
1028 kcontrol->private_data = w;
1029 w->kcontrols[i] = kcontrol;
1030 }
1031
1032 return 0;
1033}
1034
Mark Brown99497882010-05-07 20:24:05 +01001035/* We implement power down on suspend by checking the power state of
1036 * the ALSA card - when we are suspending the ALSA state for the card
1037 * is set to D3.
1038 */
1039static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1040{
Mark Brown12ea2c72011-03-02 18:17:32 +00001041 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +01001042
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001043 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +01001044 case SNDRV_CTL_POWER_D3hot:
1045 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +01001046 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001047 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001048 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +01001049 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +01001050 default:
1051 return 1;
1052 }
1053}
1054
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001055static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1056 struct list_head *widgets)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001057{
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001058 struct snd_soc_dapm_widget *w;
1059 struct list_head *it;
1060 unsigned int size = 0;
1061 unsigned int i = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001062
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001063 list_for_each(it, widgets)
1064 size++;
1065
1066 *list = kzalloc(sizeof(**list) + size * sizeof(*w), GFP_KERNEL);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001067 if (*list == NULL)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001068 return -ENOMEM;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001069
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001070 list_for_each_entry(w, widgets, work_list)
1071 (*list)->widgets[i++] = w;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001072
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001073 (*list)->num_widgets = i;
1074
1075 return 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001076}
1077
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001078/*
1079 * Common implementation for is_connected_output_ep() and
1080 * is_connected_input_ep(). The function is inlined since the combined size of
1081 * the two specialized functions is only marginally larger then the size of the
1082 * generic function and at the same time the fast path of the specialized
1083 * functions is significantly smaller than the generic function.
1084 */
1085static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1086 struct list_head *list, enum snd_soc_dapm_direction dir,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001087 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1088 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1089 enum snd_soc_dapm_direction)),
1090 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1091 enum snd_soc_dapm_direction))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001092{
1093 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001094 struct snd_soc_dapm_path *path;
1095 int con = 0;
1096
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001097 if (widget->endpoints[dir] >= 0)
1098 return widget->endpoints[dir];
Mark Brown024dc072011-10-09 11:52:05 +01001099
Mark Brownde02d072011-09-20 21:43:24 +01001100 DAPM_UPDATE_STAT(widget, path_checks);
1101
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001102 /* do we need to add this widget to the list ? */
1103 if (list)
1104 list_add_tail(&widget->work_list, list);
1105
Jeeja KP09464972016-06-15 11:16:55 +05301106 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1107 widget->endpoints[dir] = 1;
1108 return widget->endpoints[dir];
1109 }
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001110
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001111 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1112 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1113 return widget->endpoints[dir];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001114 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001115
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001116 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
Mark Browne56235e2011-09-21 18:19:14 +01001117 DAPM_UPDATE_STAT(widget, neighbour_checks);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001118
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001119 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +01001120 continue;
1121
Mark Brown8af294b2013-02-22 17:48:15 +00001122 if (path->walking)
1123 return 1;
1124
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001125 trace_snd_soc_dapm_path(widget, dir, path);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001126
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001127 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +00001128 path->walking = 1;
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001129 con += fn(path->node[dir], list, custom_stop_condition);
Mark Brown8af294b2013-02-22 17:48:15 +00001130 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001131 }
1132 }
1133
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001134 widget->endpoints[dir] = con;
Mark Brown024dc072011-10-09 11:52:05 +01001135
Richard Purdie2b97eab2006-10-06 18:32:18 +02001136 return con;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001137}
1138
1139/*
1140 * Recursively check for a completed path to an active or physically connected
1141 * output widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001142 *
1143 * Optionally, can be supplied with a function acting as a stopping condition.
1144 * This function takes the dapm widget currently being examined and the walk
1145 * direction as an arguments, it should return true if the walk should be
1146 * stopped and false otherwise.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001147 */
1148static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001149 struct list_head *list,
1150 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1151 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001152{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001153 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001154 is_connected_output_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001155}
1156
1157/*
1158 * Recursively check for a completed path to an active or physically connected
1159 * input widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001160 *
1161 * Optionally, can be supplied with a function acting as a stopping condition.
1162 * This function takes the dapm widget currently being examined and the walk
1163 * direction as an arguments, it should return true if the walk should be
1164 * stopped and false otherwise.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001165 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001166static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001167 struct list_head *list,
1168 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1169 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001170{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001171 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001172 is_connected_input_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001173}
1174
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001175/**
1176 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1177 * @dai: the soc DAI.
1178 * @stream: stream direction.
1179 * @list: list of active widgets for this stream.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001180 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1181 * walk based on custom logic.
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001182 *
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001183 * Queries DAPM graph as to whether a valid audio stream path exists for
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001184 * the initial stream specified by name. This takes into account
1185 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1186 *
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001187 * Optionally, can be supplied with a function acting as a stopping condition.
1188 * This function takes the dapm widget currently being examined and the walk
1189 * direction as an arguments, it should return true if the walk should be
1190 * stopped and false otherwise.
1191 *
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001192 * Returns the number of valid paths or negative error.
1193 */
1194int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001195 struct snd_soc_dapm_widget_list **list,
1196 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1197 enum snd_soc_dapm_direction))
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001198{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01001199 struct snd_soc_card *card = dai->component->card;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001200 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001201 LIST_HEAD(widgets);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001202 int paths;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001203 int ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001204
1205 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001206
1207 /*
1208 * For is_connected_{output,input}_ep fully discover the graph we need
1209 * to reset the cached number of inputs and outputs.
1210 */
1211 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001212 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1213 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001214 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001215
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001216 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001217 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1218 custom_stop_condition);
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001219 else
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001220 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1221 custom_stop_condition);
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001222
1223 /* Drop starting point */
1224 list_del(widgets.next);
1225
1226 ret = dapm_widget_list_create(list, &widgets);
1227 if (ret)
Lars-Peter Clausen30abbe72015-08-11 21:37:59 +02001228 paths = ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001229
1230 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001231 mutex_unlock(&card->dapm_mutex);
1232
1233 return paths;
1234}
1235
Richard Purdie2b97eab2006-10-06 18:32:18 +02001236/*
Mark Brown62ea8742012-01-21 21:14:48 +00001237 * Handler for regulator supply widget.
1238 */
1239int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1240 struct snd_kcontrol *kcontrol, int event)
1241{
Mark Brownc05b84d2012-09-07 12:57:11 +08001242 int ret;
1243
Mark Browneb270e92013-10-09 13:52:52 +01001244 soc_dapm_async_complete(w->dapm);
1245
Mark Brownc05b84d2012-09-07 12:57:11 +08001246 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001247 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001248 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001249 if (ret != 0)
1250 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001251 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001252 w->name, ret);
1253 }
1254
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001255 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001256 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001257 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001258 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001259 if (ret != 0)
1260 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001261 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001262 w->name, ret);
1263 }
1264
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001265 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001266 }
Mark Brown62ea8742012-01-21 21:14:48 +00001267}
1268EXPORT_SYMBOL_GPL(dapm_regulator_event);
1269
Ola Liljad7e7eb92012-05-24 15:26:25 +02001270/*
1271 * Handler for clock supply widget.
1272 */
1273int dapm_clock_event(struct snd_soc_dapm_widget *w,
1274 struct snd_kcontrol *kcontrol, int event)
1275{
1276 if (!w->clk)
1277 return -EIO;
1278
Mark Browneb270e92013-10-09 13:52:52 +01001279 soc_dapm_async_complete(w->dapm);
1280
Mark Brownec029952012-06-04 08:16:20 +01001281#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001282 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001283 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001284 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001285 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001286 return 0;
1287 }
Mark Brownec029952012-06-04 08:16:20 +01001288#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001289 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001290}
1291EXPORT_SYMBOL_GPL(dapm_clock_event);
1292
Mark Brownd8050022011-09-28 18:28:23 +01001293static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1294{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001295 if (w->power_checked)
1296 return w->new_power;
1297
Mark Brownd8050022011-09-28 18:28:23 +01001298 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001299 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001300 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001301 w->new_power = w->power_check(w);
1302
1303 w->power_checked = true;
1304
1305 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001306}
1307
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001308/* Generic check to see if a widget should be powered. */
Mark Browncd0f2d42009-04-20 16:56:59 +01001309static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1310{
1311 int in, out;
1312
Mark Brownde02d072011-09-20 21:43:24 +01001313 DAPM_UPDATE_STAT(w, power_checks);
1314
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001315 in = is_connected_input_ep(w, NULL, NULL);
1316 out = is_connected_output_ep(w, NULL, NULL);
Mark Browncd0f2d42009-04-20 16:56:59 +01001317 return out != 0 && in != 0;
1318}
1319
Mark Brown246d0a12009-04-22 18:24:55 +01001320/* Check to see if a power supply is needed */
1321static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1322{
1323 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001324
Mark Brownde02d072011-09-20 21:43:24 +01001325 DAPM_UPDATE_STAT(w, power_checks);
1326
Mark Brown246d0a12009-04-22 18:24:55 +01001327 /* Check if one of our outputs is connected */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001328 snd_soc_dapm_widget_for_each_sink_path(w, path) {
Mark Browna8fdac82011-09-28 18:20:26 +01001329 DAPM_UPDATE_STAT(w, neighbour_checks);
1330
Mark Brownbf3a9e12011-06-13 16:42:29 +01001331 if (path->weak)
1332 continue;
1333
Mark Brown215edda2009-09-08 18:59:05 +01001334 if (path->connected &&
1335 !path->connected(path->source, path->sink))
1336 continue;
1337
Mark Brownf68d7e12011-10-04 22:57:50 +01001338 if (dapm_widget_power_check(path->sink))
1339 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001340 }
1341
Mark Brownf68d7e12011-10-04 22:57:50 +01001342 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001343}
1344
Mark Brown35c64bc2011-09-28 18:23:53 +01001345static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1346{
Charles Keepax20bb0182015-12-02 10:22:16 +00001347 return w->connected;
Mark Brown35c64bc2011-09-28 18:23:53 +01001348}
1349
Mark Brown38357ab2009-06-06 19:03:23 +01001350static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1351 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001352 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001353{
Mark Brown828a8422011-01-15 13:14:30 +00001354 int *sort;
1355
1356 if (power_up)
1357 sort = dapm_up_seq;
1358 else
1359 sort = dapm_down_seq;
1360
Mark Brown38357ab2009-06-06 19:03:23 +01001361 if (sort[a->id] != sort[b->id])
1362 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001363 if (a->subseq != b->subseq) {
1364 if (power_up)
1365 return a->subseq - b->subseq;
1366 else
1367 return b->subseq - a->subseq;
1368 }
Mark Brownb22ead22009-06-07 12:51:26 +01001369 if (a->reg != b->reg)
1370 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001371 if (a->dapm != b->dapm)
1372 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001373
Mark Brown38357ab2009-06-06 19:03:23 +01001374 return 0;
1375}
Mark Brown42aa3412009-03-01 19:21:10 +00001376
Mark Brown38357ab2009-06-06 19:03:23 +01001377/* Insert a widget in order into a DAPM power sequence. */
1378static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1379 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001380 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001381{
1382 struct snd_soc_dapm_widget *w;
1383
1384 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001385 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001386 list_add_tail(&new_widget->power_list, &w->power_list);
1387 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001388 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001389
Mark Brown38357ab2009-06-06 19:03:23 +01001390 list_add_tail(&new_widget->power_list, list);
1391}
Mark Brown42aa3412009-03-01 19:21:10 +00001392
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001393static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001394 struct snd_soc_dapm_widget *w, int event)
1395{
Mark Brown68f89ad2010-11-03 23:51:49 -04001396 const char *ev_name;
1397 int power, ret;
1398
1399 switch (event) {
1400 case SND_SOC_DAPM_PRE_PMU:
1401 ev_name = "PRE_PMU";
1402 power = 1;
1403 break;
1404 case SND_SOC_DAPM_POST_PMU:
1405 ev_name = "POST_PMU";
1406 power = 1;
1407 break;
1408 case SND_SOC_DAPM_PRE_PMD:
1409 ev_name = "PRE_PMD";
1410 power = 0;
1411 break;
1412 case SND_SOC_DAPM_POST_PMD:
1413 ev_name = "POST_PMD";
1414 power = 0;
1415 break;
Mark Brown80114122013-02-25 15:14:19 +00001416 case SND_SOC_DAPM_WILL_PMU:
1417 ev_name = "WILL_PMU";
1418 power = 1;
1419 break;
1420 case SND_SOC_DAPM_WILL_PMD:
1421 ev_name = "WILL_PMD";
1422 power = 0;
1423 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001424 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001425 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001426 return;
1427 }
1428
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001429 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001430 return;
1431
1432 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001433 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001434 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001435 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001436 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001437 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001438 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001439 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001440 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001441 ev_name, w->name, ret);
1442 }
1443}
1444
Mark Brownb22ead22009-06-07 12:51:26 +01001445/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001446static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001447 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001448{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001449 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001450 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001451 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001452 unsigned int value = 0;
1453 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001454
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001455 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1456 reg = w->reg;
1457 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001458
1459 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001460 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001461 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001462
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001463 mask |= w->mask << w->shift;
1464 if (w->power)
1465 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001466 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001467 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001468
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001469 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001470 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1471 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001472
Mark Brown68f89ad2010-11-03 23:51:49 -04001473 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001474 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1475 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001476 }
1477
Mark Brown81628102009-06-07 13:21:24 +01001478 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001479 /* Any widget will do, they should all be updating the
1480 * same register.
1481 */
Mark Brown29376bc2011-06-19 13:49:28 +01001482
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001483 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001484 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001485 value, mask, reg, card->pop_time);
1486 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001487 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001488 }
1489
1490 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001491 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1492 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001493 }
Mark Brown42aa3412009-03-01 19:21:10 +00001494}
1495
Mark Brownb22ead22009-06-07 12:51:26 +01001496/* Apply a DAPM power sequence.
1497 *
1498 * We walk over a pre-sorted list of widgets to apply power to. In
1499 * order to minimise the number of writes to the device required
1500 * multiple widgets will be updated in a single write where possible.
1501 * Currently anything that requires more than a single write is not
1502 * handled.
1503 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001504static void dapm_seq_run(struct snd_soc_card *card,
1505 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001506{
1507 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001508 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001509 LIST_HEAD(pending);
1510 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001511 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001512 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001513 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001514 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001515 int *sort;
1516
1517 if (power_up)
1518 sort = dapm_up_seq;
1519 else
1520 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001521
Mark Brownb22ead22009-06-07 12:51:26 +01001522 list_for_each_entry_safe(w, n, list, power_list) {
1523 ret = 0;
1524
1525 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001526 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001527 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001528 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001529 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001530
Mark Brown474b62d2011-01-18 16:14:44 +00001531 if (cur_dapm && cur_dapm->seq_notifier) {
1532 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1533 if (sort[i] == cur_sort)
1534 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001535 i,
1536 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001537 }
1538
Mark Browneb270e92013-10-09 13:52:52 +01001539 if (cur_dapm && w->dapm != cur_dapm)
1540 soc_dapm_async_complete(cur_dapm);
1541
Mark Brownb22ead22009-06-07 12:51:26 +01001542 INIT_LIST_HEAD(&pending);
1543 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001544 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001545 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001546 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001547 }
1548
Mark Brown163cac02009-06-07 10:12:52 +01001549 switch (w->id) {
1550 case snd_soc_dapm_pre:
1551 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001552 list_for_each_entry_safe_continue(w, n, list,
1553 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001554
Mark Brownb22ead22009-06-07 12:51:26 +01001555 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001556 ret = w->event(w,
1557 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001558 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001559 ret = w->event(w,
1560 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001561 break;
1562
1563 case snd_soc_dapm_post:
1564 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001565 list_for_each_entry_safe_continue(w, n, list,
1566 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001567
Mark Brownb22ead22009-06-07 12:51:26 +01001568 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001569 ret = w->event(w,
1570 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001571 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001572 ret = w->event(w,
1573 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001574 break;
1575
Mark Brown163cac02009-06-07 10:12:52 +01001576 default:
Mark Brown81628102009-06-07 13:21:24 +01001577 /* Queue it up for application */
1578 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001579 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001580 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001581 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001582 list_move(&w->power_list, &pending);
1583 break;
Mark Brown163cac02009-06-07 10:12:52 +01001584 }
Mark Brownb22ead22009-06-07 12:51:26 +01001585
1586 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001587 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001588 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001589 }
Mark Brownb22ead22009-06-07 12:51:26 +01001590
1591 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001592 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001593
1594 if (cur_dapm && cur_dapm->seq_notifier) {
1595 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1596 if (sort[i] == cur_sort)
1597 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001598 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001599 }
Mark Browneb270e92013-10-09 13:52:52 +01001600
1601 list_for_each_entry(d, &card->dapm_list, list) {
1602 soc_dapm_async_complete(d);
1603 }
Mark Brown163cac02009-06-07 10:12:52 +01001604}
1605
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001606static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001607{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001608 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001609 struct snd_soc_dapm_widget_list *wlist;
1610 struct snd_soc_dapm_widget *w = NULL;
1611 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001612 int ret;
1613
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001614 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001615 return;
1616
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001617 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001618
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001619 for (wi = 0; wi < wlist->num_widgets; wi++) {
1620 w = wlist->widgets[wi];
1621
1622 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1623 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1624 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001625 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001626 w->name, ret);
1627 }
Mark Brown97404f22010-12-14 16:13:57 +00001628 }
1629
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001630 if (!w)
1631 return;
1632
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001633 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1634 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001635 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001636 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001637 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001638
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001639 for (wi = 0; wi < wlist->num_widgets; wi++) {
1640 w = wlist->widgets[wi];
1641
1642 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1643 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1644 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001645 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001646 w->name, ret);
1647 }
Mark Brown97404f22010-12-14 16:13:57 +00001648 }
1649}
1650
Mark Brown9d0624a2011-02-18 11:49:43 -08001651/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1652 * they're changing state.
1653 */
1654static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1655{
1656 struct snd_soc_dapm_context *d = data;
1657 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001658
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001659 /* If we're off and we're not supposed to go into STANDBY */
Mark Brown56fba412011-06-04 11:25:10 +01001660 if (d->bias_level == SND_SOC_BIAS_OFF &&
1661 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001662 if (d->dev)
1663 pm_runtime_get_sync(d->dev);
1664
Mark Brown9d0624a2011-02-18 11:49:43 -08001665 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1666 if (ret != 0)
1667 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001668 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001669 }
1670
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001671 /* Prepare for a transition to ON or away from ON */
1672 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1673 d->bias_level != SND_SOC_BIAS_ON) ||
1674 (d->target_bias_level != SND_SOC_BIAS_ON &&
1675 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001676 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1677 if (ret != 0)
1678 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001679 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001680 }
1681}
1682
1683/* Async callback run prior to DAPM sequences - brings to their final
1684 * state.
1685 */
1686static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1687{
1688 struct snd_soc_dapm_context *d = data;
1689 int ret;
1690
1691 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001692 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1693 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1694 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001695 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1696 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001697 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001698 ret);
1699 }
1700
1701 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001702 if (d->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_OFF);
1705 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001706 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1707 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001708
1709 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001710 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001711 }
1712
1713 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001714 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1715 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001716 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1717 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001718 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001719 ret);
1720 }
1721}
Mark Brown97404f22010-12-14 16:13:57 +00001722
Mark Brownfe4fda52011-10-03 22:36:57 +01001723static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1724 bool power, bool connect)
1725{
1726 /* If a connection is being made or broken then that update
1727 * will have marked the peer dirty, otherwise the widgets are
1728 * not connected and this update has no impact. */
1729 if (!connect)
1730 return;
1731
1732 /* If the peer is already in the state we're moving to then we
1733 * won't have an impact on it. */
1734 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001735 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001736}
1737
Mark Brown05623c42011-09-28 17:02:31 +01001738static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1739 struct list_head *up_list,
1740 struct list_head *down_list)
1741{
Mark Browndb432b42011-10-03 21:06:40 +01001742 struct snd_soc_dapm_path *path;
1743
Mark Brown05623c42011-09-28 17:02:31 +01001744 if (w->power == power)
1745 return;
1746
1747 trace_snd_soc_dapm_widget_power(w, power);
1748
Mark Browndb432b42011-10-03 21:06:40 +01001749 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001750 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001751 */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001752 snd_soc_dapm_widget_for_each_source_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001753 dapm_widget_set_peer_power(path->source, power, path->connect);
1754
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001755 /* Supplies can't affect their outputs, only their inputs */
1756 if (!w->is_supply) {
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001757 snd_soc_dapm_widget_for_each_sink_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001758 dapm_widget_set_peer_power(path->sink, power,
1759 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001760 }
1761
Mark Brown05623c42011-09-28 17:02:31 +01001762 if (power)
1763 dapm_seq_insert(w, up_list, true);
1764 else
1765 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001766}
1767
Mark Brown7c81beb2011-09-20 22:22:32 +01001768static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1769 struct list_head *up_list,
1770 struct list_head *down_list)
1771{
Mark Brown7c81beb2011-09-20 22:22:32 +01001772 int power;
1773
1774 switch (w->id) {
1775 case snd_soc_dapm_pre:
1776 dapm_seq_insert(w, down_list, false);
1777 break;
1778 case snd_soc_dapm_post:
1779 dapm_seq_insert(w, up_list, true);
1780 break;
1781
1782 default:
Mark Brownd8050022011-09-28 18:28:23 +01001783 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001784
Mark Brown05623c42011-09-28 17:02:31 +01001785 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001786 break;
1787 }
1788}
1789
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001790static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1791{
1792 if (dapm->idle_bias_off)
1793 return true;
1794
1795 switch (snd_power_get_state(dapm->card->snd_card)) {
1796 case SNDRV_CTL_POWER_D3hot:
1797 case SNDRV_CTL_POWER_D3cold:
1798 return dapm->suspend_bias_off;
1799 default:
1800 break;
1801 }
1802
1803 return false;
1804}
1805
Mark Brown42aa3412009-03-01 19:21:10 +00001806/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001807 * Scan each dapm widget for complete audio path.
1808 * A complete path is a route that has valid endpoints i.e.:-
1809 *
1810 * o DAC to output pin.
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001811 * o Input pin to ADC.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001812 * o Input pin to Output pin (bypass, sidetone)
1813 * o DAC to ADC (loopback).
1814 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001815static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001816{
1817 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001818 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001819 LIST_HEAD(up_list);
1820 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001821 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001822 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001823
Mark Brownf9fa2b12014-03-06 16:49:11 +08001824 lockdep_assert_held(&card->dapm_mutex);
1825
Mark Brown84e90932010-11-04 00:07:02 -04001826 trace_snd_soc_dapm_start(card);
1827
Mark Brown56fba412011-06-04 11:25:10 +01001828 list_for_each_entry(d, &card->dapm_list, list) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001829 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001830 d->target_bias_level = SND_SOC_BIAS_OFF;
1831 else
1832 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001833 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001834
Liam Girdwood6c120e12012-02-15 15:15:34 +00001835 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001836
Mark Brown6d3ddc82009-05-16 17:47:29 +01001837 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001838 * lists indicating if they should be powered up or down. We
1839 * only check widgets that have been flagged as dirty but note
1840 * that new widgets may be added to the dirty list while we
1841 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001842 */
Mark Browndb432b42011-10-03 21:06:40 +01001843 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001844 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001845 }
1846
Mark Brownf9de6d72011-09-28 17:19:47 +01001847 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001848 switch (w->id) {
1849 case snd_soc_dapm_pre:
1850 case snd_soc_dapm_post:
1851 /* These widgets always need to be powered */
1852 break;
1853 default:
1854 list_del_init(&w->dirty);
1855 break;
1856 }
Mark Browndb432b42011-10-03 21:06:40 +01001857
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001858 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001859 d = w->dapm;
1860
1861 /* Supplies and micbiases only bring the
1862 * context up to STANDBY as unless something
1863 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001864 * generally don't require full power. Signal
1865 * generators are virtual pins and have no
1866 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001867 */
1868 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001869 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001870 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001871 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001872 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001873 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001874 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001875 case snd_soc_dapm_micbias:
1876 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1877 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1878 break;
1879 default:
1880 d->target_bias_level = SND_SOC_BIAS_ON;
1881 break;
1882 }
1883 }
1884
1885 }
1886
Mark Brown85a843c2011-09-21 21:29:47 +01001887 /* Force all contexts in the card to the same bias state if
1888 * they're not ground referenced.
1889 */
Mark Brown56fba412011-06-04 11:25:10 +01001890 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001891 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001892 if (d->target_bias_level > bias)
1893 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001894 list_for_each_entry(d, &card->dapm_list, list)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001895 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01001896 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001897
Mark Brownde02d072011-09-20 21:43:24 +01001898 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001899
Xiang Xiao17282ba2014-03-02 00:04:03 +08001900 /* Run card bias changes at first */
1901 dapm_pre_sequence_async(&card->dapm, 0);
1902 /* Run other bias changes in parallel */
1903 list_for_each_entry(d, &card->dapm_list, list) {
1904 if (d != &card->dapm)
1905 async_schedule_domain(dapm_pre_sequence_async, d,
1906 &async_domain);
1907 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001908 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001909
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001910 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001911 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001912 }
1913
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001914 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001915 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001916 }
1917
Mark Brown6d3ddc82009-05-16 17:47:29 +01001918 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001919 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001920
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001921 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001922
Mark Brown6d3ddc82009-05-16 17:47:29 +01001923 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001924 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001925
Mark Brown9d0624a2011-02-18 11:49:43 -08001926 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001927 list_for_each_entry(d, &card->dapm_list, list) {
1928 if (d != &card->dapm)
1929 async_schedule_domain(dapm_post_sequence_async, d,
1930 &async_domain);
1931 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001932 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001933 /* Run card bias changes at last */
1934 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001935
Liam Girdwood8078d872012-02-15 15:15:35 +00001936 /* do we need to notify any clients that DAPM event is complete */
1937 list_for_each_entry(d, &card->dapm_list, list) {
1938 if (d->stream_event)
1939 d->stream_event(d, event);
1940 }
1941
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001942 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001943 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001944 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001945
Mark Brown84e90932010-11-04 00:07:02 -04001946 trace_snd_soc_dapm_done(card);
1947
Mark Brown42aa3412009-03-01 19:21:10 +00001948 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001949}
1950
Mark Brown79fb9382009-08-21 16:38:13 +01001951#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001952static ssize_t dapm_widget_power_read_file(struct file *file,
1953 char __user *user_buf,
1954 size_t count, loff_t *ppos)
1955{
1956 struct snd_soc_dapm_widget *w = file->private_data;
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02001957 struct snd_soc_card *card = w->dapm->card;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001958 enum snd_soc_dapm_direction dir, rdir;
Mark Brown79fb9382009-08-21 16:38:13 +01001959 char *buf;
1960 int in, out;
1961 ssize_t ret;
1962 struct snd_soc_dapm_path *p = NULL;
1963
1964 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1965 if (!buf)
1966 return -ENOMEM;
1967
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02001968 mutex_lock(&card->dapm_mutex);
1969
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001970 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
1971 if (w->is_supply) {
1972 in = 0;
1973 out = 0;
1974 } else {
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001975 in = is_connected_input_ep(w, NULL, NULL);
1976 out = is_connected_output_ep(w, NULL, NULL);
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001977 }
Mark Brown79fb9382009-08-21 16:38:13 +01001978
Silvio Cesare91890492019-01-12 16:28:43 +01001979 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
Mark Brownf13ebad2012-03-03 18:01:01 +00001980 w->name, w->power ? "On" : "Off",
1981 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001982
Mark Brownd033c362009-12-04 15:25:56 +00001983 if (w->reg >= 0)
Silvio Cesare91890492019-01-12 16:28:43 +01001984 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001985 " - R%d(0x%x) mask 0x%x",
1986 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001987
Silvio Cesare91890492019-01-12 16:28:43 +01001988 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
Mark Brownd033c362009-12-04 15:25:56 +00001989
Mark Brown3eef08b2009-09-14 16:49:00 +01001990 if (w->sname)
Silvio Cesare91890492019-01-12 16:28:43 +01001991 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
Mark Brown3eef08b2009-09-14 16:49:00 +01001992 w->sname,
1993 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001994
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001995 snd_soc_dapm_for_each_direction(dir) {
1996 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1997 snd_soc_dapm_widget_for_each_path(w, dir, p) {
1998 if (p->connected && !p->connected(w, p->node[rdir]))
1999 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002000
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002001 if (!p->connect)
2002 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002003
Silvio Cesare91890492019-01-12 16:28:43 +01002004 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002005 " %s \"%s\" \"%s\"\n",
2006 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
Mark Brown79fb9382009-08-21 16:38:13 +01002007 p->name ? p->name : "static",
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002008 p->node[rdir]->name);
2009 }
Mark Brown79fb9382009-08-21 16:38:13 +01002010 }
2011
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002012 mutex_unlock(&card->dapm_mutex);
2013
Mark Brown79fb9382009-08-21 16:38:13 +01002014 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2015
2016 kfree(buf);
2017 return ret;
2018}
2019
2020static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002021 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002022 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002023 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002024};
2025
Mark Brownef49e4f2011-04-04 20:48:13 +09002026static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2027 size_t count, loff_t *ppos)
2028{
2029 struct snd_soc_dapm_context *dapm = file->private_data;
2030 char *level;
2031
2032 switch (dapm->bias_level) {
2033 case SND_SOC_BIAS_ON:
2034 level = "On\n";
2035 break;
2036 case SND_SOC_BIAS_PREPARE:
2037 level = "Prepare\n";
2038 break;
2039 case SND_SOC_BIAS_STANDBY:
2040 level = "Standby\n";
2041 break;
2042 case SND_SOC_BIAS_OFF:
2043 level = "Off\n";
2044 break;
2045 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002046 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002047 level = "Unknown\n";
2048 break;
2049 }
2050
2051 return simple_read_from_buffer(user_buf, count, ppos, level,
2052 strlen(level));
2053}
2054
2055static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002056 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002057 .read = dapm_bias_read_file,
2058 .llseek = default_llseek,
2059};
2060
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002061void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2062 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002063{
Mark Brown79fb9382009-08-21 16:38:13 +01002064 struct dentry *d;
2065
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02002066 if (!parent)
2067 return;
2068
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002069 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2070
2071 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002072 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002073 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002074 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002075 }
Mark Brown79fb9382009-08-21 16:38:13 +01002076
Mark Brownef49e4f2011-04-04 20:48:13 +09002077 d = debugfs_create_file("bias_level", 0444,
2078 dapm->debugfs_dapm, dapm,
2079 &dapm_bias_fops);
2080 if (!d)
2081 dev_warn(dapm->dev,
2082 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002083}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002084
2085static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2086{
2087 struct snd_soc_dapm_context *dapm = w->dapm;
2088 struct dentry *d;
2089
2090 if (!dapm->debugfs_dapm || !w->name)
2091 return;
2092
2093 d = debugfs_create_file(w->name, 0444,
2094 dapm->debugfs_dapm, w,
2095 &dapm_widget_power_fops);
2096 if (!d)
2097 dev_warn(w->dapm->dev,
2098 "ASoC: Failed to create %s debugfs file\n",
2099 w->name);
2100}
2101
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002102static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2103{
2104 debugfs_remove_recursive(dapm->debugfs_dapm);
2105}
2106
Mark Brown79fb9382009-08-21 16:38:13 +01002107#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002108void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2109 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002110{
2111}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002112
2113static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2114{
2115}
2116
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002117static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2118{
2119}
2120
Mark Brown79fb9382009-08-21 16:38:13 +01002121#endif
2122
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002123/*
2124 * soc_dapm_connect_path() - Connects or disconnects a path
2125 * @path: The path to update
2126 * @connect: The new connect state of the path. True if the path is connected,
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002127 * false if it is disconnected.
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002128 * @reason: The reason why the path changed (for debugging only)
2129 */
2130static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2131 bool connect, const char *reason)
2132{
2133 if (path->connect == connect)
2134 return;
2135
2136 path->connect = connect;
2137 dapm_mark_dirty(path->source, reason);
2138 dapm_mark_dirty(path->sink, reason);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002139 dapm_path_invalidate(path);
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002140}
2141
Richard Purdie2b97eab2006-10-06 18:32:18 +02002142/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002143static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002144 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002145{
2146 struct snd_soc_dapm_path *path;
2147 int found = 0;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002148 bool connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002149
Mark Brownf9fa2b12014-03-06 16:49:11 +08002150 lockdep_assert_held(&card->dapm_mutex);
2151
Richard Purdie2b97eab2006-10-06 18:32:18 +02002152 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002153 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002154 found = 1;
2155 /* we now need to match the string in the enum to the path */
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002156 if (!(strcmp(path->name, e->texts[mux])))
2157 connect = true;
2158 else
2159 connect = false;
2160
2161 soc_dapm_connect_path(path, connect, "mux update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002162 }
2163
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002164 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002165 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002166
Liam Girdwood618dae12012-04-25 12:12:51 +01002167 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002168}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002169
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002170int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002171 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2172 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002173{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002174 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002175 int ret;
2176
Liam Girdwood3cd04342012-03-09 12:02:08 +00002177 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002178 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002179 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002180 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002181 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002182 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002183 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002184 return ret;
2185}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002186EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002187
Milan plzik1b075e32008-01-10 14:39:46 +01002188/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002189static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002190 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002191{
2192 struct snd_soc_dapm_path *path;
2193 int found = 0;
2194
Mark Brownf9fa2b12014-03-06 16:49:11 +08002195 lockdep_assert_held(&card->dapm_mutex);
2196
Richard Purdie2b97eab2006-10-06 18:32:18 +02002197 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002198 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002199 found = 1;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002200 soc_dapm_connect_path(path, connect, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002201 }
2202
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002203 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002204 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002205
Liam Girdwood618dae12012-04-25 12:12:51 +01002206 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002207}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002208
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002209int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002210 struct snd_kcontrol *kcontrol, int connect,
2211 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002212{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002213 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002214 int ret;
2215
Liam Girdwood3cd04342012-03-09 12:02:08 +00002216 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002217 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002218 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002219 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002220 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002221 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002222 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002223 return ret;
2224}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002225EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002226
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002227static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2228 char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002229{
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002230 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002231 struct snd_soc_dapm_widget *w;
2232 int count = 0;
2233 char *state = "not set";
2234
Mark Brown47325072016-03-18 12:04:23 +00002235 /* card won't be set for the dummy component, as a spot fix
2236 * we're checking for that case specifically here but in future
2237 * we will ensure that the dummy component looks like others.
2238 */
2239 if (!cmpnt->card)
2240 return 0;
2241
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002242 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2243 if (w->dapm != dapm)
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002244 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002245
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002246 /* only display widgets that burn power */
Richard Purdie2b97eab2006-10-06 18:32:18 +02002247 switch (w->id) {
2248 case snd_soc_dapm_hp:
2249 case snd_soc_dapm_mic:
2250 case snd_soc_dapm_spk:
2251 case snd_soc_dapm_line:
2252 case snd_soc_dapm_micbias:
2253 case snd_soc_dapm_dac:
2254 case snd_soc_dapm_adc:
2255 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002256 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002257 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002258 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002259 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002260 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002261 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002262 if (w->name)
2263 count += sprintf(buf + count, "%s: %s\n",
2264 w->name, w->power ? "On":"Off");
2265 break;
2266 default:
2267 break;
2268 }
2269 }
2270
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002271 switch (snd_soc_dapm_get_bias_level(dapm)) {
Mark Brown0be98982008-05-19 12:31:28 +02002272 case SND_SOC_BIAS_ON:
2273 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002274 break;
Mark Brown0be98982008-05-19 12:31:28 +02002275 case SND_SOC_BIAS_PREPARE:
2276 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002277 break;
Mark Brown0be98982008-05-19 12:31:28 +02002278 case SND_SOC_BIAS_STANDBY:
2279 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002280 break;
Mark Brown0be98982008-05-19 12:31:28 +02002281 case SND_SOC_BIAS_OFF:
2282 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002283 break;
2284 }
2285 count += sprintf(buf + count, "PM State: %s\n", state);
2286
2287 return count;
2288}
2289
Benoit Cousson44ba2642014-07-08 23:19:36 +02002290/* show dapm widget status in sys fs */
2291static ssize_t dapm_widget_show(struct device *dev,
2292 struct device_attribute *attr, char *buf)
2293{
2294 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2295 int i, count = 0;
2296
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002297 mutex_lock(&rtd->card->dapm_mutex);
2298
Benoit Cousson44ba2642014-07-08 23:19:36 +02002299 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002300 struct snd_soc_component *cmpnt = rtd->codec_dais[i]->component;
2301
2302 count += dapm_widget_show_component(cmpnt, buf + count);
Benoit Cousson44ba2642014-07-08 23:19:36 +02002303 }
2304
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002305 mutex_unlock(&rtd->card->dapm_mutex);
2306
Benoit Cousson44ba2642014-07-08 23:19:36 +02002307 return count;
2308}
2309
Richard Purdie2b97eab2006-10-06 18:32:18 +02002310static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2311
Takashi Iwaid29697d2015-01-30 20:16:37 +01002312struct attribute *soc_dapm_dev_attrs[] = {
2313 &dev_attr_dapm_widget.attr,
2314 NULL
2315};
Richard Purdie2b97eab2006-10-06 18:32:18 +02002316
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002317static void dapm_free_path(struct snd_soc_dapm_path *path)
2318{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002319 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2320 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002321 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002322 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002323 kfree(path);
2324}
2325
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002326void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2327{
2328 struct snd_soc_dapm_path *p, *next_p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002329 enum snd_soc_dapm_direction dir;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002330
2331 list_del(&w->list);
2332 /*
2333 * remove source and sink paths associated to this widget.
2334 * While removing the path, remove reference to it from both
2335 * source and sink widgets so that path is removed only once.
2336 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002337 snd_soc_dapm_for_each_direction(dir) {
2338 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2339 dapm_free_path(p);
2340 }
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002341
2342 kfree(w->kcontrols);
Lars-Peter Clausen48068962015-07-21 18:11:08 +02002343 kfree_const(w->name);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002344 kfree(w);
2345}
2346
Jyri Sarhafd589a12015-11-10 18:12:42 +02002347void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2348{
2349 dapm->path_sink_cache.widget = NULL;
2350 dapm->path_source_cache.widget = NULL;
2351}
2352
Richard Purdie2b97eab2006-10-06 18:32:18 +02002353/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002354static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002355{
2356 struct snd_soc_dapm_widget *w, *next_w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002357
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002358 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2359 if (w->dapm != dapm)
2360 continue;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002361 snd_soc_dapm_free_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002362 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002363 snd_soc_dapm_reset_cache(dapm);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002364}
2365
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002366static struct snd_soc_dapm_widget *dapm_find_widget(
2367 struct snd_soc_dapm_context *dapm, const char *pin,
2368 bool search_other_contexts)
2369{
2370 struct snd_soc_dapm_widget *w;
2371 struct snd_soc_dapm_widget *fallback = NULL;
2372
2373 list_for_each_entry(w, &dapm->card->widgets, list) {
2374 if (!strcmp(w->name, pin)) {
2375 if (w->dapm == dapm)
2376 return w;
2377 else
2378 fallback = w;
2379 }
2380 }
2381
2382 if (search_other_contexts)
2383 return fallback;
2384
2385 return NULL;
2386}
2387
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002388static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002389 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002390{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002391 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002392
Mark Brownf9fa2b12014-03-06 16:49:11 +08002393 dapm_assert_locked(dapm);
2394
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002395 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002396 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002397 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002398 }
2399
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002400 if (w->connected != status) {
Mark Brown1a8b2d92012-02-16 11:50:07 -08002401 dapm_mark_dirty(w, "pin configuration");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002402 dapm_widget_invalidate_input_paths(w);
2403 dapm_widget_invalidate_output_paths(w);
2404 }
Mark Brown1a8b2d92012-02-16 11:50:07 -08002405
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002406 w->connected = status;
2407 if (status == 0)
2408 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002409
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002410 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002411}
2412
Richard Purdie2b97eab2006-10-06 18:32:18 +02002413/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002414 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2415 * @dapm: DAPM context
2416 *
2417 * Walks all dapm audio paths and powers widgets according to their
2418 * stream or path usage.
2419 *
2420 * Requires external locking.
2421 *
2422 * Returns 0 for success.
2423 */
2424int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2425{
2426 /*
2427 * Suppress early reports (eg, jacks syncing their state) to avoid
2428 * silly DAPM runs during card startup.
2429 */
2430 if (!dapm->card || !dapm->card->instantiated)
2431 return 0;
2432
2433 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2434}
2435EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2436
2437/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002438 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002439 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002440 *
2441 * Walks all dapm audio paths and powers widgets according to their
2442 * stream or path usage.
2443 *
2444 * Returns 0 for success.
2445 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002446int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002447{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002448 int ret;
2449
Liam Girdwood3cd04342012-03-09 12:02:08 +00002450 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002451 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002452 mutex_unlock(&dapm->card->dapm_mutex);
2453 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002454}
Liam Girdwooda5302182008-07-07 13:35:17 +01002455EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002456
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002457/*
2458 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2459 * @w: The widget for which to update the flags
2460 *
2461 * Some widgets have a dynamic category which depends on which neighbors they
2462 * are connected to. This function update the category for these widgets.
2463 *
2464 * This function must be called whenever a path is added or removed to a widget.
2465 */
2466static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2467{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002468 enum snd_soc_dapm_direction dir;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002469 struct snd_soc_dapm_path *p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002470 unsigned int ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002471
2472 switch (w->id) {
2473 case snd_soc_dapm_input:
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002474 /* On a fully routed card an input is never a source */
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002475 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002476 return;
2477 ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002478 snd_soc_dapm_widget_for_each_source_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002479 if (p->source->id == snd_soc_dapm_micbias ||
2480 p->source->id == snd_soc_dapm_mic ||
2481 p->source->id == snd_soc_dapm_line ||
2482 p->source->id == snd_soc_dapm_output) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002483 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002484 break;
2485 }
2486 }
2487 break;
2488 case snd_soc_dapm_output:
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002489 /* On a fully routed card a output is never a sink */
2490 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002491 return;
2492 ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002493 snd_soc_dapm_widget_for_each_sink_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002494 if (p->sink->id == snd_soc_dapm_spk ||
2495 p->sink->id == snd_soc_dapm_hp ||
2496 p->sink->id == snd_soc_dapm_line ||
2497 p->sink->id == snd_soc_dapm_input) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002498 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002499 break;
2500 }
2501 }
2502 break;
2503 case snd_soc_dapm_line:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002504 ep = 0;
2505 snd_soc_dapm_for_each_direction(dir) {
2506 if (!list_empty(&w->edges[dir]))
2507 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2508 }
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002509 break;
2510 default:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002511 return;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002512 }
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002513
2514 w->is_ep = ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002515}
2516
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002517static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2518 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2519 const char *control)
2520{
2521 bool dynamic_source = false;
2522 bool dynamic_sink = false;
2523
2524 if (!control)
2525 return 0;
2526
2527 switch (source->id) {
2528 case snd_soc_dapm_demux:
2529 dynamic_source = true;
2530 break;
2531 default:
2532 break;
2533 }
2534
2535 switch (sink->id) {
2536 case snd_soc_dapm_mux:
2537 case snd_soc_dapm_switch:
2538 case snd_soc_dapm_mixer:
2539 case snd_soc_dapm_mixer_named_ctl:
2540 dynamic_sink = true;
2541 break;
2542 default:
2543 break;
2544 }
2545
2546 if (dynamic_source && dynamic_sink) {
2547 dev_err(dapm->dev,
2548 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2549 source->name, control, sink->name);
2550 return -EINVAL;
2551 } else if (!dynamic_source && !dynamic_sink) {
2552 dev_err(dapm->dev,
2553 "Control not supported for path %s -> [%s] -> %s\n",
2554 source->name, control, sink->name);
2555 return -EINVAL;
2556 }
2557
2558 return 0;
2559}
2560
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002561static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2562 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2563 const char *control,
2564 int (*connected)(struct snd_soc_dapm_widget *source,
2565 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002566{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002567 struct snd_soc_dapm_widget *widgets[2];
2568 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002569 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002570 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002571
Lars-Peter Clausene409dfb2014-10-25 17:42:02 +02002572 if (wsink->is_supply && !wsource->is_supply) {
2573 dev_err(dapm->dev,
2574 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2575 wsource->name, wsink->name);
2576 return -EINVAL;
2577 }
2578
2579 if (connected && !wsource->is_supply) {
2580 dev_err(dapm->dev,
2581 "connected() callback only supported for supply widgets (%s -> %s)\n",
2582 wsource->name, wsink->name);
2583 return -EINVAL;
2584 }
2585
2586 if (wsource->is_supply && control) {
2587 dev_err(dapm->dev,
2588 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2589 wsource->name, control, wsink->name);
2590 return -EINVAL;
2591 }
2592
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002593 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2594 if (ret)
2595 return ret;
2596
Richard Purdie2b97eab2006-10-06 18:32:18 +02002597 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2598 if (!path)
2599 return -ENOMEM;
2600
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002601 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2602 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2603 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2604 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2605
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002606 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002607 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002608 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002609
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002610 if (wsource->is_supply || wsink->is_supply)
2611 path->is_supply = 1;
2612
Richard Purdie2b97eab2006-10-06 18:32:18 +02002613 /* connect static paths */
2614 if (control == NULL) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002615 path->connect = 1;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002616 } else {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002617 switch (wsource->id) {
2618 case snd_soc_dapm_demux:
2619 ret = dapm_connect_mux(dapm, path, control, wsource);
2620 if (ret)
2621 goto err;
2622 break;
2623 default:
2624 break;
2625 }
2626
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002627 switch (wsink->id) {
2628 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002629 ret = dapm_connect_mux(dapm, path, control, wsink);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002630 if (ret != 0)
2631 goto err;
2632 break;
2633 case snd_soc_dapm_switch:
2634 case snd_soc_dapm_mixer:
2635 case snd_soc_dapm_mixer_named_ctl:
2636 ret = dapm_connect_mixer(dapm, path, control);
2637 if (ret != 0)
2638 goto err;
2639 break;
2640 default:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002641 break;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002642 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002643 }
2644
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002645 list_add(&path->list, &dapm->card->paths);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002646 snd_soc_dapm_for_each_direction(dir)
2647 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002648
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002649 snd_soc_dapm_for_each_direction(dir) {
2650 dapm_update_widget_flags(widgets[dir]);
2651 dapm_mark_dirty(widgets[dir], "Route added");
2652 }
Mark Brownfabd0382012-07-05 17:20:06 +01002653
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002654 if (dapm->card->instantiated && path->connect)
2655 dapm_path_invalidate(path);
2656
Richard Purdie2b97eab2006-10-06 18:32:18 +02002657 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002658err:
2659 kfree(path);
2660 return ret;
2661}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002662
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002663static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002664 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002665{
2666 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2667 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2668 const char *sink;
2669 const char *source;
2670 char prefixed_sink[80];
2671 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002672 const char *prefix;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002673 int ret;
2674
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002675 prefix = soc_dapm_prefix(dapm);
2676 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002677 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002678 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002679 sink = prefixed_sink;
2680 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002681 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002682 source = prefixed_source;
2683 } else {
2684 sink = route->sink;
2685 source = route->source;
2686 }
2687
Charles Keepax45a110a2015-05-11 13:50:30 +01002688 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2689 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2690
2691 if (wsink && wsource)
2692 goto skip_search;
2693
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002694 /*
2695 * find src and dest widgets over all widgets but favor a widget from
2696 * current DAPM context
2697 */
2698 list_for_each_entry(w, &dapm->card->widgets, list) {
2699 if (!wsink && !(strcmp(w->name, sink))) {
2700 wtsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002701 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002702 wsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002703 if (wsource)
2704 break;
2705 }
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002706 continue;
2707 }
2708 if (!wsource && !(strcmp(w->name, source))) {
2709 wtsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002710 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002711 wsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002712 if (wsink)
2713 break;
2714 }
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002715 }
2716 }
2717 /* use widget from another DAPM context if not found from this */
2718 if (!wsink)
2719 wsink = wtsink;
2720 if (!wsource)
2721 wsource = wtsource;
2722
2723 if (wsource == NULL) {
2724 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2725 route->source);
2726 return -ENODEV;
2727 }
2728 if (wsink == NULL) {
2729 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2730 route->sink);
2731 return -ENODEV;
2732 }
2733
Charles Keepax45a110a2015-05-11 13:50:30 +01002734skip_search:
2735 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2736 dapm_wcache_update(&dapm->path_source_cache, wsource);
2737
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002738 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2739 route->connected);
2740 if (ret)
2741 goto err;
2742
2743 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002744err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002745 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002746 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002747 return ret;
2748}
Mark Brown105f1c22008-05-13 14:52:19 +02002749
Mark Brownefcc3c62012-07-05 17:24:19 +01002750static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2751 const struct snd_soc_dapm_route *route)
2752{
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002753 struct snd_soc_dapm_widget *wsource, *wsink;
Mark Brownefcc3c62012-07-05 17:24:19 +01002754 struct snd_soc_dapm_path *path, *p;
2755 const char *sink;
2756 const char *source;
2757 char prefixed_sink[80];
2758 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002759 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002760
2761 if (route->control) {
2762 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002763 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002764 return -EINVAL;
2765 }
2766
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002767 prefix = soc_dapm_prefix(dapm);
2768 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002769 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002770 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002771 sink = prefixed_sink;
2772 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002773 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002774 source = prefixed_source;
2775 } else {
2776 sink = route->sink;
2777 source = route->source;
2778 }
2779
2780 path = NULL;
2781 list_for_each_entry(p, &dapm->card->paths, list) {
2782 if (strcmp(p->source->name, source) != 0)
2783 continue;
2784 if (strcmp(p->sink->name, sink) != 0)
2785 continue;
2786 path = p;
2787 break;
2788 }
2789
2790 if (path) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002791 wsource = path->source;
2792 wsink = path->sink;
2793
2794 dapm_mark_dirty(wsource, "Route removed");
2795 dapm_mark_dirty(wsink, "Route removed");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002796 if (path->connect)
2797 dapm_path_invalidate(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002798
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002799 dapm_free_path(path);
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002800
2801 /* Update any path related flags */
2802 dapm_update_widget_flags(wsource);
2803 dapm_update_widget_flags(wsink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002804 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002805 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002806 source, sink);
2807 }
2808
2809 return 0;
2810}
2811
Mark Brown105f1c22008-05-13 14:52:19 +02002812/**
Mark Brown105f1c22008-05-13 14:52:19 +02002813 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002814 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002815 * @route: audio routes
2816 * @num: number of routes
2817 *
2818 * Connects 2 dapm widgets together via a named audio path. The sink is
2819 * the widget receiving the audio signal, whilst the source is the sender
2820 * of the audio signal.
2821 *
2822 * Returns 0 for success else error. On error all resources can be freed
2823 * with a call to snd_soc_card_free().
2824 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002825int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002826 const struct snd_soc_dapm_route *route, int num)
2827{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002828 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002829
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002830 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002831 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002832 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002833 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002834 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2835 route->source,
2836 route->control ? route->control : "direct",
2837 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002838 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002839 }
2840 route++;
2841 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002842 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002843
Dan Carpenter60884c22012-04-13 22:25:43 +03002844 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002845}
2846EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2847
Mark Brownefcc3c62012-07-05 17:24:19 +01002848/**
2849 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2850 * @dapm: DAPM context
2851 * @route: audio routes
2852 * @num: number of routes
2853 *
2854 * Removes routes from the DAPM context.
2855 */
2856int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2857 const struct snd_soc_dapm_route *route, int num)
2858{
Rajan Vajae066ea22016-02-11 11:23:35 +05302859 int i;
Mark Brownefcc3c62012-07-05 17:24:19 +01002860
2861 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2862 for (i = 0; i < num; i++) {
2863 snd_soc_dapm_del_route(dapm, route);
2864 route++;
2865 }
2866 mutex_unlock(&dapm->card->dapm_mutex);
2867
Rajan Vajae066ea22016-02-11 11:23:35 +05302868 return 0;
Mark Brownefcc3c62012-07-05 17:24:19 +01002869}
2870EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2871
Mark Brownbf3a9e12011-06-13 16:42:29 +01002872static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2873 const struct snd_soc_dapm_route *route)
2874{
2875 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2876 route->source,
2877 true);
2878 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2879 route->sink,
2880 true);
2881 struct snd_soc_dapm_path *path;
2882 int count = 0;
2883
2884 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002885 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002886 route->source);
2887 return -ENODEV;
2888 }
2889
2890 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002891 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002892 route->sink);
2893 return -ENODEV;
2894 }
2895
2896 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002897 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002898 route->source, route->sink);
2899
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002900 snd_soc_dapm_widget_for_each_sink_path(source, path) {
Mark Brownbf3a9e12011-06-13 16:42:29 +01002901 if (path->sink == sink) {
2902 path->weak = 1;
2903 count++;
2904 }
2905 }
2906
2907 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002908 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002909 route->source, route->sink);
2910 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002911 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002912 count, route->source, route->sink);
2913
2914 return 0;
2915}
2916
2917/**
2918 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2919 * @dapm: DAPM context
2920 * @route: audio routes
2921 * @num: number of routes
2922 *
2923 * Mark existing routes matching those specified in the passed array
2924 * as being weak, meaning that they are ignored for the purpose of
2925 * power decisions. The main intended use case is for sidetone paths
2926 * which couple audio between other independent paths if they are both
2927 * active in order to make the combination work better at the user
2928 * level but which aren't intended to be "used".
2929 *
2930 * Note that CODEC drivers should not use this as sidetone type paths
2931 * can frequently also be used as bypass paths.
2932 */
2933int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2934 const struct snd_soc_dapm_route *route, int num)
2935{
2936 int i, err;
2937 int ret = 0;
2938
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002939 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002940 for (i = 0; i < num; i++) {
2941 err = snd_soc_dapm_weak_route(dapm, route);
2942 if (err)
2943 ret = err;
2944 route++;
2945 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002946 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002947
2948 return ret;
2949}
2950EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2951
Mark Brown105f1c22008-05-13 14:52:19 +02002952/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002953 * snd_soc_dapm_new_widgets - add new dapm widgets
Jonathan Corbet628536e2015-08-25 01:14:48 -06002954 * @card: card to be checked for new dapm widgets
Richard Purdie2b97eab2006-10-06 18:32:18 +02002955 *
2956 * Checks the codec for any new dapm widgets and creates them if found.
2957 *
2958 * Returns 0 for success.
2959 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002960int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002961{
2962 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002963 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002964
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002965 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002966
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002967 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002968 {
2969 if (w->new)
2970 continue;
2971
Stephen Warrenfad59882011-04-28 17:37:59 -06002972 if (w->num_kcontrols) {
2973 w->kcontrols = kzalloc(w->num_kcontrols *
2974 sizeof(struct snd_kcontrol *),
2975 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002976 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002977 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002978 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002979 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002980 }
2981
Richard Purdie2b97eab2006-10-06 18:32:18 +02002982 switch(w->id) {
2983 case snd_soc_dapm_switch:
2984 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002985 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002986 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002987 break;
2988 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002989 case snd_soc_dapm_demux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002990 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002991 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002992 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002993 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002994 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002995 break;
Nikesh Oswalc6615082015-02-02 17:06:44 +00002996 case snd_soc_dapm_dai_link:
2997 dapm_new_dai_link(w);
2998 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002999 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003000 break;
3001 }
Mark Brownb66a70d2011-02-09 18:04:11 +00003002
3003 /* Read the initial power state from the device */
3004 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003005 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08003006 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003007 val &= w->mask;
3008 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00003009 w->power = 1;
3010 }
3011
Richard Purdie2b97eab2006-10-06 18:32:18 +02003012 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003013
Mark Brown7508b122011-10-05 12:09:12 +01003014 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003015 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003016 }
3017
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003018 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3019 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003020 return 0;
3021}
3022EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3023
3024/**
3025 * snd_soc_dapm_get_volsw - dapm mixer get callback
3026 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003027 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003028 *
3029 * Callback to get the value of a dapm mixer control.
3030 *
3031 * Returns 0 for success.
3032 */
3033int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3034 struct snd_ctl_elem_value *ucontrol)
3035{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003036 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3037 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003038 struct soc_mixer_control *mc =
3039 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003040 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003041 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003042 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003043 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003044 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003045 unsigned int val;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003046 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003047
3048 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003049 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003050 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003051 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003052
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003053 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003054 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3055 ret = soc_dapm_read(dapm, reg, &val);
3056 val = (val >> shift) & mask;
3057 } else {
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003058 val = dapm_kcontrol_get_value(kcontrol);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003059 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003060 mutex_unlock(&card->dapm_mutex);
3061
Chen-Yu Tsai01ad5e72016-08-27 19:27:58 +08003062 if (ret)
3063 return ret;
3064
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003065 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003066 ucontrol->value.integer.value[0] = max - val;
3067 else
3068 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003069
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003070 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003071}
3072EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3073
3074/**
3075 * snd_soc_dapm_put_volsw - dapm mixer set callback
3076 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003077 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003078 *
3079 * Callback to set the value of a dapm mixer control.
3080 *
3081 * Returns 0 for success.
3082 */
3083int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3084 struct snd_ctl_elem_value *ucontrol)
3085{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003086 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3087 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003088 struct soc_mixer_control *mc =
3089 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003090 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003091 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003092 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003093 unsigned int mask = (1 << fls(max)) - 1;
3094 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07003095 unsigned int val;
Jarkko Nikula18626c72014-06-09 14:20:29 +03003096 int connect, change, reg_change = 0;
Mark Brown97404f22010-12-14 16:13:57 +00003097 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08003098 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003099
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003100 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003101 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003102 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003103 kcontrol->id.name);
3104
Richard Purdie2b97eab2006-10-06 18:32:18 +02003105 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02003106 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003107
3108 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003109 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003110
Liam Girdwood3cd04342012-03-09 12:02:08 +00003111 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003112
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003113 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown283375c2009-12-07 18:09:03 +00003114
Jarkko Nikula18626c72014-06-09 14:20:29 +03003115 if (reg != SND_SOC_NOPM) {
3116 mask = mask << shift;
3117 val = val << shift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02003118
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003119 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
Jarkko Nikula18626c72014-06-09 14:20:29 +03003120 }
3121
3122 if (change || reg_change) {
3123 if (reg_change) {
3124 update.kcontrol = kcontrol;
3125 update.reg = reg;
3126 update.mask = mask;
3127 update.val = val;
3128 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003129 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003130 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00003131
Nenghua Cao52765972013-12-13 20:13:49 +08003132 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00003133
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003134 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00003135 }
3136
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003137 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003138
3139 if (ret > 0)
3140 soc_dpcm_runtime_update(card);
3141
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003142 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003143}
3144EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3145
3146/**
3147 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3148 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003149 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003150 *
3151 * Callback to get the value of a dapm enumerated double mixer control.
3152 *
3153 * Returns 0 for success.
3154 */
3155int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3156 struct snd_ctl_elem_value *ucontrol)
3157{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003158 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Charles Keepax561ed682015-05-01 12:37:26 +01003159 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003160 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003161 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003162
Charles Keepax561ed682015-05-01 12:37:26 +01003163 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3164 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003165 int ret = soc_dapm_read(dapm, e->reg, &reg_val);
Charles Keepax964a0b82015-05-08 10:50:10 +01003166 if (ret) {
3167 mutex_unlock(&card->dapm_mutex);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003168 return ret;
Charles Keepax964a0b82015-05-08 10:50:10 +01003169 }
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003170 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003171 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003172 }
Charles Keepax561ed682015-05-01 12:37:26 +01003173 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003174
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003175 val = (reg_val >> e->shift_l) & e->mask;
3176 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3177 if (e->shift_l != e->shift_r) {
3178 val = (reg_val >> e->shift_r) & e->mask;
3179 val = snd_soc_enum_val_to_item(e, val);
3180 ucontrol->value.enumerated.item[1] = val;
3181 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003182
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003183 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003184}
3185EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3186
3187/**
3188 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3189 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003190 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003191 *
3192 * Callback to set the value of a dapm enumerated double mixer control.
3193 *
3194 * Returns 0 for success.
3195 */
3196int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3197 struct snd_ctl_elem_value *ucontrol)
3198{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003199 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3200 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003201 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003202 unsigned int *item = ucontrol->value.enumerated.item;
Charles Keepax561ed682015-05-01 12:37:26 +01003203 unsigned int val, change, reg_change = 0;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003204 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00003205 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08003206 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003207
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003208 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003209 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003210
3211 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003212 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003213 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003214 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003215 return -EINVAL;
Chen-Yu Tsai071133a2016-08-27 19:27:59 +08003216 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003217 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003218 }
3219
Liam Girdwood3cd04342012-03-09 12:02:08 +00003220 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003221
Charles Keepax561ed682015-05-01 12:37:26 +01003222 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00003223
Charles Keepax561ed682015-05-01 12:37:26 +01003224 if (e->reg != SND_SOC_NOPM)
3225 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3226
3227 if (change || reg_change) {
3228 if (reg_change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003229 update.kcontrol = kcontrol;
3230 update.reg = e->reg;
3231 update.mask = mask;
3232 update.val = val;
3233 card->update = &update;
3234 }
Charles Keepax561ed682015-05-01 12:37:26 +01003235 change |= reg_change;
Mark Brown3a655772009-10-05 17:23:30 +01003236
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003237 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003238
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003239 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003240 }
3241
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003242 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003243
3244 if (ret > 0)
3245 soc_dpcm_runtime_update(card);
3246
Mark Brown97404f22010-12-14 16:13:57 +00003247 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003248}
3249EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3250
3251/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003252 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3253 *
3254 * @kcontrol: mixer control
3255 * @uinfo: control element information
3256 *
3257 * Callback to provide information about a pin switch control.
3258 */
3259int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3260 struct snd_ctl_elem_info *uinfo)
3261{
3262 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3263 uinfo->count = 1;
3264 uinfo->value.integer.min = 0;
3265 uinfo->value.integer.max = 1;
3266
3267 return 0;
3268}
3269EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3270
3271/**
3272 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3273 *
3274 * @kcontrol: mixer control
3275 * @ucontrol: Value
3276 */
3277int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3278 struct snd_ctl_elem_value *ucontrol)
3279{
Mark Brown48a8c392012-02-14 17:11:15 -08003280 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003281 const char *pin = (const char *)kcontrol->private_value;
3282
Liam Girdwood3cd04342012-03-09 12:02:08 +00003283 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003284
3285 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003286 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003287
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003288 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003289
3290 return 0;
3291}
3292EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3293
3294/**
3295 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3296 *
3297 * @kcontrol: mixer control
3298 * @ucontrol: Value
3299 */
3300int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3301 struct snd_ctl_elem_value *ucontrol)
3302{
Mark Brown48a8c392012-02-14 17:11:15 -08003303 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003304 const char *pin = (const char *)kcontrol->private_value;
3305
Mark Brown8b37dbd2009-02-28 21:14:20 +00003306 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003307 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003308 else
Mark Brown48a8c392012-02-14 17:11:15 -08003309 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003310
Mark Brown48a8c392012-02-14 17:11:15 -08003311 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003312 return 0;
3313}
3314EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3315
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +01003316struct snd_soc_dapm_widget *
Mark Brown5ba06fc2012-02-16 11:07:13 -08003317snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003318 const struct snd_soc_dapm_widget *widget)
3319{
3320 struct snd_soc_dapm_widget *w;
3321
3322 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3323 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
Linus Walleijde415c82017-01-13 10:23:52 +01003324 /* Do not nag about probe deferrals */
3325 if (IS_ERR(w)) {
3326 int ret = PTR_ERR(w);
3327
3328 if (ret != -EPROBE_DEFER)
3329 dev_err(dapm->dev,
3330 "ASoC: Failed to create DAPM control %s (%d)\n",
3331 widget->name, ret);
3332 goto out_unlock;
3333 }
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003334 if (!w)
3335 dev_err(dapm->dev,
3336 "ASoC: Failed to create DAPM control %s\n",
3337 widget->name);
3338
Linus Walleijde415c82017-01-13 10:23:52 +01003339out_unlock:
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003340 mutex_unlock(&dapm->card->dapm_mutex);
3341 return w;
3342}
Subhransu S. Prustya5d56392016-06-27 09:18:03 +05303343EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003344
3345struct snd_soc_dapm_widget *
3346snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003347 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003348{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003349 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003350 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003351 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00003352 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003353
3354 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003355 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003356
Mark Brown62ea8742012-01-21 21:14:48 +00003357 switch (w->id) {
3358 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003359 w->regulator = devm_regulator_get(dapm->dev, w->name);
3360 if (IS_ERR(w->regulator)) {
3361 ret = PTR_ERR(w->regulator);
Linus Walleijde415c82017-01-13 10:23:52 +01003362 if (ret == -EPROBE_DEFER)
3363 return ERR_PTR(ret);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003364 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003365 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003366 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003367 }
Mark Brown8784c772013-01-10 19:33:47 +00003368
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003369 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003370 ret = regulator_allow_bypass(w->regulator, true);
3371 if (ret != 0)
3372 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003373 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003374 w->name, ret);
3375 }
Mark Brown62ea8742012-01-21 21:14:48 +00003376 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003377 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003378#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003379 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003380 if (IS_ERR(w->clk)) {
3381 ret = PTR_ERR(w->clk);
Linus Walleijde415c82017-01-13 10:23:52 +01003382 if (ret == -EPROBE_DEFER)
3383 return ERR_PTR(ret);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003384 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003385 w->name, ret);
3386 return NULL;
3387 }
Mark Brownec029952012-06-04 08:16:20 +01003388#else
3389 return NULL;
3390#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003391 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003392 default:
3393 break;
3394 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003395
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003396 prefix = soc_dapm_prefix(dapm);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003397 if (prefix)
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003398 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003399 else
Lars-Peter Clausen48068962015-07-21 18:11:08 +02003400 w->name = kstrdup_const(widget->name, GFP_KERNEL);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003401 if (w->name == NULL) {
3402 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003403 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003404 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003405
Mark Brown7ca3a182011-10-08 14:04:50 +01003406 switch (w->id) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003407 case snd_soc_dapm_mic:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003408 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003409 w->power_check = dapm_generic_check_power;
3410 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003411 case snd_soc_dapm_input:
3412 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003413 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003414 w->power_check = dapm_generic_check_power;
3415 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003416 case snd_soc_dapm_spk:
3417 case snd_soc_dapm_hp:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003418 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003419 w->power_check = dapm_generic_check_power;
3420 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003421 case snd_soc_dapm_output:
3422 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003423 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003424 w->power_check = dapm_generic_check_power;
3425 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003426 case snd_soc_dapm_vmid:
3427 case snd_soc_dapm_siggen:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003428 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003429 w->power_check = dapm_always_on_check_power;
3430 break;
Vinod Koul56b44372015-11-23 21:22:30 +05303431 case snd_soc_dapm_sink:
3432 w->is_ep = SND_SOC_DAPM_EP_SINK;
3433 w->power_check = dapm_always_on_check_power;
3434 break;
3435
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003436 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003437 case snd_soc_dapm_demux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003438 case snd_soc_dapm_switch:
3439 case snd_soc_dapm_mixer:
3440 case snd_soc_dapm_mixer_named_ctl:
Mark Brown63c69a62013-07-18 22:03:01 +01003441 case snd_soc_dapm_adc:
3442 case snd_soc_dapm_aif_out:
3443 case snd_soc_dapm_dac:
3444 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003445 case snd_soc_dapm_pga:
3446 case snd_soc_dapm_out_drv:
Mark Brown7ca3a182011-10-08 14:04:50 +01003447 case snd_soc_dapm_micbias:
Mark Brown7ca3a182011-10-08 14:04:50 +01003448 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003449 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02003450 case snd_soc_dapm_dai_out:
3451 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003452 w->power_check = dapm_generic_check_power;
3453 break;
3454 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003455 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003456 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003457 case snd_soc_dapm_kcontrol:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003458 w->is_supply = 1;
Mark Brown7ca3a182011-10-08 14:04:50 +01003459 w->power_check = dapm_supply_check_power;
3460 break;
3461 default:
3462 w->power_check = dapm_always_on_check_power;
3463 break;
3464 }
3465
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003466 w->dapm = dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003467 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003468 INIT_LIST_HEAD(&w->dirty);
Lars-Peter Clausen92fa1242015-05-01 18:02:42 +02003469 list_add_tail(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003470
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003471 snd_soc_dapm_for_each_direction(dir) {
3472 INIT_LIST_HEAD(&w->edges[dir]);
3473 w->endpoints[dir] = -1;
3474 }
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003475
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02003476 /* machine layer sets up unconnected pins and insertions */
Richard Purdie2b97eab2006-10-06 18:32:18 +02003477 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003478 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003479}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003480
3481/**
Mark Brown4ba13272008-05-13 14:51:19 +02003482 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003483 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003484 * @widget: widget array
3485 * @num: number of widgets
3486 *
3487 * Creates new DAPM controls based upon the templates.
3488 *
3489 * Returns 0 for success else error.
3490 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003491int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003492 const struct snd_soc_dapm_widget *widget,
3493 int num)
3494{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003495 struct snd_soc_dapm_widget *w;
3496 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003497 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003498
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003499 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003500 for (i = 0; i < num; i++) {
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003501 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
Linus Walleijde415c82017-01-13 10:23:52 +01003502 if (IS_ERR(w)) {
3503 ret = PTR_ERR(w);
3504 /* Do not nag about probe deferrals */
3505 if (ret == -EPROBE_DEFER)
3506 break;
3507 dev_err(dapm->dev,
3508 "ASoC: Failed to create DAPM control %s (%d)\n",
3509 widget->name, ret);
3510 break;
3511 }
Mark Brown5ba06fc2012-02-16 11:07:13 -08003512 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003513 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003514 "ASoC: Failed to create DAPM control %s\n",
3515 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003516 ret = -ENOMEM;
3517 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003518 }
Mark Brown4ba13272008-05-13 14:51:19 +02003519 widget++;
3520 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003521 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003522 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003523}
3524EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3525
Mark Brownc74184e2012-04-04 22:12:09 +01003526static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3527 struct snd_kcontrol *kcontrol, int event)
3528{
3529 struct snd_soc_dapm_path *source_p, *sink_p;
3530 struct snd_soc_dai *source, *sink;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003531 const struct snd_soc_pcm_stream *config = w->params + w->params_select;
Mark Brownc74184e2012-04-04 22:12:09 +01003532 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003533 struct snd_pcm_hw_params *params = NULL;
Nicolin Chen8053f212016-07-26 14:55:51 -07003534 struct snd_pcm_runtime *runtime = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003535 u64 fmt;
3536 int ret;
3537
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003538 if (WARN_ON(!config) ||
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003539 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3540 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003541 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003542
3543 /* We only support a single source and sink, pick the first */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003544 source_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_OUT],
3545 struct snd_soc_dapm_path,
3546 list_node[SND_SOC_DAPM_DIR_OUT]);
3547 sink_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_IN],
3548 struct snd_soc_dapm_path,
3549 list_node[SND_SOC_DAPM_DIR_IN]);
Mark Brownc74184e2012-04-04 22:12:09 +01003550
3551 source = source_p->source->priv;
3552 sink = sink_p->sink->priv;
3553
3554 /* Be a little careful as we don't want to overflow the mask array */
3555 if (config->formats) {
3556 fmt = ffs(config->formats) - 1;
3557 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003558 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003559 config->formats);
3560 fmt = 0;
3561 }
3562
3563 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003564 params = kzalloc(sizeof(*params), GFP_KERNEL);
3565 if (!params) {
3566 ret = -ENOMEM;
3567 goto out;
3568 }
3569 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003570
Mark Brown9747cec2012-04-26 19:12:21 +01003571 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003572 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003573 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003574 config->rate_max;
3575
Mark Brown9747cec2012-04-26 19:12:21 +01003576 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003577 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003578 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003579 = config->channels_max;
3580
3581 memset(&substream, 0, sizeof(substream));
3582
Nicolin Chen8053f212016-07-26 14:55:51 -07003583 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3584 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3585 if (!runtime) {
3586 ret = -ENOMEM;
3587 goto out;
3588 }
3589 substream.runtime = runtime;
3590
Mark Brownc74184e2012-04-04 22:12:09 +01003591 switch (event) {
3592 case SND_SOC_DAPM_PRE_PMU:
Benoit Cousson93e69582014-07-08 23:19:38 +02003593 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303594 if (source->driver->ops && source->driver->ops->startup) {
3595 ret = source->driver->ops->startup(&substream, source);
3596 if (ret < 0) {
3597 dev_err(source->dev,
3598 "ASoC: startup() failed: %d\n", ret);
3599 goto out;
3600 }
3601 source->active++;
3602 }
Benoit Cousson93e69582014-07-08 23:19:38 +02003603 ret = soc_dai_hw_params(&substream, params, source);
3604 if (ret < 0)
3605 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003606
Benoit Cousson93e69582014-07-08 23:19:38 +02003607 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303608 if (sink->driver->ops && sink->driver->ops->startup) {
3609 ret = sink->driver->ops->startup(&substream, sink);
3610 if (ret < 0) {
3611 dev_err(sink->dev,
3612 "ASoC: startup() failed: %d\n", ret);
3613 goto out;
3614 }
3615 sink->active++;
3616 }
Benoit Cousson93e69582014-07-08 23:19:38 +02003617 ret = soc_dai_hw_params(&substream, params, sink);
3618 if (ret < 0)
3619 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003620 break;
3621
3622 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003623 ret = snd_soc_dai_digital_mute(sink, 0,
3624 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003625 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003626 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003627 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003628 break;
3629
3630 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003631 ret = snd_soc_dai_digital_mute(sink, 1,
3632 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003633 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003634 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003635 ret = 0;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303636
3637 source->active--;
3638 if (source->driver->ops && source->driver->ops->shutdown) {
3639 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3640 source->driver->ops->shutdown(&substream, source);
3641 }
3642
3643 sink->active--;
3644 if (sink->driver->ops && sink->driver->ops->shutdown) {
3645 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3646 sink->driver->ops->shutdown(&substream, sink);
3647 }
Mark Brownc74184e2012-04-04 22:12:09 +01003648 break;
3649
3650 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003651 WARN(1, "Unknown event %d\n", event);
Sudip Mukherjee75881df2015-09-10 18:01:44 +05303652 ret = -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003653 }
3654
Mark Brown9747cec2012-04-26 19:12:21 +01003655out:
Nicolin Chen8053f212016-07-26 14:55:51 -07003656 kfree(runtime);
Mark Brown9747cec2012-04-26 19:12:21 +01003657 kfree(params);
3658 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003659}
3660
Nikesh Oswalc6615082015-02-02 17:06:44 +00003661static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3662 struct snd_ctl_elem_value *ucontrol)
3663{
3664 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3665
Takashi Iwai741338f2016-02-29 17:20:48 +01003666 ucontrol->value.enumerated.item[0] = w->params_select;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003667
3668 return 0;
3669}
3670
3671static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3672 struct snd_ctl_elem_value *ucontrol)
3673{
3674 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3675
3676 /* Can't change the config when widget is already powered */
3677 if (w->power)
3678 return -EBUSY;
3679
Takashi Iwai741338f2016-02-29 17:20:48 +01003680 if (ucontrol->value.enumerated.item[0] == w->params_select)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003681 return 0;
3682
Takashi Iwai741338f2016-02-29 17:20:48 +01003683 if (ucontrol->value.enumerated.item[0] >= w->num_params)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003684 return -EINVAL;
3685
Takashi Iwai741338f2016-02-29 17:20:48 +01003686 w->params_select = ucontrol->value.enumerated.item[0];
Nikesh Oswalc6615082015-02-02 17:06:44 +00003687
3688 return 0;
3689}
3690
Mark Brownc74184e2012-04-04 22:12:09 +01003691int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3692 const struct snd_soc_pcm_stream *params,
Nikesh Oswalc6615082015-02-02 17:06:44 +00003693 unsigned int num_params,
Mark Brownc74184e2012-04-04 22:12:09 +01003694 struct snd_soc_dapm_widget *source,
3695 struct snd_soc_dapm_widget *sink)
3696{
Mark Brownc74184e2012-04-04 22:12:09 +01003697 struct snd_soc_dapm_widget template;
3698 struct snd_soc_dapm_widget *w;
Mark Brownc74184e2012-04-04 22:12:09 +01003699 char *link_name;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003700 int ret, count;
3701 unsigned long private_value;
3702 const char **w_param_text;
3703 struct soc_enum w_param_enum[] = {
3704 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3705 };
3706 struct snd_kcontrol_new kcontrol_dai_link[] = {
3707 SOC_ENUM_EXT(NULL, w_param_enum[0],
3708 snd_soc_dapm_dai_link_get,
3709 snd_soc_dapm_dai_link_put),
3710 };
3711 const struct snd_soc_pcm_stream *config = params;
Mark Brownc74184e2012-04-04 22:12:09 +01003712
Nikesh Oswalc6615082015-02-02 17:06:44 +00003713 w_param_text = devm_kcalloc(card->dev, num_params,
3714 sizeof(char *), GFP_KERNEL);
3715 if (!w_param_text)
Mark Brownc74184e2012-04-04 22:12:09 +01003716 return -ENOMEM;
Mark Brownc74184e2012-04-04 22:12:09 +01003717
Charles Keepax46172b62015-03-25 11:22:35 +00003718 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3719 source->name, sink->name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003720 if (!link_name) {
3721 ret = -ENOMEM;
3722 goto outfree_w_param;
3723 }
Mark Brownc74184e2012-04-04 22:12:09 +01003724
Nikesh Oswalc6615082015-02-02 17:06:44 +00003725 for (count = 0 ; count < num_params; count++) {
3726 if (!config->stream_name) {
3727 dev_warn(card->dapm.dev,
3728 "ASoC: anonymous config %d for dai link %s\n",
3729 count, link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003730 w_param_text[count] =
Charles Keepax46172b62015-03-25 11:22:35 +00003731 devm_kasprintf(card->dev, GFP_KERNEL,
3732 "Anonymous Configuration %d",
3733 count);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003734 if (!w_param_text[count]) {
3735 ret = -ENOMEM;
3736 goto outfree_link_name;
3737 }
Nikesh Oswalc6615082015-02-02 17:06:44 +00003738 } else {
3739 w_param_text[count] = devm_kmemdup(card->dev,
3740 config->stream_name,
3741 strlen(config->stream_name) + 1,
3742 GFP_KERNEL);
3743 if (!w_param_text[count]) {
3744 ret = -ENOMEM;
3745 goto outfree_link_name;
3746 }
3747 }
3748 config++;
3749 }
3750 w_param_enum[0].items = num_params;
3751 w_param_enum[0].texts = w_param_text;
Mark Brownc74184e2012-04-04 22:12:09 +01003752
3753 memset(&template, 0, sizeof(template));
3754 template.reg = SND_SOC_NOPM;
3755 template.id = snd_soc_dapm_dai_link;
3756 template.name = link_name;
3757 template.event = snd_soc_dai_link_event;
3758 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3759 SND_SOC_DAPM_PRE_PMD;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003760 template.num_kcontrols = 1;
3761 /* duplicate w_param_enum on heap so that memory persists */
3762 private_value =
3763 (unsigned long) devm_kmemdup(card->dev,
3764 (void *)(kcontrol_dai_link[0].private_value),
3765 sizeof(struct soc_enum), GFP_KERNEL);
3766 if (!private_value) {
3767 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3768 link_name);
3769 ret = -ENOMEM;
3770 goto outfree_link_name;
3771 }
3772 kcontrol_dai_link[0].private_value = private_value;
3773 /* duplicate kcontrol_dai_link on heap so that memory persists */
3774 template.kcontrol_news =
3775 devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3776 sizeof(struct snd_kcontrol_new),
3777 GFP_KERNEL);
3778 if (!template.kcontrol_news) {
3779 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3780 link_name);
3781 ret = -ENOMEM;
3782 goto outfree_private_value;
3783 }
Mark Brownc74184e2012-04-04 22:12:09 +01003784
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003785 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003786
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003787 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
Linus Walleijde415c82017-01-13 10:23:52 +01003788 if (IS_ERR(w)) {
3789 ret = PTR_ERR(w);
3790 /* Do not nag about probe deferrals */
3791 if (ret != -EPROBE_DEFER)
3792 dev_err(card->dev,
3793 "ASoC: Failed to create %s widget (%d)\n",
3794 link_name, ret);
3795 goto outfree_kcontrol_news;
3796 }
Mark Brownc74184e2012-04-04 22:12:09 +01003797 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003798 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003799 link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003800 ret = -ENOMEM;
3801 goto outfree_kcontrol_news;
Mark Brownc74184e2012-04-04 22:12:09 +01003802 }
3803
3804 w->params = params;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003805 w->num_params = num_params;
Mark Brownc74184e2012-04-04 22:12:09 +01003806
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003807 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3808 if (ret)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003809 goto outfree_w;
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003810 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003811
3812outfree_w:
3813 devm_kfree(card->dev, w);
3814outfree_kcontrol_news:
3815 devm_kfree(card->dev, (void *)template.kcontrol_news);
3816outfree_private_value:
3817 devm_kfree(card->dev, (void *)private_value);
3818outfree_link_name:
3819 devm_kfree(card->dev, link_name);
3820outfree_w_param:
3821 for (count = 0 ; count < num_params; count++)
3822 devm_kfree(card->dev, (void *)w_param_text[count]);
3823 devm_kfree(card->dev, w_param_text);
3824
3825 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003826}
3827
Mark Brown888df392012-02-16 19:37:51 -08003828int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3829 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003830{
Mark Brown888df392012-02-16 19:37:51 -08003831 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003832 struct snd_soc_dapm_widget *w;
3833
Mark Brown888df392012-02-16 19:37:51 -08003834 WARN_ON(dapm->dev != dai->dev);
3835
3836 memset(&template, 0, sizeof(template));
3837 template.reg = SND_SOC_NOPM;
3838
3839 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003840 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003841 template.name = dai->driver->playback.stream_name;
3842 template.sname = dai->driver->playback.stream_name;
3843
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003844 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003845 template.name);
3846
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003847 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Linus Walleije2d1a422017-01-20 14:07:52 +01003848 if (IS_ERR(w)) {
3849 int ret = PTR_ERR(w);
3850
3851 /* Do not nag about probe deferrals */
3852 if (ret != -EPROBE_DEFER)
3853 dev_err(dapm->dev,
3854 "ASoC: Failed to create %s widget (%d)\n",
3855 dai->driver->playback.stream_name, ret);
3856 return ret;
3857 }
Mark Brown888df392012-02-16 19:37:51 -08003858 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003859 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003860 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003861 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003862 }
3863
3864 w->priv = dai;
3865 dai->playback_widget = w;
3866 }
3867
3868 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003869 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003870 template.name = dai->driver->capture.stream_name;
3871 template.sname = dai->driver->capture.stream_name;
3872
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003873 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003874 template.name);
3875
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003876 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Linus Walleije2d1a422017-01-20 14:07:52 +01003877 if (IS_ERR(w)) {
3878 int ret = PTR_ERR(w);
3879
3880 /* Do not nag about probe deferrals */
3881 if (ret != -EPROBE_DEFER)
3882 dev_err(dapm->dev,
3883 "ASoC: Failed to create %s widget (%d)\n",
3884 dai->driver->playback.stream_name, ret);
3885 return ret;
3886 }
Mark Brown888df392012-02-16 19:37:51 -08003887 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003888 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003889 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003890 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003891 }
3892
3893 w->priv = dai;
3894 dai->capture_widget = w;
3895 }
3896
3897 return 0;
3898}
3899
3900int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3901{
3902 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003903 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08003904 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003905
3906 /* For each DAI widget... */
3907 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003908 switch (dai_w->id) {
3909 case snd_soc_dapm_dai_in:
3910 case snd_soc_dapm_dai_out:
3911 break;
3912 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003913 continue;
Mark Brown46162742013-06-05 19:36:11 +01003914 }
Mark Brown888df392012-02-16 19:37:51 -08003915
Liam Girdwooddfbf5082018-06-14 20:26:42 +01003916 /* let users know there is no DAI to link */
3917 if (!dai_w->priv) {
3918 dev_dbg(card->dev, "dai widget %s has no DAI\n",
3919 dai_w->name);
3920 continue;
3921 }
3922
Mark Brown888df392012-02-16 19:37:51 -08003923 dai = dai_w->priv;
3924
3925 /* ...find all widgets with the same stream and link them */
3926 list_for_each_entry(w, &card->widgets, list) {
3927 if (w->dapm != dai_w->dapm)
3928 continue;
3929
Mark Brown46162742013-06-05 19:36:11 +01003930 switch (w->id) {
3931 case snd_soc_dapm_dai_in:
3932 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003933 continue;
Mark Brown46162742013-06-05 19:36:11 +01003934 default:
3935 break;
3936 }
Mark Brown888df392012-02-16 19:37:51 -08003937
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003938 if (!w->sname || !strstr(w->sname, dai_w->sname))
Mark Brown888df392012-02-16 19:37:51 -08003939 continue;
3940
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003941 if (dai_w->id == snd_soc_dapm_dai_in) {
3942 src = dai_w;
3943 sink = w;
3944 } else {
3945 src = w;
3946 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08003947 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003948 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3949 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003950 }
3951 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003952
Mark Brown888df392012-02-16 19:37:51 -08003953 return 0;
3954}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003955
Benoit Cousson44ba2642014-07-08 23:19:36 +02003956static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3957 struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003958{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003959 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003960 struct snd_soc_dapm_widget *sink, *source;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003961 int i;
3962
Benoit Cousson44ba2642014-07-08 23:19:36 +02003963 for (i = 0; i < rtd->num_codecs; i++) {
3964 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003965
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003966 /* connect BE DAI playback if widgets are valid */
3967 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003968 source = cpu_dai->playback_widget;
3969 sink = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003970 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003971 cpu_dai->component->name, source->name,
3972 codec_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003973
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003974 snd_soc_dapm_add_path(&card->dapm, source, sink,
3975 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003976 }
3977
3978 /* connect BE DAI capture if widgets are valid */
3979 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003980 source = codec_dai->capture_widget;
3981 sink = cpu_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003982 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003983 codec_dai->component->name, source->name,
3984 cpu_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003985
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003986 snd_soc_dapm_add_path(&card->dapm, source, sink,
3987 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003988 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003989 }
3990}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003991
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003992static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3993 int event)
3994{
3995 struct snd_soc_dapm_widget *w;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003996 unsigned int ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003997
3998 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3999 w = dai->playback_widget;
4000 else
4001 w = dai->capture_widget;
4002
4003 if (w) {
4004 dapm_mark_dirty(w, "stream event");
4005
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004006 if (w->id == snd_soc_dapm_dai_in) {
4007 ep = SND_SOC_DAPM_EP_SOURCE;
4008 dapm_widget_invalidate_input_paths(w);
4009 } else {
4010 ep = SND_SOC_DAPM_EP_SINK;
4011 dapm_widget_invalidate_output_paths(w);
4012 }
4013
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004014 switch (event) {
4015 case SND_SOC_DAPM_STREAM_START:
4016 w->active = 1;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004017 w->is_ep = ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004018 break;
4019 case SND_SOC_DAPM_STREAM_STOP:
4020 w->active = 0;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004021 w->is_ep = 0;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004022 break;
4023 case SND_SOC_DAPM_STREAM_SUSPEND:
4024 case SND_SOC_DAPM_STREAM_RESUME:
4025 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4026 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4027 break;
4028 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004029 }
4030}
4031
Benoit Cousson44ba2642014-07-08 23:19:36 +02004032void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4033{
Mengdong Lin1a497982015-11-18 02:34:11 -05004034 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson44ba2642014-07-08 23:19:36 +02004035
4036 /* for each BE DAI link... */
Mengdong Lin1a497982015-11-18 02:34:11 -05004037 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson44ba2642014-07-08 23:19:36 +02004038 /*
4039 * dynamic FE links have no fixed DAI mapping.
4040 * CODEC<->CODEC links have no direct connection.
4041 */
4042 if (rtd->dai_link->dynamic || rtd->dai_link->params)
4043 continue;
4044
4045 dapm_connect_dai_link_widgets(card, rtd);
4046 }
4047}
4048
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004049static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4050 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004051{
Benoit Cousson44ba2642014-07-08 23:19:36 +02004052 int i;
4053
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004054 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
Benoit Cousson44ba2642014-07-08 23:19:36 +02004055 for (i = 0; i < rtd->num_codecs; i++)
4056 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004057
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004058 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004059}
4060
4061/**
4062 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4063 * @rtd: PCM runtime data
4064 * @stream: stream name
4065 * @event: stream event
4066 *
4067 * Sends a stream event to the dapm core. The core then makes any
4068 * necessary widget power changes.
4069 *
4070 * Returns 0 for success else error.
4071 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004072void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4073 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004074{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004075 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004076
Liam Girdwood3cd04342012-03-09 12:02:08 +00004077 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004078 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004079 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004080}
Richard Purdie2b97eab2006-10-06 18:32:18 +02004081
4082/**
Charles Keepax11391102014-02-18 15:22:14 +00004083 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4084 * @dapm: DAPM context
4085 * @pin: pin name
4086 *
4087 * Enables input/output pin and its parents or children widgets iff there is
4088 * a valid audio route and active audio stream.
4089 *
4090 * Requires external locking.
4091 *
4092 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4093 * do any widget power switching.
4094 */
4095int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4096 const char *pin)
4097{
4098 return snd_soc_dapm_set_pin(dapm, pin, 1);
4099}
4100EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4101
4102/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004103 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004104 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004105 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02004106 *
Mark Brown74b8f952009-06-06 11:26:15 +01004107 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01004108 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00004109 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004110 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4111 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02004112 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004113int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004114{
Charles Keepax11391102014-02-18 15:22:14 +00004115 int ret;
4116
4117 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4118
4119 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4120
4121 mutex_unlock(&dapm->card->dapm_mutex);
4122
4123 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004124}
Liam Girdwooda5302182008-07-07 13:35:17 +01004125EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004126
4127/**
Charles Keepax11391102014-02-18 15:22:14 +00004128 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4129 * @dapm: DAPM context
4130 * @pin: pin name
4131 *
4132 * Enables input/output pin regardless of any other state. This is
4133 * intended for use with microphone bias supplies used in microphone
4134 * jack detection.
4135 *
4136 * Requires external locking.
4137 *
4138 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4139 * do any widget power switching.
4140 */
4141int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4142 const char *pin)
4143{
4144 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4145
4146 if (!w) {
4147 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4148 return -EINVAL;
4149 }
4150
4151 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02004152 if (!w->connected) {
4153 /*
4154 * w->force does not affect the number of input or output paths,
4155 * so we only have to recheck if w->connected is changed
4156 */
4157 dapm_widget_invalidate_input_paths(w);
4158 dapm_widget_invalidate_output_paths(w);
4159 w->connected = 1;
4160 }
Charles Keepax11391102014-02-18 15:22:14 +00004161 w->force = 1;
4162 dapm_mark_dirty(w, "force enable");
4163
4164 return 0;
4165}
4166EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4167
4168/**
Mark Brownda341832010-03-15 19:23:37 +00004169 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004170 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00004171 * @pin: pin name
4172 *
4173 * Enables input/output pin regardless of any other state. This is
4174 * intended for use with microphone bias supplies used in microphone
4175 * jack detection.
4176 *
4177 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4178 * do any widget power switching.
4179 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004180int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4181 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00004182{
Charles Keepax11391102014-02-18 15:22:14 +00004183 int ret;
Mark Brownda341832010-03-15 19:23:37 +00004184
Charles Keepax11391102014-02-18 15:22:14 +00004185 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00004186
Charles Keepax11391102014-02-18 15:22:14 +00004187 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09004188
Charles Keepax11391102014-02-18 15:22:14 +00004189 mutex_unlock(&dapm->card->dapm_mutex);
4190
4191 return ret;
Mark Brownda341832010-03-15 19:23:37 +00004192}
4193EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4194
4195/**
Charles Keepax11391102014-02-18 15:22:14 +00004196 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4197 * @dapm: DAPM context
4198 * @pin: pin name
4199 *
4200 * Disables input/output pin and its parents or children widgets.
4201 *
4202 * Requires external locking.
4203 *
4204 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4205 * do any widget power switching.
4206 */
4207int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4208 const char *pin)
4209{
4210 return snd_soc_dapm_set_pin(dapm, pin, 0);
4211}
4212EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4213
4214/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004215 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004216 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004217 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004218 *
Mark Brown74b8f952009-06-06 11:26:15 +01004219 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00004220 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004221 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4222 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004223 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004224int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4225 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01004226{
Charles Keepax11391102014-02-18 15:22:14 +00004227 int ret;
4228
4229 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4230
4231 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4232
4233 mutex_unlock(&dapm->card->dapm_mutex);
4234
4235 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01004236}
4237EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4238
4239/**
Charles Keepax11391102014-02-18 15:22:14 +00004240 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4241 * @dapm: DAPM context
4242 * @pin: pin name
4243 *
4244 * Marks the specified pin as being not connected, disabling it along
4245 * any parent or child widgets. At present this is identical to
4246 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4247 * additional things such as disabling controls which only affect
4248 * paths through the pin.
4249 *
4250 * Requires external locking.
4251 *
4252 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4253 * do any widget power switching.
4254 */
4255int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4256 const char *pin)
4257{
4258 return snd_soc_dapm_set_pin(dapm, pin, 0);
4259}
4260EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4261
4262/**
Mark Brown5817b522008-09-24 11:23:11 +01004263 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004264 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01004265 * @pin: pin name
4266 *
4267 * Marks the specified pin as being not connected, disabling it along
4268 * any parent or child widgets. At present this is identical to
4269 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4270 * additional things such as disabling controls which only affect
4271 * paths through the pin.
4272 *
4273 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4274 * do any widget power switching.
4275 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004276int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01004277{
Charles Keepax11391102014-02-18 15:22:14 +00004278 int ret;
4279
4280 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4281
4282 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4283
4284 mutex_unlock(&dapm->card->dapm_mutex);
4285
4286 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01004287}
4288EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4289
4290/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004291 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004292 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004293 * @pin: audio signal pin endpoint (or start point)
4294 *
4295 * Get audio pin status - connected or disconnected.
4296 *
4297 * Returns 1 for connected otherwise 0.
4298 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004299int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4300 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004301{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004302 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004303
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004304 if (w)
4305 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06004306
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004307 return 0;
4308}
Liam Girdwooda5302182008-07-07 13:35:17 +01004309EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004310
4311/**
Mark Brown1547aba2010-05-07 21:11:40 +01004312 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004313 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01004314 * @pin: audio signal pin endpoint (or start point)
4315 *
4316 * Mark the given endpoint or pin as ignoring suspend. When the
4317 * system is disabled a path between two endpoints flagged as ignoring
4318 * suspend will not be disabled. The path must already be enabled via
4319 * normal means at suspend time, it will not be turned on if it was not
4320 * already enabled.
4321 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004322int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4323 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01004324{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004325 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01004326
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004327 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004328 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004329 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01004330 }
4331
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004332 w->ignore_suspend = 1;
4333
4334 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01004335}
4336EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4337
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02004338/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02004339 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03004340 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02004341 *
4342 * Free all dapm widgets and resources.
4343 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004344void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004345{
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02004346 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004347 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02004348 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004349}
4350EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4351
Xiang Xiao57996352014-03-02 00:04:02 +08004352static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01004353{
Liam Girdwood01005a72012-07-06 16:57:05 +01004354 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01004355 struct snd_soc_dapm_widget *w;
4356 LIST_HEAD(down_list);
4357 int powerdown = 0;
4358
Liam Girdwood01005a72012-07-06 16:57:05 +01004359 mutex_lock(&card->dapm_mutex);
4360
Jarkko Nikula97c866d2010-12-14 12:18:31 +02004361 list_for_each_entry(w, &dapm->card->widgets, list) {
4362 if (w->dapm != dapm)
4363 continue;
Mark Brown51737472009-06-22 13:16:51 +01004364 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004365 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01004366 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004367 powerdown = 1;
4368 }
4369 }
4370
4371 /* If there were no widgets to power down we're already in
4372 * standby.
4373 */
4374 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004375 if (dapm->bias_level == SND_SOC_BIAS_ON)
4376 snd_soc_dapm_set_bias_level(dapm,
4377 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004378 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004379 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4380 snd_soc_dapm_set_bias_level(dapm,
4381 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004382 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004383
4384 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004385}
Mark Brown51737472009-06-22 13:16:51 +01004386
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004387/*
4388 * snd_soc_dapm_shutdown - callback for system shutdown
4389 */
4390void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4391{
Xiang Xiao57996352014-03-02 00:04:02 +08004392 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004393
Xiang Xiao57996352014-03-02 00:04:02 +08004394 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08004395 if (dapm != &card->dapm) {
4396 soc_dapm_shutdown_dapm(dapm);
4397 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4398 snd_soc_dapm_set_bias_level(dapm,
4399 SND_SOC_BIAS_OFF);
4400 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004401 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08004402
4403 soc_dapm_shutdown_dapm(&card->dapm);
4404 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4405 snd_soc_dapm_set_bias_level(&card->dapm,
4406 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01004407}
4408
Richard Purdie2b97eab2006-10-06 18:32:18 +02004409/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004410MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004411MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4412MODULE_LICENSE("GPL");