blob: 1fd2d458824edd1e814d0b91f718ec231667efab [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));
55static struct snd_soc_dapm_widget *
56snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
57 const struct snd_soc_dapm_widget *widget);
58
Richard Purdie2b97eab2006-10-06 18:32:18 +020059/* dapm power sequences - make this per codec in the future */
60static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010061 [snd_soc_dapm_pre] = 0,
Mark Brown62ea8742012-01-21 21:14:48 +000062 [snd_soc_dapm_regulator_supply] = 1,
Ola Liljad7e7eb92012-05-24 15:26:25 +020063 [snd_soc_dapm_clock_supply] = 1,
Mark Brown1dd275b2013-10-09 13:56:37 +010064 [snd_soc_dapm_supply] = 2,
65 [snd_soc_dapm_micbias] = 3,
Mark Brownc74184e2012-04-04 22:12:09 +010066 [snd_soc_dapm_dai_link] = 2,
Mark Brown1dd275b2013-10-09 13:56:37 +010067 [snd_soc_dapm_dai_in] = 4,
68 [snd_soc_dapm_dai_out] = 4,
69 [snd_soc_dapm_aif_in] = 4,
70 [snd_soc_dapm_aif_out] = 4,
71 [snd_soc_dapm_mic] = 5,
72 [snd_soc_dapm_mux] = 6,
Mark Brown1dd275b2013-10-09 13:56:37 +010073 [snd_soc_dapm_dac] = 7,
74 [snd_soc_dapm_switch] = 8,
75 [snd_soc_dapm_mixer] = 8,
76 [snd_soc_dapm_mixer_named_ctl] = 8,
77 [snd_soc_dapm_pga] = 9,
78 [snd_soc_dapm_adc] = 10,
79 [snd_soc_dapm_out_drv] = 11,
80 [snd_soc_dapm_hp] = 11,
81 [snd_soc_dapm_spk] = 11,
82 [snd_soc_dapm_line] = 11,
83 [snd_soc_dapm_kcontrol] = 12,
84 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +020085};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000086
Richard Purdie2b97eab2006-10-06 18:32:18 +020087static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010088 [snd_soc_dapm_pre] = 0,
Lars-Peter Clausen57295072013-08-05 11:27:31 +020089 [snd_soc_dapm_kcontrol] = 1,
90 [snd_soc_dapm_adc] = 2,
91 [snd_soc_dapm_hp] = 3,
92 [snd_soc_dapm_spk] = 3,
93 [snd_soc_dapm_line] = 3,
94 [snd_soc_dapm_out_drv] = 3,
Mark Brown38357ab2009-06-06 19:03:23 +010095 [snd_soc_dapm_pga] = 4,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020096 [snd_soc_dapm_switch] = 5,
Mark Brown38357ab2009-06-06 19:03:23 +010097 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010098 [snd_soc_dapm_mixer] = 5,
99 [snd_soc_dapm_dac] = 6,
100 [snd_soc_dapm_mic] = 7,
101 [snd_soc_dapm_micbias] = 8,
102 [snd_soc_dapm_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +0100103 [snd_soc_dapm_aif_in] = 10,
104 [snd_soc_dapm_aif_out] = 10,
Mark Brown46162742013-06-05 19:36:11 +0100105 [snd_soc_dapm_dai_in] = 10,
106 [snd_soc_dapm_dai_out] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +0100107 [snd_soc_dapm_dai_link] = 11,
Mark Brownc74184e2012-04-04 22:12:09 +0100108 [snd_soc_dapm_supply] = 12,
Mark Brown1dd275b2013-10-09 13:56:37 +0100109 [snd_soc_dapm_clock_supply] = 13,
110 [snd_soc_dapm_regulator_supply] = 13,
111 [snd_soc_dapm_post] = 14,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200112};
113
Mark Brownf9fa2b12014-03-06 16:49:11 +0800114static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
115{
116 if (dapm->card && dapm->card->instantiated)
117 lockdep_assert_held(&dapm->card->dapm_mutex);
118}
119
Troy Kisky12ef1932008-10-13 17:42:14 -0700120static void pop_wait(u32 pop_time)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100121{
122 if (pop_time)
123 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
124}
125
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200126static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100127{
128 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200129 char *buf;
130
131 if (!pop_time)
132 return;
133
134 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
135 if (buf == NULL)
136 return;
Mark Brown15e4c72f2008-07-02 11:51:20 +0100137
138 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200139 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100140 dev_info(dev, "%s", buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100141 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200142
143 kfree(buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100144}
145
Mark Browndb432b42011-10-03 21:06:40 +0100146static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
147{
148 return !list_empty(&w->dirty);
149}
150
Mark Brown492c0a12014-03-06 16:15:48 +0800151static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100152{
Mark Brownf9fa2b12014-03-06 16:49:11 +0800153 dapm_assert_locked(w->dapm);
154
Mark Brown75c1f892011-10-04 22:28:08 +0100155 if (!dapm_dirty_widget(w)) {
156 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
157 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100158 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100159 }
Mark Browndb432b42011-10-03 21:06:40 +0100160}
161
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200162/*
163 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of input
164 * paths
165 * @w: The widget for which to invalidate the cached number of input paths
166 *
167 * The function resets the cached number of inputs for the specified widget and
168 * all widgets that can be reached via outgoing paths from the widget.
169 *
170 * This function must be called if the number of input paths for a widget might
171 * have changed. E.g. if the source state of a widget changes or a path is added
172 * or activated with the widget as the sink.
173 */
174static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
175{
176 struct snd_soc_dapm_widget *sink;
177 struct snd_soc_dapm_path *p;
178 LIST_HEAD(list);
179
180 dapm_assert_locked(w->dapm);
181
182 if (w->inputs == -1)
183 return;
184
185 w->inputs = -1;
186 list_add_tail(&w->work_list, &list);
187
188 list_for_each_entry(w, &list, work_list) {
189 list_for_each_entry(p, &w->sinks, list_source) {
190 if (p->is_supply || p->weak || !p->connect)
191 continue;
192 sink = p->sink;
193 if (sink->inputs != -1) {
194 sink->inputs = -1;
195 list_add_tail(&sink->work_list, &list);
196 }
197 }
198 }
199}
200
201/*
202 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
203 * output paths
204 * @w: The widget for which to invalidate the cached number of output paths
205 *
206 * Resets the cached number of outputs for the specified widget and all widgets
207 * that can be reached via incoming paths from the widget.
208 *
209 * This function must be called if the number of output paths for a widget might
210 * have changed. E.g. if the sink state of a widget changes or a path is added
211 * or activated with the widget as the source.
212 */
213static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
214{
215 struct snd_soc_dapm_widget *source;
216 struct snd_soc_dapm_path *p;
217 LIST_HEAD(list);
218
219 dapm_assert_locked(w->dapm);
220
221 if (w->outputs == -1)
222 return;
223
224 w->outputs = -1;
225 list_add_tail(&w->work_list, &list);
226
227 list_for_each_entry(w, &list, work_list) {
228 list_for_each_entry(p, &w->sources, list_sink) {
229 if (p->is_supply || p->weak || !p->connect)
230 continue;
231 source = p->source;
232 if (source->outputs != -1) {
233 source->outputs = -1;
234 list_add_tail(&source->work_list, &list);
235 }
236 }
237 }
238}
239
240/*
241 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
242 * for the widgets connected to a path
243 * @p: The path to invalidate
244 *
245 * Resets the cached number of inputs for the sink of the path and the cached
246 * number of outputs for the source of the path.
247 *
248 * This function must be called when a path is added, removed or the connected
249 * state changes.
250 */
251static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
252{
253 /*
254 * Weak paths or supply paths do not influence the number of input or
255 * output paths of their neighbors.
256 */
257 if (p->weak || p->is_supply)
258 return;
259
260 /*
261 * The number of connected endpoints is the sum of the number of
262 * connected endpoints of all neighbors. If a node with 0 connected
263 * endpoints is either connected or disconnected that sum won't change,
264 * so there is no need to re-check the path.
265 */
266 if (p->source->inputs != 0)
267 dapm_widget_invalidate_input_paths(p->sink);
268 if (p->sink->outputs != 0)
269 dapm_widget_invalidate_output_paths(p->source);
270}
271
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200272void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
Mark Browne2d32ff2012-08-31 17:38:32 -0700273{
Mark Browne2d32ff2012-08-31 17:38:32 -0700274 struct snd_soc_dapm_widget *w;
275
276 mutex_lock(&card->dapm_mutex);
277
278 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200279 if (w->is_sink || w->is_source) {
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200280 dapm_mark_dirty(w, "Rechecking endpoints");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +0200281 if (w->is_sink)
282 dapm_widget_invalidate_output_paths(w);
283 if (w->is_source)
284 dapm_widget_invalidate_input_paths(w);
285 }
Mark Browne2d32ff2012-08-31 17:38:32 -0700286 }
287
288 mutex_unlock(&card->dapm_mutex);
289}
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200290EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
Mark Browne2d32ff2012-08-31 17:38:32 -0700291
Richard Purdie2b97eab2006-10-06 18:32:18 +0200292/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100293static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200294 const struct snd_soc_dapm_widget *_widget)
295{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100296 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200297}
298
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200299struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200300 unsigned int value;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200301 struct snd_soc_dapm_widget *widget;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200302 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200303 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200304};
305
306static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
307 struct snd_kcontrol *kcontrol)
308{
309 struct dapm_kcontrol_data *data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200310 struct soc_mixer_control *mc;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200311
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200312 data = kzalloc(sizeof(*data), GFP_KERNEL);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200313 if (!data) {
314 dev_err(widget->dapm->dev,
315 "ASoC: can't allocate kcontrol data for %s\n",
316 widget->name);
317 return -ENOMEM;
318 }
319
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200320 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200321
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200322 switch (widget->id) {
323 case snd_soc_dapm_switch:
324 case snd_soc_dapm_mixer:
325 case snd_soc_dapm_mixer_named_ctl:
326 mc = (struct soc_mixer_control *)kcontrol->private_value;
327
328 if (mc->autodisable) {
329 struct snd_soc_dapm_widget template;
330
331 memset(&template, 0, sizeof(template));
332 template.reg = mc->reg;
333 template.mask = (1 << fls(mc->max)) - 1;
334 template.shift = mc->shift;
335 if (mc->invert)
336 template.off_val = mc->max;
337 else
338 template.off_val = 0;
339 template.on_val = template.off_val;
340 template.id = snd_soc_dapm_kcontrol;
341 template.name = kcontrol->id.name;
342
Lars-Peter Clausen2daabd72013-08-30 17:39:33 +0200343 data->value = template.on_val;
344
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200345 data->widget = snd_soc_dapm_new_control(widget->dapm,
346 &template);
347 if (!data->widget) {
348 kfree(data);
349 return -ENOMEM;
350 }
351 }
352 break;
353 default:
354 break;
355 }
356
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200357 kcontrol->private_data = data;
358
359 return 0;
360}
361
362static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
363{
364 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200365 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200366 kfree(data);
367}
368
369static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
370 const struct snd_kcontrol *kcontrol)
371{
372 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
373
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200374 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200375}
376
377static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
378 struct snd_soc_dapm_widget *widget)
379{
380 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200381 struct snd_soc_dapm_widget_list *new_wlist;
382 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200383
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200384 if (data->wlist)
385 n = data->wlist->num_widgets + 1;
386 else
387 n = 1;
388
389 new_wlist = krealloc(data->wlist,
390 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
391 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200392 return -ENOMEM;
393
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200394 new_wlist->widgets[n - 1] = widget;
395 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200396
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200397 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200398
399 return 0;
400}
401
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200402static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
403 struct snd_soc_dapm_path *path)
404{
405 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
406
407 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200408
409 if (data->widget) {
410 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
411 path->source, NULL, NULL);
412 }
413}
414
415static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
416{
417 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
418
419 if (!data->widget)
420 return true;
421
422 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200423}
424
425static struct list_head *dapm_kcontrol_get_path_list(
426 const struct snd_kcontrol *kcontrol)
427{
428 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
429
430 return &data->paths;
431}
432
433#define dapm_kcontrol_for_each_path(path, kcontrol) \
434 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
435 list_kcontrol)
436
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530437unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200438{
439 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
440
441 return data->value;
442}
Subhransu S. Prusty5dc0158a2014-09-19 16:46:05 +0530443EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200444
445static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
446 unsigned int value)
447{
448 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
449
450 if (data->value == value)
451 return false;
452
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200453 if (data->widget)
454 data->widget->on_val = value;
455
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200456 data->value = value;
457
458 return true;
459}
460
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200461/**
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200462 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
463 * kcontrol
464 * @kcontrol: The kcontrol
465 *
466 * Note: This function must only be used on kcontrols that are known to have
467 * been registered for a CODEC. Otherwise the behaviour is undefined.
468 */
469struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
470 struct snd_kcontrol *kcontrol)
471{
472 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
473}
474EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
475
476/**
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200477 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
478 * @kcontrol: The kcontrol
479 */
480struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
481{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200482 return snd_soc_dapm_to_codec(snd_soc_dapm_kcontrol_dapm(kcontrol));
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200483}
484EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
485
Liam Girdwood6c120e12012-02-15 15:15:34 +0000486static void dapm_reset(struct snd_soc_card *card)
487{
488 struct snd_soc_dapm_widget *w;
489
Mark Brownf9fa2b12014-03-06 16:49:11 +0800490 lockdep_assert_held(&card->dapm_mutex);
491
Liam Girdwood6c120e12012-02-15 15:15:34 +0000492 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
493
494 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200495 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000496 w->power_checked = false;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000497 }
498}
499
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200500static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
501{
502 if (!dapm->component)
503 return NULL;
504 return dapm->component->name_prefix;
505}
506
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200507static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800508 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100509{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200510 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200511 return -EIO;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200512 return snd_soc_component_read(dapm->component, reg, value);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100513}
514
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200515static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
Bard Liao34775012014-04-17 20:12:56 +0800516 int reg, unsigned int mask, unsigned int value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100517{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200518 if (!dapm->component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200519 return -EIO;
Mark Brownfcf6c5e2014-12-15 13:08:48 +0000520 return snd_soc_component_update_bits(dapm->component, reg,
521 mask, value);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000522}
523
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200524static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
525 int reg, unsigned int mask, unsigned int value)
526{
527 if (!dapm->component)
528 return -EIO;
529 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
530}
531
Mark Browneb270e92013-10-09 13:52:52 +0100532static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
533{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200534 if (dapm->component)
535 snd_soc_component_async_complete(dapm->component);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100536}
537
Mark Brown452c5ea2009-05-17 21:41:23 +0100538/**
539 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800540 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100541 * @level: level to configure
542 *
543 * Configure the bias (power) levels for the SoC audio device.
544 *
545 * Returns 0 for success else error.
546 */
Mark Browned5a4c42011-02-18 11:12:42 -0800547static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200548 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100549{
Mark Browned5a4c42011-02-18 11:12:42 -0800550 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100551 int ret = 0;
552
Mark Brown84e90932010-11-04 00:07:02 -0400553 trace_snd_soc_bias_level_start(card, level);
554
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000555 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100556 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100557 if (ret != 0)
558 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100559
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +0200560 if (dapm->set_bias_level)
561 ret = dapm->set_bias_level(dapm, level);
562 else if (!card || dapm != &card->dapm)
Liam Girdwood41231282012-07-06 16:56:16 +0100563 dapm->bias_level = level;
564
Mark Brown171ec6b2011-06-06 18:15:19 +0100565 if (ret != 0)
566 goto out;
567
568 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100569 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100570out:
Mark Brown84e90932010-11-04 00:07:02 -0400571 trace_snd_soc_bias_level_done(card, level);
572
Mark Brown452c5ea2009-05-17 21:41:23 +0100573 return ret;
574}
575
Mark Brown74b8f952009-06-06 11:26:15 +0100576/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200577static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200578 struct snd_soc_dapm_path *path, const char *control_name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200579{
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200580 const struct snd_kcontrol_new *kcontrol = &path->sink->kcontrol_news[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +0200581 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100582 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200583 int i;
584
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100585 if (e->reg != SND_SOC_NOPM) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +0200586 soc_dapm_read(dapm, e->reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100587 val = (val >> e->shift_l) & e->mask;
588 item = snd_soc_enum_val_to_item(e, val);
589 } else {
590 /* since a virtual mux has no backing registers to
591 * decide which path to connect, it will try to match
592 * with the first enumeration. This is to ensure
593 * that the default mux choice (the first) will be
594 * correctly powered up during initialization.
595 */
596 item = 0;
597 }
598
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100599 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200600 if (!(strcmp(control_name, e->texts[i]))) {
Rasmus Villemoes98ad73c2014-10-21 17:01:15 +0200601 path->name = e->texts[i];
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100602 if (i == item)
603 path->connect = 1;
604 else
605 path->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200606 return 0;
607 }
608 }
609
610 return -ENODEV;
611}
612
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100613/* set up initial codec paths */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200614static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i)
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100615{
616 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200617 p->sink->kcontrol_news[i].private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100618 unsigned int reg = mc->reg;
619 unsigned int shift = mc->shift;
620 unsigned int max = mc->max;
621 unsigned int mask = (1 << fls(max)) - 1;
622 unsigned int invert = mc->invert;
623 unsigned int val;
624
625 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200626 soc_dapm_read(p->sink->dapm, reg, &val);
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100627 val = (val >> shift) & mask;
628 if (invert)
629 val = max - val;
630 p->connect = !!val;
631 } else {
632 p->connect = 0;
633 }
634}
635
Mark Brown74b8f952009-06-06 11:26:15 +0100636/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200637static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200638 struct snd_soc_dapm_path *path, const char *control_name)
639{
640 int i;
641
642 /* search for mixer kcontrol */
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +0200643 for (i = 0; i < path->sink->num_kcontrols; i++) {
644 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
645 path->name = path->sink->kcontrol_news[i].name;
646 dapm_set_mixer_path_status(path, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200647 return 0;
648 }
649 }
650 return -ENODEV;
651}
652
Stephen Warrenaf468002011-04-28 17:38:01 -0600653static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600654 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600655 const struct snd_kcontrol_new *kcontrol_new,
656 struct snd_kcontrol **kcontrol)
657{
658 struct snd_soc_dapm_widget *w;
659 int i;
660
661 *kcontrol = NULL;
662
663 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600664 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
665 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600666 for (i = 0; i < w->num_kcontrols; i++) {
667 if (&w->kcontrol_news[i] == kcontrol_new) {
668 if (w->kcontrols)
669 *kcontrol = w->kcontrols[i];
670 return 1;
671 }
672 }
673 }
674
675 return 0;
676}
677
Stephen Warren85762e72013-03-29 15:40:10 -0600678/*
679 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
680 * create it. Either way, add the widget into the control's widget list
681 */
682static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100683 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200684{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200685 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000686 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000687 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600688 size_t prefix_len;
689 int shared;
690 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600691 bool wname_in_long_name, kcname_in_long_name;
Daniel Macke5092c92014-10-07 13:41:24 +0200692 char *long_name = NULL;
Stephen Warren85762e72013-03-29 15:40:10 -0600693 const char *name;
Daniel Macke5092c92014-10-07 13:41:24 +0200694 int ret = 0;
Mark Brownefb7ac32011-03-08 17:23:24 +0000695
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200696 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000697 if (prefix)
698 prefix_len = strlen(prefix) + 1;
699 else
700 prefix_len = 0;
701
Stephen Warren85762e72013-03-29 15:40:10 -0600702 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
703 &kcontrol);
704
Stephen Warren85762e72013-03-29 15:40:10 -0600705 if (!kcontrol) {
706 if (shared) {
707 wname_in_long_name = false;
708 kcname_in_long_name = true;
709 } else {
710 switch (w->id) {
711 case snd_soc_dapm_switch:
712 case snd_soc_dapm_mixer:
713 wname_in_long_name = true;
714 kcname_in_long_name = true;
715 break;
716 case snd_soc_dapm_mixer_named_ctl:
717 wname_in_long_name = false;
718 kcname_in_long_name = true;
719 break;
720 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600721 wname_in_long_name = true;
722 kcname_in_long_name = false;
723 break;
724 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600725 return -EINVAL;
726 }
727 }
728
729 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600730 /*
731 * The control will get a prefix from the control
732 * creation process but we're also using the same
733 * prefix for widgets so cut the prefix off the
734 * front of the widget name.
735 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200736 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600737 w->name + prefix_len,
738 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200739 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200740 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600741
742 name = long_name;
743 } else if (wname_in_long_name) {
744 long_name = NULL;
745 name = w->name + prefix_len;
746 } else {
747 long_name = NULL;
748 name = w->kcontrol_news[kci].name;
749 }
750
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200751 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600752 prefix);
Daniel Macke5092c92014-10-07 13:41:24 +0200753 if (!kcontrol) {
754 ret = -ENOMEM;
755 goto exit_free;
756 }
757
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200758 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200759
760 ret = dapm_kcontrol_data_alloc(w, kcontrol);
761 if (ret) {
762 snd_ctl_free_one(kcontrol);
Daniel Macke5092c92014-10-07 13:41:24 +0200763 goto exit_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200764 }
765
Stephen Warren85762e72013-03-29 15:40:10 -0600766 ret = snd_ctl_add(card, kcontrol);
767 if (ret < 0) {
768 dev_err(dapm->dev,
769 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
770 w->name, name, ret);
Daniel Macke5092c92014-10-07 13:41:24 +0200771 goto exit_free;
Stephen Warren85762e72013-03-29 15:40:10 -0600772 }
Stephen Warren85762e72013-03-29 15:40:10 -0600773 }
774
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200775 ret = dapm_kcontrol_add_widget(kcontrol, w);
Daniel Macke5092c92014-10-07 13:41:24 +0200776 if (ret == 0)
777 w->kcontrols[kci] = kcontrol;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200778
Daniel Macke5092c92014-10-07 13:41:24 +0200779exit_free:
780 kfree(long_name);
Stephen Warren85762e72013-03-29 15:40:10 -0600781
Daniel Macke5092c92014-10-07 13:41:24 +0200782 return ret;
Stephen Warren85762e72013-03-29 15:40:10 -0600783}
784
785/* create new dapm mixer control */
786static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
787{
788 int i, ret;
789 struct snd_soc_dapm_path *path;
790
Richard Purdie2b97eab2006-10-06 18:32:18 +0200791 /* add kcontrol */
792 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200793 /* match name */
794 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200795 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600796 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200797 continue;
798
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200799 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200800 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200801 continue;
802 }
803
Mark Brown946d92a2013-08-12 23:28:42 +0100804 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600805 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200806 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100807
808 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200809 }
810 }
Stephen Warren85762e72013-03-29 15:40:10 -0600811
812 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200813}
814
815/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200816static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200817{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200818 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600819 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600820 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200821
Stephen Warrenaf468002011-04-28 17:38:01 -0600822 if (w->num_kcontrols != 1) {
823 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000824 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600825 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200826 return -EINVAL;
827 }
828
Lars-Peter Clausenfe581392013-08-01 18:30:38 +0200829 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600830 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
831 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600832 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200833
Mark Brown946d92a2013-08-12 23:28:42 +0100834 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600835 if (ret < 0)
836 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600837
Lars-Peter Clausen98407ef2014-10-25 17:41:57 +0200838 list_for_each_entry(path, &w->sources, list_sink) {
839 if (path->name)
840 dapm_kcontrol_add_path(w->kcontrols[0], path);
841 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200842
Stephen Warrenaf468002011-04-28 17:38:01 -0600843 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200844}
845
846/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200847static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200848{
Mark Browna6c65732010-03-03 17:45:21 +0000849 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200850 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000851 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200852
Mark Browna6c65732010-03-03 17:45:21 +0000853 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200854}
855
Mark Brown9949788b2010-05-07 20:24:05 +0100856/* We implement power down on suspend by checking the power state of
857 * the ALSA card - when we are suspending the ALSA state for the card
858 * is set to D3.
859 */
860static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
861{
Mark Brown12ea2c72011-03-02 18:17:32 +0000862 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +0100863
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000864 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +0100865 case SNDRV_CTL_POWER_D3hot:
866 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100867 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000868 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200869 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100870 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +0100871 default:
872 return 1;
873 }
874}
875
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100876/* add widget to list if it's not already in the list */
877static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
878 struct snd_soc_dapm_widget *w)
879{
880 struct snd_soc_dapm_widget_list *wlist;
881 int wlistsize, wlistentries, i;
882
883 if (*list == NULL)
884 return -EINVAL;
885
886 wlist = *list;
887
888 /* is this widget already in the list */
889 for (i = 0; i < wlist->num_widgets; i++) {
890 if (wlist->widgets[i] == w)
891 return 0;
892 }
893
894 /* allocate some new space */
895 wlistentries = wlist->num_widgets + 1;
896 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
897 wlistentries * sizeof(struct snd_soc_dapm_widget *);
898 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
899 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000900 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100901 w->name);
902 return -ENOMEM;
903 }
904 wlist = *list;
905
906 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000907 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100908 w->name, wlist->num_widgets);
909
910 wlist->widgets[wlist->num_widgets] = w;
911 wlist->num_widgets++;
912 return 1;
913}
914
Richard Purdie2b97eab2006-10-06 18:32:18 +0200915/*
916 * Recursively check for a completed path to an active or physically connected
917 * output widget. Returns number of complete paths.
918 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100919static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
920 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200921{
922 struct snd_soc_dapm_path *path;
923 int con = 0;
924
Mark Brown024dc072011-10-09 11:52:05 +0100925 if (widget->outputs >= 0)
926 return widget->outputs;
927
Mark Brownde02d072011-09-20 21:43:24 +0100928 DAPM_UPDATE_STAT(widget, path_checks);
929
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +0200930 if (widget->is_sink && widget->connected) {
931 widget->outputs = snd_soc_dapm_suspend_check(widget);
932 return widget->outputs;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200933 }
934
935 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100936 DAPM_UPDATE_STAT(widget, neighbour_checks);
937
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +0200938 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +0100939 continue;
940
Mark Brown8af294b2013-02-22 17:48:15 +0000941 if (path->walking)
942 return 1;
943
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100944 trace_snd_soc_dapm_output_path(widget, path);
945
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +0200946 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +0000947 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100948
949 /* do we need to add this widget to the list ? */
950 if (list) {
951 int err;
952 err = dapm_list_add_widget(list, path->sink);
953 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000954 dev_err(widget->dapm->dev,
955 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100956 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000957 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100958 return con;
959 }
960 }
961
962 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000963
964 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200965 }
966 }
967
Mark Brown024dc072011-10-09 11:52:05 +0100968 widget->outputs = con;
969
Richard Purdie2b97eab2006-10-06 18:32:18 +0200970 return con;
971}
972
973/*
974 * Recursively check for a completed path to an active or physically connected
975 * input widget. Returns number of complete paths.
976 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100977static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
978 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200979{
980 struct snd_soc_dapm_path *path;
981 int con = 0;
982
Mark Brown024dc072011-10-09 11:52:05 +0100983 if (widget->inputs >= 0)
984 return widget->inputs;
985
Mark Brownde02d072011-09-20 21:43:24 +0100986 DAPM_UPDATE_STAT(widget, path_checks);
987
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +0200988 if (widget->is_source && widget->connected) {
989 widget->inputs = snd_soc_dapm_suspend_check(widget);
990 return widget->inputs;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200991 }
992
993 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +0100994 DAPM_UPDATE_STAT(widget, neighbour_checks);
995
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +0200996 if (path->weak || path->is_supply)
Mark Brownbf3a9e12011-06-13 16:42:29 +0100997 continue;
998
Mark Brown8af294b2013-02-22 17:48:15 +0000999 if (path->walking)
1000 return 1;
1001
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001002 trace_snd_soc_dapm_input_path(widget, path);
1003
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001004 if (path->connect) {
Mark Brown8af294b2013-02-22 17:48:15 +00001005 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001006
1007 /* do we need to add this widget to the list ? */
1008 if (list) {
1009 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001010 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001011 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001012 dev_err(widget->dapm->dev,
1013 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001014 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001015 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001016 return con;
1017 }
1018 }
1019
1020 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001021
1022 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001023 }
1024 }
1025
Mark Brown024dc072011-10-09 11:52:05 +01001026 widget->inputs = con;
1027
Richard Purdie2b97eab2006-10-06 18:32:18 +02001028 return con;
1029}
1030
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001031/**
1032 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1033 * @dai: the soc DAI.
1034 * @stream: stream direction.
1035 * @list: list of active widgets for this stream.
1036 *
1037 * Queries DAPM graph as to whether an valid audio stream path exists for
1038 * the initial stream specified by name. This takes into account
1039 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1040 *
1041 * Returns the number of valid paths or negative error.
1042 */
1043int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1044 struct snd_soc_dapm_widget_list **list)
1045{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01001046 struct snd_soc_card *card = dai->component->card;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001047 struct snd_soc_dapm_widget *w;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001048 int paths;
1049
1050 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001051
1052 /*
1053 * For is_connected_{output,input}_ep fully discover the graph we need
1054 * to reset the cached number of inputs and outputs.
1055 */
1056 list_for_each_entry(w, &card->widgets, list) {
1057 w->inputs = -1;
1058 w->outputs = -1;
1059 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001060
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001061 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001062 paths = is_connected_output_ep(dai->playback_widget, list);
Lars-Peter Clausen130897a2014-10-20 19:36:39 +02001063 else
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001064 paths = is_connected_input_ep(dai->capture_widget, list);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001065
1066 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001067 mutex_unlock(&card->dapm_mutex);
1068
1069 return paths;
1070}
1071
Richard Purdie2b97eab2006-10-06 18:32:18 +02001072/*
Mark Brown62ea8742012-01-21 21:14:48 +00001073 * Handler for regulator supply widget.
1074 */
1075int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1076 struct snd_kcontrol *kcontrol, int event)
1077{
Mark Brownc05b84d2012-09-07 12:57:11 +08001078 int ret;
1079
Mark Browneb270e92013-10-09 13:52:52 +01001080 soc_dapm_async_complete(w->dapm);
1081
Mark Brownc05b84d2012-09-07 12:57:11 +08001082 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001083 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001084 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001085 if (ret != 0)
1086 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001087 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001088 w->name, ret);
1089 }
1090
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001091 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001092 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001093 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001094 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001095 if (ret != 0)
1096 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001097 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001098 w->name, ret);
1099 }
1100
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001101 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001102 }
Mark Brown62ea8742012-01-21 21:14:48 +00001103}
1104EXPORT_SYMBOL_GPL(dapm_regulator_event);
1105
Ola Liljad7e7eb92012-05-24 15:26:25 +02001106/*
1107 * Handler for clock supply widget.
1108 */
1109int dapm_clock_event(struct snd_soc_dapm_widget *w,
1110 struct snd_kcontrol *kcontrol, int event)
1111{
1112 if (!w->clk)
1113 return -EIO;
1114
Mark Browneb270e92013-10-09 13:52:52 +01001115 soc_dapm_async_complete(w->dapm);
1116
Mark Brownec029952012-06-04 08:16:20 +01001117#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001118 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001119 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001120 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001121 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001122 return 0;
1123 }
Mark Brownec029952012-06-04 08:16:20 +01001124#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001125 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001126}
1127EXPORT_SYMBOL_GPL(dapm_clock_event);
1128
Mark Brownd805002b2011-09-28 18:28:23 +01001129static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1130{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001131 if (w->power_checked)
1132 return w->new_power;
1133
Mark Brownd805002b2011-09-28 18:28:23 +01001134 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001135 w->new_power = 1;
Mark Brownd805002b2011-09-28 18:28:23 +01001136 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001137 w->new_power = w->power_check(w);
1138
1139 w->power_checked = true;
1140
1141 return w->new_power;
Mark Brownd805002b2011-09-28 18:28:23 +01001142}
1143
Mark Browncd0f2d42009-04-20 16:56:59 +01001144/* Generic check to see if a widget should be powered.
1145 */
1146static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1147{
1148 int in, out;
1149
Mark Brownde02d072011-09-20 21:43:24 +01001150 DAPM_UPDATE_STAT(w, power_checks);
1151
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001152 in = is_connected_input_ep(w, NULL);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001153 out = is_connected_output_ep(w, NULL);
Mark Browncd0f2d42009-04-20 16:56:59 +01001154 return out != 0 && in != 0;
1155}
1156
Mark Brown246d0a12009-04-22 18:24:55 +01001157/* Check to see if a power supply is needed */
1158static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1159{
1160 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001161
Mark Brownde02d072011-09-20 21:43:24 +01001162 DAPM_UPDATE_STAT(w, power_checks);
1163
Mark Brown246d0a12009-04-22 18:24:55 +01001164 /* Check if one of our outputs is connected */
1165 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001166 DAPM_UPDATE_STAT(w, neighbour_checks);
1167
Mark Brownbf3a9e12011-06-13 16:42:29 +01001168 if (path->weak)
1169 continue;
1170
Mark Brown215edda2009-09-08 18:59:05 +01001171 if (path->connected &&
1172 !path->connected(path->source, path->sink))
1173 continue;
1174
Mark Brownf68d7e12011-10-04 22:57:50 +01001175 if (dapm_widget_power_check(path->sink))
1176 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001177 }
1178
Mark Brownf68d7e12011-10-04 22:57:50 +01001179 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001180}
1181
Mark Brown35c64bc2011-09-28 18:23:53 +01001182static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1183{
1184 return 1;
1185}
1186
Mark Brown38357ab2009-06-06 19:03:23 +01001187static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1188 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001189 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001190{
Mark Brown828a8422011-01-15 13:14:30 +00001191 int *sort;
1192
1193 if (power_up)
1194 sort = dapm_up_seq;
1195 else
1196 sort = dapm_down_seq;
1197
Mark Brown38357ab2009-06-06 19:03:23 +01001198 if (sort[a->id] != sort[b->id])
1199 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001200 if (a->subseq != b->subseq) {
1201 if (power_up)
1202 return a->subseq - b->subseq;
1203 else
1204 return b->subseq - a->subseq;
1205 }
Mark Brownb22ead22009-06-07 12:51:26 +01001206 if (a->reg != b->reg)
1207 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001208 if (a->dapm != b->dapm)
1209 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001210
Mark Brown38357ab2009-06-06 19:03:23 +01001211 return 0;
1212}
Mark Brown42aa3412009-03-01 19:21:10 +00001213
Mark Brown38357ab2009-06-06 19:03:23 +01001214/* Insert a widget in order into a DAPM power sequence. */
1215static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1216 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001217 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001218{
1219 struct snd_soc_dapm_widget *w;
1220
1221 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001222 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001223 list_add_tail(&new_widget->power_list, &w->power_list);
1224 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001225 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001226
Mark Brown38357ab2009-06-06 19:03:23 +01001227 list_add_tail(&new_widget->power_list, list);
1228}
Mark Brown42aa3412009-03-01 19:21:10 +00001229
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001230static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001231 struct snd_soc_dapm_widget *w, int event)
1232{
Mark Brown68f89ad2010-11-03 23:51:49 -04001233 const char *ev_name;
1234 int power, ret;
1235
1236 switch (event) {
1237 case SND_SOC_DAPM_PRE_PMU:
1238 ev_name = "PRE_PMU";
1239 power = 1;
1240 break;
1241 case SND_SOC_DAPM_POST_PMU:
1242 ev_name = "POST_PMU";
1243 power = 1;
1244 break;
1245 case SND_SOC_DAPM_PRE_PMD:
1246 ev_name = "PRE_PMD";
1247 power = 0;
1248 break;
1249 case SND_SOC_DAPM_POST_PMD:
1250 ev_name = "POST_PMD";
1251 power = 0;
1252 break;
Mark Brown80114122013-02-25 15:14:19 +00001253 case SND_SOC_DAPM_WILL_PMU:
1254 ev_name = "WILL_PMU";
1255 power = 1;
1256 break;
1257 case SND_SOC_DAPM_WILL_PMD:
1258 ev_name = "WILL_PMD";
1259 power = 0;
1260 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001261 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001262 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001263 return;
1264 }
1265
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001266 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001267 return;
1268
1269 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001270 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001271 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001272 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001273 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001274 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001275 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001276 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001277 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001278 ev_name, w->name, ret);
1279 }
1280}
1281
Mark Brownb22ead22009-06-07 12:51:26 +01001282/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001283static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001284 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001285{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001286 struct snd_soc_dapm_context *dapm;
Mark Brown68f89ad2010-11-03 23:51:49 -04001287 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001288 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001289 unsigned int value = 0;
1290 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001291
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001292 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1293 reg = w->reg;
1294 dapm = w->dapm;
Mark Brownb22ead22009-06-07 12:51:26 +01001295
1296 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001297 WARN_ON(reg != w->reg || dapm != w->dapm);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001298 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001299
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001300 mask |= w->mask << w->shift;
1301 if (w->power)
1302 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001303 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001304 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001305
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001306 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001307 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1308 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001309
Mark Brown68f89ad2010-11-03 23:51:49 -04001310 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001311 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1312 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001313 }
1314
Mark Brown81628102009-06-07 13:21:24 +01001315 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001316 /* Any widget will do, they should all be updating the
1317 * same register.
1318 */
Mark Brown29376bc2011-06-19 13:49:28 +01001319
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001320 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001321 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001322 value, mask, reg, card->pop_time);
1323 pop_wait(card->pop_time);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001324 soc_dapm_update_bits(dapm, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001325 }
1326
1327 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001328 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1329 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001330 }
Mark Brown42aa3412009-03-01 19:21:10 +00001331}
1332
Mark Brownb22ead22009-06-07 12:51:26 +01001333/* Apply a DAPM power sequence.
1334 *
1335 * We walk over a pre-sorted list of widgets to apply power to. In
1336 * order to minimise the number of writes to the device required
1337 * multiple widgets will be updated in a single write where possible.
1338 * Currently anything that requires more than a single write is not
1339 * handled.
1340 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001341static void dapm_seq_run(struct snd_soc_card *card,
1342 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001343{
1344 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001345 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001346 LIST_HEAD(pending);
1347 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001348 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001349 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001350 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001351 int ret, i;
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;
Mark Brown163cac02009-06-07 10:12:52 +01001358
Mark Brownb22ead22009-06-07 12:51:26 +01001359 list_for_each_entry_safe(w, n, list, power_list) {
1360 ret = 0;
1361
1362 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001363 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001364 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001365 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001366 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001367
Mark Brown474b62d2011-01-18 16:14:44 +00001368 if (cur_dapm && cur_dapm->seq_notifier) {
1369 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1370 if (sort[i] == cur_sort)
1371 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001372 i,
1373 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001374 }
1375
Mark Browneb270e92013-10-09 13:52:52 +01001376 if (cur_dapm && w->dapm != cur_dapm)
1377 soc_dapm_async_complete(cur_dapm);
1378
Mark Brownb22ead22009-06-07 12:51:26 +01001379 INIT_LIST_HEAD(&pending);
1380 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001381 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001382 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001383 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001384 }
1385
Mark Brown163cac02009-06-07 10:12:52 +01001386 switch (w->id) {
1387 case snd_soc_dapm_pre:
1388 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001389 list_for_each_entry_safe_continue(w, n, list,
1390 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001391
Mark Brownb22ead22009-06-07 12:51:26 +01001392 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001393 ret = w->event(w,
1394 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001395 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001396 ret = w->event(w,
1397 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001398 break;
1399
1400 case snd_soc_dapm_post:
1401 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001402 list_for_each_entry_safe_continue(w, n, list,
1403 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001404
Mark Brownb22ead22009-06-07 12:51:26 +01001405 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001406 ret = w->event(w,
1407 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001408 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001409 ret = w->event(w,
1410 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001411 break;
1412
Mark Brown163cac02009-06-07 10:12:52 +01001413 default:
Mark Brown81628102009-06-07 13:21:24 +01001414 /* Queue it up for application */
1415 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001416 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001417 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001418 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001419 list_move(&w->power_list, &pending);
1420 break;
Mark Brown163cac02009-06-07 10:12:52 +01001421 }
Mark Brownb22ead22009-06-07 12:51:26 +01001422
1423 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001424 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001425 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001426 }
Mark Brownb22ead22009-06-07 12:51:26 +01001427
1428 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001429 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001430
1431 if (cur_dapm && cur_dapm->seq_notifier) {
1432 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1433 if (sort[i] == cur_sort)
1434 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001435 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001436 }
Mark Browneb270e92013-10-09 13:52:52 +01001437
1438 list_for_each_entry(d, &card->dapm_list, list) {
1439 soc_dapm_async_complete(d);
1440 }
Mark Brown163cac02009-06-07 10:12:52 +01001441}
1442
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001443static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001444{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001445 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001446 struct snd_soc_dapm_widget_list *wlist;
1447 struct snd_soc_dapm_widget *w = NULL;
1448 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001449 int ret;
1450
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001451 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001452 return;
1453
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001454 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001455
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001456 for (wi = 0; wi < wlist->num_widgets; wi++) {
1457 w = wlist->widgets[wi];
1458
1459 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1460 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1461 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001462 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001463 w->name, ret);
1464 }
Mark Brown97404f22010-12-14 16:13:57 +00001465 }
1466
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001467 if (!w)
1468 return;
1469
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02001470 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1471 update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001472 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001473 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001474 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001475
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001476 for (wi = 0; wi < wlist->num_widgets; wi++) {
1477 w = wlist->widgets[wi];
1478
1479 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1480 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1481 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001482 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001483 w->name, ret);
1484 }
Mark Brown97404f22010-12-14 16:13:57 +00001485 }
1486}
1487
Mark Brown9d0624a2011-02-18 11:49:43 -08001488/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1489 * they're changing state.
1490 */
1491static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1492{
1493 struct snd_soc_dapm_context *d = data;
1494 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001495
Mark Brown56fba412011-06-04 11:25:10 +01001496 /* If we're off and we're not supposed to be go into STANDBY */
1497 if (d->bias_level == SND_SOC_BIAS_OFF &&
1498 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001499 if (d->dev)
1500 pm_runtime_get_sync(d->dev);
1501
Mark Brown9d0624a2011-02-18 11:49:43 -08001502 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1503 if (ret != 0)
1504 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001505 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001506 }
1507
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001508 /* Prepare for a transition to ON or away from ON */
1509 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1510 d->bias_level != SND_SOC_BIAS_ON) ||
1511 (d->target_bias_level != SND_SOC_BIAS_ON &&
1512 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001513 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1514 if (ret != 0)
1515 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001516 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001517 }
1518}
1519
1520/* Async callback run prior to DAPM sequences - brings to their final
1521 * state.
1522 */
1523static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1524{
1525 struct snd_soc_dapm_context *d = data;
1526 int ret;
1527
1528 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001529 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1530 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1531 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001532 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1533 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001534 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001535 ret);
1536 }
1537
1538 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001539 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1540 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001541 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1542 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001543 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1544 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001545
1546 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001547 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001548 }
1549
1550 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001551 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1552 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001553 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1554 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001555 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001556 ret);
1557 }
1558}
Mark Brown97404f22010-12-14 16:13:57 +00001559
Mark Brownfe4fda52011-10-03 22:36:57 +01001560static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1561 bool power, bool connect)
1562{
1563 /* If a connection is being made or broken then that update
1564 * will have marked the peer dirty, otherwise the widgets are
1565 * not connected and this update has no impact. */
1566 if (!connect)
1567 return;
1568
1569 /* If the peer is already in the state we're moving to then we
1570 * won't have an impact on it. */
1571 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001572 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001573}
1574
Mark Brown05623c42011-09-28 17:02:31 +01001575static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1576 struct list_head *up_list,
1577 struct list_head *down_list)
1578{
Mark Browndb432b42011-10-03 21:06:40 +01001579 struct snd_soc_dapm_path *path;
1580
Mark Brown05623c42011-09-28 17:02:31 +01001581 if (w->power == power)
1582 return;
1583
1584 trace_snd_soc_dapm_widget_power(w, power);
1585
Mark Browndb432b42011-10-03 21:06:40 +01001586 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001587 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001588 */
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001589 list_for_each_entry(path, &w->sources, list_sink)
1590 dapm_widget_set_peer_power(path->source, power, path->connect);
1591
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02001592 /* Supplies can't affect their outputs, only their inputs */
1593 if (!w->is_supply) {
Lars-Peter Clausen7ddd4cd2014-10-20 19:36:34 +02001594 list_for_each_entry(path, &w->sinks, list_source)
1595 dapm_widget_set_peer_power(path->sink, power,
1596 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001597 }
1598
Mark Brown05623c42011-09-28 17:02:31 +01001599 if (power)
1600 dapm_seq_insert(w, up_list, true);
1601 else
1602 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001603}
1604
Mark Brown7c81beb2011-09-20 22:22:32 +01001605static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1606 struct list_head *up_list,
1607 struct list_head *down_list)
1608{
Mark Brown7c81beb2011-09-20 22:22:32 +01001609 int power;
1610
1611 switch (w->id) {
1612 case snd_soc_dapm_pre:
1613 dapm_seq_insert(w, down_list, false);
1614 break;
1615 case snd_soc_dapm_post:
1616 dapm_seq_insert(w, up_list, true);
1617 break;
1618
1619 default:
Mark Brownd805002b2011-09-28 18:28:23 +01001620 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001621
Mark Brown05623c42011-09-28 17:02:31 +01001622 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001623 break;
1624 }
1625}
1626
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001627static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1628{
1629 if (dapm->idle_bias_off)
1630 return true;
1631
1632 switch (snd_power_get_state(dapm->card->snd_card)) {
1633 case SNDRV_CTL_POWER_D3hot:
1634 case SNDRV_CTL_POWER_D3cold:
1635 return dapm->suspend_bias_off;
1636 default:
1637 break;
1638 }
1639
1640 return false;
1641}
1642
Mark Brown42aa3412009-03-01 19:21:10 +00001643/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001644 * Scan each dapm widget for complete audio path.
1645 * A complete path is a route that has valid endpoints i.e.:-
1646 *
1647 * o DAC to output pin.
1648 * o Input Pin to ADC.
1649 * o Input pin to Output pin (bypass, sidetone)
1650 * o DAC to ADC (loopback).
1651 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001652static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001653{
1654 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001655 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001656 LIST_HEAD(up_list);
1657 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001658 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001659 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001660
Mark Brownf9fa2b12014-03-06 16:49:11 +08001661 lockdep_assert_held(&card->dapm_mutex);
1662
Mark Brown84e90932010-11-04 00:07:02 -04001663 trace_snd_soc_dapm_start(card);
1664
Mark Brown56fba412011-06-04 11:25:10 +01001665 list_for_each_entry(d, &card->dapm_list, list) {
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001666 if (dapm_idle_bias_off(d))
Mark Brown497098be2012-03-08 15:06:09 +00001667 d->target_bias_level = SND_SOC_BIAS_OFF;
1668 else
1669 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001670 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001671
Liam Girdwood6c120e12012-02-15 15:15:34 +00001672 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001673
Mark Brown6d3ddc82009-05-16 17:47:29 +01001674 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001675 * lists indicating if they should be powered up or down. We
1676 * only check widgets that have been flagged as dirty but note
1677 * that new widgets may be added to the dirty list while we
1678 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001679 */
Mark Browndb432b42011-10-03 21:06:40 +01001680 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001681 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001682 }
1683
Mark Brownf9de6d72011-09-28 17:19:47 +01001684 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001685 switch (w->id) {
1686 case snd_soc_dapm_pre:
1687 case snd_soc_dapm_post:
1688 /* These widgets always need to be powered */
1689 break;
1690 default:
1691 list_del_init(&w->dirty);
1692 break;
1693 }
Mark Browndb432b42011-10-03 21:06:40 +01001694
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001695 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001696 d = w->dapm;
1697
1698 /* Supplies and micbiases only bring the
1699 * context up to STANDBY as unless something
1700 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001701 * generally don't require full power. Signal
1702 * generators are virtual pins and have no
1703 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001704 */
1705 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001706 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001707 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001708 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001709 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001710 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001711 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001712 case snd_soc_dapm_micbias:
1713 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1714 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1715 break;
1716 default:
1717 d->target_bias_level = SND_SOC_BIAS_ON;
1718 break;
1719 }
1720 }
1721
1722 }
1723
Mark Brown85a843c2011-09-21 21:29:47 +01001724 /* Force all contexts in the card to the same bias state if
1725 * they're not ground referenced.
1726 */
Mark Brown56fba412011-06-04 11:25:10 +01001727 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001728 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001729 if (d->target_bias_level > bias)
1730 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001731 list_for_each_entry(d, &card->dapm_list, list)
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02001732 if (!dapm_idle_bias_off(d))
Mark Brown85a843c2011-09-21 21:29:47 +01001733 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001734
Mark Brownde02d072011-09-20 21:43:24 +01001735 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001736
Xiang Xiao17282ba2014-03-02 00:04:03 +08001737 /* Run card bias changes at first */
1738 dapm_pre_sequence_async(&card->dapm, 0);
1739 /* Run other bias changes in parallel */
1740 list_for_each_entry(d, &card->dapm_list, list) {
1741 if (d != &card->dapm)
1742 async_schedule_domain(dapm_pre_sequence_async, d,
1743 &async_domain);
1744 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001745 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001746
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001747 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001748 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001749 }
1750
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001751 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001752 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001753 }
1754
Mark Brown6d3ddc82009-05-16 17:47:29 +01001755 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001756 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001757
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001758 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001759
Mark Brown6d3ddc82009-05-16 17:47:29 +01001760 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001761 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001762
Mark Brown9d0624a2011-02-18 11:49:43 -08001763 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001764 list_for_each_entry(d, &card->dapm_list, list) {
1765 if (d != &card->dapm)
1766 async_schedule_domain(dapm_post_sequence_async, d,
1767 &async_domain);
1768 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001769 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001770 /* Run card bias changes at last */
1771 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001772
Liam Girdwood8078d872012-02-15 15:15:35 +00001773 /* do we need to notify any clients that DAPM event is complete */
1774 list_for_each_entry(d, &card->dapm_list, list) {
1775 if (d->stream_event)
1776 d->stream_event(d, event);
1777 }
1778
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001779 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001780 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001781 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001782
Mark Brown84e90932010-11-04 00:07:02 -04001783 trace_snd_soc_dapm_done(card);
1784
Mark Brown42aa3412009-03-01 19:21:10 +00001785 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001786}
1787
Mark Brown79fb9382009-08-21 16:38:13 +01001788#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001789static ssize_t dapm_widget_power_read_file(struct file *file,
1790 char __user *user_buf,
1791 size_t count, loff_t *ppos)
1792{
1793 struct snd_soc_dapm_widget *w = file->private_data;
1794 char *buf;
1795 int in, out;
1796 ssize_t ret;
1797 struct snd_soc_dapm_path *p = NULL;
1798
1799 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1800 if (!buf)
1801 return -ENOMEM;
1802
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02001803 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
1804 if (w->is_supply) {
1805 in = 0;
1806 out = 0;
1807 } else {
1808 in = is_connected_input_ep(w, NULL);
1809 out = is_connected_output_ep(w, NULL);
1810 }
Mark Brown79fb9382009-08-21 16:38:13 +01001811
Mark Brownf13ebad2012-03-03 18:01:01 +00001812 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1813 w->name, w->power ? "On" : "Off",
1814 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001815
Mark Brownd033c362009-12-04 15:25:56 +00001816 if (w->reg >= 0)
1817 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001818 " - R%d(0x%x) mask 0x%x",
1819 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001820
1821 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1822
Mark Brown3eef08b2009-09-14 16:49:00 +01001823 if (w->sname)
1824 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1825 w->sname,
1826 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001827
1828 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001829 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001830 continue;
1831
Mark Brown79fb9382009-08-21 16:38:13 +01001832 if (p->connect)
1833 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001834 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001835 p->name ? p->name : "static",
1836 p->source->name);
1837 }
1838 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001839 if (p->connected && !p->connected(w, p->sink))
1840 continue;
1841
Mark Brown79fb9382009-08-21 16:38:13 +01001842 if (p->connect)
1843 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001844 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001845 p->name ? p->name : "static",
1846 p->sink->name);
1847 }
1848
1849 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1850
1851 kfree(buf);
1852 return ret;
1853}
1854
1855static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001856 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001857 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001858 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001859};
1860
Mark Brownef49e4f2011-04-04 20:48:13 +09001861static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1862 size_t count, loff_t *ppos)
1863{
1864 struct snd_soc_dapm_context *dapm = file->private_data;
1865 char *level;
1866
1867 switch (dapm->bias_level) {
1868 case SND_SOC_BIAS_ON:
1869 level = "On\n";
1870 break;
1871 case SND_SOC_BIAS_PREPARE:
1872 level = "Prepare\n";
1873 break;
1874 case SND_SOC_BIAS_STANDBY:
1875 level = "Standby\n";
1876 break;
1877 case SND_SOC_BIAS_OFF:
1878 level = "Off\n";
1879 break;
1880 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001881 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09001882 level = "Unknown\n";
1883 break;
1884 }
1885
1886 return simple_read_from_buffer(user_buf, count, ppos, level,
1887 strlen(level));
1888}
1889
1890static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001891 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001892 .read = dapm_bias_read_file,
1893 .llseek = default_llseek,
1894};
1895
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001896void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1897 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001898{
Mark Brown79fb9382009-08-21 16:38:13 +01001899 struct dentry *d;
1900
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02001901 if (!parent)
1902 return;
1903
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001904 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1905
1906 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001907 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001908 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001909 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001910 }
Mark Brown79fb9382009-08-21 16:38:13 +01001911
Mark Brownef49e4f2011-04-04 20:48:13 +09001912 d = debugfs_create_file("bias_level", 0444,
1913 dapm->debugfs_dapm, dapm,
1914 &dapm_bias_fops);
1915 if (!d)
1916 dev_warn(dapm->dev,
1917 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001918}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001919
1920static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1921{
1922 struct snd_soc_dapm_context *dapm = w->dapm;
1923 struct dentry *d;
1924
1925 if (!dapm->debugfs_dapm || !w->name)
1926 return;
1927
1928 d = debugfs_create_file(w->name, 0444,
1929 dapm->debugfs_dapm, w,
1930 &dapm_widget_power_fops);
1931 if (!d)
1932 dev_warn(w->dapm->dev,
1933 "ASoC: Failed to create %s debugfs file\n",
1934 w->name);
1935}
1936
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001937static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1938{
1939 debugfs_remove_recursive(dapm->debugfs_dapm);
1940}
1941
Mark Brown79fb9382009-08-21 16:38:13 +01001942#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001943void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1944 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001945{
1946}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001947
1948static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1949{
1950}
1951
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001952static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1953{
1954}
1955
Mark Brown79fb9382009-08-21 16:38:13 +01001956#endif
1957
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02001958/*
1959 * soc_dapm_connect_path() - Connects or disconnects a path
1960 * @path: The path to update
1961 * @connect: The new connect state of the path. True if the path is connected,
1962 * false if it is disconneted.
1963 * @reason: The reason why the path changed (for debugging only)
1964 */
1965static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
1966 bool connect, const char *reason)
1967{
1968 if (path->connect == connect)
1969 return;
1970
1971 path->connect = connect;
1972 dapm_mark_dirty(path->source, reason);
1973 dapm_mark_dirty(path->sink, reason);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02001974 dapm_path_invalidate(path);
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02001975}
1976
Richard Purdie2b97eab2006-10-06 18:32:18 +02001977/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001978static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001979 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001980{
1981 struct snd_soc_dapm_path *path;
1982 int found = 0;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02001983 bool connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001984
Mark Brownf9fa2b12014-03-06 16:49:11 +08001985 lockdep_assert_held(&card->dapm_mutex);
1986
Richard Purdie2b97eab2006-10-06 18:32:18 +02001987 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02001988 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001989 found = 1;
1990 /* we now need to match the string in the enum to the path */
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02001991 if (!(strcmp(path->name, e->texts[mux])))
1992 connect = true;
1993 else
1994 connect = false;
1995
1996 soc_dapm_connect_path(path, connect, "mux update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001997 }
1998
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001999 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002000 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002001
Liam Girdwood618dae12012-04-25 12:12:51 +01002002 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002003}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002004
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002005int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002006 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2007 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002008{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002009 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002010 int ret;
2011
Liam Girdwood3cd04342012-03-09 12:02:08 +00002012 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002013 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002014 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002015 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002016 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002017 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002018 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002019 return ret;
2020}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002021EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002022
Milan plzik1b075e32008-01-10 14:39:46 +01002023/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002024static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002025 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002026{
2027 struct snd_soc_dapm_path *path;
2028 int found = 0;
2029
Mark Brownf9fa2b12014-03-06 16:49:11 +08002030 lockdep_assert_held(&card->dapm_mutex);
2031
Richard Purdie2b97eab2006-10-06 18:32:18 +02002032 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002033 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002034 found = 1;
Lars-Peter Clausen4a201942014-10-25 17:41:56 +02002035 soc_dapm_connect_path(path, connect, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002036 }
2037
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002038 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002039 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002040
Liam Girdwood618dae12012-04-25 12:12:51 +01002041 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002042}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002043
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002044int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002045 struct snd_kcontrol *kcontrol, int connect,
2046 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002047{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002048 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002049 int ret;
2050
Liam Girdwood3cd04342012-03-09 12:02:08 +00002051 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002052 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002053 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002054 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002055 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002056 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002057 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002058 return ret;
2059}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002060EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002061
Benoit Cousson44ba2642014-07-08 23:19:36 +02002062static ssize_t dapm_widget_show_codec(struct snd_soc_codec *codec, char *buf)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002063{
Richard Purdie2b97eab2006-10-06 18:32:18 +02002064 struct snd_soc_dapm_widget *w;
2065 int count = 0;
2066 char *state = "not set";
2067
Lars-Peter Clausen00200102014-07-17 22:01:07 +02002068 list_for_each_entry(w, &codec->component.card->widgets, list) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002069 if (w->dapm != &codec->dapm)
2070 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002071
2072 /* only display widgets that burnm power */
2073 switch (w->id) {
2074 case snd_soc_dapm_hp:
2075 case snd_soc_dapm_mic:
2076 case snd_soc_dapm_spk:
2077 case snd_soc_dapm_line:
2078 case snd_soc_dapm_micbias:
2079 case snd_soc_dapm_dac:
2080 case snd_soc_dapm_adc:
2081 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002082 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002083 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002084 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002085 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002086 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002087 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002088 if (w->name)
2089 count += sprintf(buf + count, "%s: %s\n",
2090 w->name, w->power ? "On":"Off");
2091 break;
2092 default:
2093 break;
2094 }
2095 }
2096
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002097 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002098 case SND_SOC_BIAS_ON:
2099 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002100 break;
Mark Brown0be98982008-05-19 12:31:28 +02002101 case SND_SOC_BIAS_PREPARE:
2102 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002103 break;
Mark Brown0be98982008-05-19 12:31:28 +02002104 case SND_SOC_BIAS_STANDBY:
2105 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002106 break;
Mark Brown0be98982008-05-19 12:31:28 +02002107 case SND_SOC_BIAS_OFF:
2108 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002109 break;
2110 }
2111 count += sprintf(buf + count, "PM State: %s\n", state);
2112
2113 return count;
2114}
2115
Benoit Cousson44ba2642014-07-08 23:19:36 +02002116/* show dapm widget status in sys fs */
2117static ssize_t dapm_widget_show(struct device *dev,
2118 struct device_attribute *attr, char *buf)
2119{
2120 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2121 int i, count = 0;
2122
2123 for (i = 0; i < rtd->num_codecs; i++) {
2124 struct snd_soc_codec *codec = rtd->codec_dais[i]->codec;
2125 count += dapm_widget_show_codec(codec, buf + count);
2126 }
2127
2128 return count;
2129}
2130
Richard Purdie2b97eab2006-10-06 18:32:18 +02002131static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2132
Takashi Iwaid29697d2015-01-30 20:16:37 +01002133struct attribute *soc_dapm_dev_attrs[] = {
2134 &dev_attr_dapm_widget.attr,
2135 NULL
2136};
Richard Purdie2b97eab2006-10-06 18:32:18 +02002137
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002138static void dapm_free_path(struct snd_soc_dapm_path *path)
2139{
2140 list_del(&path->list_sink);
2141 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002142 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002143 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002144 kfree(path);
2145}
2146
Richard Purdie2b97eab2006-10-06 18:32:18 +02002147/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002148static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002149{
2150 struct snd_soc_dapm_widget *w, *next_w;
2151 struct snd_soc_dapm_path *p, *next_p;
2152
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002153 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2154 if (w->dapm != dapm)
2155 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002156 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002157 /*
2158 * remove source and sink paths associated to this widget.
2159 * While removing the path, remove reference to it from both
2160 * source and sink widgets so that path is removed only once.
2161 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002162 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2163 dapm_free_path(p);
2164
2165 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2166 dapm_free_path(p);
2167
Stephen Warrenfad59882011-04-28 17:37:59 -06002168 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002169 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002170 kfree(w);
2171 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002172}
2173
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002174static struct snd_soc_dapm_widget *dapm_find_widget(
2175 struct snd_soc_dapm_context *dapm, const char *pin,
2176 bool search_other_contexts)
2177{
2178 struct snd_soc_dapm_widget *w;
2179 struct snd_soc_dapm_widget *fallback = NULL;
2180
2181 list_for_each_entry(w, &dapm->card->widgets, list) {
2182 if (!strcmp(w->name, pin)) {
2183 if (w->dapm == dapm)
2184 return w;
2185 else
2186 fallback = w;
2187 }
2188 }
2189
2190 if (search_other_contexts)
2191 return fallback;
2192
2193 return NULL;
2194}
2195
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002196static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002197 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002198{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002199 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002200
Mark Brownf9fa2b12014-03-06 16:49:11 +08002201 dapm_assert_locked(dapm);
2202
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002203 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002204 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002205 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002206 }
2207
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002208 if (w->connected != status) {
Mark Brown1a8b2d92012-02-16 11:50:07 -08002209 dapm_mark_dirty(w, "pin configuration");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002210 dapm_widget_invalidate_input_paths(w);
2211 dapm_widget_invalidate_output_paths(w);
2212 }
Mark Brown1a8b2d92012-02-16 11:50:07 -08002213
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002214 w->connected = status;
2215 if (status == 0)
2216 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002217
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002218 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002219}
2220
Richard Purdie2b97eab2006-10-06 18:32:18 +02002221/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002222 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2223 * @dapm: DAPM context
2224 *
2225 * Walks all dapm audio paths and powers widgets according to their
2226 * stream or path usage.
2227 *
2228 * Requires external locking.
2229 *
2230 * Returns 0 for success.
2231 */
2232int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2233{
2234 /*
2235 * Suppress early reports (eg, jacks syncing their state) to avoid
2236 * silly DAPM runs during card startup.
2237 */
2238 if (!dapm->card || !dapm->card->instantiated)
2239 return 0;
2240
2241 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2242}
2243EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2244
2245/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002246 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002247 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002248 *
2249 * Walks all dapm audio paths and powers widgets according to their
2250 * stream or path usage.
2251 *
2252 * Returns 0 for success.
2253 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002254int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002255{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002256 int ret;
2257
Liam Girdwood3cd04342012-03-09 12:02:08 +00002258 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002259 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002260 mutex_unlock(&dapm->card->dapm_mutex);
2261 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002262}
Liam Girdwooda5302182008-07-07 13:35:17 +01002263EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002264
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002265/*
2266 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2267 * @w: The widget for which to update the flags
2268 *
2269 * Some widgets have a dynamic category which depends on which neighbors they
2270 * are connected to. This function update the category for these widgets.
2271 *
2272 * This function must be called whenever a path is added or removed to a widget.
2273 */
2274static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2275{
2276 struct snd_soc_dapm_path *p;
2277
2278 switch (w->id) {
2279 case snd_soc_dapm_input:
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002280 /* On a fully routed card a input is never a source */
2281 if (w->dapm->card->fully_routed)
2282 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002283 w->is_source = 1;
2284 list_for_each_entry(p, &w->sources, list_sink) {
2285 if (p->source->id == snd_soc_dapm_micbias ||
2286 p->source->id == snd_soc_dapm_mic ||
2287 p->source->id == snd_soc_dapm_line ||
2288 p->source->id == snd_soc_dapm_output) {
2289 w->is_source = 0;
2290 break;
2291 }
2292 }
2293 break;
2294 case snd_soc_dapm_output:
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01002295 /* On a fully routed card a output is never a sink */
2296 if (w->dapm->card->fully_routed)
2297 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002298 w->is_sink = 1;
2299 list_for_each_entry(p, &w->sinks, list_source) {
2300 if (p->sink->id == snd_soc_dapm_spk ||
2301 p->sink->id == snd_soc_dapm_hp ||
2302 p->sink->id == snd_soc_dapm_line ||
2303 p->sink->id == snd_soc_dapm_input) {
2304 w->is_sink = 0;
2305 break;
2306 }
2307 }
2308 break;
2309 case snd_soc_dapm_line:
2310 w->is_sink = !list_empty(&w->sources);
2311 w->is_source = !list_empty(&w->sinks);
2312 break;
2313 default:
2314 break;
2315 }
2316}
2317
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002318static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2319 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2320 const char *control,
2321 int (*connected)(struct snd_soc_dapm_widget *source,
2322 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002323{
2324 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002325 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002326
Lars-Peter Clausene409dfb2014-10-25 17:42:02 +02002327 if (wsink->is_supply && !wsource->is_supply) {
2328 dev_err(dapm->dev,
2329 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2330 wsource->name, wsink->name);
2331 return -EINVAL;
2332 }
2333
2334 if (connected && !wsource->is_supply) {
2335 dev_err(dapm->dev,
2336 "connected() callback only supported for supply widgets (%s -> %s)\n",
2337 wsource->name, wsink->name);
2338 return -EINVAL;
2339 }
2340
2341 if (wsource->is_supply && control) {
2342 dev_err(dapm->dev,
2343 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2344 wsource->name, control, wsink->name);
2345 return -EINVAL;
2346 }
2347
Richard Purdie2b97eab2006-10-06 18:32:18 +02002348 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2349 if (!path)
2350 return -ENOMEM;
2351
2352 path->source = wsource;
2353 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002354 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002355 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002356 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002357 INIT_LIST_HEAD(&path->list_source);
2358 INIT_LIST_HEAD(&path->list_sink);
2359
Lars-Peter Clausenc1862c82014-10-25 17:42:00 +02002360 if (wsource->is_supply || wsink->is_supply)
2361 path->is_supply = 1;
2362
Richard Purdie2b97eab2006-10-06 18:32:18 +02002363 /* connect static paths */
2364 if (control == NULL) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002365 path->connect = 1;
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002366 } else {
2367 /* connect dynamic paths */
2368 switch (wsink->id) {
2369 case snd_soc_dapm_mux:
2370 ret = dapm_connect_mux(dapm, path, control);
2371 if (ret != 0)
2372 goto err;
2373 break;
2374 case snd_soc_dapm_switch:
2375 case snd_soc_dapm_mixer:
2376 case snd_soc_dapm_mixer_named_ctl:
2377 ret = dapm_connect_mixer(dapm, path, control);
2378 if (ret != 0)
2379 goto err;
2380 break;
2381 default:
2382 dev_err(dapm->dev,
2383 "Control not supported for path %s -> [%s] -> %s\n",
2384 wsource->name, control, wsink->name);
2385 ret = -EINVAL;
2386 goto err;
2387 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002388 }
2389
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002390 list_add(&path->list, &dapm->card->paths);
2391 list_add(&path->list_sink, &wsink->sources);
2392 list_add(&path->list_source, &wsource->sinks);
2393
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002394 dapm_update_widget_flags(wsource);
2395 dapm_update_widget_flags(wsink);
2396
Lars-Peter Clausen5fe5b762014-10-25 17:41:58 +02002397 dapm_mark_dirty(wsource, "Route added");
2398 dapm_mark_dirty(wsink, "Route added");
Mark Brownfabd0382012-07-05 17:20:06 +01002399
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002400 if (dapm->card->instantiated && path->connect)
2401 dapm_path_invalidate(path);
2402
Richard Purdie2b97eab2006-10-06 18:32:18 +02002403 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002404err:
2405 kfree(path);
2406 return ret;
2407}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002408
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002409static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002410 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002411{
2412 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2413 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2414 const char *sink;
2415 const char *source;
2416 char prefixed_sink[80];
2417 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002418 const char *prefix;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002419 int ret;
2420
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002421 prefix = soc_dapm_prefix(dapm);
2422 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002423 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002424 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002425 sink = prefixed_sink;
2426 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002427 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002428 source = prefixed_source;
2429 } else {
2430 sink = route->sink;
2431 source = route->source;
2432 }
2433
2434 /*
2435 * find src and dest widgets over all widgets but favor a widget from
2436 * current DAPM context
2437 */
2438 list_for_each_entry(w, &dapm->card->widgets, list) {
2439 if (!wsink && !(strcmp(w->name, sink))) {
2440 wtsink = w;
2441 if (w->dapm == dapm)
2442 wsink = w;
2443 continue;
2444 }
2445 if (!wsource && !(strcmp(w->name, source))) {
2446 wtsource = w;
2447 if (w->dapm == dapm)
2448 wsource = w;
2449 }
2450 }
2451 /* use widget from another DAPM context if not found from this */
2452 if (!wsink)
2453 wsink = wtsink;
2454 if (!wsource)
2455 wsource = wtsource;
2456
2457 if (wsource == NULL) {
2458 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2459 route->source);
2460 return -ENODEV;
2461 }
2462 if (wsink == NULL) {
2463 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2464 route->sink);
2465 return -ENODEV;
2466 }
2467
2468 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2469 route->connected);
2470 if (ret)
2471 goto err;
2472
2473 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002474err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002475 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002476 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002477 return ret;
2478}
Mark Brown105f1c22008-05-13 14:52:19 +02002479
Mark Brownefcc3c62012-07-05 17:24:19 +01002480static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2481 const struct snd_soc_dapm_route *route)
2482{
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002483 struct snd_soc_dapm_widget *wsource, *wsink;
Mark Brownefcc3c62012-07-05 17:24:19 +01002484 struct snd_soc_dapm_path *path, *p;
2485 const char *sink;
2486 const char *source;
2487 char prefixed_sink[80];
2488 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002489 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002490
2491 if (route->control) {
2492 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002493 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002494 return -EINVAL;
2495 }
2496
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002497 prefix = soc_dapm_prefix(dapm);
2498 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002499 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002500 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002501 sink = prefixed_sink;
2502 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002503 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002504 source = prefixed_source;
2505 } else {
2506 sink = route->sink;
2507 source = route->source;
2508 }
2509
2510 path = NULL;
2511 list_for_each_entry(p, &dapm->card->paths, list) {
2512 if (strcmp(p->source->name, source) != 0)
2513 continue;
2514 if (strcmp(p->sink->name, sink) != 0)
2515 continue;
2516 path = p;
2517 break;
2518 }
2519
2520 if (path) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002521 wsource = path->source;
2522 wsink = path->sink;
2523
2524 dapm_mark_dirty(wsource, "Route removed");
2525 dapm_mark_dirty(wsink, "Route removed");
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02002526 if (path->connect)
2527 dapm_path_invalidate(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002528
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002529 dapm_free_path(path);
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02002530
2531 /* Update any path related flags */
2532 dapm_update_widget_flags(wsource);
2533 dapm_update_widget_flags(wsink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002534 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002535 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002536 source, sink);
2537 }
2538
2539 return 0;
2540}
2541
Mark Brown105f1c22008-05-13 14:52:19 +02002542/**
Mark Brown105f1c22008-05-13 14:52:19 +02002543 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002544 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002545 * @route: audio routes
2546 * @num: number of routes
2547 *
2548 * Connects 2 dapm widgets together via a named audio path. The sink is
2549 * the widget receiving the audio signal, whilst the source is the sender
2550 * of the audio signal.
2551 *
2552 * Returns 0 for success else error. On error all resources can be freed
2553 * with a call to snd_soc_card_free().
2554 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002555int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002556 const struct snd_soc_dapm_route *route, int num)
2557{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002558 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002559
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002560 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002561 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002562 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002563 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002564 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2565 route->source,
2566 route->control ? route->control : "direct",
2567 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002568 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002569 }
2570 route++;
2571 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002572 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002573
Dan Carpenter60884c22012-04-13 22:25:43 +03002574 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002575}
2576EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2577
Mark Brownefcc3c62012-07-05 17:24:19 +01002578/**
2579 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2580 * @dapm: DAPM context
2581 * @route: audio routes
2582 * @num: number of routes
2583 *
2584 * Removes routes from the DAPM context.
2585 */
2586int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2587 const struct snd_soc_dapm_route *route, int num)
2588{
2589 int i, ret = 0;
2590
2591 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2592 for (i = 0; i < num; i++) {
2593 snd_soc_dapm_del_route(dapm, route);
2594 route++;
2595 }
2596 mutex_unlock(&dapm->card->dapm_mutex);
2597
2598 return ret;
2599}
2600EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2601
Mark Brownbf3a9e12011-06-13 16:42:29 +01002602static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2603 const struct snd_soc_dapm_route *route)
2604{
2605 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2606 route->source,
2607 true);
2608 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2609 route->sink,
2610 true);
2611 struct snd_soc_dapm_path *path;
2612 int count = 0;
2613
2614 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002615 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002616 route->source);
2617 return -ENODEV;
2618 }
2619
2620 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002621 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002622 route->sink);
2623 return -ENODEV;
2624 }
2625
2626 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002627 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002628 route->source, route->sink);
2629
2630 list_for_each_entry(path, &source->sinks, list_source) {
2631 if (path->sink == sink) {
2632 path->weak = 1;
2633 count++;
2634 }
2635 }
2636
2637 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002638 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002639 route->source, route->sink);
2640 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002641 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002642 count, route->source, route->sink);
2643
2644 return 0;
2645}
2646
2647/**
2648 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2649 * @dapm: DAPM context
2650 * @route: audio routes
2651 * @num: number of routes
2652 *
2653 * Mark existing routes matching those specified in the passed array
2654 * as being weak, meaning that they are ignored for the purpose of
2655 * power decisions. The main intended use case is for sidetone paths
2656 * which couple audio between other independent paths if they are both
2657 * active in order to make the combination work better at the user
2658 * level but which aren't intended to be "used".
2659 *
2660 * Note that CODEC drivers should not use this as sidetone type paths
2661 * can frequently also be used as bypass paths.
2662 */
2663int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2664 const struct snd_soc_dapm_route *route, int num)
2665{
2666 int i, err;
2667 int ret = 0;
2668
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002669 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002670 for (i = 0; i < num; i++) {
2671 err = snd_soc_dapm_weak_route(dapm, route);
2672 if (err)
2673 ret = err;
2674 route++;
2675 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002676 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002677
2678 return ret;
2679}
2680EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2681
Mark Brown105f1c22008-05-13 14:52:19 +02002682/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002683 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002684 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002685 *
2686 * Checks the codec for any new dapm widgets and creates them if found.
2687 *
2688 * Returns 0 for success.
2689 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002690int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002691{
2692 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002693 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002694
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002695 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002696
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002697 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002698 {
2699 if (w->new)
2700 continue;
2701
Stephen Warrenfad59882011-04-28 17:37:59 -06002702 if (w->num_kcontrols) {
2703 w->kcontrols = kzalloc(w->num_kcontrols *
2704 sizeof(struct snd_kcontrol *),
2705 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002706 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002707 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002708 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002709 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002710 }
2711
Richard Purdie2b97eab2006-10-06 18:32:18 +02002712 switch(w->id) {
2713 case snd_soc_dapm_switch:
2714 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002715 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002716 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002717 break;
2718 case snd_soc_dapm_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002719 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002720 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002721 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002722 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002723 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002724 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002725 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002726 break;
2727 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002728
2729 /* Read the initial power state from the device */
2730 if (w->reg >= 0) {
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002731 soc_dapm_read(w->dapm, w->reg, &val);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002732 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002733 val &= w->mask;
2734 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002735 w->power = 1;
2736 }
2737
Richard Purdie2b97eab2006-10-06 18:32:18 +02002738 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002739
Mark Brown7508b122011-10-05 12:09:12 +01002740 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002741 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002742 }
2743
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002744 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2745 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002746 return 0;
2747}
2748EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2749
2750/**
2751 * snd_soc_dapm_get_volsw - dapm mixer get callback
2752 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002753 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002754 *
2755 * Callback to get the value of a dapm mixer control.
2756 *
2757 * Returns 0 for success.
2758 */
2759int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2760 struct snd_ctl_elem_value *ucontrol)
2761{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002762 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2763 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002764 struct soc_mixer_control *mc =
2765 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002766 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002767 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002768 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002769 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002770 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002771 unsigned int val;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002772 int ret = 0;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002773
2774 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002775 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002776 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002777 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002778
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002779 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002780 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
2781 ret = soc_dapm_read(dapm, reg, &val);
2782 val = (val >> shift) & mask;
2783 } else {
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002784 val = dapm_kcontrol_get_value(kcontrol);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002785 }
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002786 mutex_unlock(&card->dapm_mutex);
2787
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002788 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002789 ucontrol->value.integer.value[0] = max - val;
2790 else
2791 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002792
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002793 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002794}
2795EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2796
2797/**
2798 * snd_soc_dapm_put_volsw - dapm mixer set callback
2799 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002800 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002801 *
2802 * Callback to set the value of a dapm mixer control.
2803 *
2804 * Returns 0 for success.
2805 */
2806int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2807 struct snd_ctl_elem_value *ucontrol)
2808{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002809 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2810 struct snd_soc_card *card = dapm->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002811 struct soc_mixer_control *mc =
2812 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002813 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002814 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002815 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002816 unsigned int mask = (1 << fls(max)) - 1;
2817 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002818 unsigned int val;
Jarkko Nikula18626c72014-06-09 14:20:29 +03002819 int connect, change, reg_change = 0;
Mark Brown97404f22010-12-14 16:13:57 +00002820 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002821 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002822
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002823 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002824 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002825 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002826 kcontrol->id.name);
2827
Richard Purdie2b97eab2006-10-06 18:32:18 +02002828 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002829 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002830
2831 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002832 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002833
Liam Girdwood3cd04342012-03-09 12:02:08 +00002834 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002835
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002836 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown283375c2009-12-07 18:09:03 +00002837
Jarkko Nikula18626c72014-06-09 14:20:29 +03002838 if (reg != SND_SOC_NOPM) {
2839 mask = mask << shift;
2840 val = val << shift;
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02002841
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002842 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
Jarkko Nikula18626c72014-06-09 14:20:29 +03002843 }
2844
2845 if (change || reg_change) {
2846 if (reg_change) {
2847 update.kcontrol = kcontrol;
2848 update.reg = reg;
2849 update.mask = mask;
2850 update.val = val;
2851 card->update = &update;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002852 }
Jarkko Nikula18626c72014-06-09 14:20:29 +03002853 change |= reg_change;
Mark Brown97404f22010-12-14 16:13:57 +00002854
Nenghua Cao52765972013-12-13 20:13:49 +08002855 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002856
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002857 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002858 }
2859
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002860 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002861
2862 if (ret > 0)
2863 soc_dpcm_runtime_update(card);
2864
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002865 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002866}
2867EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2868
2869/**
2870 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2871 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002872 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002873 *
2874 * Callback to get the value of a dapm enumerated double mixer control.
2875 *
2876 * Returns 0 for success.
2877 */
2878int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2879 struct snd_ctl_elem_value *ucontrol)
2880{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002881 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002882 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002883 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002884
Geert Uytterhoeven69128312014-08-08 17:29:35 +02002885 if (e->reg != SND_SOC_NOPM) {
2886 int ret = soc_dapm_read(dapm, e->reg, &reg_val);
2887 if (ret)
2888 return ret;
2889 } else {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002890 reg_val = dapm_kcontrol_get_value(kcontrol);
Geert Uytterhoeven69128312014-08-08 17:29:35 +02002891 }
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002892
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002893 val = (reg_val >> e->shift_l) & e->mask;
2894 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2895 if (e->shift_l != e->shift_r) {
2896 val = (reg_val >> e->shift_r) & e->mask;
2897 val = snd_soc_enum_val_to_item(e, val);
2898 ucontrol->value.enumerated.item[1] = val;
2899 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002900
Geert Uytterhoeven69128312014-08-08 17:29:35 +02002901 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002902}
2903EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2904
2905/**
2906 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2907 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002908 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002909 *
2910 * Callback to set the value of a dapm enumerated double mixer control.
2911 *
2912 * Returns 0 for success.
2913 */
2914int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2915 struct snd_ctl_elem_value *ucontrol)
2916{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002917 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2918 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002919 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002920 unsigned int *item = ucontrol->value.enumerated.item;
2921 unsigned int val, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002922 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002923 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002924 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002925
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002926 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002927 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002928
2929 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002930 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002931 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002932 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002933 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002934 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002935 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002936 }
2937
Liam Girdwood3cd04342012-03-09 12:02:08 +00002938 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002939
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002940 if (e->reg != SND_SOC_NOPM)
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002941 change = soc_dapm_test_bits(dapm, e->reg, mask, val);
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002942 else
2943 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00002944
Richard Purdie2b97eab2006-10-06 18:32:18 +02002945 if (change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002946 if (e->reg != SND_SOC_NOPM) {
2947 update.kcontrol = kcontrol;
2948 update.reg = e->reg;
2949 update.mask = mask;
2950 update.val = val;
2951 card->update = &update;
2952 }
Mark Brown3a655772009-10-05 17:23:30 +01002953
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002954 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002955
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002956 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002957 }
2958
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002959 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002960
2961 if (ret > 0)
2962 soc_dpcm_runtime_update(card);
2963
Mark Brown97404f22010-12-14 16:13:57 +00002964 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002965}
2966EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2967
2968/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002969 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2970 *
2971 * @kcontrol: mixer control
2972 * @uinfo: control element information
2973 *
2974 * Callback to provide information about a pin switch control.
2975 */
2976int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2977 struct snd_ctl_elem_info *uinfo)
2978{
2979 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2980 uinfo->count = 1;
2981 uinfo->value.integer.min = 0;
2982 uinfo->value.integer.max = 1;
2983
2984 return 0;
2985}
2986EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2987
2988/**
2989 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2990 *
2991 * @kcontrol: mixer control
2992 * @ucontrol: Value
2993 */
2994int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2995 struct snd_ctl_elem_value *ucontrol)
2996{
Mark Brown48a8c392012-02-14 17:11:15 -08002997 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002998 const char *pin = (const char *)kcontrol->private_value;
2999
Liam Girdwood3cd04342012-03-09 12:02:08 +00003000 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003001
3002 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003003 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003004
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003005 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003006
3007 return 0;
3008}
3009EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3010
3011/**
3012 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3013 *
3014 * @kcontrol: mixer control
3015 * @ucontrol: Value
3016 */
3017int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3018 struct snd_ctl_elem_value *ucontrol)
3019{
Mark Brown48a8c392012-02-14 17:11:15 -08003020 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003021 const char *pin = (const char *)kcontrol->private_value;
3022
Mark Brown8b37dbd2009-02-28 21:14:20 +00003023 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003024 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003025 else
Mark Brown48a8c392012-02-14 17:11:15 -08003026 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003027
Mark Brown48a8c392012-02-14 17:11:15 -08003028 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003029 return 0;
3030}
3031EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3032
Mark Brown5ba06fc2012-02-16 11:07:13 -08003033static struct snd_soc_dapm_widget *
3034snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3035 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003036{
3037 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003038 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00003039 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003040
3041 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003042 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003043
Mark Brown62ea8742012-01-21 21:14:48 +00003044 switch (w->id) {
3045 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003046 w->regulator = devm_regulator_get(dapm->dev, w->name);
3047 if (IS_ERR(w->regulator)) {
3048 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003049 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003050 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003051 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003052 }
Mark Brown8784c772013-01-10 19:33:47 +00003053
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003054 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003055 ret = regulator_allow_bypass(w->regulator, true);
3056 if (ret != 0)
3057 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003058 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003059 w->name, ret);
3060 }
Mark Brown62ea8742012-01-21 21:14:48 +00003061 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003062 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003063#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003064 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003065 if (IS_ERR(w->clk)) {
3066 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003067 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003068 w->name, ret);
3069 return NULL;
3070 }
Mark Brownec029952012-06-04 08:16:20 +01003071#else
3072 return NULL;
3073#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003074 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003075 default:
3076 break;
3077 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003078
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003079 prefix = soc_dapm_prefix(dapm);
3080 if (prefix)
3081 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003082 else
3083 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3084
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003085 if (w->name == NULL) {
3086 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003087 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003088 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003089
Mark Brown7ca3a182011-10-08 14:04:50 +01003090 switch (w->id) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003091 case snd_soc_dapm_mic:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003092 w->is_source = 1;
3093 w->power_check = dapm_generic_check_power;
3094 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003095 case snd_soc_dapm_input:
3096 if (!dapm->card->fully_routed)
3097 w->is_source = 1;
3098 w->power_check = dapm_generic_check_power;
3099 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003100 case snd_soc_dapm_spk:
3101 case snd_soc_dapm_hp:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003102 w->is_sink = 1;
3103 w->power_check = dapm_generic_check_power;
3104 break;
Lars-Peter Clausen86d75002014-12-21 11:05:44 +01003105 case snd_soc_dapm_output:
3106 if (!dapm->card->fully_routed)
3107 w->is_sink = 1;
3108 w->power_check = dapm_generic_check_power;
3109 break;
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003110 case snd_soc_dapm_vmid:
3111 case snd_soc_dapm_siggen:
3112 w->is_source = 1;
3113 w->power_check = dapm_always_on_check_power;
3114 break;
3115 case snd_soc_dapm_mux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003116 case snd_soc_dapm_switch:
3117 case snd_soc_dapm_mixer:
3118 case snd_soc_dapm_mixer_named_ctl:
Mark Brown63c69a62013-07-18 22:03:01 +01003119 case snd_soc_dapm_adc:
3120 case snd_soc_dapm_aif_out:
3121 case snd_soc_dapm_dac:
3122 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003123 case snd_soc_dapm_pga:
3124 case snd_soc_dapm_out_drv:
Mark Brown7ca3a182011-10-08 14:04:50 +01003125 case snd_soc_dapm_micbias:
Mark Brown7ca3a182011-10-08 14:04:50 +01003126 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003127 case snd_soc_dapm_dai_link:
Lars-Peter Clausencdef2ad2014-10-20 19:36:38 +02003128 case snd_soc_dapm_dai_out:
3129 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003130 w->power_check = dapm_generic_check_power;
3131 break;
3132 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003133 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003134 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003135 case snd_soc_dapm_kcontrol:
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003136 w->is_supply = 1;
Mark Brown7ca3a182011-10-08 14:04:50 +01003137 w->power_check = dapm_supply_check_power;
3138 break;
3139 default:
3140 w->power_check = dapm_always_on_check_power;
3141 break;
3142 }
3143
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003144 w->dapm = dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003145 INIT_LIST_HEAD(&w->sources);
3146 INIT_LIST_HEAD(&w->sinks);
3147 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003148 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003149 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003150
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003151 w->inputs = -1;
3152 w->outputs = -1;
3153
Richard Purdie2b97eab2006-10-06 18:32:18 +02003154 /* machine layer set ups unconnected pins and insertions */
3155 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003156 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003157}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003158
3159/**
Mark Brown4ba13272008-05-13 14:51:19 +02003160 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003161 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003162 * @widget: widget array
3163 * @num: number of widgets
3164 *
3165 * Creates new DAPM controls based upon the templates.
3166 *
3167 * Returns 0 for success else error.
3168 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003169int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003170 const struct snd_soc_dapm_widget *widget,
3171 int num)
3172{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003173 struct snd_soc_dapm_widget *w;
3174 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003175 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003176
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003177 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003178 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003179 w = snd_soc_dapm_new_control(dapm, widget);
3180 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003181 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003182 "ASoC: Failed to create DAPM control %s\n",
3183 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003184 ret = -ENOMEM;
3185 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003186 }
Mark Brown4ba13272008-05-13 14:51:19 +02003187 widget++;
3188 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003189 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003190 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003191}
3192EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3193
Mark Brownc74184e2012-04-04 22:12:09 +01003194static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3195 struct snd_kcontrol *kcontrol, int event)
3196{
3197 struct snd_soc_dapm_path *source_p, *sink_p;
3198 struct snd_soc_dai *source, *sink;
3199 const struct snd_soc_pcm_stream *config = w->params;
3200 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003201 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003202 u64 fmt;
3203 int ret;
3204
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003205 if (WARN_ON(!config) ||
3206 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3207 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003208
3209 /* We only support a single source and sink, pick the first */
3210 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3211 list_sink);
3212 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3213 list_source);
3214
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003215 if (WARN_ON(!source_p || !sink_p) ||
3216 WARN_ON(!sink_p->source || !source_p->sink) ||
3217 WARN_ON(!source_p->source || !sink_p->sink))
3218 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003219
3220 source = source_p->source->priv;
3221 sink = sink_p->sink->priv;
3222
3223 /* Be a little careful as we don't want to overflow the mask array */
3224 if (config->formats) {
3225 fmt = ffs(config->formats) - 1;
3226 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003227 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003228 config->formats);
3229 fmt = 0;
3230 }
3231
3232 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003233 params = kzalloc(sizeof(*params), GFP_KERNEL);
3234 if (!params) {
3235 ret = -ENOMEM;
3236 goto out;
3237 }
3238 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003239
Mark Brown9747cec2012-04-26 19:12:21 +01003240 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003241 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003242 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003243 config->rate_max;
3244
Mark Brown9747cec2012-04-26 19:12:21 +01003245 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003246 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003247 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003248 = config->channels_max;
3249
3250 memset(&substream, 0, sizeof(substream));
3251
3252 switch (event) {
3253 case SND_SOC_DAPM_PRE_PMU:
Benoit Cousson93e69582014-07-08 23:19:38 +02003254 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3255 ret = soc_dai_hw_params(&substream, params, source);
3256 if (ret < 0)
3257 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003258
Benoit Cousson93e69582014-07-08 23:19:38 +02003259 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3260 ret = soc_dai_hw_params(&substream, params, sink);
3261 if (ret < 0)
3262 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003263 break;
3264
3265 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003266 ret = snd_soc_dai_digital_mute(sink, 0,
3267 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003268 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003269 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003270 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003271 break;
3272
3273 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003274 ret = snd_soc_dai_digital_mute(sink, 1,
3275 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003276 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003277 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003278 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003279 break;
3280
3281 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003282 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003283 return -EINVAL;
3284 }
3285
Mark Brown9747cec2012-04-26 19:12:21 +01003286out:
3287 kfree(params);
3288 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003289}
3290
3291int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3292 const struct snd_soc_pcm_stream *params,
3293 struct snd_soc_dapm_widget *source,
3294 struct snd_soc_dapm_widget *sink)
3295{
Mark Brownc74184e2012-04-04 22:12:09 +01003296 struct snd_soc_dapm_widget template;
3297 struct snd_soc_dapm_widget *w;
3298 size_t len;
3299 char *link_name;
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003300 int ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003301
3302 len = strlen(source->name) + strlen(sink->name) + 2;
3303 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3304 if (!link_name)
3305 return -ENOMEM;
3306 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3307
3308 memset(&template, 0, sizeof(template));
3309 template.reg = SND_SOC_NOPM;
3310 template.id = snd_soc_dapm_dai_link;
3311 template.name = link_name;
3312 template.event = snd_soc_dai_link_event;
3313 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3314 SND_SOC_DAPM_PRE_PMD;
3315
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003316 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003317
3318 w = snd_soc_dapm_new_control(&card->dapm, &template);
3319 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003320 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003321 link_name);
3322 return -ENOMEM;
3323 }
3324
3325 w->params = params;
3326
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003327 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3328 if (ret)
3329 return ret;
3330 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
Mark Brownc74184e2012-04-04 22:12:09 +01003331}
3332
Mark Brown888df392012-02-16 19:37:51 -08003333int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3334 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003335{
Mark Brown888df392012-02-16 19:37:51 -08003336 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003337 struct snd_soc_dapm_widget *w;
3338
Mark Brown888df392012-02-16 19:37:51 -08003339 WARN_ON(dapm->dev != dai->dev);
3340
3341 memset(&template, 0, sizeof(template));
3342 template.reg = SND_SOC_NOPM;
3343
3344 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003345 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003346 template.name = dai->driver->playback.stream_name;
3347 template.sname = dai->driver->playback.stream_name;
3348
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003349 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003350 template.name);
3351
3352 w = snd_soc_dapm_new_control(dapm, &template);
3353 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003354 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003355 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003356 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003357 }
3358
3359 w->priv = dai;
3360 dai->playback_widget = w;
3361 }
3362
3363 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003364 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003365 template.name = dai->driver->capture.stream_name;
3366 template.sname = dai->driver->capture.stream_name;
3367
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003368 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003369 template.name);
3370
3371 w = snd_soc_dapm_new_control(dapm, &template);
3372 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003373 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003374 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003375 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003376 }
3377
3378 w->priv = dai;
3379 dai->capture_widget = w;
3380 }
3381
3382 return 0;
3383}
3384
3385int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3386{
3387 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003388 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08003389 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003390
3391 /* For each DAI widget... */
3392 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003393 switch (dai_w->id) {
3394 case snd_soc_dapm_dai_in:
3395 case snd_soc_dapm_dai_out:
3396 break;
3397 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003398 continue;
Mark Brown46162742013-06-05 19:36:11 +01003399 }
Mark Brown888df392012-02-16 19:37:51 -08003400
3401 dai = dai_w->priv;
3402
3403 /* ...find all widgets with the same stream and link them */
3404 list_for_each_entry(w, &card->widgets, list) {
3405 if (w->dapm != dai_w->dapm)
3406 continue;
3407
Mark Brown46162742013-06-05 19:36:11 +01003408 switch (w->id) {
3409 case snd_soc_dapm_dai_in:
3410 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003411 continue;
Mark Brown46162742013-06-05 19:36:11 +01003412 default:
3413 break;
3414 }
Mark Brown888df392012-02-16 19:37:51 -08003415
Russell King19c2c5f2013-08-04 20:24:03 +01003416 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003417 continue;
3418
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003419 if (dai_w->id == snd_soc_dapm_dai_in) {
3420 src = dai_w;
3421 sink = w;
3422 } else {
3423 src = w;
3424 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08003425 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003426 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3427 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003428 }
3429 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003430
Mark Brown888df392012-02-16 19:37:51 -08003431 return 0;
3432}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003433
Benoit Cousson44ba2642014-07-08 23:19:36 +02003434static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3435 struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003436{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003437 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003438 struct snd_soc_dapm_widget *sink, *source;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003439 int i;
3440
Benoit Cousson44ba2642014-07-08 23:19:36 +02003441 for (i = 0; i < rtd->num_codecs; i++) {
3442 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003443
3444 /* there is no point in connecting BE DAI links with dummies */
3445 if (snd_soc_dai_is_dummy(codec_dai) ||
3446 snd_soc_dai_is_dummy(cpu_dai))
3447 continue;
3448
3449 /* connect BE DAI playback if widgets are valid */
3450 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003451 source = cpu_dai->playback_widget;
3452 sink = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003453 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003454 cpu_dai->component->name, source->name,
3455 codec_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003456
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003457 snd_soc_dapm_add_path(&card->dapm, source, sink,
3458 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003459 }
3460
3461 /* connect BE DAI capture if widgets are valid */
3462 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003463 source = codec_dai->capture_widget;
3464 sink = cpu_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003465 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003466 codec_dai->component->name, source->name,
3467 cpu_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003468
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003469 snd_soc_dapm_add_path(&card->dapm, source, sink,
3470 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003471 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003472 }
3473}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003474
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003475static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3476 int event)
3477{
3478 struct snd_soc_dapm_widget *w;
3479
3480 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3481 w = dai->playback_widget;
3482 else
3483 w = dai->capture_widget;
3484
3485 if (w) {
3486 dapm_mark_dirty(w, "stream event");
3487
3488 switch (event) {
3489 case SND_SOC_DAPM_STREAM_START:
3490 w->active = 1;
3491 break;
3492 case SND_SOC_DAPM_STREAM_STOP:
3493 w->active = 0;
3494 break;
3495 case SND_SOC_DAPM_STREAM_SUSPEND:
3496 case SND_SOC_DAPM_STREAM_RESUME:
3497 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3498 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3499 break;
3500 }
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003501
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003502 if (w->id == snd_soc_dapm_dai_in) {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003503 w->is_source = w->active;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003504 dapm_widget_invalidate_input_paths(w);
3505 } else {
Lars-Peter Clausen6dd98b02014-10-25 17:41:59 +02003506 w->is_sink = w->active;
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003507 dapm_widget_invalidate_output_paths(w);
3508 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003509 }
3510}
3511
Benoit Cousson44ba2642014-07-08 23:19:36 +02003512void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3513{
3514 struct snd_soc_pcm_runtime *rtd = card->rtd;
3515 int i;
3516
3517 /* for each BE DAI link... */
3518 for (i = 0; i < card->num_rtd; i++) {
3519 rtd = &card->rtd[i];
3520
3521 /*
3522 * dynamic FE links have no fixed DAI mapping.
3523 * CODEC<->CODEC links have no direct connection.
3524 */
3525 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3526 continue;
3527
3528 dapm_connect_dai_link_widgets(card, rtd);
3529 }
3530}
3531
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003532static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3533 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003534{
Benoit Cousson44ba2642014-07-08 23:19:36 +02003535 int i;
3536
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003537 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
Benoit Cousson44ba2642014-07-08 23:19:36 +02003538 for (i = 0; i < rtd->num_codecs; i++)
3539 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003540
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003541 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003542}
3543
3544/**
3545 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3546 * @rtd: PCM runtime data
3547 * @stream: stream name
3548 * @event: stream event
3549 *
3550 * Sends a stream event to the dapm core. The core then makes any
3551 * necessary widget power changes.
3552 *
3553 * Returns 0 for success else error.
3554 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003555void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3556 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003557{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003558 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003559
Liam Girdwood3cd04342012-03-09 12:02:08 +00003560 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003561 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003562 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003563}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003564
3565/**
Charles Keepax11391102014-02-18 15:22:14 +00003566 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3567 * @dapm: DAPM context
3568 * @pin: pin name
3569 *
3570 * Enables input/output pin and its parents or children widgets iff there is
3571 * a valid audio route and active audio stream.
3572 *
3573 * Requires external locking.
3574 *
3575 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3576 * do any widget power switching.
3577 */
3578int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3579 const char *pin)
3580{
3581 return snd_soc_dapm_set_pin(dapm, pin, 1);
3582}
3583EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3584
3585/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003586 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003587 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003588 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003589 *
Mark Brown74b8f952009-06-06 11:26:15 +01003590 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003591 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003592 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003593 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3594 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003595 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003596int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003597{
Charles Keepax11391102014-02-18 15:22:14 +00003598 int ret;
3599
3600 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3601
3602 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3603
3604 mutex_unlock(&dapm->card->dapm_mutex);
3605
3606 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003607}
Liam Girdwooda5302182008-07-07 13:35:17 +01003608EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003609
3610/**
Charles Keepax11391102014-02-18 15:22:14 +00003611 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3612 * @dapm: DAPM context
3613 * @pin: pin name
3614 *
3615 * Enables input/output pin regardless of any other state. This is
3616 * intended for use with microphone bias supplies used in microphone
3617 * jack detection.
3618 *
3619 * Requires external locking.
3620 *
3621 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3622 * do any widget power switching.
3623 */
3624int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3625 const char *pin)
3626{
3627 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3628
3629 if (!w) {
3630 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3631 return -EINVAL;
3632 }
3633
3634 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen92a99ea2014-10-25 17:42:03 +02003635 if (!w->connected) {
3636 /*
3637 * w->force does not affect the number of input or output paths,
3638 * so we only have to recheck if w->connected is changed
3639 */
3640 dapm_widget_invalidate_input_paths(w);
3641 dapm_widget_invalidate_output_paths(w);
3642 w->connected = 1;
3643 }
Charles Keepax11391102014-02-18 15:22:14 +00003644 w->force = 1;
3645 dapm_mark_dirty(w, "force enable");
3646
3647 return 0;
3648}
3649EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3650
3651/**
Mark Brownda341832010-03-15 19:23:37 +00003652 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003653 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003654 * @pin: pin name
3655 *
3656 * Enables input/output pin regardless of any other state. This is
3657 * intended for use with microphone bias supplies used in microphone
3658 * jack detection.
3659 *
3660 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3661 * do any widget power switching.
3662 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003663int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3664 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003665{
Charles Keepax11391102014-02-18 15:22:14 +00003666 int ret;
Mark Brownda341832010-03-15 19:23:37 +00003667
Charles Keepax11391102014-02-18 15:22:14 +00003668 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00003669
Charles Keepax11391102014-02-18 15:22:14 +00003670 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09003671
Charles Keepax11391102014-02-18 15:22:14 +00003672 mutex_unlock(&dapm->card->dapm_mutex);
3673
3674 return ret;
Mark Brownda341832010-03-15 19:23:37 +00003675}
3676EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3677
3678/**
Charles Keepax11391102014-02-18 15:22:14 +00003679 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3680 * @dapm: DAPM context
3681 * @pin: pin name
3682 *
3683 * Disables input/output pin and its parents or children widgets.
3684 *
3685 * Requires external locking.
3686 *
3687 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3688 * do any widget power switching.
3689 */
3690int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3691 const char *pin)
3692{
3693 return snd_soc_dapm_set_pin(dapm, pin, 0);
3694}
3695EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3696
3697/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003698 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003699 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003700 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003701 *
Mark Brown74b8f952009-06-06 11:26:15 +01003702 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00003703 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003704 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3705 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003706 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003707int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3708 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003709{
Charles Keepax11391102014-02-18 15:22:14 +00003710 int ret;
3711
3712 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3713
3714 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3715
3716 mutex_unlock(&dapm->card->dapm_mutex);
3717
3718 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01003719}
3720EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3721
3722/**
Charles Keepax11391102014-02-18 15:22:14 +00003723 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3724 * @dapm: DAPM context
3725 * @pin: pin name
3726 *
3727 * Marks the specified pin as being not connected, disabling it along
3728 * any parent or child widgets. At present this is identical to
3729 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3730 * additional things such as disabling controls which only affect
3731 * paths through the pin.
3732 *
3733 * Requires external locking.
3734 *
3735 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3736 * do any widget power switching.
3737 */
3738int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3739 const char *pin)
3740{
3741 return snd_soc_dapm_set_pin(dapm, pin, 0);
3742}
3743EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3744
3745/**
Mark Brown5817b522008-09-24 11:23:11 +01003746 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003747 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003748 * @pin: pin name
3749 *
3750 * Marks the specified pin as being not connected, disabling it along
3751 * any parent or child widgets. At present this is identical to
3752 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3753 * additional things such as disabling controls which only affect
3754 * paths through the pin.
3755 *
3756 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3757 * do any widget power switching.
3758 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003759int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003760{
Charles Keepax11391102014-02-18 15:22:14 +00003761 int ret;
3762
3763 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3764
3765 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3766
3767 mutex_unlock(&dapm->card->dapm_mutex);
3768
3769 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01003770}
3771EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3772
3773/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003774 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003775 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003776 * @pin: audio signal pin endpoint (or start point)
3777 *
3778 * Get audio pin status - connected or disconnected.
3779 *
3780 * Returns 1 for connected otherwise 0.
3781 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003782int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3783 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003784{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003785 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003786
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003787 if (w)
3788 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003789
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003790 return 0;
3791}
Liam Girdwooda5302182008-07-07 13:35:17 +01003792EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003793
3794/**
Mark Brown1547aba2010-05-07 21:11:40 +01003795 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003796 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003797 * @pin: audio signal pin endpoint (or start point)
3798 *
3799 * Mark the given endpoint or pin as ignoring suspend. When the
3800 * system is disabled a path between two endpoints flagged as ignoring
3801 * suspend will not be disabled. The path must already be enabled via
3802 * normal means at suspend time, it will not be turned on if it was not
3803 * already enabled.
3804 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003805int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3806 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003807{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003808 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003809
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003810 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003811 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003812 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003813 }
3814
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003815 w->ignore_suspend = 1;
3816
3817 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003818}
3819EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3820
Lars-Peter Clausencdc45082014-10-20 19:36:33 +02003821/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003822 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003823 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003824 *
3825 * Free all dapm widgets and resources.
3826 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003827void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003828{
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003829 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003830 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003831 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003832}
3833EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3834
Xiang Xiao57996352014-03-02 00:04:02 +08003835static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003836{
Liam Girdwood01005a72012-07-06 16:57:05 +01003837 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003838 struct snd_soc_dapm_widget *w;
3839 LIST_HEAD(down_list);
3840 int powerdown = 0;
3841
Liam Girdwood01005a72012-07-06 16:57:05 +01003842 mutex_lock(&card->dapm_mutex);
3843
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003844 list_for_each_entry(w, &dapm->card->widgets, list) {
3845 if (w->dapm != dapm)
3846 continue;
Mark Brown51737472009-06-22 13:16:51 +01003847 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003848 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003849 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003850 powerdown = 1;
3851 }
3852 }
3853
3854 /* If there were no widgets to power down we're already in
3855 * standby.
3856 */
3857 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003858 if (dapm->bias_level == SND_SOC_BIAS_ON)
3859 snd_soc_dapm_set_bias_level(dapm,
3860 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003861 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003862 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3863 snd_soc_dapm_set_bias_level(dapm,
3864 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003865 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003866
3867 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003868}
Mark Brown51737472009-06-22 13:16:51 +01003869
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003870/*
3871 * snd_soc_dapm_shutdown - callback for system shutdown
3872 */
3873void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3874{
Xiang Xiao57996352014-03-02 00:04:02 +08003875 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003876
Xiang Xiao57996352014-03-02 00:04:02 +08003877 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08003878 if (dapm != &card->dapm) {
3879 soc_dapm_shutdown_dapm(dapm);
3880 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3881 snd_soc_dapm_set_bias_level(dapm,
3882 SND_SOC_BIAS_OFF);
3883 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003884 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08003885
3886 soc_dapm_shutdown_dapm(&card->dapm);
3887 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3888 snd_soc_dapm_set_bias_level(&card->dapm,
3889 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01003890}
3891
Richard Purdie2b97eab2006-10-06 18:32:18 +02003892/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003893MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003894MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3895MODULE_LICENSE("GPL");