blob: 27dd02e57b31b0083f00a7864005a8ca867c42fb [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
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800333 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
334 dev_warn(widget->dapm->dev,
335 "ASoC: Unsupported stereo autodisable control '%s'\n",
336 ctrl_name);
337
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200338 if (mc->autodisable) {
339 struct snd_soc_dapm_widget template;
340
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100341 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
Charles Keepax773da9b2015-05-01 12:37:25 +0100342 "Autodisable");
343 if (!name) {
344 ret = -ENOMEM;
345 goto err_data;
346 }
347
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200348 memset(&template, 0, sizeof(template));
349 template.reg = mc->reg;
350 template.mask = (1 << fls(mc->max)) - 1;
351 template.shift = mc->shift;
352 if (mc->invert)
353 template.off_val = mc->max;
354 else
355 template.off_val = 0;
356 template.on_val = template.off_val;
357 template.id = snd_soc_dapm_kcontrol;
Charles Keepax773da9b2015-05-01 12:37:25 +0100358 template.name = name;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200359
Lars-Peter Clausen2daabd72013-08-30 17:39:33 +0200360 data->value = template.on_val;
361
Liam Girdwood02aa78a2015-05-25 18:21:17 +0100362 data->widget =
363 snd_soc_dapm_new_control_unlocked(widget->dapm,
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200364 &template);
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200365 kfree(name);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200366 if (!data->widget) {
Charles Keepax773da9b2015-05-01 12:37:25 +0100367 ret = -ENOMEM;
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200368 goto err_data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200369 }
370 }
371 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200372 case snd_soc_dapm_demux:
Charles Keepax561ed682015-05-01 12:37:26 +0100373 case snd_soc_dapm_mux:
374 e = (struct soc_enum *)kcontrol->private_value;
375
376 if (e->autodisable) {
377 struct snd_soc_dapm_widget template;
378
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100379 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
Charles Keepax561ed682015-05-01 12:37:26 +0100380 "Autodisable");
381 if (!name) {
382 ret = -ENOMEM;
383 goto err_data;
384 }
385
386 memset(&template, 0, sizeof(template));
387 template.reg = e->reg;
388 template.mask = e->mask << e->shift_l;
389 template.shift = e->shift_l;
390 template.off_val = snd_soc_enum_item_to_val(e, 0);
391 template.on_val = template.off_val;
392 template.id = snd_soc_dapm_kcontrol;
393 template.name = name;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200394
395 data->value = template.on_val;
396
Charles Keepaxffacb482015-06-26 10:39:43 +0100397 data->widget = snd_soc_dapm_new_control_unlocked(
398 widget->dapm, &template);
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200399 kfree(name);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200400 if (!data->widget) {
Charles Keepax561ed682015-05-01 12:37:26 +0100401 ret = -ENOMEM;
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200402 goto err_data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200403 }
Charles Keepax561ed682015-05-01 12:37:26 +0100404
405 snd_soc_dapm_add_path(widget->dapm, data->widget,
406 widget, NULL, NULL);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200407 }
408 break;
409 default:
410 break;
411 }
412
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200413 kcontrol->private_data = data;
414
415 return 0;
Charles Keepax773da9b2015-05-01 12:37:25 +0100416
Charles Keepax773da9b2015-05-01 12:37:25 +0100417err_data:
418 kfree(data);
419 return ret;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200420}
421
422static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
423{
424 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200425 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200426 kfree(data);
427}
428
429static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
430 const struct snd_kcontrol *kcontrol)
431{
432 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
433
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200434 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200435}
436
437static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
438 struct snd_soc_dapm_widget *widget)
439{
440 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200441 struct snd_soc_dapm_widget_list *new_wlist;
442 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200443
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200444 if (data->wlist)
445 n = data->wlist->num_widgets + 1;
446 else
447 n = 1;
448
449 new_wlist = krealloc(data->wlist,
450 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
451 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200452 return -ENOMEM;
453
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200454 new_wlist->widgets[n - 1] = widget;
455 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200456
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200457 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200458
459 return 0;
460}
461
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200462static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
463 struct snd_soc_dapm_path *path)
464{
465 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
466
467 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200468}
469
470static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
471{
472 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
473
474 if (!data->widget)
475 return true;
476
477 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200478}
479
480static struct list_head *dapm_kcontrol_get_path_list(
481 const struct snd_kcontrol *kcontrol)
482{
483 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
484
485 return &data->paths;
486}
487
488#define dapm_kcontrol_for_each_path(path, kcontrol) \
489 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
490 list_kcontrol)
491
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530492unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200493{
494 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
495
496 return data->value;
497}
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530498EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200499
500static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
501 unsigned int value)
502{
503 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
504
505 if (data->value == value)
506 return false;
507
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200508 if (data->widget)
509 data->widget->on_val = value;
510
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200511 data->value = value;
512
513 return true;
514}
515
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200516/**
Mythri P K93e39a12015-10-20 22:30:08 +0530517 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
518 * kcontrol
519 * @kcontrol: The kcontrol
520 */
521struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
522 struct snd_kcontrol *kcontrol)
523{
524 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
525}
526EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
527
528/**
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200529 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
530 * kcontrol
531 * @kcontrol: The kcontrol
532 *
533 * Note: This function must only be used on kcontrols that are known to have
534 * been registered for a CODEC. Otherwise the behaviour is undefined.
535 */
536struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
537 struct snd_kcontrol *kcontrol)
538{
539 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
540}
541EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
542
Liam Girdwood6c120e12012-02-15 15:15:34 +0000543static void dapm_reset(struct snd_soc_card *card)
544{
545 struct snd_soc_dapm_widget *w;
546
Mark Brownf9fa2b12014-03-06 16:49:11 +0800547 lockdep_assert_held(&card->dapm_mutex);
548
Liam Girdwood6c120e12012-02-15 15:15:34 +0000549 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
550
551 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200552 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000553 w->power_checked = false;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000554 }
555}
556
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200557static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
558{
559 if (!dapm->component)
560 return NULL;
561 return dapm->component->name_prefix;
562}
563
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200564static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800565 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100566{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200567 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200568 return -EIO;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200569 return snd_soc_component_read(dapm->component, reg, value);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100570}
571
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200572static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
Bard Liao34775012014-04-17 20:12:56 +0800573 int reg, unsigned int mask, unsigned int value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100574{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200575 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200576 return -EIO;
Mark Brownfcf6c5e2014-12-15 13:08:48 +0000577 return snd_soc_component_update_bits(dapm->component, reg,
578 mask, value);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000579}
580
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200581static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
582 int reg, unsigned int mask, unsigned int value)
583{
584 if (!dapm->component)
585 return -EIO;
586 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
587}
588
Mark Browneb270e92013-10-09 13:52:52 +0100589static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
590{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200591 if (dapm->component)
592 snd_soc_component_async_complete(dapm->component);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100593}
594
Charles Keepax45a110a2015-05-11 13:50:30 +0100595static struct snd_soc_dapm_widget *
596dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
597{
598 struct snd_soc_dapm_widget *w = wcache->widget;
599 struct list_head *wlist;
600 const int depth = 2;
601 int i = 0;
602
603 if (w) {
604 wlist = &w->dapm->card->widgets;
605
606 list_for_each_entry_from(w, wlist, list) {
607 if (!strcmp(name, w->name))
608 return w;
609
610 if (++i == depth)
611 break;
612 }
613 }
614
615 return NULL;
616}
617
618static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
619 struct snd_soc_dapm_widget *w)
620{
621 wcache->widget = w;
622}
623
Mark Brown452c5ea2009-05-17 21:41:23 +0100624/**
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200625 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
626 * @dapm: The DAPM context for which to set the level
627 * @level: The level to set
628 *
629 * Forces the DAPM bias level to a specific state. It will call the bias level
630 * callback of DAPM context with the specified level. This will even happen if
631 * the context is already at the same level. Furthermore it will not go through
632 * the normal bias level sequencing, meaning any intermediate states between the
633 * current and the target state will not be entered.
634 *
635 * Note that the change in bias level is only temporary and the next time
636 * snd_soc_dapm_sync() is called the state will be set to the level as
637 * determined by the DAPM core. The function is mainly intended to be used to
638 * used during probe or resume from suspend to power up the device so
639 * initialization can be done, before the DAPM core takes over.
640 */
641int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
642 enum snd_soc_bias_level level)
643{
644 int ret = 0;
645
646 if (dapm->set_bias_level)
647 ret = dapm->set_bias_level(dapm, level);
648
Lars-Peter Clausenf4bf8d72015-04-27 22:13:25 +0200649 if (ret == 0)
650 dapm->bias_level = level;
651
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200652 return ret;
653}
654EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
655
Mark Brown452c5ea2009-05-17 21:41:23 +0100656/**
657 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800658 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100659 * @level: level to configure
660 *
661 * Configure the bias (power) levels for the SoC audio device.
662 *
663 * Returns 0 for success else error.
664 */
Mark Browned5a4c42011-02-18 11:12:42 -0800665static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200666 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100667{
Mark Browned5a4c42011-02-18 11:12:42 -0800668 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100669 int ret = 0;
670
Mark Brown84e90932010-11-04 00:07:02 -0400671 trace_snd_soc_bias_level_start(card, level);
672
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000673 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100674 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100675 if (ret != 0)
676 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100677
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200678 if (!card || dapm != &card->dapm)
679 ret = snd_soc_dapm_force_bias_level(dapm, level);
Liam Girdwood41231282012-07-06 16:56:16 +0100680
Mark Brown171ec6b2011-06-06 18:15:19 +0100681 if (ret != 0)
682 goto out;
683
684 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100685 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100686out:
Mark Brown84e90932010-11-04 00:07:02 -0400687 trace_snd_soc_bias_level_done(card, level);
688
Mark Brown452c5ea2009-05-17 21:41:23 +0100689 return ret;
690}
691
Mark Brown74b8f952009-06-06 11:26:15 +0100692/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200693static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200694 struct snd_soc_dapm_path *path, const char *control_name,
695 struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200696{
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200697 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200698 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100699 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200700 int i;
701
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100702 if (e->reg != SND_SOC_NOPM) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200703 soc_dapm_read(dapm, e->reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100704 val = (val >> e->shift_l) & e->mask;
705 item = snd_soc_enum_val_to_item(e, val);
706 } else {
707 /* since a virtual mux has no backing registers to
708 * decide which path to connect, it will try to match
709 * with the first enumeration. This is to ensure
710 * that the default mux choice (the first) will be
711 * correctly powered up during initialization.
712 */
713 item = 0;
714 }
715
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100716 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200717 if (!(strcmp(control_name, e->texts[i]))) {
Rasmus Villemoes98ad73c2014-10-21 17:01:15 +0200718 path->name = e->texts[i];
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100719 if (i == item)
720 path->connect = 1;
721 else
722 path->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200723 return 0;
724 }
725 }
726
727 return -ENODEV;
728}
729
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100730/* set up initial codec paths */
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800731static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
732 int nth_path)
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100733{
734 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200735 p->sink->kcontrol_news[i].private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100736 unsigned int reg = mc->reg;
737 unsigned int shift = mc->shift;
738 unsigned int max = mc->max;
739 unsigned int mask = (1 << fls(max)) - 1;
740 unsigned int invert = mc->invert;
741 unsigned int val;
742
743 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200744 soc_dapm_read(p->sink->dapm, reg, &val);
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800745 /*
746 * The nth_path argument allows this function to know
747 * which path of a kcontrol it is setting the initial
748 * status for. Ideally this would support any number
749 * of paths and channels. But since kcontrols only come
750 * in mono and stereo variants, we are limited to 2
751 * channels.
752 *
753 * The following code assumes for stereo controls the
754 * first path is the left channel, and all remaining
755 * paths are the right channel.
756 */
757 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
758 if (reg != mc->rreg)
759 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
760 val = (val >> mc->rshift) & mask;
761 } else {
762 val = (val >> shift) & mask;
763 }
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100764 if (invert)
765 val = max - val;
766 p->connect = !!val;
767 } else {
768 p->connect = 0;
769 }
770}
771
Mark Brown74b8f952009-06-06 11:26:15 +0100772/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200773static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200774 struct snd_soc_dapm_path *path, const char *control_name)
775{
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800776 int i, nth_path = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200777
778 /* search for mixer kcontrol */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200779 for (i = 0; i < path->sink->num_kcontrols; i++) {
780 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
781 path->name = path->sink->kcontrol_news[i].name;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +0800782 dapm_set_mixer_path_status(path, i, nth_path++);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200783 return 0;
784 }
785 }
786 return -ENODEV;
787}
788
Stephen Warrenaf468002011-04-28 17:38:01 -0600789static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600790 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600791 const struct snd_kcontrol_new *kcontrol_new,
792 struct snd_kcontrol **kcontrol)
793{
794 struct snd_soc_dapm_widget *w;
795 int i;
796
797 *kcontrol = NULL;
798
799 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600800 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
801 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600802 for (i = 0; i < w->num_kcontrols; i++) {
803 if (&w->kcontrol_news[i] == kcontrol_new) {
804 if (w->kcontrols)
805 *kcontrol = w->kcontrols[i];
806 return 1;
807 }
808 }
809 }
810
811 return 0;
812}
813
Stephen Warren85762e72013-03-29 15:40:10 -0600814/*
815 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
816 * create it. Either way, add the widget into the control's widget list
817 */
Jeeja KP19a2557b2015-10-20 22:30:06 +0530818static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100819 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200820{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200821 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000822 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000823 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600824 size_t prefix_len;
825 int shared;
826 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600827 bool wname_in_long_name, kcname_in_long_name;
Daniel Macke5092c92014-10-07 13:41:24 +0200828 char *long_name = NULL;
Stephen Warren85762e72013-03-29 15:40:10 -0600829 const char *name;
Daniel Macke5092c92014-10-07 13:41:24 +0200830 int ret = 0;
Mark Brownefb7ac32011-03-08 17:23:24 +0000831
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200832 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000833 if (prefix)
834 prefix_len = strlen(prefix) + 1;
835 else
836 prefix_len = 0;
837
Stephen Warren85762e72013-03-29 15:40:10 -0600838 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
839 &kcontrol);
840
Stephen Warren85762e72013-03-29 15:40:10 -0600841 if (!kcontrol) {
842 if (shared) {
843 wname_in_long_name = false;
844 kcname_in_long_name = true;
845 } else {
846 switch (w->id) {
847 case snd_soc_dapm_switch:
848 case snd_soc_dapm_mixer:
Jeeja KP19a2557b2015-10-20 22:30:06 +0530849 case snd_soc_dapm_pga:
Chen-Yu Tsaia3930ed2016-08-27 19:28:00 +0800850 case snd_soc_dapm_out_drv:
Stephen Warren85762e72013-03-29 15:40:10 -0600851 wname_in_long_name = true;
852 kcname_in_long_name = true;
853 break;
854 case snd_soc_dapm_mixer_named_ctl:
855 wname_in_long_name = false;
856 kcname_in_long_name = true;
857 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200858 case snd_soc_dapm_demux:
Stephen Warren85762e72013-03-29 15:40:10 -0600859 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600860 wname_in_long_name = true;
861 kcname_in_long_name = false;
862 break;
863 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600864 return -EINVAL;
865 }
866 }
867
868 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600869 /*
870 * The control will get a prefix from the control
871 * creation process but we're also using the same
872 * prefix for widgets so cut the prefix off the
873 * front of the widget name.
874 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200875 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600876 w->name + prefix_len,
877 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200878 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200879 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600880
881 name = long_name;
882 } else if (wname_in_long_name) {
883 long_name = NULL;
884 name = w->name + prefix_len;
885 } else {
886 long_name = NULL;
887 name = w->kcontrol_news[kci].name;
888 }
889
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200890 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600891 prefix);
Daniel Macke5092c92014-10-07 13:41:24 +0200892 if (!kcontrol) {
893 ret = -ENOMEM;
894 goto exit_free;
895 }
896
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200897 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200898
Lars-Peter Clausen41d80022016-02-03 21:59:50 +0100899 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200900 if (ret) {
901 snd_ctl_free_one(kcontrol);
Daniel Macke5092c92014-10-07 13:41:24 +0200902 goto exit_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200903 }
904
Stephen Warren85762e72013-03-29 15:40:10 -0600905 ret = snd_ctl_add(card, kcontrol);
906 if (ret < 0) {
907 dev_err(dapm->dev,
908 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
909 w->name, name, ret);
Daniel Macke5092c92014-10-07 13:41:24 +0200910 goto exit_free;
Stephen Warren85762e72013-03-29 15:40:10 -0600911 }
Stephen Warren85762e72013-03-29 15:40:10 -0600912 }
913
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200914 ret = dapm_kcontrol_add_widget(kcontrol, w);
Daniel Macke5092c92014-10-07 13:41:24 +0200915 if (ret == 0)
916 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200917
Daniel Macke5092c92014-10-07 13:41:24 +0200918exit_free:
919 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600920
Daniel Macke5092c92014-10-07 13:41:24 +0200921 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600922}
923
924/* create new dapm mixer control */
925static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
926{
927 int i, ret;
928 struct snd_soc_dapm_path *path;
Charles Keepax561ed682015-05-01 12:37:26 +0100929 struct dapm_kcontrol_data *data;
Stephen Warren85762e72013-03-29 15:40:10 -0600930
Richard Purdie2b97eab2006-10-06 18:32:18 +0200931 /* add kcontrol */
932 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200933 /* match name */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +0200934 snd_soc_dapm_widget_for_each_source_path(w, path) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200935 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600936 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200937 continue;
938
Charles Keepax561ed682015-05-01 12:37:26 +0100939 if (!w->kcontrols[i]) {
Jeeja KP19a2557b2015-10-20 22:30:06 +0530940 ret = dapm_create_or_share_kcontrol(w, i);
Charles Keepax561ed682015-05-01 12:37:26 +0100941 if (ret < 0)
942 return ret;
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200943 }
944
Mark Brown946d92a2013-08-12 23:28:42 +0100945 dapm_kcontrol_add_path(w->kcontrols[i], path);
Charles Keepax561ed682015-05-01 12:37:26 +0100946
947 data = snd_kcontrol_chip(w->kcontrols[i]);
948 if (data->widget)
949 snd_soc_dapm_add_path(data->widget->dapm,
950 data->widget,
951 path->source,
952 NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200953 }
954 }
Stephen Warren85762e72013-03-29 15:40:10 -0600955
956 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200957}
958
959/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200960static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200961{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200962 struct snd_soc_dapm_context *dapm = w->dapm;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200963 enum snd_soc_dapm_direction dir;
Stephen Warren85762e72013-03-29 15:40:10 -0600964 struct snd_soc_dapm_path *path;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200965 const char *type;
Stephen Warrenaf468002011-04-28 17:38:01 -0600966 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200967
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200968 switch (w->id) {
969 case snd_soc_dapm_mux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200970 dir = SND_SOC_DAPM_DIR_OUT;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200971 type = "mux";
972 break;
973 case snd_soc_dapm_demux:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200974 dir = SND_SOC_DAPM_DIR_IN;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200975 type = "demux";
976 break;
977 default:
978 return -EINVAL;
979 }
980
Stephen Warrenaf468002011-04-28 17:38:01 -0600981 if (w->num_kcontrols != 1) {
982 dev_err(dapm->dev,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200983 "ASoC: %s %s has incorrect number of controls\n", type,
Stephen Warrenaf468002011-04-28 17:38:01 -0600984 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200985 return -EINVAL;
986 }
987
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200988 if (list_empty(&w->edges[dir])) {
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200989 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
Stephen Warren85762e72013-03-29 15:40:10 -0600990 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600991 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200992
Jeeja KP19a2557b2015-10-20 22:30:06 +0530993 ret = dapm_create_or_share_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600994 if (ret < 0)
995 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600996
Lars-Peter Clausena3423b02015-08-11 21:38:00 +0200997 snd_soc_dapm_widget_for_each_path(w, dir, path) {
998 if (path->name)
999 dapm_kcontrol_add_path(w->kcontrols[0], path);
Lars-Peter Clausen98407ef2014-10-25 17:41:57 +02001000 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001001
Stephen Warrenaf468002011-04-28 17:38:01 -06001002 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001003}
1004
1005/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001006static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001007{
Jeeja KP19a2557b2015-10-20 22:30:06 +05301008 int i, ret;
1009
1010 for (i = 0; i < w->num_kcontrols; i++) {
1011 ret = dapm_create_or_share_kcontrol(w, i);
1012 if (ret < 0)
1013 return ret;
1014 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001015
Mark Browna6c65732010-03-03 17:45:21 +00001016 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001017}
1018
Nikesh Oswalc6615082015-02-02 17:06:44 +00001019/* create new dapm dai link control */
1020static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1021{
1022 int i, ret;
1023 struct snd_kcontrol *kcontrol;
1024 struct snd_soc_dapm_context *dapm = w->dapm;
1025 struct snd_card *card = dapm->card->snd_card;
1026
1027 /* create control for links with > 1 config */
1028 if (w->num_params <= 1)
1029 return 0;
1030
1031 /* add kcontrol */
1032 for (i = 0; i < w->num_kcontrols; i++) {
1033 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1034 w->name, NULL);
1035 ret = snd_ctl_add(card, kcontrol);
1036 if (ret < 0) {
1037 dev_err(dapm->dev,
1038 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1039 w->name, w->kcontrol_news[i].name, ret);
1040 return ret;
1041 }
1042 kcontrol->private_data = w;
1043 w->kcontrols[i] = kcontrol;
1044 }
1045
1046 return 0;
1047}
1048
Mark Brown99497882010-05-07 20:24:05 +01001049/* We implement power down on suspend by checking the power state of
1050 * the ALSA card - when we are suspending the ALSA state for the card
1051 * is set to D3.
1052 */
1053static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1054{
Mark Brown12ea2c72011-03-02 18:17:32 +00001055 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +01001056
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001057 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +01001058 case SNDRV_CTL_POWER_D3hot:
1059 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +01001060 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001061 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001062 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +01001063 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +01001064 default:
1065 return 1;
1066 }
1067}
1068
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001069static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1070 struct list_head *widgets)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001071{
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001072 struct snd_soc_dapm_widget *w;
1073 struct list_head *it;
1074 unsigned int size = 0;
1075 unsigned int i = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001076
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001077 list_for_each(it, widgets)
1078 size++;
1079
1080 *list = kzalloc(sizeof(**list) + size * sizeof(*w), GFP_KERNEL);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001081 if (*list == NULL)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001082 return -ENOMEM;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001083
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001084 list_for_each_entry(w, widgets, work_list)
1085 (*list)->widgets[i++] = w;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001086
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001087 (*list)->num_widgets = i;
1088
1089 return 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001090}
1091
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001092/*
1093 * Common implementation for is_connected_output_ep() and
1094 * is_connected_input_ep(). The function is inlined since the combined size of
1095 * the two specialized functions is only marginally larger then the size of the
1096 * generic function and at the same time the fast path of the specialized
1097 * functions is significantly smaller than the generic function.
1098 */
1099static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1100 struct list_head *list, enum snd_soc_dapm_direction dir,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001101 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1102 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1103 enum snd_soc_dapm_direction)),
1104 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1105 enum snd_soc_dapm_direction))
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001106{
1107 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001108 struct snd_soc_dapm_path *path;
1109 int con = 0;
1110
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001111 if (widget->endpoints[dir] >= 0)
1112 return widget->endpoints[dir];
Mark Brown024dc072011-10-09 11:52:05 +01001113
Mark Brownde02d072011-09-20 21:43:24 +01001114 DAPM_UPDATE_STAT(widget, path_checks);
1115
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001116 /* do we need to add this widget to the list ? */
1117 if (list)
1118 list_add_tail(&widget->work_list, list);
1119
Jeeja KP09464972016-06-15 11:16:55 +05301120 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1121 widget->endpoints[dir] = 1;
1122 return widget->endpoints[dir];
1123 }
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001124
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001125 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1126 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1127 return widget->endpoints[dir];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001128 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001129
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001130 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
Mark Browne56235e02011-09-21 18:19:14 +01001131 DAPM_UPDATE_STAT(widget, neighbour_checks);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001132
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001133 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +01001134 continue;
1135
Mark Brown8af294b2013-02-22 17:48:15 +00001136 if (path->walking)
1137 return 1;
1138
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001139 trace_snd_soc_dapm_path(widget, dir, path);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001140
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001141 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +00001142 path->walking = 1;
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001143 con += fn(path->node[dir], list, custom_stop_condition);
Mark Brown8af294b2013-02-22 17:48:15 +00001144 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001145 }
1146 }
1147
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001148 widget->endpoints[dir] = con;
Mark Brown024dc072011-10-09 11:52:05 +01001149
Richard Purdie2b97eab2006-10-06 18:32:18 +02001150 return con;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001151}
1152
1153/*
1154 * Recursively check for a completed path to an active or physically connected
1155 * output widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001156 *
1157 * Optionally, can be supplied with a function acting as a stopping condition.
1158 * This function takes the dapm widget currently being examined and the walk
1159 * direction as an arguments, it should return true if the walk should be
1160 * stopped and false otherwise.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001161 */
1162static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001163 struct list_head *list,
1164 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1165 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001166{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001167 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001168 is_connected_output_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001169}
1170
1171/*
1172 * Recursively check for a completed path to an active or physically connected
1173 * input widget. Returns number of complete paths.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001174 *
1175 * Optionally, can be supplied with a function acting as a stopping condition.
1176 * This function takes the dapm widget currently being examined and the walk
1177 * direction as an arguments, it should return true if the walk should be
1178 * stopped and false otherwise.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001179 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001180static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001181 struct list_head *list,
1182 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1183 enum snd_soc_dapm_direction))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001184{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001185 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001186 is_connected_input_ep, custom_stop_condition);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001187}
1188
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001189/**
1190 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1191 * @dai: the soc DAI.
1192 * @stream: stream direction.
1193 * @list: list of active widgets for this stream.
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001194 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1195 * walk based on custom logic.
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001196 *
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001197 * Queries DAPM graph as to whether a valid audio stream path exists for
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001198 * the initial stream specified by name. This takes into account
1199 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1200 *
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001201 * Optionally, can be supplied with a function acting as a stopping condition.
1202 * This function takes the dapm widget currently being examined and the walk
1203 * direction as an arguments, it should return true if the walk should be
1204 * stopped and false otherwise.
1205 *
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001206 * Returns the number of valid paths or negative error.
1207 */
1208int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001209 struct snd_soc_dapm_widget_list **list,
1210 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1211 enum snd_soc_dapm_direction))
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001212{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01001213 struct snd_soc_card *card = dai->component->card;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001214 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001215 LIST_HEAD(widgets);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001216 int paths;
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001217 int ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001218
1219 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001220
1221 /*
1222 * For is_connected_{output,input}_ep fully discover the graph we need
1223 * to reset the cached number of inputs and outputs.
1224 */
1225 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001226 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1227 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001228 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001229
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001230 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001231 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1232 custom_stop_condition);
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001233 else
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001234 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1235 custom_stop_condition);
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001236
1237 /* Drop starting point */
1238 list_del(widgets.next);
1239
1240 ret = dapm_widget_list_create(list, &widgets);
1241 if (ret)
Lars-Peter Clausen30abbe72015-08-11 21:37:59 +02001242 paths = ret;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001243
1244 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001245 mutex_unlock(&card->dapm_mutex);
1246
1247 return paths;
1248}
1249
Richard Purdie2b97eab2006-10-06 18:32:18 +02001250/*
Mark Brown62ea8742012-01-21 21:14:48 +00001251 * Handler for regulator supply widget.
1252 */
1253int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1254 struct snd_kcontrol *kcontrol, int event)
1255{
Mark Brownc05b84d2012-09-07 12:57:11 +08001256 int ret;
1257
Mark Browneb270e92013-10-09 13:52:52 +01001258 soc_dapm_async_complete(w->dapm);
1259
Mark Brownc05b84d2012-09-07 12:57:11 +08001260 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001261 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001262 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001263 if (ret != 0)
1264 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001265 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001266 w->name, ret);
1267 }
1268
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001269 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001270 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001271 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001272 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001273 if (ret != 0)
1274 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001275 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001276 w->name, ret);
1277 }
1278
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001279 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001280 }
Mark Brown62ea8742012-01-21 21:14:48 +00001281}
1282EXPORT_SYMBOL_GPL(dapm_regulator_event);
1283
Ola Liljad7e7eb92012-05-24 15:26:25 +02001284/*
1285 * Handler for clock supply widget.
1286 */
1287int dapm_clock_event(struct snd_soc_dapm_widget *w,
1288 struct snd_kcontrol *kcontrol, int event)
1289{
1290 if (!w->clk)
1291 return -EIO;
1292
Mark Browneb270e92013-10-09 13:52:52 +01001293 soc_dapm_async_complete(w->dapm);
1294
Mark Brownec029952012-06-04 08:16:20 +01001295#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001296 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001297 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001298 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001299 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001300 return 0;
1301 }
Mark Brownec029952012-06-04 08:16:20 +01001302#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001303 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001304}
1305EXPORT_SYMBOL_GPL(dapm_clock_event);
1306
Mark Brownd8050022011-09-28 18:28:23 +01001307static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1308{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001309 if (w->power_checked)
1310 return w->new_power;
1311
Mark Brownd8050022011-09-28 18:28:23 +01001312 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001313 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001314 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001315 w->new_power = w->power_check(w);
1316
1317 w->power_checked = true;
1318
1319 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001320}
1321
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001322/* Generic check to see if a widget should be powered. */
Mark Browncd0f2d42009-04-20 16:56:59 +01001323static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1324{
1325 int in, out;
1326
Mark Brownde02d072011-09-20 21:43:24 +01001327 DAPM_UPDATE_STAT(w, power_checks);
1328
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001329 in = is_connected_input_ep(w, NULL, NULL);
1330 out = is_connected_output_ep(w, NULL, NULL);
Mark Browncd0f2d42009-04-20 16:56:59 +01001331 return out != 0 && in != 0;
1332}
1333
Mark Brown246d0a12009-04-22 18:24:55 +01001334/* Check to see if a power supply is needed */
1335static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1336{
1337 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001338
Mark Brownde02d072011-09-20 21:43:24 +01001339 DAPM_UPDATE_STAT(w, power_checks);
1340
Mark Brown246d0a12009-04-22 18:24:55 +01001341 /* Check if one of our outputs is connected */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001342 snd_soc_dapm_widget_for_each_sink_path(w, path) {
Mark Browna8fdac82011-09-28 18:20:26 +01001343 DAPM_UPDATE_STAT(w, neighbour_checks);
1344
Mark Brownbf3a9e12011-06-13 16:42:29 +01001345 if (path->weak)
1346 continue;
1347
Mark Brown215edda2009-09-08 18:59:05 +01001348 if (path->connected &&
1349 !path->connected(path->source, path->sink))
1350 continue;
1351
Mark Brownf68d7e12011-10-04 22:57:50 +01001352 if (dapm_widget_power_check(path->sink))
1353 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001354 }
1355
Mark Brownf68d7e12011-10-04 22:57:50 +01001356 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001357}
1358
Mark Brown35c64bc2011-09-28 18:23:53 +01001359static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1360{
Charles Keepax20bb0182015-12-02 10:22:16 +00001361 return w->connected;
Mark Brown35c64bc2011-09-28 18:23:53 +01001362}
1363
Mark Brown38357ab2009-06-06 19:03:23 +01001364static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1365 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001366 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001367{
Mark Brown828a8422011-01-15 13:14:30 +00001368 int *sort;
1369
1370 if (power_up)
1371 sort = dapm_up_seq;
1372 else
1373 sort = dapm_down_seq;
1374
Mark Brown38357ab2009-06-06 19:03:23 +01001375 if (sort[a->id] != sort[b->id])
1376 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001377 if (a->subseq != b->subseq) {
1378 if (power_up)
1379 return a->subseq - b->subseq;
1380 else
1381 return b->subseq - a->subseq;
1382 }
Mark Brownb22ead22009-06-07 12:51:26 +01001383 if (a->reg != b->reg)
1384 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001385 if (a->dapm != b->dapm)
1386 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001387
Mark Brown38357ab2009-06-06 19:03:23 +01001388 return 0;
1389}
Mark Brown42aa3412009-03-01 19:21:10 +00001390
Mark Brown38357ab2009-06-06 19:03:23 +01001391/* Insert a widget in order into a DAPM power sequence. */
1392static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1393 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001394 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001395{
1396 struct snd_soc_dapm_widget *w;
1397
1398 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001399 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001400 list_add_tail(&new_widget->power_list, &w->power_list);
1401 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001402 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001403
Mark Brown38357ab2009-06-06 19:03:23 +01001404 list_add_tail(&new_widget->power_list, list);
1405}
Mark Brown42aa3412009-03-01 19:21:10 +00001406
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001407static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001408 struct snd_soc_dapm_widget *w, int event)
1409{
Mark Brown68f89ad2010-11-03 23:51:49 -04001410 const char *ev_name;
1411 int power, ret;
1412
1413 switch (event) {
1414 case SND_SOC_DAPM_PRE_PMU:
1415 ev_name = "PRE_PMU";
1416 power = 1;
1417 break;
1418 case SND_SOC_DAPM_POST_PMU:
1419 ev_name = "POST_PMU";
1420 power = 1;
1421 break;
1422 case SND_SOC_DAPM_PRE_PMD:
1423 ev_name = "PRE_PMD";
1424 power = 0;
1425 break;
1426 case SND_SOC_DAPM_POST_PMD:
1427 ev_name = "POST_PMD";
1428 power = 0;
1429 break;
Mark Brown80114122013-02-25 15:14:19 +00001430 case SND_SOC_DAPM_WILL_PMU:
1431 ev_name = "WILL_PMU";
1432 power = 1;
1433 break;
1434 case SND_SOC_DAPM_WILL_PMD:
1435 ev_name = "WILL_PMD";
1436 power = 0;
1437 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001438 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001439 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001440 return;
1441 }
1442
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001443 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001444 return;
1445
1446 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001447 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001448 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001449 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001450 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001451 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001452 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001453 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001454 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001455 ev_name, w->name, ret);
1456 }
1457}
1458
Mark Brownb22ead22009-06-07 12:51:26 +01001459/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001460static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001461 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001462{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001463 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001464 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001465 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001466 unsigned int value = 0;
1467 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001468
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001469 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1470 reg = w->reg;
1471 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001472
1473 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001474 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001475 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001476
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001477 mask |= w->mask << w->shift;
1478 if (w->power)
1479 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001480 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001481 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001482
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001483 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001484 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1485 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001486
Mark Brown68f89ad2010-11-03 23:51:49 -04001487 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001488 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1489 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001490 }
1491
Mark Brown81628102009-06-07 13:21:24 +01001492 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001493 /* Any widget will do, they should all be updating the
1494 * same register.
1495 */
Mark Brown29376bc2011-06-19 13:49:28 +01001496
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001497 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001498 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001499 value, mask, reg, card->pop_time);
1500 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001501 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001502 }
1503
1504 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001505 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1506 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001507 }
Mark Brown42aa3412009-03-01 19:21:10 +00001508}
1509
Mark Brownb22ead22009-06-07 12:51:26 +01001510/* Apply a DAPM power sequence.
1511 *
1512 * We walk over a pre-sorted list of widgets to apply power to. In
1513 * order to minimise the number of writes to the device required
1514 * multiple widgets will be updated in a single write where possible.
1515 * Currently anything that requires more than a single write is not
1516 * handled.
1517 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001518static void dapm_seq_run(struct snd_soc_card *card,
1519 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001520{
1521 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001522 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001523 LIST_HEAD(pending);
1524 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001525 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001526 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001527 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001528 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001529 int *sort;
1530
1531 if (power_up)
1532 sort = dapm_up_seq;
1533 else
1534 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001535
Mark Brownb22ead22009-06-07 12:51:26 +01001536 list_for_each_entry_safe(w, n, list, power_list) {
1537 ret = 0;
1538
1539 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001540 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001541 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001542 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001543 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001544
Mark Brown474b62d2011-01-18 16:14:44 +00001545 if (cur_dapm && cur_dapm->seq_notifier) {
1546 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1547 if (sort[i] == cur_sort)
1548 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001549 i,
1550 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001551 }
1552
Mark Browneb270e92013-10-09 13:52:52 +01001553 if (cur_dapm && w->dapm != cur_dapm)
1554 soc_dapm_async_complete(cur_dapm);
1555
Mark Brownb22ead22009-06-07 12:51:26 +01001556 INIT_LIST_HEAD(&pending);
1557 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001558 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001559 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001560 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001561 }
1562
Mark Brown163cac02009-06-07 10:12:52 +01001563 switch (w->id) {
1564 case snd_soc_dapm_pre:
1565 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001566 list_for_each_entry_safe_continue(w, n, list,
1567 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001568
Mark Brownb22ead22009-06-07 12:51:26 +01001569 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001570 ret = w->event(w,
1571 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001572 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001573 ret = w->event(w,
1574 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001575 break;
1576
1577 case snd_soc_dapm_post:
1578 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001579 list_for_each_entry_safe_continue(w, n, list,
1580 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001581
Mark Brownb22ead22009-06-07 12:51:26 +01001582 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001583 ret = w->event(w,
1584 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001585 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001586 ret = w->event(w,
1587 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001588 break;
1589
Mark Brown163cac02009-06-07 10:12:52 +01001590 default:
Mark Brown81628102009-06-07 13:21:24 +01001591 /* Queue it up for application */
1592 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001593 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001594 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001595 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001596 list_move(&w->power_list, &pending);
1597 break;
Mark Brown163cac02009-06-07 10:12:52 +01001598 }
Mark Brownb22ead22009-06-07 12:51:26 +01001599
1600 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001601 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001602 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001603 }
Mark Brownb22ead22009-06-07 12:51:26 +01001604
1605 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001606 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001607
1608 if (cur_dapm && cur_dapm->seq_notifier) {
1609 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1610 if (sort[i] == cur_sort)
1611 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001612 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001613 }
Mark Browneb270e92013-10-09 13:52:52 +01001614
1615 list_for_each_entry(d, &card->dapm_list, list) {
1616 soc_dapm_async_complete(d);
1617 }
Mark Brown163cac02009-06-07 10:12:52 +01001618}
1619
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001620static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001621{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001622 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001623 struct snd_soc_dapm_widget_list *wlist;
1624 struct snd_soc_dapm_widget *w = NULL;
1625 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001626 int ret;
1627
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001628 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001629 return;
1630
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001631 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001632
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001633 for (wi = 0; wi < wlist->num_widgets; wi++) {
1634 w = wlist->widgets[wi];
1635
1636 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1637 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1638 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001639 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001640 w->name, ret);
1641 }
Mark Brown97404f22010-12-14 16:13:57 +00001642 }
1643
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001644 if (!w)
1645 return;
1646
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001647 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1648 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001649 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001650 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001651 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001652
Chen-Yu Tsaie411b0b2016-11-02 15:35:58 +08001653 if (update->has_second_set) {
1654 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1655 update->mask2, update->val2);
1656 if (ret < 0)
1657 dev_err(w->dapm->dev,
1658 "ASoC: %s DAPM update failed: %d\n",
1659 w->name, ret);
1660 }
1661
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001662 for (wi = 0; wi < wlist->num_widgets; wi++) {
1663 w = wlist->widgets[wi];
1664
1665 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1666 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1667 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001668 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001669 w->name, ret);
1670 }
Mark Brown97404f22010-12-14 16:13:57 +00001671 }
1672}
1673
Mark Brown9d0624a2011-02-18 11:49:43 -08001674/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1675 * they're changing state.
1676 */
1677static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1678{
1679 struct snd_soc_dapm_context *d = data;
1680 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001681
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001682 /* If we're off and we're not supposed to go into STANDBY */
Mark Brown56fba412011-06-04 11:25:10 +01001683 if (d->bias_level == SND_SOC_BIAS_OFF &&
1684 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001685 if (d->dev)
1686 pm_runtime_get_sync(d->dev);
1687
Mark Brown9d0624a2011-02-18 11:49:43 -08001688 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1689 if (ret != 0)
1690 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001691 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001692 }
1693
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001694 /* Prepare for a transition to ON or away from ON */
1695 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1696 d->bias_level != SND_SOC_BIAS_ON) ||
1697 (d->target_bias_level != SND_SOC_BIAS_ON &&
1698 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001699 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1700 if (ret != 0)
1701 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001702 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001703 }
1704}
1705
1706/* Async callback run prior to DAPM sequences - brings to their final
1707 * state.
1708 */
1709static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1710{
1711 struct snd_soc_dapm_context *d = data;
1712 int ret;
1713
1714 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001715 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1716 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1717 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001718 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1719 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001720 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001721 ret);
1722 }
1723
1724 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001725 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1726 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001727 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1728 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001729 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1730 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001731
1732 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001733 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001734 }
1735
1736 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001737 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1738 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001739 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1740 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001741 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001742 ret);
1743 }
1744}
Mark Brown97404f22010-12-14 16:13:57 +00001745
Mark Brownfe4fda52011-10-03 22:36:57 +01001746static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1747 bool power, bool connect)
1748{
1749 /* If a connection is being made or broken then that update
1750 * will have marked the peer dirty, otherwise the widgets are
1751 * not connected and this update has no impact. */
1752 if (!connect)
1753 return;
1754
1755 /* If the peer is already in the state we're moving to then we
1756 * won't have an impact on it. */
1757 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001758 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001759}
1760
Mark Brown05623c42011-09-28 17:02:31 +01001761static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1762 struct list_head *up_list,
1763 struct list_head *down_list)
1764{
Mark Browndb432b42011-10-03 21:06:40 +01001765 struct snd_soc_dapm_path *path;
1766
Mark Brown05623c42011-09-28 17:02:31 +01001767 if (w->power == power)
1768 return;
1769
1770 trace_snd_soc_dapm_widget_power(w, power);
1771
Mark Browndb432b42011-10-03 21:06:40 +01001772 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001773 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001774 */
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001775 snd_soc_dapm_widget_for_each_source_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001776 dapm_widget_set_peer_power(path->source, power, path->connect);
1777
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001778 /* Supplies can't affect their outputs, only their inputs */
1779 if (!w->is_supply) {
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02001780 snd_soc_dapm_widget_for_each_sink_path(w, path)
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001781 dapm_widget_set_peer_power(path->sink, power,
1782 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001783 }
1784
Mark Brown05623c42011-09-28 17:02:31 +01001785 if (power)
1786 dapm_seq_insert(w, up_list, true);
1787 else
1788 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001789}
1790
Mark Brown7c81beb2011-09-20 22:22:32 +01001791static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1792 struct list_head *up_list,
1793 struct list_head *down_list)
1794{
Mark Brown7c81beb2011-09-20 22:22:32 +01001795 int power;
1796
1797 switch (w->id) {
1798 case snd_soc_dapm_pre:
1799 dapm_seq_insert(w, down_list, false);
1800 break;
1801 case snd_soc_dapm_post:
1802 dapm_seq_insert(w, up_list, true);
1803 break;
1804
1805 default:
Mark Brownd8050022011-09-28 18:28:23 +01001806 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001807
Mark Brown05623c42011-09-28 17:02:31 +01001808 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001809 break;
1810 }
1811}
1812
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001813static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1814{
1815 if (dapm->idle_bias_off)
1816 return true;
1817
1818 switch (snd_power_get_state(dapm->card->snd_card)) {
1819 case SNDRV_CTL_POWER_D3hot:
1820 case SNDRV_CTL_POWER_D3cold:
1821 return dapm->suspend_bias_off;
1822 default:
1823 break;
1824 }
1825
1826 return false;
1827}
1828
Mark Brown42aa3412009-03-01 19:21:10 +00001829/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001830 * Scan each dapm widget for complete audio path.
1831 * A complete path is a route that has valid endpoints i.e.:-
1832 *
1833 * o DAC to output pin.
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02001834 * o Input pin to ADC.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001835 * o Input pin to Output pin (bypass, sidetone)
1836 * o DAC to ADC (loopback).
1837 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001838static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001839{
1840 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001841 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001842 LIST_HEAD(up_list);
1843 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001844 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001845 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001846
Mark Brownf9fa2b12014-03-06 16:49:11 +08001847 lockdep_assert_held(&card->dapm_mutex);
1848
Mark Brown84e90932010-11-04 00:07:02 -04001849 trace_snd_soc_dapm_start(card);
1850
Mark Brown56fba412011-06-04 11:25:10 +01001851 list_for_each_entry(d, &card->dapm_list, list) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001852 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001853 d->target_bias_level = SND_SOC_BIAS_OFF;
1854 else
1855 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001856 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001857
Liam Girdwood6c120e12012-02-15 15:15:34 +00001858 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001859
Mark Brown6d3ddc82009-05-16 17:47:29 +01001860 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001861 * lists indicating if they should be powered up or down. We
1862 * only check widgets that have been flagged as dirty but note
1863 * that new widgets may be added to the dirty list while we
1864 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001865 */
Mark Browndb432b42011-10-03 21:06:40 +01001866 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001867 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001868 }
1869
Mark Brownf9de6d72011-09-28 17:19:47 +01001870 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001871 switch (w->id) {
1872 case snd_soc_dapm_pre:
1873 case snd_soc_dapm_post:
1874 /* These widgets always need to be powered */
1875 break;
1876 default:
1877 list_del_init(&w->dirty);
1878 break;
1879 }
Mark Browndb432b42011-10-03 21:06:40 +01001880
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001881 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001882 d = w->dapm;
1883
1884 /* Supplies and micbiases only bring the
1885 * context up to STANDBY as unless something
1886 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001887 * generally don't require full power. Signal
1888 * generators are virtual pins and have no
1889 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001890 */
1891 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001892 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001893 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001894 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001895 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001896 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001897 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001898 case snd_soc_dapm_micbias:
1899 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1900 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1901 break;
1902 default:
1903 d->target_bias_level = SND_SOC_BIAS_ON;
1904 break;
1905 }
1906 }
1907
1908 }
1909
Mark Brown85a843c2011-09-21 21:29:47 +01001910 /* Force all contexts in the card to the same bias state if
1911 * they're not ground referenced.
1912 */
Mark Brown56fba412011-06-04 11:25:10 +01001913 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001914 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001915 if (d->target_bias_level > bias)
1916 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001917 list_for_each_entry(d, &card->dapm_list, list)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001918 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01001919 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001920
Mark Brownde02d072011-09-20 21:43:24 +01001921 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001922
Xiang Xiao17282ba2014-03-02 00:04:03 +08001923 /* Run card bias changes at first */
1924 dapm_pre_sequence_async(&card->dapm, 0);
1925 /* Run other bias changes in parallel */
1926 list_for_each_entry(d, &card->dapm_list, list) {
1927 if (d != &card->dapm)
1928 async_schedule_domain(dapm_pre_sequence_async, d,
1929 &async_domain);
1930 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001931 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001932
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001933 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001934 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001935 }
1936
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001937 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001938 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001939 }
1940
Mark Brown6d3ddc82009-05-16 17:47:29 +01001941 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001942 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001943
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001944 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001945
Mark Brown6d3ddc82009-05-16 17:47:29 +01001946 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001947 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001948
Mark Brown9d0624a2011-02-18 11:49:43 -08001949 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001950 list_for_each_entry(d, &card->dapm_list, list) {
1951 if (d != &card->dapm)
1952 async_schedule_domain(dapm_post_sequence_async, d,
1953 &async_domain);
1954 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001955 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001956 /* Run card bias changes at last */
1957 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001958
Liam Girdwood8078d872012-02-15 15:15:35 +00001959 /* do we need to notify any clients that DAPM event is complete */
1960 list_for_each_entry(d, &card->dapm_list, list) {
1961 if (d->stream_event)
1962 d->stream_event(d, event);
1963 }
1964
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001965 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001966 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001967 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001968
Mark Brown84e90932010-11-04 00:07:02 -04001969 trace_snd_soc_dapm_done(card);
1970
Mark Brown42aa3412009-03-01 19:21:10 +00001971 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001972}
1973
Mark Brown79fb9382009-08-21 16:38:13 +01001974#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001975static ssize_t dapm_widget_power_read_file(struct file *file,
1976 char __user *user_buf,
1977 size_t count, loff_t *ppos)
1978{
1979 struct snd_soc_dapm_widget *w = file->private_data;
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02001980 struct snd_soc_card *card = w->dapm->card;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02001981 enum snd_soc_dapm_direction dir, rdir;
Mark Brown79fb9382009-08-21 16:38:13 +01001982 char *buf;
1983 int in, out;
1984 ssize_t ret;
1985 struct snd_soc_dapm_path *p = NULL;
1986
1987 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1988 if (!buf)
1989 return -ENOMEM;
1990
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02001991 mutex_lock(&card->dapm_mutex);
1992
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001993 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
1994 if (w->is_supply) {
1995 in = 0;
1996 out = 0;
1997 } else {
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001998 in = is_connected_input_ep(w, NULL, NULL);
1999 out = is_connected_output_ep(w, NULL, NULL);
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002000 }
Mark Brown79fb9382009-08-21 16:38:13 +01002001
Mark Brownf13ebad2012-03-03 18:01:01 +00002002 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2003 w->name, w->power ? "On" : "Off",
2004 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01002005
Mark Brownd033c362009-12-04 15:25:56 +00002006 if (w->reg >= 0)
2007 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002008 " - R%d(0x%x) mask 0x%x",
2009 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00002010
2011 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
2012
Mark Brown3eef08b2009-09-14 16:49:00 +01002013 if (w->sname)
2014 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2015 w->sname,
2016 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01002017
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002018 snd_soc_dapm_for_each_direction(dir) {
2019 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2020 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2021 if (p->connected && !p->connected(w, p->node[rdir]))
2022 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002023
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002024 if (!p->connect)
2025 continue;
Mark Brown215edda2009-09-08 18:59:05 +01002026
Mark Brown79fb9382009-08-21 16:38:13 +01002027 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002028 " %s \"%s\" \"%s\"\n",
2029 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
Mark Brown79fb9382009-08-21 16:38:13 +01002030 p->name ? p->name : "static",
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002031 p->node[rdir]->name);
2032 }
Mark Brown79fb9382009-08-21 16:38:13 +01002033 }
2034
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002035 mutex_unlock(&card->dapm_mutex);
2036
Mark Brown79fb9382009-08-21 16:38:13 +01002037 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2038
2039 kfree(buf);
2040 return ret;
2041}
2042
2043static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002044 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002045 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002046 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002047};
2048
Mark Brownef49e4f2011-04-04 20:48:13 +09002049static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2050 size_t count, loff_t *ppos)
2051{
2052 struct snd_soc_dapm_context *dapm = file->private_data;
2053 char *level;
2054
2055 switch (dapm->bias_level) {
2056 case SND_SOC_BIAS_ON:
2057 level = "On\n";
2058 break;
2059 case SND_SOC_BIAS_PREPARE:
2060 level = "Prepare\n";
2061 break;
2062 case SND_SOC_BIAS_STANDBY:
2063 level = "Standby\n";
2064 break;
2065 case SND_SOC_BIAS_OFF:
2066 level = "Off\n";
2067 break;
2068 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002069 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002070 level = "Unknown\n";
2071 break;
2072 }
2073
2074 return simple_read_from_buffer(user_buf, count, ppos, level,
2075 strlen(level));
2076}
2077
2078static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002079 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002080 .read = dapm_bias_read_file,
2081 .llseek = default_llseek,
2082};
2083
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002084void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2085 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002086{
Mark Brown79fb9382009-08-21 16:38:13 +01002087 struct dentry *d;
2088
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02002089 if (!parent)
2090 return;
2091
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002092 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2093
2094 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002095 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002096 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002097 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002098 }
Mark Brown79fb9382009-08-21 16:38:13 +01002099
Mark Brownef49e4f2011-04-04 20:48:13 +09002100 d = debugfs_create_file("bias_level", 0444,
2101 dapm->debugfs_dapm, dapm,
2102 &dapm_bias_fops);
2103 if (!d)
2104 dev_warn(dapm->dev,
2105 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002106}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002107
2108static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2109{
2110 struct snd_soc_dapm_context *dapm = w->dapm;
2111 struct dentry *d;
2112
2113 if (!dapm->debugfs_dapm || !w->name)
2114 return;
2115
2116 d = debugfs_create_file(w->name, 0444,
2117 dapm->debugfs_dapm, w,
2118 &dapm_widget_power_fops);
2119 if (!d)
2120 dev_warn(w->dapm->dev,
2121 "ASoC: Failed to create %s debugfs file\n",
2122 w->name);
2123}
2124
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002125static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2126{
2127 debugfs_remove_recursive(dapm->debugfs_dapm);
2128}
2129
Mark Brown79fb9382009-08-21 16:38:13 +01002130#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002131void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2132 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002133{
2134}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002135
2136static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2137{
2138}
2139
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002140static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2141{
2142}
2143
Mark Brown79fb9382009-08-21 16:38:13 +01002144#endif
2145
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002146/*
2147 * soc_dapm_connect_path() - Connects or disconnects a path
2148 * @path: The path to update
2149 * @connect: The new connect state of the path. True if the path is connected,
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002150 * false if it is disconnected.
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002151 * @reason: The reason why the path changed (for debugging only)
2152 */
2153static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2154 bool connect, const char *reason)
2155{
2156 if (path->connect == connect)
2157 return;
2158
2159 path->connect = connect;
2160 dapm_mark_dirty(path->source, reason);
2161 dapm_mark_dirty(path->sink, reason);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002162 dapm_path_invalidate(path);
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002163}
2164
Richard Purdie2b97eab2006-10-06 18:32:18 +02002165/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002166static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002167 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002168{
2169 struct snd_soc_dapm_path *path;
2170 int found = 0;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002171 bool connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002172
Mark Brownf9fa2b12014-03-06 16:49:11 +08002173 lockdep_assert_held(&card->dapm_mutex);
2174
Richard Purdie2b97eab2006-10-06 18:32:18 +02002175 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002176 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002177 found = 1;
2178 /* we now need to match the string in the enum to the path */
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002179 if (!(strcmp(path->name, e->texts[mux])))
2180 connect = true;
2181 else
2182 connect = false;
2183
2184 soc_dapm_connect_path(path, connect, "mux update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002185 }
2186
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002187 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002188 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002189
Liam Girdwood618dae12012-04-25 12:12:51 +01002190 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002191}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002192
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002193int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002194 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2195 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002196{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002197 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002198 int ret;
2199
Liam Girdwood3cd04342012-03-09 12:02:08 +00002200 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002201 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002202 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002203 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002204 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002205 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002206 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002207 return ret;
2208}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002209EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002210
Milan plzik1b075e32008-01-10 14:39:46 +01002211/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002212static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002213 struct snd_kcontrol *kcontrol,
2214 int connect, int rconnect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002215{
2216 struct snd_soc_dapm_path *path;
2217 int found = 0;
2218
Mark Brownf9fa2b12014-03-06 16:49:11 +08002219 lockdep_assert_held(&card->dapm_mutex);
2220
Richard Purdie2b97eab2006-10-06 18:32:18 +02002221 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002222 dapm_kcontrol_for_each_path(path, kcontrol) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002223 /*
2224 * Ideally this function should support any number of
2225 * paths and channels. But since kcontrols only come
2226 * in mono and stereo variants, we are limited to 2
2227 * channels.
2228 *
2229 * The following code assumes for stereo controls the
2230 * first path (when 'found == 0') is the left channel,
2231 * and all remaining paths (when 'found == 1') are the
2232 * right channel.
2233 *
2234 * A stereo control is signified by a valid 'rconnect'
2235 * value, either 0 for unconnected, or >= 0 for connected.
2236 * This is chosen instead of using snd_soc_volsw_is_stereo,
2237 * so that the behavior of snd_soc_dapm_mixer_update_power
2238 * doesn't change even when the kcontrol passed in is
2239 * stereo.
2240 *
2241 * It passes 'connect' as the path connect status for
2242 * the left channel, and 'rconnect' for the right
2243 * channel.
2244 */
2245 if (found && rconnect >= 0)
2246 soc_dapm_connect_path(path, rconnect, "mixer update");
2247 else
2248 soc_dapm_connect_path(path, connect, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002249 found = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002250 }
2251
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002252 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002253 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002254
Liam Girdwood618dae12012-04-25 12:12:51 +01002255 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002256}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002257
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002258int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002259 struct snd_kcontrol *kcontrol, int connect,
2260 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002261{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002262 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002263 int ret;
2264
Liam Girdwood3cd04342012-03-09 12:02:08 +00002265 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002266 card->update = update;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08002267 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002268 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002269 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002270 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002271 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002272 return ret;
2273}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002274EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002275
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002276static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2277 char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002278{
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002279 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002280 struct snd_soc_dapm_widget *w;
2281 int count = 0;
2282 char *state = "not set";
2283
Mark Brown47325072016-03-18 12:04:23 +00002284 /* card won't be set for the dummy component, as a spot fix
2285 * we're checking for that case specifically here but in future
2286 * we will ensure that the dummy component looks like others.
2287 */
2288 if (!cmpnt->card)
2289 return 0;
2290
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002291 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2292 if (w->dapm != dapm)
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002293 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002294
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002295 /* only display widgets that burn power */
Richard Purdie2b97eab2006-10-06 18:32:18 +02002296 switch (w->id) {
2297 case snd_soc_dapm_hp:
2298 case snd_soc_dapm_mic:
2299 case snd_soc_dapm_spk:
2300 case snd_soc_dapm_line:
2301 case snd_soc_dapm_micbias:
2302 case snd_soc_dapm_dac:
2303 case snd_soc_dapm_adc:
2304 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002305 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002306 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002307 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002308 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002309 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002310 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002311 if (w->name)
2312 count += sprintf(buf + count, "%s: %s\n",
2313 w->name, w->power ? "On":"Off");
2314 break;
2315 default:
2316 break;
2317 }
2318 }
2319
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002320 switch (snd_soc_dapm_get_bias_level(dapm)) {
Mark Brown0be98982008-05-19 12:31:28 +02002321 case SND_SOC_BIAS_ON:
2322 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002323 break;
Mark Brown0be98982008-05-19 12:31:28 +02002324 case SND_SOC_BIAS_PREPARE:
2325 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002326 break;
Mark Brown0be98982008-05-19 12:31:28 +02002327 case SND_SOC_BIAS_STANDBY:
2328 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002329 break;
Mark Brown0be98982008-05-19 12:31:28 +02002330 case SND_SOC_BIAS_OFF:
2331 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002332 break;
2333 }
2334 count += sprintf(buf + count, "PM State: %s\n", state);
2335
2336 return count;
2337}
2338
Benoit Cousson44ba2642014-07-08 23:19:36 +02002339/* show dapm widget status in sys fs */
2340static ssize_t dapm_widget_show(struct device *dev,
2341 struct device_attribute *attr, char *buf)
2342{
2343 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2344 int i, count = 0;
2345
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002346 mutex_lock(&rtd->card->dapm_mutex);
2347
Benoit Cousson44ba2642014-07-08 23:19:36 +02002348 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausenb3c25fb2015-07-06 15:38:10 +02002349 struct snd_soc_component *cmpnt = rtd->codec_dais[i]->component;
2350
2351 count += dapm_widget_show_component(cmpnt, buf + count);
Benoit Cousson44ba2642014-07-08 23:19:36 +02002352 }
2353
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002354 mutex_unlock(&rtd->card->dapm_mutex);
2355
Benoit Cousson44ba2642014-07-08 23:19:36 +02002356 return count;
2357}
2358
Richard Purdie2b97eab2006-10-06 18:32:18 +02002359static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2360
Takashi Iwaid29697d2015-01-30 20:16:37 +01002361struct attribute *soc_dapm_dev_attrs[] = {
2362 &dev_attr_dapm_widget.attr,
2363 NULL
2364};
Richard Purdie2b97eab2006-10-06 18:32:18 +02002365
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002366static void dapm_free_path(struct snd_soc_dapm_path *path)
2367{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002368 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2369 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002370 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002371 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002372 kfree(path);
2373}
2374
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002375void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2376{
2377 struct snd_soc_dapm_path *p, *next_p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002378 enum snd_soc_dapm_direction dir;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002379
2380 list_del(&w->list);
2381 /*
2382 * remove source and sink paths associated to this widget.
2383 * While removing the path, remove reference to it from both
2384 * source and sink widgets so that path is removed only once.
2385 */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002386 snd_soc_dapm_for_each_direction(dir) {
2387 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2388 dapm_free_path(p);
2389 }
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002390
2391 kfree(w->kcontrols);
Lars-Peter Clausen48068962015-07-21 18:11:08 +02002392 kfree_const(w->name);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002393 kfree(w);
2394}
2395
Jyri Sarhafd589a12015-11-10 18:12:42 +02002396void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2397{
2398 dapm->path_sink_cache.widget = NULL;
2399 dapm->path_source_cache.widget = NULL;
2400}
2401
Richard Purdie2b97eab2006-10-06 18:32:18 +02002402/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002403static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002404{
2405 struct snd_soc_dapm_widget *w, *next_w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002406
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002407 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2408 if (w->dapm != dapm)
2409 continue;
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002410 snd_soc_dapm_free_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002411 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002412 snd_soc_dapm_reset_cache(dapm);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002413}
2414
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002415static struct snd_soc_dapm_widget *dapm_find_widget(
2416 struct snd_soc_dapm_context *dapm, const char *pin,
2417 bool search_other_contexts)
2418{
2419 struct snd_soc_dapm_widget *w;
2420 struct snd_soc_dapm_widget *fallback = NULL;
2421
2422 list_for_each_entry(w, &dapm->card->widgets, list) {
2423 if (!strcmp(w->name, pin)) {
2424 if (w->dapm == dapm)
2425 return w;
2426 else
2427 fallback = w;
2428 }
2429 }
2430
2431 if (search_other_contexts)
2432 return fallback;
2433
2434 return NULL;
2435}
2436
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002437static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002438 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002439{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002440 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002441
Mark Brownf9fa2b12014-03-06 16:49:11 +08002442 dapm_assert_locked(dapm);
2443
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002444 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002445 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002446 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002447 }
2448
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002449 if (w->connected != status) {
Mark Brown1a8b2d92012-02-16 11:50:07 -08002450 dapm_mark_dirty(w, "pin configuration");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002451 dapm_widget_invalidate_input_paths(w);
2452 dapm_widget_invalidate_output_paths(w);
2453 }
Mark Brown1a8b2d92012-02-16 11:50:07 -08002454
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002455 w->connected = status;
2456 if (status == 0)
2457 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002458
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002459 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002460}
2461
Richard Purdie2b97eab2006-10-06 18:32:18 +02002462/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002463 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2464 * @dapm: DAPM context
2465 *
2466 * Walks all dapm audio paths and powers widgets according to their
2467 * stream or path usage.
2468 *
2469 * Requires external locking.
2470 *
2471 * Returns 0 for success.
2472 */
2473int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2474{
2475 /*
2476 * Suppress early reports (eg, jacks syncing their state) to avoid
2477 * silly DAPM runs during card startup.
2478 */
2479 if (!dapm->card || !dapm->card->instantiated)
2480 return 0;
2481
2482 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2483}
2484EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2485
2486/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002487 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002488 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002489 *
2490 * Walks all dapm audio paths and powers widgets according to their
2491 * stream or path usage.
2492 *
2493 * Returns 0 for success.
2494 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002495int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002496{
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002497 int ret;
2498
Liam Girdwood3cd04342012-03-09 12:02:08 +00002499 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002500 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002501 mutex_unlock(&dapm->card->dapm_mutex);
2502 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002503}
Liam Girdwooda5302182008-07-07 13:35:17 +01002504EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002505
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002506/*
2507 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2508 * @w: The widget for which to update the flags
2509 *
2510 * Some widgets have a dynamic category which depends on which neighbors they
2511 * are connected to. This function update the category for these widgets.
2512 *
2513 * This function must be called whenever a path is added or removed to a widget.
2514 */
2515static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2516{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002517 enum snd_soc_dapm_direction dir;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002518 struct snd_soc_dapm_path *p;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002519 unsigned int ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002520
2521 switch (w->id) {
2522 case snd_soc_dapm_input:
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02002523 /* On a fully routed card an input is never a source */
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002524 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002525 return;
2526 ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002527 snd_soc_dapm_widget_for_each_source_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002528 if (p->source->id == snd_soc_dapm_micbias ||
2529 p->source->id == snd_soc_dapm_mic ||
2530 p->source->id == snd_soc_dapm_line ||
2531 p->source->id == snd_soc_dapm_output) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002532 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002533 break;
2534 }
2535 }
2536 break;
2537 case snd_soc_dapm_output:
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002538 /* On a fully routed card a output is never a sink */
2539 if (w->dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002540 return;
2541 ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002542 snd_soc_dapm_widget_for_each_sink_path(w, p) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002543 if (p->sink->id == snd_soc_dapm_spk ||
2544 p->sink->id == snd_soc_dapm_hp ||
2545 p->sink->id == snd_soc_dapm_line ||
2546 p->sink->id == snd_soc_dapm_input) {
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002547 ep = 0;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002548 break;
2549 }
2550 }
2551 break;
2552 case snd_soc_dapm_line:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002553 ep = 0;
2554 snd_soc_dapm_for_each_direction(dir) {
2555 if (!list_empty(&w->edges[dir]))
2556 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2557 }
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002558 break;
2559 default:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002560 return;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002561 }
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002562
2563 w->is_ep = ep;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002564}
2565
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002566static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2567 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2568 const char *control)
2569{
2570 bool dynamic_source = false;
2571 bool dynamic_sink = false;
2572
2573 if (!control)
2574 return 0;
2575
2576 switch (source->id) {
2577 case snd_soc_dapm_demux:
2578 dynamic_source = true;
2579 break;
2580 default:
2581 break;
2582 }
2583
2584 switch (sink->id) {
2585 case snd_soc_dapm_mux:
2586 case snd_soc_dapm_switch:
2587 case snd_soc_dapm_mixer:
2588 case snd_soc_dapm_mixer_named_ctl:
2589 dynamic_sink = true;
2590 break;
2591 default:
2592 break;
2593 }
2594
2595 if (dynamic_source && dynamic_sink) {
2596 dev_err(dapm->dev,
2597 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2598 source->name, control, sink->name);
2599 return -EINVAL;
2600 } else if (!dynamic_source && !dynamic_sink) {
2601 dev_err(dapm->dev,
2602 "Control not supported for path %s -> [%s] -> %s\n",
2603 source->name, control, sink->name);
2604 return -EINVAL;
2605 }
2606
2607 return 0;
2608}
2609
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002610static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2611 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2612 const char *control,
2613 int (*connected)(struct snd_soc_dapm_widget *source,
2614 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002615{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002616 struct snd_soc_dapm_widget *widgets[2];
2617 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002618 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002619 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002620
Lars-Peter Clausene409dfb2014-10-25 17:42:02 +02002621 if (wsink->is_supply && !wsource->is_supply) {
2622 dev_err(dapm->dev,
2623 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2624 wsource->name, wsink->name);
2625 return -EINVAL;
2626 }
2627
2628 if (connected && !wsource->is_supply) {
2629 dev_err(dapm->dev,
2630 "connected() callback only supported for supply widgets (%s -> %s)\n",
2631 wsource->name, wsink->name);
2632 return -EINVAL;
2633 }
2634
2635 if (wsource->is_supply && control) {
2636 dev_err(dapm->dev,
2637 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2638 wsource->name, control, wsink->name);
2639 return -EINVAL;
2640 }
2641
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002642 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2643 if (ret)
2644 return ret;
2645
Richard Purdie2b97eab2006-10-06 18:32:18 +02002646 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2647 if (!path)
2648 return -ENOMEM;
2649
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002650 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2651 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2652 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2653 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2654
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002655 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002656 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002657 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002658
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002659 if (wsource->is_supply || wsink->is_supply)
2660 path->is_supply = 1;
2661
Richard Purdie2b97eab2006-10-06 18:32:18 +02002662 /* connect static paths */
2663 if (control == NULL) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002664 path->connect = 1;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002665 } else {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002666 switch (wsource->id) {
2667 case snd_soc_dapm_demux:
2668 ret = dapm_connect_mux(dapm, path, control, wsource);
2669 if (ret)
2670 goto err;
2671 break;
2672 default:
2673 break;
2674 }
2675
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002676 switch (wsink->id) {
2677 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002678 ret = dapm_connect_mux(dapm, path, control, wsink);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002679 if (ret != 0)
2680 goto err;
2681 break;
2682 case snd_soc_dapm_switch:
2683 case snd_soc_dapm_mixer:
2684 case snd_soc_dapm_mixer_named_ctl:
2685 ret = dapm_connect_mixer(dapm, path, control);
2686 if (ret != 0)
2687 goto err;
2688 break;
2689 default:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002690 break;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002691 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002692 }
2693
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002694 list_add(&path->list, &dapm->card->paths);
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002695 snd_soc_dapm_for_each_direction(dir)
2696 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002697
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02002698 snd_soc_dapm_for_each_direction(dir) {
2699 dapm_update_widget_flags(widgets[dir]);
2700 dapm_mark_dirty(widgets[dir], "Route added");
2701 }
Mark Brownfabd0382012-07-05 17:20:06 +01002702
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002703 if (dapm->card->instantiated && path->connect)
2704 dapm_path_invalidate(path);
2705
Richard Purdie2b97eab2006-10-06 18:32:18 +02002706 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002707err:
2708 kfree(path);
2709 return ret;
2710}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002711
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002712static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002713 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002714{
2715 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2716 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2717 const char *sink;
2718 const char *source;
2719 char prefixed_sink[80];
2720 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002721 const char *prefix;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002722 int ret;
2723
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002724 prefix = soc_dapm_prefix(dapm);
2725 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002726 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002727 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002728 sink = prefixed_sink;
2729 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002730 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002731 source = prefixed_source;
2732 } else {
2733 sink = route->sink;
2734 source = route->source;
2735 }
2736
Charles Keepax45a110a2015-05-11 13:50:30 +01002737 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2738 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2739
2740 if (wsink && wsource)
2741 goto skip_search;
2742
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002743 /*
2744 * find src and dest widgets over all widgets but favor a widget from
2745 * current DAPM context
2746 */
2747 list_for_each_entry(w, &dapm->card->widgets, list) {
2748 if (!wsink && !(strcmp(w->name, sink))) {
2749 wtsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002750 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002751 wsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002752 if (wsource)
2753 break;
2754 }
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002755 continue;
2756 }
2757 if (!wsource && !(strcmp(w->name, source))) {
2758 wtsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002759 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002760 wsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002761 if (wsink)
2762 break;
2763 }
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002764 }
2765 }
2766 /* use widget from another DAPM context if not found from this */
2767 if (!wsink)
2768 wsink = wtsink;
2769 if (!wsource)
2770 wsource = wtsource;
2771
2772 if (wsource == NULL) {
2773 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2774 route->source);
2775 return -ENODEV;
2776 }
2777 if (wsink == NULL) {
2778 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2779 route->sink);
2780 return -ENODEV;
2781 }
2782
Charles Keepax45a110a2015-05-11 13:50:30 +01002783skip_search:
2784 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2785 dapm_wcache_update(&dapm->path_source_cache, wsource);
2786
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002787 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2788 route->connected);
2789 if (ret)
2790 goto err;
2791
2792 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002793err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002794 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002795 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002796 return ret;
2797}
Mark Brown105f1c22008-05-13 14:52:19 +02002798
Mark Brownefcc3c62012-07-05 17:24:19 +01002799static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2800 const struct snd_soc_dapm_route *route)
2801{
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002802 struct snd_soc_dapm_widget *wsource, *wsink;
Mark Brownefcc3c62012-07-05 17:24:19 +01002803 struct snd_soc_dapm_path *path, *p;
2804 const char *sink;
2805 const char *source;
2806 char prefixed_sink[80];
2807 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002808 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002809
2810 if (route->control) {
2811 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002812 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002813 return -EINVAL;
2814 }
2815
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002816 prefix = soc_dapm_prefix(dapm);
2817 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002818 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002819 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002820 sink = prefixed_sink;
2821 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002822 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002823 source = prefixed_source;
2824 } else {
2825 sink = route->sink;
2826 source = route->source;
2827 }
2828
2829 path = NULL;
2830 list_for_each_entry(p, &dapm->card->paths, list) {
2831 if (strcmp(p->source->name, source) != 0)
2832 continue;
2833 if (strcmp(p->sink->name, sink) != 0)
2834 continue;
2835 path = p;
2836 break;
2837 }
2838
2839 if (path) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002840 wsource = path->source;
2841 wsink = path->sink;
2842
2843 dapm_mark_dirty(wsource, "Route removed");
2844 dapm_mark_dirty(wsink, "Route removed");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002845 if (path->connect)
2846 dapm_path_invalidate(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002847
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002848 dapm_free_path(path);
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002849
2850 /* Update any path related flags */
2851 dapm_update_widget_flags(wsource);
2852 dapm_update_widget_flags(wsink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002853 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002854 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002855 source, sink);
2856 }
2857
2858 return 0;
2859}
2860
Mark Brown105f1c22008-05-13 14:52:19 +02002861/**
Mark Brown105f1c22008-05-13 14:52:19 +02002862 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002863 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002864 * @route: audio routes
2865 * @num: number of routes
2866 *
2867 * Connects 2 dapm widgets together via a named audio path. The sink is
2868 * the widget receiving the audio signal, whilst the source is the sender
2869 * of the audio signal.
2870 *
2871 * Returns 0 for success else error. On error all resources can be freed
2872 * with a call to snd_soc_card_free().
2873 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002874int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002875 const struct snd_soc_dapm_route *route, int num)
2876{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002877 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002878
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002879 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002880 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002881 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002882 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002883 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2884 route->source,
2885 route->control ? route->control : "direct",
2886 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002887 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002888 }
2889 route++;
2890 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002891 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002892
Dan Carpenter60884c22012-04-13 22:25:43 +03002893 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002894}
2895EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2896
Mark Brownefcc3c62012-07-05 17:24:19 +01002897/**
2898 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2899 * @dapm: DAPM context
2900 * @route: audio routes
2901 * @num: number of routes
2902 *
2903 * Removes routes from the DAPM context.
2904 */
2905int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2906 const struct snd_soc_dapm_route *route, int num)
2907{
Rajan Vajae066ea22016-02-11 11:23:35 +05302908 int i;
Mark Brownefcc3c62012-07-05 17:24:19 +01002909
2910 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2911 for (i = 0; i < num; i++) {
2912 snd_soc_dapm_del_route(dapm, route);
2913 route++;
2914 }
2915 mutex_unlock(&dapm->card->dapm_mutex);
2916
Rajan Vajae066ea22016-02-11 11:23:35 +05302917 return 0;
Mark Brownefcc3c62012-07-05 17:24:19 +01002918}
2919EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2920
Mark Brownbf3a9e12011-06-13 16:42:29 +01002921static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2922 const struct snd_soc_dapm_route *route)
2923{
2924 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2925 route->source,
2926 true);
2927 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2928 route->sink,
2929 true);
2930 struct snd_soc_dapm_path *path;
2931 int count = 0;
2932
2933 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002934 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002935 route->source);
2936 return -ENODEV;
2937 }
2938
2939 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002940 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002941 route->sink);
2942 return -ENODEV;
2943 }
2944
2945 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002946 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002947 route->source, route->sink);
2948
Lars-Peter Clausene63bfd42015-07-26 19:05:00 +02002949 snd_soc_dapm_widget_for_each_sink_path(source, path) {
Mark Brownbf3a9e12011-06-13 16:42:29 +01002950 if (path->sink == sink) {
2951 path->weak = 1;
2952 count++;
2953 }
2954 }
2955
2956 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002957 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002958 route->source, route->sink);
2959 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002960 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002961 count, route->source, route->sink);
2962
2963 return 0;
2964}
2965
2966/**
2967 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2968 * @dapm: DAPM context
2969 * @route: audio routes
2970 * @num: number of routes
2971 *
2972 * Mark existing routes matching those specified in the passed array
2973 * as being weak, meaning that they are ignored for the purpose of
2974 * power decisions. The main intended use case is for sidetone paths
2975 * which couple audio between other independent paths if they are both
2976 * active in order to make the combination work better at the user
2977 * level but which aren't intended to be "used".
2978 *
2979 * Note that CODEC drivers should not use this as sidetone type paths
2980 * can frequently also be used as bypass paths.
2981 */
2982int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2983 const struct snd_soc_dapm_route *route, int num)
2984{
2985 int i, err;
2986 int ret = 0;
2987
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002988 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002989 for (i = 0; i < num; i++) {
2990 err = snd_soc_dapm_weak_route(dapm, route);
2991 if (err)
2992 ret = err;
2993 route++;
2994 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002995 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002996
2997 return ret;
2998}
2999EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3000
Mark Brown105f1c22008-05-13 14:52:19 +02003001/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003002 * snd_soc_dapm_new_widgets - add new dapm widgets
Jonathan Corbet628536e2015-08-25 01:14:48 -06003003 * @card: card to be checked for new dapm widgets
Richard Purdie2b97eab2006-10-06 18:32:18 +02003004 *
3005 * Checks the codec for any new dapm widgets and creates them if found.
3006 *
3007 * Returns 0 for success.
3008 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02003009int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003010{
3011 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00003012 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003013
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003014 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003015
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003016 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003017 {
3018 if (w->new)
3019 continue;
3020
Stephen Warrenfad59882011-04-28 17:37:59 -06003021 if (w->num_kcontrols) {
3022 w->kcontrols = kzalloc(w->num_kcontrols *
3023 sizeof(struct snd_kcontrol *),
3024 GFP_KERNEL);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003025 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003026 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06003027 return -ENOMEM;
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003028 }
Stephen Warrenfad59882011-04-28 17:37:59 -06003029 }
3030
Richard Purdie2b97eab2006-10-06 18:32:18 +02003031 switch(w->id) {
3032 case snd_soc_dapm_switch:
3033 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00003034 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003035 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003036 break;
3037 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003038 case snd_soc_dapm_demux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003039 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003040 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003041 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06003042 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02003043 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003044 break;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003045 case snd_soc_dapm_dai_link:
3046 dapm_new_dai_link(w);
3047 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01003048 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003049 break;
3050 }
Mark Brownb66a70d2011-02-09 18:04:11 +00003051
3052 /* Read the initial power state from the device */
3053 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003054 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08003055 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003056 val &= w->mask;
3057 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00003058 w->power = 1;
3059 }
3060
Richard Purdie2b97eab2006-10-06 18:32:18 +02003061 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003062
Mark Brown7508b122011-10-05 12:09:12 +01003063 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02003064 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003065 }
3066
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003067 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3068 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003069 return 0;
3070}
3071EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3072
3073/**
3074 * snd_soc_dapm_get_volsw - dapm mixer get callback
3075 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003076 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003077 *
3078 * Callback to get the value of a dapm mixer control.
3079 *
3080 * Returns 0 for success.
3081 */
3082int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3083 struct snd_ctl_elem_value *ucontrol)
3084{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003085 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3086 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003087 struct soc_mixer_control *mc =
3088 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003089 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003090 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003091 int max = mc->max;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003092 unsigned int width = fls(max);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003093 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003094 unsigned int invert = mc->invert;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003095 unsigned int reg_val, val, rval = 0;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003096 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003097
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003098 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003099 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003100 ret = soc_dapm_read(dapm, reg, &reg_val);
3101 val = (reg_val >> shift) & mask;
3102
3103 if (ret == 0 && reg != mc->rreg)
3104 ret = soc_dapm_read(dapm, mc->rreg, &reg_val);
3105
3106 if (snd_soc_volsw_is_stereo(mc))
3107 rval = (reg_val >> mc->rshift) & mask;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003108 } else {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003109 reg_val = dapm_kcontrol_get_value(kcontrol);
3110 val = reg_val & mask;
3111
3112 if (snd_soc_volsw_is_stereo(mc))
3113 rval = (reg_val >> width) & mask;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003114 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003115 mutex_unlock(&card->dapm_mutex);
3116
Chen-Yu Tsai01ad5e72016-08-27 19:27:58 +08003117 if (ret)
3118 return ret;
3119
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003120 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003121 ucontrol->value.integer.value[0] = max - val;
3122 else
3123 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003124
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003125 if (snd_soc_volsw_is_stereo(mc)) {
3126 if (invert)
3127 ucontrol->value.integer.value[1] = max - rval;
3128 else
3129 ucontrol->value.integer.value[1] = rval;
3130 }
3131
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003132 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003133}
3134EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3135
3136/**
3137 * snd_soc_dapm_put_volsw - dapm mixer set callback
3138 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003139 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003140 *
3141 * Callback to set the value of a dapm mixer control.
3142 *
3143 * Returns 0 for success.
3144 */
3145int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3146 struct snd_ctl_elem_value *ucontrol)
3147{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003148 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3149 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003150 struct soc_mixer_control *mc =
3151 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003152 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003153 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003154 int max = mc->max;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003155 unsigned int width = fls(max);
3156 unsigned int mask = (1 << width) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003157 unsigned int invert = mc->invert;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003158 unsigned int val, rval = 0;
3159 int connect, rconnect = -1, change, reg_change = 0;
Chen-Yu Tsaie411b0b2016-11-02 15:35:58 +08003160 struct snd_soc_dapm_update update = { NULL };
Nenghua Cao52765972013-12-13 20:13:49 +08003161 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003162
3163 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02003164 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003165
3166 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003167 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003168
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003169 if (snd_soc_volsw_is_stereo(mc)) {
3170 rval = (ucontrol->value.integer.value[1] & mask);
3171 rconnect = !!rval;
3172 if (invert)
3173 rval = max - rval;
3174 }
3175
Liam Girdwood3cd04342012-03-09 12:02:08 +00003176 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003177
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003178 /* This assumes field width < (bits in unsigned int / 2) */
3179 if (width > sizeof(unsigned int) * 8 / 2)
3180 dev_warn(dapm->dev,
3181 "ASoC: control %s field width limit exceeded\n",
3182 kcontrol->id.name);
3183 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
Mark Brown283375c2009-12-07 18:09:03 +00003184
Jarkko Nikula18626c72014-06-09 14:20:29 +03003185 if (reg != SND_SOC_NOPM) {
Jarkko Nikula18626c72014-06-09 14:20:29 +03003186 val = val << shift;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003187 rval = rval << mc->rshift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02003188
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003189 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3190
3191 if (snd_soc_volsw_is_stereo(mc))
3192 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3193 mask << mc->rshift,
3194 rval);
Jarkko Nikula18626c72014-06-09 14:20:29 +03003195 }
3196
3197 if (change || reg_change) {
3198 if (reg_change) {
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003199 if (snd_soc_volsw_is_stereo(mc)) {
3200 update.has_second_set = true;
3201 update.reg2 = mc->rreg;
3202 update.mask2 = mask << mc->rshift;
3203 update.val2 = rval;
3204 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003205 update.kcontrol = kcontrol;
3206 update.reg = reg;
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003207 update.mask = mask << shift;
Jarkko Nikula18626c72014-06-09 14:20:29 +03003208 update.val = val;
3209 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003210 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003211 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00003212
Chen-Yu Tsaie7aa4502016-11-02 15:35:59 +08003213 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3214 rconnect);
Mark Brown97404f22010-12-14 16:13:57 +00003215
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003216 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00003217 }
3218
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003219 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003220
3221 if (ret > 0)
3222 soc_dpcm_runtime_update(card);
3223
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003224 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003225}
3226EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3227
3228/**
3229 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3230 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003231 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003232 *
3233 * Callback to get the value of a dapm enumerated double mixer control.
3234 *
3235 * Returns 0 for success.
3236 */
3237int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3238 struct snd_ctl_elem_value *ucontrol)
3239{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003240 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Charles Keepax561ed682015-05-01 12:37:26 +01003241 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003242 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003243 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003244
Charles Keepax561ed682015-05-01 12:37:26 +01003245 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3246 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003247 int ret = soc_dapm_read(dapm, e->reg, &reg_val);
Charles Keepax964a0b82015-05-08 10:50:10 +01003248 if (ret) {
3249 mutex_unlock(&card->dapm_mutex);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003250 return ret;
Charles Keepax964a0b82015-05-08 10:50:10 +01003251 }
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003252 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003253 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003254 }
Charles Keepax561ed682015-05-01 12:37:26 +01003255 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003256
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003257 val = (reg_val >> e->shift_l) & e->mask;
3258 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3259 if (e->shift_l != e->shift_r) {
3260 val = (reg_val >> e->shift_r) & e->mask;
3261 val = snd_soc_enum_val_to_item(e, val);
3262 ucontrol->value.enumerated.item[1] = val;
3263 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003264
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003265 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003266}
3267EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3268
3269/**
3270 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3271 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003272 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003273 *
3274 * Callback to set the value of a dapm enumerated double mixer control.
3275 *
3276 * Returns 0 for success.
3277 */
3278int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3279 struct snd_ctl_elem_value *ucontrol)
3280{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003281 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3282 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003283 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003284 unsigned int *item = ucontrol->value.enumerated.item;
Charles Keepax561ed682015-05-01 12:37:26 +01003285 unsigned int val, change, reg_change = 0;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003286 unsigned int mask;
Chen-Yu Tsaie411b0b2016-11-02 15:35:58 +08003287 struct snd_soc_dapm_update update = { NULL };
Nenghua Cao52765972013-12-13 20:13:49 +08003288 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003289
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003290 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003291 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003292
3293 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003294 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003295 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003296 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003297 return -EINVAL;
Chen-Yu Tsai071133a2016-08-27 19:27:59 +08003298 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003299 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003300 }
3301
Liam Girdwood3cd04342012-03-09 12:02:08 +00003302 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003303
Charles Keepax561ed682015-05-01 12:37:26 +01003304 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00003305
Charles Keepax561ed682015-05-01 12:37:26 +01003306 if (e->reg != SND_SOC_NOPM)
3307 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3308
3309 if (change || reg_change) {
3310 if (reg_change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003311 update.kcontrol = kcontrol;
3312 update.reg = e->reg;
3313 update.mask = mask;
3314 update.val = val;
3315 card->update = &update;
3316 }
Charles Keepax561ed682015-05-01 12:37:26 +01003317 change |= reg_change;
Mark Brown3a655772009-10-05 17:23:30 +01003318
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003319 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003320
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003321 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003322 }
3323
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003324 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003325
3326 if (ret > 0)
3327 soc_dpcm_runtime_update(card);
3328
Mark Brown97404f22010-12-14 16:13:57 +00003329 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003330}
3331EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3332
3333/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003334 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3335 *
3336 * @kcontrol: mixer control
3337 * @uinfo: control element information
3338 *
3339 * Callback to provide information about a pin switch control.
3340 */
3341int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3342 struct snd_ctl_elem_info *uinfo)
3343{
3344 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3345 uinfo->count = 1;
3346 uinfo->value.integer.min = 0;
3347 uinfo->value.integer.max = 1;
3348
3349 return 0;
3350}
3351EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3352
3353/**
3354 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3355 *
3356 * @kcontrol: mixer control
3357 * @ucontrol: Value
3358 */
3359int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3360 struct snd_ctl_elem_value *ucontrol)
3361{
Mark Brown48a8c392012-02-14 17:11:15 -08003362 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003363 const char *pin = (const char *)kcontrol->private_value;
3364
Liam Girdwood3cd04342012-03-09 12:02:08 +00003365 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003366
3367 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003368 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003369
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003370 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003371
3372 return 0;
3373}
3374EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3375
3376/**
3377 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3378 *
3379 * @kcontrol: mixer control
3380 * @ucontrol: Value
3381 */
3382int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3383 struct snd_ctl_elem_value *ucontrol)
3384{
Mark Brown48a8c392012-02-14 17:11:15 -08003385 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003386 const char *pin = (const char *)kcontrol->private_value;
3387
Mark Brown8b37dbd2009-02-28 21:14:20 +00003388 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003389 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003390 else
Mark Brown48a8c392012-02-14 17:11:15 -08003391 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003392
Mark Brown48a8c392012-02-14 17:11:15 -08003393 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003394 return 0;
3395}
3396EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3397
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +01003398struct snd_soc_dapm_widget *
Mark Brown5ba06fc2012-02-16 11:07:13 -08003399snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003400 const struct snd_soc_dapm_widget *widget)
3401{
3402 struct snd_soc_dapm_widget *w;
3403
3404 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3405 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3406 if (!w)
3407 dev_err(dapm->dev,
3408 "ASoC: Failed to create DAPM control %s\n",
3409 widget->name);
3410
3411 mutex_unlock(&dapm->card->dapm_mutex);
3412 return w;
3413}
Subhransu S. Prustya5d56392016-06-27 09:18:03 +05303414EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003415
3416struct snd_soc_dapm_widget *
3417snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003418 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003419{
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003420 enum snd_soc_dapm_direction dir;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003421 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003422 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00003423 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003424
3425 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003426 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003427
Mark Brown62ea8742012-01-21 21:14:48 +00003428 switch (w->id) {
3429 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003430 w->regulator = devm_regulator_get(dapm->dev, w->name);
3431 if (IS_ERR(w->regulator)) {
3432 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003433 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003434 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003435 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003436 }
Mark Brown8784c772013-01-10 19:33:47 +00003437
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003438 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003439 ret = regulator_allow_bypass(w->regulator, true);
3440 if (ret != 0)
3441 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003442 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003443 w->name, ret);
3444 }
Mark Brown62ea8742012-01-21 21:14:48 +00003445 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003446 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003447#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003448 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003449 if (IS_ERR(w->clk)) {
3450 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003451 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003452 w->name, ret);
3453 return NULL;
3454 }
Mark Brownec029952012-06-04 08:16:20 +01003455#else
3456 return NULL;
3457#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003458 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003459 default:
3460 break;
3461 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003462
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003463 prefix = soc_dapm_prefix(dapm);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003464 if (prefix)
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003465 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003466 else
Lars-Peter Clausen48068962015-07-21 18:11:08 +02003467 w->name = kstrdup_const(widget->name, GFP_KERNEL);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003468 if (w->name == NULL) {
3469 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003470 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003471 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003472
Mark Brown7ca3a182011-10-08 14:04:50 +01003473 switch (w->id) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003474 case snd_soc_dapm_mic:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003475 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003476 w->power_check = dapm_generic_check_power;
3477 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003478 case snd_soc_dapm_input:
3479 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003480 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003481 w->power_check = dapm_generic_check_power;
3482 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003483 case snd_soc_dapm_spk:
3484 case snd_soc_dapm_hp:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003485 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003486 w->power_check = dapm_generic_check_power;
3487 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003488 case snd_soc_dapm_output:
3489 if (!dapm->card->fully_routed)
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003490 w->is_ep = SND_SOC_DAPM_EP_SINK;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003491 w->power_check = dapm_generic_check_power;
3492 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003493 case snd_soc_dapm_vmid:
3494 case snd_soc_dapm_siggen:
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003495 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003496 w->power_check = dapm_always_on_check_power;
3497 break;
Vinod Koul56b44372015-11-23 21:22:30 +05303498 case snd_soc_dapm_sink:
3499 w->is_ep = SND_SOC_DAPM_EP_SINK;
3500 w->power_check = dapm_always_on_check_power;
3501 break;
3502
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003503 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003504 case snd_soc_dapm_demux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003505 case snd_soc_dapm_switch:
3506 case snd_soc_dapm_mixer:
3507 case snd_soc_dapm_mixer_named_ctl:
Mark Brown63c69a62013-07-18 22:03:01 +01003508 case snd_soc_dapm_adc:
3509 case snd_soc_dapm_aif_out:
3510 case snd_soc_dapm_dac:
3511 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003512 case snd_soc_dapm_pga:
3513 case snd_soc_dapm_out_drv:
Mark Brown7ca3a182011-10-08 14:04:50 +01003514 case snd_soc_dapm_micbias:
Mark Brown7ca3a182011-10-08 14:04:50 +01003515 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003516 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02003517 case snd_soc_dapm_dai_out:
3518 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003519 w->power_check = dapm_generic_check_power;
3520 break;
3521 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003522 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003523 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003524 case snd_soc_dapm_kcontrol:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003525 w->is_supply = 1;
Mark Brown7ca3a182011-10-08 14:04:50 +01003526 w->power_check = dapm_supply_check_power;
3527 break;
3528 default:
3529 w->power_check = dapm_always_on_check_power;
3530 break;
3531 }
3532
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003533 w->dapm = dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003534 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003535 INIT_LIST_HEAD(&w->dirty);
Lars-Peter Clausen92fa1242015-05-01 18:02:42 +02003536 list_add_tail(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003537
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003538 snd_soc_dapm_for_each_direction(dir) {
3539 INIT_LIST_HEAD(&w->edges[dir]);
3540 w->endpoints[dir] = -1;
3541 }
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003542
Peter Meerwald-Stadlerc8046002016-08-16 16:56:17 +02003543 /* machine layer sets up unconnected pins and insertions */
Richard Purdie2b97eab2006-10-06 18:32:18 +02003544 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003545 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003546}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003547
3548/**
Mark Brown4ba13272008-05-13 14:51:19 +02003549 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003550 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003551 * @widget: widget array
3552 * @num: number of widgets
3553 *
3554 * Creates new DAPM controls based upon the templates.
3555 *
3556 * Returns 0 for success else error.
3557 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003558int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003559 const struct snd_soc_dapm_widget *widget,
3560 int num)
3561{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003562 struct snd_soc_dapm_widget *w;
3563 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003564 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003565
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003566 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003567 for (i = 0; i < num; i++) {
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003568 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003569 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003570 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003571 "ASoC: Failed to create DAPM control %s\n",
3572 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003573 ret = -ENOMEM;
3574 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003575 }
Mark Brown4ba13272008-05-13 14:51:19 +02003576 widget++;
3577 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003578 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003579 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003580}
3581EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3582
Mark Brownc74184e2012-04-04 22:12:09 +01003583static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3584 struct snd_kcontrol *kcontrol, int event)
3585{
3586 struct snd_soc_dapm_path *source_p, *sink_p;
3587 struct snd_soc_dai *source, *sink;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003588 const struct snd_soc_pcm_stream *config = w->params + w->params_select;
Mark Brownc74184e2012-04-04 22:12:09 +01003589 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003590 struct snd_pcm_hw_params *params = NULL;
Nicolin Chen8053f212016-07-26 14:55:51 -07003591 struct snd_pcm_runtime *runtime = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003592 u64 fmt;
3593 int ret;
3594
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003595 if (WARN_ON(!config) ||
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003596 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3597 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003598 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003599
3600 /* We only support a single source and sink, pick the first */
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02003601 source_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_OUT],
3602 struct snd_soc_dapm_path,
3603 list_node[SND_SOC_DAPM_DIR_OUT]);
3604 sink_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_IN],
3605 struct snd_soc_dapm_path,
3606 list_node[SND_SOC_DAPM_DIR_IN]);
Mark Brownc74184e2012-04-04 22:12:09 +01003607
3608 source = source_p->source->priv;
3609 sink = sink_p->sink->priv;
3610
3611 /* Be a little careful as we don't want to overflow the mask array */
3612 if (config->formats) {
3613 fmt = ffs(config->formats) - 1;
3614 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003615 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003616 config->formats);
3617 fmt = 0;
3618 }
3619
3620 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003621 params = kzalloc(sizeof(*params), GFP_KERNEL);
3622 if (!params) {
3623 ret = -ENOMEM;
3624 goto out;
3625 }
3626 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003627
Mark Brown9747cec2012-04-26 19:12:21 +01003628 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003629 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003630 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003631 config->rate_max;
3632
Mark Brown9747cec2012-04-26 19:12:21 +01003633 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003634 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003635 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003636 = config->channels_max;
3637
3638 memset(&substream, 0, sizeof(substream));
3639
Nicolin Chen8053f212016-07-26 14:55:51 -07003640 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3641 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3642 if (!runtime) {
3643 ret = -ENOMEM;
3644 goto out;
3645 }
3646 substream.runtime = runtime;
3647
Mark Brownc74184e2012-04-04 22:12:09 +01003648 switch (event) {
3649 case SND_SOC_DAPM_PRE_PMU:
Benoit Cousson93e69582014-07-08 23:19:38 +02003650 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303651 if (source->driver->ops && source->driver->ops->startup) {
3652 ret = source->driver->ops->startup(&substream, source);
3653 if (ret < 0) {
3654 dev_err(source->dev,
3655 "ASoC: startup() failed: %d\n", ret);
3656 goto out;
3657 }
3658 source->active++;
3659 }
Benoit Cousson93e69582014-07-08 23:19:38 +02003660 ret = soc_dai_hw_params(&substream, params, source);
3661 if (ret < 0)
3662 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003663
Benoit Cousson93e69582014-07-08 23:19:38 +02003664 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303665 if (sink->driver->ops && sink->driver->ops->startup) {
3666 ret = sink->driver->ops->startup(&substream, sink);
3667 if (ret < 0) {
3668 dev_err(sink->dev,
3669 "ASoC: startup() failed: %d\n", ret);
3670 goto out;
3671 }
3672 sink->active++;
3673 }
Benoit Cousson93e69582014-07-08 23:19:38 +02003674 ret = soc_dai_hw_params(&substream, params, sink);
3675 if (ret < 0)
3676 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003677 break;
3678
3679 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003680 ret = snd_soc_dai_digital_mute(sink, 0,
3681 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003682 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003683 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003684 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003685 break;
3686
3687 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003688 ret = snd_soc_dai_digital_mute(sink, 1,
3689 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003690 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003691 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003692 ret = 0;
Jeeja KP9b8ef9f2015-10-20 22:30:07 +05303693
3694 source->active--;
3695 if (source->driver->ops && source->driver->ops->shutdown) {
3696 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3697 source->driver->ops->shutdown(&substream, source);
3698 }
3699
3700 sink->active--;
3701 if (sink->driver->ops && sink->driver->ops->shutdown) {
3702 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3703 sink->driver->ops->shutdown(&substream, sink);
3704 }
Mark Brownc74184e2012-04-04 22:12:09 +01003705 break;
3706
3707 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003708 WARN(1, "Unknown event %d\n", event);
Sudip Mukherjee75881df2015-09-10 18:01:44 +05303709 ret = -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003710 }
3711
Mark Brown9747cec2012-04-26 19:12:21 +01003712out:
Nicolin Chen8053f212016-07-26 14:55:51 -07003713 kfree(runtime);
Mark Brown9747cec2012-04-26 19:12:21 +01003714 kfree(params);
3715 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003716}
3717
Nikesh Oswalc6615082015-02-02 17:06:44 +00003718static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3719 struct snd_ctl_elem_value *ucontrol)
3720{
3721 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3722
Takashi Iwai741338f2016-02-29 17:20:48 +01003723 ucontrol->value.enumerated.item[0] = w->params_select;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003724
3725 return 0;
3726}
3727
3728static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3729 struct snd_ctl_elem_value *ucontrol)
3730{
3731 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3732
3733 /* Can't change the config when widget is already powered */
3734 if (w->power)
3735 return -EBUSY;
3736
Takashi Iwai741338f2016-02-29 17:20:48 +01003737 if (ucontrol->value.enumerated.item[0] == w->params_select)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003738 return 0;
3739
Takashi Iwai741338f2016-02-29 17:20:48 +01003740 if (ucontrol->value.enumerated.item[0] >= w->num_params)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003741 return -EINVAL;
3742
Takashi Iwai741338f2016-02-29 17:20:48 +01003743 w->params_select = ucontrol->value.enumerated.item[0];
Nikesh Oswalc6615082015-02-02 17:06:44 +00003744
3745 return 0;
3746}
3747
Mark Brownc74184e2012-04-04 22:12:09 +01003748int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3749 const struct snd_soc_pcm_stream *params,
Nikesh Oswalc6615082015-02-02 17:06:44 +00003750 unsigned int num_params,
Mark Brownc74184e2012-04-04 22:12:09 +01003751 struct snd_soc_dapm_widget *source,
3752 struct snd_soc_dapm_widget *sink)
3753{
Mark Brownc74184e2012-04-04 22:12:09 +01003754 struct snd_soc_dapm_widget template;
3755 struct snd_soc_dapm_widget *w;
Mark Brownc74184e2012-04-04 22:12:09 +01003756 char *link_name;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003757 int ret, count;
3758 unsigned long private_value;
3759 const char **w_param_text;
3760 struct soc_enum w_param_enum[] = {
3761 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3762 };
3763 struct snd_kcontrol_new kcontrol_dai_link[] = {
3764 SOC_ENUM_EXT(NULL, w_param_enum[0],
3765 snd_soc_dapm_dai_link_get,
3766 snd_soc_dapm_dai_link_put),
3767 };
3768 const struct snd_soc_pcm_stream *config = params;
Mark Brownc74184e2012-04-04 22:12:09 +01003769
Nikesh Oswalc6615082015-02-02 17:06:44 +00003770 w_param_text = devm_kcalloc(card->dev, num_params,
3771 sizeof(char *), GFP_KERNEL);
3772 if (!w_param_text)
Mark Brownc74184e2012-04-04 22:12:09 +01003773 return -ENOMEM;
Mark Brownc74184e2012-04-04 22:12:09 +01003774
Charles Keepax46172b62015-03-25 11:22:35 +00003775 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3776 source->name, sink->name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003777 if (!link_name) {
3778 ret = -ENOMEM;
3779 goto outfree_w_param;
3780 }
Mark Brownc74184e2012-04-04 22:12:09 +01003781
Nikesh Oswalc6615082015-02-02 17:06:44 +00003782 for (count = 0 ; count < num_params; count++) {
3783 if (!config->stream_name) {
3784 dev_warn(card->dapm.dev,
3785 "ASoC: anonymous config %d for dai link %s\n",
3786 count, link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003787 w_param_text[count] =
Charles Keepax46172b62015-03-25 11:22:35 +00003788 devm_kasprintf(card->dev, GFP_KERNEL,
3789 "Anonymous Configuration %d",
3790 count);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003791 if (!w_param_text[count]) {
3792 ret = -ENOMEM;
3793 goto outfree_link_name;
3794 }
Nikesh Oswalc6615082015-02-02 17:06:44 +00003795 } else {
3796 w_param_text[count] = devm_kmemdup(card->dev,
3797 config->stream_name,
3798 strlen(config->stream_name) + 1,
3799 GFP_KERNEL);
3800 if (!w_param_text[count]) {
3801 ret = -ENOMEM;
3802 goto outfree_link_name;
3803 }
3804 }
3805 config++;
3806 }
3807 w_param_enum[0].items = num_params;
3808 w_param_enum[0].texts = w_param_text;
Mark Brownc74184e2012-04-04 22:12:09 +01003809
3810 memset(&template, 0, sizeof(template));
3811 template.reg = SND_SOC_NOPM;
3812 template.id = snd_soc_dapm_dai_link;
3813 template.name = link_name;
3814 template.event = snd_soc_dai_link_event;
3815 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3816 SND_SOC_DAPM_PRE_PMD;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003817 template.num_kcontrols = 1;
3818 /* duplicate w_param_enum on heap so that memory persists */
3819 private_value =
3820 (unsigned long) devm_kmemdup(card->dev,
3821 (void *)(kcontrol_dai_link[0].private_value),
3822 sizeof(struct soc_enum), GFP_KERNEL);
3823 if (!private_value) {
3824 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3825 link_name);
3826 ret = -ENOMEM;
3827 goto outfree_link_name;
3828 }
3829 kcontrol_dai_link[0].private_value = private_value;
3830 /* duplicate kcontrol_dai_link on heap so that memory persists */
3831 template.kcontrol_news =
3832 devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3833 sizeof(struct snd_kcontrol_new),
3834 GFP_KERNEL);
3835 if (!template.kcontrol_news) {
3836 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3837 link_name);
3838 ret = -ENOMEM;
3839 goto outfree_private_value;
3840 }
Mark Brownc74184e2012-04-04 22:12:09 +01003841
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003842 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003843
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003844 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
Mark Brownc74184e2012-04-04 22:12:09 +01003845 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003846 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003847 link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003848 ret = -ENOMEM;
3849 goto outfree_kcontrol_news;
Mark Brownc74184e2012-04-04 22:12:09 +01003850 }
3851
3852 w->params = params;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003853 w->num_params = num_params;
Mark Brownc74184e2012-04-04 22:12:09 +01003854
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003855 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3856 if (ret)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003857 goto outfree_w;
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003858 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003859
3860outfree_w:
3861 devm_kfree(card->dev, w);
3862outfree_kcontrol_news:
3863 devm_kfree(card->dev, (void *)template.kcontrol_news);
3864outfree_private_value:
3865 devm_kfree(card->dev, (void *)private_value);
3866outfree_link_name:
3867 devm_kfree(card->dev, link_name);
3868outfree_w_param:
3869 for (count = 0 ; count < num_params; count++)
3870 devm_kfree(card->dev, (void *)w_param_text[count]);
3871 devm_kfree(card->dev, w_param_text);
3872
3873 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003874}
3875
Mark Brown888df392012-02-16 19:37:51 -08003876int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3877 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003878{
Mark Brown888df392012-02-16 19:37:51 -08003879 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003880 struct snd_soc_dapm_widget *w;
3881
Mark Brown888df392012-02-16 19:37:51 -08003882 WARN_ON(dapm->dev != dai->dev);
3883
3884 memset(&template, 0, sizeof(template));
3885 template.reg = SND_SOC_NOPM;
3886
3887 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003888 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003889 template.name = dai->driver->playback.stream_name;
3890 template.sname = dai->driver->playback.stream_name;
3891
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003892 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003893 template.name);
3894
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003895 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Mark Brown888df392012-02-16 19:37:51 -08003896 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003897 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003898 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003899 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003900 }
3901
3902 w->priv = dai;
3903 dai->playback_widget = w;
3904 }
3905
3906 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003907 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003908 template.name = dai->driver->capture.stream_name;
3909 template.sname = dai->driver->capture.stream_name;
3910
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003911 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003912 template.name);
3913
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003914 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Mark Brown888df392012-02-16 19:37:51 -08003915 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003916 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003917 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003918 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003919 }
3920
3921 w->priv = dai;
3922 dai->capture_widget = w;
3923 }
3924
3925 return 0;
3926}
3927
3928int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3929{
3930 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003931 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08003932 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003933
3934 /* For each DAI widget... */
3935 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003936 switch (dai_w->id) {
3937 case snd_soc_dapm_dai_in:
3938 case snd_soc_dapm_dai_out:
3939 break;
3940 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003941 continue;
Mark Brown46162742013-06-05 19:36:11 +01003942 }
Mark Brown888df392012-02-16 19:37:51 -08003943
3944 dai = dai_w->priv;
3945
3946 /* ...find all widgets with the same stream and link them */
3947 list_for_each_entry(w, &card->widgets, list) {
3948 if (w->dapm != dai_w->dapm)
3949 continue;
3950
Mark Brown46162742013-06-05 19:36:11 +01003951 switch (w->id) {
3952 case snd_soc_dapm_dai_in:
3953 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003954 continue;
Mark Brown46162742013-06-05 19:36:11 +01003955 default:
3956 break;
3957 }
Mark Brown888df392012-02-16 19:37:51 -08003958
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003959 if (!w->sname || !strstr(w->sname, dai_w->sname))
Mark Brown888df392012-02-16 19:37:51 -08003960 continue;
3961
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003962 if (dai_w->id == snd_soc_dapm_dai_in) {
3963 src = dai_w;
3964 sink = w;
3965 } else {
3966 src = w;
3967 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08003968 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003969 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3970 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003971 }
3972 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003973
Mark Brown888df392012-02-16 19:37:51 -08003974 return 0;
3975}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003976
Benoit Cousson44ba2642014-07-08 23:19:36 +02003977static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3978 struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003979{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003980 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003981 struct snd_soc_dapm_widget *sink, *source;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003982 int i;
3983
Benoit Cousson44ba2642014-07-08 23:19:36 +02003984 for (i = 0; i < rtd->num_codecs; i++) {
3985 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003986
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003987 /* connect BE DAI playback if widgets are valid */
3988 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003989 source = cpu_dai->playback_widget;
3990 sink = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003991 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003992 cpu_dai->component->name, source->name,
3993 codec_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003994
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003995 snd_soc_dapm_add_path(&card->dapm, source, sink,
3996 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003997 }
3998
3999 /* connect BE DAI capture if widgets are valid */
4000 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02004001 source = codec_dai->capture_widget;
4002 sink = cpu_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004003 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004004 codec_dai->component->name, source->name,
4005 cpu_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004006
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02004007 snd_soc_dapm_add_path(&card->dapm, source, sink,
4008 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004009 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004010 }
4011}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004012
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004013static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4014 int event)
4015{
4016 struct snd_soc_dapm_widget *w;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004017 unsigned int ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004018
4019 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4020 w = dai->playback_widget;
4021 else
4022 w = dai->capture_widget;
4023
4024 if (w) {
4025 dapm_mark_dirty(w, "stream event");
4026
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004027 if (w->id == snd_soc_dapm_dai_in) {
4028 ep = SND_SOC_DAPM_EP_SOURCE;
4029 dapm_widget_invalidate_input_paths(w);
4030 } else {
4031 ep = SND_SOC_DAPM_EP_SINK;
4032 dapm_widget_invalidate_output_paths(w);
4033 }
4034
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004035 switch (event) {
4036 case SND_SOC_DAPM_STREAM_START:
4037 w->active = 1;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004038 w->is_ep = ep;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004039 break;
4040 case SND_SOC_DAPM_STREAM_STOP:
4041 w->active = 0;
Lars-Peter Clausena3423b02015-08-11 21:38:00 +02004042 w->is_ep = 0;
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004043 break;
4044 case SND_SOC_DAPM_STREAM_SUSPEND:
4045 case SND_SOC_DAPM_STREAM_RESUME:
4046 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4047 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4048 break;
4049 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00004050 }
4051}
4052
Benoit Cousson44ba2642014-07-08 23:19:36 +02004053void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4054{
Mengdong Lin1a497982015-11-18 02:34:11 -05004055 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson44ba2642014-07-08 23:19:36 +02004056
4057 /* for each BE DAI link... */
Mengdong Lin1a497982015-11-18 02:34:11 -05004058 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson44ba2642014-07-08 23:19:36 +02004059 /*
4060 * dynamic FE links have no fixed DAI mapping.
4061 * CODEC<->CODEC links have no direct connection.
4062 */
4063 if (rtd->dai_link->dynamic || rtd->dai_link->params)
4064 continue;
4065
4066 dapm_connect_dai_link_widgets(card, rtd);
4067 }
4068}
4069
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004070static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4071 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004072{
Benoit Cousson44ba2642014-07-08 23:19:36 +02004073 int i;
4074
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02004075 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
Benoit Cousson44ba2642014-07-08 23:19:36 +02004076 for (i = 0; i < rtd->num_codecs; i++)
4077 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004078
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004079 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004080}
4081
4082/**
4083 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4084 * @rtd: PCM runtime data
4085 * @stream: stream name
4086 * @event: stream event
4087 *
4088 * Sends a stream event to the dapm core. The core then makes any
4089 * necessary widget power changes.
4090 *
4091 * Returns 0 for success else error.
4092 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004093void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4094 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004095{
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00004096 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004097
Liam Girdwood3cd04342012-03-09 12:02:08 +00004098 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00004099 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00004100 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004101}
Richard Purdie2b97eab2006-10-06 18:32:18 +02004102
4103/**
Charles Keepax11391102014-02-18 15:22:14 +00004104 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4105 * @dapm: DAPM context
4106 * @pin: pin name
4107 *
4108 * Enables input/output pin and its parents or children widgets iff there is
4109 * a valid audio route and active audio stream.
4110 *
4111 * Requires external locking.
4112 *
4113 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4114 * do any widget power switching.
4115 */
4116int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4117 const char *pin)
4118{
4119 return snd_soc_dapm_set_pin(dapm, pin, 1);
4120}
4121EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4122
4123/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004124 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004125 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004126 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02004127 *
Mark Brown74b8f952009-06-06 11:26:15 +01004128 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01004129 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00004130 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004131 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4132 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02004133 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004134int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004135{
Charles Keepax11391102014-02-18 15:22:14 +00004136 int ret;
4137
4138 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4139
4140 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4141
4142 mutex_unlock(&dapm->card->dapm_mutex);
4143
4144 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02004145}
Liam Girdwooda5302182008-07-07 13:35:17 +01004146EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004147
4148/**
Charles Keepax11391102014-02-18 15:22:14 +00004149 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4150 * @dapm: DAPM context
4151 * @pin: pin name
4152 *
4153 * Enables input/output pin regardless of any other state. This is
4154 * intended for use with microphone bias supplies used in microphone
4155 * jack detection.
4156 *
4157 * Requires external locking.
4158 *
4159 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4160 * do any widget power switching.
4161 */
4162int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4163 const char *pin)
4164{
4165 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4166
4167 if (!w) {
4168 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4169 return -EINVAL;
4170 }
4171
4172 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02004173 if (!w->connected) {
4174 /*
4175 * w->force does not affect the number of input or output paths,
4176 * so we only have to recheck if w->connected is changed
4177 */
4178 dapm_widget_invalidate_input_paths(w);
4179 dapm_widget_invalidate_output_paths(w);
4180 w->connected = 1;
4181 }
Charles Keepax11391102014-02-18 15:22:14 +00004182 w->force = 1;
4183 dapm_mark_dirty(w, "force enable");
4184
4185 return 0;
4186}
4187EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4188
4189/**
Mark Brownda341832010-03-15 19:23:37 +00004190 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004191 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00004192 * @pin: pin name
4193 *
4194 * Enables input/output pin regardless of any other state. This is
4195 * intended for use with microphone bias supplies used in microphone
4196 * jack detection.
4197 *
4198 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4199 * do any widget power switching.
4200 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004201int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4202 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00004203{
Charles Keepax11391102014-02-18 15:22:14 +00004204 int ret;
Mark Brownda341832010-03-15 19:23:37 +00004205
Charles Keepax11391102014-02-18 15:22:14 +00004206 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00004207
Charles Keepax11391102014-02-18 15:22:14 +00004208 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09004209
Charles Keepax11391102014-02-18 15:22:14 +00004210 mutex_unlock(&dapm->card->dapm_mutex);
4211
4212 return ret;
Mark Brownda341832010-03-15 19:23:37 +00004213}
4214EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4215
4216/**
Charles Keepax11391102014-02-18 15:22:14 +00004217 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4218 * @dapm: DAPM context
4219 * @pin: pin name
4220 *
4221 * Disables input/output pin and its parents or children widgets.
4222 *
4223 * Requires external locking.
4224 *
4225 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4226 * do any widget power switching.
4227 */
4228int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4229 const char *pin)
4230{
4231 return snd_soc_dapm_set_pin(dapm, pin, 0);
4232}
4233EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4234
4235/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004236 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004237 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004238 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004239 *
Mark Brown74b8f952009-06-06 11:26:15 +01004240 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00004241 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004242 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4243 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004244 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004245int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4246 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01004247{
Charles Keepax11391102014-02-18 15:22:14 +00004248 int ret;
4249
4250 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4251
4252 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4253
4254 mutex_unlock(&dapm->card->dapm_mutex);
4255
4256 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01004257}
4258EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4259
4260/**
Charles Keepax11391102014-02-18 15:22:14 +00004261 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4262 * @dapm: DAPM context
4263 * @pin: pin name
4264 *
4265 * Marks the specified pin as being not connected, disabling it along
4266 * any parent or child widgets. At present this is identical to
4267 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4268 * additional things such as disabling controls which only affect
4269 * paths through the pin.
4270 *
4271 * Requires external locking.
4272 *
4273 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4274 * do any widget power switching.
4275 */
4276int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4277 const char *pin)
4278{
4279 return snd_soc_dapm_set_pin(dapm, pin, 0);
4280}
4281EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4282
4283/**
Mark Brown5817b522008-09-24 11:23:11 +01004284 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004285 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01004286 * @pin: pin name
4287 *
4288 * Marks the specified pin as being not connected, disabling it along
4289 * any parent or child widgets. At present this is identical to
4290 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4291 * additional things such as disabling controls which only affect
4292 * paths through the pin.
4293 *
4294 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4295 * do any widget power switching.
4296 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004297int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01004298{
Charles Keepax11391102014-02-18 15:22:14 +00004299 int ret;
4300
4301 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4302
4303 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4304
4305 mutex_unlock(&dapm->card->dapm_mutex);
4306
4307 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01004308}
4309EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4310
4311/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004312 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004313 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004314 * @pin: audio signal pin endpoint (or start point)
4315 *
4316 * Get audio pin status - connected or disconnected.
4317 *
4318 * Returns 1 for connected otherwise 0.
4319 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004320int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4321 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004322{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004323 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004324
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004325 if (w)
4326 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06004327
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004328 return 0;
4329}
Liam Girdwooda5302182008-07-07 13:35:17 +01004330EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004331
4332/**
Mark Brown1547aba2010-05-07 21:11:40 +01004333 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004334 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01004335 * @pin: audio signal pin endpoint (or start point)
4336 *
4337 * Mark the given endpoint or pin as ignoring suspend. When the
4338 * system is disabled a path between two endpoints flagged as ignoring
4339 * suspend will not be disabled. The path must already be enabled via
4340 * normal means at suspend time, it will not be turned on if it was not
4341 * already enabled.
4342 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004343int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4344 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01004345{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004346 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01004347
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004348 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004349 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004350 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01004351 }
4352
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004353 w->ignore_suspend = 1;
4354
4355 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01004356}
4357EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4358
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02004359/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02004360 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03004361 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02004362 *
4363 * Free all dapm widgets and resources.
4364 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004365void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004366{
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02004367 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004368 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02004369 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004370}
4371EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4372
Xiang Xiao57996352014-03-02 00:04:02 +08004373static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01004374{
Liam Girdwood01005a72012-07-06 16:57:05 +01004375 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01004376 struct snd_soc_dapm_widget *w;
4377 LIST_HEAD(down_list);
4378 int powerdown = 0;
4379
Liam Girdwood01005a72012-07-06 16:57:05 +01004380 mutex_lock(&card->dapm_mutex);
4381
Jarkko Nikula97c866d2010-12-14 12:18:31 +02004382 list_for_each_entry(w, &dapm->card->widgets, list) {
4383 if (w->dapm != dapm)
4384 continue;
Mark Brown51737472009-06-22 13:16:51 +01004385 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004386 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01004387 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004388 powerdown = 1;
4389 }
4390 }
4391
4392 /* If there were no widgets to power down we're already in
4393 * standby.
4394 */
4395 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004396 if (dapm->bias_level == SND_SOC_BIAS_ON)
4397 snd_soc_dapm_set_bias_level(dapm,
4398 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004399 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004400 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4401 snd_soc_dapm_set_bias_level(dapm,
4402 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004403 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004404
4405 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004406}
Mark Brown51737472009-06-22 13:16:51 +01004407
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004408/*
4409 * snd_soc_dapm_shutdown - callback for system shutdown
4410 */
4411void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4412{
Xiang Xiao57996352014-03-02 00:04:02 +08004413 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004414
Xiang Xiao57996352014-03-02 00:04:02 +08004415 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08004416 if (dapm != &card->dapm) {
4417 soc_dapm_shutdown_dapm(dapm);
4418 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4419 snd_soc_dapm_set_bias_level(dapm,
4420 SND_SOC_BIAS_OFF);
4421 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004422 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08004423
4424 soc_dapm_shutdown_dapm(&card->dapm);
4425 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4426 snd_soc_dapm_set_bias_level(&card->dapm,
4427 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01004428}
4429
Richard Purdie2b97eab2006-10-06 18:32:18 +02004430/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004431MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004432MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4433MODULE_LICENSE("GPL");