blob: e0de8072c5144e92fce3ec9518787a4db526d726 [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 Clausen57295072013-08-05 11:27:31 +020050static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
51 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
52 const char *control,
53 int (*connected)(struct snd_soc_dapm_widget *source,
54 struct snd_soc_dapm_widget *sink));
Vladimir Zapolskiy5353f652015-06-02 00:57:53 +030055
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +010056struct snd_soc_dapm_widget *
Lars-Peter Clausen57295072013-08-05 11:27:31 +020057snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
58 const struct snd_soc_dapm_widget *widget);
59
Liam Girdwood02aa78a2015-05-25 18:21:17 +010060struct snd_soc_dapm_widget *
61snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +020062 const struct snd_soc_dapm_widget *widget);
63
Richard Purdie2b97eab2006-10-06 18:32:18 +020064/* dapm power sequences - make this per codec in the future */
65static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010066 [snd_soc_dapm_pre] = 0,
Mark Brown62ea8742012-01-21 21:14:48 +000067 [snd_soc_dapm_regulator_supply] = 1,
Ola Liljad7e7eb92012-05-24 15:26:25 +020068 [snd_soc_dapm_clock_supply] = 1,
Mark Brown1dd275b2013-10-09 13:56:37 +010069 [snd_soc_dapm_supply] = 2,
70 [snd_soc_dapm_micbias] = 3,
Mark Brownc74184e2012-04-04 22:12:09 +010071 [snd_soc_dapm_dai_link] = 2,
Mark Brown1dd275b2013-10-09 13:56:37 +010072 [snd_soc_dapm_dai_in] = 4,
73 [snd_soc_dapm_dai_out] = 4,
74 [snd_soc_dapm_aif_in] = 4,
75 [snd_soc_dapm_aif_out] = 4,
76 [snd_soc_dapm_mic] = 5,
77 [snd_soc_dapm_mux] = 6,
Lars-Peter Clausend714f972015-05-01 18:02:43 +020078 [snd_soc_dapm_demux] = 6,
Mark Brown1dd275b2013-10-09 13:56:37 +010079 [snd_soc_dapm_dac] = 7,
80 [snd_soc_dapm_switch] = 8,
81 [snd_soc_dapm_mixer] = 8,
82 [snd_soc_dapm_mixer_named_ctl] = 8,
83 [snd_soc_dapm_pga] = 9,
84 [snd_soc_dapm_adc] = 10,
85 [snd_soc_dapm_out_drv] = 11,
86 [snd_soc_dapm_hp] = 11,
87 [snd_soc_dapm_spk] = 11,
88 [snd_soc_dapm_line] = 11,
89 [snd_soc_dapm_kcontrol] = 12,
90 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +020091};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000092
Richard Purdie2b97eab2006-10-06 18:32:18 +020093static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010094 [snd_soc_dapm_pre] = 0,
Lars-Peter Clausen57295072013-08-05 11:27:31 +020095 [snd_soc_dapm_kcontrol] = 1,
96 [snd_soc_dapm_adc] = 2,
97 [snd_soc_dapm_hp] = 3,
98 [snd_soc_dapm_spk] = 3,
99 [snd_soc_dapm_line] = 3,
100 [snd_soc_dapm_out_drv] = 3,
Mark Brown38357ab2009-06-06 19:03:23 +0100101 [snd_soc_dapm_pga] = 4,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +0200102 [snd_soc_dapm_switch] = 5,
Mark Brown38357ab2009-06-06 19:03:23 +0100103 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +0100104 [snd_soc_dapm_mixer] = 5,
105 [snd_soc_dapm_dac] = 6,
106 [snd_soc_dapm_mic] = 7,
107 [snd_soc_dapm_micbias] = 8,
108 [snd_soc_dapm_mux] = 9,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200109 [snd_soc_dapm_demux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +0100110 [snd_soc_dapm_aif_in] = 10,
111 [snd_soc_dapm_aif_out] = 10,
Mark Brown46162742013-06-05 19:36:11 +0100112 [snd_soc_dapm_dai_in] = 10,
113 [snd_soc_dapm_dai_out] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +0100114 [snd_soc_dapm_dai_link] = 11,
Mark Brownc74184e2012-04-04 22:12:09 +0100115 [snd_soc_dapm_supply] = 12,
Mark Brown1dd275b2013-10-09 13:56:37 +0100116 [snd_soc_dapm_clock_supply] = 13,
117 [snd_soc_dapm_regulator_supply] = 13,
118 [snd_soc_dapm_post] = 14,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200119};
120
Mark Brownf9fa2b12014-03-06 16:49:11 +0800121static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
122{
123 if (dapm->card && dapm->card->instantiated)
124 lockdep_assert_held(&dapm->card->dapm_mutex);
125}
126
Troy Kisky12ef1932008-10-13 17:42:14 -0700127static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100128{
129 if (pop_time)
130 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
131}
132
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200133static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100134{
135 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200136 char *buf;
137
138 if (!pop_time)
139 return;
140
141 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
142 if (buf == NULL)
143 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100144
145 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200146 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100147 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100148 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200149
150 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100151}
152
Mark Browndb432b42011-10-03 21:06:40 +0100153static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
154{
155 return !list_empty(&w->dirty);
156}
157
Mark Brown492c0a12014-03-06 16:15:48 +0800158static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100159{
Mark Brownf9fa2b12014-03-06 16:49:11 +0800160 dapm_assert_locked(w->dapm);
161
Mark Brown75c1f892011-10-04 22:28:08 +0100162 if (!dapm_dirty_widget(w)) {
163 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
164 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100165 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100166 }
Mark Browndb432b42011-10-03 21:06:40 +0100167}
168
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200169/*
170 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of input
171 * paths
172 * @w: The widget for which to invalidate the cached number of input paths
173 *
174 * The function resets the cached number of inputs for the specified widget and
175 * all widgets that can be reached via outgoing paths from the widget.
176 *
177 * This function must be called if the number of input paths for a widget might
178 * have changed. E.g. if the source state of a widget changes or a path is added
179 * or activated with the widget as the sink.
180 */
181static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
182{
183 struct snd_soc_dapm_widget *sink;
184 struct snd_soc_dapm_path *p;
185 LIST_HEAD(list);
186
187 dapm_assert_locked(w->dapm);
188
189 if (w->inputs == -1)
190 return;
191
192 w->inputs = -1;
193 list_add_tail(&w->work_list, &list);
194
195 list_for_each_entry(w, &list, work_list) {
196 list_for_each_entry(p, &w->sinks, list_source) {
197 if (p->is_supply || p->weak || !p->connect)
198 continue;
199 sink = p->sink;
200 if (sink->inputs != -1) {
201 sink->inputs = -1;
202 list_add_tail(&sink->work_list, &list);
203 }
204 }
205 }
206}
207
208/*
209 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
210 * output paths
211 * @w: The widget for which to invalidate the cached number of output paths
212 *
213 * Resets the cached number of outputs for the specified widget and all widgets
214 * that can be reached via incoming paths from the widget.
215 *
216 * This function must be called if the number of output paths for a widget might
217 * have changed. E.g. if the sink state of a widget changes or a path is added
218 * or activated with the widget as the source.
219 */
220static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
221{
222 struct snd_soc_dapm_widget *source;
223 struct snd_soc_dapm_path *p;
224 LIST_HEAD(list);
225
226 dapm_assert_locked(w->dapm);
227
228 if (w->outputs == -1)
229 return;
230
231 w->outputs = -1;
232 list_add_tail(&w->work_list, &list);
233
234 list_for_each_entry(w, &list, work_list) {
235 list_for_each_entry(p, &w->sources, list_sink) {
236 if (p->is_supply || p->weak || !p->connect)
237 continue;
238 source = p->source;
239 if (source->outputs != -1) {
240 source->outputs = -1;
241 list_add_tail(&source->work_list, &list);
242 }
243 }
244 }
245}
246
247/*
248 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
249 * for the widgets connected to a path
250 * @p: The path to invalidate
251 *
252 * Resets the cached number of inputs for the sink of the path and the cached
253 * number of outputs for the source of the path.
254 *
255 * This function must be called when a path is added, removed or the connected
256 * state changes.
257 */
258static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
259{
260 /*
261 * Weak paths or supply paths do not influence the number of input or
262 * output paths of their neighbors.
263 */
264 if (p->weak || p->is_supply)
265 return;
266
267 /*
268 * The number of connected endpoints is the sum of the number of
269 * connected endpoints of all neighbors. If a node with 0 connected
270 * endpoints is either connected or disconnected that sum won't change,
271 * so there is no need to re-check the path.
272 */
273 if (p->source->inputs != 0)
274 dapm_widget_invalidate_input_paths(p->sink);
275 if (p->sink->outputs != 0)
276 dapm_widget_invalidate_output_paths(p->source);
277}
278
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200279void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
Mark Browne2d32ff2012-08-31 17:38:32 -0700280{
Mark Browne2d32ff2012-08-31 17:38:32 -0700281 struct snd_soc_dapm_widget *w;
282
283 mutex_lock(&card->dapm_mutex);
284
285 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200286 if (w->is_sink || w->is_source) {
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200287 dapm_mark_dirty(w, "Rechecking endpoints");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200288 if (w->is_sink)
289 dapm_widget_invalidate_output_paths(w);
290 if (w->is_source)
291 dapm_widget_invalidate_input_paths(w);
292 }
Mark Browne2d32ff2012-08-31 17:38:32 -0700293 }
294
295 mutex_unlock(&card->dapm_mutex);
296}
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200297EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
Mark Browne2d32ff2012-08-31 17:38:32 -0700298
Richard Purdie2b97eab2006-10-06 18:32:18 +0200299/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100300static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200301 const struct snd_soc_dapm_widget *_widget)
302{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100303 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200304}
305
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200306struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200307 unsigned int value;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200308 struct snd_soc_dapm_widget *widget;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200309 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200310 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200311};
312
313static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
314 struct snd_kcontrol *kcontrol)
315{
316 struct dapm_kcontrol_data *data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200317 struct soc_mixer_control *mc;
Charles Keepax561ed682015-05-01 12:37:26 +0100318 struct soc_enum *e;
Charles Keepax773da9b2015-05-01 12:37:25 +0100319 const char *name;
320 int ret;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200321
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200322 data = kzalloc(sizeof(*data), GFP_KERNEL);
Charles Keepax40b7bea2015-05-01 12:37:24 +0100323 if (!data)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200324 return -ENOMEM;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200325
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200326 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200327
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200328 switch (widget->id) {
329 case snd_soc_dapm_switch:
330 case snd_soc_dapm_mixer:
331 case snd_soc_dapm_mixer_named_ctl:
332 mc = (struct soc_mixer_control *)kcontrol->private_value;
333
334 if (mc->autodisable) {
335 struct snd_soc_dapm_widget template;
336
Charles Keepax773da9b2015-05-01 12:37:25 +0100337 name = kasprintf(GFP_KERNEL, "%s %s", kcontrol->id.name,
338 "Autodisable");
339 if (!name) {
340 ret = -ENOMEM;
341 goto err_data;
342 }
343
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200344 memset(&template, 0, sizeof(template));
345 template.reg = mc->reg;
346 template.mask = (1 << fls(mc->max)) - 1;
347 template.shift = mc->shift;
348 if (mc->invert)
349 template.off_val = mc->max;
350 else
351 template.off_val = 0;
352 template.on_val = template.off_val;
353 template.id = snd_soc_dapm_kcontrol;
Charles Keepax773da9b2015-05-01 12:37:25 +0100354 template.name = name;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200355
Lars-Peter Clausen2daabd72013-08-30 17:39:33 +0200356 data->value = template.on_val;
357
Liam Girdwood02aa78a2015-05-25 18:21:17 +0100358 data->widget =
359 snd_soc_dapm_new_control_unlocked(widget->dapm,
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200360 &template);
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200361 kfree(name);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200362 if (!data->widget) {
Charles Keepax773da9b2015-05-01 12:37:25 +0100363 ret = -ENOMEM;
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200364 goto err_data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200365 }
366 }
367 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200368 case snd_soc_dapm_demux:
Charles Keepax561ed682015-05-01 12:37:26 +0100369 case snd_soc_dapm_mux:
370 e = (struct soc_enum *)kcontrol->private_value;
371
372 if (e->autodisable) {
373 struct snd_soc_dapm_widget template;
374
375 name = kasprintf(GFP_KERNEL, "%s %s", kcontrol->id.name,
376 "Autodisable");
377 if (!name) {
378 ret = -ENOMEM;
379 goto err_data;
380 }
381
382 memset(&template, 0, sizeof(template));
383 template.reg = e->reg;
384 template.mask = e->mask << e->shift_l;
385 template.shift = e->shift_l;
386 template.off_val = snd_soc_enum_item_to_val(e, 0);
387 template.on_val = template.off_val;
388 template.id = snd_soc_dapm_kcontrol;
389 template.name = name;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200390
391 data->value = template.on_val;
392
Charles Keepaxffacb482015-06-26 10:39:43 +0100393 data->widget = snd_soc_dapm_new_control_unlocked(
394 widget->dapm, &template);
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200395 kfree(name);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200396 if (!data->widget) {
Charles Keepax561ed682015-05-01 12:37:26 +0100397 ret = -ENOMEM;
Lars-Peter Clausene18077b2015-07-08 21:59:59 +0200398 goto err_data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200399 }
Charles Keepax561ed682015-05-01 12:37:26 +0100400
401 snd_soc_dapm_add_path(widget->dapm, data->widget,
402 widget, NULL, NULL);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200403 }
404 break;
405 default:
406 break;
407 }
408
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200409 kcontrol->private_data = data;
410
411 return 0;
Charles Keepax773da9b2015-05-01 12:37:25 +0100412
Charles Keepax773da9b2015-05-01 12:37:25 +0100413err_data:
414 kfree(data);
415 return ret;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200416}
417
418static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
419{
420 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200421 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200422 kfree(data);
423}
424
425static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
426 const struct snd_kcontrol *kcontrol)
427{
428 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
429
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200430 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200431}
432
433static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
434 struct snd_soc_dapm_widget *widget)
435{
436 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200437 struct snd_soc_dapm_widget_list *new_wlist;
438 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200439
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200440 if (data->wlist)
441 n = data->wlist->num_widgets + 1;
442 else
443 n = 1;
444
445 new_wlist = krealloc(data->wlist,
446 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
447 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200448 return -ENOMEM;
449
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200450 new_wlist->widgets[n - 1] = widget;
451 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200452
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200453 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200454
455 return 0;
456}
457
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200458static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
459 struct snd_soc_dapm_path *path)
460{
461 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
462
463 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200464}
465
466static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
467{
468 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
469
470 if (!data->widget)
471 return true;
472
473 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200474}
475
476static struct list_head *dapm_kcontrol_get_path_list(
477 const struct snd_kcontrol *kcontrol)
478{
479 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
480
481 return &data->paths;
482}
483
484#define dapm_kcontrol_for_each_path(path, kcontrol) \
485 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
486 list_kcontrol)
487
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530488unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200489{
490 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
491
492 return data->value;
493}
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530494EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200495
496static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
497 unsigned int value)
498{
499 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
500
501 if (data->value == value)
502 return false;
503
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200504 if (data->widget)
505 data->widget->on_val = value;
506
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200507 data->value = value;
508
509 return true;
510}
511
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200512/**
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200513 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
514 * kcontrol
515 * @kcontrol: The kcontrol
516 *
517 * Note: This function must only be used on kcontrols that are known to have
518 * been registered for a CODEC. Otherwise the behaviour is undefined.
519 */
520struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
521 struct snd_kcontrol *kcontrol)
522{
523 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
524}
525EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
526
Liam Girdwood6c120e12012-02-15 15:15:34 +0000527static void dapm_reset(struct snd_soc_card *card)
528{
529 struct snd_soc_dapm_widget *w;
530
Mark Brownf9fa2b12014-03-06 16:49:11 +0800531 lockdep_assert_held(&card->dapm_mutex);
532
Liam Girdwood6c120e12012-02-15 15:15:34 +0000533 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
534
535 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200536 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000537 w->power_checked = false;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000538 }
539}
540
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200541static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
542{
543 if (!dapm->component)
544 return NULL;
545 return dapm->component->name_prefix;
546}
547
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200548static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800549 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100550{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200551 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200552 return -EIO;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200553 return snd_soc_component_read(dapm->component, reg, value);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100554}
555
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200556static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
Bard Liao34775012014-04-17 20:12:56 +0800557 int reg, unsigned int mask, unsigned int value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100558{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200559 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200560 return -EIO;
Mark Brownfcf6c5e2014-12-15 13:08:48 +0000561 return snd_soc_component_update_bits(dapm->component, reg,
562 mask, value);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000563}
564
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200565static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
566 int reg, unsigned int mask, unsigned int value)
567{
568 if (!dapm->component)
569 return -EIO;
570 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
571}
572
Mark Browneb270e92013-10-09 13:52:52 +0100573static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
574{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200575 if (dapm->component)
576 snd_soc_component_async_complete(dapm->component);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100577}
578
Charles Keepax45a110a2015-05-11 13:50:30 +0100579static struct snd_soc_dapm_widget *
580dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
581{
582 struct snd_soc_dapm_widget *w = wcache->widget;
583 struct list_head *wlist;
584 const int depth = 2;
585 int i = 0;
586
587 if (w) {
588 wlist = &w->dapm->card->widgets;
589
590 list_for_each_entry_from(w, wlist, list) {
591 if (!strcmp(name, w->name))
592 return w;
593
594 if (++i == depth)
595 break;
596 }
597 }
598
599 return NULL;
600}
601
602static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
603 struct snd_soc_dapm_widget *w)
604{
605 wcache->widget = w;
606}
607
Mark Brown452c5ea2009-05-17 21:41:23 +0100608/**
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200609 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
610 * @dapm: The DAPM context for which to set the level
611 * @level: The level to set
612 *
613 * Forces the DAPM bias level to a specific state. It will call the bias level
614 * callback of DAPM context with the specified level. This will even happen if
615 * the context is already at the same level. Furthermore it will not go through
616 * the normal bias level sequencing, meaning any intermediate states between the
617 * current and the target state will not be entered.
618 *
619 * Note that the change in bias level is only temporary and the next time
620 * snd_soc_dapm_sync() is called the state will be set to the level as
621 * determined by the DAPM core. The function is mainly intended to be used to
622 * used during probe or resume from suspend to power up the device so
623 * initialization can be done, before the DAPM core takes over.
624 */
625int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
626 enum snd_soc_bias_level level)
627{
628 int ret = 0;
629
630 if (dapm->set_bias_level)
631 ret = dapm->set_bias_level(dapm, level);
632
Lars-Peter Clausenf4bf8d72015-04-27 22:13:25 +0200633 if (ret == 0)
634 dapm->bias_level = level;
635
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200636 return ret;
637}
638EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
639
Mark Brown452c5ea2009-05-17 21:41:23 +0100640/**
641 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800642 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100643 * @level: level to configure
644 *
645 * Configure the bias (power) levels for the SoC audio device.
646 *
647 * Returns 0 for success else error.
648 */
Mark Browned5a4c42011-02-18 11:12:42 -0800649static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200650 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100651{
Mark Browned5a4c42011-02-18 11:12:42 -0800652 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100653 int ret = 0;
654
Mark Brown84e90932010-11-04 00:07:02 -0400655 trace_snd_soc_bias_level_start(card, level);
656
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000657 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100658 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100659 if (ret != 0)
660 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100661
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200662 if (!card || dapm != &card->dapm)
663 ret = snd_soc_dapm_force_bias_level(dapm, level);
Liam Girdwood41231282012-07-06 16:56:16 +0100664
Mark Brown171ec6b2011-06-06 18:15:19 +0100665 if (ret != 0)
666 goto out;
667
668 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100669 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100670out:
Mark Brown84e90932010-11-04 00:07:02 -0400671 trace_snd_soc_bias_level_done(card, level);
672
Mark Brown452c5ea2009-05-17 21:41:23 +0100673 return ret;
674}
675
Mark Brown74b8f952009-06-06 11:26:15 +0100676/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200677static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200678 struct snd_soc_dapm_path *path, const char *control_name,
679 struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200680{
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200681 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200682 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100683 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200684 int i;
685
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100686 if (e->reg != SND_SOC_NOPM) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200687 soc_dapm_read(dapm, e->reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100688 val = (val >> e->shift_l) & e->mask;
689 item = snd_soc_enum_val_to_item(e, val);
690 } else {
691 /* since a virtual mux has no backing registers to
692 * decide which path to connect, it will try to match
693 * with the first enumeration. This is to ensure
694 * that the default mux choice (the first) will be
695 * correctly powered up during initialization.
696 */
697 item = 0;
698 }
699
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100700 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200701 if (!(strcmp(control_name, e->texts[i]))) {
Rasmus Villemoes98ad73c2014-10-21 17:01:15 +0200702 path->name = e->texts[i];
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100703 if (i == item)
704 path->connect = 1;
705 else
706 path->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200707 return 0;
708 }
709 }
710
711 return -ENODEV;
712}
713
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100714/* set up initial codec paths */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200715static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i)
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100716{
717 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200718 p->sink->kcontrol_news[i].private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100719 unsigned int reg = mc->reg;
720 unsigned int shift = mc->shift;
721 unsigned int max = mc->max;
722 unsigned int mask = (1 << fls(max)) - 1;
723 unsigned int invert = mc->invert;
724 unsigned int val;
725
726 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200727 soc_dapm_read(p->sink->dapm, reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100728 val = (val >> shift) & mask;
729 if (invert)
730 val = max - val;
731 p->connect = !!val;
732 } else {
733 p->connect = 0;
734 }
735}
736
Mark Brown74b8f952009-06-06 11:26:15 +0100737/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200738static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200739 struct snd_soc_dapm_path *path, const char *control_name)
740{
741 int i;
742
743 /* search for mixer kcontrol */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200744 for (i = 0; i < path->sink->num_kcontrols; i++) {
745 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
746 path->name = path->sink->kcontrol_news[i].name;
747 dapm_set_mixer_path_status(path, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200748 return 0;
749 }
750 }
751 return -ENODEV;
752}
753
Stephen Warrenaf468002011-04-28 17:38:01 -0600754static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600755 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600756 const struct snd_kcontrol_new *kcontrol_new,
757 struct snd_kcontrol **kcontrol)
758{
759 struct snd_soc_dapm_widget *w;
760 int i;
761
762 *kcontrol = NULL;
763
764 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600765 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
766 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600767 for (i = 0; i < w->num_kcontrols; i++) {
768 if (&w->kcontrol_news[i] == kcontrol_new) {
769 if (w->kcontrols)
770 *kcontrol = w->kcontrols[i];
771 return 1;
772 }
773 }
774 }
775
776 return 0;
777}
778
Stephen Warren85762e72013-03-29 15:40:10 -0600779/*
780 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
781 * create it. Either way, add the widget into the control's widget list
782 */
783static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100784 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200785{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200786 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000787 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000788 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600789 size_t prefix_len;
790 int shared;
791 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600792 bool wname_in_long_name, kcname_in_long_name;
Daniel Macke5092c92014-10-07 13:41:24 +0200793 char *long_name = NULL;
Stephen Warren85762e72013-03-29 15:40:10 -0600794 const char *name;
Daniel Macke5092c92014-10-07 13:41:24 +0200795 int ret = 0;
Mark Brownefb7ac32011-03-08 17:23:24 +0000796
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200797 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000798 if (prefix)
799 prefix_len = strlen(prefix) + 1;
800 else
801 prefix_len = 0;
802
Stephen Warren85762e72013-03-29 15:40:10 -0600803 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
804 &kcontrol);
805
Stephen Warren85762e72013-03-29 15:40:10 -0600806 if (!kcontrol) {
807 if (shared) {
808 wname_in_long_name = false;
809 kcname_in_long_name = true;
810 } else {
811 switch (w->id) {
812 case snd_soc_dapm_switch:
813 case snd_soc_dapm_mixer:
814 wname_in_long_name = true;
815 kcname_in_long_name = true;
816 break;
817 case snd_soc_dapm_mixer_named_ctl:
818 wname_in_long_name = false;
819 kcname_in_long_name = true;
820 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200821 case snd_soc_dapm_demux:
Stephen Warren85762e72013-03-29 15:40:10 -0600822 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600823 wname_in_long_name = true;
824 kcname_in_long_name = false;
825 break;
826 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600827 return -EINVAL;
828 }
829 }
830
831 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600832 /*
833 * The control will get a prefix from the control
834 * creation process but we're also using the same
835 * prefix for widgets so cut the prefix off the
836 * front of the widget name.
837 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200838 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600839 w->name + prefix_len,
840 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200841 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200842 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600843
844 name = long_name;
845 } else if (wname_in_long_name) {
846 long_name = NULL;
847 name = w->name + prefix_len;
848 } else {
849 long_name = NULL;
850 name = w->kcontrol_news[kci].name;
851 }
852
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200853 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600854 prefix);
Daniel Macke5092c92014-10-07 13:41:24 +0200855 if (!kcontrol) {
856 ret = -ENOMEM;
857 goto exit_free;
858 }
859
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200860 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200861
862 ret = dapm_kcontrol_data_alloc(w, kcontrol);
863 if (ret) {
864 snd_ctl_free_one(kcontrol);
Daniel Macke5092c92014-10-07 13:41:24 +0200865 goto exit_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200866 }
867
Stephen Warren85762e72013-03-29 15:40:10 -0600868 ret = snd_ctl_add(card, kcontrol);
869 if (ret < 0) {
870 dev_err(dapm->dev,
871 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
872 w->name, name, ret);
Daniel Macke5092c92014-10-07 13:41:24 +0200873 goto exit_free;
Stephen Warren85762e72013-03-29 15:40:10 -0600874 }
Stephen Warren85762e72013-03-29 15:40:10 -0600875 }
876
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200877 ret = dapm_kcontrol_add_widget(kcontrol, w);
Daniel Macke5092c92014-10-07 13:41:24 +0200878 if (ret == 0)
879 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200880
Daniel Macke5092c92014-10-07 13:41:24 +0200881exit_free:
882 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600883
Daniel Macke5092c92014-10-07 13:41:24 +0200884 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600885}
886
887/* create new dapm mixer control */
888static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
889{
890 int i, ret;
891 struct snd_soc_dapm_path *path;
Charles Keepax561ed682015-05-01 12:37:26 +0100892 struct dapm_kcontrol_data *data;
Stephen Warren85762e72013-03-29 15:40:10 -0600893
Richard Purdie2b97eab2006-10-06 18:32:18 +0200894 /* add kcontrol */
895 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200896 /* match name */
897 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200898 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600899 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200900 continue;
901
Charles Keepax561ed682015-05-01 12:37:26 +0100902 if (!w->kcontrols[i]) {
903 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
904 if (ret < 0)
905 return ret;
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200906 }
907
Mark Brown946d92a2013-08-12 23:28:42 +0100908 dapm_kcontrol_add_path(w->kcontrols[i], path);
Charles Keepax561ed682015-05-01 12:37:26 +0100909
910 data = snd_kcontrol_chip(w->kcontrols[i]);
911 if (data->widget)
912 snd_soc_dapm_add_path(data->widget->dapm,
913 data->widget,
914 path->source,
915 NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200916 }
917 }
Stephen Warren85762e72013-03-29 15:40:10 -0600918
919 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200920}
921
922/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200923static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200924{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200925 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600926 struct snd_soc_dapm_path *path;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200927 struct list_head *paths;
928 const char *type;
Stephen Warrenaf468002011-04-28 17:38:01 -0600929 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200930
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200931 switch (w->id) {
932 case snd_soc_dapm_mux:
933 paths = &w->sources;
934 type = "mux";
935 break;
936 case snd_soc_dapm_demux:
937 paths = &w->sinks;
938 type = "demux";
939 break;
940 default:
941 return -EINVAL;
942 }
943
Stephen Warrenaf468002011-04-28 17:38:01 -0600944 if (w->num_kcontrols != 1) {
945 dev_err(dapm->dev,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200946 "ASoC: %s %s has incorrect number of controls\n", type,
Stephen Warrenaf468002011-04-28 17:38:01 -0600947 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200948 return -EINVAL;
949 }
950
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200951 if (list_empty(paths)) {
952 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
Stephen Warren85762e72013-03-29 15:40:10 -0600953 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600954 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200955
Mark Brown946d92a2013-08-12 23:28:42 +0100956 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600957 if (ret < 0)
958 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600959
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200960 if (w->id == snd_soc_dapm_mux) {
961 list_for_each_entry(path, &w->sources, list_sink) {
962 if (path->name)
963 dapm_kcontrol_add_path(w->kcontrols[0], path);
964 }
965 } else {
966 list_for_each_entry(path, &w->sinks, list_source) {
967 if (path->name)
968 dapm_kcontrol_add_path(w->kcontrols[0], path);
969 }
Lars-Peter Clausen98407ef2014-10-25 17:41:57 +0200970 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200971
Stephen Warrenaf468002011-04-28 17:38:01 -0600972 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200973}
974
975/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200976static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200977{
Mark Browna6c65732010-03-03 17:45:21 +0000978 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200979 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000980 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200981
Mark Browna6c65732010-03-03 17:45:21 +0000982 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200983}
984
Nikesh Oswalc6615082015-02-02 17:06:44 +0000985/* create new dapm dai link control */
986static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
987{
988 int i, ret;
989 struct snd_kcontrol *kcontrol;
990 struct snd_soc_dapm_context *dapm = w->dapm;
991 struct snd_card *card = dapm->card->snd_card;
992
993 /* create control for links with > 1 config */
994 if (w->num_params <= 1)
995 return 0;
996
997 /* add kcontrol */
998 for (i = 0; i < w->num_kcontrols; i++) {
999 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1000 w->name, NULL);
1001 ret = snd_ctl_add(card, kcontrol);
1002 if (ret < 0) {
1003 dev_err(dapm->dev,
1004 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1005 w->name, w->kcontrol_news[i].name, ret);
1006 return ret;
1007 }
1008 kcontrol->private_data = w;
1009 w->kcontrols[i] = kcontrol;
1010 }
1011
1012 return 0;
1013}
1014
Mark Brown99497882010-05-07 20:24:05 +01001015/* We implement power down on suspend by checking the power state of
1016 * the ALSA card - when we are suspending the ALSA state for the card
1017 * is set to D3.
1018 */
1019static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1020{
Mark Brown12ea2c72011-03-02 18:17:32 +00001021 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +01001022
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001023 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +01001024 case SNDRV_CTL_POWER_D3hot:
1025 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +01001026 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001027 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001028 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +01001029 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +01001030 default:
1031 return 1;
1032 }
1033}
1034
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001035/* add widget to list if it's not already in the list */
1036static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
1037 struct snd_soc_dapm_widget *w)
1038{
1039 struct snd_soc_dapm_widget_list *wlist;
1040 int wlistsize, wlistentries, i;
1041
1042 if (*list == NULL)
1043 return -EINVAL;
1044
1045 wlist = *list;
1046
1047 /* is this widget already in the list */
1048 for (i = 0; i < wlist->num_widgets; i++) {
1049 if (wlist->widgets[i] == w)
1050 return 0;
1051 }
1052
1053 /* allocate some new space */
1054 wlistentries = wlist->num_widgets + 1;
1055 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
1056 wlistentries * sizeof(struct snd_soc_dapm_widget *);
1057 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
1058 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001059 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001060 w->name);
1061 return -ENOMEM;
1062 }
1063 wlist = *list;
1064
1065 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001066 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001067 w->name, wlist->num_widgets);
1068
1069 wlist->widgets[wlist->num_widgets] = w;
1070 wlist->num_widgets++;
1071 return 1;
1072}
1073
Richard Purdie2b97eab2006-10-06 18:32:18 +02001074/*
1075 * Recursively check for a completed path to an active or physically connected
1076 * output widget. Returns number of complete paths.
1077 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001078static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1079 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001080{
1081 struct snd_soc_dapm_path *path;
1082 int con = 0;
1083
Mark Brown024dc072011-10-09 11:52:05 +01001084 if (widget->outputs >= 0)
1085 return widget->outputs;
1086
Mark Brownde02d072011-09-20 21:43:24 +01001087 DAPM_UPDATE_STAT(widget, path_checks);
1088
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001089 if (widget->is_sink && widget->connected) {
1090 widget->outputs = snd_soc_dapm_suspend_check(widget);
1091 return widget->outputs;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001092 }
1093
1094 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +01001095 DAPM_UPDATE_STAT(widget, neighbour_checks);
1096
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001097 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +01001098 continue;
1099
Mark Brown8af294b2013-02-22 17:48:15 +00001100 if (path->walking)
1101 return 1;
1102
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001103 trace_snd_soc_dapm_output_path(widget, path);
1104
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001105 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +00001106 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001107
1108 /* do we need to add this widget to the list ? */
1109 if (list) {
1110 int err;
1111 err = dapm_list_add_widget(list, path->sink);
1112 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001113 dev_err(widget->dapm->dev,
1114 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001115 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001116 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001117 return con;
1118 }
1119 }
1120
1121 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001122
1123 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001124 }
1125 }
1126
Mark Brown024dc072011-10-09 11:52:05 +01001127 widget->outputs = con;
1128
Richard Purdie2b97eab2006-10-06 18:32:18 +02001129 return con;
1130}
1131
1132/*
1133 * Recursively check for a completed path to an active or physically connected
1134 * input widget. Returns number of complete paths.
1135 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001136static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1137 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001138{
1139 struct snd_soc_dapm_path *path;
1140 int con = 0;
1141
Mark Brown024dc072011-10-09 11:52:05 +01001142 if (widget->inputs >= 0)
1143 return widget->inputs;
1144
Mark Brownde02d072011-09-20 21:43:24 +01001145 DAPM_UPDATE_STAT(widget, path_checks);
1146
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001147 if (widget->is_source && widget->connected) {
1148 widget->inputs = snd_soc_dapm_suspend_check(widget);
1149 return widget->inputs;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001150 }
1151
1152 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +01001153 DAPM_UPDATE_STAT(widget, neighbour_checks);
1154
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001155 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +01001156 continue;
1157
Mark Brown8af294b2013-02-22 17:48:15 +00001158 if (path->walking)
1159 return 1;
1160
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001161 trace_snd_soc_dapm_input_path(widget, path);
1162
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001163 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +00001164 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001165
1166 /* do we need to add this widget to the list ? */
1167 if (list) {
1168 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001169 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001170 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001171 dev_err(widget->dapm->dev,
1172 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001173 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001174 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001175 return con;
1176 }
1177 }
1178
1179 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001180
1181 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001182 }
1183 }
1184
Mark Brown024dc072011-10-09 11:52:05 +01001185 widget->inputs = con;
1186
Richard Purdie2b97eab2006-10-06 18:32:18 +02001187 return con;
1188}
1189
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001190/**
1191 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1192 * @dai: the soc DAI.
1193 * @stream: stream direction.
1194 * @list: list of active widgets for this stream.
1195 *
1196 * Queries DAPM graph as to whether an valid audio stream path exists for
1197 * the initial stream specified by name. This takes into account
1198 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1199 *
1200 * Returns the number of valid paths or negative error.
1201 */
1202int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1203 struct snd_soc_dapm_widget_list **list)
1204{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01001205 struct snd_soc_card *card = dai->component->card;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001206 struct snd_soc_dapm_widget *w;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001207 int paths;
1208
1209 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001210
1211 /*
1212 * For is_connected_{output,input}_ep fully discover the graph we need
1213 * to reset the cached number of inputs and outputs.
1214 */
1215 list_for_each_entry(w, &card->widgets, list) {
1216 w->inputs = -1;
1217 w->outputs = -1;
1218 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001219
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001220 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001221 paths = is_connected_output_ep(dai->playback_widget, list);
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001222 else
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001223 paths = is_connected_input_ep(dai->capture_widget, list);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001224
1225 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001226 mutex_unlock(&card->dapm_mutex);
1227
1228 return paths;
1229}
1230
Richard Purdie2b97eab2006-10-06 18:32:18 +02001231/*
Mark Brown62ea8742012-01-21 21:14:48 +00001232 * Handler for regulator supply widget.
1233 */
1234int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1235 struct snd_kcontrol *kcontrol, int event)
1236{
Mark Brownc05b84d2012-09-07 12:57:11 +08001237 int ret;
1238
Mark Browneb270e92013-10-09 13:52:52 +01001239 soc_dapm_async_complete(w->dapm);
1240
Mark Brownc05b84d2012-09-07 12:57:11 +08001241 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001242 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001243 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001244 if (ret != 0)
1245 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001246 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001247 w->name, ret);
1248 }
1249
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001250 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001251 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001252 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001253 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001254 if (ret != 0)
1255 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001256 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001257 w->name, ret);
1258 }
1259
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001260 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001261 }
Mark Brown62ea8742012-01-21 21:14:48 +00001262}
1263EXPORT_SYMBOL_GPL(dapm_regulator_event);
1264
Ola Liljad7e7eb92012-05-24 15:26:25 +02001265/*
1266 * Handler for clock supply widget.
1267 */
1268int dapm_clock_event(struct snd_soc_dapm_widget *w,
1269 struct snd_kcontrol *kcontrol, int event)
1270{
1271 if (!w->clk)
1272 return -EIO;
1273
Mark Browneb270e92013-10-09 13:52:52 +01001274 soc_dapm_async_complete(w->dapm);
1275
Mark Brownec029952012-06-04 08:16:20 +01001276#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001277 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001278 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001279 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001280 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001281 return 0;
1282 }
Mark Brownec029952012-06-04 08:16:20 +01001283#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001284 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001285}
1286EXPORT_SYMBOL_GPL(dapm_clock_event);
1287
Mark Brownd8050022011-09-28 18:28:23 +01001288static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1289{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001290 if (w->power_checked)
1291 return w->new_power;
1292
Mark Brownd8050022011-09-28 18:28:23 +01001293 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001294 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001295 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001296 w->new_power = w->power_check(w);
1297
1298 w->power_checked = true;
1299
1300 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001301}
1302
Mark Browncd0f2d42009-04-20 16:56:59 +01001303/* Generic check to see if a widget should be powered.
1304 */
1305static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1306{
1307 int in, out;
1308
Mark Brownde02d072011-09-20 21:43:24 +01001309 DAPM_UPDATE_STAT(w, power_checks);
1310
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001311 in = is_connected_input_ep(w, NULL);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001312 out = is_connected_output_ep(w, NULL);
Mark Browncd0f2d42009-04-20 16:56:59 +01001313 return out != 0 && in != 0;
1314}
1315
Mark Brown246d0a12009-04-22 18:24:55 +01001316/* Check to see if a power supply is needed */
1317static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1318{
1319 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001320
Mark Brownde02d072011-09-20 21:43:24 +01001321 DAPM_UPDATE_STAT(w, power_checks);
1322
Mark Brown246d0a12009-04-22 18:24:55 +01001323 /* Check if one of our outputs is connected */
1324 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001325 DAPM_UPDATE_STAT(w, neighbour_checks);
1326
Mark Brownbf3a9e12011-06-13 16:42:29 +01001327 if (path->weak)
1328 continue;
1329
Mark Brown215edda2009-09-08 18:59:05 +01001330 if (path->connected &&
1331 !path->connected(path->source, path->sink))
1332 continue;
1333
Mark Brownf68d7e12011-10-04 22:57:50 +01001334 if (dapm_widget_power_check(path->sink))
1335 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001336 }
1337
Mark Brownf68d7e12011-10-04 22:57:50 +01001338 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001339}
1340
Mark Brown35c64bc2011-09-28 18:23:53 +01001341static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1342{
1343 return 1;
1344}
1345
Mark Brown38357ab2009-06-06 19:03:23 +01001346static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1347 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001348 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001349{
Mark Brown828a8422011-01-15 13:14:30 +00001350 int *sort;
1351
1352 if (power_up)
1353 sort = dapm_up_seq;
1354 else
1355 sort = dapm_down_seq;
1356
Mark Brown38357ab2009-06-06 19:03:23 +01001357 if (sort[a->id] != sort[b->id])
1358 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001359 if (a->subseq != b->subseq) {
1360 if (power_up)
1361 return a->subseq - b->subseq;
1362 else
1363 return b->subseq - a->subseq;
1364 }
Mark Brownb22ead22009-06-07 12:51:26 +01001365 if (a->reg != b->reg)
1366 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001367 if (a->dapm != b->dapm)
1368 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001369
Mark Brown38357ab2009-06-06 19:03:23 +01001370 return 0;
1371}
Mark Brown42aa3412009-03-01 19:21:10 +00001372
Mark Brown38357ab2009-06-06 19:03:23 +01001373/* Insert a widget in order into a DAPM power sequence. */
1374static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1375 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001376 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001377{
1378 struct snd_soc_dapm_widget *w;
1379
1380 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001381 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001382 list_add_tail(&new_widget->power_list, &w->power_list);
1383 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001384 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001385
Mark Brown38357ab2009-06-06 19:03:23 +01001386 list_add_tail(&new_widget->power_list, list);
1387}
Mark Brown42aa3412009-03-01 19:21:10 +00001388
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001389static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001390 struct snd_soc_dapm_widget *w, int event)
1391{
Mark Brown68f89ad2010-11-03 23:51:49 -04001392 const char *ev_name;
1393 int power, ret;
1394
1395 switch (event) {
1396 case SND_SOC_DAPM_PRE_PMU:
1397 ev_name = "PRE_PMU";
1398 power = 1;
1399 break;
1400 case SND_SOC_DAPM_POST_PMU:
1401 ev_name = "POST_PMU";
1402 power = 1;
1403 break;
1404 case SND_SOC_DAPM_PRE_PMD:
1405 ev_name = "PRE_PMD";
1406 power = 0;
1407 break;
1408 case SND_SOC_DAPM_POST_PMD:
1409 ev_name = "POST_PMD";
1410 power = 0;
1411 break;
Mark Brown80114122013-02-25 15:14:19 +00001412 case SND_SOC_DAPM_WILL_PMU:
1413 ev_name = "WILL_PMU";
1414 power = 1;
1415 break;
1416 case SND_SOC_DAPM_WILL_PMD:
1417 ev_name = "WILL_PMD";
1418 power = 0;
1419 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001420 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001421 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001422 return;
1423 }
1424
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001425 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001426 return;
1427
1428 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001429 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001430 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001431 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001432 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001433 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001434 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001435 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001436 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001437 ev_name, w->name, ret);
1438 }
1439}
1440
Mark Brownb22ead22009-06-07 12:51:26 +01001441/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001442static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001443 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001444{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001445 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001446 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001447 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001448 unsigned int value = 0;
1449 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001450
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001451 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1452 reg = w->reg;
1453 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001454
1455 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001456 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001457 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001458
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001459 mask |= w->mask << w->shift;
1460 if (w->power)
1461 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001462 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001463 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001464
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001465 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001466 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1467 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001468
Mark Brown68f89ad2010-11-03 23:51:49 -04001469 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001470 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1471 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001472 }
1473
Mark Brown81628102009-06-07 13:21:24 +01001474 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001475 /* Any widget will do, they should all be updating the
1476 * same register.
1477 */
Mark Brown29376bc2011-06-19 13:49:28 +01001478
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001479 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001480 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001481 value, mask, reg, card->pop_time);
1482 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001483 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001484 }
1485
1486 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001487 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1488 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001489 }
Mark Brown42aa3412009-03-01 19:21:10 +00001490}
1491
Mark Brownb22ead22009-06-07 12:51:26 +01001492/* Apply a DAPM power sequence.
1493 *
1494 * We walk over a pre-sorted list of widgets to apply power to. In
1495 * order to minimise the number of writes to the device required
1496 * multiple widgets will be updated in a single write where possible.
1497 * Currently anything that requires more than a single write is not
1498 * handled.
1499 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001500static void dapm_seq_run(struct snd_soc_card *card,
1501 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001502{
1503 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001504 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001505 LIST_HEAD(pending);
1506 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001507 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001508 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001509 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001510 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001511 int *sort;
1512
1513 if (power_up)
1514 sort = dapm_up_seq;
1515 else
1516 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001517
Mark Brownb22ead22009-06-07 12:51:26 +01001518 list_for_each_entry_safe(w, n, list, power_list) {
1519 ret = 0;
1520
1521 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001522 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001523 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001524 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001525 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001526
Mark Brown474b62d2011-01-18 16:14:44 +00001527 if (cur_dapm && cur_dapm->seq_notifier) {
1528 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1529 if (sort[i] == cur_sort)
1530 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001531 i,
1532 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001533 }
1534
Mark Browneb270e92013-10-09 13:52:52 +01001535 if (cur_dapm && w->dapm != cur_dapm)
1536 soc_dapm_async_complete(cur_dapm);
1537
Mark Brownb22ead22009-06-07 12:51:26 +01001538 INIT_LIST_HEAD(&pending);
1539 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001540 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001541 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001542 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001543 }
1544
Mark Brown163cac02009-06-07 10:12:52 +01001545 switch (w->id) {
1546 case snd_soc_dapm_pre:
1547 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001548 list_for_each_entry_safe_continue(w, n, list,
1549 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001550
Mark Brownb22ead22009-06-07 12:51:26 +01001551 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001552 ret = w->event(w,
1553 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001554 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001555 ret = w->event(w,
1556 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001557 break;
1558
1559 case snd_soc_dapm_post:
1560 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001561 list_for_each_entry_safe_continue(w, n, list,
1562 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001563
Mark Brownb22ead22009-06-07 12:51:26 +01001564 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001565 ret = w->event(w,
1566 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001567 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001568 ret = w->event(w,
1569 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001570 break;
1571
Mark Brown163cac02009-06-07 10:12:52 +01001572 default:
Mark Brown81628102009-06-07 13:21:24 +01001573 /* Queue it up for application */
1574 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001575 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001576 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001577 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001578 list_move(&w->power_list, &pending);
1579 break;
Mark Brown163cac02009-06-07 10:12:52 +01001580 }
Mark Brownb22ead22009-06-07 12:51:26 +01001581
1582 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001583 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001584 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001585 }
Mark Brownb22ead22009-06-07 12:51:26 +01001586
1587 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001588 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001589
1590 if (cur_dapm && cur_dapm->seq_notifier) {
1591 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1592 if (sort[i] == cur_sort)
1593 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001594 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001595 }
Mark Browneb270e92013-10-09 13:52:52 +01001596
1597 list_for_each_entry(d, &card->dapm_list, list) {
1598 soc_dapm_async_complete(d);
1599 }
Mark Brown163cac02009-06-07 10:12:52 +01001600}
1601
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001602static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001603{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001604 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001605 struct snd_soc_dapm_widget_list *wlist;
1606 struct snd_soc_dapm_widget *w = NULL;
1607 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001608 int ret;
1609
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001610 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001611 return;
1612
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001613 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001614
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001615 for (wi = 0; wi < wlist->num_widgets; wi++) {
1616 w = wlist->widgets[wi];
1617
1618 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1619 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1620 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001621 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001622 w->name, ret);
1623 }
Mark Brown97404f22010-12-14 16:13:57 +00001624 }
1625
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001626 if (!w)
1627 return;
1628
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001629 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1630 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001631 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001632 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001633 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001634
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001635 for (wi = 0; wi < wlist->num_widgets; wi++) {
1636 w = wlist->widgets[wi];
1637
1638 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1639 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1640 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001641 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001642 w->name, ret);
1643 }
Mark Brown97404f22010-12-14 16:13:57 +00001644 }
1645}
1646
Mark Brown9d0624a2011-02-18 11:49:43 -08001647/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1648 * they're changing state.
1649 */
1650static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1651{
1652 struct snd_soc_dapm_context *d = data;
1653 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001654
Mark Brown56fba412011-06-04 11:25:10 +01001655 /* If we're off and we're not supposed to be go into STANDBY */
1656 if (d->bias_level == SND_SOC_BIAS_OFF &&
1657 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001658 if (d->dev)
1659 pm_runtime_get_sync(d->dev);
1660
Mark Brown9d0624a2011-02-18 11:49:43 -08001661 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1662 if (ret != 0)
1663 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001664 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001665 }
1666
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001667 /* Prepare for a transition to ON or away from ON */
1668 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1669 d->bias_level != SND_SOC_BIAS_ON) ||
1670 (d->target_bias_level != SND_SOC_BIAS_ON &&
1671 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001672 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1673 if (ret != 0)
1674 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001675 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001676 }
1677}
1678
1679/* Async callback run prior to DAPM sequences - brings to their final
1680 * state.
1681 */
1682static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1683{
1684 struct snd_soc_dapm_context *d = data;
1685 int ret;
1686
1687 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001688 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1689 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1690 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001691 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1692 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001693 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001694 ret);
1695 }
1696
1697 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001698 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1699 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001700 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1701 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001702 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1703 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001704
1705 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001706 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001707 }
1708
1709 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001710 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1711 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001712 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1713 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001714 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001715 ret);
1716 }
1717}
Mark Brown97404f22010-12-14 16:13:57 +00001718
Mark Brownfe4fda52011-10-03 22:36:57 +01001719static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1720 bool power, bool connect)
1721{
1722 /* If a connection is being made or broken then that update
1723 * will have marked the peer dirty, otherwise the widgets are
1724 * not connected and this update has no impact. */
1725 if (!connect)
1726 return;
1727
1728 /* If the peer is already in the state we're moving to then we
1729 * won't have an impact on it. */
1730 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001731 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001732}
1733
Mark Brown05623c42011-09-28 17:02:31 +01001734static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1735 struct list_head *up_list,
1736 struct list_head *down_list)
1737{
Mark Browndb432b42011-10-03 21:06:40 +01001738 struct snd_soc_dapm_path *path;
1739
Mark Brown05623c42011-09-28 17:02:31 +01001740 if (w->power == power)
1741 return;
1742
1743 trace_snd_soc_dapm_widget_power(w, power);
1744
Mark Browndb432b42011-10-03 21:06:40 +01001745 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001746 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001747 */
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001748 list_for_each_entry(path, &w->sources, list_sink)
1749 dapm_widget_set_peer_power(path->source, power, path->connect);
1750
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001751 /* Supplies can't affect their outputs, only their inputs */
1752 if (!w->is_supply) {
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001753 list_for_each_entry(path, &w->sinks, list_source)
1754 dapm_widget_set_peer_power(path->sink, power,
1755 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001756 }
1757
Mark Brown05623c42011-09-28 17:02:31 +01001758 if (power)
1759 dapm_seq_insert(w, up_list, true);
1760 else
1761 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001762}
1763
Mark Brown7c81beb2011-09-20 22:22:32 +01001764static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1765 struct list_head *up_list,
1766 struct list_head *down_list)
1767{
Mark Brown7c81beb2011-09-20 22:22:32 +01001768 int power;
1769
1770 switch (w->id) {
1771 case snd_soc_dapm_pre:
1772 dapm_seq_insert(w, down_list, false);
1773 break;
1774 case snd_soc_dapm_post:
1775 dapm_seq_insert(w, up_list, true);
1776 break;
1777
1778 default:
Mark Brownd8050022011-09-28 18:28:23 +01001779 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001780
Mark Brown05623c42011-09-28 17:02:31 +01001781 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001782 break;
1783 }
1784}
1785
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001786static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1787{
1788 if (dapm->idle_bias_off)
1789 return true;
1790
1791 switch (snd_power_get_state(dapm->card->snd_card)) {
1792 case SNDRV_CTL_POWER_D3hot:
1793 case SNDRV_CTL_POWER_D3cold:
1794 return dapm->suspend_bias_off;
1795 default:
1796 break;
1797 }
1798
1799 return false;
1800}
1801
Mark Brown42aa3412009-03-01 19:21:10 +00001802/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001803 * Scan each dapm widget for complete audio path.
1804 * A complete path is a route that has valid endpoints i.e.:-
1805 *
1806 * o DAC to output pin.
1807 * o Input Pin to ADC.
1808 * o Input pin to Output pin (bypass, sidetone)
1809 * o DAC to ADC (loopback).
1810 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001811static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001812{
1813 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001814 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001815 LIST_HEAD(up_list);
1816 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001817 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001818 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001819
Mark Brownf9fa2b12014-03-06 16:49:11 +08001820 lockdep_assert_held(&card->dapm_mutex);
1821
Mark Brown84e90932010-11-04 00:07:02 -04001822 trace_snd_soc_dapm_start(card);
1823
Mark Brown56fba412011-06-04 11:25:10 +01001824 list_for_each_entry(d, &card->dapm_list, list) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001825 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001826 d->target_bias_level = SND_SOC_BIAS_OFF;
1827 else
1828 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001829 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001830
Liam Girdwood6c120e12012-02-15 15:15:34 +00001831 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001832
Mark Brown6d3ddc82009-05-16 17:47:29 +01001833 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001834 * lists indicating if they should be powered up or down. We
1835 * only check widgets that have been flagged as dirty but note
1836 * that new widgets may be added to the dirty list while we
1837 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001838 */
Mark Browndb432b42011-10-03 21:06:40 +01001839 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001840 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001841 }
1842
Mark Brownf9de6d72011-09-28 17:19:47 +01001843 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001844 switch (w->id) {
1845 case snd_soc_dapm_pre:
1846 case snd_soc_dapm_post:
1847 /* These widgets always need to be powered */
1848 break;
1849 default:
1850 list_del_init(&w->dirty);
1851 break;
1852 }
Mark Browndb432b42011-10-03 21:06:40 +01001853
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001854 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001855 d = w->dapm;
1856
1857 /* Supplies and micbiases only bring the
1858 * context up to STANDBY as unless something
1859 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001860 * generally don't require full power. Signal
1861 * generators are virtual pins and have no
1862 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001863 */
1864 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001865 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001866 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001867 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001868 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001869 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001870 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001871 case snd_soc_dapm_micbias:
1872 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1873 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1874 break;
1875 default:
1876 d->target_bias_level = SND_SOC_BIAS_ON;
1877 break;
1878 }
1879 }
1880
1881 }
1882
Mark Brown85a843c2011-09-21 21:29:47 +01001883 /* Force all contexts in the card to the same bias state if
1884 * they're not ground referenced.
1885 */
Mark Brown56fba412011-06-04 11:25:10 +01001886 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001887 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001888 if (d->target_bias_level > bias)
1889 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001890 list_for_each_entry(d, &card->dapm_list, list)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001891 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01001892 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001893
Mark Brownde02d072011-09-20 21:43:24 +01001894 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001895
Xiang Xiao17282ba2014-03-02 00:04:03 +08001896 /* Run card bias changes at first */
1897 dapm_pre_sequence_async(&card->dapm, 0);
1898 /* Run other bias changes in parallel */
1899 list_for_each_entry(d, &card->dapm_list, list) {
1900 if (d != &card->dapm)
1901 async_schedule_domain(dapm_pre_sequence_async, d,
1902 &async_domain);
1903 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001904 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001905
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001906 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001907 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001908 }
1909
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001910 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001911 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001912 }
1913
Mark Brown6d3ddc82009-05-16 17:47:29 +01001914 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001915 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001916
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001917 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001918
Mark Brown6d3ddc82009-05-16 17:47:29 +01001919 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001920 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001921
Mark Brown9d0624a2011-02-18 11:49:43 -08001922 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001923 list_for_each_entry(d, &card->dapm_list, list) {
1924 if (d != &card->dapm)
1925 async_schedule_domain(dapm_post_sequence_async, d,
1926 &async_domain);
1927 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001928 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001929 /* Run card bias changes at last */
1930 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001931
Liam Girdwood8078d872012-02-15 15:15:35 +00001932 /* do we need to notify any clients that DAPM event is complete */
1933 list_for_each_entry(d, &card->dapm_list, list) {
1934 if (d->stream_event)
1935 d->stream_event(d, event);
1936 }
1937
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001938 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001939 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001940 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001941
Mark Brown84e90932010-11-04 00:07:02 -04001942 trace_snd_soc_dapm_done(card);
1943
Mark Brown42aa3412009-03-01 19:21:10 +00001944 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001945}
1946
Mark Brown79fb9382009-08-21 16:38:13 +01001947#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001948static ssize_t dapm_widget_power_read_file(struct file *file,
1949 char __user *user_buf,
1950 size_t count, loff_t *ppos)
1951{
1952 struct snd_soc_dapm_widget *w = file->private_data;
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02001953 struct snd_soc_card *card = w->dapm->card;
Mark Brown79fb9382009-08-21 16:38:13 +01001954 char *buf;
1955 int in, out;
1956 ssize_t ret;
1957 struct snd_soc_dapm_path *p = NULL;
1958
1959 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1960 if (!buf)
1961 return -ENOMEM;
1962
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02001963 mutex_lock(&card->dapm_mutex);
1964
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001965 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
1966 if (w->is_supply) {
1967 in = 0;
1968 out = 0;
1969 } else {
1970 in = is_connected_input_ep(w, NULL);
1971 out = is_connected_output_ep(w, NULL);
1972 }
Mark Brown79fb9382009-08-21 16:38:13 +01001973
Mark Brownf13ebad2012-03-03 18:01:01 +00001974 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1975 w->name, w->power ? "On" : "Off",
1976 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001977
Mark Brownd033c362009-12-04 15:25:56 +00001978 if (w->reg >= 0)
1979 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001980 " - R%d(0x%x) mask 0x%x",
1981 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001982
1983 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1984
Mark Brown3eef08b2009-09-14 16:49:00 +01001985 if (w->sname)
1986 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1987 w->sname,
1988 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001989
1990 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001991 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001992 continue;
1993
Mark Brown79fb9382009-08-21 16:38:13 +01001994 if (p->connect)
1995 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001996 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001997 p->name ? p->name : "static",
1998 p->source->name);
1999 }
2000 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01002001 if (p->connected && !p->connected(w, p->sink))
2002 continue;
2003
Mark Brown79fb9382009-08-21 16:38:13 +01002004 if (p->connect)
2005 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00002006 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01002007 p->name ? p->name : "static",
2008 p->sink->name);
2009 }
2010
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002011 mutex_unlock(&card->dapm_mutex);
2012
Mark Brown79fb9382009-08-21 16:38:13 +01002013 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2014
2015 kfree(buf);
2016 return ret;
2017}
2018
2019static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002020 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002021 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002022 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002023};
2024
Mark Brownef49e4f2011-04-04 20:48:13 +09002025static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2026 size_t count, loff_t *ppos)
2027{
2028 struct snd_soc_dapm_context *dapm = file->private_data;
2029 char *level;
2030
2031 switch (dapm->bias_level) {
2032 case SND_SOC_BIAS_ON:
2033 level = "On\n";
2034 break;
2035 case SND_SOC_BIAS_PREPARE:
2036 level = "Prepare\n";
2037 break;
2038 case SND_SOC_BIAS_STANDBY:
2039 level = "Standby\n";
2040 break;
2041 case SND_SOC_BIAS_OFF:
2042 level = "Off\n";
2043 break;
2044 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002045 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002046 level = "Unknown\n";
2047 break;
2048 }
2049
2050 return simple_read_from_buffer(user_buf, count, ppos, level,
2051 strlen(level));
2052}
2053
2054static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002055 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002056 .read = dapm_bias_read_file,
2057 .llseek = default_llseek,
2058};
2059
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002060void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2061 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002062{
Mark Brown79fb9382009-08-21 16:38:13 +01002063 struct dentry *d;
2064
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02002065 if (!parent)
2066 return;
2067
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002068 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2069
2070 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002071 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002072 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002073 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002074 }
Mark Brown79fb9382009-08-21 16:38:13 +01002075
Mark Brownef49e4f2011-04-04 20:48:13 +09002076 d = debugfs_create_file("bias_level", 0444,
2077 dapm->debugfs_dapm, dapm,
2078 &dapm_bias_fops);
2079 if (!d)
2080 dev_warn(dapm->dev,
2081 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002082}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002083
2084static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2085{
2086 struct snd_soc_dapm_context *dapm = w->dapm;
2087 struct dentry *d;
2088
2089 if (!dapm->debugfs_dapm || !w->name)
2090 return;
2091
2092 d = debugfs_create_file(w->name, 0444,
2093 dapm->debugfs_dapm, w,
2094 &dapm_widget_power_fops);
2095 if (!d)
2096 dev_warn(w->dapm->dev,
2097 "ASoC: Failed to create %s debugfs file\n",
2098 w->name);
2099}
2100
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002101static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2102{
2103 debugfs_remove_recursive(dapm->debugfs_dapm);
2104}
2105
Mark Brown79fb9382009-08-21 16:38:13 +01002106#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002107void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2108 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002109{
2110}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002111
2112static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2113{
2114}
2115
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002116static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2117{
2118}
2119
Mark Brown79fb9382009-08-21 16:38:13 +01002120#endif
2121
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002122/*
2123 * soc_dapm_connect_path() - Connects or disconnects a path
2124 * @path: The path to update
2125 * @connect: The new connect state of the path. True if the path is connected,
2126 * false if it is disconneted.
2127 * @reason: The reason why the path changed (for debugging only)
2128 */
2129static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2130 bool connect, const char *reason)
2131{
2132 if (path->connect == connect)
2133 return;
2134
2135 path->connect = connect;
2136 dapm_mark_dirty(path->source, reason);
2137 dapm_mark_dirty(path->sink, reason);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002138 dapm_path_invalidate(path);
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002139}
2140
Richard Purdie2b97eab2006-10-06 18:32:18 +02002141/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002142static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002143 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002144{
2145 struct snd_soc_dapm_path *path;
2146 int found = 0;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002147 bool connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002148
Mark Brownf9fa2b12014-03-06 16:49:11 +08002149 lockdep_assert_held(&card->dapm_mutex);
2150
Richard Purdie2b97eab2006-10-06 18:32:18 +02002151 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002152 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002153 found = 1;
2154 /* we now need to match the string in the enum to the path */
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002155 if (!(strcmp(path->name, e->texts[mux])))
2156 connect = true;
2157 else
2158 connect = false;
2159
2160 soc_dapm_connect_path(path, connect, "mux update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002161 }
2162
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002163 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002164 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002165
Liam Girdwood618dae12012-04-25 12:12:51 +01002166 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002167}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002168
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002169int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002170 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2171 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002172{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002173 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002174 int ret;
2175
Liam Girdwood3cd04342012-03-09 12:02:08 +00002176 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002177 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002178 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002179 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002180 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002181 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002182 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002183 return ret;
2184}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002185EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002186
Milan plzik1b075e32008-01-10 14:39:46 +01002187/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002188static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002189 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002190{
2191 struct snd_soc_dapm_path *path;
2192 int found = 0;
2193
Mark Brownf9fa2b12014-03-06 16:49:11 +08002194 lockdep_assert_held(&card->dapm_mutex);
2195
Richard Purdie2b97eab2006-10-06 18:32:18 +02002196 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002197 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002198 found = 1;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002199 soc_dapm_connect_path(path, connect, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002200 }
2201
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002202 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002203 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002204
Liam Girdwood618dae12012-04-25 12:12:51 +01002205 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002206}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002207
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002208int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002209 struct snd_kcontrol *kcontrol, int connect,
2210 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002211{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002212 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002213 int ret;
2214
Liam Girdwood3cd04342012-03-09 12:02:08 +00002215 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002216 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002217 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002218 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002219 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002220 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002221 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002222 return ret;
2223}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002224EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002225
Benoit Cousson44ba2642014-07-08 23:19:36 +02002226static ssize_t dapm_widget_show_codec(struct snd_soc_codec *codec, char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002227{
Richard Purdie2b97eab2006-10-06 18:32:18 +02002228 struct snd_soc_dapm_widget *w;
2229 int count = 0;
2230 char *state = "not set";
2231
Lars-Peter Clausen00200102014-07-17 22:01:07 +02002232 list_for_each_entry(w, &codec->component.card->widgets, list) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002233 if (w->dapm != &codec->dapm)
2234 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002235
2236 /* only display widgets that burnm power */
2237 switch (w->id) {
2238 case snd_soc_dapm_hp:
2239 case snd_soc_dapm_mic:
2240 case snd_soc_dapm_spk:
2241 case snd_soc_dapm_line:
2242 case snd_soc_dapm_micbias:
2243 case snd_soc_dapm_dac:
2244 case snd_soc_dapm_adc:
2245 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002246 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002247 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002248 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002249 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002250 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002251 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002252 if (w->name)
2253 count += sprintf(buf + count, "%s: %s\n",
2254 w->name, w->power ? "On":"Off");
2255 break;
2256 default:
2257 break;
2258 }
2259 }
2260
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002261 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002262 case SND_SOC_BIAS_ON:
2263 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002264 break;
Mark Brown0be98982008-05-19 12:31:28 +02002265 case SND_SOC_BIAS_PREPARE:
2266 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002267 break;
Mark Brown0be98982008-05-19 12:31:28 +02002268 case SND_SOC_BIAS_STANDBY:
2269 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002270 break;
Mark Brown0be98982008-05-19 12:31:28 +02002271 case SND_SOC_BIAS_OFF:
2272 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002273 break;
2274 }
2275 count += sprintf(buf + count, "PM State: %s\n", state);
2276
2277 return count;
2278}
2279
Benoit Cousson44ba2642014-07-08 23:19:36 +02002280/* show dapm widget status in sys fs */
2281static ssize_t dapm_widget_show(struct device *dev,
2282 struct device_attribute *attr, char *buf)
2283{
2284 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2285 int i, count = 0;
2286
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002287 mutex_lock(&rtd->card->dapm_mutex);
2288
Benoit Cousson44ba2642014-07-08 23:19:36 +02002289 for (i = 0; i < rtd->num_codecs; i++) {
2290 struct snd_soc_codec *codec = rtd->codec_dais[i]->codec;
2291 count += dapm_widget_show_codec(codec, buf + count);
2292 }
2293
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002294 mutex_unlock(&rtd->card->dapm_mutex);
2295
Benoit Cousson44ba2642014-07-08 23:19:36 +02002296 return count;
2297}
2298
Richard Purdie2b97eab2006-10-06 18:32:18 +02002299static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2300
Takashi Iwaid29697d2015-01-30 20:16:37 +01002301struct attribute *soc_dapm_dev_attrs[] = {
2302 &dev_attr_dapm_widget.attr,
2303 NULL
2304};
Richard Purdie2b97eab2006-10-06 18:32:18 +02002305
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002306static void dapm_free_path(struct snd_soc_dapm_path *path)
2307{
2308 list_del(&path->list_sink);
2309 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002310 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002311 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002312 kfree(path);
2313}
2314
Richard Purdie2b97eab2006-10-06 18:32:18 +02002315/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002316static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002317{
2318 struct snd_soc_dapm_widget *w, *next_w;
2319 struct snd_soc_dapm_path *p, *next_p;
2320
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002321 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2322 if (w->dapm != dapm)
2323 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002324 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002325 /*
2326 * remove source and sink paths associated to this widget.
2327 * While removing the path, remove reference to it from both
2328 * source and sink widgets so that path is removed only once.
2329 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002330 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2331 dapm_free_path(p);
2332
2333 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2334 dapm_free_path(p);
2335
Stephen Warrenfad59882011-04-28 17:37:59 -06002336 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002337 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002338 kfree(w);
2339 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002340}
2341
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002342static struct snd_soc_dapm_widget *dapm_find_widget(
2343 struct snd_soc_dapm_context *dapm, const char *pin,
2344 bool search_other_contexts)
2345{
2346 struct snd_soc_dapm_widget *w;
2347 struct snd_soc_dapm_widget *fallback = NULL;
2348
2349 list_for_each_entry(w, &dapm->card->widgets, list) {
2350 if (!strcmp(w->name, pin)) {
2351 if (w->dapm == dapm)
2352 return w;
2353 else
2354 fallback = w;
2355 }
2356 }
2357
2358 if (search_other_contexts)
2359 return fallback;
2360
2361 return NULL;
2362}
2363
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002364static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002365 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002366{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002367 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002368
Mark Brownf9fa2b12014-03-06 16:49:11 +08002369 dapm_assert_locked(dapm);
2370
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002371 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002372 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002373 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002374 }
2375
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002376 if (w->connected != status) {
Mark Brown1a8b2d92012-02-16 11:50:07 -08002377 dapm_mark_dirty(w, "pin configuration");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002378 dapm_widget_invalidate_input_paths(w);
2379 dapm_widget_invalidate_output_paths(w);
2380 }
Mark Brown1a8b2d92012-02-16 11:50:07 -08002381
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002382 w->connected = status;
2383 if (status == 0)
2384 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002385
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002386 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002387}
2388
Richard Purdie2b97eab2006-10-06 18:32:18 +02002389/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002390 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2391 * @dapm: DAPM context
2392 *
2393 * Walks all dapm audio paths and powers widgets according to their
2394 * stream or path usage.
2395 *
2396 * Requires external locking.
2397 *
2398 * Returns 0 for success.
2399 */
2400int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2401{
2402 /*
2403 * Suppress early reports (eg, jacks syncing their state) to avoid
2404 * silly DAPM runs during card startup.
2405 */
2406 if (!dapm->card || !dapm->card->instantiated)
2407 return 0;
2408
2409 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2410}
2411EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2412
2413/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002414 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002415 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002416 *
2417 * Walks all dapm audio paths and powers widgets according to their
2418 * stream or path usage.
2419 *
2420 * Returns 0 for success.
2421 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002422int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002423{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002424 int ret;
2425
Liam Girdwood3cd04342012-03-09 12:02:08 +00002426 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002427 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002428 mutex_unlock(&dapm->card->dapm_mutex);
2429 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002430}
Liam Girdwooda5302182008-07-07 13:35:17 +01002431EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002432
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002433/*
2434 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2435 * @w: The widget for which to update the flags
2436 *
2437 * Some widgets have a dynamic category which depends on which neighbors they
2438 * are connected to. This function update the category for these widgets.
2439 *
2440 * This function must be called whenever a path is added or removed to a widget.
2441 */
2442static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2443{
2444 struct snd_soc_dapm_path *p;
2445
2446 switch (w->id) {
2447 case snd_soc_dapm_input:
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002448 /* On a fully routed card a input is never a source */
2449 if (w->dapm->card->fully_routed)
2450 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002451 w->is_source = 1;
2452 list_for_each_entry(p, &w->sources, list_sink) {
2453 if (p->source->id == snd_soc_dapm_micbias ||
2454 p->source->id == snd_soc_dapm_mic ||
2455 p->source->id == snd_soc_dapm_line ||
2456 p->source->id == snd_soc_dapm_output) {
2457 w->is_source = 0;
2458 break;
2459 }
2460 }
2461 break;
2462 case snd_soc_dapm_output:
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002463 /* On a fully routed card a output is never a sink */
2464 if (w->dapm->card->fully_routed)
2465 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002466 w->is_sink = 1;
2467 list_for_each_entry(p, &w->sinks, list_source) {
2468 if (p->sink->id == snd_soc_dapm_spk ||
2469 p->sink->id == snd_soc_dapm_hp ||
2470 p->sink->id == snd_soc_dapm_line ||
2471 p->sink->id == snd_soc_dapm_input) {
2472 w->is_sink = 0;
2473 break;
2474 }
2475 }
2476 break;
2477 case snd_soc_dapm_line:
2478 w->is_sink = !list_empty(&w->sources);
2479 w->is_source = !list_empty(&w->sinks);
2480 break;
2481 default:
2482 break;
2483 }
2484}
2485
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002486static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2487 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2488 const char *control)
2489{
2490 bool dynamic_source = false;
2491 bool dynamic_sink = false;
2492
2493 if (!control)
2494 return 0;
2495
2496 switch (source->id) {
2497 case snd_soc_dapm_demux:
2498 dynamic_source = true;
2499 break;
2500 default:
2501 break;
2502 }
2503
2504 switch (sink->id) {
2505 case snd_soc_dapm_mux:
2506 case snd_soc_dapm_switch:
2507 case snd_soc_dapm_mixer:
2508 case snd_soc_dapm_mixer_named_ctl:
2509 dynamic_sink = true;
2510 break;
2511 default:
2512 break;
2513 }
2514
2515 if (dynamic_source && dynamic_sink) {
2516 dev_err(dapm->dev,
2517 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2518 source->name, control, sink->name);
2519 return -EINVAL;
2520 } else if (!dynamic_source && !dynamic_sink) {
2521 dev_err(dapm->dev,
2522 "Control not supported for path %s -> [%s] -> %s\n",
2523 source->name, control, sink->name);
2524 return -EINVAL;
2525 }
2526
2527 return 0;
2528}
2529
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002530static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2531 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2532 const char *control,
2533 int (*connected)(struct snd_soc_dapm_widget *source,
2534 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002535{
2536 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002537 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002538
Lars-Peter Clausene409dfb2014-10-25 17:42:02 +02002539 if (wsink->is_supply && !wsource->is_supply) {
2540 dev_err(dapm->dev,
2541 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2542 wsource->name, wsink->name);
2543 return -EINVAL;
2544 }
2545
2546 if (connected && !wsource->is_supply) {
2547 dev_err(dapm->dev,
2548 "connected() callback only supported for supply widgets (%s -> %s)\n",
2549 wsource->name, wsink->name);
2550 return -EINVAL;
2551 }
2552
2553 if (wsource->is_supply && control) {
2554 dev_err(dapm->dev,
2555 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2556 wsource->name, control, wsink->name);
2557 return -EINVAL;
2558 }
2559
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002560 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2561 if (ret)
2562 return ret;
2563
Richard Purdie2b97eab2006-10-06 18:32:18 +02002564 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2565 if (!path)
2566 return -ENOMEM;
2567
2568 path->source = wsource;
2569 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002570 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002571 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002572 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002573 INIT_LIST_HEAD(&path->list_source);
2574 INIT_LIST_HEAD(&path->list_sink);
2575
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002576 if (wsource->is_supply || wsink->is_supply)
2577 path->is_supply = 1;
2578
Richard Purdie2b97eab2006-10-06 18:32:18 +02002579 /* connect static paths */
2580 if (control == NULL) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002581 path->connect = 1;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002582 } else {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002583 switch (wsource->id) {
2584 case snd_soc_dapm_demux:
2585 ret = dapm_connect_mux(dapm, path, control, wsource);
2586 if (ret)
2587 goto err;
2588 break;
2589 default:
2590 break;
2591 }
2592
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002593 switch (wsink->id) {
2594 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002595 ret = dapm_connect_mux(dapm, path, control, wsink);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002596 if (ret != 0)
2597 goto err;
2598 break;
2599 case snd_soc_dapm_switch:
2600 case snd_soc_dapm_mixer:
2601 case snd_soc_dapm_mixer_named_ctl:
2602 ret = dapm_connect_mixer(dapm, path, control);
2603 if (ret != 0)
2604 goto err;
2605 break;
2606 default:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002607 break;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002608 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002609 }
2610
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002611 list_add(&path->list, &dapm->card->paths);
2612 list_add(&path->list_sink, &wsink->sources);
2613 list_add(&path->list_source, &wsource->sinks);
2614
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002615 dapm_update_widget_flags(wsource);
2616 dapm_update_widget_flags(wsink);
2617
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002618 dapm_mark_dirty(wsource, "Route added");
2619 dapm_mark_dirty(wsink, "Route added");
Mark Brownfabd0382012-07-05 17:20:06 +01002620
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002621 if (dapm->card->instantiated && path->connect)
2622 dapm_path_invalidate(path);
2623
Richard Purdie2b97eab2006-10-06 18:32:18 +02002624 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002625err:
2626 kfree(path);
2627 return ret;
2628}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002629
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002630static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002631 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002632{
2633 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2634 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2635 const char *sink;
2636 const char *source;
2637 char prefixed_sink[80];
2638 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002639 const char *prefix;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002640 int ret;
2641
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002642 prefix = soc_dapm_prefix(dapm);
2643 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002644 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002645 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002646 sink = prefixed_sink;
2647 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002648 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002649 source = prefixed_source;
2650 } else {
2651 sink = route->sink;
2652 source = route->source;
2653 }
2654
Charles Keepax45a110a2015-05-11 13:50:30 +01002655 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2656 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2657
2658 if (wsink && wsource)
2659 goto skip_search;
2660
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002661 /*
2662 * find src and dest widgets over all widgets but favor a widget from
2663 * current DAPM context
2664 */
2665 list_for_each_entry(w, &dapm->card->widgets, list) {
2666 if (!wsink && !(strcmp(w->name, sink))) {
2667 wtsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002668 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002669 wsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002670 if (wsource)
2671 break;
2672 }
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002673 continue;
2674 }
2675 if (!wsource && !(strcmp(w->name, source))) {
2676 wtsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002677 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002678 wsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002679 if (wsink)
2680 break;
2681 }
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002682 }
2683 }
2684 /* use widget from another DAPM context if not found from this */
2685 if (!wsink)
2686 wsink = wtsink;
2687 if (!wsource)
2688 wsource = wtsource;
2689
2690 if (wsource == NULL) {
2691 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2692 route->source);
2693 return -ENODEV;
2694 }
2695 if (wsink == NULL) {
2696 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2697 route->sink);
2698 return -ENODEV;
2699 }
2700
Charles Keepax45a110a2015-05-11 13:50:30 +01002701skip_search:
2702 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2703 dapm_wcache_update(&dapm->path_source_cache, wsource);
2704
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002705 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2706 route->connected);
2707 if (ret)
2708 goto err;
2709
2710 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002711err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002712 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002713 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002714 return ret;
2715}
Mark Brown105f1c22008-05-13 14:52:19 +02002716
Mark Brownefcc3c62012-07-05 17:24:19 +01002717static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2718 const struct snd_soc_dapm_route *route)
2719{
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002720 struct snd_soc_dapm_widget *wsource, *wsink;
Mark Brownefcc3c62012-07-05 17:24:19 +01002721 struct snd_soc_dapm_path *path, *p;
2722 const char *sink;
2723 const char *source;
2724 char prefixed_sink[80];
2725 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002726 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002727
2728 if (route->control) {
2729 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002730 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002731 return -EINVAL;
2732 }
2733
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002734 prefix = soc_dapm_prefix(dapm);
2735 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002736 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002737 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002738 sink = prefixed_sink;
2739 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002740 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002741 source = prefixed_source;
2742 } else {
2743 sink = route->sink;
2744 source = route->source;
2745 }
2746
2747 path = NULL;
2748 list_for_each_entry(p, &dapm->card->paths, list) {
2749 if (strcmp(p->source->name, source) != 0)
2750 continue;
2751 if (strcmp(p->sink->name, sink) != 0)
2752 continue;
2753 path = p;
2754 break;
2755 }
2756
2757 if (path) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002758 wsource = path->source;
2759 wsink = path->sink;
2760
2761 dapm_mark_dirty(wsource, "Route removed");
2762 dapm_mark_dirty(wsink, "Route removed");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002763 if (path->connect)
2764 dapm_path_invalidate(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002765
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002766 dapm_free_path(path);
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002767
2768 /* Update any path related flags */
2769 dapm_update_widget_flags(wsource);
2770 dapm_update_widget_flags(wsink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002771 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002772 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002773 source, sink);
2774 }
2775
2776 return 0;
2777}
2778
Mark Brown105f1c22008-05-13 14:52:19 +02002779/**
Mark Brown105f1c22008-05-13 14:52:19 +02002780 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002781 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002782 * @route: audio routes
2783 * @num: number of routes
2784 *
2785 * Connects 2 dapm widgets together via a named audio path. The sink is
2786 * the widget receiving the audio signal, whilst the source is the sender
2787 * of the audio signal.
2788 *
2789 * Returns 0 for success else error. On error all resources can be freed
2790 * with a call to snd_soc_card_free().
2791 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002792int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002793 const struct snd_soc_dapm_route *route, int num)
2794{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002795 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002796
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002797 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002798 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002799 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002800 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002801 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2802 route->source,
2803 route->control ? route->control : "direct",
2804 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002805 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002806 }
2807 route++;
2808 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002809 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002810
Dan Carpenter60884c22012-04-13 22:25:43 +03002811 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002812}
2813EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2814
Mark Brownefcc3c62012-07-05 17:24:19 +01002815/**
2816 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2817 * @dapm: DAPM context
2818 * @route: audio routes
2819 * @num: number of routes
2820 *
2821 * Removes routes from the DAPM context.
2822 */
2823int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2824 const struct snd_soc_dapm_route *route, int num)
2825{
2826 int i, ret = 0;
2827
2828 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2829 for (i = 0; i < num; i++) {
2830 snd_soc_dapm_del_route(dapm, route);
2831 route++;
2832 }
2833 mutex_unlock(&dapm->card->dapm_mutex);
2834
2835 return ret;
2836}
2837EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2838
Mark Brownbf3a9e12011-06-13 16:42:29 +01002839static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2840 const struct snd_soc_dapm_route *route)
2841{
2842 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2843 route->source,
2844 true);
2845 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2846 route->sink,
2847 true);
2848 struct snd_soc_dapm_path *path;
2849 int count = 0;
2850
2851 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002852 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002853 route->source);
2854 return -ENODEV;
2855 }
2856
2857 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002858 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002859 route->sink);
2860 return -ENODEV;
2861 }
2862
2863 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002864 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002865 route->source, route->sink);
2866
2867 list_for_each_entry(path, &source->sinks, list_source) {
2868 if (path->sink == sink) {
2869 path->weak = 1;
2870 count++;
2871 }
2872 }
2873
2874 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002875 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002876 route->source, route->sink);
2877 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002878 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002879 count, route->source, route->sink);
2880
2881 return 0;
2882}
2883
2884/**
2885 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2886 * @dapm: DAPM context
2887 * @route: audio routes
2888 * @num: number of routes
2889 *
2890 * Mark existing routes matching those specified in the passed array
2891 * as being weak, meaning that they are ignored for the purpose of
2892 * power decisions. The main intended use case is for sidetone paths
2893 * which couple audio between other independent paths if they are both
2894 * active in order to make the combination work better at the user
2895 * level but which aren't intended to be "used".
2896 *
2897 * Note that CODEC drivers should not use this as sidetone type paths
2898 * can frequently also be used as bypass paths.
2899 */
2900int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2901 const struct snd_soc_dapm_route *route, int num)
2902{
2903 int i, err;
2904 int ret = 0;
2905
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002906 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002907 for (i = 0; i < num; i++) {
2908 err = snd_soc_dapm_weak_route(dapm, route);
2909 if (err)
2910 ret = err;
2911 route++;
2912 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002913 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002914
2915 return ret;
2916}
2917EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2918
Mark Brown105f1c22008-05-13 14:52:19 +02002919/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002920 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002921 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002922 *
2923 * Checks the codec for any new dapm widgets and creates them if found.
2924 *
2925 * Returns 0 for success.
2926 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002927int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002928{
2929 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002930 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002931
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002932 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002933
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002934 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002935 {
2936 if (w->new)
2937 continue;
2938
Stephen Warrenfad59882011-04-28 17:37:59 -06002939 if (w->num_kcontrols) {
2940 w->kcontrols = kzalloc(w->num_kcontrols *
2941 sizeof(struct snd_kcontrol *),
2942 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002943 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002944 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002945 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002946 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002947 }
2948
Richard Purdie2b97eab2006-10-06 18:32:18 +02002949 switch(w->id) {
2950 case snd_soc_dapm_switch:
2951 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002952 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002953 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002954 break;
2955 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002956 case snd_soc_dapm_demux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002957 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002958 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002959 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002960 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002961 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002962 break;
Nikesh Oswalc6615082015-02-02 17:06:44 +00002963 case snd_soc_dapm_dai_link:
2964 dapm_new_dai_link(w);
2965 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002966 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002967 break;
2968 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002969
2970 /* Read the initial power state from the device */
2971 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002972 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002973 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002974 val &= w->mask;
2975 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002976 w->power = 1;
2977 }
2978
Richard Purdie2b97eab2006-10-06 18:32:18 +02002979 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002980
Mark Brown7508b122011-10-05 12:09:12 +01002981 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002982 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002983 }
2984
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002985 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2986 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002987 return 0;
2988}
2989EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2990
2991/**
2992 * snd_soc_dapm_get_volsw - dapm mixer get callback
2993 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002994 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002995 *
2996 * Callback to get the value of a dapm mixer control.
2997 *
2998 * Returns 0 for success.
2999 */
3000int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3001 struct snd_ctl_elem_value *ucontrol)
3002{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003003 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3004 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003005 struct soc_mixer_control *mc =
3006 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003007 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003008 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003009 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003010 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003011 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003012 unsigned int val;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003013 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003014
3015 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003016 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003017 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003018 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003019
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003020 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003021 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3022 ret = soc_dapm_read(dapm, reg, &val);
3023 val = (val >> shift) & mask;
3024 } else {
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003025 val = dapm_kcontrol_get_value(kcontrol);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003026 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003027 mutex_unlock(&card->dapm_mutex);
3028
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003029 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003030 ucontrol->value.integer.value[0] = max - val;
3031 else
3032 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003033
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003034 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003035}
3036EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3037
3038/**
3039 * snd_soc_dapm_put_volsw - dapm mixer set callback
3040 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003041 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003042 *
3043 * Callback to set the value of a dapm mixer control.
3044 *
3045 * Returns 0 for success.
3046 */
3047int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3048 struct snd_ctl_elem_value *ucontrol)
3049{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003050 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3051 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003052 struct soc_mixer_control *mc =
3053 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003054 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003055 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003056 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003057 unsigned int mask = (1 << fls(max)) - 1;
3058 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07003059 unsigned int val;
Jarkko Nikula18626c72014-06-09 14:20:29 +03003060 int connect, change, reg_change = 0;
Mark Brown97404f22010-12-14 16:13:57 +00003061 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08003062 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003063
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003064 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003065 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003066 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003067 kcontrol->id.name);
3068
Richard Purdie2b97eab2006-10-06 18:32:18 +02003069 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02003070 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003071
3072 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003073 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003074
Liam Girdwood3cd04342012-03-09 12:02:08 +00003075 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003076
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003077 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown283375c2009-12-07 18:09:03 +00003078
Jarkko Nikula18626c72014-06-09 14:20:29 +03003079 if (reg != SND_SOC_NOPM) {
3080 mask = mask << shift;
3081 val = val << shift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02003082
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003083 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
Jarkko Nikula18626c72014-06-09 14:20:29 +03003084 }
3085
3086 if (change || reg_change) {
3087 if (reg_change) {
3088 update.kcontrol = kcontrol;
3089 update.reg = reg;
3090 update.mask = mask;
3091 update.val = val;
3092 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003093 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003094 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00003095
Nenghua Cao52765972013-12-13 20:13:49 +08003096 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00003097
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003098 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00003099 }
3100
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003101 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003102
3103 if (ret > 0)
3104 soc_dpcm_runtime_update(card);
3105
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003106 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003107}
3108EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3109
3110/**
3111 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3112 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003113 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003114 *
3115 * Callback to get the value of a dapm enumerated double mixer control.
3116 *
3117 * Returns 0 for success.
3118 */
3119int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3120 struct snd_ctl_elem_value *ucontrol)
3121{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003122 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Charles Keepax561ed682015-05-01 12:37:26 +01003123 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003124 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003125 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003126
Charles Keepax561ed682015-05-01 12:37:26 +01003127 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3128 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003129 int ret = soc_dapm_read(dapm, e->reg, &reg_val);
Charles Keepax964a0b82015-05-08 10:50:10 +01003130 if (ret) {
3131 mutex_unlock(&card->dapm_mutex);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003132 return ret;
Charles Keepax964a0b82015-05-08 10:50:10 +01003133 }
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003134 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003135 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003136 }
Charles Keepax561ed682015-05-01 12:37:26 +01003137 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003138
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003139 val = (reg_val >> e->shift_l) & e->mask;
3140 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3141 if (e->shift_l != e->shift_r) {
3142 val = (reg_val >> e->shift_r) & e->mask;
3143 val = snd_soc_enum_val_to_item(e, val);
3144 ucontrol->value.enumerated.item[1] = val;
3145 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003146
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003147 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003148}
3149EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3150
3151/**
3152 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3153 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003154 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003155 *
3156 * Callback to set the value of a dapm enumerated double mixer control.
3157 *
3158 * Returns 0 for success.
3159 */
3160int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3161 struct snd_ctl_elem_value *ucontrol)
3162{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003163 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3164 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003165 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003166 unsigned int *item = ucontrol->value.enumerated.item;
Charles Keepax561ed682015-05-01 12:37:26 +01003167 unsigned int val, change, reg_change = 0;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003168 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00003169 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08003170 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003171
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003172 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003173 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003174
3175 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003176 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003177 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003178 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003179 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003180 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003181 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003182 }
3183
Liam Girdwood3cd04342012-03-09 12:02:08 +00003184 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003185
Charles Keepax561ed682015-05-01 12:37:26 +01003186 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00003187
Charles Keepax561ed682015-05-01 12:37:26 +01003188 if (e->reg != SND_SOC_NOPM)
3189 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3190
3191 if (change || reg_change) {
3192 if (reg_change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003193 update.kcontrol = kcontrol;
3194 update.reg = e->reg;
3195 update.mask = mask;
3196 update.val = val;
3197 card->update = &update;
3198 }
Charles Keepax561ed682015-05-01 12:37:26 +01003199 change |= reg_change;
Mark Brown3a655772009-10-05 17:23:30 +01003200
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003201 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003202
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003203 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003204 }
3205
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003206 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003207
3208 if (ret > 0)
3209 soc_dpcm_runtime_update(card);
3210
Mark Brown97404f22010-12-14 16:13:57 +00003211 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003212}
3213EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3214
3215/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003216 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3217 *
3218 * @kcontrol: mixer control
3219 * @uinfo: control element information
3220 *
3221 * Callback to provide information about a pin switch control.
3222 */
3223int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3224 struct snd_ctl_elem_info *uinfo)
3225{
3226 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3227 uinfo->count = 1;
3228 uinfo->value.integer.min = 0;
3229 uinfo->value.integer.max = 1;
3230
3231 return 0;
3232}
3233EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3234
3235/**
3236 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3237 *
3238 * @kcontrol: mixer control
3239 * @ucontrol: Value
3240 */
3241int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3242 struct snd_ctl_elem_value *ucontrol)
3243{
Mark Brown48a8c392012-02-14 17:11:15 -08003244 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003245 const char *pin = (const char *)kcontrol->private_value;
3246
Liam Girdwood3cd04342012-03-09 12:02:08 +00003247 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003248
3249 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003250 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003251
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003252 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003253
3254 return 0;
3255}
3256EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3257
3258/**
3259 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3260 *
3261 * @kcontrol: mixer control
3262 * @ucontrol: Value
3263 */
3264int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3265 struct snd_ctl_elem_value *ucontrol)
3266{
Mark Brown48a8c392012-02-14 17:11:15 -08003267 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003268 const char *pin = (const char *)kcontrol->private_value;
3269
Mark Brown8b37dbd2009-02-28 21:14:20 +00003270 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003271 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003272 else
Mark Brown48a8c392012-02-14 17:11:15 -08003273 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003274
Mark Brown48a8c392012-02-14 17:11:15 -08003275 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003276 return 0;
3277}
3278EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3279
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +01003280struct snd_soc_dapm_widget *
Mark Brown5ba06fc2012-02-16 11:07:13 -08003281snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003282 const struct snd_soc_dapm_widget *widget)
3283{
3284 struct snd_soc_dapm_widget *w;
3285
3286 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3287 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3288 if (!w)
3289 dev_err(dapm->dev,
3290 "ASoC: Failed to create DAPM control %s\n",
3291 widget->name);
3292
3293 mutex_unlock(&dapm->card->dapm_mutex);
3294 return w;
3295}
3296
3297struct snd_soc_dapm_widget *
3298snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003299 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003300{
3301 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003302 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00003303 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003304
3305 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003306 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003307
Mark Brown62ea8742012-01-21 21:14:48 +00003308 switch (w->id) {
3309 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003310 w->regulator = devm_regulator_get(dapm->dev, w->name);
3311 if (IS_ERR(w->regulator)) {
3312 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003313 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003314 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003315 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003316 }
Mark Brown8784c772013-01-10 19:33:47 +00003317
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003318 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003319 ret = regulator_allow_bypass(w->regulator, true);
3320 if (ret != 0)
3321 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003322 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003323 w->name, ret);
3324 }
Mark Brown62ea8742012-01-21 21:14:48 +00003325 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003326 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003327#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003328 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003329 if (IS_ERR(w->clk)) {
3330 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003331 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003332 w->name, ret);
3333 return NULL;
3334 }
Mark Brownec029952012-06-04 08:16:20 +01003335#else
3336 return NULL;
3337#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003338 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003339 default:
3340 break;
3341 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003342
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003343 prefix = soc_dapm_prefix(dapm);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003344 if (prefix)
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003345 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003346 else
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003347 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003348 if (w->name == NULL) {
3349 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003350 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003351 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003352
Mark Brown7ca3a182011-10-08 14:04:50 +01003353 switch (w->id) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003354 case snd_soc_dapm_mic:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003355 w->is_source = 1;
3356 w->power_check = dapm_generic_check_power;
3357 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003358 case snd_soc_dapm_input:
3359 if (!dapm->card->fully_routed)
3360 w->is_source = 1;
3361 w->power_check = dapm_generic_check_power;
3362 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003363 case snd_soc_dapm_spk:
3364 case snd_soc_dapm_hp:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003365 w->is_sink = 1;
3366 w->power_check = dapm_generic_check_power;
3367 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003368 case snd_soc_dapm_output:
3369 if (!dapm->card->fully_routed)
3370 w->is_sink = 1;
3371 w->power_check = dapm_generic_check_power;
3372 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003373 case snd_soc_dapm_vmid:
3374 case snd_soc_dapm_siggen:
3375 w->is_source = 1;
3376 w->power_check = dapm_always_on_check_power;
3377 break;
3378 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003379 case snd_soc_dapm_demux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003380 case snd_soc_dapm_switch:
3381 case snd_soc_dapm_mixer:
3382 case snd_soc_dapm_mixer_named_ctl:
Mark Brown63c69a62013-07-18 22:03:01 +01003383 case snd_soc_dapm_adc:
3384 case snd_soc_dapm_aif_out:
3385 case snd_soc_dapm_dac:
3386 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003387 case snd_soc_dapm_pga:
3388 case snd_soc_dapm_out_drv:
Mark Brown7ca3a182011-10-08 14:04:50 +01003389 case snd_soc_dapm_micbias:
Mark Brown7ca3a182011-10-08 14:04:50 +01003390 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003391 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02003392 case snd_soc_dapm_dai_out:
3393 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003394 w->power_check = dapm_generic_check_power;
3395 break;
3396 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003397 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003398 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003399 case snd_soc_dapm_kcontrol:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003400 w->is_supply = 1;
Mark Brown7ca3a182011-10-08 14:04:50 +01003401 w->power_check = dapm_supply_check_power;
3402 break;
3403 default:
3404 w->power_check = dapm_always_on_check_power;
3405 break;
3406 }
3407
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003408 w->dapm = dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003409 INIT_LIST_HEAD(&w->sources);
3410 INIT_LIST_HEAD(&w->sinks);
3411 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003412 INIT_LIST_HEAD(&w->dirty);
Lars-Peter Clausen92fa1242015-05-01 18:02:42 +02003413 list_add_tail(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003414
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003415 w->inputs = -1;
3416 w->outputs = -1;
3417
Richard Purdie2b97eab2006-10-06 18:32:18 +02003418 /* machine layer set ups unconnected pins and insertions */
3419 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003420 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003421}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003422
3423/**
Mark Brown4ba13272008-05-13 14:51:19 +02003424 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003425 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003426 * @widget: widget array
3427 * @num: number of widgets
3428 *
3429 * Creates new DAPM controls based upon the templates.
3430 *
3431 * Returns 0 for success else error.
3432 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003433int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003434 const struct snd_soc_dapm_widget *widget,
3435 int num)
3436{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003437 struct snd_soc_dapm_widget *w;
3438 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003439 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003440
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003441 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003442 for (i = 0; i < num; i++) {
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003443 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003444 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003445 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003446 "ASoC: Failed to create DAPM control %s\n",
3447 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003448 ret = -ENOMEM;
3449 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003450 }
Mark Brown4ba13272008-05-13 14:51:19 +02003451 widget++;
3452 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003453 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003454 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003455}
3456EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3457
Mark Brownc74184e2012-04-04 22:12:09 +01003458static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3459 struct snd_kcontrol *kcontrol, int event)
3460{
3461 struct snd_soc_dapm_path *source_p, *sink_p;
3462 struct snd_soc_dai *source, *sink;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003463 const struct snd_soc_pcm_stream *config = w->params + w->params_select;
Mark Brownc74184e2012-04-04 22:12:09 +01003464 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003465 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003466 u64 fmt;
3467 int ret;
3468
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003469 if (WARN_ON(!config) ||
3470 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3471 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003472
3473 /* We only support a single source and sink, pick the first */
3474 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3475 list_sink);
3476 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3477 list_source);
3478
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003479 if (WARN_ON(!source_p || !sink_p) ||
3480 WARN_ON(!sink_p->source || !source_p->sink) ||
3481 WARN_ON(!source_p->source || !sink_p->sink))
3482 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003483
3484 source = source_p->source->priv;
3485 sink = sink_p->sink->priv;
3486
3487 /* Be a little careful as we don't want to overflow the mask array */
3488 if (config->formats) {
3489 fmt = ffs(config->formats) - 1;
3490 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003491 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003492 config->formats);
3493 fmt = 0;
3494 }
3495
3496 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003497 params = kzalloc(sizeof(*params), GFP_KERNEL);
3498 if (!params) {
3499 ret = -ENOMEM;
3500 goto out;
3501 }
3502 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003503
Mark Brown9747cec2012-04-26 19:12:21 +01003504 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003505 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003506 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003507 config->rate_max;
3508
Mark Brown9747cec2012-04-26 19:12:21 +01003509 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003510 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003511 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003512 = config->channels_max;
3513
3514 memset(&substream, 0, sizeof(substream));
3515
3516 switch (event) {
3517 case SND_SOC_DAPM_PRE_PMU:
Benoit Cousson93e69582014-07-08 23:19:38 +02003518 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3519 ret = soc_dai_hw_params(&substream, params, source);
3520 if (ret < 0)
3521 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003522
Benoit Cousson93e69582014-07-08 23:19:38 +02003523 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3524 ret = soc_dai_hw_params(&substream, params, sink);
3525 if (ret < 0)
3526 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003527 break;
3528
3529 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003530 ret = snd_soc_dai_digital_mute(sink, 0,
3531 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003532 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003533 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003534 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003535 break;
3536
3537 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003538 ret = snd_soc_dai_digital_mute(sink, 1,
3539 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003540 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003541 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003542 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003543 break;
3544
3545 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003546 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003547 return -EINVAL;
3548 }
3549
Mark Brown9747cec2012-04-26 19:12:21 +01003550out:
3551 kfree(params);
3552 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003553}
3554
Nikesh Oswalc6615082015-02-02 17:06:44 +00003555static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3556 struct snd_ctl_elem_value *ucontrol)
3557{
3558 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3559
3560 ucontrol->value.integer.value[0] = w->params_select;
3561
3562 return 0;
3563}
3564
3565static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3566 struct snd_ctl_elem_value *ucontrol)
3567{
3568 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3569
3570 /* Can't change the config when widget is already powered */
3571 if (w->power)
3572 return -EBUSY;
3573
3574 if (ucontrol->value.integer.value[0] == w->params_select)
3575 return 0;
3576
3577 if (ucontrol->value.integer.value[0] >= w->num_params)
3578 return -EINVAL;
3579
3580 w->params_select = ucontrol->value.integer.value[0];
3581
3582 return 0;
3583}
3584
Mark Brownc74184e2012-04-04 22:12:09 +01003585int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3586 const struct snd_soc_pcm_stream *params,
Nikesh Oswalc6615082015-02-02 17:06:44 +00003587 unsigned int num_params,
Mark Brownc74184e2012-04-04 22:12:09 +01003588 struct snd_soc_dapm_widget *source,
3589 struct snd_soc_dapm_widget *sink)
3590{
Mark Brownc74184e2012-04-04 22:12:09 +01003591 struct snd_soc_dapm_widget template;
3592 struct snd_soc_dapm_widget *w;
Mark Brownc74184e2012-04-04 22:12:09 +01003593 char *link_name;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003594 int ret, count;
3595 unsigned long private_value;
3596 const char **w_param_text;
3597 struct soc_enum w_param_enum[] = {
3598 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3599 };
3600 struct snd_kcontrol_new kcontrol_dai_link[] = {
3601 SOC_ENUM_EXT(NULL, w_param_enum[0],
3602 snd_soc_dapm_dai_link_get,
3603 snd_soc_dapm_dai_link_put),
3604 };
3605 const struct snd_soc_pcm_stream *config = params;
Mark Brownc74184e2012-04-04 22:12:09 +01003606
Nikesh Oswalc6615082015-02-02 17:06:44 +00003607 w_param_text = devm_kcalloc(card->dev, num_params,
3608 sizeof(char *), GFP_KERNEL);
3609 if (!w_param_text)
Mark Brownc74184e2012-04-04 22:12:09 +01003610 return -ENOMEM;
Mark Brownc74184e2012-04-04 22:12:09 +01003611
Charles Keepax46172b62015-03-25 11:22:35 +00003612 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3613 source->name, sink->name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003614 if (!link_name) {
3615 ret = -ENOMEM;
3616 goto outfree_w_param;
3617 }
Mark Brownc74184e2012-04-04 22:12:09 +01003618
Nikesh Oswalc6615082015-02-02 17:06:44 +00003619 for (count = 0 ; count < num_params; count++) {
3620 if (!config->stream_name) {
3621 dev_warn(card->dapm.dev,
3622 "ASoC: anonymous config %d for dai link %s\n",
3623 count, link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003624 w_param_text[count] =
Charles Keepax46172b62015-03-25 11:22:35 +00003625 devm_kasprintf(card->dev, GFP_KERNEL,
3626 "Anonymous Configuration %d",
3627 count);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003628 if (!w_param_text[count]) {
3629 ret = -ENOMEM;
3630 goto outfree_link_name;
3631 }
Nikesh Oswalc6615082015-02-02 17:06:44 +00003632 } else {
3633 w_param_text[count] = devm_kmemdup(card->dev,
3634 config->stream_name,
3635 strlen(config->stream_name) + 1,
3636 GFP_KERNEL);
3637 if (!w_param_text[count]) {
3638 ret = -ENOMEM;
3639 goto outfree_link_name;
3640 }
3641 }
3642 config++;
3643 }
3644 w_param_enum[0].items = num_params;
3645 w_param_enum[0].texts = w_param_text;
Mark Brownc74184e2012-04-04 22:12:09 +01003646
3647 memset(&template, 0, sizeof(template));
3648 template.reg = SND_SOC_NOPM;
3649 template.id = snd_soc_dapm_dai_link;
3650 template.name = link_name;
3651 template.event = snd_soc_dai_link_event;
3652 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3653 SND_SOC_DAPM_PRE_PMD;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003654 template.num_kcontrols = 1;
3655 /* duplicate w_param_enum on heap so that memory persists */
3656 private_value =
3657 (unsigned long) devm_kmemdup(card->dev,
3658 (void *)(kcontrol_dai_link[0].private_value),
3659 sizeof(struct soc_enum), GFP_KERNEL);
3660 if (!private_value) {
3661 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3662 link_name);
3663 ret = -ENOMEM;
3664 goto outfree_link_name;
3665 }
3666 kcontrol_dai_link[0].private_value = private_value;
3667 /* duplicate kcontrol_dai_link on heap so that memory persists */
3668 template.kcontrol_news =
3669 devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3670 sizeof(struct snd_kcontrol_new),
3671 GFP_KERNEL);
3672 if (!template.kcontrol_news) {
3673 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3674 link_name);
3675 ret = -ENOMEM;
3676 goto outfree_private_value;
3677 }
Mark Brownc74184e2012-04-04 22:12:09 +01003678
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003679 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003680
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003681 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
Mark Brownc74184e2012-04-04 22:12:09 +01003682 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003683 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003684 link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003685 ret = -ENOMEM;
3686 goto outfree_kcontrol_news;
Mark Brownc74184e2012-04-04 22:12:09 +01003687 }
3688
3689 w->params = params;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003690 w->num_params = num_params;
Mark Brownc74184e2012-04-04 22:12:09 +01003691
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003692 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3693 if (ret)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003694 goto outfree_w;
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003695 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003696
3697outfree_w:
3698 devm_kfree(card->dev, w);
3699outfree_kcontrol_news:
3700 devm_kfree(card->dev, (void *)template.kcontrol_news);
3701outfree_private_value:
3702 devm_kfree(card->dev, (void *)private_value);
3703outfree_link_name:
3704 devm_kfree(card->dev, link_name);
3705outfree_w_param:
3706 for (count = 0 ; count < num_params; count++)
3707 devm_kfree(card->dev, (void *)w_param_text[count]);
3708 devm_kfree(card->dev, w_param_text);
3709
3710 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003711}
3712
Mark Brown888df392012-02-16 19:37:51 -08003713int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3714 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003715{
Mark Brown888df392012-02-16 19:37:51 -08003716 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003717 struct snd_soc_dapm_widget *w;
3718
Mark Brown888df392012-02-16 19:37:51 -08003719 WARN_ON(dapm->dev != dai->dev);
3720
3721 memset(&template, 0, sizeof(template));
3722 template.reg = SND_SOC_NOPM;
3723
3724 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003725 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003726 template.name = dai->driver->playback.stream_name;
3727 template.sname = dai->driver->playback.stream_name;
3728
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003729 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003730 template.name);
3731
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003732 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Mark Brown888df392012-02-16 19:37:51 -08003733 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003734 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003735 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003736 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003737 }
3738
3739 w->priv = dai;
3740 dai->playback_widget = w;
3741 }
3742
3743 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003744 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003745 template.name = dai->driver->capture.stream_name;
3746 template.sname = dai->driver->capture.stream_name;
3747
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003748 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003749 template.name);
3750
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003751 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Mark Brown888df392012-02-16 19:37:51 -08003752 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003753 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003754 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003755 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003756 }
3757
3758 w->priv = dai;
3759 dai->capture_widget = w;
3760 }
3761
3762 return 0;
3763}
3764
3765int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3766{
3767 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003768 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08003769 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003770
3771 /* For each DAI widget... */
3772 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003773 switch (dai_w->id) {
3774 case snd_soc_dapm_dai_in:
3775 case snd_soc_dapm_dai_out:
3776 break;
3777 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003778 continue;
Mark Brown46162742013-06-05 19:36:11 +01003779 }
Mark Brown888df392012-02-16 19:37:51 -08003780
3781 dai = dai_w->priv;
3782
3783 /* ...find all widgets with the same stream and link them */
3784 list_for_each_entry(w, &card->widgets, list) {
3785 if (w->dapm != dai_w->dapm)
3786 continue;
3787
Mark Brown46162742013-06-05 19:36:11 +01003788 switch (w->id) {
3789 case snd_soc_dapm_dai_in:
3790 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003791 continue;
Mark Brown46162742013-06-05 19:36:11 +01003792 default:
3793 break;
3794 }
Mark Brown888df392012-02-16 19:37:51 -08003795
Lars-Peter Clausena798c242015-07-21 11:51:35 +02003796 if (!w->sname || !strstr(w->sname, dai_w->sname))
Mark Brown888df392012-02-16 19:37:51 -08003797 continue;
3798
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003799 if (dai_w->id == snd_soc_dapm_dai_in) {
3800 src = dai_w;
3801 sink = w;
3802 } else {
3803 src = w;
3804 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08003805 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003806 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3807 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003808 }
3809 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003810
Mark Brown888df392012-02-16 19:37:51 -08003811 return 0;
3812}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003813
Benoit Cousson44ba2642014-07-08 23:19:36 +02003814static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3815 struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003816{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003817 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003818 struct snd_soc_dapm_widget *sink, *source;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003819 int i;
3820
Benoit Cousson44ba2642014-07-08 23:19:36 +02003821 for (i = 0; i < rtd->num_codecs; i++) {
3822 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003823
3824 /* there is no point in connecting BE DAI links with dummies */
3825 if (snd_soc_dai_is_dummy(codec_dai) ||
3826 snd_soc_dai_is_dummy(cpu_dai))
3827 continue;
3828
3829 /* connect BE DAI playback if widgets are valid */
3830 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003831 source = cpu_dai->playback_widget;
3832 sink = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003833 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003834 cpu_dai->component->name, source->name,
3835 codec_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003836
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003837 snd_soc_dapm_add_path(&card->dapm, source, sink,
3838 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003839 }
3840
3841 /* connect BE DAI capture if widgets are valid */
3842 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003843 source = codec_dai->capture_widget;
3844 sink = cpu_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003845 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003846 codec_dai->component->name, source->name,
3847 cpu_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003848
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003849 snd_soc_dapm_add_path(&card->dapm, source, sink,
3850 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003851 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003852 }
3853}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003854
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003855static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3856 int event)
3857{
3858 struct snd_soc_dapm_widget *w;
3859
3860 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3861 w = dai->playback_widget;
3862 else
3863 w = dai->capture_widget;
3864
3865 if (w) {
3866 dapm_mark_dirty(w, "stream event");
3867
3868 switch (event) {
3869 case SND_SOC_DAPM_STREAM_START:
3870 w->active = 1;
3871 break;
3872 case SND_SOC_DAPM_STREAM_STOP:
3873 w->active = 0;
3874 break;
3875 case SND_SOC_DAPM_STREAM_SUSPEND:
3876 case SND_SOC_DAPM_STREAM_RESUME:
3877 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3878 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3879 break;
3880 }
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003881
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003882 if (w->id == snd_soc_dapm_dai_in) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003883 w->is_source = w->active;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003884 dapm_widget_invalidate_input_paths(w);
3885 } else {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003886 w->is_sink = w->active;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003887 dapm_widget_invalidate_output_paths(w);
3888 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003889 }
3890}
3891
Benoit Cousson44ba2642014-07-08 23:19:36 +02003892void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3893{
3894 struct snd_soc_pcm_runtime *rtd = card->rtd;
3895 int i;
3896
3897 /* for each BE DAI link... */
3898 for (i = 0; i < card->num_rtd; i++) {
3899 rtd = &card->rtd[i];
3900
3901 /*
3902 * dynamic FE links have no fixed DAI mapping.
3903 * CODEC<->CODEC links have no direct connection.
3904 */
3905 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3906 continue;
3907
3908 dapm_connect_dai_link_widgets(card, rtd);
3909 }
3910}
3911
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003912static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3913 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003914{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003915 int i;
3916
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003917 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
Benoit Cousson44ba2642014-07-08 23:19:36 +02003918 for (i = 0; i < rtd->num_codecs; i++)
3919 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003920
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003921 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003922}
3923
3924/**
3925 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3926 * @rtd: PCM runtime data
3927 * @stream: stream name
3928 * @event: stream event
3929 *
3930 * Sends a stream event to the dapm core. The core then makes any
3931 * necessary widget power changes.
3932 *
3933 * Returns 0 for success else error.
3934 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003935void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3936 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003937{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003938 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003939
Liam Girdwood3cd04342012-03-09 12:02:08 +00003940 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003941 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003942 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003943}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003944
3945/**
Charles Keepax11391102014-02-18 15:22:14 +00003946 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3947 * @dapm: DAPM context
3948 * @pin: pin name
3949 *
3950 * Enables input/output pin and its parents or children widgets iff there is
3951 * a valid audio route and active audio stream.
3952 *
3953 * Requires external locking.
3954 *
3955 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3956 * do any widget power switching.
3957 */
3958int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3959 const char *pin)
3960{
3961 return snd_soc_dapm_set_pin(dapm, pin, 1);
3962}
3963EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3964
3965/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003966 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003967 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003968 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003969 *
Mark Brown74b8f952009-06-06 11:26:15 +01003970 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003971 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003972 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003973 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3974 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003975 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003976int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003977{
Charles Keepax11391102014-02-18 15:22:14 +00003978 int ret;
3979
3980 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3981
3982 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3983
3984 mutex_unlock(&dapm->card->dapm_mutex);
3985
3986 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003987}
Liam Girdwooda5302182008-07-07 13:35:17 +01003988EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003989
3990/**
Charles Keepax11391102014-02-18 15:22:14 +00003991 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3992 * @dapm: DAPM context
3993 * @pin: pin name
3994 *
3995 * Enables input/output pin regardless of any other state. This is
3996 * intended for use with microphone bias supplies used in microphone
3997 * jack detection.
3998 *
3999 * Requires external locking.
4000 *
4001 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4002 * do any widget power switching.
4003 */
4004int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4005 const char *pin)
4006{
4007 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4008
4009 if (!w) {
4010 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4011 return -EINVAL;
4012 }
4013
4014 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02004015 if (!w->connected) {
4016 /*
4017 * w->force does not affect the number of input or output paths,
4018 * so we only have to recheck if w->connected is changed
4019 */
4020 dapm_widget_invalidate_input_paths(w);
4021 dapm_widget_invalidate_output_paths(w);
4022 w->connected = 1;
4023 }
Charles Keepax11391102014-02-18 15:22:14 +00004024 w->force = 1;
4025 dapm_mark_dirty(w, "force enable");
4026
4027 return 0;
4028}
4029EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4030
4031/**
Mark Brownda341832010-03-15 19:23:37 +00004032 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004033 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00004034 * @pin: pin name
4035 *
4036 * Enables input/output pin regardless of any other state. This is
4037 * intended for use with microphone bias supplies used in microphone
4038 * jack detection.
4039 *
4040 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4041 * do any widget power switching.
4042 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004043int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4044 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00004045{
Charles Keepax11391102014-02-18 15:22:14 +00004046 int ret;
Mark Brownda341832010-03-15 19:23:37 +00004047
Charles Keepax11391102014-02-18 15:22:14 +00004048 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00004049
Charles Keepax11391102014-02-18 15:22:14 +00004050 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09004051
Charles Keepax11391102014-02-18 15:22:14 +00004052 mutex_unlock(&dapm->card->dapm_mutex);
4053
4054 return ret;
Mark Brownda341832010-03-15 19:23:37 +00004055}
4056EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4057
4058/**
Charles Keepax11391102014-02-18 15:22:14 +00004059 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4060 * @dapm: DAPM context
4061 * @pin: pin name
4062 *
4063 * Disables input/output pin and its parents or children widgets.
4064 *
4065 * Requires external locking.
4066 *
4067 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4068 * do any widget power switching.
4069 */
4070int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4071 const char *pin)
4072{
4073 return snd_soc_dapm_set_pin(dapm, pin, 0);
4074}
4075EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4076
4077/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004078 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004079 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004080 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004081 *
Mark Brown74b8f952009-06-06 11:26:15 +01004082 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00004083 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004084 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4085 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004086 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004087int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4088 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01004089{
Charles Keepax11391102014-02-18 15:22:14 +00004090 int ret;
4091
4092 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4093
4094 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4095
4096 mutex_unlock(&dapm->card->dapm_mutex);
4097
4098 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01004099}
4100EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4101
4102/**
Charles Keepax11391102014-02-18 15:22:14 +00004103 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4104 * @dapm: DAPM context
4105 * @pin: pin name
4106 *
4107 * Marks the specified pin as being not connected, disabling it along
4108 * any parent or child widgets. At present this is identical to
4109 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4110 * additional things such as disabling controls which only affect
4111 * paths through the pin.
4112 *
4113 * Requires external locking.
4114 *
4115 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4116 * do any widget power switching.
4117 */
4118int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4119 const char *pin)
4120{
4121 return snd_soc_dapm_set_pin(dapm, pin, 0);
4122}
4123EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4124
4125/**
Mark Brown5817b522008-09-24 11:23:11 +01004126 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004127 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01004128 * @pin: pin name
4129 *
4130 * Marks the specified pin as being not connected, disabling it along
4131 * any parent or child widgets. At present this is identical to
4132 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4133 * additional things such as disabling controls which only affect
4134 * paths through the pin.
4135 *
4136 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4137 * do any widget power switching.
4138 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004139int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01004140{
Charles Keepax11391102014-02-18 15:22:14 +00004141 int ret;
4142
4143 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4144
4145 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4146
4147 mutex_unlock(&dapm->card->dapm_mutex);
4148
4149 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01004150}
4151EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4152
4153/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004154 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004155 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004156 * @pin: audio signal pin endpoint (or start point)
4157 *
4158 * Get audio pin status - connected or disconnected.
4159 *
4160 * Returns 1 for connected otherwise 0.
4161 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004162int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4163 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004164{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004165 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004166
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004167 if (w)
4168 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06004169
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004170 return 0;
4171}
Liam Girdwooda5302182008-07-07 13:35:17 +01004172EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004173
4174/**
Mark Brown1547aba2010-05-07 21:11:40 +01004175 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004176 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01004177 * @pin: audio signal pin endpoint (or start point)
4178 *
4179 * Mark the given endpoint or pin as ignoring suspend. When the
4180 * system is disabled a path between two endpoints flagged as ignoring
4181 * suspend will not be disabled. The path must already be enabled via
4182 * normal means at suspend time, it will not be turned on if it was not
4183 * already enabled.
4184 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004185int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4186 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01004187{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004188 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01004189
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004190 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004191 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004192 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01004193 }
4194
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004195 w->ignore_suspend = 1;
4196
4197 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01004198}
4199EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4200
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02004201/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02004202 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03004203 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02004204 *
4205 * Free all dapm widgets and resources.
4206 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004207void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004208{
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02004209 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004210 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02004211 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004212}
4213EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4214
Xiang Xiao57996352014-03-02 00:04:02 +08004215static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01004216{
Liam Girdwood01005a72012-07-06 16:57:05 +01004217 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01004218 struct snd_soc_dapm_widget *w;
4219 LIST_HEAD(down_list);
4220 int powerdown = 0;
4221
Liam Girdwood01005a72012-07-06 16:57:05 +01004222 mutex_lock(&card->dapm_mutex);
4223
Jarkko Nikula97c866d2010-12-14 12:18:31 +02004224 list_for_each_entry(w, &dapm->card->widgets, list) {
4225 if (w->dapm != dapm)
4226 continue;
Mark Brown51737472009-06-22 13:16:51 +01004227 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004228 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01004229 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004230 powerdown = 1;
4231 }
4232 }
4233
4234 /* If there were no widgets to power down we're already in
4235 * standby.
4236 */
4237 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004238 if (dapm->bias_level == SND_SOC_BIAS_ON)
4239 snd_soc_dapm_set_bias_level(dapm,
4240 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004241 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004242 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4243 snd_soc_dapm_set_bias_level(dapm,
4244 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004245 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004246
4247 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004248}
Mark Brown51737472009-06-22 13:16:51 +01004249
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004250/*
4251 * snd_soc_dapm_shutdown - callback for system shutdown
4252 */
4253void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4254{
Xiang Xiao57996352014-03-02 00:04:02 +08004255 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004256
Xiang Xiao57996352014-03-02 00:04:02 +08004257 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08004258 if (dapm != &card->dapm) {
4259 soc_dapm_shutdown_dapm(dapm);
4260 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4261 snd_soc_dapm_set_bias_level(dapm,
4262 SND_SOC_BIAS_OFF);
4263 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004264 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08004265
4266 soc_dapm_shutdown_dapm(&card->dapm);
4267 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4268 snd_soc_dapm_set_bias_level(&card->dapm,
4269 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01004270}
4271
Richard Purdie2b97eab2006-10-06 18:32:18 +02004272/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004273MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004274MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4275MODULE_LICENSE("GPL");