blob: 1779430013eae524ddd83a76079ba98211926706 [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);
361 if (!data->widget) {
Charles Keepax773da9b2015-05-01 12:37:25 +0100362 ret = -ENOMEM;
363 goto err_name;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200364 }
365 }
366 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200367 case snd_soc_dapm_demux:
Charles Keepax561ed682015-05-01 12:37:26 +0100368 case snd_soc_dapm_mux:
369 e = (struct soc_enum *)kcontrol->private_value;
370
371 if (e->autodisable) {
372 struct snd_soc_dapm_widget template;
373
374 name = kasprintf(GFP_KERNEL, "%s %s", kcontrol->id.name,
375 "Autodisable");
376 if (!name) {
377 ret = -ENOMEM;
378 goto err_data;
379 }
380
381 memset(&template, 0, sizeof(template));
382 template.reg = e->reg;
383 template.mask = e->mask << e->shift_l;
384 template.shift = e->shift_l;
385 template.off_val = snd_soc_enum_item_to_val(e, 0);
386 template.on_val = template.off_val;
387 template.id = snd_soc_dapm_kcontrol;
388 template.name = name;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200389
390 data->value = template.on_val;
391
Charles Keepaxffacb482015-06-26 10:39:43 +0100392 data->widget = snd_soc_dapm_new_control_unlocked(
393 widget->dapm, &template);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200394 if (!data->widget) {
Charles Keepax561ed682015-05-01 12:37:26 +0100395 ret = -ENOMEM;
396 goto err_name;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200397 }
Charles Keepax561ed682015-05-01 12:37:26 +0100398
399 snd_soc_dapm_add_path(widget->dapm, data->widget,
400 widget, NULL, NULL);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200401 }
402 break;
403 default:
404 break;
405 }
406
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200407 kcontrol->private_data = data;
408
409 return 0;
Charles Keepax773da9b2015-05-01 12:37:25 +0100410
411err_name:
412 kfree(name);
413err_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);
Charles Keepax773da9b2015-05-01 12:37:25 +0100421 if (data->widget)
422 kfree(data->widget->name);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200423 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200424 kfree(data);
425}
426
427static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
428 const struct snd_kcontrol *kcontrol)
429{
430 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
431
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200432 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200433}
434
435static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
436 struct snd_soc_dapm_widget *widget)
437{
438 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200439 struct snd_soc_dapm_widget_list *new_wlist;
440 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200441
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200442 if (data->wlist)
443 n = data->wlist->num_widgets + 1;
444 else
445 n = 1;
446
447 new_wlist = krealloc(data->wlist,
448 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
449 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200450 return -ENOMEM;
451
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200452 new_wlist->widgets[n - 1] = widget;
453 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200454
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200455 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200456
457 return 0;
458}
459
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200460static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
461 struct snd_soc_dapm_path *path)
462{
463 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
464
465 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200466}
467
468static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
469{
470 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
471
472 if (!data->widget)
473 return true;
474
475 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200476}
477
478static struct list_head *dapm_kcontrol_get_path_list(
479 const struct snd_kcontrol *kcontrol)
480{
481 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
482
483 return &data->paths;
484}
485
486#define dapm_kcontrol_for_each_path(path, kcontrol) \
487 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
488 list_kcontrol)
489
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530490unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200491{
492 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
493
494 return data->value;
495}
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530496EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200497
498static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
499 unsigned int value)
500{
501 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
502
503 if (data->value == value)
504 return false;
505
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200506 if (data->widget)
507 data->widget->on_val = value;
508
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200509 data->value = value;
510
511 return true;
512}
513
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200514/**
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200515 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
516 * kcontrol
517 * @kcontrol: The kcontrol
518 *
519 * Note: This function must only be used on kcontrols that are known to have
520 * been registered for a CODEC. Otherwise the behaviour is undefined.
521 */
522struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
523 struct snd_kcontrol *kcontrol)
524{
525 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
526}
527EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
528
Liam Girdwood6c120e12012-02-15 15:15:34 +0000529static void dapm_reset(struct snd_soc_card *card)
530{
531 struct snd_soc_dapm_widget *w;
532
Mark Brownf9fa2b12014-03-06 16:49:11 +0800533 lockdep_assert_held(&card->dapm_mutex);
534
Liam Girdwood6c120e12012-02-15 15:15:34 +0000535 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
536
537 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200538 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000539 w->power_checked = false;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000540 }
541}
542
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200543static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
544{
545 if (!dapm->component)
546 return NULL;
547 return dapm->component->name_prefix;
548}
549
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200550static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800551 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100552{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200553 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200554 return -EIO;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200555 return snd_soc_component_read(dapm->component, reg, value);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100556}
557
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200558static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
Bard Liao34775012014-04-17 20:12:56 +0800559 int reg, unsigned int mask, unsigned int value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100560{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200561 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200562 return -EIO;
Mark Brownfcf6c5e2014-12-15 13:08:48 +0000563 return snd_soc_component_update_bits(dapm->component, reg,
564 mask, value);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000565}
566
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200567static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
568 int reg, unsigned int mask, unsigned int value)
569{
570 if (!dapm->component)
571 return -EIO;
572 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
573}
574
Mark Browneb270e92013-10-09 13:52:52 +0100575static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
576{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200577 if (dapm->component)
578 snd_soc_component_async_complete(dapm->component);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100579}
580
Charles Keepax45a110a2015-05-11 13:50:30 +0100581static struct snd_soc_dapm_widget *
582dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
583{
584 struct snd_soc_dapm_widget *w = wcache->widget;
585 struct list_head *wlist;
586 const int depth = 2;
587 int i = 0;
588
589 if (w) {
590 wlist = &w->dapm->card->widgets;
591
592 list_for_each_entry_from(w, wlist, list) {
593 if (!strcmp(name, w->name))
594 return w;
595
596 if (++i == depth)
597 break;
598 }
599 }
600
601 return NULL;
602}
603
604static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
605 struct snd_soc_dapm_widget *w)
606{
607 wcache->widget = w;
608}
609
Mark Brown452c5ea2009-05-17 21:41:23 +0100610/**
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200611 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
612 * @dapm: The DAPM context for which to set the level
613 * @level: The level to set
614 *
615 * Forces the DAPM bias level to a specific state. It will call the bias level
616 * callback of DAPM context with the specified level. This will even happen if
617 * the context is already at the same level. Furthermore it will not go through
618 * the normal bias level sequencing, meaning any intermediate states between the
619 * current and the target state will not be entered.
620 *
621 * Note that the change in bias level is only temporary and the next time
622 * snd_soc_dapm_sync() is called the state will be set to the level as
623 * determined by the DAPM core. The function is mainly intended to be used to
624 * used during probe or resume from suspend to power up the device so
625 * initialization can be done, before the DAPM core takes over.
626 */
627int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
628 enum snd_soc_bias_level level)
629{
630 int ret = 0;
631
632 if (dapm->set_bias_level)
633 ret = dapm->set_bias_level(dapm, level);
634
Lars-Peter Clausenf4bf8d72015-04-27 22:13:25 +0200635 if (ret == 0)
636 dapm->bias_level = level;
637
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200638 return ret;
639}
640EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
641
Mark Brown452c5ea2009-05-17 21:41:23 +0100642/**
643 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800644 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100645 * @level: level to configure
646 *
647 * Configure the bias (power) levels for the SoC audio device.
648 *
649 * Returns 0 for success else error.
650 */
Mark Browned5a4c42011-02-18 11:12:42 -0800651static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200652 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100653{
Mark Browned5a4c42011-02-18 11:12:42 -0800654 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100655 int ret = 0;
656
Mark Brown84e90932010-11-04 00:07:02 -0400657 trace_snd_soc_bias_level_start(card, level);
658
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000659 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100660 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100661 if (ret != 0)
662 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100663
Lars-Peter Clausenfa880772015-04-27 22:13:23 +0200664 if (!card || dapm != &card->dapm)
665 ret = snd_soc_dapm_force_bias_level(dapm, level);
Liam Girdwood41231282012-07-06 16:56:16 +0100666
Mark Brown171ec6b2011-06-06 18:15:19 +0100667 if (ret != 0)
668 goto out;
669
670 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100671 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100672out:
Mark Brown84e90932010-11-04 00:07:02 -0400673 trace_snd_soc_bias_level_done(card, level);
674
Mark Brown452c5ea2009-05-17 21:41:23 +0100675 return ret;
676}
677
Mark Brown74b8f952009-06-06 11:26:15 +0100678/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200679static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200680 struct snd_soc_dapm_path *path, const char *control_name,
681 struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200682{
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200683 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200684 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100685 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200686 int i;
687
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100688 if (e->reg != SND_SOC_NOPM) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200689 soc_dapm_read(dapm, e->reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100690 val = (val >> e->shift_l) & e->mask;
691 item = snd_soc_enum_val_to_item(e, val);
692 } else {
693 /* since a virtual mux has no backing registers to
694 * decide which path to connect, it will try to match
695 * with the first enumeration. This is to ensure
696 * that the default mux choice (the first) will be
697 * correctly powered up during initialization.
698 */
699 item = 0;
700 }
701
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100702 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200703 if (!(strcmp(control_name, e->texts[i]))) {
Rasmus Villemoes98ad73c2014-10-21 17:01:15 +0200704 path->name = e->texts[i];
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100705 if (i == item)
706 path->connect = 1;
707 else
708 path->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200709 return 0;
710 }
711 }
712
713 return -ENODEV;
714}
715
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100716/* set up initial codec paths */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200717static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i)
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100718{
719 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200720 p->sink->kcontrol_news[i].private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100721 unsigned int reg = mc->reg;
722 unsigned int shift = mc->shift;
723 unsigned int max = mc->max;
724 unsigned int mask = (1 << fls(max)) - 1;
725 unsigned int invert = mc->invert;
726 unsigned int val;
727
728 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200729 soc_dapm_read(p->sink->dapm, reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100730 val = (val >> shift) & mask;
731 if (invert)
732 val = max - val;
733 p->connect = !!val;
734 } else {
735 p->connect = 0;
736 }
737}
738
Mark Brown74b8f952009-06-06 11:26:15 +0100739/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200740static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200741 struct snd_soc_dapm_path *path, const char *control_name)
742{
743 int i;
744
745 /* search for mixer kcontrol */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200746 for (i = 0; i < path->sink->num_kcontrols; i++) {
747 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
748 path->name = path->sink->kcontrol_news[i].name;
749 dapm_set_mixer_path_status(path, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200750 return 0;
751 }
752 }
753 return -ENODEV;
754}
755
Stephen Warrenaf468002011-04-28 17:38:01 -0600756static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600757 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600758 const struct snd_kcontrol_new *kcontrol_new,
759 struct snd_kcontrol **kcontrol)
760{
761 struct snd_soc_dapm_widget *w;
762 int i;
763
764 *kcontrol = NULL;
765
766 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600767 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
768 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600769 for (i = 0; i < w->num_kcontrols; i++) {
770 if (&w->kcontrol_news[i] == kcontrol_new) {
771 if (w->kcontrols)
772 *kcontrol = w->kcontrols[i];
773 return 1;
774 }
775 }
776 }
777
778 return 0;
779}
780
Stephen Warren85762e72013-03-29 15:40:10 -0600781/*
782 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
783 * create it. Either way, add the widget into the control's widget list
784 */
785static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100786 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200787{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200788 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000789 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000790 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600791 size_t prefix_len;
792 int shared;
793 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600794 bool wname_in_long_name, kcname_in_long_name;
Daniel Macke5092c92014-10-07 13:41:24 +0200795 char *long_name = NULL;
Stephen Warren85762e72013-03-29 15:40:10 -0600796 const char *name;
Daniel Macke5092c92014-10-07 13:41:24 +0200797 int ret = 0;
Mark Brownefb7ac32011-03-08 17:23:24 +0000798
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200799 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000800 if (prefix)
801 prefix_len = strlen(prefix) + 1;
802 else
803 prefix_len = 0;
804
Stephen Warren85762e72013-03-29 15:40:10 -0600805 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
806 &kcontrol);
807
Stephen Warren85762e72013-03-29 15:40:10 -0600808 if (!kcontrol) {
809 if (shared) {
810 wname_in_long_name = false;
811 kcname_in_long_name = true;
812 } else {
813 switch (w->id) {
814 case snd_soc_dapm_switch:
815 case snd_soc_dapm_mixer:
816 wname_in_long_name = true;
817 kcname_in_long_name = true;
818 break;
819 case snd_soc_dapm_mixer_named_ctl:
820 wname_in_long_name = false;
821 kcname_in_long_name = true;
822 break;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200823 case snd_soc_dapm_demux:
Stephen Warren85762e72013-03-29 15:40:10 -0600824 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600825 wname_in_long_name = true;
826 kcname_in_long_name = false;
827 break;
828 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600829 return -EINVAL;
830 }
831 }
832
833 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600834 /*
835 * The control will get a prefix from the control
836 * creation process but we're also using the same
837 * prefix for widgets so cut the prefix off the
838 * front of the widget name.
839 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200840 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600841 w->name + prefix_len,
842 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200843 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200844 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600845
846 name = long_name;
847 } else if (wname_in_long_name) {
848 long_name = NULL;
849 name = w->name + prefix_len;
850 } else {
851 long_name = NULL;
852 name = w->kcontrol_news[kci].name;
853 }
854
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200855 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600856 prefix);
Daniel Macke5092c92014-10-07 13:41:24 +0200857 if (!kcontrol) {
858 ret = -ENOMEM;
859 goto exit_free;
860 }
861
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200862 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200863
864 ret = dapm_kcontrol_data_alloc(w, kcontrol);
865 if (ret) {
866 snd_ctl_free_one(kcontrol);
Daniel Macke5092c92014-10-07 13:41:24 +0200867 goto exit_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200868 }
869
Stephen Warren85762e72013-03-29 15:40:10 -0600870 ret = snd_ctl_add(card, kcontrol);
871 if (ret < 0) {
872 dev_err(dapm->dev,
873 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
874 w->name, name, ret);
Daniel Macke5092c92014-10-07 13:41:24 +0200875 goto exit_free;
Stephen Warren85762e72013-03-29 15:40:10 -0600876 }
Stephen Warren85762e72013-03-29 15:40:10 -0600877 }
878
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200879 ret = dapm_kcontrol_add_widget(kcontrol, w);
Daniel Macke5092c92014-10-07 13:41:24 +0200880 if (ret == 0)
881 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200882
Daniel Macke5092c92014-10-07 13:41:24 +0200883exit_free:
884 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600885
Daniel Macke5092c92014-10-07 13:41:24 +0200886 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600887}
888
889/* create new dapm mixer control */
890static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
891{
892 int i, ret;
893 struct snd_soc_dapm_path *path;
Charles Keepax561ed682015-05-01 12:37:26 +0100894 struct dapm_kcontrol_data *data;
Stephen Warren85762e72013-03-29 15:40:10 -0600895
Richard Purdie2b97eab2006-10-06 18:32:18 +0200896 /* add kcontrol */
897 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200898 /* match name */
899 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200900 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600901 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200902 continue;
903
Charles Keepax561ed682015-05-01 12:37:26 +0100904 if (!w->kcontrols[i]) {
905 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
906 if (ret < 0)
907 return ret;
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200908 }
909
Mark Brown946d92a2013-08-12 23:28:42 +0100910 dapm_kcontrol_add_path(w->kcontrols[i], path);
Charles Keepax561ed682015-05-01 12:37:26 +0100911
912 data = snd_kcontrol_chip(w->kcontrols[i]);
913 if (data->widget)
914 snd_soc_dapm_add_path(data->widget->dapm,
915 data->widget,
916 path->source,
917 NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200918 }
919 }
Stephen Warren85762e72013-03-29 15:40:10 -0600920
921 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200922}
923
924/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200925static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200926{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200927 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600928 struct snd_soc_dapm_path *path;
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200929 struct list_head *paths;
930 const char *type;
Stephen Warrenaf468002011-04-28 17:38:01 -0600931 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200932
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200933 switch (w->id) {
934 case snd_soc_dapm_mux:
935 paths = &w->sources;
936 type = "mux";
937 break;
938 case snd_soc_dapm_demux:
939 paths = &w->sinks;
940 type = "demux";
941 break;
942 default:
943 return -EINVAL;
944 }
945
Stephen Warrenaf468002011-04-28 17:38:01 -0600946 if (w->num_kcontrols != 1) {
947 dev_err(dapm->dev,
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200948 "ASoC: %s %s has incorrect number of controls\n", type,
Stephen Warrenaf468002011-04-28 17:38:01 -0600949 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200950 return -EINVAL;
951 }
952
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200953 if (list_empty(paths)) {
954 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
Stephen Warren85762e72013-03-29 15:40:10 -0600955 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600956 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200957
Mark Brown946d92a2013-08-12 23:28:42 +0100958 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600959 if (ret < 0)
960 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600961
Lars-Peter Clausend714f972015-05-01 18:02:43 +0200962 if (w->id == snd_soc_dapm_mux) {
963 list_for_each_entry(path, &w->sources, list_sink) {
964 if (path->name)
965 dapm_kcontrol_add_path(w->kcontrols[0], path);
966 }
967 } else {
968 list_for_each_entry(path, &w->sinks, list_source) {
969 if (path->name)
970 dapm_kcontrol_add_path(w->kcontrols[0], path);
971 }
Lars-Peter Clausen98407ef2014-10-25 17:41:57 +0200972 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200973
Stephen Warrenaf468002011-04-28 17:38:01 -0600974 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200975}
976
977/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200978static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200979{
Mark Browna6c65732010-03-03 17:45:21 +0000980 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200981 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000982 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200983
Mark Browna6c65732010-03-03 17:45:21 +0000984 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200985}
986
Nikesh Oswalc6615082015-02-02 17:06:44 +0000987/* create new dapm dai link control */
988static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
989{
990 int i, ret;
991 struct snd_kcontrol *kcontrol;
992 struct snd_soc_dapm_context *dapm = w->dapm;
993 struct snd_card *card = dapm->card->snd_card;
994
995 /* create control for links with > 1 config */
996 if (w->num_params <= 1)
997 return 0;
998
999 /* add kcontrol */
1000 for (i = 0; i < w->num_kcontrols; i++) {
1001 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1002 w->name, NULL);
1003 ret = snd_ctl_add(card, kcontrol);
1004 if (ret < 0) {
1005 dev_err(dapm->dev,
1006 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1007 w->name, w->kcontrol_news[i].name, ret);
1008 return ret;
1009 }
1010 kcontrol->private_data = w;
1011 w->kcontrols[i] = kcontrol;
1012 }
1013
1014 return 0;
1015}
1016
Mark Brown99497882010-05-07 20:24:05 +01001017/* We implement power down on suspend by checking the power state of
1018 * the ALSA card - when we are suspending the ALSA state for the card
1019 * is set to D3.
1020 */
1021static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1022{
Mark Brown12ea2c72011-03-02 18:17:32 +00001023 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +01001024
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001025 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +01001026 case SNDRV_CTL_POWER_D3hot:
1027 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +01001028 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001029 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001030 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +01001031 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +01001032 default:
1033 return 1;
1034 }
1035}
1036
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001037/* add widget to list if it's not already in the list */
1038static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
1039 struct snd_soc_dapm_widget *w)
1040{
1041 struct snd_soc_dapm_widget_list *wlist;
1042 int wlistsize, wlistentries, i;
1043
1044 if (*list == NULL)
1045 return -EINVAL;
1046
1047 wlist = *list;
1048
1049 /* is this widget already in the list */
1050 for (i = 0; i < wlist->num_widgets; i++) {
1051 if (wlist->widgets[i] == w)
1052 return 0;
1053 }
1054
1055 /* allocate some new space */
1056 wlistentries = wlist->num_widgets + 1;
1057 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
1058 wlistentries * sizeof(struct snd_soc_dapm_widget *);
1059 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
1060 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001061 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001062 w->name);
1063 return -ENOMEM;
1064 }
1065 wlist = *list;
1066
1067 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001068 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001069 w->name, wlist->num_widgets);
1070
1071 wlist->widgets[wlist->num_widgets] = w;
1072 wlist->num_widgets++;
1073 return 1;
1074}
1075
Richard Purdie2b97eab2006-10-06 18:32:18 +02001076/*
1077 * Recursively check for a completed path to an active or physically connected
1078 * output widget. Returns number of complete paths.
1079 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001080static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1081 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001082{
1083 struct snd_soc_dapm_path *path;
1084 int con = 0;
1085
Mark Brown024dc072011-10-09 11:52:05 +01001086 if (widget->outputs >= 0)
1087 return widget->outputs;
1088
Mark Brownde02d072011-09-20 21:43:24 +01001089 DAPM_UPDATE_STAT(widget, path_checks);
1090
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001091 if (widget->is_sink && widget->connected) {
1092 widget->outputs = snd_soc_dapm_suspend_check(widget);
1093 return widget->outputs;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001094 }
1095
1096 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +01001097 DAPM_UPDATE_STAT(widget, neighbour_checks);
1098
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001099 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +01001100 continue;
1101
Mark Brown8af294b2013-02-22 17:48:15 +00001102 if (path->walking)
1103 return 1;
1104
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001105 trace_snd_soc_dapm_output_path(widget, path);
1106
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001107 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +00001108 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001109
1110 /* do we need to add this widget to the list ? */
1111 if (list) {
1112 int err;
1113 err = dapm_list_add_widget(list, path->sink);
1114 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001115 dev_err(widget->dapm->dev,
1116 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001117 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001118 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001119 return con;
1120 }
1121 }
1122
1123 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001124
1125 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001126 }
1127 }
1128
Mark Brown024dc072011-10-09 11:52:05 +01001129 widget->outputs = con;
1130
Richard Purdie2b97eab2006-10-06 18:32:18 +02001131 return con;
1132}
1133
1134/*
1135 * Recursively check for a completed path to an active or physically connected
1136 * input widget. Returns number of complete paths.
1137 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001138static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1139 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001140{
1141 struct snd_soc_dapm_path *path;
1142 int con = 0;
1143
Mark Brown024dc072011-10-09 11:52:05 +01001144 if (widget->inputs >= 0)
1145 return widget->inputs;
1146
Mark Brownde02d072011-09-20 21:43:24 +01001147 DAPM_UPDATE_STAT(widget, path_checks);
1148
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001149 if (widget->is_source && widget->connected) {
1150 widget->inputs = snd_soc_dapm_suspend_check(widget);
1151 return widget->inputs;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001152 }
1153
1154 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +01001155 DAPM_UPDATE_STAT(widget, neighbour_checks);
1156
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001157 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +01001158 continue;
1159
Mark Brown8af294b2013-02-22 17:48:15 +00001160 if (path->walking)
1161 return 1;
1162
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001163 trace_snd_soc_dapm_input_path(widget, path);
1164
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001165 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +00001166 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001167
1168 /* do we need to add this widget to the list ? */
1169 if (list) {
1170 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001171 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001172 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001173 dev_err(widget->dapm->dev,
1174 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001175 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001176 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001177 return con;
1178 }
1179 }
1180
1181 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001182
1183 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001184 }
1185 }
1186
Mark Brown024dc072011-10-09 11:52:05 +01001187 widget->inputs = con;
1188
Richard Purdie2b97eab2006-10-06 18:32:18 +02001189 return con;
1190}
1191
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001192/**
1193 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1194 * @dai: the soc DAI.
1195 * @stream: stream direction.
1196 * @list: list of active widgets for this stream.
1197 *
1198 * Queries DAPM graph as to whether an valid audio stream path exists for
1199 * the initial stream specified by name. This takes into account
1200 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1201 *
1202 * Returns the number of valid paths or negative error.
1203 */
1204int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1205 struct snd_soc_dapm_widget_list **list)
1206{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01001207 struct snd_soc_card *card = dai->component->card;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001208 struct snd_soc_dapm_widget *w;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001209 int paths;
1210
1211 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001212
1213 /*
1214 * For is_connected_{output,input}_ep fully discover the graph we need
1215 * to reset the cached number of inputs and outputs.
1216 */
1217 list_for_each_entry(w, &card->widgets, list) {
1218 w->inputs = -1;
1219 w->outputs = -1;
1220 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001221
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001222 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001223 paths = is_connected_output_ep(dai->playback_widget, list);
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001224 else
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001225 paths = is_connected_input_ep(dai->capture_widget, list);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001226
1227 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001228 mutex_unlock(&card->dapm_mutex);
1229
1230 return paths;
1231}
1232
Richard Purdie2b97eab2006-10-06 18:32:18 +02001233/*
Mark Brown62ea8742012-01-21 21:14:48 +00001234 * Handler for regulator supply widget.
1235 */
1236int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1237 struct snd_kcontrol *kcontrol, int event)
1238{
Mark Brownc05b84d2012-09-07 12:57:11 +08001239 int ret;
1240
Mark Browneb270e92013-10-09 13:52:52 +01001241 soc_dapm_async_complete(w->dapm);
1242
Mark Brownc05b84d2012-09-07 12:57:11 +08001243 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001244 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001245 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001246 if (ret != 0)
1247 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001248 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001249 w->name, ret);
1250 }
1251
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001252 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001253 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001254 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001255 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001256 if (ret != 0)
1257 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001258 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001259 w->name, ret);
1260 }
1261
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001262 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001263 }
Mark Brown62ea8742012-01-21 21:14:48 +00001264}
1265EXPORT_SYMBOL_GPL(dapm_regulator_event);
1266
Ola Liljad7e7eb92012-05-24 15:26:25 +02001267/*
1268 * Handler for clock supply widget.
1269 */
1270int dapm_clock_event(struct snd_soc_dapm_widget *w,
1271 struct snd_kcontrol *kcontrol, int event)
1272{
1273 if (!w->clk)
1274 return -EIO;
1275
Mark Browneb270e92013-10-09 13:52:52 +01001276 soc_dapm_async_complete(w->dapm);
1277
Mark Brownec029952012-06-04 08:16:20 +01001278#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001279 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001280 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001281 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001282 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001283 return 0;
1284 }
Mark Brownec029952012-06-04 08:16:20 +01001285#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001286 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001287}
1288EXPORT_SYMBOL_GPL(dapm_clock_event);
1289
Mark Brownd8050022011-09-28 18:28:23 +01001290static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1291{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001292 if (w->power_checked)
1293 return w->new_power;
1294
Mark Brownd8050022011-09-28 18:28:23 +01001295 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001296 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001297 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001298 w->new_power = w->power_check(w);
1299
1300 w->power_checked = true;
1301
1302 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001303}
1304
Mark Browncd0f2d42009-04-20 16:56:59 +01001305/* Generic check to see if a widget should be powered.
1306 */
1307static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1308{
1309 int in, out;
1310
Mark Brownde02d072011-09-20 21:43:24 +01001311 DAPM_UPDATE_STAT(w, power_checks);
1312
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001313 in = is_connected_input_ep(w, NULL);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001314 out = is_connected_output_ep(w, NULL);
Mark Browncd0f2d42009-04-20 16:56:59 +01001315 return out != 0 && in != 0;
1316}
1317
Mark Brown246d0a12009-04-22 18:24:55 +01001318/* Check to see if a power supply is needed */
1319static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1320{
1321 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001322
Mark Brownde02d072011-09-20 21:43:24 +01001323 DAPM_UPDATE_STAT(w, power_checks);
1324
Mark Brown246d0a12009-04-22 18:24:55 +01001325 /* Check if one of our outputs is connected */
1326 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001327 DAPM_UPDATE_STAT(w, neighbour_checks);
1328
Mark Brownbf3a9e12011-06-13 16:42:29 +01001329 if (path->weak)
1330 continue;
1331
Mark Brown215edda2009-09-08 18:59:05 +01001332 if (path->connected &&
1333 !path->connected(path->source, path->sink))
1334 continue;
1335
Mark Brownf68d7e12011-10-04 22:57:50 +01001336 if (dapm_widget_power_check(path->sink))
1337 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001338 }
1339
Mark Brownf68d7e12011-10-04 22:57:50 +01001340 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001341}
1342
Mark Brown35c64bc2011-09-28 18:23:53 +01001343static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1344{
1345 return 1;
1346}
1347
Mark Brown38357ab2009-06-06 19:03:23 +01001348static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1349 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001350 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001351{
Mark Brown828a8422011-01-15 13:14:30 +00001352 int *sort;
1353
1354 if (power_up)
1355 sort = dapm_up_seq;
1356 else
1357 sort = dapm_down_seq;
1358
Mark Brown38357ab2009-06-06 19:03:23 +01001359 if (sort[a->id] != sort[b->id])
1360 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001361 if (a->subseq != b->subseq) {
1362 if (power_up)
1363 return a->subseq - b->subseq;
1364 else
1365 return b->subseq - a->subseq;
1366 }
Mark Brownb22ead22009-06-07 12:51:26 +01001367 if (a->reg != b->reg)
1368 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001369 if (a->dapm != b->dapm)
1370 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001371
Mark Brown38357ab2009-06-06 19:03:23 +01001372 return 0;
1373}
Mark Brown42aa3412009-03-01 19:21:10 +00001374
Mark Brown38357ab2009-06-06 19:03:23 +01001375/* Insert a widget in order into a DAPM power sequence. */
1376static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1377 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001378 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001379{
1380 struct snd_soc_dapm_widget *w;
1381
1382 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001383 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001384 list_add_tail(&new_widget->power_list, &w->power_list);
1385 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001386 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001387
Mark Brown38357ab2009-06-06 19:03:23 +01001388 list_add_tail(&new_widget->power_list, list);
1389}
Mark Brown42aa3412009-03-01 19:21:10 +00001390
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001391static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001392 struct snd_soc_dapm_widget *w, int event)
1393{
Mark Brown68f89ad2010-11-03 23:51:49 -04001394 const char *ev_name;
1395 int power, ret;
1396
1397 switch (event) {
1398 case SND_SOC_DAPM_PRE_PMU:
1399 ev_name = "PRE_PMU";
1400 power = 1;
1401 break;
1402 case SND_SOC_DAPM_POST_PMU:
1403 ev_name = "POST_PMU";
1404 power = 1;
1405 break;
1406 case SND_SOC_DAPM_PRE_PMD:
1407 ev_name = "PRE_PMD";
1408 power = 0;
1409 break;
1410 case SND_SOC_DAPM_POST_PMD:
1411 ev_name = "POST_PMD";
1412 power = 0;
1413 break;
Mark Brown80114122013-02-25 15:14:19 +00001414 case SND_SOC_DAPM_WILL_PMU:
1415 ev_name = "WILL_PMU";
1416 power = 1;
1417 break;
1418 case SND_SOC_DAPM_WILL_PMD:
1419 ev_name = "WILL_PMD";
1420 power = 0;
1421 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001422 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001423 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001424 return;
1425 }
1426
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001427 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001428 return;
1429
1430 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001431 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001432 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001433 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001434 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001435 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001436 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001437 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001438 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001439 ev_name, w->name, ret);
1440 }
1441}
1442
Mark Brownb22ead22009-06-07 12:51:26 +01001443/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001444static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001445 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001446{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001447 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001448 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001449 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001450 unsigned int value = 0;
1451 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001452
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001453 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1454 reg = w->reg;
1455 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001456
1457 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001458 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001459 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001460
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001461 mask |= w->mask << w->shift;
1462 if (w->power)
1463 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001464 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001465 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001466
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001467 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001468 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1469 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001470
Mark Brown68f89ad2010-11-03 23:51:49 -04001471 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001472 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1473 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001474 }
1475
Mark Brown81628102009-06-07 13:21:24 +01001476 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001477 /* Any widget will do, they should all be updating the
1478 * same register.
1479 */
Mark Brown29376bc2011-06-19 13:49:28 +01001480
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001481 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001482 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001483 value, mask, reg, card->pop_time);
1484 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001485 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001486 }
1487
1488 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001489 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1490 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001491 }
Mark Brown42aa3412009-03-01 19:21:10 +00001492}
1493
Mark Brownb22ead22009-06-07 12:51:26 +01001494/* Apply a DAPM power sequence.
1495 *
1496 * We walk over a pre-sorted list of widgets to apply power to. In
1497 * order to minimise the number of writes to the device required
1498 * multiple widgets will be updated in a single write where possible.
1499 * Currently anything that requires more than a single write is not
1500 * handled.
1501 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001502static void dapm_seq_run(struct snd_soc_card *card,
1503 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001504{
1505 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001506 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001507 LIST_HEAD(pending);
1508 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001509 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001510 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001511 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001512 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001513 int *sort;
1514
1515 if (power_up)
1516 sort = dapm_up_seq;
1517 else
1518 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001519
Mark Brownb22ead22009-06-07 12:51:26 +01001520 list_for_each_entry_safe(w, n, list, power_list) {
1521 ret = 0;
1522
1523 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001524 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001525 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001526 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001527 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001528
Mark Brown474b62d2011-01-18 16:14:44 +00001529 if (cur_dapm && cur_dapm->seq_notifier) {
1530 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1531 if (sort[i] == cur_sort)
1532 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001533 i,
1534 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001535 }
1536
Mark Browneb270e92013-10-09 13:52:52 +01001537 if (cur_dapm && w->dapm != cur_dapm)
1538 soc_dapm_async_complete(cur_dapm);
1539
Mark Brownb22ead22009-06-07 12:51:26 +01001540 INIT_LIST_HEAD(&pending);
1541 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001542 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001543 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001544 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001545 }
1546
Mark Brown163cac02009-06-07 10:12:52 +01001547 switch (w->id) {
1548 case snd_soc_dapm_pre:
1549 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001550 list_for_each_entry_safe_continue(w, n, list,
1551 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001552
Mark Brownb22ead22009-06-07 12:51:26 +01001553 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001554 ret = w->event(w,
1555 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001556 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001557 ret = w->event(w,
1558 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001559 break;
1560
1561 case snd_soc_dapm_post:
1562 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001563 list_for_each_entry_safe_continue(w, n, list,
1564 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001565
Mark Brownb22ead22009-06-07 12:51:26 +01001566 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001567 ret = w->event(w,
1568 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001569 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001570 ret = w->event(w,
1571 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001572 break;
1573
Mark Brown163cac02009-06-07 10:12:52 +01001574 default:
Mark Brown81628102009-06-07 13:21:24 +01001575 /* Queue it up for application */
1576 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001577 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001578 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001579 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001580 list_move(&w->power_list, &pending);
1581 break;
Mark Brown163cac02009-06-07 10:12:52 +01001582 }
Mark Brownb22ead22009-06-07 12:51:26 +01001583
1584 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001585 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001586 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001587 }
Mark Brownb22ead22009-06-07 12:51:26 +01001588
1589 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001590 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001591
1592 if (cur_dapm && cur_dapm->seq_notifier) {
1593 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1594 if (sort[i] == cur_sort)
1595 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001596 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001597 }
Mark Browneb270e92013-10-09 13:52:52 +01001598
1599 list_for_each_entry(d, &card->dapm_list, list) {
1600 soc_dapm_async_complete(d);
1601 }
Mark Brown163cac02009-06-07 10:12:52 +01001602}
1603
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001604static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001605{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001606 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001607 struct snd_soc_dapm_widget_list *wlist;
1608 struct snd_soc_dapm_widget *w = NULL;
1609 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001610 int ret;
1611
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001612 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001613 return;
1614
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001615 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001616
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001617 for (wi = 0; wi < wlist->num_widgets; wi++) {
1618 w = wlist->widgets[wi];
1619
1620 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1621 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1622 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001623 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001624 w->name, ret);
1625 }
Mark Brown97404f22010-12-14 16:13:57 +00001626 }
1627
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001628 if (!w)
1629 return;
1630
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001631 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1632 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001633 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001634 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001635 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001636
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001637 for (wi = 0; wi < wlist->num_widgets; wi++) {
1638 w = wlist->widgets[wi];
1639
1640 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1641 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1642 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001643 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001644 w->name, ret);
1645 }
Mark Brown97404f22010-12-14 16:13:57 +00001646 }
1647}
1648
Mark Brown9d0624a2011-02-18 11:49:43 -08001649/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1650 * they're changing state.
1651 */
1652static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1653{
1654 struct snd_soc_dapm_context *d = data;
1655 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001656
Mark Brown56fba412011-06-04 11:25:10 +01001657 /* If we're off and we're not supposed to be go into STANDBY */
1658 if (d->bias_level == SND_SOC_BIAS_OFF &&
1659 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001660 if (d->dev)
1661 pm_runtime_get_sync(d->dev);
1662
Mark Brown9d0624a2011-02-18 11:49:43 -08001663 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1664 if (ret != 0)
1665 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001666 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001667 }
1668
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001669 /* Prepare for a transition to ON or away from ON */
1670 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1671 d->bias_level != SND_SOC_BIAS_ON) ||
1672 (d->target_bias_level != SND_SOC_BIAS_ON &&
1673 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001674 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1675 if (ret != 0)
1676 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001677 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001678 }
1679}
1680
1681/* Async callback run prior to DAPM sequences - brings to their final
1682 * state.
1683 */
1684static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1685{
1686 struct snd_soc_dapm_context *d = data;
1687 int ret;
1688
1689 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001690 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1691 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1692 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001693 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1694 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001695 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001696 ret);
1697 }
1698
1699 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001700 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1701 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001702 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1703 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001704 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1705 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001706
1707 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001708 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001709 }
1710
1711 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001712 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1713 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001714 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1715 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001716 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001717 ret);
1718 }
1719}
Mark Brown97404f22010-12-14 16:13:57 +00001720
Mark Brownfe4fda52011-10-03 22:36:57 +01001721static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1722 bool power, bool connect)
1723{
1724 /* If a connection is being made or broken then that update
1725 * will have marked the peer dirty, otherwise the widgets are
1726 * not connected and this update has no impact. */
1727 if (!connect)
1728 return;
1729
1730 /* If the peer is already in the state we're moving to then we
1731 * won't have an impact on it. */
1732 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001733 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001734}
1735
Mark Brown05623c42011-09-28 17:02:31 +01001736static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1737 struct list_head *up_list,
1738 struct list_head *down_list)
1739{
Mark Browndb432b42011-10-03 21:06:40 +01001740 struct snd_soc_dapm_path *path;
1741
Mark Brown05623c42011-09-28 17:02:31 +01001742 if (w->power == power)
1743 return;
1744
1745 trace_snd_soc_dapm_widget_power(w, power);
1746
Mark Browndb432b42011-10-03 21:06:40 +01001747 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001748 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001749 */
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001750 list_for_each_entry(path, &w->sources, list_sink)
1751 dapm_widget_set_peer_power(path->source, power, path->connect);
1752
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001753 /* Supplies can't affect their outputs, only their inputs */
1754 if (!w->is_supply) {
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001755 list_for_each_entry(path, &w->sinks, list_source)
1756 dapm_widget_set_peer_power(path->sink, power,
1757 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001758 }
1759
Mark Brown05623c42011-09-28 17:02:31 +01001760 if (power)
1761 dapm_seq_insert(w, up_list, true);
1762 else
1763 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001764}
1765
Mark Brown7c81beb2011-09-20 22:22:32 +01001766static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1767 struct list_head *up_list,
1768 struct list_head *down_list)
1769{
Mark Brown7c81beb2011-09-20 22:22:32 +01001770 int power;
1771
1772 switch (w->id) {
1773 case snd_soc_dapm_pre:
1774 dapm_seq_insert(w, down_list, false);
1775 break;
1776 case snd_soc_dapm_post:
1777 dapm_seq_insert(w, up_list, true);
1778 break;
1779
1780 default:
Mark Brownd8050022011-09-28 18:28:23 +01001781 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001782
Mark Brown05623c42011-09-28 17:02:31 +01001783 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001784 break;
1785 }
1786}
1787
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001788static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1789{
1790 if (dapm->idle_bias_off)
1791 return true;
1792
1793 switch (snd_power_get_state(dapm->card->snd_card)) {
1794 case SNDRV_CTL_POWER_D3hot:
1795 case SNDRV_CTL_POWER_D3cold:
1796 return dapm->suspend_bias_off;
1797 default:
1798 break;
1799 }
1800
1801 return false;
1802}
1803
Mark Brown42aa3412009-03-01 19:21:10 +00001804/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001805 * Scan each dapm widget for complete audio path.
1806 * A complete path is a route that has valid endpoints i.e.:-
1807 *
1808 * o DAC to output pin.
1809 * o Input Pin to ADC.
1810 * o Input pin to Output pin (bypass, sidetone)
1811 * o DAC to ADC (loopback).
1812 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001813static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001814{
1815 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001816 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001817 LIST_HEAD(up_list);
1818 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001819 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001820 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001821
Mark Brownf9fa2b12014-03-06 16:49:11 +08001822 lockdep_assert_held(&card->dapm_mutex);
1823
Mark Brown84e90932010-11-04 00:07:02 -04001824 trace_snd_soc_dapm_start(card);
1825
Mark Brown56fba412011-06-04 11:25:10 +01001826 list_for_each_entry(d, &card->dapm_list, list) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001827 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001828 d->target_bias_level = SND_SOC_BIAS_OFF;
1829 else
1830 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001831 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001832
Liam Girdwood6c120e12012-02-15 15:15:34 +00001833 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001834
Mark Brown6d3ddc82009-05-16 17:47:29 +01001835 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001836 * lists indicating if they should be powered up or down. We
1837 * only check widgets that have been flagged as dirty but note
1838 * that new widgets may be added to the dirty list while we
1839 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001840 */
Mark Browndb432b42011-10-03 21:06:40 +01001841 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001842 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001843 }
1844
Mark Brownf9de6d72011-09-28 17:19:47 +01001845 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001846 switch (w->id) {
1847 case snd_soc_dapm_pre:
1848 case snd_soc_dapm_post:
1849 /* These widgets always need to be powered */
1850 break;
1851 default:
1852 list_del_init(&w->dirty);
1853 break;
1854 }
Mark Browndb432b42011-10-03 21:06:40 +01001855
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001856 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001857 d = w->dapm;
1858
1859 /* Supplies and micbiases only bring the
1860 * context up to STANDBY as unless something
1861 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001862 * generally don't require full power. Signal
1863 * generators are virtual pins and have no
1864 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001865 */
1866 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001867 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001868 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001869 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001870 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001871 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001872 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001873 case snd_soc_dapm_micbias:
1874 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1875 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1876 break;
1877 default:
1878 d->target_bias_level = SND_SOC_BIAS_ON;
1879 break;
1880 }
1881 }
1882
1883 }
1884
Mark Brown85a843c2011-09-21 21:29:47 +01001885 /* Force all contexts in the card to the same bias state if
1886 * they're not ground referenced.
1887 */
Mark Brown56fba412011-06-04 11:25:10 +01001888 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001889 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001890 if (d->target_bias_level > bias)
1891 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001892 list_for_each_entry(d, &card->dapm_list, list)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001893 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01001894 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001895
Mark Brownde02d072011-09-20 21:43:24 +01001896 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001897
Xiang Xiao17282ba2014-03-02 00:04:03 +08001898 /* Run card bias changes at first */
1899 dapm_pre_sequence_async(&card->dapm, 0);
1900 /* Run other bias changes in parallel */
1901 list_for_each_entry(d, &card->dapm_list, list) {
1902 if (d != &card->dapm)
1903 async_schedule_domain(dapm_pre_sequence_async, d,
1904 &async_domain);
1905 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001906 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001907
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001908 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001909 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001910 }
1911
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001912 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001913 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001914 }
1915
Mark Brown6d3ddc82009-05-16 17:47:29 +01001916 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001917 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001918
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001919 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001920
Mark Brown6d3ddc82009-05-16 17:47:29 +01001921 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001922 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001923
Mark Brown9d0624a2011-02-18 11:49:43 -08001924 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001925 list_for_each_entry(d, &card->dapm_list, list) {
1926 if (d != &card->dapm)
1927 async_schedule_domain(dapm_post_sequence_async, d,
1928 &async_domain);
1929 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001930 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001931 /* Run card bias changes at last */
1932 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001933
Liam Girdwood8078d872012-02-15 15:15:35 +00001934 /* do we need to notify any clients that DAPM event is complete */
1935 list_for_each_entry(d, &card->dapm_list, list) {
1936 if (d->stream_event)
1937 d->stream_event(d, event);
1938 }
1939
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001940 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001941 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001942 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001943
Mark Brown84e90932010-11-04 00:07:02 -04001944 trace_snd_soc_dapm_done(card);
1945
Mark Brown42aa3412009-03-01 19:21:10 +00001946 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001947}
1948
Mark Brown79fb9382009-08-21 16:38:13 +01001949#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001950static ssize_t dapm_widget_power_read_file(struct file *file,
1951 char __user *user_buf,
1952 size_t count, loff_t *ppos)
1953{
1954 struct snd_soc_dapm_widget *w = file->private_data;
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02001955 struct snd_soc_card *card = w->dapm->card;
Mark Brown79fb9382009-08-21 16:38:13 +01001956 char *buf;
1957 int in, out;
1958 ssize_t ret;
1959 struct snd_soc_dapm_path *p = NULL;
1960
1961 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1962 if (!buf)
1963 return -ENOMEM;
1964
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02001965 mutex_lock(&card->dapm_mutex);
1966
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001967 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
1968 if (w->is_supply) {
1969 in = 0;
1970 out = 0;
1971 } else {
1972 in = is_connected_input_ep(w, NULL);
1973 out = is_connected_output_ep(w, NULL);
1974 }
Mark Brown79fb9382009-08-21 16:38:13 +01001975
Mark Brownf13ebad2012-03-03 18:01:01 +00001976 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1977 w->name, w->power ? "On" : "Off",
1978 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001979
Mark Brownd033c362009-12-04 15:25:56 +00001980 if (w->reg >= 0)
1981 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001982 " - R%d(0x%x) mask 0x%x",
1983 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001984
1985 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1986
Mark Brown3eef08b2009-09-14 16:49:00 +01001987 if (w->sname)
1988 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1989 w->sname,
1990 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001991
1992 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001993 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001994 continue;
1995
Mark Brown79fb9382009-08-21 16:38:13 +01001996 if (p->connect)
1997 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001998 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001999 p->name ? p->name : "static",
2000 p->source->name);
2001 }
2002 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01002003 if (p->connected && !p->connected(w, p->sink))
2004 continue;
2005
Mark Brown79fb9382009-08-21 16:38:13 +01002006 if (p->connect)
2007 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00002008 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01002009 p->name ? p->name : "static",
2010 p->sink->name);
2011 }
2012
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002013 mutex_unlock(&card->dapm_mutex);
2014
Mark Brown79fb9382009-08-21 16:38:13 +01002015 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2016
2017 kfree(buf);
2018 return ret;
2019}
2020
2021static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002022 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002023 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002024 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002025};
2026
Mark Brownef49e4f2011-04-04 20:48:13 +09002027static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2028 size_t count, loff_t *ppos)
2029{
2030 struct snd_soc_dapm_context *dapm = file->private_data;
2031 char *level;
2032
2033 switch (dapm->bias_level) {
2034 case SND_SOC_BIAS_ON:
2035 level = "On\n";
2036 break;
2037 case SND_SOC_BIAS_PREPARE:
2038 level = "Prepare\n";
2039 break;
2040 case SND_SOC_BIAS_STANDBY:
2041 level = "Standby\n";
2042 break;
2043 case SND_SOC_BIAS_OFF:
2044 level = "Off\n";
2045 break;
2046 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002047 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002048 level = "Unknown\n";
2049 break;
2050 }
2051
2052 return simple_read_from_buffer(user_buf, count, ppos, level,
2053 strlen(level));
2054}
2055
2056static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002057 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002058 .read = dapm_bias_read_file,
2059 .llseek = default_llseek,
2060};
2061
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002062void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2063 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002064{
Mark Brown79fb9382009-08-21 16:38:13 +01002065 struct dentry *d;
2066
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02002067 if (!parent)
2068 return;
2069
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002070 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2071
2072 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002073 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002074 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002075 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002076 }
Mark Brown79fb9382009-08-21 16:38:13 +01002077
Mark Brownef49e4f2011-04-04 20:48:13 +09002078 d = debugfs_create_file("bias_level", 0444,
2079 dapm->debugfs_dapm, dapm,
2080 &dapm_bias_fops);
2081 if (!d)
2082 dev_warn(dapm->dev,
2083 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002084}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002085
2086static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2087{
2088 struct snd_soc_dapm_context *dapm = w->dapm;
2089 struct dentry *d;
2090
2091 if (!dapm->debugfs_dapm || !w->name)
2092 return;
2093
2094 d = debugfs_create_file(w->name, 0444,
2095 dapm->debugfs_dapm, w,
2096 &dapm_widget_power_fops);
2097 if (!d)
2098 dev_warn(w->dapm->dev,
2099 "ASoC: Failed to create %s debugfs file\n",
2100 w->name);
2101}
2102
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002103static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2104{
2105 debugfs_remove_recursive(dapm->debugfs_dapm);
2106}
2107
Mark Brown79fb9382009-08-21 16:38:13 +01002108#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002109void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2110 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002111{
2112}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002113
2114static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2115{
2116}
2117
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002118static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2119{
2120}
2121
Mark Brown79fb9382009-08-21 16:38:13 +01002122#endif
2123
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002124/*
2125 * soc_dapm_connect_path() - Connects or disconnects a path
2126 * @path: The path to update
2127 * @connect: The new connect state of the path. True if the path is connected,
2128 * false if it is disconneted.
2129 * @reason: The reason why the path changed (for debugging only)
2130 */
2131static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2132 bool connect, const char *reason)
2133{
2134 if (path->connect == connect)
2135 return;
2136
2137 path->connect = connect;
2138 dapm_mark_dirty(path->source, reason);
2139 dapm_mark_dirty(path->sink, reason);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002140 dapm_path_invalidate(path);
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002141}
2142
Richard Purdie2b97eab2006-10-06 18:32:18 +02002143/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002144static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002145 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002146{
2147 struct snd_soc_dapm_path *path;
2148 int found = 0;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002149 bool connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002150
Mark Brownf9fa2b12014-03-06 16:49:11 +08002151 lockdep_assert_held(&card->dapm_mutex);
2152
Richard Purdie2b97eab2006-10-06 18:32:18 +02002153 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002154 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002155 found = 1;
2156 /* we now need to match the string in the enum to the path */
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002157 if (!(strcmp(path->name, e->texts[mux])))
2158 connect = true;
2159 else
2160 connect = false;
2161
2162 soc_dapm_connect_path(path, connect, "mux update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002163 }
2164
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002165 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002166 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002167
Liam Girdwood618dae12012-04-25 12:12:51 +01002168 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002169}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002170
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002171int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002172 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2173 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002174{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002175 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002176 int ret;
2177
Liam Girdwood3cd04342012-03-09 12:02:08 +00002178 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002179 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002180 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002181 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002182 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002183 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002184 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002185 return ret;
2186}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002187EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002188
Milan plzik1b075e32008-01-10 14:39:46 +01002189/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002190static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002191 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002192{
2193 struct snd_soc_dapm_path *path;
2194 int found = 0;
2195
Mark Brownf9fa2b12014-03-06 16:49:11 +08002196 lockdep_assert_held(&card->dapm_mutex);
2197
Richard Purdie2b97eab2006-10-06 18:32:18 +02002198 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002199 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002200 found = 1;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002201 soc_dapm_connect_path(path, connect, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002202 }
2203
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002204 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002205 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002206
Liam Girdwood618dae12012-04-25 12:12:51 +01002207 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002208}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002209
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002210int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002211 struct snd_kcontrol *kcontrol, int connect,
2212 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002213{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002214 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002215 int ret;
2216
Liam Girdwood3cd04342012-03-09 12:02:08 +00002217 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002218 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002219 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002220 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002221 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002222 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002223 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002224 return ret;
2225}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002226EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002227
Benoit Cousson44ba2642014-07-08 23:19:36 +02002228static ssize_t dapm_widget_show_codec(struct snd_soc_codec *codec, char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002229{
Richard Purdie2b97eab2006-10-06 18:32:18 +02002230 struct snd_soc_dapm_widget *w;
2231 int count = 0;
2232 char *state = "not set";
2233
Lars-Peter Clausen00200102014-07-17 22:01:07 +02002234 list_for_each_entry(w, &codec->component.card->widgets, list) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002235 if (w->dapm != &codec->dapm)
2236 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002237
2238 /* only display widgets that burnm power */
2239 switch (w->id) {
2240 case snd_soc_dapm_hp:
2241 case snd_soc_dapm_mic:
2242 case snd_soc_dapm_spk:
2243 case snd_soc_dapm_line:
2244 case snd_soc_dapm_micbias:
2245 case snd_soc_dapm_dac:
2246 case snd_soc_dapm_adc:
2247 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002248 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002249 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002250 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002251 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002252 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002253 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002254 if (w->name)
2255 count += sprintf(buf + count, "%s: %s\n",
2256 w->name, w->power ? "On":"Off");
2257 break;
2258 default:
2259 break;
2260 }
2261 }
2262
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002263 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002264 case SND_SOC_BIAS_ON:
2265 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002266 break;
Mark Brown0be98982008-05-19 12:31:28 +02002267 case SND_SOC_BIAS_PREPARE:
2268 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002269 break;
Mark Brown0be98982008-05-19 12:31:28 +02002270 case SND_SOC_BIAS_STANDBY:
2271 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002272 break;
Mark Brown0be98982008-05-19 12:31:28 +02002273 case SND_SOC_BIAS_OFF:
2274 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002275 break;
2276 }
2277 count += sprintf(buf + count, "PM State: %s\n", state);
2278
2279 return count;
2280}
2281
Benoit Cousson44ba2642014-07-08 23:19:36 +02002282/* show dapm widget status in sys fs */
2283static ssize_t dapm_widget_show(struct device *dev,
2284 struct device_attribute *attr, char *buf)
2285{
2286 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2287 int i, count = 0;
2288
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002289 mutex_lock(&rtd->card->dapm_mutex);
2290
Benoit Cousson44ba2642014-07-08 23:19:36 +02002291 for (i = 0; i < rtd->num_codecs; i++) {
2292 struct snd_soc_codec *codec = rtd->codec_dais[i]->codec;
2293 count += dapm_widget_show_codec(codec, buf + count);
2294 }
2295
Lars-Peter Clausene50b1e02015-07-06 17:01:24 +02002296 mutex_unlock(&rtd->card->dapm_mutex);
2297
Benoit Cousson44ba2642014-07-08 23:19:36 +02002298 return count;
2299}
2300
Richard Purdie2b97eab2006-10-06 18:32:18 +02002301static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2302
Takashi Iwaid29697d2015-01-30 20:16:37 +01002303struct attribute *soc_dapm_dev_attrs[] = {
2304 &dev_attr_dapm_widget.attr,
2305 NULL
2306};
Richard Purdie2b97eab2006-10-06 18:32:18 +02002307
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002308static void dapm_free_path(struct snd_soc_dapm_path *path)
2309{
2310 list_del(&path->list_sink);
2311 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002312 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002313 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002314 kfree(path);
2315}
2316
Richard Purdie2b97eab2006-10-06 18:32:18 +02002317/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002318static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002319{
2320 struct snd_soc_dapm_widget *w, *next_w;
2321 struct snd_soc_dapm_path *p, *next_p;
2322
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002323 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2324 if (w->dapm != dapm)
2325 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002326 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002327 /*
2328 * remove source and sink paths associated to this widget.
2329 * While removing the path, remove reference to it from both
2330 * source and sink widgets so that path is removed only once.
2331 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002332 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2333 dapm_free_path(p);
2334
2335 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2336 dapm_free_path(p);
2337
Stephen Warrenfad59882011-04-28 17:37:59 -06002338 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002339 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002340 kfree(w);
2341 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002342}
2343
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002344static struct snd_soc_dapm_widget *dapm_find_widget(
2345 struct snd_soc_dapm_context *dapm, const char *pin,
2346 bool search_other_contexts)
2347{
2348 struct snd_soc_dapm_widget *w;
2349 struct snd_soc_dapm_widget *fallback = NULL;
2350
2351 list_for_each_entry(w, &dapm->card->widgets, list) {
2352 if (!strcmp(w->name, pin)) {
2353 if (w->dapm == dapm)
2354 return w;
2355 else
2356 fallback = w;
2357 }
2358 }
2359
2360 if (search_other_contexts)
2361 return fallback;
2362
2363 return NULL;
2364}
2365
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002366static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002367 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002368{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002369 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002370
Mark Brownf9fa2b12014-03-06 16:49:11 +08002371 dapm_assert_locked(dapm);
2372
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002373 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002374 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002375 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002376 }
2377
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002378 if (w->connected != status) {
Mark Brown1a8b2d92012-02-16 11:50:07 -08002379 dapm_mark_dirty(w, "pin configuration");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002380 dapm_widget_invalidate_input_paths(w);
2381 dapm_widget_invalidate_output_paths(w);
2382 }
Mark Brown1a8b2d92012-02-16 11:50:07 -08002383
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002384 w->connected = status;
2385 if (status == 0)
2386 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002387
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002388 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002389}
2390
Richard Purdie2b97eab2006-10-06 18:32:18 +02002391/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002392 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2393 * @dapm: DAPM context
2394 *
2395 * Walks all dapm audio paths and powers widgets according to their
2396 * stream or path usage.
2397 *
2398 * Requires external locking.
2399 *
2400 * Returns 0 for success.
2401 */
2402int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2403{
2404 /*
2405 * Suppress early reports (eg, jacks syncing their state) to avoid
2406 * silly DAPM runs during card startup.
2407 */
2408 if (!dapm->card || !dapm->card->instantiated)
2409 return 0;
2410
2411 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2412}
2413EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2414
2415/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002416 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002417 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002418 *
2419 * Walks all dapm audio paths and powers widgets according to their
2420 * stream or path usage.
2421 *
2422 * Returns 0 for success.
2423 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002424int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002425{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002426 int ret;
2427
Liam Girdwood3cd04342012-03-09 12:02:08 +00002428 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002429 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002430 mutex_unlock(&dapm->card->dapm_mutex);
2431 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002432}
Liam Girdwooda5302182008-07-07 13:35:17 +01002433EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002434
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002435/*
2436 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2437 * @w: The widget for which to update the flags
2438 *
2439 * Some widgets have a dynamic category which depends on which neighbors they
2440 * are connected to. This function update the category for these widgets.
2441 *
2442 * This function must be called whenever a path is added or removed to a widget.
2443 */
2444static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2445{
2446 struct snd_soc_dapm_path *p;
2447
2448 switch (w->id) {
2449 case snd_soc_dapm_input:
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002450 /* On a fully routed card a input is never a source */
2451 if (w->dapm->card->fully_routed)
2452 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002453 w->is_source = 1;
2454 list_for_each_entry(p, &w->sources, list_sink) {
2455 if (p->source->id == snd_soc_dapm_micbias ||
2456 p->source->id == snd_soc_dapm_mic ||
2457 p->source->id == snd_soc_dapm_line ||
2458 p->source->id == snd_soc_dapm_output) {
2459 w->is_source = 0;
2460 break;
2461 }
2462 }
2463 break;
2464 case snd_soc_dapm_output:
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002465 /* On a fully routed card a output is never a sink */
2466 if (w->dapm->card->fully_routed)
2467 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002468 w->is_sink = 1;
2469 list_for_each_entry(p, &w->sinks, list_source) {
2470 if (p->sink->id == snd_soc_dapm_spk ||
2471 p->sink->id == snd_soc_dapm_hp ||
2472 p->sink->id == snd_soc_dapm_line ||
2473 p->sink->id == snd_soc_dapm_input) {
2474 w->is_sink = 0;
2475 break;
2476 }
2477 }
2478 break;
2479 case snd_soc_dapm_line:
2480 w->is_sink = !list_empty(&w->sources);
2481 w->is_source = !list_empty(&w->sinks);
2482 break;
2483 default:
2484 break;
2485 }
2486}
2487
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002488static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2489 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2490 const char *control)
2491{
2492 bool dynamic_source = false;
2493 bool dynamic_sink = false;
2494
2495 if (!control)
2496 return 0;
2497
2498 switch (source->id) {
2499 case snd_soc_dapm_demux:
2500 dynamic_source = true;
2501 break;
2502 default:
2503 break;
2504 }
2505
2506 switch (sink->id) {
2507 case snd_soc_dapm_mux:
2508 case snd_soc_dapm_switch:
2509 case snd_soc_dapm_mixer:
2510 case snd_soc_dapm_mixer_named_ctl:
2511 dynamic_sink = true;
2512 break;
2513 default:
2514 break;
2515 }
2516
2517 if (dynamic_source && dynamic_sink) {
2518 dev_err(dapm->dev,
2519 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2520 source->name, control, sink->name);
2521 return -EINVAL;
2522 } else if (!dynamic_source && !dynamic_sink) {
2523 dev_err(dapm->dev,
2524 "Control not supported for path %s -> [%s] -> %s\n",
2525 source->name, control, sink->name);
2526 return -EINVAL;
2527 }
2528
2529 return 0;
2530}
2531
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002532static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2533 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2534 const char *control,
2535 int (*connected)(struct snd_soc_dapm_widget *source,
2536 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002537{
2538 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002539 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002540
Lars-Peter Clausene409dfb2014-10-25 17:42:02 +02002541 if (wsink->is_supply && !wsource->is_supply) {
2542 dev_err(dapm->dev,
2543 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2544 wsource->name, wsink->name);
2545 return -EINVAL;
2546 }
2547
2548 if (connected && !wsource->is_supply) {
2549 dev_err(dapm->dev,
2550 "connected() callback only supported for supply widgets (%s -> %s)\n",
2551 wsource->name, wsink->name);
2552 return -EINVAL;
2553 }
2554
2555 if (wsource->is_supply && control) {
2556 dev_err(dapm->dev,
2557 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2558 wsource->name, control, wsink->name);
2559 return -EINVAL;
2560 }
2561
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002562 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2563 if (ret)
2564 return ret;
2565
Richard Purdie2b97eab2006-10-06 18:32:18 +02002566 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2567 if (!path)
2568 return -ENOMEM;
2569
2570 path->source = wsource;
2571 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002572 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002573 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002574 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002575 INIT_LIST_HEAD(&path->list_source);
2576 INIT_LIST_HEAD(&path->list_sink);
2577
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002578 if (wsource->is_supply || wsink->is_supply)
2579 path->is_supply = 1;
2580
Richard Purdie2b97eab2006-10-06 18:32:18 +02002581 /* connect static paths */
2582 if (control == NULL) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002583 path->connect = 1;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002584 } else {
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002585 switch (wsource->id) {
2586 case snd_soc_dapm_demux:
2587 ret = dapm_connect_mux(dapm, path, control, wsource);
2588 if (ret)
2589 goto err;
2590 break;
2591 default:
2592 break;
2593 }
2594
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002595 switch (wsink->id) {
2596 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002597 ret = dapm_connect_mux(dapm, path, control, wsink);
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002598 if (ret != 0)
2599 goto err;
2600 break;
2601 case snd_soc_dapm_switch:
2602 case snd_soc_dapm_mixer:
2603 case snd_soc_dapm_mixer_named_ctl:
2604 ret = dapm_connect_mixer(dapm, path, control);
2605 if (ret != 0)
2606 goto err;
2607 break;
2608 default:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002609 break;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002610 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002611 }
2612
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002613 list_add(&path->list, &dapm->card->paths);
2614 list_add(&path->list_sink, &wsink->sources);
2615 list_add(&path->list_source, &wsource->sinks);
2616
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002617 dapm_update_widget_flags(wsource);
2618 dapm_update_widget_flags(wsink);
2619
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002620 dapm_mark_dirty(wsource, "Route added");
2621 dapm_mark_dirty(wsink, "Route added");
Mark Brownfabd0382012-07-05 17:20:06 +01002622
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002623 if (dapm->card->instantiated && path->connect)
2624 dapm_path_invalidate(path);
2625
Richard Purdie2b97eab2006-10-06 18:32:18 +02002626 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002627err:
2628 kfree(path);
2629 return ret;
2630}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002631
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002632static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002633 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002634{
2635 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2636 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2637 const char *sink;
2638 const char *source;
2639 char prefixed_sink[80];
2640 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002641 const char *prefix;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002642 int ret;
2643
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002644 prefix = soc_dapm_prefix(dapm);
2645 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002646 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002647 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002648 sink = prefixed_sink;
2649 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002650 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002651 source = prefixed_source;
2652 } else {
2653 sink = route->sink;
2654 source = route->source;
2655 }
2656
Charles Keepax45a110a2015-05-11 13:50:30 +01002657 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2658 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2659
2660 if (wsink && wsource)
2661 goto skip_search;
2662
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002663 /*
2664 * find src and dest widgets over all widgets but favor a widget from
2665 * current DAPM context
2666 */
2667 list_for_each_entry(w, &dapm->card->widgets, list) {
2668 if (!wsink && !(strcmp(w->name, sink))) {
2669 wtsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002670 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002671 wsink = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002672 if (wsource)
2673 break;
2674 }
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002675 continue;
2676 }
2677 if (!wsource && !(strcmp(w->name, source))) {
2678 wtsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002679 if (w->dapm == dapm) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002680 wsource = w;
Charles Keepax70c75102015-05-07 11:33:58 +01002681 if (wsink)
2682 break;
2683 }
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002684 }
2685 }
2686 /* use widget from another DAPM context if not found from this */
2687 if (!wsink)
2688 wsink = wtsink;
2689 if (!wsource)
2690 wsource = wtsource;
2691
2692 if (wsource == NULL) {
2693 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2694 route->source);
2695 return -ENODEV;
2696 }
2697 if (wsink == NULL) {
2698 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2699 route->sink);
2700 return -ENODEV;
2701 }
2702
Charles Keepax45a110a2015-05-11 13:50:30 +01002703skip_search:
2704 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2705 dapm_wcache_update(&dapm->path_source_cache, wsource);
2706
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002707 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2708 route->connected);
2709 if (ret)
2710 goto err;
2711
2712 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002713err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002714 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002715 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002716 return ret;
2717}
Mark Brown105f1c22008-05-13 14:52:19 +02002718
Mark Brownefcc3c62012-07-05 17:24:19 +01002719static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2720 const struct snd_soc_dapm_route *route)
2721{
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002722 struct snd_soc_dapm_widget *wsource, *wsink;
Mark Brownefcc3c62012-07-05 17:24:19 +01002723 struct snd_soc_dapm_path *path, *p;
2724 const char *sink;
2725 const char *source;
2726 char prefixed_sink[80];
2727 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002728 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002729
2730 if (route->control) {
2731 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002732 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002733 return -EINVAL;
2734 }
2735
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002736 prefix = soc_dapm_prefix(dapm);
2737 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002738 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002739 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002740 sink = prefixed_sink;
2741 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002742 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002743 source = prefixed_source;
2744 } else {
2745 sink = route->sink;
2746 source = route->source;
2747 }
2748
2749 path = NULL;
2750 list_for_each_entry(p, &dapm->card->paths, list) {
2751 if (strcmp(p->source->name, source) != 0)
2752 continue;
2753 if (strcmp(p->sink->name, sink) != 0)
2754 continue;
2755 path = p;
2756 break;
2757 }
2758
2759 if (path) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002760 wsource = path->source;
2761 wsink = path->sink;
2762
2763 dapm_mark_dirty(wsource, "Route removed");
2764 dapm_mark_dirty(wsink, "Route removed");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002765 if (path->connect)
2766 dapm_path_invalidate(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002767
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002768 dapm_free_path(path);
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002769
2770 /* Update any path related flags */
2771 dapm_update_widget_flags(wsource);
2772 dapm_update_widget_flags(wsink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002773 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002774 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002775 source, sink);
2776 }
2777
2778 return 0;
2779}
2780
Mark Brown105f1c22008-05-13 14:52:19 +02002781/**
Mark Brown105f1c22008-05-13 14:52:19 +02002782 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002783 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002784 * @route: audio routes
2785 * @num: number of routes
2786 *
2787 * Connects 2 dapm widgets together via a named audio path. The sink is
2788 * the widget receiving the audio signal, whilst the source is the sender
2789 * of the audio signal.
2790 *
2791 * Returns 0 for success else error. On error all resources can be freed
2792 * with a call to snd_soc_card_free().
2793 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002794int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002795 const struct snd_soc_dapm_route *route, int num)
2796{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002797 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002798
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002799 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002800 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002801 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002802 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002803 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2804 route->source,
2805 route->control ? route->control : "direct",
2806 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002807 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002808 }
2809 route++;
2810 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002811 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002812
Dan Carpenter60884c22012-04-13 22:25:43 +03002813 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002814}
2815EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2816
Mark Brownefcc3c62012-07-05 17:24:19 +01002817/**
2818 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2819 * @dapm: DAPM context
2820 * @route: audio routes
2821 * @num: number of routes
2822 *
2823 * Removes routes from the DAPM context.
2824 */
2825int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2826 const struct snd_soc_dapm_route *route, int num)
2827{
2828 int i, ret = 0;
2829
2830 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2831 for (i = 0; i < num; i++) {
2832 snd_soc_dapm_del_route(dapm, route);
2833 route++;
2834 }
2835 mutex_unlock(&dapm->card->dapm_mutex);
2836
2837 return ret;
2838}
2839EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2840
Mark Brownbf3a9e12011-06-13 16:42:29 +01002841static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2842 const struct snd_soc_dapm_route *route)
2843{
2844 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2845 route->source,
2846 true);
2847 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2848 route->sink,
2849 true);
2850 struct snd_soc_dapm_path *path;
2851 int count = 0;
2852
2853 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002854 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002855 route->source);
2856 return -ENODEV;
2857 }
2858
2859 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002860 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002861 route->sink);
2862 return -ENODEV;
2863 }
2864
2865 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002866 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002867 route->source, route->sink);
2868
2869 list_for_each_entry(path, &source->sinks, list_source) {
2870 if (path->sink == sink) {
2871 path->weak = 1;
2872 count++;
2873 }
2874 }
2875
2876 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002877 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002878 route->source, route->sink);
2879 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002880 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002881 count, route->source, route->sink);
2882
2883 return 0;
2884}
2885
2886/**
2887 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2888 * @dapm: DAPM context
2889 * @route: audio routes
2890 * @num: number of routes
2891 *
2892 * Mark existing routes matching those specified in the passed array
2893 * as being weak, meaning that they are ignored for the purpose of
2894 * power decisions. The main intended use case is for sidetone paths
2895 * which couple audio between other independent paths if they are both
2896 * active in order to make the combination work better at the user
2897 * level but which aren't intended to be "used".
2898 *
2899 * Note that CODEC drivers should not use this as sidetone type paths
2900 * can frequently also be used as bypass paths.
2901 */
2902int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2903 const struct snd_soc_dapm_route *route, int num)
2904{
2905 int i, err;
2906 int ret = 0;
2907
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002908 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002909 for (i = 0; i < num; i++) {
2910 err = snd_soc_dapm_weak_route(dapm, route);
2911 if (err)
2912 ret = err;
2913 route++;
2914 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002915 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002916
2917 return ret;
2918}
2919EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2920
Mark Brown105f1c22008-05-13 14:52:19 +02002921/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002922 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002923 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002924 *
2925 * Checks the codec for any new dapm widgets and creates them if found.
2926 *
2927 * Returns 0 for success.
2928 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002929int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002930{
2931 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002932 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002933
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002934 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002935
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002936 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002937 {
2938 if (w->new)
2939 continue;
2940
Stephen Warrenfad59882011-04-28 17:37:59 -06002941 if (w->num_kcontrols) {
2942 w->kcontrols = kzalloc(w->num_kcontrols *
2943 sizeof(struct snd_kcontrol *),
2944 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002945 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002946 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002947 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002948 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002949 }
2950
Richard Purdie2b97eab2006-10-06 18:32:18 +02002951 switch(w->id) {
2952 case snd_soc_dapm_switch:
2953 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002954 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002955 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002956 break;
2957 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02002958 case snd_soc_dapm_demux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002959 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002960 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002961 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002962 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002963 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002964 break;
Nikesh Oswalc6615082015-02-02 17:06:44 +00002965 case snd_soc_dapm_dai_link:
2966 dapm_new_dai_link(w);
2967 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002968 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002969 break;
2970 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002971
2972 /* Read the initial power state from the device */
2973 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002974 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002975 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002976 val &= w->mask;
2977 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002978 w->power = 1;
2979 }
2980
Richard Purdie2b97eab2006-10-06 18:32:18 +02002981 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002982
Mark Brown7508b122011-10-05 12:09:12 +01002983 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002984 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002985 }
2986
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002987 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2988 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002989 return 0;
2990}
2991EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2992
2993/**
2994 * snd_soc_dapm_get_volsw - dapm mixer get callback
2995 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002996 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002997 *
2998 * Callback to get the value of a dapm mixer control.
2999 *
3000 * Returns 0 for success.
3001 */
3002int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3003 struct snd_ctl_elem_value *ucontrol)
3004{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003005 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3006 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003007 struct soc_mixer_control *mc =
3008 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003009 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003010 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003011 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003012 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003013 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003014 unsigned int val;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003015 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003016
3017 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003018 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003019 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003020 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003021
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003022 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003023 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3024 ret = soc_dapm_read(dapm, reg, &val);
3025 val = (val >> shift) & mask;
3026 } else {
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003027 val = dapm_kcontrol_get_value(kcontrol);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003028 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003029 mutex_unlock(&card->dapm_mutex);
3030
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003031 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003032 ucontrol->value.integer.value[0] = max - val;
3033 else
3034 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003035
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003036 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003037}
3038EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3039
3040/**
3041 * snd_soc_dapm_put_volsw - dapm mixer set callback
3042 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003043 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003044 *
3045 * Callback to set the value of a dapm mixer control.
3046 *
3047 * Returns 0 for success.
3048 */
3049int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3050 struct snd_ctl_elem_value *ucontrol)
3051{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003052 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3053 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003054 struct soc_mixer_control *mc =
3055 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003056 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003057 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003058 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003059 unsigned int mask = (1 << fls(max)) - 1;
3060 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07003061 unsigned int val;
Jarkko Nikula18626c72014-06-09 14:20:29 +03003062 int connect, change, reg_change = 0;
Mark Brown97404f22010-12-14 16:13:57 +00003063 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08003064 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003065
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003066 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003067 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003068 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02003069 kcontrol->id.name);
3070
Richard Purdie2b97eab2006-10-06 18:32:18 +02003071 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02003072 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003073
3074 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003075 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003076
Liam Girdwood3cd04342012-03-09 12:02:08 +00003077 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003078
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003079 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown283375c2009-12-07 18:09:03 +00003080
Jarkko Nikula18626c72014-06-09 14:20:29 +03003081 if (reg != SND_SOC_NOPM) {
3082 mask = mask << shift;
3083 val = val << shift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02003084
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003085 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
Jarkko Nikula18626c72014-06-09 14:20:29 +03003086 }
3087
3088 if (change || reg_change) {
3089 if (reg_change) {
3090 update.kcontrol = kcontrol;
3091 update.reg = reg;
3092 update.mask = mask;
3093 update.val = val;
3094 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02003095 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03003096 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00003097
Nenghua Cao52765972013-12-13 20:13:49 +08003098 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00003099
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003100 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00003101 }
3102
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003103 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003104
3105 if (ret > 0)
3106 soc_dpcm_runtime_update(card);
3107
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003108 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003109}
3110EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3111
3112/**
3113 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3114 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003115 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003116 *
3117 * Callback to get the value of a dapm enumerated double mixer control.
3118 *
3119 * Returns 0 for success.
3120 */
3121int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3122 struct snd_ctl_elem_value *ucontrol)
3123{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003124 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Charles Keepax561ed682015-05-01 12:37:26 +01003125 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003126 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003127 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003128
Charles Keepax561ed682015-05-01 12:37:26 +01003129 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3130 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003131 int ret = soc_dapm_read(dapm, e->reg, &reg_val);
Charles Keepax964a0b82015-05-08 10:50:10 +01003132 if (ret) {
3133 mutex_unlock(&card->dapm_mutex);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003134 return ret;
Charles Keepax964a0b82015-05-08 10:50:10 +01003135 }
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003136 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003137 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003138 }
Charles Keepax561ed682015-05-01 12:37:26 +01003139 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003140
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003141 val = (reg_val >> e->shift_l) & e->mask;
3142 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3143 if (e->shift_l != e->shift_r) {
3144 val = (reg_val >> e->shift_r) & e->mask;
3145 val = snd_soc_enum_val_to_item(e, val);
3146 ucontrol->value.enumerated.item[1] = val;
3147 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003148
Geert Uytterhoeven69128312014-08-08 17:29:35 +02003149 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003150}
3151EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3152
3153/**
3154 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3155 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003156 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02003157 *
3158 * Callback to set the value of a dapm enumerated double mixer control.
3159 *
3160 * Returns 0 for success.
3161 */
3162int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3163 struct snd_ctl_elem_value *ucontrol)
3164{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003165 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3166 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003167 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003168 unsigned int *item = ucontrol->value.enumerated.item;
Charles Keepax561ed682015-05-01 12:37:26 +01003169 unsigned int val, change, reg_change = 0;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003170 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00003171 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08003172 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003173
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003174 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003175 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003176
3177 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003178 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003179 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003180 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003181 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003182 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003183 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003184 }
3185
Liam Girdwood3cd04342012-03-09 12:02:08 +00003186 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003187
Charles Keepax561ed682015-05-01 12:37:26 +01003188 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00003189
Charles Keepax561ed682015-05-01 12:37:26 +01003190 if (e->reg != SND_SOC_NOPM)
3191 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3192
3193 if (change || reg_change) {
3194 if (reg_change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01003195 update.kcontrol = kcontrol;
3196 update.reg = e->reg;
3197 update.mask = mask;
3198 update.val = val;
3199 card->update = &update;
3200 }
Charles Keepax561ed682015-05-01 12:37:26 +01003201 change |= reg_change;
Mark Brown3a655772009-10-05 17:23:30 +01003202
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01003203 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003204
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003205 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003206 }
3207
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003208 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003209
3210 if (ret > 0)
3211 soc_dpcm_runtime_update(card);
3212
Mark Brown97404f22010-12-14 16:13:57 +00003213 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003214}
3215EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3216
3217/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003218 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3219 *
3220 * @kcontrol: mixer control
3221 * @uinfo: control element information
3222 *
3223 * Callback to provide information about a pin switch control.
3224 */
3225int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3226 struct snd_ctl_elem_info *uinfo)
3227{
3228 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3229 uinfo->count = 1;
3230 uinfo->value.integer.min = 0;
3231 uinfo->value.integer.max = 1;
3232
3233 return 0;
3234}
3235EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3236
3237/**
3238 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3239 *
3240 * @kcontrol: mixer control
3241 * @ucontrol: Value
3242 */
3243int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3244 struct snd_ctl_elem_value *ucontrol)
3245{
Mark Brown48a8c392012-02-14 17:11:15 -08003246 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003247 const char *pin = (const char *)kcontrol->private_value;
3248
Liam Girdwood3cd04342012-03-09 12:02:08 +00003249 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003250
3251 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003252 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003253
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003254 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003255
3256 return 0;
3257}
3258EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3259
3260/**
3261 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3262 *
3263 * @kcontrol: mixer control
3264 * @ucontrol: Value
3265 */
3266int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3267 struct snd_ctl_elem_value *ucontrol)
3268{
Mark Brown48a8c392012-02-14 17:11:15 -08003269 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003270 const char *pin = (const char *)kcontrol->private_value;
3271
Mark Brown8b37dbd2009-02-28 21:14:20 +00003272 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003273 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003274 else
Mark Brown48a8c392012-02-14 17:11:15 -08003275 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003276
Mark Brown48a8c392012-02-14 17:11:15 -08003277 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003278 return 0;
3279}
3280EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3281
Liam Girdwoodcc76e7d2015-06-04 15:13:09 +01003282struct snd_soc_dapm_widget *
Mark Brown5ba06fc2012-02-16 11:07:13 -08003283snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003284 const struct snd_soc_dapm_widget *widget)
3285{
3286 struct snd_soc_dapm_widget *w;
3287
3288 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3289 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3290 if (!w)
3291 dev_err(dapm->dev,
3292 "ASoC: Failed to create DAPM control %s\n",
3293 widget->name);
3294
3295 mutex_unlock(&dapm->card->dapm_mutex);
3296 return w;
3297}
3298
3299struct snd_soc_dapm_widget *
3300snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003301 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003302{
3303 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003304 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00003305 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003306
3307 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003308 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003309
Mark Brown62ea8742012-01-21 21:14:48 +00003310 switch (w->id) {
3311 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003312 w->regulator = devm_regulator_get(dapm->dev, w->name);
3313 if (IS_ERR(w->regulator)) {
3314 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003315 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003316 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003317 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003318 }
Mark Brown8784c772013-01-10 19:33:47 +00003319
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003320 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003321 ret = regulator_allow_bypass(w->regulator, true);
3322 if (ret != 0)
3323 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003324 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003325 w->name, ret);
3326 }
Mark Brown62ea8742012-01-21 21:14:48 +00003327 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003328 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003329#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003330 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003331 if (IS_ERR(w->clk)) {
3332 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003333 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003334 w->name, ret);
3335 return NULL;
3336 }
Mark Brownec029952012-06-04 08:16:20 +01003337#else
3338 return NULL;
3339#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003340 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003341 default:
3342 break;
3343 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003344
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003345 prefix = soc_dapm_prefix(dapm);
Koro Chenfdb6eb02015-05-11 10:36:53 +08003346 if (prefix) {
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003347 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Koro Chenfdb6eb02015-05-11 10:36:53 +08003348 if (widget->sname)
3349 w->sname = kasprintf(GFP_KERNEL, "%s %s", prefix,
3350 widget->sname);
3351 } else {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003352 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
Koro Chenfdb6eb02015-05-11 10:36:53 +08003353 if (widget->sname)
3354 w->sname = kasprintf(GFP_KERNEL, "%s", widget->sname);
3355 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003356 if (w->name == NULL) {
3357 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003358 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003359 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003360
Mark Brown7ca3a182011-10-08 14:04:50 +01003361 switch (w->id) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003362 case snd_soc_dapm_mic:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003363 w->is_source = 1;
3364 w->power_check = dapm_generic_check_power;
3365 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003366 case snd_soc_dapm_input:
3367 if (!dapm->card->fully_routed)
3368 w->is_source = 1;
3369 w->power_check = dapm_generic_check_power;
3370 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003371 case snd_soc_dapm_spk:
3372 case snd_soc_dapm_hp:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003373 w->is_sink = 1;
3374 w->power_check = dapm_generic_check_power;
3375 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003376 case snd_soc_dapm_output:
3377 if (!dapm->card->fully_routed)
3378 w->is_sink = 1;
3379 w->power_check = dapm_generic_check_power;
3380 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003381 case snd_soc_dapm_vmid:
3382 case snd_soc_dapm_siggen:
3383 w->is_source = 1;
3384 w->power_check = dapm_always_on_check_power;
3385 break;
3386 case snd_soc_dapm_mux:
Lars-Peter Clausend714f972015-05-01 18:02:43 +02003387 case snd_soc_dapm_demux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003388 case snd_soc_dapm_switch:
3389 case snd_soc_dapm_mixer:
3390 case snd_soc_dapm_mixer_named_ctl:
Mark Brown63c69a62013-07-18 22:03:01 +01003391 case snd_soc_dapm_adc:
3392 case snd_soc_dapm_aif_out:
3393 case snd_soc_dapm_dac:
3394 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003395 case snd_soc_dapm_pga:
3396 case snd_soc_dapm_out_drv:
Mark Brown7ca3a182011-10-08 14:04:50 +01003397 case snd_soc_dapm_micbias:
Mark Brown7ca3a182011-10-08 14:04:50 +01003398 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003399 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02003400 case snd_soc_dapm_dai_out:
3401 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003402 w->power_check = dapm_generic_check_power;
3403 break;
3404 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003405 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003406 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003407 case snd_soc_dapm_kcontrol:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003408 w->is_supply = 1;
Mark Brown7ca3a182011-10-08 14:04:50 +01003409 w->power_check = dapm_supply_check_power;
3410 break;
3411 default:
3412 w->power_check = dapm_always_on_check_power;
3413 break;
3414 }
3415
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003416 w->dapm = dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003417 INIT_LIST_HEAD(&w->sources);
3418 INIT_LIST_HEAD(&w->sinks);
3419 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003420 INIT_LIST_HEAD(&w->dirty);
Lars-Peter Clausen92fa1242015-05-01 18:02:42 +02003421 list_add_tail(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003422
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003423 w->inputs = -1;
3424 w->outputs = -1;
3425
Richard Purdie2b97eab2006-10-06 18:32:18 +02003426 /* machine layer set ups unconnected pins and insertions */
3427 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003428 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003429}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003430
3431/**
Mark Brown4ba13272008-05-13 14:51:19 +02003432 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003433 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003434 * @widget: widget array
3435 * @num: number of widgets
3436 *
3437 * Creates new DAPM controls based upon the templates.
3438 *
3439 * Returns 0 for success else error.
3440 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003441int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003442 const struct snd_soc_dapm_widget *widget,
3443 int num)
3444{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003445 struct snd_soc_dapm_widget *w;
3446 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003447 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003448
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003449 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003450 for (i = 0; i < num; i++) {
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003451 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003452 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003453 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003454 "ASoC: Failed to create DAPM control %s\n",
3455 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003456 ret = -ENOMEM;
3457 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003458 }
Mark Brown4ba13272008-05-13 14:51:19 +02003459 widget++;
3460 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003461 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003462 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003463}
3464EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3465
Mark Brownc74184e2012-04-04 22:12:09 +01003466static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3467 struct snd_kcontrol *kcontrol, int event)
3468{
3469 struct snd_soc_dapm_path *source_p, *sink_p;
3470 struct snd_soc_dai *source, *sink;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003471 const struct snd_soc_pcm_stream *config = w->params + w->params_select;
Mark Brownc74184e2012-04-04 22:12:09 +01003472 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003473 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003474 u64 fmt;
3475 int ret;
3476
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003477 if (WARN_ON(!config) ||
3478 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3479 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003480
3481 /* We only support a single source and sink, pick the first */
3482 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3483 list_sink);
3484 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3485 list_source);
3486
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003487 if (WARN_ON(!source_p || !sink_p) ||
3488 WARN_ON(!sink_p->source || !source_p->sink) ||
3489 WARN_ON(!source_p->source || !sink_p->sink))
3490 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003491
3492 source = source_p->source->priv;
3493 sink = sink_p->sink->priv;
3494
3495 /* Be a little careful as we don't want to overflow the mask array */
3496 if (config->formats) {
3497 fmt = ffs(config->formats) - 1;
3498 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003499 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003500 config->formats);
3501 fmt = 0;
3502 }
3503
3504 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003505 params = kzalloc(sizeof(*params), GFP_KERNEL);
3506 if (!params) {
3507 ret = -ENOMEM;
3508 goto out;
3509 }
3510 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003511
Mark Brown9747cec2012-04-26 19:12:21 +01003512 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003513 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003514 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003515 config->rate_max;
3516
Mark Brown9747cec2012-04-26 19:12:21 +01003517 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003518 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003519 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003520 = config->channels_max;
3521
3522 memset(&substream, 0, sizeof(substream));
3523
3524 switch (event) {
3525 case SND_SOC_DAPM_PRE_PMU:
Benoit Cousson93e69582014-07-08 23:19:38 +02003526 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3527 ret = soc_dai_hw_params(&substream, params, source);
3528 if (ret < 0)
3529 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003530
Benoit Cousson93e69582014-07-08 23:19:38 +02003531 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3532 ret = soc_dai_hw_params(&substream, params, sink);
3533 if (ret < 0)
3534 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003535 break;
3536
3537 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003538 ret = snd_soc_dai_digital_mute(sink, 0,
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 unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003542 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003543 break;
3544
3545 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003546 ret = snd_soc_dai_digital_mute(sink, 1,
3547 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003548 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003549 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003550 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003551 break;
3552
3553 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003554 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003555 return -EINVAL;
3556 }
3557
Mark Brown9747cec2012-04-26 19:12:21 +01003558out:
3559 kfree(params);
3560 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003561}
3562
Nikesh Oswalc6615082015-02-02 17:06:44 +00003563static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3564 struct snd_ctl_elem_value *ucontrol)
3565{
3566 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3567
3568 ucontrol->value.integer.value[0] = w->params_select;
3569
3570 return 0;
3571}
3572
3573static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3574 struct snd_ctl_elem_value *ucontrol)
3575{
3576 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3577
3578 /* Can't change the config when widget is already powered */
3579 if (w->power)
3580 return -EBUSY;
3581
3582 if (ucontrol->value.integer.value[0] == w->params_select)
3583 return 0;
3584
3585 if (ucontrol->value.integer.value[0] >= w->num_params)
3586 return -EINVAL;
3587
3588 w->params_select = ucontrol->value.integer.value[0];
3589
3590 return 0;
3591}
3592
Mark Brownc74184e2012-04-04 22:12:09 +01003593int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3594 const struct snd_soc_pcm_stream *params,
Nikesh Oswalc6615082015-02-02 17:06:44 +00003595 unsigned int num_params,
Mark Brownc74184e2012-04-04 22:12:09 +01003596 struct snd_soc_dapm_widget *source,
3597 struct snd_soc_dapm_widget *sink)
3598{
Mark Brownc74184e2012-04-04 22:12:09 +01003599 struct snd_soc_dapm_widget template;
3600 struct snd_soc_dapm_widget *w;
Mark Brownc74184e2012-04-04 22:12:09 +01003601 char *link_name;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003602 int ret, count;
3603 unsigned long private_value;
3604 const char **w_param_text;
3605 struct soc_enum w_param_enum[] = {
3606 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3607 };
3608 struct snd_kcontrol_new kcontrol_dai_link[] = {
3609 SOC_ENUM_EXT(NULL, w_param_enum[0],
3610 snd_soc_dapm_dai_link_get,
3611 snd_soc_dapm_dai_link_put),
3612 };
3613 const struct snd_soc_pcm_stream *config = params;
Mark Brownc74184e2012-04-04 22:12:09 +01003614
Nikesh Oswalc6615082015-02-02 17:06:44 +00003615 w_param_text = devm_kcalloc(card->dev, num_params,
3616 sizeof(char *), GFP_KERNEL);
3617 if (!w_param_text)
Mark Brownc74184e2012-04-04 22:12:09 +01003618 return -ENOMEM;
Mark Brownc74184e2012-04-04 22:12:09 +01003619
Charles Keepax46172b62015-03-25 11:22:35 +00003620 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3621 source->name, sink->name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003622 if (!link_name) {
3623 ret = -ENOMEM;
3624 goto outfree_w_param;
3625 }
Mark Brownc74184e2012-04-04 22:12:09 +01003626
Nikesh Oswalc6615082015-02-02 17:06:44 +00003627 for (count = 0 ; count < num_params; count++) {
3628 if (!config->stream_name) {
3629 dev_warn(card->dapm.dev,
3630 "ASoC: anonymous config %d for dai link %s\n",
3631 count, link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003632 w_param_text[count] =
Charles Keepax46172b62015-03-25 11:22:35 +00003633 devm_kasprintf(card->dev, GFP_KERNEL,
3634 "Anonymous Configuration %d",
3635 count);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003636 if (!w_param_text[count]) {
3637 ret = -ENOMEM;
3638 goto outfree_link_name;
3639 }
Nikesh Oswalc6615082015-02-02 17:06:44 +00003640 } else {
3641 w_param_text[count] = devm_kmemdup(card->dev,
3642 config->stream_name,
3643 strlen(config->stream_name) + 1,
3644 GFP_KERNEL);
3645 if (!w_param_text[count]) {
3646 ret = -ENOMEM;
3647 goto outfree_link_name;
3648 }
3649 }
3650 config++;
3651 }
3652 w_param_enum[0].items = num_params;
3653 w_param_enum[0].texts = w_param_text;
Mark Brownc74184e2012-04-04 22:12:09 +01003654
3655 memset(&template, 0, sizeof(template));
3656 template.reg = SND_SOC_NOPM;
3657 template.id = snd_soc_dapm_dai_link;
3658 template.name = link_name;
3659 template.event = snd_soc_dai_link_event;
3660 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3661 SND_SOC_DAPM_PRE_PMD;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003662 template.num_kcontrols = 1;
3663 /* duplicate w_param_enum on heap so that memory persists */
3664 private_value =
3665 (unsigned long) devm_kmemdup(card->dev,
3666 (void *)(kcontrol_dai_link[0].private_value),
3667 sizeof(struct soc_enum), GFP_KERNEL);
3668 if (!private_value) {
3669 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3670 link_name);
3671 ret = -ENOMEM;
3672 goto outfree_link_name;
3673 }
3674 kcontrol_dai_link[0].private_value = private_value;
3675 /* duplicate kcontrol_dai_link on heap so that memory persists */
3676 template.kcontrol_news =
3677 devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3678 sizeof(struct snd_kcontrol_new),
3679 GFP_KERNEL);
3680 if (!template.kcontrol_news) {
3681 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3682 link_name);
3683 ret = -ENOMEM;
3684 goto outfree_private_value;
3685 }
Mark Brownc74184e2012-04-04 22:12:09 +01003686
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003687 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003688
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003689 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
Mark Brownc74184e2012-04-04 22:12:09 +01003690 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003691 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003692 link_name);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003693 ret = -ENOMEM;
3694 goto outfree_kcontrol_news;
Mark Brownc74184e2012-04-04 22:12:09 +01003695 }
3696
3697 w->params = params;
Nikesh Oswalc6615082015-02-02 17:06:44 +00003698 w->num_params = num_params;
Mark Brownc74184e2012-04-04 22:12:09 +01003699
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003700 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3701 if (ret)
Nikesh Oswalc6615082015-02-02 17:06:44 +00003702 goto outfree_w;
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003703 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
Nikesh Oswalc6615082015-02-02 17:06:44 +00003704
3705outfree_w:
3706 devm_kfree(card->dev, w);
3707outfree_kcontrol_news:
3708 devm_kfree(card->dev, (void *)template.kcontrol_news);
3709outfree_private_value:
3710 devm_kfree(card->dev, (void *)private_value);
3711outfree_link_name:
3712 devm_kfree(card->dev, link_name);
3713outfree_w_param:
3714 for (count = 0 ; count < num_params; count++)
3715 devm_kfree(card->dev, (void *)w_param_text[count]);
3716 devm_kfree(card->dev, w_param_text);
3717
3718 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003719}
3720
Mark Brown888df392012-02-16 19:37:51 -08003721int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3722 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003723{
Mark Brown888df392012-02-16 19:37:51 -08003724 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003725 struct snd_soc_dapm_widget *w;
3726
Mark Brown888df392012-02-16 19:37:51 -08003727 WARN_ON(dapm->dev != dai->dev);
3728
3729 memset(&template, 0, sizeof(template));
3730 template.reg = SND_SOC_NOPM;
3731
3732 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003733 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003734 template.name = dai->driver->playback.stream_name;
3735 template.sname = dai->driver->playback.stream_name;
3736
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003737 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003738 template.name);
3739
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003740 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Mark Brown888df392012-02-16 19:37:51 -08003741 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003742 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003743 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003744 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003745 }
3746
3747 w->priv = dai;
3748 dai->playback_widget = w;
3749 }
3750
3751 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003752 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003753 template.name = dai->driver->capture.stream_name;
3754 template.sname = dai->driver->capture.stream_name;
3755
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003756 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003757 template.name);
3758
Liam Girdwood02aa78a2015-05-25 18:21:17 +01003759 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
Mark Brown888df392012-02-16 19:37:51 -08003760 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003761 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003762 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003763 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003764 }
3765
3766 w->priv = dai;
3767 dai->capture_widget = w;
3768 }
3769
3770 return 0;
3771}
3772
3773int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3774{
3775 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003776 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08003777 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003778
3779 /* For each DAI widget... */
3780 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003781 switch (dai_w->id) {
3782 case snd_soc_dapm_dai_in:
3783 case snd_soc_dapm_dai_out:
3784 break;
3785 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003786 continue;
Mark Brown46162742013-06-05 19:36:11 +01003787 }
Mark Brown888df392012-02-16 19:37:51 -08003788
3789 dai = dai_w->priv;
3790
3791 /* ...find all widgets with the same stream and link them */
3792 list_for_each_entry(w, &card->widgets, list) {
3793 if (w->dapm != dai_w->dapm)
3794 continue;
3795
Mark Brown46162742013-06-05 19:36:11 +01003796 switch (w->id) {
3797 case snd_soc_dapm_dai_in:
3798 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003799 continue;
Mark Brown46162742013-06-05 19:36:11 +01003800 default:
3801 break;
3802 }
Mark Brown888df392012-02-16 19:37:51 -08003803
Russell King19c2c5f2013-08-04 20:24:03 +01003804 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003805 continue;
3806
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003807 if (dai_w->id == snd_soc_dapm_dai_in) {
3808 src = dai_w;
3809 sink = w;
3810 } else {
3811 src = w;
3812 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08003813 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003814 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3815 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003816 }
3817 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003818
Mark Brown888df392012-02-16 19:37:51 -08003819 return 0;
3820}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003821
Benoit Cousson44ba2642014-07-08 23:19:36 +02003822static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3823 struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003824{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003825 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003826 struct snd_soc_dapm_widget *sink, *source;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003827 int i;
3828
Benoit Cousson44ba2642014-07-08 23:19:36 +02003829 for (i = 0; i < rtd->num_codecs; i++) {
3830 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003831
3832 /* there is no point in connecting BE DAI links with dummies */
3833 if (snd_soc_dai_is_dummy(codec_dai) ||
3834 snd_soc_dai_is_dummy(cpu_dai))
3835 continue;
3836
3837 /* connect BE DAI playback if widgets are valid */
3838 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003839 source = cpu_dai->playback_widget;
3840 sink = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003841 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003842 cpu_dai->component->name, source->name,
3843 codec_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003844
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003845 snd_soc_dapm_add_path(&card->dapm, source, sink,
3846 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003847 }
3848
3849 /* connect BE DAI capture if widgets are valid */
3850 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003851 source = codec_dai->capture_widget;
3852 sink = cpu_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003853 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003854 codec_dai->component->name, source->name,
3855 cpu_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003856
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003857 snd_soc_dapm_add_path(&card->dapm, source, sink,
3858 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003859 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003860 }
3861}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003862
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003863static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3864 int event)
3865{
3866 struct snd_soc_dapm_widget *w;
3867
3868 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3869 w = dai->playback_widget;
3870 else
3871 w = dai->capture_widget;
3872
3873 if (w) {
3874 dapm_mark_dirty(w, "stream event");
3875
3876 switch (event) {
3877 case SND_SOC_DAPM_STREAM_START:
3878 w->active = 1;
3879 break;
3880 case SND_SOC_DAPM_STREAM_STOP:
3881 w->active = 0;
3882 break;
3883 case SND_SOC_DAPM_STREAM_SUSPEND:
3884 case SND_SOC_DAPM_STREAM_RESUME:
3885 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3886 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3887 break;
3888 }
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003889
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003890 if (w->id == snd_soc_dapm_dai_in) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003891 w->is_source = w->active;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003892 dapm_widget_invalidate_input_paths(w);
3893 } else {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003894 w->is_sink = w->active;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003895 dapm_widget_invalidate_output_paths(w);
3896 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003897 }
3898}
3899
Benoit Cousson44ba2642014-07-08 23:19:36 +02003900void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3901{
3902 struct snd_soc_pcm_runtime *rtd = card->rtd;
3903 int i;
3904
3905 /* for each BE DAI link... */
3906 for (i = 0; i < card->num_rtd; i++) {
3907 rtd = &card->rtd[i];
3908
3909 /*
3910 * dynamic FE links have no fixed DAI mapping.
3911 * CODEC<->CODEC links have no direct connection.
3912 */
3913 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3914 continue;
3915
3916 dapm_connect_dai_link_widgets(card, rtd);
3917 }
3918}
3919
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003920static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3921 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003922{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003923 int i;
3924
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003925 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
Benoit Cousson44ba2642014-07-08 23:19:36 +02003926 for (i = 0; i < rtd->num_codecs; i++)
3927 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003928
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003929 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003930}
3931
3932/**
3933 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3934 * @rtd: PCM runtime data
3935 * @stream: stream name
3936 * @event: stream event
3937 *
3938 * Sends a stream event to the dapm core. The core then makes any
3939 * necessary widget power changes.
3940 *
3941 * Returns 0 for success else error.
3942 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003943void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3944 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003945{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003946 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003947
Liam Girdwood3cd04342012-03-09 12:02:08 +00003948 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003949 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003950 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003951}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003952
3953/**
Charles Keepax11391102014-02-18 15:22:14 +00003954 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3955 * @dapm: DAPM context
3956 * @pin: pin name
3957 *
3958 * Enables input/output pin and its parents or children widgets iff there is
3959 * a valid audio route and active audio stream.
3960 *
3961 * Requires external locking.
3962 *
3963 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3964 * do any widget power switching.
3965 */
3966int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3967 const char *pin)
3968{
3969 return snd_soc_dapm_set_pin(dapm, pin, 1);
3970}
3971EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3972
3973/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003974 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003975 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003976 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003977 *
Mark Brown74b8f952009-06-06 11:26:15 +01003978 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003979 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003980 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003981 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3982 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003983 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003984int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003985{
Charles Keepax11391102014-02-18 15:22:14 +00003986 int ret;
3987
3988 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3989
3990 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3991
3992 mutex_unlock(&dapm->card->dapm_mutex);
3993
3994 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003995}
Liam Girdwooda5302182008-07-07 13:35:17 +01003996EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003997
3998/**
Charles Keepax11391102014-02-18 15:22:14 +00003999 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4000 * @dapm: DAPM context
4001 * @pin: pin name
4002 *
4003 * Enables input/output pin regardless of any other state. This is
4004 * intended for use with microphone bias supplies used in microphone
4005 * jack detection.
4006 *
4007 * Requires external locking.
4008 *
4009 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4010 * do any widget power switching.
4011 */
4012int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4013 const char *pin)
4014{
4015 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4016
4017 if (!w) {
4018 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4019 return -EINVAL;
4020 }
4021
4022 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02004023 if (!w->connected) {
4024 /*
4025 * w->force does not affect the number of input or output paths,
4026 * so we only have to recheck if w->connected is changed
4027 */
4028 dapm_widget_invalidate_input_paths(w);
4029 dapm_widget_invalidate_output_paths(w);
4030 w->connected = 1;
4031 }
Charles Keepax11391102014-02-18 15:22:14 +00004032 w->force = 1;
4033 dapm_mark_dirty(w, "force enable");
4034
4035 return 0;
4036}
4037EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4038
4039/**
Mark Brownda341832010-03-15 19:23:37 +00004040 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004041 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00004042 * @pin: pin name
4043 *
4044 * Enables input/output pin regardless of any other state. This is
4045 * intended for use with microphone bias supplies used in microphone
4046 * jack detection.
4047 *
4048 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4049 * do any widget power switching.
4050 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004051int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4052 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00004053{
Charles Keepax11391102014-02-18 15:22:14 +00004054 int ret;
Mark Brownda341832010-03-15 19:23:37 +00004055
Charles Keepax11391102014-02-18 15:22:14 +00004056 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00004057
Charles Keepax11391102014-02-18 15:22:14 +00004058 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09004059
Charles Keepax11391102014-02-18 15:22:14 +00004060 mutex_unlock(&dapm->card->dapm_mutex);
4061
4062 return ret;
Mark Brownda341832010-03-15 19:23:37 +00004063}
4064EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4065
4066/**
Charles Keepax11391102014-02-18 15:22:14 +00004067 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4068 * @dapm: DAPM context
4069 * @pin: pin name
4070 *
4071 * Disables input/output pin and its parents or children widgets.
4072 *
4073 * Requires external locking.
4074 *
4075 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4076 * do any widget power switching.
4077 */
4078int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4079 const char *pin)
4080{
4081 return snd_soc_dapm_set_pin(dapm, pin, 0);
4082}
4083EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4084
4085/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004086 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004087 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004088 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004089 *
Mark Brown74b8f952009-06-06 11:26:15 +01004090 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00004091 *
Liam Girdwooda5302182008-07-07 13:35:17 +01004092 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4093 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004094 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004095int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4096 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01004097{
Charles Keepax11391102014-02-18 15:22:14 +00004098 int ret;
4099
4100 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4101
4102 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4103
4104 mutex_unlock(&dapm->card->dapm_mutex);
4105
4106 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01004107}
4108EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4109
4110/**
Charles Keepax11391102014-02-18 15:22:14 +00004111 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4112 * @dapm: DAPM context
4113 * @pin: pin name
4114 *
4115 * Marks the specified pin as being not connected, disabling it along
4116 * any parent or child widgets. At present this is identical to
4117 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4118 * additional things such as disabling controls which only affect
4119 * paths through the pin.
4120 *
4121 * Requires external locking.
4122 *
4123 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4124 * do any widget power switching.
4125 */
4126int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4127 const char *pin)
4128{
4129 return snd_soc_dapm_set_pin(dapm, pin, 0);
4130}
4131EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4132
4133/**
Mark Brown5817b522008-09-24 11:23:11 +01004134 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004135 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01004136 * @pin: pin name
4137 *
4138 * Marks the specified pin as being not connected, disabling it along
4139 * any parent or child widgets. At present this is identical to
4140 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4141 * additional things such as disabling controls which only affect
4142 * paths through the pin.
4143 *
4144 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4145 * do any widget power switching.
4146 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004147int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01004148{
Charles Keepax11391102014-02-18 15:22:14 +00004149 int ret;
4150
4151 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4152
4153 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4154
4155 mutex_unlock(&dapm->card->dapm_mutex);
4156
4157 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01004158}
4159EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4160
4161/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004162 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004163 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004164 * @pin: audio signal pin endpoint (or start point)
4165 *
4166 * Get audio pin status - connected or disconnected.
4167 *
4168 * Returns 1 for connected otherwise 0.
4169 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004170int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4171 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004172{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004173 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004174
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004175 if (w)
4176 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06004177
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004178 return 0;
4179}
Liam Girdwooda5302182008-07-07 13:35:17 +01004180EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004181
4182/**
Mark Brown1547aba2010-05-07 21:11:40 +01004183 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004184 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01004185 * @pin: audio signal pin endpoint (or start point)
4186 *
4187 * Mark the given endpoint or pin as ignoring suspend. When the
4188 * system is disabled a path between two endpoints flagged as ignoring
4189 * suspend will not be disabled. The path must already be enabled via
4190 * normal means at suspend time, it will not be turned on if it was not
4191 * already enabled.
4192 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004193int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4194 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01004195{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004196 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01004197
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004198 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004199 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004200 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01004201 }
4202
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004203 w->ignore_suspend = 1;
4204
4205 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01004206}
4207EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4208
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02004209/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02004210 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03004211 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02004212 *
4213 * Free all dapm widgets and resources.
4214 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004215void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004216{
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02004217 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004218 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02004219 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004220}
4221EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4222
Xiang Xiao57996352014-03-02 00:04:02 +08004223static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01004224{
Liam Girdwood01005a72012-07-06 16:57:05 +01004225 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01004226 struct snd_soc_dapm_widget *w;
4227 LIST_HEAD(down_list);
4228 int powerdown = 0;
4229
Liam Girdwood01005a72012-07-06 16:57:05 +01004230 mutex_lock(&card->dapm_mutex);
4231
Jarkko Nikula97c866d2010-12-14 12:18:31 +02004232 list_for_each_entry(w, &dapm->card->widgets, list) {
4233 if (w->dapm != dapm)
4234 continue;
Mark Brown51737472009-06-22 13:16:51 +01004235 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004236 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01004237 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004238 powerdown = 1;
4239 }
4240 }
4241
4242 /* If there were no widgets to power down we're already in
4243 * standby.
4244 */
4245 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004246 if (dapm->bias_level == SND_SOC_BIAS_ON)
4247 snd_soc_dapm_set_bias_level(dapm,
4248 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004249 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004250 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4251 snd_soc_dapm_set_bias_level(dapm,
4252 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004253 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004254
4255 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004256}
Mark Brown51737472009-06-22 13:16:51 +01004257
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004258/*
4259 * snd_soc_dapm_shutdown - callback for system shutdown
4260 */
4261void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4262{
Xiang Xiao57996352014-03-02 00:04:02 +08004263 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004264
Xiang Xiao57996352014-03-02 00:04:02 +08004265 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08004266 if (dapm != &card->dapm) {
4267 soc_dapm_shutdown_dapm(dapm);
4268 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4269 snd_soc_dapm_set_bias_level(dapm,
4270 SND_SOC_BIAS_OFF);
4271 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004272 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08004273
4274 soc_dapm_shutdown_dapm(&card->dapm);
4275 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4276 snd_soc_dapm_set_bias_level(&card->dapm,
4277 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01004278}
4279
Richard Purdie2b97eab2006-10-06 18:32:18 +02004280/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004281MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004282MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4283MODULE_LICENSE("GPL");