blob: 34a149deeba1274e56879a60affba3713e5fd42b [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) {
Sudheer Papothi9eb600c2016-01-29 02:04:49 +0530285 if (w->ignore_suspend)
286 continue;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200287 if (w->is_ep) {
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200288 dapm_mark_dirty(w, "Rechecking endpoints");
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200289 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200290 dapm_widget_invalidate_output_paths(w);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200291 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200292 dapm_widget_invalidate_input_paths(w);
293 }
Mark Browne2d32ff2012-08-31 17:38:32 -0700294 }
295
296 mutex_unlock(&card->dapm_mutex);
297}
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200298EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
Mark Browne2d32ff2012-08-31 17:38:32 -0700299
Richard Purdie2b97eab2006-10-06 18:32:18 +0200300/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100301static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200302 const struct snd_soc_dapm_widget *_widget)
303{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100304 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200305}
306
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200307struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200308 unsigned int value;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200309 struct snd_soc_dapm_widget *widget;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200310 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200311 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200312};
313
314static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100315 struct snd_kcontrol *kcontrol, const char *ctrl_name)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200316{
317 struct dapm_kcontrol_data *data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200318 struct soc_mixer_control *mc;
Charles Keepax561ed682015-05-01 12:37:26 +0100319 struct soc_enum *e;
Charles Keepax773da9b2015-05-01 12:37:25 +0100320 const char *name;
321 int ret;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200322
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200323 data = kzalloc(sizeof(*data), GFP_KERNEL);
Charles Keepax40b7bea2015-05-01 12:37:24 +0100324 if (!data)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200325 return -ENOMEM;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200326
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200327 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200328
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200329 switch (widget->id) {
330 case snd_soc_dapm_switch:
331 case snd_soc_dapm_mixer:
332 case snd_soc_dapm_mixer_named_ctl:
333 mc = (struct soc_mixer_control *)kcontrol->private_value;
334
335 if (mc->autodisable) {
336 struct snd_soc_dapm_widget template;
337
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100338 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
Charles Keepax773da9b2015-05-01 12:37:25 +0100339 "Autodisable");
340 if (!name) {
341 ret = -ENOMEM;
342 goto err_data;
343 }
344
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200345 memset(&template, 0, sizeof(template));
346 template.reg = mc->reg;
347 template.mask = (1 << fls(mc->max)) - 1;
348 template.shift = mc->shift;
349 if (mc->invert)
350 template.off_val = mc->max;
351 else
352 template.off_val = 0;
353 template.on_val = template.off_val;
354 template.id = snd_soc_dapm_kcontrol;
Charles Keepax773da9b2015-05-01 12:37:25 +0100355 template.name = name;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200356
Lars-Peter Clausen2daabd72013-08-30 17:39:33 +0200357 data->value = template.on_val;
358
Liam Girdwood02aa78a2015-05-25 18:21:17 +0100359 data->widget =
360 snd_soc_dapm_new_control_unlocked(widget->dapm,
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200361 &template);
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200362 kfree(name);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200363 if (!data->widget) {
Charles Keepax773da9b2015-05-01 12:37:25 +0100364 ret = -ENOMEM;
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200365 goto err_data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200366 }
367 }
368 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200369 case snd_soc_dapm_demux:
Charles Keepax561ed682015-05-01 12:37:26 +0100370 case snd_soc_dapm_mux:
371 e = (struct soc_enum *)kcontrol->private_value;
372
373 if (e->autodisable) {
374 struct snd_soc_dapm_widget template;
375
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100376 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
Charles Keepax561ed682015-05-01 12:37:26 +0100377 "Autodisable");
378 if (!name) {
379 ret = -ENOMEM;
380 goto err_data;
381 }
382
383 memset(&template, 0, sizeof(template));
384 template.reg = e->reg;
385 template.mask = e->mask << e->shift_l;
386 template.shift = e->shift_l;
387 template.off_val = snd_soc_enum_item_to_val(e, 0);
388 template.on_val = template.off_val;
389 template.id = snd_soc_dapm_kcontrol;
390 template.name = name;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200391
392 data->value = template.on_val;
393
Charles Keepaxffacb482015-06-26 10:39:43 +0100394 data->widget = snd_soc_dapm_new_control_unlocked(
395 widget->dapm, &template);
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200396 kfree(name);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200397 if (!data->widget) {
Charles Keepax561ed682015-05-01 12:37:26 +0100398 ret = -ENOMEM;
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200399 goto err_data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200400 }
Charles Keepax561ed682015-05-01 12:37:26 +0100401
402 snd_soc_dapm_add_path(widget->dapm, data->widget,
403 widget, NULL, NULL);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200404 }
405 break;
406 default:
407 break;
408 }
409
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200410 kcontrol->private_data = data;
411
412 return 0;
Charles Keepax773da9b2015-05-01 12:37:25 +0100413
Charles Keepax773da9b2015-05-01 12:37:25 +0100414err_data:
415 kfree(data);
416 return ret;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200417}
418
419static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
420{
421 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200422 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200423 kfree(data);
424}
425
426static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
427 const struct snd_kcontrol *kcontrol)
428{
429 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
430
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200431 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200432}
433
434static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
435 struct snd_soc_dapm_widget *widget)
436{
437 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200438 struct snd_soc_dapm_widget_list *new_wlist;
439 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200440
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200441 if (data->wlist)
442 n = data->wlist->num_widgets + 1;
443 else
444 n = 1;
445
446 new_wlist = krealloc(data->wlist,
447 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
448 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200449 return -ENOMEM;
450
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200451 new_wlist->widgets[n - 1] = widget;
452 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200453
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200454 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200455
456 return 0;
457}
458
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200459static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
460 struct snd_soc_dapm_path *path)
461{
462 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
463
464 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200465}
466
467static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
468{
469 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
470
471 if (!data->widget)
472 return true;
473
474 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200475}
476
477static struct list_head *dapm_kcontrol_get_path_list(
478 const struct snd_kcontrol *kcontrol)
479{
480 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
481
482 return &data->paths;
483}
484
485#define dapm_kcontrol_for_each_path(path, kcontrol) \
486 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
487 list_kcontrol)
488
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530489unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200490{
491 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
492
493 return data->value;
494}
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530495EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200496
497static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
498 unsigned int value)
499{
500 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
501
502 if (data->value == value)
503 return false;
504
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200505 if (data->widget)
506 data->widget->on_val = value;
507
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200508 data->value = value;
509
510 return true;
511}
512
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200513/**
Mythri P K93e39a12015-10-20 22:30:08 +0530514 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
515 * kcontrol
516 * @kcontrol: The kcontrol
517 */
518struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
519 struct snd_kcontrol *kcontrol)
520{
521 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
522}
523EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
524
525/**
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200526 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
527 * kcontrol
528 * @kcontrol: The kcontrol
529 *
530 * Note: This function must only be used on kcontrols that are known to have
531 * been registered for a CODEC. Otherwise the behaviour is undefined.
532 */
533struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
534 struct snd_kcontrol *kcontrol)
535{
536 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
537}
538EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
539
Liam Girdwood6c120e12012-02-15 15:15:34 +0000540static void dapm_reset(struct snd_soc_card *card)
541{
542 struct snd_soc_dapm_widget *w;
543
Mark Brownf9fa2b12014-03-06 16:49:11 +0800544 lockdep_assert_held(&card->dapm_mutex);
545
Liam Girdwood6c120e12012-02-15 15:15:34 +0000546 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
547
548 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200549 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000550 w->power_checked = false;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000551 }
552}
553
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200554static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
555{
556 if (!dapm->component)
557 return NULL;
558 return dapm->component->name_prefix;
559}
560
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200561static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800562 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100563{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200564 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200565 return -EIO;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200566 return snd_soc_component_read(dapm->component, reg, value);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100567}
568
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200569static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
Bard Liao34775012014-04-17 20:12:56 +0800570 int reg, unsigned int mask, 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;
Mark Brownfcf6c5e2014-12-15 13:08:48 +0000574 return snd_soc_component_update_bits(dapm->component, reg,
575 mask, value);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000576}
577
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200578static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
579 int reg, unsigned int mask, unsigned int value)
580{
581 if (!dapm->component)
582 return -EIO;
583 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
584}
585
Mark Browneb270e92013-10-09 13:52:52 +0100586static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
587{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200588 if (dapm->component)
589 snd_soc_component_async_complete(dapm->component);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100590}
591
Charles Keepax45a110a2015-05-11 13:50:30 +0100592static struct snd_soc_dapm_widget *
593dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
594{
595 struct snd_soc_dapm_widget *w = wcache->widget;
596 struct list_head *wlist;
597 const int depth = 2;
598 int i = 0;
599
600 if (w) {
601 wlist = &w->dapm->card->widgets;
602
603 list_for_each_entry_from(w, wlist, list) {
604 if (!strcmp(name, w->name))
605 return w;
606
607 if (++i == depth)
608 break;
609 }
610 }
611
612 return NULL;
613}
614
615static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
616 struct snd_soc_dapm_widget *w)
617{
618 wcache->widget = w;
619}
620
Mark Brown452c5ea2009-05-17 21:41:23 +0100621/**
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200622 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
623 * @dapm: The DAPM context for which to set the level
624 * @level: The level to set
625 *
626 * Forces the DAPM bias level to a specific state. It will call the bias level
627 * callback of DAPM context with the specified level. This will even happen if
628 * the context is already at the same level. Furthermore it will not go through
629 * the normal bias level sequencing, meaning any intermediate states between the
630 * current and the target state will not be entered.
631 *
632 * Note that the change in bias level is only temporary and the next time
633 * snd_soc_dapm_sync() is called the state will be set to the level as
634 * determined by the DAPM core. The function is mainly intended to be used to
635 * used during probe or resume from suspend to power up the device so
636 * initialization can be done, before the DAPM core takes over.
637 */
638int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
639 enum snd_soc_bias_level level)
640{
641 int ret = 0;
642
643 if (dapm->set_bias_level)
644 ret = dapm->set_bias_level(dapm, level);
645
Lars-Peter Clausenf4bf8d72015-04-27 22:13:25 +0200646 if (ret == 0)
647 dapm->bias_level = level;
648
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200649 return ret;
650}
651EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
652
Mark Brown452c5ea2009-05-17 21:41:23 +0100653/**
654 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800655 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100656 * @level: level to configure
657 *
658 * Configure the bias (power) levels for the SoC audio device.
659 *
660 * Returns 0 for success else error.
661 */
Mark Browned5a4c42011-02-18 11:12:42 -0800662static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200663 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100664{
Mark Browned5a4c42011-02-18 11:12:42 -0800665 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100666 int ret = 0;
667
Mark Brown84e90932010-11-04 00:07:02 -0400668 trace_snd_soc_bias_level_start(card, level);
669
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000670 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100671 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100672 if (ret != 0)
673 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100674
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200675 if (!card || dapm != &card->dapm)
676 ret = snd_soc_dapm_force_bias_level(dapm, level);
Liam Girdwood41231282012-07-06 16:56:16 +0100677
Mark Brown171ec6b2011-06-06 18:15:19 +0100678 if (ret != 0)
679 goto out;
680
681 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100682 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100683out:
Mark Brown84e90932010-11-04 00:07:02 -0400684 trace_snd_soc_bias_level_done(card, level);
685
Mark Brown452c5ea2009-05-17 21:41:23 +0100686 return ret;
687}
688
Mark Brown74b8f952009-06-06 11:26:15 +0100689/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200690static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200691 struct snd_soc_dapm_path *path, const char *control_name,
692 struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200693{
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200694 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200695 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100696 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200697 int i;
698
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100699 if (e->reg != SND_SOC_NOPM) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200700 soc_dapm_read(dapm, e->reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100701 val = (val >> e->shift_l) & e->mask;
702 item = snd_soc_enum_val_to_item(e, val);
703 } else {
704 /* since a virtual mux has no backing registers to
705 * decide which path to connect, it will try to match
706 * with the first enumeration. This is to ensure
707 * that the default mux choice (the first) will be
708 * correctly powered up during initialization.
709 */
710 item = 0;
711 }
712
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100713 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200714 if (!(strcmp(control_name, e->texts[i]))) {
Rasmus Villemoes98ad73c2014-10-21 17:01:15 +0200715 path->name = e->texts[i];
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100716 if (i == item)
717 path->connect = 1;
718 else
719 path->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200720 return 0;
721 }
722 }
723
724 return -ENODEV;
725}
726
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100727/* set up initial codec paths */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200728static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i)
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100729{
730 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200731 p->sink->kcontrol_news[i].private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100732 unsigned int reg = mc->reg;
733 unsigned int shift = mc->shift;
734 unsigned int max = mc->max;
735 unsigned int mask = (1 << fls(max)) - 1;
736 unsigned int invert = mc->invert;
737 unsigned int val;
738
739 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200740 soc_dapm_read(p->sink->dapm, reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100741 val = (val >> shift) & mask;
742 if (invert)
743 val = max - val;
744 p->connect = !!val;
745 } else {
746 p->connect = 0;
747 }
748}
749
Mark Brown74b8f952009-06-06 11:26:15 +0100750/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200751static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200752 struct snd_soc_dapm_path *path, const char *control_name)
753{
754 int i;
755
756 /* search for mixer kcontrol */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200757 for (i = 0; i < path->sink->num_kcontrols; i++) {
758 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
759 path->name = path->sink->kcontrol_news[i].name;
760 dapm_set_mixer_path_status(path, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200761 return 0;
762 }
763 }
764 return -ENODEV;
765}
766
Stephen Warrenaf468002011-04-28 17:38:01 -0600767static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600768 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600769 const struct snd_kcontrol_new *kcontrol_new,
770 struct snd_kcontrol **kcontrol)
771{
772 struct snd_soc_dapm_widget *w;
773 int i;
774
775 *kcontrol = NULL;
776
777 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600778 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
779 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600780 for (i = 0; i < w->num_kcontrols; i++) {
781 if (&w->kcontrol_news[i] == kcontrol_new) {
782 if (w->kcontrols)
783 *kcontrol = w->kcontrols[i];
784 return 1;
785 }
786 }
787 }
788
789 return 0;
790}
791
Stephen Warren85762e72013-03-29 15:40:10 -0600792/*
793 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
794 * create it. Either way, add the widget into the control's widget list
795 */
Jeeja KP19a2557b2015-10-20 22:30:06 +0530796static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100797 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200798{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200799 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000800 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000801 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600802 size_t prefix_len;
803 int shared;
804 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600805 bool wname_in_long_name, kcname_in_long_name;
Daniel Macke5092c92014-10-07 13:41:24 +0200806 char *long_name = NULL;
Stephen Warren85762e72013-03-29 15:40:10 -0600807 const char *name;
Daniel Macke5092c92014-10-07 13:41:24 +0200808 int ret = 0;
Mark Brownefb7ac32011-03-08 17:23:24 +0000809
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200810 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000811 if (prefix)
812 prefix_len = strlen(prefix) + 1;
813 else
814 prefix_len = 0;
815
Stephen Warren85762e72013-03-29 15:40:10 -0600816 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
817 &kcontrol);
818
Stephen Warren85762e72013-03-29 15:40:10 -0600819 if (!kcontrol) {
820 if (shared) {
821 wname_in_long_name = false;
822 kcname_in_long_name = true;
823 } else {
824 switch (w->id) {
825 case snd_soc_dapm_switch:
826 case snd_soc_dapm_mixer:
Jeeja KP19a2557b2015-10-20 22:30:06 +0530827 case snd_soc_dapm_pga:
Chen-Yu Tsaia3930ed2016-08-27 19:28:00 +0800828 case snd_soc_dapm_out_drv:
Stephen Warren85762e72013-03-29 15:40:10 -0600829 wname_in_long_name = true;
830 kcname_in_long_name = true;
831 break;
832 case snd_soc_dapm_mixer_named_ctl:
833 wname_in_long_name = false;
834 kcname_in_long_name = true;
835 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200836 case snd_soc_dapm_demux:
Stephen Warren85762e72013-03-29 15:40:10 -0600837 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600838 wname_in_long_name = true;
839 kcname_in_long_name = false;
840 break;
841 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600842 return -EINVAL;
843 }
844 }
845
846 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600847 /*
848 * The control will get a prefix from the control
849 * creation process but we're also using the same
850 * prefix for widgets so cut the prefix off the
851 * front of the widget name.
852 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200853 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600854 w->name + prefix_len,
855 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200856 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200857 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600858
859 name = long_name;
860 } else if (wname_in_long_name) {
861 long_name = NULL;
862 name = w->name + prefix_len;
863 } else {
864 long_name = NULL;
865 name = w->kcontrol_news[kci].name;
866 }
867
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200868 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600869 prefix);
Daniel Macke5092c92014-10-07 13:41:24 +0200870 if (!kcontrol) {
871 ret = -ENOMEM;
872 goto exit_free;
873 }
874
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200875 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200876
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100877 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200878 if (ret) {
879 snd_ctl_free_one(kcontrol);
Daniel Macke5092c92014-10-07 13:41:24 +0200880 goto exit_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200881 }
882
Stephen Warren85762e72013-03-29 15:40:10 -0600883 ret = snd_ctl_add(card, kcontrol);
884 if (ret < 0) {
885 dev_err(dapm->dev,
886 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
887 w->name, name, ret);
Daniel Macke5092c92014-10-07 13:41:24 +0200888 goto exit_free;
Stephen Warren85762e72013-03-29 15:40:10 -0600889 }
Stephen Warren85762e72013-03-29 15:40:10 -0600890 }
891
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200892 ret = dapm_kcontrol_add_widget(kcontrol, w);
Daniel Macke5092c92014-10-07 13:41:24 +0200893 if (ret == 0)
894 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200895
Daniel Macke5092c92014-10-07 13:41:24 +0200896exit_free:
897 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600898
Daniel Macke5092c92014-10-07 13:41:24 +0200899 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600900}
901
902/* create new dapm mixer control */
903static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
904{
905 int i, ret;
906 struct snd_soc_dapm_path *path;
Charles Keepax561ed682015-05-01 12:37:26 +0100907 struct dapm_kcontrol_data *data;
Stephen Warren85762e72013-03-29 15:40:10 -0600908
Richard Purdie2b97eab2006-10-06 18:32:18 +0200909 /* add kcontrol */
910 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200911 /* match name */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +0200912 snd_soc_dapm_widget_for_each_source_path(w, path) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200913 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600914 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200915 continue;
916
Charles Keepax561ed682015-05-01 12:37:26 +0100917 if (!w->kcontrols[i]) {
Jeeja KP19a2557b2015-10-20 22:30:06 +0530918 ret = dapm_create_or_share_kcontrol(w, i);
Charles Keepax561ed682015-05-01 12:37:26 +0100919 if (ret < 0)
920 return ret;
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200921 }
922
Mark Brown946d92a2013-08-12 23:28:42 +0100923 dapm_kcontrol_add_path(w->kcontrols[i], path);
Charles Keepax561ed682015-05-01 12:37:26 +0100924
925 data = snd_kcontrol_chip(w->kcontrols[i]);
926 if (data->widget)
927 snd_soc_dapm_add_path(data->widget->dapm,
928 data->widget,
929 path->source,
930 NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200931 }
932 }
Stephen Warren85762e72013-03-29 15:40:10 -0600933
934 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200935}
936
937/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200938static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200939{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200940 struct snd_soc_dapm_context *dapm = w->dapm;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200941 enum snd_soc_dapm_direction dir;
Stephen Warren85762e72013-03-29 15:40:10 -0600942 struct snd_soc_dapm_path *path;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200943 const char *type;
Stephen Warrenaf468002011-04-28 17:38:01 -0600944 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200945
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200946 switch (w->id) {
947 case snd_soc_dapm_mux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200948 dir = SND_SOC_DAPM_DIR_OUT;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200949 type = "mux";
950 break;
951 case snd_soc_dapm_demux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200952 dir = SND_SOC_DAPM_DIR_IN;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200953 type = "demux";
954 break;
955 default:
956 return -EINVAL;
957 }
958
Stephen Warrenaf468002011-04-28 17:38:01 -0600959 if (w->num_kcontrols != 1) {
960 dev_err(dapm->dev,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200961 "ASoC: %s %s has incorrect number of controls\n", type,
Stephen Warrenaf468002011-04-28 17:38:01 -0600962 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200963 return -EINVAL;
964 }
965
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200966 if (list_empty(&w->edges[dir])) {
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200967 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
Stephen Warren85762e72013-03-29 15:40:10 -0600968 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600969 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200970
Jeeja KP19a2557b2015-10-20 22:30:06 +0530971 ret = dapm_create_or_share_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600972 if (ret < 0)
973 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600974
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200975 snd_soc_dapm_widget_for_each_path(w, dir, path) {
976 if (path->name)
977 dapm_kcontrol_add_path(w->kcontrols[0], path);
Lars-Peter Clausen98407ef2014-10-25 17:41:57 +0200978 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200979
Stephen Warrenaf468002011-04-28 17:38:01 -0600980 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200981}
982
983/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200984static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200985{
Jeeja KP19a2557b2015-10-20 22:30:06 +0530986 int i, ret;
987
988 for (i = 0; i < w->num_kcontrols; i++) {
989 ret = dapm_create_or_share_kcontrol(w, i);
990 if (ret < 0)
991 return ret;
992 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200993
Mark Browna6c65732010-03-03 17:45:21 +0000994 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200995}
996
Nikesh Oswalc6615082015-02-02 17:06:44 +0000997/* create new dapm dai link control */
998static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
999{
1000 int i, ret;
1001 struct snd_kcontrol *kcontrol;
1002 struct snd_soc_dapm_context *dapm = w->dapm;
1003 struct snd_card *card = dapm->card->snd_card;
1004
1005 /* create control for links with > 1 config */
1006 if (w->num_params <= 1)
1007 return 0;
1008
1009 /* add kcontrol */
1010 for (i = 0; i < w->num_kcontrols; i++) {
1011 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1012 w->name, NULL);
1013 ret = snd_ctl_add(card, kcontrol);
1014 if (ret < 0) {
1015 dev_err(dapm->dev,
1016 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1017 w->name, w->kcontrol_news[i].name, ret);
1018 return ret;
1019 }
1020 kcontrol->private_data = w;
1021 w->kcontrols[i] = kcontrol;
1022 }
1023
1024 return 0;
1025}
1026
Mark Brown99497882010-05-07 20:24:05 +01001027/* We implement power down on suspend by checking the power state of
1028 * the ALSA card - when we are suspending the ALSA state for the card
1029 * is set to D3.
1030 */
1031static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1032{
Mark Brown12ea2c72011-03-02 18:17:32 +00001033 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +01001034
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001035 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +01001036 case SNDRV_CTL_POWER_D3hot:
1037 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +01001038 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001039 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001040 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +01001041 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +01001042 default:
1043 return 1;
1044 }
1045}
1046
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001047static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1048 struct list_head *widgets)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001049{
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001050 struct snd_soc_dapm_widget *w;
1051 struct list_head *it;
1052 unsigned int size = 0;
1053 unsigned int i = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001054
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001055 list_for_each(it, widgets)
1056 size++;
1057
1058 *list = kzalloc(sizeof(**list) + size * sizeof(*w), GFP_KERNEL);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001059 if (*list == NULL)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001060 return -ENOMEM;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001061
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001062 list_for_each_entry(w, widgets, work_list)
1063 (*list)->widgets[i++] = w;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001064
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001065 (*list)->num_widgets = i;
1066
1067 return 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001068}
1069
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001070/*
1071 * Common implementation for is_connected_output_ep() and
1072 * is_connected_input_ep(). The function is inlined since the combined size of
1073 * the two specialized functions is only marginally larger then the size of the
1074 * generic function and at the same time the fast path of the specialized
1075 * functions is significantly smaller than the generic function.
1076 */
1077static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1078 struct list_head *list, enum snd_soc_dapm_direction dir,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001079 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1080 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1081 enum snd_soc_dapm_direction)),
1082 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1083 enum snd_soc_dapm_direction))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001084{
1085 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001086 struct snd_soc_dapm_path *path;
1087 int con = 0;
1088
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001089 if (widget->endpoints[dir] >= 0)
1090 return widget->endpoints[dir];
Mark Brown024dc072011-10-09 11:52:05 +01001091
Mark Brownde02d072011-09-20 21:43:24 +01001092 DAPM_UPDATE_STAT(widget, path_checks);
1093
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001094 /* do we need to add this widget to the list ? */
1095 if (list)
1096 list_add_tail(&widget->work_list, list);
1097
Jeeja KP09464972016-06-15 11:16:55 +05301098 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1099 widget->endpoints[dir] = 1;
1100 return widget->endpoints[dir];
1101 }
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001102
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001103 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1104 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1105 return widget->endpoints[dir];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001106 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001107
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001108 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
Mark Browne56235e2011-09-21 18:19:14 +01001109 DAPM_UPDATE_STAT(widget, neighbour_checks);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001110
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001111 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +01001112 continue;
1113
Mark Brown8af294b2013-02-22 17:48:15 +00001114 if (path->walking)
1115 return 1;
1116
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001117 trace_snd_soc_dapm_path(widget, dir, path);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001118
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001119 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +00001120 path->walking = 1;
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001121 con += fn(path->node[dir], list, custom_stop_condition);
Mark Brown8af294b2013-02-22 17:48:15 +00001122 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001123 }
1124 }
1125
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001126 widget->endpoints[dir] = con;
Mark Brown024dc072011-10-09 11:52:05 +01001127
Richard Purdie2b97eab2006-10-06 18:32:18 +02001128 return con;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001129}
1130
1131/*
1132 * Recursively check for a completed path to an active or physically connected
1133 * output widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001134 *
1135 * Optionally, can be supplied with a function acting as a stopping condition.
1136 * This function takes the dapm widget currently being examined and the walk
1137 * direction as an arguments, it should return true if the walk should be
1138 * stopped and false otherwise.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001139 */
1140static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001141 struct list_head *list,
1142 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1143 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001144{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001145 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001146 is_connected_output_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001147}
1148
1149/*
1150 * Recursively check for a completed path to an active or physically connected
1151 * input widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001152 *
1153 * Optionally, can be supplied with a function acting as a stopping condition.
1154 * This function takes the dapm widget currently being examined and the walk
1155 * direction as an arguments, it should return true if the walk should be
1156 * stopped and false otherwise.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001157 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001158static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001159 struct list_head *list,
1160 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1161 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001162{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001163 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001164 is_connected_input_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001165}
1166
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001167/**
1168 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1169 * @dai: the soc DAI.
1170 * @stream: stream direction.
1171 * @list: list of active widgets for this stream.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001172 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1173 * walk based on custom logic.
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001174 *
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001175 * Queries DAPM graph as to whether a valid audio stream path exists for
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001176 * the initial stream specified by name. This takes into account
1177 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1178 *
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001179 * Optionally, can be supplied with a function acting as a stopping condition.
1180 * This function takes the dapm widget currently being examined and the walk
1181 * direction as an arguments, it should return true if the walk should be
1182 * stopped and false otherwise.
1183 *
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001184 * Returns the number of valid paths or negative error.
1185 */
1186int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001187 struct snd_soc_dapm_widget_list **list,
1188 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1189 enum snd_soc_dapm_direction))
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001190{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01001191 struct snd_soc_card *card = dai->component->card;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001192 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001193 LIST_HEAD(widgets);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001194 int paths;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001195 int ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001196
1197 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001198
1199 /*
1200 * For is_connected_{output,input}_ep fully discover the graph we need
1201 * to reset the cached number of inputs and outputs.
1202 */
1203 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001204 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1205 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001206 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001207
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001208 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001209 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1210 custom_stop_condition);
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001211 else
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001212 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1213 custom_stop_condition);
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001214
1215 /* Drop starting point */
1216 list_del(widgets.next);
1217
1218 ret = dapm_widget_list_create(list, &widgets);
1219 if (ret)
Lars-Peter Clausen30abbe72015-08-11 21:37:59 +02001220 paths = ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001221
1222 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001223 mutex_unlock(&card->dapm_mutex);
1224
1225 return paths;
1226}
1227
Richard Purdie2b97eab2006-10-06 18:32:18 +02001228/*
Mark Brown62ea8742012-01-21 21:14:48 +00001229 * Handler for regulator supply widget.
1230 */
1231int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1232 struct snd_kcontrol *kcontrol, int event)
1233{
Mark Brownc05b84d2012-09-07 12:57:11 +08001234 int ret;
1235
Mark Browneb270e92013-10-09 13:52:52 +01001236 soc_dapm_async_complete(w->dapm);
1237
Mark Brownc05b84d2012-09-07 12:57:11 +08001238 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001239 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001240 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001241 if (ret != 0)
1242 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001243 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001244 w->name, ret);
1245 }
1246
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001247 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001248 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001249 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001250 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001251 if (ret != 0)
1252 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001253 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001254 w->name, ret);
1255 }
1256
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001257 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001258 }
Mark Brown62ea8742012-01-21 21:14:48 +00001259}
1260EXPORT_SYMBOL_GPL(dapm_regulator_event);
1261
Ola Liljad7e7eb92012-05-24 15:26:25 +02001262/*
1263 * Handler for clock supply widget.
1264 */
1265int dapm_clock_event(struct snd_soc_dapm_widget *w,
1266 struct snd_kcontrol *kcontrol, int event)
1267{
1268 if (!w->clk)
1269 return -EIO;
1270
Mark Browneb270e92013-10-09 13:52:52 +01001271 soc_dapm_async_complete(w->dapm);
1272
Mark Brownec029952012-06-04 08:16:20 +01001273#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001274 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001275 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001276 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001277 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001278 return 0;
1279 }
Mark Brownec029952012-06-04 08:16:20 +01001280#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001281 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001282}
1283EXPORT_SYMBOL_GPL(dapm_clock_event);
1284
Mark Brownd8050022011-09-28 18:28:23 +01001285static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1286{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001287 if (w->power_checked)
1288 return w->new_power;
1289
Mark Brownd8050022011-09-28 18:28:23 +01001290 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001291 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001292 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001293 w->new_power = w->power_check(w);
1294
1295 w->power_checked = true;
1296
1297 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001298}
1299
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001300/* Generic check to see if a widget should be powered. */
Mark Browncd0f2d42009-04-20 16:56:59 +01001301static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1302{
1303 int in, out;
1304
Mark Brownde02d072011-09-20 21:43:24 +01001305 DAPM_UPDATE_STAT(w, power_checks);
1306
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001307 in = is_connected_input_ep(w, NULL, NULL);
1308 out = is_connected_output_ep(w, NULL, NULL);
Mark Browncd0f2d42009-04-20 16:56:59 +01001309 return out != 0 && in != 0;
1310}
1311
Mark Brown246d0a12009-04-22 18:24:55 +01001312/* Check to see if a power supply is needed */
1313static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1314{
1315 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001316
Mark Brownde02d072011-09-20 21:43:24 +01001317 DAPM_UPDATE_STAT(w, power_checks);
1318
Mark Brown246d0a12009-04-22 18:24:55 +01001319 /* Check if one of our outputs is connected */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001320 snd_soc_dapm_widget_for_each_sink_path(w, path) {
Mark Browna8fdac82011-09-28 18:20:26 +01001321 DAPM_UPDATE_STAT(w, neighbour_checks);
1322
Mark Brownbf3a9e12011-06-13 16:42:29 +01001323 if (path->weak)
1324 continue;
1325
Mark Brown215edda2009-09-08 18:59:05 +01001326 if (path->connected &&
1327 !path->connected(path->source, path->sink))
1328 continue;
1329
Mark Brownf68d7e12011-10-04 22:57:50 +01001330 if (dapm_widget_power_check(path->sink))
1331 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001332 }
1333
Mark Brownf68d7e12011-10-04 22:57:50 +01001334 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001335}
1336
Mark Brown35c64bc2011-09-28 18:23:53 +01001337static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1338{
Charles Keepax20bb0182015-12-02 10:22:16 +00001339 return w->connected;
Mark Brown35c64bc2011-09-28 18:23:53 +01001340}
1341
Mark Brown38357ab2009-06-06 19:03:23 +01001342static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1343 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001344 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001345{
Mark Brown828a8422011-01-15 13:14:30 +00001346 int *sort;
1347
1348 if (power_up)
1349 sort = dapm_up_seq;
1350 else
1351 sort = dapm_down_seq;
1352
Mark Brown38357ab2009-06-06 19:03:23 +01001353 if (sort[a->id] != sort[b->id])
1354 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001355 if (a->subseq != b->subseq) {
1356 if (power_up)
1357 return a->subseq - b->subseq;
1358 else
1359 return b->subseq - a->subseq;
1360 }
Mark Brownb22ead22009-06-07 12:51:26 +01001361 if (a->reg != b->reg)
1362 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001363 if (a->dapm != b->dapm)
1364 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001365
Mark Brown38357ab2009-06-06 19:03:23 +01001366 return 0;
1367}
Mark Brown42aa3412009-03-01 19:21:10 +00001368
Mark Brown38357ab2009-06-06 19:03:23 +01001369/* Insert a widget in order into a DAPM power sequence. */
1370static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1371 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001372 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001373{
1374 struct snd_soc_dapm_widget *w;
1375
1376 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001377 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001378 list_add_tail(&new_widget->power_list, &w->power_list);
1379 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001380 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001381
Mark Brown38357ab2009-06-06 19:03:23 +01001382 list_add_tail(&new_widget->power_list, list);
1383}
Mark Brown42aa3412009-03-01 19:21:10 +00001384
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001385static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001386 struct snd_soc_dapm_widget *w, int event)
1387{
Mark Brown68f89ad2010-11-03 23:51:49 -04001388 const char *ev_name;
1389 int power, ret;
1390
1391 switch (event) {
1392 case SND_SOC_DAPM_PRE_PMU:
1393 ev_name = "PRE_PMU";
1394 power = 1;
1395 break;
1396 case SND_SOC_DAPM_POST_PMU:
1397 ev_name = "POST_PMU";
1398 power = 1;
1399 break;
1400 case SND_SOC_DAPM_PRE_PMD:
1401 ev_name = "PRE_PMD";
1402 power = 0;
1403 break;
1404 case SND_SOC_DAPM_POST_PMD:
1405 ev_name = "POST_PMD";
1406 power = 0;
1407 break;
Mark Brown80114122013-02-25 15:14:19 +00001408 case SND_SOC_DAPM_WILL_PMU:
1409 ev_name = "WILL_PMU";
1410 power = 1;
1411 break;
1412 case SND_SOC_DAPM_WILL_PMD:
1413 ev_name = "WILL_PMD";
1414 power = 0;
1415 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001416 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001417 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001418 return;
1419 }
1420
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001421 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001422 return;
1423
1424 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001425 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001426 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001427 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001428 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001429 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001430 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001431 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001432 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001433 ev_name, w->name, ret);
1434 }
1435}
1436
Mark Brownb22ead22009-06-07 12:51:26 +01001437/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001438static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001439 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001440{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001441 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001442 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001443 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001444 unsigned int value = 0;
1445 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001446
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001447 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1448 reg = w->reg;
1449 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001450
1451 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001452 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001453 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001454
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001455 mask |= w->mask << w->shift;
1456 if (w->power)
1457 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001458 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001459 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001460
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001461 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001462 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1463 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001464
Mark Brown68f89ad2010-11-03 23:51:49 -04001465 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001466 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1467 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001468 }
1469
Mark Brown81628102009-06-07 13:21:24 +01001470 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001471 /* Any widget will do, they should all be updating the
1472 * same register.
1473 */
Mark Brown29376bc2011-06-19 13:49:28 +01001474
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001475 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001476 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001477 value, mask, reg, card->pop_time);
1478 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001479 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001480 }
1481
1482 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001483 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1484 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001485 }
Mark Brown42aa3412009-03-01 19:21:10 +00001486}
1487
Mark Brownb22ead22009-06-07 12:51:26 +01001488/* Apply a DAPM power sequence.
1489 *
1490 * We walk over a pre-sorted list of widgets to apply power to. In
1491 * order to minimise the number of writes to the device required
1492 * multiple widgets will be updated in a single write where possible.
1493 * Currently anything that requires more than a single write is not
1494 * handled.
1495 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001496static void dapm_seq_run(struct snd_soc_card *card,
1497 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001498{
1499 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001500 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001501 LIST_HEAD(pending);
1502 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001503 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001504 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001505 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001506 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001507 int *sort;
1508
1509 if (power_up)
1510 sort = dapm_up_seq;
1511 else
1512 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001513
Mark Brownb22ead22009-06-07 12:51:26 +01001514 list_for_each_entry_safe(w, n, list, power_list) {
1515 ret = 0;
1516
1517 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001518 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001519 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Banajit Goswami853ed7c2015-04-10 16:50:43 -07001520 if (cur_dapm && !list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001521 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001522
Mark Brown474b62d2011-01-18 16:14:44 +00001523 if (cur_dapm && cur_dapm->seq_notifier) {
1524 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1525 if (sort[i] == cur_sort)
1526 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001527 i,
1528 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001529 }
1530
Mark Browneb270e92013-10-09 13:52:52 +01001531 if (cur_dapm && w->dapm != cur_dapm)
1532 soc_dapm_async_complete(cur_dapm);
1533
Mark Brownb22ead22009-06-07 12:51:26 +01001534 INIT_LIST_HEAD(&pending);
1535 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001536 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001537 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001538 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001539 }
1540
Mark Brown163cac02009-06-07 10:12:52 +01001541 switch (w->id) {
1542 case snd_soc_dapm_pre:
1543 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001544 list_for_each_entry_safe_continue(w, n, list,
1545 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001546
Mark Brownb22ead22009-06-07 12:51:26 +01001547 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001548 ret = w->event(w,
1549 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001550 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001551 ret = w->event(w,
1552 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001553 break;
1554
1555 case snd_soc_dapm_post:
1556 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001557 list_for_each_entry_safe_continue(w, n, list,
1558 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001559
Mark Brownb22ead22009-06-07 12:51:26 +01001560 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001561 ret = w->event(w,
1562 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001563 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001564 ret = w->event(w,
1565 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001566 break;
1567
Mark Brown163cac02009-06-07 10:12:52 +01001568 default:
Mark Brown81628102009-06-07 13:21:24 +01001569 /* Queue it up for application */
1570 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001571 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001572 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001573 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001574 list_move(&w->power_list, &pending);
1575 break;
Mark Brown163cac02009-06-07 10:12:52 +01001576 }
Mark Brownb22ead22009-06-07 12:51:26 +01001577
Banajit Goswamib2fac522013-10-10 18:17:43 -07001578 /*
1579 * Add this debug log to keep track of widgets being
1580 * powered-up and powered-down.
1581 */
1582 dev_dbg(w->dapm->dev, "dapm: powering %s widget %s\n",
1583 power_up ? "up" : "down", w->name);
1584
Mark Brownb22ead22009-06-07 12:51:26 +01001585 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001586 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001587 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001588 }
Mark Brownb22ead22009-06-07 12:51:26 +01001589
Banajit Goswami853ed7c2015-04-10 16:50:43 -07001590 if (cur_dapm && !list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001591 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001592
1593 if (cur_dapm && cur_dapm->seq_notifier) {
1594 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1595 if (sort[i] == cur_sort)
1596 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001597 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001598 }
Mark Browneb270e92013-10-09 13:52:52 +01001599
1600 list_for_each_entry(d, &card->dapm_list, list) {
1601 soc_dapm_async_complete(d);
1602 }
Mark Brown163cac02009-06-07 10:12:52 +01001603}
1604
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001605static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001606{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001607 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001608 struct snd_soc_dapm_widget_list *wlist;
1609 struct snd_soc_dapm_widget *w = NULL;
1610 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001611 int ret;
1612
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001613 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001614 return;
1615
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001616 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001617
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001618 for (wi = 0; wi < wlist->num_widgets; wi++) {
1619 w = wlist->widgets[wi];
1620
1621 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1622 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1623 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001624 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001625 w->name, ret);
1626 }
Mark Brown97404f22010-12-14 16:13:57 +00001627 }
1628
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001629 if (!w)
1630 return;
1631
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001632 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1633 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001634 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001635 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001636 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001637
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001638 for (wi = 0; wi < wlist->num_widgets; wi++) {
1639 w = wlist->widgets[wi];
1640
1641 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1642 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1643 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001644 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001645 w->name, ret);
1646 }
Mark Brown97404f22010-12-14 16:13:57 +00001647 }
1648}
1649
Mark Brown9d0624a2011-02-18 11:49:43 -08001650/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1651 * they're changing state.
1652 */
1653static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1654{
1655 struct snd_soc_dapm_context *d = data;
1656 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001657
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001658 /* If we're off and we're not supposed to go into STANDBY */
Mark Brown56fba412011-06-04 11:25:10 +01001659 if (d->bias_level == SND_SOC_BIAS_OFF &&
1660 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001661 if (d->dev)
1662 pm_runtime_get_sync(d->dev);
1663
Mark Brown9d0624a2011-02-18 11:49:43 -08001664 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1665 if (ret != 0)
1666 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001667 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001668 }
1669
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001670 /* Prepare for a transition to ON or away from ON */
1671 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1672 d->bias_level != SND_SOC_BIAS_ON) ||
1673 (d->target_bias_level != SND_SOC_BIAS_ON &&
1674 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001675 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1676 if (ret != 0)
1677 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001678 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001679 }
1680}
1681
1682/* Async callback run prior to DAPM sequences - brings to their final
1683 * state.
1684 */
1685static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1686{
1687 struct snd_soc_dapm_context *d = data;
1688 int ret;
1689
1690 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001691 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1692 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1693 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001694 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1695 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001696 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001697 ret);
1698 }
1699
1700 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001701 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1702 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001703 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1704 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001705 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1706 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001707
1708 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001709 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001710 }
1711
1712 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001713 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1714 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001715 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1716 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001717 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001718 ret);
1719 }
1720}
Mark Brown97404f22010-12-14 16:13:57 +00001721
Mark Brownfe4fda52011-10-03 22:36:57 +01001722static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1723 bool power, bool connect)
1724{
1725 /* If a connection is being made or broken then that update
1726 * will have marked the peer dirty, otherwise the widgets are
1727 * not connected and this update has no impact. */
1728 if (!connect)
1729 return;
1730
1731 /* If the peer is already in the state we're moving to then we
1732 * won't have an impact on it. */
1733 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001734 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001735}
1736
Mark Brown05623c42011-09-28 17:02:31 +01001737static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1738 struct list_head *up_list,
1739 struct list_head *down_list)
1740{
Mark Browndb432b42011-10-03 21:06:40 +01001741 struct snd_soc_dapm_path *path;
1742
Mark Brown05623c42011-09-28 17:02:31 +01001743 if (w->power == power)
1744 return;
1745
1746 trace_snd_soc_dapm_widget_power(w, power);
1747
Mark Browndb432b42011-10-03 21:06:40 +01001748 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001749 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001750 */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001751 snd_soc_dapm_widget_for_each_source_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001752 dapm_widget_set_peer_power(path->source, power, path->connect);
1753
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001754 /* Supplies can't affect their outputs, only their inputs */
1755 if (!w->is_supply) {
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001756 snd_soc_dapm_widget_for_each_sink_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001757 dapm_widget_set_peer_power(path->sink, power,
1758 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001759 }
1760
Mark Brown05623c42011-09-28 17:02:31 +01001761 if (power)
1762 dapm_seq_insert(w, up_list, true);
1763 else
1764 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001765}
1766
Mark Brown7c81beb2011-09-20 22:22:32 +01001767static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1768 struct list_head *up_list,
1769 struct list_head *down_list)
1770{
Mark Brown7c81beb2011-09-20 22:22:32 +01001771 int power;
1772
1773 switch (w->id) {
1774 case snd_soc_dapm_pre:
1775 dapm_seq_insert(w, down_list, false);
1776 break;
1777 case snd_soc_dapm_post:
1778 dapm_seq_insert(w, up_list, true);
1779 break;
1780
1781 default:
Mark Brownd8050022011-09-28 18:28:23 +01001782 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001783
Mark Brown05623c42011-09-28 17:02:31 +01001784 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001785 break;
1786 }
1787}
1788
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001789static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1790{
1791 if (dapm->idle_bias_off)
1792 return true;
1793
1794 switch (snd_power_get_state(dapm->card->snd_card)) {
1795 case SNDRV_CTL_POWER_D3hot:
1796 case SNDRV_CTL_POWER_D3cold:
1797 return dapm->suspend_bias_off;
1798 default:
1799 break;
1800 }
1801
1802 return false;
1803}
1804
Mark Brown42aa3412009-03-01 19:21:10 +00001805/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001806 * Scan each dapm widget for complete audio path.
1807 * A complete path is a route that has valid endpoints i.e.:-
1808 *
1809 * o DAC to output pin.
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001810 * o Input pin to ADC.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001811 * o Input pin to Output pin (bypass, sidetone)
1812 * o DAC to ADC (loopback).
1813 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001814static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001815{
1816 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001817 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001818 LIST_HEAD(up_list);
1819 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001820 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001821 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001822
Mark Brownf9fa2b12014-03-06 16:49:11 +08001823 lockdep_assert_held(&card->dapm_mutex);
1824
Mark Brown84e90932010-11-04 00:07:02 -04001825 trace_snd_soc_dapm_start(card);
Banajit Goswami853ed7c2015-04-10 16:50:43 -07001826 mutex_lock(&card->dapm_power_mutex);
Mark Brown84e90932010-11-04 00:07:02 -04001827
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);
Banajit Goswami853ed7c2015-04-10 16:50:43 -07001945 mutex_unlock(&card->dapm_power_mutex);
Mark Browncb507e72009-07-08 18:54:57 +01001946
Mark Brown84e90932010-11-04 00:07:02 -04001947 trace_snd_soc_dapm_done(card);
1948
Mark Brown42aa3412009-03-01 19:21:10 +00001949 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001950}
1951
Mark Brown79fb9382009-08-21 16:38:13 +01001952#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001953static ssize_t dapm_widget_power_read_file(struct file *file,
1954 char __user *user_buf,
1955 size_t count, loff_t *ppos)
1956{
1957 struct snd_soc_dapm_widget *w = file->private_data;
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02001958 struct snd_soc_card *card = w->dapm->card;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001959 enum snd_soc_dapm_direction dir, rdir;
Mark Brown79fb9382009-08-21 16:38:13 +01001960 char *buf;
1961 int in, out;
1962 ssize_t ret;
1963 struct snd_soc_dapm_path *p = NULL;
1964
1965 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1966 if (!buf)
1967 return -ENOMEM;
1968
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02001969 mutex_lock(&card->dapm_mutex);
1970
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001971 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
1972 if (w->is_supply) {
1973 in = 0;
1974 out = 0;
1975 } else {
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001976 in = is_connected_input_ep(w, NULL, NULL);
1977 out = is_connected_output_ep(w, NULL, NULL);
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001978 }
Mark Brown79fb9382009-08-21 16:38:13 +01001979
Mark Brownf13ebad2012-03-03 18:01:01 +00001980 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1981 w->name, w->power ? "On" : "Off",
1982 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001983
Mark Brownd033c362009-12-04 15:25:56 +00001984 if (w->reg >= 0)
1985 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001986 " - R%d(0x%x) mask 0x%x",
1987 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001988
1989 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1990
Mark Brown3eef08b2009-09-14 16:49:00 +01001991 if (w->sname)
1992 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1993 w->sname,
1994 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001995
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001996 snd_soc_dapm_for_each_direction(dir) {
1997 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1998 snd_soc_dapm_widget_for_each_path(w, dir, p) {
1999 if (p->connected && !p->connected(w, p->node[rdir]))
2000 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002001
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002002 if (!p->connect)
2003 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002004
Mark Brown79fb9382009-08-21 16:38:13 +01002005 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002006 " %s \"%s\" \"%s\"\n",
2007 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
Mark Brown79fb9382009-08-21 16:38:13 +01002008 p->name ? p->name : "static",
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002009 p->node[rdir]->name);
2010 }
Mark Brown79fb9382009-08-21 16:38:13 +01002011 }
2012
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002013 mutex_unlock(&card->dapm_mutex);
2014
Mark Brown79fb9382009-08-21 16:38:13 +01002015 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2016
2017 kfree(buf);
2018 return ret;
2019}
2020
2021static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002022 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002023 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002024 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002025};
2026
Mark Brownef49e4f2011-04-04 20:48:13 +09002027static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2028 size_t count, loff_t *ppos)
2029{
2030 struct snd_soc_dapm_context *dapm = file->private_data;
2031 char *level;
2032
2033 switch (dapm->bias_level) {
2034 case SND_SOC_BIAS_ON:
2035 level = "On\n";
2036 break;
2037 case SND_SOC_BIAS_PREPARE:
2038 level = "Prepare\n";
2039 break;
2040 case SND_SOC_BIAS_STANDBY:
2041 level = "Standby\n";
2042 break;
2043 case SND_SOC_BIAS_OFF:
2044 level = "Off\n";
2045 break;
2046 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002047 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002048 level = "Unknown\n";
2049 break;
2050 }
2051
2052 return simple_read_from_buffer(user_buf, count, ppos, level,
2053 strlen(level));
2054}
2055
2056static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002057 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002058 .read = dapm_bias_read_file,
2059 .llseek = default_llseek,
2060};
2061
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002062void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2063 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002064{
Mark Brown79fb9382009-08-21 16:38:13 +01002065 struct dentry *d;
2066
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02002067 if (!parent)
2068 return;
2069
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002070 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2071
2072 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002073 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002074 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002075 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002076 }
Mark Brown79fb9382009-08-21 16:38:13 +01002077
Mark Brownef49e4f2011-04-04 20:48:13 +09002078 d = debugfs_create_file("bias_level", 0444,
2079 dapm->debugfs_dapm, dapm,
2080 &dapm_bias_fops);
2081 if (!d)
2082 dev_warn(dapm->dev,
2083 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002084}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002085
2086static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2087{
2088 struct snd_soc_dapm_context *dapm = w->dapm;
2089 struct dentry *d;
2090
2091 if (!dapm->debugfs_dapm || !w->name)
2092 return;
2093
2094 d = debugfs_create_file(w->name, 0444,
2095 dapm->debugfs_dapm, w,
2096 &dapm_widget_power_fops);
2097 if (!d)
2098 dev_warn(w->dapm->dev,
2099 "ASoC: Failed to create %s debugfs file\n",
2100 w->name);
2101}
2102
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002103static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2104{
2105 debugfs_remove_recursive(dapm->debugfs_dapm);
2106}
2107
Mark Brown79fb9382009-08-21 16:38:13 +01002108#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002109void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2110 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002111{
2112}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002113
2114static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2115{
2116}
2117
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002118static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2119{
2120}
2121
Mark Brown79fb9382009-08-21 16:38:13 +01002122#endif
2123
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002124/*
2125 * soc_dapm_connect_path() - Connects or disconnects a path
2126 * @path: The path to update
2127 * @connect: The new connect state of the path. True if the path is connected,
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002128 * false if it is disconnected.
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002129 * @reason: The reason why the path changed (for debugging only)
2130 */
2131static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2132 bool connect, const char *reason)
2133{
2134 if (path->connect == connect)
2135 return;
2136
2137 path->connect = connect;
2138 dapm_mark_dirty(path->source, reason);
2139 dapm_mark_dirty(path->sink, reason);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002140 dapm_path_invalidate(path);
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002141}
2142
Richard Purdie2b97eab2006-10-06 18:32:18 +02002143/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002144static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002145 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002146{
2147 struct snd_soc_dapm_path *path;
2148 int found = 0;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002149 bool connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002150
Mark Brownf9fa2b12014-03-06 16:49:11 +08002151 lockdep_assert_held(&card->dapm_mutex);
2152
Richard Purdie2b97eab2006-10-06 18:32:18 +02002153 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002154 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002155 found = 1;
2156 /* we now need to match the string in the enum to the path */
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002157 if (!(strcmp(path->name, e->texts[mux])))
2158 connect = true;
2159 else
2160 connect = false;
2161
2162 soc_dapm_connect_path(path, connect, "mux update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002163 }
2164
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002165 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002166 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002167
Liam Girdwood618dae12012-04-25 12:12:51 +01002168 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002169}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002170
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002171int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002172 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2173 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002174{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002175 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002176 int ret;
2177
Liam Girdwood3cd04342012-03-09 12:02:08 +00002178 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002179 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002180 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002181 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002182 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002183 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002184 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002185 return ret;
2186}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002187EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002188
Milan plzik1b075e32008-01-10 14:39:46 +01002189/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002190static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002191 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002192{
2193 struct snd_soc_dapm_path *path;
2194 int found = 0;
2195
Mark Brownf9fa2b12014-03-06 16:49:11 +08002196 lockdep_assert_held(&card->dapm_mutex);
2197
Richard Purdie2b97eab2006-10-06 18:32:18 +02002198 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002199 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002200 found = 1;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002201 soc_dapm_connect_path(path, connect, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002202 }
2203
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002204 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002205 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002206
Liam Girdwood618dae12012-04-25 12:12:51 +01002207 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002208}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002209
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002210int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002211 struct snd_kcontrol *kcontrol, int connect,
2212 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002213{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002214 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002215 int ret;
2216
Liam Girdwood3cd04342012-03-09 12:02:08 +00002217 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002218 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002219 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002220 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002221 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002222 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002223 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002224 return ret;
2225}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002226EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002227
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002228static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2229 char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002230{
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002231 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002232 struct snd_soc_dapm_widget *w;
2233 int count = 0;
2234 char *state = "not set";
2235
Mark Brown47325072016-03-18 12:04:23 +00002236 /* card won't be set for the dummy component, as a spot fix
2237 * we're checking for that case specifically here but in future
2238 * we will ensure that the dummy component looks like others.
2239 */
2240 if (!cmpnt->card)
2241 return 0;
2242
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002243 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2244 if (w->dapm != dapm)
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002245 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002246
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002247 /* only display widgets that burn power */
Richard Purdie2b97eab2006-10-06 18:32:18 +02002248 switch (w->id) {
2249 case snd_soc_dapm_hp:
2250 case snd_soc_dapm_mic:
2251 case snd_soc_dapm_spk:
2252 case snd_soc_dapm_line:
2253 case snd_soc_dapm_micbias:
2254 case snd_soc_dapm_dac:
2255 case snd_soc_dapm_adc:
2256 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002257 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002258 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002259 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002260 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002261 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002262 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002263 if (w->name)
2264 count += sprintf(buf + count, "%s: %s\n",
2265 w->name, w->power ? "On":"Off");
2266 break;
2267 default:
2268 break;
2269 }
2270 }
2271
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002272 switch (snd_soc_dapm_get_bias_level(dapm)) {
Mark Brown0be98982008-05-19 12:31:28 +02002273 case SND_SOC_BIAS_ON:
2274 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002275 break;
Mark Brown0be98982008-05-19 12:31:28 +02002276 case SND_SOC_BIAS_PREPARE:
2277 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002278 break;
Mark Brown0be98982008-05-19 12:31:28 +02002279 case SND_SOC_BIAS_STANDBY:
2280 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002281 break;
Mark Brown0be98982008-05-19 12:31:28 +02002282 case SND_SOC_BIAS_OFF:
2283 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002284 break;
2285 }
2286 count += sprintf(buf + count, "PM State: %s\n", state);
2287
2288 return count;
2289}
2290
Benoit Cousson44ba2642014-07-08 23:19:36 +02002291/* show dapm widget status in sys fs */
2292static ssize_t dapm_widget_show(struct device *dev,
2293 struct device_attribute *attr, char *buf)
2294{
2295 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2296 int i, count = 0;
2297
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002298 mutex_lock(&rtd->card->dapm_mutex);
2299
Benoit Cousson44ba2642014-07-08 23:19:36 +02002300 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002301 struct snd_soc_component *cmpnt = rtd->codec_dais[i]->component;
2302
2303 count += dapm_widget_show_component(cmpnt, buf + count);
Benoit Cousson44ba2642014-07-08 23:19:36 +02002304 }
2305
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002306 mutex_unlock(&rtd->card->dapm_mutex);
2307
Benoit Cousson44ba2642014-07-08 23:19:36 +02002308 return count;
2309}
2310
Richard Purdie2b97eab2006-10-06 18:32:18 +02002311static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2312
Takashi Iwaid29697d2015-01-30 20:16:37 +01002313struct attribute *soc_dapm_dev_attrs[] = {
2314 &dev_attr_dapm_widget.attr,
2315 NULL
2316};
Richard Purdie2b97eab2006-10-06 18:32:18 +02002317
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002318static void dapm_free_path(struct snd_soc_dapm_path *path)
2319{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002320 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2321 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002322 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002323 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002324 kfree(path);
2325}
2326
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002327void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2328{
2329 struct snd_soc_dapm_path *p, *next_p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002330 enum snd_soc_dapm_direction dir;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002331
2332 list_del(&w->list);
2333 /*
2334 * remove source and sink paths associated to this widget.
2335 * While removing the path, remove reference to it from both
2336 * source and sink widgets so that path is removed only once.
2337 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002338 snd_soc_dapm_for_each_direction(dir) {
2339 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2340 dapm_free_path(p);
2341 }
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002342
2343 kfree(w->kcontrols);
Lars-Peter Clausen48068962015-07-21 18:11:08 +02002344 kfree_const(w->name);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002345 kfree(w);
2346}
2347
Jyri Sarhafd589a12015-11-10 18:12:42 +02002348void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2349{
2350 dapm->path_sink_cache.widget = NULL;
2351 dapm->path_source_cache.widget = NULL;
2352}
2353
Richard Purdie2b97eab2006-10-06 18:32:18 +02002354/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002355static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002356{
2357 struct snd_soc_dapm_widget *w, *next_w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002358
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002359 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2360 if (w->dapm != dapm)
2361 continue;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002362 snd_soc_dapm_free_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002363 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002364 snd_soc_dapm_reset_cache(dapm);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002365}
2366
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002367static struct snd_soc_dapm_widget *dapm_find_widget(
2368 struct snd_soc_dapm_context *dapm, const char *pin,
2369 bool search_other_contexts)
2370{
2371 struct snd_soc_dapm_widget *w;
2372 struct snd_soc_dapm_widget *fallback = NULL;
2373
2374 list_for_each_entry(w, &dapm->card->widgets, list) {
2375 if (!strcmp(w->name, pin)) {
2376 if (w->dapm == dapm)
2377 return w;
2378 else
2379 fallback = w;
2380 }
2381 }
2382
2383 if (search_other_contexts)
2384 return fallback;
2385
2386 return NULL;
2387}
2388
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002389static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002390 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002391{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002392 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002393
Mark Brownf9fa2b12014-03-06 16:49:11 +08002394 dapm_assert_locked(dapm);
2395
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002396 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002397 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002398 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002399 }
2400
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002401 if (w->connected != status) {
Mark Brown1a8b2d92012-02-16 11:50:07 -08002402 dapm_mark_dirty(w, "pin configuration");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002403 dapm_widget_invalidate_input_paths(w);
2404 dapm_widget_invalidate_output_paths(w);
2405 }
Mark Brown1a8b2d92012-02-16 11:50:07 -08002406
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002407 w->connected = status;
2408 if (status == 0)
2409 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002410
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002411 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002412}
2413
Richard Purdie2b97eab2006-10-06 18:32:18 +02002414/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002415 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2416 * @dapm: DAPM context
2417 *
2418 * Walks all dapm audio paths and powers widgets according to their
2419 * stream or path usage.
2420 *
2421 * Requires external locking.
2422 *
2423 * Returns 0 for success.
2424 */
2425int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2426{
2427 /*
2428 * Suppress early reports (eg, jacks syncing their state) to avoid
2429 * silly DAPM runs during card startup.
2430 */
2431 if (!dapm->card || !dapm->card->instantiated)
2432 return 0;
2433
2434 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2435}
2436EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2437
2438/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002439 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002440 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002441 *
2442 * Walks all dapm audio paths and powers widgets according to their
2443 * stream or path usage.
2444 *
2445 * Returns 0 for success.
2446 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002447int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002448{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002449 int ret;
2450
Liam Girdwood3cd04342012-03-09 12:02:08 +00002451 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002452 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002453 mutex_unlock(&dapm->card->dapm_mutex);
2454 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002455}
Liam Girdwooda5302182008-07-07 13:35:17 +01002456EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002457
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002458/*
2459 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2460 * @w: The widget for which to update the flags
2461 *
2462 * Some widgets have a dynamic category which depends on which neighbors they
2463 * are connected to. This function update the category for these widgets.
2464 *
2465 * This function must be called whenever a path is added or removed to a widget.
2466 */
2467static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2468{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002469 enum snd_soc_dapm_direction dir;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002470 struct snd_soc_dapm_path *p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002471 unsigned int ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002472
2473 switch (w->id) {
2474 case snd_soc_dapm_input:
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002475 /* On a fully routed card an input is never a source */
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002476 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002477 return;
2478 ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002479 snd_soc_dapm_widget_for_each_source_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002480 if (p->source->id == snd_soc_dapm_micbias ||
2481 p->source->id == snd_soc_dapm_mic ||
2482 p->source->id == snd_soc_dapm_line ||
2483 p->source->id == snd_soc_dapm_output) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002484 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002485 break;
2486 }
2487 }
2488 break;
2489 case snd_soc_dapm_output:
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002490 /* On a fully routed card a output is never a sink */
2491 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002492 return;
2493 ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002494 snd_soc_dapm_widget_for_each_sink_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002495 if (p->sink->id == snd_soc_dapm_spk ||
2496 p->sink->id == snd_soc_dapm_hp ||
2497 p->sink->id == snd_soc_dapm_line ||
2498 p->sink->id == snd_soc_dapm_input) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002499 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002500 break;
2501 }
2502 }
2503 break;
2504 case snd_soc_dapm_line:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002505 ep = 0;
2506 snd_soc_dapm_for_each_direction(dir) {
2507 if (!list_empty(&w->edges[dir]))
2508 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2509 }
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002510 break;
2511 default:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002512 return;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002513 }
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002514
2515 w->is_ep = ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002516}
2517
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002518static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2519 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2520 const char *control)
2521{
2522 bool dynamic_source = false;
2523 bool dynamic_sink = false;
2524
2525 if (!control)
2526 return 0;
2527
2528 switch (source->id) {
2529 case snd_soc_dapm_demux:
2530 dynamic_source = true;
2531 break;
2532 default:
2533 break;
2534 }
2535
2536 switch (sink->id) {
2537 case snd_soc_dapm_mux:
2538 case snd_soc_dapm_switch:
2539 case snd_soc_dapm_mixer:
2540 case snd_soc_dapm_mixer_named_ctl:
2541 dynamic_sink = true;
2542 break;
2543 default:
2544 break;
2545 }
2546
2547 if (dynamic_source && dynamic_sink) {
2548 dev_err(dapm->dev,
2549 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2550 source->name, control, sink->name);
2551 return -EINVAL;
2552 } else if (!dynamic_source && !dynamic_sink) {
2553 dev_err(dapm->dev,
2554 "Control not supported for path %s -> [%s] -> %s\n",
2555 source->name, control, sink->name);
2556 return -EINVAL;
2557 }
2558
2559 return 0;
2560}
2561
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002562static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2563 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2564 const char *control,
2565 int (*connected)(struct snd_soc_dapm_widget *source,
2566 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002567{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002568 struct snd_soc_dapm_widget *widgets[2];
2569 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002570 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002571 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002572
Lars-Peter Clausene409dfb2014-10-25 17:42:02 +02002573 if (wsink->is_supply && !wsource->is_supply) {
2574 dev_err(dapm->dev,
2575 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2576 wsource->name, wsink->name);
2577 return -EINVAL;
2578 }
2579
2580 if (connected && !wsource->is_supply) {
2581 dev_err(dapm->dev,
2582 "connected() callback only supported for supply widgets (%s -> %s)\n",
2583 wsource->name, wsink->name);
2584 return -EINVAL;
2585 }
2586
2587 if (wsource->is_supply && control) {
2588 dev_err(dapm->dev,
2589 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2590 wsource->name, control, wsink->name);
2591 return -EINVAL;
2592 }
2593
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002594 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2595 if (ret)
2596 return ret;
2597
Richard Purdie2b97eab2006-10-06 18:32:18 +02002598 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2599 if (!path)
2600 return -ENOMEM;
2601
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002602 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2603 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2604 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2605 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2606
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002607 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002608 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002609 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002610
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002611 if (wsource->is_supply || wsink->is_supply)
2612 path->is_supply = 1;
2613
Richard Purdie2b97eab2006-10-06 18:32:18 +02002614 /* connect static paths */
2615 if (control == NULL) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002616 path->connect = 1;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002617 } else {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002618 switch (wsource->id) {
2619 case snd_soc_dapm_demux:
2620 ret = dapm_connect_mux(dapm, path, control, wsource);
2621 if (ret)
2622 goto err;
2623 break;
2624 default:
2625 break;
2626 }
2627
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002628 switch (wsink->id) {
2629 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002630 ret = dapm_connect_mux(dapm, path, control, wsink);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002631 if (ret != 0)
2632 goto err;
2633 break;
2634 case snd_soc_dapm_switch:
2635 case snd_soc_dapm_mixer:
2636 case snd_soc_dapm_mixer_named_ctl:
2637 ret = dapm_connect_mixer(dapm, path, control);
2638 if (ret != 0)
2639 goto err;
2640 break;
2641 default:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002642 break;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002643 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002644 }
2645
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002646 list_add(&path->list, &dapm->card->paths);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002647 snd_soc_dapm_for_each_direction(dir)
2648 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002649
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002650 snd_soc_dapm_for_each_direction(dir) {
2651 dapm_update_widget_flags(widgets[dir]);
2652 dapm_mark_dirty(widgets[dir], "Route added");
2653 }
Mark Brownfabd0382012-07-05 17:20:06 +01002654
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002655 if (dapm->card->instantiated && path->connect)
2656 dapm_path_invalidate(path);
2657
Richard Purdie2b97eab2006-10-06 18:32:18 +02002658 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002659err:
2660 kfree(path);
2661 return ret;
2662}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002663
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002664static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002665 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002666{
2667 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2668 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2669 const char *sink;
2670 const char *source;
2671 char prefixed_sink[80];
2672 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002673 const char *prefix;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002674 int ret;
2675
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002676 prefix = soc_dapm_prefix(dapm);
2677 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002678 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002679 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002680 sink = prefixed_sink;
2681 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002682 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002683 source = prefixed_source;
2684 } else {
2685 sink = route->sink;
2686 source = route->source;
2687 }
2688
Charles Keepax45a110a2015-05-11 13:50:30 +01002689 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2690 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2691
2692 if (wsink && wsource)
2693 goto skip_search;
2694
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002695 /*
2696 * find src and dest widgets over all widgets but favor a widget from
2697 * current DAPM context
2698 */
2699 list_for_each_entry(w, &dapm->card->widgets, list) {
2700 if (!wsink && !(strcmp(w->name, sink))) {
2701 wtsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002702 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002703 wsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002704 if (wsource)
2705 break;
2706 }
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002707 continue;
2708 }
2709 if (!wsource && !(strcmp(w->name, source))) {
2710 wtsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002711 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002712 wsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002713 if (wsink)
2714 break;
2715 }
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002716 }
2717 }
2718 /* use widget from another DAPM context if not found from this */
2719 if (!wsink)
2720 wsink = wtsink;
2721 if (!wsource)
2722 wsource = wtsource;
2723
2724 if (wsource == NULL) {
2725 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2726 route->source);
2727 return -ENODEV;
2728 }
2729 if (wsink == NULL) {
2730 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2731 route->sink);
2732 return -ENODEV;
2733 }
2734
Charles Keepax45a110a2015-05-11 13:50:30 +01002735skip_search:
2736 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2737 dapm_wcache_update(&dapm->path_source_cache, wsource);
2738
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002739 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2740 route->connected);
2741 if (ret)
2742 goto err;
2743
2744 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002745err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002746 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002747 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002748 return ret;
2749}
Mark Brown105f1c22008-05-13 14:52:19 +02002750
Mark Brownefcc3c62012-07-05 17:24:19 +01002751static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2752 const struct snd_soc_dapm_route *route)
2753{
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002754 struct snd_soc_dapm_widget *wsource, *wsink;
Mark Brownefcc3c62012-07-05 17:24:19 +01002755 struct snd_soc_dapm_path *path, *p;
2756 const char *sink;
2757 const char *source;
2758 char prefixed_sink[80];
2759 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002760 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002761
2762 if (route->control) {
2763 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002764 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002765 return -EINVAL;
2766 }
2767
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002768 prefix = soc_dapm_prefix(dapm);
2769 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002770 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002771 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002772 sink = prefixed_sink;
2773 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002774 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002775 source = prefixed_source;
2776 } else {
2777 sink = route->sink;
2778 source = route->source;
2779 }
2780
2781 path = NULL;
2782 list_for_each_entry(p, &dapm->card->paths, list) {
2783 if (strcmp(p->source->name, source) != 0)
2784 continue;
2785 if (strcmp(p->sink->name, sink) != 0)
2786 continue;
2787 path = p;
2788 break;
2789 }
2790
2791 if (path) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002792 wsource = path->source;
2793 wsink = path->sink;
2794
2795 dapm_mark_dirty(wsource, "Route removed");
2796 dapm_mark_dirty(wsink, "Route removed");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002797 if (path->connect)
2798 dapm_path_invalidate(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002799
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002800 dapm_free_path(path);
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002801
2802 /* Update any path related flags */
2803 dapm_update_widget_flags(wsource);
2804 dapm_update_widget_flags(wsink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002805 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002806 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002807 source, sink);
2808 }
2809
2810 return 0;
2811}
2812
Mark Brown105f1c22008-05-13 14:52:19 +02002813/**
Mark Brown105f1c22008-05-13 14:52:19 +02002814 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002815 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002816 * @route: audio routes
2817 * @num: number of routes
2818 *
2819 * Connects 2 dapm widgets together via a named audio path. The sink is
2820 * the widget receiving the audio signal, whilst the source is the sender
2821 * of the audio signal.
2822 *
2823 * Returns 0 for success else error. On error all resources can be freed
2824 * with a call to snd_soc_card_free().
2825 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002826int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002827 const struct snd_soc_dapm_route *route, int num)
2828{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002829 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002830
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002831 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002832 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002833 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002834 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002835 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2836 route->source,
2837 route->control ? route->control : "direct",
2838 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002839 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002840 }
2841 route++;
2842 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002843 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002844
Dan Carpenter60884c22012-04-13 22:25:43 +03002845 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002846}
2847EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2848
Mark Brownefcc3c62012-07-05 17:24:19 +01002849/**
2850 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2851 * @dapm: DAPM context
2852 * @route: audio routes
2853 * @num: number of routes
2854 *
2855 * Removes routes from the DAPM context.
2856 */
2857int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2858 const struct snd_soc_dapm_route *route, int num)
2859{
Rajan Vajae066ea22016-02-11 11:23:35 +05302860 int i;
Mark Brownefcc3c62012-07-05 17:24:19 +01002861
2862 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2863 for (i = 0; i < num; i++) {
2864 snd_soc_dapm_del_route(dapm, route);
2865 route++;
2866 }
2867 mutex_unlock(&dapm->card->dapm_mutex);
2868
Rajan Vajae066ea22016-02-11 11:23:35 +05302869 return 0;
Mark Brownefcc3c62012-07-05 17:24:19 +01002870}
2871EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2872
Mark Brownbf3a9e12011-06-13 16:42:29 +01002873static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2874 const struct snd_soc_dapm_route *route)
2875{
2876 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2877 route->source,
2878 true);
2879 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2880 route->sink,
2881 true);
2882 struct snd_soc_dapm_path *path;
2883 int count = 0;
2884
2885 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002886 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002887 route->source);
2888 return -ENODEV;
2889 }
2890
2891 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002892 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002893 route->sink);
2894 return -ENODEV;
2895 }
2896
2897 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002898 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002899 route->source, route->sink);
2900
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002901 snd_soc_dapm_widget_for_each_sink_path(source, path) {
Mark Brownbf3a9e12011-06-13 16:42:29 +01002902 if (path->sink == sink) {
2903 path->weak = 1;
2904 count++;
2905 }
2906 }
2907
2908 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002909 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002910 route->source, route->sink);
2911 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002912 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002913 count, route->source, route->sink);
2914
2915 return 0;
2916}
2917
2918/**
2919 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2920 * @dapm: DAPM context
2921 * @route: audio routes
2922 * @num: number of routes
2923 *
2924 * Mark existing routes matching those specified in the passed array
2925 * as being weak, meaning that they are ignored for the purpose of
2926 * power decisions. The main intended use case is for sidetone paths
2927 * which couple audio between other independent paths if they are both
2928 * active in order to make the combination work better at the user
2929 * level but which aren't intended to be "used".
2930 *
2931 * Note that CODEC drivers should not use this as sidetone type paths
2932 * can frequently also be used as bypass paths.
2933 */
2934int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2935 const struct snd_soc_dapm_route *route, int num)
2936{
2937 int i, err;
2938 int ret = 0;
2939
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002940 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002941 for (i = 0; i < num; i++) {
2942 err = snd_soc_dapm_weak_route(dapm, route);
2943 if (err)
2944 ret = err;
2945 route++;
2946 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002947 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002948
2949 return ret;
2950}
2951EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2952
Mark Brown105f1c22008-05-13 14:52:19 +02002953/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002954 * snd_soc_dapm_new_widgets - add new dapm widgets
Jonathan Corbet628536e2015-08-25 01:14:48 -06002955 * @card: card to be checked for new dapm widgets
Richard Purdie2b97eab2006-10-06 18:32:18 +02002956 *
2957 * Checks the codec for any new dapm widgets and creates them if found.
2958 *
2959 * Returns 0 for success.
2960 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002961int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002962{
2963 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002964 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002965
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002966 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002967
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002968 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002969 {
2970 if (w->new)
2971 continue;
2972
Stephen Warrenfad59882011-04-28 17:37:59 -06002973 if (w->num_kcontrols) {
2974 w->kcontrols = kzalloc(w->num_kcontrols *
2975 sizeof(struct snd_kcontrol *),
2976 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002977 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002978 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002979 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002980 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002981 }
2982
Richard Purdie2b97eab2006-10-06 18:32:18 +02002983 switch(w->id) {
2984 case snd_soc_dapm_switch:
2985 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002986 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002987 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002988 break;
2989 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002990 case snd_soc_dapm_demux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002991 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002992 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002993 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002994 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002995 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002996 break;
Nikesh Oswalc6615082015-02-02 17:06:44 +00002997 case snd_soc_dapm_dai_link:
2998 dapm_new_dai_link(w);
2999 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01003000 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003001 break;
3002 }
Mark Brownb66a70d2011-02-09 18:04:11 +00003003
3004 /* Read the initial power state from the device */
3005 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003006 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08003007 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003008 val &= w->mask;
3009 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00003010 w->power = 1;
3011 }
3012
Richard Purdie2b97eab2006-10-06 18:32:18 +02003013 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003014
Mark Brown7508b122011-10-05 12:09:12 +01003015 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003016 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003017 }
3018
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003019 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3020 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003021 return 0;
3022}
3023EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3024
3025/**
3026 * snd_soc_dapm_get_volsw - dapm mixer get callback
3027 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003028 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003029 *
3030 * Callback to get the value of a dapm mixer control.
3031 *
3032 * Returns 0 for success.
3033 */
3034int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3035 struct snd_ctl_elem_value *ucontrol)
3036{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003037 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3038 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003039 struct soc_mixer_control *mc =
3040 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003041 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003042 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003043 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003044 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003045 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003046 unsigned int val;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003047 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003048
3049 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003050 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003051 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003052 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003053
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003054 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003055 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3056 ret = soc_dapm_read(dapm, reg, &val);
3057 val = (val >> shift) & mask;
3058 } else {
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003059 val = dapm_kcontrol_get_value(kcontrol);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003060 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003061 mutex_unlock(&card->dapm_mutex);
3062
Chen-Yu Tsai01ad5e72016-08-27 19:27:58 +08003063 if (ret)
3064 return ret;
3065
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003066 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003067 ucontrol->value.integer.value[0] = max - val;
3068 else
3069 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003070
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003071 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003072}
3073EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3074
3075/**
3076 * snd_soc_dapm_put_volsw - dapm mixer set callback
3077 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003078 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003079 *
3080 * Callback to set the value of a dapm mixer control.
3081 *
3082 * Returns 0 for success.
3083 */
3084int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3085 struct snd_ctl_elem_value *ucontrol)
3086{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003087 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3088 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003089 struct soc_mixer_control *mc =
3090 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003091 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003092 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003093 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003094 unsigned int mask = (1 << fls(max)) - 1;
3095 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07003096 unsigned int val;
Jarkko Nikula18626c72014-06-09 14:20:29 +03003097 int connect, change, reg_change = 0;
Mark Brown97404f22010-12-14 16:13:57 +00003098 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08003099 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003100
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003101 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003102 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003103 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003104 kcontrol->id.name);
3105
Richard Purdie2b97eab2006-10-06 18:32:18 +02003106 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02003107 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003108
3109 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003110 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003111
Liam Girdwood3cd04342012-03-09 12:02:08 +00003112 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003113
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003114 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown283375c2009-12-07 18:09:03 +00003115
Jarkko Nikula18626c72014-06-09 14:20:29 +03003116 if (reg != SND_SOC_NOPM) {
3117 mask = mask << shift;
3118 val = val << shift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02003119
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003120 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
Jarkko Nikula18626c72014-06-09 14:20:29 +03003121 }
3122
3123 if (change || reg_change) {
3124 if (reg_change) {
3125 update.kcontrol = kcontrol;
3126 update.reg = reg;
3127 update.mask = mask;
3128 update.val = val;
3129 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003130 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003131 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00003132
Nenghua Cao52765972013-12-13 20:13:49 +08003133 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00003134
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003135 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00003136 }
3137
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003138 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003139
3140 if (ret > 0)
3141 soc_dpcm_runtime_update(card);
3142
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003143 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003144}
3145EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3146
3147/**
3148 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3149 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003150 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003151 *
3152 * Callback to get the value of a dapm enumerated double mixer control.
3153 *
3154 * Returns 0 for success.
3155 */
3156int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3157 struct snd_ctl_elem_value *ucontrol)
3158{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003159 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Charles Keepax561ed682015-05-01 12:37:26 +01003160 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003161 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003162 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003163
Charles Keepax561ed682015-05-01 12:37:26 +01003164 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3165 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003166 int ret = soc_dapm_read(dapm, e->reg, &reg_val);
Charles Keepax964a0b82015-05-08 10:50:10 +01003167 if (ret) {
3168 mutex_unlock(&card->dapm_mutex);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003169 return ret;
Charles Keepax964a0b82015-05-08 10:50:10 +01003170 }
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003171 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003172 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003173 }
Charles Keepax561ed682015-05-01 12:37:26 +01003174 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003175
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003176 val = (reg_val >> e->shift_l) & e->mask;
3177 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3178 if (e->shift_l != e->shift_r) {
3179 val = (reg_val >> e->shift_r) & e->mask;
3180 val = snd_soc_enum_val_to_item(e, val);
3181 ucontrol->value.enumerated.item[1] = val;
3182 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003183
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003184 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003185}
3186EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3187
3188/**
3189 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3190 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003191 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003192 *
3193 * Callback to set the value of a dapm enumerated double mixer control.
3194 *
3195 * Returns 0 for success.
3196 */
3197int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3198 struct snd_ctl_elem_value *ucontrol)
3199{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003200 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3201 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003202 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003203 unsigned int *item = ucontrol->value.enumerated.item;
Charles Keepax561ed682015-05-01 12:37:26 +01003204 unsigned int val, change, reg_change = 0;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003205 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00003206 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08003207 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003208
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003209 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003210 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003211
3212 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003213 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003214 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003215 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003216 return -EINVAL;
Chen-Yu Tsai071133a2016-08-27 19:27:59 +08003217 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003218 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003219 }
3220
Liam Girdwood3cd04342012-03-09 12:02:08 +00003221 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003222
Charles Keepax561ed682015-05-01 12:37:26 +01003223 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00003224
Charles Keepax561ed682015-05-01 12:37:26 +01003225 if (e->reg != SND_SOC_NOPM)
3226 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3227
3228 if (change || reg_change) {
3229 if (reg_change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003230 update.kcontrol = kcontrol;
3231 update.reg = e->reg;
3232 update.mask = mask;
3233 update.val = val;
3234 card->update = &update;
3235 }
Charles Keepax561ed682015-05-01 12:37:26 +01003236 change |= reg_change;
Mark Brown3a655772009-10-05 17:23:30 +01003237
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003238 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003239
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003240 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003241 }
3242
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003243 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003244
3245 if (ret > 0)
3246 soc_dpcm_runtime_update(card);
3247
Mark Brown97404f22010-12-14 16:13:57 +00003248 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003249}
3250EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3251
3252/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003253 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3254 *
3255 * @kcontrol: mixer control
3256 * @uinfo: control element information
3257 *
3258 * Callback to provide information about a pin switch control.
3259 */
3260int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3261 struct snd_ctl_elem_info *uinfo)
3262{
3263 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3264 uinfo->count = 1;
3265 uinfo->value.integer.min = 0;
3266 uinfo->value.integer.max = 1;
3267
3268 return 0;
3269}
3270EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3271
3272/**
3273 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3274 *
3275 * @kcontrol: mixer control
3276 * @ucontrol: Value
3277 */
3278int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3279 struct snd_ctl_elem_value *ucontrol)
3280{
Mark Brown48a8c392012-02-14 17:11:15 -08003281 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003282 const char *pin = (const char *)kcontrol->private_value;
3283
Liam Girdwood3cd04342012-03-09 12:02:08 +00003284 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003285
3286 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003287 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003288
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003289 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003290
3291 return 0;
3292}
3293EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3294
3295/**
3296 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3297 *
3298 * @kcontrol: mixer control
3299 * @ucontrol: Value
3300 */
3301int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3302 struct snd_ctl_elem_value *ucontrol)
3303{
Mark Brown48a8c392012-02-14 17:11:15 -08003304 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003305 const char *pin = (const char *)kcontrol->private_value;
3306
Mark Brown8b37dbd2009-02-28 21:14:20 +00003307 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003308 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003309 else
Mark Brown48a8c392012-02-14 17:11:15 -08003310 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003311
Mark Brown48a8c392012-02-14 17:11:15 -08003312 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003313 return 0;
3314}
3315EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3316
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +01003317struct snd_soc_dapm_widget *
Mark Brown5ba06fc2012-02-16 11:07:13 -08003318snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003319 const struct snd_soc_dapm_widget *widget)
3320{
3321 struct snd_soc_dapm_widget *w;
3322
3323 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3324 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3325 if (!w)
3326 dev_err(dapm->dev,
3327 "ASoC: Failed to create DAPM control %s\n",
3328 widget->name);
3329
3330 mutex_unlock(&dapm->card->dapm_mutex);
3331 return w;
3332}
Subhransu S. Prustya5d56392016-06-27 09:18:03 +05303333EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003334
3335struct snd_soc_dapm_widget *
3336snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003337 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003338{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003339 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003340 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003341 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00003342 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003343
3344 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003345 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003346
Mark Brown62ea8742012-01-21 21:14:48 +00003347 switch (w->id) {
3348 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003349 w->regulator = devm_regulator_get(dapm->dev, w->name);
3350 if (IS_ERR(w->regulator)) {
3351 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003352 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003353 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003354 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003355 }
Mark Brown8784c772013-01-10 19:33:47 +00003356
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003357 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003358 ret = regulator_allow_bypass(w->regulator, true);
3359 if (ret != 0)
3360 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003361 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003362 w->name, ret);
3363 }
Mark Brown62ea8742012-01-21 21:14:48 +00003364 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003365 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003366#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003367 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003368 if (IS_ERR(w->clk)) {
3369 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003370 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003371 w->name, ret);
3372 return NULL;
3373 }
Mark Brownec029952012-06-04 08:16:20 +01003374#else
3375 return NULL;
3376#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003377 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003378 default:
3379 break;
3380 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003381
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003382 prefix = soc_dapm_prefix(dapm);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003383 if (prefix)
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003384 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003385 else
Lars-Peter Clausen48068962015-07-21 18:11:08 +02003386 w->name = kstrdup_const(widget->name, GFP_KERNEL);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003387 if (w->name == NULL) {
3388 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003389 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003390 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003391
Mark Brown7ca3a182011-10-08 14:04:50 +01003392 switch (w->id) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003393 case snd_soc_dapm_mic:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003394 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003395 w->power_check = dapm_generic_check_power;
3396 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003397 case snd_soc_dapm_input:
3398 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003399 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003400 w->power_check = dapm_generic_check_power;
3401 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003402 case snd_soc_dapm_spk:
3403 case snd_soc_dapm_hp:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003404 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003405 w->power_check = dapm_generic_check_power;
3406 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003407 case snd_soc_dapm_output:
3408 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003409 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003410 w->power_check = dapm_generic_check_power;
3411 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003412 case snd_soc_dapm_vmid:
3413 case snd_soc_dapm_siggen:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003414 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003415 w->power_check = dapm_always_on_check_power;
3416 break;
Vinod Koul56b44372015-11-23 21:22:30 +05303417 case snd_soc_dapm_sink:
3418 w->is_ep = SND_SOC_DAPM_EP_SINK;
3419 w->power_check = dapm_always_on_check_power;
3420 break;
3421
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003422 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003423 case snd_soc_dapm_demux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003424 case snd_soc_dapm_switch:
3425 case snd_soc_dapm_mixer:
3426 case snd_soc_dapm_mixer_named_ctl:
Mark Brown63c69a62013-07-18 22:03:01 +01003427 case snd_soc_dapm_adc:
3428 case snd_soc_dapm_aif_out:
3429 case snd_soc_dapm_dac:
3430 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003431 case snd_soc_dapm_pga:
3432 case snd_soc_dapm_out_drv:
Mark Brown7ca3a182011-10-08 14:04:50 +01003433 case snd_soc_dapm_micbias:
Mark Brown7ca3a182011-10-08 14:04:50 +01003434 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003435 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02003436 case snd_soc_dapm_dai_out:
3437 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003438 w->power_check = dapm_generic_check_power;
3439 break;
3440 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003441 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003442 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003443 case snd_soc_dapm_kcontrol:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003444 w->is_supply = 1;
Mark Brown7ca3a182011-10-08 14:04:50 +01003445 w->power_check = dapm_supply_check_power;
3446 break;
3447 default:
3448 w->power_check = dapm_always_on_check_power;
3449 break;
3450 }
3451
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003452 w->dapm = dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003453 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003454 INIT_LIST_HEAD(&w->dirty);
Lars-Peter Clausen92fa1242015-05-01 18:02:42 +02003455 list_add_tail(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003456
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003457 snd_soc_dapm_for_each_direction(dir) {
3458 INIT_LIST_HEAD(&w->edges[dir]);
3459 w->endpoints[dir] = -1;
3460 }
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003461
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02003462 /* machine layer sets up unconnected pins and insertions */
Richard Purdie2b97eab2006-10-06 18:32:18 +02003463 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003464 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003465}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003466
3467/**
Mark Brown4ba13272008-05-13 14:51:19 +02003468 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003469 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003470 * @widget: widget array
3471 * @num: number of widgets
3472 *
3473 * Creates new DAPM controls based upon the templates.
3474 *
3475 * Returns 0 for success else error.
3476 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003477int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003478 const struct snd_soc_dapm_widget *widget,
3479 int num)
3480{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003481 struct snd_soc_dapm_widget *w;
3482 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003483 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003484
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003485 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003486 for (i = 0; i < num; i++) {
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003487 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003488 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003489 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003490 "ASoC: Failed to create DAPM control %s\n",
3491 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003492 ret = -ENOMEM;
3493 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003494 }
Mark Brown4ba13272008-05-13 14:51:19 +02003495 widget++;
3496 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003497 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003498 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003499}
3500EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3501
Mark Brownc74184e2012-04-04 22:12:09 +01003502static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3503 struct snd_kcontrol *kcontrol, int event)
3504{
3505 struct snd_soc_dapm_path *source_p, *sink_p;
3506 struct snd_soc_dai *source, *sink;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003507 const struct snd_soc_pcm_stream *config = w->params + w->params_select;
Mark Brownc74184e2012-04-04 22:12:09 +01003508 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003509 struct snd_pcm_hw_params *params = NULL;
Nicolin Chen8053f212016-07-26 14:55:51 -07003510 struct snd_pcm_runtime *runtime = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003511 u64 fmt;
3512 int ret;
3513
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003514 if (WARN_ON(!config) ||
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003515 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3516 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003517 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003518
3519 /* We only support a single source and sink, pick the first */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003520 source_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_OUT],
3521 struct snd_soc_dapm_path,
3522 list_node[SND_SOC_DAPM_DIR_OUT]);
3523 sink_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_IN],
3524 struct snd_soc_dapm_path,
3525 list_node[SND_SOC_DAPM_DIR_IN]);
Mark Brownc74184e2012-04-04 22:12:09 +01003526
3527 source = source_p->source->priv;
3528 sink = sink_p->sink->priv;
3529
3530 /* Be a little careful as we don't want to overflow the mask array */
3531 if (config->formats) {
3532 fmt = ffs(config->formats) - 1;
3533 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003534 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003535 config->formats);
3536 fmt = 0;
3537 }
3538
3539 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003540 params = kzalloc(sizeof(*params), GFP_KERNEL);
3541 if (!params) {
3542 ret = -ENOMEM;
3543 goto out;
3544 }
3545 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003546
Mark Brown9747cec2012-04-26 19:12:21 +01003547 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003548 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003549 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003550 config->rate_max;
3551
Mark Brown9747cec2012-04-26 19:12:21 +01003552 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003553 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003554 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003555 = config->channels_max;
3556
3557 memset(&substream, 0, sizeof(substream));
3558
Nicolin Chen8053f212016-07-26 14:55:51 -07003559 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3560 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3561 if (!runtime) {
3562 ret = -ENOMEM;
3563 goto out;
3564 }
3565 substream.runtime = runtime;
3566
Mark Brownc74184e2012-04-04 22:12:09 +01003567 switch (event) {
3568 case SND_SOC_DAPM_PRE_PMU:
Benoit Cousson93e69582014-07-08 23:19:38 +02003569 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303570 if (source->driver->ops && source->driver->ops->startup) {
3571 ret = source->driver->ops->startup(&substream, source);
3572 if (ret < 0) {
3573 dev_err(source->dev,
3574 "ASoC: startup() failed: %d\n", ret);
3575 goto out;
3576 }
3577 source->active++;
3578 }
Benoit Cousson93e69582014-07-08 23:19:38 +02003579 ret = soc_dai_hw_params(&substream, params, source);
3580 if (ret < 0)
3581 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003582
Benoit Cousson93e69582014-07-08 23:19:38 +02003583 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303584 if (sink->driver->ops && sink->driver->ops->startup) {
3585 ret = sink->driver->ops->startup(&substream, sink);
3586 if (ret < 0) {
3587 dev_err(sink->dev,
3588 "ASoC: startup() failed: %d\n", ret);
3589 goto out;
3590 }
3591 sink->active++;
3592 }
Benoit Cousson93e69582014-07-08 23:19:38 +02003593 ret = soc_dai_hw_params(&substream, params, sink);
3594 if (ret < 0)
3595 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003596 break;
3597
3598 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003599 ret = snd_soc_dai_digital_mute(sink, 0,
3600 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003601 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003602 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003603 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003604 break;
3605
3606 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003607 ret = snd_soc_dai_digital_mute(sink, 1,
3608 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003609 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003610 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003611 ret = 0;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303612
3613 source->active--;
3614 if (source->driver->ops && source->driver->ops->shutdown) {
3615 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3616 source->driver->ops->shutdown(&substream, source);
3617 }
3618
3619 sink->active--;
3620 if (sink->driver->ops && sink->driver->ops->shutdown) {
3621 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3622 sink->driver->ops->shutdown(&substream, sink);
3623 }
Mark Brownc74184e2012-04-04 22:12:09 +01003624 break;
3625
3626 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003627 WARN(1, "Unknown event %d\n", event);
Sudip Mukherjee75881df2015-09-10 18:01:44 +05303628 ret = -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003629 }
3630
Mark Brown9747cec2012-04-26 19:12:21 +01003631out:
Nicolin Chen8053f212016-07-26 14:55:51 -07003632 kfree(runtime);
Mark Brown9747cec2012-04-26 19:12:21 +01003633 kfree(params);
3634 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003635}
3636
Nikesh Oswalc6615082015-02-02 17:06:44 +00003637static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3638 struct snd_ctl_elem_value *ucontrol)
3639{
3640 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3641
Takashi Iwai741338f2016-02-29 17:20:48 +01003642 ucontrol->value.enumerated.item[0] = w->params_select;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003643
3644 return 0;
3645}
3646
3647static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3648 struct snd_ctl_elem_value *ucontrol)
3649{
3650 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3651
3652 /* Can't change the config when widget is already powered */
3653 if (w->power)
3654 return -EBUSY;
3655
Takashi Iwai741338f2016-02-29 17:20:48 +01003656 if (ucontrol->value.enumerated.item[0] == w->params_select)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003657 return 0;
3658
Takashi Iwai741338f2016-02-29 17:20:48 +01003659 if (ucontrol->value.enumerated.item[0] >= w->num_params)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003660 return -EINVAL;
3661
Takashi Iwai741338f2016-02-29 17:20:48 +01003662 w->params_select = ucontrol->value.enumerated.item[0];
Nikesh Oswalc6615082015-02-02 17:06:44 +00003663
3664 return 0;
3665}
3666
Mark Brownc74184e2012-04-04 22:12:09 +01003667int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3668 const struct snd_soc_pcm_stream *params,
Nikesh Oswalc6615082015-02-02 17:06:44 +00003669 unsigned int num_params,
Mark Brownc74184e2012-04-04 22:12:09 +01003670 struct snd_soc_dapm_widget *source,
3671 struct snd_soc_dapm_widget *sink)
3672{
Mark Brownc74184e2012-04-04 22:12:09 +01003673 struct snd_soc_dapm_widget template;
3674 struct snd_soc_dapm_widget *w;
Mark Brownc74184e2012-04-04 22:12:09 +01003675 char *link_name;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003676 int ret, count;
3677 unsigned long private_value;
3678 const char **w_param_text;
3679 struct soc_enum w_param_enum[] = {
3680 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3681 };
3682 struct snd_kcontrol_new kcontrol_dai_link[] = {
3683 SOC_ENUM_EXT(NULL, w_param_enum[0],
3684 snd_soc_dapm_dai_link_get,
3685 snd_soc_dapm_dai_link_put),
3686 };
3687 const struct snd_soc_pcm_stream *config = params;
Mark Brownc74184e2012-04-04 22:12:09 +01003688
Nikesh Oswalc6615082015-02-02 17:06:44 +00003689 w_param_text = devm_kcalloc(card->dev, num_params,
3690 sizeof(char *), GFP_KERNEL);
3691 if (!w_param_text)
Mark Brownc74184e2012-04-04 22:12:09 +01003692 return -ENOMEM;
Mark Brownc74184e2012-04-04 22:12:09 +01003693
Charles Keepax46172b62015-03-25 11:22:35 +00003694 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3695 source->name, sink->name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003696 if (!link_name) {
3697 ret = -ENOMEM;
3698 goto outfree_w_param;
3699 }
Mark Brownc74184e2012-04-04 22:12:09 +01003700
Nikesh Oswalc6615082015-02-02 17:06:44 +00003701 for (count = 0 ; count < num_params; count++) {
3702 if (!config->stream_name) {
3703 dev_warn(card->dapm.dev,
3704 "ASoC: anonymous config %d for dai link %s\n",
3705 count, link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003706 w_param_text[count] =
Charles Keepax46172b62015-03-25 11:22:35 +00003707 devm_kasprintf(card->dev, GFP_KERNEL,
3708 "Anonymous Configuration %d",
3709 count);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003710 if (!w_param_text[count]) {
3711 ret = -ENOMEM;
3712 goto outfree_link_name;
3713 }
Nikesh Oswalc6615082015-02-02 17:06:44 +00003714 } else {
3715 w_param_text[count] = devm_kmemdup(card->dev,
3716 config->stream_name,
3717 strlen(config->stream_name) + 1,
3718 GFP_KERNEL);
3719 if (!w_param_text[count]) {
3720 ret = -ENOMEM;
3721 goto outfree_link_name;
3722 }
3723 }
3724 config++;
3725 }
3726 w_param_enum[0].items = num_params;
3727 w_param_enum[0].texts = w_param_text;
Mark Brownc74184e2012-04-04 22:12:09 +01003728
3729 memset(&template, 0, sizeof(template));
3730 template.reg = SND_SOC_NOPM;
3731 template.id = snd_soc_dapm_dai_link;
3732 template.name = link_name;
3733 template.event = snd_soc_dai_link_event;
3734 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3735 SND_SOC_DAPM_PRE_PMD;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003736 template.num_kcontrols = 1;
3737 /* duplicate w_param_enum on heap so that memory persists */
3738 private_value =
3739 (unsigned long) devm_kmemdup(card->dev,
3740 (void *)(kcontrol_dai_link[0].private_value),
3741 sizeof(struct soc_enum), GFP_KERNEL);
3742 if (!private_value) {
3743 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3744 link_name);
3745 ret = -ENOMEM;
3746 goto outfree_link_name;
3747 }
3748 kcontrol_dai_link[0].private_value = private_value;
3749 /* duplicate kcontrol_dai_link on heap so that memory persists */
3750 template.kcontrol_news =
3751 devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3752 sizeof(struct snd_kcontrol_new),
3753 GFP_KERNEL);
3754 if (!template.kcontrol_news) {
3755 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3756 link_name);
3757 ret = -ENOMEM;
3758 goto outfree_private_value;
3759 }
Mark Brownc74184e2012-04-04 22:12:09 +01003760
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003761 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003762
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003763 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
Mark Brownc74184e2012-04-04 22:12:09 +01003764 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003765 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003766 link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003767 ret = -ENOMEM;
3768 goto outfree_kcontrol_news;
Mark Brownc74184e2012-04-04 22:12:09 +01003769 }
3770
3771 w->params = params;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003772 w->num_params = num_params;
Mark Brownc74184e2012-04-04 22:12:09 +01003773
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003774 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3775 if (ret)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003776 goto outfree_w;
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003777 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003778
3779outfree_w:
3780 devm_kfree(card->dev, w);
3781outfree_kcontrol_news:
3782 devm_kfree(card->dev, (void *)template.kcontrol_news);
3783outfree_private_value:
3784 devm_kfree(card->dev, (void *)private_value);
3785outfree_link_name:
3786 devm_kfree(card->dev, link_name);
3787outfree_w_param:
3788 for (count = 0 ; count < num_params; count++)
3789 devm_kfree(card->dev, (void *)w_param_text[count]);
3790 devm_kfree(card->dev, w_param_text);
3791
3792 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003793}
3794
Mark Brown888df392012-02-16 19:37:51 -08003795int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3796 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003797{
Mark Brown888df392012-02-16 19:37:51 -08003798 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003799 struct snd_soc_dapm_widget *w;
3800
Mark Brown888df392012-02-16 19:37:51 -08003801 WARN_ON(dapm->dev != dai->dev);
3802
3803 memset(&template, 0, sizeof(template));
3804 template.reg = SND_SOC_NOPM;
3805
3806 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003807 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003808 template.name = dai->driver->playback.stream_name;
3809 template.sname = dai->driver->playback.stream_name;
3810
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003811 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003812 template.name);
3813
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003814 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Mark Brown888df392012-02-16 19:37:51 -08003815 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003816 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003817 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003818 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003819 }
3820
3821 w->priv = dai;
3822 dai->playback_widget = w;
3823 }
3824
3825 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003826 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003827 template.name = dai->driver->capture.stream_name;
3828 template.sname = dai->driver->capture.stream_name;
3829
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003830 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003831 template.name);
3832
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003833 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Mark Brown888df392012-02-16 19:37:51 -08003834 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003835 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003836 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003837 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003838 }
3839
3840 w->priv = dai;
3841 dai->capture_widget = w;
3842 }
3843
3844 return 0;
3845}
3846
3847int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3848{
3849 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003850 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08003851 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003852
3853 /* For each DAI widget... */
3854 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003855 switch (dai_w->id) {
3856 case snd_soc_dapm_dai_in:
3857 case snd_soc_dapm_dai_out:
3858 break;
3859 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003860 continue;
Mark Brown46162742013-06-05 19:36:11 +01003861 }
Mark Brown888df392012-02-16 19:37:51 -08003862
3863 dai = dai_w->priv;
3864
3865 /* ...find all widgets with the same stream and link them */
3866 list_for_each_entry(w, &card->widgets, list) {
3867 if (w->dapm != dai_w->dapm)
3868 continue;
3869
Mark Brown46162742013-06-05 19:36:11 +01003870 switch (w->id) {
3871 case snd_soc_dapm_dai_in:
3872 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003873 continue;
Mark Brown46162742013-06-05 19:36:11 +01003874 default:
3875 break;
3876 }
Mark Brown888df392012-02-16 19:37:51 -08003877
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003878 if (!w->sname || !strstr(w->sname, dai_w->sname))
Mark Brown888df392012-02-16 19:37:51 -08003879 continue;
3880
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003881 if (dai_w->id == snd_soc_dapm_dai_in) {
3882 src = dai_w;
3883 sink = w;
3884 } else {
3885 src = w;
3886 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08003887 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003888 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3889 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003890 }
3891 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003892
Mark Brown888df392012-02-16 19:37:51 -08003893 return 0;
3894}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003895
Benoit Cousson44ba2642014-07-08 23:19:36 +02003896static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3897 struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003898{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003899 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003900 struct snd_soc_dapm_widget *sink, *source;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003901 int i;
3902
Benoit Cousson44ba2642014-07-08 23:19:36 +02003903 for (i = 0; i < rtd->num_codecs; i++) {
3904 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003905
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003906 /* connect BE DAI playback if widgets are valid */
3907 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003908 source = cpu_dai->playback_widget;
3909 sink = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003910 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003911 cpu_dai->component->name, source->name,
3912 codec_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003913
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003914 snd_soc_dapm_add_path(&card->dapm, source, sink,
3915 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003916 }
3917
3918 /* connect BE DAI capture if widgets are valid */
3919 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003920 source = codec_dai->capture_widget;
3921 sink = cpu_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003922 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003923 codec_dai->component->name, source->name,
3924 cpu_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003925
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003926 snd_soc_dapm_add_path(&card->dapm, source, sink,
3927 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003928 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003929 }
3930}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003931
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003932static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3933 int event)
3934{
3935 struct snd_soc_dapm_widget *w;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003936 unsigned int ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003937
3938 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3939 w = dai->playback_widget;
3940 else
3941 w = dai->capture_widget;
3942
3943 if (w) {
3944 dapm_mark_dirty(w, "stream event");
3945
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003946 if (w->id == snd_soc_dapm_dai_in) {
3947 ep = SND_SOC_DAPM_EP_SOURCE;
3948 dapm_widget_invalidate_input_paths(w);
3949 } else {
3950 ep = SND_SOC_DAPM_EP_SINK;
3951 dapm_widget_invalidate_output_paths(w);
3952 }
3953
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003954 switch (event) {
3955 case SND_SOC_DAPM_STREAM_START:
3956 w->active = 1;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003957 w->is_ep = ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003958 break;
3959 case SND_SOC_DAPM_STREAM_STOP:
3960 w->active = 0;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003961 w->is_ep = 0;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003962 break;
3963 case SND_SOC_DAPM_STREAM_SUSPEND:
3964 case SND_SOC_DAPM_STREAM_RESUME:
3965 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3966 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3967 break;
3968 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003969 }
3970}
3971
Benoit Cousson44ba2642014-07-08 23:19:36 +02003972void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3973{
Mengdong Lin1a497982015-11-18 02:34:11 -05003974 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson44ba2642014-07-08 23:19:36 +02003975
3976 /* for each BE DAI link... */
Mengdong Lin1a497982015-11-18 02:34:11 -05003977 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson44ba2642014-07-08 23:19:36 +02003978 /*
3979 * dynamic FE links have no fixed DAI mapping.
3980 * CODEC<->CODEC links have no direct connection.
3981 */
3982 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3983 continue;
3984
3985 dapm_connect_dai_link_widgets(card, rtd);
3986 }
3987}
3988
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003989static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3990 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003991{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003992 int i;
3993
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003994 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
Benoit Cousson44ba2642014-07-08 23:19:36 +02003995 for (i = 0; i < rtd->num_codecs; i++)
3996 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003997
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003998 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003999}
4000
4001/**
4002 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4003 * @rtd: PCM runtime data
4004 * @stream: stream name
4005 * @event: stream event
4006 *
4007 * Sends a stream event to the dapm core. The core then makes any
4008 * necessary widget power changes.
4009 *
4010 * Returns 0 for success else error.
4011 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004012void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4013 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004014{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004015 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004016
Liam Girdwood3cd04342012-03-09 12:02:08 +00004017 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004018 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00004019 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004020}
Richard Purdie2b97eab2006-10-06 18:32:18 +02004021
4022/**
Charles Keepax11391102014-02-18 15:22:14 +00004023 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4024 * @dapm: DAPM context
4025 * @pin: pin name
4026 *
4027 * Enables input/output pin and its parents or children widgets iff there is
4028 * a valid audio route and active audio stream.
4029 *
4030 * Requires external locking.
4031 *
4032 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4033 * do any widget power switching.
4034 */
4035int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4036 const char *pin)
4037{
4038 return snd_soc_dapm_set_pin(dapm, pin, 1);
4039}
4040EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4041
4042/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004043 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004044 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004045 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02004046 *
Mark Brown74b8f952009-06-06 11:26:15 +01004047 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01004048 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00004049 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004050 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4051 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02004052 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004053int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004054{
Charles Keepax11391102014-02-18 15:22:14 +00004055 int ret;
4056
4057 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4058
4059 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4060
4061 mutex_unlock(&dapm->card->dapm_mutex);
4062
4063 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004064}
Liam Girdwooda5302182008-07-07 13:35:17 +01004065EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004066
4067/**
Charles Keepax11391102014-02-18 15:22:14 +00004068 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4069 * @dapm: DAPM context
4070 * @pin: pin name
4071 *
4072 * Enables input/output pin regardless of any other state. This is
4073 * intended for use with microphone bias supplies used in microphone
4074 * jack detection.
4075 *
4076 * Requires external locking.
4077 *
4078 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4079 * do any widget power switching.
4080 */
4081int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4082 const char *pin)
4083{
4084 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4085
4086 if (!w) {
4087 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4088 return -EINVAL;
4089 }
4090
4091 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02004092 if (!w->connected) {
4093 /*
4094 * w->force does not affect the number of input or output paths,
4095 * so we only have to recheck if w->connected is changed
4096 */
4097 dapm_widget_invalidate_input_paths(w);
4098 dapm_widget_invalidate_output_paths(w);
4099 w->connected = 1;
4100 }
Charles Keepax11391102014-02-18 15:22:14 +00004101 w->force = 1;
4102 dapm_mark_dirty(w, "force enable");
4103
4104 return 0;
4105}
4106EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4107
4108/**
Mark Brownda341832010-03-15 19:23:37 +00004109 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004110 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00004111 * @pin: pin name
4112 *
4113 * Enables input/output pin regardless of any other state. This is
4114 * intended for use with microphone bias supplies used in microphone
4115 * jack detection.
4116 *
4117 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4118 * do any widget power switching.
4119 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004120int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4121 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00004122{
Charles Keepax11391102014-02-18 15:22:14 +00004123 int ret;
Mark Brownda341832010-03-15 19:23:37 +00004124
Charles Keepax11391102014-02-18 15:22:14 +00004125 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00004126
Charles Keepax11391102014-02-18 15:22:14 +00004127 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09004128
Charles Keepax11391102014-02-18 15:22:14 +00004129 mutex_unlock(&dapm->card->dapm_mutex);
4130
4131 return ret;
Mark Brownda341832010-03-15 19:23:37 +00004132}
4133EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4134
4135/**
Charles Keepax11391102014-02-18 15:22:14 +00004136 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4137 * @dapm: DAPM context
4138 * @pin: pin name
4139 *
4140 * Disables input/output pin and its parents or children widgets.
4141 *
4142 * Requires external locking.
4143 *
4144 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4145 * do any widget power switching.
4146 */
4147int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4148 const char *pin)
4149{
4150 return snd_soc_dapm_set_pin(dapm, pin, 0);
4151}
4152EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4153
4154/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004155 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004156 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004157 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004158 *
Mark Brown74b8f952009-06-06 11:26:15 +01004159 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00004160 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004161 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4162 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004163 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004164int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4165 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01004166{
Charles Keepax11391102014-02-18 15:22:14 +00004167 int ret;
4168
4169 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4170
4171 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4172
4173 mutex_unlock(&dapm->card->dapm_mutex);
4174
4175 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01004176}
4177EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4178
4179/**
Charles Keepax11391102014-02-18 15:22:14 +00004180 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4181 * @dapm: DAPM context
4182 * @pin: pin name
4183 *
4184 * Marks the specified pin as being not connected, disabling it along
4185 * any parent or child widgets. At present this is identical to
4186 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4187 * additional things such as disabling controls which only affect
4188 * paths through the pin.
4189 *
4190 * Requires external locking.
4191 *
4192 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4193 * do any widget power switching.
4194 */
4195int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4196 const char *pin)
4197{
4198 return snd_soc_dapm_set_pin(dapm, pin, 0);
4199}
4200EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4201
4202/**
Mark Brown5817b522008-09-24 11:23:11 +01004203 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004204 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01004205 * @pin: pin name
4206 *
4207 * Marks the specified pin as being not connected, disabling it along
4208 * any parent or child widgets. At present this is identical to
4209 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4210 * additional things such as disabling controls which only affect
4211 * paths through the pin.
4212 *
4213 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4214 * do any widget power switching.
4215 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004216int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01004217{
Charles Keepax11391102014-02-18 15:22:14 +00004218 int ret;
4219
4220 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4221
4222 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4223
4224 mutex_unlock(&dapm->card->dapm_mutex);
4225
4226 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01004227}
4228EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4229
4230/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004231 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004232 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004233 * @pin: audio signal pin endpoint (or start point)
4234 *
4235 * Get audio pin status - connected or disconnected.
4236 *
4237 * Returns 1 for connected otherwise 0.
4238 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004239int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4240 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004241{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004242 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004243
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004244 if (w)
4245 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06004246
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004247 return 0;
4248}
Liam Girdwooda5302182008-07-07 13:35:17 +01004249EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004250
4251/**
Mark Brown1547aba2010-05-07 21:11:40 +01004252 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004253 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01004254 * @pin: audio signal pin endpoint (or start point)
4255 *
4256 * Mark the given endpoint or pin as ignoring suspend. When the
4257 * system is disabled a path between two endpoints flagged as ignoring
4258 * suspend will not be disabled. The path must already be enabled via
4259 * normal means at suspend time, it will not be turned on if it was not
4260 * already enabled.
4261 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004262int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4263 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01004264{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004265 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01004266
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004267 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004268 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004269 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01004270 }
4271
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004272 w->ignore_suspend = 1;
4273
4274 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01004275}
4276EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4277
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02004278/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02004279 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03004280 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02004281 *
4282 * Free all dapm widgets and resources.
4283 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004284void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004285{
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02004286 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004287 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02004288 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004289}
4290EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4291
Xiang Xiao57996352014-03-02 00:04:02 +08004292static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01004293{
Liam Girdwood01005a72012-07-06 16:57:05 +01004294 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01004295 struct snd_soc_dapm_widget *w;
4296 LIST_HEAD(down_list);
4297 int powerdown = 0;
4298
Liam Girdwood01005a72012-07-06 16:57:05 +01004299 mutex_lock(&card->dapm_mutex);
4300
Jarkko Nikula97c866d2010-12-14 12:18:31 +02004301 list_for_each_entry(w, &dapm->card->widgets, list) {
4302 if (w->dapm != dapm)
4303 continue;
Mark Brown51737472009-06-22 13:16:51 +01004304 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004305 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01004306 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004307 powerdown = 1;
4308 }
4309 }
4310
4311 /* If there were no widgets to power down we're already in
4312 * standby.
4313 */
4314 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004315 if (dapm->bias_level == SND_SOC_BIAS_ON)
4316 snd_soc_dapm_set_bias_level(dapm,
4317 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004318 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004319 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4320 snd_soc_dapm_set_bias_level(dapm,
4321 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004322 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004323
4324 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004325}
Mark Brown51737472009-06-22 13:16:51 +01004326
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004327/*
4328 * snd_soc_dapm_shutdown - callback for system shutdown
4329 */
4330void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4331{
Xiang Xiao57996352014-03-02 00:04:02 +08004332 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004333
Xiang Xiao57996352014-03-02 00:04:02 +08004334 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08004335 if (dapm != &card->dapm) {
4336 soc_dapm_shutdown_dapm(dapm);
4337 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4338 snd_soc_dapm_set_bias_level(dapm,
4339 SND_SOC_BIAS_OFF);
4340 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004341 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08004342
4343 soc_dapm_shutdown_dapm(&card->dapm);
4344 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4345 snd_soc_dapm_set_bias_level(&card->dapm,
4346 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01004347}
4348
Richard Purdie2b97eab2006-10-06 18:32:18 +02004349/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004350MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004351MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4352MODULE_LICENSE("GPL");