blob: c46b1ff528854c680c1ecc035fd3b4ef6fa1f0dc [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
Liam Girdwood612a3fe2012-02-06 16:05:29 +000017 * mic/headphone insertion events.
Richard Purdie2b97eab2006-10-06 18:32:18 +020018 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Liam Girdwood612a3fe2012-02-06 16:05:29 +000021 * o Delayed power down of audio subsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020024 */
25
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080029#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020030#include <linux/delay.h>
31#include <linux/pm.h>
32#include <linux/bitops.h>
33#include <linux/platform_device.h>
34#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020035#include <linux/debugfs.h>
Mark Brownf1aac482011-12-05 15:17:06 +000036#include <linux/pm_runtime.h>
Mark Brown62ea8742012-01-21 21:14:48 +000037#include <linux/regulator/consumer.h>
Ola Liljad7e7eb92012-05-24 15:26:25 +020038#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020040#include <sound/core.h>
41#include <sound/pcm.h>
42#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020043#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020044#include <sound/initval.h>
45
Mark Brown84e90932010-11-04 00:07:02 -040046#include <trace/events/asoc.h>
47
Mark Brownde02d072011-09-20 21:43:24 +010048#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
49
Lars-Peter Clausena3423b02015-08-11 21:38:00 +020050#define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
51 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
52
53#define snd_soc_dapm_for_each_direction(dir) \
54 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
55 (dir)++)
56
Lars-Peter Clausen57295072013-08-05 11:27:31 +020057static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
58 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
59 const char *control,
60 int (*connected)(struct snd_soc_dapm_widget *source,
61 struct snd_soc_dapm_widget *sink));
Vladimir Zapolskiy5353f652015-06-02 00:57:53 +030062
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +010063struct snd_soc_dapm_widget *
Lars-Peter Clausen57295072013-08-05 11:27:31 +020064snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
65 const struct snd_soc_dapm_widget *widget);
66
Liam Girdwood02aa78a2015-05-25 18:21:17 +010067struct snd_soc_dapm_widget *
68snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +020069 const struct snd_soc_dapm_widget *widget);
70
Richard Purdie2b97eab2006-10-06 18:32:18 +020071/* dapm power sequences - make this per codec in the future */
72static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010073 [snd_soc_dapm_pre] = 0,
Mark Brown62ea8742012-01-21 21:14:48 +000074 [snd_soc_dapm_regulator_supply] = 1,
Ola Liljad7e7eb92012-05-24 15:26:25 +020075 [snd_soc_dapm_clock_supply] = 1,
Mark Brown1dd275b2013-10-09 13:56:37 +010076 [snd_soc_dapm_supply] = 2,
77 [snd_soc_dapm_micbias] = 3,
Mark Brownc74184e2012-04-04 22:12:09 +010078 [snd_soc_dapm_dai_link] = 2,
Mark Brown1dd275b2013-10-09 13:56:37 +010079 [snd_soc_dapm_dai_in] = 4,
80 [snd_soc_dapm_dai_out] = 4,
81 [snd_soc_dapm_aif_in] = 4,
82 [snd_soc_dapm_aif_out] = 4,
83 [snd_soc_dapm_mic] = 5,
84 [snd_soc_dapm_mux] = 6,
Lars-Peter Clausend714f972015-05-01 18:02:43 +020085 [snd_soc_dapm_demux] = 6,
Mark Brown1dd275b2013-10-09 13:56:37 +010086 [snd_soc_dapm_dac] = 7,
87 [snd_soc_dapm_switch] = 8,
88 [snd_soc_dapm_mixer] = 8,
89 [snd_soc_dapm_mixer_named_ctl] = 8,
90 [snd_soc_dapm_pga] = 9,
91 [snd_soc_dapm_adc] = 10,
92 [snd_soc_dapm_out_drv] = 11,
93 [snd_soc_dapm_hp] = 11,
94 [snd_soc_dapm_spk] = 11,
95 [snd_soc_dapm_line] = 11,
96 [snd_soc_dapm_kcontrol] = 12,
97 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +020098};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000099
Richard Purdie2b97eab2006-10-06 18:32:18 +0200100static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +0100101 [snd_soc_dapm_pre] = 0,
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200102 [snd_soc_dapm_kcontrol] = 1,
103 [snd_soc_dapm_adc] = 2,
104 [snd_soc_dapm_hp] = 3,
105 [snd_soc_dapm_spk] = 3,
106 [snd_soc_dapm_line] = 3,
107 [snd_soc_dapm_out_drv] = 3,
Mark Brown38357ab2009-06-06 19:03:23 +0100108 [snd_soc_dapm_pga] = 4,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +0200109 [snd_soc_dapm_switch] = 5,
Mark Brown38357ab2009-06-06 19:03:23 +0100110 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +0100111 [snd_soc_dapm_mixer] = 5,
112 [snd_soc_dapm_dac] = 6,
113 [snd_soc_dapm_mic] = 7,
114 [snd_soc_dapm_micbias] = 8,
115 [snd_soc_dapm_mux] = 9,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200116 [snd_soc_dapm_demux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +0100117 [snd_soc_dapm_aif_in] = 10,
118 [snd_soc_dapm_aif_out] = 10,
Mark Brown46162742013-06-05 19:36:11 +0100119 [snd_soc_dapm_dai_in] = 10,
120 [snd_soc_dapm_dai_out] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +0100121 [snd_soc_dapm_dai_link] = 11,
Mark Brownc74184e2012-04-04 22:12:09 +0100122 [snd_soc_dapm_supply] = 12,
Mark Brown1dd275b2013-10-09 13:56:37 +0100123 [snd_soc_dapm_clock_supply] = 13,
124 [snd_soc_dapm_regulator_supply] = 13,
125 [snd_soc_dapm_post] = 14,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200126};
127
Mark Brownf9fa2b12014-03-06 16:49:11 +0800128static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
129{
130 if (dapm->card && dapm->card->instantiated)
131 lockdep_assert_held(&dapm->card->dapm_mutex);
132}
133
Troy Kisky12ef1932008-10-13 17:42:14 -0700134static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100135{
136 if (pop_time)
137 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
138}
139
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200140static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100141{
142 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200143 char *buf;
144
145 if (!pop_time)
146 return;
147
148 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
149 if (buf == NULL)
150 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100151
152 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200153 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100154 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100155 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200156
157 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100158}
159
Mark Browndb432b42011-10-03 21:06:40 +0100160static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
161{
162 return !list_empty(&w->dirty);
163}
164
Mark Brown492c0a12014-03-06 16:15:48 +0800165static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100166{
Mark Brownf9fa2b12014-03-06 16:49:11 +0800167 dapm_assert_locked(w->dapm);
168
Mark Brown75c1f892011-10-04 22:28:08 +0100169 if (!dapm_dirty_widget(w)) {
170 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
171 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100172 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100173 }
Mark Browndb432b42011-10-03 21:06:40 +0100174}
175
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200176/*
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200177 * Common implementation for dapm_widget_invalidate_input_paths() and
178 * dapm_widget_invalidate_output_paths(). The function is inlined since the
179 * combined size of the two specialized functions is only marginally larger then
180 * the size of the generic function and at the same time the fast path of the
181 * specialized functions is significantly smaller than the generic function.
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200182 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200183static __always_inline void dapm_widget_invalidate_paths(
184 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200185{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200186 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
187 struct snd_soc_dapm_widget *node;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200188 struct snd_soc_dapm_path *p;
189 LIST_HEAD(list);
190
191 dapm_assert_locked(w->dapm);
192
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200193 if (w->endpoints[dir] == -1)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200194 return;
195
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200196 list_add_tail(&w->work_list, &list);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200197 w->endpoints[dir] = -1;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200198
199 list_for_each_entry(w, &list, work_list) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200200 snd_soc_dapm_widget_for_each_path(w, dir, p) {
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200201 if (p->is_supply || p->weak || !p->connect)
202 continue;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200203 node = p->node[rdir];
204 if (node->endpoints[dir] != -1) {
205 node->endpoints[dir] = -1;
206 list_add_tail(&node->work_list, &list);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200207 }
208 }
209 }
210}
211
212/*
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200213 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
214 * input paths
215 * @w: The widget for which to invalidate the cached number of input paths
216 *
217 * Resets the cached number of inputs for the specified widget and all widgets
218 * that can be reached via outcoming paths from the widget.
219 *
220 * This function must be called if the number of output paths for a widget might
221 * have changed. E.g. if the source state of a widget changes or a path is added
222 * or activated with the widget as the sink.
223 */
224static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
225{
226 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
227}
228
229/*
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200230 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
231 * output paths
232 * @w: The widget for which to invalidate the cached number of output paths
233 *
234 * Resets the cached number of outputs for the specified widget and all widgets
235 * that can be reached via incoming paths from the widget.
236 *
237 * This function must be called if the number of output paths for a widget might
238 * have changed. E.g. if the sink state of a widget changes or a path is added
239 * or activated with the widget as the source.
240 */
241static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
242{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200243 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200244}
245
246/*
247 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
248 * for the widgets connected to a path
249 * @p: The path to invalidate
250 *
251 * Resets the cached number of inputs for the sink of the path and the cached
252 * number of outputs for the source of the path.
253 *
254 * This function must be called when a path is added, removed or the connected
255 * state changes.
256 */
257static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
258{
259 /*
260 * Weak paths or supply paths do not influence the number of input or
261 * output paths of their neighbors.
262 */
263 if (p->weak || p->is_supply)
264 return;
265
266 /*
267 * The number of connected endpoints is the sum of the number of
268 * connected endpoints of all neighbors. If a node with 0 connected
269 * endpoints is either connected or disconnected that sum won't change,
270 * so there is no need to re-check the path.
271 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200272 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200273 dapm_widget_invalidate_input_paths(p->sink);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200274 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200275 dapm_widget_invalidate_output_paths(p->source);
276}
277
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200278void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
Mark Browne2d32ff2012-08-31 17:38:32 -0700279{
Mark Browne2d32ff2012-08-31 17:38:32 -0700280 struct snd_soc_dapm_widget *w;
281
282 mutex_lock(&card->dapm_mutex);
283
284 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200285 if (w->is_ep) {
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200286 dapm_mark_dirty(w, "Rechecking endpoints");
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200287 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200288 dapm_widget_invalidate_output_paths(w);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200289 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200290 dapm_widget_invalidate_input_paths(w);
291 }
Mark Browne2d32ff2012-08-31 17:38:32 -0700292 }
293
294 mutex_unlock(&card->dapm_mutex);
295}
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200296EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
Mark Browne2d32ff2012-08-31 17:38:32 -0700297
Richard Purdie2b97eab2006-10-06 18:32:18 +0200298/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100299static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200300 const struct snd_soc_dapm_widget *_widget)
301{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100302 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200303}
304
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200305struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200306 unsigned int value;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200307 struct snd_soc_dapm_widget *widget;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200308 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200309 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200310};
311
312static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100313 struct snd_kcontrol *kcontrol, const char *ctrl_name)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200314{
315 struct dapm_kcontrol_data *data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200316 struct soc_mixer_control *mc;
Charles Keepax561ed682015-05-01 12:37:26 +0100317 struct soc_enum *e;
Charles Keepax773da9b2015-05-01 12:37:25 +0100318 const char *name;
319 int ret;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200320
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200321 data = kzalloc(sizeof(*data), GFP_KERNEL);
Charles Keepax40b7bea2015-05-01 12:37:24 +0100322 if (!data)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200323 return -ENOMEM;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200324
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200325 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200326
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200327 switch (widget->id) {
328 case snd_soc_dapm_switch:
329 case snd_soc_dapm_mixer:
330 case snd_soc_dapm_mixer_named_ctl:
331 mc = (struct soc_mixer_control *)kcontrol->private_value;
332
333 if (mc->autodisable) {
334 struct snd_soc_dapm_widget template;
335
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100336 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
Charles Keepax773da9b2015-05-01 12:37:25 +0100337 "Autodisable");
338 if (!name) {
339 ret = -ENOMEM;
340 goto err_data;
341 }
342
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200343 memset(&template, 0, sizeof(template));
344 template.reg = mc->reg;
345 template.mask = (1 << fls(mc->max)) - 1;
346 template.shift = mc->shift;
347 if (mc->invert)
348 template.off_val = mc->max;
349 else
350 template.off_val = 0;
351 template.on_val = template.off_val;
352 template.id = snd_soc_dapm_kcontrol;
Charles Keepax773da9b2015-05-01 12:37:25 +0100353 template.name = name;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200354
Lars-Peter Clausen2daabd72013-08-30 17:39:33 +0200355 data->value = template.on_val;
356
Liam Girdwood02aa78a2015-05-25 18:21:17 +0100357 data->widget =
358 snd_soc_dapm_new_control_unlocked(widget->dapm,
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200359 &template);
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200360 kfree(name);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200361 if (!data->widget) {
Charles Keepax773da9b2015-05-01 12:37:25 +0100362 ret = -ENOMEM;
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200363 goto err_data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200364 }
365 }
366 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200367 case snd_soc_dapm_demux:
Charles Keepax561ed682015-05-01 12:37:26 +0100368 case snd_soc_dapm_mux:
369 e = (struct soc_enum *)kcontrol->private_value;
370
371 if (e->autodisable) {
372 struct snd_soc_dapm_widget template;
373
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100374 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
Charles Keepax561ed682015-05-01 12:37:26 +0100375 "Autodisable");
376 if (!name) {
377 ret = -ENOMEM;
378 goto err_data;
379 }
380
381 memset(&template, 0, sizeof(template));
382 template.reg = e->reg;
383 template.mask = e->mask << e->shift_l;
384 template.shift = e->shift_l;
385 template.off_val = snd_soc_enum_item_to_val(e, 0);
386 template.on_val = template.off_val;
387 template.id = snd_soc_dapm_kcontrol;
388 template.name = name;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200389
390 data->value = template.on_val;
391
Charles Keepaxffacb482015-06-26 10:39:43 +0100392 data->widget = snd_soc_dapm_new_control_unlocked(
393 widget->dapm, &template);
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200394 kfree(name);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200395 if (!data->widget) {
Charles Keepax561ed682015-05-01 12:37:26 +0100396 ret = -ENOMEM;
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200397 goto err_data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200398 }
Charles Keepax561ed682015-05-01 12:37:26 +0100399
400 snd_soc_dapm_add_path(widget->dapm, data->widget,
401 widget, NULL, NULL);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200402 }
403 break;
404 default:
405 break;
406 }
407
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200408 kcontrol->private_data = data;
409
410 return 0;
Charles Keepax773da9b2015-05-01 12:37:25 +0100411
Charles Keepax773da9b2015-05-01 12:37:25 +0100412err_data:
413 kfree(data);
414 return ret;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200415}
416
417static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
418{
419 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200420 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200421 kfree(data);
422}
423
424static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
425 const struct snd_kcontrol *kcontrol)
426{
427 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
428
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200429 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200430}
431
432static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
433 struct snd_soc_dapm_widget *widget)
434{
435 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200436 struct snd_soc_dapm_widget_list *new_wlist;
437 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200438
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200439 if (data->wlist)
440 n = data->wlist->num_widgets + 1;
441 else
442 n = 1;
443
444 new_wlist = krealloc(data->wlist,
445 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
446 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200447 return -ENOMEM;
448
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200449 new_wlist->widgets[n - 1] = widget;
450 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200451
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200452 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200453
454 return 0;
455}
456
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200457static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
458 struct snd_soc_dapm_path *path)
459{
460 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
461
462 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200463}
464
465static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
466{
467 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
468
469 if (!data->widget)
470 return true;
471
472 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200473}
474
475static struct list_head *dapm_kcontrol_get_path_list(
476 const struct snd_kcontrol *kcontrol)
477{
478 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
479
480 return &data->paths;
481}
482
483#define dapm_kcontrol_for_each_path(path, kcontrol) \
484 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
485 list_kcontrol)
486
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530487unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200488{
489 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
490
491 return data->value;
492}
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530493EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200494
495static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
496 unsigned int value)
497{
498 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
499
500 if (data->value == value)
501 return false;
502
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200503 if (data->widget)
504 data->widget->on_val = value;
505
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200506 data->value = value;
507
508 return true;
509}
510
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200511/**
Mythri P K93e39a12015-10-20 22:30:08 +0530512 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
513 * kcontrol
514 * @kcontrol: The kcontrol
515 */
516struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
517 struct snd_kcontrol *kcontrol)
518{
519 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
520}
521EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
522
523/**
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200524 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
525 * kcontrol
526 * @kcontrol: The kcontrol
527 *
528 * Note: This function must only be used on kcontrols that are known to have
529 * been registered for a CODEC. Otherwise the behaviour is undefined.
530 */
531struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
532 struct snd_kcontrol *kcontrol)
533{
534 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
535}
536EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
537
Liam Girdwood6c120e12012-02-15 15:15:34 +0000538static void dapm_reset(struct snd_soc_card *card)
539{
540 struct snd_soc_dapm_widget *w;
541
Mark Brownf9fa2b12014-03-06 16:49:11 +0800542 lockdep_assert_held(&card->dapm_mutex);
543
Liam Girdwood6c120e12012-02-15 15:15:34 +0000544 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
545
546 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200547 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000548 w->power_checked = false;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000549 }
550}
551
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200552static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
553{
554 if (!dapm->component)
555 return NULL;
556 return dapm->component->name_prefix;
557}
558
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200559static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800560 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100561{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200562 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200563 return -EIO;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200564 return snd_soc_component_read(dapm->component, reg, value);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100565}
566
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200567static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
Bard Liao34775012014-04-17 20:12:56 +0800568 int reg, unsigned int mask, unsigned int value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100569{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200570 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200571 return -EIO;
Mark Brownfcf6c5e2014-12-15 13:08:48 +0000572 return snd_soc_component_update_bits(dapm->component, reg,
573 mask, value);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000574}
575
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200576static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
577 int reg, unsigned int mask, unsigned int value)
578{
579 if (!dapm->component)
580 return -EIO;
581 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
582}
583
Mark Browneb270e92013-10-09 13:52:52 +0100584static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
585{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200586 if (dapm->component)
587 snd_soc_component_async_complete(dapm->component);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100588}
589
Charles Keepax45a110a2015-05-11 13:50:30 +0100590static struct snd_soc_dapm_widget *
591dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
592{
593 struct snd_soc_dapm_widget *w = wcache->widget;
594 struct list_head *wlist;
595 const int depth = 2;
596 int i = 0;
597
598 if (w) {
599 wlist = &w->dapm->card->widgets;
600
601 list_for_each_entry_from(w, wlist, list) {
602 if (!strcmp(name, w->name))
603 return w;
604
605 if (++i == depth)
606 break;
607 }
608 }
609
610 return NULL;
611}
612
613static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
614 struct snd_soc_dapm_widget *w)
615{
616 wcache->widget = w;
617}
618
Mark Brown452c5ea2009-05-17 21:41:23 +0100619/**
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200620 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
621 * @dapm: The DAPM context for which to set the level
622 * @level: The level to set
623 *
624 * Forces the DAPM bias level to a specific state. It will call the bias level
625 * callback of DAPM context with the specified level. This will even happen if
626 * the context is already at the same level. Furthermore it will not go through
627 * the normal bias level sequencing, meaning any intermediate states between the
628 * current and the target state will not be entered.
629 *
630 * Note that the change in bias level is only temporary and the next time
631 * snd_soc_dapm_sync() is called the state will be set to the level as
632 * determined by the DAPM core. The function is mainly intended to be used to
633 * used during probe or resume from suspend to power up the device so
634 * initialization can be done, before the DAPM core takes over.
635 */
636int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
637 enum snd_soc_bias_level level)
638{
639 int ret = 0;
640
641 if (dapm->set_bias_level)
642 ret = dapm->set_bias_level(dapm, level);
643
Lars-Peter Clausenf4bf8d72015-04-27 22:13:25 +0200644 if (ret == 0)
645 dapm->bias_level = level;
646
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200647 return ret;
648}
649EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
650
Mark Brown452c5ea2009-05-17 21:41:23 +0100651/**
652 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800653 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100654 * @level: level to configure
655 *
656 * Configure the bias (power) levels for the SoC audio device.
657 *
658 * Returns 0 for success else error.
659 */
Mark Browned5a4c42011-02-18 11:12:42 -0800660static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200661 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100662{
Mark Browned5a4c42011-02-18 11:12:42 -0800663 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100664 int ret = 0;
665
Mark Brown84e90932010-11-04 00:07:02 -0400666 trace_snd_soc_bias_level_start(card, level);
667
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000668 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100669 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100670 if (ret != 0)
671 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100672
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200673 if (!card || dapm != &card->dapm)
674 ret = snd_soc_dapm_force_bias_level(dapm, level);
Liam Girdwood41231282012-07-06 16:56:16 +0100675
Mark Brown171ec6b2011-06-06 18:15:19 +0100676 if (ret != 0)
677 goto out;
678
679 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100680 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100681out:
Mark Brown84e90932010-11-04 00:07:02 -0400682 trace_snd_soc_bias_level_done(card, level);
683
Mark Brown452c5ea2009-05-17 21:41:23 +0100684 return ret;
685}
686
Mark Brown74b8f952009-06-06 11:26:15 +0100687/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200688static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200689 struct snd_soc_dapm_path *path, const char *control_name,
690 struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200691{
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200692 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200693 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100694 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200695 int i;
696
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100697 if (e->reg != SND_SOC_NOPM) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200698 soc_dapm_read(dapm, e->reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100699 val = (val >> e->shift_l) & e->mask;
700 item = snd_soc_enum_val_to_item(e, val);
701 } else {
702 /* since a virtual mux has no backing registers to
703 * decide which path to connect, it will try to match
704 * with the first enumeration. This is to ensure
705 * that the default mux choice (the first) will be
706 * correctly powered up during initialization.
707 */
708 item = 0;
709 }
710
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100711 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200712 if (!(strcmp(control_name, e->texts[i]))) {
Rasmus Villemoes98ad73c2014-10-21 17:01:15 +0200713 path->name = e->texts[i];
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100714 if (i == item)
715 path->connect = 1;
716 else
717 path->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200718 return 0;
719 }
720 }
721
722 return -ENODEV;
723}
724
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100725/* set up initial codec paths */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200726static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i)
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100727{
728 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200729 p->sink->kcontrol_news[i].private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100730 unsigned int reg = mc->reg;
731 unsigned int shift = mc->shift;
732 unsigned int max = mc->max;
733 unsigned int mask = (1 << fls(max)) - 1;
734 unsigned int invert = mc->invert;
735 unsigned int val;
736
737 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200738 soc_dapm_read(p->sink->dapm, reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100739 val = (val >> shift) & mask;
740 if (invert)
741 val = max - val;
742 p->connect = !!val;
743 } else {
744 p->connect = 0;
745 }
746}
747
Mark Brown74b8f952009-06-06 11:26:15 +0100748/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200749static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200750 struct snd_soc_dapm_path *path, const char *control_name)
751{
752 int i;
753
754 /* search for mixer kcontrol */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200755 for (i = 0; i < path->sink->num_kcontrols; i++) {
756 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
757 path->name = path->sink->kcontrol_news[i].name;
758 dapm_set_mixer_path_status(path, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200759 return 0;
760 }
761 }
762 return -ENODEV;
763}
764
Stephen Warrenaf468002011-04-28 17:38:01 -0600765static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600766 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600767 const struct snd_kcontrol_new *kcontrol_new,
768 struct snd_kcontrol **kcontrol)
769{
770 struct snd_soc_dapm_widget *w;
771 int i;
772
773 *kcontrol = NULL;
774
775 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600776 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
777 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600778 for (i = 0; i < w->num_kcontrols; i++) {
779 if (&w->kcontrol_news[i] == kcontrol_new) {
780 if (w->kcontrols)
781 *kcontrol = w->kcontrols[i];
782 return 1;
783 }
784 }
785 }
786
787 return 0;
788}
789
Stephen Warren85762e72013-03-29 15:40:10 -0600790/*
791 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
792 * create it. Either way, add the widget into the control's widget list
793 */
Jeeja KP19a2557b2015-10-20 22:30:06 +0530794static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100795 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200796{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200797 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000798 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000799 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600800 size_t prefix_len;
801 int shared;
802 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600803 bool wname_in_long_name, kcname_in_long_name;
Daniel Macke5092c92014-10-07 13:41:24 +0200804 char *long_name = NULL;
Stephen Warren85762e72013-03-29 15:40:10 -0600805 const char *name;
Daniel Macke5092c92014-10-07 13:41:24 +0200806 int ret = 0;
Mark Brownefb7ac32011-03-08 17:23:24 +0000807
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200808 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000809 if (prefix)
810 prefix_len = strlen(prefix) + 1;
811 else
812 prefix_len = 0;
813
Stephen Warren85762e72013-03-29 15:40:10 -0600814 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
815 &kcontrol);
816
Stephen Warren85762e72013-03-29 15:40:10 -0600817 if (!kcontrol) {
818 if (shared) {
819 wname_in_long_name = false;
820 kcname_in_long_name = true;
821 } else {
822 switch (w->id) {
823 case snd_soc_dapm_switch:
824 case snd_soc_dapm_mixer:
Jeeja KP19a2557b2015-10-20 22:30:06 +0530825 case snd_soc_dapm_pga:
Stephen Warren85762e72013-03-29 15:40:10 -0600826 wname_in_long_name = true;
827 kcname_in_long_name = true;
828 break;
829 case snd_soc_dapm_mixer_named_ctl:
830 wname_in_long_name = false;
831 kcname_in_long_name = true;
832 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200833 case snd_soc_dapm_demux:
Stephen Warren85762e72013-03-29 15:40:10 -0600834 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600835 wname_in_long_name = true;
836 kcname_in_long_name = false;
837 break;
838 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600839 return -EINVAL;
840 }
841 }
842
843 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600844 /*
845 * The control will get a prefix from the control
846 * creation process but we're also using the same
847 * prefix for widgets so cut the prefix off the
848 * front of the widget name.
849 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200850 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600851 w->name + prefix_len,
852 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200853 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200854 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600855
856 name = long_name;
857 } else if (wname_in_long_name) {
858 long_name = NULL;
859 name = w->name + prefix_len;
860 } else {
861 long_name = NULL;
862 name = w->kcontrol_news[kci].name;
863 }
864
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200865 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600866 prefix);
Daniel Macke5092c92014-10-07 13:41:24 +0200867 if (!kcontrol) {
868 ret = -ENOMEM;
869 goto exit_free;
870 }
871
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200872 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200873
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100874 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200875 if (ret) {
876 snd_ctl_free_one(kcontrol);
Daniel Macke5092c92014-10-07 13:41:24 +0200877 goto exit_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200878 }
879
Stephen Warren85762e72013-03-29 15:40:10 -0600880 ret = snd_ctl_add(card, kcontrol);
881 if (ret < 0) {
882 dev_err(dapm->dev,
883 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
884 w->name, name, ret);
Daniel Macke5092c92014-10-07 13:41:24 +0200885 goto exit_free;
Stephen Warren85762e72013-03-29 15:40:10 -0600886 }
Stephen Warren85762e72013-03-29 15:40:10 -0600887 }
888
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200889 ret = dapm_kcontrol_add_widget(kcontrol, w);
Daniel Macke5092c92014-10-07 13:41:24 +0200890 if (ret == 0)
891 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200892
Daniel Macke5092c92014-10-07 13:41:24 +0200893exit_free:
894 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600895
Daniel Macke5092c92014-10-07 13:41:24 +0200896 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600897}
898
899/* create new dapm mixer control */
900static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
901{
902 int i, ret;
903 struct snd_soc_dapm_path *path;
Charles Keepax561ed682015-05-01 12:37:26 +0100904 struct dapm_kcontrol_data *data;
Stephen Warren85762e72013-03-29 15:40:10 -0600905
Richard Purdie2b97eab2006-10-06 18:32:18 +0200906 /* add kcontrol */
907 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200908 /* match name */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +0200909 snd_soc_dapm_widget_for_each_source_path(w, path) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200910 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600911 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200912 continue;
913
Charles Keepax561ed682015-05-01 12:37:26 +0100914 if (!w->kcontrols[i]) {
Jeeja KP19a2557b2015-10-20 22:30:06 +0530915 ret = dapm_create_or_share_kcontrol(w, i);
Charles Keepax561ed682015-05-01 12:37:26 +0100916 if (ret < 0)
917 return ret;
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200918 }
919
Mark Brown946d92a2013-08-12 23:28:42 +0100920 dapm_kcontrol_add_path(w->kcontrols[i], path);
Charles Keepax561ed682015-05-01 12:37:26 +0100921
922 data = snd_kcontrol_chip(w->kcontrols[i]);
923 if (data->widget)
924 snd_soc_dapm_add_path(data->widget->dapm,
925 data->widget,
926 path->source,
927 NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200928 }
929 }
Stephen Warren85762e72013-03-29 15:40:10 -0600930
931 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200932}
933
934/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200935static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200936{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200937 struct snd_soc_dapm_context *dapm = w->dapm;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200938 enum snd_soc_dapm_direction dir;
Stephen Warren85762e72013-03-29 15:40:10 -0600939 struct snd_soc_dapm_path *path;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200940 const char *type;
Stephen Warrenaf468002011-04-28 17:38:01 -0600941 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200942
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200943 switch (w->id) {
944 case snd_soc_dapm_mux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200945 dir = SND_SOC_DAPM_DIR_OUT;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200946 type = "mux";
947 break;
948 case snd_soc_dapm_demux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200949 dir = SND_SOC_DAPM_DIR_IN;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200950 type = "demux";
951 break;
952 default:
953 return -EINVAL;
954 }
955
Stephen Warrenaf468002011-04-28 17:38:01 -0600956 if (w->num_kcontrols != 1) {
957 dev_err(dapm->dev,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200958 "ASoC: %s %s has incorrect number of controls\n", type,
Stephen Warrenaf468002011-04-28 17:38:01 -0600959 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200960 return -EINVAL;
961 }
962
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200963 if (list_empty(&w->edges[dir])) {
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200964 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
Stephen Warren85762e72013-03-29 15:40:10 -0600965 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600966 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200967
Jeeja KP19a2557b2015-10-20 22:30:06 +0530968 ret = dapm_create_or_share_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600969 if (ret < 0)
970 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600971
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200972 snd_soc_dapm_widget_for_each_path(w, dir, path) {
973 if (path->name)
974 dapm_kcontrol_add_path(w->kcontrols[0], path);
Lars-Peter Clausen98407ef2014-10-25 17:41:57 +0200975 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200976
Stephen Warrenaf468002011-04-28 17:38:01 -0600977 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200978}
979
980/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200981static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200982{
Jeeja KP19a2557b2015-10-20 22:30:06 +0530983 int i, ret;
984
985 for (i = 0; i < w->num_kcontrols; i++) {
986 ret = dapm_create_or_share_kcontrol(w, i);
987 if (ret < 0)
988 return ret;
989 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200990
Mark Browna6c65732010-03-03 17:45:21 +0000991 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200992}
993
Nikesh Oswalc6615082015-02-02 17:06:44 +0000994/* create new dapm dai link control */
995static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
996{
997 int i, ret;
998 struct snd_kcontrol *kcontrol;
999 struct snd_soc_dapm_context *dapm = w->dapm;
1000 struct snd_card *card = dapm->card->snd_card;
1001
1002 /* create control for links with > 1 config */
1003 if (w->num_params <= 1)
1004 return 0;
1005
1006 /* add kcontrol */
1007 for (i = 0; i < w->num_kcontrols; i++) {
1008 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1009 w->name, NULL);
1010 ret = snd_ctl_add(card, kcontrol);
1011 if (ret < 0) {
1012 dev_err(dapm->dev,
1013 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1014 w->name, w->kcontrol_news[i].name, ret);
1015 return ret;
1016 }
1017 kcontrol->private_data = w;
1018 w->kcontrols[i] = kcontrol;
1019 }
1020
1021 return 0;
1022}
1023
Mark Brown99497882010-05-07 20:24:05 +01001024/* We implement power down on suspend by checking the power state of
1025 * the ALSA card - when we are suspending the ALSA state for the card
1026 * is set to D3.
1027 */
1028static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1029{
Mark Brown12ea2c72011-03-02 18:17:32 +00001030 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +01001031
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001032 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +01001033 case SNDRV_CTL_POWER_D3hot:
1034 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +01001035 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001036 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001037 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +01001038 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +01001039 default:
1040 return 1;
1041 }
1042}
1043
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001044static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1045 struct list_head *widgets)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001046{
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001047 struct snd_soc_dapm_widget *w;
1048 struct list_head *it;
1049 unsigned int size = 0;
1050 unsigned int i = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001051
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001052 list_for_each(it, widgets)
1053 size++;
1054
1055 *list = kzalloc(sizeof(**list) + size * sizeof(*w), GFP_KERNEL);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001056 if (*list == NULL)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001057 return -ENOMEM;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001058
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001059 list_for_each_entry(w, widgets, work_list)
1060 (*list)->widgets[i++] = w;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001061
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001062 (*list)->num_widgets = i;
1063
1064 return 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001065}
1066
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001067/*
1068 * Common implementation for is_connected_output_ep() and
1069 * is_connected_input_ep(). The function is inlined since the combined size of
1070 * the two specialized functions is only marginally larger then the size of the
1071 * generic function and at the same time the fast path of the specialized
1072 * functions is significantly smaller than the generic function.
1073 */
1074static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1075 struct list_head *list, enum snd_soc_dapm_direction dir,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001076 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1077 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1078 enum snd_soc_dapm_direction)),
1079 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1080 enum snd_soc_dapm_direction))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001081{
1082 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001083 struct snd_soc_dapm_path *path;
1084 int con = 0;
1085
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001086 if (widget->endpoints[dir] >= 0)
1087 return widget->endpoints[dir];
Mark Brown024dc072011-10-09 11:52:05 +01001088
Mark Brownde02d072011-09-20 21:43:24 +01001089 DAPM_UPDATE_STAT(widget, path_checks);
1090
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001091 /* do we need to add this widget to the list ? */
1092 if (list)
1093 list_add_tail(&widget->work_list, list);
1094
Jeeja KP09464972016-06-15 11:16:55 +05301095 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1096 widget->endpoints[dir] = 1;
1097 return widget->endpoints[dir];
1098 }
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001099
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001100 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1101 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1102 return widget->endpoints[dir];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001103 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001104
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001105 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
Mark Browne56235e2011-09-21 18:19:14 +01001106 DAPM_UPDATE_STAT(widget, neighbour_checks);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001107
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001108 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +01001109 continue;
1110
Mark Brown8af294b2013-02-22 17:48:15 +00001111 if (path->walking)
1112 return 1;
1113
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001114 trace_snd_soc_dapm_path(widget, dir, path);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001115
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001116 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +00001117 path->walking = 1;
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001118 con += fn(path->node[dir], list, custom_stop_condition);
Mark Brown8af294b2013-02-22 17:48:15 +00001119 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001120 }
1121 }
1122
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001123 widget->endpoints[dir] = con;
Mark Brown024dc072011-10-09 11:52:05 +01001124
Richard Purdie2b97eab2006-10-06 18:32:18 +02001125 return con;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001126}
1127
1128/*
1129 * Recursively check for a completed path to an active or physically connected
1130 * output widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001131 *
1132 * Optionally, can be supplied with a function acting as a stopping condition.
1133 * This function takes the dapm widget currently being examined and the walk
1134 * direction as an arguments, it should return true if the walk should be
1135 * stopped and false otherwise.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001136 */
1137static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001138 struct list_head *list,
1139 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1140 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001141{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001142 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001143 is_connected_output_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001144}
1145
1146/*
1147 * Recursively check for a completed path to an active or physically connected
1148 * input widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001149 *
1150 * Optionally, can be supplied with a function acting as a stopping condition.
1151 * This function takes the dapm widget currently being examined and the walk
1152 * direction as an arguments, it should return true if the walk should be
1153 * stopped and false otherwise.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001154 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001155static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001156 struct list_head *list,
1157 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1158 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001159{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001160 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001161 is_connected_input_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001162}
1163
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001164/**
1165 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1166 * @dai: the soc DAI.
1167 * @stream: stream direction.
1168 * @list: list of active widgets for this stream.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001169 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1170 * walk based on custom logic.
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001171 *
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001172 * Queries DAPM graph as to whether a valid audio stream path exists for
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001173 * the initial stream specified by name. This takes into account
1174 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1175 *
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001176 * Optionally, can be supplied with a function acting as a stopping condition.
1177 * This function takes the dapm widget currently being examined and the walk
1178 * direction as an arguments, it should return true if the walk should be
1179 * stopped and false otherwise.
1180 *
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001181 * Returns the number of valid paths or negative error.
1182 */
1183int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001184 struct snd_soc_dapm_widget_list **list,
1185 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1186 enum snd_soc_dapm_direction))
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001187{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01001188 struct snd_soc_card *card = dai->component->card;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001189 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001190 LIST_HEAD(widgets);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001191 int paths;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001192 int ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001193
1194 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001195
1196 /*
1197 * For is_connected_{output,input}_ep fully discover the graph we need
1198 * to reset the cached number of inputs and outputs.
1199 */
1200 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001201 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1202 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001203 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001204
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001205 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001206 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1207 custom_stop_condition);
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001208 else
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001209 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1210 custom_stop_condition);
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001211
1212 /* Drop starting point */
1213 list_del(widgets.next);
1214
1215 ret = dapm_widget_list_create(list, &widgets);
1216 if (ret)
Lars-Peter Clausen30abbe72015-08-11 21:37:59 +02001217 paths = ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001218
1219 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001220 mutex_unlock(&card->dapm_mutex);
1221
1222 return paths;
1223}
1224
Richard Purdie2b97eab2006-10-06 18:32:18 +02001225/*
Mark Brown62ea8742012-01-21 21:14:48 +00001226 * Handler for regulator supply widget.
1227 */
1228int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1229 struct snd_kcontrol *kcontrol, int event)
1230{
Mark Brownc05b84d2012-09-07 12:57:11 +08001231 int ret;
1232
Mark Browneb270e92013-10-09 13:52:52 +01001233 soc_dapm_async_complete(w->dapm);
1234
Mark Brownc05b84d2012-09-07 12:57:11 +08001235 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001236 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001237 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001238 if (ret != 0)
1239 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001240 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001241 w->name, ret);
1242 }
1243
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001244 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001245 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001246 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001247 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001248 if (ret != 0)
1249 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001250 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001251 w->name, ret);
1252 }
1253
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001254 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001255 }
Mark Brown62ea8742012-01-21 21:14:48 +00001256}
1257EXPORT_SYMBOL_GPL(dapm_regulator_event);
1258
Ola Liljad7e7eb92012-05-24 15:26:25 +02001259/*
1260 * Handler for clock supply widget.
1261 */
1262int dapm_clock_event(struct snd_soc_dapm_widget *w,
1263 struct snd_kcontrol *kcontrol, int event)
1264{
1265 if (!w->clk)
1266 return -EIO;
1267
Mark Browneb270e92013-10-09 13:52:52 +01001268 soc_dapm_async_complete(w->dapm);
1269
Mark Brownec029952012-06-04 08:16:20 +01001270#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001271 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001272 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001273 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001274 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001275 return 0;
1276 }
Mark Brownec029952012-06-04 08:16:20 +01001277#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001278 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001279}
1280EXPORT_SYMBOL_GPL(dapm_clock_event);
1281
Mark Brownd8050022011-09-28 18:28:23 +01001282static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1283{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001284 if (w->power_checked)
1285 return w->new_power;
1286
Mark Brownd8050022011-09-28 18:28:23 +01001287 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001288 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001289 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001290 w->new_power = w->power_check(w);
1291
1292 w->power_checked = true;
1293
1294 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001295}
1296
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001297/* Generic check to see if a widget should be powered. */
Mark Browncd0f2d42009-04-20 16:56:59 +01001298static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1299{
1300 int in, out;
1301
Mark Brownde02d072011-09-20 21:43:24 +01001302 DAPM_UPDATE_STAT(w, power_checks);
1303
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001304 in = is_connected_input_ep(w, NULL, NULL);
1305 out = is_connected_output_ep(w, NULL, NULL);
Mark Browncd0f2d42009-04-20 16:56:59 +01001306 return out != 0 && in != 0;
1307}
1308
Mark Brown246d0a12009-04-22 18:24:55 +01001309/* Check to see if a power supply is needed */
1310static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1311{
1312 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001313
Mark Brownde02d072011-09-20 21:43:24 +01001314 DAPM_UPDATE_STAT(w, power_checks);
1315
Mark Brown246d0a12009-04-22 18:24:55 +01001316 /* Check if one of our outputs is connected */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001317 snd_soc_dapm_widget_for_each_sink_path(w, path) {
Mark Browna8fdac82011-09-28 18:20:26 +01001318 DAPM_UPDATE_STAT(w, neighbour_checks);
1319
Mark Brownbf3a9e12011-06-13 16:42:29 +01001320 if (path->weak)
1321 continue;
1322
Mark Brown215edda2009-09-08 18:59:05 +01001323 if (path->connected &&
1324 !path->connected(path->source, path->sink))
1325 continue;
1326
Mark Brownf68d7e12011-10-04 22:57:50 +01001327 if (dapm_widget_power_check(path->sink))
1328 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001329 }
1330
Mark Brownf68d7e12011-10-04 22:57:50 +01001331 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001332}
1333
Mark Brown35c64bc2011-09-28 18:23:53 +01001334static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1335{
Charles Keepax20bb0182015-12-02 10:22:16 +00001336 return w->connected;
Mark Brown35c64bc2011-09-28 18:23:53 +01001337}
1338
Mark Brown38357ab2009-06-06 19:03:23 +01001339static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1340 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001341 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001342{
Mark Brown828a8422011-01-15 13:14:30 +00001343 int *sort;
1344
1345 if (power_up)
1346 sort = dapm_up_seq;
1347 else
1348 sort = dapm_down_seq;
1349
Mark Brown38357ab2009-06-06 19:03:23 +01001350 if (sort[a->id] != sort[b->id])
1351 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001352 if (a->subseq != b->subseq) {
1353 if (power_up)
1354 return a->subseq - b->subseq;
1355 else
1356 return b->subseq - a->subseq;
1357 }
Mark Brownb22ead22009-06-07 12:51:26 +01001358 if (a->reg != b->reg)
1359 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001360 if (a->dapm != b->dapm)
1361 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001362
Mark Brown38357ab2009-06-06 19:03:23 +01001363 return 0;
1364}
Mark Brown42aa3412009-03-01 19:21:10 +00001365
Mark Brown38357ab2009-06-06 19:03:23 +01001366/* Insert a widget in order into a DAPM power sequence. */
1367static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1368 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001369 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001370{
1371 struct snd_soc_dapm_widget *w;
1372
1373 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001374 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001375 list_add_tail(&new_widget->power_list, &w->power_list);
1376 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001377 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001378
Mark Brown38357ab2009-06-06 19:03:23 +01001379 list_add_tail(&new_widget->power_list, list);
1380}
Mark Brown42aa3412009-03-01 19:21:10 +00001381
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001382static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001383 struct snd_soc_dapm_widget *w, int event)
1384{
Mark Brown68f89ad2010-11-03 23:51:49 -04001385 const char *ev_name;
1386 int power, ret;
1387
1388 switch (event) {
1389 case SND_SOC_DAPM_PRE_PMU:
1390 ev_name = "PRE_PMU";
1391 power = 1;
1392 break;
1393 case SND_SOC_DAPM_POST_PMU:
1394 ev_name = "POST_PMU";
1395 power = 1;
1396 break;
1397 case SND_SOC_DAPM_PRE_PMD:
1398 ev_name = "PRE_PMD";
1399 power = 0;
1400 break;
1401 case SND_SOC_DAPM_POST_PMD:
1402 ev_name = "POST_PMD";
1403 power = 0;
1404 break;
Mark Brown80114122013-02-25 15:14:19 +00001405 case SND_SOC_DAPM_WILL_PMU:
1406 ev_name = "WILL_PMU";
1407 power = 1;
1408 break;
1409 case SND_SOC_DAPM_WILL_PMD:
1410 ev_name = "WILL_PMD";
1411 power = 0;
1412 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001413 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001414 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001415 return;
1416 }
1417
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001418 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001419 return;
1420
1421 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001422 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001423 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001424 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001425 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001426 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001427 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001428 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001429 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001430 ev_name, w->name, ret);
1431 }
1432}
1433
Mark Brownb22ead22009-06-07 12:51:26 +01001434/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001435static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001436 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001437{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001438 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001439 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001440 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001441 unsigned int value = 0;
1442 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001443
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001444 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1445 reg = w->reg;
1446 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001447
1448 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001449 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001450 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001451
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001452 mask |= w->mask << w->shift;
1453 if (w->power)
1454 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001455 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001456 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001457
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001458 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001459 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1460 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001461
Mark Brown68f89ad2010-11-03 23:51:49 -04001462 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001463 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1464 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001465 }
1466
Mark Brown81628102009-06-07 13:21:24 +01001467 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001468 /* Any widget will do, they should all be updating the
1469 * same register.
1470 */
Mark Brown29376bc2011-06-19 13:49:28 +01001471
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001472 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001473 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001474 value, mask, reg, card->pop_time);
1475 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001476 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001477 }
1478
1479 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001480 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1481 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001482 }
Mark Brown42aa3412009-03-01 19:21:10 +00001483}
1484
Mark Brownb22ead22009-06-07 12:51:26 +01001485/* Apply a DAPM power sequence.
1486 *
1487 * We walk over a pre-sorted list of widgets to apply power to. In
1488 * order to minimise the number of writes to the device required
1489 * multiple widgets will be updated in a single write where possible.
1490 * Currently anything that requires more than a single write is not
1491 * handled.
1492 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001493static void dapm_seq_run(struct snd_soc_card *card,
1494 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001495{
1496 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001497 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001498 LIST_HEAD(pending);
1499 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001500 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001501 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001502 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001503 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001504 int *sort;
1505
1506 if (power_up)
1507 sort = dapm_up_seq;
1508 else
1509 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001510
Mark Brownb22ead22009-06-07 12:51:26 +01001511 list_for_each_entry_safe(w, n, list, power_list) {
1512 ret = 0;
1513
1514 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001515 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001516 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001517 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001518 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001519
Mark Brown474b62d2011-01-18 16:14:44 +00001520 if (cur_dapm && cur_dapm->seq_notifier) {
1521 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1522 if (sort[i] == cur_sort)
1523 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001524 i,
1525 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001526 }
1527
Mark Browneb270e92013-10-09 13:52:52 +01001528 if (cur_dapm && w->dapm != cur_dapm)
1529 soc_dapm_async_complete(cur_dapm);
1530
Mark Brownb22ead22009-06-07 12:51:26 +01001531 INIT_LIST_HEAD(&pending);
1532 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001533 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001534 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001535 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001536 }
1537
Mark Brown163cac02009-06-07 10:12:52 +01001538 switch (w->id) {
1539 case snd_soc_dapm_pre:
1540 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001541 list_for_each_entry_safe_continue(w, n, list,
1542 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001543
Mark Brownb22ead22009-06-07 12:51:26 +01001544 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001545 ret = w->event(w,
1546 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001547 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001548 ret = w->event(w,
1549 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001550 break;
1551
1552 case snd_soc_dapm_post:
1553 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001554 list_for_each_entry_safe_continue(w, n, list,
1555 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001556
Mark Brownb22ead22009-06-07 12:51:26 +01001557 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001558 ret = w->event(w,
1559 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001560 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001561 ret = w->event(w,
1562 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001563 break;
1564
Mark Brown163cac02009-06-07 10:12:52 +01001565 default:
Mark Brown81628102009-06-07 13:21:24 +01001566 /* Queue it up for application */
1567 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001568 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001569 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001570 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001571 list_move(&w->power_list, &pending);
1572 break;
Mark Brown163cac02009-06-07 10:12:52 +01001573 }
Mark Brownb22ead22009-06-07 12:51:26 +01001574
1575 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001576 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001577 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001578 }
Mark Brownb22ead22009-06-07 12:51:26 +01001579
1580 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001581 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001582
1583 if (cur_dapm && cur_dapm->seq_notifier) {
1584 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1585 if (sort[i] == cur_sort)
1586 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001587 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001588 }
Mark Browneb270e92013-10-09 13:52:52 +01001589
1590 list_for_each_entry(d, &card->dapm_list, list) {
1591 soc_dapm_async_complete(d);
1592 }
Mark Brown163cac02009-06-07 10:12:52 +01001593}
1594
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001595static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001596{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001597 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001598 struct snd_soc_dapm_widget_list *wlist;
1599 struct snd_soc_dapm_widget *w = NULL;
1600 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001601 int ret;
1602
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001603 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001604 return;
1605
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001606 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001607
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001608 for (wi = 0; wi < wlist->num_widgets; wi++) {
1609 w = wlist->widgets[wi];
1610
1611 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1612 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1613 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001614 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001615 w->name, ret);
1616 }
Mark Brown97404f22010-12-14 16:13:57 +00001617 }
1618
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001619 if (!w)
1620 return;
1621
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001622 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1623 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001624 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001625 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001626 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001627
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001628 for (wi = 0; wi < wlist->num_widgets; wi++) {
1629 w = wlist->widgets[wi];
1630
1631 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1632 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1633 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001634 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001635 w->name, ret);
1636 }
Mark Brown97404f22010-12-14 16:13:57 +00001637 }
1638}
1639
Mark Brown9d0624a2011-02-18 11:49:43 -08001640/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1641 * they're changing state.
1642 */
1643static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1644{
1645 struct snd_soc_dapm_context *d = data;
1646 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001647
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001648 /* If we're off and we're not supposed to go into STANDBY */
Mark Brown56fba412011-06-04 11:25:10 +01001649 if (d->bias_level == SND_SOC_BIAS_OFF &&
1650 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001651 if (d->dev)
1652 pm_runtime_get_sync(d->dev);
1653
Mark Brown9d0624a2011-02-18 11:49:43 -08001654 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1655 if (ret != 0)
1656 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001657 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001658 }
1659
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001660 /* Prepare for a transition to ON or away from ON */
1661 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1662 d->bias_level != SND_SOC_BIAS_ON) ||
1663 (d->target_bias_level != SND_SOC_BIAS_ON &&
1664 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001665 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1666 if (ret != 0)
1667 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001668 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001669 }
1670}
1671
1672/* Async callback run prior to DAPM sequences - brings to their final
1673 * state.
1674 */
1675static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1676{
1677 struct snd_soc_dapm_context *d = data;
1678 int ret;
1679
1680 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001681 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1682 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1683 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001684 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1685 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001686 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001687 ret);
1688 }
1689
1690 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001691 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1692 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001693 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1694 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001695 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1696 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001697
1698 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001699 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001700 }
1701
1702 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001703 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1704 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001705 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1706 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001707 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001708 ret);
1709 }
1710}
Mark Brown97404f22010-12-14 16:13:57 +00001711
Mark Brownfe4fda52011-10-03 22:36:57 +01001712static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1713 bool power, bool connect)
1714{
1715 /* If a connection is being made or broken then that update
1716 * will have marked the peer dirty, otherwise the widgets are
1717 * not connected and this update has no impact. */
1718 if (!connect)
1719 return;
1720
1721 /* If the peer is already in the state we're moving to then we
1722 * won't have an impact on it. */
1723 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001724 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001725}
1726
Mark Brown05623c42011-09-28 17:02:31 +01001727static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1728 struct list_head *up_list,
1729 struct list_head *down_list)
1730{
Mark Browndb432b42011-10-03 21:06:40 +01001731 struct snd_soc_dapm_path *path;
1732
Mark Brown05623c42011-09-28 17:02:31 +01001733 if (w->power == power)
1734 return;
1735
1736 trace_snd_soc_dapm_widget_power(w, power);
1737
Mark Browndb432b42011-10-03 21:06:40 +01001738 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001739 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001740 */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001741 snd_soc_dapm_widget_for_each_source_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001742 dapm_widget_set_peer_power(path->source, power, path->connect);
1743
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001744 /* Supplies can't affect their outputs, only their inputs */
1745 if (!w->is_supply) {
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001746 snd_soc_dapm_widget_for_each_sink_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001747 dapm_widget_set_peer_power(path->sink, power,
1748 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001749 }
1750
Mark Brown05623c42011-09-28 17:02:31 +01001751 if (power)
1752 dapm_seq_insert(w, up_list, true);
1753 else
1754 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001755}
1756
Mark Brown7c81beb2011-09-20 22:22:32 +01001757static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1758 struct list_head *up_list,
1759 struct list_head *down_list)
1760{
Mark Brown7c81beb2011-09-20 22:22:32 +01001761 int power;
1762
1763 switch (w->id) {
1764 case snd_soc_dapm_pre:
1765 dapm_seq_insert(w, down_list, false);
1766 break;
1767 case snd_soc_dapm_post:
1768 dapm_seq_insert(w, up_list, true);
1769 break;
1770
1771 default:
Mark Brownd8050022011-09-28 18:28:23 +01001772 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001773
Mark Brown05623c42011-09-28 17:02:31 +01001774 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001775 break;
1776 }
1777}
1778
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001779static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1780{
1781 if (dapm->idle_bias_off)
1782 return true;
1783
1784 switch (snd_power_get_state(dapm->card->snd_card)) {
1785 case SNDRV_CTL_POWER_D3hot:
1786 case SNDRV_CTL_POWER_D3cold:
1787 return dapm->suspend_bias_off;
1788 default:
1789 break;
1790 }
1791
1792 return false;
1793}
1794
Mark Brown42aa3412009-03-01 19:21:10 +00001795/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001796 * Scan each dapm widget for complete audio path.
1797 * A complete path is a route that has valid endpoints i.e.:-
1798 *
1799 * o DAC to output pin.
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001800 * o Input pin to ADC.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001801 * o Input pin to Output pin (bypass, sidetone)
1802 * o DAC to ADC (loopback).
1803 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001804static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001805{
1806 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001807 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001808 LIST_HEAD(up_list);
1809 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001810 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001811 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001812
Mark Brownf9fa2b12014-03-06 16:49:11 +08001813 lockdep_assert_held(&card->dapm_mutex);
1814
Mark Brown84e90932010-11-04 00:07:02 -04001815 trace_snd_soc_dapm_start(card);
1816
Mark Brown56fba412011-06-04 11:25:10 +01001817 list_for_each_entry(d, &card->dapm_list, list) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001818 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001819 d->target_bias_level = SND_SOC_BIAS_OFF;
1820 else
1821 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001822 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001823
Liam Girdwood6c120e12012-02-15 15:15:34 +00001824 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001825
Mark Brown6d3ddc82009-05-16 17:47:29 +01001826 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001827 * lists indicating if they should be powered up or down. We
1828 * only check widgets that have been flagged as dirty but note
1829 * that new widgets may be added to the dirty list while we
1830 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001831 */
Mark Browndb432b42011-10-03 21:06:40 +01001832 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001833 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001834 }
1835
Mark Brownf9de6d72011-09-28 17:19:47 +01001836 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001837 switch (w->id) {
1838 case snd_soc_dapm_pre:
1839 case snd_soc_dapm_post:
1840 /* These widgets always need to be powered */
1841 break;
1842 default:
1843 list_del_init(&w->dirty);
1844 break;
1845 }
Mark Browndb432b42011-10-03 21:06:40 +01001846
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001847 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001848 d = w->dapm;
1849
1850 /* Supplies and micbiases only bring the
1851 * context up to STANDBY as unless something
1852 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001853 * generally don't require full power. Signal
1854 * generators are virtual pins and have no
1855 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001856 */
1857 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001858 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001859 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001860 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001861 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001862 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001863 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001864 case snd_soc_dapm_micbias:
1865 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1866 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1867 break;
1868 default:
1869 d->target_bias_level = SND_SOC_BIAS_ON;
1870 break;
1871 }
1872 }
1873
1874 }
1875
Mark Brown85a843c2011-09-21 21:29:47 +01001876 /* Force all contexts in the card to the same bias state if
1877 * they're not ground referenced.
1878 */
Mark Brown56fba412011-06-04 11:25:10 +01001879 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001880 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001881 if (d->target_bias_level > bias)
1882 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001883 list_for_each_entry(d, &card->dapm_list, list)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001884 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01001885 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001886
Mark Brownde02d072011-09-20 21:43:24 +01001887 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001888
Xiang Xiao17282ba2014-03-02 00:04:03 +08001889 /* Run card bias changes at first */
1890 dapm_pre_sequence_async(&card->dapm, 0);
1891 /* Run other bias changes in parallel */
1892 list_for_each_entry(d, &card->dapm_list, list) {
1893 if (d != &card->dapm)
1894 async_schedule_domain(dapm_pre_sequence_async, d,
1895 &async_domain);
1896 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001897 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001898
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001899 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001900 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001901 }
1902
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001903 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001904 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001905 }
1906
Mark Brown6d3ddc82009-05-16 17:47:29 +01001907 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001908 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001909
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001910 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001911
Mark Brown6d3ddc82009-05-16 17:47:29 +01001912 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001913 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001914
Mark Brown9d0624a2011-02-18 11:49:43 -08001915 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001916 list_for_each_entry(d, &card->dapm_list, list) {
1917 if (d != &card->dapm)
1918 async_schedule_domain(dapm_post_sequence_async, d,
1919 &async_domain);
1920 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001921 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001922 /* Run card bias changes at last */
1923 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001924
Liam Girdwood8078d872012-02-15 15:15:35 +00001925 /* do we need to notify any clients that DAPM event is complete */
1926 list_for_each_entry(d, &card->dapm_list, list) {
1927 if (d->stream_event)
1928 d->stream_event(d, event);
1929 }
1930
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001931 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001932 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001933 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001934
Mark Brown84e90932010-11-04 00:07:02 -04001935 trace_snd_soc_dapm_done(card);
1936
Mark Brown42aa3412009-03-01 19:21:10 +00001937 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001938}
1939
Mark Brown79fb9382009-08-21 16:38:13 +01001940#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001941static ssize_t dapm_widget_power_read_file(struct file *file,
1942 char __user *user_buf,
1943 size_t count, loff_t *ppos)
1944{
1945 struct snd_soc_dapm_widget *w = file->private_data;
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02001946 struct snd_soc_card *card = w->dapm->card;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001947 enum snd_soc_dapm_direction dir, rdir;
Mark Brown79fb9382009-08-21 16:38:13 +01001948 char *buf;
1949 int in, out;
1950 ssize_t ret;
1951 struct snd_soc_dapm_path *p = NULL;
1952
1953 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1954 if (!buf)
1955 return -ENOMEM;
1956
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02001957 mutex_lock(&card->dapm_mutex);
1958
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001959 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
1960 if (w->is_supply) {
1961 in = 0;
1962 out = 0;
1963 } else {
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001964 in = is_connected_input_ep(w, NULL, NULL);
1965 out = is_connected_output_ep(w, NULL, NULL);
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001966 }
Mark Brown79fb9382009-08-21 16:38:13 +01001967
Mark Brownf13ebad2012-03-03 18:01:01 +00001968 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1969 w->name, w->power ? "On" : "Off",
1970 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001971
Mark Brownd033c362009-12-04 15:25:56 +00001972 if (w->reg >= 0)
1973 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001974 " - R%d(0x%x) mask 0x%x",
1975 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001976
1977 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1978
Mark Brown3eef08b2009-09-14 16:49:00 +01001979 if (w->sname)
1980 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1981 w->sname,
1982 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001983
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001984 snd_soc_dapm_for_each_direction(dir) {
1985 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1986 snd_soc_dapm_widget_for_each_path(w, dir, p) {
1987 if (p->connected && !p->connected(w, p->node[rdir]))
1988 continue;
Mark Brown215edda2009-09-08 18:59:05 +01001989
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001990 if (!p->connect)
1991 continue;
Mark Brown215edda2009-09-08 18:59:05 +01001992
Mark Brown79fb9382009-08-21 16:38:13 +01001993 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001994 " %s \"%s\" \"%s\"\n",
1995 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
Mark Brown79fb9382009-08-21 16:38:13 +01001996 p->name ? p->name : "static",
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001997 p->node[rdir]->name);
1998 }
Mark Brown79fb9382009-08-21 16:38:13 +01001999 }
2000
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002001 mutex_unlock(&card->dapm_mutex);
2002
Mark Brown79fb9382009-08-21 16:38:13 +01002003 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2004
2005 kfree(buf);
2006 return ret;
2007}
2008
2009static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002010 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002011 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002012 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002013};
2014
Mark Brownef49e4f2011-04-04 20:48:13 +09002015static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2016 size_t count, loff_t *ppos)
2017{
2018 struct snd_soc_dapm_context *dapm = file->private_data;
2019 char *level;
2020
2021 switch (dapm->bias_level) {
2022 case SND_SOC_BIAS_ON:
2023 level = "On\n";
2024 break;
2025 case SND_SOC_BIAS_PREPARE:
2026 level = "Prepare\n";
2027 break;
2028 case SND_SOC_BIAS_STANDBY:
2029 level = "Standby\n";
2030 break;
2031 case SND_SOC_BIAS_OFF:
2032 level = "Off\n";
2033 break;
2034 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002035 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002036 level = "Unknown\n";
2037 break;
2038 }
2039
2040 return simple_read_from_buffer(user_buf, count, ppos, level,
2041 strlen(level));
2042}
2043
2044static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002045 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002046 .read = dapm_bias_read_file,
2047 .llseek = default_llseek,
2048};
2049
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002050void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2051 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002052{
Mark Brown79fb9382009-08-21 16:38:13 +01002053 struct dentry *d;
2054
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02002055 if (!parent)
2056 return;
2057
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002058 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2059
2060 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002061 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002062 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002063 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002064 }
Mark Brown79fb9382009-08-21 16:38:13 +01002065
Mark Brownef49e4f2011-04-04 20:48:13 +09002066 d = debugfs_create_file("bias_level", 0444,
2067 dapm->debugfs_dapm, dapm,
2068 &dapm_bias_fops);
2069 if (!d)
2070 dev_warn(dapm->dev,
2071 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002072}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002073
2074static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2075{
2076 struct snd_soc_dapm_context *dapm = w->dapm;
2077 struct dentry *d;
2078
2079 if (!dapm->debugfs_dapm || !w->name)
2080 return;
2081
2082 d = debugfs_create_file(w->name, 0444,
2083 dapm->debugfs_dapm, w,
2084 &dapm_widget_power_fops);
2085 if (!d)
2086 dev_warn(w->dapm->dev,
2087 "ASoC: Failed to create %s debugfs file\n",
2088 w->name);
2089}
2090
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002091static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2092{
2093 debugfs_remove_recursive(dapm->debugfs_dapm);
2094}
2095
Mark Brown79fb9382009-08-21 16:38:13 +01002096#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002097void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2098 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002099{
2100}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002101
2102static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2103{
2104}
2105
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002106static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2107{
2108}
2109
Mark Brown79fb9382009-08-21 16:38:13 +01002110#endif
2111
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002112/*
2113 * soc_dapm_connect_path() - Connects or disconnects a path
2114 * @path: The path to update
2115 * @connect: The new connect state of the path. True if the path is connected,
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002116 * false if it is disconnected.
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002117 * @reason: The reason why the path changed (for debugging only)
2118 */
2119static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2120 bool connect, const char *reason)
2121{
2122 if (path->connect == connect)
2123 return;
2124
2125 path->connect = connect;
2126 dapm_mark_dirty(path->source, reason);
2127 dapm_mark_dirty(path->sink, reason);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002128 dapm_path_invalidate(path);
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002129}
2130
Richard Purdie2b97eab2006-10-06 18:32:18 +02002131/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002132static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002133 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002134{
2135 struct snd_soc_dapm_path *path;
2136 int found = 0;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002137 bool connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002138
Mark Brownf9fa2b12014-03-06 16:49:11 +08002139 lockdep_assert_held(&card->dapm_mutex);
2140
Richard Purdie2b97eab2006-10-06 18:32:18 +02002141 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002142 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002143 found = 1;
2144 /* we now need to match the string in the enum to the path */
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002145 if (!(strcmp(path->name, e->texts[mux])))
2146 connect = true;
2147 else
2148 connect = false;
2149
2150 soc_dapm_connect_path(path, connect, "mux update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002151 }
2152
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002153 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002154 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002155
Liam Girdwood618dae12012-04-25 12:12:51 +01002156 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002157}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002158
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002159int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002160 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2161 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002162{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002163 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002164 int ret;
2165
Liam Girdwood3cd04342012-03-09 12:02:08 +00002166 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002167 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002168 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002169 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002170 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002171 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002172 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002173 return ret;
2174}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002175EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002176
Milan plzik1b075e32008-01-10 14:39:46 +01002177/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002178static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002179 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002180{
2181 struct snd_soc_dapm_path *path;
2182 int found = 0;
2183
Mark Brownf9fa2b12014-03-06 16:49:11 +08002184 lockdep_assert_held(&card->dapm_mutex);
2185
Richard Purdie2b97eab2006-10-06 18:32:18 +02002186 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002187 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002188 found = 1;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002189 soc_dapm_connect_path(path, connect, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002190 }
2191
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002192 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002193 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002194
Liam Girdwood618dae12012-04-25 12:12:51 +01002195 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002196}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002197
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002198int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002199 struct snd_kcontrol *kcontrol, int connect,
2200 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002201{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002202 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002203 int ret;
2204
Liam Girdwood3cd04342012-03-09 12:02:08 +00002205 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002206 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002207 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002208 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002209 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002210 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002211 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002212 return ret;
2213}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002214EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002215
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002216static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2217 char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002218{
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002219 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002220 struct snd_soc_dapm_widget *w;
2221 int count = 0;
2222 char *state = "not set";
2223
Mark Brown47325072016-03-18 12:04:23 +00002224 /* card won't be set for the dummy component, as a spot fix
2225 * we're checking for that case specifically here but in future
2226 * we will ensure that the dummy component looks like others.
2227 */
2228 if (!cmpnt->card)
2229 return 0;
2230
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002231 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2232 if (w->dapm != dapm)
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002233 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002234
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002235 /* only display widgets that burn power */
Richard Purdie2b97eab2006-10-06 18:32:18 +02002236 switch (w->id) {
2237 case snd_soc_dapm_hp:
2238 case snd_soc_dapm_mic:
2239 case snd_soc_dapm_spk:
2240 case snd_soc_dapm_line:
2241 case snd_soc_dapm_micbias:
2242 case snd_soc_dapm_dac:
2243 case snd_soc_dapm_adc:
2244 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002245 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002246 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002247 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002248 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002249 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002250 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002251 if (w->name)
2252 count += sprintf(buf + count, "%s: %s\n",
2253 w->name, w->power ? "On":"Off");
2254 break;
2255 default:
2256 break;
2257 }
2258 }
2259
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002260 switch (snd_soc_dapm_get_bias_level(dapm)) {
Mark Brown0be98982008-05-19 12:31:28 +02002261 case SND_SOC_BIAS_ON:
2262 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002263 break;
Mark Brown0be98982008-05-19 12:31:28 +02002264 case SND_SOC_BIAS_PREPARE:
2265 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002266 break;
Mark Brown0be98982008-05-19 12:31:28 +02002267 case SND_SOC_BIAS_STANDBY:
2268 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002269 break;
Mark Brown0be98982008-05-19 12:31:28 +02002270 case SND_SOC_BIAS_OFF:
2271 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002272 break;
2273 }
2274 count += sprintf(buf + count, "PM State: %s\n", state);
2275
2276 return count;
2277}
2278
Benoit Cousson44ba2642014-07-08 23:19:36 +02002279/* show dapm widget status in sys fs */
2280static ssize_t dapm_widget_show(struct device *dev,
2281 struct device_attribute *attr, char *buf)
2282{
2283 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2284 int i, count = 0;
2285
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002286 mutex_lock(&rtd->card->dapm_mutex);
2287
Benoit Cousson44ba2642014-07-08 23:19:36 +02002288 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002289 struct snd_soc_component *cmpnt = rtd->codec_dais[i]->component;
2290
2291 count += dapm_widget_show_component(cmpnt, buf + count);
Benoit Cousson44ba2642014-07-08 23:19:36 +02002292 }
2293
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002294 mutex_unlock(&rtd->card->dapm_mutex);
2295
Benoit Cousson44ba2642014-07-08 23:19:36 +02002296 return count;
2297}
2298
Richard Purdie2b97eab2006-10-06 18:32:18 +02002299static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2300
Takashi Iwaid29697d2015-01-30 20:16:37 +01002301struct attribute *soc_dapm_dev_attrs[] = {
2302 &dev_attr_dapm_widget.attr,
2303 NULL
2304};
Richard Purdie2b97eab2006-10-06 18:32:18 +02002305
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002306static void dapm_free_path(struct snd_soc_dapm_path *path)
2307{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002308 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2309 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002310 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002311 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002312 kfree(path);
2313}
2314
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002315void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2316{
2317 struct snd_soc_dapm_path *p, *next_p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002318 enum snd_soc_dapm_direction dir;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002319
2320 list_del(&w->list);
2321 /*
2322 * remove source and sink paths associated to this widget.
2323 * While removing the path, remove reference to it from both
2324 * source and sink widgets so that path is removed only once.
2325 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002326 snd_soc_dapm_for_each_direction(dir) {
2327 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2328 dapm_free_path(p);
2329 }
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002330
2331 kfree(w->kcontrols);
Lars-Peter Clausen48068962015-07-21 18:11:08 +02002332 kfree_const(w->name);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002333 kfree(w);
2334}
2335
Jyri Sarhafd589a12015-11-10 18:12:42 +02002336void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2337{
2338 dapm->path_sink_cache.widget = NULL;
2339 dapm->path_source_cache.widget = NULL;
2340}
2341
Richard Purdie2b97eab2006-10-06 18:32:18 +02002342/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002343static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002344{
2345 struct snd_soc_dapm_widget *w, *next_w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002346
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002347 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2348 if (w->dapm != dapm)
2349 continue;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002350 snd_soc_dapm_free_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002351 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002352 snd_soc_dapm_reset_cache(dapm);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002353}
2354
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002355static struct snd_soc_dapm_widget *dapm_find_widget(
2356 struct snd_soc_dapm_context *dapm, const char *pin,
2357 bool search_other_contexts)
2358{
2359 struct snd_soc_dapm_widget *w;
2360 struct snd_soc_dapm_widget *fallback = NULL;
2361
2362 list_for_each_entry(w, &dapm->card->widgets, list) {
2363 if (!strcmp(w->name, pin)) {
2364 if (w->dapm == dapm)
2365 return w;
2366 else
2367 fallback = w;
2368 }
2369 }
2370
2371 if (search_other_contexts)
2372 return fallback;
2373
2374 return NULL;
2375}
2376
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002377static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002378 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002379{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002380 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002381
Mark Brownf9fa2b12014-03-06 16:49:11 +08002382 dapm_assert_locked(dapm);
2383
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002384 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002385 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002386 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002387 }
2388
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002389 if (w->connected != status) {
Mark Brown1a8b2d92012-02-16 11:50:07 -08002390 dapm_mark_dirty(w, "pin configuration");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002391 dapm_widget_invalidate_input_paths(w);
2392 dapm_widget_invalidate_output_paths(w);
2393 }
Mark Brown1a8b2d92012-02-16 11:50:07 -08002394
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002395 w->connected = status;
2396 if (status == 0)
2397 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002398
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002399 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002400}
2401
Richard Purdie2b97eab2006-10-06 18:32:18 +02002402/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002403 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2404 * @dapm: DAPM context
2405 *
2406 * Walks all dapm audio paths and powers widgets according to their
2407 * stream or path usage.
2408 *
2409 * Requires external locking.
2410 *
2411 * Returns 0 for success.
2412 */
2413int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2414{
2415 /*
2416 * Suppress early reports (eg, jacks syncing their state) to avoid
2417 * silly DAPM runs during card startup.
2418 */
2419 if (!dapm->card || !dapm->card->instantiated)
2420 return 0;
2421
2422 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2423}
2424EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2425
2426/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002427 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002428 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002429 *
2430 * Walks all dapm audio paths and powers widgets according to their
2431 * stream or path usage.
2432 *
2433 * Returns 0 for success.
2434 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002435int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002436{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002437 int ret;
2438
Liam Girdwood3cd04342012-03-09 12:02:08 +00002439 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002440 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002441 mutex_unlock(&dapm->card->dapm_mutex);
2442 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002443}
Liam Girdwooda5302182008-07-07 13:35:17 +01002444EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002445
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002446/*
2447 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2448 * @w: The widget for which to update the flags
2449 *
2450 * Some widgets have a dynamic category which depends on which neighbors they
2451 * are connected to. This function update the category for these widgets.
2452 *
2453 * This function must be called whenever a path is added or removed to a widget.
2454 */
2455static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2456{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002457 enum snd_soc_dapm_direction dir;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002458 struct snd_soc_dapm_path *p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002459 unsigned int ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002460
2461 switch (w->id) {
2462 case snd_soc_dapm_input:
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002463 /* On a fully routed card an input is never a source */
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002464 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002465 return;
2466 ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002467 snd_soc_dapm_widget_for_each_source_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002468 if (p->source->id == snd_soc_dapm_micbias ||
2469 p->source->id == snd_soc_dapm_mic ||
2470 p->source->id == snd_soc_dapm_line ||
2471 p->source->id == snd_soc_dapm_output) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002472 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002473 break;
2474 }
2475 }
2476 break;
2477 case snd_soc_dapm_output:
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002478 /* On a fully routed card a output is never a sink */
2479 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002480 return;
2481 ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002482 snd_soc_dapm_widget_for_each_sink_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002483 if (p->sink->id == snd_soc_dapm_spk ||
2484 p->sink->id == snd_soc_dapm_hp ||
2485 p->sink->id == snd_soc_dapm_line ||
2486 p->sink->id == snd_soc_dapm_input) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002487 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002488 break;
2489 }
2490 }
2491 break;
2492 case snd_soc_dapm_line:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002493 ep = 0;
2494 snd_soc_dapm_for_each_direction(dir) {
2495 if (!list_empty(&w->edges[dir]))
2496 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2497 }
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002498 break;
2499 default:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002500 return;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002501 }
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002502
2503 w->is_ep = ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002504}
2505
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002506static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2507 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2508 const char *control)
2509{
2510 bool dynamic_source = false;
2511 bool dynamic_sink = false;
2512
2513 if (!control)
2514 return 0;
2515
2516 switch (source->id) {
2517 case snd_soc_dapm_demux:
2518 dynamic_source = true;
2519 break;
2520 default:
2521 break;
2522 }
2523
2524 switch (sink->id) {
2525 case snd_soc_dapm_mux:
2526 case snd_soc_dapm_switch:
2527 case snd_soc_dapm_mixer:
2528 case snd_soc_dapm_mixer_named_ctl:
2529 dynamic_sink = true;
2530 break;
2531 default:
2532 break;
2533 }
2534
2535 if (dynamic_source && dynamic_sink) {
2536 dev_err(dapm->dev,
2537 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2538 source->name, control, sink->name);
2539 return -EINVAL;
2540 } else if (!dynamic_source && !dynamic_sink) {
2541 dev_err(dapm->dev,
2542 "Control not supported for path %s -> [%s] -> %s\n",
2543 source->name, control, sink->name);
2544 return -EINVAL;
2545 }
2546
2547 return 0;
2548}
2549
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002550static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2551 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2552 const char *control,
2553 int (*connected)(struct snd_soc_dapm_widget *source,
2554 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002555{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002556 struct snd_soc_dapm_widget *widgets[2];
2557 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002558 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002559 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002560
Lars-Peter Clausene409dfb2014-10-25 17:42:02 +02002561 if (wsink->is_supply && !wsource->is_supply) {
2562 dev_err(dapm->dev,
2563 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2564 wsource->name, wsink->name);
2565 return -EINVAL;
2566 }
2567
2568 if (connected && !wsource->is_supply) {
2569 dev_err(dapm->dev,
2570 "connected() callback only supported for supply widgets (%s -> %s)\n",
2571 wsource->name, wsink->name);
2572 return -EINVAL;
2573 }
2574
2575 if (wsource->is_supply && control) {
2576 dev_err(dapm->dev,
2577 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2578 wsource->name, control, wsink->name);
2579 return -EINVAL;
2580 }
2581
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002582 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2583 if (ret)
2584 return ret;
2585
Richard Purdie2b97eab2006-10-06 18:32:18 +02002586 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2587 if (!path)
2588 return -ENOMEM;
2589
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002590 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2591 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2592 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2593 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2594
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002595 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002596 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002597 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002598
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002599 if (wsource->is_supply || wsink->is_supply)
2600 path->is_supply = 1;
2601
Richard Purdie2b97eab2006-10-06 18:32:18 +02002602 /* connect static paths */
2603 if (control == NULL) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002604 path->connect = 1;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002605 } else {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002606 switch (wsource->id) {
2607 case snd_soc_dapm_demux:
2608 ret = dapm_connect_mux(dapm, path, control, wsource);
2609 if (ret)
2610 goto err;
2611 break;
2612 default:
2613 break;
2614 }
2615
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002616 switch (wsink->id) {
2617 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002618 ret = dapm_connect_mux(dapm, path, control, wsink);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002619 if (ret != 0)
2620 goto err;
2621 break;
2622 case snd_soc_dapm_switch:
2623 case snd_soc_dapm_mixer:
2624 case snd_soc_dapm_mixer_named_ctl:
2625 ret = dapm_connect_mixer(dapm, path, control);
2626 if (ret != 0)
2627 goto err;
2628 break;
2629 default:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002630 break;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002631 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002632 }
2633
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002634 list_add(&path->list, &dapm->card->paths);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002635 snd_soc_dapm_for_each_direction(dir)
2636 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002637
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002638 snd_soc_dapm_for_each_direction(dir) {
2639 dapm_update_widget_flags(widgets[dir]);
2640 dapm_mark_dirty(widgets[dir], "Route added");
2641 }
Mark Brownfabd0382012-07-05 17:20:06 +01002642
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002643 if (dapm->card->instantiated && path->connect)
2644 dapm_path_invalidate(path);
2645
Richard Purdie2b97eab2006-10-06 18:32:18 +02002646 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002647err:
2648 kfree(path);
2649 return ret;
2650}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002651
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002652static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002653 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002654{
2655 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2656 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2657 const char *sink;
2658 const char *source;
2659 char prefixed_sink[80];
2660 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002661 const char *prefix;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002662 int ret;
2663
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002664 prefix = soc_dapm_prefix(dapm);
2665 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002666 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002667 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002668 sink = prefixed_sink;
2669 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002670 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002671 source = prefixed_source;
2672 } else {
2673 sink = route->sink;
2674 source = route->source;
2675 }
2676
Charles Keepax45a110a2015-05-11 13:50:30 +01002677 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2678 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2679
2680 if (wsink && wsource)
2681 goto skip_search;
2682
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002683 /*
2684 * find src and dest widgets over all widgets but favor a widget from
2685 * current DAPM context
2686 */
2687 list_for_each_entry(w, &dapm->card->widgets, list) {
2688 if (!wsink && !(strcmp(w->name, sink))) {
2689 wtsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002690 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002691 wsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002692 if (wsource)
2693 break;
2694 }
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002695 continue;
2696 }
2697 if (!wsource && !(strcmp(w->name, source))) {
2698 wtsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002699 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002700 wsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002701 if (wsink)
2702 break;
2703 }
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002704 }
2705 }
2706 /* use widget from another DAPM context if not found from this */
2707 if (!wsink)
2708 wsink = wtsink;
2709 if (!wsource)
2710 wsource = wtsource;
2711
2712 if (wsource == NULL) {
2713 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2714 route->source);
2715 return -ENODEV;
2716 }
2717 if (wsink == NULL) {
2718 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2719 route->sink);
2720 return -ENODEV;
2721 }
2722
Charles Keepax45a110a2015-05-11 13:50:30 +01002723skip_search:
2724 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2725 dapm_wcache_update(&dapm->path_source_cache, wsource);
2726
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002727 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2728 route->connected);
2729 if (ret)
2730 goto err;
2731
2732 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002733err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002734 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002735 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002736 return ret;
2737}
Mark Brown105f1c22008-05-13 14:52:19 +02002738
Mark Brownefcc3c62012-07-05 17:24:19 +01002739static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2740 const struct snd_soc_dapm_route *route)
2741{
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002742 struct snd_soc_dapm_widget *wsource, *wsink;
Mark Brownefcc3c62012-07-05 17:24:19 +01002743 struct snd_soc_dapm_path *path, *p;
2744 const char *sink;
2745 const char *source;
2746 char prefixed_sink[80];
2747 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002748 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002749
2750 if (route->control) {
2751 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002752 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002753 return -EINVAL;
2754 }
2755
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002756 prefix = soc_dapm_prefix(dapm);
2757 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002758 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002759 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002760 sink = prefixed_sink;
2761 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002762 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002763 source = prefixed_source;
2764 } else {
2765 sink = route->sink;
2766 source = route->source;
2767 }
2768
2769 path = NULL;
2770 list_for_each_entry(p, &dapm->card->paths, list) {
2771 if (strcmp(p->source->name, source) != 0)
2772 continue;
2773 if (strcmp(p->sink->name, sink) != 0)
2774 continue;
2775 path = p;
2776 break;
2777 }
2778
2779 if (path) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002780 wsource = path->source;
2781 wsink = path->sink;
2782
2783 dapm_mark_dirty(wsource, "Route removed");
2784 dapm_mark_dirty(wsink, "Route removed");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002785 if (path->connect)
2786 dapm_path_invalidate(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002787
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002788 dapm_free_path(path);
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002789
2790 /* Update any path related flags */
2791 dapm_update_widget_flags(wsource);
2792 dapm_update_widget_flags(wsink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002793 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002794 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002795 source, sink);
2796 }
2797
2798 return 0;
2799}
2800
Mark Brown105f1c22008-05-13 14:52:19 +02002801/**
Mark Brown105f1c22008-05-13 14:52:19 +02002802 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002803 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002804 * @route: audio routes
2805 * @num: number of routes
2806 *
2807 * Connects 2 dapm widgets together via a named audio path. The sink is
2808 * the widget receiving the audio signal, whilst the source is the sender
2809 * of the audio signal.
2810 *
2811 * Returns 0 for success else error. On error all resources can be freed
2812 * with a call to snd_soc_card_free().
2813 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002814int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002815 const struct snd_soc_dapm_route *route, int num)
2816{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002817 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002818
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002819 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002820 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002821 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002822 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002823 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2824 route->source,
2825 route->control ? route->control : "direct",
2826 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002827 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002828 }
2829 route++;
2830 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002831 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002832
Dan Carpenter60884c22012-04-13 22:25:43 +03002833 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002834}
2835EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2836
Mark Brownefcc3c62012-07-05 17:24:19 +01002837/**
2838 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2839 * @dapm: DAPM context
2840 * @route: audio routes
2841 * @num: number of routes
2842 *
2843 * Removes routes from the DAPM context.
2844 */
2845int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2846 const struct snd_soc_dapm_route *route, int num)
2847{
Rajan Vajae066ea22016-02-11 11:23:35 +05302848 int i;
Mark Brownefcc3c62012-07-05 17:24:19 +01002849
2850 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2851 for (i = 0; i < num; i++) {
2852 snd_soc_dapm_del_route(dapm, route);
2853 route++;
2854 }
2855 mutex_unlock(&dapm->card->dapm_mutex);
2856
Rajan Vajae066ea22016-02-11 11:23:35 +05302857 return 0;
Mark Brownefcc3c62012-07-05 17:24:19 +01002858}
2859EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2860
Mark Brownbf3a9e12011-06-13 16:42:29 +01002861static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2862 const struct snd_soc_dapm_route *route)
2863{
2864 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2865 route->source,
2866 true);
2867 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2868 route->sink,
2869 true);
2870 struct snd_soc_dapm_path *path;
2871 int count = 0;
2872
2873 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002874 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002875 route->source);
2876 return -ENODEV;
2877 }
2878
2879 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002880 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002881 route->sink);
2882 return -ENODEV;
2883 }
2884
2885 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002886 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002887 route->source, route->sink);
2888
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002889 snd_soc_dapm_widget_for_each_sink_path(source, path) {
Mark Brownbf3a9e12011-06-13 16:42:29 +01002890 if (path->sink == sink) {
2891 path->weak = 1;
2892 count++;
2893 }
2894 }
2895
2896 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002897 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002898 route->source, route->sink);
2899 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002900 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002901 count, route->source, route->sink);
2902
2903 return 0;
2904}
2905
2906/**
2907 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2908 * @dapm: DAPM context
2909 * @route: audio routes
2910 * @num: number of routes
2911 *
2912 * Mark existing routes matching those specified in the passed array
2913 * as being weak, meaning that they are ignored for the purpose of
2914 * power decisions. The main intended use case is for sidetone paths
2915 * which couple audio between other independent paths if they are both
2916 * active in order to make the combination work better at the user
2917 * level but which aren't intended to be "used".
2918 *
2919 * Note that CODEC drivers should not use this as sidetone type paths
2920 * can frequently also be used as bypass paths.
2921 */
2922int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2923 const struct snd_soc_dapm_route *route, int num)
2924{
2925 int i, err;
2926 int ret = 0;
2927
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002928 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002929 for (i = 0; i < num; i++) {
2930 err = snd_soc_dapm_weak_route(dapm, route);
2931 if (err)
2932 ret = err;
2933 route++;
2934 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002935 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002936
2937 return ret;
2938}
2939EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2940
Mark Brown105f1c22008-05-13 14:52:19 +02002941/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002942 * snd_soc_dapm_new_widgets - add new dapm widgets
Jonathan Corbet628536e2015-08-25 01:14:48 -06002943 * @card: card to be checked for new dapm widgets
Richard Purdie2b97eab2006-10-06 18:32:18 +02002944 *
2945 * Checks the codec for any new dapm widgets and creates them if found.
2946 *
2947 * Returns 0 for success.
2948 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002949int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002950{
2951 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002952 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002953
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002954 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002955
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002956 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002957 {
2958 if (w->new)
2959 continue;
2960
Stephen Warrenfad59882011-04-28 17:37:59 -06002961 if (w->num_kcontrols) {
2962 w->kcontrols = kzalloc(w->num_kcontrols *
2963 sizeof(struct snd_kcontrol *),
2964 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002965 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002966 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002967 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002968 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002969 }
2970
Richard Purdie2b97eab2006-10-06 18:32:18 +02002971 switch(w->id) {
2972 case snd_soc_dapm_switch:
2973 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002974 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002975 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002976 break;
2977 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002978 case snd_soc_dapm_demux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002979 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002980 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002981 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002982 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002983 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002984 break;
Nikesh Oswalc6615082015-02-02 17:06:44 +00002985 case snd_soc_dapm_dai_link:
2986 dapm_new_dai_link(w);
2987 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002988 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002989 break;
2990 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002991
2992 /* Read the initial power state from the device */
2993 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002994 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002995 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002996 val &= w->mask;
2997 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002998 w->power = 1;
2999 }
3000
Richard Purdie2b97eab2006-10-06 18:32:18 +02003001 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003002
Mark Brown7508b122011-10-05 12:09:12 +01003003 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003004 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003005 }
3006
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003007 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3008 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003009 return 0;
3010}
3011EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3012
3013/**
3014 * snd_soc_dapm_get_volsw - dapm mixer get callback
3015 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003016 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003017 *
3018 * Callback to get the value of a dapm mixer control.
3019 *
3020 * Returns 0 for success.
3021 */
3022int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3023 struct snd_ctl_elem_value *ucontrol)
3024{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003025 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3026 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003027 struct soc_mixer_control *mc =
3028 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003029 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003030 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003031 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003032 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003033 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003034 unsigned int val;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003035 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003036
3037 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003038 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003039 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003040 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003041
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003042 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003043 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3044 ret = soc_dapm_read(dapm, reg, &val);
3045 val = (val >> shift) & mask;
3046 } else {
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003047 val = dapm_kcontrol_get_value(kcontrol);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003048 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003049 mutex_unlock(&card->dapm_mutex);
3050
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003051 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003052 ucontrol->value.integer.value[0] = max - val;
3053 else
3054 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003055
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003056 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003057}
3058EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3059
3060/**
3061 * snd_soc_dapm_put_volsw - dapm mixer set callback
3062 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003063 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003064 *
3065 * Callback to set the value of a dapm mixer control.
3066 *
3067 * Returns 0 for success.
3068 */
3069int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3070 struct snd_ctl_elem_value *ucontrol)
3071{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003072 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3073 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003074 struct soc_mixer_control *mc =
3075 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003076 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003077 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003078 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003079 unsigned int mask = (1 << fls(max)) - 1;
3080 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07003081 unsigned int val;
Jarkko Nikula18626c72014-06-09 14:20:29 +03003082 int connect, change, reg_change = 0;
Mark Brown97404f22010-12-14 16:13:57 +00003083 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08003084 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003085
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003086 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003087 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003088 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003089 kcontrol->id.name);
3090
Richard Purdie2b97eab2006-10-06 18:32:18 +02003091 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02003092 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003093
3094 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003095 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003096
Liam Girdwood3cd04342012-03-09 12:02:08 +00003097 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003098
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003099 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown283375c2009-12-07 18:09:03 +00003100
Jarkko Nikula18626c72014-06-09 14:20:29 +03003101 if (reg != SND_SOC_NOPM) {
3102 mask = mask << shift;
3103 val = val << shift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02003104
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003105 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
Jarkko Nikula18626c72014-06-09 14:20:29 +03003106 }
3107
3108 if (change || reg_change) {
3109 if (reg_change) {
3110 update.kcontrol = kcontrol;
3111 update.reg = reg;
3112 update.mask = mask;
3113 update.val = val;
3114 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003115 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003116 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00003117
Nenghua Cao52765972013-12-13 20:13:49 +08003118 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00003119
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003120 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00003121 }
3122
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003123 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003124
3125 if (ret > 0)
3126 soc_dpcm_runtime_update(card);
3127
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003128 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003129}
3130EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3131
3132/**
3133 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3134 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003135 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003136 *
3137 * Callback to get the value of a dapm enumerated double mixer control.
3138 *
3139 * Returns 0 for success.
3140 */
3141int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3142 struct snd_ctl_elem_value *ucontrol)
3143{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003144 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Charles Keepax561ed682015-05-01 12:37:26 +01003145 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003146 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003147 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003148
Charles Keepax561ed682015-05-01 12:37:26 +01003149 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3150 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003151 int ret = soc_dapm_read(dapm, e->reg, &reg_val);
Charles Keepax964a0b82015-05-08 10:50:10 +01003152 if (ret) {
3153 mutex_unlock(&card->dapm_mutex);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003154 return ret;
Charles Keepax964a0b82015-05-08 10:50:10 +01003155 }
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003156 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003157 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003158 }
Charles Keepax561ed682015-05-01 12:37:26 +01003159 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003160
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003161 val = (reg_val >> e->shift_l) & e->mask;
3162 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3163 if (e->shift_l != e->shift_r) {
3164 val = (reg_val >> e->shift_r) & e->mask;
3165 val = snd_soc_enum_val_to_item(e, val);
3166 ucontrol->value.enumerated.item[1] = val;
3167 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003168
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003169 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003170}
3171EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3172
3173/**
3174 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3175 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003176 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003177 *
3178 * Callback to set the value of a dapm enumerated double mixer control.
3179 *
3180 * Returns 0 for success.
3181 */
3182int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3183 struct snd_ctl_elem_value *ucontrol)
3184{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003185 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3186 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003187 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003188 unsigned int *item = ucontrol->value.enumerated.item;
Charles Keepax561ed682015-05-01 12:37:26 +01003189 unsigned int val, change, reg_change = 0;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003190 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00003191 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08003192 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003193
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003194 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003195 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003196
3197 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003198 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003199 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003200 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003201 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003202 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003203 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003204 }
3205
Liam Girdwood3cd04342012-03-09 12:02:08 +00003206 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003207
Charles Keepax561ed682015-05-01 12:37:26 +01003208 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00003209
Charles Keepax561ed682015-05-01 12:37:26 +01003210 if (e->reg != SND_SOC_NOPM)
3211 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3212
3213 if (change || reg_change) {
3214 if (reg_change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003215 update.kcontrol = kcontrol;
3216 update.reg = e->reg;
3217 update.mask = mask;
3218 update.val = val;
3219 card->update = &update;
3220 }
Charles Keepax561ed682015-05-01 12:37:26 +01003221 change |= reg_change;
Mark Brown3a655772009-10-05 17:23:30 +01003222
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003223 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003224
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003225 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003226 }
3227
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003228 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003229
3230 if (ret > 0)
3231 soc_dpcm_runtime_update(card);
3232
Mark Brown97404f22010-12-14 16:13:57 +00003233 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003234}
3235EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3236
3237/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003238 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3239 *
3240 * @kcontrol: mixer control
3241 * @uinfo: control element information
3242 *
3243 * Callback to provide information about a pin switch control.
3244 */
3245int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3246 struct snd_ctl_elem_info *uinfo)
3247{
3248 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3249 uinfo->count = 1;
3250 uinfo->value.integer.min = 0;
3251 uinfo->value.integer.max = 1;
3252
3253 return 0;
3254}
3255EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3256
3257/**
3258 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3259 *
3260 * @kcontrol: mixer control
3261 * @ucontrol: Value
3262 */
3263int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3264 struct snd_ctl_elem_value *ucontrol)
3265{
Mark Brown48a8c392012-02-14 17:11:15 -08003266 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003267 const char *pin = (const char *)kcontrol->private_value;
3268
Liam Girdwood3cd04342012-03-09 12:02:08 +00003269 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003270
3271 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003272 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003273
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003274 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003275
3276 return 0;
3277}
3278EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3279
3280/**
3281 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3282 *
3283 * @kcontrol: mixer control
3284 * @ucontrol: Value
3285 */
3286int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3287 struct snd_ctl_elem_value *ucontrol)
3288{
Mark Brown48a8c392012-02-14 17:11:15 -08003289 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003290 const char *pin = (const char *)kcontrol->private_value;
3291
Mark Brown8b37dbd2009-02-28 21:14:20 +00003292 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003293 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003294 else
Mark Brown48a8c392012-02-14 17:11:15 -08003295 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003296
Mark Brown48a8c392012-02-14 17:11:15 -08003297 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003298 return 0;
3299}
3300EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3301
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +01003302struct snd_soc_dapm_widget *
Mark Brown5ba06fc2012-02-16 11:07:13 -08003303snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003304 const struct snd_soc_dapm_widget *widget)
3305{
3306 struct snd_soc_dapm_widget *w;
3307
3308 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3309 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3310 if (!w)
3311 dev_err(dapm->dev,
3312 "ASoC: Failed to create DAPM control %s\n",
3313 widget->name);
3314
3315 mutex_unlock(&dapm->card->dapm_mutex);
3316 return w;
3317}
Subhransu S. Prustya5d56392016-06-27 09:18:03 +05303318EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003319
3320struct snd_soc_dapm_widget *
3321snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003322 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003323{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003324 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003325 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003326 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00003327 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003328
3329 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003330 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003331
Mark Brown62ea8742012-01-21 21:14:48 +00003332 switch (w->id) {
3333 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003334 w->regulator = devm_regulator_get(dapm->dev, w->name);
3335 if (IS_ERR(w->regulator)) {
3336 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003337 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003338 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003339 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003340 }
Mark Brown8784c772013-01-10 19:33:47 +00003341
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003342 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003343 ret = regulator_allow_bypass(w->regulator, true);
3344 if (ret != 0)
3345 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003346 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003347 w->name, ret);
3348 }
Mark Brown62ea8742012-01-21 21:14:48 +00003349 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003350 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003351#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003352 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003353 if (IS_ERR(w->clk)) {
3354 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003355 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003356 w->name, ret);
3357 return NULL;
3358 }
Mark Brownec029952012-06-04 08:16:20 +01003359#else
3360 return NULL;
3361#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003362 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003363 default:
3364 break;
3365 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003366
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003367 prefix = soc_dapm_prefix(dapm);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003368 if (prefix)
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003369 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003370 else
Lars-Peter Clausen48068962015-07-21 18:11:08 +02003371 w->name = kstrdup_const(widget->name, GFP_KERNEL);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003372 if (w->name == NULL) {
3373 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003374 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003375 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003376
Mark Brown7ca3a182011-10-08 14:04:50 +01003377 switch (w->id) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003378 case snd_soc_dapm_mic:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003379 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003380 w->power_check = dapm_generic_check_power;
3381 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003382 case snd_soc_dapm_input:
3383 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003384 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003385 w->power_check = dapm_generic_check_power;
3386 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003387 case snd_soc_dapm_spk:
3388 case snd_soc_dapm_hp:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003389 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003390 w->power_check = dapm_generic_check_power;
3391 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003392 case snd_soc_dapm_output:
3393 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003394 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003395 w->power_check = dapm_generic_check_power;
3396 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003397 case snd_soc_dapm_vmid:
3398 case snd_soc_dapm_siggen:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003399 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003400 w->power_check = dapm_always_on_check_power;
3401 break;
Vinod Koul56b44372015-11-23 21:22:30 +05303402 case snd_soc_dapm_sink:
3403 w->is_ep = SND_SOC_DAPM_EP_SINK;
3404 w->power_check = dapm_always_on_check_power;
3405 break;
3406
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003407 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003408 case snd_soc_dapm_demux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003409 case snd_soc_dapm_switch:
3410 case snd_soc_dapm_mixer:
3411 case snd_soc_dapm_mixer_named_ctl:
Mark Brown63c69a62013-07-18 22:03:01 +01003412 case snd_soc_dapm_adc:
3413 case snd_soc_dapm_aif_out:
3414 case snd_soc_dapm_dac:
3415 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003416 case snd_soc_dapm_pga:
3417 case snd_soc_dapm_out_drv:
Mark Brown7ca3a182011-10-08 14:04:50 +01003418 case snd_soc_dapm_micbias:
Mark Brown7ca3a182011-10-08 14:04:50 +01003419 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003420 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02003421 case snd_soc_dapm_dai_out:
3422 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003423 w->power_check = dapm_generic_check_power;
3424 break;
3425 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003426 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003427 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003428 case snd_soc_dapm_kcontrol:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003429 w->is_supply = 1;
Mark Brown7ca3a182011-10-08 14:04:50 +01003430 w->power_check = dapm_supply_check_power;
3431 break;
3432 default:
3433 w->power_check = dapm_always_on_check_power;
3434 break;
3435 }
3436
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003437 w->dapm = dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003438 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003439 INIT_LIST_HEAD(&w->dirty);
Lars-Peter Clausen92fa1242015-05-01 18:02:42 +02003440 list_add_tail(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003441
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003442 snd_soc_dapm_for_each_direction(dir) {
3443 INIT_LIST_HEAD(&w->edges[dir]);
3444 w->endpoints[dir] = -1;
3445 }
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003446
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02003447 /* machine layer sets up unconnected pins and insertions */
Richard Purdie2b97eab2006-10-06 18:32:18 +02003448 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003449 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003450}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003451
3452/**
Mark Brown4ba13272008-05-13 14:51:19 +02003453 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003454 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003455 * @widget: widget array
3456 * @num: number of widgets
3457 *
3458 * Creates new DAPM controls based upon the templates.
3459 *
3460 * Returns 0 for success else error.
3461 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003462int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003463 const struct snd_soc_dapm_widget *widget,
3464 int num)
3465{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003466 struct snd_soc_dapm_widget *w;
3467 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003468 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003469
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003470 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003471 for (i = 0; i < num; i++) {
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003472 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003473 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003474 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003475 "ASoC: Failed to create DAPM control %s\n",
3476 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003477 ret = -ENOMEM;
3478 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003479 }
Mark Brown4ba13272008-05-13 14:51:19 +02003480 widget++;
3481 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003482 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003483 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003484}
3485EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3486
Mark Brownc74184e2012-04-04 22:12:09 +01003487static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3488 struct snd_kcontrol *kcontrol, int event)
3489{
3490 struct snd_soc_dapm_path *source_p, *sink_p;
3491 struct snd_soc_dai *source, *sink;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003492 const struct snd_soc_pcm_stream *config = w->params + w->params_select;
Mark Brownc74184e2012-04-04 22:12:09 +01003493 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003494 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003495 u64 fmt;
3496 int ret;
3497
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003498 if (WARN_ON(!config) ||
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003499 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3500 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003501 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003502
3503 /* We only support a single source and sink, pick the first */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003504 source_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_OUT],
3505 struct snd_soc_dapm_path,
3506 list_node[SND_SOC_DAPM_DIR_OUT]);
3507 sink_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_IN],
3508 struct snd_soc_dapm_path,
3509 list_node[SND_SOC_DAPM_DIR_IN]);
Mark Brownc74184e2012-04-04 22:12:09 +01003510
3511 source = source_p->source->priv;
3512 sink = sink_p->sink->priv;
3513
3514 /* Be a little careful as we don't want to overflow the mask array */
3515 if (config->formats) {
3516 fmt = ffs(config->formats) - 1;
3517 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003518 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003519 config->formats);
3520 fmt = 0;
3521 }
3522
3523 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003524 params = kzalloc(sizeof(*params), GFP_KERNEL);
3525 if (!params) {
3526 ret = -ENOMEM;
3527 goto out;
3528 }
3529 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003530
Mark Brown9747cec2012-04-26 19:12:21 +01003531 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003532 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003533 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003534 config->rate_max;
3535
Mark Brown9747cec2012-04-26 19:12:21 +01003536 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003537 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003538 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003539 = config->channels_max;
3540
3541 memset(&substream, 0, sizeof(substream));
3542
3543 switch (event) {
3544 case SND_SOC_DAPM_PRE_PMU:
Benoit Cousson93e69582014-07-08 23:19:38 +02003545 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303546 if (source->driver->ops && source->driver->ops->startup) {
3547 ret = source->driver->ops->startup(&substream, source);
3548 if (ret < 0) {
3549 dev_err(source->dev,
3550 "ASoC: startup() failed: %d\n", ret);
3551 goto out;
3552 }
3553 source->active++;
3554 }
Benoit Cousson93e69582014-07-08 23:19:38 +02003555 ret = soc_dai_hw_params(&substream, params, source);
3556 if (ret < 0)
3557 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003558
Benoit Cousson93e69582014-07-08 23:19:38 +02003559 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303560 if (sink->driver->ops && sink->driver->ops->startup) {
3561 ret = sink->driver->ops->startup(&substream, sink);
3562 if (ret < 0) {
3563 dev_err(sink->dev,
3564 "ASoC: startup() failed: %d\n", ret);
3565 goto out;
3566 }
3567 sink->active++;
3568 }
Benoit Cousson93e69582014-07-08 23:19:38 +02003569 ret = soc_dai_hw_params(&substream, params, sink);
3570 if (ret < 0)
3571 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003572 break;
3573
3574 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003575 ret = snd_soc_dai_digital_mute(sink, 0,
3576 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003577 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003578 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003579 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003580 break;
3581
3582 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003583 ret = snd_soc_dai_digital_mute(sink, 1,
3584 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003585 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003586 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003587 ret = 0;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303588
3589 source->active--;
3590 if (source->driver->ops && source->driver->ops->shutdown) {
3591 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3592 source->driver->ops->shutdown(&substream, source);
3593 }
3594
3595 sink->active--;
3596 if (sink->driver->ops && sink->driver->ops->shutdown) {
3597 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3598 sink->driver->ops->shutdown(&substream, sink);
3599 }
Mark Brownc74184e2012-04-04 22:12:09 +01003600 break;
3601
3602 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003603 WARN(1, "Unknown event %d\n", event);
Sudip Mukherjee75881df2015-09-10 18:01:44 +05303604 ret = -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003605 }
3606
Mark Brown9747cec2012-04-26 19:12:21 +01003607out:
3608 kfree(params);
3609 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003610}
3611
Nikesh Oswalc6615082015-02-02 17:06:44 +00003612static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3613 struct snd_ctl_elem_value *ucontrol)
3614{
3615 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3616
Takashi Iwai741338f2016-02-29 17:20:48 +01003617 ucontrol->value.enumerated.item[0] = w->params_select;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003618
3619 return 0;
3620}
3621
3622static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3623 struct snd_ctl_elem_value *ucontrol)
3624{
3625 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3626
3627 /* Can't change the config when widget is already powered */
3628 if (w->power)
3629 return -EBUSY;
3630
Takashi Iwai741338f2016-02-29 17:20:48 +01003631 if (ucontrol->value.enumerated.item[0] == w->params_select)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003632 return 0;
3633
Takashi Iwai741338f2016-02-29 17:20:48 +01003634 if (ucontrol->value.enumerated.item[0] >= w->num_params)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003635 return -EINVAL;
3636
Takashi Iwai741338f2016-02-29 17:20:48 +01003637 w->params_select = ucontrol->value.enumerated.item[0];
Nikesh Oswalc6615082015-02-02 17:06:44 +00003638
3639 return 0;
3640}
3641
Mark Brownc74184e2012-04-04 22:12:09 +01003642int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3643 const struct snd_soc_pcm_stream *params,
Nikesh Oswalc6615082015-02-02 17:06:44 +00003644 unsigned int num_params,
Mark Brownc74184e2012-04-04 22:12:09 +01003645 struct snd_soc_dapm_widget *source,
3646 struct snd_soc_dapm_widget *sink)
3647{
Mark Brownc74184e2012-04-04 22:12:09 +01003648 struct snd_soc_dapm_widget template;
3649 struct snd_soc_dapm_widget *w;
Mark Brownc74184e2012-04-04 22:12:09 +01003650 char *link_name;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003651 int ret, count;
3652 unsigned long private_value;
3653 const char **w_param_text;
3654 struct soc_enum w_param_enum[] = {
3655 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3656 };
3657 struct snd_kcontrol_new kcontrol_dai_link[] = {
3658 SOC_ENUM_EXT(NULL, w_param_enum[0],
3659 snd_soc_dapm_dai_link_get,
3660 snd_soc_dapm_dai_link_put),
3661 };
3662 const struct snd_soc_pcm_stream *config = params;
Mark Brownc74184e2012-04-04 22:12:09 +01003663
Nikesh Oswalc6615082015-02-02 17:06:44 +00003664 w_param_text = devm_kcalloc(card->dev, num_params,
3665 sizeof(char *), GFP_KERNEL);
3666 if (!w_param_text)
Mark Brownc74184e2012-04-04 22:12:09 +01003667 return -ENOMEM;
Mark Brownc74184e2012-04-04 22:12:09 +01003668
Charles Keepax46172b62015-03-25 11:22:35 +00003669 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3670 source->name, sink->name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003671 if (!link_name) {
3672 ret = -ENOMEM;
3673 goto outfree_w_param;
3674 }
Mark Brownc74184e2012-04-04 22:12:09 +01003675
Nikesh Oswalc6615082015-02-02 17:06:44 +00003676 for (count = 0 ; count < num_params; count++) {
3677 if (!config->stream_name) {
3678 dev_warn(card->dapm.dev,
3679 "ASoC: anonymous config %d for dai link %s\n",
3680 count, link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003681 w_param_text[count] =
Charles Keepax46172b62015-03-25 11:22:35 +00003682 devm_kasprintf(card->dev, GFP_KERNEL,
3683 "Anonymous Configuration %d",
3684 count);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003685 if (!w_param_text[count]) {
3686 ret = -ENOMEM;
3687 goto outfree_link_name;
3688 }
Nikesh Oswalc6615082015-02-02 17:06:44 +00003689 } else {
3690 w_param_text[count] = devm_kmemdup(card->dev,
3691 config->stream_name,
3692 strlen(config->stream_name) + 1,
3693 GFP_KERNEL);
3694 if (!w_param_text[count]) {
3695 ret = -ENOMEM;
3696 goto outfree_link_name;
3697 }
3698 }
3699 config++;
3700 }
3701 w_param_enum[0].items = num_params;
3702 w_param_enum[0].texts = w_param_text;
Mark Brownc74184e2012-04-04 22:12:09 +01003703
3704 memset(&template, 0, sizeof(template));
3705 template.reg = SND_SOC_NOPM;
3706 template.id = snd_soc_dapm_dai_link;
3707 template.name = link_name;
3708 template.event = snd_soc_dai_link_event;
3709 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3710 SND_SOC_DAPM_PRE_PMD;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003711 template.num_kcontrols = 1;
3712 /* duplicate w_param_enum on heap so that memory persists */
3713 private_value =
3714 (unsigned long) devm_kmemdup(card->dev,
3715 (void *)(kcontrol_dai_link[0].private_value),
3716 sizeof(struct soc_enum), GFP_KERNEL);
3717 if (!private_value) {
3718 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3719 link_name);
3720 ret = -ENOMEM;
3721 goto outfree_link_name;
3722 }
3723 kcontrol_dai_link[0].private_value = private_value;
3724 /* duplicate kcontrol_dai_link on heap so that memory persists */
3725 template.kcontrol_news =
3726 devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3727 sizeof(struct snd_kcontrol_new),
3728 GFP_KERNEL);
3729 if (!template.kcontrol_news) {
3730 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3731 link_name);
3732 ret = -ENOMEM;
3733 goto outfree_private_value;
3734 }
Mark Brownc74184e2012-04-04 22:12:09 +01003735
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003736 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003737
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003738 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
Mark Brownc74184e2012-04-04 22:12:09 +01003739 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003740 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003741 link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003742 ret = -ENOMEM;
3743 goto outfree_kcontrol_news;
Mark Brownc74184e2012-04-04 22:12:09 +01003744 }
3745
3746 w->params = params;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003747 w->num_params = num_params;
Mark Brownc74184e2012-04-04 22:12:09 +01003748
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003749 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3750 if (ret)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003751 goto outfree_w;
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003752 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003753
3754outfree_w:
3755 devm_kfree(card->dev, w);
3756outfree_kcontrol_news:
3757 devm_kfree(card->dev, (void *)template.kcontrol_news);
3758outfree_private_value:
3759 devm_kfree(card->dev, (void *)private_value);
3760outfree_link_name:
3761 devm_kfree(card->dev, link_name);
3762outfree_w_param:
3763 for (count = 0 ; count < num_params; count++)
3764 devm_kfree(card->dev, (void *)w_param_text[count]);
3765 devm_kfree(card->dev, w_param_text);
3766
3767 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003768}
3769
Mark Brown888df392012-02-16 19:37:51 -08003770int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3771 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003772{
Mark Brown888df392012-02-16 19:37:51 -08003773 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003774 struct snd_soc_dapm_widget *w;
3775
Mark Brown888df392012-02-16 19:37:51 -08003776 WARN_ON(dapm->dev != dai->dev);
3777
3778 memset(&template, 0, sizeof(template));
3779 template.reg = SND_SOC_NOPM;
3780
3781 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003782 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003783 template.name = dai->driver->playback.stream_name;
3784 template.sname = dai->driver->playback.stream_name;
3785
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003786 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003787 template.name);
3788
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003789 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Mark Brown888df392012-02-16 19:37:51 -08003790 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003791 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003792 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003793 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003794 }
3795
3796 w->priv = dai;
3797 dai->playback_widget = w;
3798 }
3799
3800 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003801 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003802 template.name = dai->driver->capture.stream_name;
3803 template.sname = dai->driver->capture.stream_name;
3804
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003805 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003806 template.name);
3807
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003808 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Mark Brown888df392012-02-16 19:37:51 -08003809 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003810 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003811 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003812 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003813 }
3814
3815 w->priv = dai;
3816 dai->capture_widget = w;
3817 }
3818
3819 return 0;
3820}
3821
3822int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3823{
3824 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003825 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08003826 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003827
3828 /* For each DAI widget... */
3829 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003830 switch (dai_w->id) {
3831 case snd_soc_dapm_dai_in:
3832 case snd_soc_dapm_dai_out:
3833 break;
3834 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003835 continue;
Mark Brown46162742013-06-05 19:36:11 +01003836 }
Mark Brown888df392012-02-16 19:37:51 -08003837
3838 dai = dai_w->priv;
3839
3840 /* ...find all widgets with the same stream and link them */
3841 list_for_each_entry(w, &card->widgets, list) {
3842 if (w->dapm != dai_w->dapm)
3843 continue;
3844
Mark Brown46162742013-06-05 19:36:11 +01003845 switch (w->id) {
3846 case snd_soc_dapm_dai_in:
3847 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003848 continue;
Mark Brown46162742013-06-05 19:36:11 +01003849 default:
3850 break;
3851 }
Mark Brown888df392012-02-16 19:37:51 -08003852
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003853 if (!w->sname || !strstr(w->sname, dai_w->sname))
Mark Brown888df392012-02-16 19:37:51 -08003854 continue;
3855
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003856 if (dai_w->id == snd_soc_dapm_dai_in) {
3857 src = dai_w;
3858 sink = w;
3859 } else {
3860 src = w;
3861 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08003862 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003863 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3864 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003865 }
3866 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003867
Mark Brown888df392012-02-16 19:37:51 -08003868 return 0;
3869}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003870
Benoit Cousson44ba2642014-07-08 23:19:36 +02003871static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3872 struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003873{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003874 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003875 struct snd_soc_dapm_widget *sink, *source;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003876 int i;
3877
Benoit Cousson44ba2642014-07-08 23:19:36 +02003878 for (i = 0; i < rtd->num_codecs; i++) {
3879 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003880
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003881 /* connect BE DAI playback if widgets are valid */
3882 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003883 source = cpu_dai->playback_widget;
3884 sink = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003885 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003886 cpu_dai->component->name, source->name,
3887 codec_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003888
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003889 snd_soc_dapm_add_path(&card->dapm, source, sink,
3890 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003891 }
3892
3893 /* connect BE DAI capture if widgets are valid */
3894 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003895 source = codec_dai->capture_widget;
3896 sink = cpu_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003897 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003898 codec_dai->component->name, source->name,
3899 cpu_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003900
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003901 snd_soc_dapm_add_path(&card->dapm, source, sink,
3902 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003903 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003904 }
3905}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003906
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003907static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3908 int event)
3909{
3910 struct snd_soc_dapm_widget *w;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003911 unsigned int ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003912
3913 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3914 w = dai->playback_widget;
3915 else
3916 w = dai->capture_widget;
3917
3918 if (w) {
3919 dapm_mark_dirty(w, "stream event");
3920
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003921 if (w->id == snd_soc_dapm_dai_in) {
3922 ep = SND_SOC_DAPM_EP_SOURCE;
3923 dapm_widget_invalidate_input_paths(w);
3924 } else {
3925 ep = SND_SOC_DAPM_EP_SINK;
3926 dapm_widget_invalidate_output_paths(w);
3927 }
3928
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003929 switch (event) {
3930 case SND_SOC_DAPM_STREAM_START:
3931 w->active = 1;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003932 w->is_ep = ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003933 break;
3934 case SND_SOC_DAPM_STREAM_STOP:
3935 w->active = 0;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003936 w->is_ep = 0;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003937 break;
3938 case SND_SOC_DAPM_STREAM_SUSPEND:
3939 case SND_SOC_DAPM_STREAM_RESUME:
3940 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3941 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3942 break;
3943 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003944 }
3945}
3946
Benoit Cousson44ba2642014-07-08 23:19:36 +02003947void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3948{
Mengdong Lin1a497982015-11-18 02:34:11 -05003949 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson44ba2642014-07-08 23:19:36 +02003950
3951 /* for each BE DAI link... */
Mengdong Lin1a497982015-11-18 02:34:11 -05003952 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson44ba2642014-07-08 23:19:36 +02003953 /*
3954 * dynamic FE links have no fixed DAI mapping.
3955 * CODEC<->CODEC links have no direct connection.
3956 */
3957 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3958 continue;
3959
3960 dapm_connect_dai_link_widgets(card, rtd);
3961 }
3962}
3963
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003964static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3965 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003966{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003967 int i;
3968
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003969 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
Benoit Cousson44ba2642014-07-08 23:19:36 +02003970 for (i = 0; i < rtd->num_codecs; i++)
3971 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003972
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003973 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003974}
3975
3976/**
3977 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3978 * @rtd: PCM runtime data
3979 * @stream: stream name
3980 * @event: stream event
3981 *
3982 * Sends a stream event to the dapm core. The core then makes any
3983 * necessary widget power changes.
3984 *
3985 * Returns 0 for success else error.
3986 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003987void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3988 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003989{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003990 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003991
Liam Girdwood3cd04342012-03-09 12:02:08 +00003992 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003993 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003994 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003995}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003996
3997/**
Charles Keepax11391102014-02-18 15:22:14 +00003998 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3999 * @dapm: DAPM context
4000 * @pin: pin name
4001 *
4002 * Enables input/output pin and its parents or children widgets iff there is
4003 * a valid audio route and active audio stream.
4004 *
4005 * Requires external locking.
4006 *
4007 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4008 * do any widget power switching.
4009 */
4010int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4011 const char *pin)
4012{
4013 return snd_soc_dapm_set_pin(dapm, pin, 1);
4014}
4015EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4016
4017/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004018 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004019 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004020 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02004021 *
Mark Brown74b8f952009-06-06 11:26:15 +01004022 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01004023 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00004024 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004025 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4026 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02004027 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004028int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004029{
Charles Keepax11391102014-02-18 15:22:14 +00004030 int ret;
4031
4032 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4033
4034 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4035
4036 mutex_unlock(&dapm->card->dapm_mutex);
4037
4038 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004039}
Liam Girdwooda5302182008-07-07 13:35:17 +01004040EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004041
4042/**
Charles Keepax11391102014-02-18 15:22:14 +00004043 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4044 * @dapm: DAPM context
4045 * @pin: pin name
4046 *
4047 * Enables input/output pin regardless of any other state. This is
4048 * intended for use with microphone bias supplies used in microphone
4049 * jack detection.
4050 *
4051 * Requires external locking.
4052 *
4053 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4054 * do any widget power switching.
4055 */
4056int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4057 const char *pin)
4058{
4059 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4060
4061 if (!w) {
4062 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4063 return -EINVAL;
4064 }
4065
4066 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02004067 if (!w->connected) {
4068 /*
4069 * w->force does not affect the number of input or output paths,
4070 * so we only have to recheck if w->connected is changed
4071 */
4072 dapm_widget_invalidate_input_paths(w);
4073 dapm_widget_invalidate_output_paths(w);
4074 w->connected = 1;
4075 }
Charles Keepax11391102014-02-18 15:22:14 +00004076 w->force = 1;
4077 dapm_mark_dirty(w, "force enable");
4078
4079 return 0;
4080}
4081EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4082
4083/**
Mark Brownda341832010-03-15 19:23:37 +00004084 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004085 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00004086 * @pin: pin name
4087 *
4088 * Enables input/output pin regardless of any other state. This is
4089 * intended for use with microphone bias supplies used in microphone
4090 * jack detection.
4091 *
4092 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4093 * do any widget power switching.
4094 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004095int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4096 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00004097{
Charles Keepax11391102014-02-18 15:22:14 +00004098 int ret;
Mark Brownda341832010-03-15 19:23:37 +00004099
Charles Keepax11391102014-02-18 15:22:14 +00004100 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00004101
Charles Keepax11391102014-02-18 15:22:14 +00004102 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09004103
Charles Keepax11391102014-02-18 15:22:14 +00004104 mutex_unlock(&dapm->card->dapm_mutex);
4105
4106 return ret;
Mark Brownda341832010-03-15 19:23:37 +00004107}
4108EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4109
4110/**
Charles Keepax11391102014-02-18 15:22:14 +00004111 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4112 * @dapm: DAPM context
4113 * @pin: pin name
4114 *
4115 * Disables input/output pin and its parents or children widgets.
4116 *
4117 * Requires external locking.
4118 *
4119 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4120 * do any widget power switching.
4121 */
4122int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4123 const char *pin)
4124{
4125 return snd_soc_dapm_set_pin(dapm, pin, 0);
4126}
4127EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4128
4129/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004130 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004131 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004132 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004133 *
Mark Brown74b8f952009-06-06 11:26:15 +01004134 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00004135 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004136 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4137 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004138 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004139int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4140 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01004141{
Charles Keepax11391102014-02-18 15:22:14 +00004142 int ret;
4143
4144 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4145
4146 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4147
4148 mutex_unlock(&dapm->card->dapm_mutex);
4149
4150 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01004151}
4152EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4153
4154/**
Charles Keepax11391102014-02-18 15:22:14 +00004155 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4156 * @dapm: DAPM context
4157 * @pin: pin name
4158 *
4159 * Marks the specified pin as being not connected, disabling it along
4160 * any parent or child widgets. At present this is identical to
4161 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4162 * additional things such as disabling controls which only affect
4163 * paths through the pin.
4164 *
4165 * Requires external locking.
4166 *
4167 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4168 * do any widget power switching.
4169 */
4170int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4171 const char *pin)
4172{
4173 return snd_soc_dapm_set_pin(dapm, pin, 0);
4174}
4175EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4176
4177/**
Mark Brown5817b522008-09-24 11:23:11 +01004178 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004179 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01004180 * @pin: pin name
4181 *
4182 * Marks the specified pin as being not connected, disabling it along
4183 * any parent or child widgets. At present this is identical to
4184 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4185 * additional things such as disabling controls which only affect
4186 * paths through the pin.
4187 *
4188 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4189 * do any widget power switching.
4190 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004191int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01004192{
Charles Keepax11391102014-02-18 15:22:14 +00004193 int ret;
4194
4195 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4196
4197 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4198
4199 mutex_unlock(&dapm->card->dapm_mutex);
4200
4201 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01004202}
4203EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4204
4205/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004206 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004207 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004208 * @pin: audio signal pin endpoint (or start point)
4209 *
4210 * Get audio pin status - connected or disconnected.
4211 *
4212 * Returns 1 for connected otherwise 0.
4213 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004214int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4215 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004216{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004217 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004218
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004219 if (w)
4220 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06004221
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004222 return 0;
4223}
Liam Girdwooda5302182008-07-07 13:35:17 +01004224EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004225
4226/**
Mark Brown1547aba2010-05-07 21:11:40 +01004227 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004228 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01004229 * @pin: audio signal pin endpoint (or start point)
4230 *
4231 * Mark the given endpoint or pin as ignoring suspend. When the
4232 * system is disabled a path between two endpoints flagged as ignoring
4233 * suspend will not be disabled. The path must already be enabled via
4234 * normal means at suspend time, it will not be turned on if it was not
4235 * already enabled.
4236 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004237int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4238 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01004239{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004240 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01004241
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004242 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004243 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004244 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01004245 }
4246
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004247 w->ignore_suspend = 1;
4248
4249 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01004250}
4251EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4252
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02004253/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02004254 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03004255 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02004256 *
4257 * Free all dapm widgets and resources.
4258 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004259void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004260{
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02004261 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004262 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02004263 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004264}
4265EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4266
Xiang Xiao57996352014-03-02 00:04:02 +08004267static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01004268{
Liam Girdwood01005a72012-07-06 16:57:05 +01004269 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01004270 struct snd_soc_dapm_widget *w;
4271 LIST_HEAD(down_list);
4272 int powerdown = 0;
4273
Liam Girdwood01005a72012-07-06 16:57:05 +01004274 mutex_lock(&card->dapm_mutex);
4275
Jarkko Nikula97c866d2010-12-14 12:18:31 +02004276 list_for_each_entry(w, &dapm->card->widgets, list) {
4277 if (w->dapm != dapm)
4278 continue;
Mark Brown51737472009-06-22 13:16:51 +01004279 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004280 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01004281 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004282 powerdown = 1;
4283 }
4284 }
4285
4286 /* If there were no widgets to power down we're already in
4287 * standby.
4288 */
4289 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004290 if (dapm->bias_level == SND_SOC_BIAS_ON)
4291 snd_soc_dapm_set_bias_level(dapm,
4292 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004293 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004294 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4295 snd_soc_dapm_set_bias_level(dapm,
4296 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004297 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004298
4299 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004300}
Mark Brown51737472009-06-22 13:16:51 +01004301
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004302/*
4303 * snd_soc_dapm_shutdown - callback for system shutdown
4304 */
4305void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4306{
Xiang Xiao57996352014-03-02 00:04:02 +08004307 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004308
Xiang Xiao57996352014-03-02 00:04:02 +08004309 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08004310 if (dapm != &card->dapm) {
4311 soc_dapm_shutdown_dapm(dapm);
4312 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4313 snd_soc_dapm_set_bias_level(dapm,
4314 SND_SOC_BIAS_OFF);
4315 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004316 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08004317
4318 soc_dapm_shutdown_dapm(&card->dapm);
4319 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4320 snd_soc_dapm_set_bias_level(&card->dapm,
4321 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01004322}
4323
Richard Purdie2b97eab2006-10-06 18:32:18 +02004324/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004325MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004326MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4327MODULE_LICENSE("GPL");